/mandos/release

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

« back to all changes in this revision

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

Merge from Teddy

Show diffs side-by-side

added added

removed removed

Lines of Context:
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() */
174
175
  perror(print_text);
175
176
}
176
177
 
 
178
int fprintf_plus(FILE *stream, const char *format, ...){
 
179
  va_list ap;
 
180
  va_start (ap, format);
 
181
  
 
182
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
 
183
                             program_invocation_short_name));
 
184
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
 
185
}
 
186
 
177
187
/*
178
188
 * Make additional room in "buffer" for at least BUFFER_SIZE more
179
189
 * bytes. "buffer_capacity" is how much is currently allocated,
192
202
}
193
203
 
194
204
/* 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){
 
205
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
206
                int af){
197
207
  int ret;
198
208
  server *new_server = malloc(sizeof(server));
199
209
  if(new_server == NULL){
200
210
    perror_plus("malloc");
201
 
    return -1;
 
211
    return false;
202
212
  }
203
213
  *new_server = (server){ .ip = strdup(ip),
204
214
                          .port = port,
206
216
                          .af = af };
207
217
  if(new_server->ip == NULL){
208
218
    perror_plus("strdup");
209
 
    return -1;
 
219
    return false;
210
220
  }
211
221
  /* Special case of first server */
212
222
  if (mc.current_server == NULL){
223
233
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
224
234
  if(ret == -1){
225
235
    perror_plus("clock_gettime");
226
 
    return -1;
 
236
    return false;
227
237
  }
228
 
  return 0;
 
238
  return true;
229
239
}
230
240
 
231
241
/* 
253
263
    
254
264
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
255
265
    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));
 
266
      fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
 
267
                   gpgme_strsource(rc), gpgme_strerror(rc));
259
268
      return false;
260
269
    }
261
270
    
262
271
    rc = gpgme_op_import(mc.ctx, pgp_data);
263
272
    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));
 
273
      fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
 
274
                   gpgme_strsource(rc), gpgme_strerror(rc));
267
275
      return false;
268
276
    }
269
277
    
276
284
  }
277
285
  
278
286
  if(debug){
279
 
    fprintf(stderr, "Mandos plugin mandos-client: "
280
 
            "Initializing GPGME\n");
 
287
    fprintf_plus(stderr, "Initializing GPGME\n");
281
288
  }
282
289
  
283
290
  /* Init GPGME */
284
291
  gpgme_check_version(NULL);
285
292
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
286
293
  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));
 
294
    fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
 
295
                 gpgme_strsource(rc), gpgme_strerror(rc));
290
296
    return false;
291
297
  }
292
298
  
293
299
  /* Set GPGME home directory for the OpenPGP engine only */
294
300
  rc = gpgme_get_engine_info(&engine_info);
295
301
  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));
 
302
    fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
 
303
                 gpgme_strsource(rc), gpgme_strerror(rc));
299
304
    return false;
300
305
  }
301
306
  while(engine_info != NULL){
307
312
    engine_info = engine_info->next;
308
313
  }
309
314
  if(engine_info == NULL){
310
 
    fprintf(stderr, "Mandos plugin mandos-client: "
311
 
            "Could not set GPGME home dir to %s\n", tempdir);
 
315
    fprintf_plus(stderr, "Could not set GPGME home dir to %s\n",
 
316
                 tempdir);
312
317
    return false;
313
318
  }
314
319
  
315
320
  /* Create new GPGME "context" */
316
321
  rc = gpgme_new(&(mc.ctx));
317
322
  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));
 
323
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
 
324
                 "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
 
325
                 gpgme_strerror(rc));
321
326
    return false;
322
327
  }
323
328
  
342
347
  ssize_t plaintext_length = 0;
343
348
  
344
349
  if(debug){
345
 
    fprintf(stderr, "Mandos plugin mandos-client: "
346
 
            "Trying to decrypt OpenPGP data\n");
 
350
    fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
347
351
  }
348
352
  
349
353
  /* Create new GPGME data buffer from memory cryptotext */
350
354
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
351
355
                               0);
352
356
  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));
 
357
    fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
 
358
                 gpgme_strsource(rc), gpgme_strerror(rc));
356
359
    return -1;
357
360
  }
358
361
  
359
362
  /* Create new empty GPGME data buffer for the plaintext */
360
363
  rc = gpgme_data_new(&dh_plain);
361
364
  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));
 
365
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
 
366
                 "bad gpgme_data_new: %s: %s\n",
 
367
                 gpgme_strsource(rc), gpgme_strerror(rc));
365
368
    gpgme_data_release(dh_crypto);
366
369
    return -1;
367
370
  }
370
373
     data buffer */
371
374
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
372
375
  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));
 
376
    fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
 
377
                 gpgme_strsource(rc), gpgme_strerror(rc));
376
378
    plaintext_length = -1;
377
379
    if(debug){
378
380
      gpgme_decrypt_result_t result;
379
381
      result = gpgme_op_decrypt_result(mc.ctx);
380
382
      if(result == NULL){
381
 
        fprintf(stderr, "Mandos plugin mandos-client: "
382
 
                "gpgme_op_decrypt_result failed\n");
 
383
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
383
384
      } 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);
 
385
        fprintf_plus(stderr, "Unsupported algorithm: %s\n",
 
386
                     result->unsupported_algorithm);
 
387
        fprintf_plus(stderr, "Wrong key usage: %u\n",
 
388
                     result->wrong_key_usage);
390
389
        if(result->file_name != NULL){
391
 
          fprintf(stderr, "Mandos plugin mandos-client: "
392
 
                  "File name: %s\n", result->file_name);
 
390
          fprintf_plus(stderr, "File name: %s\n", result->file_name);
393
391
        }
394
392
        gpgme_recipient_t recipient;
