/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-05 17:20:46 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090905172046-s92efz3m1j4c1sti
* plugins-runner.c (main): Bug fix: Close config file.

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