/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-06-02 10:33:04 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090602103304-k6eokrft7wr00255
* plugins.d/mandos-client.c (pgp_packet_decrypt): Remove redundant
                                                 "if" statement.

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