/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-09-08 06:28:20 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090908062820-xgkdkrnvuze9vc72
* debian/mandos-client.README.Debian: Improved wording and formatting.
                                      Updated location of nfsroot.txt.
* debian/mandos.README.Debian: Improved wording and formatting.

Show diffs side-by-side

added added

removed removed

Lines of Context:
71
71
                                   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
72
72
                                */
73
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
 
                                   getuid(), getgid(), setuid(),
 
74
                                   getuid(), getgid(), seteuid(),
75
75
                                   setgid() */
76
76
#include <arpa/inet.h>          /* inet_pton(), htons */
77
77
#include <iso646.h>             /* not, or, and */
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, sigaction,
84
 
                                   sig_atomic_t */
 
83
                                   sigaction(), SIGTERM, sig_atomic_t,
 
84
                                   raise() */
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;
168
167
  gpgme_error_t rc;
169
168
  gpgme_engine_info_t engine_info;
170
169
  
173
172
   * Helper function to insert pub and seckey to the engine keyring.
174
173
   */
175
174
  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
/* 
514
514
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
515
515
                      __attribute__((unused)) const char *txt){}
516
516
 
 
517
sig_atomic_t quit_now = 0;
 
518
int signal_received = 0;
 
519
 
517
520
/* Called when a Mandos server is found */
518
521
static int start_mandos_communication(const char *ip, uint16_t port,
519
522
                                      AvahiIfIndex if_index,
520
523
                                      int af){
521
 
  int ret, tcp_sd;
 
524
  int ret, tcp_sd = -1;
522
525
  ssize_t sret;
523
526
  union {
524
527
    struct sockaddr_in in;
528
531
  char *decrypted_buffer;
529
532
  size_t buffer_length = 0;
530
533
  size_t buffer_capacity = 0;
531
 
  ssize_t decrypted_buffer_size;
532
534
  size_t written;
533
535
  int retval = 0;
534
536
  gnutls_session_t session;
535
537
  int pf;                       /* Protocol family */
536
538
  
 
539
  if(quit_now){
 
540
    return -1;
 
541
  }
 
542
  
537
543
  switch(af){
538
544
  case AF_INET6:
539
545
    pf = PF_INET6;
559
565
  tcp_sd = socket(pf, SOCK_STREAM, 0);
560
566
  if(tcp_sd < 0){
561
567
    perror("socket");
562
 
    return -1;
 
568
    retval = -1;
 
569
    goto mandos_end;
 
570
  }
 
571
  
 
572
  if(quit_now){
 
573
    goto mandos_end;
563
574
  }
564
575
  
565
576
  memset(&to, 0, sizeof(to));
572
583
  }
573
584
  if(ret < 0 ){
574
585
    perror("inet_pton");
575
 
    return -1;
 
586
    retval = -1;
 
587
    goto mandos_end;
576
588
  }
577
589
  if(ret == 0){
578
590
    fprintf(stderr, "Bad address: %s\n", ip);
579
 
    return -1;
 
591
    retval = -1;
 
592
    goto mandos_end;
580
593
  }
581
594
  if(af == AF_INET6){
582
595
    to.in6.sin6_port = htons(port); /* Spurious warnings from
589
602
      if(if_index == AVAHI_IF_UNSPEC){
590
603
        fprintf(stderr, "An IPv6 link-local address is incomplete"
591
604
                " without a network interface\n");
592
 
        return -1;
 
605
        retval = -1;
 
606
        goto mandos_end;
593
607
      }
594
608
      /* Set the network interface number as scope */
595
609
      to.in6.sin6_scope_id = (uint32_t)if_index;
600
614
                                     -Wunreachable-code */
601
615
  }
602
616
  
 
617
  if(quit_now){
 
618
    goto mandos_end;
 
619
  }
 
620
  
603
621
  if(debug){
604
622
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
605
623
      char interface[IF_NAMESIZE];
632
650
    }
633
651
  }
634
652
  
 
653
  if(quit_now){
 
654
    goto mandos_end;
 
655
  }
 
656
  
635
657
  if(af == AF_INET6){
636
658
    ret = connect(tcp_sd, &to.in6, sizeof(to));
637
659
  } else {
639
661
  }
640
662
  if(ret < 0){
641
663
    perror("connect");
642
 
    return -1;
 
664
    retval = -1;
 
665
    goto mandos_end;
 
666
  }
 
667
  
 
668
  if(quit_now){
 
669
    goto mandos_end;
643
670
  }
644
671
  
645
672
  const char *out = mandos_protocol_version;
664
691
        break;
665
692
      }
