/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

* plugins.d/mandos-client.c (runnable_hook): Bug fix: stat using the
                                             full path.
  (mail): Take new "--network-hook-dir" option.

Show diffs side-by-side

added added

removed removed

Lines of Context:
110
110
                                   init_gnutls_session(),
111
111
                                   GNUTLS_* */
112
112
#include <gnutls/openpgp.h>
113
 
                         /* gnutls_certificate_set_openpgp_key_file(),
114
 
                            GNUTLS_OPENPGP_FMT_BASE64 */
 
113
                          /* gnutls_certificate_set_openpgp_key_file(),
 
114
                                   GNUTLS_OPENPGP_FMT_BASE64 */
115
115
 
116
116
/* GPGME */
117
117
#include <gpgme.h>              /* All GPGME types, constants and
174
174
  perror(print_text);
175
175
}
176
176
 
177
 
int fprintf_plus(FILE *stream, const char *format, ...){
178
 
  va_list ap;
179
 
  va_start (ap, format);
180
 
  
181
 
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
182
 
                             program_invocation_short_name));
183
 
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
184
 
}
185
 
 
186
177
/*
187
178
 * Make additional room in "buffer" for at least BUFFER_SIZE more
188
179
 * bytes. "buffer_capacity" is how much is currently allocated,
189
180
 * "buffer_length" is how much is already used.
190
181
 */
191
182
size_t incbuffer(char **buffer, size_t buffer_length,
192
 
                 size_t buffer_capacity){
 
183
                  size_t buffer_capacity){
193
184
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
194
185
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
195
186
    if(buffer == NULL){
201
192
}
202
193
 
203
194
/* Add server to set of servers to retry periodically */
204
 
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
205
 
               int af){
 
195
int add_server(const char *ip, uint16_t port,
 
196
                 AvahiIfIndex if_index,
 
197
                 int af){
206
198
  int ret;
207
199
  server *new_server = malloc(sizeof(server));
208
200
  if(new_server == NULL){
210
202
    return -1;
211
203
  }
212
204
  *new_server = (server){ .ip = strdup(ip),
213
 
                          .port = port,
214
 
                          .if_index = if_index,
215
 
                          .af = af };
 
205
                         .port = port,
 
206
                         .if_index = if_index,
 
207
                         .af = af };
216
208
  if(new_server->ip == NULL){
217
209
    perror_plus("strdup");
218
210
    return -1;
240
232
/* 
241
233
 * Initialize GPGME.
242
234
 */
243
 
static bool init_gpgme(const char *seckey, const char *pubkey,
244
 
                       const char *tempdir){
 
235
static bool init_gpgme(const char *seckey,
 
236
                       const char *pubkey, const char *tempdir){
245
237
  gpgme_error_t rc;
246
238
  gpgme_engine_info_t engine_info;
247
239
  
262
254
    
263
255
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
264
256
    if(rc != GPG_ERR_NO_ERROR){
265
 
      fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
266
 
                   gpgme_strsource(rc), gpgme_strerror(rc));
 
257
      fprintf(stderr, "Mandos plugin mandos-client: "
 
258
              "bad gpgme_data_new_from_fd: %s: %s\n",
 
259
              gpgme_strsource(rc), gpgme_strerror(rc));
267
260
      return false;
268
261
    }
269
262
    
270
263
    rc = gpgme_op_import(mc.ctx, pgp_data);
271
264
    if(rc != GPG_ERR_NO_ERROR){
272
 
      fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
273
 
                   gpgme_strsource(rc), gpgme_strerror(rc));
 
265
      fprintf(stderr, "Mandos plugin mandos-client: "
 
266
              "bad gpgme_op_import: %s: %s\n",
 
267
              gpgme_strsource(rc), gpgme_strerror(rc));
274
268
      return false;
275
269
    }
276
270
    
283
277
  }
284
278
  
285
279
  if(debug){
286
 
    fprintf_plus(stderr, "Initializing GPGME\n");
 
280
    fprintf(stderr, "Mandos plugin mandos-client: "
 
281
            "Initializing GPGME\n");
287
282
  }
288
283
  
289
284
  /* Init GPGME */
290
285
  gpgme_check_version(NULL);
291
286
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
292
287
  if(rc != GPG_ERR_NO_ERROR){
293
 
    fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
294
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
288
    fprintf(stderr, "Mandos plugin mandos-client: "
 
289
            "bad gpgme_engine_check_version: %s: %s\n",
 
290
            gpgme_strsource(rc), gpgme_strerror(rc));
295
291
    return false;
296
292
  }
297
293
  
298
294
  /* Set GPGME home directory for the OpenPGP engine only */
299
295
  rc = gpgme_get_engine_info(&engine_info);
300
296
  if(rc != GPG_ERR_NO_ERROR){
301
 
    fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
302
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
297
    fprintf(stderr, "Mandos plugin mandos-client: "
 
298
            "bad gpgme_get_engine_info: %s: %s\n",
 
299
            gpgme_strsource(rc), gpgme_strerror(rc));
303
300
    return false;
304
301
  }
305
302
  while(engine_info != NULL){
311
308
    engine_info = engine_info->next;
312
309
  }
313
310
  if(engine_info == NULL){
314
 
    fprintf_plus(stderr, "Could not set GPGME home dir to %s\n",
315
 
                 tempdir);
 
311
    fprintf(stderr, "Mandos plugin mandos-client: "
 
312
            "Could not set GPGME home dir to %s\n", tempdir);
316
313
    return false;
317
314
  }
318
315
  
319
316
  /* Create new GPGME "context" */
320
317
  rc = gpgme_new(&(mc.ctx));
321
318
  if(rc != GPG_ERR_NO_ERROR){
322
 
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
323
 
                 "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
324
 
                 gpgme_strerror(rc));
 
319
    fprintf(stderr, "Mandos plugin mandos-client: "
 
320
            "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
 
321
            gpgme_strerror(rc));
325
322
    return false;
326
323
  }
327
324
  
346
343
  ssize_t plaintext_length = 0;
347
344
  
348
345
  if(debug){
349
 
    fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
 
346
    fprintf(stderr, "Mandos plugin mandos-client: "
 
347
            "Trying to decrypt OpenPGP data\n");
350
348
  }
351
349
  
352
350
  /* Create new GPGME data buffer from memory cryptotext */
353
351
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
354
352
                               0);
