/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: 2012-06-01 18:39:03 UTC
  • mfrom: (237.8.1 release)
  • mto: (237.4.33 release)
  • mto: This revision was merged to the branch mainline in revision 593.
  • Revision ID: teddy@recompile.se-20120601183903-hydodgfmlau5d109
Tags: version-1.5.5-1
* Makefile (version): Changed to "1.5.5".
* NEWS (Version 1.5.5): New entry.
* debian/changelog (1.5.5-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-2012 Teddy Hogeborn
 
13
 * Copyright © 2008-2012 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
41
41
 
42
42
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
43
                                   stdout, ferror(), remove() */
44
 
#include <stdint.h>             /* uint16_t, uint32_t */
 
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() */
73
73
                                */
74
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
75
                                   getuid(), getgid(), seteuid(),
76
 
                                   setgid(), pause() */
 
76
                                   setgid(), pause(), _exit() */
77
77
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
78
78
#include <iso646.h>             /* not, or, and */
79
79
#include <argp.h>               /* struct argp_option, error_t, struct
87
87
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
88
88
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
89
89
                                   WEXITSTATUS(), WTERMSIG() */
 
90
#include <grp.h>                /* setgroups() */
90
91
 
91
92
#ifdef __linux__
92
93
#include <sys/klog.h>           /* klogctl() */
169
170
 
170
171
/* Function to use when printing errors */
171
172
void perror_plus(const char *print_text){
 
173
  int e = errno;
172
174
  fprintf(stderr, "Mandos plugin %s: ",
173
175
          program_invocation_short_name);
 
176
  errno = e;
174
177
  perror(print_text);
175
178
}
176
179
 
 
180
__attribute__((format (gnu_printf, 2, 3)))
 
181
int fprintf_plus(FILE *stream, const char *format, ...){
 
182
  va_list ap;
 
183
  va_start (ap, format);
 
184
  
 
185
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
 
186
                             program_invocation_short_name));
 
187
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
 
188
}
 
189
 
177
190
/*
178
191
 * Make additional room in "buffer" for at least BUFFER_SIZE more
179
192
 * bytes. "buffer_capacity" is how much is currently allocated,
192
205
}
193
206
 
194
207
/* Add server to set of servers to retry periodically */
195
 
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
196
 
               int af){
 
208
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
209
                int af){
197
210
  int ret;
198
211
  server *new_server = malloc(sizeof(server));
199
212
  if(new_server == NULL){
200
213
    perror_plus("malloc");
201
 
    return -1;
 
214
    return false;
202
215
  }
203
216
  *new_server = (server){ .ip = strdup(ip),
204
217
                          .port = port,
206
219
                          .af = af };
207
220
  if(new_server->ip == NULL){
208
221
    perror_plus("strdup");
209
 
    return -1;
 
222
    return false;
210
223
  }
211
224
  /* Special case of first server */
212
225
  if (mc.current_server == NULL){
223
236
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
224
237
  if(ret == -1){
225
238
    perror_plus("clock_gettime");
226
 
    return -1;
 
239
    return false;
227
240
  }
228
 
  return 0;
 
241
  return true;
229
242
}
230
243
 
231
244
/* 
253
266
    
254
267
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
255
268
    if(rc != GPG_ERR_NO_ERROR){
256
 
      fprintf(stderr, "Mandos plugin mandos-client: "
257
 
              "bad gpgme_data_new_from_fd: %s: %s\n",
258
 
              gpgme_strsource(rc), gpgme_strerror(rc));
 
269
      fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
 
270
                   gpgme_strsource(rc), gpgme_strerror(rc));
259
271
      return false;
260
272
    }
261
273
    
262
274
    rc = gpgme_op_import(mc.ctx, pgp_data);
263
275
    if(rc != GPG_ERR_NO_ERROR){
264
 
      fprintf(stderr, "Mandos plugin mandos-client: "
265
 
              "bad gpgme_op_import: %s: %s\n",
266
 
              gpgme_strsource(rc), gpgme_strerror(rc));
 
276
      fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
 
277
                   gpgme_strsource(rc), gpgme_strerror(rc));
267
278
      return false;
268
279
    }
269
280
    
276
287
  }
277
288
  
278
289
  if(debug){
279
 
    fprintf(stderr, "Mandos plugin mandos-client: "
280
 
            "Initializing GPGME\n");
 
290
    fprintf_plus(stderr, "Initializing GPGME\n");
281
291
  }
282
292
  
283
293
  /* Init GPGME */
284
294
  gpgme_check_version(NULL);
285
295
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
286
296
  if(rc != GPG_ERR_NO_ERROR){
287
 
    fprintf(stderr, "Mandos plugin mandos-client: "
288
 
            "bad gpgme_engine_check_version: %s: %s\n",
289
 
            gpgme_strsource(rc), gpgme_strerror(rc));
 
297
    fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
 
298
                 gpgme_strsource(rc), gpgme_strerror(rc));
290
299
    return false;
291
300
  }
292
301
  
293
302
  /* Set GPGME home directory for the OpenPGP engine only */
294
303
  rc = gpgme_get_engine_info(&engine_info);
295
304
  if(rc != GPG_ERR_NO_ERROR){
296
 
    fprintf(stderr, "Mandos plugin mandos-client: "
297
 
            "bad gpgme_get_engine_info: %s: %s\n",
298
 
            gpgme_strsource(rc), gpgme_strerror(rc));
 
305
    fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
 
306
                 gpgme_strsource(rc), gpgme_strerror(rc));
299
307
    return false;
300
308
  }
301
309
  while(engine_info != NULL){
307
315
    engine_info = engine_info->next;
308
316
  }
309
317
  if(engine_info == NULL){
310
 
    fprintf(stderr, "Mandos plugin mandos-client: "
311
 
            "Could not set GPGME home dir to %s\n", tempdir);
 
318
    fprintf_plus(stderr, "Could not set GPGME home dir to %s\n",
 
319
                 tempdir);
312
320
    return false;
313
321
  }
314
322
  
315
323
  /* Create new GPGME "context" */
316
324
  rc = gpgme_new(&(mc.ctx));
317
325
  if(rc != GPG_ERR_NO_ERROR){
318
 
    fprintf(stderr, "Mandos plugin mandos-client: "
319
 
            "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
320
 
            gpgme_strerror(rc));
 
326
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
 
327
                 "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
 
328
                 gpgme_strerror(rc));
