/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

* plugins.d/mandos-client.c (good_interface): Add error message.
  (up_interface): Use get_flags() and good_flags().  Also check the
                  IFF_RUNNING flags.
  (runnable_hook): Bug fix: Add NULL to execl() call.
  (main): Use network interfaces which are up and running in
          preference to any other interfaces.

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,2009 Teddy Hogeborn
13
 
 * Copyright © 2008,2009 Björn Påhlsson
 
12
 * Copyright © 2008-2011 Teddy Hogeborn
 
13
 * Copyright © 2008-2011 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@fukt.bsnet.se>.
 
29
 * Contact the authors at <mandos@recompile.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, EXIT_FAILURE,
47
 
                                   srand(), strtof() */
 
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
 
47
                                   strtof(), abort() */
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() */
 
56
#include <sys/stat.h>           /* open(), S_ISREG */
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
 
#include <time.h>               /* nanosleep(), time() */
 
65
#include <errno.h>              /* perror(), errno,
 
66
                                   program_invocation_short_name */
 
67
#include <time.h>               /* nanosleep(), time(), sleep() */
67
68
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
69
                                   SIOCSIFFLAGS, if_indextoname(),
69
70
                                   if_nametoindex(), IF_NAMESIZE */
71
72
                                   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
72
73
                                */
73
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
 
                                   getuid(), getgid(), setuid(),
75
 
                                   setgid() */
76
 
#include <arpa/inet.h>          /* inet_pton(), htons */
 
75
                                   getuid(), getgid(), seteuid(),
 
76
                                   setgid(), pause() */
 
77
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
77
78
#include <iso646.h>             /* not, or, and */
78
79
#include <argp.h>               /* struct argp_option, error_t, struct
79
80
                                   argp_state, struct argp,
80
81
                                   argp_parse(), ARGP_KEY_ARG,
81
82
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
83
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
 
                                   sigaction(), SIGTERM, sigaction,
84
 
                                   sig_atomic_t */
 
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 */
85
88
 
86
89
#ifdef __linux__
87
90
#include <sys/klog.h>           /* klogctl() */
120
123
#define PATHDIR "/conf/conf.d/mandos"
121
124
#define SECKEY "seckey.txt"
122
125
#define PUBKEY "pubkey.txt"
 
126
#define HOOKDIR "/lib/mandos/network-hooks.d"
123
127
 
124
128
bool debug = false;
125
129
static const char mandos_protocol_version[] = "1";
126
130
const char *argp_program_version = "mandos-client " VERSION;
127
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
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;
128
145
 
129
146
/* Used for passing in values through the Avahi callback functions */
130
147
typedef struct {
135
152
  gnutls_dh_params_t dh_params;
136
153
  const char *priority;
137
154
  gpgme_ctx_t ctx;
 
155
  server *current_server;
138
156
} mandos_context;
139
157
 
140
158
/* global context so signal handler can reach it*/
141
159
mandos_context mc = { .simple_poll = NULL, .server = NULL,
142
160
                      .dh_bits = 1024, .priority = "SECURE256"
143
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
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
}
144
173
 
145
174
/*
146
175
 * Make additional room in "buffer" for at least BUFFER_SIZE more
159
188
  return buffer_capacity;
160
189
}
161
190
 
 
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
 
162
229
/* 
163
230
 * Initialize GPGME.
164
231
 */
165
232
static bool init_gpgme(const char *seckey,
166
233
                       const char *pubkey, const char *tempdir){
167
 
  int ret;
168
234
  gpgme_error_t rc;
169
235
  gpgme_engine_info_t engine_info;
170
236
  
173
239
   * Helper function to insert pub and seckey to the engine keyring.
174
240
   */
175
241
  bool import_key(const char *filename){
 
242
    int ret;
176
243
    int fd;
177
244
    gpgme_data_t pgp_data;
178
245
    
179
246
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
180
247
    if(fd == -1){
181
 
      perror("open");
 
248
      perror_plus("open");
182
249
      return false;
183
250
    }
184
251
    
198
265
    
199
266
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
200
267
    if(ret == -1){
201
 
      perror("close");
 
268
      perror_plus("close");
202
269
    }
203
270
    gpgme_data_release(pgp_data);
204
271
    return true;
217
284
    return false;
218
285
  }
219
286
  
220
 
    /* Set GPGME home directory for the OpenPGP engine only */
 
287
  /* Set GPGME home directory for the OpenPGP engine only */
221
288
  rc = gpgme_get_engine_info(&engine_info);
222
289
  if(rc != GPG_ERR_NO_ERROR){
223
290
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
249
316
    return false;
250
317
  }
251
318
  
252
 
  return true; 
 
319
  return true;
253
320
}
254
321
 
