/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2009-08-27 23:12:09 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090827231209-ry9loqws0gr7ze4g
* plugins.d/mandos-client.c (quit_now): Move up declaration before
                                        first use.
  (resolve_callback, ): Check "quit_now" flag.
  (main): Renamed "interface_taken_up" to "take_down_interface" for
          clarity.  Check existence of interface before taking it up.
          Set "take_down_interface" and "tempdir_created" flags before
          any action is taken, to make sure cleanup is never missed.
          Check "quit_now" flag throughout.

Show diffs side-by-side

added added

removed removed

Lines of Context:
309
309
        }
310
310
        gpgme_recipient_t recipient;
311
311
        recipient = result->recipients;
312
 
        if(recipient){
313
 
          while(recipient != NULL){
314
 
            fprintf(stderr, "Public key algorithm: %s\n",
315
 
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
316
 
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
317
 
            fprintf(stderr, "Secret key available: %s\n",
318
 
                    recipient->status == GPG_ERR_NO_SECKEY
319
 
                    ? "No" : "Yes");
320
 
            recipient = recipient->next;
321
 
          }
 
312
        while(recipient != NULL){
 
313
          fprintf(stderr, "Public key algorithm: %s\n",
 
314
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
315
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
316
          fprintf(stderr, "Secret key available: %s\n",
 
317
                  recipient->status == GPG_ERR_NO_SECKEY
 
318
                  ? "No" : "Yes");
 
319
          recipient = recipient->next;
322
320
        }
323
321
      }
324
322
    }
782
780
  return retval;
783
781
}
784
782
 
 
783
sig_atomic_t quit_now = 0;
 
784
 
785
785
static void resolve_callback(AvahiSServiceResolver *r,
786
786
                             AvahiIfIndex interface,
787
787
                             AvahiProtocol proto,
801
801
  /* Called whenever a service has been resolved successfully or
802
802
     timed out */
803
803
  
 
804
  if(quit_now){
 
805
    return;
 
806
  }
 
807
  
804
808
  switch(event){
805
809
  default:
806
810
  case AVAHI_RESOLVER_FAILURE:
877
881
  }
878
882
}
879
883
 
880
 
