/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-05-24 23:36:15 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090524233615-ux63hgxddxl3c7l4
* debian/mandos-client.postinst (configure): Don't look for user and
                                             group with the old name
                                             if upgrading from a new
                                             enough version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
80
80
                                   argp_parse(), ARGP_KEY_ARG,
81
81
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
82
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
 
                                   sigaction(), SIGTERM, sig_atomic_t,
84
 
                                   raise() */
 
83
                                   sigaction(), SIGTERM, sigaction,
 
84
                                   sig_atomic_t */
85
85
 
86
86
#ifdef __linux__
87
87
#include <sys/klog.h>           /* klogctl() */
164
164
 */
165
165
static bool init_gpgme(const char *seckey,
166
166
                       const char *pubkey, const char *tempdir){
 
167
  int ret;
167
168
  gpgme_error_t rc;
168
169
  gpgme_engine_info_t engine_info;
169
170
  
172
173
   * Helper function to insert pub and seckey to the engine keyring.
173
174
   */
174
175
  bool import_key(const char *filename){
175
 
    int ret;
176
176
    int fd;
177
177
    gpgme_data_t pgp_data;
178
178
    
249
249
    return false;
250
250
  }
251
251
  
252
 
  return true;
 
252
  return true; 
253
253
}
254
254
 
255
255
/* 
309
309
        }
310
310
        gpgme_recipient_t recipient;
311
311
        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;
 
312
        if(recipient){
 
313
          while(recipient != NULL){
 
314
            fprintf(stderr, "Public key algorithm: %s\n",
 
315
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
316
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
317
            fprintf(stderr, "Secret key available: %s\n",
 
318
                    recipient->status == GPG_ERR_NO_SECKEY
 
319
                    ? "No" : "Yes");
 
320
            recipient = recipient->next;
 
321
          }
320
322
        }
321
323
      }
322
324
    }
514
516
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
515
517
                      __attribute__((unused)) const char *txt){}
516
518
 
517
 
sig_atomic_t quit_now = 0;
518
 
int signal_received = 0;
519
 
 
520
519
/* Called when a Mandos server is found */
521
520
static int start_mandos_communication(const char *ip, uint16_t port,
522
521
                                      AvahiIfIndex if_index,
523
522
                                      int af){
524
 
  int ret, tcp_sd = -1;
 
523
  int ret, tcp_sd;
525
524
  ssize_t sret;
526
525
  union {
527
526
    struct sockaddr_in in;
531
530
  char *decrypted_buffer;
532
531
  size_t buffer_length = 0;
533
532
  size_t buffer_capacity = 0;
 
533
  ssize_t decrypted_buffer_size;
534
534
  size_t written;
535
535
  int retval = 0;
536
536
  gnutls_session_t session;
537
537
  int pf;                       /* Protocol family */
538
538
  
539
 
  if(quit_now){
540
 
    return -1;
541
 
  }
542
 
  
543
539
  switch(af){
544
540
  case AF_INET6:
545
541
    pf = PF_INET6;
565
561
  tcp_sd = socket(pf, SOCK_STREAM, 0);
566
562
  if(tcp_sd < 0){
567
563
    perror("socket");
568
 
    retval = -1;
569
 
    goto mandos_end;
570
 
  }
571
 
  
572
 
  if(quit_now){
573
 
    goto mandos_end;
 
564
    return -1;
574
565
  }
575
566
  
576
567
  memset(&to, 0, sizeof(to));
583
574
  }
584
575
  if(ret < 0 ){
585
576
    perror("inet_pton");
586
 
    retval = -1;
587
 
    goto mandos_end;
 
577
    return -1;
588
578
  }
589
579
  if(ret == 0){
590
580
    fprintf(stderr, "Bad address: %s\n", ip);
591
 
    retval = -1;
592
 
    goto mandos_end;
 
581
    return -1;
593
582
  }
594
583
  if(af == AF_INET6){
595
584
    to.in6.sin6_port = htons(port); /* Spurious warnings from
602
591
      if(if_index == AVAHI_IF_UNSPEC){
603
592
        fprintf(stderr, "An IPv6 link-local address is incomplete"
604
593
                " without a network interface\n");
605
 
        retval = -1;
606
 
        goto mandos_end;
 
594
        return -1;
607
595
      }
608
596
      /* Set the network interface number as scope */
609
597
      to.in6.sin6_scope_id = (uint32_t)if_index;
614
602
                                     -Wunreachable-code */
615
603
  }
616
604
  
617
 
  if(quit_now){
618
 
    goto mandos_end;
619
 
  }
620
 
  
621
605
  if(debug){
622
606
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
623
607
      char interface[IF_NAMESIZE];
650
634
    }
651
635
  }
652
636
  
653
 
  if(quit_now){
654
 
    goto mandos_end;
655
 
  }
656
 
  
657
637
  if(af == AF_INET6){
658
638
    ret = connect(tcp_sd, &to.in6, sizeof(to));
659
639
  } else {
661
641
  }
662
642
  if(ret < 0){
663
643
    perror("connect");
664
 
    retval = -1;
665
 
    goto mandos_end;
666
 
  }
667
 
  
668
 
  if(quit_now){
669
 
    goto mandos_end;
 
644
    return -1;
670
645
  }
671
646
  
672
647
  const char *out = mandos_protocol_version;
691
666
        break;
692
667
      }
