/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-12 18:56:52 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090212185652-ast00yprt2pe2l4p
Overflows are not detected by sscanf(), so stop using it:

* plugin-runner.c (main/parse_opt): Change from using "sscanf()" to
                                    "strtoimax()".
* plugins.d/mandos-client.c (main/parse_opt, main): Change from using
                                                    "sscanf()" to
                                                    "strtoimax()" and
                                                    "strtof()".
* splashy.c (main): Change from using "sscanf()" to "strtoimax()".
* usplash.c (main): - '' -

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
 
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:
938
854
       the callback function is called the Avahi server will free the
939
855
       resolver for us. */
940
856
    
941
 
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
942
 
                                    name, type, domain, protocol, 0,
943
 
                                    resolve_callback, NULL) == NULL)
 
857
    if(!(avahi_s_service_resolver_new(mc.server, interface,
 
858
                                       protocol, name, type, domain,
 
859
                                       AVAHI_PROTO_INET6, 0,
 
860
                                       resolve_callback, NULL)))
944
861
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
945
862
              name, avahi_strerror(avahi_server_errno(mc.server)));
946
863
    break;
957
874
  }
958
875
}
959
876
 
 
877
sig_atomic_t quit_now = 0;
 
878
 
960
879
/* stop main loop after sigterm has been called */
961
 
static void handle_sigterm(int sig){
 
880
static void handle_sigterm(__attribute__((unused)) int sig){
962
881
  if(quit_now){
963
882
    return;
964
883
  }
965
884
  quit_now = 1;
966
 
  signal_received = sig;
967
885
  int old_errno = errno;
968
886
  if(mc.simple_poll != NULL){
969
887
    avahi_simple_poll_quit(mc.simple_poll);
980
898
  int exitcode = EXIT_SUCCESS;
981
899
  const char *interface = "eth0";
982
900
  struct ifreq network;
983
 
  int sd = -1;
984
 
  bool take_down_interface = false;
 
901
  int sd;
985
902
  uid_t uid;
986
903
  gid_t gid;
987
904
  char *connect_to = NULL;
991
908
  const char *seckey = PATHDIR "/" SECKEY;
992
909
  const char *pubkey = PATHDIR "/" PUBKEY;
993
910
  
 
911
  /* Initialize Mandos context */
 
912
  mc = (mandos_context){ .simple_poll = NULL, .server = NULL,
 
913
                         .dh_bits = 1024, .priority = "SECURE256"
 
914
                         ":!CTYPE-X.509:+CTYPE-OPENPGP" };
994
915
  bool gnutls_initialized = false;
995
916
  bool gpgme_initialized = false;
996
917
  float delay = 2.5f;
1130
1051
    exitcode = EXIT_FAILURE;
1131
1052
    goto end;
1132
1053
  }
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
 
  }
 
1054
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1055
  if(ret == -1){
 
1056
    perror("sigaction");
 
1057
    exitcode = EXIT_FAILURE;
 
1058
    goto end;
 
1059
  }  
1176
1060
  
1177
1061
  /* If the interface is down, bring it up */
1178
1062
  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
1063
#ifdef __linux__
1191
1064
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1192
1065
       messages to mess up the prompt */
1229
1102
    }
1230
1103
    if((network.ifr_flags & IFF_UP) == 0){
1231
1104
      network.ifr_flags |= IFF_UP;
1232
 
      take_down_interface = true;
1233
1105
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1234
1106
      if(ret == -1){
1235
 
        take_down_interface = false;
1236
1107
        perror("ioctl SIOCSIFFLAGS");
1237
1108
        exitcode = EXIT_FAILURE;
1238
1109
#ifdef __linux__
1260
1131
        perror("nanosleep");
1261
1132
      }
1262
1133
    }
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
 
      }
 
1134
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1135
    if(ret == -1){
 
1136
      perror("close");
1269
1137
    }
1270
1138
#ifdef __linux__
1271
1139
    if(restore_loglevel){
1278
1146
#endif  /* __linux__ */
1279
1147
  }
1280
1148
  
1281
 
  if(quit_now){
1282
 
    goto end;
1283
 
  }
1284
 
  
1285
1149
  uid = getuid();
1286
1150
  gid = getgid();
1287
1151
  