321
329
    return false;
322
330
  }
323
331
  
342
350
  ssize_t plaintext_length = 0;
343
351
  
344
352
  if(debug){
345
 
    fprintf(stderr, "Mandos plugin mandos-client: "
346
 
            "Trying to decrypt OpenPGP data\n");
 
353
    fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
347
354
  }
348
355
  
349
356
  /* Create new GPGME data buffer from memory cryptotext */
350
357
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
351
358
                               0);
352
359
  if(rc != GPG_ERR_NO_ERROR){
353
 
    fprintf(stderr, "Mandos plugin mandos-client: "
354
 
            "bad gpgme_data_new_from_mem: %s: %s\n",
355
 
            gpgme_strsource(rc), gpgme_strerror(rc));
 
360
    fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
 
361
                 gpgme_strsource(rc), gpgme_strerror(rc));
356
362
    return -1;
357
363
  }
358
364
  
359
365
  /* Create new empty GPGME data buffer for the plaintext */
360
366
  rc = gpgme_data_new(&dh_plain);
361
367
  if(rc != GPG_ERR_NO_ERROR){
362
 
    fprintf(stderr, "Mandos plugin mandos-client: "
363
 
            "bad gpgme_data_new: %s: %s\n",
364
 
            gpgme_strsource(rc), gpgme_strerror(rc));
 
368
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
 
369
                 "bad gpgme_data_new: %s: %s\n",
 
370
                 gpgme_strsource(rc), gpgme_strerror(rc));
365
371
    gpgme_data_release(dh_crypto);
366
372
    return -1;
367
373
  }
370
376
     data buffer */
371
377
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
372
378
  if(rc != GPG_ERR_NO_ERROR){
373
 
    fprintf(stderr, "Mandos plugin mandos-client: "
374
 
            "bad gpgme_op_decrypt: %s: %s\n",
375
 
            gpgme_strsource(rc), gpgme_strerror(rc));
 
379
    fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
 
380
                 gpgme_strsource(rc), gpgme_strerror(rc));
376
381
    plaintext_length = -1;
377
382
    if(debug){
378
383
      gpgme_decrypt_result_t result;
379
384
      result = gpgme_op_decrypt_result(mc.ctx);
380
385
      if(result == NULL){
381
 
        fprintf(stderr, "Mandos plugin mandos-client: "
382
 
                "gpgme_op_decrypt_result failed\n");
 
386
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
383
387
      } else {
384
 
        fprintf(stderr, "Mandos plugin mandos-client: "
385
 
                "Unsupported algorithm: %s\n",
386
 
                result->unsupported_algorithm);
387
 
        fprintf(stderr, "Mandos plugin mandos-client: "
388
 
                "Wrong key usage: %u\n",
389
 
                result->wrong_key_usage);
 
388
        fprintf_plus(stderr, "Unsupported algorithm: %s\n",
 
389
                     result->unsupported_algorithm);
 
390
        fprintf_plus(stderr, "Wrong key usage: %u\n",
 
391
                     result->wrong_key_usage);
390
392
        if(result->file_name != NULL){
391
 
          fprintf(stderr, "Mandos plugin mandos-client: "
392
 
                  "File name: %s\n", result->file_name);
 
393
          fprintf_plus(stderr, "File name: %s\n", result->file_name);
393
394
        }
394
395
        gpgme_recipient_t recipient;
395
396
        recipient = result->recipients;
396
397
        while(recipient != NULL){
397
 
          fprintf(stderr, "Mandos plugin mandos-client: "
398
 
                  "Public key algorithm: %s\n",
399
 
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
400
 
          fprintf(stderr, "Mandos plugin mandos-client: "
401
 
                  "Key ID: %s\n", recipient->keyid);
402
 
          fprintf(stderr, "Mandos plugin mandos-client: "
403
 
                  "Secret key available: %s\n",
404
 
                  recipient->status == GPG_ERR_NO_SECKEY
405
 
                  ? "No" : "Yes");
 
398
          fprintf_plus(stderr, "Public key algorithm: %s\n",
 
399
                       gpgme_pubkey_algo_name
 
400
                       (recipient->pubkey_algo));
 
401
          fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
 
402
          fprintf_plus(stderr, "Secret key available: %s\n",
 
403
                       recipient->status == GPG_ERR_NO_SECKEY
 
404
                       ? "No" : "Yes");
406
405
          recipient = recipient->next;
407
406
        }
408
407
      }
411
410
  }
412
411
  
413
412
  if(debug){
414
 
    fprintf(stderr, "Mandos plugin mandos-client: "
415
 
            "Decryption of OpenPGP data succeeded\n");
 
413
    fprintf_plus(stderr, "Decryption of OpenPGP data succeeded\n");
416
414
  }
417
415
  
418
416
  /* Seek back to the beginning of the GPGME plaintext data buffer */
449
447
  }
450
448
  
