/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-02-21 21:27:14 UTC
  • mfrom: (237.6.1 nmu)
  • mto: (237.4.31 release)
  • mto: This revision was merged to the branch mainline in revision 560.
  • Revision ID: teddy@recompile.se-20120221212714-40iub281d4yvrk9r
Tags: version-1.5.3-1.1
Merge NMU change.

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
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)))
177
181
int fprintf_plus(FILE *stream, const char *format, ...){
178
182
  va_list ap;
179
183
  va_start (ap, format);
180
184
  
181
 
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name));
 
185
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
 
186
                             program_invocation_short_name));
182
187
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
183
188
}
184
189
 
200
205
}
201
206
 
202
207
/* Add server to set of servers to retry periodically */
203
 
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
204
 
               int af){
 
208
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
209
                int af){
205
210
  int ret;
206
211
  server *new_server = malloc(sizeof(server));
207
212
  if(new_server == NULL){
208
213
    perror_plus("malloc");
209
 
    return -1;
 
214
    return false;
210
215
  }
211
216
  *new_server = (server){ .ip = strdup(ip),
212
217
                          .port = port,
214
219
                          .af = af };
215
220
  if(new_server->ip == NULL){
216
221
    perror_plus("strdup");
217
 
    return -1;
 
222
    return false;
218
223
  }
219
224
  /* Special case of first server */
220
225
  if (mc.current_server == NULL){
231
236
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
232
237
  if(ret == -1){
233
238
    perror_plus("clock_gettime");
234
 
    return -1;
 
239
    return false;
235
240
  }
236
 
  return 0;
 
241
  return true;
237
242
}
238
243
 
239
244
/* 
261
266
    
262
267
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
263
268
    if(rc != GPG_ERR_NO_ERROR){
264
 
      fprintf(stderr, "Mandos plugin mandos-client: "
265
 
              "bad gpgme_data_new_from_fd: %s: %s\n",
266
 
              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));
267
271
      return false;
268
272
    }
269
273
    
270
274
    rc = gpgme_op_import(mc.ctx, pgp_data);
271
275
    if(rc != GPG_ERR_NO_ERROR){
272
 
      fprintf(stderr, "Mandos plugin mandos-client: "
273
 
              "bad gpgme_op_import: %s: %s\n",
274
 
              gpgme_strsource(rc), gpgme_strerror(rc));
 
276
      fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
 
277
                   gpgme_strsource(rc), gpgme_strerror(rc));
275
278
      return false;
276
279
    }
277
280
    
284
287
  }
285
288
  
286
289
  if(debug){
287
 
    fprintf(stderr, "Mandos plugin mandos-client: "
288
 
            "Initializing GPGME\n");
 
290
    fprintf_plus(stderr, "Initializing GPGME\n");
289
291
  }
290
292
  
291
293
  /* Init GPGME */
292
294
  gpgme_check_version(NULL);
293
295
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
294
296
  if(rc != GPG_ERR_NO_ERROR){
295
 
    fprintf(stderr, "Mandos plugin mandos-client: "
296
 
            "bad gpgme_engine_check_version: %s: %s\n",
297
 
            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));
298
299
    return false;
299
300
  }
300
301
  
301
302
  /* Set GPGME home directory for the OpenPGP engine only */
302
303
  rc = gpgme_get_engine_info(&engine_info);
303
304
  if(rc != GPG_ERR_NO_ERROR){
304
 
    fprintf(stderr, "Mandos plugin mandos-client: "
305
 
            "bad gpgme_get_engine_info: %s: %s\n",
306
 
            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));
307
307
    return false;
308
308
  }
309
309
  while(engine_info != NULL){
315
315
    engine_info = engine_info->next;
316
316
  }
317
317
  if(engine_info == NULL){
318
 
    fprintf(stderr, "Mandos plugin mandos-client: "
319
 
            "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);
320
320
    return false;
321
321
  }
322
322
  
323
323
  /* Create new GPGME "context" */
324
324
  rc = gpgme_new(&(mc.ctx));
325
325
  if(rc != GPG_ERR_NO_ERROR){
326
 
    fprintf(stderr, "Mandos plugin mandos-client: "
327
 
            "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
328
 
            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));
329
329
    return false;
330
330
  }
331
331
  
350
350
  ssize_t plaintext_length = 0;
351
351
  
352
352
  if(debug){
353
 
    fprintf(stderr, "Mandos plugin mandos-client: "
354
 
            "Trying to decrypt OpenPGP data\n");
 
353
    fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
355
354
  }
356
355
  
357
356
  /* Create new GPGME data buffer from memory cryptotext */
358
357
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
359
358
                               0);
360
359
  if(rc != GPG_ERR_NO_ERROR){
361
 
    fprintf(stderr, "Mandos plugin mandos-client: "
362
 
            "bad gpgme_data_new_from_mem: %s: %s\n",
363
 
            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));
364
362
    return -1;
365
363
  }
366
364
  
367
365
  /* Create new empty GPGME data buffer for the plaintext */
368
366
  rc = gpgme_data_new(&dh_plain);
369
367
  if(rc != GPG_ERR_NO_ERROR){
370
 
    fprintf(stderr, "Mandos plugin mandos-client: "
371
 
            "bad gpgme_data_new: %s: %s\n",
372
 
            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));
373
371
    gpgme_data_release(dh_crypto);
374
372
    return -1;
375
373
  }
378
376
     data buffer */
379
377
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
380
378
  if(rc != GPG_ERR_NO_ERROR){
381
 
    fprintf(stderr, "Mandos plugin mandos-client: "
382
 
            "bad gpgme_op_decrypt: %s: %s\n",
383
 
            gpgme_strsource(rc), gpgme_strerror(rc));
 
379
    fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
 
380
                 gpgme_strsource(rc), gpgme_strerror(rc));
384
381
    plaintext_length = -1;
