/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2014-07-17 03:24:57 UTC
  • Revision ID: teddy@recompile.se-20140717032457-7074pobx7ft1oiio
Tags: version-1.6.7-1
* Makefile (version): Changed to "1.6.7".
* NEWS (Version 1.6.7): New entry.
* debian/changelog (1.6.7-1): - '' -

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-2014 Teddy Hogeborn
 
13
 * Copyright © 2008-2014 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
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
33
33
#ifndef _LARGEFILE_SOURCE
34
34
#define _LARGEFILE_SOURCE
35
 
#endif
 
35
#endif  /* not _LARGEFILE_SOURCE */
36
36
#ifndef _FILE_OFFSET_BITS
37
37
#define _FILE_OFFSET_BITS 64
38
 
#endif
 
38
#endif  /* not _FILE_OFFSET_BITS */
39
39
 
40
40
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
41
41
 
42
42
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
 
                                   stdout, ferror(), remove() */
44
 
#include <stdint.h>             /* uint16_t, uint32_t */
 
43
                                   stdout, ferror() */
 
44
#include <stdint.h>             /* uint16_t, uint32_t, intptr_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
47
                                   strtof(), abort() */
55
55
                                   opendir(), DIR */
56
56
#include <sys/stat.h>           /* open(), S_ISREG */
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
 
                                   inet_pton(), connect() */
59
 
#include <fcntl.h>              /* open() */
 
58
                                   inet_pton(), connect(),
 
59
                                   getnameinfo() */
 
60
#include <fcntl.h>              /* open(), unlinkat() */
60
61
#include <dirent.h>             /* opendir(), struct dirent, readdir()
61
62
                                 */
62
63
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
64
                                   strtoimax() */
64
 
#include <assert.h>             /* assert() */
65
65
#include <errno.h>              /* perror(), errno,
66
66
                                   program_invocation_short_name */
67
67
#include <time.h>               /* nanosleep(), time(), sleep() */
73
73
                                */
74
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
75
                                   getuid(), getgid(), seteuid(),
76
 
                                   setgid(), pause(), _exit() */
77
 
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
 
76
                                   setgid(), pause(), _exit(),
 
77
                                   unlinkat() */
 
78
#include <arpa/inet.h>          /* inet_pton(), htons() */
78
79
#include <iso646.h>             /* not, or, and */
79
80
#include <argp.h>               /* struct argp_option, error_t, struct
80
81
                                   argp_state, struct argp,
88
89
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
89
90
                                   WEXITSTATUS(), WTERMSIG() */
90
91
#include <grp.h>                /* setgroups() */
 
92
#include <argz.h>               /* argz_add_sep(), argz_next(),
 
93
                                   argz_delete(), argz_append(),
 
94
                                   argz_stringify(), argz_add(),
 
95
                                   argz_count() */
 
96
#include <netdb.h>              /* getnameinfo(), NI_NUMERICHOST,
 
97
                                   EAI_SYSTEM, gai_strerror() */
91
98
 
92
99
#ifdef __linux__
93
100
#include <sys/klog.h>           /* klogctl() */
135
142
static const char sys_class_net[] = "/sys/class/net";
136
143
char *connect_to = NULL;
137
144
const char *hookdir = HOOKDIR;
 
145
int hookdir_fd = -1;
 
146
uid_t uid = 65534;
 
147
gid_t gid = 65534;
138
148
 
139
149
/* Doubly linked list that need to be circularly linked when used */
140
150
typedef struct server{
141
151
  const char *ip;
142
 
  uint16_t port;
 
152
  in_port_t port;
143
153
  AvahiIfIndex if_index;
144
154
  int af;
145
155
  struct timespec last_seen;
149
159
 
150
160
/* Used for passing in values through the Avahi callback functions */
151
161
typedef struct {
152
 
  AvahiSimplePoll *simple_poll;
153
162
  AvahiServer *server;
154
163
  gnutls_certificate_credentials_t cred;
155
164
  unsigned int dh_bits;
157
166
  const char *priority;
158
167
  gpgme_ctx_t ctx;
159
168
  server *current_server;
 
169
  char *interfaces;
 
170
  size_t interfaces_size;
160
171
} mandos_context;
161
172
 
162
 
/* global context so signal handler can reach it*/
163
 
mandos_context mc = { .simple_poll = NULL, .server = NULL,
164
 
                      .dh_bits = 1024, .priority = "SECURE256"
165
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
166
 
                      .current_server = NULL };
 
173
/* global so signal handler can reach it*/
 
174
AvahiSimplePoll *simple_poll;
167
175
 
168
176
sig_atomic_t quit_now = 0;
169
177
int signal_received = 0;
170
178
 
171
179
/* Function to use when printing errors */
172
180
void perror_plus(const char *print_text){
 
181
  int e = errno;
173
182
  fprintf(stderr, "Mandos plugin %s: ",
174
183
          program_invocation_short_name);
 
184
  errno = e;
175
185
  perror(print_text);
176
186
}
177
187
 
178
 
__attribute__((format (gnu_printf, 2, 3)))
 
188
__attribute__((format (gnu_printf, 2, 3), nonnull))
179
189
int fprintf_plus(FILE *stream, const char *format, ...){
180
190
  va_list ap;
181
191
  va_start (ap, format);
182
192
  
183
193
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
184
194
                             program_invocation_short_name));
185
 
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
 
195
  return (int)TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
186
196
}
187
197
 
188
198
/*
190
200
 * bytes. "buffer_capacity" is how much is currently allocated,
191
201
 * "buffer_length" is how much is already used.
192
202
 */
 
203
__attribute__((nonnull, warn_unused_result))
193
204
size_t incbuffer(char **buffer, size_t buffer_length,
194
205
                 size_t buffer_capacity){
195
206
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
196
 
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
197
 
    if(buffer == NULL){
 
207
    char *new_buf = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
 
208
    if(new_buf == NULL){
 
209
      int old_errno = errno;
 
210
      free(*buffer);
 
211
      errno = old_errno;
 
212
      *buffer = NULL;
198
213
      return 0;
199
214
    }
 
215
    *buffer = new_buf;
200
216
    buffer_capacity += BUFFER_SIZE;
201
217
  }
202
218
  return buffer_capacity;
203
219
}
204
220
 
