/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 10:18:17 UTC
  • mfrom: (237.2.14 mandos-release)
  • Revision ID: teddy@fukt.bsnet.se-20090213101817-3ld1fzg058g9zkfv
Merge from release branch.

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
 
71
67
                                   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
72
68
                                */
73
69
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
 
                                   getuid(), getgid(), seteuid(),
 
70
                                   getuid(), getgid(), setuid(),
75
71
                                   setgid() */
76
72
#include <arpa/inet.h>          /* inet_pton(), htons */
77
73
#include <iso646.h>             /* not, or, and */
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() */
164
160
 */
165
161
static bool init_gpgme(const char *seckey,
166
162
                       const char *pubkey, const char *tempdir){
 
163
  int ret;
167
164
  gpgme_error_t rc;
168
165
  gpgme_engine_info_t engine_info;
169
166
  
172
169
   * Helper function to insert pub and seckey to the engine keyring.
173
170
   */
174
171
  bool import_key(const char *filename){
175
 
    int ret;
176
172
    int fd;
177
173
    gpgme_data_t pgp_data;
178
174
    
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;
531
526
  char *decrypted_buffer;
532
527
  size_t buffer_length = 0;
533
528
  size_t buffer_capacity = 0;
 
529
  ssize_t decrypted_buffer_size;
534
530
  size_t written;
535
531
  int retval = 0;
536
532
  gnutls_session_t session;
537
533
  int pf;                       /* Protocol family */
538
534
  
539
 
  if(quit_now){
540
 
    return -1;
541
 
  }
542
 
  
543
535
  switch(af){
544
536
  case AF_INET6:
545
537
    pf = PF_INET6;
565
557
  tcp_sd = socket(pf, SOCK_STREAM, 0);
566
558
  if(tcp_sd < 0){
567
559
    perror("socket");
568
 
    retval = -1;
569
 
    goto mandos_end;
570
 
  }
571
 
  
572
 
  if(quit_now){
573
 
    goto mandos_end;
 
560
    return -1;
574
561
  }
575
562
  
576
563
  memset(&to, 0, sizeof(to));
577
564
  if(af == AF_INET6){
578
 
    to.in6.sin6_family = (sa_family_t)af;
 
565
    to.in6.sin6_family = (uint16_t)af;
579
566
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
580
567
  } else {                      /* IPv4 */
581
568
    to.in.sin_family = (sa_family_t)af;
583
570
  }
584
571
  if(ret < 0 ){
585
572
    perror("inet_pton");
586
 
    retval = -1;
587
 
    goto mandos_end;
 
573
    return -1;
588
574
  }
589
575
  if(ret == 0){
590
576
    fprintf(stderr, "Bad address: %s\n", ip);
591
 
    retval = -1;
592
 
    goto mandos_end;
 
577
    return -1;
593
578
  }
594
579
  if(af == AF_INET6){
595
580
    to.in6.sin6_port = htons(port); /* Spurious warnings from
602
587
      if(if_index == AVAHI_IF_UNSPEC){
603
588
        fprintf(stderr, "An IPv6 link-local address is incomplete"
604
589
                " without a network interface\n");
605
 
        retval = -1;
606
 
        goto mandos_end;
 
590
        return -1;
607
591
      }
608
592
      /* Set the network interface number as scope */
609
593
      to.in6.sin6_scope_id = (uint32_t)if_index;
614
598
                                     -Wunreachable-code */
615
599
  }
616
600
  
617
 
  if(quit_now){
618
 
    goto mandos_end;
619
 
  }
620
 
  
621
601
  if(debug){
622
602
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
623
603
      char interface[IF_NAMESIZE];
650
630
    }
651
631
  }
652
632
  
653
 
  if(quit_now){
654
 
    goto mandos_end;
655
 
  }
656
 
  
657
633
  if(af == AF_INET6){
658
634
    ret = connect(tcp_sd, &to.in6, sizeof(to));
659
635
  } else {
661
637
  }
662
638
  if(ret < 0){
663
639
    perror("connect");
664
 
    retval = -1;
665
 
    goto mandos_end;
666
 
  }
667
 
  
668
 
  if(quit_now){
669
 
    goto mandos_end;
 
640
    return -1;
670
641
  }
671
642
  
672
643
  const char *out = mandos_protocol_version;
691
662
        break;
692
663
      }
693
664
    }