385
382
    if(debug){
386
383
      gpgme_decrypt_result_t result;
387
384
      result = gpgme_op_decrypt_result(mc.ctx);
388
385
      if(result == NULL){
389
 
        fprintf(stderr, "Mandos plugin mandos-client: "
390
 
                "gpgme_op_decrypt_result failed\n");
 
386
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
391
387
      } else {
392
 
        fprintf(stderr, "Mandos plugin mandos-client: "
393
 
                "Unsupported algorithm: %s\n",
394
 
                result->unsupported_algorithm);
395
 
        fprintf(stderr, "Mandos plugin mandos-client: "
396
 
                "Wrong key usage: %u\n",
397
 
                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);
398
392
        if(result->file_name != NULL){
399
 
          fprintf(stderr, "Mandos plugin mandos-client: "
400
 
                  "File name: %s\n", result->file_name);
 
393
          fprintf_plus(stderr, "File name: %s\n", result->file_name);
401
394
        }
402
395
        gpgme_recipient_t recipient;
403
396
        recipient = result->recipients;
404
397
        while(recipient != NULL){
405
 
          fprintf(stderr, "Mandos plugin mandos-client: "
406
 
                  "Public key algorithm: %s\n",
407
 
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
408
 
          fprintf(stderr, "Mandos plugin mandos-client: "
409
 
                  "Key ID: %s\n", recipient->keyid);
410
 
          fprintf(stderr, "Mandos plugin mandos-client: "
411
 
                  "Secret key available: %s\n",
412
 
                  recipient->status == GPG_ERR_NO_SECKEY
413
 
                  ? "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");
414
405
          recipient = recipient->next;
415
406
        }
416
407
      }
419
410
  }
420
411
  
421
412
  if(debug){
422
 
    fprintf(stderr, "Mandos plugin mandos-client: "
423
 
            "Decryption of OpenPGP data succeeded\n");
 
413
    fprintf_plus(stderr, "Decryption of OpenPGP data succeeded\n");
424
414
  }
425
415
  
426
416
  /* Seek back to the beginning of the GPGME plaintext data buffer */
457
447
  }
458
448
  
459
449
  if(debug){
460
 
    fprintf(stderr, "Mandos plugin mandos-client: "
461
 
            "Decrypted password is: ");
 
450
    fprintf_plus(stderr, "Decrypted password is: ");
462
451
    for(ssize_t i = 0; i < plaintext_length; i++){
463
452
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
464
453
    }
486
475
/* GnuTLS log function callback */
487
476
static void debuggnutls(__attribute__((unused)) int level,
488
477
                        const char* string){
489
 
  fprintf(stderr, "Mandos plugin mandos-client: GnuTLS: %s", string);
 
478
  fprintf_plus(stderr, "GnuTLS: %s", string);
490
479
}
491
480
 
492
481
static int init_gnutls_global(const char *pubkeyfilename,
494
483
  int ret;
495
484
  
496
485
  if(debug){
497
 
    fprintf(stderr, "Mandos plugin mandos-client: "
498
 
            "Initializing GnuTLS\n");
 
486
    fprintf_plus(stderr, "Initializing GnuTLS\n");
499
487
  }
500
488
  
501
489
  ret = gnutls_global_init();
502
490
  if(ret != GNUTLS_E_SUCCESS){
503
 
    fprintf(stderr, "Mandos plugin mandos-client: "
504
 
            "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
 
491
    fprintf_plus(stderr, "GnuTLS global_init: %s\n",
 
492
                 safer_gnutls_strerror(ret));
505
493
    return -1;
506
494
  }
507
495
  
516
504
  /* OpenPGP credentials */
517
505
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
518
506
  if(ret != GNUTLS_E_SUCCESS){
519
 
    fprintf(stderr, "Mandos plugin mandos-client: "
520
 
            "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
 
507
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
 
508
                 safer_gnutls_strerror(ret));
521
509
    gnutls_global_deinit();
522
510
    return -1;
523
511
  }
524
512
  
525
513
  if(debug){
526
 
    fprintf(stderr, "Mandos plugin mandos-client: "
527
 
            "Attempting to use OpenPGP public key %s and"
528
 
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
529
 
            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);
530
518
  }
531
519
  
532
520
  ret = gnutls_certificate_set_openpgp_key_file
533
521
    (mc.cred, pubkeyfilename, seckeyfilename,
534
522
     GNUTLS_OPENPGP_FMT_BASE64);
535
523
  if(ret != GNUTLS_E_SUCCESS){
536
 
    fprintf(stderr,
537
 
            "Mandos plugin mandos-client: "
538
 
            "Error[%d] while reading the OpenPGP key pair ('%s',"
539
 
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
540
 
    fprintf(stderr, "Mandos plugin mandos-client: "
541
 
            "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));
542
529
    goto globalfail;
543
530
  }
544
531
  
545
532
  /* GnuTLS server initialization */
546
533
  ret = gnutls_dh_params_init(&mc.dh_params);
547
534
  if(ret != GNUTLS_E_SUCCESS){
548
 
    fprintf(stderr, "Mandos plugin mandos-client: "
549
 
            "Error in GnuTLS DH parameter initialization:"
550
 
            " %s\n", safer_gnutls_strerror(ret));
 
535
    fprintf_plus(stderr, "Error in GnuTLS DH parameter"
 
536
                 " initialization: %s\n",
 
537
                 safer_gnutls_strerror(ret));
551
538
    goto globalfail;
552
539
  }
553
540
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
554
541
  if(ret != GNUTLS_E_SUCCESS){
555
 
    fprintf(stderr, "Mandos plugin mandos-client: "
556
 
            "Error in GnuTLS prime generation: %s\n",
557
 
            safer_gnutls_strerror(ret));
 
542
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
 
543
                 safer_gnutls_strerror(ret));
558
544
    goto globalfail;
559
545
  }
560
546
  
580
566
    }
581
567
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
582
568
  if(ret != GNUTLS_E_SUCCESS){
583
 
    fprintf(stderr, "Mandos plugin mandos-client: "
584
 
            "Error in GnuTLS session initialization: %s\n",
585
 
            safer_gnutls_strerror(ret));
 
569
    fprintf_plus(stderr,
 
570
                 "Error in GnuTLS session initialization: %s\n",
 
571
                 safer_gnutls_strerror(ret));
586
572
  }
587
573
  
588
574
  {
595
581
      }
596
582
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
597
583
    if(ret != GNUTLS_E_SUCCESS){
598
 
      fprintf(stderr, "Mandos plugin mandos-client: "
599
 
              "Syntax error at: %s\n", err);
600
 
      fprintf(stderr, "Mandos plugin mandos-client: "
601
 
              "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));
602
587
      gnutls_deinit(*session);
603
588
      return -1;
604
589
    }
613
598
    }
614
599
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
615
600
  if(ret != GNUTLS_E_SUCCESS){
616
 
    fprintf(stderr, "Mandos plugin mandos-client: "
617
 
            "Error setting GnuTLS credentials: %s\n",
618
 
            safer_gnutls_strerror(ret));
 
601
    fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
 
602
                 safer_gnutls_strerror(ret));
619
603
    gnutls_deinit(*session);
620
604
    return -1;
621
605
  }
666
650
    pf = PF_INET;
667
651
    break;
668
652
  default:
669
 
    fprintf(stderr, "Mandos plugin mandos-client: "
670
 
            "Bad address family: %d\n", af);
 
653
    fprintf_plus(stderr, "Bad address family: %d\n", af);
671
654
    errno = EINVAL;
672
655
    return -1;
673
656
  }
678
661
  }
