/mandos/trunk

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

« back to all changes in this revision

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

  • Committer: Teddy Hogeborn
  • Date: 2009-02-07 04:50:39 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090207045039-xkr6b80vtqwqrq8l
* Makefile (install-client-nokey): Move "initramfs-tools-script" from
                                   "/scripts/local-top/mandos" to
                                   "/scripts/init-premount/mandos".
  (uninstall-client): - '' -
* debian/mandos-client.dirs: - '' -
* initramfs-tools-script (PREREQ): Added "udev".

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 */
31
31
 
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
33
 
#ifndef _LARGEFILE_SOURCE
34
33
#define _LARGEFILE_SOURCE
35
 
#endif
36
 
#ifndef _FILE_OFFSET_BITS
37
34
#define _FILE_OFFSET_BITS 64
38
 
#endif
39
35
 
40
36
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
41
37
 
42
38
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
 
                                   stdout, ferror(), remove() */
 
39
                                   stdout, ferror(), sscanf(),
 
40
                                   remove() */
44
41
#include <stdint.h>             /* uint16_t, uint32_t */
45
42
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
43
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
47
 
                                   srand(), strtof() */
48
 
#include <stdbool.h>            /* bool, false, true */
 
44
                                   srand() */
 
45
#include <stdbool.h>            /* bool, true */
49
46
#include <string.h>             /* memset(), strcmp(), strlen(),
50
47
                                   strerror(), asprintf(), strcpy() */
51
 
#include <sys/ioctl.h>          /* ioctl */
 
48
#include <sys/ioctl.h>          /* ioctl */
52
49
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
53
50
                                   sockaddr_in6, PF_INET6,
54
 
                                   SOCK_STREAM, uid_t, gid_t, open(),
55
 
                                   opendir(), DIR */
 
51
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
 
52
                                   uid_t, gid_t, open(), opendir(),
 
53
                                   DIR */
56
54
#include <sys/stat.h>           /* open() */
57
55
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
 
                                   inet_pton(), connect() */
 
56
                                   struct in6_addr, inet_pton(),
 
57
                                   connect() */
59
58
#include <fcntl.h>              /* open() */
60
59
#include <dirent.h>             /* opendir(), struct dirent, readdir()
61
60
                                 */
62
 
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
 
                                   strtoimax() */
 
61
#include <inttypes.h>           /* PRIu16, intmax_t, SCNdMAX */
64
62
#include <assert.h>             /* assert() */
65
63
#include <errno.h>              /* perror(), errno */
66
64
#include <time.h>               /* nanosleep(), time() */
67
65
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
66
                                   SIOCSIFFLAGS, if_indextoname(),
69
67
                                   if_nametoindex(), IF_NAMESIZE */
70
 
#include <netinet/in.h>         /* IN6_IS_ADDR_LINKLOCAL,
71
 
                                   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
72
 
                                */
 
68
#include <netinet/in.h>
73
69
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
70
                                   getuid(), getgid(), setuid(),
75
71
                                   setgid() */
76
72
#include <arpa/inet.h>          /* inet_pton(), htons */
77
 
#include <iso646.h>             /* not, or, and */
 
73
#include <iso646.h>             /* not, and, or */
78
74
#include <argp.h>               /* struct argp_option, error_t, struct
79
75
                                   argp_state, struct argp,
80
76
                                   argp_parse(), ARGP_KEY_ARG,
81
77
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
 
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
 
                                   sigaction(), SIGTERM, sig_atomic_t,
84
 
                                   raise() */
85
 
 
86
78
#ifdef __linux__
87
79
#include <sys/klog.h>           /* klogctl() */
88
 
#endif  /* __linux__ */
 
80
#endif
89
81
 
90
82
/* Avahi */
91
83
/* All Avahi types, constants and functions
137
129
  gpgme_ctx_t ctx;
138
130
} mandos_context;
139
131
 
140
 
/* global context so signal handler can reach it*/
141
 