355
353
  if(rc != GPG_ERR_NO_ERROR){
356
 
    fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
357
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
354
    fprintf(stderr, "Mandos plugin mandos-client: "
 
355
            "bad gpgme_data_new_from_mem: %s: %s\n",
 
356
            gpgme_strsource(rc), gpgme_strerror(rc));
358
357
    return -1;
359
358
  }
360
359
  
361
360
  /* Create new empty GPGME data buffer for the plaintext */
362
361
  rc = gpgme_data_new(&dh_plain);
363
362
  if(rc != GPG_ERR_NO_ERROR){
364
 
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
365
 
                 "bad gpgme_data_new: %s: %s\n",
366
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
363
    fprintf(stderr, "Mandos plugin mandos-client: "
 
364
            "bad gpgme_data_new: %s: %s\n",
 
365
            gpgme_strsource(rc), gpgme_strerror(rc));
367
366
    gpgme_data_release(dh_crypto);
368
367
    return -1;
369
368
  }
372
371
     data buffer */
373
372
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
374
373
  if(rc != GPG_ERR_NO_ERROR){
375
 
    fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
376
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
374
    fprintf(stderr, "Mandos plugin mandos-client: "
 
375
            "bad gpgme_op_decrypt: %s: %s\n",
 
376
            gpgme_strsource(rc), gpgme_strerror(rc));
377
377
    plaintext_length = -1;
378
378
    if(debug){
379
379
      gpgme_decrypt_result_t result;
380
380
      result = gpgme_op_decrypt_result(mc.ctx);
381
381
      if(result == NULL){
382
 
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
 
382
        fprintf(stderr, "Mandos plugin mandos-client: "
 
383
                "gpgme_op_decrypt_result failed\n");
383
384
      } else {
384
 
        fprintf_plus(stderr, "Unsupported algorithm: %s\n",
385
 
                     result->unsupported_algorithm);
386
 
        fprintf_plus(stderr, "Wrong key usage: %u\n",
387
 
                     result->wrong_key_usage);
 
385
        fprintf(stderr, "Mandos plugin mandos-client: "
 
386
                "Unsupported algorithm: %s\n",
 
387
                result->unsupported_algorithm);
 
388
        fprintf(stderr, "Mandos plugin mandos-client: "
 
389
                "Wrong key usage: %u\n",
 
390
                result->wrong_key_usage);
388
391
        if(result->file_name != NULL){
389
 
          fprintf_plus(stderr, "File name: %s\n", result->file_name);
 
392
          fprintf(stderr, "Mandos plugin mandos-client: "
 
393
                  "File name: %s\n", result->file_name);
390
394
        }
391
395
        gpgme_recipient_t recipient;
392
396
        recipient = result->recipients;
393
397
        while(recipient != NULL){
394
 
          fprintf_plus(stderr, "Public key algorithm: %s\n",
395
 
                       gpgme_pubkey_algo_name
396
 
                       (recipient->pubkey_algo));
397
 
          fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
398
 
          fprintf_plus(stderr, "Secret key available: %s\n",
399
 
                       recipient->status == GPG_ERR_NO_SECKEY
400
 
                       ? "No" : "Yes");
 
398
          fprintf(stderr, "Mandos plugin mandos-client: "
 
399
                  "Public key algorithm: %s\n",
 
400
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
401
          fprintf(stderr, "Mandos plugin mandos-client: "
 
402
                  "Key ID: %s\n", recipient->keyid);
 
403
          fprintf(stderr, "Mandos plugin mandos-client: "
 
404
                  "Secret key available: %s\n",
 
405
                  recipient->status == GPG_ERR_NO_SECKEY
 
406
                  ? "No" : "Yes");
401
407
          recipient = recipient->next;
402
408
        }
403
409
      }
406
412
  }
407
413
  
408
414
  if(debug){
409
 
    fprintf_plus(stderr, "Decryption of OpenPGP data succeeded\n");
 
415
    fprintf(stderr, "Mandos plugin mandos-client: "
 
416
            "Decryption of OpenPGP data succeeded\n");
410
417
  }
411
418
  
412
419
  /* Seek back to the beginning of the GPGME plaintext data buffer */
419
426
  *plaintext = NULL;
420
427
  while(true){
421
428
    plaintext_capacity = incbuffer(plaintext,
422
 
                                   (size_t)plaintext_length,
423
 
                                   plaintext_capacity);
 
429
                                      (size_t)plaintext_length,
 
430
                                      plaintext_capacity);
424
431
    if(plaintext_capacity == 0){
425
 
      perror_plus("incbuffer");
426
 
      plaintext_length = -1;
427
 
      goto decrypt_end;
 
432
        perror_plus("incbuffer");
 
433
        plaintext_length = -1;
 
434
        goto decrypt_end;
428
435
    }
429
436
    
430
437
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
443
450
  }
444
451
  
445
452
  if(debug){
446
 
    fprintf_plus(stderr, "Decrypted password is: ");
 
453
    fprintf(stderr, "Mandos plugin mandos-client: "
 
454
            "Decrypted password is: ");
447
455
    for(ssize_t i = 0; i < plaintext_length; i++){
448
456
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
449
457
    }
471
479
/* GnuTLS log function callback */
472
480
static void debuggnutls(__attribute__((unused)) int level,
473
481
                        const char* string){
474
 
  fprintf_plus(stderr, "GnuTLS: %s", string);
 
482
  fprintf(stderr, "Mandos plugin mandos-client: GnuTLS: %s", string);
475
483
}
476
484
 
477
485
static int init_gnutls_global(const char *pubkeyfilename,
479
487
  int ret;
480
488
  
481
489
  if(debug){
482
 
    fprintf_plus(stderr, "Initializing GnuTLS\n");
 
490
    fprintf(stderr, "Mandos plugin mandos-client: "
 
491
            "Initializing GnuTLS\n");
483
492
  }
484
493
  
485
494
  ret = gnutls_global_init();
486
495
  if(ret != GNUTLS_E_SUCCESS){
487
 
    fprintf_plus(stderr, "GnuTLS global_init: %s\n",
488
 
                 safer_gnutls_strerror(ret));
 
496
    fprintf(stderr, "Mandos plugin mandos-client: "
 
497
            "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
489
498
    return -1;
490
499
  }
491
500
  
500
509
  /* OpenPGP credentials */
501
510
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
502
511
  if(ret != GNUTLS_E_SUCCESS){
503
 
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
504
 
                 safer_gnutls_strerror(ret));
 
512
    fprintf(stderr, "Mandos plugin mandos-client: "
 
513
            "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
505
514
    gnutls_global_deinit();
506
515
    return -1;
507
516
  }
508
517
  
509
518
  if(debug){
510
 
    fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
511
 
                 " secret key %s as GnuTLS credentials\n",
512
 
                 pubkeyfilename,
513
 
                 seckeyfilename);
 
519
    fprintf(stderr, "Mandos plugin mandos-client: "
 
520
            "Attempting to use OpenPGP public key %s and"
 
521
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
 
522
            seckeyfilename);
514
523
  }
515
524
  
516
525
  ret = gnutls_certificate_set_openpgp_key_file
517
526
    (mc.cred, pubkeyfilename, seckeyfilename,
518
527
     GNUTLS_OPENPGP_FMT_BASE64);
519
528
  if(ret != GNUTLS_E_SUCCESS){
520
 
    fprintf_plus(stderr,
521
 
                 "Error[%d] while reading the OpenPGP key pair ('%s',"
522
 
                 " '%s')\n", ret, pubkeyfilename, seckeyfilename);
523
 
    fprintf_plus(stderr, "The GnuTLS error is: %s\n",
524
 
                 safer_gnutls_strerror(ret));
 
529
    fprintf(stderr,
 
530
            "Mandos plugin mandos-client: "
 
531
            "Error[%d] while reading the OpenPGP key pair ('%s',"
 
532
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
533
    fprintf(stderr, "Mandos plugin mandos-client: "
 
534
            "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
525
535
    goto globalfail;
526
536
  }
527
537
  
528
538
  /* GnuTLS server initialization */
529
539
  ret = gnutls_dh_params_init(&mc.dh_params);
530
540
  if(ret != GNUTLS_E_SUCCESS){
531
 
    fprintf_plus(stderr, "Error in GnuTLS DH parameter"
532
 
                 " initialization: %s\n",
533
 
                 safer_gnutls_strerror(ret));
 
541
    fprintf(stderr, "Mandos plugin mandos-client: "
 
542
            "Error in GnuTLS DH parameter initialization:"
 
543
            " %s\n", safer_gnutls_strerror(ret));
534
544
    goto globalfail;
535
545
  }
536
546
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
537
547
  if(ret != GNUTLS_E_SUCCESS){
538
 
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
539
 
                 safer_gnutls_strerror(ret));
 
548
    fprintf(stderr, "Mandos plugin mandos-client: "
 
549
            "Error in GnuTLS prime generation: %s\n",
 
550
            safer_gnutls_strerror(ret));
540
551
    goto globalfail;
541
552
  }
542
553
  
562
573
    }
563
574
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
564
575
  if(ret != GNUTLS_E_SUCCESS){
565
 
    fprintf_plus(stderr,
566
 
                 "Error in GnuTLS session initialization: %s\n",
567
 
                 safer_gnutls_strerror(ret));
 
576
    fprintf(stderr, "Mandos plugin mandos-client: "
 
577
            "Error in GnuTLS session initialization: %s\n",
 
578
            safer_gnutls_strerror(ret));
568
579
  }
569
580
  
570
581
  {
577
588
      }
578
589
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
579
590
    if(ret != GNUTLS_E_SUCCESS){
580
 
      fprintf_plus(stderr, "Syntax error at: %s\n", err);
581
 
      fprintf_plus(stderr, "GnuTLS error: %s\n",
582
 
                   safer_gnutls_strerror(ret));
 
591
      fprintf(stderr, "Mandos plugin mandos-client: "
 
592
              "Syntax error at: %s\n", err);
 
593
      fprintf(stderr, "Mandos plugin mandos-client: "
 
594
              "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
583
595
      gnutls_deinit(*session);
584
596
      return -1;
585
597
    }
594
606
    }
595
607
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
596
608
  if(ret != GNUTLS_E_SUCCESS){
597
 
    fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
598
 
                 safer_gnutls_strerror(ret));
 
609
    fprintf(stderr, "Mandos plugin mandos-client: "
 
610
            "Error setting GnuTLS credentials: %s\n",
 
611
            safer_gnutls_strerror(ret));
599
612
    gnutls_deinit(*session);
600
613
    return -1;
601
614
  }
646
659
    pf = PF_INET;
647
660
    break;
648
661
  default:
649
 
    fprintf_plus(stderr, "Bad address family: %d\n", af);
 
662
    fprintf(stderr, "Mandos plugin mandos-client: "
 
663
            "Bad address family: %d\n", af);
650
664
    errno = EINVAL;
651
665
    return -1;
652
666
  }
657
671
  }