679
662
  
680
663
  if(debug){
681
 
    fprintf(stderr, "Mandos plugin mandos-client: "
682
 
            "Setting up a TCP connection to %s, port %" PRIu16
683
 
            "\n", ip, port);
 
664
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
 
665
                 PRIu16 "\n", ip, port);
684
666
  }
685
667
  
686
668
  tcp_sd = socket(pf, SOCK_STREAM, 0);
712
694
  }
713
695
  if(ret == 0){
714
696
    int e = errno;
715
 
    fprintf(stderr, "Mandos plugin mandos-client: "
716
 
            "Bad address: %s\n", ip);
 
697
    fprintf_plus(stderr, "Bad address: %s\n", ip);
717
698
    errno = e;
718
699
    goto mandos_end;
719
700
  }
726
707
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
727
708
                                -Wunreachable-code*/
728
709
      if(if_index == AVAHI_IF_UNSPEC){
729
 
        fprintf(stderr, "Mandos plugin mandos-client: "
730
 
                "An IPv6 link-local address is incomplete"
731
 
                " without a network interface\n");
 
710
        fprintf_plus(stderr, "An IPv6 link-local address is"
 
711
                     " incomplete without a network interface\n");
732
712
        errno = EINVAL;
733
713
        goto mandos_end;
734
714
      }
752
732
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
753
733
        perror_plus("if_indextoname");
754
734
      } else {
755
 
        fprintf(stderr, "Mandos plugin mandos-client: "
756
 
                "Connection to: %s%%%s, port %" PRIu16 "\n",
757
 
                ip, interface, port);
 
735
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
 
736
                     "\n", ip, interface, port);
758
737
      }
759
738
    } else {
760
 
      fprintf(stderr, "Mandos plugin mandos-client: "
761
 
              "Connection to: %s, port %" PRIu16 "\n", ip, port);
 
739
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
 
740
                   ip, port);
762
741
    }
763
742
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
764
743
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
774
753
      perror_plus("inet_ntop");
775
754
    } else {
776
755
      if(strcmp(addrstr, ip) != 0){
777
 
        fprintf(stderr, "Mandos plugin mandos-client: "
778
 
                "Canonical address form: %s\n", addrstr);
 
756
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
779
757
      }
780
758
    }
781
759
  }
835
813
  }
836
814
  
837
815
  if(debug){
838
 
    fprintf(stderr, "Mandos plugin mandos-client: "
839
 
            "Establishing TLS session with %s\n", ip);
 
816
    fprintf_plus(stderr, "Establishing TLS session with %s\n", ip);
840
817
  }
841
818
  
842
819
  if(quit_now){
862
839
  
863
840
  if(ret != GNUTLS_E_SUCCESS){
864
841
    if(debug){
865
 
      fprintf(stderr, "Mandos plugin mandos-client: "
866
 
              "*** GnuTLS Handshake failed ***\n");
 
842
      fprintf_plus(stderr, "*** GnuTLS Handshake failed ***\n");
867
843
      gnutls_perror(ret);
868
844
    }
869
845
    errno = EPROTO;
873
849
  /* Read OpenPGP packet that contains the wanted password */
874
850
  
875
851
  if(debug){
876
 
    fprintf(stderr, "Mandos plugin mandos-client: "
877
 
            "Retrieving OpenPGP encrypted password from %s\n", ip);
 
852
    fprintf_plus(stderr, "Retrieving OpenPGP encrypted password from"
 
853
                 " %s\n", ip);
878
854
  }
879
855
  
880
856
  while(true){
918
894
          }
919
895
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
920
896
        if(ret < 0){
921
 
          fprintf(stderr, "Mandos plugin mandos-client: "
922
 
                  "*** GnuTLS Re-handshake failed ***\n");
 
897
          fprintf_plus(stderr, "*** GnuTLS Re-handshake failed "
 
898
                       "***\n");
923
899
          gnutls_perror(ret);
924
900
          errno = EPROTO;
925
901
          goto mandos_end;
926
902
        }
927
903
        break;
928
904
      default:
929
 
        fprintf(stderr, "Mandos plugin mandos-client: "
930
 
                "Unknown error while reading data from"
931
 
                " encrypted session with Mandos server\n");
 
905
        fprintf_plus(stderr, "Unknown error while reading data from"
 
906
                     " encrypted session with Mandos server\n");
932
907
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
933
908
        errno = EIO;
934
909
        goto mandos_end;
939
914
  }
940
915
  
941
916
  if(debug){
942
 
    fprintf(stderr, "Mandos plugin mandos-client: "
943
 
            "Closing TLS session\n");
 
917
    fprintf_plus(stderr, "Closing TLS session\n");
944
918
  }
945
919
  
946
920
  if(quit_now){
975
949
        if(ret == 0 and ferror(stdout)){
976
950
          int e = errno;
977
951
          if(debug){
978
 
            fprintf(stderr, "Mandos plugin mandos-client: "
979
 
                    "Error writing encrypted data: %s\n",
980
 
                    strerror(errno));
 
952
            fprintf_plus(stderr, "Error writing encrypted data: %s\n",
 
953
                         strerror(errno));
981
954
          }
982
955
          errno = e;
983
956
          goto mandos_end;
1040
1013
  switch(event){
1041
1014
  default:
1042
1015
  case AVAHI_RESOLVER_FAILURE:
1043
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1044
 
            "(Avahi Resolver) Failed to resolve service '%s'"
1045
 
            " of type '%s' in domain '%s': %s\n", name, type, domain,
1046
 
            avahi_strerror(avahi_server_errno(mc.server)));
 
1016
    fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
 
1017
                 "'%s' of type '%s' in domain '%s': %s\n", name, type,
 
1018
                 domain,
 
1019
                 avahi_strerror(avahi_server_errno(mc.server)));
1047
1020
    break;
1048
1021
    
1049
1022
  case AVAHI_RESOLVER_FOUND:
1051
1024
      char ip[AVAHI_ADDRESS_STR_MAX];
1052
1025
      avahi_address_snprint(ip, sizeof(ip), address);
1053
1026
      if(debug){
1054
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1055
 
                "Mandos server \"%s\" found on %s (%s, %"
1056
 
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1057
 
                ip, (intmax_t)interface, port);
 
1027
        fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
 
1028
                     PRIdMAX ") on port %" PRIu16 "\n", name,
 
1029
                     host_name, ip, (intmax_t)interface, port);
1058
1030
      }
