/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-21 20:04:06 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090521200406-2d5yqwuelzai5si8
* Makefile (GNUTLS_CFLAGS, GNUTLS_LIBS): Use "pkg-config" instead of
                                         "libgnutls-config".

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() */
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;
537
536
  gnutls_session_t session;
538
537
  int pf;                       /* Protocol family */
539
538
  
540
 
  if(quit_now){
541
 
    return -1;
542
 
  }
543
 
  
544
539
  switch(af){
545
540
  case AF_INET6:
546
541
    pf = PF_INET6;
566
561
  tcp_sd = socket(pf, SOCK_STREAM, 0);
567
562
  if(tcp_sd < 0){
568
563
    perror("socket");
569
 
    retval = -1;
570
 
    goto mandos_end;
571
 
  }
572
 
  
573
 
  if(quit_now){
574
 
    goto mandos_end;
 
564
    return -1;
575
565
  }
576
566
  
577
567
  memset(&to, 0, sizeof(to));
584
574
  }
585
575
  if(ret < 0 ){
586
576
    perror("inet_pton");
587
 
    retval = -1;
588
 
    goto mandos_end;
 
577
    return -1;
589
578
  }
590
579
  if(ret == 0){
591
580
    fprintf(stderr, "Bad address: %s\n", ip);
592
 
    retval = -1;
593
 
    goto mandos_end;
 
581
    return -1;
594
582
  }
595
583
  if(af == AF_INET6){
596
584
    to.in6.sin6_port = htons(port); /* Spurious warnings from
603
591
      if(if_index == AVAHI_IF_UNSPEC){
604
592
        fprintf(stderr, "An IPv6 link-local address is incomplete"
605
593
                " without a network interface\n");
606
 
        retval = -1;
607
 
        goto mandos_end;
 
594
        return -1;
608
595
      }
609
596
      /* Set the network interface number as scope */
610
597
      to.in6.sin6_scope_id = (uint32_t)if_index;
615
602
                                     -Wunreachable-code */
616
603
  }
617
604
  
618
 
  if(quit_now){
619
 
    goto mandos_end;
620
 
  }
621
 
  
622
605
  if(debug){
623
606
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
624
607
      char interface[IF_NAMESIZE];
651
634
    }
652
635
  }
653
636
  
654
 
  if(quit_now){
655
 
    goto mandos_end;
656
 
  }
657
 
  
658
637
  if(af == AF_INET6){
659
638
    ret = connect(tcp_sd, &to.in6, sizeof(to));
660
639
  } else {
662
641
  }
663
642
  if(ret < 0){
664
643
    perror("connect");
665
 
    retval = -1;
666
 
    goto mandos_end;
667
 
  }
668
 
  
669
 
  if(quit_now){
670
 
    goto mandos_end;
 
644
    return -1;
671
645
  }
672
646
  
673
647
  const char *out = mandos_protocol_version;
692
666
        break;
693
667
      }
694
668
    }
695
 
  
696
 
    if(quit_now){
697
 
      goto mandos_end;
698
 
    }
699
669
  }
700
670
  
701
671
  if(debug){
702
672
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
703
673
  }
704
674
  
705
 
  if(quit_now){
706
 
    goto mandos_end;
707
 
  }
708
 
  
709
675
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
710
676
  
711
 
  if(quit_now){
712
 
    goto mandos_end;
713
 
  }