205
221
/* Add server to set of servers to retry periodically */
206
 
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
207
 
                int af){
 
222
__attribute__((nonnull, warn_unused_result))
 
223
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
 
224
                int af, server **current_server){
208
225
  int ret;
209
226
  server *new_server = malloc(sizeof(server));
210
227
  if(new_server == NULL){
217
234
                          .af = af };
218
235
  if(new_server->ip == NULL){
219
236
    perror_plus("strdup");
 
237
    free(new_server);
 
238
    return false;
 
239
  }
 
240
  ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
 
241
  if(ret == -1){
 
242
    perror_plus("clock_gettime");
 
243
#ifdef __GNUC__
 
244
#pragma GCC diagnostic push
 
245
#pragma GCC diagnostic ignored "-Wcast-qual"
 
246
#endif
 
247
    free((char *)(new_server->ip));
 
248
#ifdef __GNUC__
 
249
#pragma GCC diagnostic pop
 
250
#endif
 
251
    free(new_server);
220
252
    return false;
221
253
  }
222
254
  /* Special case of first server */
223
 
  if (mc.current_server == NULL){
 
255
  if(*current_server == NULL){
224
256
    new_server->next = new_server;
225
257
    new_server->prev = new_server;
226
 
    mc.current_server = new_server;
227
 
  /* Place the new server last in the list */
 
258
    *current_server = new_server;
228
259
  } else {
229
 
    new_server->next = mc.current_server;
230
 
    new_server->prev = mc.current_server->prev;
 
260
    /* Place the new server last in the list */
 
261
    new_server->next = *current_server;
 
262
    new_server->prev = (*current_server)->prev;
231
263
    new_server->prev->next = new_server;
232
 
    mc.current_server->prev = new_server;
233
 
  }
234
 
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
235
 
  if(ret == -1){
236
 
    perror_plus("clock_gettime");
237
 
    return false;
 
264
    (*current_server)->prev = new_server;
238
265
  }
239
266
  return true;
240
267
}
242
269
/* 
243
270
 * Initialize GPGME.
244
271
 */
245
 
static bool init_gpgme(const char *seckey, const char *pubkey,
246
 
                       const char *tempdir){
 
272
__attribute__((nonnull, warn_unused_result))
 
273
static bool init_gpgme(const char * const seckey,
 
274
                       const char * const pubkey,
 
275
                       const char * const tempdir,
 
276
                       mandos_context *mc){
247
277
  gpgme_error_t rc;
248
278
  gpgme_engine_info_t engine_info;
249
279
  
250
 
  
251
280
  /*
252
281
   * Helper function to insert pub and seckey to the engine keyring.
253
282
   */
254
 
  bool import_key(const char *filename){
 
283
  bool import_key(const char * const filename){
255
284
    int ret;
256
285
    int fd;
257
286
    gpgme_data_t pgp_data;
269
298
      return false;
270
299
    }
271
300
    
272
 
    rc = gpgme_op_import(mc.ctx, pgp_data);
 
301
    rc = gpgme_op_import(mc->ctx, pgp_data);
273
302
    if(rc != GPG_ERR_NO_ERROR){
274
303
      fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
275
304
                   gpgme_strsource(rc), gpgme_strerror(rc));
319
348
  }
320
349
  
321
350
  /* Create new GPGME "context" */
322
 
  rc = gpgme_new(&(mc.ctx));
 
351
  rc = gpgme_new(&(mc->ctx));
323
352
  if(rc != GPG_ERR_NO_ERROR){
324
353
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
325
354
                 "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
338
367
 * Decrypt OpenPGP data.
339
368
 * Returns -1 on error
340
369
 */
 
370
__attribute__((nonnull, warn_unused_result))
341
371
static ssize_t pgp_packet_decrypt(const char *cryptotext,
342
372
                                  size_t crypto_size,
343
 
                                  char **plaintext){
 
373
                                  char **plaintext,
 
374
                                  mandos_context *mc){
344
375
  gpgme_data_t dh_crypto, dh_plain;
345
376
  gpgme_error_t rc;
346
377
  ssize_t ret;
372
403
  
373
404
  /* Decrypt data from the cryptotext data buffer to the plaintext
374
405
     data buffer */
375
 
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
 
406
  rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
376
407
  if(rc != GPG_ERR_NO_ERROR){
377
408
    fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
378
409
                 gpgme_strsource(rc), gpgme_strerror(rc));
379
410
    plaintext_length = -1;
380
411
    if(debug){
381
412
      gpgme_decrypt_result_t result;
382
 
      result = gpgme_op_decrypt_result(mc.ctx);
 
413
      result = gpgme_op_decrypt_result(mc->ctx);
383
414
      if(result == NULL){
384
415
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
385
416
      } else {
462
493
  return plaintext_length;
463
494
}
464
495
 
465
 
static const char * safer_gnutls_strerror(int value){
466
 
  const char *ret = gnutls_strerror(value); /* Spurious warning from
467
 
                                               -Wunreachable-code */
 
496
__attribute__((warn_unused_result))
 
497
static const char *safer_gnutls_strerror(int value){
 
498
  const char *ret = gnutls_strerror(value);
468
499
  if(ret == NULL)
469
500
    ret = "(unknown)";
470
501
  return ret;
471
502
}
472
503
 
473
504
/* GnuTLS log function callback */
 
505
__attribute__((nonnull))
474
506
static void debuggnutls(__attribute__((unused)) int level,
475
507
                        const char* string){
476
508
  fprintf_plus(stderr, "GnuTLS: %s", string);
477
509
}
478
510
 
 
511
__attribute__((nonnull, warn_unused_result))
479
512
static int init_gnutls_global(const char *pubkeyfilename,
480
 
                              const char *seckeyfilename){
 
513
                              const char *seckeyfilename,
 
514
                              mandos_context *mc){
481
515
  int ret;
482
516
  
483
517
  if(debug){
500
534
  }
501
535
  
502
536
  /* OpenPGP credentials */
503
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
537
  ret = gnutls_certificate_allocate_credentials(&mc->cred);
504
538
  if(ret != GNUTLS_E_SUCCESS){
505
539
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
506
540
                 safer_gnutls_strerror(ret));
516
550
  }
517
551
  
518
552
  ret = gnutls_certificate_set_openpgp_key_file
519
 
    (mc.cred, pubkeyfilename, seckeyfilename,
 
553
    (mc->cred, pubkeyfilename, seckeyfilename,
520
554
     GNUTLS_OPENPGP_FMT_BASE64);
521
555
  if(ret != GNUTLS_E_SUCCESS){
522
556
    fprintf_plus(stderr,
528
562
  }
529
563
  
530
564
  /* GnuTLS server initialization */
531
 
  ret = gnutls_dh_params_init(&mc.dh_params);
 
565
  ret = gnutls_dh_params_init(&mc->dh_params);
532
566
  if(ret != GNUTLS_E_SUCCESS){
533
567
    fprintf_plus(stderr, "Error in GnuTLS DH parameter"
534
568
                 " initialization: %s\n",
535
569
                 safer_gnutls_strerror(ret));
536
570
    goto globalfail;
537
571
  }
538
 
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
 
572
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
539
573
  if(ret != GNUTLS_E_SUCCESS){
540
574
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
541
575
                 safer_gnutls_strerror(ret));
542
576
    goto globalfail;
543
577
  }
544
578
  
545
 
  gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
 
579
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
546
580
  
547
581
  return 0;
548
582
  
549
583
 globalfail:
550
584
  
551
 
  gnutls_certificate_free_credentials(mc.cred);
 
585
  gnutls_certificate_free_credentials(mc->cred);
552
586
  gnutls_global_deinit();
553
 
  gnutls_dh_params_deinit(mc.dh_params);
 
587
  gnutls_dh_params_deinit(mc->dh_params);
554
588
  return -1;
555
589
}
556
590
 
557
 
static int init_gnutls_session(gnutls_session_t *session){
 
591
__attribute__((nonnull, warn_unused_result))
 
592
static int init_gnutls_session(gnutls_session_t *session,
 
593
                               mandos_context *mc){
558
594
  int ret;
559
595
  /* GnuTLS session creation */
560
596
  do {
572
608
  {
573
609
    const char *err;
574
610
    do {
575
 
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
 
611
      ret = gnutls_priority_set_direct(*session, mc->priority, &err);
576
612
      if(quit_now){
577
613
        gnutls_deinit(*session);
578
614
        return -1;
589
625
  
590
626
  do {
591
627
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
592
 
                                 mc.cred);
 
628
                                 mc->cred);
593
629
    if(quit_now){
594
630
      gnutls_deinit(*session);
595
631
      return -1;
605
641
  /* ignore client certificate if any. */
606
642
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
607
643
  
608
 
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
 
644
  gnutls_dh_set_prime_bits(*session, mc->dh_bits);
609
645
  
610
646
  return 0;
611
647
}
615
651
                      __attribute__((unused)) const char *txt){}
616
652
 
617
653
/* Called when a Mandos server is found */
618
 
static int start_mandos_communication(const char *ip, uint16_t port,
 
654
__attribute__((nonnull, warn_unused_result))
 
655
static int start_mandos_communication(const char *ip, in_port_t port,
619
656
                                      AvahiIfIndex if_index,
620
 
                                      int af){
 
657
                                      int af, mandos_context *mc){
621
658
  int ret, tcp_sd = -1;
622
659
  ssize_t sret;
623
 
  union {
624
 
    struct sockaddr_in in;
625
 
    struct sockaddr_in6 in6;
626
 
  } to;
 
660
  struct sockaddr_storage to;
627
661
  char *buffer = NULL;
628
662
  char *decrypted_buffer = NULL;
629
663
  size_t buffer_length = 0;
653
687
    return -1;
654
688
  }
655
689
  
656
 
  ret = init_gnutls_session(&session);
 
690
  /* If the interface is specified and we have a list of interfaces */
 
691
  if(if_index != AVAHI_IF_UNSPEC and mc->interfaces != NULL){
 
692
    /* Check if the interface is one of the interfaces we are using */
 
693
    bool match = false;
 
694
    {
 
695
      char *interface = NULL;
 
696
      while((interface=argz_next(mc->interfaces, mc->interfaces_size,
 
697
                                 interface))){
 
698
        if(if_nametoindex(interface) == (unsigned int)if_index){
 
699
          match = true;
 
700
          break;
 
701
        }
 
702
      }
 
703
    }
 
704
    if(not match){
 
705
      /* This interface does not match any in the list, so we don't
 
706
         connect to the server */
 
707
      if(debug){
 
708
        char interface[IF_NAMESIZE];
 
709
        if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
710
          perror_plus("if_indextoname");
 
711
        } else {
 
712
          fprintf_plus(stderr, "Skipping server on non-used interface"
 
713
                       " \"%s\"\n",
 
714
                       if_indextoname((unsigned int)if_index,
 
715
                                      interface));
 
716
        }
 
717
      }
 
718
      return -1;
 
719
    }
 
720
  }
 
721
  
 
722
  ret = init_gnutls_session(&session, mc);
657
723
  if(ret != 0){
658
724
    return -1;
659
725
  }
660
726
  
661
727
  if(debug){
662
728
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
663
 
                 PRIu16 "\n", ip, port);
 
729
                 PRIuMAX "\n", ip, (uintmax_t)port);
664
730
  }
665
731
  
666
732
  tcp_sd = socket(pf, SOCK_STREAM, 0);
678
744
  
679
745
  memset(&to, 0, sizeof(to));
680
746
  if(af == AF_INET6){
681
 
    to.in6.sin6_family = (sa_family_t)af;
682
 
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
 
747
    ((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
 
748
    ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
683
749
  } else {                      /* IPv4 */
684
 
    to.in.sin_family = (sa_family_t)af;
685
 
    ret = inet_pton(af, ip, &to.in.sin_addr);
 
750
    ((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
 
751
    ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
686
752
  }
687
753
  if(ret < 0 ){
688
754
    int e = errno;
697
763
    goto mandos_end;
698
764
  }
699
765
  if(af == AF_INET6){
700
 
    to.in6.sin6_port = htons(port); /* Spurious warnings from
701
 
                                       -Wconversion and
702
 
                                       -Wunreachable-code */
703
 
    
704
 
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
705
 
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
706
 
                                -Wunreachable-code*/
 
766
    ((struct sockaddr_in6 *)&to)->sin6_port = htons(port);
 
767
    if(IN6_IS_ADDR_LINKLOCAL
 
768
       (&((struct sockaddr_in6 *)&to)->sin6_addr)){
707
769
      if(if_index == AVAHI_IF_UNSPEC){
708
770
        fprintf_plus(stderr, "An IPv6 link-local address is"
709
771
                     " incomplete without a network interface\n");
711
773
        goto mandos_end;
712
774
      }
713
775
      /* Set the network interface number as scope */
714
 
      to.in6.sin6_scope_id = (uint32_t)if_index;
 
776
      ((struct sockaddr_in6 *)&to)->sin6_scope_id = (uint32_t)if_index;
715
777
    }
716
778
  } else {
717
 
    to.in.sin_port = htons(port); /* Spurious warnings from
718
 
                                     -Wconversion and
719
 
                                     -Wunreachable-code */
 
779
    ((struct sockaddr_in *)&to)->sin_port = htons(port);
720
780
  }
721
781
  
722
782
  if(quit_now){
730
790
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
731
791
        perror_plus("if_indextoname");
732
792
      } else {
733
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
734
 
                     "\n", ip, interface, port);
 
793
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
 
794
                     "\n", ip, interface, (uintmax_t)port);
735
795
      }
736
796
    } else {
737
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
738
 
                   ip, port);
 
797
      fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
 
798
                   ip, (uintmax_t)port);
739
799
    }
740
800
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
741
801
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
742
 
    const char *pcret;
743
802
    if(af == AF_INET6){
744
 
      pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
745
 
                        sizeof(addrstr));
 
803
      ret = getnameinfo((struct sockaddr *)&to,
 
804
                        sizeof(struct sockaddr_in6),
 
805
                        addrstr, sizeof(addrstr), NULL, 0,
 
806
                        NI_NUMERICHOST);
746
807
    } else {
747
 
      pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
748
 
                        sizeof(addrstr));
 
808
      ret = getnameinfo((struct sockaddr *)&to,
 
809
                        sizeof(struct sockaddr_in),
 
810
                        addrstr, sizeof(addrstr), NULL, 0,
 
811
                        NI_NUMERICHOST);
749
812
    }
750
 
    if(pcret == NULL){
751
 
      perror_plus("inet_ntop");
752
 
    } else {
753
 
      if(strcmp(addrstr, ip) != 0){
754
 
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
755
 
      }
 
813
    if(ret == EAI_SYSTEM){
 
814
      perror_plus("getnameinfo");
 
815
    } else if(ret != 0) {
 
816
      fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
 
817
    } else if(strcmp(addrstr, ip) != 0){
 
818
      fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
756
819
    }
757
820
  }
758
821
  
762
825
  }
763
826
  
764
827
  if(af == AF_INET6){
765
 
    ret = connect(tcp_sd, &to.in6, sizeof(to));
 
828
    ret = connect(tcp_sd, (struct sockaddr *)&to,
 
829
                  sizeof(struct sockaddr_in6));
766
830
  } else {
767
 
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
 
831
    ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
 
832
                  sizeof(struct sockaddr_in));
768
833
  }
769
834
  if(ret < 0){
770
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
835
    if((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
771
836
      int e = errno;
772
837
      perror_plus("connect");
773
838
      errno = e;
819
884
    goto mandos_end;
820
885
  }
821
886
  
822
 
  /* Spurious warning from -Wint-to-pointer-cast */
823
 
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
 
887
  /* This casting via intptr_t is to eliminate warning about casting
 
888
     an int to a pointer type.  This is exactly how the GnuTLS Guile
 
889
     function "set-session-transport-fd!" does it. */
 
890
  gnutls_transport_set_ptr(session,
 
891
                           (gnutls_transport_ptr_t)(intptr_t)tcp_sd);
824
892
  
825
893
  if(quit_now){
826
894
    errno = EINTR;
931
999
  if(buffer_length > 0){
932
1000
    ssize_t decrypted_buffer_size;
933
1001
    decrypted_buffer_size = pgp_packet_decrypt(buffer, buffer_length,
934
 
                                               &decrypted_buffer);
 
1002
                                               &decrypted_buffer, mc);
935
1003
    if(decrypted_buffer_size >= 0){
936
1004
      
937
1005
      written = 0;
985
1053
  return retval;
986
1054
}
987
1055
 
 
1056
__attribute__((nonnull))
988
1057
static void resolve_callback(AvahiSServiceResolver *r,
989
1058
                             AvahiIfIndex interface,
990
1059
                             AvahiProtocol proto,
998
1067
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
999
1068
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1000
1069
                             flags,
1001
 
                             AVAHI_GCC_UNUSED void* userdata){
1002
 
  assert(r);
 
1070
                             void *mc){
 
1071
  if(r == NULL){
 
1072
    return;
 
1073
  }
1003
1074
  
1004
1075
  /* Called whenever a service has been resolved successfully or
1005
1076
     timed out */
1006
1077
  
1007
1078
  if(quit_now){
 
1079
    avahi_s_service_resolver_free(r);
1008
1080
    return;
1009
1081
  }
1010
1082
  
1014
1086
    fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
1015
1087
                 "'%s' of type '%s' in domain '%s': %s\n", name, type,
1016
1088
                 domain,
1017
 
                 avahi_strerror(avahi_server_errno(mc.server)));
 
1089
                 avahi_strerror(avahi_server_errno
 
1090
                                (((mandos_context*)mc)->server)));
1018
1091
    break;
1019
1092
    
1020
1093
  case AVAHI_RESOLVER_FOUND:
1026
1099
                     PRIdMAX ") on port %" PRIu16 "\n", name,
1027
1100
                     host_name, ip, (intmax_t)interface, port);
1028
1101
      }
1029
 
      int ret = start_mandos_communication(ip, port, interface,
1030
 
                                           avahi_proto_to_af(proto));
 
1102
      int ret = start_mandos_communication(ip, (in_port_t)port,
 
1103
                                           interface,
 
1104
                                           avahi_proto_to_af(proto),
 
1105
                                           mc);
1031
1106
      if(ret == 0){
1032
 
        avahi_simple_poll_quit(mc.simple_poll);
 
1107
        avahi_simple_poll_quit(simple_poll);
1033
1108
      } else {
1034
 
        if(not add_server(ip, port, interface,
1035
 
                          avahi_proto_to_af(proto))){
 
1109
        if(not add_server(ip, (in_port_t)port, interface,
 
1110
                          avahi_proto_to_af(proto),
 
1111
                          &((mandos_context*)mc)->current_server)){
1036
1112
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1037
1113
                       " list\n", name);
1038
1114
        }
1051
1127
                            const char *domain,
1052
1128
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1053
1129
                            flags,
1054
 
                            AVAHI_GCC_UNUSED void* userdata){
1055
 
  assert(b);
 
1130
                            void *mc){
 
1131
  if(b == NULL){
 
1132
    return;
 
1133
  }
1056
1134
  
1057
1135
  /* Called whenever a new services becomes available on the LAN or
1058
1136
     is removed from the LAN */
1066
1144
  case AVAHI_BROWSER_FAILURE:
1067
1145
    
1068
1146
    fprintf_plus(stderr, "(Avahi browser) %s\n",
1069
 
                 avahi_strerror(avahi_server_errno(mc.server)));
1070
 
    avahi_simple_poll_quit(mc.simple_poll);
 
1147
                 avahi_strerror(avahi_server_errno
 
1148
                                (((mandos_context*)mc)->server)));
 
1149
    avahi_simple_poll_quit(simple_poll);
1071
1150
    return;
1072
1151
    
1073
1152
  case AVAHI_BROWSER_NEW:
1076
1155
       the callback function is called the Avahi server will free the
1077
1156
       resolver for us. */
1078
1157
    
1079
 
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1080
 
                                    name, type, domain, protocol, 0,
1081
 
                                    resolve_callback, NULL) == NULL)
 
1158
    if(avahi_s_service_resolver_new(((mandos_context*)mc)->server,
 
1159
                                    interface, protocol, name, type,
 
1160
                                    domain, protocol, 0,
 
1161
                                    resolve_callback, mc) == NULL)
1082
1162
      fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1083
1163
                   " %s\n", name,
1084
 
                   avahi_strerror(avahi_server_errno(mc.server)));
 
1164
                   avahi_strerror(avahi_server_errno
 
1165
                                  (((mandos_context*)mc)->server)));
