/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-04-17 01:16:37 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090417011637-bcqjpm7fskm4370v
Code cleanup and one bug fix.

* mandos (Client.enable): Bug fix: Do not enable if already enabled.
  (MandosServer.__init__): Create empty set of clients if none passed.
  (main): Do not create clients set; do not pass a clients set to
          MandosServer.  Use tcp_server.clients throughout.

Show diffs side-by-side

added added

removed removed

Lines of Context:
80
80
                                   argp_parse(), ARGP_KEY_ARG,
81
81
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
82
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
 
                                   sigaction(), SIGTERM, sig_atomic_t,
84
 
                                   raise() */
 
83
                                   sigaction(), SIGTERM, sigaction,
 
84
                                   sig_atomic_t */
85
85
 
86
86
#ifdef __linux__
87
87
#include <sys/klog.h>           /* klogctl() */
249
249
    return false;
250
250
  }
251
251
  
252
 
  return true;
 
252
  return true; 
253
253
}
254
254
 
255
255
/* 
309
309
        }
310
310
        gpgme_recipient_t recipient;
311
311
        recipient = result->recipients;
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;
 
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
          }
320
322
        }
321
323
      }
322
324
    }
514
516
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
515
517
                      __attribute__((unused)) const char *txt){}
516
518
 
517
 
sig_atomic_t quit_now = 0;
518
 
int signal_received = 0;
519
 
 
520
519
/* Called when a Mandos server is found */
521
520
static int start_mandos_communication(const char *ip, uint16_t port,
522
521
                                      AvahiIfIndex if_index,
802
801
  /* Called whenever a service has been resolved successfully or
803
802
     timed out */
804
803
  
805
 
  if(quit_now){
806
 
    return;
807
 
  }
808
 
  
809
804
  switch(event){
810
805
  default:
811
806
  case AVAHI_RESOLVER_FAILURE:
882
877
  }
883
878
}
884
879
 
 
880
sig_atomic_t quit_now = 0;
 
881
 
885
882
/* stop main loop after sigterm has been called */
886
 
static void handle_sigterm(int sig){
 
883
static void handle_sigterm(__attribute__((unused)) int sig){
887
884
  if(quit_now){
888
885
    return;
889
886
  }
890
887
  quit_now = 1;
891
 
  signal_received = sig;
892
888
  int old_errno = errno;
893
889
  if(mc.simple_poll != NULL){
894
890
    avahi_simple_poll_quit(mc.simple_poll);
905
901
  int exitcode = EXIT_SUCCESS;
906
902
  const char *interface = "eth0";
907
903
  struct ifreq network;
908
 
  int sd = -1;
909
 
  bool take_down_interface = false;
 
904
  int sd;
910
905
  uid_t uid;
911
906
  gid_t gid;
912
907
  char *connect_to = NULL;
1055
1050
    exitcode = EXIT_FAILURE;
1056
1051
    goto end;
1057
1052
  }
1058
 
  ret = sigaction(SIGINT, &sigterm_action, &old_sigterm_action);
1059
 
  if(ret == -1){
1060
 
    perror("sigaction");
1061
 
    exitcode = EXIT_FAILURE;
1062
 
    goto end;
1063
 
  }
1064
 
  ret = sigaction(SIGHUP, &sigterm_action, NULL);
1065
 
  if(ret == -1){
1066
 
    perror("sigaction");
1067
 
    exitcode = EXIT_FAILURE;
1068
 
    goto end;
1069
 
  }
1070
 
  ret = sigaction(SIGTERM, &sigterm_action, NULL);
1071
 
  if(ret == -1){
1072
 
    perror("sigaction");
1073
 
    exitcode = EXIT_FAILURE;
1074
 
    goto end;
1075
 
  }
 
1053
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1054
  if(ret == -1){
 
1055
    perror("sigaction");
 
1056
    exitcode = EXIT_FAILURE;
 
1057
    goto end;
 
1058
  }  
1076
1059
  
1077
1060
  /* If the interface is down, bring it up */
1078
1061
  if(interface[0] != '\0'){
1079
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1080
 
    if(if_index == 0){
1081
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1082
 
      exitcode = EXIT_FAILURE;
1083
 
      goto end;
1084
 
    }
1085
 
    
1086
 
    if(quit_now){
1087
 
      goto end;
1088
 
    }
1089
 
    
1090
1062
#ifdef __linux__
1091
1063
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1092
1064
       messages to mess up the prompt */
1129
1101
    }
1130
1102
    if((network.ifr_flags & IFF_UP) == 0){
1131
1103
      network.ifr_flags |= IFF_UP;
1132
 
      take_down_interface = true;
1133
1104
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1134
1105
      if(ret == -1){
1135
 
        take_down_interface = false;
1136
1106
        perror("ioctl SIOCSIFFLAGS");
1137
1107
        exitcode = EXIT_FAILURE;
1138
1108
#ifdef __linux__
1160
1130
        perror("nanosleep");
1161
1131
      }
1162
1132
    }
1163
 
    if(not take_down_interface){
1164
 
      /* We won't need the socket anymore */
1165
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1166
 
      if(ret == -1){
1167
 
        perror("close");
1168
 
      }
 
1133
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1134
    if(ret == -1){
 
1135
      perror("close");
1169
1136
    }
1170
1137
#ifdef __linux__
1171
1138
    if(restore_loglevel){
1178
1145
#endif  /* __linux__ */
1179
1146
  }
1180
1147
  
1181
 
  if(quit_now){
1182
 
    goto end;
1183
 
  }
1184
 
  
1185
1148
  uid = getuid();
1186
1149
  gid = getgid();
1187
1150
  
1196
1159
    perror("setuid");
1197
1160
  }
1198
1161
  
1199
 
  if(quit_now){
1200
 
    goto end;
1201
 
  }
1202
 
  
1203
1162
  ret = init_gnutls_global(pubkey, seckey);
1204
1163
  if(ret == -1){
1205
1164
    fprintf(stderr, "init_gnutls_global failed\n");
1209
1168
    gnutls_initialized = true;
1210
1169
  }
1211
1170
  
1212
 
  if(quit_now){
1213
 
    goto end;
1214
 
  }
1215
 
  
1216
 
  tempdir_created = true;
1217
1171
  if(mkdtemp(tempdir) == NULL){
1218
 
    tempdir_created = false;
1219
1172
    perror("mkdtemp");
1220
1173
    goto end;
1221
1174
  }
1222
 
  
1223
 
  if(quit_now){
1224
 
    goto end;
1225
 
  }
 
1175
  tempdir_created = true;
1226
1176
  
1227
1177
  if(not init_gpgme(pubkey, seckey, tempdir)){
1228
1178
    fprintf(stderr, "init_gpgme failed\n");
1232
1182
    gpgme_initialized = true;
1233
1183
  }
1234
1184
  
1235
 
  if(quit_now){
1236
 
    goto end;
 
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
    }
1237
1192
  }
1238
1193
  
1239
1194
  if(connect_to != NULL){
1245
1200
      exitcode = EXIT_FAILURE;
1246
1201
      goto end;
1247
1202
    }
1248
 
    
1249
 
    if(quit_now){
1250
 
      goto end;
1251
 
    }
1252
 
    
1253
1203
    uint16_t port;
1254
1204
    errno = 0;
1255
1205
    tmpmax = strtoimax(address+1, &tmp, 10);
1259
1209
      exitcode = EXIT_FAILURE;
1260
1210
      goto end;
1261
1211
    }