451
449
  if(debug){
452
 
    fprintf(stderr, "Mandos plugin mandos-client: "
453
 
            "Decrypted password is: ");
 
450
    fprintf_plus(stderr, "Decrypted password is: ");
454
451
    for(ssize_t i = 0; i < plaintext_length; i++){
455
452
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
456
453
    }
478
475
/* GnuTLS log function callback */
479
476
static void debuggnutls(__attribute__((unused)) int level,
480
477
                        const char* string){
481
 
  fprintf(stderr, "Mandos plugin mandos-client: GnuTLS: %s", string);
 
478
  fprintf_plus(stderr, "GnuTLS: %s", string);
482
479
}
483
480
 
484
481
static int init_gnutls_global(const char *pubkeyfilename,
486
483
  int ret;
487
484
  
488
485
  if(debug){
489
 
    fprintf(stderr, "Mandos plugin mandos-client: "
490
 
            "Initializing GnuTLS\n");
 
486
    fprintf_plus(stderr, "Initializing GnuTLS\n");
491
487
  }
492
488
  
493
489
  ret = gnutls_global_init();
494
490
  if(ret != GNUTLS_E_SUCCESS){
495
 
    fprintf(stderr, "Mandos plugin mandos-client: "
496
 
            "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
 
491
    fprintf_plus(stderr, "GnuTLS global_init: %s\n",
 
492
                 safer_gnutls_strerror(ret));
497
493
    return -1;
498
494
  }
499
495
  
508
504
  /* OpenPGP credentials */
509
505
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
510
506
  if(ret != GNUTLS_E_SUCCESS){
511
 
    fprintf(stderr, "Mandos plugin mandos-client: "
512
 
            "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
 
507
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
 
508
                 safer_gnutls_strerror(ret));
513
509
    gnutls_global_deinit();
514
510
    return -1;
515
511
  }
516
512
  
517
513
  if(debug){
518
 
    fprintf(stderr, "Mandos plugin mandos-client: "
519
 
            "Attempting to use OpenPGP public key %s and"
520
 
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
521
 
            seckeyfilename);
 
514
    fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
 
515
                 " secret key %s as GnuTLS credentials\n",
 
516
                 pubkeyfilename,
 
517
                 seckeyfilename);
522
518
  }
523
519
  
524
520
  ret = gnutls_certificate_set_openpgp_key_file
525
521
    (mc.cred, pubkeyfilename, seckeyfilename,
526
522
     GNUTLS_OPENPGP_FMT_BASE64);
527
523
  if(ret != GNUTLS_E_SUCCESS){
528
 
    fprintf(stderr,
529
 
            "Mandos plugin mandos-client: "
530
 
            "Error[%d] while reading the OpenPGP key pair ('%s',"
531
 
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
532
 
    fprintf(stderr, "Mandos plugin mandos-client: "
533
 
            "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
 
524
    fprintf_plus(stderr,
 
525
                 "Error[%d] while reading the OpenPGP key pair ('%s',"
 
526
                 " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
527
    fprintf_plus(stderr, "The GnuTLS error is: %s\n",
 
528
                 safer_gnutls_strerror(ret));
534
529
    goto globalfail;
535
530
  }
536
531
  
537
532
  /* GnuTLS server initialization */
538
533
  ret = gnutls_dh_params_init(&mc.dh_params);
539
534
  if(ret != GNUTLS_E_SUCCESS){
540
 
    fprintf(stderr, "Mandos plugin mandos-client: "
541
 
            "Error in GnuTLS DH parameter initialization:"
542
 
            " %s\n", safer_gnutls_strerror(ret));
 
535
    fprintf_plus(stderr, "Error in GnuTLS DH parameter"
 
536
                 " initialization: %s\n",
 
537
                 safer_gnutls_strerror(ret));
543
538
    goto globalfail;
544
539
  }
545
540
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
546
541
  if(ret != GNUTLS_E_SUCCESS){
547
 
    fprintf(stderr, "Mandos plugin mandos-client: "
548
 
            "Error in GnuTLS prime generation: %s\n",
549
 
            safer_gnutls_strerror(ret));
 
542
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
 
543
                 safer_gnutls_strerror(ret));
550
544
    goto globalfail;
551
545
  }
552
546
  
572
566
    }
573
567
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
574
568
  if(ret != GNUTLS_E_SUCCESS){
575
 
    fprintf(stderr, "Mandos plugin mandos-client: "
576
 
            "Error in GnuTLS session initialization: %s\n",
577
 
            safer_gnutls_strerror(ret));
 
569
    fprintf_plus(stderr,
 
570
                 "Error in GnuTLS session initialization: %s\n",
 
571
                 safer_gnutls_strerror(ret));
578
572
  }
579
573
  
580
574
  {
587
581
      }
588
582
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
589
583
    if(ret != GNUTLS_E_SUCCESS){
590
 
      fprintf(stderr, "Mandos plugin mandos-client: "
591
 
              "Syntax error at: %s\n", err);
592
 
      fprintf(stderr, "Mandos plugin mandos-client: "
593
 
              "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
 
584
      fprintf_plus(stderr, "Syntax error at: %s\n", err);
 
585
      fprintf_plus(stderr, "GnuTLS error: %s\n",
 
586
                   safer_gnutls_strerror(ret));
594
587
      gnutls_deinit(*session);
595
588
      return -1;
596
589
    }
605
598
    }
606
599
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
607
600
  if(ret != GNUTLS_E_SUCCESS){
608
 
    fprintf(stderr, "Mandos plugin mandos-client: "
609
 
            "Error setting GnuTLS credentials: %s\n",
610
 
            safer_gnutls_strerror(ret));
 
601
    fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
 
602
                 safer_gnutls_strerror(ret));
611
603
    gnutls_deinit(*session);
612
604
    return -1;
613
605
  }
658
650
    pf = PF_INET;
659
651
    break;
660
652
  default:
661
 
    fprintf(stderr, "Mandos plugin mandos-client: "
662
 
            "Bad address family: %d\n", af);
 
653
    fprintf_plus(stderr, "Bad address family: %d\n", af);
663
654
    errno = EINVAL;
664
655
    return -1;
665
656
  }
670
661
  }
671
662
  
672
663
  if(debug){
673
 
    fprintf(stderr, "Mandos plugin mandos-client: "
674
 
            "Setting up a TCP connection to %s, port %" PRIu16
675
 
            "\n", ip, port);
 
664
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
 
665
                 PRIu16 "\n", ip, port);
676
666
  }
677
667
  
678
668
  tcp_sd = socket(pf, SOCK_STREAM, 0);
704
694
  }
705
695
  if(ret == 0){
706
696
    int e = errno;
707
 
    fprintf(stderr, "Mandos plugin mandos-client: "
708
 
            "Bad address: %s\n", ip);
 
697
    fprintf_plus(stderr, "Bad address: %s\n", ip);
709
698
    errno = e;
710
699
    goto mandos_end;
711
700
  }
718
707
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
719
708
                                -Wunreachable-code*/
720
709
      if(if_index == AVAHI_IF_UNSPEC){
721
 
        fprintf(stderr, "Mandos plugin mandos-client: "
722
 
                "An IPv6 link-local address is incomplete"
723
 
                " without a network interface\n");
 
710
        fprintf_plus(stderr, "An IPv6 link-local address is"
 
711
                     " incomplete without a network interface\n");
724
712
        errno = EINVAL;
725
713
        goto mandos_end;
726
714
      }
744
732
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
745
733
        perror_plus("if_indextoname");
746
734
      } else {
747
 
        fprintf(stderr, "Mandos plugin mandos-client: "
748
 
                "Connection to: %s%%%s, port %" PRIu16 "\n",
749
 
                ip, interface, port);
 
735
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
 
736
                     "\n", ip, interface, port);
