/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

* initramfs-tools-hook-conf: Security bug fix: Add code to handle
                             being called by "mkinitramfs-kpkg"
                             instead of "update-initramfs".

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2011 Teddy Hogeborn
13
 
 * Copyright © 2008-2011 Björn Påhlsson
 
12
 * Copyright © 2008,2009 Teddy Hogeborn
 
13
 * Copyright © 2008,2009 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
26
26
 * along with this program.  If not, see
27
27
 * <http://www.gnu.org/licenses/>.
28
28
 * 
29
 
 * Contact the authors at <mandos@recompile.se>.
 
29
 * Contact the authors at <mandos@fukt.bsnet.se>.
30
30
 */
31
31
 
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
43
43
                                   stdout, ferror(), remove() */
44
44
#include <stdint.h>             /* uint16_t, uint32_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
 
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
 
                                   strtof(), abort() */
 
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
 
47
                                   srand(), strtof() */
48
48
#include <stdbool.h>            /* bool, false, true */
49
49
#include <string.h>             /* memset(), strcmp(), strlen(),
50
50
                                   strerror(), asprintf(), strcpy() */
53
53
                                   sockaddr_in6, PF_INET6,
54
54
                                   SOCK_STREAM, uid_t, gid_t, open(),
55
55
                                   opendir(), DIR */
56
 
#include <sys/stat.h>           /* open(), S_ISREG */
 
56
#include <sys/stat.h>           /* open() */
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
58
                                   inet_pton(), connect() */
59
59
#include <fcntl.h>              /* open() */
62
62
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
63
                                   strtoimax() */
64
64
#include <assert.h>             /* assert() */
65
 
#include <errno.h>              /* perror(), errno,
66
 
                                   program_invocation_short_name */
67
 
#include <time.h>               /* nanosleep(), time(), sleep() */
 
65
#include <errno.h>              /* perror(), errno */
 
66
#include <time.h>               /* nanosleep(), time() */
68
67
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
69
68
                                   SIOCSIFFLAGS, if_indextoname(),
70
69
                                   if_nametoindex(), IF_NAMESIZE */
72
71
                                   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
73
72
                                */
74
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
 
                                   getuid(), getgid(), seteuid(),
76
 
                                   setgid(), pause() */
77
 
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
 
74
                                   getuid(), getgid(), setuid(),
 
75
                                   setgid() */
 
76
#include <arpa/inet.h>          /* inet_pton(), htons */
78
77
#include <iso646.h>             /* not, or, and */
79
78
#include <argp.h>               /* struct argp_option, error_t, struct
80
79
                                   argp_state, struct argp,
81
80
                                   argp_parse(), ARGP_KEY_ARG,
82
81
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
83
82
#include <signal.h>             /* sigemptyset(), sigaddset(),
84
 
                                   sigaction(), SIGTERM, sig_atomic_t,
85
 
                                   raise() */
86
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
87
 
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
 
83
                                   sigaction(), SIGTERM, sigaction,
 
84
                                   sig_atomic_t */
88
85
 
89
86
#ifdef __linux__
90
87
#include <sys/klog.h>           /* klogctl() */
123
120
#define PATHDIR "/conf/conf.d/mandos"
124
121
#define SECKEY "seckey.txt"
125
122
#define PUBKEY "pubkey.txt"
126
 
#define HOOKDIR "/lib/mandos/network-hooks.d"
127
123
 
128
124
bool debug = false;
129
125
static const char mandos_protocol_version[] = "1";
130
126
const char *argp_program_version = "mandos-client " VERSION;
131
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
132
 
static const char sys_class_net[] = "/sys/class/net";
133
 
char *connect_to = NULL;
134
 
 
135
 
/* Doubly linked list that need to be circularly linked when used */
136
 
typedef struct server{
137
 
  const char *ip;
138
 
  uint16_t port;
139
 
  AvahiIfIndex if_index;
140
 
  int af;
141
 
  struct timespec last_seen;
142
 
  struct server *next;
143
 
  struct server *prev;
144
 
} server;
 
127
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
145
128
 
146
129
/* Used for passing in values through the Avahi callback functions */
147
130
typedef struct {
152
135
  gnutls_dh_params_t dh_params;
153
136
  const char *priority;
154
137
  gpgme_ctx_t ctx;
155
 
  server *current_server;
156
138
} mandos_context;
157
139
 
158
140
/* global context so signal handler can reach it*/
159
141
mandos_context mc = { .simple_poll = NULL, .server = NULL,
160
142
                      .dh_bits = 1024, .priority = "SECURE256"
161
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
162
 
                      .current_server = NULL };
163
 
 
164
 
sig_atomic_t quit_now = 0;
165
 
int signal_received = 0;
166
 
 
167
 
/* Function to use when printing errors */
168
 
void perror_plus(const char *print_text){
169
 
  fprintf(stderr, "Mandos plugin %s: ",
170
 
          program_invocation_short_name);
171
 
  perror(print_text);
172
 
}
 
143
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
173
144
 
174
145
/*
175
146
 * Make additional room in "buffer" for at least BUFFER_SIZE more
188
159
  return buffer_capacity;
189
160
}
190
161
 
191
 
/* Add server to set of servers to retry periodically */
192
 
int add_server(const char *ip, uint16_t port,
193
 
                 AvahiIfIndex if_index,
194
 
                 int af){
195
 
  int ret;
196
 
  server *new_server = malloc(sizeof(server));
197
 
  if(new_server == NULL){
198
 
    perror_plus("malloc");
199
 
    return -1;
200
 
  }
201
 
  *new_server = (server){ .ip = strdup(ip),
202
 
                         .port = port,
203
 
                         .if_index = if_index,
204
 
                         .af = af };
205
 
  if(new_server->ip == NULL){
206
 
    perror_plus("strdup");
207
 
    return -1;
208
 
  }
209
 
  /* Special case of first server */
210
 
  if (mc.current_server == NULL){
211
 
    new_server->next = new_server;
212
 
    new_server->prev = new_server;
213
 
    mc.current_server = new_server;
214
 
  /* Place the new server last in the list */
215
 
  } else {
216
 
    new_server->next = mc.current_server;
217
 
    new_server->prev = mc.current_server->prev;
218
 
    new_server->prev->next = new_server;
219
 
    mc.current_server->prev = new_server;
220
 
  }
221
 
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
222
 
  if(ret == -1){
223
 
    perror_plus("clock_gettime");
224
 
    return -1;
225
 
  }
226
 
  return 0;
227
 
}
228
 
 
229
162
/* 
230
163
 * Initialize GPGME.
231
164
 */
232
165
static bool init_gpgme(const char *seckey,
233
166
                       const char *pubkey, const char *tempdir){
 
167
  int ret;
234
168
  gpgme_error_t rc;
235
169
  gpgme_engine_info_t engine_info;
236
170
  
239
173
   * Helper function to insert pub and seckey to the engine keyring.
240
174
   */
241
175
  bool import_key(const char *filename){
242
 
    int ret;
243
176
    int fd;
244
177
    gpgme_data_t pgp_data;
245
178
    
246
179
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
247
180
    if(fd == -1){
248
 
      perror_plus("open");
 
181
      perror("open");
249
182
      return false;
250
183
    }
251
184
    
265
198
    
266
199
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
267
200
    if(ret == -1){
268
 
      perror_plus("close");
 
201
      perror("close");
269
202
    }
270
203
    gpgme_data_release(pgp_data);
271
204
    return true;
284
217
    return false;
285
218
  }
286
219
  
287
 
  /* Set GPGME home directory for the OpenPGP engine only */
 
220
    /* Set GPGME home directory for the OpenPGP engine only */
288
221
  rc = gpgme_get_engine_info(&engine_info);
289
222
  if(rc != GPG_ERR_NO_ERROR){
290
223
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
316
249
    return false;
317
250
  }
318
251
  
319
 
  return true;
 
252
  return true; 
320
253
}
321
254
 
