/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-06-02 10:33:04 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090602103304-k6eokrft7wr00255
* plugins.d/mandos-client.c (pgp_packet_decrypt): Remove redundant
                                                 "if" statement.

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
/* 
514
514
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
515
515
                      __attribute__((unused)) const char *txt){}
516
516
 
517
 
sig_atomic_t quit_now = 0;
518
 
int signal_received = 0;
519
 
 
520
517
/* Called when a Mandos server is found */
521
518
static int start_mandos_communication(const char *ip, uint16_t port,
522
519
                                      AvahiIfIndex if_index,
802
799
  /* Called whenever a service has been resolved successfully or
803
800
     timed out */
804
801
  
805
 
  if(quit_now){
806
 
    return;
807
 
  }
808
 
  
809
802
  switch(event){
810
803
  default:
811
804
  case AVAHI_RESOLVER_FAILURE:
882
875
  }
883
876
}
884
877
 
 
878
sig_atomic_t quit_now = 0;
 
879
 
885
880
/* stop main loop after sigterm has been called */
886
 
static void handle_sigterm(int sig){
 
881
static void handle_sigterm(__attribute__((unused)) int sig){
887
882
  if(quit_now){
888
883
    return;
889
884
  }
890
885
  quit_now = 1;
891
 
  signal_received = sig;
892
886
  int old_errno = errno;
893
887
  if(mc.simple_poll != NULL){
894
888
    avahi_simple_poll_quit(mc.simple_poll);
905
899
  int exitcode = EXIT_SUCCESS;
906
900
  const char *interface = "eth0";
907
901
  struct ifreq network;
908
 
  int sd = -1;
909
 
  bool take_down_interface = false;
 
902
  int sd;
910
903
  uid_t uid;
911
904
  gid_t gid;
912
905
  char *connect_to = NULL;
1055
1048
    exitcode = EXIT_FAILURE;
1056
1049
    goto end;
1057
1050
  }
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
 
  }
 
1051
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1052
  if(ret == -1){
 
1053
    perror("sigaction");
 
1054
    exitcode = EXIT_FAILURE;
 
1055
    goto end;
 
1056
  }  
1076
1057
  
1077
1058
  /* If the interface is down, bring it up */
1078
1059
  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
1060
#ifdef __linux__
1091
1061
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1092
1062
       messages to mess up the prompt */
1129
1099
    }
1130
1100
    if((network.ifr_flags & IFF_UP) == 0){
1131
1101
      network.ifr_flags |= IFF_UP;
1132
 
      take_down_interface = true;
1133
1102
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1134
1103
      if(ret == -1){
1135
 
        take_down_interface = false;
1136
1104
        perror("ioctl SIOCSIFFLAGS");
1137
1105
        exitcode = EXIT_FAILURE;
1138
1106
#ifdef __linux__
1160
1128
        perror("nanosleep");
1161
1129
      }
1162
1130
    }
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
 
      }
 
1131
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1132
    if(ret == -1){
 
1133
      perror("close");
1169
1134
    }
1170
1135
#ifdef __linux__
1171
1136
    if(restore_loglevel){
1178
1143
#endif  /* __linux__ */
1179
1144
  }
1180
1145
  
1181
 
  if(quit_now){
1182
 
    goto end;
1183
 
  }
1184
 
  
1185
1146
  uid = getuid();
1186
1147
  gid = getgid();
1187
1148
  
1196
1157
    perror("setuid");
1197
1158
  }
1198
1159
  
1199
 
  if(quit_now){
1200
 
    goto end;
1201
 
  }
1202
 
  
1203
1160
  ret = init_gnutls_global(pubkey, seckey);
1204
1161
  if(ret == -1){
1205
1162
    fprintf(stderr, "init_gnutls_global failed\n");
1209
1166
    gnutls_initialized = true;
1210
1167
  }
1211
1168
  
1212
 
  if(quit_now){
1213
 
    goto end;
1214
 
  }
1215
 
  
1216
 
  tempdir_created = true;
1217
1169
  if(mkdtemp(tempdir) == NULL){
1218
 
    tempdir_created = false;
1219
1170
    perror("mkdtemp");
1220
1171
    goto end;
1221
1172
  }
1222
 
  
1223
 
  if(quit_now){
1224
 
    goto end;
1225
 
  }
 
1173
  tempdir_created = true;
1226
1174
  
1227
1175
  if(not init_gpgme(pubkey, seckey, tempdir)){
1228
1176
    fprintf(stderr, "init_gpgme failed\n");
1232
1180
    gpgme_initialized = true;
1233
1181
  }
1234
1182
  
1235
 
  if(quit_now){
1236
 
    goto end;
 
1183
  if(interface[0] != '\0'){
 
1184
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1185
    if(if_index == 0){
 
1186
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1187
      exitcode = EXIT_FAILURE;
 
1188
      goto end;
 
1189
    }
1237
1190
  }
1238
1191
  
1239
1192
  if(connect_to != NULL){
1245
1198
      exitcode = EXIT_FAILURE;
1246
1199
      goto end;
1247
1200
    }
1248
 
    
1249
 
    if(quit_now){
1250
 
      goto end;
1251
 
    }
1252
 
    
1253
1201
    uint16_t port;
1254
1202
    errno = 0;
1255
1203
    tmpmax = strtoimax(address+1, &tmp, 10);
1259
1207
      exitcode = EXIT_FAILURE;
1260
1208
      goto end;
1261
1209
    }
1262
 
  
1263
 
    if(quit_now){
1264
 
      goto end;
1265
 
    }
1266
 
    
1267
1210
    port = (uint16_t)tmpmax;
1268
1211
    *address = '\0';
1269
1212
    address = connect_to;
1274
1217
    } else {
1275
1218
      af = AF_INET;
1276
1219
    }
1277
 
    
1278
 
    if(quit_now){
1279
 
      goto end;
1280
 
    }
1281
 
    
1282
1220
    ret = start_mandos_communication(address, port, if_index, af);
1283
1221
    if(ret < 0){
1284
1222
      exitcode = EXIT_FAILURE;
1287
1225
    }
1288
1226
    goto end;
1289
1227
  }
1290
 
  
1291
 
  if(quit_now){
1292
 
    goto end;
1293
 
  }
1294
 
  
 
1228
    
1295
1229
  {
1296
1230
    AvahiServerConfig config;
1297
1231
    /* Do not publish any local Zeroconf records */
1318
1252
    goto end;
1319
1253
  }
1320
1254
  
1321
 
  if(quit_now){
1322
 
    goto end;
1323
 
  }
1324
 
  
1325
1255
  /* Create the Avahi service browser */
1326
1256
  sb = avahi_s_service_browser_new(mc.server, if_index,
1327
1257
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1333
1263
    goto end;
1334
1264
  }
1335
1265
  
1336
 
  if(quit_now){
1337
 
    goto end;
1338
 
  }
1339
 
  
1340
1266
  /* Run the main loop */
1341
1267
  
1342
1268
  if(debug){
1371
1297
    gpgme_release(mc.ctx);
1372
1298
  }
1373
1299
  
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
1300
  /* Removes the temp directory used by GPGME */
1393
1301
  if(tempdir_created){
1394
1302
    DIR *d;
1433
1341
    }
1434
1342
  }
1435
1343
  
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
1344
  return exitcode;
1445
1345
}