750
737
      }
751
738
    } else {
752
 
      fprintf(stderr, "Mandos plugin mandos-client: "
753
 
              "Connection to: %s, port %" PRIu16 "\n", ip, port);
 
739
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
 
740
                   ip, port);
754
741
    }
755
742
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
756
743
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
766
753
      perror_plus("inet_ntop");
767
754
    } else {
768
755
      if(strcmp(addrstr, ip) != 0){
769
 
        fprintf(stderr, "Mandos plugin mandos-client: "
770
 
                "Canonical address form: %s\n", addrstr);
 
756
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
771
757
      }
772
758
    }
773
759
  }
827
813
  }
828
814
  
829
815
  if(debug){
830
 
    fprintf(stderr, "Mandos plugin mandos-client: "
831
 
            "Establishing TLS session with %s\n", ip);
 
816
    fprintf_plus(stderr, "Establishing TLS session with %s\n", ip);
832
817
  }
833
818
  
834
819
  if(quit_now){
836
821
    goto mandos_end;
837
822
  }
838
823
  
839
 
  /* Spurious warning from -Wint-to-pointer-cast */
840
 
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
 
824
  /* This casting via intptr_t is to eliminate warning about casting
 
825
     an int to a pointer type.  This is exactly how the GnuTLS Guile
 
826
     function "set-session-transport-fd!" does it. */
 
827
  gnutls_transport_set_ptr(session,
 
828
                           (gnutls_transport_ptr_t)(intptr_t)tcp_sd);
841
829
  
842
830
  if(quit_now){
843
831
    errno = EINTR;
854
842
  
855
843
  if(ret != GNUTLS_E_SUCCESS){
856
844
    if(debug){
857
 
      fprintf(stderr, "Mandos plugin mandos-client: "
858
 
              "*** GnuTLS Handshake failed ***\n");
 
845
      fprintf_plus(stderr, "*** GnuTLS Handshake failed ***\n");
859
846
      gnutls_perror(ret);
860
847
    }
861
848
    errno = EPROTO;
865
852
  /* Read OpenPGP packet that contains the wanted password */
866
853
  
867
854
  if(debug){
868
 
    fprintf(stderr, "Mandos plugin mandos-client: "
869
 
            "Retrieving OpenPGP encrypted password from %s\n", ip);
 
855
    fprintf_plus(stderr, "Retrieving OpenPGP encrypted password from"
 
856
                 " %s\n", ip);
870
857
  }
871
858
  
872
859
  while(true){
910
897
          }
911
898
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
912
899
        if(ret < 0){
913
 
          fprintf(stderr, "Mandos plugin mandos-client: "
914
 
                  "*** GnuTLS Re-handshake failed ***\n");
 
900
          fprintf_plus(stderr, "*** GnuTLS Re-handshake failed "
 
901
                       "***\n");
915
902
          gnutls_perror(ret);
916
903
          errno = EPROTO;
917
904
          goto mandos_end;
918
905
        }
919
906
        break;
920
907
      default:
921
 
        fprintf(stderr, "Mandos plugin mandos-client: "
922
 
                "Unknown error while reading data from"
923
 
                " encrypted session with Mandos server\n");
 
908
        fprintf_plus(stderr, "Unknown error while reading data from"
 
909
                     " encrypted session with Mandos server\n");
924
910
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
925
911
        errno = EIO;
926
912
        goto mandos_end;
931
917
  }
932
918
  
933
919
  if(debug){
934
 
    fprintf(stderr, "Mandos plugin mandos-client: "
935
 
            "Closing TLS session\n");
 
920
    fprintf_plus(stderr, "Closing TLS session\n");
936
921
  }
937
922
  
938
923
  if(quit_now){
967
952
        if(ret == 0 and ferror(stdout)){
968
953
          int e = errno;
969
954
          if(debug){
970
 
            fprintf(stderr, "Mandos plugin mandos-client: "
971
 
                    "Error writing encrypted data: %s\n",
972
 
                    strerror(errno));
 
955
            fprintf_plus(stderr, "Error writing encrypted data: %s\n",
 
956
                         strerror(errno));
973
957
          }
974
958
          errno = e;
975
959
          goto mandos_end;
1032
1016
  switch(event){
1033
1017
  default:
1034
1018
  case AVAHI_RESOLVER_FAILURE:
1035
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1036
 
            "(Avahi Resolver) Failed to resolve service '%s'"
1037
 
            " of type '%s' in domain '%s': %s\n", name, type, domain,
1038
 
            avahi_strerror(avahi_server_errno(mc.server)));
 
1019
    fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
 
1020
                 "'%s' of type '%s' in domain '%s': %s\n", name, type,
 
1021
                 domain,
 
1022
                 avahi_strerror(avahi_server_errno(mc.server)));
1039
1023
    break;
1040
1024
    
1041
1025
  case AVAHI_RESOLVER_FOUND:
1043
1027
      char ip[AVAHI_ADDRESS_STR_MAX];
1044
1028
      avahi_address_snprint(ip, sizeof(ip), address);
1045
1029
      if(debug){
1046
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1047
 
                "Mandos server \"%s\" found on %s (%s, %"
1048
 
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1049
 
                ip, (intmax_t)interface, port);
 
1030
        fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
 
1031
                     PRIdMAX ") on port %" PRIu16 "\n", name,
 
1032
                     host_name, ip, (intmax_t)interface, port);
1050
1033
      }
1051
1034
      int ret = start_mandos_communication(ip, port, interface,
1052
1035
                                           avahi_proto_to_af(proto));
1053
1036
      if(ret == 0){
1054
1037
        avahi_simple_poll_quit(mc.simple_poll);
1055
1038
      } else {
1056
 
        ret = add_server(ip, port, interface,
1057
 
                         avahi_proto_to_af(proto));
 
1039
        if(not add_server(ip, port, interface,
 
1040
                          avahi_proto_to_af(proto))){
 
1041
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
 
1042
                       " list\n", name);
 
1043
        }
1058
1044
      }
1059
1045
    }
1060
1046
  }
1084
1070
  default:
1085
1071
  case AVAHI_BROWSER_FAILURE:
1086
1072
    