1059
1031
      int ret = start_mandos_communication(ip, port, interface,
1060
1032
                                           avahi_proto_to_af(proto));
1061
1033
      if(ret == 0){
1062
1034
        avahi_simple_poll_quit(mc.simple_poll);
1063
1035
      } else {
1064
 
        ret = add_server(ip, port, interface,
1065
 
                         avahi_proto_to_af(proto));
 
1036
        if(not add_server(ip, port, interface,
 
1037
                          avahi_proto_to_af(proto))){
 
1038
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
 
1039
                       " list\n", name);
 
1040
        }
1066
1041
      }
1067
1042
    }
1068
1043
  }
1092
1067
  default:
1093
1068
  case AVAHI_BROWSER_FAILURE:
1094
1069
    
1095
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1096
 
            "(Avahi browser) %s\n",
1097
 
            avahi_strerror(avahi_server_errno(mc.server)));
 
1070
    fprintf_plus(stderr, "(Avahi browser) %s\n",
 
1071
                 avahi_strerror(avahi_server_errno(mc.server)));
1098
1072
    avahi_simple_poll_quit(mc.simple_poll);
1099
1073
    return;
1100
1074
    
1107
1081
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1108
1082
                                    name, type, domain, protocol, 0,
1109
1083
                                    resolve_callback, NULL) == NULL)
1110
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1111
 
              "Avahi: Failed to resolve service '%s': %s\n",
1112
 
              name, avahi_strerror(avahi_server_errno(mc.server)));
 
1084
      fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
 
1085
                   " %s\n", name,
 
1086
                   avahi_strerror(avahi_server_errno(mc.server)));
1113
1087
    break;
1114
1088
    
1115
1089
  case AVAHI_BROWSER_REMOVE:
1118
1092
  case AVAHI_BROWSER_ALL_FOR_NOW:
1119
1093
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1120
1094
    if(debug){
1121
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1122
 
              "No Mandos server found, still searching...\n");
 
1095
      fprintf_plus(stderr, "No Mandos server found, still"
 
1096
                   " searching...\n");
1123
1097
    }
1124
1098
    break;
1125
1099
  }
1164
1138
  /* Reject the loopback device */
1165
1139
  if(ifr->ifr_flags & IFF_LOOPBACK){
1166
1140
    if(debug){
1167
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1168
 
              "Rejecting loopback interface \"%s\"\n", ifname);
 
1141
      fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
 
1142
                   ifname);
1169
1143
    }
1170
1144
    return false;
1171
1145
  }
1172
1146
  /* Accept point-to-point devices only if connect_to is specified */
1173
1147
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1174
1148
    if(debug){
1175
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1176
 
              "Accepting point-to-point interface \"%s\"\n", ifname);
 
1149
      fprintf_plus(stderr, "Accepting point-to-point interface"
 
1150
                   " \"%s\"\n", ifname);
1177
1151
    }
1178
1152
    return true;
1179
1153
  }
1180
1154
  /* Otherwise, reject non-broadcast-capable devices */
1181
1155
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1182
1156
    if(debug){
1183
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1184
 
              "Rejecting non-broadcast interface \"%s\"\n", ifname);
 
1157
      fprintf_plus(stderr, "Rejecting non-broadcast interface"
 
1158
                   " \"%s\"\n", ifname);
1185
1159
    }
1186
1160
    return false;
1187
1161
  }
1188
1162
  /* Reject non-ARP interfaces (including dummy interfaces) */
1189
1163
  if(ifr->ifr_flags & IFF_NOARP){
1190
1164
    if(debug){
1191
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1192
 
              "Rejecting non-ARP interface \"%s\"\n", ifname);
 
1165
      fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
 
1166
                   ifname);
1193
1167
    }
1194
1168
    return false;
1195
1169
  }
1196
1170
  
1197
1171
  /* Accept this device */
1198
1172
  if(debug){
1199
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1200
 
            "Interface \"%s\" is good\n", ifname);
 
1173
    fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1201
1174
  }
1202
1175
  return true;
1203
1176
}
1215
1188
  struct ifreq ifr;
1216
1189
  if(not get_flags(if_entry->d_name, &ifr)){
1217
1190
    if(debug){
1218
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1219
 
              "Failed to get flags for interface \"%s\"\n",
1220
 
              if_entry->d_name);
 
1191
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1192
                   "\"%s\"\n", if_entry->d_name);
1221
1193
    }
1222
1194
    return 0;
1223
1195
  }
1241
1213
  struct ifreq ifr;
1242
1214
  if(not get_flags(if_entry->d_name, &ifr)){
1243
1215
    if(debug){
1244
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1245
 
              "Failed to get flags for interface \"%s\"\n",
1246
 
              if_entry->d_name);
 
1216
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1217
                   "\"%s\"\n", if_entry->d_name);
1247
1218
    }
1248
1219
    return 0;
1249
1220
  }
1251
1222
  /* Reject down interfaces */
1252
1223
  if(not (ifr.ifr_flags & IFF_UP)){
1253
1224
    if(debug){
1254
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1255
 
              "Rejecting down interface \"%s\"\n",
1256
 
              if_entry->d_name);
 
1225
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
 
1226
                   if_entry->d_name);
1257
1227
    }
1258
1228
    return 0;
1259
1229
  }
1261
1231
  /* Reject non-running interfaces */