395
393
        recipient = result->recipients;
396
394
        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");
 
395
          fprintf_plus(stderr, "Public key algorithm: %s\n",
 
396
                       gpgme_pubkey_algo_name
 
397
                       (recipient->pubkey_algo));
 
398
          fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
 
399
          fprintf_plus(stderr, "Secret key available: %s\n",
 
400
                       recipient->status == GPG_ERR_NO_SECKEY
 
401
                       ? "No" : "Yes");
406
402
          recipient = recipient->next;
407
403
        }
408
404
      }
411
407
  }
412
408
  
413
409
  if(debug){
414
 
    fprintf(stderr, "Mandos plugin mandos-client: "
415
 
            "Decryption of OpenPGP data succeeded\n");
 
410
    fprintf_plus(stderr, "Decryption of OpenPGP data succeeded\n");
416
411
  }
417
412
  
418
413
  /* Seek back to the beginning of the GPGME plaintext data buffer */
449
444
  }
450
445
  
451
446
  if(debug){
452
 
    fprintf(stderr, "Mandos plugin mandos-client: "
453
 
            "Decrypted password is: ");
 
447
    fprintf_plus(stderr, "Decrypted password is: ");
454
448
    for(ssize_t i = 0; i < plaintext_length; i++){
455
449
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
456
450
    }
478
472
/* GnuTLS log function callback */
479
473
static void debuggnutls(__attribute__((unused)) int level,
480
474
                        const char* string){
481
 
  fprintf(stderr, "Mandos plugin mandos-client: GnuTLS: %s", string);
 
475
  fprintf_plus(stderr, "GnuTLS: %s", string);
482
476
}
483
477
 
484
478
static int init_gnutls_global(const char *pubkeyfilename,
486
480
  int ret;
487
481
  
488
482
  if(debug){
489
 
    fprintf(stderr, "Mandos plugin mandos-client: "
490
 
            "Initializing GnuTLS\n");
 
483
    fprintf_plus(stderr, "Initializing GnuTLS\n");
491
484
  }
492
485
  
493
486
  ret = gnutls_global_init();
494
487
  if(ret != GNUTLS_E_SUCCESS){
495
 
    fprintf(stderr, "Mandos plugin mandos-client: "
496
 
            "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
 
488
    fprintf_plus(stderr, "GnuTLS global_init: %s\n",
 
489
                 safer_gnutls_strerror(ret));
497
490
    return -1;
498
491
  }
499
492
  
508
501
  /* OpenPGP credentials */
509
502
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
510
503
  if(ret != GNUTLS_E_SUCCESS){
511
 
    fprintf(stderr, "Mandos plugin mandos-client: "
512
 
            "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
 
504
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
 
505
                 safer_gnutls_strerror(ret));
513
506
    gnutls_global_deinit();
514
507
    return -1;
515
508
  }
516
509
  
517
510
  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);
 
511
    fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
 
512
                 " secret key %s as GnuTLS credentials\n",
 
513
                 pubkeyfilename,
 
514
                 seckeyfilename);
522
515
  }
523
516
  
524
517
  ret = gnutls_certificate_set_openpgp_key_file
525
518
    (mc.cred, pubkeyfilename, seckeyfilename,
526
519
     GNUTLS_OPENPGP_FMT_BASE64);
527
520
  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));
 
521
    fprintf_plus(stderr,
 
522
                 "Error[%d] while reading the OpenPGP key pair ('%s',"
 
523
                 " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
524
    fprintf_plus(stderr, "The GnuTLS error is: %s\n",
 
525
                 safer_gnutls_strerror(ret));
534
526
    goto globalfail;
535
527
  }
536
528
  
537
529
  /* GnuTLS server initialization */
538
530
  ret = gnutls_dh_params_init(&mc.dh_params);
539
531
  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));
 
532
    fprintf_plus(stderr, "Error in GnuTLS DH parameter"
 
533
                 " initialization: %s\n",
 
534
                 safer_gnutls_strerror(ret));
543
535
    goto globalfail;
544
536
  }
545
537
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
546
538
  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));
 
539
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
 
540
                 safer_gnutls_strerror(ret));
550
541
    goto globalfail;
551
542
  }
552
543
  
572
563
    }
573
564
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
574
565
  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));
 
566
    fprintf_plus(stderr,
 
567
                 "Error in GnuTLS session initialization: %s\n",
 
568
                 safer_gnutls_strerror(ret));
578
569
  }
579
570
  
580
571
  {
587
578
      }
588
579
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
589
580
    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));
 
581
      fprintf_plus(stderr, "Syntax error at: %s\n", err);
 
582
      fprintf_plus(stderr, "GnuTLS error: %s\n",
 
583
                   safer_gnutls_strerror(ret));
594
584
      gnutls_deinit(*session);
595
585
      return -1;
596
586
    }
605
595
    }
606
596
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
607
597
  if(ret != GNUTLS_E_SUCCESS){
608
 
    fprintf(stderr, "Mandos plugin mandos-client: "
609
 
            "Error setting GnuTLS credentials: %s\n",
610
 
            safer_gnutls_strerror(ret));
 
598
    fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
 
599
                 safer_gnutls_strerror(ret));
611
600
    gnutls_deinit(*session);
612
601
    return -1;
613
602
  }
658
647
    pf = PF_INET;
659
648
    break;
660
649
  default:
661
 
    fprintf(stderr, "Mandos plugin mandos-client: "
662
 
            "Bad address family: %d\n", af);
 
650
    fprintf_plus(stderr, "Bad address family: %d\n", af);
663
651
    errno = EINVAL;
664
652
    return -1;
665
653
  }
670
658
  }
671
659
  