255
322
/* 
309
376
        }
310
377
        gpgme_recipient_t recipient;
311
378
        recipient = result->recipients;
312
 
        if(recipient){
313
 
          while(recipient != NULL){
314
 
            fprintf(stderr, "Public key algorithm: %s\n",
315
 
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
316
 
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
317
 
            fprintf(stderr, "Secret key available: %s\n",
318
 
                    recipient->status == GPG_ERR_NO_SECKEY
319
 
                    ? "No" : "Yes");
320
 
            recipient = recipient->next;
321
 
          }
 
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;
322
387
        }
323
388
      }
324
389
    }
331
396
  
332
397
  /* Seek back to the beginning of the GPGME plaintext data buffer */
333
398
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
334
 
    perror("gpgme_data_seek");
 
399
    perror_plus("gpgme_data_seek");
335
400
    plaintext_length = -1;
336
401
    goto decrypt_end;
337
402
  }
342
407
                                      (size_t)plaintext_length,
343
408
                                      plaintext_capacity);
344
409
    if(plaintext_capacity == 0){
345
 
        perror("incbuffer");
 
410
        perror_plus("incbuffer");
346
411
        plaintext_length = -1;
347
412
        goto decrypt_end;
348
413
    }
355
420
      break;
356
421
    }
357
422
    if(ret < 0){
358
 
      perror("gpgme_data_read");
 
423
      perror_plus("gpgme_data_read");
359
424
      plaintext_length = -1;
360
425
      goto decrypt_end;
361
426
    }
418
483
  }
419
484
  
420
485
  /* OpenPGP credentials */
421
 
  gnutls_certificate_allocate_credentials(&mc.cred);
 
486
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
422
487
  if(ret != GNUTLS_E_SUCCESS){
423
 
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
424
 
                                                    from
425
 
                                                    -Wunreachable-code
426
 
                                                 */
 
488
    fprintf(stderr, "GnuTLS memory error: %s\n",
427
489
            safer_gnutls_strerror(ret));
428
490
    gnutls_global_deinit();
429
491
    return -1;
476
538
static int init_gnutls_session(gnutls_session_t *session){
477
539
  int ret;
478
540
  /* GnuTLS session creation */
479
 
  ret = gnutls_init(session, GNUTLS_SERVER);
 
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);
480
547
  if(ret != GNUTLS_E_SUCCESS){
481
548
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
482
549
            safer_gnutls_strerror(ret));
484
551
  
485
552
  {
486
553
    const char *err;
487
 
    ret = gnutls_priority_set_direct(*session, mc.priority, &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);
488
561
    if(ret != GNUTLS_E_SUCCESS){
489
562
      fprintf(stderr, "Syntax error at: %s\n", err);
490
563
      fprintf(stderr, "GnuTLS error: %s\n",
494
567
    }
495
568
  }
496
569
  
497
 
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
498
 
                               mc.cred);
 
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);
499
578
  if(ret != GNUTLS_E_SUCCESS){
500
579
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
501
580
            safer_gnutls_strerror(ret));
504
583
  }
505
584
  
506
585
  /* ignore client certificate if any. */
507
 
  gnutls_certificate_server_set_request(*session,
508
 
                                        GNUTLS_CERT_IGNORE);
 
586
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
509
587
  
510
588
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
511
589
  