mandos_context mc = { .simple_poll = NULL, .server = NULL,
142
 
                      .dh_bits = 1024, .priority = "SECURE256"
143
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
144
 
 
145
132
/*
146
 
 * Make additional room in "buffer" for at least BUFFER_SIZE more
147
 
 * bytes. "buffer_capacity" is how much is currently allocated,
 
133
 * Make room in "buffer" for at least BUFFER_SIZE additional bytes.
 
134
 * "buffer_capacity" is how much is currently allocated,
148
135
 * "buffer_length" is how much is already used.
149
136
 */
150
 
size_t incbuffer(char **buffer, size_t buffer_length,
 
137
size_t adjustbuffer(char **buffer, size_t buffer_length,
151
138
                  size_t buffer_capacity){
152
139
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
153
140
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
162
149
/* 
163
150
 * Initialize GPGME.
164
151
 */
165
 
static bool init_gpgme(const char *seckey,
 
152
static bool init_gpgme(mandos_context *mc, const char *seckey,
166
153
                       const char *pubkey, const char *tempdir){
 
154
  int ret;
167
155
  gpgme_error_t rc;
168
156
  gpgme_engine_info_t engine_info;
169
157
  
172
160
   * Helper function to insert pub and seckey to the engine keyring.
173
161
   */
174
162
  bool import_key(const char *filename){
175
 
    int ret;
176
163
    int fd;
177
164
    gpgme_data_t pgp_data;
178
165
    
189
176
      return false;
190
177
    }
191
178
    
192
 
    rc = gpgme_op_import(mc.ctx, pgp_data);
 
179
    rc = gpgme_op_import(mc->ctx, pgp_data);
193
180
    if(rc != GPG_ERR_NO_ERROR){
194
181
      fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
195
182
              gpgme_strsource(rc), gpgme_strerror(rc));
205
192
  }
206
193
  
207
194
  if(debug){
208
 
    fprintf(stderr, "Initializing GPGME\n");
 
195
    fprintf(stderr, "Initialize gpgme\n");
209
196
  }
210
197
  
211
198
  /* Init GPGME */
238
225
  }
239
226
  
240
227
  /* Create new GPGME "context" */
241
 
  rc = gpgme_new(&(mc.ctx));
 
228
  rc = gpgme_new(&(mc->ctx));
242
229
  if(rc != GPG_ERR_NO_ERROR){
243
230
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
244
231
            gpgme_strsource(rc), gpgme_strerror(rc));
249
236
    return false;
250
237
  }
251
238
  
252
 
  return true;
 
239
  return true; 
253
240
}
254
241
 
255
242
/* 
256
243
 * Decrypt OpenPGP data.
257
244
 * Returns -1 on error
258
245
 */
259
 
static ssize_t pgp_packet_decrypt(const char *cryptotext,
 
246
static ssize_t pgp_packet_decrypt(const mandos_context *mc,
 
247
                                  const char *cryptotext,
260
248
                                  size_t crypto_size,
261
249
                                  char **plaintext){
262
250
  gpgme_data_t dh_crypto, dh_plain;
289
277
  
290
278
  /* Decrypt data from the cryptotext data buffer to the plaintext
291
279
     data buffer */
292
 
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
 
280
  rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
293
281
  if(rc != GPG_ERR_NO_ERROR){
294
282
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
295
283
            gpgme_strsource(rc), gpgme_strerror(rc));
296
284
    plaintext_length = -1;
297
285
    if(debug){
298
286
      gpgme_decrypt_result_t result;
299
 
      result = gpgme_op_decrypt_result(mc.ctx);
 
287
      result = gpgme_op_decrypt_result(mc->ctx);
300
288
      if(result == NULL){
301
289
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
302
290
      } else {
309
297
        }
310
298
        gpgme_recipient_t recipient;
311
299
        recipient = result->recipients;
312
 
        while(recipient != NULL){
313
 
          fprintf(stderr, "Public key algorithm: %s\n",
314
 
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
315
 
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
316
 
          fprintf(stderr, "Secret key available: %s\n",
317
 
                  recipient->status == GPG_ERR_NO_SECKEY
318
 
                  ? "No" : "Yes");
319
 
          recipient = recipient->next;
 
300
        if(recipient){
 
301
          while(recipient != NULL){
 
302
            fprintf(stderr, "Public key algorithm: %s\n",
 
303
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
304
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
305
            fprintf(stderr, "Secret key available: %s\n",
 
306
                    recipient->status == GPG_ERR_NO_SECKEY
 
307
                    ? "No" : "Yes");
 
308
            recipient = recipient->next;
 
309
          }
320
310
        }
321
311
      }
322
312
    }
336
326
  
337
327
  *plaintext = NULL;
338
328
  while(true){
339
 
    plaintext_capacity = incbuffer(plaintext,
 
329
    plaintext_capacity = adjustbuffer(plaintext,
340
330
                                      (size_t)plaintext_length,
341
331
                                      plaintext_capacity);
342
332
    if(plaintext_capacity == 0){
343
 
        perror("incbuffer");
 
333
        perror("adjustbuffer");
344
334
        plaintext_length = -1;
345
335
        goto decrypt_end;
346
336
    }
392
382
  fprintf(stderr, "GnuTLS: %s", string);
393
383
}
394
384
 
395
 
static int init_gnutls_global(const char *pubkeyfilename,
 
385
static int init_gnutls_global(mandos_context *mc,
 
386
                              const char *pubkeyfilename,
396
387
                              const char *seckeyfilename){
397
388
  int ret;
398
389
  
416
407
  }
417
408
  
418
409
  /* OpenPGP credentials */
419
 
  gnutls_certificate_allocate_credentials(&mc.cred);
 
410
  gnutls_certificate_allocate_credentials(&mc->cred);
420
411
  if(ret != GNUTLS_E_SUCCESS){
421
412
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
422
 
                                                    from
423
 
                                                    -Wunreachable-code
424
 
                                                 */
 
413
                                                  * from
 
414
                                                  * -Wunreachable-code
 
415
                                                  */
425
416
            safer_gnutls_strerror(ret));
426
417
    gnutls_global_deinit();
427
418
    return -1;
434
425
  }
435
426
  
436
427
  ret = gnutls_certificate_set_openpgp_key_file
437
 
    (mc.cred, pubkeyfilename, seckeyfilename,
 
428
    (mc->cred, pubkeyfilename, seckeyfilename,
438
429
     GNUTLS_OPENPGP_FMT_BASE64);
439
430
  if(ret != GNUTLS_E_SUCCESS){
440
431
    fprintf(stderr,
446
437
  }
447
438
  
448
439
  /* GnuTLS server initialization */
449
 
  ret = gnutls_dh_params_init(&mc.dh_params);
 
440
  ret = gnutls_dh_params_init(&mc->dh_params);
450
441
  if(ret != GNUTLS_E_SUCCESS){
451
442
    fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
452
443
            " %s\n", safer_gnutls_strerror(ret));
453
444
    goto globalfail;
454
445
  }
455
 
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
 
446
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
456
447
  if(ret != GNUTLS_E_SUCCESS){
457
448
    fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
458
449
            safer_gnutls_strerror(ret));
459
450
    goto globalfail;
460
451
  }
461
452
  
462
 
  gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
 
453
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
463
454
  
464
455
  return 0;
465
456
  
466
457
 globalfail:
467
458
  
468
 
  gnutls_certificate_free_credentials(mc.cred);
 
459
  gnutls_certificate_free_credentials(mc->cred);
469
460
  gnutls_global_deinit();
470
 
  gnutls_dh_params_deinit(mc.dh_params);
 
461
  gnutls_dh_params_deinit(mc->dh_params);
471
462
  return -1;
472
463
}
473
464
 
474
 
static int init_gnutls_session(gnutls_session_t *session){
 
465
static int init_gnutls_session(mandos_context *mc,
 
466
                               gnutls_session_t *session){
475
467
  int ret;
476
468
  /* GnuTLS session creation */
477
469
  ret = gnutls_init(session, GNUTLS_SERVER);
482
474
  
483
475
  {
484
476
    const char *err;
485
 
    ret = gnutls_priority_set_direct(*session, mc.priority, &err);
 
477
    ret = gnutls_priority_set_direct(*session, mc->priority, &err);
486
478
    if(ret != GNUTLS_E_SUCCESS){
487
479
      fprintf(stderr, "Syntax error at: %s\n", err);
488
480
      fprintf(stderr, "GnuTLS error: %s\n",
493
485
  }
494
486
  
495
487
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
496
 
                               mc.cred);
 
488
                               mc->cred);
497
489
  if(ret != GNUTLS_E_SUCCESS){
498
490
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
499
491
            safer_gnutls_strerror(ret));
505
497
  gnutls_certificate_server_set_request(*session,
506
498
                                        GNUTLS_CERT_IGNORE);
507
499
  
508
 
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
 
500
  gnutls_dh_set_prime_bits(*session, mc->dh_bits);
509
501
  
510
502
  return 0;
511
503
}
514
506
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
515
507
                      __attribute__((unused)) const char *txt){}
516
508
 
517
 
sig_atomic_t quit_now = 0;
518
 
int signal_received = 0;
519
 
 
520
509
/* Called when a Mandos server is found */
521
510
static int start_mandos_communication(const char *ip, uint16_t port,
522
511
                                      AvahiIfIndex if_index,
523
 
                                      int af){
524
 
  int ret, tcp_sd = -1;
 
512
                                      mandos_context *mc){
 
513
  int ret, tcp_sd;
525
514
  ssize_t sret;
526
 
  union {
527
 
    struct sockaddr_in in;
528
 
    struct sockaddr_in6 in6;
529
 
  } to;
 
515
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
530
516
  char *buffer = NULL;
531
517
  char *decrypted_buffer;
532
518
  size_t buffer_length = 0;
533
519
  size_t buffer_capacity = 0;
 
520
  ssize_t decrypted_buffer_size;
534
521
  size_t written;
535
522
  int retval = 0;
 
523
  char interface[IF_NAMESIZE];
536
524
  gnutls_session_t session;
537
 
  int pf;                       /* Protocol family */
538
 
  
539
 
  if(quit_now){
540
 
    return -1;
541
 
  }
542
 
  
543
 
  switch(af){
544
 
  case AF_INET6:
545
 
    pf = PF_INET6;
546
 
    break;
547
 
  case AF_INET:
548
 
    pf = PF_INET;
549
 
    break;
550
 
  default:
551
 
    fprintf(stderr, "Bad address family: %d\n", af);
552
 
    return -1;
553
 
  }
554
 
  
555
 
  ret = init_gnutls_session(&session);
 
525
  
 
526
  ret = init_gnutls_session(mc, &session);
556
527
  if(ret != 0){
557
528
    return -1;
558
529
  }
559
530
  
560
531
  if(debug){
561
 
    fprintf(stderr, "Setting up a TCP connection to %s, port %" PRIu16
 
532
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
562
533
            "\n", ip, port);
563
534
  }
564
535
  
565
 
  tcp_sd = socket(pf, SOCK_STREAM, 0);
 
536
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
566
537
  if(tcp_sd < 0){
567
538
    perror("socket");
568
 
    retval = -1;
569
 
    goto mandos_end;
 
539
    return -1;
570
540
  }
571
541
  
572
 
  if(quit_now){
573
 
    goto mandos_end;
 
542
  if(debug){
 
543
    if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
544
      perror("if_indextoname");
 
545
      return -1;
 
546
    }
 
547
    fprintf(stderr, "Binding to interface %s\n", interface);
574
548
  }
575
549
  
576
550
  memset(&to, 0, sizeof(to));
577
 
  if(af == AF_INET6){
578
 
    to.in6.sin6_family = (sa_family_t)af;
579
 
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
580
 
  } else {                      /* IPv4 */
581
 
    to.in.sin_family = (sa_family_t)af;
582
 
    ret = inet_pton(af, ip, &to.in.sin_addr);
583
 
  }
 
551
  to.in6.sin6_family = AF_INET6;
 
552
  /* It would be nice to have a way to detect if we were passed an
 
553
     IPv4 address here.   Now we assume an IPv6 address. */
 
554
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
584
555
  if(ret < 0 ){
585
556
    perror("inet_pton");
586
 
    retval = -1;
587
 
    goto mandos_end;
 
557
    return -1;
588
558
  }
589
559
  if(ret == 0){
590
560
    fprintf(stderr, "Bad address: %s\n", ip);
591
 
    retval = -1;
592
 
    goto mandos_end;
 
561
    return -1;
593
562
  }
594
 
  if(af == AF_INET6){
595
 
    to.in6.sin6_port = htons(port); /* Spurious warnings from
596
 
                                       -Wconversion and
597
 
                                       -Wunreachable-code */
598
 
    
599
 
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
600
 
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
601
 
                              -Wunreachable-code*/
602
 
      if(if_index == AVAHI_IF_UNSPEC){
603
 
        fprintf(stderr, "An IPv6 link-local address is incomplete"
604
 
                " without a network interface\n");
605
 
        retval = -1;
606
 
        goto mandos_end;
607
 
      }
608
 
      /* Set the network interface number as scope */
609
 
      to.in6.sin6_scope_id = (uint32_t)if_index;
610
 
    }
611
 
  } else {
612
 
    to.in.sin_port = htons(port); /* Spurious warnings from
 
563
  to.in6.sin6_port = htons(port); /* Spurious warnings from
613
564
                                     -Wconversion and
614
565
                                     -Wunreachable-code */
615
 
  }
616
566
  
617
 
  if(quit_now){
618
 
    goto mandos_end;
619
 
  }
 
567
  to.in6.sin6_scope_id = (uint32_t)if_index;
620
568
  
621
569
  if(debug){
622
 
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
623
 
      char interface[IF_NAMESIZE];
624
 
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
625
 
        perror("if_indextoname");
626
 
      } else {
627
 
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
628
 
                ip, interface, port);
629
 
      }
630
 
    } else {
631
 
      fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
632
 
              port);
633
 
    }
634
 
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
635
 
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
636
 
    const char *pcret;
637
 
    if(af == AF_INET6){
638
 
      pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
639
 
                        sizeof(addrstr));
640
 
    } else {
641
 
      pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
642
 
                        sizeof(addrstr));
643
 
    }