672
660
  if(debug){
673
 
    fprintf(stderr, "Mandos plugin mandos-client: "
674
 
            "Setting up a TCP connection to %s, port %" PRIu16
675
 
            "\n", ip, port);
 
661
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
 
662
                 PRIu16 "\n", ip, port);
676
663
  }
677
664
  
678
665
  tcp_sd = socket(pf, SOCK_STREAM, 0);
704
691
  }
705
692
  if(ret == 0){
706
693
    int e = errno;
707
 
    fprintf(stderr, "Mandos plugin mandos-client: "
708
 
            "Bad address: %s\n", ip);
 
694
    fprintf_plus(stderr, "Bad address: %s\n", ip);
709
695
    errno = e;
710
696
    goto mandos_end;
711
697
  }
718
704
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
719
705
                                -Wunreachable-code*/
720
706
      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");
 
707
        fprintf_plus(stderr, "An IPv6 link-local address is"
 
708
                     " incomplete without a network interface\n");
724
709
        errno = EINVAL;
725
710
        goto mandos_end;
726
711
      }
744
729
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
745
730
        perror_plus("if_indextoname");
746
731
      } else {
747
 
        fprintf(stderr, "Mandos plugin mandos-client: "
748
 
                "Connection to: %s%%%s, port %" PRIu16 "\n",
749
 
                ip, interface, port);
 
732
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
 
733
                     "\n", ip, interface, port);
750
734
      }
751
735
    } else {
752
 
      fprintf(stderr, "Mandos plugin mandos-client: "
753
 
              "Connection to: %s, port %" PRIu16 "\n", ip, port);
 
736
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
 
737
                   ip, port);
754
738
    }
755
739
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
756
740
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
766
750
      perror_plus("inet_ntop");
767
751
    } else {
768
752
      if(strcmp(addrstr, ip) != 0){
769
 
        fprintf(stderr, "Mandos plugin mandos-client: "
770
 
                "Canonical address form: %s\n", addrstr);
 
753
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
771
754
      }
772
755
    }
773
756
  }
827
810
  }
828
811
  
829
812
  if(debug){
830
 
    fprintf(stderr, "Mandos plugin mandos-client: "
831
 
            "Establishing TLS session with %s\n", ip);
 
813
    fprintf_plus(stderr, "Establishing TLS session with %s\n", ip);
832
814
  }
833
815
  
834
816
  if(quit_now){
854
836
  
855
837
  if(ret != GNUTLS_E_SUCCESS){
856
838
    if(debug){
857
 
      fprintf(stderr, "Mandos plugin mandos-client: "
858
 
              "*** GnuTLS Handshake failed ***\n");
 
839
      fprintf_plus(stderr, "*** GnuTLS Handshake failed ***\n");
859
840
      gnutls_perror(ret);
860
841
    }
861
842
    errno = EPROTO;
865
846
  /* Read OpenPGP packet that contains the wanted password */
866
847
  
867
848
  if(debug){
868
 
    fprintf(stderr, "Mandos plugin mandos-client: "
869
 
            "Retrieving OpenPGP encrypted password from %s\n", ip);
 
849
    fprintf_plus(stderr, "Retrieving OpenPGP encrypted password from"
 
850
                 " %s\n", ip);
870
851
  }
871
852
  
872
853
  while(true){
910
891
          }
911
892
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
912
893
        if(ret < 0){
913
 
          fprintf(stderr, "Mandos plugin mandos-client: "
914
 
                  "*** GnuTLS Re-handshake failed ***\n");
 
894
          fprintf_plus(stderr, "*** GnuTLS Re-handshake failed "
 
895
                       "***\n");
915
896
          gnutls_perror(ret);
916
897
          errno = EPROTO;
917
898
          goto mandos_end;
918
899
        }
919
900
        break;
920
901
      default:
921
 
        fprintf(stderr, "Mandos plugin mandos-client: "
922
 
                "Unknown error while reading data from"
923
 
                " encrypted session with Mandos server\n");
 
902
        fprintf_plus(stderr, "Unknown error while reading data from"
 
903
                     " encrypted session with Mandos server\n");
924
904
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
925
905
        errno = EIO;
926
906
        goto mandos_end;
931
911
  }
932
912
  
933
913
  if(debug){
934
 
    fprintf(stderr, "Mandos plugin mandos-client: "
935
 
            "Closing TLS session\n");
 
914
    fprintf_plus(stderr, "Closing TLS session\n");
936
915
  }
937
916
  
938
917
  if(quit_now){
967
946
        if(ret == 0 and ferror(stdout)){
968
947
          int e = errno;
969
948
          if(debug){
970
 
            fprintf(stderr, "Mandos plugin mandos-client: "
971
 
                    "Error writing encrypted data: %s\n",
972
 
                    strerror(errno));
 
949
            fprintf_plus(stderr, "Error writing encrypted data: %s\n",
 
950
                         strerror(errno));
973
951
          }
974
952
          errno = e;
975
953
          goto mandos_end;
1032
1010
  switch(event){
1033
1011
  default:
1034
1012
  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)));
 
1013
    fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
 
1014
                 "'%s' of type '%s' in domain '%s': %s\n", name, type,
 
1015
                 domain,
 
1016
                 avahi_strerror(avahi_server_errno(mc.server)));
1039
1017
    break;
1040
1018
    
1041
1019
  case AVAHI_RESOLVER_FOUND:
1043
1021
      char ip[AVAHI_ADDRESS_STR_MAX];
1044
1022
      avahi_address_snprint(ip, sizeof(ip), address);
1045
1023
      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);
 
1024
        fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
 
1025
                     PRIdMAX ") on port %" PRIu16 "\n", name,
 
1026
                     host_name, ip, (intmax_t)interface, port);
1050
1027
      }
1051
1028
      int ret = start_mandos_communication(ip, port, interface,
1052
1029
                                           avahi_proto_to_af(proto));