322
255
/* 
376
309
        }
377
310
        gpgme_recipient_t recipient;
378
311
        recipient = result->recipients;
379
 
        while(recipient != NULL){
380
 
          fprintf(stderr, "Public key algorithm: %s\n",
381
 
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
382
 
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
383
 
          fprintf(stderr, "Secret key available: %s\n",
384
 
                  recipient->status == GPG_ERR_NO_SECKEY
385
 
                  ? "No" : "Yes");
386
 
          recipient = recipient->next;
 
312
        if(recipient){
 
313
          while(recipient != NULL){
 
314
            fprintf(stderr, "Public key algorithm: %s\n",
 
315
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
316
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
317
            fprintf(stderr, "Secret key available: %s\n",
 
318
                    recipient->status == GPG_ERR_NO_SECKEY
 
319
                    ? "No" : "Yes");
 
320
            recipient = recipient->next;
 
321
          }
387
322
        }
388
323
      }
389
324
    }
396
331
  
397
332
  /* Seek back to the beginning of the GPGME plaintext data buffer */
398
333
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
399
 
    perror_plus("gpgme_data_seek");
 
334
    perror("gpgme_data_seek");
400
335
    plaintext_length = -1;
401
336
    goto decrypt_end;
402
337
  }
407
342
                                      (size_t)plaintext_length,
408
343
                                      plaintext_capacity);
409
344
    if(plaintext_capacity == 0){
410
 
        perror_plus("incbuffer");
 
345
        perror("incbuffer");
411
346
        plaintext_length = -1;
412
347
        goto decrypt_end;
413
348
    }
420
355
      break;
421
356
    }
422
357
    if(ret < 0){
423
 
      perror_plus("gpgme_data_read");
 
358
      perror("gpgme_data_read");
424
359
      plaintext_length = -1;
425
360
      goto decrypt_end;
426
361
    }
483
418
  }
484
419
  
485
420
  /* OpenPGP credentials */
486
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
421
  gnutls_certificate_allocate_credentials(&mc.cred);
487
422
  if(ret != GNUTLS_E_SUCCESS){
488
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
423
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
424
                                                    from
 
425
                                                    -Wunreachable-code
 
426
                                                 */
489
427
            safer_gnutls_strerror(ret));
490
428
    gnutls_global_deinit();
491
429
    return -1;
538
476
static int init_gnutls_session(gnutls_session_t *session){
539
477
  int ret;
540
478
  /* GnuTLS session creation */
541
 
  do {
542
 
    ret = gnutls_init(session, GNUTLS_SERVER);
543
 
    if(quit_now){
544
 
      return -1;
545
 
    }
546
 
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
479
  ret = gnutls_init(session, GNUTLS_SERVER);
547
480
  if(ret != GNUTLS_E_SUCCESS){
548
481
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
549
482
            safer_gnutls_strerror(ret));
551
484
  
552
485
  {
553
486
    const char *err;
554
 
    do {
555
 
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
556
 
      if(quit_now){
557
 
        gnutls_deinit(*session);
558
 
        return -1;
559
 
      }
560
 
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
487
    ret = gnutls_priority_set_direct(*session, mc.priority, &err);
561
488
    if(ret != GNUTLS_E_SUCCESS){
562
489
      fprintf(stderr, "Syntax error at: %s\n", err);
563
490
      fprintf(stderr, "GnuTLS error: %s\n",
567
494
    }
568
495
  }
569
496
  
570
 
  do {
571
 
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
572
 
                                 mc.cred);
573
 
    if(quit_now){
574
 
      gnutls_deinit(*session);
575
 
      return -1;
576
 
    }
577
 
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
 
497
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
 
498
                               mc.cred);
578
499
  if(ret != GNUTLS_E_SUCCESS){
579
500
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
580
501
            safer_gnutls_strerror(ret));
583
504
  }
584
505
  
585
506
  /* ignore client certificate if any. */
586
 
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
 
507
  gnutls_certificate_server_set_request(*session,
 
508
                                        GNUTLS_CERT_IGNORE);
587
509
  
588
510
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
589
511
  
598
520
static int start_mandos_communication(const char *ip, uint16_t port,
599
521
                                      AvahiIfIndex if_index,
600
522
                                      int af){
601
 
  int ret, tcp_sd = -1;
 
523
  int ret, tcp_sd;
602
524
  ssize_t sret;
603
525
  union {
604
526
    struct sockaddr_in in;
605
527
    struct sockaddr_in6 in6;
606
528
  } to;
607
529
  char *buffer = NULL;
608
 
  char *decrypted_buffer = NULL;
 
530
  char *decrypted_buffer;
609
531
  size_t buffer_length = 0;
610
532
  size_t buffer_capacity = 0;
 
533
  ssize_t decrypted_buffer_size;
611
534
  size_t written;
612
 
  int retval = -1;
 
535
  int retval = 0;
613
536
  gnutls_session_t session;
614
537
  int pf;                       /* Protocol family */
615
538
  
616
 
  errno = 0;
617
 
  
618
 
  if(quit_now){
619
 
    errno = EINTR;
620
 
    return -1;
621
 
  }
622
 
  
623
539
  switch(af){
624
540
  case AF_INET6:
625
541
    pf = PF_INET6;
629
545
    break;
630
546
  default:
631
547
    fprintf(stderr, "Bad address family: %d\n", af);
632
 
    errno = EINVAL;
633
548
    return -1;
634
549
  }
635
550
  
645
560
  
646
561
  tcp_sd = socket(pf, SOCK_STREAM, 0);
647
562
  if(tcp_sd < 0){
648
 
    int e = errno;
649
 
    perror_plus("socket");
650
 
    errno = e;
651
 
    goto mandos_end;
652
 
  }
653
 
  
654
 
  if(quit_now){
655
 
    errno = EINTR;
656
 
    goto mandos_end;
 
563
    perror("socket");
 
564
    return -1;
657
565
  }
658
566
  
659
567
  memset(&to, 0, sizeof(to));
660
568
  if(af == AF_INET6){
661
 
    to.in6.sin6_family = (sa_family_t)af;
 
569
    to.in6.sin6_family = (uint16_t)af;
662
570
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
663
571
  } else {                      /* IPv4 */
664
572
    to.in.sin_family = (sa_family_t)af;
665
573
    ret = inet_pton(af, ip, &to.in.sin_addr);
666
574
  }
667
575
  if(ret < 0 ){
668
 
    int e = errno;
669
 
    perror_plus("inet_pton");
670
 
    errno = e;
671
 
    goto mandos_end;
 
576
    perror("inet_pton");
 
577
    return -1;
672
578
  }
673
579
  if(ret == 0){
674
 
    int e = errno;
675
580
    fprintf(stderr, "Bad address: %s\n", ip);
676
 
    errno = e;
677
 
    goto mandos_end;
 
581
    return -1;
678
582
  }
679
583
  if(af == AF_INET6){
680
584
    to.in6.sin6_port = htons(port); /* Spurious warnings from
687
591
      if(if_index == AVAHI_IF_UNSPEC){
688
592
        fprintf(stderr, "An IPv6 link-local address is incomplete"
689
593
                " without a network interface\n");
690
 
        errno = EINVAL;
691
 
        goto mandos_end;
 
594
        return -1;
692
595
      }
693
596
      /* Set the network interface number as scope */
694
597
      to.in6.sin6_scope_id = (uint32_t)if_index;
699
602
                                     -Wunreachable-code */
700
603
  }
701
604
  
702
 
  if(quit_now){
703
 
    errno = EINTR;
704
 
    goto mandos_end;
705
 
  }
706
 
  
707
605
  if(debug){
708
606
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
709
607
      char interface[IF_NAMESIZE];
710
608
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
711
 
        perror_plus("if_indextoname");
 
609
        perror("if_indextoname");
712
610
      } else {
713
611
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
714
612
                ip, interface, port);
728
626
                        sizeof(addrstr));
729
627
    }
730
628
    if(pcret == NULL){
731
 
      perror_plus("inet_ntop");
 
629
      perror("inet_ntop");
732
630
    } else {
733
631
      if(strcmp(addrstr, ip) != 0){
734
632
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
736
634
    }
737
635
  }
738
636
  
739
 
  if(quit_now){
740
 
    errno = EINTR;
741
 
    goto mandos_end;
742
 
  }
743
 
  
744
637
  if(af == AF_INET6){
745
638
    ret = connect(tcp_sd, &to.in6, sizeof(to));
746
639
  } else {
747
640
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
748
641
  }
749
642
  if(ret < 0){
750
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
751
 
      int e = errno;
752
 
      perror_plus("connect");
753
 
      errno = e;
754
 
    }
755
 
    goto mandos_end;
756
 
  }