658
672
  
659
673
  if(debug){
660
 
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
661
 
                 PRIu16 "\n", ip, port);
 
674
    fprintf(stderr, "Mandos plugin mandos-client: "
 
675
            "Setting up a TCP connection to %s, port %" PRIu16
 
676
            "\n", ip, port);
662
677
  }
663
678
  
664
679
  tcp_sd = socket(pf, SOCK_STREAM, 0);
690
705
  }
691
706
  if(ret == 0){
692
707
    int e = errno;
693
 
    fprintf_plus(stderr, "Bad address: %s\n", ip);
 
708
    fprintf(stderr, "Mandos plugin mandos-client: "
 
709
            "Bad address: %s\n", ip);
694
710
    errno = e;
695
711
    goto mandos_end;
696
712
  }
701
717
    
702
718
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
703
719
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
704
 
                                -Wunreachable-code*/
 
720
                              -Wunreachable-code*/
705
721
      if(if_index == AVAHI_IF_UNSPEC){
706
 
        fprintf_plus(stderr, "An IPv6 link-local address is"
707
 
                     " incomplete without a network interface\n");
 
722
        fprintf(stderr, "Mandos plugin mandos-client: "
 
723
                "An IPv6 link-local address is incomplete"
 
724
                " without a network interface\n");
708
725
        errno = EINVAL;
709
726
        goto mandos_end;
710
727
      }
728
745
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
729
746
        perror_plus("if_indextoname");
730
747
      } else {
731
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
732
 
                     "\n", ip, interface, port);
 
748
        fprintf(stderr, "Mandos plugin mandos-client: "
 
749
                "Connection to: %s%%%s, port %" PRIu16 "\n",
 
750
                ip, interface, port);
733
751
      }
734
752
    } else {
735
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
736
 
                   ip, port);
 
753
      fprintf(stderr, "Mandos plugin mandos-client: "
 
754
              "Connection to: %s, port %" PRIu16 "\n", ip, port);