1053
1030
      if(ret == 0){
1054
1031
        avahi_simple_poll_quit(mc.simple_poll);
1055
1032
      } else {
1056
 
        ret = add_server(ip, port, interface,
1057
 
                         avahi_proto_to_af(proto));
 
1033
        if(not add_server(ip, port, interface,
 
1034
                          avahi_proto_to_af(proto))){
 
1035
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
 
1036
                       " list\n", name);
 
1037
        }
1058
1038
      }
1059
1039
    }
1060
1040
  }
1084
1064
  default:
1085
1065
  case AVAHI_BROWSER_FAILURE:
1086
1066
    
1087
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1088
 
            "(Avahi browser) %s\n",
1089
 
            avahi_strerror(avahi_server_errno(mc.server)));
 
1067
    fprintf_plus(stderr, "(Avahi browser) %s\n",
 
1068
                 avahi_strerror(avahi_server_errno(mc.server)));
1090
1069
    avahi_simple_poll_quit(mc.simple_poll);
1091
1070
    return;
1092
1071
    
1099
1078
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1100
1079
                                    name, type, domain, protocol, 0,
1101
1080
                                    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)));
 
1081
      fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
 
1082
                   " %s\n", name,
 
1083
                   avahi_strerror(avahi_server_errno(mc.server)));
1105
1084
    break;
1106
1085
    
1107
1086
  case AVAHI_BROWSER_REMOVE:
1110
1089
  case AVAHI_BROWSER_ALL_FOR_NOW:
1111
1090
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1112
1091
    if(debug){
1113
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1114
 
              "No Mandos server found, still searching...\n");
 
1092
      fprintf_plus(stderr, "No Mandos server found, still"
 
1093
                   " searching...\n");
1115
1094
    }
1116
1095
    break;
1117
1096
  }
1156
1135
  /* Reject the loopback device */
1157
1136
  if(ifr->ifr_flags & IFF_LOOPBACK){
1158
1137
    if(debug){
1159
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1160
 
              "Rejecting loopback interface \"%s\"\n", ifname);
 
1138
      fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
 
1139
                   ifname);
1161
1140
    }
1162
1141
    return false;
1163
1142
  }
1164
1143
  /* Accept point-to-point devices only if connect_to is specified */
1165
1144
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1166
1145
    if(debug){
1167
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1168
 
              "Accepting point-to-point interface \"%s\"\n", ifname);
 
1146
      fprintf_plus(stderr, "Accepting point-to-point interface"
 
1147
                   " \"%s\"\n", ifname);
1169
1148
    }
1170
1149
    return true;
1171
1150
  }
1172
1151
  /* Otherwise, reject non-broadcast-capable devices */
1173
1152
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1174
1153
    if(debug){
1175
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1176
 
              "Rejecting non-broadcast interface \"%s\"\n", ifname);
 
1154
      fprintf_plus(stderr, "Rejecting non-broadcast interface"
 
1155
                   " \"%s\"\n", ifname);
1177
1156
    }
1178
1157
    return false;
1179
1158
  }
1180
1159
  /* Reject non-ARP interfaces (including dummy interfaces) */
1181
1160
  if(ifr->ifr_flags & IFF_NOARP){
1182
1161
    if(debug){
1183
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1184
 
              "Rejecting non-ARP interface \"%s\"\n", ifname);
 
1162
      fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
 
1163
                   ifname);
1185
1164
    }
1186
1165
    return false;
1187
1166
  }
1188
1167
  
1189
1168
  /* Accept this device */
1190
1169
  if(debug){
1191
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1192
 
            "Interface \"%s\" is good\n", ifname);
 
1170
    fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1193
1171
  }
1194
1172
  return true;
1195
1173
}
1207
1185
  struct ifreq ifr;
1208
1186
  if(not get_flags(if_entry->d_name, &ifr)){
1209
1187
    if(debug){
1210
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1211
 
              "Failed to get flags for interface \"%s\"\n",
1212
 
              if_entry->d_name);
 
1188
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1189
                   "\"%s\"\n", if_entry->d_name);
1213
1190
    }
1214
1191
    return 0;
1215
1192
  }
1233
1210
  struct ifreq ifr;
1234
1211
  if(not get_flags(if_entry->d_name, &ifr)){
1235
1212
    if(debug){
1236
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1237
 
              "Failed to get flags for interface \"%s\"\n",
1238
 
              if_entry->d_name);
 
1213
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1214
                   "\"%s\"\n", if_entry->d_name);
1239
1215
    }
1240
1216
    return 0;
1241
1217
  }
1243
1219
  /* Reject down interfaces */
1244
1220
  if(not (ifr.ifr_flags & IFF_UP)){
1245
1221
    if(debug){
1246
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1247
 
              "Rejecting down interface \"%s\"\n",
1248
 
              if_entry->d_name);
 
1222
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
 
1223
                   if_entry->d_name);
1249
1224
    }
1250
1225
    return 0;
1251
1226
  }
1253
1228
  /* Reject non-running interfaces */
1254
1229
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1255
1230
    if(debug){
1256
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1257
 
              "Rejecting non-running interface \"%s\"\n",
1258
 
              if_entry->d_name);
 
1231
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
 
1232
                   if_entry->d_name);
1259
1233
    }
1260
1234
    return 0;
1261
1235
  }
1295
1269
  if((direntry->d_name)[sret] != '\0'){
1296
1270
    /* Contains non-allowed characters */
1297
1271
    if(debug){
1298
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1299
 
              "Ignoring hook \"%s\" with bad name\n",
1300
 
              direntry->d_name);
 
1272
      fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
 
1273
                   direntry->d_name);
1301
1274
    }
1302
1275
    return 0;
1303
1276
  }