520
598
static int start_mandos_communication(const char *ip, uint16_t port,
521
599
                                      AvahiIfIndex if_index,
522
600
                                      int af){
523
 
  int ret, tcp_sd;
 
601
  int ret, tcp_sd = -1;
524
602
  ssize_t sret;
525
603
  union {
526
604
    struct sockaddr_in in;
527
605
    struct sockaddr_in6 in6;
528
606
  } to;
529
607
  char *buffer = NULL;
530
 
  char *decrypted_buffer;
 
608
  char *decrypted_buffer = NULL;
531
609
  size_t buffer_length = 0;
532
610
  size_t buffer_capacity = 0;
533
 
  ssize_t decrypted_buffer_size;
534
611
  size_t written;
535
 
  int retval = 0;
 
612
  int retval = -1;
536
613
  gnutls_session_t session;
537
614
  int pf;                       /* Protocol family */
538
615
  
 
616
  errno = 0;
 
617
  
 
618
  if(quit_now){
 
619
    errno = EINTR;
 
620
    return -1;
 
621
  }
 
622
  
539
623
  switch(af){
540
624
  case AF_INET6:
541
625
    pf = PF_INET6;
545
629
    break;
546
630
  default:
547
631
    fprintf(stderr, "Bad address family: %d\n", af);
 
632
    errno = EINVAL;
548
633
    return -1;
549
634
  }
550
635
  
560
645
  
561
646
  tcp_sd = socket(pf, SOCK_STREAM, 0);
562
647
  if(tcp_sd < 0){
563
 
    perror("socket");
564
 
    return -1;
 
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;
565
657
  }
566
658
  
567
659
  memset(&to, 0, sizeof(to));
573
665
    ret = inet_pton(af, ip, &to.in.sin_addr);
574
666
  }
575
667
  if(ret < 0 ){
576
 
    perror("inet_pton");
577
 
    return -1;
 
668
    int e = errno;
 
669
    perror_plus("inet_pton");
 
670
    errno = e;
 
671
    goto mandos_end;
578
672
  }
579
673
  if(ret == 0){
 
674
    int e = errno;
580
675
    fprintf(stderr, "Bad address: %s\n", ip);
581
 
    return -1;
 
676
    errno = e;
 
677
    goto mandos_end;
582
678
  }
583
679
  if(af == AF_INET6){
584
680
    to.in6.sin6_port = htons(port); /* Spurious warnings from
591
687
      if(if_index == AVAHI_IF_UNSPEC){
592
688
        fprintf(stderr, "An IPv6 link-local address is incomplete"
593
689
                " without a network interface\n");
594
 
        return -1;
 
690
        errno = EINVAL;
 
691
        goto mandos_end;
595
692
      }
596
693
      /* Set the network interface number as scope */
597
694
      to.in6.sin6_scope_id = (uint32_t)if_index;
602
699
                                     -Wunreachable-code */
603
700
  }
604
701
  
 
702
  if(quit_now){
 
703
    errno = EINTR;
 
704
    goto mandos_end;
 
705
  }
 
706
  
605
707
  if(debug){
606
708
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
607
709
      char interface[IF_NAMESIZE];
608
710
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
609
 
        perror("if_indextoname");
 
711
        perror_plus("if_indextoname");
610
712
      } else {
611
713
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
612
714
                ip, interface, port);
626
728
                        sizeof(addrstr));
627
729
    }
628
730
    if(pcret == NULL){
629
 
      perror("inet_ntop");
 
731
      perror_plus("inet_ntop");
630
732
    } else {
631
733
      if(strcmp(addrstr, ip) != 0){
632
734
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
634
736
    }
635
737
  }
636
738
  
 
739
  if(quit_now){
 
740
    errno = EINTR;
 
741
    goto mandos_end;
 
742
  }
 
743
  
637
744
  if(af == AF_INET6){
638
745
    ret = connect(tcp_sd, &to.in6, sizeof(to));
639
746
  } else {
640
747
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
641
748
  }
642
749
  if(ret < 0){
643
 
    perror("connect");
644
 
    return -1;
 
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;
645
761
  }
646
762
  
647
763
  const char *out = mandos_protocol_version;
651
767
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
652
768
                                   out_size - written));
653
769
    if(ret == -1){
654
 
      perror("write");
655
 
      retval = -1;
 
770
      int e = errno;
 
771
      perror_plus("write");
 
772
      errno = e;
656
773
      goto mandos_end;
657
774
    }
658
775
    written += (size_t)ret;
666
783
        break;
667
784
      }
668
785
    }
 
786
  
 
787
    if(quit_now){
 
788
      errno = EINTR;
 
789
      goto mandos_end;
 
790
    }
669
791
  }
670
792
  
671
793
  if(debug){
672
794
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
673
795
  }
674
796
  
 
797
  if(quit_now){
 
798
    errno = EINTR;
 
799
    goto mandos_end;
 
800
  }
 
801
  
 
802
  /* Spurious warning from -Wint-to-pointer-cast */
