/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-13 05:38:21 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090213053821-03e696gckk4nbjps
Support not using IPv6 in server:

* mandos (AvahiService.__init__): Take new "protocol" parameter.  All
                                  callers changed.
  (IPv6_TCPServer.__init__): Take new "use_ipv6" parameter.  All
                             callers changed.
  (IPv6_TCPServer.server_bind): Create IPv4 socket if not using IPv6.
  (main): New "--no-ipv6" command line option.  New "use_ipv6" config
          option.
* mandos-options.xml ([@id="address"]): Document conditional IPv4
                                        address support.
  ([@id="ipv6"]): New paragraph.
* mandos.conf (use_ipv6): New config option.
* mandos.conf.xml (OPTIONS): Document new "use_dbus" option.
  (EXAMPLE): Changed to use IPv6 link-local address.  Added "use_ipv6"
             option.
* mandos.xml (SYNOPSIS): New "--no-ipv6" option.
  (OPTIONS): Document new "--no-ipv6" option.

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