1319
1292
  if(not (S_ISREG(st.st_mode))){
1320
1293
    /* Not a regular file */
1321
1294
    if(debug){
1322
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1323
 
              "Ignoring hook \"%s\" - not a file\n",
1324
 
              direntry->d_name);
 
1295
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
 
1296
                   direntry->d_name);
1325
1297
    }
1326
1298
    return 0;
1327
1299
  }
1328
1300
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1329
1301
    /* Not executable */
1330
1302
    if(debug){
1331
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1332
 
              "Ignoring hook \"%s\" - not executable\n",
1333
 
              direntry->d_name);
 
1303
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
 
1304
                   direntry->d_name);
1334
1305
    }
1335
1306
    return 0;
1336
1307
  }
 
1308
  if(debug){
 
1309
    fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
 
1310
                 direntry->d_name);
 
1311
  }
1337
1312
  return 1;
1338
1313
}
1339
1314
 
1346
1321
  while(true){
1347
1322
    if(mc.current_server == NULL){
1348
1323
      if (debug){
1349
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1350
 
                "Wait until first server is found. No timeout!\n");
 
1324
        fprintf_plus(stderr, "Wait until first server is found."
 
1325
                     " No timeout!\n");
1351
1326
      }
1352
1327
      ret = avahi_simple_poll_iterate(s, -1);
1353
1328
    } else {
1354
1329
      if (debug){
1355
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1356
 
                "Check current_server if we should run it,"
1357
 
                " or wait\n");
 
1330
        fprintf_plus(stderr, "Check current_server if we should run"
 
1331
                     " it, or wait\n");
1358
1332
      }
1359
1333
      /* the current time */
1360
1334
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1376
1350
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1377
1351
      
1378
1352
      if (debug){
1379
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1380
 
                "Blocking for %" PRIdMAX " ms\n", block_time);
 
1353
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
 
1354
                     block_time);
1381
1355
      }
1382
1356
      
1383
1357
      if(block_time <= 0){
1410
1384
  }
1411
1385
}
1412
1386
 
 
1387
bool run_network_hooks(const char *mode, const char *interface,
 
1388
                       const float delay){
 
1389
  struct dirent **direntries;
 
1390
  struct dirent *direntry;
 
1391
  int ret;
 
1392
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1393
                         alphasort);
 
1394
  if(numhooks == -1){
 
1395
    perror_plus("scandir");
 
1396
  } else {
 
1397
    int devnull = open("/dev/null", O_RDONLY);
 
1398
    for(int i = 0; i < numhooks; i++){
 
1399
      direntry = direntries[i];
 
1400
      char *fullname = NULL;
 
1401
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1402
      if(ret < 0){
 
1403
        perror_plus("asprintf");
 
1404
        continue;
 
1405
      }
 
1406
      if(debug){
 
1407
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1408
                     direntry->d_name);
 
1409
      }
 
1410
      pid_t hook_pid = fork();
 
1411
      if(hook_pid == 0){
 
1412
        /* Child */
 
1413
        /* Raise privileges */
 
1414
        errno = 0;
 
1415
        ret = seteuid(0);
 
1416
        if(ret == -1){
 
1417
          perror_plus("seteuid");
 
1418
        }
 
1419
        /* Raise privileges even more */
 
1420
        errno = 0;
 
1421
        ret = setuid(0);
 
1422
        if(ret == -1){
 
1423
          perror_plus("setuid");
 
1424
        }
 
1425
        /* Set group */
 
1426
        errno = 0;
 
1427
        ret = setgid(0);
 
1428
        if(ret == -1){
 
1429
          perror_plus("setgid");
 
1430
        }
 
1431
        /* Reset supplementary groups */
 
1432
        errno = 0;
 
1433
        ret = setgroups(0, NULL);
 
1434
        if(ret == -1){
 
1435
          perror_plus("setgroups");
 
1436
        }
 
1437
        dup2(devnull, STDIN_FILENO);
 
1438
        close(devnull);
 
1439
        dup2(STDERR_FILENO, STDOUT_FILENO);
 
1440
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1441
        if(ret == -1){
 
1442
          perror_plus("setenv");
 
1443
          _exit(EX_OSERR);
 
1444
        }
 
1445
        ret = setenv("DEVICE", interface, 1);
 
1446
        if(ret == -1){
 
1447
          perror_plus("setenv");
 
1448
          _exit(EX_OSERR);
 
1449
        }
 
1450
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1451
        if(ret == -1){
 
1452
          perror_plus("setenv");
 
1453
          _exit(EX_OSERR);
 
1454
        }
 
1455
        ret = setenv("MODE", mode, 1);
 
1456
        if(ret == -1){
 
1457
          perror_plus("setenv");
 
1458
          _exit(EX_OSERR);
 
1459
        }
 
1460
        char *delaystring;
 
1461
        ret = asprintf(&delaystring, "%f", delay);
 
1462
        if(ret == -1){
 
1463
          perror_plus("asprintf");
 
1464
          _exit(EX_OSERR);
 
1465
        }
 
1466
        ret = setenv("DELAY", delaystring, 1);
 
1467
        if(ret == -1){
 
1468
          free(delaystring);
 
1469
          perror_plus("setenv");
 
1470
          _exit(EX_OSERR);
 
1471
        }
 
1472
        free(delaystring);
 
1473
        ret = execl(fullname, direntry->d_name, mode, NULL);
 
1474
        perror_plus("execl");
 
1475
      } else {
 
1476
        int status;
 
1477
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1478
          perror_plus("waitpid");
 
1479
          free(fullname);
 
1480
          continue;
 
1481
        }
 
1482
        if(WIFEXITED(status)){
 
1483
          if(WEXITSTATUS(status) != 0){
 
1484
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1485
                         " with status %d\n", direntry->d_name,
 
1486
                         WEXITSTATUS(status));
 
1487
            free(fullname);
 
1488
            continue;
 
1489
          }
 
1490
        } else if(WIFSIGNALED(status)){
 
1491
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1492
                       " signal %d\n", direntry->d_name,
 
1493
                       WTERMSIG(status));
 