sig_atomic_t quit_now = 0;
881
 
 
882
884
/* stop main loop after sigterm has been called */
883
885
static void handle_sigterm(__attribute__((unused)) int sig){
884
886
  if(quit_now){
901
903
  int exitcode = EXIT_SUCCESS;
902
904
  const char *interface = "eth0";
903
905
  struct ifreq network;
904
 
  int sd;
 
906
  int sd = -1;
 
907
  bool take_down_interface = false;
905
908
  uid_t uid;
906
909
  gid_t gid;
907
910
  char *connect_to = NULL;
1059
1062
  
1060
1063
  /* If the interface is down, bring it up */
1061
1064
  if(interface[0] != '\0'){
 
1065
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1066
    if(if_index == 0){
 
1067
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1068
      exitcode = EXIT_FAILURE;
 
1069
      goto end;
 
1070
    }
 
1071
    
 
1072
    if(quit_now){
 
1073
      goto end;
 
1074
    }
 
1075
    
1062
1076
#ifdef __linux__
1063
1077
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1064
1078
       messages to mess up the prompt */
1101
1115
    }
1102
1116
    if((network.ifr_flags & IFF_UP) == 0){
1103
1117
      network.ifr_flags |= IFF_UP;
 
1118
      take_down_interface = true;
1104
1119
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1105
1120
      if(ret == -1){
 
1121
        take_down_interface = false;
1106
1122
        perror("ioctl SIOCSIFFLAGS");
1107
1123
        exitcode = EXIT_FAILURE;
1108
1124
#ifdef __linux__
1130
1146
        perror("nanosleep");
1131
1147
      }
1132
1148
    }
1133
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1134
 
    if(ret == -1){
1135
 
      perror("close");
 
1149
    if(not take_down_interface){
 
1150
      /* We won't need the socket anymore */
 
1151
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1152
      if(ret == -1){
 
1153
        perror("close");
 
1154
      }
1136
1155
    }
1137
1156
#ifdef __linux__
1138
1157
    if(restore_loglevel){
1145
1164
#endif  /* __linux__ */
1146
1165
  }
1147
1166
  
 
1167
  if(quit_now){
 
1168
    goto end;
 
1169
  }
 
1170
  
1148
1171
  uid = getuid();
1149
1172
  gid = getgid();
1150
1173
  
1159
1182
    perror("setuid");
1160
1183
  }
1161
1184
  
 
1185
  if(quit_now){
 
1186
    goto end;
 
1187
  }
 
1188
  
1162
1189
  ret = init_gnutls_global(pubkey, seckey);
1163
1190
  if(ret == -1){
1164
1191
    fprintf(stderr, "init_gnutls_global failed\n");
1168
1195
    gnutls_initialized = true;
1169
1196
  }
1170
1197
  
 
1198
  if(quit_now){
 
1199
    goto end;
 
1200
  }
 
1201
  
 
1202
  tempdir_created = true;
1171
1203
  if(mkdtemp(tempdir) == NULL){
 
1204
    tempdir_created = false;
1172
1205
    perror("mkdtemp");
1173
1206
    goto end;
1174
1207
  }
1175
 
  tempdir_created = true;
 
1208
  
 
1209
  if(quit_now){
 
1210
    goto end;
 
1211
  }
1176
1212
  
1177
1213
  if(not init_gpgme(pubkey, seckey, tempdir)){
1178
1214
    fprintf(stderr, "init_gpgme failed\n");
1182
1218
    gpgme_initialized = true;
1183
1219
  }
1184
1220
  
1185
 
  if(interface[0] != '\0'){
1186
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1187
 
    if(if_index == 0){
1188
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1189
 
      exitcode = EXIT_FAILURE;
1190
 
      goto end;
1191
 
    }
 
1221
  if(quit_now){
 
1222
    goto end;
1192
1223
  }
1193
1224
  
1194
1225
  if(connect_to != NULL){
1200
1231
      exitcode = EXIT_FAILURE;
1201
1232
      goto end;
1202
1233
    }
 
1234
    
 
1235
    if(quit_now){
 
1236
      goto end;
 
1237
    }
 
1238
    
1203
1239
    uint16_t port;
1204
1240
    errno = 0;
1205
1241
    tmpmax = strtoimax(address+1, &tmp, 10);
1209
1245
      exitcode = EXIT_FAILURE;
1210
1246
      goto end;
1211
1247
    }
 
1248
  
 
1249
    if(quit_now){
 
1250
      goto end;
 
1251
    }
 
1252
    
1212
1253
    port = (uint16_t)tmpmax;
1213
1254
    *address = '\0';
1214
1255
    address = connect_to;
1219
1260
    } else {
1220
1261
      af = AF_INET;
1221
1262
    }
 
1263
    
 
1264
    if(quit_now){
 
1265
      goto end;
 
1266
    }
 
1267
    
1222
1268
    ret = start_mandos_communication(address, port, if_index, af);
1223
1269
    if(ret < 0){
1224
1270
      exitcode = EXIT_FAILURE;
1227
1273
    }
1228
1274
    goto end;
1229
1275
  }
1230
 
    
 
1276
  
 
1277
  if(quit_now){
 
1278
    goto end;
 
1279
  }
 
1280
  
1231
1281
  {
1232
1282
    AvahiServerConfig config;
1233
1283
    /* Do not publish any local Zeroconf records */
1254
1304
    goto end;
1255
1305
  }
1256
1306
  
 
1307
  if(quit_now){
 
1308
    goto end;
 
1309
  }
 
1310
  
1257
1311
  /* Create the Avahi service browser */
1258
1312
  sb = avahi_s_service_browser_new(mc.server, if_index,
1259
1313
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1265
1319
    goto end;
1266
1320
  }
1267
1321
  
 
1322
  if(quit_now){
 
1323
    goto end;
 
1324
  }
 
1325
  
1268
1326
  /* Run the main loop */
1269
1327
  
1270
1328
  if(debug){
1299
1357
    gpgme_release(mc.ctx);
1300
1358
  }
1301
1359
  
 
1360
  /* Take down the network interface */
 
1361
  if(take_down_interface){
 
1362
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1363
    if(ret == -1){
 
1364
      perror("ioctl SIOCGIFFLAGS");
 
1365
    } else if(network.ifr_flags & IFF_UP) {
 
1366
      network.ifr_flags &= ~IFF_UP; /* clear flag */
 
1367
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1368
      if(ret == -1){
 
1369
        perror("ioctl SIOCSIFFLAGS");
 
1370
      }
 
1371
    }
 
1372
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1373
    if(ret == -1){
 
1374
      perror("close");
 
1375
    }
 
1376
  }
 
1377
  
1302
1378
  /* Removes the temp directory used by GPGME */
1303
1379
  if(tempdir_created){
1304
1380
    DIR *d;