1262
 
  
1263
 
    if(quit_now){
1264
 
      goto end;
1265
 
    }
1266
 
    
1267
1212
    port = (uint16_t)tmpmax;
1268
1213
    *address = '\0';
1269
1214
    address = connect_to;
1274
1219
    } else {
1275
1220
      af = AF_INET;
1276
1221
    }
1277
 
    
1278
 
    if(quit_now){
1279
 
      goto end;
1280
 
    }
1281
 
    
1282
1222
    ret = start_mandos_communication(address, port, if_index, af);
1283
1223
    if(ret < 0){
1284
1224
      exitcode = EXIT_FAILURE;
1287
1227
    }
1288
1228
    goto end;
1289
1229
  }
1290
 
  
1291
 
  if(quit_now){
1292
 
    goto end;
1293
 
  }
1294
 
  
 
1230
    
1295
1231
  {
1296
1232
    AvahiServerConfig config;
1297
1233
    /* Do not publish any local Zeroconf records */
1318
1254
    goto end;
1319
1255
  }
1320
1256
  
1321
 
  if(quit_now){
1322
 
    goto end;
1323
 
  }
1324
 
  
1325
1257
  /* Create the Avahi service browser */
1326
1258
  sb = avahi_s_service_browser_new(mc.server, if_index,
1327
1259
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1333
1265
    goto end;
1334
1266
  }
1335
1267
  
1336
 
  if(quit_now){
1337
 
    goto end;
1338
 
  }
1339
 
  
1340
1268
  /* Run the main loop */
1341
1269
  
1342
1270
  if(debug){
1371
1299
    gpgme_release(mc.ctx);
1372
1300
  }
1373
1301
  
1374
 
  /* Take down the network interface */
1375
 
  if(take_down_interface){
1376
 
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1377
 
    if(ret == -1){
1378
 
      perror("ioctl SIOCGIFFLAGS");
1379
 
    } else if(network.ifr_flags & IFF_UP) {
1380
 
      network.ifr_flags &= ~IFF_UP; /* clear flag */
1381
 
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1382
 
      if(ret == -1){
1383
 
        perror("ioctl SIOCSIFFLAGS");
1384
 
      }
1385
 
    }
1386
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1387
 
    if(ret == -1){
1388
 
      perror("close");
1389
 
    }
1390
 
  }
1391
 
  
1392
1302
  /* Removes the temp directory used by GPGME */
1393
1303
  if(tempdir_created){
1394
1304
    DIR *d;
1433
1343
    }
1434
1344
  }
1435
1345
  
1436
 
  if(quit_now){
1437
 
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
1438
 
    if(ret == -1){
1439
 
      perror("sigaction");
1440
 
    }
1441
 
    raise(signal_received);
1442
 
  }
1443
 
  
1444
1346
  return exitcode;
1445
1347
}