/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-09 02:13:58 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090209021358-6nrlc91oi08hn5hb
* initramfs-tools-hook: Whitespace change only.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
37
37
 
38
38
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
39
 
                                   stdout, ferror(), sscanf */
 
39
                                   stdout, ferror(), sscanf(),
 
40
                                   remove() */
40
41
#include <stdint.h>             /* uint16_t, uint32_t */
41
42
#include <stddef.h>             /* NULL, size_t, ssize_t */
42
43
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
43
44
                                   srand() */
44
 
#include <stdbool.h>            /* bool, true */
 
45
#include <stdbool.h>            /* bool, false, true */
45
46
#include <string.h>             /* memset(), strcmp(), strlen(),
46
47
                                   strerror(), asprintf(), strcpy() */
47
 
#include <sys/ioctl.h>          /* ioctl */
 
48
#include <sys/ioctl.h>          /* ioctl */
48
49
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
49
50
                                   sockaddr_in6, PF_INET6,
50
 
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
51
 
                                   uid_t, gid_t, open(), opendir(),
52
 
                                   DIR */
 
51
                                   SOCK_STREAM, uid_t, gid_t, open(),
 
52
                                   opendir(), DIR */
53
53
#include <sys/stat.h>           /* open() */
54
54
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
55
 
                                   struct in6_addr, inet_pton(),
56
 
                                   connect() */
 
55
                                   inet_pton(), connect() */
57
56
#include <fcntl.h>              /* open() */
58
57
#include <dirent.h>             /* opendir(), struct dirent, readdir()
59
58
                                 */
60
59
#include <inttypes.h>           /* PRIu16, intmax_t, SCNdMAX */
61
60
#include <assert.h>             /* assert() */
62
61
#include <errno.h>              /* perror(), errno */
63
 
#include <time.h>               /* time(), nanosleep() */
 
62
#include <time.h>               /* nanosleep(), time() */
64
63
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
65
64
                                   SIOCSIFFLAGS, if_indextoname(),
66
65
                                   if_nametoindex(), IF_NAMESIZE */
67
 
#include <netinet/in.h>
 
66
#include <netinet/in.h>         /* IN6_IS_ADDR_LINKLOCAL,
 
67
                                   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
 
68
                                */
68
69
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
69
70
                                   getuid(), getgid(), setuid(),
70
71
                                   setgid() */
71
72
#include <arpa/inet.h>          /* inet_pton(), htons */
72
 
#include <iso646.h>             /* not, and, or */
 
73
#include <iso646.h>             /* not, or, and */
73
74
#include <argp.h>               /* struct argp_option, error_t, struct
74
75
                                   argp_state, struct argp,
75
76
                                   argp_parse(), ARGP_KEY_ARG,
76
77
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
 
78
#ifdef __linux__
77
79
#include <sys/klog.h>           /* klogctl() */
 
80
#endif
78
81
 
79
82
/* Avahi */
80
83
/* All Avahi types, constants and functions
154
157
  
155
158
  
156
159
  /*
157
 
   * Helper function to insert pub and seckey to the enigne keyring.
 
160
   * Helper function to insert pub and seckey to the engine keyring.
158
161
   */
159
162
  bool import_key(const char *filename){
160
163
    int fd;
365
368
  return plaintext_length;
366
369
}
367
370
 
368
 