644
 
    if(pcret == NULL){
 
570
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
 
571
            port);
 
572
    char addrstr[INET6_ADDRSTRLEN] = "";
 
573
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
 
574
                 sizeof(addrstr)) == NULL){
645
575
      perror("inet_ntop");
646
576
    } else {
647
577
      if(strcmp(addrstr, ip) != 0){
650
580
    }
651
581
  }
652
582
  
653
 
  if(quit_now){
654
 
    goto mandos_end;
655
 
  }
656
 
  
657
 
  if(af == AF_INET6){
658
 
    ret = connect(tcp_sd, &to.in6, sizeof(to));
659
 
  } else {
660
 
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
661
 
  }
 
583
  ret = connect(tcp_sd, &to.in, sizeof(to));
662
584
  if(ret < 0){
663
585
    perror("connect");
664
 
    retval = -1;
665
 
    goto mandos_end;
666
 
  }
667
 
  
668
 
  if(quit_now){
669
 
    goto mandos_end;
 
586
    return -1;
670
587
  }
671
588
  
672
589
  const char *out = mandos_protocol_version;
691
608
        break;
692
609
      }
693
610
    }
694
 
  
695
 
    if(quit_now){
696
 
      goto mandos_end;
697
 
    }
698
611
  }
699
612
  