1085
1166
    break;
1086
1167
    
1087
1168
  case AVAHI_BROWSER_REMOVE:
1106
1187
  signal_received = sig;
1107
1188
  int old_errno = errno;
1108
1189
  /* set main loop to exit */
1109
 
  if(mc.simple_poll != NULL){
1110
 
    avahi_simple_poll_quit(mc.simple_poll);
 
1190
  if(simple_poll != NULL){
 
1191
    avahi_simple_poll_quit(simple_poll);
1111
1192
  }
1112
1193
  errno = old_errno;
1113
1194
}
1114
1195
 
 
1196
__attribute__((nonnull, warn_unused_result))
1115
1197
bool get_flags(const char *ifname, struct ifreq *ifr){
1116
1198
  int ret;
 
1199
  error_t ret_errno;
1117
1200
  
1118
1201
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1119
1202
  if(s < 0){
 
1203
    ret_errno = errno;
1120
1204
    perror_plus("socket");
 
1205
    errno = ret_errno;
1121
1206
    return false;
1122
1207
  }
1123
1208
  strcpy(ifr->ifr_name, ifname);
1124
1209
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1125
1210
  if(ret == -1){
1126
1211
    if(debug){
 
1212
      ret_errno = errno;
1127
1213
      perror_plus("ioctl SIOCGIFFLAGS");
 
1214
      errno = ret_errno;
1128
1215
    }
1129
1216
    return false;
1130
1217
  }
1131
1218
  return true;
1132
1219
}
1133
1220
 
 
1221
__attribute__((nonnull, warn_unused_result))
1134
1222
bool good_flags(const char *ifname, const struct ifreq *ifr){
1135
1223
  
1136
1224
  /* Reject the loopback device */
1178
1266
 * corresponds to an acceptable network device.
1179
1267
 * (This function is passed to scandir(3) as a filter function.)
1180
1268
 */
 
1269
__attribute__((nonnull, warn_unused_result))
1181
1270
int good_interface(const struct dirent *if_entry){
1182
1271
  if(if_entry->d_name[0] == '.'){
1183
1272
    return 0;
1199
1288
}
1200
1289
 
1201
1290
/* 
1202
 
 * This function determines if a directory entry in /sys/class/net
1203
 
 * corresponds to an acceptable network device which is up.
1204
 
 * (This function is passed to scandir(3) as a filter function.)
1205
 
 */
1206
 
int up_interface(const struct dirent *if_entry){
1207
 
  if(if_entry->d_name[0] == '.'){
1208
 
    return 0;
1209
 
  }
1210
 
  
1211
 
  struct ifreq ifr;
1212
 
  if(not get_flags(if_entry->d_name, &ifr)){
1213
 
    if(debug){
1214
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1215
 
                   "\"%s\"\n", if_entry->d_name);
1216
 
    }
1217
 
    return 0;
1218
 
  }
1219
 
  
1220
 
  /* Reject down interfaces */
1221
 
  if(not (ifr.ifr_flags & IFF_UP)){
1222
 
    if(debug){
1223
 
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1224
 
                   if_entry->d_name);
1225
 
    }
1226
 
    return 0;
1227
 
  }
1228
 
  
1229
 
  /* Reject non-running interfaces */
1230
 
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1231
 
    if(debug){
1232
 
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1233
 
                   if_entry->d_name);
1234
 
    }
1235
 
    return 0;
1236
 
  }
1237
 
  
1238
 
  if(not good_flags(if_entry->d_name, &ifr)){
1239
 
    return 0;
1240
 
  }
1241
 
  return 1;
1242
 
}
1243
 
 
 
1291
 * This function determines if a network interface is up.
 
1292
 */
 
1293
__attribute__((nonnull, warn_unused_result))
 
1294
bool interface_is_up(const char *interface){
 
1295
  struct ifreq ifr;
 
1296
  if(not get_flags(interface, &ifr)){
 
1297
    if(debug){
 
1298
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1299
                   "\"%s\"\n", interface);
 
1300
    }
 
1301
    return false;
 
1302
  }
 
1303
  
 
1304
  return (bool)(ifr.ifr_flags & IFF_UP);
 
1305
}
 
1306
 
 
1307
/* 
 
1308
 * This function determines if a network interface is running
 
1309
 */
 
1310
__attribute__((nonnull, warn_unused_result))
 
1311
bool interface_is_running(const char *interface){
 
1312
  struct ifreq ifr;
 
1313
  if(not get_flags(interface, &ifr)){
 
1314
    if(debug){
 
1315
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1316
                   "\"%s\"\n", interface);
 
1317
    }
 
1318
    return false;
 
1319
  }
 
1320
  
 
1321
  return (bool)(ifr.ifr_flags & IFF_RUNNING);
 
1322
}
 