757
 
  
758
 
  if(quit_now){
759
 
    errno = EINTR;
760
 
    goto mandos_end;
 
643
    perror("connect");
 
644
    return -1;
761
645
  }
762
646
  
763
647
  const char *out = mandos_protocol_version;
767
651
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
768
652
                                   out_size - written));
769
653
    if(ret == -1){
770
 
      int e = errno;
771
 
      perror_plus("write");
772
 
      errno = e;
 
654
      perror("write");
 
655
      retval = -1;
773
656
      goto mandos_end;
774
657
    }
775
658
    written += (size_t)ret;
783
666
        break;
784
667
      }
785
668
    }
786
 
  
787
 
    if(quit_now){
788
 
      errno = EINTR;
789
 
      goto mandos_end;
790
 
    }
791
669
  }
792
670
  
793
671
  if(debug){
794
672
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
795
673
  }
796
674
  
797
 
  if(quit_now){
798
 
    errno = EINTR;
799
 
    goto mandos_end;
800
 
  }
801
 
  
802
 
  /* Spurious warning from -Wint-to-pointer-cast */
803
675
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
804
676
  
805
 
  if(quit_now){
806
 
    errno = EINTR;
807
 
    goto mandos_end;
808
 
  }
809
 
  
810
 
  do {
 
677
  do{
811
678
    ret = gnutls_handshake(session);
812
 
    if(quit_now){
813
 
      errno = EINTR;
814
 
      goto mandos_end;
815
 
    }
816
679
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
817
680
  
818
681
  if(ret != GNUTLS_E_SUCCESS){
820
683
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
821
684
      gnutls_perror(ret);
822
685
    }
823
 
    errno = EPROTO;
 
686
    retval = -1;
824
687
    goto mandos_end;
825
688
  }
826
689
  
832
695
  }
833
696
  
834
697
  while(true){
835
 
    
836
 
    if(quit_now){
837
 
      errno = EINTR;
838
 
      goto mandos_end;
839
 
    }
840
 
    
841
698
    buffer_capacity = incbuffer(&buffer, buffer_length,
842
699
                                   buffer_capacity);
843
700
    if(buffer_capacity == 0){
844
 
      int e = errno;
845
 
      perror_plus("incbuffer");
846
 
      errno = e;
847
 
      goto mandos_end;
848
 
    }
849
 
    
850
 
    if(quit_now){
851
 
      errno = EINTR;
 
701
      perror("incbuffer");
 
702
      retval = -1;
852
703
      goto mandos_end;
853
704
    }
854
705
    
863
714
      case GNUTLS_E_AGAIN:
864
715
        break;
865
716
      case GNUTLS_E_REHANDSHAKE:
866
 
        do {
 
717
        do{
867
718
          ret = gnutls_handshake(session);
868
 
          
869
 
          if(quit_now){
870
 
            errno = EINTR;
871
 
            goto mandos_end;
872
 
          }
873
719
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
874
720
        if(ret < 0){
875
721
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
876
722
          gnutls_perror(ret);
877
 
          errno = EPROTO;
 
723
          retval = -1;
878
724
          goto mandos_end;
879
725
        }
880
726
        break;
881
727
      default:
882
728
        fprintf(stderr, "Unknown error while reading data from"
883
729
                " encrypted session with Mandos server\n");
 
730
        retval = -1;
884
731
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
885
 
        errno = EIO;
886
732
        goto mandos_end;
887
733
      }
888
734
    } else {
894
740
    fprintf(stderr, "Closing TLS session\n");
895
741
  }
896
742
  
897
 
  if(quit_now){
898
 
    errno = EINTR;
899
 
    goto mandos_end;
900
 
  }
901
 
  
902
 
  do {
903
 
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
904
 
    if(quit_now){
905
 
      errno = EINTR;
906
 
      goto mandos_end;
907
 
    }
908
 
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
743
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
909
744
  
910
745
  if(buffer_length > 0){
911
 
    ssize_t decrypted_buffer_size;
912
746
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
913
747
                                               buffer_length,
914
748
                                               &decrypted_buffer);
915
749
    if(decrypted_buffer_size >= 0){
916
 
      
917
750
      written = 0;
918
751
      while(written < (size_t) decrypted_buffer_size){
919
 
        if(quit_now){
920
 
          errno = EINTR;
921
 
          goto mandos_end;
922
 
        }
923
 
        
924
752
        ret = (int)fwrite(decrypted_buffer + written, 1,
925
753
                          (size_t)decrypted_buffer_size - written,
926
754
                          stdout);
927
755
        if(ret == 0 and ferror(stdout)){
928
 
          int e = errno;
929
756
          if(debug){
930
757
            fprintf(stderr, "Error writing encrypted data: %s\n",
931
758
                    strerror(errno));
932
759
          }
933
 
          errno = e;
934
 
          goto mandos_end;
 
760
          retval = -1;
 
761
          break;
935
762
        }
936
763
        written += (size_t)ret;
937
764
      }
938
 
      retval = 0;
 
765
      free(decrypted_buffer);
 
766
    } else {
 
767
      retval = -1;
939
768
    }
 
769
  } else {
 
770
    retval = -1;
940
771
  }
941
772
  
942
773
  /* Shutdown procedure */
943
774
  
944
775
 mandos_end:
945
 
  {
946
 
    int e = errno;
947
 
    free(decrypted_buffer);
948
 
    free(buffer);
949
 
    if(tcp_sd >= 0){
950
 
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
951
 
    }
952
 
    if(ret == -1){
953
 
      if(e == 0){
954
 
        e = errno;
955
 
      }
956
 
      perror_plus("close");
957
 
    }
958
 
    gnutls_deinit(session);
959
 
    errno = e;
960
 
    if(quit_now){
961
 
      errno = EINTR;
962
 
      retval = -1;
963
 
    }
 
776
  free(buffer);
 
777
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
778
  if(ret == -1){
 
779
    perror("close");
964
780
  }
 
781
  gnutls_deinit(session);
965
782
  return retval;
966
783
}
967
784
 
984
801
  /* Called whenever a service has been resolved successfully or
985
802
     timed out */
986
803
  
987
 
  if(quit_now){
988
 
    return;
989
 
  }
990
 
  
991
804
  switch(event){
992
805
  default:
993
806
  case AVAHI_RESOLVER_FAILURE:
1009
822
                                           avahi_proto_to_af(proto));
1010
823
      if(ret == 0){
1011
824
        avahi_simple_poll_quit(mc.simple_poll);
1012
 
      } else {
1013
 
        ret = add_server(ip, port, interface,
1014
 
                         avahi_proto_to_af(proto));
1015
825
      }
1016
826
    }
1017
827
  }
1033
843
  /* Called whenever a new services becomes available on the LAN or
1034
844
     is removed from the LAN */
1035
845
  
1036
 
  if(quit_now){
1037
 
    return;
1038
 
  }
1039
 
  
1040
846
  switch(event){
1041
847
  default:
1042
848
  case AVAHI_BROWSER_FAILURE:
1071
877
  }
1072
878
}
1073
879
 
1074
 
/* Signal handler that stops main loop after SIGTERM */
1075
 