1262
1232
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1263
1233
    if(debug){
1264
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1265
 
              "Rejecting non-running interface \"%s\"\n",
1266
 
              if_entry->d_name);
 
1234
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
 
1235
                   if_entry->d_name);
1267
1236
    }
1268
1237
    return 0;
1269
1238
  }
1303
1272
  if((direntry->d_name)[sret] != '\0'){
1304
1273
    /* Contains non-allowed characters */
1305
1274
    if(debug){
1306
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1307
 
              "Ignoring hook \"%s\" with bad name\n",
1308
 
              direntry->d_name);
 
1275
      fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
 
1276
                   direntry->d_name);
1309
1277
    }
1310
1278
    return 0;
1311
1279
  }
1327
1295
  if(not (S_ISREG(st.st_mode))){
1328
1296
    /* Not a regular file */
1329
1297
    if(debug){
1330
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1331
 
              "Ignoring hook \"%s\" - not a file\n",
1332
 
              direntry->d_name);
 
1298
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
 
1299
                   direntry->d_name);
1333
1300
    }
1334
1301
    return 0;
1335
1302
  }
1336
1303
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1337
1304
    /* Not executable */
1338
1305
    if(debug){
1339
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1340
 
              "Ignoring hook \"%s\" - not executable\n",
1341
 
              direntry->d_name);
 
1306
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
 
1307
                   direntry->d_name);
1342
1308
    }
1343
1309
    return 0;
1344
1310
  }
 
1311
  if(debug){
 
1312
    fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
 
1313
                 direntry->d_name);
 
1314
  }
1345
1315
  return 1;
1346
1316
}
1347
1317
 
1354
1324
  while(true){
1355
1325
    if(mc.current_server == NULL){
1356
1326
      if (debug){
1357
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1358
 
                "Wait until first server is found. No timeout!\n");
 
1327
        fprintf_plus(stderr, "Wait until first server is found."
 
1328
                     " No timeout!\n");
1359
1329
      }
1360
1330
      ret = avahi_simple_poll_iterate(s, -1);
1361
1331
    } else {
1362
1332
      if (debug){
1363
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1364
 
                "Check current_server if we should run it,"
1365
 
                " or wait\n");
 
1333
        fprintf_plus(stderr, "Check current_server if we should run"
 
1334
                     " it, or wait\n");
1366
1335
      }
1367
1336
      /* the current time */
1368
1337
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1384
1353
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1385
1354
      
1386
1355
      if (debug){
1387
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1388
 
                "Blocking for %" PRIdMAX " ms\n", block_time);
 
1356
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
 
1357
                     block_time);
1389
1358
      }
1390
1359
      
1391
1360
      if(block_time <= 0){
1418
1387
  }
1419
1388
}
1420
1389
 
 
1390
bool run_network_hooks(const char *mode, const char *interface,
 
1391
                       const float delay){
 
1392
  struct dirent **direntries;
 
1393
  struct dirent *direntry;
 
1394
  int ret;
 
1395
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1396
                         alphasort);
 
1397
  if(numhooks == -1){
 
1398
    perror_plus("scandir");
 
1399
  } else {
 
1400
    int devnull = open("/dev/null", O_RDONLY);
 
1401
    for(int i = 0; i < numhooks; i++){
 
1402
      direntry = direntries[i];
 
1403
      char *fullname = NULL;
 
1404
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1405
      if(ret < 0){
 
1406
        perror_plus("asprintf");
 
1407
        continue;
 
1408
      }
 
1409
      if(debug){
 
1410
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1411
                     direntry->d_name);
 
1412
      }
 
1413
      pid_t hook_pid = fork();
 
1414
      if(hook_pid == 0){
 
1415
        /* Child */
 
1416
        /* Raise privileges */
 
1417
        errno = 0;
 
1418
        ret = seteuid(0);
 
1419
        if(ret == -1){
 
1420
          perror_plus("seteuid");
 
1421
        }
 
1422
        /* Raise privileges even more */
 
1423
        errno = 0;
 
1424
        ret = setuid(0);
 
1425
        if(ret == -1){
 
1426
          perror_plus("setuid");
 
1427
        }
 
1428
        /* Set group */
 
1429
        errno = 0;
 
1430
        ret = setgid(0);
 
1431
        if(ret == -1){
 
1432
          perror_plus("setgid");
 
1433
        }
 
1434
        /* Reset supplementary groups */
 
1435
        errno = 0;
 
1436
        ret = setgroups(0, NULL);
 
1437
        if(ret == -1){
 
1438
          perror_plus("setgroups");
 
1439
        }
 
1440
        dup2(devnull, STDIN_FILENO);
 
1441
        close(devnull);
 
1442
        dup2(STDERR_FILENO, STDOUT_FILENO);
 
1443
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1444
        if(ret == -1){
 
1445
          perror_plus("setenv");
 
1446
          _exit(EX_OSERR);
 
1447
        }
 
1448
        ret = setenv("DEVICE", interface, 1);
 
1449
        if(ret == -1){
 
1450
          perror_plus("setenv");
 
1451
          _exit(EX_OSERR);
 
1452
        }
 
1453
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1454
        if(ret == -1){
 
1455
          perror_plus("setenv");
 
1456
          _exit(EX_OSERR);
 
1457
        }
 
1458
        ret = setenv("MODE", mode, 1);
 
1459
        if(ret == -1){
 
1460
          perror_plus("setenv");
 
1461
          _exit(EX_OSERR);
 
1462
        }
 
1463
        char *delaystring;
 
1464
        ret = asprintf(&delaystring, "%f", delay);
 
1465
        if(ret == -1){
 
1466
          perror_plus("asprintf");
 
1467
          _exit(EX_OSERR);
 
1468
        }
 
1469
        ret = setenv("DELAY", delaystring, 1);
 
1470
        if(ret == -1){
 
1471
          free(delaystring);
 
1472
          perror_plus("setenv");
 
1473
          _exit(EX_OSERR);
 
1474
        }
 
1475
        free(delaystring);
 
1476
        if(connect_to != NULL){
 
1477
          ret = setenv("CONNECT", connect_to, 1);
 
1478
          if(ret == -1){
 
1479
            perror_plus("setenv");
 
1480
            _exit(EX_OSERR);
 
1481
          }
 
1482
        }
 
1483
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1484
          perror_plus("execl");
 
1485
          _exit(EXIT_FAILURE);
 
1486
        }
 
1487
      } else {
 
1488
        int status;
 
1489
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1490
          perror_plus("waitpid");
 
1491
          free(fullname);
 
1492
          continue;
 
1493
        }
 