675
803
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
676
804
  
677
 
  do{
 
805
  if(quit_now){
 
806
    errno = EINTR;
 
807
    goto mandos_end;
 
808
  }
 
809
  
 
810
  do {
678
811
    ret = gnutls_handshake(session);
 
812
    if(quit_now){
 
813
      errno = EINTR;
 
814
      goto mandos_end;
 
815
    }
679
816
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
680
817
  
681
818
  if(ret != GNUTLS_E_SUCCESS){
683
820
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
684
821
      gnutls_perror(ret);
685
822
    }
686
 
    retval = -1;
 
823
    errno = EPROTO;
687
824
    goto mandos_end;
688
825
  }
689
826
  
695
832
  }
696
833
  
697
834
  while(true){
 
835
    
 
836
    if(quit_now){
 
837
      errno = EINTR;
 
838
      goto mandos_end;
 
839
    }
 
840
    
698
841
    buffer_capacity = incbuffer(&buffer, buffer_length,
699
842
                                   buffer_capacity);
700
843
    if(buffer_capacity == 0){
701
 
      perror("incbuffer");
702
 
      retval = -1;
 
844
      int e = errno;
 
845
      perror_plus("incbuffer");
 
846
      errno = e;
 
847
      goto mandos_end;
 
848
    }
 
849
    
 
850
    if(quit_now){
 
851
      errno = EINTR;
703
852
      goto mandos_end;
704
853
    }
705
854
    
714
863
      case GNUTLS_E_AGAIN:
715
864
        break;
716
865
      case GNUTLS_E_REHANDSHAKE:
717
 
        do{
 
866
        do {
718
867
          ret = gnutls_handshake(session);
 
868
          
 
869
          if(quit_now){
 
870
            errno = EINTR;
 
871
            goto mandos_end;
 
872
          }
719
873
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
720
874
        if(ret < 0){
721
875
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
722
876
          gnutls_perror(ret);
723
 
          retval = -1;
 
877
          errno = EPROTO;
724
878
          goto mandos_end;
725
879
        }
726
880
        break;
727
881
      default:
728
882
        fprintf(stderr, "Unknown error while reading data from"
729
883
                " encrypted session with Mandos server\n");
730
 
        retval = -1;
731
884
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
885
        errno = EIO;
732
886
        goto mandos_end;
733
887
      }
734
888
    } else {
740
894
    fprintf(stderr, "Closing TLS session\n");
741
895
  }
742
896
  
743
 
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
 
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);
744
909
  
745
910
  if(buffer_length > 0){
 
911
    ssize_t decrypted_buffer_size;
746
912
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
747
913
                                               buffer_length,
748
914
                                               &decrypted_buffer);
749
915
    if(decrypted_buffer_size >= 0){
 
916
      
750
917
      written = 0;
751
918
      while(written < (size_t) decrypted_buffer_size){
 
919
        if(quit_now){
 
920
          errno = EINTR;
 
921
          goto mandos_end;
 
922
        }
 
923
        
752
924
        ret = (int)fwrite(decrypted_buffer + written, 1,
753
925
                          (size_t)decrypted_buffer_size - written,
754
926
                          stdout);
755
927
        if(ret == 0 and ferror(stdout)){
 
928
          int e = errno;
756
929
          if(debug){
757
930
            fprintf(stderr, "Error writing encrypted data: %s\n",
758
931
                    strerror(errno));
759
932
          }
760
 
          retval = -1;
761
 
          break;
 
933
          errno = e;
 
934
          goto mandos_end;
762
935
        }
763
936
        written += (size_t)ret;
764
937
      }
765
 
      free(decrypted_buffer);
766
 
    } else {
767
 
      retval = -1;
 
938
      retval = 0;
768
939
    }
769
 
  } else {
770
 
    retval = -1;
771
940
  }
772
941
  
773
942
  /* Shutdown procedure */
774
943
  
775
944
 mandos_end:
776
 
  free(buffer);
777
 
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
778
 
  if(ret == -1){
779
 
    perror("close");
 
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
    }
780
964
  }
781
 
  gnutls_deinit(session);
782
965
  return retval;
783
966
}
784
967
 
801
984
  /* Called whenever a service has been resolved successfully or
802
985
     timed out */
803
986
  
 
987
  if(quit_now){
 
988
    return;
 
989
  }
 