1087
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1088
 
            "(Avahi browser) %s\n",
1089
 
            avahi_strerror(avahi_server_errno(mc.server)));
 
1073
    fprintf_plus(stderr, "(Avahi browser) %s\n",
 
1074
                 avahi_strerror(avahi_server_errno(mc.server)));
1090
1075
    avahi_simple_poll_quit(mc.simple_poll);
1091
1076
    return;
1092
1077
    
1099
1084
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1100
1085
                                    name, type, domain, protocol, 0,
1101
1086
                                    resolve_callback, NULL) == NULL)
1102
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1103
 
              "Avahi: Failed to resolve service '%s': %s\n",
1104
 
              name, avahi_strerror(avahi_server_errno(mc.server)));
 
1087
      fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
 
1088
                   " %s\n", name,
 
1089
                   avahi_strerror(avahi_server_errno(mc.server)));
1105
1090
    break;
1106
1091
    
1107
1092
  case AVAHI_BROWSER_REMOVE:
1110
1095
  case AVAHI_BROWSER_ALL_FOR_NOW:
1111
1096
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1112
1097
    if(debug){
1113
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1114
 
              "No Mandos server found, still searching...\n");
 
1098
      fprintf_plus(stderr, "No Mandos server found, still"
 
1099
                   " searching...\n");
1115
1100
    }
1116
1101
    break;
1117
1102
  }
1156
1141
  /* Reject the loopback device */
1157
1142
  if(ifr->ifr_flags & IFF_LOOPBACK){
1158
1143
    if(debug){
1159
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1160
 
              "Rejecting loopback interface \"%s\"\n", ifname);
 
1144
      fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
 
1145
                   ifname);
1161
1146
    }
1162
1147
    return false;
1163
1148
  }
1164
1149
  /* Accept point-to-point devices only if connect_to is specified */
1165
1150
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1166
1151
    if(debug){
1167
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1168
 
              "Accepting point-to-point interface \"%s\"\n", ifname);
 
1152
      fprintf_plus(stderr, "Accepting point-to-point interface"
 
1153
                   " \"%s\"\n", ifname);
1169
1154
    }
1170
1155
    return true;
1171
1156
  }
1172
1157
  /* Otherwise, reject non-broadcast-capable devices */
1173
1158
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1174
1159
    if(debug){
1175
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1176
 
              "Rejecting non-broadcast interface \"%s\"\n", ifname);
 
1160
      fprintf_plus(stderr, "Rejecting non-broadcast interface"
 
1161
                   " \"%s\"\n", ifname);
1177
1162
    }
1178
1163
    return false;
1179
1164
  }
1180
1165
  /* Reject non-ARP interfaces (including dummy interfaces) */
1181
1166
  if(ifr->ifr_flags & IFF_NOARP){
1182
1167
    if(debug){
1183
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1184
 
              "Rejecting non-ARP interface \"%s\"\n", ifname);
 
1168
      fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
 
1169
                   ifname);
1185
1170
    }
1186
1171
    return false;
1187
1172
  }
1188
1173
  
1189
1174
  /* Accept this device */
1190
1175
  if(debug){
1191
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1192
 
            "Interface \"%s\" is good\n", ifname);
 
1176
    fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1193
1177
  }
1194
1178
  return true;
1195
1179
}
1207
1191
  struct ifreq ifr;
1208
1192
  if(not get_flags(if_entry->d_name, &ifr)){
1209
1193
    if(debug){
1210
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1211
 
              "Failed to get flags for interface \"%s\"\n",
1212
 
              if_entry->d_name);
 
1194
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1195
                   "\"%s\"\n", if_entry->d_name);
1213
1196
    }
1214
1197
    return 0;
1215
1198
  }
1233
1216
  struct ifreq ifr;
1234
1217
  if(not get_flags(if_entry->d_name, &ifr)){
1235
1218
    if(debug){
1236
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1237
 
              "Failed to get flags for interface \"%s\"\n",
1238
 
              if_entry->d_name);
 
1219
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1220
                   "\"%s\"\n", if_entry->d_name);
1239
1221
    }
1240
1222
    return 0;
1241
1223
  }
1243
1225
  /* Reject down interfaces */
1244
1226
  if(not (ifr.ifr_flags & IFF_UP)){
1245
1227
    if(debug){
1246
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1247
 
              "Rejecting down interface \"%s\"\n",
1248
 
              if_entry->d_name);
 
1228
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
 
1229
                   if_entry->d_name);
1249
1230
    }
1250
1231
    return 0;
1251
1232
  }
1253
1234
  /* Reject non-running interfaces */
1254
1235
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1255
1236
    if(debug){
1256
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1257
 
              "Rejecting non-running interface \"%s\"\n",
1258
 
              if_entry->d_name);
 
1237
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
 
1238
                   if_entry->d_name);
1259
1239
    }
1260
1240
    return 0;
1261
1241
  }
1295
1275
  if((direntry->d_name)[sret] != '\0'){
1296
1276
    /* Contains non-allowed characters */
1297
1277
    if(debug){
1298
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1299
 
              "Ignoring hook \"%s\" with bad name\n",
1300
 
              direntry->d_name);
 
1278
      fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
 
1279
                   direntry->d_name);
1301
1280
    }
1302
1281
    return 0;
1303
1282
  }
1319
1298
  if(not (S_ISREG(st.st_mode))){
1320
1299
    /* Not a regular file */
1321
1300
    if(debug){
1322
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1323
 
              "Ignoring hook \"%s\" - not a file\n",
1324
 
              direntry->d_name);
 
1301
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
 
1302
                   direntry->d_name);
1325
1303
    }
1326
1304
    return 0;
1327
1305
  }
1328
1306
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1329
1307
    /* Not executable */
1330
1308
    if(debug){
1331
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1332
 
              "Ignoring hook \"%s\" - not executable\n",
1333
 
              direntry->d_name);
 
1309
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
 
1310
                   direntry->d_name);
1334
1311
    }
1335
1312
    return 0;
1336
1313
  }
 
1314
  if(debug){
 
1315
    fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
 
1316
                 direntry->d_name);
 
1317
  }
1337
1318
  return 1;
1338
1319
}
1339
1320
 
