/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,
523
518
                                      int af){
524
 
  int ret, tcp_sd = -1;
 
519
  int ret, tcp_sd;
525
520
  ssize_t sret;
526
521
  union {
527
522
    struct sockaddr_in in;
537
532
  gnutls_session_t session;
538
533
  int pf;                       /* Protocol family */
539
534
  
540
 
  if(quit_now){
541
 
    return -1;
542
 
  }
543
 
  
544
535
  switch(af){
545
536
  case AF_INET6:
546
537
    pf = PF_INET6;
566
557
  tcp_sd = socket(pf, SOCK_STREAM, 0);
567
558
  if(tcp_sd < 0){
568
559
    perror("socket");
569
 
    retval = -1;
570
 
    goto mandos_end;
571
 
  }
572
 
  
573
 
  if(quit_now){
574
 
    goto mandos_end;
 
560
    return -1;
575
561
  }
576
562
  
577
563
  memset(&to, 0, sizeof(to));
578
564
  if(af == AF_INET6){
579
 
    to.in6.sin6_family = (sa_family_t)af;
 
565
    to.in6.sin6_family = (uint16_t)af;
580
566
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
581
567
  } else {                      /* IPv4 */
582
568
    to.in.sin_family = (sa_family_t)af;
584
570
  }
585
571
  if(ret < 0 ){
586
572
    perror("inet_pton");
587
 
    retval = -1;
588
 
    goto mandos_end;
 
573
    return -1;
589
574
  }
590
575
  if(ret == 0){
591
576
    fprintf(stderr, "Bad address: %s\n", ip);
592
 
    retval = -1;
593
 
    goto mandos_end;
 
577
    return -1;
594
578
  }
595
579
  if(af == AF_INET6){
596
580
    to.in6.sin6_port = htons(port); /* Spurious warnings from
603
587
      if(if_index == AVAHI_IF_UNSPEC){
604
588
        fprintf(stderr, "An IPv6 link-local address is incomplete"
605
589
                " without a network interface\n");
606
 
        retval = -1;
607
 
        goto mandos_end;
 
590
        return -1;
608
591
      }
609
592
      /* Set the network interface number as scope */
610
593
      to.in6.sin6_scope_id = (uint32_t)if_index;
615
598
                                     -Wunreachable-code */
616
599
  }
617
600
  
618
 
  if(quit_now){
619
 
    goto mandos_end;
620
 
  }
621
 
  
622
601
  if(debug){
623
602
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
624
603
      char interface[IF_NAMESIZE];
651
630
    }
652
631
  }
653
632
  
654
 
  if(quit_now){
655
 
    goto mandos_end;
656
 
  }
657
 
  
658
633
  if(af == AF_INET6){
659
634
    ret = connect(tcp_sd, &to.in6, sizeof(to));
660
635
  } else {
662
637
  }
663
638
  if(ret < 0){
664
639
    perror("connect");
665
 
    retval = -1;
666
 
    goto mandos_end;
667
 
  }
668
 
  
669
 
  if(quit_now){
670
 
    goto mandos_end;
 
640
    return -1;
671
641
  }
672
642
  
673
643
  const char *out = mandos_protocol_version;
692
662
        break;
693
663
      }
694
664
    }
695
 
  
696
 
    if(quit_now){
697
 
      goto mandos_end;
698
 
    }
699
665
  }
700
666
  
701
667
  if(debug){
702
668
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
703
669
  }
704
670
  
705
 
  if(quit_now){
706
 
    goto mandos_end;
707
 
  }
708
 
  
709
671
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
710
672
  
711
 
  if(quit_now){
712
 
    goto mandos_end;
713
 
  }
