/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

fixed incorrect include comments

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