1494
          free(fullname);
 
1495
          continue;
 
1496
        } else {
 
1497
          fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1498
                       " crashed\n", direntry->d_name);
 
1499
          free(fullname);
 
1500
          continue;
 
1501
        }
 
1502
      }
 
1503
      free(fullname);
 
1504
      if(debug){
 
1505
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1506
                     direntry->d_name);
 
1507
      }
 
1508
    }
 
1509
    close(devnull);
 
1510
  }
 
1511
  return true;
 
1512
}
 
1513
 
1413
1514
int main(int argc, char *argv[]){
1414
1515
  AvahiSServiceBrowser *sb = NULL;
1415
1516
  int error;
1573
1674
        argp_state_help(state, state->out_stream,
1574
1675
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1575
1676
      case 'V':                 /* --version */
1576
 
        fprintf(state->out_stream, "Mandos plugin mandos-client: ");
1577
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
 
1677
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1578
1678
        exit(argp_err_exit_status);
1579
1679
        break;
1580
1680
      default:
1607
1707
  {
1608
1708
    /* Work around Debian bug #633582:
1609
1709
       <http://bugs.debian.org/633582> */
1610
 
    struct stat st;
1611
1710
    
1612
1711
    /* Re-raise priviliges */
1613
1712
    errno = 0;
1614
1713
    ret = seteuid(0);
1615
1714
    if(ret == -1){
1616
1715
      perror_plus("seteuid");
1617
 
    }
1618
 
    
1619
 
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1620
 
      int seckey_fd = open(seckey, O_RDONLY);
1621
 
      if(seckey_fd == -1){
1622
 
        perror_plus("open");
1623
 
      } else {
1624
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1625
 
        if(ret == -1){
1626
 
          perror_plus("fstat");
1627
 
        } else {
1628
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1629
 
            ret = fchown(seckey_fd, uid, gid);
1630
 
            if(ret == -1){
1631
 
              perror_plus("fchown");
1632
 
            }
1633
 
          }
1634
 
        }
1635
 
        TEMP_FAILURE_RETRY(close(seckey_fd));
1636
 
      }
1637
 
    }
1638
 
    
1639
 
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1640
 
      int pubkey_fd = open(pubkey, O_RDONLY);
1641
 
      if(pubkey_fd == -1){
1642
 
        perror_plus("open");
1643
 
      } else {
1644
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1645
 
        if(ret == -1){
1646
 
          perror_plus("fstat");
1647
 
        } else {
1648
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1649
 
            ret = fchown(pubkey_fd, uid, gid);
1650
 
            if(ret == -1){
1651
 
              perror_plus("fchown");
1652
 
            }
1653
 
          }
1654
 
        }
1655
 
        TEMP_FAILURE_RETRY(close(pubkey_fd));
1656
 
      }
1657
 
    }
1658
 
    
1659
 
    /* Lower privileges */
1660
 
    errno = 0;
1661
 
    ret = seteuid(uid);
1662
 
    if(ret == -1){
1663
 
      perror_plus("seteuid");
1664
 
    }
1665
 
  }
1666
 
  
1667
 
  /* Find network hooks and run them */
1668
 
  {
1669
 
    struct dirent **direntries;
1670
 
    struct dirent *direntry;
1671
 
    int numhooks = scandir(hookdir, &direntries, runnable_hook,
1672
 
                           alphasort);
1673
 
    if(numhooks == -1){
1674
 
      perror_plus("scandir");
1675
1716
    } else {
1676
 
      int devnull = open("/dev/null", O_RDONLY);
1677
 
      for(int i = 0; i < numhooks; i++){
1678
 
        direntry = direntries[0];
1679
 
        char *fullname = NULL;
1680
 
        ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1681
 
        if(ret < 0){
1682
 
          perror_plus("asprintf");
1683
 
          continue;
1684
 
        }
1685
 
        pid_t hook_pid = fork();
1686
 
        if(hook_pid == 0){
1687
 
          /* Child */
1688
 
          dup2(devnull, STDIN_FILENO);
1689
 
          close(devnull);
1690
 
          dup2(STDERR_FILENO, STDOUT_FILENO);
1691
 
          ret = setenv("DEVICE", interface, 1);
1692
 
          if(ret == -1){
1693
 
            perror_plus("setenv");
1694
 
            exit(1);
1695
 
          }
1696
 
          ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1697
 
          if(ret == -1){
1698
 
            perror_plus("setenv");
1699
 
            exit(1);
1700
 
          }
1701
 
          ret = setenv("MODE", "start", 1);
1702
 
          if(ret == -1){
1703
 
            perror_plus("setenv");
1704
 
            exit(1);
1705
 
          }
1706
 
          char *delaystring;
1707
 
          ret = asprintf(&delaystring, "%f", delay);
1708
 
          if(ret == -1){
1709
 
            perror_plus("asprintf");
1710
 
            exit(1);
1711
 
          }
1712
 
          ret = setenv("DELAY", delaystring, 1);
1713
 
          if(ret == -1){
1714
 
            free(delaystring);
1715
 
            perror_plus("setenv");
1716
 
            exit(1);
1717
 
          }
1718
 
          free(delaystring);
1719
 
          ret = execl(fullname, direntry->d_name, "start", NULL);
1720
 
          perror_plus("execl");
1721
 
        } else {
1722
 
          int status;
1723
 
          if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1724
 
            perror_plus("waitpid");
1725
 
            free(fullname);
1726
 
            continue;
1727
 
          }
1728
 
          if(WIFEXITED(status)){
1729
 
            if(WEXITSTATUS(status) != 0){
1730
 
              fprintf(stderr, "Mandos plugin mandos-client: "
1731
 
                      "Warning: network hook \"%s\" exited"
1732
 
                      " with status %d\n", direntry->d_name,
1733
 
                      WEXITSTATUS(status));
1734
 
              free(fullname);
1735
 
              continue;
1736
 
            }
1737
 
          } else if(WIFSIGNALED(status)){
1738
 
            fprintf(stderr, "Mandos plugin mandos-client: "
1739
 
                    "Warning: network hook \"%s\" died by"
1740
 
                    " signal %d\n", direntry->d_name,
1741
 
                    WTERMSIG(status));
1742
 
            free(fullname);
1743
 
            continue;
1744
 
          } else {
1745
 
            fprintf(stderr, "Mandos plugin mandos-client: "
1746
 
                    "Warning: network hook \"%s\" crashed\n",
1747
 
                    direntry->d_name);
1748
 
            free(fullname);
1749
 
            continue;
1750
 
          }
1751
 
        }
1752
 
        free(fullname);
1753
 
        if(quit_now){
1754
 
          goto end;
1755
 
        }
1756
 
      }
1757
 
      close(devnull);
 
1717
      struct stat st;
 
1718
      
 
1719
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1720
        int seckey_fd = open(seckey, O_RDONLY);
 
1721
        if(seckey_fd == -1){
 
1722
          perror_plus("open");
 
1723
        } else {
 
1724
          ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1725
          if(ret == -1){
 
1726
            perror_plus("fstat");
 
1727
          } else {
 
1728
            if(S_ISREG(st.st_mode)
 
1729
               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)
 
1750
               and st.st_uid == 0 and st.st_gid == 0){
 
1751
              ret = fchown(pubkey_fd, uid, gid);
 
1752
              if(ret == -1){
 
1753
                perror_plus("fchown");
 
1754
              }
 
1755
            }
 
1756
          }
 
1757
          TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1758
        }
 