static void handle_sigterm(int sig){
 
880
sig_atomic_t quit_now = 0;
 
881
 
 
882
/* stop main loop after sigterm has been called */
 
883
static void handle_sigterm(__attribute__((unused)) int sig){
1076
884
  if(quit_now){
1077
885
    return;
1078
886
  }
1079
887
  quit_now = 1;
1080
 
  signal_received = sig;
1081
888
  int old_errno = errno;
1082
 
  /* set main loop to exit */
1083
889
  if(mc.simple_poll != NULL){
1084
890
    avahi_simple_poll_quit(mc.simple_poll);
1085
891
  }
1086
892
  errno = old_errno;
1087
893
}
1088
894
 
1089
 
bool get_flags(const char *ifname, struct ifreq *ifr){
1090
 
  int ret;
1091
 
  
1092
 
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1093
 
  if(s < 0){
1094
 
    perror_plus("socket");
1095
 
    return false;
1096
 
  }
1097
 
  strcpy(ifr->ifr_name, ifname);
1098
 
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1099
 
  if(ret == -1){
1100
 
    if(debug){
1101
 
      perror_plus("ioctl SIOCGIFFLAGS");
1102
 
    }
1103
 
    return false;
1104
 
  }
1105
 
  return true;
1106
 
}
1107
 
 
1108
 
bool good_flags(const char *ifname, const struct ifreq *ifr){
1109
 
  
1110
 
  /* Reject the loopback device */
1111
 
  if(ifr->ifr_flags & IFF_LOOPBACK){
1112
 
    if(debug){
1113
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1114
 
              ifname);
1115
 
    }
1116
 
    return false;
1117
 
  }
1118
 
  /* Accept point-to-point devices only if connect_to is specified */
1119
 
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1120
 
    if(debug){
1121
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1122
 
              ifname);
1123
 
    }
1124
 
    return true;
1125
 
  }
1126
 
  /* Otherwise, reject non-broadcast-capable devices */
1127
 
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1128
 
    if(debug){
1129
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1130
 
              ifname);
1131
 
    }
1132
 
    return false;
1133
 
  }
1134
 
  /* Reject non-ARP interfaces (including dummy interfaces) */
1135
 
  if(ifr->ifr_flags & IFF_NOARP){
1136
 
    if(debug){
1137
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1138
 
    }
1139
 
    return false;
1140
 
  }
1141
 
  
1142
 
  /* Accept this device */
1143
 
  if(debug){
1144
 
    fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1145
 
  }
1146
 
  return true;
1147
 
}
1148
 
 
1149
 
/* 
1150
 
 * This function determines if a directory entry in /sys/class/net
1151
 
 * corresponds to an acceptable network device.
1152
 
 * (This function is passed to scandir(3) as a filter function.)
1153
 
 */
1154
 
int good_interface(const struct dirent *if_entry){
1155
 
  if(if_entry->d_name[0] == '.'){
1156
 
    return 0;
1157
 
  }
1158
 
  
1159
 
  struct ifreq ifr;
1160
 
  if(not get_flags(if_entry->d_name, &ifr)){
1161
 
    if(debug){
1162
 
      fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
1163
 
              if_entry->d_name);
1164
 
    }
1165
 
    return 0;
1166
 
  }
1167
 
  
1168
 
  if(not good_flags(if_entry->d_name, &ifr)){
1169
 
    return 0;
1170
 
  }
1171
 
  return 1;
1172
 
}
1173
 
 
1174
 
/* 
1175
 
 * This function determines if a directory entry in /sys/class/net
1176
 
 * corresponds to an acceptable network device which is up.
1177
 
 * (This function is passed to scandir(3) as a filter function.)
1178
 
 */
1179
 
int up_interface(const struct dirent *if_entry){
1180
 
  if(if_entry->d_name[0] == '.'){
1181
 
    return 0;
1182
 
  }
1183
 
  
1184
 
  struct ifreq ifr;
1185
 
  if(not get_flags(if_entry->d_name, &ifr)){
1186
 
    if(debug){
1187
 
      fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
1188
 
              if_entry->d_name);
1189
 
    }
1190
 
    return 0;
1191
 
  }
1192
 
  
1193
 
  /* Reject down interfaces */
1194
 
  if(not (ifr.ifr_flags & IFF_UP)){
1195
 
    if(debug){
1196
 
      fprintf(stderr, "Rejecting down interface \"%s\"\n",
1197
 
              if_entry->d_name);
1198
 
    }
1199
 
    return 0;
1200
 
  }
1201
 
  
1202
 
  /* Reject non-running interfaces */
1203
 
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1204
 
    if(debug){
1205
 
      fprintf(stderr, "Rejecting non-running interface \"%s\"\n",
1206
 
              if_entry->d_name);
1207
 
    }
1208
 
    return 0;
1209
 
  }
1210
 
  
1211
 
  if(not good_flags(if_entry->d_name, &ifr)){
1212
 
    return 0;
1213
 
  }
1214
 
  return 1;
1215
 
}
1216
 
 
1217
 
int notdotentries(const struct dirent *direntry){
1218
 
  /* Skip "." and ".." */
1219
 
  if(direntry->d_name[0] == '.'
1220
 
     and (direntry->d_name[1] == '\0'
1221
 
          or (direntry->d_name[1] == '.'
1222
 
              and direntry->d_name[2] == '\0'))){
1223
 
    return 0;
1224
 
  }
1225
 
  return 1;
1226
 
}
1227
 
 
1228
 
/* Is this directory entry a runnable program? */
1229
 
int runnable_hook(const struct dirent *direntry){
1230
 
  int ret;
1231
 
  struct stat st;
1232
 
  
1233
 
  if((direntry->d_name)[0] == '\0'){
1234
 
    /* Empty name? */
1235
 
    return 0;
1236
 
  }
1237
 
  
1238
 
  /* Save pointer to last character */
1239
 
  char *end = strchr(direntry->d_name, '\0')-1;
1240
 
  
1241
 
  if(*end == '~'){
1242
 
    /* Backup name~ */
1243
 
    return 0;
1244
 
  }
1245
 
  
1246
 
  if(((direntry->d_name)[0] == '#')
1247
 
     and (*end == '#')){
1248
 
    /* Temporary #name# */
1249
 
    return 0;
1250
 
  }
1251
 
  
1252
 
  /* XXX more rules here */
1253
 
  
1254
 
  ret = stat(direntry->d_name, &st);
1255
 
  if(ret == -1){
1256
 
    if(debug){
1257
 
      perror_plus("Could not stat plugin");
1258
 
    }
1259
 
    return 0;
1260
 
  }
1261
 
  if(not (S_ISREG(st.st_mode))){
1262
 
    /* Not a regular file */
1263
 
    return 0;
1264
 
  }
1265
 
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1266
 
    /* Not executable */
1267
 
    return 0;
1268
 
  }
1269
 
  return 1;
1270
 
}
1271
 
 
1272
 
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1273
 
  int ret;
1274
 
  struct timespec now;
1275
 
  struct timespec waited_time;
1276
 
  intmax_t block_time;