1494
        if(WIFEXITED(status)){
 
1495
          if(WEXITSTATUS(status) != 0){
 
1496
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1497
                         " with status %d\n", direntry->d_name,
 
1498
                         WEXITSTATUS(status));
 
1499
            free(fullname);
 
1500
            continue;
 
1501
          }
 
1502
        } else if(WIFSIGNALED(status)){
 
1503
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1504
                       " signal %d\n", direntry->d_name,
 
1505
                       WTERMSIG(status));
 
1506
          free(fullname);
 
1507
          continue;
 
1508
        } else {
 
1509
          fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1510
                       " crashed\n", direntry->d_name);
 
1511
          free(fullname);
 
1512
          continue;
 
1513
        }
 
1514
      }
 
1515
      free(fullname);
 
1516
      if(debug){
 
1517
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1518
                     direntry->d_name);
 
1519
      }
 
1520
    }
 
1521
    close(devnull);
 
1522
  }
 
1523
  return true;
 
1524
}
 
1525
 
1421
1526
int main(int argc, char *argv[]){
1422
1527
  AvahiSServiceBrowser *sb = NULL;
1423
1528
  int error;
1503
1608
        .group = 2 },
1504
1609
      { .name = "retry", .key = 132,
1505
1610
        .arg = "SECONDS",
1506
 
        .doc = "Retry interval used when denied by the mandos server",
 
1611
        .doc = "Retry interval used when denied by the Mandos server",
1507
1612
        .group = 2 },
1508
1613
      { .name = "network-hook-dir", .key = 133,
1509
1614
        .arg = "DIR",
1581
1686
        argp_state_help(state, state->out_stream,
1582
1687
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1583
1688
      case 'V':                 /* --version */
1584
 
        fprintf(state->out_stream, "Mandos plugin mandos-client: ");
1585
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
 
1689
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1586
1690
        exit(argp_err_exit_status);
1587
1691
        break;
1588
1692
      default:
1615
1719
  {
1616
1720
    /* Work around Debian bug #633582:
1617
1721
       <http://bugs.debian.org/633582> */
1618
 
    struct stat st;
1619
1722
    
1620
1723
    /* Re-raise priviliges */
1621
1724
    errno = 0;
1622
1725
    ret = seteuid(0);
1623
1726
    if(ret == -1){
1624
1727
      perror_plus("seteuid");
1625
 
    }
1626
 
    
1627
 
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1628
 
      int seckey_fd = open(seckey, O_RDONLY);
1629
 
      if(seckey_fd == -1){
1630
 
        perror_plus("open");
1631
 
      } else {
1632
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1633
 
        if(ret == -1){
1634
 
          perror_plus("fstat");
1635
 
        } else {
1636
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1637
 
            ret = fchown(seckey_fd, uid, gid);
1638
 
            if(ret == -1){
1639
 
              perror_plus("fchown");
1640
 
            }
1641
 
          }
1642
 
        }
1643
 
        TEMP_FAILURE_RETRY(close(seckey_fd));
1644
 
      }
1645
 
    }
1646
 
    
1647
 
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1648
 
      int pubkey_fd = open(pubkey, O_RDONLY);
1649
 
      if(pubkey_fd == -1){
1650
 
        perror_plus("open");
1651
 
      } else {
1652
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1653
 
        if(ret == -1){
1654
 
          perror_plus("fstat");
1655
 
        } else {
1656
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1657
 
            ret = fchown(pubkey_fd, uid, gid);
1658
 
            if(ret == -1){
1659
 
              perror_plus("fchown");
1660
 
            }
1661
 
          }
1662
 
        }
1663
 
        TEMP_FAILURE_RETRY(close(pubkey_fd));
1664
 
      }
1665
 
    }
1666
 
    
1667
 
    /* Lower privileges */
1668
 
    errno = 0;
1669
 
    ret = seteuid(uid);
1670
 
    if(ret == -1){
1671
 
      perror_plus("seteuid");
1672
 
    }
1673
 
  }
1674
 
  
1675
 
  /* Find network hooks and run them */