700
613
  if(debug){
701
614
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
702
615
  }
703
616
  
704
 
  if(quit_now){
705
 
    goto mandos_end;
706
 
  }
707
 
  
708
617
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
709
618
  
710
 
  if(quit_now){
711
 
    goto mandos_end;
712
 
  }
713
 
  
714
 
  do {
 
619
  do{
715
620
    ret = gnutls_handshake(session);
716
 
    if(quit_now){
717
 
      goto mandos_end;
718
 
    }
719
621
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
720
622
  
721
623
  if(ret != GNUTLS_E_SUCCESS){
730
632
  /* Read OpenPGP packet that contains the wanted password */
731
633
  
732
634
  if(debug){
733
 
    fprintf(stderr, "Retrieving OpenPGP encrypted password from %s\n",
 
635
    fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
734
636
            ip);
735
637
  }
736
638
  
737
639
  while(true){
738
 
    
739
 
    if(quit_now){
740
 
      goto mandos_end;
741
 
    }
742
 
    
743
 
    buffer_capacity = incbuffer(&buffer, buffer_length,
 
640
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
744
641
                                   buffer_capacity);
745
642
    if(buffer_capacity == 0){
746
 
      perror("incbuffer");
 
643
      perror("adjustbuffer");
747
644
      retval = -1;
748
645
      goto mandos_end;
749
646
    }
750
647
    
751
 
    if(quit_now){
752
 
      goto mandos_end;
753
 
    }
754
 
    
755
648
    sret = gnutls_record_recv(session, buffer+buffer_length,
756
649
                              BUFFER_SIZE);
757
650
    if(sret == 0){
763
656
      case GNUTLS_E_AGAIN:
764
657
        break;
765
658
      case GNUTLS_E_REHANDSHAKE:
766
 
        do {
 
659
        do{
767
660
          ret = gnutls_handshake(session);
768
 
          
769
 
          if(quit_now){
770
 
            goto mandos_end;
771
 
          }
772
661
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
773
662
        if(ret < 0){
774
663
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
793
682
    fprintf(stderr, "Closing TLS session\n");
794
683
  }
795
684
  
796
 
  if(quit_now){
797
 
    goto mandos_end;
798
 
  }
799
 
  
800
685
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
801
686
  
802
 
  if(quit_now){
803
 
    goto mandos_end;
804
 
  }
805
 
  
806
687
  if(buffer_length > 0){
807
 
    ssize_t decrypted_buffer_size;
808
 
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
 
688
    decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
809
689
                                               buffer_length,
810
690
                                               &decrypted_buffer);
811
691
    if(decrypted_buffer_size >= 0){
812
 
      
813
692
      written = 0;
814
693
      while(written < (size_t) decrypted_buffer_size){
815
 
        if(quit_now){
816
 
          goto mandos_end;
817
 
        }
818
 
        
819
694
        ret = (int)fwrite(decrypted_buffer + written, 1,
820
695
                          (size_t)decrypted_buffer_size - written,
821
696
                          stdout);
841
716
  
842
717
 mandos_end:
843
718
  free(buffer);
844
 
  if(tcp_sd >= 0){
845
 
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
846
 
  }
 
719
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
847
720
  if(ret == -1){
848
721
    perror("close");
849
722
  }
850
723
  gnutls_deinit(session);
851
 
  if(quit_now){
852
 
    retval = -1;
853
 
  }
854
724
  return retval;
855
725
}
856
726
 
857
727
static void resolve_callback(AvahiSServiceResolver *r,
858
728
                             AvahiIfIndex interface,
859
 
                             AvahiProtocol proto,
 
729
                             AVAHI_GCC_UNUSED AvahiProtocol protocol,
860
730
                             AvahiResolverEvent event,
861
731
                             const char *name,
862
732
                             const char *type,
867
737
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
868
738
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
869
739
                             flags,
870
 
                             AVAHI_GCC_UNUSED void* userdata){
 
740
                             void* userdata){
 
741
  mandos_context *mc = userdata;
871
742
  assert(r);
872
743
  
873
744
  /* Called whenever a service has been resolved successfully or
874
745
     timed out */
875
746
  
876
 
  if(quit_now){
877
 
    return;
878
 
  }
879
 
  
880
747
  switch(event){
881
748
  default:
882
749
  case AVAHI_RESOLVER_FAILURE:
883
750
    fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
884
751
            " of type '%s' in domain '%s': %s\n", name, type, domain,
885
 
            avahi_strerror(avahi_server_errno(mc.server)));
 
752
            avahi_strerror(avahi_server_errno(mc->server)));
886
753
    break;
887
754
    
888
755
  case AVAHI_RESOLVER_FOUND:
894
761
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
895
762
                ip, (intmax_t)interface, port);
896
763
      }
897
 
      int ret = start_mandos_communication(ip, port, interface,
898
 
                                           avahi_proto_to_af(proto));
 
764
      int ret = start_mandos_communication(ip, port, interface, mc);
899
765
      if(ret == 0){
900
 
        avahi_simple_poll_quit(mc.simple_poll);
 
766
        avahi_simple_poll_quit(mc->simple_poll);
901
767
      }
902
768
    }
903
769
  }
913
779
                            const char *domain,
914
780
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
915
781
                            flags,
916
 
                            AVAHI_GCC_UNUSED void* userdata){
 
782
                            void* userdata){
 
783
  mandos_context *mc = userdata;
917
784
  assert(b);
918
785
  
919
786
  /* Called whenever a new services becomes available on the LAN or
920
787
     is removed from the LAN */
921
788
  
922
 
  if(quit_now){
923
 
    return;
924
 
  }
925
 
  
926
789
  switch(event){
927
790
  default:
928
791
  case AVAHI_BROWSER_FAILURE:
929
792
    
930
793
    fprintf(stderr, "(Avahi browser) %s\n",
931
 
            avahi_strerror(avahi_server_errno(mc.server)));
932
 
    avahi_simple_poll_quit(mc.simple_poll);
 
794
            avahi_strerror(avahi_server_errno(mc->server)));
 
795
    avahi_simple_poll_quit(mc->simple_poll);
933
796
    return;
934
797
    
935
798
  case AVAHI_BROWSER_NEW:
938
801
       the callback function is called the Avahi server will free the
939
802
       resolver for us. */
940
803
    
941
 
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
942
 
                                    name, type, domain, protocol, 0,
943
 
                                    resolve_callback, NULL) == NULL)
 
804
    if(!(avahi_s_service_resolver_new(mc->server, interface,
 
805
                                       protocol, name, type, domain,
 
806
                                       AVAHI_PROTO_INET6, 0,
 
807
                                       resolve_callback, mc)))
944
808
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
945
 
              name, avahi_strerror(avahi_server_errno(mc.server)));
 
809
              name, avahi_strerror(avahi_server_errno(mc->server)));