1759
      }
 
1760
    
 
1761
      /* Lower privileges */
 
1762
      errno = 0;
 
1763
      ret = seteuid(uid);
 
1764
      if(ret == -1){
 
1765
        perror_plus("seteuid");
 
1766
      }
1758
1767
    }
1759
1768
  }
1760
1769
  
 
1770
  /* Run network hooks */
 
1771
  if(not run_network_hooks("start", interface, delay)){
 
1772
    goto end;
 
1773
  }
 
1774
  
1761
1775
  if(not debug){
1762
1776
    avahi_set_log_function(empty_log);
1763
1777
  }
1777
1791
      /* Pick the first interface returned */
1778
1792
      interface = strdup(direntries[0]->d_name);
1779
1793
      if(debug){
1780
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1781
 
                "Using interface \"%s\"\n", interface);
 
1794
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1782
1795
      }
1783
1796
      if(interface == NULL){
1784
1797
        perror_plus("malloc");
1789
1802
      free(direntries);
1790
1803
    } else {
1791
1804
      free(direntries);
1792
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1793
 
              "Could not find a network interface\n");
 
1805
      fprintf_plus(stderr, "Could not find a network interface\n");
1794
1806
      exitcode = EXIT_FAILURE;
1795
1807
      goto end;
1796
1808
    }
1802
1814
  srand((unsigned int) time(NULL));
1803
1815
  mc.simple_poll = avahi_simple_poll_new();
1804
1816
  if(mc.simple_poll == NULL){
1805
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1806
 
            "Avahi: Failed to create simple poll object.\n");
 
1817
    fprintf_plus(stderr,
 
1818
                 "Avahi: Failed to create simple poll object.\n");
1807
1819
    exitcode = EX_UNAVAILABLE;
1808
1820
    goto end;
1809
1821
  }
1875
1887
  if(strcmp(interface, "none") != 0){
1876
1888
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1877
1889
    if(if_index == 0){
1878
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1879
 
              "No such interface: \"%s\"\n", interface);
 
1890
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1880
1891
      exitcode = EX_UNAVAILABLE;
1881
1892
      goto end;
1882
1893
    }
2002
2013
#endif  /* __linux__ */
2003
2014
    /* Lower privileges */
2004
2015
    errno = 0;
2005
 
    if(take_down_interface){
2006
 
      /* Lower privileges */
2007
 
      ret = seteuid(uid);
2008
 
      if(ret == -1){
2009
 
        perror_plus("seteuid");
2010
 
      }
2011
 
    } else {
2012
 
      /* Lower privileges permanently */
2013
 
      ret = setuid(uid);
2014
 
      if(ret == -1){
2015
 
        perror_plus("setuid");
2016
 
      }
 
2016
    /* Lower privileges */
 
2017
    ret = seteuid(uid);
 
2018
    if(ret == -1){
 
2019
      perror_plus("seteuid");
2017
2020
    }
2018
2021
  }
2019
2022
  
2023
2026
  
2024
2027
  ret = init_gnutls_global(pubkey, seckey);
2025
2028
  if(ret == -1){
2026
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2027
 
            "init_gnutls_global failed\n");
 
2029
    fprintf_plus(stderr, "init_gnutls_global failed\n");
2028
2030
    exitcode = EX_UNAVAILABLE;
2029
2031
    goto end;
2030
2032
  } else {
2046
2048
  }
2047
2049
  
2048
2050
  if(not init_gpgme(pubkey, seckey, tempdir)){
2049
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2050
 
            "init_gpgme failed\n");
 
2051
    fprintf_plus(stderr, "init_gpgme failed\n");
2051
2052
    exitcode = EX_UNAVAILABLE;