694
 
  
695
 
    if(quit_now){
696
 
      goto mandos_end;
697
 
    }
698
665
  }
699
666
  
700
667
  if(debug){
701
668
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
702
669
  }
703
670
  
704
 
  if(quit_now){
705
 
    goto mandos_end;
706
 
  }
707
 
  
708
671
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
709
672
  
710
 
  if(quit_now){
711
 
    goto mandos_end;
712
 
  }
713
 
  
714
 
  do {
 
673
  do{
715
674
    ret = gnutls_handshake(session);
716
 
    if(quit_now){
717
 
      goto mandos_end;
718
 
    }
719
675
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
720
676
  
721
677
  if(ret != GNUTLS_E_SUCCESS){
735
691
  }
736
692
  
737
693
  while(true){
738
 
    
739
 
    if(quit_now){
740
 
      goto mandos_end;
741
 
    }
742
 
    
743
694
    buffer_capacity = incbuffer(&buffer, buffer_length,
744
695
                                   buffer_capacity);
745
696
    if(buffer_capacity == 0){
748
699
      goto mandos_end;
749
700
    }
750
701
    
751
 
    if(quit_now){
752
 
      goto mandos_end;
753
 
    }
754
 
    
755
702
    sret = gnutls_record_recv(session, buffer+buffer_length,
756
703
                              BUFFER_SIZE);
757
704
    if(sret == 0){
763
710
      case GNUTLS_E_AGAIN:
764
711
        break;
765
712
      case GNUTLS_E_REHANDSHAKE:
766
 
        do {
 
713
        do{
767
714
          ret = gnutls_handshake(session);
768
 
          
769
 
          if(quit_now){
770
 
            goto mandos_end;
771
 
          }
772
715
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
773
716
        if(ret < 0){
774
717
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
793
736
    fprintf(stderr, "Closing TLS session\n");
794
737
  }
795
738
  
796
 
  if(quit_now){
797
 
    goto mandos_end;
798
 
  }
799
 
  
800
739
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
801
740
  
802
 
  if(quit_now){
803
 
    goto mandos_end;
804
 
  }
805
 
  
806
741
  if(buffer_length > 0){
807
 
    ssize_t decrypted_buffer_size;
808
742
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
809
743
                                               buffer_length,
810
744
                                               &decrypted_buffer);
811
745
    if(decrypted_buffer_size >= 0){
812
 
      
813
746
      written = 0;
814
747
      while(written < (size_t) decrypted_buffer_size){
815
 
        if(quit_now){
816
 
          goto mandos_end;
817
 
        }
818
 
        
819
748
        ret = (int)fwrite(decrypted_buffer + written, 1,
820
749
                          (size_t)decrypted_buffer_size - written,
821
750
                          stdout);
841
770
  
842
771
 mandos_end:
843
772
  free(buffer);
844
 
  if(tcp_sd >= 0){
845
 
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
846
 
  }
 
773
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
847
774
  if(ret == -1){
848
775
    perror("close");
849
776
  }
850
777
  gnutls_deinit(session);
851
 
  if(quit_now){
852
 
    retval = -1;
853
 
  }
854
778
  return retval;
855
779
}
856
780
 
873
797
  /* Called whenever a service has been resolved successfully or
874
798
     timed out */
875
799
  
876
 
  if(quit_now){
877
 
    return;
878
 
  }
879
 
  
880
800
  switch(event){
881
801
  default:
882
802
  case AVAHI_RESOLVER_FAILURE:
919
839
  /* Called whenever a new services becomes available on the LAN or
920
840
     is removed from the LAN */
921
841
  
922
 
  if(quit_now){
923
 
    return;
924
 
  }
925
 
  
926
842
  switch(event){
927
843
  default:
928
844
  case AVAHI_BROWSER_FAILURE:
957
873
  }
958
874
}
959
875
 
 
876
sig_atomic_t quit_now = 0;
 
877
 
960
878
/* stop main loop after sigterm has been called */
961
 
static void handle_sigterm(int sig){
 
879
static void handle_sigterm(__attribute__((unused)) int sig){
962
880
  if(quit_now){
963
881
    return;
964
882
  }
965
883
  quit_now = 1;
966
 
  signal_received = sig;
967
884
  int old_errno = errno;
968
885
  if(mc.simple_poll != NULL){
969
886
    avahi_simple_poll_quit(mc.simple_poll);
980
897
  int exitcode = EXIT_SUCCESS;
981
898
  const char *interface = "eth0";
982
899
  struct ifreq network;
983
 
  int sd = -1;
984
 
  bool take_down_interface = false;
 
900
  int sd;
985
901
  uid_t uid;
986
902
  gid_t gid;
987
903
  char *connect_to = NULL;
1130
1046
    exitcode = EXIT_FAILURE;
1131
1047
    goto end;
1132
1048
  }
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
 
  }
 
1049
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1050
  if(ret == -1){
 
1051
    perror("sigaction");
 
1052
    exitcode = EXIT_FAILURE;
 
1053
    goto end;
 
1054
  }  
1176
1055
  
1177
1056
  /* If the interface is down, bring it up */
1178
1057
  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
1058
#ifdef __linux__
1191
1059
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1192
1060
       messages to mess up the prompt */
1229
1097
    }
1230
1098
    if((network.ifr_flags & IFF_UP) == 0){
1231
1099
      network.ifr_flags |= IFF_UP;
1232
 
      take_down_interface = true;
1233
1100
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1234
1101
      if(ret == -1){
1235
 
        take_down_interface = false;
1236
1102
        perror("ioctl SIOCSIFFLAGS");
1237
1103
        exitcode = EXIT_FAILURE;
1238
1104
#ifdef __linux__
1260
1126
        perror("nanosleep");
1261
1127
      }
1262
1128
    }
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
 
      }
 
1129
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1130
    if(ret == -1){
 
1131
      perror("close");
1269
1132
    }
1270
1133
#ifdef __linux__
1271
1134
    if(restore_loglevel){
1278
1141
#endif  /* __linux__ */
1279
1142
  }
1280
1143
  
1281
 
  if(quit_now){
1282
 
    goto end;
1283
 
  }
1284
 
  
1285
1144
  uid = getuid();
1286
1145
  gid = getgid();
1287
1146
  
1288
 
  /* Drop any group privileges we might have, just to be safe */
1289
1147
  errno = 0;
1290
 
  ret = setgid(gid);
 
1148
  setgid(gid);
1291
1149
  if(ret == -1){
1292
1150
    perror("setgid");
1293
1151
  }
1294
1152
  
1295
 
  /* Drop user privileges */
1296
 
  errno = 0;
1297
 
  /* Will we need privileges later? */
1298
 
  if(take_down_interface){
1299
 
    /* Drop user privileges temporarily */
1300
 
    ret = seteuid(uid);
1301
 
    if(ret == -1){
1302
 
      perror("seteuid");
1303
 
    }
1304
 
  } else {
1305
 
    /* Drop user privileges permanently */
1306
 
    ret = setuid(uid);
1307
 
    if(ret == -1){
1308
 
      perror("setuid");
1309
 
    }
1310
 
  }
1311
 
  
1312
 
  if(quit_now){
1313
 
    goto end;
 
1153
  ret = setuid(uid);
 
1154
  if(ret == -1){
 
1155
    perror("setuid");
1314
1156
  }
1315
1157
  
1316
1158
  ret = init_gnutls_global(pubkey, seckey);
1322
1164
    gnutls_initialized = true;
1323
1165
  }
1324
1166
  
1325
 
  if(quit_now){
1326
 
    goto end;
1327
 
  }
1328
 
  
1329
 
  tempdir_created = true;
1330
1167
  if(mkdtemp(tempdir) == NULL){
1331
 
    tempdir_created = false;
1332
1168
    perror("mkdtemp");
1333
1169
    goto end;
1334
1170
  }
1335
 
  
1336
 
  if(quit_now){
1337
 
    goto end;
1338
 
  }
 
1171
  tempdir_created = true;
1339
1172
  
1340
1173
  if(not init_gpgme(pubkey, seckey, tempdir)){
1341
1174
    fprintf(stderr, "init_gpgme failed\n");
1345
1178
    gpgme_initialized = true;
1346
1179
  }
1347
1180
  
1348
 
  if(quit_now){
1349
 
    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
    }
1350
1188
  }
1351
1189
  
1352
1190
  if(connect_to != NULL){
1358
1196
      exitcode = EXIT_FAILURE;
1359
1197
      goto end;
1360
1198
    }
1361
 
    
1362
 
    if(quit_now){
1363
 
      goto end;
1364
 
    }
1365
 
    
1366
1199
    uint16_t port;
1367
1200
    errno = 0;
1368
1201
    tmpmax = strtoimax(address+1, &tmp, 10);
1372
1205
      exitcode = EXIT_FAILURE;
1373
1206
      goto end;
1374
1207
    }
1375
 
  
1376
 
    if(quit_now){
1377
 
      goto end;
1378
 
    }
1379
 
    
1380
1208
    port = (uint16_t)tmpmax;
1381
1209
    *address = '\0';
1382
1210
    address = connect_to;
1387
1215
    } else {
1388
1216
      af = AF_INET;
1389
1217
    }
1390
 
    
1391
 
    if(quit_now){
1392
 
      goto end;
1393
 
    }
1394
 
    
1395
1218
    ret = start_mandos_communication(address, port, if_index, af);
1396
1219
    if(ret < 0){
1397
1220
      exitcode = EXIT_FAILURE;
1400
1223
    }
1401
1224
    goto end;
1402
1225
  }
1403
 
  
1404
 
  if(quit_now){
1405
 
    goto end;
1406
 
  }
1407
 
  
 
1226
    
1408
1227
  {
1409
1228
    AvahiServerConfig config;
1410
1229
    /* Do not publish any local Zeroconf records */
1431
1250
    goto end;
1432
1251
  }
1433
1252
  
1434
 
  if(quit_now){
1435
 
    goto end;
1436
 
  }
1437
 
  
1438
1253
  /* Create the Avahi service browser */
1439
1254
  sb = avahi_s_service_browser_new(mc.server, if_index,
1440
1255
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1446
1261
    goto end;
1447
1262
  }
1448
1263
  
1449
 
  if(quit_now){
1450
 
    goto end;
1451
 
  }
1452
 
  
1453
1264
  /* Run the main loop */
1454
1265
  
1455
1266
  if(debug){
1484
1295
    gpgme_release(mc.ctx);
1485
1296
  }
1486
1297
  
1487
 
  /* Take down the network interface */
1488
 
  if(take_down_interface){
1489
 
    /* Re-raise priviliges */
1490
 
    errno = 0;
1491
 
    ret = seteuid(0);
1492
 
    if(ret == -1){
1493
 
      perror("seteuid");
1494
 
    }
1495
 
    if(geteuid() == 0){
1496
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1497
 
      if(ret == -1){
1498
 
        perror("ioctl SIOCGIFFLAGS");
1499
 
      } else if(network.ifr_flags & IFF_UP) {
1500
 
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1501
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1502
 
        if(ret == -1){
1503
 
          perror("ioctl SIOCSIFFLAGS");
1504
 
        }
1505
 
      }
1506
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1507
 
      if(ret == -1){
1508
 
        perror("close");
1509
 
      }
1510
 
      /* Lower privileges, permanently this time */
1511
 
      errno = 0;
1512
 
      ret = setuid(uid);
1513
 
      if(ret == -1){
1514
 
        perror("setuid");
1515
 
      }
1516
 
    }
1517
 
  }
1518
 
  
1519
1298
  /* Removes the temp directory used by GPGME */
1520
1299
  if(tempdir_created){
1521
1300
    DIR *d;
1560
1339
    }
1561
1340
  }
1562
1341
  
1563
 
  if(quit_now){
1564
 
    sigemptyset(&old_sigterm_action.sa_mask);
1565
 
    old_sigterm_action.sa_handler = SIG_DFL;
1566
 
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
1567
 
    if(ret == -1){
1568
 
      perror("sigaction");
1569
 
    }
1570
 
    raise(signal_received);
1571
 
  }
1572
 
  
1573
1342
  return exitcode;
1574
1343
}