693
668
    }
694
 
  
695
 
    if(quit_now){
696
 
      goto mandos_end;
697
 
    }
698
669
  }
699
670
  
700
671
  if(debug){
701
672
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
702
673
  }
703
674
  
704
 
  if(quit_now){
705
 
    goto mandos_end;
706
 
  }
707
 
  
708
675
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
709
676
  
710
 
  if(quit_now){
711
 
    goto mandos_end;
712
 
  }
713
 
  
714
 
  do {
 
677
  do{
715
678
    ret = gnutls_handshake(session);
716
 
    if(quit_now){
717
 
      goto mandos_end;
718
 
    }
719
679
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
720
680
  
721
681
  if(ret != GNUTLS_E_SUCCESS){
735
695
  }
736
696
  
737
697
  while(true){
738
 
    
739
 
    if(quit_now){
740
 
      goto mandos_end;
741
 
    }
742
 
    
743
698
    buffer_capacity = incbuffer(&buffer, buffer_length,
744
699
                                   buffer_capacity);
745
700
    if(buffer_capacity == 0){
748
703
      goto mandos_end;
749
704
    }
750
705
    
751
 
    if(quit_now){
752
 
      goto mandos_end;
753
 
    }
754
 
    
755
706
    sret = gnutls_record_recv(session, buffer+buffer_length,
756
707
                              BUFFER_SIZE);
757
708
    if(sret == 0){
763
714
      case GNUTLS_E_AGAIN:
764
715
        break;
765
716
      case GNUTLS_E_REHANDSHAKE:
766
 
        do {
 
717
        do{
767
718
          ret = gnutls_handshake(session);
768
 
          
769
 
          if(quit_now){
770
 
            goto mandos_end;
771
 
          }
772
719
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
773
720
        if(ret < 0){
774
721
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
793
740
    fprintf(stderr, "Closing TLS session\n");
794
741
  }
795
742
  
796
 
  if(quit_now){
797
 
    goto mandos_end;
798
 
  }
799
 
  
800
743
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
801
744
  
802
 
  if(quit_now){
803
 
    goto mandos_end;
804
 
  }
805
 
  
806
745
  if(buffer_length > 0){
807
 
    ssize_t decrypted_buffer_size;
808
746
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
809
747
                                               buffer_length,
810
748
                                               &decrypted_buffer);
811
749
    if(decrypted_buffer_size >= 0){
812
 
      
813
750
      written = 0;
814
751
      while(written < (size_t) decrypted_buffer_size){
815
 
        if(quit_now){
816
 
          goto mandos_end;
817
 
        }
818
 
        
819
752
        ret = (int)fwrite(decrypted_buffer + written, 1,
820
753
                          (size_t)decrypted_buffer_size - written,
821
754
                          stdout);
841
774
  
842
775
 mandos_end:
843
776
  free(buffer);
844
 
  if(tcp_sd >= 0){
845
 
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
846
 
  }
 
777
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
847
778
  if(ret == -1){
848
779
    perror("close");
849
780
  }
850
781
  gnutls_deinit(session);
851
 
  if(quit_now){
852
 
    retval = -1;
853
 
  }
854
782
  return retval;
855
783
}
856
784
 
873
801
  /* Called whenever a service has been resolved successfully or
874
802
     timed out */
875
803
  
876
 
  if(quit_now){
877
 
    return;
878
 
  }
879
 
  
880
804
  switch(event){
881
805
  default:
882
806
  case AVAHI_RESOLVER_FAILURE:
919
843
  /* Called whenever a new services becomes available on the LAN or
920
844
     is removed from the LAN */
921
845
  
922
 
  if(quit_now){
923
 
    return;
924
 
  }
925
 
  
926
846
  switch(event){
927
847
  default:
928
848
  case AVAHI_BROWSER_FAILURE:
957
877
  }
958
878
}
959
879
 
 
880
sig_atomic_t quit_now = 0;
 
881
 
960
882
/* stop main loop after sigterm has been called */
961
 
static void handle_sigterm(int sig){
 
883
static void handle_sigterm(__attribute__((unused)) int sig){
962
884
  if(quit_now){
963
885
    return;
964
886
  }
965
887
  quit_now = 1;
966
 
  signal_received = sig;
967
888
  int old_errno = errno;
968
889
  if(mc.simple_poll != NULL){
969
890
    avahi_simple_poll_quit(mc.simple_poll);
980
901
  int exitcode = EXIT_SUCCESS;
981
902
  const char *interface = "eth0";
982
903
  struct ifreq network;
983
 
  int sd = -1;
984
 
  bool take_down_interface = false;
 
904
  int sd;
985
905
  uid_t uid;
986
906
  gid_t gid;
987
907
  char *connect_to = NULL;
1130
1050
    exitcode = EXIT_FAILURE;
1131
1051
    goto end;
1132
1052
  }
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
 
  }
 
1053
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1054
  if(ret == -1){
 
1055
    perror("sigaction");
 
1056
    exitcode = EXIT_FAILURE;
 
1057
    goto end;
 
1058
  }  
1176
1059
  
1177
1060
  /* If the interface is down, bring it up */
1178
1061
  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
 
    
1190
1062
#ifdef __linux__
1191
1063
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1192
1064
       messages to mess up the prompt */
1229
1101
    }
1230
1102
    if((network.ifr_flags & IFF_UP) == 0){
1231
1103
      network.ifr_flags |= IFF_UP;
1232
 
      take_down_interface = true;
1233
1104
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1234
1105
      if(ret == -1){
1235
 
        take_down_interface = false;
1236
1106
        perror("ioctl SIOCSIFFLAGS");
1237
1107
        exitcode = EXIT_FAILURE;
1238
1108
#ifdef __linux__
1260
1130
        perror("nanosleep");
1261
1131
      }
1262
1132
    }
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
 
      }
 
1133
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1134
    if(ret == -1){
 
1135
      perror("close");
1269
1136
    }
1270
1137
#ifdef __linux__
1271
1138
    if(restore_loglevel){
1278
1145
#endif  /* __linux__ */
1279
1146
  }
1280
1147
  
1281
 
  if(quit_now){
1282
 
    goto end;
1283
 
  }
1284
 
  
1285
1148
  uid = getuid();
1286
1149
  gid = getgid();
1287
1150
  
1296
1159
    perror("setuid");
1297
1160
  }
1298
1161
  
1299
 
  if(quit_now){
1300
 
    goto end;
1301
 
  }
1302
 
  
1303
1162
  ret = init_gnutls_global(pubkey, seckey);
1304
1163
  if(ret == -1){
1305
1164
    fprintf(stderr, "init_gnutls_global failed\n");
1309
1168
    gnutls_initialized = true;
1310
1169
  }
1311
1170
  
1312
 
  if(quit_now){
1313
 
    goto end;
1314
 
  }
1315
 
  
1316
 
  tempdir_created = true;
1317
1171
  if(mkdtemp(tempdir) == NULL){
1318
 
    tempdir_created = false;
1319
1172
    perror("mkdtemp");
1320
1173
    goto end;
1321
1174
  }
1322
 
  
1323
 
  if(quit_now){
1324
 
    goto end;
1325
 
  }
 
1175
  tempdir_created = true;
1326
1176
  
1327
1177
  if(not init_gpgme(pubkey, seckey, tempdir)){
1328
1178
    fprintf(stderr, "init_gpgme failed\n");
1332
1182
    gpgme_initialized = true;
1333
1183
  }
1334
1184
  
1335
 
  if(quit_now){
1336
 
    goto end;
 
1185
  if(interface[0] != '\0'){
 
1186
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1187
    if(if_index == 0){
 
1188
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1189
      exitcode = EXIT_FAILURE;
 
1190
      goto end;
 
1191
    }
1337
1192
  }
1338
1193
  
1339
1194
  if(connect_to != NULL){
1345
1200
      exitcode = EXIT_FAILURE;
1346
1201
      goto end;
1347
1202
    }
1348
 
    
1349
 
    if(quit_now){
1350
 
      goto end;
1351
 
    }
1352
 
    
1353
1203
    uint16_t port;
1354
1204
    errno = 0;
1355
1205
    tmpmax = strtoimax(address+1, &tmp, 10);
1359
1209
      exitcode = EXIT_FAILURE;
1360
1210
      goto end;
1361
1211
    }
1362
 
  
1363
 
    if(quit_now){
1364
 
      goto end;
1365
 
    }
1366
 
    
1367
1212
    port = (uint16_t)tmpmax;
1368
1213
    *address = '\0';
1369
1214
    address = connect_to;
1374
1219
    } else {
1375
1220
      af = AF_INET;
1376
1221
    }
1377
 
    
1378
 
    if(quit_now){
1379
 
      goto end;
1380
 
    }
1381
 
    
1382
1222
    ret = start_mandos_communication(address, port, if_index, af);
1383
1223
    if(ret < 0){
1384
1224
      exitcode = EXIT_FAILURE;
1387
1227
    }
1388
1228
    goto end;
1389
1229
  }
1390
 
  
1391
 
  if(quit_now){
1392
 
    goto end;
1393
 
  }
1394
 
  
 
1230
    
1395
1231
  {
1396
1232
    AvahiServerConfig config;
1397
1233
    /* Do not publish any local Zeroconf records */
1418
1254
    goto end;
1419
1255
  }
1420
1256
  
1421
 
  if(quit_now){
1422
 
    goto end;
1423
 
  }
1424
 
  
1425
1257
  /* Create the Avahi service browser */
1426
1258
  sb = avahi_s_service_browser_new(mc.server, if_index,
1427
1259
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1433
1265
    goto end;
1434
1266
  }
1435
1267
  
1436
 
  if(quit_now){
1437
 
    goto end;
1438
 
  }
1439
 
  
1440
1268
  /* Run the main loop */
1441
1269
  
1442
1270
  if(debug){
1471
1299
    gpgme_release(mc.ctx);
1472
1300
  }
1473
1301
  
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
 
  
1492
1302
  /* Removes the temp directory used by GPGME */
1493
1303
  if(tempdir_created){
1494
1304
    DIR *d;
1533
1343
    }
1534
1344
  }
1535
1345
  
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
 
  
1546
1346
  return exitcode;
1547
1347
}