1346
1327
  while(true){
1347
1328
    if(mc.current_server == NULL){
1348
1329
      if (debug){
1349
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1350
 
                "Wait until first server is found. No timeout!\n");
 
1330
        fprintf_plus(stderr, "Wait until first server is found."
 
1331
                     " No timeout!\n");
1351
1332
      }
1352
1333
      ret = avahi_simple_poll_iterate(s, -1);
1353
1334
    } else {
1354
1335
      if (debug){
1355
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1356
 
                "Check current_server if we should run it,"
1357
 
                " or wait\n");
 
1336
        fprintf_plus(stderr, "Check current_server if we should run"
 
1337
                     " it, or wait\n");
1358
1338
      }
1359
1339
      /* the current time */
1360
1340
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1376
1356
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1377
1357
      
1378
1358
      if (debug){
1379
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1380
 
                "Blocking for %" PRIdMAX " ms\n", block_time);
 
1359
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
 
1360
                     block_time);
1381
1361
      }
1382
1362
      
1383
1363
      if(block_time <= 0){
1422
1402
  } else {
1423
1403
    int devnull = open("/dev/null", O_RDONLY);
1424
1404
    for(int i = 0; i < numhooks; i++){
1425
 
      direntry = direntries[0];
 
1405
      direntry = direntries[i];
1426
1406
      char *fullname = NULL;
1427
1407
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1428
1408
      if(ret < 0){
1429
1409
        perror_plus("asprintf");
1430
1410
        continue;
1431
1411
      }
 
1412
      if(debug){
 
1413
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1414
                     direntry->d_name);
 
1415
      }
1432
1416
      pid_t hook_pid = fork();
1433
1417
      if(hook_pid == 0){
1434
1418
        /* Child */
 
1419
        /* Raise privileges */
 
1420
        errno = 0;
 
1421
        ret = seteuid(0);
 
1422
        if(ret == -1){
 
1423
          perror_plus("seteuid");
 
1424
        }
 
1425
        /* Raise privileges even more */
 
1426
        errno = 0;
 
1427
        ret = setuid(0);
 
1428
        if(ret == -1){
 
1429
          perror_plus("setuid");
 
1430
        }
 
1431
        /* Set group */
 
1432
        errno = 0;
 
1433
        ret = setgid(0);
 
1434
        if(ret == -1){
 
1435
          perror_plus("setgid");
 
1436
        }
 
1437
        /* Reset supplementary groups */
 
1438
        errno = 0;
 
1439
        ret = setgroups(0, NULL);
 
1440
        if(ret == -1){
 
1441
          perror_plus("setgroups");
 
1442
        }
1435
1443
        dup2(devnull, STDIN_FILENO);
1436
1444
        close(devnull);
1437
1445
        dup2(STDERR_FILENO, STDOUT_FILENO);
1438
1446
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1439
1447
        if(ret == -1){
1440
1448
          perror_plus("setenv");
1441
 
          return false;
 
1449
          _exit(EX_OSERR);
1442
1450
        }
1443
1451
        ret = setenv("DEVICE", interface, 1);
1444
1452
        if(ret == -1){
1445
1453
          perror_plus("setenv");
1446
 
          return false;
 
1454
          _exit(EX_OSERR);
1447
1455
        }
1448
 
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1456
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1449
1457
        if(ret == -1){
1450
1458
          perror_plus("setenv");
1451
 
          return false;
 
1459
          _exit(EX_OSERR);
1452
1460
        }
1453
1461
        ret = setenv("MODE", mode, 1);
1454
1462
        if(ret == -1){
1455
1463
          perror_plus("setenv");
1456
 
          return false;
 
1464
          _exit(EX_OSERR);
1457
1465
        }
1458
1466
        char *delaystring;
1459
1467
        ret = asprintf(&delaystring, "%f", delay);
1460
1468
        if(ret == -1){
1461
1469
          perror_plus("asprintf");
1462
 
          return false;
 
1470
          _exit(EX_OSERR);
1463
1471
        }
1464
1472
        ret = setenv("DELAY", delaystring, 1);
1465
1473
        if(ret == -1){
1466
1474
          free(delaystring);
1467
1475
          perror_plus("setenv");
1468
 
          return false;
 
1476
          _exit(EX_OSERR);
1469
1477
        }
1470
1478
        free(delaystring);
1471
 
        ret = execl(fullname, direntry->d_name, mode, NULL);
1472
 
        perror_plus("execl");
 
1479
        if(connect_to != NULL){
 
1480
          ret = setenv("CONNECT", connect_to, 1);
 
1481
          if(ret == -1){
 
1482
            perror_plus("setenv");
 
1483
            _exit(EX_OSERR);
 
1484
          }
 
1485
        }
 
1486
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1487
          perror_plus("execl");
 
1488
          _exit(EXIT_FAILURE);
 
1489
        }
1473
1490
      } else {
1474
1491
        int status;
1475
1492
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1479
1496
        }
1480
1497
        if(WIFEXITED(status)){
1481
1498
          if(WEXITSTATUS(status) != 0){
1482
 
            fprintf(stderr, "Mandos plugin mandos-client: "
1483
 
                    "Warning: network hook \"%s\" exited"
1484
 
                    " with status %d\n", direntry->d_name,
1485
 
                    WEXITSTATUS(status));
 
1499
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1500
                         " with status %d\n", direntry->d_name,
 
1501
                         WEXITSTATUS(status));
1486
1502
            free(fullname);
1487
1503
            continue;
1488
1504
          }
1489
1505
        } else if(WIFSIGNALED(status)){
1490
 
          fprintf(stderr, "Mandos plugin mandos-client: "
1491
 
                  "Warning: network hook \"%s\" died by"
1492
 
                  " signal %d\n", direntry->d_name,
1493
 
                  WTERMSIG(status));
 
1506
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1507
                       " signal %d\n", direntry->d_name,
 
1508
                       WTERMSIG(status));
1494
1509
          free(fullname);
1495
1510
          continue;
1496
1511
        } else {
1497
 
          fprintf(stderr, "Mandos plugin mandos-client: "
1498
 
                  "Warning: network hook \"%s\" crashed\n",
1499
 
                  direntry->d_name);
 
1512
          fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1513
                       " crashed\n", direntry->d_name);
1500
1514
          free(fullname);
1501
1515
          continue;
1502
1516
        }
1503
1517
      }
1504
1518
      free(fullname);