1323
 
 
1324
__attribute__((nonnull, pure, warn_unused_result))
1244
1325
int notdotentries(const struct dirent *direntry){
1245
1326
  /* Skip "." and ".." */
1246
1327
  if(direntry->d_name[0] == '.'
1253
1334
}
1254
1335
 
1255
1336
/* Is this directory entry a runnable program? */
 
1337
__attribute__((nonnull, warn_unused_result))
1256
1338
int runnable_hook(const struct dirent *direntry){
1257
1339
  int ret;
1258
1340
  size_t sret;
1266
1348
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1267
1349
                "abcdefghijklmnopqrstuvwxyz"
1268
1350
                "0123456789"
1269
 
                "_-");
 
1351
                "_.-");
1270
1352
  if((direntry->d_name)[sret] != '\0'){
1271
1353
    /* Contains non-allowed characters */
1272
1354
    if(debug){
1276
1358
    return 0;
1277
1359
  }
1278
1360
  
1279
 
  char *fullname = NULL;
1280
 
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1281
 
  if(ret < 0){
1282
 
    perror_plus("asprintf");
1283
 
    return 0;
1284
 
  }
1285
 
  
1286
 
  ret = stat(fullname, &st);
 
1361
  ret = fstatat(hookdir_fd, direntry->d_name, &st, 0);
1287
1362
  if(ret == -1){
1288
1363
    if(debug){
1289
1364
      perror_plus("Could not stat hook");
1313
1388
  return 1;
1314
1389
}
1315
1390
 
1316
 
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
 
1391
__attribute__((nonnull, warn_unused_result))
 
1392
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
 
1393
                            mandos_context *mc){
1317
1394
  int ret;
1318
1395
  struct timespec now;
1319
1396
  struct timespec waited_time;
1320
1397
  intmax_t block_time;
1321
1398
  
1322
1399
  while(true){
1323
 
    if(mc.current_server == NULL){
1324
 
      if (debug){
 
1400
    if(mc->current_server == NULL){
 
1401
      if(debug){
1325
1402
        fprintf_plus(stderr, "Wait until first server is found."
1326
1403
                     " No timeout!\n");
1327
1404
      }
1328
1405
      ret = avahi_simple_poll_iterate(s, -1);
1329
1406
    } else {
1330
 
      if (debug){
 
1407
      if(debug){
1331
1408
        fprintf_plus(stderr, "Check current_server if we should run"
1332
1409
                     " it, or wait\n");
1333
1410
      }
1340
1417
      /* Calculating in ms how long time between now and server
1341
1418
         who we visted longest time ago. Now - last seen.  */
1342
1419
      waited_time.tv_sec = (now.tv_sec
1343
 
                            - mc.current_server->last_seen.tv_sec);
 
1420
                            - mc->current_server->last_seen.tv_sec);
1344
1421
      waited_time.tv_nsec = (now.tv_nsec
1345
 
                             - mc.current_server->last_seen.tv_nsec);
 
1422
                             - mc->current_server->last_seen.tv_nsec);
1346
1423
      /* total time is 10s/10,000ms.
1347
1424
         Converting to s from ms by dividing by 1,000,
1348
1425
         and ns to ms by dividing by 1,000,000. */
1350
1427
                     - ((intmax_t)waited_time.tv_sec * 1000))
1351
1428
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1352
1429
      
1353
 
      if (debug){
 
1430
      if(debug){
1354
1431
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1355
1432
                     block_time);
1356
1433
      }
1357
1434
      
1358
1435
      if(block_time <= 0){
1359
 
        ret = start_mandos_communication(mc.current_server->ip,
1360
 
                                         mc.current_server->port,
1361
 
                                         mc.current_server->if_index,
1362
 
                                         mc.current_server->af);
 
1436
        ret = start_mandos_communication(mc->current_server->ip,
 
1437
                                         mc->current_server->port,
 
1438
                                         mc->current_server->if_index,
 
1439
                                         mc->current_server->af, mc);
1363
1440
        if(ret == 0){
1364
 
          avahi_simple_poll_quit(mc.simple_poll);
 
1441
          avahi_simple_poll_quit(s);
1365
1442
          return 0;
1366
1443
        }
1367
1444
        ret = clock_gettime(CLOCK_MONOTONIC,
1368
 
                            &mc.current_server->last_seen);
 
1445
                            &mc->current_server->last_seen);
1369
1446
        if(ret == -1){
1370
1447
          perror_plus("clock_gettime");
1371
1448
          return -1;
1372
1449
        }
1373
 
        mc.current_server = mc.current_server->next;
 
1450
        mc->current_server = mc->current_server->next;
1374
1451
        block_time = 0;         /* Call avahi to find new Mandos
1375
1452
                                   servers, but don't block */
1376
1453
      }
1378
1455
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1379
1456
    }
1380
1457
    if(ret != 0){
1381
 
      if (ret > 0 or errno != EINTR){
 
1458
      if(ret > 0 or errno != EINTR){
1382
1459
        return (ret != 1) ? ret : 0;
1383
1460
      }
1384
1461
    }
1385
1462
  }
1386
1463
}
1387
1464
 
1388
 
bool run_network_hooks(const char *mode, const char *interface,
 
1465
/* Set effective uid to 0, return errno */
 
1466
__attribute__((warn_unused_result))
 
1467
error_t raise_privileges(void){
 
1468
  error_t old_errno = errno;
 
1469
  error_t ret_errno = 0;
 
1470
  if(seteuid(0) == -1){
 
1471
    ret_errno = errno;
 
1472
  }
 
1473
  errno = old_errno;
 
1474
  return ret_errno;
 
1475
}
 
1476
 
 
1477
/* Set effective and real user ID to 0.  Return errno. */
 
1478
__attribute__((warn_unused_result))
 
1479
error_t raise_privileges_permanently(void){
 
1480
  error_t old_errno = errno;
 
1481
  error_t ret_errno = raise_privileges();
 
1482
  if(ret_errno != 0){
 
1483
    errno = old_errno;
 
1484
    return ret_errno;
 
1485
  }
 
1486
  if(setuid(0) == -1){
 
1487
    ret_errno = errno;
 
1488
  }
 
1489
  errno = old_errno;
 
1490
  return ret_errno;
 
1491
}
 
1492
 
 
1493
/* Set effective user ID to unprivileged saved user ID */
 
1494
__attribute__((warn_unused_result))
 
1495
error_t lower_privileges(void){
 
1496
  error_t old_errno = errno;
 
1497
  error_t ret_errno = 0;
 
1498
  if(seteuid(uid) == -1){
 
1499
    ret_errno = errno;
 
1500
  }
 
1501
  errno = old_errno;
 
1502
  return ret_errno;
 
1503
}
 
1504
 
 
1505
/* Lower privileges permanently */
 
1506
__attribute__((warn_unused_result))
 
1507
error_t lower_privileges_permanently(void){
 
1508
  error_t old_errno = errno;
 
1509
  error_t ret_errno = 0;
 
1510
  if(setuid(uid) == -1){
 
1511
    ret_errno = errno;
 
1512
  }
 
1513
  errno = old_errno;
 
1514
  return ret_errno;
 
1515
}
 
1516
 
 
1517
__attribute__((nonnull))
 
1518
void run_network_hooks(const char *mode, const char *interface,
1389
1519
                       const float delay){
1390
 
  struct dirent **direntries;
1391
 
  struct dirent *direntry;
1392
 
  int ret;
1393
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1394
 
                         alphasort);
 
1520
  struct dirent **direntries = NULL;
 
1521
  if(hookdir_fd == -1){
 
1522
    hookdir_fd = open(hookdir, O_RDONLY);
 
1523
    if(hookdir_fd == -1){
 
1524
      if(errno == ENOENT){
 
1525
        if(debug){
 
1526
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1527
                       " found\n", hookdir);
 
1528
        }
 
1529
      } else {
 
1530
        perror_plus("open");
 
1531
      }
 
1532
      return;
 
1533
    }
 
1534
  }
 
1535
#ifdef __GLIBC__
 
1536
#if __GLIBC_PREREQ(2, 15)
 
1537
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
 
1538
                           runnable_hook, alphasort);
 
1539
#else  /* not __GLIBC_PREREQ(2, 15) */
 
1540
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1541
                         alphasort);
 
1542
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
1543
#else   /* not __GLIBC__ */
 
1544
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1545
                         alphasort);
 