737
755
    }
738
756
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
739
757
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
749
767
      perror_plus("inet_ntop");
750
768
    } else {
751
769
      if(strcmp(addrstr, ip) != 0){
752
 
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
 
770
        fprintf(stderr, "Mandos plugin mandos-client: "
 
771
                "Canonical address form: %s\n", addrstr);
753
772
      }
754
773
    }
755
774
  }
783
802
  while(true){
784
803
    size_t out_size = strlen(out);
785
804
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
786
 
                                        out_size - written));
 
805
                                   out_size - written));
787
806
    if(ret == -1){
788
807
      int e = errno;
789
808
      perror_plus("write");
809
828
  }
810
829
  
811
830
  if(debug){
812
 
    fprintf_plus(stderr, "Establishing TLS session with %s\n", ip);
 
831
    fprintf(stderr, "Mandos plugin mandos-client: "
 
832
            "Establishing TLS session with %s\n", ip);
813
833
  }
814
834
  
815
835
  if(quit_now){
835
855
  
836
856
  if(ret != GNUTLS_E_SUCCESS){
837
857
    if(debug){
838
 
      fprintf_plus(stderr, "*** GnuTLS Handshake failed ***\n");
 
858
      fprintf(stderr, "Mandos plugin mandos-client: "
 
859
              "*** GnuTLS Handshake failed ***\n");
839
860
      gnutls_perror(ret);
840
861
    }
841
862
    errno = EPROTO;
845
866
  /* Read OpenPGP packet that contains the wanted password */
846
867
  
847
868
  if(debug){
848
 
    fprintf_plus(stderr, "Retrieving OpenPGP encrypted password from"
849
 
                 " %s\n", ip);
 
869
    fprintf(stderr, "Mandos plugin mandos-client: "
 
870
            "Retrieving OpenPGP encrypted password from %s\n", ip);
850
871
  }
851
872
  
852
873
  while(true){
857
878
    }
858
879
    
859
880
    buffer_capacity = incbuffer(&buffer, buffer_length,
860
 
                                buffer_capacity);
 
881
                                   buffer_capacity);
861
882
    if(buffer_capacity == 0){
862
883
      int e = errno;
863
884
      perror_plus("incbuffer");
890
911
          }
891
912
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
892
913
        if(ret < 0){
893
 
          fprintf_plus(stderr, "*** GnuTLS Re-handshake failed "
894
 
                       "***\n");
 
914
          fprintf(stderr, "Mandos plugin mandos-client: "
 
915
                  "*** GnuTLS Re-handshake failed ***\n");
895
916
          gnutls_perror(ret);
896
917
          errno = EPROTO;
897
918
          goto mandos_end;
898
919
        }
899
920
        break;
900
921
      default:
901
 
        fprintf_plus(stderr, "Unknown error while reading data from"
902
 
                     " encrypted session with Mandos server\n");
 
922
        fprintf(stderr, "Mandos plugin mandos-client: "
 
923
                "Unknown error while reading data from"
 
924
                " encrypted session with Mandos server\n");
903
925
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
904
926
        errno = EIO;
905
927
        goto mandos_end;
910
932
  }
911
933
  
912
934
  if(debug){
913
 
    fprintf_plus(stderr, "Closing TLS session\n");
 
935
    fprintf(stderr, "Mandos plugin mandos-client: "
 
936
            "Closing TLS session\n");
914
937
  }
915
938
  
916
939
  if(quit_now){
928
951
  
929
952
  if(buffer_length > 0){
930
953
    ssize_t decrypted_buffer_size;
931
 
    decrypted_buffer_size = pgp_packet_decrypt(buffer, buffer_length,
 
954
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
 
955
                                               buffer_length,
932
956
                                               &decrypted_buffer);
933
957
    if(decrypted_buffer_size >= 0){
934
958
      
945
969
        if(ret == 0 and ferror(stdout)){
946
970
          int e = errno;
947
971
          if(debug){
948
 
            fprintf_plus(stderr, "Error writing encrypted data: %s\n",
949
 
                         strerror(errno));
 
972
            fprintf(stderr, "Mandos plugin mandos-client: "
 
973
                    "Error writing encrypted data: %s\n",
 
974
                    strerror(errno));
950
975
          }
951
976
          errno = e;
952
977
          goto mandos_end;
1009
1034
  switch(event){
1010
1035
  default:
1011
1036
  case AVAHI_RESOLVER_FAILURE:
1012
 
    fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
1013
 
                 "'%s' of type '%s' in domain '%s': %s\n", name, type,
1014
 
                 domain,
1015
 
                 avahi_strerror(avahi_server_errno(mc.server)));
 
1037
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1038
            "(Avahi Resolver) Failed to resolve service '%s'"
 
1039
            " of type '%s' in domain '%s': %s\n", name, type, domain,
 
1040
            avahi_strerror(avahi_server_errno(mc.server)));
1016
1041
    break;
1017
1042
    
1018
1043
  case AVAHI_RESOLVER_FOUND:
1020
1045
      char ip[AVAHI_ADDRESS_STR_MAX];
1021
1046
      avahi_address_snprint(ip, sizeof(ip), address);
1022
1047
      if(debug){
1023
 
        fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
1024
 
                     PRIdMAX ") on port %" PRIu16 "\n", name,
1025
 
                     host_name, ip, (intmax_t)interface, port);
 
1048
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1049
                "Mandos server \"%s\" found on %s (%s, %"
 
1050
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
 
1051
                ip, (intmax_t)interface, port);
1026
1052
      }
1027
1053
      int ret = start_mandos_communication(ip, port, interface,
1028
1054
                                           avahi_proto_to_af(proto));
1060
1086
  default:
1061
1087
  case AVAHI_BROWSER_FAILURE:
1062
1088
    
1063
 
    fprintf_plus(stderr, "(Avahi browser) %s\n",
1064
 
                 avahi_strerror(avahi_server_errno(mc.server)));
 
1089
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1090
            "(Avahi browser) %s\n",
 
1091
            avahi_strerror(avahi_server_errno(mc.server)));
1065
1092
    avahi_simple_poll_quit(mc.simple_poll);
1066
1093
    return;
1067
1094
    
1074
1101
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1075
1102
                                    name, type, domain, protocol, 0,
1076
1103
                                    resolve_callback, NULL) == NULL)
1077
 
      fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1078
 
                   " %s\n", name,
