/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:
30
30
 */
31
31
 
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
 
33
#ifndef _LARGEFILE_SOURCE
33
34
#define _LARGEFILE_SOURCE
 
35
#endif
 
36
#ifndef _FILE_OFFSET_BITS
34
37
#define _FILE_OFFSET_BITS 64
 
38
#endif
35
39
 
36
40
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
37
41
 
305
309
        }
306
310
        gpgme_recipient_t recipient;
307
311
        recipient = result->recipients;
308
 
        if(recipient){
309
 
          while(recipient != NULL){
310
 
            fprintf(stderr, "Public key algorithm: %s\n",
311
 
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
312
 
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
313
 
            fprintf(stderr, "Secret key available: %s\n",
314
 
                    recipient->status == GPG_ERR_NO_SECKEY
315
 
                    ? "No" : "Yes");
316
 
            recipient = recipient->next;
317
 
          }
 
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;
318
320
        }
319
321
      }
320
322
    }
562
564
  
563
565
  memset(&to, 0, sizeof(to));
564
566
  if(af == AF_INET6){
565
 
    to.in6.sin6_family = (uint16_t)af;
 
567
    to.in6.sin6_family = (sa_family_t)af;
566
568
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
567
569
  } else {                      /* IPv4 */
568
570
    to.in.sin_family = (sa_family_t)af;
778
780
  return retval;
779
781
}
780
782
 
 
783
sig_atomic_t quit_now = 0;
 
784
 
781
785
static void resolve_callback(AvahiSServiceResolver *r,
782
786
                             AvahiIfIndex interface,
783
787
                             AvahiProtocol proto,
797
801
  /* Called whenever a service has been resolved successfully or
798
802
     timed out */
799
803
  
 
804
  if(quit_now){
 
805
    return;
 
806
  }
 
807
  
800
808
  switch(event){
801
809
  default:
802
810
  case AVAHI_RESOLVER_FAILURE:
873
881
  }
874
882
}
875
883
 
876
 
sig_atomic_t quit_now = 0;
877
 
 
878
884
/* stop main loop after sigterm has been called */
879
885
static void handle_sigterm(__attribute__((unused)) int sig){
880
886
  if(quit_now){
897
903
  int exitcode = EXIT_SUCCESS;
898
904
  const char *interface = "eth0";
899
905
  struct ifreq network;
900
 
  int sd;
 
906
  int sd = -1;
 
907
  bool take_down_interface = false;
901
908
  uid_t uid;
902
909
  gid_t gid;
903
910
  char *connect_to = NULL;
1055
1062
  
1056
1063
  /* If the interface is down, bring it up */
1057
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
    
1058
1076
#ifdef __linux__
1059
1077
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1060
1078
       messages to mess up the prompt */
1097
1115
    }
1098
1116
    if((network.ifr_flags & IFF_UP) == 0){
1099
1117
      network.ifr_flags |= IFF_UP;
 
1118
      take_down_interface = true;
1100
1119
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1101
1120
      if(ret == -1){
 
1121
        take_down_interface = false;
1102
1122
        perror("ioctl SIOCSIFFLAGS");
1103
1123
        exitcode = EXIT_FAILURE;
1104
1124
#ifdef __linux__
1126
1146
        perror("nanosleep");
1127
1147
      }
1128
1148
    }
1129
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1130
 
    if(ret == -1){
1131
 
      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
      }
1132
1155
    }
1133
1156
#ifdef __linux__
1134
1157
    if(restore_loglevel){
1141
1164
#endif  /* __linux__ */
1142
1165
  }
1143
1166
  
 
1167
  if(quit_now){
 
1168
    goto end;
 
1169
  }
 
1170
  
1144
1171
  uid = getuid();
1145
1172
  gid = getgid();
1146
1173
  
1155
1182
    perror("setuid");
1156
1183
  }
1157
1184
  
 
1185
  if(quit_now){
 
1186
    goto end;
 
1187
  }
 
1188
  
1158
1189
  ret = init_gnutls_global(pubkey, seckey);
1159
1190
  if(ret == -1){
1160
1191
    fprintf(stderr, "init_gnutls_global failed\n");
1164
1195
    gnutls_initialized = true;
1165
1196
  }
1166
1197
  
 
1198
  if(quit_now){
 
1199
    goto end;
 
1200
  }
 
1201
  
 
1202
  tempdir_created = true;
1167
1203
  if(mkdtemp(tempdir) == NULL){
 
1204
    tempdir_created = false;
1168
1205
    perror("mkdtemp");
1169
1206
    goto end;
1170
1207
  }
1171
 
  tempdir_created = true;
 
1208
  
 
1209
  if(quit_now){
 
1210
    goto end;
 
1211
  }
1172
1212
  
1173
1213
  if(not init_gpgme(pubkey, seckey, tempdir)){
1174
1214
    fprintf(stderr, "init_gpgme failed\n");
1178
1218
    gpgme_initialized = true;
1179
1219
  }
1180
1220
  
1181
 
  if(interface[0] != '\0'){
1182
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1183
 
    if(if_index == 0){
1184
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1185
 
      exitcode = EXIT_FAILURE;
1186
 
      goto end;
1187
 
    }
 
1221
  if(quit_now){
 
1222
    goto end;
1188
1223
  }
1189
1224
  
1190
1225
  if(connect_to != NULL){
1196
1231
      exitcode = EXIT_FAILURE;
1197
1232
      goto end;
1198
1233
    }
 
1234
    
 
1235
    if(quit_now){
 
1236
      goto end;
 
1237
    }
 
1238
    
1199
1239
    uint16_t port;
1200
1240
    errno = 0;
1201
1241
    tmpmax = strtoimax(address+1, &tmp, 10);
1205
1245
      exitcode = EXIT_FAILURE;
1206
1246
      goto end;
1207
1247
    }
 
1248
  
 
1249
    if(quit_now){
 
1250
      goto end;
 
1251
    }
 
1252
    
1208
1253
    port = (uint16_t)tmpmax;
1209
1254
    *address = '\0';
1210
1255
    address = connect_to;
1215
1260
    } else {
1216
1261
      af = AF_INET;
1217
1262
    }
 
1263
    
 
1264
    if(quit_now){
 
1265
      goto end;
 
1266
    }
 
1267
    
1218
1268
    ret = start_mandos_communication(address, port, if_index, af);
1219
1269
    if(ret < 0){
1220
1270
      exitcode = EXIT_FAILURE;
1223
1273
    }
1224
1274
    goto end;
1225
1275
  }
1226
 
    
 
1276
  
 
1277
  if(quit_now){
 
1278
    goto end;
 
1279
  }
 
1280
  
1227
1281
  {
1228
1282
    AvahiServerConfig config;
1229
1283
    /* Do not publish any local Zeroconf records */
1250
1304
    goto end;
1251
1305
  }
1252
1306
  
 
1307
  if(quit_now){
 
1308
    goto end;
 
1309
  }
 
1310
  
1253
1311
  /* Create the Avahi service browser */
1254
1312
  sb = avahi_s_service_browser_new(mc.server, if_index,
1255
1313
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1261
1319
    goto end;
1262
1320
  }
1263
1321
  
 
1322
  if(quit_now){
 
1323
    goto end;
 
1324
  }
 
1325
  
1264
1326
  /* Run the main loop */
1265
1327
  
1266
1328
  if(debug){
1295
1357
    gpgme_release(mc.ctx);
1296
1358
  }
1297
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
  
1298
1378
  /* Removes the temp directory used by GPGME */
1299
1379
  if(tempdir_created){
1300
1380
    DIR *d;