666
693
    }
 
694
  
 
695
    if(quit_now){
 
696
      goto mandos_end;
 
697
    }
667
698
  }
668
699
  
669
700
  if(debug){
670
701
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
671
702
  }
672
703
  
 
704
  if(quit_now){
 
705
    goto mandos_end;
 
706
  }
 
707
  
673
708
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
674
709
  
675
 
  do{
 
710
  if(quit_now){
 
711
    goto mandos_end;
 
712
  }
 
713
  
 
714
  do {
676
715
    ret = gnutls_handshake(session);
 
716
    if(quit_now){
 
717
      goto mandos_end;
 
718
    }
677
719
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
678
720
  
679
721
  if(ret != GNUTLS_E_SUCCESS){
693
735
  }
694
736
  
695
737
  while(true){
 
738
    
 
739
    if(quit_now){
 
740
      goto mandos_end;
 
741
    }
 
742
    
696
743
    buffer_capacity = incbuffer(&buffer, buffer_length,
697
744
                                   buffer_capacity);
698
745
    if(buffer_capacity == 0){
701
748
      goto mandos_end;
702
749
    }
703
750
    
 
751
    if(quit_now){
 
752
      goto mandos_end;
 
753
    }
 
754
    
704
755
    sret = gnutls_record_recv(session, buffer+buffer_length,
705
756
                              BUFFER_SIZE);
706
757
    if(sret == 0){
712
763
      case GNUTLS_E_AGAIN:
713
764
        break;
714
765
      case GNUTLS_E_REHANDSHAKE:
715
 
        do{
 
766
        do {
716
767
          ret = gnutls_handshake(session);
 
768
          
 
769
          if(quit_now){
 
770
            goto mandos_end;
 
771
          }
717
772
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
718
773
        if(ret < 0){
719
774
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
738
793
    fprintf(stderr, "Closing TLS session\n");
739
794
  }
740
795
  
 
796
  if(quit_now){
 
797
    goto mandos_end;
 
798
  }
 
799
  
741
800
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
742
801
  
 
802
  if(quit_now){
 
803
    goto mandos_end;
 
804
  }
 
805
  
743
806
  if(buffer_length > 0){
 
807
    ssize_t decrypted_buffer_size;
744
808
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
745
809
                                               buffer_length,
746
810
                                               &decrypted_buffer);
747
811
    if(decrypted_buffer_size >= 0){
 
812
      
748
813
      written = 0;
749
814
      while(written < (size_t) decrypted_buffer_size){
 
815
        if(quit_now){
 
816
          goto mandos_end;
 
817
        }
 
818
        
750
819
        ret = (int)fwrite(decrypted_buffer + written, 1,
751
820
                          (size_t)decrypted_buffer_size - written,
752
821
                          stdout);
772
841
  
773
842
 mandos_end:
774
843
  free(buffer);
775
 
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
844
  if(tcp_sd >= 0){
 
845
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
846
  }
776
847
  if(ret == -1){
777
848
    perror("close");
778
849
  }
779
850
  gnutls_deinit(session);
 
851
  if(quit_now){
 
852
    retval = -1;
 
853
  }
780
854
  return retval;
781
855
}
782
856
 
799
873
  /* Called whenever a service has been resolved successfully or
800
874
     timed out */
801
875
  
 
876
  if(quit_now){
 
877
    return;
 
878
  }
 
879
  
802
880
  switch(event){
803
881
  default:
804
882
  case AVAHI_RESOLVER_FAILURE:
841
919
  /* Called whenever a new services becomes available on the LAN or
842
920
     is removed from the LAN */
843
921
  
 
922
  if(quit_now){
 
923
    return;
 
924
  }
 
925
  
844
926
  switch(event){
845
927
  default:
846
928
  case AVAHI_BROWSER_FAILURE:
875
957
  }
876
958
}
877
959
 
878
 
sig_atomic_t quit_now = 0;
879
 
 
880
960
/* stop main loop after sigterm has been called */
881
 
static void handle_sigterm(__attribute__((unused)) int sig){
 
961
static void handle_sigterm(int sig){
882
962
  if(quit_now){
883
963
    return;
884
964
  }
885
965
  quit_now = 1;
 
966
  signal_received = sig;
886
967
  int old_errno = errno;
887
968
  if(mc.simple_poll != NULL){
888
969
    avahi_simple_poll_quit(mc.simple_poll);
900
981
  const char *interface = "eth0";
901
982
  struct ifreq network;
902
983
  int sd = -1;
903
 
  bool interface_taken_up = false;
 
984
  bool take_down_interface = false;
904
985
  uid_t uid;
905
986
  gid_t gid;
906
987
  char *connect_to = NULL;
1049
1130
    exitcode = EXIT_FAILURE;
1050
1131
    goto end;
1051
1132
  }
1052
 
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1053
 
  if(ret == -1){
1054
 
    perror("sigaction");
1055
 
    exitcode = EXIT_FAILURE;
1056
 
    goto end;
1057
 
  }  
 
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
  }
1058
1176
  
1059
1177
  /* If the interface is down, bring it up */
1060
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
    
1061
1190
#ifdef __linux__
1062
1191
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1063
1192
       messages to mess up the prompt */
1100
1229
    }
1101
1230
    if((network.ifr_flags & IFF_UP) == 0){
1102
1231
      network.ifr_flags |= IFF_UP;
 
1232
      take_down_interface = true;
1103
1233
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1104
1234
      if(ret == -1){
 
1235
        take_down_interface = false;
1105
1236
        perror("ioctl SIOCSIFFLAGS");
1106
1237
        exitcode = EXIT_FAILURE;
1107
1238
#ifdef __linux__
1114
1245
#endif  /* __linux__ */
1115
1246
        goto end;
1116
1247
      }
1117
 
      interface_taken_up = true;
1118
1248
    }
1119
1249
    /* sleep checking until interface is running */
1120
1250
    for(int i=0; i < delay * 4; i++){
1130
1260
        perror("nanosleep");
1131
1261
      }
1132
1262
    }
1133
 
    if(not interface_taken_up){
 
1263
    if(not take_down_interface){
 
1264
      /* We won't need the socket anymore */
1134
1265
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1135
1266
      if(ret == -1){
1136
1267
        perror("close");
1147
1278
#endif  /* __linux__ */
1148
1279
  }
1149
1280
  
 
1281
  if(quit_now){
 
1282
    goto end;
 
1283
  }
 
1284
  
1150
1285
  uid = getuid();
1151
1286
  gid = getgid();
1152
1287
  
 
1288
  /* Drop any group privileges we might have, just to be safe */
1153
1289
  errno = 0;
1154
 
  setgid(gid);
 
1290
  ret = setgid(gid);
1155
1291
  if(ret == -1){
1156
1292
    perror("setgid");
1157
1293
  }
1158
1294
  
1159
 
  ret = setuid(uid);
1160
 
  if(ret == -1){
1161
 
    perror("setuid");
 
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;
1162
1314
  }
1163
1315
  
1164
1316
  ret = init_gnutls_global(pubkey, seckey);
1170
1322
    gnutls_initialized = true;
1171
1323
  }
1172
1324
  
 
1325
  if(quit_now){
 
1326
    goto end;
 
1327
  }
 
1328
  
 
1329
  tempdir_created = true;
1173
1330
  if(mkdtemp(tempdir) == NULL){
 
1331
    tempdir_created = false;
1174
1332
    perror("mkdtemp");
1175
1333
    goto end;
1176
1334
  }
1177
 
  tempdir_created = true;
 
1335
  
 
1336
  if(quit_now){
 
1337
    goto end;
 
1338
  }
1178
1339
  
1179
1340
  if(not init_gpgme(pubkey, seckey, tempdir)){
1180
1341
    fprintf(stderr, "init_gpgme failed\n");
1184
1345
    gpgme_initialized = true;
1185
1346
  }
1186
1347
  
1187
 
  if(interface[0] != '\0'){
1188
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1189
 
    if(if_index == 0){
1190
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1191
 
      exitcode = EXIT_FAILURE;
1192
 
      goto end;
1193
 
    }
 
1348
  if(quit_now){
 
1349
    goto end;
1194
1350
  }
1195
1351
  
1196
1352
  if(connect_to != NULL){
1202
1358
      exitcode = EXIT_FAILURE;
1203
1359
      goto end;
1204
1360
    }
 
1361
    
 
1362
    if(quit_now){
 
1363
      goto end;
 
1364
    }
 
1365
    
1205
1366
    uint16_t port;
1206
1367
    errno = 0;
1207
1368
    tmpmax = strtoimax(address+1, &tmp, 10);
1211
1372
      exitcode = EXIT_FAILURE;
1212
1373
      goto end;
1213
1374
    }
 
1375
  
 
1376
    if(quit_now){
 
1377
      goto end;
 
1378
    }
 
1379
    
1214
1380
    port = (uint16_t)tmpmax;
1215
1381
    *address = '\0';
1216
1382
    address = connect_to;
1221
1387
    } else {
1222
1388
      af = AF_INET;
1223
1389
    }
 
1390
    
 
1391
    if(quit_now){
 
1392
      goto end;
 
1393
    }
 
1394
    
1224
1395
    ret = start_mandos_communication(address, port, if_index, af);
1225
1396
    if(ret < 0){
1226
1397
      exitcode = EXIT_FAILURE;
1229
1400
    }
1230
1401
    goto end;
1231
1402
  }
1232
 
    
 
1403
  
 
1404
  if(quit_now){
 
1405
    goto end;
 
1406
  }
 
1407
  
1233
1408
  {
1234
1409
    AvahiServerConfig config;
1235
1410
    /* Do not publish any local Zeroconf records */
1256
1431
    goto end;
1257
1432
  }
1258
1433
  
 
1434
  if(quit_now){
 
1435
    goto end;
 
1436
  }
 
1437
  
1259
1438
  /* Create the Avahi service browser */
1260
1439
  sb = avahi_s_service_browser_new(mc.server, if_index,
1261
1440
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1267
1446
    goto end;
1268
1447
  }
1269
1448
  
 
1449
  if(quit_now){
 
1450
    goto end;
 
1451
  }
 
1452
  
1270
1453
  /* Run the main loop */
1271
1454
  
1272
1455
  if(debug){
1302
1485
  }
1303
1486
  
1304
1487
  /* Take down the network interface */
1305
 
  if(interface_taken_up){
1306
 
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1488
  if(take_down_interface){
 
1489
    /* Re-raise priviliges */
 
1490
    errno = 0;
 
1491
    ret = seteuid(0);
1307
1492
    if(ret == -1){
1308
 
      perror("ioctl SIOCGIFFLAGS");
1309
 
    } else if(network.ifr_flags & IFF_UP) {
1310
 
      network.ifr_flags &= ~IFF_UP; /* clear flag */
1311
 
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1312
 
      if(ret == -1){
1313
 
        perror("ioctl SIOCSIFFLAGS");
1314
 
      }
 
1493
      perror("seteuid");
1315
1494
    }
1316
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1317
 
    if(ret == -1){
1318
 
      perror("close");
 
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
      }
1319
1516
    }
1320
1517
  }
1321
1518
  
1363
1560
    }
1364
1561
  }
1365
1562
  
 
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
  
1366
1573
  return exitcode;
1367
1574
}