1079
 
                   avahi_strerror(avahi_server_errno(mc.server)));
 
1104
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1105
              "Avahi: Failed to resolve service '%s': %s\n",
 
1106
              name, avahi_strerror(avahi_server_errno(mc.server)));
1080
1107
    break;
1081
1108
    
1082
1109
  case AVAHI_BROWSER_REMOVE:
1085
1112
  case AVAHI_BROWSER_ALL_FOR_NOW:
1086
1113
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1087
1114
    if(debug){
1088
 
      fprintf_plus(stderr, "No Mandos server found, still"
1089
 
                   " searching...\n");
 
1115
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1116
              "No Mandos server found, still searching...\n");
1090
1117
    }
1091
1118
    break;
1092
1119
  }
1131
1158
  /* Reject the loopback device */
1132
1159
  if(ifr->ifr_flags & IFF_LOOPBACK){
1133
1160
    if(debug){
1134
 
      fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1135
 
                   ifname);
 
1161
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1162
              "Rejecting loopback interface \"%s\"\n", ifname);
1136
1163
    }
1137
1164
    return false;
1138
1165
  }
1139
1166
  /* Accept point-to-point devices only if connect_to is specified */
1140
1167
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1141
1168
    if(debug){
1142
 
      fprintf_plus(stderr, "Accepting point-to-point interface"
1143
 
                   " \"%s\"\n", ifname);
 
1169
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1170
              "Accepting point-to-point interface \"%s\"\n", ifname);
1144
1171
    }
1145
1172
    return true;
1146
1173
  }
1147
1174
  /* Otherwise, reject non-broadcast-capable devices */
1148
1175
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1149
1176
    if(debug){
1150
 
      fprintf_plus(stderr, "Rejecting non-broadcast interface"
1151
 
                   " \"%s\"\n", ifname);
 
1177
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1178
              "Rejecting non-broadcast interface \"%s\"\n", ifname);
1152
1179
    }
1153
1180
    return false;
1154
1181
  }
1155
1182
  /* Reject non-ARP interfaces (including dummy interfaces) */
1156
1183
  if(ifr->ifr_flags & IFF_NOARP){
1157
1184
    if(debug){
1158
 
      fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1159
 
                   ifname);
 
1185
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1186
              "Rejecting non-ARP interface \"%s\"\n", ifname);
1160
1187
    }
1161
1188
    return false;
1162
1189
  }
1163
1190
  
1164
1191
  /* Accept this device */
1165
1192
  if(debug){
1166
 
    fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
 
1193
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1194
            "Interface \"%s\" is good\n", ifname);
1167
1195
  }
1168
1196
  return true;
1169
1197
}
1181
1209
  struct ifreq ifr;
1182
1210
  if(not get_flags(if_entry->d_name, &ifr)){
1183
1211
    if(debug){
1184
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1185
 
                   "\"%s\"\n", if_entry->d_name);
 
1212
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1213
              "Failed to get flags for interface \"%s\"\n",
 
1214
              if_entry->d_name);
1186
1215
    }
1187
1216
    return 0;
1188
1217
  }
1206
1235
  struct ifreq ifr;
1207
1236
  if(not get_flags(if_entry->d_name, &ifr)){
1208
1237
    if(debug){
1209
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1210
 
                   "\"%s\"\n", if_entry->d_name);
 
1238
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1239
              "Failed to get flags for interface \"%s\"\n",
 
1240
              if_entry->d_name);
1211
1241
    }
1212
1242
    return 0;
1213
1243
  }
1215
1245
  /* Reject down interfaces */
1216
1246
  if(not (ifr.ifr_flags & IFF_UP)){
1217
1247
    if(debug){
1218
 
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1219
 
                   if_entry->d_name);
 
1248
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1249
              "Rejecting down interface \"%s\"\n",
 
1250
              if_entry->d_name);
1220
1251
    }
1221
1252
    return 0;
1222
1253
  }
1224
1255
  /* Reject non-running interfaces */
1225
1256
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1226
1257
    if(debug){
1227
 
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1228
 
                   if_entry->d_name);
 
1258
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1259
              "Rejecting non-running interface \"%s\"\n",
 
1260
              if_entry->d_name);
1229
1261
    }
1230
1262
    return 0;
1231
1263
  }
1250
1282
/* Is this directory entry a runnable program? */
1251
1283
int runnable_hook(const struct dirent *direntry){
1252
1284
  int ret;
1253
 
  size_t sret;
1254
1285
  struct stat st;
1255
1286
  
1256
1287
  if((direntry->d_name)[0] == '\0'){
1258
1289
    return 0;
1259
1290
  }
1260
1291
  
1261
 
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1262
 
                "abcdefghijklmnopqrstuvwxyz"
1263
 
                "0123456789"
1264
 
                "_-");
1265
 
  if((direntry->d_name)[sret] != '\0'){
1266
 
    /* Contains non-allowed characters */
1267
 
    if(debug){
1268
 
      fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1269
 
                   direntry->d_name);
1270
 
    }
1271
 
    return 0;
1272
 
  }
 
1292
  /* Save pointer to last character */
 
1293
  char *end = strchr(direntry->d_name, '\0')-1;
 
1294
  
 
1295
  if(*end == '~'){
 
1296
    /* Backup name~ */
 
1297
    return 0;
 
1298
  }
 
1299
  
 
1300
  if(((direntry->d_name)[0] == '#')
 
1301
     and (*end == '#')){
 
1302
    /* Temporary #name# */
 
1303
    return 0;
 
1304
  }
 
1305
  
 
1306
  /* XXX more rules here */
1273
1307
  
1274
1308
  char *fullname = NULL;
1275
 
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1309
  ret = asprintf(&fullname, "%s/%s", hookdir,
 
1310
                 direntry->d_name);
1276
1311
  if(ret < 0){
1277
1312
    perror_plus("asprintf");
1278
1313
    return 0;
1281
1316
  ret = stat(fullname, &st);
1282
1317
  if(ret == -1){
1283
1318
    if(debug){
1284
 
      perror_plus("Could not stat hook");
 
1319
      perror_plus("Could not stat plugin");
1285
1320
    }
1286
1321
    return 0;
1287
1322
  }
1288
1323
  if(not (S_ISREG(st.st_mode))){
1289
1324
    /* Not a regular file */
1290
 
    if(debug){
1291
 
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1292
 
                   direntry->d_name);
1293
 
    }
1294
1325
    return 0;
1295
1326
  }
