/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

* initramfs-tools-hook: Install network hooks (and any required files)
                        in the initramfs image.
* plugins.d/mandos-client.c (main): Also set MANDOSNETHOOKDIR for
                                    network hooks.
* plugins.d/mandos-client.xml (OPTIONS): Use <filename
                                         class="directory">, not
                                         <directory>.

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