990
  
804
991
  switch(event){
805
992
  default:
806
993
  case AVAHI_RESOLVER_FAILURE:
822
1009
                                           avahi_proto_to_af(proto));
823
1010
      if(ret == 0){
824
1011
        avahi_simple_poll_quit(mc.simple_poll);
 
1012
      } else {
 
1013
        ret = add_server(ip, port, interface,
 
1014
                         avahi_proto_to_af(proto));
825
1015
      }
826
1016
    }
827
1017
  }
843
1033
  /* Called whenever a new services becomes available on the LAN or
844
1034
     is removed from the LAN */
845
1035
  
 
1036
  if(quit_now){
 
1037
    return;
 
1038
  }
 
1039
  
846
1040
  switch(event){
847
1041
  default:
848
1042
  case AVAHI_BROWSER_FAILURE:
877
1071
  }
878
1072
}
879
1073
 
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){
 
1074
/* Signal handler that stops main loop after SIGTERM */
 
1075
static void handle_sigterm(int sig){
884
1076
  if(quit_now){
885
1077
    return;
886
1078
  }
887
1079
  quit_now = 1;
 
1080
  signal_received = sig;
888
1081
  int old_errno = errno;
 
1082
  /* set main loop to exit */
889
1083
  if(mc.simple_poll != NULL){
890
1084
    avahi_simple_poll_quit(mc.simple_poll);
891
1085
  }
892
1086
  errno = old_errno;
893
1087
}
894
1088
 
 
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
 
895
1343
int main(int argc, char *argv[]){
896
1344
  AvahiSServiceBrowser *sb = NULL;
897
1345
  int error;
899
1347
  intmax_t tmpmax;
900
1348
  char *tmp;
901
1349
  int exitcode = EXIT_SUCCESS;
902
 
  const char *interface = "eth0";
 
1350
  const char *interface = "";
903
1351
  struct ifreq network;
904
 
  int sd;
 
1352
  int sd = -1;
 
1353
  bool take_down_interface = false;
905
1354
  uid_t uid;
906
1355
  gid_t gid;
907
 
  char *connect_to = NULL;
908
1356
  char tempdir[] = "/tmp/mandosXXXXXX";
909
1357
  bool tempdir_created = false;
910
1358
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
914
1362
  bool gnutls_initialized = false;
915
1363
  bool gpgme_initialized = false;
916
1364
  float delay = 2.5f;
 
1365
  double retry_interval = 10; /* 10s between trying a server and
 
1366
                                 retrying the same server again */
917
1367
  
918
 
  struct sigaction old_sigterm_action;
 
1368
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
919
1369
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
920
1370
  
 
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
  
921
1392
  {
922
1393
    struct argp_option options[] = {
923
1394
      { .name = "debug", .key = 128,
952
1423
        .arg = "SECONDS",
953
1424
        .doc = "Maximum delay to wait for interface startup",
954
1425
        .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 },
955
1439
      { .name = NULL }
956
1440
    };
957
1441
    
958
1442
    error_t parse_opt(int key, char *arg,
959
1443
                      struct argp_state *state){
 
1444
      errno = 0;
960
1445
      switch(key){
961
1446
      case 128:                 /* --debug */
962
1447
        debug = true;
978
1463
        tmpmax = strtoimax(arg, &tmp, 10);
979
1464
        if(errno != 0 or tmp == arg or *tmp != '\0'
980
1465
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
981
 
          fprintf(stderr, "Bad number of DH bits\n");
982
 
          exit(EXIT_FAILURE);
 
1466
          argp_error(state, "Bad number of DH bits");
983
1467
        }
984
1468
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
985
1469
        break;
990
1474
        errno = 0;
991
1475
        delay = strtof(arg, &tmp);
992
1476
        if(errno != 0 or tmp == arg or *tmp != '\0'){
993
 
          fprintf(stderr, "Bad delay\n");
994
 
          exit(EXIT_FAILURE);
 
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");
995
1486
        }
996
1487
        break;
997
 
      case ARGP_KEY_ARG:
998
 
        argp_usage(state);
999
 
      case ARGP_KEY_END:
 
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);
1000
1501
        break;
1001
1502
      default:
1002
1503
        return ARGP_ERR_UNKNOWN;
1003
1504
      }
1004
 
      return 0;
 
1505
      return errno;
1005
1506
    }
1006
1507
    
1007
1508
    struct argp argp = { .options = options, .parser = parse_opt,
1008
1509
                         .args_doc = "",
1009
1510
                         .doc = "Mandos client -- Get and decrypt"
1010
1511
                         " passwords from a Mandos server" };
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
 
    }
 
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);
1017
1624
  }