1277
 
  
1278
 
  while(true){
1279
 
    if(mc.current_server == NULL){
1280
 
      if (debug){
1281
 
        fprintf(stderr,
1282
 
                "Wait until first server is found. No timeout!\n");
1283
 
      }
1284
 
      ret = avahi_simple_poll_iterate(s, -1);
1285
 
    } else {
1286
 
      if (debug){
1287
 
        fprintf(stderr, "Check current_server if we should run it,"
1288
 
                " or wait\n");
1289
 
      }
1290
 
      /* the current time */
1291
 
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1292
 
      if(ret == -1){
1293
 
        perror_plus("clock_gettime");
1294
 
        return -1;
1295
 
      }
1296
 
      /* Calculating in ms how long time between now and server
1297
 
         who we visted longest time ago. Now - last seen.  */
1298
 
      waited_time.tv_sec = (now.tv_sec
1299
 
                            - mc.current_server->last_seen.tv_sec);
1300
 
      waited_time.tv_nsec = (now.tv_nsec
1301
 
                             - mc.current_server->last_seen.tv_nsec);
1302
 
      /* total time is 10s/10,000ms.
1303
 
         Converting to s from ms by dividing by 1,000,
1304
 
         and ns to ms by dividing by 1,000,000. */
1305
 
      block_time = ((retry_interval
1306
 
                     - ((intmax_t)waited_time.tv_sec * 1000))
1307
 
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1308
 
      
1309
 
      if (debug){
1310
 
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1311
 
      }
1312
 
      
1313
 
      if(block_time <= 0){
1314
 
        ret = start_mandos_communication(mc.current_server->ip,
1315
 
                                         mc.current_server->port,
1316
 
                                         mc.current_server->if_index,
1317
 
                                         mc.current_server->af);
1318
 
        if(ret == 0){
1319
 
          avahi_simple_poll_quit(mc.simple_poll);
1320
 
          return 0;
1321
 
        }
1322
 
        ret = clock_gettime(CLOCK_MONOTONIC,
1323
 
                            &mc.current_server->last_seen);
1324
 
        if(ret == -1){
1325
 
          perror_plus("clock_gettime");
1326
 
          return -1;
1327
 
        }
1328
 
        mc.current_server = mc.current_server->next;
1329
 
        block_time = 0;         /* Call avahi to find new Mandos
1330
 
                                   servers, but don't block */
1331
 
      }
1332
 
      
1333
 
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1334
 
    }
1335
 
    if(ret != 0){
1336
 
      if (ret > 0 or errno != EINTR) {
1337
 
        return (ret != 1) ? ret : 0;
1338
 
      }
1339
 
    }
1340
 
  }
1341
 
}
1342
 
 
1343
895
int main(int argc, char *argv[]){
1344
896
  AvahiSServiceBrowser *sb = NULL;
1345
897
  int error;
1347
899
  intmax_t tmpmax;
1348
900
  char *tmp;
1349
901
  int exitcode = EXIT_SUCCESS;
1350
 
  const char *interface = "";
 
902
  const char *interface = "eth0";
1351
903
  struct ifreq network;
1352
 
  int sd = -1;
1353
 
  bool take_down_interface = false;
 
904
  int sd;
1354
905
  uid_t uid;
1355
906
  gid_t gid;
 
907
  char *connect_to = NULL;
1356
908
  char tempdir[] = "/tmp/mandosXXXXXX";
1357
909
  bool tempdir_created = false;
1358
910
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1362
914
  bool gnutls_initialized = false;
1363
915
  bool gpgme_initialized = false;
1364
916
  float delay = 2.5f;
1365
 
  double retry_interval = 10; /* 10s between trying a server and
1366
 
                                 retrying the same server again */
1367
917
  
1368
 
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
 
918
  struct sigaction old_sigterm_action;
1369
919
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1370
920
  
1371
 
  uid = getuid();
1372
 
  gid = getgid();
1373
 
  
1374
 
  /* Lower any group privileges we might have, just to be safe */
1375
 
  errno = 0;
1376
 
  ret = setgid(gid);
1377
 
  if(ret == -1){
1378
 
    perror_plus("setgid");
1379
 
  }
1380
 
  
1381
 
  /* Lower user privileges (temporarily) */
1382
 
  errno = 0;
1383
 
  ret = seteuid(uid);
1384
 
  if(ret == -1){
1385
 
    perror_plus("seteuid");
1386
 
  }
1387
 
  
1388
 
  if(quit_now){
1389
 
    goto end;
1390
 
  }
1391
 
  
1392
921
  {
1393
922
    struct argp_option options[] = {
1394
923
      { .name = "debug", .key = 128,
1423
952
        .arg = "SECONDS",
1424
953
        .doc = "Maximum delay to wait for interface startup",
1425
954
        .group = 2 },
1426
 
      { .name = "retry", .key = 132,
1427
 
        .arg = "SECONDS",
1428
 
        .doc = "Retry interval used when denied by the mandos server",
1429
 
        .group = 2 },
1430
 
      /*
1431
 
       * These reproduce what we would get without ARGP_NO_HELP
1432
 
       */
1433
 
      { .name = "help", .key = '?',
1434
 
        .doc = "Give this help list", .group = -1 },
1435
 
      { .name = "usage", .key = -3,
1436
 
        .doc = "Give a short usage message", .group = -1 },
1437
 
      { .name = "version", .key = 'V',
1438
 
        .doc = "Print program version", .group = -1 },
1439
955
      { .name = NULL }
1440
956
    };
1441
957
    
1442
958
    error_t parse_opt(int key, char *arg,
1443
959
                      struct argp_state *state){
1444
 
      errno = 0;
1445
960
      switch(key){
1446
961
      case 128:                 /* --debug */
1447
962
        debug = true;
1463
978
        tmpmax = strtoimax(arg, &tmp, 10);
1464
979
        if(errno != 0 or tmp == arg or *tmp != '\0'
1465
980
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1466
 
          argp_error(state, "Bad number of DH bits");
 
981
          fprintf(stderr, "Bad number of DH bits\n");
 
982
          exit(EXIT_FAILURE);
1467
983
        }
1468
984
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1469
985
        break;
1474
990
        errno = 0;
1475
991
        delay = strtof(arg, &tmp);
1476
992
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1477
 
          argp_error(state, "Bad delay");
1478
 
        }
1479
 
      case 132:                 /* --retry */
1480
 
        errno = 0;
1481
 
        retry_interval = strtod(arg, &tmp);
1482
 
        if(errno != 0 or tmp == arg or *tmp != '\0'
1483
 
           or (retry_interval * 1000) > INT_MAX
1484
 
           or retry_interval < 0){
1485
 
          argp_error(state, "Bad retry interval");
 
993
          fprintf(stderr, "Bad delay\n");
 
994
          exit(EXIT_FAILURE);
1486
995
        }
1487
996
        break;
1488
 
        /*
1489
 
         * These reproduce what we would get without ARGP_NO_HELP
1490
 
         */
1491
 
      case '?':                 /* --help */
1492
 
        argp_state_help(state, state->out_stream,
1493
 
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1494
 
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
1495
 
      case -3:                  /* --usage */
1496
 
        argp_state_help(state, state->out_stream,
1497
 
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1498
 
      case 'V':                 /* --version */
1499
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
1500
 
        exit(argp_err_exit_status);
 
997
      case ARGP_KEY_ARG:
 
998
        argp_usage(state);
 
999
      case ARGP_KEY_END:
1501
1000
        break;
1502
1001
      default:
1503
1002
        return ARGP_ERR_UNKNOWN;
1504
1003
      }
1505
 
      return errno;
 
1004
      return 0;
1506
1005
    }
1507
1006
    
1508
1007
    struct argp argp = { .options = options, .parser = parse_opt,
1509
1008
                         .args_doc = "",
1510
1009
                         .doc = "Mandos client -- Get and decrypt"
1511
1010
                         " passwords from a Mandos server" };
1512
 
    ret = argp_parse(&argp, argc, argv,
1513
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1514
 
    switch(ret){
1515
 
    case 0:
1516
 
      break;
1517
 
    case ENOMEM:
1518
 
    default:
1519
 
      errno = ret;
1520
 
      perror_plus("argp_parse");
1521
 
      exitcode = EX_OSERR;
1522
 
      goto end;
1523
 
    case EINVAL:
1524
 
      exitcode = EX_USAGE;
1525
 
      goto end;
1526
 
    }
1527
 
  }
1528
 
    
1529
 
  {
1530
 
    /* Work around Debian bug #633582:
1531
 
       <http://bugs.debian.org/633582> */
1532
 
    struct stat st;
1533
 
    
1534
 
    /* Re-raise priviliges */
1535
 
    errno = 0;
1536
 
    ret = seteuid(0);
1537
 
    if(ret == -1){
1538
 
      perror_plus("seteuid");
1539
 
    }
1540
 
    
1541
 
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1542
 
      int seckey_fd = open(seckey, O_RDONLY);
1543
 
      if(seckey_fd == -1){
1544
 
        perror_plus("open");
1545
 
      } else {
1546
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1547
 
        if(ret == -1){
1548
 
          perror_plus("fstat");
1549
 
        } else {
1550
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1551
 
            ret = fchown(seckey_fd, uid, gid);
1552
 
            if(ret == -1){
1553
 
              perror_plus("fchown");
1554
 
            }
1555
 
          }
1556
 
        }
1557
 
        TEMP_FAILURE_RETRY(close(seckey_fd));
1558
 
      }
1559
 
    }
1560
 
    
1561
 
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1562
 
      int pubkey_fd = open(pubkey, O_RDONLY);
1563
 
      if(pubkey_fd == -1){
1564
 
        perror_plus("open");
1565
 
      } else {
1566
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1567
 
        if(ret == -1){
1568
 
          perror_plus("fstat");
1569
 
        } else {
1570
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1571
 
            ret = fchown(pubkey_fd, uid, gid);
1572
 
            if(ret == -1){
1573
 
              perror_plus("fchown");
1574
 
            }
1575
 
          }
1576
 
        }
1577
 
        TEMP_FAILURE_RETRY(close(pubkey_fd));
1578
 
      }
1579
 
    }