1505
 
      if(quit_now){
1506
 
        break;
 
1519
      if(debug){
 
1520
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1521
                     direntry->d_name);
1507
1522
      }
1508
1523
    }
1509
1524
    close(devnull);
1596
1611
        .group = 2 },
1597
1612
      { .name = "retry", .key = 132,
1598
1613
        .arg = "SECONDS",
1599
 
        .doc = "Retry interval used when denied by the mandos server",
 
1614
        .doc = "Retry interval used when denied by the Mandos server",
1600
1615
        .group = 2 },
1601
1616
      { .name = "network-hook-dir", .key = 133,
1602
1617
        .arg = "DIR",
1674
1689
        argp_state_help(state, state->out_stream,
1675
1690
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1676
1691
      case 'V':                 /* --version */
1677
 
        fprintf(state->out_stream, "Mandos plugin mandos-client: ");
1678
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
 
1692
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1679
1693
        exit(argp_err_exit_status);
1680
1694
        break;
1681
1695
      default:
1708
1722
  {
1709
1723
    /* Work around Debian bug #633582:
1710
1724
       <http://bugs.debian.org/633582> */
1711
 
    struct stat st;
1712
1725
    
1713
1726
    /* Re-raise priviliges */
1714
1727
    errno = 0;
1715
1728
    ret = seteuid(0);
1716
1729
    if(ret == -1){
1717
1730
      perror_plus("seteuid");
1718
 
    }
1719
 
    
1720
 
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1721
 
      int seckey_fd = open(seckey, O_RDONLY);
1722
 
      if(seckey_fd == -1){
1723
 
        perror_plus("open");
1724
 
      } else {
1725
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1726
 
        if(ret == -1){
1727
 
          perror_plus("fstat");
1728
 
        } else {
1729
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1730
 
            ret = fchown(seckey_fd, uid, gid);
1731
 
            if(ret == -1){
1732
 
              perror_plus("fchown");
1733
 
            }
1734
 
          }
1735
 
        }
1736
 
        TEMP_FAILURE_RETRY(close(seckey_fd));
1737
 
      }
1738
 
    }
1739
 
    
1740
 
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1741
 
      int pubkey_fd = open(pubkey, O_RDONLY);
1742
 
      if(pubkey_fd == -1){
1743
 
        perror_plus("open");
1744
 
      } else {
1745
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1746
 
        if(ret == -1){
1747
 
          perror_plus("fstat");
1748
 
        } else {
1749
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1750
 
            ret = fchown(pubkey_fd, uid, gid);
1751
 
            if(ret == -1){
1752
 
              perror_plus("fchown");
1753
 
            }
1754
 
          }
1755
 
        }
1756
 
        TEMP_FAILURE_RETRY(close(pubkey_fd));
1757
 
      }
1758
 
    }
1759
 
    
1760
 
    /* Lower privileges */
1761
 
    errno = 0;
1762
 
    ret = seteuid(uid);
1763
 
    if(ret == -1){
1764
 
      perror_plus("seteuid");
 
1731
    } else {
 
1732
      struct stat st;
 
1733
      
 
1734
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1735
        int seckey_fd = open(seckey, O_RDONLY);
 
1736
        if(seckey_fd == -1){
 
1737
          perror_plus("open");
 
1738
        } else {
 
1739
          ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1740
          if(ret == -1){
 
1741
            perror_plus("fstat");
 
1742
          } else {
 
1743
            if(S_ISREG(st.st_mode)
 
1744
               and st.st_uid == 0 and st.st_gid == 0){
 
1745
              ret = fchown(seckey_fd, uid, gid);
 
1746
              if(ret == -1){
 
1747
                perror_plus("fchown");
 
1748
              }
 
1749
            }
 
1750
          }
 
1751
          TEMP_FAILURE_RETRY(close(seckey_fd));
 
1752
        }
 
1753
      }
 
1754
    
 
1755
      if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
 
1756
        int pubkey_fd = open(pubkey, O_RDONLY);
 
1757
        if(pubkey_fd == -1){
 
1758
          perror_plus("open");
 
1759
        } else {
 
1760
          ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1761
          if(ret == -1){
 
1762
            perror_plus("fstat");
 
1763
          } else {
 
1764
            if(S_ISREG(st.st_mode)
 
1765
               and st.st_uid == 0 and st.st_gid == 0){
 
1766
              ret = fchown(pubkey_fd, uid, gid);
 
1767
              if(ret == -1){
 
1768
                perror_plus("fchown");
 
1769
              }
 
1770
            }
 
1771
          }
 
1772
          TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1773
        }
 
1774
      }
 
1775
    
 
1776
      /* Lower privileges */
 
1777
      errno = 0;
 
1778
      ret = seteuid(uid);
 
1779
      if(ret == -1){
 
1780
        perror_plus("seteuid");
 
1781
      }
1765
1782
    }
1766
1783
  }
1767
1784
  
1768
1785
  /* Run network hooks */
1769
 
  {
1770
 
    /* Re-raise priviliges */
1771
 
    errno = 0;
1772
 
    ret = seteuid(0);
1773
 
    if(ret == -1){
1774
 
      perror_plus("seteuid");
1775
 
    }
1776
 
    if(not run_network_hooks("start", interface, delay)){
1777
 
      goto end;
1778
 
    }
1779
 
    /* Lower privileges */
1780
 
    errno = 0;
1781
 
    ret = seteuid(uid);
1782
 
    if(ret == -1){
1783
 
      perror_plus("seteuid");
1784
 
    }
 
1786
  if(not run_network_hooks("start", interface, delay)){
 
1787
    goto end;
1785
1788
  }
1786
1789
  
1787
1790
  if(not debug){
1803
1806
      /* Pick the first interface returned */
1804
1807
      interface = strdup(direntries[0]->d_name);
1805
1808
      if(debug){
1806
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1807
 
                "Using interface \"%s\"\n", interface);
 
1809
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1808
1810
      }
1809
1811
      if(interface == NULL){
1810
1812
        perror_plus("malloc");
1815
1817
      free(direntries);
1816
1818
    } else {
1817
1819
      free(direntries);
1818
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1819
 
              "Could not find a network interface\n");
 
1820
      fprintf_plus(stderr, "Could not find a network interface\n");
1820
1821
      exitcode = EXIT_FAILURE;
1821
1822
      goto end;
1822
1823
    }