1296
1327
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1297
1328
    /* Not executable */
1298
 
    if(debug){
1299
 
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1300
 
                   direntry->d_name);
1301
 
    }
1302
1329
    return 0;
1303
1330
  }
1304
1331
  return 1;
1313
1340
  while(true){
1314
1341
    if(mc.current_server == NULL){
1315
1342
      if (debug){
1316
 
        fprintf_plus(stderr, "Wait until first server is found."
1317
 
                     " No timeout!\n");
 
1343
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1344
                "Wait until first server is found. No timeout!\n");
1318
1345
      }
1319
1346
      ret = avahi_simple_poll_iterate(s, -1);
1320
1347
    } else {
1321
1348
      if (debug){
1322
 
        fprintf_plus(stderr, "Check current_server if we should run"
1323
 
                     " it, or wait\n");
 
1349
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1350
                "Check current_server if we should run it,"
 
1351
                " or wait\n");
1324
1352
      }
1325
1353
      /* the current time */
1326
1354
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1342
1370
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1343
1371
      
1344
1372
      if (debug){
1345
 
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1346
 
                     block_time);
 
1373
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1374
                "Blocking for %" PRIdMAX " ms\n", block_time);
1347
1375
      }
1348
1376
      
1349
1377
      if(block_time <= 0){
1376
1404
  }
1377
1405
}
1378
1406
 
1379
 
bool run_network_hooks(const char *mode, const char *interface,
1380
 
                       const float delay){
1381
 
  struct dirent **direntries;
1382
 
  struct dirent *direntry;
1383
 
  int ret;
1384
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1385
 
                         alphasort);
1386
 
  if(numhooks == -1){
1387
 
    perror_plus("scandir");
1388
 
  } else {
1389
 
    int devnull = open("/dev/null", O_RDONLY);
1390
 
    for(int i = 0; i < numhooks; i++){
1391
 
      direntry = direntries[0];
1392
 
      char *fullname = NULL;
1393
 
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1394
 
      if(ret < 0){
1395
 
        perror_plus("asprintf");
1396
 
        continue;
1397
 
      }
1398
 
      pid_t hook_pid = fork();
1399
 
      if(hook_pid == 0){
1400
 
        /* Child */
1401
 
        dup2(devnull, STDIN_FILENO);
1402
 
        close(devnull);
1403
 
        dup2(STDERR_FILENO, STDOUT_FILENO);
1404
 
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1405
 
        if(ret == -1){
1406
 
          perror_plus("setenv");
1407
 
          return false;
1408
 
        }
1409
 
        ret = setenv("DEVICE", interface, 1);
1410
 
        if(ret == -1){
1411
 
          perror_plus("setenv");
1412
 
          return false;
1413
 
        }
1414
 
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1415
 
        if(ret == -1){
1416
 
          perror_plus("setenv");
1417
 
          return false;
1418
 
        }
1419
 
        ret = setenv("MODE", mode, 1);
1420
 
        if(ret == -1){
1421
 
          perror_plus("setenv");
1422
 
          return false;
1423
 
        }
1424
 
        char *delaystring;
1425
 
        ret = asprintf(&delaystring, "%f", delay);
1426
 
        if(ret == -1){
1427
 
          perror_plus("asprintf");
1428
 
          return false;
1429
 
        }
1430
 
        ret = setenv("DELAY", delaystring, 1);
1431
 
        if(ret == -1){
1432
 
          free(delaystring);
1433
 
          perror_plus("setenv");
1434
 
          return false;
1435
 
        }
1436
 
        free(delaystring);
1437
 
        ret = execl(fullname, direntry->d_name, mode, NULL);
1438
 
        perror_plus("execl");
1439
 
      } else {
1440
 
        int status;
1441
 
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1442
 
          perror_plus("waitpid");
1443
 
          free(fullname);
1444
 
          continue;
1445
 
        }
1446
 
        if(WIFEXITED(status)){
1447
 
          if(WEXITSTATUS(status) != 0){
1448
 
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1449
 
                         " with status %d\n", direntry->d_name,
1450
 
                         WEXITSTATUS(status));
1451
 
            free(fullname);
1452
 
            continue;
1453
 
          }
1454
 
        } else if(WIFSIGNALED(status)){
1455
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1456
 
                       " signal %d\n", direntry->d_name,
1457
 
                       WTERMSIG(status));
1458
 
          free(fullname);
1459
 
          continue;
1460
 
        } else {
1461
 
          fprintf_plus(stderr, "Warning: network hook \"%s\""
1462
 
                       " crashed\n", direntry->d_name);
1463
 
          free(fullname);
1464
 
          continue;
1465
 
        }
1466
 
      }
1467
 
      free(fullname);
1468
 
      if(quit_now){
1469
 
        break;
1470
 
      }
1471
 
    }
1472
 
    close(devnull);
1473
 
  }
1474
 
  return true;
