/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

merge

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