946
810
    break;
947
811
    
948
812
  case AVAHI_BROWSER_REMOVE:
957
821
  }
958
822
}
959
823
 
960
 
/* stop main loop after sigterm has been called */
961
 
static void handle_sigterm(int sig){
962
 
  if(quit_now){
963
 
    return;
964
 
  }
965
 
  quit_now = 1;
966
 
  signal_received = sig;
967
 
  int old_errno = errno;
968
 
  if(mc.simple_poll != NULL){
969
 
    avahi_simple_poll_quit(mc.simple_poll);
970
 
  }
971
 
  errno = old_errno;
972
 
}
973
 
 
974
824
int main(int argc, char *argv[]){
975
825
  AvahiSServiceBrowser *sb = NULL;
976
826
  int error;
977
827
  int ret;
978
828
  intmax_t tmpmax;
979
 
  char *tmp;
 
829
  int numchars;
980
830
  int exitcode = EXIT_SUCCESS;
981
831
  const char *interface = "eth0";
982
832
  struct ifreq network;
983
 
  int sd = -1;
984
 
  bool take_down_interface = false;
 
833
  int sd;
985
834
  uid_t uid;
986
835
  gid_t gid;
987
836
  char *connect_to = NULL;
991
840
  const char *seckey = PATHDIR "/" SECKEY;
992
841
  const char *pubkey = PATHDIR "/" PUBKEY;
993
842
  
 
843
  mandos_context mc = { .simple_poll = NULL, .server = NULL,
 
844
                        .dh_bits = 1024, .priority = "SECURE256"
 
845
                        ":!CTYPE-X.509:+CTYPE-OPENPGP" };
994
846
  bool gnutls_initialized = false;
995
847
  bool gpgme_initialized = false;
996
 
  float delay = 2.5f;
997
 
  
998
 
  struct sigaction old_sigterm_action;
999
 
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
 
848
  double delay = 2.5;
1000
849
  
1001
850
  {
1002
851
    struct argp_option options[] = {
1008
857
        .group = 1 },
1009
858
      { .name = "interface", .key = 'i',
1010
859
        .arg = "NAME",
1011
 
        .doc = "Network interface that will be used to search for"
1012
 
        " Mandos servers",
 
860
        .doc = "Interface that will be used to search for Mandos"
 
861
        " servers",
1013
862
        .group = 1 },
1014
863
      { .name = "seckey", .key = 's',
1015
864
        .arg = "FILE",
1054
903
        pubkey = arg;
1055
904
        break;
1056
905
      case 129:                 /* --dh-bits */
1057
 
        errno = 0;
1058
 
        tmpmax = strtoimax(arg, &tmp, 10);
1059
 
        if(errno != 0 or tmp == arg or *tmp != '\0'
1060
 
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
 
906
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
907
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
 
908
           or arg[numchars] != '\0'){
1061
909
          fprintf(stderr, "Bad number of DH bits\n");
1062
910
          exit(EXIT_FAILURE);
1063
911
        }
1067
915
        mc.priority = arg;
1068
916
        break;
1069
917
      case 131:                 /* --delay */
1070
 
        errno = 0;
1071
 
        delay = strtof(arg, &tmp);
1072
 
        if(errno != 0 or tmp == arg or *tmp != '\0'){
 
918
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
 
919
        if(ret < 1 or arg[numchars] != '\0'){
1073
920
          fprintf(stderr, "Bad delay\n");
1074
921
          exit(EXIT_FAILURE);
1075
922
        }
1096
943
    }
1097
944
  }
1098
945
  
1099
 
  if(not debug){
1100
 
    avahi_set_log_function(empty_log);
1101
 
  }
1102
 
  
1103
 
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1104
 
     from the signal handler */
1105
 
  /* Initialize the pseudo-RNG for Avahi */
1106
 
  srand((unsigned int) time(NULL));
1107
 
  mc.simple_poll = avahi_simple_poll_new();
1108
 
  if(mc.simple_poll == NULL){
1109
 
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1110
 
    exitcode = EXIT_FAILURE;
1111
 
    goto end;
1112
 
  }
1113
 
  
1114
 
  sigemptyset(&sigterm_action.sa_mask);
1115
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1116
 
  if(ret == -1){
1117
 
    perror("sigaddset");
1118
 
    exitcode = EXIT_FAILURE;
1119
 
    goto end;
1120
 
  }
1121
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1122
 
  if(ret == -1){
1123
 
    perror("sigaddset");
1124
 
    exitcode = EXIT_FAILURE;
1125
 
    goto end;
1126
 
  }
1127
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1128
 
  if(ret == -1){
1129
 
    perror("sigaddset");
1130
 
    exitcode = EXIT_FAILURE;
1131
 
    goto end;
1132
 
  }
1133
 
  /* Need to check if the handler is SIG_IGN before handling:
1134
 
     | [[info:libc:Initial Signal Actions]] |
1135
 
     | [[info:libc:Basic Signal Handling]]  |
1136
 
  */
1137
 
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1138
 
  if(ret == -1){
1139
 
    perror("sigaction");
1140
 
    return EXIT_FAILURE;
1141
 
  }
1142
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1143
 
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1144
 
    if(ret == -1){
1145
 
      perror("sigaction");
1146
 
      exitcode = EXIT_FAILURE;
1147
 
      goto end;
1148
 
    }
1149
 
  }
1150
 
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1151
 
  if(ret == -1){
1152
 
    perror("sigaction");
1153
 
    return EXIT_FAILURE;
1154
 
  }
1155
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1156
 
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1157
 
    if(ret == -1){
1158
 
      perror("sigaction");
1159
 
      exitcode = EXIT_FAILURE;
1160
 
      goto end;
1161
 
    }
1162
 
  }