714
 
  
715
677
  do{
716
678
    ret = gnutls_handshake(session);
717
 
    if(quit_now){
718
 
      goto mandos_end;
719
 
    }
720
679
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
721
680
  
722
681
  if(ret != GNUTLS_E_SUCCESS){
736
695
  }
737
696
  
738
697
  while(true){
739
 
    
740
 
    if(quit_now){
741
 
      goto mandos_end;
742
 
    }
743
 
    
744
698
    buffer_capacity = incbuffer(&buffer, buffer_length,
745
699
                                   buffer_capacity);
746
700
    if(buffer_capacity == 0){
749
703
      goto mandos_end;
750
704
    }
751
705
    
752
 
    if(quit_now){
753
 
      goto mandos_end;
754
 
    }
755
 
    
756
706
    sret = gnutls_record_recv(session, buffer+buffer_length,
757
707
                              BUFFER_SIZE);
758
708
    if(sret == 0){
766
716
      case GNUTLS_E_REHANDSHAKE:
767
717
        do{
768
718
          ret = gnutls_handshake(session);
769
 
          
770
 
          if(quit_now){
771
 
            goto mandos_end;
772
 
          }
773
719
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
774
720
        if(ret < 0){
775
721
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
794
740
    fprintf(stderr, "Closing TLS session\n");
795
741
  }
796
742
  
797
 
  if(quit_now){
798
 
    goto mandos_end;
799
 
  }
800
 
  
801
743
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
802
744
  
803
 
  if(quit_now){
804
 
    goto mandos_end;
805
 
  }
806
 
  
807
745
  if(buffer_length > 0){
808
746
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
809
747
                                               buffer_length,
811
749
    if(decrypted_buffer_size >= 0){
812
750
      written = 0;
813
751
      while(written < (size_t) decrypted_buffer_size){
814
 
        if(quit_now){
815
 
          goto mandos_end;
816
 
        }
817
 
        
818
752
        ret = (int)fwrite(decrypted_buffer + written, 1,
819
753
                          (size_t)decrypted_buffer_size - written,
820
754
                          stdout);
840
774
  
841
775
 mandos_end:
842
776
  free(buffer);
843
 
  if(tcp_sd >= 0){
844
 
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
845
 
  }
 
777
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
846
778
  if(ret == -1){
847
779
    perror("close");
848
780
  }
849
781
  gnutls_deinit(session);
850
 
  if(quit_now){
851
 
    retval = -1;
852
 
  }
853
782
  return retval;
854
783
}
855
784
 
872
801
  /* Called whenever a service has been resolved successfully or
873
802
     timed out */
874
803
  
875
 
  if(quit_now){
876
 
    return;
877
 
  }
878
 
  
879
804
  switch(event){
880
805
  default:
881
806
  case AVAHI_RESOLVER_FAILURE:
918
843
  /* Called whenever a new services becomes available on the LAN or
919
844
     is removed from the LAN */
920
845
  
921
 
  if(quit_now){
922
 
    return;
923
 
  }
924
 
  
925
846
  switch(event){
926
847
  default:
927
848
  case AVAHI_BROWSER_FAILURE:
956
877
  }
957
878
}
958
879
 
 
880
sig_atomic_t quit_now = 0;
 
881
 
959
882
/* stop main loop after sigterm has been called */
960
 
static void handle_sigterm(int sig){
 
883
static void handle_sigterm(__attribute__((unused)) int sig){
961
884
  if(quit_now){
962
885
    return;
963
886
  }
964
887
  quit_now = 1;
965
 
  signal_received = sig;
966
888
  int old_errno = errno;
967
889
  if(mc.simple_poll != NULL){
968
890
    avahi_simple_poll_quit(mc.simple_poll);
979
901
  int exitcode = EXIT_SUCCESS;
980
902
  const char *interface = "eth0";
981
903
  struct ifreq network;
982
 
  int sd = -1;
983
 
  bool take_down_interface = false;
 
904
  int sd;
984
905
  uid_t uid;
985
906
  gid_t gid;
986
907
  char *connect_to = NULL;
1129
1050
    exitcode = EXIT_FAILURE;
1130
1051
    goto end;
1131
1052
  }
1132
 
  /* Need to check if the handler is SIG_IGN before handling:
1133
 
     | [[info:libc:Initial Signal Actions]] |
1134
 
     | [[info:libc:Basic Signal Handling]]  |
1135
 
  */
1136
 
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1137
 
  if(ret == -1){
1138
 
    perror("sigaction");
1139
 
    return EXIT_FAILURE;
1140
 
  }
1141
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1142
 
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1143
 
    if(ret == -1){
1144
 
      perror("sigaction");
1145
 
      exitcode = EXIT_FAILURE;
1146
 
      goto end;
1147
 
    }
1148
 
  }
1149
 
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1150
 
  if(ret == -1){
1151
 
    perror("sigaction");
1152
 
    return EXIT_FAILURE;
1153
 
  }
1154
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1155
 
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1156
 
    if(ret == -1){
1157
 
      perror("sigaction");
1158
 
      exitcode = EXIT_FAILURE;
1159
 
      goto end;
1160
 
    }
1161
 
  }
1162
 
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1163
 
  if(ret == -1){
1164
 
    perror("sigaction");
1165
 
    return EXIT_FAILURE;
1166
 
  }
1167
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1168
 
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1169
 
    if(ret == -1){
1170
 
      perror("sigaction");
1171
 
      exitcode = EXIT_FAILURE;
1172
 
      goto end;
1173
 
    }
1174
 
  }
 
1053
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1054
  if(ret == -1){
 
1055
    perror("sigaction");
 
1056
    exitcode = EXIT_FAILURE;
 
1057
    goto end;
 
1058
  }  
1175
1059
  
1176
1060
  /* If the interface is down, bring it up */
1177
1061
  if(interface[0] != '\0'){
1178
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1179
 
    if(if_index == 0){
1180
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1181
 
      exitcode = EXIT_FAILURE;
1182
 
      goto end;
1183
 
    }
1184
 
    
1185
 
    if(quit_now){
1186
 
      goto end;
1187
 
    }
1188
 
    
1189
1062
#ifdef __linux__
1190
1063
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1191
1064
       messages to mess up the prompt */
1228
1101
    }
1229
1102
    if((network.ifr_flags & IFF_UP) == 0){
1230
1103
      network.ifr_flags |= IFF_UP;
1231
 
      take_down_interface = true;
1232
1104
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1233
1105
      if(ret == -1){
1234
 
        take_down_interface = false;
1235
1106
        perror("ioctl SIOCSIFFLAGS");
1236
1107
        exitcode = EXIT_FAILURE;
1237
1108
#ifdef __linux__
1259
1130
        perror("nanosleep");
1260
1131
      }
1261
1132
    }
1262
 
    if(not take_down_interface){
1263
 
      /* We won't need the socket anymore */
1264
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1265
 
      if(ret == -1){
1266
 
        perror("close");
1267
 
      }
 
1133
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1134
    if(ret == -1){
 
1135
      perror("close");
1268
1136
    }
1269
1137
#ifdef __linux__
1270
1138
    if(restore_loglevel){
1277
1145
#endif  /* __linux__ */
1278
1146
  }
1279
1147
  
1280
 
  if(quit_now){
1281
 
    goto end;
1282
 
  }
1283
 
  
1284
1148
  uid = getuid();
1285
1149
  gid = getgid();
1286
1150
  
1295
1159
    perror("setuid");
1296
1160
  }
1297
1161
  
1298
 
  if(quit_now){
1299
 
    goto end;
1300
 
  }
1301
 
  
1302
1162
  ret = init_gnutls_global(pubkey, seckey);
1303
1163
  if(ret == -1){
1304
1164
    fprintf(stderr, "init_gnutls_global failed\n");
1308
1168
    gnutls_initialized = true;
1309
1169
  }
1310
1170
  
1311
 
  if(quit_now){
1312
 
    goto end;
1313
 
  }
1314
 
  
1315
 
  tempdir_created = true;
1316
1171
  if(mkdtemp(tempdir) == NULL){
1317
 
    tempdir_created = false;
1318
1172
    perror("mkdtemp");
1319
1173
    goto end;
1320
1174
  }
1321
 
  
1322
 
  if(quit_now){
1323
 
    goto end;
1324
 
  }
 
1175
  tempdir_created = true;
1325
1176
  
1326
1177
  if(not init_gpgme(pubkey, seckey, tempdir)){
1327
1178
    fprintf(stderr, "init_gpgme failed\n");
1331
1182
    gpgme_initialized = true;
1332
1183
  }
1333
1184
  
1334
 
  if(quit_now){
1335
 
    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
    }
1336
1192
  }
1337
1193
  
1338
1194
  if(connect_to != NULL){
1344
1200
      exitcode = EXIT_FAILURE;
1345
1201
      goto end;
1346
1202
    }
1347
 
    
1348
 
    if(quit_now){
1349
 
      goto end;
1350
 
    }
1351
 
    
1352
1203
    uint16_t port;
1353
1204
    errno = 0;
1354
1205
    tmpmax = strtoimax(address+1, &tmp, 10);
1358
1209
      exitcode = EXIT_FAILURE;
1359
1210
      goto end;
1360
1211
    }
1361
 
  
1362
 
    if(quit_now){
1363
 
      goto end;
1364
 
    }
1365
 
    
1366
1212
    port = (uint16_t)tmpmax;
1367
1213
    *address = '\0';
1368
1214
    address = connect_to;
1373
1219
    } else {
1374
1220
      af = AF_INET;
1375
1221
    }
1376
 
    
1377
 
    if(quit_now){
1378
 
      goto end;
1379
 
    }
1380
 
    
1381
1222
    ret = start_mandos_communication(address, port, if_index, af);
1382
1223
    if(ret < 0){
1383
1224
      exitcode = EXIT_FAILURE;
1386
1227
    }
1387
1228
    goto end;
1388
1229
  }
1389
 
  
1390
 
  if(quit_now){
1391
 
    goto end;
1392
 
  }
1393
 
  
 
1230
    
1394
1231
  {
1395
1232
    AvahiServerConfig config;
1396
1233
    /* Do not publish any local Zeroconf records */
1417
1254
    goto end;
1418
1255
  }
1419
1256
  
1420
 
  if(quit_now){
1421
 
    goto end;
1422
 
  }
1423
 
  
1424
1257
  /* Create the Avahi service browser */
1425
1258
  sb = avahi_s_service_browser_new(mc.server, if_index,
1426
1259
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1432
1265
    goto end;
1433
1266
  }
1434
1267
  
1435
 
  if(quit_now){
1436
 
    goto end;
1437
 
  }
1438
 
  
1439
1268
  /* Run the main loop */
1440
1269
  
1441
1270
  if(debug){
1470
1299
    gpgme_release(mc.ctx);
1471
1300
  }
1472
1301
  
1473
 
  /* Take down the network interface */
1474
 
  if(take_down_interface){
1475
 
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1476
 
    if(ret == -1){
1477
 
      perror("ioctl SIOCGIFFLAGS");
1478
 
    } else if(network.ifr_flags & IFF_UP) {
1479
 
      network.ifr_flags &= ~IFF_UP; /* clear flag */
1480
 
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1481
 
      if(ret == -1){
1482
 
        perror("ioctl SIOCSIFFLAGS");
1483
 
      }
1484
 
    }
1485
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1486
 
    if(ret == -1){
1487
 
      perror("close");
1488
 
    }
1489
 
  }
1490
 
  
1491
1302
  /* Removes the temp directory used by GPGME */
1492
1303
  if(tempdir_created){
1493
1304
    DIR *d;
1532
1343
    }
1533
1344
  }
1534
1345
  
1535
 
  if(quit_now){
1536
 
    sigemptyset(&old_sigterm_action.sa_mask);
1537
 
    old_sigterm_action.sa_handler = SIG_DFL;
1538
 
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
1539
 
    if(ret == -1){
1540
 
      perror("sigaction");
1541
 
    }
1542
 
    raise(signal_received);
1543
 
  }
1544
 
  
1545
1346
  return exitcode;
1546
1347
}