1546
#endif  /* not __GLIBC__ */
1395
1547
  if(numhooks == -1){
1396
1548
    perror_plus("scandir");
1397
 
  } else {
1398
 
    int devnull = open("/dev/null", O_RDONLY);
1399
 
    for(int i = 0; i < numhooks; i++){
1400
 
      direntry = direntries[i];
1401
 
      char *fullname = NULL;
1402
 
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1403
 
      if(ret < 0){
 
1549
    return;
 
1550
  }
 
1551
  struct dirent *direntry;
 
1552
  int ret;
 
1553
  int devnull = open("/dev/null", O_RDONLY);
 
1554
  for(int i = 0; i < numhooks; i++){
 
1555
    direntry = direntries[i];
 
1556
    if(debug){
 
1557
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1558
                   direntry->d_name);
 
1559
    }
 
1560
    pid_t hook_pid = fork();
 
1561
    if(hook_pid == 0){
 
1562
      /* Child */
 
1563
      /* Raise privileges */
 
1564
      errno = raise_privileges_permanently();
 
1565
      if(errno != 0){
 
1566
        perror_plus("Failed to raise privileges");
 
1567
        _exit(EX_NOPERM);
 
1568
      }
 
1569
      /* Set group */
 
1570
      errno = 0;
 
1571
      ret = setgid(0);
 
1572
      if(ret == -1){
 
1573
        perror_plus("setgid");
 
1574
        _exit(EX_NOPERM);
 
1575
      }
 
1576
      /* Reset supplementary groups */
 
1577
      errno = 0;
 
1578
      ret = setgroups(0, NULL);
 
1579
      if(ret == -1){
 
1580
        perror_plus("setgroups");
 
1581
        _exit(EX_NOPERM);
 
1582
      }
 
1583
      ret = dup2(devnull, STDIN_FILENO);
 
1584
      if(ret == -1){
 
1585
        perror_plus("dup2(devnull, STDIN_FILENO)");
 
1586
        _exit(EX_OSERR);
 
1587
      }
 
1588
      ret = close(devnull);
 
1589
      if(ret == -1){
 
1590
        perror_plus("close");
 
1591
        _exit(EX_OSERR);
 
1592
      }
 
1593
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
1594
      if(ret == -1){
 
1595
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
1596
        _exit(EX_OSERR);
 
1597
      }
 
1598
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1599
      if(ret == -1){
 
1600
        perror_plus("setenv");
 
1601
        _exit(EX_OSERR);
 
1602
      }
 
1603
      ret = setenv("DEVICE", interface, 1);
 
1604
      if(ret == -1){
 
1605
        perror_plus("setenv");
 
1606
        _exit(EX_OSERR);
 
1607
      }
 
1608
      ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1609
      if(ret == -1){
 
1610
        perror_plus("setenv");
 
1611
        _exit(EX_OSERR);
 
1612
      }
 
1613
      ret = setenv("MODE", mode, 1);
 
1614
      if(ret == -1){
 
1615
        perror_plus("setenv");
 
1616
        _exit(EX_OSERR);
 
1617
      }
 
1618
      char *delaystring;
 
1619
      ret = asprintf(&delaystring, "%f", (double)delay);
 
1620
      if(ret == -1){
1404
1621
        perror_plus("asprintf");
1405
 
        continue;
1406
 
      }
1407
 
      if(debug){
1408
 
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
1409
 
                     direntry->d_name);
1410
 
      }
1411
 
      pid_t hook_pid = fork();
1412
 
      if(hook_pid == 0){
1413
 
        /* Child */
1414
 
        /* Raise privileges */
1415
 
        errno = 0;
1416
 
        ret = seteuid(0);
1417
 
        if(ret == -1){
1418
 
          perror_plus("seteuid");
1419
 
        }
1420
 
        /* Raise privileges even more */
1421
 
        errno = 0;
1422
 
        ret = setuid(0);
1423
 
        if(ret == -1){
1424
 
          perror_plus("setuid");
1425
 
        }
1426
 
        /* Set group */
1427
 
        errno = 0;
1428
 
        ret = setgid(0);
1429
 
        if(ret == -1){
1430
 
          perror_plus("setgid");
1431
 
        }
1432
 
        /* Reset supplementary groups */
1433
 
        errno = 0;
1434
 
        ret = setgroups(0, NULL);
1435
 
        if(ret == -1){
1436
 
          perror_plus("setgroups");
1437
 
        }
1438
 
        dup2(devnull, STDIN_FILENO);
1439
 
        close(devnull);
1440
 
        dup2(STDERR_FILENO, STDOUT_FILENO);
1441
 
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1442
 
        if(ret == -1){
1443
 
          perror_plus("setenv");
1444
 
          _exit(EX_OSERR);
1445
 
        }
1446
 
        ret = setenv("DEVICE", interface, 1);
1447
 
        if(ret == -1){
1448
 
          perror_plus("setenv");
1449
 
          _exit(EX_OSERR);
1450
 
        }
1451
 
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1452
 
        if(ret == -1){
1453
 
          perror_plus("setenv");
1454
 
          _exit(EX_OSERR);
1455
 
        }
1456
 
        ret = setenv("MODE", mode, 1);
1457
 
        if(ret == -1){
1458
 
          perror_plus("setenv");
1459
 
          _exit(EX_OSERR);
1460
 
        }
1461
 
        char *delaystring;
1462
 
        ret = asprintf(&delaystring, "%f", delay);
1463
 
        if(ret == -1){
1464
 
          perror_plus("asprintf");
1465
 
          _exit(EX_OSERR);
1466
 
        }
1467
 
        ret = setenv("DELAY", delaystring, 1);
1468
 
        if(ret == -1){
1469
 
          free(delaystring);
1470
 
          perror_plus("setenv");
1471
 
          _exit(EX_OSERR);
1472
 
        }
 
1622
        _exit(EX_OSERR);
 
1623
      }
 
1624
      ret = setenv("DELAY", delaystring, 1);
 
1625
      if(ret == -1){
1473
1626
        free(delaystring);
1474
 
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1475
 
          perror_plus("execl");
1476
 
          _exit(EXIT_FAILURE);
1477
 
        }
1478
 
      } else {
1479
 
        int status;
1480
 
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1481
 
          perror_plus("waitpid");
1482
 
          free(fullname);
1483
 
          continue;
1484
 
        }
1485
 
        if(WIFEXITED(status)){
1486
 
          if(WEXITSTATUS(status) != 0){
1487
 
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1488
 
                         " with status %d\n", direntry->d_name,
1489
 
                         WEXITSTATUS(status));
1490
 
            free(fullname);
1491
 
            continue;
1492
 
          }
1493
 
        } else if(WIFSIGNALED(status)){
1494
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1495
 
                       " signal %d\n", direntry->d_name,
1496
 
                       WTERMSIG(status));
1497
 
          free(fullname);
1498
 
          continue;
1499
 
        } else {
1500
 
          fprintf_plus(stderr, "Warning: network hook \"%s\""
1501
 
                       " crashed\n", direntry->d_name);
1502
 
          free(fullname);
1503
 
          continue;
1504
 
        }
1505
 
      }
1506
 
      free(fullname);
1507
 
      if(debug){
1508
 
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1509
 
                     direntry->d_name);
1510
 
      }
1511
 
    }
1512
 
    close(devnull);
1513
 
  }
1514
 
  return true;
 
1627
        perror_plus("setenv");
 
1628
        _exit(EX_OSERR);
 
1629
      }
 
1630
      free(delaystring);
 
1631
      if(connect_to != NULL){
 
1632
        ret = setenv("CONNECT", connect_to, 1);
 
1633
        if(ret == -1){
 
1634
          perror_plus("setenv");
 
1635
          _exit(EX_OSERR);
 
1636
        }
 
1637
      }
 
1638
      int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
 
1639
      if(hook_fd == -1){
 
1640
        perror_plus("openat");
 
1641
        _exit(EXIT_FAILURE);
 
1642
      }
 
1643
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
1644
        perror_plus("close");
 
1645
        _exit(EXIT_FAILURE);
 
1646
      }
 
1647
      if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
 
1648
                 environ) == -1){
 
1649
        perror_plus("fexecve");
 
1650
        _exit(EXIT_FAILURE);
 
1651
      }
 
1652
    } else {
 
1653
      int status;
 
1654
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1655
        perror_plus("waitpid");
 
1656
        free(direntry);
 
1657
        continue;
 
1658
      }
 
1659
      if(WIFEXITED(status)){
 
1660
        if(WEXITSTATUS(status) != 0){
 
1661
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1662
                       " with status %d\n", direntry->d_name,
 
1663
                       WEXITSTATUS(status));
 
1664
          free(direntry);
 
1665
          continue;
 
1666
        }
 
1667
      } else if(WIFSIGNALED(status)){
 
1668
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1669
                     " signal %d\n", direntry->d_name,
 
1670
                     WTERMSIG(status));
 
1671
        free(direntry);
 
1672
        continue;
 
1673
      } else {
 
1674
        fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1675
                     " crashed\n", direntry->d_name);
 
1676
        free(direntry);
 
1677
        continue;
 
1678
      }
 
1679
    }
 
1680
    if(debug){
 
1681
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1682
                   direntry->d_name);
 
1683
    }
 
1684
    free(direntry);
 
1685
  }
 
1686
  free(direntries);
 
1687
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
1688
    perror_plus("close");
 
1689
  } else {
 
1690
    hookdir_fd = -1;
 
1691
  }
 
1692
  close(devnull);
 
1693
}
 
1694
 
 
1695
__attribute__((nonnull, warn_unused_result))
 
1696
error_t bring_up_interface(const char *const interface,
 
1697
                           const float delay){
 
1698
  error_t old_errno = errno;
 
1699
  int ret;
 
1700
  struct ifreq network;
 
1701
  unsigned int if_index = if_nametoindex(interface);
 
1702
  if(if_index == 0){
 
1703
    fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1704
    errno = old_errno;
 
1705
    return ENXIO;
 
1706
  }
 
1707
  
 
1708
  if(quit_now){
 
1709
    errno = old_errno;
 
1710
    return EINTR;
 
1711
  }
 
1712
  
 
1713
  if(not interface_is_up(interface)){
 
1714
    error_t ret_errno = 0, ioctl_errno = 0;
 
1715
    if(not get_flags(interface, &network)){
 
1716
      ret_errno = errno;
 
1717
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1718
                   "\"%s\"\n", interface);
 
1719
      errno = old_errno;
 
1720
      return ret_errno;
 
1721
    }
 
1722
    network.ifr_flags |= IFF_UP; /* set flag */
 
1723
    
 
1724
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1725
    if(sd == -1){
 
1726
      ret_errno = errno;
 
1727
      perror_plus("socket");
 
1728
      errno = old_errno;
 
1729
      return ret_errno;
 
1730
    }
 
1731
    
 
1732
    if(quit_now){
 
1733
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1734
      if(ret == -1){
 
1735
        perror_plus("close");
 
1736
      }
 
1737
      errno = old_errno;
 
1738
      return EINTR;
 
1739
    }
 
1740
    
 
1741
    if(debug){
 
1742
      fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
 
1743
                   interface);
 
1744
    }
 
1745
    
 
1746
    /* Raise privileges */
 
1747
    ret_errno = raise_privileges();
 
1748
    if(ret_errno != 0){
 
1749
      errno = ret_errno;
 
1750
      perror_plus("Failed to raise privileges");
 
1751
    }
 
1752
    
 
1753
#ifdef __linux__
 
1754
    int ret_linux;
 
1755
    bool restore_loglevel = false;
 
1756
    if(ret_errno == 0){
 
1757
      /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1758
         messages about the network interface to mess up the prompt */
 
1759
      ret_linux = klogctl(8, NULL, 5);
 
1760
      if(ret_linux == -1){
 
1761
        perror_plus("klogctl");
 
1762
      } else {
 
1763
        restore_loglevel = true;
 
1764
      }
 
1765
    }
 
1766
#endif  /* __linux__ */
 
1767
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1768
    ioctl_errno = errno;
 
1769
#ifdef __linux__
 
1770
    if(restore_loglevel){
 
1771
      ret_linux = klogctl(7, NULL, 0);
 
1772
      if(ret_linux == -1){
 
1773
        perror_plus("klogctl");
 
1774
      }
 
1775
    }
 
1776
#endif  /* __linux__ */
 
1777
    
 
1778
    /* If raise_privileges() succeeded above */
 
1779
    if(ret_errno == 0){
 
1780
      /* Lower privileges */
 
1781
      ret_errno = lower_privileges();
 
1782
      if(ret_errno != 0){
 
1783
        errno = ret_errno;
 
1784
        perror_plus("Failed to lower privileges");
 
1785
      }
 
1786
    }
 
1787
    
 
1788
    /* Close the socket */
 
1789
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1790
    if(ret == -1){
 
1791
      perror_plus("close");
 
1792
    }
 