1163
 
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1164
 
  if(ret == -1){
1165
 
    perror("sigaction");
1166
 
    return EXIT_FAILURE;
1167
 
  }
1168
 
  if(old_sigterm_action.sa_handler != SIG_IGN){
1169
 
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1170
 
    if(ret == -1){
1171
 
      perror("sigaction");
1172
 
      exitcode = EXIT_FAILURE;
1173
 
      goto end;
1174
 
    }
1175
 
  }
1176
 
  
1177
946
  /* If the interface is down, bring it up */
1178
 
  if(interface[0] != '\0'){
1179
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1180
 
    if(if_index == 0){
1181
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1182
 
      exitcode = EXIT_FAILURE;
1183
 
      goto end;
1184
 
    }
1185
 
    
1186
 
    if(quit_now){
1187
 
      goto end;
1188
 
    }
1189
 
    
 
947
  {
1190
948
#ifdef __linux__
1191
949
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1192
950
       messages to mess up the prompt */
1193
951
    ret = klogctl(8, NULL, 5);
1194
 
    bool restore_loglevel = true;
1195
952
    if(ret == -1){
1196
 
      restore_loglevel = false;
1197
953
      perror("klogctl");
1198
954
    }
1199
 
#endif  /* __linux__ */
 
955
#endif
1200
956
    
1201
957
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1202
958
    if(sd < 0){
1203
959
      perror("socket");
1204
960
      exitcode = EXIT_FAILURE;
1205
961
#ifdef __linux__
1206
 
      if(restore_loglevel){
1207
 
        ret = klogctl(7, NULL, 0);
1208
 
        if(ret == -1){
1209
 
          perror("klogctl");
1210
 
        }
 
962
      ret = klogctl(7, NULL, 0);
 
963
      if(ret == -1){
 
964
        perror("klogctl");
1211
965
      }
1212
 
#endif  /* __linux__ */
 
966
#endif
1213
967
      goto end;
1214
968
    }
1215
969
    strcpy(network.ifr_name, interface);
1217
971
    if(ret == -1){
1218
972
      perror("ioctl SIOCGIFFLAGS");
1219
973
#ifdef __linux__
1220
 
      if(restore_loglevel){
1221
 
        ret = klogctl(7, NULL, 0);
1222
 
        if(ret == -1){
1223
 
          perror("klogctl");
1224
 
        }
 
974
      ret = klogctl(7, NULL, 0);
 
975
      if(ret == -1){
 
976
        perror("klogctl");
1225
977
      }
1226
 
#endif  /* __linux__ */
 
978
#endif
1227
979
      exitcode = EXIT_FAILURE;
1228
980
      goto end;
1229
981
    }
1230
982
    if((network.ifr_flags & IFF_UP) == 0){
1231
983
      network.ifr_flags |= IFF_UP;
1232
 
      take_down_interface = true;
1233
984
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1234
985
      if(ret == -1){
1235
 
        take_down_interface = false;
1236
986
        perror("ioctl SIOCSIFFLAGS");
1237
987
        exitcode = EXIT_FAILURE;
1238
988
#ifdef __linux__
1239
 
        if(restore_loglevel){
1240
 
          ret = klogctl(7, NULL, 0);
1241
 
          if(ret == -1){
1242
 
            perror("klogctl");
1243
 
          }
 
989
        ret = klogctl(7, NULL, 0);
 
990
        if(ret == -1){
 
991
          perror("klogctl");
1244
992
        }
1245
 
#endif  /* __linux__ */
 
993
#endif
1246
994
        goto end;
1247
995
      }
1248
996
    }
1260
1008
        perror("nanosleep");
1261
1009
      }
1262
1010
    }
1263
 
    if(not take_down_interface){
1264
 
      /* We won't need the socket anymore */
1265
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1266
 
      if(ret == -1){
1267
 
        perror("close");
1268
 
      }
 
1011
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1012
    if(ret == -1){
 
1013
      perror("close");
1269
1014
    }
1270
1015
#ifdef __linux__
1271
 
    if(restore_loglevel){
1272
 
      /* Restores kernel loglevel to default */
1273
 
      ret = klogctl(7, NULL, 0);
1274
 
      if(ret == -1){
1275
 
        perror("klogctl");
1276
 
      }
 
1016
    /* Restores kernel loglevel to default */
 
1017
    ret = klogctl(7, NULL, 0);
 
1018
    if(ret == -1){
 
1019
      perror("klogctl");
1277
1020
    }
1278
 
#endif  /* __linux__ */
1279
 
  }
1280
 
  
1281
 
  if(quit_now){
1282
 
    goto end;
 
1021
#endif
1283
1022
  }
1284
1023
  
1285
1024
  uid = getuid();
1286
1025
  gid = getgid();
1287
1026
  
1288
 
  errno = 0;
1289
1027
  setgid(gid);
1290
1028
  if(ret == -1){
1291
1029
    perror("setgid");
1296
1034
    perror("setuid");
1297
1035
  }
1298
1036
  
1299
 
  if(quit_now){
1300
 
    goto end;
1301
 
  }
1302
 
  
1303
 
  ret = init_gnutls_global(pubkey, seckey);
 
1037
  ret = init_gnutls_global(&mc, pubkey, seckey);
1304
1038
  if(ret == -1){
1305
1039
    fprintf(stderr, "init_gnutls_global failed\n");
1306
1040
    exitcode = EXIT_FAILURE;
1309
1043
    gnutls_initialized = true;
1310
1044
  }
1311
1045
  
1312
 
  if(quit_now){
1313
 
    goto end;
1314
 
  }
1315
 
  
1316
 
  tempdir_created = true;
1317
1046
  if(mkdtemp(tempdir) == NULL){
1318
 
    tempdir_created = false;
1319
1047
    perror("mkdtemp");
1320
1048
    goto end;
1321
1049
  }
1322
 
  
1323
 
  if(quit_now){
1324
 
    goto end;
1325
 
  }
1326
 
  
1327
 
  if(not init_gpgme(pubkey, seckey, tempdir)){
 
1050
  tempdir_created = true;
 
1051
  
 
1052
  if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
1328
1053
    fprintf(stderr, "init_gpgme failed\n");
1329
1054
    exitcode = EXIT_FAILURE;
1330
1055
    goto end;
1332
1057
    gpgme_initialized = true;
1333
1058
  }
1334
1059
  
1335
 
  if(quit_now){
 
1060
  if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1061
  if(if_index == 0){
 
1062
    fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1063
    exitcode = EXIT_FAILURE;
1336
1064
    goto end;
1337
1065
  }
1338
1066
  
1345
1073
      exitcode = EXIT_FAILURE;
1346
1074
      goto end;
1347
1075
    }
1348
 
    
1349
 
    if(quit_now){
1350
 
      goto end;
1351
 
    }
1352
 
    
1353
1076
    uint16_t port;
1354
 
    errno = 0;
1355
 
    tmpmax = strtoimax(address+1, &tmp, 10);
1356
 
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1357
 
       or tmpmax != (uint16_t)tmpmax){
 
1077
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
1078
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
 
1079
       or address[numchars+1] != '\0'){
1358
1080
      fprintf(stderr, "Bad port number\n");
1359
1081
      exitcode = EXIT_FAILURE;
1360
1082
      goto end;
1361
1083
    }
1362
 
  
1363
 
    if(quit_now){
1364
 
      goto end;
1365
 
    }
1366
 
    
1367
1084
    port = (uint16_t)tmpmax;
1368
1085
    *address = '\0';
1369
1086
    address = connect_to;
1370
 
    /* Colon in address indicates IPv6 */
1371
 
    int af;
1372
 
    if(strchr(address, ':') != NULL){
1373
 
      af = AF_INET6;
1374
 
    } else {
1375
 
      af = AF_INET;
1376
 
    }
1377
 
    
1378
 
    if(quit_now){
1379
 
      goto end;
1380
 
    }
1381
 
    
1382
 
    ret = start_mandos_communication(address, port, if_index, af);
 
1087
    ret = start_mandos_communication(address, port, if_index, &mc);
1383
1088
    if(ret < 0){
1384
1089
      exitcode = EXIT_FAILURE;
1385
1090
    } else {
1388
1093
    goto end;
1389
1094
  }
1390
1095
  
1391
 
  if(quit_now){
 
1096
  if(not debug){
 
1097
    avahi_set_log_function(empty_log);
 
1098
  }
 
1099
  
 
1100
  /* Initialize the pseudo-RNG for Avahi */
 
1101
  srand((unsigned int) time(NULL));
 
1102
  
 
1103
  /* Allocate main Avahi loop object */
 
1104
  mc.simple_poll = avahi_simple_poll_new();
 
1105
  if(mc.simple_poll == NULL){
 
1106
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
 
1107
    exitcode = EXIT_FAILURE;
1392
1108
    goto end;
1393
1109
  }
1394
1110
  
1418
1134
    goto end;
1419
1135
  }
1420
1136
  
1421
 
  if(quit_now){
1422
 
    goto end;
1423
 
  }
1424
 
  
1425
1137
  /* Create the Avahi service browser */
1426
1138
  sb = avahi_s_service_browser_new(mc.server, if_index,
1427
 
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1428
 
                                   NULL, 0, browse_callback, NULL);
 
1139
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
 
1140
                                   NULL, 0, browse_callback, &mc);
1429
1141
  if(sb == NULL){
1430
1142
    fprintf(stderr, "Failed to create service browser: %s\n",
1431
1143
            avahi_strerror(avahi_server_errno(mc.server)));
1433
1145
    goto end;
1434
1146
  }
1435
1147
  
1436
 
  if(quit_now){
1437
 
    goto end;
1438
 
  }
1439
 
  
1440
1148
  /* Run the main loop */
1441
1149
  
1442
1150
  if(debug){
1471
1179
    gpgme_release(mc.ctx);
1472
1180
  }
1473
1181
  
1474
 
  /* Take down the network interface */
1475
 
  if(take_down_interface){
1476
 
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1477
 
    if(ret == -1){
1478
 
      perror("ioctl SIOCGIFFLAGS");
1479
 
    } else if(network.ifr_flags & IFF_UP) {
1480
 
      network.ifr_flags &= ~IFF_UP; /* clear flag */
1481
 
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1482
 
      if(ret == -1){
1483
 
        perror("ioctl SIOCSIFFLAGS");
1484
 
      }
1485
 
    }
1486
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1487
 
    if(ret == -1){
1488
 
      perror("close");
1489
 
    }
1490
 
  }
1491
 
  
1492
1182
  /* Removes the temp directory used by GPGME */
1493
1183
  if(tempdir_created){
1494
1184
    DIR *d;
1533
1223
    }
1534
1224
  }
1535
1225
  
1536
 
  if(quit_now){
1537
 
    sigemptyset(&old_sigterm_action.sa_mask);
1538
 
    old_sigterm_action.sa_handler = SIG_DFL;
1539
 
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
1540
 
    if(ret == -1){
1541
 
      perror("sigaction");
1542
 
    }
1543
 
    raise(signal_received);
1544
 
  }
1545
 
  
1546
1226
  return exitcode;
1547
1227
}