1018
1625
  
1019
1626
  if(not debug){
1020
1627
    avahi_set_log_function(empty_log);
1021
1628
  }
1022
1629
  
 
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
  
1023
1662
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1024
1663
     from the signal handler */
1025
1664
  /* Initialize the pseudo-RNG for Avahi */
1027
1666
  mc.simple_poll = avahi_simple_poll_new();
1028
1667
  if(mc.simple_poll == NULL){
1029
1668
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1030
 
    exitcode = EXIT_FAILURE;
 
1669
    exitcode = EX_UNAVAILABLE;
1031
1670
    goto end;
1032
1671
  }
1033
1672
  
1034
1673
  sigemptyset(&sigterm_action.sa_mask);
1035
1674
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1036
1675
  if(ret == -1){
1037
 
    perror("sigaddset");
1038
 
    exitcode = EXIT_FAILURE;
 
1676
    perror_plus("sigaddset");
 
1677
    exitcode = EX_OSERR;
1039
1678
    goto end;
1040
1679
  }
1041
1680
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1042
1681
  if(ret == -1){
1043
 
    perror("sigaddset");
1044
 
    exitcode = EXIT_FAILURE;
 
1682
    perror_plus("sigaddset");
 
1683
    exitcode = EX_OSERR;
1045
1684
    goto end;
1046
1685
  }
1047
1686
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1048
1687
  if(ret == -1){
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
 
  }  
 
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
  }
1059
1735
  
1060
1736
  /* If the interface is down, bring it up */
1061
 
  if(interface[0] != '\0'){
 
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
    
1062
1756
#ifdef __linux__
1063
1757
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1064
 
       messages to mess up the prompt */
 
1758
       messages about the network interface to mess up the prompt */
1065
1759
    ret = klogctl(8, NULL, 5);
1066
1760
    bool restore_loglevel = true;
1067
1761
    if(ret == -1){
1068
1762
      restore_loglevel = false;
1069
 
      perror("klogctl");
 
1763
      perror_plus("klogctl");
1070
1764
    }
1071
1765
#endif  /* __linux__ */
1072
1766
    
1073
1767
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1074
1768
    if(sd < 0){
1075
 
      perror("socket");
1076
 
      exitcode = EXIT_FAILURE;
 
1769
      perror_plus("socket");
 
1770
      exitcode = EX_OSERR;
1077
1771
#ifdef __linux__
1078
1772
      if(restore_loglevel){
1079
1773
        ret = klogctl(7, NULL, 0);
1080
1774
        if(ret == -1){
1081
 
          perror("klogctl");
 
1775
          perror_plus("klogctl");
1082
1776
        }
1083
1777
      }
1084
1778
#endif  /* __linux__ */
 
1779
      /* Lower privileges */
 
1780
      errno = 0;
 
1781
      ret = seteuid(uid);
 
1782
      if(ret == -1){
 
1783
        perror_plus("seteuid");
 
1784
      }
1085
1785
      goto end;
1086
1786
    }
1087
1787
    strcpy(network.ifr_name, interface);
1088
1788
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1089
1789
    if(ret == -1){
1090
 
      perror("ioctl SIOCGIFFLAGS");
 
1790
      perror_plus("ioctl SIOCGIFFLAGS");
1091
1791
#ifdef __linux__
1092
1792
      if(restore_loglevel){
1093
1793
        ret = klogctl(7, NULL, 0);
1094
1794
        if(ret == -1){
1095
 
          perror("klogctl");
 
1795
          perror_plus("klogctl");
1096
1796
        }
1097
1797
      }
1098
1798
#endif  /* __linux__ */
1099
 
      exitcode = EXIT_FAILURE;
 
1799
      exitcode = EX_OSERR;
 
1800
      /* Lower privileges */
 
1801
      errno = 0;
 
1802
      ret = seteuid(uid);
 
1803
      if(ret == -1){
 
1804
        perror_plus("seteuid");
 
1805
      }
1100
1806
      goto end;
1101
1807
    }
1102
1808
    if((network.ifr_flags & IFF_UP) == 0){
1103
1809
      network.ifr_flags |= IFF_UP;
 
1810
      take_down_interface = true;
1104
1811
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1105
1812
      if(ret == -1){
1106
 
        perror("ioctl SIOCSIFFLAGS");
1107
 
        exitcode = EXIT_FAILURE;
 
1813
        take_down_interface = false;
 
1814
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1815
        exitcode = EX_OSERR;
1108
1816
#ifdef __linux__
1109
1817
        if(restore_loglevel){
1110
1818
          ret = klogctl(7, NULL, 0);
1111
1819
          if(ret == -1){
1112
 
            perror("klogctl");
 
1820
            perror_plus("klogctl");
1113
1821
          }
1114
1822
        }
1115
1823
#endif  /* __linux__ */
 
1824
        /* Lower privileges */
 
1825
        errno = 0;
 
1826
        ret = seteuid(uid);
 
1827
        if(ret == -1){
 
1828
          perror_plus("seteuid");
 
1829
        }
1116
1830
        goto end;
1117
1831
      }
1118
1832
    }
1119
 
    /* sleep checking until interface is running */
 
1833
    /* Sleep checking until interface is running.
 
1834
       Check every 0.25s, up to total time of delay */
1120
1835
    for(int i=0; i < delay * 4; i++){
1121
1836
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1122
1837
      if(ret == -1){
1123
 
        perror("ioctl SIOCGIFFLAGS");
 
1838
        perror_plus("ioctl SIOCGIFFLAGS");
1124
1839
      } else if(network.ifr_flags & IFF_RUNNING){
1125
1840
        break;
1126
1841
      }
1127
1842
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1128
1843
      ret = nanosleep(&sleeptime, NULL);
1129
1844
      if(ret == -1 and errno != EINTR){
1130
 
        perror("nanosleep");
 
1845
        perror_plus("nanosleep");
1131
1846
      }
1132
1847
    }
1133
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1134
 
    if(ret == -1){
1135
 
      perror("close");
 
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
      }
1136
1854
    }
