/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-01-31 10:33:17 UTC
  • mfrom: (24.1.129 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20090131103317-wzqvyr532sjcjt7u
Merge from Björn:

* mandos-ctl: New option "--remove-client".  Only default to listing
              clients if no clients were given on the command line.
* plugins.d/mandos-client.c: Lower kernel log level while bringing up
                             network interface.  New option "--delay"
                             to control the maximum delay to wait for
                             running interface.
* plugins.d/mandos-client.xml (SYNOPSIS, OPTIONS): New option
                                                   "--delay".

Show diffs side-by-side

added added

removed removed

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