1580
 
    
1581
 
    /* Lower privileges */
1582
 
    errno = 0;
1583
 
    ret = seteuid(uid);
1584
 
    if(ret == -1){
1585
 
      perror_plus("seteuid");
1586
 
    }
1587
 
  }
1588
 
  
1589
 
  /* Find network hooks and run them */
1590
 
  {
1591
 
    struct dirent **direntries;
1592
 
    struct dirent *direntry;
1593
 
    int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
1594
 
                           alphasort);
1595
 
    int devnull = open("/dev/null", O_RDONLY);
1596
 
    for(int i = 0; i < numhooks; i++){
1597
 
      direntry = direntries[0];
1598
 
      char *fullname = NULL;
1599
 
      ret = asprintf(&fullname, "%s/%s", tempdir,
1600
 
                     direntry->d_name);
1601
 
      if(ret < 0){
1602
 
        perror_plus("asprintf");
1603
 
        continue;
1604
 
      }
1605
 
      pid_t hook_pid = fork();
1606
 
      if(hook_pid == 0){
1607
 
        /* Child */
1608
 
        dup2(devnull, STDIN_FILENO);
1609
 
        close(devnull);
1610
 
        dup2(STDERR_FILENO, STDOUT_FILENO);
1611
 
        setenv("DEVICE", interface, 1);
1612
 
        setenv("VERBOSE", debug ? "1" : "0", 1);
1613
 
        setenv("MODE", "start", 1);
1614
 
        /* setenv( XXX more here */
1615
 
        ret = execl(fullname, direntry->d_name, "start", NULL);
1616
 
        perror_plus("execl");
1617
 
      }
1618
 
      free(fullname);
1619
 
      if(quit_now){
1620
 
        goto end;
1621
 
      }
1622
 
    }
1623
 
    close(devnull);
 
1011
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
1012
    if(ret == ARGP_ERR_UNKNOWN){
 
1013
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
1014
      exitcode = EXIT_FAILURE;
 
1015
      goto end;
 
1016
    }
1624
1017
  }
1625
1018
  
1626
1019
  if(not debug){
1627
1020
    avahi_set_log_function(empty_log);
1628
1021
  }
1629
1022
  
1630
 
  if(interface[0] == '\0'){
1631
 
    struct dirent **direntries;
1632
 
    /* First look for interfaces that are up */
1633
 
    ret = scandir(sys_class_net, &direntries, up_interface,
1634
 
                  alphasort);
1635
 
    if(ret == 0){
1636
 
      /* No up interfaces, look for any good interfaces */
1637
 
      free(direntries);
1638
 
      ret = scandir(sys_class_net, &direntries, good_interface,
1639
 
                    alphasort);
1640
 
    }
1641
 
    if(ret >= 1){
1642
 
      /* Pick the first interface returned */
1643
 
      interface = strdup(direntries[0]->d_name);
1644
 
      if(debug){
1645
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1646
 
      }
1647
 
      if(interface == NULL){
1648
 
        perror_plus("malloc");
1649
 
        free(direntries);
1650
 
        exitcode = EXIT_FAILURE;
1651
 
        goto end;
1652
 
      }
1653
 
      free(direntries);
1654
 
    } else {
1655
 
      free(direntries);
1656
 
      fprintf(stderr, "Could not find a network interface\n");
1657
 
      exitcode = EXIT_FAILURE;
1658
 
      goto end;
1659
 
    }
1660
 
  }
1661
 
  
1662
1023
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1663
1024
     from the signal handler */
1664
1025
  /* Initialize the pseudo-RNG for Avahi */
1666
1027
  mc.simple_poll = avahi_simple_poll_new();
1667
1028
  if(mc.simple_poll == NULL){
1668
1029
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1669
 
    exitcode = EX_UNAVAILABLE;
 
1030
    exitcode = EXIT_FAILURE;
1670
1031
    goto end;
1671
1032
  }
1672
1033
  
1673
1034
  sigemptyset(&sigterm_action.sa_mask);
1674
1035
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1675
1036
  if(ret == -1){
1676
 
    perror_plus("sigaddset");
1677
 
    exitcode = EX_OSERR;
 
1037
    perror("sigaddset");
 
1038
    exitcode = EXIT_FAILURE;
1678
1039
    goto end;
1679
1040
  }
1680
1041
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1681
1042
  if(ret == -1){
1682
 
    perror_plus("sigaddset");
1683
 
    exitcode = EX_OSERR;
 
1043
    perror("sigaddset");
 
1044
    exitcode = EXIT_FAILURE;
1684
1045
    goto end;
1685
1046
  }
1686
1047
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1687
1048
  if(ret == -1){
1688
 
    perror_plus("sigaddset");
1689
 
    exitcode = EX_OSERR;
1690
 
    goto end;
1691
 
  }
1692
 
  /* Need to check if the handler is SIG_IGN before handling:
1693
 
     | [[info:libc:Initial Signal Actions]] |
1694
 
     | [[info:libc:Basic Signal Handling]]  |
1695
 
  */
1696
 
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1697
 
  if(ret == -1){
1698
 
    perror_plus("sigaction");
1699
 
    return EX_OSERR;
1700
 
  }
1701
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1702
 
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1703
 
    if(ret == -1){
1704
 
      perror_plus("sigaction");
1705
 
      exitcode = EX_OSERR;
1706
 
      goto end;
1707
 
    }
1708
 
  }
1709
 
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1710
 
  if(ret == -1){
1711
 
    perror_plus("sigaction");
1712
 
    return EX_OSERR;
1713
 
  }
1714
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1715
 
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1716
 
    if(ret == -1){
1717
 
      perror_plus("sigaction");
1718
 
      exitcode = EX_OSERR;
1719
 
      goto end;
1720
 
    }
1721
 
  }
1722
 
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1723
 
  if(ret == -1){
1724
 
    perror_plus("sigaction");
1725
 
    return EX_OSERR;
1726
 
  }
1727
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1728
 
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1729
 
    if(ret == -1){
1730
 
      perror_plus("sigaction");
1731
 
      exitcode = EX_OSERR;
1732
 
      goto end;
1733
 
    }
1734
 
  }
 
1049
    perror("sigaddset");
 
1050
    exitcode = EXIT_FAILURE;
 
1051
    goto end;
 
1052
  }
 
1053
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1054
  if(ret == -1){
 
1055
    perror("sigaction");
 
1056
    exitcode = EXIT_FAILURE;
 
1057
    goto end;
 
1058
  }  
1735
1059
  
1736
1060
  /* If the interface is down, bring it up */
