/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 11:09:07 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090602110907-utt933ql22lq8j7l
* plugins.d/mandos-client.c (main): Take down network interface on
                                    exit if it was brought up.

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);
906
900
  const char *interface = "eth0";
907
901
  struct ifreq network;
908
902
  int sd = -1;
909
 
  bool take_down_interface = false;
 
903
  bool interface_taken_up = false;
910
904
  uid_t uid;
911
905
  gid_t gid;
912
906
  char *connect_to = NULL;
1055
1049
    exitcode = EXIT_FAILURE;
1056
1050
    goto end;
1057
1051
  }
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
 
  }
 
1052
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1053
  if(ret == -1){
 
1054
    perror("sigaction");
 
1055
    exitcode = EXIT_FAILURE;
 
1056
    goto end;
 
1057
  }  
1076
1058
  
1077
1059
  /* If the interface is down, bring it up */
1078
1060
  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
1061
#ifdef __linux__
1091
1062
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1092
1063
       messages to mess up the prompt */
1129
1100
    }
1130
1101
    if((network.ifr_flags & IFF_UP) == 0){
1131
1102
      network.ifr_flags |= IFF_UP;
1132
 
      take_down_interface = true;
1133
1103
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1134
1104
      if(ret == -1){
1135
 
        take_down_interface = false;
1136
1105
        perror("ioctl SIOCSIFFLAGS");
1137
1106
        exitcode = EXIT_FAILURE;
1138
1107
#ifdef __linux__
1145
1114
#endif  /* __linux__ */
1146
1115
        goto end;
1147
1116
      }
 
1117
      interface_taken_up = true;
1148
1118
    }
1149
1119
    /* sleep checking until interface is running */
1150
1120
    for(int i=0; i < delay * 4; i++){
1160
1130
        perror("nanosleep");
1161
1131
      }
1162
1132
    }
1163
 
    if(not take_down_interface){
1164
 
      /* We won't need the socket anymore */
 
1133
    if(not interface_taken_up){
1165
1134
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1166
1135
      if(ret == -1){
1167
1136
        perror("close");
1178
1147
#endif  /* __linux__ */
1179
1148
  }
1180
1149
  
1181
 
  if(quit_now){
1182
 
    goto end;
1183
 
  }
1184
 
  
1185
1150
  uid = getuid();
1186
1151
  gid = getgid();
1187
1152
  
1196
1161
    perror("setuid");
1197
1162
  }
1198
1163
  
1199
 
  if(quit_now){
1200
 
    goto end;
1201
 
  }
1202
 
  
1203
1164
  ret = init_gnutls_global(pubkey, seckey);
1204
1165
  if(ret == -1){
1205
1166
    fprintf(stderr, "init_gnutls_global failed\n");
1209
1170
    gnutls_initialized = true;
1210
1171
  }
1211
1172
  
1212
 
  if(quit_now){
1213
 
    goto end;
1214
 
  }
1215
 
  
1216
 
  tempdir_created = true;
1217
1173
  if(mkdtemp(tempdir) == NULL){
1218
 
    tempdir_created = false;
1219
1174
    perror("mkdtemp");
1220
1175
    goto end;
1221
1176
  }
1222
 
  
1223
 
  if(quit_now){
1224
 
    goto end;
1225
 
  }
 
1177
  tempdir_created = true;
1226
1178
  
1227
1179
  if(not init_gpgme(pubkey, seckey, tempdir)){
1228
1180
    fprintf(stderr, "init_gpgme failed\n");
1232
1184
    gpgme_initialized = true;
1233
1185
  }
1234
1186
  
1235
 
  if(quit_now){
1236
 
    goto end;
 
1187
  if(interface[0] != '\0'){
 
1188
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1189
    if(if_index == 0){
 
1190
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1191
      exitcode = EXIT_FAILURE;
 
1192
      goto end;
 
1193
    }
1237
1194
  }
1238
1195
  
1239
1196
  if(connect_to != NULL){
1245
1202
      exitcode = EXIT_FAILURE;
1246
1203
      goto end;
1247
1204
    }
1248
 
    
1249
 
    if(quit_now){
1250
 
      goto end;
1251
 
    }
1252
 
    
1253
1205
    uint16_t port;
1254
1206
    errno = 0;
1255
1207
    tmpmax = strtoimax(address+1, &tmp, 10);
1259
1211
      exitcode = EXIT_FAILURE;
1260
1212
      goto end;
1261
1213
    }
1262
 
  
1263
 
    if(quit_now){
1264
 
      goto end;
1265
 
    }
1266
 
    
1267
1214
    port = (uint16_t)tmpmax;
1268
1215
    *address = '\0';
1269
1216
    address = connect_to;
1274
1221
    } else {
1275
1222
      af = AF_INET;
1276
1223
    }
1277
 
    
1278
 
    if(quit_now){
1279
 
      goto end;
1280
 
    }
1281
 
    
1282
1224
    ret = start_mandos_communication(address, port, if_index, af);
1283
1225
    if(ret < 0){
1284
1226
      exitcode = EXIT_FAILURE;
1287
1229
    }
1288
1230
    goto end;
1289
1231
  }
1290
 
  
1291
 
  if(quit_now){
1292
 
    goto end;
1293
 
  }
1294
 
  
 
1232
    
1295
1233
  {
1296
1234
    AvahiServerConfig config;
1297
1235
    /* Do not publish any local Zeroconf records */
1318
1256
    goto end;
1319
1257
  }
1320
1258
  
1321
 
  if(quit_now){
1322
 
    goto end;
1323
 
  }
1324
 
  
1325
1259
  /* Create the Avahi service browser */
1326
1260
  sb = avahi_s_service_browser_new(mc.server, if_index,
1327
1261
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1333
1267
    goto end;
1334
1268
  }
1335
1269
  
1336
 
  if(quit_now){
1337
 
    goto end;
1338
 
  }
1339
 
  
1340
1270
  /* Run the main loop */
1341
1271
  
1342
1272
  if(debug){
1372
1302
  }
1373
1303
  
1374
1304
  /* Take down the network interface */
1375
 
  if(take_down_interface){
 
1305
  if(interface_taken_up){
1376
1306
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1377
1307
    if(ret == -1){
1378
1308
      perror("ioctl SIOCGIFFLAGS");
1433
1363
    }
1434
1364
  }
1435
1365
  
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
1366
  return exitcode;
1445
1367
}