1676
 
  {
1677
 
    struct dirent **direntries;
1678
 
    struct dirent *direntry;
1679
 
    int numhooks = scandir(hookdir, &direntries, runnable_hook,
1680
 
                           alphasort);
1681
 
    if(numhooks == -1){
1682
 
      perror_plus("scandir");
1683
1728
    } else {
1684
 
      int devnull = open("/dev/null", O_RDONLY);
1685
 
      for(int i = 0; i < numhooks; i++){
1686
 
        direntry = direntries[0];
1687
 
        char *fullname = NULL;
1688
 
        ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1689
 
        if(ret < 0){
1690
 
          perror_plus("asprintf");
1691
 
          continue;
1692
 
        }
1693
 
        pid_t hook_pid = fork();
1694
 
        if(hook_pid == 0){
1695
 
          /* Child */
1696
 
          dup2(devnull, STDIN_FILENO);
1697
 
          close(devnull);
1698
 
          dup2(STDERR_FILENO, STDOUT_FILENO);
1699
 
          ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1700
 
          if(ret == -1){
1701
 
            perror_plus("setenv");
1702
 
            exit(1);
1703
 
          }
1704
 
          ret = setenv("DEVICE", interface, 1);
1705
 
          if(ret == -1){
1706
 
            perror_plus("setenv");
1707
 
            exit(1);
1708
 
          }
1709
 
          ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1710
 
          if(ret == -1){
1711
 
            perror_plus("setenv");
1712
 
            exit(1);
1713
 
          }
1714
 
          ret = setenv("MODE", "start", 1);
1715
 
          if(ret == -1){
1716
 
            perror_plus("setenv");
1717
 
            exit(1);
1718
 
          }
1719
 
          char *delaystring;
1720
 
          ret = asprintf(&delaystring, "%f", delay);
1721
 
          if(ret == -1){
1722
 
            perror_plus("asprintf");
1723
 
            exit(1);
1724
 
          }
1725
 
          ret = setenv("DELAY", delaystring, 1);
1726
 
          if(ret == -1){
1727
 
            free(delaystring);
1728
 
            perror_plus("setenv");
1729
 
            exit(1);
1730
 
          }
1731
 
          free(delaystring);
1732
 
          ret = execl(fullname, direntry->d_name, "start", NULL);
1733
 
          perror_plus("execl");
1734
 
        } else {
1735
 
          int status;
1736
 
          if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1737
 
            perror_plus("waitpid");
1738
 
            free(fullname);
1739
 
            continue;
1740
 
          }
1741
 
          if(WIFEXITED(status)){
1742
 
            if(WEXITSTATUS(status) != 0){
1743
 
              fprintf(stderr, "Mandos plugin mandos-client: "
1744
 
                      "Warning: network hook \"%s\" exited"
1745
 
                      " with status %d\n", direntry->d_name,
1746
 
                      WEXITSTATUS(status));
1747
 
              free(fullname);
1748
 
              continue;
1749
 
            }
1750
 
          } else if(WIFSIGNALED(status)){
1751
 
            fprintf(stderr, "Mandos plugin mandos-client: "
1752
 
                    "Warning: network hook \"%s\" died by"
1753
 
                    " signal %d\n", direntry->d_name,
1754
 
                    WTERMSIG(status));
1755
 
            free(fullname);
1756
 
            continue;
1757
 
          } else {
1758
 
            fprintf(stderr, "Mandos plugin mandos-client: "
1759
 
                    "Warning: network hook \"%s\" crashed\n",
1760
 
                    direntry->d_name);
1761
 
            free(fullname);
1762
 
            continue;
1763
 
          }
1764
 
        }
1765
 
        free(fullname);
1766
 
        if(quit_now){
1767
 
          goto end;
1768
 
        }
1769
 
      }
1770
 
      close(devnull);
 
1729
      struct stat st;
 
1730
      
 
1731
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1732
        int seckey_fd = open(seckey, O_RDONLY);
 
1733
        if(seckey_fd == -1){
 
1734
          perror_plus("open");
 
1735
        } else {
 
1736
          ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1737
          if(ret == -1){
 
1738
            perror_plus("fstat");
 
1739
          } else {
 
1740
            if(S_ISREG(st.st_mode)
 
1741
               and st.st_uid == 0 and st.st_gid == 0){
 
1742
              ret = fchown(seckey_fd, uid, gid);
 
1743
              if(ret == -1){
 
1744
                perror_plus("fchown");
 
1745
              }
 
1746
            }
 
1747
          }
 
1748
          TEMP_FAILURE_RETRY(close(seckey_fd));
 
1749
        }
 
1750
      }
 
1751
    
 
1752
      if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
 
1753
        int pubkey_fd = open(pubkey, O_RDONLY);
 
1754
        if(pubkey_fd == -1){
 
1755
          perror_plus("open");
 
1756
        } else {
 
1757
          ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1758
          if(ret == -1){
 
1759
            perror_plus("fstat");
 
1760
          } else {
 
1761
            if(S_ISREG(st.st_mode)
 
1762
               and st.st_uid == 0 and st.st_gid == 0){
 
1763
              ret = fchown(pubkey_fd, uid, gid);
 
1764
              if(ret == -1){
 
1765
                perror_plus("fchown");
 
1766
              }
 
1767
            }
 
1768
          }
 
1769
          TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1770
        }
 
1771
      }
 
1772
    
 
1773
      /* Lower privileges */
 
1774
      errno = 0;
 
1775
      ret = seteuid(uid);
 
1776
      if(ret == -1){
 
1777
        perror_plus("seteuid");
 
1778
      }
1771
1779
    }
1772
1780
  }
1773
1781
  
 
1782
  /* Run network hooks */
 
1783
  if(not run_network_hooks("start", interface, delay)){
 
1784
    goto end;
 
1785
  }
 
1786
  
1774
1787
  if(not debug){
1775
1788
    avahi_set_log_function(empty_log);
1776
1789
  }
1790
1803
      /* Pick the first interface returned */
1791
1804
      interface = strdup(direntries[0]->d_name);
1792
1805
      if(debug){
1793
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1794
 
                "Using interface \"%s\"\n", interface);
 
1806
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1795
1807
      }
1796
1808
      if(interface == NULL){
1797
1809
        perror_plus("malloc");
1802
1814
      free(direntries);
1803
1815
    } else {
1804
1816
      free(direntries);
1805
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1806
 
              "Could not find a network interface\n");
 
1817
      fprintf_plus(stderr, "Could not find a network interface\n");
1807
1818
      exitcode = EXIT_FAILURE;
1808
1819
      goto end;
1809
1820
    }
1815
1826
  srand((unsigned int) time(NULL));
1816
1827
  mc.simple_poll = avahi_simple_poll_new();
1817
1828
  if(mc.simple_poll == NULL){
1818
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1819
 
            "Avahi: Failed to create simple poll object.\n");
 
1829
    fprintf_plus(stderr,
 
1830
                 "Avahi: Failed to create simple poll object.\n");
1820
1831
    exitcode = EX_UNAVAILABLE;
1821
1832
    goto end;
1822
1833
  }
1888
1899
  if(strcmp(interface, "none") != 0){
1889
1900
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1890
1901
    if(if_index == 0){
1891
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1892
 
              "No such interface: \"%s\"\n", interface);
 
1902
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1893
1903
      exitcode = EX_UNAVAILABLE;
1894
1904
      goto end;
1895
1905
    }
2015
2025
#endif  /* __linux__ */
2016
2026
    /* Lower privileges */
2017
2027
    errno = 0;
2018
 
    if(take_down_interface){
2019
 
      /* Lower privileges */
2020
 
      ret = seteuid(uid);
2021
 
      if(ret == -1){
2022
 
        perror_plus("seteuid");
2023
 
      }
2024
 
    } else {
2025
 
      /* Lower privileges permanently */
2026
 
      ret = setuid(uid);
2027
 
      if(ret == -1){
2028
 
        perror_plus("setuid");
2029
 
      }
 
2028
    /* Lower privileges */
 
2029
    ret = seteuid(uid);
 
2030
    if(ret == -1){
 
2031
      perror_plus("seteuid");
2030
2032
    }
2031
2033
  }
2032
2034
  
2036
2038
  
2037
2039
  ret = init_gnutls_global(pubkey, seckey);
2038
2040
  if(ret == -1){
2039
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2040
 
            "init_gnutls_global failed\n");
 