static const char * safer_gnutls_strerror(int value) {
 
371
static const char * safer_gnutls_strerror(int value){
369
372
  const char *ret = gnutls_strerror(value); /* Spurious warning from
370
373
                                               -Wunreachable-code */
371
374
  if(ret == NULL)
389
392
  }
390
393
  
391
394
  ret = gnutls_global_init();
392
 
  if(ret != GNUTLS_E_SUCCESS) {
 
395
  if(ret != GNUTLS_E_SUCCESS){
393
396
    fprintf(stderr, "GnuTLS global_init: %s\n",
394
397
            safer_gnutls_strerror(ret));
395
398
    return -1;
407
410
  gnutls_certificate_allocate_credentials(&mc->cred);
408
411
  if(ret != GNUTLS_E_SUCCESS){
409
412
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
410
 
                                                  * from
411
 
                                                  * -Wunreachable-code
412
 
                                                  */
 
413
                                                    from
 
414
                                                    -Wunreachable-code
 
415
                                                 */
413
416
            safer_gnutls_strerror(ret));
414
417
    gnutls_global_deinit();
415
418
    return -1;
424
427
  ret = gnutls_certificate_set_openpgp_key_file
425
428
    (mc->cred, pubkeyfilename, seckeyfilename,
426
429
     GNUTLS_OPENPGP_FMT_BASE64);
427
 
  if(ret != GNUTLS_E_SUCCESS) {
 
430
  if(ret != GNUTLS_E_SUCCESS){
428
431
    fprintf(stderr,
429
432
            "Error[%d] while reading the OpenPGP key pair ('%s',"
430
433
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
435
438
  
436
439
  /* GnuTLS server initialization */
437
440
  ret = gnutls_dh_params_init(&mc->dh_params);
438
 
  if(ret != GNUTLS_E_SUCCESS) {
 
441
  if(ret != GNUTLS_E_SUCCESS){
439
442
    fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
440
443
            " %s\n", safer_gnutls_strerror(ret));
441
444
    goto globalfail;
442
445
  }
443
446
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
444
 
  if(ret != GNUTLS_E_SUCCESS) {
 
447
  if(ret != GNUTLS_E_SUCCESS){
445
448
    fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
446
449
            safer_gnutls_strerror(ret));
447
450
    goto globalfail;
472
475
  {
473
476
    const char *err;
474
477
    ret = gnutls_priority_set_direct(*session, mc->priority, &err);
475
 
    if(ret != GNUTLS_E_SUCCESS) {
 
478
    if(ret != GNUTLS_E_SUCCESS){
476
479
      fprintf(stderr, "Syntax error at: %s\n", err);
477
480
      fprintf(stderr, "GnuTLS error: %s\n",
478
481
              safer_gnutls_strerror(ret));
483
486
  
484
487
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
485
488
                               mc->cred);
486
 
  if(ret != GNUTLS_E_SUCCESS) {
 
489
  if(ret != GNUTLS_E_SUCCESS){
487
490
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
488
491
            safer_gnutls_strerror(ret));
489
492
    gnutls_deinit(*session);
506
509
/* Called when a Mandos server is found */
507
510
static int start_mandos_communication(const char *ip, uint16_t port,
508
511
                                      AvahiIfIndex if_index,
509
 
                                      mandos_context *mc){
 
512
                                      mandos_context *mc, int af){
510
513
  int ret, tcp_sd;
511
514
  ssize_t sret;
512
 
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
 
515
  union {
 
516
    struct sockaddr_in in;
 
517
    struct sockaddr_in6 in6;
 
518
  } to;
513
519
  char *buffer = NULL;
514
520
  char *decrypted_buffer;
515
521
  size_t buffer_length = 0;
517
523
  ssize_t decrypted_buffer_size;
518
524
  size_t written;
519
525
  int retval = 0;
520
 
  char interface[IF_NAMESIZE];
521
526
  gnutls_session_t session;
 
527
  int pf;                       /* Protocol family */
 
528
  
 
529
  switch(af){
 
530
  case AF_INET6:
 
531
    pf = PF_INET6;
 
532
    break;
 
533
  case AF_INET:
 
534
    pf = PF_INET;
 
535
    break;
 
536
  default:
 
537
    fprintf(stderr, "Bad address family: %d\n", af);
 
538
    return -1;
 
539
  }
522
540
  
523
541
  ret = init_gnutls_session(mc, &session);
524
542
  if(ret != 0){
526
544
  }
527
545
  
528
546
  if(debug){
529
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
 
547
    fprintf(stderr, "Setting up a TCP connection to %s, port %" PRIu16
530
548
            "\n", ip, port);
531
549
  }
532
550
  
533
 
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
534
 
  if(tcp_sd < 0) {
 
551
  tcp_sd = socket(pf, SOCK_STREAM, 0);
 
552
  if(tcp_sd < 0){
535
553
    perror("socket");
536
554
    return -1;
537
555
  }
538
556
  
539
 
  if(debug){
540
 
    if(if_indextoname((unsigned int)if_index, interface) == NULL){
541
 
      perror("if_indextoname");
542
 
      return -1;
543
 
    }
544
 
    fprintf(stderr, "Binding to interface %s\n", interface);
545
 
  }
546
 
  
547
557
  memset(&to, 0, sizeof(to));
548
 
  to.in6.sin6_family = AF_INET6;
549
 
  /* It would be nice to have a way to detect if we were passed an
550
 
     IPv4 address here.   Now we assume an IPv6 address. */
551
 
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
 
558
  if(af == AF_INET6){
 
559
    to.in6.sin6_family = (uint16_t)af;
 
560
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
 
561
  } else {                      /* IPv4 */
 
562
    to.in.sin_family = (sa_family_t)af;
 
563
    ret = inet_pton(af, ip, &to.in.sin_addr);
 
564
  }
552
565
  if(ret < 0 ){
553
566
    perror("inet_pton");
554
567
    return -1;
557
570
    fprintf(stderr, "Bad address: %s\n", ip);
558
571
    return -1;
559
572
  }
560
 
  to.in6.sin6_port = htons(port); /* Spurious warnings from
 
573
  if(af == AF_INET6){
 
574
    to.in6.sin6_port = htons(port); /* Spurious warnings from
 
575
                                       -Wconversion and
 
576
                                       -Wunreachable-code */
 
577
    
 
578
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
 
579
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
 
580
                              -Wunreachable-code*/
 
581
      if(if_index == AVAHI_IF_UNSPEC){
 
582
        fprintf(stderr, "An IPv6 link-local address is incomplete"
 
583
                " without a network interface\n");
 
584
        return -1;
 
585
      }
 
586
      /* Set the network interface number as scope */
 
587
      to.in6.sin6_scope_id = (uint32_t)if_index;
 
588
    }
 
589
  } else {
 
590
    to.in.sin_port = htons(port); /* Spurious warnings from
561
591
                                     -Wconversion and
562
592
                                     -Wunreachable-code */
563
 
  
564
 
  to.in6.sin6_scope_id = (uint32_t)if_index;
 
593
  }
565
594
  
566
595
  if(debug){
567
 
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
568
 
            port);
569
 
    char addrstr[INET6_ADDRSTRLEN] = "";
570
 
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
571
 
                 sizeof(addrstr)) == NULL){
 
596
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
 
597
      char interface[IF_NAMESIZE];
 
598
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
599
        perror("if_indextoname");
 
600
      } else {
 
601
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
 
602
                ip, interface, port);
 
603
      }
 
604
    } else {
 
605
      fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
 
606
              port);
 
607
    }
 
608
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
 
609
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
 
610
    const char *pcret;
 
611
    if(af == AF_INET6){
 
612
      pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
 
613
                        sizeof(addrstr));
 
614
    } else {
 
615
      pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
 
616
                        sizeof(addrstr));
 
617
    }
 
618
    if(pcret == NULL){
572
619
      perror("inet_ntop");
573
620
    } else {
574
621
      if(strcmp(addrstr, ip) != 0){
577
624
    }
578
625
  }
579
626
  
580
 
  ret = connect(tcp_sd, &to.in, sizeof(to));
 
627
  if(af == AF_INET6){
 
628
    ret = connect(tcp_sd, &to.in6, sizeof(to));
 
629
  } else {
 
630
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
 
631
  }
581
632
  if(ret < 0){
582
633
    perror("connect");
583
634
    return -1;
629
680
  /* Read OpenPGP packet that contains the wanted password */
630
681
  
631
682
  if(debug){
632
 
    fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
 
683
    fprintf(stderr, "Retrieving OpenPGP encrypted password from %s\n",
633
684
            ip);
634
685
  }
635
686
  
723
774
 
724
775
static void resolve_callback(AvahiSServiceResolver *r,
725
776
                             AvahiIfIndex interface,
726
 
                             AVAHI_GCC_UNUSED AvahiProtocol protocol,
 
777
                             AvahiProtocol proto,
727
778
                             AvahiResolverEvent event,
728
779
                             const char *name,
729
780
                             const char *type,
734
785
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
735
786
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
736
787
                             flags,
737
 
                             void* userdata) {
 
788
                             void* userdata){
738
789
  mandos_context *mc = userdata;
739
790
  assert(r);
740
791
  
741
792
  /* Called whenever a service has been resolved successfully or
742
793
     timed out */
743
794
  
744
 
  switch(event) {
 
795
  switch(event){
745
796
  default:
746
797
  case AVAHI_RESOLVER_FAILURE:
747
798
    fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
758
809
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
759
810
                ip, (intmax_t)interface, port);
760
811
      }
761
 
      int ret = start_mandos_communication(ip, port, interface, mc);
 
812
      int ret = start_mandos_communication(ip, port, interface, mc,
 
813
                                           avahi_proto_to_af(proto));
762
814
      if(ret == 0){
763
815
        avahi_simple_poll_quit(mc->simple_poll);
764
816
      }
767
819
  avahi_s_service_resolver_free(r);
768
820
}
769
821
 
770
 
static void browse_callback( AvahiSServiceBrowser *b,
771
 
                             AvahiIfIndex interface,
772
 
                             AvahiProtocol protocol,
773
 
                             AvahiBrowserEvent event,
774
 
                             const char *name,
775
 
                             const char *type,
776
 
                             const char *domain,
777
 
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
778
 
                             flags,
779
 
                             void* userdata) {
 
822
static void browse_callback(AvahiSServiceBrowser *b,
 
823
                            AvahiIfIndex interface,
 
824
                            AvahiProtocol protocol,
 
825
                            AvahiBrowserEvent event,
 
826
                            const char *name,
 
827
                            const char *type,
 
828
                            const char *domain,
 
829
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
 
830
                            flags,
 
831
                            void* userdata){
780
832
  mandos_context *mc = userdata;
781
833
  assert(b);
782
834
  
783
835
  /* Called whenever a new services becomes available on the LAN or
784
836
     is removed from the LAN */
785
837
  
786
 
  switch(event) {
 
838
  switch(event){
787
839
  default:
788
840
  case AVAHI_BROWSER_FAILURE:
789
841
    
819
871
}
820
872
 
821
873
int main(int argc, char *argv[]){
822
 
    AvahiSServiceBrowser *sb = NULL;
823
 
    int error;
824
 
    int ret;
825
 
    intmax_t tmpmax;
826
 
    int numchars;
827
 
    int exitcode = EXIT_SUCCESS;
828
 
    const char *interface = "eth0";
829
 
    struct ifreq network;
830
 
    int sd;
831
 
    uid_t uid;
832
 
    gid_t gid;
833
 
    char *connect_to = NULL;
834
 
    char tempdir[] = "/tmp/mandosXXXXXX";
835
 
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
836
 
    const char *seckey = PATHDIR "/" SECKEY;
837
 
    const char *pubkey = PATHDIR "/" PUBKEY;
838
 
    
839
 
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
840
 
                          .dh_bits = 1024, .priority = "SECURE256"
841
 
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
842
 
    bool gnutls_initalized = false;
843
 
    bool gpgme_initalized = false;
844
 
    double delay = 2.5;
845
 
    
846
 
    {
847
 
      struct argp_option options[] = {
848
 
        { .name = "debug", .key = 128,
849
 
          .doc = "Debug mode", .group = 3 },
850
 
        { .name = "connect", .key = 'c',
851
 
          .arg = "ADDRESS:PORT",
852
 
          .doc = "Connect directly to a specific Mandos server",
853
 
          .group = 1 },
854
 
        { .name = "interface", .key = 'i',
855
 
          .arg = "NAME",
856
 
          .doc = "Interface that will be used to search for Mandos"
857
 
          " servers",
858
 
          .group = 1 },
859
 
        { .name = "seckey", .key = 's',
860
 
          .arg = "FILE",
861
 
          .doc = "OpenPGP secret key file base name",
862
 
          .group = 1 },
863
 
        { .name = "pubkey", .key = 'p',
864
 
          .arg = "FILE",
865
 
          .doc = "OpenPGP public key file base name",
866
 
          .group = 2 },
867
 
        { .name = "dh-bits", .key = 129,
868
 
          .arg = "BITS",
869
 
          .doc = "Bit length of the prime number used in the"
870
 
          " Diffie-Hellman key exchange",
871
 
          .group = 2 },
872
 
        { .name = "priority", .key = 130,
873
 
          .arg = "STRING",
874
 
          .doc = "GnuTLS priority string for the TLS handshake",
875
 
          .group = 1 },
876
 
        { .name = "delay", .key = 131,
877
 
          .arg = "SECONDS",
878
 
          .doc = "Maximum delay to wait for interface startup",
879
 
          .group = 2 },
880
 
        { .name = NULL }
881
 
      };
882
 
      
883
 
      error_t parse_opt(int key, char *arg,
884
 
                        struct argp_state *state) {
885
 
        switch(key) {
886
 
        case 128:               /* --debug */
887
 
          debug = true;
888
 
          break;
889
 
        case 'c':               /* --connect */
890
 
          connect_to = arg;
891
 
          break;
892
 
        case 'i':               /* --interface */
893
 
          interface = arg;
894
 
          break;
895
 
        case 's':               /* --seckey */
896
 
          seckey = arg;
897
 
          break;
898
 
        case 'p':               /* --pubkey */
899
 
          pubkey = arg;
900
 
          break;
901
 
        case 129:               /* --dh-bits */
902
 
          ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
903
 
          if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
904
 
             or arg[numchars] != '\0'){
905
 
            fprintf(stderr, "Bad number of DH bits\n");
906
 
            exit(EXIT_FAILURE);
907
 
          }
908
 
          mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
909
 
          break;
910
 
        case 130:               /* --priority */
911
 
          mc.priority = arg;
912
 
          break;
913
 
        case 131:               /* --delay */
914
 
          ret = sscanf(arg, "%lf%n", &delay, &numchars);
915
 
          if(ret < 1 or arg[numchars] != '\0'){
916
 
            fprintf(stderr, "Bad delay\n");
917
 
            exit(EXIT_FAILURE);
918
 
          }
919
 
          break;
920
 
        case ARGP_KEY_ARG:
921
 
          argp_usage(state);
922
 
        case ARGP_KEY_END:
923
 
          break;
924
 
        default:
925
 
          return ARGP_ERR_UNKNOWN;
926
 
        }
927
 
        return 0;
928
 
      }
929
 
      
930
 
      struct argp argp = { .options = options, .parser = parse_opt,
931
 
                           .args_doc = "",
932
 
                           .doc = "Mandos client -- Get and decrypt"
933
 
                           " passwords from a Mandos server" };
934
 
      ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
935
 
      if(ret == ARGP_ERR_UNKNOWN){
936
 
        fprintf(stderr, "Unknown error while parsing arguments\n");
937
 
        exitcode = EXIT_FAILURE;
938
 
        goto end;
939
 
      }
940
 
    }
941
 
    
942
 
    /* If the interface is down, bring it up */
943
 
    {
944
 
      // Lower kernel loglevel to KERN_NOTICE to avoid
945
 
      // KERN_INFO messages to mess up the prompt
946
 
      ret = klogctl(8, NULL, 5);
947
 
      if(ret == -1){
948
 
        perror("klogctl");
949
 
      }
950
 
 
951
 
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
952
 
      if(sd < 0) {
953
 
        perror("socket");
954
 
        exitcode = EXIT_FAILURE;
955
 
        ret = klogctl(7, NULL, 0);
956
 
        if(ret == -1){
957
 
          perror("klogctl");
958
 
        }
959
 
        goto end;
960
 
      }
961
 
      strcpy(network.ifr_name, interface);
962
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
963
 
      if(ret == -1){
964
 
        perror("ioctl SIOCGIFFLAGS");
965
 
        ret = klogctl(7, NULL, 0);
966
 
        if(ret == -1){
967
 
          perror("klogctl");
968
 
        }
969
 
        exitcode = EXIT_FAILURE;
970
 
        goto end;
971
 
      }
972
 
      if((network.ifr_flags & IFF_UP) == 0){
973
 
        network.ifr_flags |= IFF_UP;
974
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
975
 
        if(ret == -1){
976
 
          perror("ioctl SIOCSIFFLAGS");
977
 
          exitcode = EXIT_FAILURE;
 
874
  AvahiSServiceBrowser *sb = NULL;
 
875
  int error;
 
876
  int ret;
 
877
  intmax_t tmpmax;
 
878
  int numchars;
 
879
  int exitcode = EXIT_SUCCESS;
 
880
  const char *interface = "eth0";
 
881
  struct ifreq network;
 
882
  int sd;
 
883
  uid_t uid;
 
884
  gid_t gid;
 
885
  char *connect_to = NULL;
 
886
  char tempdir[] = "/tmp/mandosXXXXXX";
 
887
  bool tempdir_created = false;
 
888
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
 
889
  const char *seckey = PATHDIR "/" SECKEY;
 
890
  const char *pubkey = PATHDIR "/" PUBKEY;
 
891
  
 
892
  mandos_context mc = { .simple_poll = NULL, .server = NULL,
 
893
                        .dh_bits = 1024, .priority = "SECURE256"
 
894
                        ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
895
  bool gnutls_initialized = false;
 
896
  bool gpgme_initialized = false;
 
897
  double delay = 2.5;
 
898
  
 
899
  {
 
900
    struct argp_option options[] = {
 
901
      { .name = "debug", .key = 128,
 
902
        .doc = "Debug mode", .group = 3 },
 
903
      { .name = "connect", .key = 'c',
 
904
        .arg = "ADDRESS:PORT",
 
905
        .doc = "Connect directly to a specific Mandos server",
 
906
        .group = 1 },
 
907
      { .name = "interface", .key = 'i',
 
908
        .arg = "NAME",
 
909
        .doc = "Network interface that will be used to search for"
 
910
        " Mandos servers",
 
911
        .group = 1 },
 
912
      { .name = "seckey", .key = 's',
 
913
        .arg = "FILE",
 
914
        .doc = "OpenPGP secret key file base name",
 
915
        .group = 1 },
 
916
      { .name = "pubkey", .key = 'p',
 
917
        .arg = "FILE",
 
918
        .doc = "OpenPGP public key file base name",
 
919
        .group = 2 },
 
920
      { .name = "dh-bits", .key = 129,
 
921
        .arg = "BITS",
 
922
        .doc = "Bit length of the prime number used in the"
 
923
        " Diffie-Hellman key exchange",
 
924
        .group = 2 },
 
925
      { .name = "priority", .key = 130,
 
926
        .arg = "STRING",
 
927
        .doc = "GnuTLS priority string for the TLS handshake",
 
928
        .group = 1 },
 
929
      { .name = "delay", .key = 131,
 
930
        .arg = "SECONDS",
 
931
        .doc = "Maximum delay to wait for interface startup",
 
932
        .group = 2 },
 
933
      { .name = NULL }
 
934
    };
 
935
    
 
936
    error_t parse_opt(int key, char *arg,
 
937
                      struct argp_state *state){
 
938
      switch(key){
 
939
      case 128:                 /* --debug */
 
940
        debug = true;
 
941
        break;
 
942
      case 'c':                 /* --connect */
 
943
        connect_to = arg;
 
944
        break;
 
945
      case 'i':                 /* --interface */
 
946
        interface = arg;
 
947
        break;
 
948
      case 's':                 /* --seckey */
 
949
        seckey = arg;
 
950
        break;
 
951
      case 'p':                 /* --pubkey */
 
952
        pubkey = arg;
 
953
        break;
 
954
      case 129:                 /* --dh-bits */
 
955
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
956
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
 
957
           or arg[numchars] != '\0'){
 
958
          fprintf(stderr, "Bad number of DH bits\n");
 
959
          exit(EXIT_FAILURE);
 
960
        }
 
961
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
 
962
        break;
 
963
      case 130:                 /* --priority */
 
964
        mc.priority = arg;
 
965
        break;
 
966
      case 131:                 /* --delay */
 
967
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
 
968
        if(ret < 1 or arg[numchars] != '\0'){
 
969
          fprintf(stderr, "Bad delay\n");
 
970
          exit(EXIT_FAILURE);
 
971
        }
 
972
        break;
 
973
      case ARGP_KEY_ARG:
 
974
        argp_usage(state);
 
975
      case ARGP_KEY_END:
 
976
        break;
 
977
      default:
 
978
        return ARGP_ERR_UNKNOWN;
 
979
      }
 
980
      return 0;
 
981
    }
 
982
    
 
983
    struct argp argp = { .options = options, .parser = parse_opt,
 
984
                         .args_doc = "",
 
985
                         .doc = "Mandos client -- Get and decrypt"
 
986
                         " passwords from a Mandos server" };
 
987
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
988
    if(ret == ARGP_ERR_UNKNOWN){
 
989
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
990
      exitcode = EXIT_FAILURE;
 
991
      goto end;
 
992
    }
 
993
  }
 
994
  
 
995
  /* If the interface is down, bring it up */
 
996
  if(interface[0] != '\0'){
 
997
#ifdef __linux__
 
998
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
999
       messages to mess up the prompt */
 
1000
    ret = klogctl(8, NULL, 5);
 
1001
    bool restore_loglevel = true;
 
1002
    if(ret == -1){
 
1003
      restore_loglevel = false;
 
1004
      perror("klogctl");
 
1005
    }
 
1006
#endif
 
1007
    
 
1008
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1009
    if(sd < 0){
 
1010
      perror("socket");
 
1011
      exitcode = EXIT_FAILURE;
 
1012
#ifdef __linux__
 
1013
      if(restore_loglevel){
 
1014
        ret = klogctl(7, NULL, 0);
 
1015
        if(ret == -1){
 
1016
          perror("klogctl");
 
1017
        }
 
1018
      }
 
1019
#endif
 
1020
      goto end;
 
1021
    }
 
1022
    strcpy(network.ifr_name, interface);
 
1023
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1024
    if(ret == -1){
 
1025
      perror("ioctl SIOCGIFFLAGS");
 
1026
#ifdef __linux__
 
1027
      if(restore_loglevel){
 
1028
        ret = klogctl(7, NULL, 0);
 
1029
        if(ret == -1){
 
1030
          perror("klogctl");
 
1031
        }
 
1032
      }
 
1033
#endif
 
1034
      exitcode = EXIT_FAILURE;
 
1035
      goto end;
 
1036
    }
 
1037
    if((network.ifr_flags & IFF_UP) == 0){
 
1038
      network.ifr_flags |= IFF_UP;
 
1039
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1040
      if(ret == -1){
 
1041
        perror("ioctl SIOCSIFFLAGS");
 
1042
        exitcode = EXIT_FAILURE;
 
1043
#ifdef __linux__
 
1044
        if(restore_loglevel){
978
1045
          ret = klogctl(7, NULL, 0);
979
1046
          if(ret == -1){
980
1047
            perror("klogctl");
981
1048
          }
982
 
          goto end;
983
 
        }
984
 
      }
985
 
      // sleep checking until interface is running
986
 
      for(int i=0; i < delay * 4; i++){
987
 
        ret = ioctl(sd, SIOCGIFFLAGS, &network);
988
 
        if(ret == -1){
989
 
          perror("ioctl SIOCGIFFLAGS");
990
 
        } else if(network.ifr_flags & IFF_RUNNING){
991
 
          break;
992
 
        }
993
 
        struct timespec sleeptime = { .tv_nsec = 250000000 };
994
 
        nanosleep(&sleeptime, NULL);
995
 
      }
996
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1049
        }
 
1050
#endif
 
1051
        goto end;
 
1052
      }
 
1053
    }
 
1054
    /* sleep checking until interface is running */
 
1055
    for(int i=0; i < delay * 4; i++){
 
1056
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
997
1057
      if(ret == -1){
998
 
        perror("close");
999
 
      }
1000
 
      // Restores kernel loglevel to default
 
1058
        perror("ioctl SIOCGIFFLAGS");
 
1059
      } else if(network.ifr_flags & IFF_RUNNING){
 
1060
        break;
 
1061
      }
 
1062
      struct timespec sleeptime = { .tv_nsec = 250000000 };
 
1063
      ret = nanosleep(&sleeptime, NULL);
 
1064
      if(ret == -1 and errno != EINTR){
 
1065
        perror("nanosleep");
 
1066
      }
 
1067
    }
 
1068
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1069
    if(ret == -1){
 
1070
      perror("close");
 
1071
    }
 
1072
#ifdef __linux__
 
1073
    if(restore_loglevel){
 
1074
      /* Restores kernel loglevel to default */
1001
1075
      ret = klogctl(7, NULL, 0);
1002
1076
      if(ret == -1){
1003
1077
        perror("klogctl");
1004
1078
      }
1005
1079
    }
1006
 
    
1007
 
    uid = getuid();
1008
 
    gid = getgid();
1009
 
    
1010
 
    ret = setuid(uid);
1011
 
    if(ret == -1){
1012
 
      perror("setuid");
1013
 
    }
1014
 
    
1015
 
    setgid(gid);
1016
 
    if(ret == -1){
1017
 
      perror("setgid");
1018
 
    }
1019
 
    
1020
 
    ret = init_gnutls_global(&mc, pubkey, seckey);
1021
 
    if(ret == -1){
1022
 
      fprintf(stderr, "init_gnutls_global failed\n");
1023
 
      exitcode = EXIT_FAILURE;
1024
 
      goto end;
1025
 
    } else {
1026
 
      gnutls_initalized = true;
1027
 
    }
1028
 
    
1029
 
    if(mkdtemp(tempdir) == NULL){
1030
 
      perror("mkdtemp");
1031
 
      tempdir[0] = '\0';
1032
 
      goto end;
1033
 
    }
1034
 
    
1035
 
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
1036
 
      fprintf(stderr, "gpgme_initalized failed\n");
1037
 
      exitcode = EXIT_FAILURE;
1038
 
      goto end;
1039
 
    } else {
1040
 
      gpgme_initalized = true;
1041
 
    }
1042
 
    
 
1080
#endif
 
1081
  }
 
1082
  
 
1083
  uid = getuid();
 
1084
  gid = getgid();
 
1085
  
 
1086
  errno = 0;
 
1087
  setgid(gid);
 
1088
  if(ret == -1){
 
1089
    perror("setgid");
 
1090
  }
 
1091
  
 
1092
  ret = setuid(uid);
 
1093
  if(ret == -1){
 
1094
    perror("setuid");
 
1095
  }
 
1096
  
 
1097
  ret = init_gnutls_global(&mc, pubkey, seckey);
 
1098
  if(ret == -1){
 
1099
    fprintf(stderr, "init_gnutls_global failed\n");
 
1100
    exitcode = EXIT_FAILURE;
 
1101
    goto end;
 
1102
  } else {
 
1103
    gnutls_initialized = true;
 
1104
  }
 
1105
  
 
1106
  if(mkdtemp(tempdir) == NULL){
 
1107
    perror("mkdtemp");
 
1108
    goto end;
 
1109
  }
 
1110
  tempdir_created = true;
 
1111
  
 
1112
  if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
 
1113
    fprintf(stderr, "init_gpgme failed\n");
 
1114
    exitcode = EXIT_FAILURE;
 
1115
    goto end;
 
1116
  } else {
 
1117
    gpgme_initialized = true;
 
1118
  }
 
1119
  
 
1120
  if(interface[0] != '\0'){
1043
1121
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1044
1122
    if(if_index == 0){
1045
1123
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1046
 
      exit(EXIT_FAILURE);
1047
 
    }
1048
 
    
1049
 
    if(connect_to != NULL){
1050
 
      /* Connect directly, do not use Zeroconf */
1051
 
      /* (Mainly meant for debugging) */
1052
 
      char *address = strrchr(connect_to, ':');
1053
 
      if(address == NULL){
1054
 
        fprintf(stderr, "No colon in address\n");
1055
 
        exitcode = EXIT_FAILURE;
1056
 
        goto end;
1057
 
      }
1058
 
      uint16_t port;
1059
 
      ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1060
 
      if(ret < 1 or tmpmax != (uint16_t)tmpmax
1061
 
         or address[numchars+1] != '\0'){
1062
 
        fprintf(stderr, "Bad port number\n");
1063
 
        exitcode = EXIT_FAILURE;
1064
 
        goto end;
1065
 
      }
1066
 
      port = (uint16_t)tmpmax;
1067
 
      *address = '\0';
1068
 
      address = connect_to;
1069
 
      ret = start_mandos_communication(address, port, if_index, &mc);
1070
 
      if(ret < 0){
1071
 
        exitcode = EXIT_FAILURE;
1072
 
      } else {
1073
 
        exitcode = EXIT_SUCCESS;
1074
 
      }
1075
 
      goto end;
1076
 
    }
1077
 
    
1078
 
    if(not debug){
1079
 
      avahi_set_log_function(empty_log);
1080
 
    }
1081
 
    
1082
 
    /* Initialize the pseudo-RNG for Avahi */
1083
 
    srand((unsigned int) time(NULL));
1084
 
    
1085
 
    /* Allocate main Avahi loop object */
1086
 
    mc.simple_poll = avahi_simple_poll_new();
1087
 
    if(mc.simple_poll == NULL) {
1088
 
        fprintf(stderr, "Avahi: Failed to create simple poll"
1089
 
                " object.\n");
1090
 
        exitcode = EXIT_FAILURE;
1091
 
        goto end;
1092
 
    }
1093
 
    
1094
 
    {
1095
 
      AvahiServerConfig config;
1096
 
      /* Do not publish any local Zeroconf records */
1097
 
      avahi_server_config_init(&config);
1098
 
      config.publish_hinfo = 0;
1099
 
      config.publish_addresses = 0;
1100
 
      config.publish_workstation = 0;
1101
 
      config.publish_domain = 0;
1102
 
      
1103
 
      /* Allocate a new server */
1104
 
      mc.server = avahi_server_new(avahi_simple_poll_get
1105
 
                                   (mc.simple_poll), &config, NULL,
1106
 
                                   NULL, &error);
1107
 
      
1108
 
      /* Free the Avahi configuration data */
1109
 
      avahi_server_config_free(&config);
1110
 
    }
1111
 
    
1112
 
    /* Check if creating the Avahi server object succeeded */
1113
 
    if(mc.server == NULL) {
1114
 
        fprintf(stderr, "Failed to create Avahi server: %s\n",
1115
 
                avahi_strerror(error));
1116
 
        exitcode = EXIT_FAILURE;
1117
 
        goto end;
1118
 
    }
1119
 
    
1120
 
    /* Create the Avahi service browser */
1121
 
    sb = avahi_s_service_browser_new(mc.server, if_index,
1122
 
                                     AVAHI_PROTO_INET6,
1123
 
                                     "_mandos._tcp", NULL, 0,
1124
 
                                     browse_callback, &mc);
1125
 
    if(sb == NULL) {
1126
 
        fprintf(stderr, "Failed to create service browser: %s\n",
1127
 
                avahi_strerror(avahi_server_errno(mc.server)));
1128
 
        exitcode = EXIT_FAILURE;
1129
 
        goto end;
1130
 
    }
1131
 
    
1132
 
    /* Run the main loop */
1133
 
    
1134
 
    if(debug){
1135
 
      fprintf(stderr, "Starting Avahi loop search\n");
1136
 
    }
1137
 
 
1138
 
    avahi_simple_poll_loop(mc.simple_poll);
1139
 
    
 
1124
      exitcode = EXIT_FAILURE;
 
1125
      goto end;
 
1126
    }
 
1127
  }
 
1128
  
 
1129
  if(connect_to != NULL){
 
1130
    /* Connect directly, do not use Zeroconf */
 
1131
    /* (Mainly meant for debugging) */
 
1132
    char *address = strrchr(connect_to, ':');
 
1133
    if(address == NULL){
 
1134
      fprintf(stderr, "No colon in address\n");
 
1135
      exitcode = EXIT_FAILURE;
 
1136
      goto end;
 
1137
    }
 
1138
    uint16_t port;
 
1139
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
1140
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
 
1141
       or address[numchars+1] != '\0'){
 
1142
      fprintf(stderr, "Bad port number\n");
 
1143
      exitcode = EXIT_FAILURE;
 
1144
      goto end;
 
1145
    }
 
1146
    port = (uint16_t)tmpmax;
 
1147
    *address = '\0';
 
1148
    address = connect_to;
 
1149
    /* Colon in address indicates IPv6 */
 
1150
    int af;
 
1151
    if(strchr(address, ':') != NULL){
 
1152
      af = AF_INET6;
 
1153
    } else {
 
1154
      af = AF_INET;
 
1155
    }
 
1156
    ret = start_mandos_communication(address, port, if_index, &mc,
 
1157
                                     af);
 
1158
    if(ret < 0){
 
1159
      exitcode = EXIT_FAILURE;
 
1160
    } else {
 
1161
      exitcode = EXIT_SUCCESS;
 
1162
    }
 
1163
    goto end;
 
1164
  }
 
1165
  
 
1166
  if(not debug){
 
1167
    avahi_set_log_function(empty_log);
 
1168
  }
 
1169
  
 
1170
  /* Initialize the pseudo-RNG for Avahi */
 
1171
  srand((unsigned int) time(NULL));
 
1172
  
 
1173
  /* Allocate main Avahi loop object */
 
1174
  mc.simple_poll = avahi_simple_poll_new();
 
1175
  if(mc.simple_poll == NULL){
 
1176
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
 
1177
    exitcode = EXIT_FAILURE;
 
1178
    goto end;
 
1179
  }
 
1180
  
 
1181
  {
 
1182
    AvahiServerConfig config;
 
1183
    /* Do not publish any local Zeroconf records */
 
1184
    avahi_server_config_init(&config);
 
1185
    config.publish_hinfo = 0;
 
1186
    config.publish_addresses = 0;
 
1187
    config.publish_workstation = 0;
 
1188
    config.publish_domain = 0;
 
1189
    
 
1190
    /* Allocate a new server */
 
1191
    mc.server = avahi_server_new(avahi_simple_poll_get
 
1192
                                 (mc.simple_poll), &config, NULL,
 
1193
                                 NULL, &error);
 
1194
    
 
1195
    /* Free the Avahi configuration data */
 
1196
    avahi_server_config_free(&config);
 
1197
  }
 
1198
  
 
1199
  /* Check if creating the Avahi server object succeeded */
 
1200
  if(mc.server == NULL){
 
1201
    fprintf(stderr, "Failed to create Avahi server: %s\n",
 
1202
            avahi_strerror(error));
 
1203
    exitcode = EXIT_FAILURE;
 
1204
    goto end;
 
1205
  }
 
1206
  
 
1207
  /* Create the Avahi service browser */
 
1208
  sb = avahi_s_service_browser_new(mc.server, if_index,
 
1209
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
 
1210
                                   NULL, 0, browse_callback, &mc);
 
1211
  if(sb == NULL){
 
1212
    fprintf(stderr, "Failed to create service browser: %s\n",
 
1213
            avahi_strerror(avahi_server_errno(mc.server)));
 
1214
    exitcode = EXIT_FAILURE;
 
1215
    goto end;
 
1216
  }
 
1217
  
 
1218
  /* Run the main loop */
 
1219
  
 
1220
  if(debug){
 
1221
    fprintf(stderr, "Starting Avahi loop search\n");
 
1222
  }
 
1223
  
 
1224
  avahi_simple_poll_loop(mc.simple_poll);
 
1225
  
1140
1226
 end:
1141
 
    
1142
 
    if(debug){
1143
 
      fprintf(stderr, "%s exiting\n", argv[0]);
1144
 
    }
1145
 
    
1146
 
    /* Cleanup things */
1147
 
    if(sb != NULL)
1148
 
        avahi_s_service_browser_free(sb);
1149
 
    
1150
 
    if(mc.server != NULL)
1151
 
        avahi_server_free(mc.server);
1152
 
    
1153
 
    if(mc.simple_poll != NULL)
1154
 
        avahi_simple_poll_free(mc.simple_poll);
1155
 
    
1156
 
    if(gnutls_initalized){
1157
 
      gnutls_certificate_free_credentials(mc.cred);
1158
 
      gnutls_global_deinit();
1159
 
      gnutls_dh_params_deinit(mc.dh_params);
1160
 
    }
1161
 
    
1162
 
    if(gpgme_initalized){
1163
 
      gpgme_release(mc.ctx);
1164
 
    }
1165
 
    
1166
 
    /* Removes the temp directory used by GPGME */
1167
 
    if(tempdir[0] != '\0'){
1168
 
      DIR *d;
1169
 
      struct dirent *direntry;
1170
 
      d = opendir(tempdir);
1171
 
      if(d == NULL){
1172
 
        if(errno != ENOENT){
1173
 
          perror("opendir");
1174
 
        }
1175
 
      } else {
1176
 
        while(true){
1177
 
          direntry = readdir(d);
1178
 
          if(direntry == NULL){
1179
 
            break;
1180
 
          }
1181
 
          if(direntry->d_type == DT_REG){
1182
 
            char *fullname = NULL;
1183
 
            ret = asprintf(&fullname, "%s/%s", tempdir,
1184
 
                           direntry->d_name);
1185
 
            if(ret < 0){
1186
 
              perror("asprintf");
1187
 
              continue;
1188
 
            }
1189
 
            ret = unlink(fullname);
1190
 
            if(ret == -1){
1191
 
              fprintf(stderr, "unlink(\"%s\"): %s",
1192
 
                      fullname, strerror(errno));
1193
 
            }
1194
 
            free(fullname);
1195
 
          }
1196
 
        }
1197
 
        closedir(d);
1198
 
      }
1199
 
      ret = rmdir(tempdir);
1200
 
      if(ret == -1 and errno != ENOENT){
1201
 
        perror("rmdir");
1202
 
      }
1203
 
    }
1204
 
          
1205
 
    return exitcode;
 
1227
  
 
1228
  if(debug){
 
1229
    fprintf(stderr, "%s exiting\n", argv[0]);
 
1230
  }
 
1231
  
 
1232
  /* Cleanup things */
 
1233
  if(sb != NULL)
 
1234
    avahi_s_service_browser_free(sb);
 
1235
  
 
1236
  if(mc.server != NULL)
 
1237
    avahi_server_free(mc.server);
 
1238
  
 
1239
  if(mc.simple_poll != NULL)
 
1240
    avahi_simple_poll_free(mc.simple_poll);
 
1241
  
 
1242
  if(gnutls_initialized){
 
1243
    gnutls_certificate_free_credentials(mc.cred);
 
1244
    gnutls_global_deinit();
 
1245
    gnutls_dh_params_deinit(mc.dh_params);
 
1246
  }
 
1247
  
 
1248
  if(gpgme_initialized){
 
1249
    gpgme_release(mc.ctx);
 
1250
  }
 
1251
  
 
1252
  /* Removes the temp directory used by GPGME */
 
1253
  if(tempdir_created){
 
1254
    DIR *d;
 
1255
    struct dirent *direntry;
 
1256
    d = opendir(tempdir);
 
1257
    if(d == NULL){
 
1258
      if(errno != ENOENT){
 
1259
        perror("opendir");
 
1260
      }
 
1261
    } else {
 
1262
      while(true){
 
1263
        direntry = readdir(d);
 
1264
        if(direntry == NULL){
 
1265
          break;
 
1266
        }
 
1267
        /* Skip "." and ".." */
 
1268
        if(direntry->d_name[0] == '.'
 
1269
           and (direntry->d_name[1] == '\0'
 
1270
                or (direntry->d_name[1] == '.'
 
1271
                    and direntry->d_name[2] == '\0'))){
 
1272
          continue;
 
1273
        }
 
1274
        char *fullname = NULL;
 
1275
        ret = asprintf(&fullname, "%s/%s", tempdir,
 
1276
                       direntry->d_name);
 
1277
        if(ret < 0){
 
1278
          perror("asprintf");
 
1279
          continue;
 
1280
        }
 
1281
        ret = remove(fullname);
 
1282
        if(ret == -1){
 
1283
          fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
 
1284
                  strerror(errno));
 
1285
        }
 
1286
        free(fullname);
 
1287
      }
 
1288
      closedir(d);
 
1289
    }
 
1290
    ret = rmdir(tempdir);
 
1291
    if(ret == -1 and errno != ENOENT){
 
1292
      perror("rmdir");
 
1293
    }
 
1294
  }
 
1295
  
 
1296
  return exitcode;
1206
1297
}