1793
    
 
1794
    if(ret_setflags == -1){
 
1795
      errno = ioctl_errno;
 
1796
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1797
      errno = old_errno;
 
1798
      return ioctl_errno;
 
1799
    }
 
1800
  } else if(debug){
 
1801
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
 
1802
                 interface);
 
1803
  }
 
1804
  
 
1805
  /* Sleep checking until interface is running.
 
1806
     Check every 0.25s, up to total time of delay */
 
1807
  for(int i=0; i < delay * 4; i++){
 
1808
    if(interface_is_running(interface)){
 
1809
      break;
 
1810
    }
 
1811
    struct timespec sleeptime = { .tv_nsec = 250000000 };
 
1812
    ret = nanosleep(&sleeptime, NULL);
 
1813
    if(ret == -1 and errno != EINTR){
 
1814
      perror_plus("nanosleep");
 
1815
    }
 
1816
  }
 
1817
  
 
1818
  errno = old_errno;
 
1819
  return 0;
 
1820
}
 
1821
 
 
1822
__attribute__((nonnull, warn_unused_result))
 
1823
error_t take_down_interface(const char *const interface){
 
1824
  error_t old_errno = errno;
 
1825
  struct ifreq network;
 
1826
  unsigned int if_index = if_nametoindex(interface);
 
1827
  if(if_index == 0){
 
1828
    fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1829
    errno = old_errno;
 
1830
    return ENXIO;
 
1831
  }
 
1832
  if(interface_is_up(interface)){
 
1833
    error_t ret_errno = 0, ioctl_errno = 0;
 
1834
    if(not get_flags(interface, &network) and debug){
 
1835
      ret_errno = errno;
 
1836
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1837
                   "\"%s\"\n", interface);
 
1838
      errno = old_errno;
 
1839
      return ret_errno;
 
1840
    }
 
1841
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
1842
    
 
1843
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1844
    if(sd == -1){
 
1845
      ret_errno = errno;
 
1846
      perror_plus("socket");
 
1847
      errno = old_errno;
 
1848
      return ret_errno;
 
1849
    }
 
1850
    
 
1851
    if(debug){
 
1852
      fprintf_plus(stderr, "Taking down interface \"%s\"\n",
 
1853
                   interface);
 
1854
    }
 
1855
    
 
1856
    /* Raise privileges */
 
1857
    ret_errno = raise_privileges();
 
1858
    if(ret_errno != 0){
 
1859
      errno = ret_errno;
 
1860
      perror_plus("Failed to raise privileges");
 
1861
    }
 
1862
    
 
1863
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1864
    ioctl_errno = errno;
 
1865
    
 
1866
    /* If raise_privileges() succeeded above */
 
1867
    if(ret_errno == 0){
 
1868
      /* Lower privileges */
 
1869
      ret_errno = lower_privileges();
 
1870
      if(ret_errno != 0){
 
1871
        errno = ret_errno;
 
1872
        perror_plus("Failed to lower privileges");
 
1873
      }
 
1874
    }
 
1875
    
 
1876
    /* Close the socket */
 
1877
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1878
    if(ret == -1){
 
1879
      perror_plus("close");
 
1880
    }
 
1881
    
 
1882
    if(ret_setflags == -1){
 
1883
      errno = ioctl_errno;
 
1884
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1885
      errno = old_errno;
 
1886
      return ioctl_errno;
 
1887
    }
 
1888
  } else if(debug){
 
1889
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
 
1890
                 interface);
 
1891
  }
 
1892
  
 
1893
  errno = old_errno;
 
1894
  return 0;
1515
1895
}
1516
1896
 
1517
1897
int main(int argc, char *argv[]){
 
1898
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
 
1899
                        .priority = "SECURE256:!CTYPE-X.509:"
 
1900
                        "+CTYPE-OPENPGP", .current_server = NULL,
 
1901
                        .interfaces = NULL, .interfaces_size = 0 };
1518
1902
  AvahiSServiceBrowser *sb = NULL;
1519
 
  int error;
 
1903
  error_t ret_errno;
1520
1904
  int ret;
1521
1905
  intmax_t tmpmax;
1522
1906
  char *tmp;
1523
1907
  int exitcode = EXIT_SUCCESS;
1524
 
  const char *interface = "";
1525
 
  struct ifreq network;
1526
 
  int sd = -1;
1527
 
  bool take_down_interface = false;
1528
 
  uid_t uid;
1529
 
  gid_t gid;
1530
 
  char tempdir[] = "/tmp/mandosXXXXXX";
1531
 
  bool tempdir_created = false;
 
1908
  char *interfaces_to_take_down = NULL;
 
1909
  size_t interfaces_to_take_down_size = 0;
 
1910
  char run_tempdir[] = "/run/tmp/mandosXXXXXX";
 
1911
  char old_tempdir[] = "/tmp/mandosXXXXXX";
 
1912
  char *tempdir = NULL;
1532
1913
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1533
1914
  const char *seckey = PATHDIR "/" SECKEY;
1534
1915
  const char *pubkey = PATHDIR "/" PUBKEY;
 
1916
  char *interfaces_hooks = NULL;
1535
1917
  
1536
1918
  bool gnutls_initialized = false;
1537
1919
  bool gpgme_initialized = false;
1599
1981
        .group = 2 },
1600
1982
      { .name = "retry", .key = 132,
1601
1983
        .arg = "SECONDS",
1602
 
        .doc = "Retry interval used when denied by the mandos server",
 
1984
        .doc = "Retry interval used when denied by the Mandos server",
1603
1985
        .group = 2 },