714
 
  
715
673
  do{
716
674
    ret = gnutls_handshake(session);
717
 
    if(quit_now){
718
 
      goto mandos_end;
719
 
    }
720
675
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
721
676
  
722
677
  if(ret != GNUTLS_E_SUCCESS){
736
691
  }
737
692
  
738
693
  while(true){
739
 
    
740
 
    if(quit_now){
741
 
      goto mandos_end;
742
 
    }
743
 
    
744
694
    buffer_capacity = incbuffer(&buffer, buffer_length,
745
695
                                   buffer_capacity);
746
696
    if(buffer_capacity == 0){
749
699
      goto mandos_end;
750
700
    }
751
701
    
752
 
    if(quit_now){
753
 
      goto mandos_end;
754
 
    }
755
 
    
756
702
    sret = gnutls_record_recv(session, buffer+buffer_length,
757
703
                              BUFFER_SIZE);
758
704
    if(sret == 0){
766
712
      case GNUTLS_E_REHANDSHAKE:
767
713
        do{
768
714
          ret = gnutls_handshake(session);
769
 
          
770
 
          if(quit_now){
771
 
            goto mandos_end;
772
 
          }
773
715
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
774
716
        if(ret < 0){
775
717
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
794
736
    fprintf(stderr, "Closing TLS session\n");
795
737
  }
796
738
  
797
 
  if(quit_now){
798
 
    goto mandos_end;
799
 
  }
800
 
  
801
739
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
802
740
  
803
 
  if(quit_now){
804
 
    goto mandos_end;
805
 
  }
806
 
  
807
741
  if(buffer_length > 0){
808
742
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
809
743
                                               buffer_length,
811
745
    if(decrypted_buffer_size >= 0){
812
746
      written = 0;
813
747
      while(written < (size_t) decrypted_buffer_size){
814
 
        if(quit_now){
815
 
          goto mandos_end;
816
 
        }
817
 
        
818
748
        ret = (int)fwrite(decrypted_buffer + written, 1,
819
749
                          (size_t)decrypted_buffer_size - written,
820
750
                          stdout);
840
770
  
841
771
 mandos_end:
842
772
  free(buffer);
843
 
  if(tcp_sd >= 0){
844
 
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
845
 
  }
 
773
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
846
774
  if(ret == -1){
847
775
    perror("close");
848
776
  }
849
777
  gnutls_deinit(session);
850
 
  if(quit_now){
851
 
    retval = -1;
852
 
  }
853
778
  return retval;
854
779
}
855
780
 
872
797
  /* Called whenever a service has been resolved successfully or
873
798
     timed out */
874
799
  
875
 
  if(quit_now){
876
 
    return;
877
 
  }
878
 
  
879
800
  switch(event){
880
801
  default:
881
802
  case AVAHI_RESOLVER_FAILURE:
918
839
  /* Called whenever a new services becomes available on the LAN or
919
840
     is removed from the LAN */
920
841
  
921
 
  if(quit_now){
922
 
    return;
923
 
  }
924
 
  
925
842
  switch(event){
926
843
  default:
927
844
  case AVAHI_BROWSER_FAILURE:
956
873
  }
957
874
}
958
875
 
 
876
sig_atomic_t quit_now = 0;
 
877
 
959
878
/* stop main loop after sigterm has been called */
960
 
static void handle_sigterm(int sig){
 
879
static void handle_sigterm(__attribute__((unused)) int sig){
961
880
  if(quit_now){
962
881
    return;
963
882
  }
964
883
  quit_now = 1;
965
 
  signal_received = sig;
966
884
  int old_errno = errno;
967
885
  if(mc.simple_poll != NULL){
968
886
    avahi_simple_poll_quit(mc.simple_poll);
979
897
  int exitcode = EXIT_SUCCESS;
980
898
  const char *interface = "eth0";
981
899
  struct ifreq network;
982
 
  int sd = -1;
983
 
  bool take_down_interface = false;
 
900
  int sd;
984
901
  uid_t uid;
985
902
  gid_t gid;
986
903
  char *connect_to = NULL;
1129
1046
    exitcode = EXIT_FAILURE;
1130
1047
    goto end;
1131
1048
  }
1132
 
  /* Need to check if the handler is SIG_IGN before handling:
1133
 
     | [[info:libc:Initial Signal Actions]] |
1134
 
     | [[info:libc:Basic Signal Handling]]  |
1135
 
  */
1136
 
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1137
 
  if(ret == -1){
1138
 
    perror("sigaction");
1139
 
    return EXIT_FAILURE;
1140
 
  }
1141
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1142
 
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1143
 
    if(ret == -1){
1144
 
      perror("sigaction");
1145
 
      exitcode = EXIT_FAILURE;
1146
 
      goto end;
1147
 
    }
1148
 
  }
1149
 
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1150
 
  if(ret == -1){
1151
 
    perror("sigaction");
1152
 
    return EXIT_FAILURE;
1153
 
  }
1154
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1155
 
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1156
 
    if(ret == -1){
1157
 
      perror("sigaction");
1158
 
      exitcode = EXIT_FAILURE;
1159
 
      goto end;
1160
 
    }
1161
 
  }
1162
 
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1163
 
  if(ret == -1){
1164
 
    perror("sigaction");
1165
 
    return EXIT_FAILURE;
1166
 
  }
1167
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1168
 
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1169
 
    if(ret == -1){
1170
 
      perror("sigaction");
1171
 
      exitcode = EXIT_FAILURE;
1172
 
      goto end;
1173
 
    }
1174
 
  }
 
1049
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1050
  if(ret == -1){
 
1051
    perror("sigaction");
 
1052
    exitcode = EXIT_FAILURE;
 
1053
    goto end;
 
1054
  }  
1175
1055
  
1176
1056
  /* If the interface is down, bring it up */
1177
1057
  if(interface[0] != '\0'){
1178
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1179
 
    if(if_index == 0){
1180
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1181
 
      exitcode = EXIT_FAILURE;
1182
 
      goto end;
1183
 
    }
1184
 
    
1185
 
    if(quit_now){
1186
 
      goto end;
1187
 
    }
1188
 
    
1189
1058
#ifdef __linux__
1190
1059
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1191
1060
       messages to mess up the prompt */
1228
1097
    }
1229
1098
    if((network.ifr_flags & IFF_UP) == 0){
1230
1099
      network.ifr_flags |= IFF_UP;
1231
 
      take_down_interface = true;
1232
1100
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1233
1101
      if(ret == -1){
1234
 
        take_down_interface = false;
1235
1102
        perror("ioctl SIOCSIFFLAGS");
1236
1103
        exitcode = EXIT_FAILURE;
1237
1104
#ifdef __linux__
1259
1126
        perror("nanosleep");
1260
1127
      }
1261
1128
    }
1262
 
    if(not take_down_interface){
1263
 
      /* We won't need the socket anymore */
1264
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1265
 
      if(ret == -1){
1266
 
        perror("close");
1267
 
      }
 
1129
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1130
    if(ret == -1){
 
1131
      perror("close");
1268
1132
    }
1269
1133
#ifdef __linux__
1270
1134
    if(restore_loglevel){
1277
1141
#endif  /* __linux__ */
1278
1142
  }
1279
1143
  
1280
 
  if(quit_now){
1281
 
    goto end;
1282
 
  }
1283
 
  
1284
1144
  uid = getuid();
1285
1145
  gid = getgid();
1286
1146
  
1295
1155
    perror("setuid");
1296
1156
  }
1297
1157
  
1298
 
  if(quit_now){
1299
 
    goto end;
1300
 
  }
1301
 
  
1302
1158
  ret = init_gnutls_global(pubkey, seckey);
1303
1159
  if(ret == -1){
1304
1160
    fprintf(stderr, "init_gnutls_global failed\n");
1308
1164
    gnutls_initialized = true;
1309
1165
  }
1310
1166
  
1311
 
  if(quit_now){
1312
 
    goto end;
1313
 
  }
1314
 
  
1315
 
  tempdir_created = true;
1316
1167
  if(mkdtemp(tempdir) == NULL){
1317
 
    tempdir_created = false;
1318
1168
    perror("mkdtemp");
1319
1169
    goto end;
1320
1170
  }
1321
 
  
1322
 
  if(quit_now){
1323
 
    goto end;
1324
 
  }
 
1171
  tempdir_created = true;
1325
1172
  
1326
1173
  if(not init_gpgme(pubkey, seckey, tempdir)){
1327
1174
    fprintf(stderr, "init_gpgme failed\n");
1331
1178
    gpgme_initialized = true;
1332
1179
  }
1333
1180
  
1334
 
  if(quit_now){
1335
 
    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
    }
1336
1188
  }
1337
1189
  
1338
1190
  if(connect_to != NULL){
1344
1196
      exitcode = EXIT_FAILURE;
1345
1197
      goto end;
1346
1198
    }
1347
 
    
1348
 
    if(quit_now){
1349
 
      goto end;
1350
 
    }
1351
 
    
1352
1199
    uint16_t port;
1353
1200
    errno = 0;
1354
1201
    tmpmax = strtoimax(address+1, &tmp, 10);
1358
1205
      exitcode = EXIT_FAILURE;
1359
1206
      goto end;
1360
1207
    }
1361
 
  
1362
 
    if(quit_now){
1363
 
      goto end;
1364
 
    }
1365
 
    
1366
1208
    port = (uint16_t)tmpmax;
1367
1209
    *address = '\0';
1368
1210
    address = connect_to;
1373
1215
    } else {
1374
1216
      af = AF_INET;
1375
1217
    }
1376
 
    
1377
 
    if(quit_now){
1378
 
      goto end;
1379
 
    }
1380
 
    
1381
1218
    ret = start_mandos_communication(address, port, if_index, af);
1382
1219
    if(ret < 0){
1383
1220
      exitcode = EXIT_FAILURE;
1386
1223
    }
1387
1224
    goto end;
1388
1225
  }
1389
 
  
1390
 
  if(quit_now){
1391
 
    goto end;
1392
 
  }
1393
 
  
 
1226
    
1394
1227
  {
1395
1228
    AvahiServerConfig config;
1396
1229
    /* Do not publish any local Zeroconf records */
1417
1250
    goto end;
1418
1251
  }
1419
1252
  
1420
 
  if(quit_now){
1421
 
    goto end;
1422
 
  }
1423
 
  
1424
1253
  /* Create the Avahi service browser */
1425
1254
  sb = avahi_s_service_browser_new(mc.server, if_index,
1426
1255
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1432
1261
    goto end;
1433
1262
  }
1434
1263
  
1435
 
  if(quit_now){
1436
 
    goto end;
1437
 
  }
1438
 
  
1439
1264
  /* Run the main loop */
1440
1265
  
1441
1266
  if(debug){
1470
1295
    gpgme_release(mc.ctx);
1471
1296
  }
1472
1297
  
1473
 
  /* Take down the network interface */
1474
 
  if(take_down_interface){
1475
 
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1476
 
    if(ret == -1){
1477
 
      perror("ioctl SIOCGIFFLAGS");
1478
 
    } else if(network.ifr_flags & IFF_UP) {
1479
 
      network.ifr_flags &= ~IFF_UP; /* clear flag */
1480
 
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1481
 
      if(ret == -1){
1482
 
        perror("ioctl SIOCSIFFLAGS");
1483
 
      }
1484
 
    }
1485
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1486
 
    if(ret == -1){
1487
 
      perror("close");
1488
 
    }
1489
 
  }
1490
 
  
1491
1298
  /* Removes the temp directory used by GPGME */
1492
1299
  if(tempdir_created){
1493
1300
    DIR *d;
1532
1339
    }
1533
1340
  }
1534
1341
  
1535
 
  if(quit_now){
1536
 
    sigemptyset(&old_sigterm_action.sa_mask);
1537
 
    old_sigterm_action.sa_handler = SIG_DFL;
1538
 
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
1539
 
    if(ret == -1){
1540
 
      perror("sigaction");
1541
 
    }
1542
 
    raise(signal_received);
1543
 
  }
1544
 
  
1545
1342
  return exitcode;
1546
1343
}