/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-02-12 23:17:14 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090212231714-59wxtc7h3w7ss8v6
* plugins.d/mandos-client.c (browse_callback, main): Do not require
                                                     IPv6.
  (main): Removed redundant initialization of "mc".

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