1475
 
}
1476
 
 
1477
1407
int main(int argc, char *argv[]){
1478
1408
  AvahiSServiceBrowser *sb = NULL;
1479
1409
  int error;
1637
1567
        argp_state_help(state, state->out_stream,
1638
1568
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1639
1569
      case 'V':                 /* --version */
1640
 
        fprintf_plus(state->out_stream,
1641
 
                     "Mandos plugin mandos-client: ");
1642
 
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
 
1570
        fprintf(state->out_stream, "Mandos plugin mandos-client: ");
 
1571
        fprintf(state->out_stream, "%s\n", argp_program_version);
1643
1572
        exit(argp_err_exit_status);
1644
1573
        break;
1645
1574
      default:
1690
1619
        if(ret == -1){
1691
1620
          perror_plus("fstat");
1692
1621
        } else {
1693
 
          if(S_ISREG(st.st_mode)
1694
 
             and st.st_uid == 0 and st.st_gid == 0){
 
1622
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1695
1623
            ret = fchown(seckey_fd, uid, gid);
1696
1624
            if(ret == -1){
1697
1625
              perror_plus("fchown");
1711
1639
        if(ret == -1){
1712
1640
          perror_plus("fstat");
1713
1641
        } else {
1714
 
          if(S_ISREG(st.st_mode)
1715
 
             and st.st_uid == 0 and st.st_gid == 0){
 
1642
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1716
1643
            ret = fchown(pubkey_fd, uid, gid);
1717
1644
            if(ret == -1){
1718
1645
              perror_plus("fchown");
1731
1658
    }
1732
1659
  }
1733
1660
  
1734
 
  /* Run network hooks */
 
1661
  /* Find network hooks and run them */
1735
1662
  {
1736
 
    /* Re-raise priviliges */
1737
 
    errno = 0;
1738
 
    ret = seteuid(0);
1739
 
    if(ret == -1){
1740
 
      perror_plus("seteuid");
1741
 
    }
1742
 
    if(not run_network_hooks("start", interface, delay)){
1743
 
      goto end;
1744
 
    }
1745
 
    /* Lower privileges */
1746
 
    errno = 0;
1747
 
    ret = seteuid(uid);
1748
 
    if(ret == -1){
1749
 
      perror_plus("seteuid");
 
1663
    struct dirent **direntries;
 
1664
    struct dirent *direntry;
 
1665
    int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1666
                           alphasort);
 
1667
    if(numhooks == -1){
 
1668
      perror_plus("scandir");
 
1669
    } else {
 
1670
      int devnull = open("/dev/null", O_RDONLY);
 
1671
      for(int i = 0; i < numhooks; i++){
 
1672
        direntry = direntries[0];
 
1673
        char *fullname = NULL;
 
1674
        ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1675
        if(ret < 0){
 
1676
          perror_plus("asprintf");
 
1677
          continue;
 
1678
        }
 
1679
        pid_t hook_pid = fork();
 
1680
        if(hook_pid == 0){
 
1681
          /* Child */
 
1682
          dup2(devnull, STDIN_FILENO);
 
1683
          close(devnull);
 
1684
          dup2(STDERR_FILENO, STDOUT_FILENO);
 
1685
          ret = setenv("DEVICE", interface, 1);
 
1686
          if(ret == -1){
 
1687
            perror_plus("setenv");
 
1688
            exit(1);
 
1689
          }
 
1690
          ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1691
          if(ret == -1){
 
1692
            perror_plus("setenv");
 
1693
            exit(1);
 
1694
          }
 
1695
          ret = setenv("MODE", "start", 1);
 
1696
          if(ret == -1){
 
1697
            perror_plus("setenv");
 
1698
            exit(1);
 
1699
          }
 
1700
          char *delaystring;
 
1701
          ret = asprintf(&delaystring, "%f", delay);
 
1702
          if(ret == -1){
 
1703
            perror_plus("asprintf");
 
1704
            exit(1);
 
1705
          }
 
1706
          ret = setenv("DELAY", delaystring, 1);
 
1707
          if(ret == -1){
 
1708
            free(delaystring);
 
1709
            perror_plus("setenv");
 
1710
            exit(1);
 
1711
          }
 
1712
          free(delaystring);
 
1713
          ret = execl(fullname, direntry->d_name, "start", NULL);
 
1714
          perror_plus("execl");
 
1715
        } else {
 
1716
          int status;
 
1717
          if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1718
            perror_plus("waitpid");
 
1719
            free(fullname);
 
1720
            continue;
 
1721
          }
 
1722
          if(WIFEXITED(status)){
 
1723
            if(WEXITSTATUS(status) != 0){
 
1724
              fprintf(stderr, "Mandos plugin mandos-client: "
 
1725
                      "Warning: network hook \"%s\" exited"
 
1726
                      " with status %d\n", direntry->d_name,
 
1727
                      WEXITSTATUS(status));
 
1728
              free(fullname);
 
1729
              continue;
 
1730
            }
 
1731
          } else if(WIFSIGNALED(status)){
 
1732
            fprintf(stderr, "Mandos plugin mandos-client: "
 
1733
                    "Warning: network hook \"%s\" died by"
 
1734
                    " signal %d\n", direntry->d_name,
 
1735
                    WTERMSIG(status));
 
1736
            free(fullname);
 
1737
            continue;
 
1738
          } else {
 
1739
            fprintf(stderr, "Mandos plugin mandos-client: "
 
1740
                    "Warning: network hook \"%s\" crashed\n",
 
1741
                    direntry->d_name);
 
1742
            free(fullname);
 
1743
            continue;
 
1744
          }
 
1745
        }
 
1746
        free(fullname);
 
1747
        if(quit_now){
 
1748
          goto end;
 
1749
        }
 
1750
      }
 
1751
      close(devnull);
1750
1752
    }
1751
1753
  }
1752
1754
  
1769
1771
      /* Pick the first interface returned */
1770
1772
      interface = strdup(direntries[0]->d_name);
1771
1773
      if(debug){
1772
 
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
 
1774
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1775
                "Using interface \"%s\"\n", interface);
1773
1776
      }
1774
1777
      if(interface == NULL){
1775
1778
        perror_plus("malloc");
1780
1783
      free(direntries);
1781
1784
    } else {
1782
1785
      free(direntries);
1783
 
      fprintf_plus(stderr, "Could not find a network interface\n");
 
1786
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1787
              "Could not find a network interface\n");
1784
1788
      exitcode = EXIT_FAILURE;
1785
1789
      goto end;
1786
1790
    }
1792
1796
  srand((unsigned int) time(NULL));
1793
1797
  mc.simple_poll = avahi_simple_poll_new();
1794
1798
  if(mc.simple_poll == NULL){
1795
 
    fprintf_plus(stderr,
1796
 
                 "Avahi: Failed to create simple poll object.\n");
 
1799
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1800
            "Avahi: Failed to create simple poll object.\n");
1797
1801
    exitcode = EX_UNAVAILABLE;
1798
1802
    goto end;
1799
1803
  }
1865
1869
  if(strcmp(interface, "none") != 0){
1866
1870
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1867
1871
    if(if_index == 0){
1868
 
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1872
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1873
              "No such interface: \"%s\"\n", interface);
1869
1874
      exitcode = EX_UNAVAILABLE;
1870
1875
      goto end;
1871
1876
    }
1991
1996
#endif  /* __linux__ */
1992
1997
    /* Lower privileges */
1993
1998
    errno = 0;
1994
 
    /* Lower privileges */
1995
 
    ret = seteuid(uid);
1996
 
    if(ret == -1){
1997
 
      perror_plus("seteuid");
 
1999
    if(take_down_interface){
 
2000
      /* Lower privileges */
 
2001
      ret = seteuid(uid);
 
2002
      if(ret == -1){
 
2003
        perror_plus("seteuid");
 
2004
      }
 
2005
    } else {
 
2006
      /* Lower privileges permanently */
 
2007
      ret = setuid(uid);
 
2008
      if(ret == -1){
 
2009
        perror_plus("setuid");
 
2010
      }
1998
2011
    }
1999
2012
  }
2000
2013
  
2004
2017
  
2005
2018
  ret = init_gnutls_global(pubkey, seckey);
2006
2019
  if(ret == -1){
2007
 
    fprintf_plus(stderr, "init_gnutls_global failed\n");
 
2020
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2021
            "init_gnutls_global failed\n");
2008
2022
    exitcode = EX_UNAVAILABLE;
2009
2023
    goto end;
2010
2024
  } else {
2026
2040
  }
2027
2041
  
2028
2042
  if(not init_gpgme(pubkey, seckey, tempdir)){
2029
 
    fprintf_plus(stderr, "init_gpgme failed\n");
 
2043
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2044
            "init_gpgme failed\n");
2030
2045
    exitcode = EX_UNAVAILABLE;
2031
2046
    goto end;
2032
2047
  } else {
2042
2057
    /* (Mainly meant for debugging) */
2043
2058
    char *address = strrchr(connect_to, ':');
2044
2059
    if(address == NULL){
2045
 
      fprintf_plus(stderr, "No colon in address\n");
 
2060
      fprintf(stderr, "Mandos plugin mandos-client: "
 
2061
              "No colon in address\n");
2046
2062
      exitcode = EX_USAGE;
2047
2063
      goto end;
2048
2064
    }
2056
2072
    tmpmax = strtoimax(address+1, &tmp, 10);
2057
2073
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2058
2074
       or tmpmax != (uint16_t)tmpmax){
2059
 
      fprintf_plus(stderr, "Bad port number\n");
 
2075
      fprintf(stderr, "Mandos plugin mandos-client: "
 
2076
              "Bad port number\n");
2060
2077
      exitcode = EX_USAGE;
2061
2078
      goto end;
2062
2079
    }