1737
 
  if(strcmp(interface, "none") != 0){
1738
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1739
 
    if(if_index == 0){
1740
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1741
 
      exitcode = EX_UNAVAILABLE;
1742
 
      goto end;
1743
 
    }
1744
 
    
1745
 
    if(quit_now){
1746
 
      goto end;
1747
 
    }
1748
 
    
1749
 
    /* Re-raise priviliges */
1750
 
    errno = 0;
1751
 
    ret = seteuid(0);
1752
 
    if(ret == -1){
1753
 
      perror_plus("seteuid");
1754
 
    }
1755
 
    
 
1061
  if(interface[0] != '\0'){
1756
1062
#ifdef __linux__
1757
1063
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1758
 
       messages about the network interface to mess up the prompt */
 
1064
       messages to mess up the prompt */
1759
1065
    ret = klogctl(8, NULL, 5);
1760
1066
    bool restore_loglevel = true;
1761
1067
    if(ret == -1){
1762
1068
      restore_loglevel = false;
1763
 
      perror_plus("klogctl");
 
1069
      perror("klogctl");
1764
1070
    }
1765
1071
#endif  /* __linux__ */
1766
1072
    
1767
1073
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1768
1074
    if(sd < 0){
1769
 
      perror_plus("socket");
1770
 
      exitcode = EX_OSERR;
 
1075
      perror("socket");
 
1076
      exitcode = EXIT_FAILURE;
1771
1077
#ifdef __linux__
1772
1078
      if(restore_loglevel){
1773
1079
        ret = klogctl(7, NULL, 0);
1774
1080
        if(ret == -1){
1775
 
          perror_plus("klogctl");
 
1081
          perror("klogctl");
1776
1082
        }
1777
1083
      }
1778
1084
#endif  /* __linux__ */
1779
 
      /* Lower privileges */
1780
 
      errno = 0;
1781
 
      ret = seteuid(uid);
1782
 
      if(ret == -1){
1783
 
        perror_plus("seteuid");
1784
 
      }
1785
1085
      goto end;
1786
1086
    }
1787
1087
    strcpy(network.ifr_name, interface);
1788
1088
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1789
1089
    if(ret == -1){
1790
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1090
      perror("ioctl SIOCGIFFLAGS");
1791
1091
#ifdef __linux__
1792
1092
      if(restore_loglevel){
1793
1093
        ret = klogctl(7, NULL, 0);
1794
1094
        if(ret == -1){
1795
 
          perror_plus("klogctl");
 
1095
          perror("klogctl");
1796
1096
        }
1797
1097
      }
1798
1098
#endif  /* __linux__ */
1799
 
      exitcode = EX_OSERR;
1800
 
      /* Lower privileges */
1801
 
      errno = 0;
1802
 
      ret = seteuid(uid);
1803
 
      if(ret == -1){
1804
 
        perror_plus("seteuid");
1805
 
      }
 
1099
      exitcode = EXIT_FAILURE;
1806
1100
      goto end;
1807
1101
    }
1808
1102
    if((network.ifr_flags & IFF_UP) == 0){
1809
1103
      network.ifr_flags |= IFF_UP;
1810
 
      take_down_interface = true;
1811
1104
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1812
1105
      if(ret == -1){
1813
 
        take_down_interface = false;
1814
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1815
 
        exitcode = EX_OSERR;
 
1106
        perror("ioctl SIOCSIFFLAGS");
 
1107
        exitcode = EXIT_FAILURE;
1816
1108
#ifdef __linux__
1817
1109
        if(restore_loglevel){
1818
1110
          ret = klogctl(7, NULL, 0);
1819
1111
          if(ret == -1){
1820
 
            perror_plus("klogctl");
 
1112
            perror("klogctl");
1821
1113
          }
1822
1114
        }
1823
1115
#endif  /* __linux__ */
1824
 
        /* Lower privileges */
1825
 
        errno = 0;
1826
 
        ret = seteuid(uid);
1827
 
        if(ret == -1){
1828
 
          perror_plus("seteuid");
1829
 
        }
1830
1116
        goto end;
1831
1117
      }
1832
1118
    }
1833
 
    /* Sleep checking until interface is running.
1834
 
       Check every 0.25s, up to total time of delay */
 
1119
    /* sleep checking until interface is running */
1835
1120
    for(int i=0; i < delay * 4; i++){
1836
1121
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1837
1122
      if(ret == -1){
1838
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1123
        perror("ioctl SIOCGIFFLAGS");
1839
1124
      } else if(network.ifr_flags & IFF_RUNNING){
1840
1125
        break;
1841
1126
      }
1842
1127
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1843
1128
      ret = nanosleep(&sleeptime, NULL);
1844
1129
      if(ret == -1 and errno != EINTR){
1845
 
        perror_plus("nanosleep");
 
1130
        perror("nanosleep");
1846
1131
      }
1847
1132
    }
1848
 
    if(not take_down_interface){
1849
 
      /* We won't need the socket anymore */
1850
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1851
 
      if(ret == -1){
1852
 
        perror_plus("close");
1853
 
      }
 
1133
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1134
    if(ret == -1){
 
1135
      perror("close");
1854
1136
    }
1855
1137
#ifdef __linux__
1856
1138
    if(restore_loglevel){
1857
1139
      /* Restores kernel loglevel to default */
1858
1140
      ret = klogctl(7, NULL, 0);
1859
1141
      if(ret == -1){
1860
 
        perror_plus("klogctl");
 
1142
        perror("klogctl");
1861
1143
      }
1862
1144
    }
1863
1145
#endif  /* __linux__ */
1864
 
    /* Lower privileges */
1865
 
    errno = 0;
1866
 
    if(take_down_interface){
1867
 
      /* Lower privileges */
1868
 
      ret = seteuid(uid);
1869
 
      if(ret == -1){
1870
 
        perror_plus("seteuid");
1871
 
      }
1872
 
    } else {
1873
 
      /* Lower privileges permanently */
1874
 
      ret = setuid(uid);
1875
 
      if(ret == -1){
1876
 
        perror_plus("setuid");
1877
 
      }
1878
 
    }
1879
 
  }
1880
 
  
1881
 
  if(quit_now){
1882
 
    goto end;
 
1146
  }
 
1147
  
 
1148
  uid = getuid();
 
1149
  gid = getgid();
 
1150
  
 
1151
  errno = 0;
 
1152
  setgid(gid);
 
1153
  if(ret == -1){
 
1154
    perror("setgid");
 
1155
  }
 
1156
  
 
1157
  ret = setuid(uid);
 
1158
  if(ret == -1){
 
1159
    perror("setuid");
1883
1160
  }
1884
1161
  
1885
1162
  ret = init_gnutls_global(pubkey, seckey);
1886
1163
  if(ret == -1){
1887
1164
    fprintf(stderr, "init_gnutls_global failed\n");
1888
 
    exitcode = EX_UNAVAILABLE;
 
1165
    exitcode = EXIT_FAILURE;
1889
1166
    goto end;
1890
1167
  } else {
1891
1168
    gnutls_initialized = true;
1892
1169
  }
1893
1170
  
1894
 
  if(quit_now){
1895
 
    goto end;
1896
 
  }
1897
 
  
1898
1171
  if(mkdtemp(tempdir) == NULL){
1899
 
    perror_plus("mkdtemp");
 
1172
    perror("mkdtemp");
1900
1173
    goto end;
1901
1174
  }
1902
1175
  tempdir_created = true;
1903
1176
  
1904
 
  if(quit_now){
1905
 
    goto end;
1906
 
  }
1907
 
  
1908
1177
  if(not init_gpgme(pubkey, seckey, tempdir)){
1909
1178
    fprintf(stderr, "init_gpgme failed\n");
1910
 
    exitcode = EX_UNAVAILABLE;
 
1179
    exitcode = EXIT_FAILURE;
1911
1180
    goto end;
1912
1181
  } else {
1913
1182
    gpgme_initialized = true;
1914
1183
  }
1915
1184
  
1916
 
  if(quit_now){
1917
 
    goto end;
 
1185
  if(interface[0] != '\0'){
 
1186
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1187
    if(if_index == 0){
 
1188
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1189
      exitcode = EXIT_FAILURE;
 
1190
      goto end;
 
1191
    }
1918
1192
  }
1919
1193
  
1920
1194
  if(connect_to != NULL){
1923
1197
    char *address = strrchr(connect_to, ':');
1924
1198
    if(address == NULL){
1925
1199
      fprintf(stderr, "No colon in address\n");
1926
 
      exitcode = EX_USAGE;
1927
 
      goto end;
1928
 
    }
1929
 
    
1930
 
    if(quit_now){
1931
 
      goto end;
1932
 
    }
1933
 
    
 
1200
      exitcode = EXIT_FAILURE;
 
1201
      goto end;
 
1202
    }
1934
1203
    uint16_t port;
1935
1204
    errno = 0;
1936
1205
    tmpmax = strtoimax(address+1, &tmp, 10);
1937
1206
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1938
1207
       or tmpmax != (uint16_t)tmpmax){
1939
1208
      fprintf(stderr, "Bad port number\n");
1940
 
      exitcode = EX_USAGE;
1941
 
      goto end;
1942
 
    }
1943
 
  
1944
 
    if(quit_now){
1945
 
      goto end;
1946
 
    }
1947
 
    
 
1209
      exitcode = EXIT_FAILURE;
 
1210
      goto end;
 
1211
    }
1948
1212
    port = (uint16_t)tmpmax;
1949
1213
    *address = '\0';
 
1214
    address = connect_to;
1950
1215
    /* Colon in address indicates IPv6 */
1951
1216
    int af;
1952
 
    if(strchr(connect_to, ':') != NULL){
 
1217
    if(strchr(address, ':') != NULL){
1953
1218
      af = AF_INET6;
1954
 
      /* Accept [] around IPv6 address - see RFC 5952 */
1955
 
      if(connect_to[0] == '[' and address[-1] == ']')
1956
 
        {
1957
 
          connect_to++;
1958
 
          address[-1] = '\0';
1959
 
        }
1960
1219
    } else {
1961
1220
      af = AF_INET;
1962
1221
    }
1963
 
    address = connect_to;
1964
 
    
1965
 
    if(quit_now){
1966
 
      goto end;
1967
 
    }
1968
 
    
1969
 
    while(not quit_now){
1970
 
      ret = start_mandos_communication(address, port, if_index, af);
1971
 
      if(quit_now or ret == 0){
1972
 
        break;
1973
 
      }
1974
 
      if(debug){
1975
 
        fprintf(stderr, "Retrying in %d seconds\n",
1976
 
                (int)retry_interval);
1977
 
      }
1978
 
      sleep((int)retry_interval);
1979
 
    }
1980
 
    
1981
 
    if (not quit_now){
 
1222
    ret = start_mandos_communication(address, port, if_index, af);
 
1223
    if(ret < 0){
 
1224
      exitcode = EXIT_FAILURE;
 
1225
    } else {
1982
1226
      exitcode = EXIT_SUCCESS;
1983
1227
    }
 
1228
    goto end;
 
1229
  }
1984
1230
    
1985
 
    goto end;
1986
 
  }
1987
 
  
1988
 
  if(quit_now){
1989
 
    goto end;
1990
 
  }
1991
 
  
1992
1231
  {
1993
1232
    AvahiServerConfig config;
1994
1233
    /* Do not publish any local Zeroconf records */
2011
1250
  if(mc.server == NULL){
2012
1251
    fprintf(stderr, "Failed to create Avahi server: %s\n",
2013
1252
            avahi_strerror(error));
2014
 
    exitcode = EX_UNAVAILABLE;
2015
 
    goto end;
2016
 
  }
2017
 
  
2018
 
  if(quit_now){
 
1253
    exitcode = EXIT_FAILURE;
2019
1254
    goto end;
2020
1255
  }
2021
1256
  
2026
1261
  if(sb == NULL){
2027
1262
    fprintf(stderr, "Failed to create service browser: %s\n",
2028
1263
            avahi_strerror(avahi_server_errno(mc.server)));
2029
 
    exitcode = EX_UNAVAILABLE;
2030
 
    goto end;
2031
 
  }
2032
 
  
2033
 
  if(quit_now){
 
1264
    exitcode = EXIT_FAILURE;
2034
1265
    goto end;
2035
1266
  }
2036
1267
  
2039
1270
  if(debug){
2040
1271
    fprintf(stderr, "Starting Avahi loop search\n");
2041
1272
  }
2042
 
 
2043
 
  ret = avahi_loop_with_timeout(mc.simple_poll,
2044
 
                                (int)(retry_interval * 1000));
2045
 
  if(debug){
2046
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
2047
 
            (ret == 0) ? "successfully" : "with error");
2048
 
  }
 
1273
  
 
1274
  avahi_simple_poll_loop(mc.simple_poll);
2049
1275
  
2050
1276
 end:
2051
1277
  
2072
1298
  if(gpgme_initialized){
2073
1299
    gpgme_release(mc.ctx);
2074
1300
  }
2075
 
 
2076
 
  /* Cleans up the circular linked list of Mandos servers the client
2077
 
     has seen */
2078
 
  if(mc.current_server != NULL){
2079
 
    mc.current_server->prev->next = NULL;
2080
 
    while(mc.current_server != NULL){
2081
 
      server *next = mc.current_server->next;
2082
 
      free(mc.current_server);
2083
 
      mc.current_server = next;
2084
 
    }
2085
 
  }
2086
 
  
2087
 
  /* XXX run network hooks "stop" here  */
2088
 
  
2089
 
  /* Take down the network interface */
2090
 
  if(take_down_interface){
2091
 
    /* Re-raise priviliges */
2092
 
    errno = 0;
2093
 
    ret = seteuid(0);
2094
 
    if(ret == -1){
2095
 
      perror_plus("seteuid");
2096
 
    }
2097
 
    if(geteuid() == 0){
2098
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2099
 
      if(ret == -1){
2100
 
        perror_plus("ioctl SIOCGIFFLAGS");
2101
 
      } else if(network.ifr_flags & IFF_UP) {
2102
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2103
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
2104
 
        if(ret == -1){
2105
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2106
 
        }
2107
 
      }
2108
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2109
 
      if(ret == -1){
2110
 
        perror_plus("close");
2111
 
      }
2112
 
      /* Lower privileges permanently */
2113
 
      errno = 0;
2114
 
      ret = setuid(uid);
2115
 
      if(ret == -1){
2116
 
        perror_plus("setuid");
2117
 
      }
2118
 
    }
2119
 
  }
2120
 
  
2121
 
  /* Removes the GPGME temp directory and all files inside */
 
1301
  
 
1302
  /* Removes the temp directory used by GPGME */
2122
1303
  if(tempdir_created){
2123
 
    struct dirent **direntries = NULL;
2124
 
    struct dirent *direntry = NULL;
2125
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
2126
 
                             alphasort);
2127
 
    if (numentries > 0){
2128
 
      for(int i = 0; i < numentries; i++){
2129
 
        direntry = direntries[i];
 
1304
    DIR *d;
 
1305
    struct dirent *direntry;
 
1306
    d = opendir(tempdir);
 
1307
    if(d == NULL){
 
1308
      if(errno != ENOENT){
 
1309
        perror("opendir");
 
1310
      }
 
1311
    } else {
 
1312
      while(true){
 
1313
        direntry = readdir(d);
 
1314
        if(direntry == NULL){
 
1315
          break;
 
1316
        }
 
1317
        /* Skip "." and ".." */
 
1318
        if(direntry->d_name[0] == '.'
 
1319
           and (direntry->d_name[1] == '\0'
 
1320
                or (direntry->d_name[1] == '.'
 
1321
                    and direntry->d_name[2] == '\0'))){
 
1322
          continue;
 
1323
        }
2130
1324
        char *fullname = NULL;
2131
1325
        ret = asprintf(&fullname, "%s/%s", tempdir,
2132
1326
                       direntry->d_name);
2133
1327
        if(ret < 0){
2134
 
          perror_plus("asprintf");
 
1328
          perror("asprintf");
2135
1329
          continue;
2136
1330
        }
2137
1331
        ret = remove(fullname);
2141
1335
        }
2142
1336
        free(fullname);
2143
1337
      }
2144
 
    }
2145
 
 
2146
 
    /* need to clean even if 0 because man page doesn't specify */
2147
 
    free(direntries);
2148
 
    if (numentries == -1){
2149
 
      perror_plus("scandir");
 
1338
      closedir(d);
2150
1339
    }
2151
1340
    ret = rmdir(tempdir);
2152
1341
    if(ret == -1 and errno != ENOENT){
2153
 
      perror_plus("rmdir");
2154
 
    }
2155
 
  }
2156
 
  
2157
 
  if(quit_now){
2158
 
    sigemptyset(&old_sigterm_action.sa_mask);
2159
 
    old_sigterm_action.sa_handler = SIG_DFL;
2160
 
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
2161
 
                                            &old_sigterm_action,
2162
 
                                            NULL));
2163
 
    if(ret == -1){
2164
 
      perror_plus("sigaction");
2165
 
    }
2166
 
    do {
2167
 
      ret = raise(signal_received);
2168
 
    } while(ret != 0 and errno == EINTR);
2169
 
    if(ret != 0){
2170
 
      perror_plus("raise");
2171
 
      abort();
2172
 
    }
2173
 
    TEMP_FAILURE_RETRY(pause());
 
1342
      perror("rmdir");
 
1343
    }
2174
1344
  }
2175
1345
  
2176
1346
  return exitcode;