2052
2053
    goto end;
2053
2054
  } else {
2063
2064
    /* (Mainly meant for debugging) */
2064
2065
    char *address = strrchr(connect_to, ':');
2065
2066
    if(address == NULL){
2066
 
      fprintf(stderr, "Mandos plugin mandos-client: "
2067
 
              "No colon in address\n");
 
2067
      fprintf_plus(stderr, "No colon in address\n");
2068
2068
      exitcode = EX_USAGE;
2069
2069
      goto end;
2070
2070
    }
2078
2078
    tmpmax = strtoimax(address+1, &tmp, 10);
2079
2079
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2080
2080
       or tmpmax != (uint16_t)tmpmax){
2081
 
      fprintf(stderr, "Mandos plugin mandos-client: "
2082
 
              "Bad port number\n");
 
2081
      fprintf_plus(stderr, "Bad port number\n");
2083
2082
      exitcode = EX_USAGE;
2084
2083
      goto end;
2085
2084
    }
2115
2114
        break;
2116
2115
      }
2117
2116
      if(debug){
2118
 
        fprintf(stderr, "Mandos plugin mandos-client: "
2119
 
                "Retrying in %d seconds\n", (int)retry_interval);
 
2117
        fprintf_plus(stderr, "Retrying in %d seconds\n",
 
2118
                     (int)retry_interval);
2120
2119
      }
2121
2120
      sleep((int)retry_interval);
2122
2121
    }
2152
2151
  
2153
2152
  /* Check if creating the Avahi server object succeeded */
2154
2153
  if(mc.server == NULL){
2155
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2156
 
            "Failed to create Avahi server: %s\n",
2157
 
            avahi_strerror(error));
 
2154
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
 
2155
                 avahi_strerror(error));
2158
2156
    exitcode = EX_UNAVAILABLE;
2159
2157
    goto end;
2160
2158
  }
2168
2166
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2169
2167
                                   NULL, 0, browse_callback, NULL);
2170
2168
  if(sb == NULL){
2171
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2172
 
            "Failed to create service browser: %s\n",
2173
 
            avahi_strerror(avahi_server_errno(mc.server)));
 
2169
    fprintf_plus(stderr, "Failed to create service browser: %s\n",
 
2170
                 avahi_strerror(avahi_server_errno(mc.server)));
2174
2171
    exitcode = EX_UNAVAILABLE;
2175
2172
    goto end;
2176
2173
  }
2182
2179
  /* Run the main loop */
2183
2180
  
2184
2181
  if(debug){
2185
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2186
 
            "Starting Avahi loop search\n");
 
2182
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2187
2183
  }
2188
2184
 
2189
2185
  ret = avahi_loop_with_timeout(mc.simple_poll,
2190
2186
                                (int)(retry_interval * 1000));
2191
2187
  if(debug){
2192
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2193
 
            "avahi_loop_with_timeout exited %s\n",
2194
 
            (ret == 0) ? "successfully" : "with error");
 
2188
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
 
2189
                 (ret == 0) ? "successfully" : "with error");
2195
2190
  }
2196
2191
  
2197
2192
 end:
2198
2193
  
2199
2194
  if(debug){
2200
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2201
 
            "%s exiting\n", argv[0]);
 
2195
    fprintf_plus(stderr, "%s exiting\n", argv[0]);
2202
2196
  }
2203
2197
  
2204
2198
  /* Cleanup things */
2220
2214
  if(gpgme_initialized){
2221
2215
    gpgme_release(mc.ctx);
2222
2216
  }
2223
 
 
 
2217
  
2224
2218
  /* Cleans up the circular linked list of Mandos servers the client
2225
2219
     has seen */
2226
2220
  if(mc.current_server != NULL){
2232
2226
    }
2233
2227
  }
2234
2228
  
2235
 
  /* XXX run network hooks "stop" here  */
 
2229
  /* Run network hooks */
 
2230
  run_network_hooks("stop", interface, delay);
2236
2231
  
2237
 
  /* Take down the network interface */
2238
 
  if(take_down_interface){
2239
 
    /* Re-raise priviliges */
 
2232
  /* Re-raise priviliges */
 
2233
  {
2240
2234
    errno = 0;
2241
2235
    ret = seteuid(0);
2242
2236
    if(ret == -1){
2243
2237
      perror_plus("seteuid");
2244
2238
    }
2245
 
    if(geteuid() == 0){
 
2239
    
 
2240
    /* Take down the network interface */
 
2241
    if(take_down_interface and geteuid() == 0){
2246
2242
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2247
2243
      if(ret == -1){
2248
2244
        perror_plus("ioctl SIOCGIFFLAGS");
2257
2253
      if(ret == -1){
2258
2254
        perror_plus("close");
2259
2255
      }
2260
 
      /* Lower privileges permanently */
2261
 
      errno = 0;
2262
 
      ret = setuid(uid);
2263
 
      if(ret == -1){
2264
 
        perror_plus("setuid");
2265
 
      }
2266
2256
    }
2267
2257
  }
 
2258
  /* Lower privileges permanently */
 
2259
  errno = 0;
 
2260
  ret = setuid(uid);
 
2261
  if(ret == -1){
 
2262
    perror_plus("setuid");
 
2263
  }
2268
2264
  
2269
2265
  /* Removes the GPGME temp directory and all files inside */
2270
2266
  if(tempdir_created){
2284
2280
        }
2285
2281
        ret = remove(fullname);
2286
2282
        if(ret == -1){
2287
 
          fprintf(stderr, "Mandos plugin mandos-client: "
2288
 
                  "remove(\"%s\"): %s\n", fullname, strerror(errno));
 
2283
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
2284
                       strerror(errno));
2289
2285
        }
2290
2286
        free(fullname);
2291
2287
      }