2092
2109
        break;
2093
2110
      }
2094
2111
      if(debug){
2095
 
        fprintf_plus(stderr, "Retrying in %d seconds\n",
2096
 
                     (int)retry_interval);
 
2112
        fprintf(stderr, "Mandos plugin mandos-client: "
 
2113
                "Retrying in %d seconds\n", (int)retry_interval);
2097
2114
      }
2098
2115
      sleep((int)retry_interval);
2099
2116
    }
2129
2146
  
2130
2147
  /* Check if creating the Avahi server object succeeded */
2131
2148
  if(mc.server == NULL){
2132
 
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2133
 
                 avahi_strerror(error));
 
2149
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2150
            "Failed to create Avahi server: %s\n",
 
2151
            avahi_strerror(error));
2134
2152
    exitcode = EX_UNAVAILABLE;
2135
2153
    goto end;
2136
2154
  }
2144
2162
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2145
2163
                                   NULL, 0, browse_callback, NULL);
2146
2164
  if(sb == NULL){
2147
 
    fprintf_plus(stderr, "Failed to create service browser: %s\n",
2148
 
                 avahi_strerror(avahi_server_errno(mc.server)));
 
2165
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2166
            "Failed to create service browser: %s\n",
 
2167
            avahi_strerror(avahi_server_errno(mc.server)));
2149
2168
    exitcode = EX_UNAVAILABLE;
2150
2169
    goto end;
2151
2170
  }
2157
2176
  /* Run the main loop */
2158
2177
  
2159
2178
  if(debug){
2160
 
    fprintf_plus(stderr, "Starting Avahi loop search\n");
 
2179
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2180
            "Starting Avahi loop search\n");
2161
2181
  }
2162
2182
 
2163
2183
  ret = avahi_loop_with_timeout(mc.simple_poll,
2164
2184
                                (int)(retry_interval * 1000));
2165
2185
  if(debug){
2166
 
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2167
 
                 (ret == 0) ? "successfully" : "with error");
 
2186
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2187
            "avahi_loop_with_timeout exited %s\n",
 
2188
            (ret == 0) ? "successfully" : "with error");
2168
2189
  }
2169
2190
  
2170
2191
 end:
2171
2192
  
2172
2193
  if(debug){
2173
 
    fprintf_plus(stderr, "%s exiting\n", argv[0]);
 
2194
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2195
            "%s exiting\n", argv[0]);
2174
2196
  }
2175
2197
  
2176
2198
  /* Cleanup things */
2204
2226
    }
2205
2227
  }
2206
2228
  
2207
 
  /* Re-raise priviliges */
2208
 
  {
 
2229
  /* XXX run network hooks "stop" here  */
 
2230
  
 
2231
  /* Take down the network interface */
 
2232
  if(take_down_interface){
 
2233
    /* Re-raise priviliges */
2209
2234
    errno = 0;
2210
2235
    ret = seteuid(0);
2211
2236
    if(ret == -1){
2212
2237
      perror_plus("seteuid");
2213
2238
    }
2214
 
    /* Run network hooks */
2215
 
    if(not run_network_hooks("stop", interface, delay)){
2216
 
      goto end;
2217
 
    }
2218
 
    
2219
 
    /* Take down the network interface */
2220
 
    if(take_down_interface and geteuid() == 0){
 
2239
    if(geteuid() == 0){
2221
2240
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2222
2241
      if(ret == -1){
2223
2242
        perror_plus("ioctl SIOCGIFFLAGS");
2232
2251
      if(ret == -1){
2233
2252
        perror_plus("close");
2234
2253
      }
 
2254
      /* Lower privileges permanently */
 
2255
      errno = 0;
 
2256
      ret = setuid(uid);
 
2257
      if(ret == -1){
 
2258
        perror_plus("setuid");
 
2259
      }
2235
2260
    }
2236
2261
  }
2237
 
  /* Lower privileges permanently */
2238
 
  errno = 0;
2239
 
  ret = setuid(uid);
2240
 
  if(ret == -1){
2241
 
    perror_plus("setuid");
2242
 
  }
2243
2262
  
2244
2263
  /* Removes the GPGME temp directory and all files inside */
2245
2264
  if(tempdir_created){
2259
2278
        }
2260
2279
        ret = remove(fullname);
2261
2280
        if(ret == -1){
2262
 
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2263
 
                       strerror(errno));
 
2281
          fprintf(stderr, "Mandos plugin mandos-client: "
 
2282
                  "remove(\"%s\"): %s\n", fullname, strerror(errno));
2264
2283
        }
2265
2284
        free(fullname);
2266
2285
      }