2041
    fprintf_plus(stderr, "init_gnutls_global failed\n");
2041
2042
    exitcode = EX_UNAVAILABLE;
2042
2043
    goto end;
2043
2044
  } else {
2059
2060
  }
2060
2061
  
2061
2062
  if(not init_gpgme(pubkey, seckey, tempdir)){
2062
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2063
 
            "init_gpgme failed\n");
 
2063
    fprintf_plus(stderr, "init_gpgme failed\n");
2064
2064
    exitcode = EX_UNAVAILABLE;
2065
2065
    goto end;
2066
2066
  } else {
2076
2076
    /* (Mainly meant for debugging) */
2077
2077
    char *address = strrchr(connect_to, ':');
2078
2078
    if(address == NULL){
2079
 
      fprintf(stderr, "Mandos plugin mandos-client: "
2080
 
              "No colon in address\n");
 
2079
      fprintf_plus(stderr, "No colon in address\n");
2081
2080
      exitcode = EX_USAGE;
2082
2081
      goto end;
2083
2082
    }
2091
2090
    tmpmax = strtoimax(address+1, &tmp, 10);
2092
2091
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2093
2092
       or tmpmax != (uint16_t)tmpmax){
2094
 
      fprintf(stderr, "Mandos plugin mandos-client: "
2095
 
              "Bad port number\n");
 
2093
      fprintf_plus(stderr, "Bad port number\n");
2096
2094
      exitcode = EX_USAGE;
2097
2095
      goto end;
2098
2096
    }
2128
2126
        break;
2129
2127
      }
2130
2128
      if(debug){
2131
 
        fprintf(stderr, "Mandos plugin mandos-client: "
2132
 
                "Retrying in %d seconds\n", (int)retry_interval);
 
2129
        fprintf_plus(stderr, "Retrying in %d seconds\n",
 
2130
                     (int)retry_interval);
2133
2131
      }
2134
2132
      sleep((int)retry_interval);
2135
2133
    }
2165
2163
  
2166
2164
  /* Check if creating the Avahi server object succeeded */
2167
2165
  if(mc.server == NULL){
2168
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2169
 
            "Failed to create Avahi server: %s\n",
2170
 
            avahi_strerror(error));
 
2166
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
 
2167
                 avahi_strerror(error));
2171
2168
    exitcode = EX_UNAVAILABLE;
2172
2169
    goto end;
2173
2170
  }
2181
2178
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2182
2179
                                   NULL, 0, browse_callback, NULL);
2183
2180
  if(sb == NULL){
2184
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2185
 
            "Failed to create service browser: %s\n",
2186
 
            avahi_strerror(avahi_server_errno(mc.server)));
 
2181
    fprintf_plus(stderr, "Failed to create service browser: %s\n",
 
2182
                 avahi_strerror(avahi_server_errno(mc.server)));
2187
2183
    exitcode = EX_UNAVAILABLE;
2188
2184
    goto end;
2189
2185
  }
2195
2191
  /* Run the main loop */
2196
2192
  
2197
2193
  if(debug){
2198
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2199
 
            "Starting Avahi loop search\n");
 
2194
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2200
2195
  }
2201
2196
 
2202
2197
  ret = avahi_loop_with_timeout(mc.simple_poll,
2203
2198
                                (int)(retry_interval * 1000));
2204
2199
  if(debug){
2205
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2206
 
            "avahi_loop_with_timeout exited %s\n",
2207
 
            (ret == 0) ? "successfully" : "with error");
 
2200
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
 
2201
                 (ret == 0) ? "successfully" : "with error");
2208
2202
  }
2209
2203
  
2210
2204
 end:
2211
2205
  
2212
2206
  if(debug){
2213
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2214
 
            "%s exiting\n", argv[0]);
 
2207
    fprintf_plus(stderr, "%s exiting\n", argv[0]);
2215
2208
  }
2216
2209
  
2217
2210
  /* Cleanup things */
2233
2226
  if(gpgme_initialized){
2234
2227
    gpgme_release(mc.ctx);
2235
2228
  }
2236
 
 
 
2229
  
2237
2230
  /* Cleans up the circular linked list of Mandos servers the client
2238
2231
     has seen */
2239
2232
  if(mc.current_server != NULL){
2245
2238
    }
2246
2239
  }
2247
2240
  
2248
 
  /* XXX run network hooks "stop" here  */
 
2241
  /* Run network hooks */
 
2242
  run_network_hooks("stop", interface, delay);
2249
2243
  
2250
 
  /* Take down the network interface */
2251
 
  if(take_down_interface){
2252
 
    /* Re-raise priviliges */
 
2244
  /* Re-raise priviliges */
 
2245
  {
2253
2246
    errno = 0;
2254
2247
    ret = seteuid(0);
2255
2248
    if(ret == -1){
2256
2249
      perror_plus("seteuid");
2257
2250
    }
2258
 
    if(geteuid() == 0){
 
2251
    
 
2252
    /* Take down the network interface */
 
2253
    if(take_down_interface and geteuid() == 0){
2259
2254
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2260
2255
      if(ret == -1){
2261
2256
        perror_plus("ioctl SIOCGIFFLAGS");
2270
2265
      if(ret == -1){
2271
2266
        perror_plus("close");
2272
2267
      }
2273
 
      /* Lower privileges permanently */
2274
 
      errno = 0;
2275
 
      ret = setuid(uid);
2276
 
      if(ret == -1){
2277
 
        perror_plus("setuid");
2278
 
      }
2279
2268
    }
2280
2269
  }
 
2270
  /* Lower privileges permanently */
 
2271
  errno = 0;
 
2272
  ret = setuid(uid);
 
2273
  if(ret == -1){
 
2274
    perror_plus("setuid");
 
2275
  }
2281
2276
  
2282
2277
  /* Removes the GPGME temp directory and all files inside */
2283
2278
  if(tempdir_created){
2297
2292
        }
2298
2293
        ret = remove(fullname);
2299
2294
        if(ret == -1){
2300
 
          fprintf(stderr, "Mandos plugin mandos-client: "
2301
 
                  "remove(\"%s\"): %s\n", fullname, strerror(errno));
 
2295
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
2296
                       strerror(errno));
2302
2297
        }
2303
2298
        free(fullname);
2304
2299
      }