1137
1855
#ifdef __linux__
1138
1856
    if(restore_loglevel){
1139
1857
      /* Restores kernel loglevel to default */
1140
1858
      ret = klogctl(7, NULL, 0);
1141
1859
      if(ret == -1){
1142
 
        perror("klogctl");
 
1860
        perror_plus("klogctl");
1143
1861
      }
1144
1862
    }
1145
1863
#endif  /* __linux__ */
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");
 
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;
1160
1883
  }
1161
1884
  
1162
1885
  ret = init_gnutls_global(pubkey, seckey);
1163
1886
  if(ret == -1){
1164
1887
    fprintf(stderr, "init_gnutls_global failed\n");
1165
 
    exitcode = EXIT_FAILURE;
 
1888
    exitcode = EX_UNAVAILABLE;
1166
1889
    goto end;
1167
1890
  } else {
1168
1891
    gnutls_initialized = true;
1169
1892
  }
1170
1893
  
 
1894
  if(quit_now){
 
1895
    goto end;
 
1896
  }
 
1897
  
1171
1898
  if(mkdtemp(tempdir) == NULL){
1172
 
    perror("mkdtemp");
 
1899
    perror_plus("mkdtemp");
1173
1900
    goto end;
1174
1901
  }
1175
1902
  tempdir_created = true;
1176
1903
  
 
1904
  if(quit_now){
 
1905
    goto end;
 
1906
  }
 
1907
  
1177
1908
  if(not init_gpgme(pubkey, seckey, tempdir)){
1178
1909
    fprintf(stderr, "init_gpgme failed\n");
1179
 
    exitcode = EXIT_FAILURE;
 
1910
    exitcode = EX_UNAVAILABLE;
1180
1911
    goto end;
1181
1912
  } else {
1182
1913
    gpgme_initialized = true;
1183
1914
  }
1184
1915
  
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
 
    }
 
1916
  if(quit_now){
 
1917
    goto end;
1192
1918
  }
1193
1919
  
1194
1920
  if(connect_to != NULL){
1197
1923
    char *address = strrchr(connect_to, ':');
1198
1924
    if(address == NULL){
1199
1925
      fprintf(stderr, "No colon in address\n");
1200
 
      exitcode = EXIT_FAILURE;
1201
 
      goto end;
1202
 
    }
 
1926
      exitcode = EX_USAGE;
 
1927
      goto end;
 
1928
    }
 
