/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-30 03:10:29 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090830031029-cj8po3rc4g1cux1d
* mandos: White-space fixes only.
* mandos-ctl: - '' -
* plugin-runner.c: - '' -
* plugins.d/askpass-fifo.c: - '' -
* plugins.d/mandos-client.c: - '' -
* plugins.d/password-prompt.c: - '' -

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