1296
1160
    perror("setuid");
1297
1161
  }
1298
1162
  
1299
 
  if(quit_now){
1300
 
    goto end;
1301
 
  }
1302
 
  
1303
1163
  ret = init_gnutls_global(pubkey, seckey);
1304
1164
  if(ret == -1){
1305
1165
    fprintf(stderr, "init_gnutls_global failed\n");
1309
1169
    gnutls_initialized = true;
1310
1170
  }
1311
1171
  
1312
 
  if(quit_now){
1313
 
    goto end;
1314
 
  }
1315
 
  
1316
 
  tempdir_created = true;
1317
1172
  if(mkdtemp(tempdir) == NULL){
1318
 
    tempdir_created = false;
1319
1173
    perror("mkdtemp");
1320
1174
    goto end;
1321
1175
  }
1322
 
  
1323
 
  if(quit_now){
1324
 
    goto end;
1325
 
  }
 
1176
  tempdir_created = true;
1326
1177
  
1327
1178
  if(not init_gpgme(pubkey, seckey, tempdir)){
1328
1179
    fprintf(stderr, "init_gpgme failed\n");
1332
1183
    gpgme_initialized = true;
1333
1184
  }
1334
1185
  
1335
 
  if(quit_now){
1336
 
    goto end;
 
1186
  if(interface[0] != '\0'){
 
1187
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1188
    if(if_index == 0){
 
1189
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1190
      exitcode = EXIT_FAILURE;
 
1191
      goto end;
 
1192
    }
1337
1193
  }
1338
1194
  
1339
1195
  if(connect_to != NULL){
1345
1201
      exitcode = EXIT_FAILURE;
1346
1202
      goto end;
1347
1203
    }
1348
 
    
1349
 
    if(quit_now){
1350
 
      goto end;
1351
 
    }
1352
 
    
1353
1204
    uint16_t port;
1354
1205
    errno = 0;
1355
1206
    tmpmax = strtoimax(address+1, &tmp, 10);
1359
1210
      exitcode = EXIT_FAILURE;
1360
1211
      goto end;
1361
1212
    }
1362
 
  
1363
 
    if(quit_now){
1364
 
      goto end;
1365
 
    }
1366
 
    
1367
1213
    port = (uint16_t)tmpmax;
1368
1214
    *address = '\0';
1369
1215
    address = connect_to;
1374
1220
    } else {
1375
1221
      af = AF_INET;
1376
1222
    }
1377
 
    
1378
 
    if(quit_now){
1379
 
      goto end;
1380
 
    }
1381
 
    
1382
1223
    ret = start_mandos_communication(address, port, if_index, af);
1383
1224
    if(ret < 0){
1384
1225
      exitcode = EXIT_FAILURE;
1387
1228
    }
1388
1229
    goto end;
1389
1230
  }
1390
 
  
1391
 
  if(quit_now){
1392
 
    goto end;
1393
 
  }
1394
 
  
 
1231
    
1395
1232
  {
1396
1233
    AvahiServerConfig config;
1397
1234
    /* Do not publish any local Zeroconf records */
1418
1255
    goto end;
1419
1256
  }
1420
1257
  
1421
 
  if(quit_now){
1422
 
    goto end;
1423
 
  }
1424
 
  
1425
1258
  /* Create the Avahi service browser */
1426
1259
  sb = avahi_s_service_browser_new(mc.server, if_index,
1427
 
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
 
1260
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
1428
1261
                                   NULL, 0, browse_callback, NULL);
1429
1262
  if(sb == NULL){
1430
1263
    fprintf(stderr, "Failed to create service browser: %s\n",
1433
1266
    goto end;
1434
1267
  }
1435
1268
  
1436
 
  if(quit_now){
1437
 
    goto end;
1438
 
  }
1439
 
  
1440
1269
  /* Run the main loop */
1441
1270
  
1442
1271
  if(debug){
1471
1300
    gpgme_release(mc.ctx);
1472
1301
  }
1473
1302
  
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
1303
  /* Removes the temp directory used by GPGME */
1493
1304
  if(tempdir_created){
1494
1305
    DIR *d;
1533
1344
    }
1534
1345
  }
1535
1346
  
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
1347
  return exitcode;
1547
1348
}