1929
    
 
1930
    if(quit_now){
 
1931
      goto end;
 
1932
    }
 
1933
    
1203
1934
    uint16_t port;
1204
1935
    errno = 0;
1205
1936
    tmpmax = strtoimax(address+1, &tmp, 10);
1206
1937
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1207
1938
       or tmpmax != (uint16_t)tmpmax){
1208
1939
      fprintf(stderr, "Bad port number\n");
1209
 
      exitcode = EXIT_FAILURE;
1210
 
      goto end;
1211
 
    }
 
1940
      exitcode = EX_USAGE;
 
1941
      goto end;
 
1942
    }
 
1943
  
 
1944
    if(quit_now){
 
1945
      goto end;
 
1946
    }
 
1947
    
1212
1948
    port = (uint16_t)tmpmax;
1213
1949
    *address = '\0';
1214
 
    address = connect_to;
1215
1950
    /* Colon in address indicates IPv6 */
1216
1951
    int af;
1217
 
    if(strchr(address, ':') != NULL){
 
1952
    if(strchr(connect_to, ':') != NULL){
1218
1953
      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
        }
1219
1960
    } else {
1220
1961
      af = AF_INET;
1221
1962
    }
1222
 
    ret = start_mandos_communication(address, port, if_index, af);
1223
 
    if(ret < 0){
1224
 
      exitcode = EXIT_FAILURE;
1225
 
    } else {
 
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){
1226
1982
      exitcode = EXIT_SUCCESS;
1227
1983
    }
1228
 
    goto end;
1229
 
  }
1230
1984
    
 
1985
    goto end;
 
1986
  }
 
1987
  
 
1988
  if(quit_now){
 
1989
    goto end;
 
1990
  }
 
1991
  
1231
1992
  {
1232
1993
    AvahiServerConfig config;
1233
1994
    /* Do not publish any local Zeroconf records */
1250
2011
  if(mc.server == NULL){
1251
2012
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1252
2013
            avahi_strerror(error));
1253
 
    exitcode = EXIT_FAILURE;
 
2014
    exitcode = EX_UNAVAILABLE;
 
2015
    goto end;
 
2016
  }
 
2017
  
 
2018
  if(quit_now){
1254
2019
    goto end;
1255
2020
  }
1256
2021
  
1261
2026
  if(sb == NULL){
1262
2027
    fprintf(stderr, "Failed to create service browser: %s\n",
1263
2028
            avahi_strerror(avahi_server_errno(mc.server)));
1264
 
    exitcode = EXIT_FAILURE;
 
2029
    exitcode = EX_UNAVAILABLE;
 
2030
    goto end;
 
2031
  }
 
2032
  
 
2033
  if(quit_now){
1265
2034
    goto end;
1266
2035
  }
1267
2036
  
1270
2039
  if(debug){
1271
2040
    fprintf(stderr, "Starting Avahi loop search\n");
1272
2041
  }
1273
 
  
1274
 
  avahi_simple_poll_loop(mc.simple_poll);
 
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
  }
1275
2049
  
1276
2050
 end:
1277
2051
  
1298
2072
  if(gpgme_initialized){
1299
2073
    gpgme_release(mc.ctx);
1300
2074
  }
1301
 
  
1302
 
  /* Removes the temp directory used by GPGME */
 
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 */
1303
2122
  if(tempdir_created){
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
 
        }
 
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];
1324
2130
        char *fullname = NULL;
1325
2131
        ret = asprintf(&fullname, "%s/%s", tempdir,
1326
2132
                       direntry->d_name);
1327
2133
        if(ret < 0){
1328
 
          perror("asprintf");
 
2134
          perror_plus("asprintf");
1329
2135
          continue;
1330
2136
        }
1331
2137
        ret = remove(fullname);
1335
2141
        }
1336
2142
        free(fullname);
1337
2143
      }
1338
 
      closedir(d);
 
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");
1339
2150
    }
1340
2151
    ret = rmdir(tempdir);
1341
2152
    if(ret == -1 and errno != ENOENT){
1342
 
      perror("rmdir");
1343
 
    }
 
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());
1344
2174
  }
1345
2175
  
1346
2176
  return exitcode;