1828
1829
  srand((unsigned int) time(NULL));
1829
1830
  mc.simple_poll = avahi_simple_poll_new();
1830
1831
  if(mc.simple_poll == NULL){
1831
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1832
 
            "Avahi: Failed to create simple poll object.\n");
 
1832
    fprintf_plus(stderr,
 
1833
                 "Avahi: Failed to create simple poll object.\n");
1833
1834
    exitcode = EX_UNAVAILABLE;
1834
1835
    goto end;
1835
1836
  }
1901
1902
  if(strcmp(interface, "none") != 0){
1902
1903
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1903
1904
    if(if_index == 0){
1904
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1905
 
              "No such interface: \"%s\"\n", interface);
 
1905
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1906
1906
      exitcode = EX_UNAVAILABLE;
1907
1907
      goto end;
1908
1908
    }
2041
2041
  
2042
2042
  ret = init_gnutls_global(pubkey, seckey);
2043
2043
  if(ret == -1){
2044
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2045
 
            "init_gnutls_global failed\n");
 
2044
    fprintf_plus(stderr, "init_gnutls_global failed\n");
2046
2045
    exitcode = EX_UNAVAILABLE;
2047
2046
    goto end;
2048
2047
  } else {
2064
2063
  }
2065
2064
  
2066
2065
  if(not init_gpgme(pubkey, seckey, tempdir)){
2067
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2068
 
            "init_gpgme failed\n");
 
2066
    fprintf_plus(stderr, "init_gpgme failed\n");
2069
2067
    exitcode = EX_UNAVAILABLE;
2070
2068
    goto end;
2071
2069
  } else {
2081
2079
    /* (Mainly meant for debugging) */
2082
2080
    char *address = strrchr(connect_to, ':');
2083
2081
    if(address == NULL){
2084
 
      fprintf(stderr, "Mandos plugin mandos-client: "
2085
 
              "No colon in address\n");
 
2082
      fprintf_plus(stderr, "No colon in address\n");
2086
2083
      exitcode = EX_USAGE;
2087
2084
      goto end;
2088
2085
    }
2096
2093
    tmpmax = strtoimax(address+1, &tmp, 10);
2097
2094
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2098
2095
       or tmpmax != (uint16_t)tmpmax){
2099
 
      fprintf(stderr, "Mandos plugin mandos-client: "
2100
 
              "Bad port number\n");
 
2096
      fprintf_plus(stderr, "Bad port number\n");
2101
2097
      exitcode = EX_USAGE;
2102
2098
      goto end;
2103
2099
    }
2133
2129
        break;
2134
2130
      }
2135
2131
      if(debug){
2136
 
        fprintf(stderr, "Mandos plugin mandos-client: "
2137
 
                "Retrying in %d seconds\n", (int)retry_interval);
 
2132
        fprintf_plus(stderr, "Retrying in %d seconds\n",
 
2133
                     (int)retry_interval);
2138
2134
      }
2139
2135
      sleep((int)retry_interval);
2140
2136
    }
2170
2166
  
2171
2167
  /* Check if creating the Avahi server object succeeded */
2172
2168
  if(mc.server == NULL){
2173
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2174
 
            "Failed to create Avahi server: %s\n",
2175
 
            avahi_strerror(error));
 
2169
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
 
2170
                 avahi_strerror(error));
2176
2171
    exitcode = EX_UNAVAILABLE;
2177
2172
    goto end;
2178
2173
  }
2186
2181
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2187
2182
                                   NULL, 0, browse_callback, NULL);
2188
2183
  if(sb == NULL){
2189
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2190
 
            "Failed to create service browser: %s\n",
2191
 
            avahi_strerror(avahi_server_errno(mc.server)));
 
2184
    fprintf_plus(stderr, "Failed to create service browser: %s\n",
 
2185
                 avahi_strerror(avahi_server_errno(mc.server)));
2192
2186
    exitcode = EX_UNAVAILABLE;
2193
2187
    goto end;
2194
2188
  }
2200
2194
  /* Run the main loop */
2201
2195
  
2202
2196
  if(debug){
2203
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2204
 
            "Starting Avahi loop search\n");
 
2197
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2205
2198
  }
2206
2199
 
2207
2200
  ret = avahi_loop_with_timeout(mc.simple_poll,
2208
2201
                                (int)(retry_interval * 1000));
2209
2202
  if(debug){
2210
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2211
 
            "avahi_loop_with_timeout exited %s\n",
2212
 
            (ret == 0) ? "successfully" : "with error");
 
2203
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
 
2204
                 (ret == 0) ? "successfully" : "with error");
2213
2205
  }
2214
2206
  
2215
2207
 end:
2216
2208
  
2217
2209
  if(debug){
2218
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2219
 
            "%s exiting\n", argv[0]);
 
2210
    fprintf_plus(stderr, "%s exiting\n", argv[0]);
2220
2211
  }
2221
2212
  
2222
2213
  /* Cleanup things */
2238
2229
  if(gpgme_initialized){
2239
2230
    gpgme_release(mc.ctx);
2240
2231
  }
2241
 
 
 
2232
  
2242
2233
  /* Cleans up the circular linked list of Mandos servers the client
2243
2234
     has seen */
2244
2235
  if(mc.current_server != NULL){
2250
2241
    }
2251
2242
  }
2252
2243
  
 
2244
  /* Run network hooks */
 
2245
  run_network_hooks("stop", interface, delay);
 
2246
  
2253
2247
  /* Re-raise priviliges */
2254
2248
  {
2255
2249
    errno = 0;
2257
2251
    if(ret == -1){
2258
2252
      perror_plus("seteuid");
2259
2253
    }
2260
 
    /* Run network hooks */
2261
 
    if(not run_network_hooks("stop", interface, delay)){
2262
 
      goto end;
2263
 
    }
2264
2254
    
2265
2255
    /* Take down the network interface */
2266
2256
    if(take_down_interface and geteuid() == 0){
2305
2295
        }
2306
2296
        ret = remove(fullname);
2307
2297
        if(ret == -1){
2308
 
          fprintf(stderr, "Mandos plugin mandos-client: "
2309
 
                  "remove(\"%s\"): %s\n", fullname, strerror(errno));
 
2298
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
2299
                       strerror(errno));
2310
2300
        }
2311
2301
        free(fullname);
2312
2302
      }