1604
1986
      { .name = "network-hook-dir", .key = 133,
1605
1987
        .arg = "DIR",
1628
2010
        connect_to = arg;
1629
2011
        break;
1630
2012
      case 'i':                 /* --interface */
1631
 
        interface = arg;
 
2013
        ret_errno = argz_add_sep(&mc.interfaces, &mc.interfaces_size,
 
2014
                                 arg, (int)',');
 
2015
        if(ret_errno != 0){
 
2016
          argp_error(state, "%s", strerror(ret_errno));
 
2017
        }
1632
2018
        break;
1633
2019
      case 's':                 /* --seckey */
1634
2020
        seckey = arg;
1711
2097
    /* Work around Debian bug #633582:
1712
2098
       <http://bugs.debian.org/633582> */
1713
2099
    
1714
 
    /* Re-raise priviliges */
1715
 
    errno = 0;
1716
 
    ret = seteuid(0);
1717
 
    if(ret == -1){
1718
 
      perror_plus("seteuid");
 
2100
    /* Re-raise privileges */
 
2101
    ret_errno = raise_privileges();
 
2102
    if(ret_errno != 0){
 
2103
      errno = ret_errno;
 
2104
      perror_plus("Failed to raise privileges");
1719
2105
    } else {
1720
2106
      struct stat st;
1721
2107
      
1762
2148
      }
1763
2149
    
1764
2150
      /* Lower privileges */
1765
 
      errno = 0;
1766
 
      ret = seteuid(uid);
1767
 
      if(ret == -1){
1768
 
        perror_plus("seteuid");
 
2151
      ret_errno = lower_privileges();
 
2152
      if(ret_errno != 0){
 
2153
        errno = ret_errno;
 
2154
        perror_plus("Failed to lower privileges");
 
2155
      }
 
2156
    }
 
2157
  }
 
2158
  
 
2159
  /* Remove invalid interface names (except "none") */
 
2160
  {
 
2161
    char *interface = NULL;
 
2162
    while((interface = argz_next(mc.interfaces, mc.interfaces_size,
 
2163
                                 interface))){
 
2164
      if(strcmp(interface, "none") != 0
 
2165
         and if_nametoindex(interface) == 0){
 
2166
        if(interface[0] != '\0'){
 
2167
          fprintf_plus(stderr, "Not using nonexisting interface"
 
2168
                       " \"%s\"\n", interface);
 
2169
        }
 
2170
        argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
 
2171
        interface = NULL;
1769
2172
      }
1770
2173
    }
1771
2174
  }
1772
2175
  
1773
2176
  /* Run network hooks */
1774
 
  if(not run_network_hooks("start", interface, delay)){
1775
 
    goto end;
 
2177
  {
 
2178
    if(mc.interfaces != NULL){
 
2179
      interfaces_hooks = malloc(mc.interfaces_size);
 
2180
      if(interfaces_hooks == NULL){
 
2181
        perror_plus("malloc");
 
2182
        goto end;
 
2183
      }
 
2184
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
 
2185
      argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
 
2186
    }
 
2187
    run_network_hooks("start", interfaces_hooks != NULL ?
 
2188
                      interfaces_hooks : "", delay);
1776
2189
  }
1777
2190
  
1778
2191
  if(not debug){
1779
2192
    avahi_set_log_function(empty_log);
1780
2193
  }
1781
2194
  
1782
 
  if(interface[0] == '\0'){
1783
 
    struct dirent **direntries;
1784
 
    /* First look for interfaces that are up */
1785
 
    ret = scandir(sys_class_net, &direntries, up_interface,
1786
 
                  alphasort);
1787
 
    if(ret == 0){
1788
 
      /* No up interfaces, look for any good interfaces */
1789
 
      free(direntries);
1790
 
      ret = scandir(sys_class_net, &direntries, good_interface,
1791
 
                    alphasort);
1792
 
    }
1793
 
    if(ret >= 1){
1794
 
      /* Pick the first interface returned */
1795
 
      interface = strdup(direntries[0]->d_name);
1796
 
      if(debug){
1797
 
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1798
 
      }
1799
 
      if(interface == NULL){
1800
 
        perror_plus("malloc");
1801
 
        free(direntries);
1802
 
        exitcode = EXIT_FAILURE;
1803
 
        goto end;
1804
 
      }
1805
 
      free(direntries);
1806
 
    } else {
1807
 
      free(direntries);
1808
 
      fprintf_plus(stderr, "Could not find a network interface\n");
1809
 
      exitcode = EXIT_FAILURE;
1810
 
      goto end;
1811
 
    }
1812
 
  }
1813
 
  
1814
2195
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1815
2196
     from the signal handler */
1816
2197
  /* Initialize the pseudo-RNG for Avahi */
1817
2198
  srand((unsigned int) time(NULL));
1818
 
  mc.simple_poll = avahi_simple_poll_new();
1819
 
  if(mc.simple_poll == NULL){
 
2199
  simple_poll = avahi_simple_poll_new();
 
2200
  if(simple_poll == NULL){
1820
2201
    fprintf_plus(stderr,
1821
2202
                 "Avahi: Failed to create simple poll object.\n");
1822
2203
    exitcode = EX_UNAVAILABLE;
1886
2267
    }
1887
2268
  }
1888
2269
  
1889
 
  /* If the interface is down, bring it up */
1890
 
  if(strcmp(interface, "none") != 0){
1891
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1892
 
    if(if_index == 0){
1893
 
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1894
 
      exitcode = EX_UNAVAILABLE;
1895
 
      goto end;
1896
 
    }
1897
 
    
1898
 
    if(quit_now){
1899
 
      goto end;
1900
 
    }
1901
 
    
1902
 
    /* Re-raise priviliges */
1903
 
    errno = 0;
1904
 
    ret = seteuid(0);
1905
 
    if(ret == -1){
1906
 
      perror_plus("seteuid");
1907
 
    }
1908
 
    
1909
 
#ifdef __linux__
1910
 
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1911
 
       messages about the network interface to mess up the prompt */
1912
 
    ret = klogctl(8, NULL, 5);
1913
 
    bool restore_loglevel = true;
1914
 
    if(ret == -1){
1915
 
      restore_loglevel = false;
1916
 
      perror_plus("klogctl");
1917
 
    }
1918
 
#endif  /* __linux__ */
1919
 
    
1920
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1921
 
    if(sd < 0){
1922
 
      perror_plus("socket");
1923
 
      exitcode = EX_OSERR;
1924
 
#ifdef __linux__
1925
 
      if(restore_loglevel){
1926
 
        ret = klogctl(7, NULL, 0);
1927
 
        if(ret == -1){
1928
 
          perror_plus("klogctl");
1929
 
        }
1930
 
      }
1931
 
#endif  /* __linux__ */
1932
 
      /* Lower privileges */
1933
 
      errno = 0;
1934
 
      ret = seteuid(uid);
1935
 
      if(ret == -1){
1936
 
        perror_plus("seteuid");
1937
 
      }
1938
 
      goto end;
1939
 
    }
1940
 
    strcpy(network.ifr_name, interface);
1941
 
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1942
 
    if(ret == -1){
1943
 
      perror_plus("ioctl SIOCGIFFLAGS");
1944
 
#ifdef __linux__
1945
 
      if(restore_loglevel){
1946
 
        ret = klogctl(7, NULL, 0);
1947
 
        if(ret == -1){
1948
 
          perror_plus("klogctl");
1949
 
        }
1950
 
      }
1951
 
#endif  /* __linux__ */
1952
 
      exitcode = EX_OSERR;
1953
 
      /* Lower privileges */
1954
 
      errno = 0;
1955
 
      ret = seteuid(uid);
1956
 
      if(ret == -1){
1957
 
        perror_plus("seteuid");
1958
 
      }
1959
 
      goto end;
1960
 
    }
1961
 
    if((network.ifr_flags & IFF_UP) == 0){
1962
 
      network.ifr_flags |= IFF_UP;
1963
 
      take_down_interface = true;
1964
 
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1965
 
      if(ret == -1){
1966
 
        take_down_interface = false;
1967
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1968
 
        exitcode = EX_OSERR;
1969
 
#ifdef __linux__
1970
 
        if(restore_loglevel){
1971
 
          ret = klogctl(7, NULL, 0);
1972
 
          if(ret == -1){
1973
 
            perror_plus("klogctl");
 
2270
  /* If no interfaces were specified, make a list */
 
2271
  if(mc.interfaces == NULL){
 
2272
    struct dirent **direntries = NULL;
 
2273
    /* Look for any good interfaces */
 
2274
    ret = scandir(sys_class_net, &direntries, good_interface,
 
2275
                  alphasort);
 
2276
    if(ret >= 1){
 
2277
      /* Add all found interfaces to interfaces list */
 
2278
      for(int i = 0; i < ret; ++i){
 
2279
        ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
 
2280
                             direntries[i]->d_name);
 
2281
        if(ret_errno != 0){
 
2282
          errno = ret_errno;
 
2283
          perror_plus("argz_add");
 
2284
          free(direntries[i]);
 
2285
          continue;
 
2286
        }
 
2287
        if(debug){
 
2288
          fprintf_plus(stderr, "Will use interface \"%s\"\n",
 
2289
                       direntries[i]->d_name);
 
2290
        }
 
2291
        free(direntries[i]);
 
2292
      }
 
2293
      free(direntries);
 
2294
    } else {
 
2295
      if(ret == 0){
 
2296
        free(direntries);
 
2297
      }
 
2298
      fprintf_plus(stderr, "Could not find a network interface\n");
 
2299
      exitcode = EXIT_FAILURE;
 
2300
      goto end;
 
2301
    }
 
2302
  }
 
2303
  
 
2304
  /* Bring up interfaces which are down, and remove any "none"s */
 
2305
  {
 
2306
    char *interface = NULL;
 
2307
    while((interface = argz_next(mc.interfaces, mc.interfaces_size,
 
2308
                                 interface))){
 
2309
      /* If interface name is "none", stop bringing up interfaces.
 
2310
         Also remove all instances of "none" from the list */
 
2311
      if(strcmp(interface, "none") == 0){
 
2312
        argz_delete(&mc.interfaces, &mc.interfaces_size,
 
2313
                    interface);
 
2314
        interface = NULL;
 
2315
        while((interface = argz_next(mc.interfaces,
 
2316
                                     mc.interfaces_size, interface))){
 
2317
          if(strcmp(interface, "none") == 0){
 
2318
            argz_delete(&mc.interfaces, &mc.interfaces_size,
 
2319
                        interface);
 
2320
            interface = NULL;
1974
2321
          }
1975
2322
        }
1976
 
#endif  /* __linux__ */
1977
 
        /* Lower privileges */
1978
 
        errno = 0;
1979
 
        ret = seteuid(uid);
1980
 
        if(ret == -1){
1981
 
          perror_plus("seteuid");
1982
 
        }
1983
 
        goto end;
1984
 
      }
1985
 
    }
1986
 
    /* Sleep checking until interface is running.
1987
 
       Check every 0.25s, up to total time of delay */
1988
 
    for(int i=0; i < delay * 4; i++){
1989
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1990
 
      if(ret == -1){
1991
 
        perror_plus("ioctl SIOCGIFFLAGS");
1992
 
      } else if(network.ifr_flags & IFF_RUNNING){
1993
2323
        break;
1994
2324
      }
1995
 
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1996
 
      ret = nanosleep(&sleeptime, NULL);
1997
 
      if(ret == -1 and errno != EINTR){
1998
 
        perror_plus("nanosleep");
1999
 
      }
2000
 
    }
2001
 
    if(not take_down_interface){
2002
 
      /* We won't need the socket anymore */
2003
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2004
 
      if(ret == -1){
2005
 
        perror_plus("close");
2006
 
      }
2007
 
    }
2008
 
#ifdef __linux__
2009
 
    if(restore_loglevel){
2010
 
      /* Restores kernel loglevel to default */
2011
 
      ret = klogctl(7, NULL, 0);
2012
 
      if(ret == -1){
2013
 
        perror_plus("klogctl");
2014
 
      }
2015
 
    }
2016
 
#endif  /* __linux__ */
2017
 
    /* Lower privileges */
2018
 
    errno = 0;
2019
 
    /* Lower privileges */
2020
 
    ret = seteuid(uid);
2021
 
    if(ret == -1){
2022
 
      perror_plus("seteuid");
2023
 
    }
 
2325
      bool interface_was_up = interface_is_up(interface);
 
2326
      errno = bring_up_interface(interface, delay);
 
2327
      if(not interface_was_up){
 
2328
        if(errno != 0){
 
2329
          perror_plus("Failed to bring up interface");
 
2330
        } else {
 
2331
          errno = argz_add(&interfaces_to_take_down,
 
2332
                           &interfaces_to_take_down_size,
 
2333
                           interface);
 
2334
          if(errno != 0){
 
2335
            perror_plus("argz_add");
 
2336
          }
 
2337
        }
 
2338
      }
 
2339
    }
 
2340
    if(debug and (interfaces_to_take_down == NULL)){
 
2341
      fprintf_plus(stderr, "No interfaces were brought up\n");
 
2342
    }
 
2343
  }
 
2344
  
 
2345
  /* If we only got one interface, explicitly use only that one */
 
2346
  if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
 
2347
    if(debug){
 
2348
      fprintf_plus(stderr, "Using only interface \"%s\"\n",
 
2349
                   mc.interfaces);
 
2350
    }
 
2351
    if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
2024
2352
  }
2025
2353
  
2026
2354
  if(quit_now){
2027
2355
    goto end;
2028
2356
  }
2029
2357
  
2030
 
  ret = init_gnutls_global(pubkey, seckey);
 
2358
  ret = init_gnutls_global(pubkey, seckey, &mc);
2031
2359
  if(ret == -1){
2032
2360
    fprintf_plus(stderr, "init_gnutls_global failed\n");
2033
2361
    exitcode = EX_UNAVAILABLE;
2040
2368
    goto end;
2041
2369
  }
2042
2370
  
2043
 
  if(mkdtemp(tempdir) == NULL){
 
2371
  /* Try /run/tmp before /tmp */
 
2372
  tempdir = mkdtemp(run_tempdir);
 
2373
  if(tempdir == NULL and errno == ENOENT){
 
2374
      if(debug){
 
2375
        fprintf_plus(stderr, "Tempdir %s did not work, trying %s\n",
 
2376
                     run_tempdir, old_tempdir);
 
2377
      }
 
2378
      tempdir = mkdtemp(old_tempdir);
 
2379
  }
 
2380
  if(tempdir == NULL){
2044
2381
    perror_plus("mkdtemp");
2045
2382
    goto end;
2046
2383
  }
2047
 
  tempdir_created = true;
2048
2384
  
2049
2385
  if(quit_now){
2050
2386
    goto end;
2051
2387
  }
2052
2388
  
2053
 
  if(not init_gpgme(pubkey, seckey, tempdir)){
 
2389
  if(not init_gpgme(pubkey, seckey, tempdir, &mc)){
2054
2390
    fprintf_plus(stderr, "init_gpgme failed\n");
2055
2391
    exitcode = EX_UNAVAILABLE;
2056
2392
    goto end;
2066
2402
    /* Connect directly, do not use Zeroconf */
2067
2403
    /* (Mainly meant for debugging) */
2068
2404
    char *address = strrchr(connect_to, ':');
 
2405
    
2069
2406
    if(address == NULL){
2070
2407
      fprintf_plus(stderr, "No colon in address\n");
2071
2408
      exitcode = EX_USAGE;
2076
2413
      goto end;
2077
2414
    }
2078
2415
    
2079
 
    uint16_t port;
 
2416
    in_port_t port;
2080
2417
    errno = 0;
2081
2418
    tmpmax = strtoimax(address+1, &tmp, 10);
2082
2419
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2083
 
       or tmpmax != (uint16_t)tmpmax){
 
2420
       or tmpmax != (in_port_t)tmpmax){
2084
2421
      fprintf_plus(stderr, "Bad port number\n");
2085
2422
      exitcode = EX_USAGE;
2086
2423
      goto end;
2087
2424
    }
2088
 
  
 
2425
    
2089
2426
    if(quit_now){
2090
2427
      goto end;
2091
2428
    }
2092
2429
    
2093
 
    port = (uint16_t)tmpmax;
 
2430
    port = (in_port_t)tmpmax;
2094
2431
    *address = '\0';
2095
2432
    /* Colon in address indicates IPv6 */
2096
2433
    int af;
2112
2449
    }
2113
2450
    
2114
2451
    while(not quit_now){
2115
 
      ret = start_mandos_communication(address, port, if_index, af);
 
2452
      ret = start_mandos_communication(address, port, if_index, af,
 
2453
                                       &mc);
2116
2454
      if(quit_now or ret == 0){
2117
2455
        break;
2118
2456
      }
2120
2458
        fprintf_plus(stderr, "Retrying in %d seconds\n",
2121
2459
                     (int)retry_interval);
2122
2460
      }
2123
 
      sleep((int)retry_interval);
 
2461
      sleep((unsigned int)retry_interval);
2124
2462
    }
2125
2463
    
2126
 
    if (not quit_now){
 
2464
    if(not quit_now){
2127
2465
      exitcode = EXIT_SUCCESS;
2128
2466
    }
2129
2467
    
2144
2482
    config.publish_domain = 0;
2145
2483
    
2146
2484
    /* Allocate a new server */
2147
 
    mc.server = avahi_server_new(avahi_simple_poll_get
2148
 
                                 (mc.simple_poll), &config, NULL,
2149
 
                                 NULL, &error);
 
2485
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
 
2486
                                 &config, NULL, NULL, &ret_errno);
2150
2487
    
2151
2488
    /* Free the Avahi configuration data */
2152
2489
    avahi_server_config_free(&config);
2155
2492
  /* Check if creating the Avahi server object succeeded */
2156
2493
  if(mc.server == NULL){
2157
2494
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2158
 
                 avahi_strerror(error));
 
2495
                 avahi_strerror(ret_errno));
2159
2496
    exitcode = EX_UNAVAILABLE;
2160
2497
    goto end;
2161
2498
  }
2167
2504
  /* Create the Avahi service browser */
2168
2505
  sb = avahi_s_service_browser_new(mc.server, if_index,
2169
2506
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2170
 
                                   NULL, 0, browse_callback, NULL);
 
2507
                                   NULL, 0, browse_callback,
 
2508
                                   (void *)&mc);
2171
2509
  if(sb == NULL){
2172
2510
    fprintf_plus(stderr, "Failed to create service browser: %s\n",
2173
2511
                 avahi_strerror(avahi_server_errno(mc.server)));
2184
2522
  if(debug){
2185
2523
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2186
2524
  }
2187
 
 
2188
 
  ret = avahi_loop_with_timeout(mc.simple_poll,
2189
 
                                (int)(retry_interval * 1000));
 
2525
  
 
2526
  ret = avahi_loop_with_timeout(simple_poll,
 
2527
                                (int)(retry_interval * 1000), &mc);
2190
2528
  if(debug){
2191
2529
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2192
2530
                 (ret == 0) ? "successfully" : "with error");
2199
2537
  }
2200
2538
  
2201
2539
  /* Cleanup things */
 
2540
  free(mc.interfaces);
 
2541
  
2202
2542
  if(sb != NULL)
2203
2543
    avahi_s_service_browser_free(sb);
2204
2544
  
2205
2545
  if(mc.server != NULL)
2206
2546
    avahi_server_free(mc.server);
2207
2547
  
2208
 
  if(mc.simple_poll != NULL)
2209
 
    avahi_simple_poll_free(mc.simple_poll);
 
2548
  if(simple_poll != NULL)
 
2549
    avahi_simple_poll_free(simple_poll);
2210
2550
  
2211
2551
  if(gnutls_initialized){
2212
2552
    gnutls_certificate_free_credentials(mc.cred);
2224
2564
    mc.current_server->prev->next = NULL;
2225
2565
    while(mc.current_server != NULL){
2226
2566
      server *next = mc.current_server->next;
 
2567
#ifdef __GNUC__
 
2568
#pragma GCC diagnostic push
 
2569
#pragma GCC diagnostic ignored "-Wcast-qual"
 
2570
#endif
 
2571
      free((char *)(mc.current_server->ip));
 
2572
#ifdef __GNUC__
 
2573
#pragma GCC diagnostic pop
 
2574
#endif
2227
2575
      free(mc.current_server);
2228
2576
      mc.current_server = next;
2229
2577
    }
2230
2578
  }
2231
2579
  
2232
 
  /* Run network hooks */
2233
 
  run_network_hooks("stop", interface, delay);
2234
 
  
2235
 
  /* Re-raise priviliges */
 
2580
  /* Re-raise privileges */
2236
2581
  {
2237
 
    errno = 0;
2238
 
    ret = seteuid(0);
2239
 
    if(ret == -1){
2240
 
      perror_plus("seteuid");
 
2582
    ret_errno = raise_privileges();
 
2583
    if(ret_errno != 0){
 
2584
      errno = ret_errno;
 
2585
      perror_plus("Failed to raise privileges");
 
2586
    } else {
 
2587
      
 
2588
      /* Run network hooks */
 
2589
      run_network_hooks("stop", interfaces_hooks != NULL ?
 
2590
                        interfaces_hooks : "", delay);
 
2591
      
 
2592
      /* Take down the network interfaces which were brought up */
 
2593
      {
 
2594
        char *interface = NULL;
 
2595
        while((interface=argz_next(interfaces_to_take_down,
 
2596
                                   interfaces_to_take_down_size,
 
2597
                                   interface))){
 
2598
          ret_errno = take_down_interface(interface);
 
2599
          if(ret_errno != 0){
 
2600
            errno = ret_errno;
 
2601
            perror_plus("Failed to take down interface");
 
2602
          }
 
2603
        }
 
2604
        if(debug and (interfaces_to_take_down == NULL)){
 
2605
          fprintf_plus(stderr, "No interfaces needed to be taken"
 
2606
                       " down\n");
 
2607
        }
 
2608
      }
2241
2609
    }
2242
2610
    
2243
 
    /* Take down the network interface */
2244
 
    if(take_down_interface and geteuid() == 0){
2245
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2246
 
      if(ret == -1){
2247
 
        perror_plus("ioctl SIOCGIFFLAGS");
2248
 
      } else if(network.ifr_flags & IFF_UP){
2249
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2250
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
2251
 
        if(ret == -1){
2252
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2253
 
        }
2254
 
      }
2255
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2256
 
      if(ret == -1){
2257
 
        perror_plus("close");
2258
 
      }
 
2611
    ret_errno = lower_privileges_permanently();
 
2612
    if(ret_errno != 0){
 
2613
      errno = ret_errno;
 
2614
      perror_plus("Failed to lower privileges permanently");
2259
2615
    }
2260
2616
  }
2261
 
  /* Lower privileges permanently */
2262
 
  errno = 0;
2263
 
  ret = setuid(uid);
2264
 
  if(ret == -1){
2265
 
    perror_plus("setuid");
2266
 
  }
 
2617
  
 
2618
  free(interfaces_to_take_down);
 
2619
  free(interfaces_hooks);
2267
2620
  
2268
2621
  /* Removes the GPGME temp directory and all files inside */
2269
 
  if(tempdir_created){
 
2622
  if(tempdir != NULL){
2270
2623
    struct dirent **direntries = NULL;
2271
 
    struct dirent *direntry = NULL;
2272
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
2273
 
                             alphasort);
2274
 
    if (numentries > 0){
2275
 
      for(int i = 0; i < numentries; i++){
2276
 
        direntry = direntries[i];
2277
 
        char *fullname = NULL;
2278
 
        ret = asprintf(&fullname, "%s/%s", tempdir,
2279
 
                       direntry->d_name);
2280
 
        if(ret < 0){
2281
 
          perror_plus("asprintf");
2282
 
          continue;
2283
 
        }
2284
 
        ret = remove(fullname);
2285
 
        if(ret == -1){
2286
 
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2287
 
                       strerror(errno));
2288
 
        }
2289
 
        free(fullname);
 
2624
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
 
2625
                                                  O_NOFOLLOW));
 
2626
    if(tempdir_fd == -1){
 
2627
      perror_plus("open");
 
2628
    } else {
 
2629
#ifdef __GLIBC__
 
2630
#if __GLIBC_PREREQ(2, 15)
 
2631
      int numentries = scandirat(tempdir_fd, ".", &direntries,
 
2632
                                 notdotentries, alphasort);
 
2633
#else  /* not __GLIBC_PREREQ(2, 15) */
 
2634
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
2635
                               alphasort);
 
2636
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
2637
#else   /* not __GLIBC__ */
 
2638
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
2639
                               alphasort);
 
2640
#endif  /* not __GLIBC__ */
 
2641
      if(numentries >= 0){
 
2642
        for(int i = 0; i < numentries; i++){
 
2643
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
 
2644
          if(ret == -1){
 
2645
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
 
2646
                         " \"%s\", 0): %s\n", tempdir,
 
2647
                         direntries[i]->d_name, strerror(errno));
 
2648
          }
 
2649
          free(direntries[i]);
 
2650
        }
 
2651
        
 
2652
        /* need to clean even if 0 because man page doesn't specify */
 
2653
        free(direntries);
 
2654
        if(numentries == -1){
 
2655
          perror_plus("scandir");
 
2656
        }
 
2657
        ret = rmdir(tempdir);
 
2658
        if(ret == -1 and errno != ENOENT){
 
2659
          perror_plus("rmdir");
 
2660
        }
2290
2661
      }
2291
 
    }
2292
 
 
2293
 
    /* need to clean even if 0 because man page doesn't specify */
2294
 
    free(direntries);
2295
 
    if (numentries == -1){
2296
 
      perror_plus("scandir");
2297
 
    }
2298
 
    ret = rmdir(tempdir);
2299
 
    if(ret == -1 and errno != ENOENT){
2300
 
      perror_plus("rmdir");
 
2662
      TEMP_FAILURE_RETRY(close(tempdir_fd));
2301
2663
    }
2302
2664
  }
2303
2665