/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: Some white space fixes.
  (runnable_hook): Simplify name rule.  More debug messages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
                                   sockaddr_in6, PF_INET6,
54
54
                                   SOCK_STREAM, uid_t, gid_t, open(),
55
55
                                   opendir(), DIR */
56
 
#include <sys/stat.h>           /* open() */
 
56
#include <sys/stat.h>           /* open(), S_ISREG */
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
58
                                   inet_pton(), connect() */
59
59
#include <fcntl.h>              /* open() */
85
85
                                   raise() */
86
86
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
87
87
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
 
88
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
 
89
                                   WEXITSTATUS(), WTERMSIG() */
88
90
 
89
91
#ifdef __linux__
90
92
#include <sys/klog.h>           /* klogctl() */
108
110
                                   init_gnutls_session(),
109
111
                                   GNUTLS_* */
110
112
#include <gnutls/openpgp.h>
111
 
                          /* gnutls_certificate_set_openpgp_key_file(),
112
 
                                   GNUTLS_OPENPGP_FMT_BASE64 */
 
113
                         /* gnutls_certificate_set_openpgp_key_file(),
 
114
                            GNUTLS_OPENPGP_FMT_BASE64 */
113
115
 
114
116
/* GPGME */
115
117
#include <gpgme.h>              /* All GPGME types, constants and
123
125
#define PATHDIR "/conf/conf.d/mandos"
124
126
#define SECKEY "seckey.txt"
125
127
#define PUBKEY "pubkey.txt"
 
128
#define HOOKDIR "/lib/mandos/network-hooks.d"
126
129
 
127
130
bool debug = false;
128
131
static const char mandos_protocol_version[] = "1";
130
133
const char *argp_program_bug_address = "<mandos@recompile.se>";
131
134
static const char sys_class_net[] = "/sys/class/net";
132
135
char *connect_to = NULL;
 
136
const char *hookdir = HOOKDIR;
133
137
 
134
138
/* Doubly linked list that need to be circularly linked when used */
135
139
typedef struct server{
176
180
 * "buffer_length" is how much is already used.
177
181
 */
178
182
size_t incbuffer(char **buffer, size_t buffer_length,
179
 
                  size_t buffer_capacity){
 
183
                 size_t buffer_capacity){
180
184
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
181
185
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
182
186
    if(buffer == NULL){
188
192
}
189
193
 
190
194
/* Add server to set of servers to retry periodically */
191
 
int add_server(const char *ip, uint16_t port,
192
 
                 AvahiIfIndex if_index,
193
 
                 int af){
 
195
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
196
               int af){
194
197
  int ret;
195
198
  server *new_server = malloc(sizeof(server));
196
199
  if(new_server == NULL){
198
201
    return -1;
199
202
  }
200
203
  *new_server = (server){ .ip = strdup(ip),
201
 
                         .port = port,
202
 
                         .if_index = if_index,
203
 
                         .af = af };
 
204
                          .port = port,
 
205
                          .if_index = if_index,
 
206
                          .af = af };
204
207
  if(new_server->ip == NULL){
205
208
    perror_plus("strdup");
206
209
    return -1;
228
231
/* 
229
232
 * Initialize GPGME.
230
233
 */
231
 
static bool init_gpgme(const char *seckey,
232
 
                       const char *pubkey, const char *tempdir){
 
234
static bool init_gpgme(const char *seckey, const char *pubkey,
 
235
                       const char *tempdir){
233
236
  gpgme_error_t rc;
234
237
  gpgme_engine_info_t engine_info;
235
238
  
250
253
    
251
254
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
252
255
    if(rc != GPG_ERR_NO_ERROR){
253
 
      fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
 
256
      fprintf(stderr, "Mandos plugin mandos-client: "
 
257
              "bad gpgme_data_new_from_fd: %s: %s\n",
254
258
              gpgme_strsource(rc), gpgme_strerror(rc));
255
259
      return false;
256
260
    }
257
261
    
258
262
    rc = gpgme_op_import(mc.ctx, pgp_data);
259
263
    if(rc != GPG_ERR_NO_ERROR){
260
 
      fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
 
264
      fprintf(stderr, "Mandos plugin mandos-client: "
 
265
              "bad gpgme_op_import: %s: %s\n",
261
266
              gpgme_strsource(rc), gpgme_strerror(rc));
262
267
      return false;
263
268
    }
271
276
  }
272
277
  
273
278
  if(debug){
274
 
    fprintf(stderr, "Initializing GPGME\n");
 
279
    fprintf(stderr, "Mandos plugin mandos-client: "
 
280
            "Initializing GPGME\n");
275
281
  }
276
282
  
277
283
  /* Init GPGME */
278
284
  gpgme_check_version(NULL);
279
285
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
280
286
  if(rc != GPG_ERR_NO_ERROR){
281
 
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
 
287
    fprintf(stderr, "Mandos plugin mandos-client: "
 
288
            "bad gpgme_engine_check_version: %s: %s\n",
282
289
            gpgme_strsource(rc), gpgme_strerror(rc));
283
290
    return false;
284
291
  }
286
293
  /* Set GPGME home directory for the OpenPGP engine only */
287
294
  rc = gpgme_get_engine_info(&engine_info);
288
295
  if(rc != GPG_ERR_NO_ERROR){
289
 
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
 
296
    fprintf(stderr, "Mandos plugin mandos-client: "
 
297
            "bad gpgme_get_engine_info: %s: %s\n",
290
298
            gpgme_strsource(rc), gpgme_strerror(rc));
291
299
    return false;
292
300
  }
299
307
    engine_info = engine_info->next;
300
308
  }
301
309
  if(engine_info == NULL){
302
 
    fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
 
310
    fprintf(stderr, "Mandos plugin mandos-client: "
 
311
            "Could not set GPGME home dir to %s\n", tempdir);
303
312
    return false;
304
313
  }
305
314
  
306
315
  /* Create new GPGME "context" */
307
316
  rc = gpgme_new(&(mc.ctx));
308
317
  if(rc != GPG_ERR_NO_ERROR){
309
 
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
310
 
            gpgme_strsource(rc), gpgme_strerror(rc));
 
318
    fprintf(stderr, "Mandos plugin mandos-client: "
 
319
            "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
 
320
            gpgme_strerror(rc));
311
321
    return false;
312
322
  }
313
323
  
332
342
  ssize_t plaintext_length = 0;
333
343
  
334
344
  if(debug){
335
 
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
 
345
    fprintf(stderr, "Mandos plugin mandos-client: "
 
346
            "Trying to decrypt OpenPGP data\n");
336
347
  }
337
348
  
338
349
  /* Create new GPGME data buffer from memory cryptotext */
339
350
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
340
351
                               0);
341
352
  if(rc != GPG_ERR_NO_ERROR){
342
 
    fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
 
353
    fprintf(stderr, "Mandos plugin mandos-client: "
 
354
            "bad gpgme_data_new_from_mem: %s: %s\n",
343
355
            gpgme_strsource(rc), gpgme_strerror(rc));
344
356
    return -1;
345
357
  }
347
359
  /* Create new empty GPGME data buffer for the plaintext */
348
360
  rc = gpgme_data_new(&dh_plain);
349
361
  if(rc != GPG_ERR_NO_ERROR){
350
 
    fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
 
362
    fprintf(stderr, "Mandos plugin mandos-client: "
 
363
            "bad gpgme_data_new: %s: %s\n",
351
364
            gpgme_strsource(rc), gpgme_strerror(rc));
352
365
    gpgme_data_release(dh_crypto);
353
366
    return -1;
357
370
     data buffer */
358
371
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
359
372
  if(rc != GPG_ERR_NO_ERROR){
360
 
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
 
373
    fprintf(stderr, "Mandos plugin mandos-client: "
 
374
            "bad gpgme_op_decrypt: %s: %s\n",
361
375
            gpgme_strsource(rc), gpgme_strerror(rc));
362
376
    plaintext_length = -1;
363
377
    if(debug){
364
378
      gpgme_decrypt_result_t result;
365
379
      result = gpgme_op_decrypt_result(mc.ctx);
366
380
      if(result == NULL){
367
 
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
 
381
        fprintf(stderr, "Mandos plugin mandos-client: "
 
382
                "gpgme_op_decrypt_result failed\n");
368
383
      } else {
369
 
        fprintf(stderr, "Unsupported algorithm: %s\n",
 
384
        fprintf(stderr, "Mandos plugin mandos-client: "
 
385
                "Unsupported algorithm: %s\n",
370
386
                result->unsupported_algorithm);
371
 
        fprintf(stderr, "Wrong key usage: %u\n",
 
387
        fprintf(stderr, "Mandos plugin mandos-client: "
 
388
                "Wrong key usage: %u\n",
372
389
                result->wrong_key_usage);
373
390
        if(result->file_name != NULL){
374
 
          fprintf(stderr, "File name: %s\n", result->file_name);
 
391
          fprintf(stderr, "Mandos plugin mandos-client: "
 
392
                  "File name: %s\n", result->file_name);
375
393
        }
376
394
        gpgme_recipient_t recipient;
377
395
        recipient = result->recipients;
378
396
        while(recipient != NULL){
379
 
          fprintf(stderr, "Public key algorithm: %s\n",
 
397
          fprintf(stderr, "Mandos plugin mandos-client: "
 
398
                  "Public key algorithm: %s\n",
380
399
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
381
 
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
382
 
          fprintf(stderr, "Secret key available: %s\n",
 
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",
383
404
                  recipient->status == GPG_ERR_NO_SECKEY
384
405
                  ? "No" : "Yes");
385
406
          recipient = recipient->next;
390
411
  }
391
412
  
392
413
  if(debug){
393
 
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
 
414
    fprintf(stderr, "Mandos plugin mandos-client: "
 
415
            "Decryption of OpenPGP data succeeded\n");
394
416
  }
395
417
  
396
418
  /* Seek back to the beginning of the GPGME plaintext data buffer */
403
425
  *plaintext = NULL;
404
426
  while(true){
405
427
    plaintext_capacity = incbuffer(plaintext,
406
 
                                      (size_t)plaintext_length,
407
 
                                      plaintext_capacity);
 
428
                                   (size_t)plaintext_length,
 
429
                                   plaintext_capacity);
408
430
    if(plaintext_capacity == 0){
409
 
        perror_plus("incbuffer");
410
 
        plaintext_length = -1;
411
 
        goto decrypt_end;
 
431
      perror_plus("incbuffer");
 
432
      plaintext_length = -1;
 
433
      goto decrypt_end;
412
434
    }
413
435
    
414
436
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
427
449
  }
428
450
  
429
451
  if(debug){
430
 
    fprintf(stderr, "Decrypted password is: ");
 
452
    fprintf(stderr, "Mandos plugin mandos-client: "
 
453
            "Decrypted password is: ");
431
454
    for(ssize_t i = 0; i < plaintext_length; i++){
432
455
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
433
456
    }
455
478
/* GnuTLS log function callback */
456
479
static void debuggnutls(__attribute__((unused)) int level,
457
480
                        const char* string){
458
 
  fprintf(stderr, "GnuTLS: %s", string);
 
481
  fprintf(stderr, "Mandos plugin mandos-client: GnuTLS: %s", string);
459
482
}
460
483
 
461
484
static int init_gnutls_global(const char *pubkeyfilename,
463
486
  int ret;
464
487
  
465
488
  if(debug){
466
 
    fprintf(stderr, "Initializing GnuTLS\n");
 
489
    fprintf(stderr, "Mandos plugin mandos-client: "
 
490
            "Initializing GnuTLS\n");
467
491
  }
468
492
  
469
493
  ret = gnutls_global_init();
470
494
  if(ret != GNUTLS_E_SUCCESS){
471
 
    fprintf(stderr, "GnuTLS global_init: %s\n",
472
 
            safer_gnutls_strerror(ret));
 
495
    fprintf(stderr, "Mandos plugin mandos-client: "
 
496
            "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
473
497
    return -1;
474
498
  }
475
499
  
484
508
  /* OpenPGP credentials */
485
509
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
486
510
  if(ret != GNUTLS_E_SUCCESS){
487
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
488
 
            safer_gnutls_strerror(ret));
 
511
    fprintf(stderr, "Mandos plugin mandos-client: "
 
512
            "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
489
513
    gnutls_global_deinit();
490
514
    return -1;
491
515
  }
492
516
  
493
517
  if(debug){
494
 
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
 
518
    fprintf(stderr, "Mandos plugin mandos-client: "
 
519
            "Attempting to use OpenPGP public key %s and"
495
520
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
496
521
            seckeyfilename);
497
522
  }
501
526
     GNUTLS_OPENPGP_FMT_BASE64);
502
527
  if(ret != GNUTLS_E_SUCCESS){
503
528
    fprintf(stderr,
 
529
            "Mandos plugin mandos-client: "
504
530
            "Error[%d] while reading the OpenPGP key pair ('%s',"
505
531
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
506
 
    fprintf(stderr, "The GnuTLS error is: %s\n",
507
 
            safer_gnutls_strerror(ret));
 
532
    fprintf(stderr, "Mandos plugin mandos-client: "
 
533
            "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
508
534
    goto globalfail;
509
535
  }
510
536
  
511
537
  /* GnuTLS server initialization */
512
538
  ret = gnutls_dh_params_init(&mc.dh_params);
513
539
  if(ret != GNUTLS_E_SUCCESS){
514
 
    fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
 
540
    fprintf(stderr, "Mandos plugin mandos-client: "
 
541
            "Error in GnuTLS DH parameter initialization:"
515
542
            " %s\n", safer_gnutls_strerror(ret));
516
543
    goto globalfail;
517
544
  }
518
545
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
519
546
  if(ret != GNUTLS_E_SUCCESS){
520
 
    fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
 
547
    fprintf(stderr, "Mandos plugin mandos-client: "
 
548
            "Error in GnuTLS prime generation: %s\n",
521
549
            safer_gnutls_strerror(ret));
522
550
    goto globalfail;
523
551
  }
544
572
    }
545
573
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
546
574
  if(ret != GNUTLS_E_SUCCESS){
547
 
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
 
575
    fprintf(stderr, "Mandos plugin mandos-client: "
 
576
            "Error in GnuTLS session initialization: %s\n",
548
577
            safer_gnutls_strerror(ret));
549
578
  }
550
579
  
558
587
      }
559
588
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
560
589
    if(ret != GNUTLS_E_SUCCESS){
561
 
      fprintf(stderr, "Syntax error at: %s\n", err);
562
 
      fprintf(stderr, "GnuTLS error: %s\n",
563
 
              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));
564
594
      gnutls_deinit(*session);
565
595
      return -1;
566
596
    }
575
605
    }
576
606
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
577
607
  if(ret != GNUTLS_E_SUCCESS){
578
 
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
 
608
    fprintf(stderr, "Mandos plugin mandos-client: "
 
609
            "Error setting GnuTLS credentials: %s\n",
579
610
            safer_gnutls_strerror(ret));
580
611
    gnutls_deinit(*session);
581
612
    return -1;
627
658
    pf = PF_INET;
628
659
    break;
629
660
  default:
630
 
    fprintf(stderr, "Bad address family: %d\n", af);
 
661
    fprintf(stderr, "Mandos plugin mandos-client: "
 
662
            "Bad address family: %d\n", af);
631
663
    errno = EINVAL;
632
664
    return -1;
633
665
  }
638
670
  }
639
671
  
640
672
  if(debug){
641
 
    fprintf(stderr, "Setting up a TCP connection to %s, port %" PRIu16
 
673
    fprintf(stderr, "Mandos plugin mandos-client: "
 
674
            "Setting up a TCP connection to %s, port %" PRIu16
642
675
            "\n", ip, port);
643
676
  }
644
677
  
671
704
  }
672
705
  if(ret == 0){
673
706
    int e = errno;
674
 
    fprintf(stderr, "Bad address: %s\n", ip);
 
707
    fprintf(stderr, "Mandos plugin mandos-client: "
 
708
            "Bad address: %s\n", ip);
675
709
    errno = e;
676
710
    goto mandos_end;
677
711
  }
682
716
    
683
717
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
684
718
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
685
 
                              -Wunreachable-code*/
 
719
                                -Wunreachable-code*/
686
720
      if(if_index == AVAHI_IF_UNSPEC){
687
 
        fprintf(stderr, "An IPv6 link-local address is incomplete"
 
721
        fprintf(stderr, "Mandos plugin mandos-client: "
 
722
                "An IPv6 link-local address is incomplete"
688
723
                " without a network interface\n");
689
724
        errno = EINVAL;
690
725
        goto mandos_end;
709
744
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
710
745
        perror_plus("if_indextoname");
711
746
      } else {
712
 
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
 
747
        fprintf(stderr, "Mandos plugin mandos-client: "
 
748
                "Connection to: %s%%%s, port %" PRIu16 "\n",
713
749
                ip, interface, port);
714
750
      }
715
751
    } else {
716
 
      fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
717
 
              port);
 
752
      fprintf(stderr, "Mandos plugin mandos-client: "
 
753
              "Connection to: %s, port %" PRIu16 "\n", ip, port);
718
754
    }
719
755
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
720
756
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
730
766
      perror_plus("inet_ntop");
731
767
    } else {
732
768
      if(strcmp(addrstr, ip) != 0){
733
 
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
 
769
        fprintf(stderr, "Mandos plugin mandos-client: "
 
770
                "Canonical address form: %s\n", addrstr);
734
771
      }
735
772
    }
736
773
  }
764
801
  while(true){
765
802
    size_t out_size = strlen(out);
766
803
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
767
 
                                   out_size - written));
 
804
                                        out_size - written));
768
805
    if(ret == -1){
769
806
      int e = errno;
770
807
      perror_plus("write");
790
827
  }
791
828
  
792
829
  if(debug){
793
 
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
 
830
    fprintf(stderr, "Mandos plugin mandos-client: "
 
831
            "Establishing TLS session with %s\n", ip);
794
832
  }
795
833
  
796
834
  if(quit_now){
816
854
  
817
855
  if(ret != GNUTLS_E_SUCCESS){
818
856
    if(debug){
819
 
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
 
857
      fprintf(stderr, "Mandos plugin mandos-client: "
 
858
              "*** GnuTLS Handshake failed ***\n");
820
859
      gnutls_perror(ret);
821
860
    }
822
861
    errno = EPROTO;
826
865
  /* Read OpenPGP packet that contains the wanted password */
827
866
  
828
867
  if(debug){
829
 
    fprintf(stderr, "Retrieving OpenPGP encrypted password from %s\n",
830
 
            ip);
 
868
    fprintf(stderr, "Mandos plugin mandos-client: "
 
869
            "Retrieving OpenPGP encrypted password from %s\n", ip);
831
870
  }
832
871
  
833
872
  while(true){
838
877
    }
839
878
    
840
879
    buffer_capacity = incbuffer(&buffer, buffer_length,
841
 
                                   buffer_capacity);
 
880
                                buffer_capacity);
842
881
    if(buffer_capacity == 0){
843
882
      int e = errno;
844
883
      perror_plus("incbuffer");
871
910
          }
872
911
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
873
912
        if(ret < 0){
874
 
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
 
913
          fprintf(stderr, "Mandos plugin mandos-client: "
 
914
                  "*** GnuTLS Re-handshake failed ***\n");
875
915
          gnutls_perror(ret);
876
916
          errno = EPROTO;
877
917
          goto mandos_end;
878
918
        }
879
919
        break;
880
920
      default:
881
 
        fprintf(stderr, "Unknown error while reading data from"
 
921
        fprintf(stderr, "Mandos plugin mandos-client: "
 
922
                "Unknown error while reading data from"
882
923
                " encrypted session with Mandos server\n");
883
924
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
884
925
        errno = EIO;
890
931
  }
891
932
  
892
933
  if(debug){
893
 
    fprintf(stderr, "Closing TLS session\n");
 
934
    fprintf(stderr, "Mandos plugin mandos-client: "
 
935
            "Closing TLS session\n");
894
936
  }
895
937
  
896
938
  if(quit_now){
908
950
  
909
951
  if(buffer_length > 0){
910
952
    ssize_t decrypted_buffer_size;
911
 
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
912
 
                                               buffer_length,
 
953
    decrypted_buffer_size = pgp_packet_decrypt(buffer, buffer_length,
913
954
                                               &decrypted_buffer);
914
955
    if(decrypted_buffer_size >= 0){
915
956
      
926
967
        if(ret == 0 and ferror(stdout)){
927
968
          int e = errno;
928
969
          if(debug){
929
 
            fprintf(stderr, "Error writing encrypted data: %s\n",
 
970
            fprintf(stderr, "Mandos plugin mandos-client: "
 
971
                    "Error writing encrypted data: %s\n",
930
972
                    strerror(errno));
931
973
          }
932
974
          errno = e;
990
1032
  switch(event){
991
1033
  default:
992
1034
  case AVAHI_RESOLVER_FAILURE:
993
 
    fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
 
1035
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1036
            "(Avahi Resolver) Failed to resolve service '%s'"
994
1037
            " of type '%s' in domain '%s': %s\n", name, type, domain,
995
1038
            avahi_strerror(avahi_server_errno(mc.server)));
996
1039
    break;
1000
1043
      char ip[AVAHI_ADDRESS_STR_MAX];
1001
1044
      avahi_address_snprint(ip, sizeof(ip), address);
1002
1045
      if(debug){
1003
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
 
1046
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1047
                "Mandos server \"%s\" found on %s (%s, %"
1004
1048
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1005
1049
                ip, (intmax_t)interface, port);
1006
1050
      }
1040
1084
  default:
1041
1085
  case AVAHI_BROWSER_FAILURE:
1042
1086
    
1043
 
    fprintf(stderr, "(Avahi browser) %s\n",
 
1087
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1088
            "(Avahi browser) %s\n",
1044
1089
            avahi_strerror(avahi_server_errno(mc.server)));
1045
1090
    avahi_simple_poll_quit(mc.simple_poll);
1046
1091
    return;
1054
1099
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1055
1100
                                    name, type, domain, protocol, 0,
1056
1101
                                    resolve_callback, NULL) == NULL)
1057
 
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
 
1102
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1103
              "Avahi: Failed to resolve service '%s': %s\n",
1058
1104
              name, avahi_strerror(avahi_server_errno(mc.server)));
1059
1105
    break;
1060
1106
    
1064
1110
  case AVAHI_BROWSER_ALL_FOR_NOW:
1065
1111
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1066
1112
    if(debug){
1067
 
      fprintf(stderr, "No Mandos server found, still searching...\n");
 
1113
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1114
              "No Mandos server found, still searching...\n");
1068
1115
    }
1069
1116
    break;
1070
1117
  }
1085
1132
  errno = old_errno;
1086
1133
}
1087
1134
 
1088
 
/* 
1089
 
 * This function determines if a directory entry in /sys/class/net
1090
 
 * corresponds to an acceptable network device.
1091
 
 * (This function is passed to scandir(3) as a filter function.)
1092
 
 */
1093
 
int good_interface(const struct dirent *if_entry){
1094
 
  ssize_t ssret;
1095
 
  char *flagname = NULL;
1096
 
  if(if_entry->d_name[0] == '.'){
1097
 
    return 0;
1098
 
  }
1099
 
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1100
 
                     if_entry->d_name);
1101
 
  if(ret < 0){
1102
 
    perror_plus("asprintf");
1103
 
    return 0;
1104
 
  }
1105
 
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1106
 
  if(flags_fd == -1){
1107
 
    perror_plus("open");
1108
 
    free(flagname);
1109
 
    return 0;
1110
 
  }
1111
 
  free(flagname);
1112
 
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1113
 
  /* read line from flags_fd */
1114
 
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1115
 
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1116
 
  flagstring[(size_t)to_read] = '\0';
1117
 
  if(flagstring == NULL){
1118
 
    perror_plus("malloc");
1119
 
    close(flags_fd);
1120
 
    return 0;
1121
 
  }
1122
 
  while(to_read > 0){
1123
 
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1124
 
                                             (size_t)to_read));
1125
 
    if(ssret == -1){
1126
 
      perror_plus("read");
1127
 
      free(flagstring);
1128
 
      close(flags_fd);
1129
 
      return 0;
1130
 
    }
1131
 
    to_read -= ssret;
1132
 
    if(ssret == 0){
1133
 
      break;
1134
 
    }
1135
 
  }
1136
 
  close(flags_fd);
1137
 
  intmax_t tmpmax;
1138
 
  char *tmp;
1139
 
  errno = 0;
1140
 
  tmpmax = strtoimax(flagstring, &tmp, 0);
1141
 
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1142
 
                                         and not (isspace(*tmp)))
1143
 
     or tmpmax != (ifreq_flags)tmpmax){
 
1135
bool get_flags(const char *ifname, struct ifreq *ifr){
 
1136
  int ret;
 
1137
  
 
1138
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1139
  if(s < 0){
 
1140
    perror_plus("socket");
 
1141
    return false;
 
1142
  }
 
1143
  strcpy(ifr->ifr_name, ifname);
 
1144
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
 
1145
  if(ret == -1){
1144
1146
    if(debug){
1145
 
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1146
 
              flagstring, if_entry->d_name);
 
1147
      perror_plus("ioctl SIOCGIFFLAGS");
1147
1148
    }
1148
 
    free(flagstring);
1149
 
    return 0;
 
1149
    return false;
1150
1150
  }
1151
 
  free(flagstring);
1152
 
  ifreq_flags flags = (ifreq_flags)tmpmax;
 
1151
  return true;
 
1152
}
 
1153
 
 
1154
bool good_flags(const char *ifname, const struct ifreq *ifr){
 
1155
  
1153
1156
  /* Reject the loopback device */
1154
 
  if(flags & IFF_LOOPBACK){
 
1157
  if(ifr->ifr_flags & IFF_LOOPBACK){
1155
1158
    if(debug){
1156
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1157
 
              if_entry->d_name);
 
1159
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1160
              "Rejecting loopback interface \"%s\"\n", ifname);
1158
1161
    }
1159
 
    return 0;
 
1162
    return false;
1160
1163
  }
1161
1164
  /* Accept point-to-point devices only if connect_to is specified */
1162
 
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
 
1165
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1163
1166
    if(debug){
1164
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1165
 
              if_entry->d_name);
 
1167
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1168
              "Accepting point-to-point interface \"%s\"\n", ifname);
1166
1169
    }
1167
 
    return 1;
 
1170
    return true;
1168
1171
  }
1169
1172
  /* Otherwise, reject non-broadcast-capable devices */
1170
 
  if(not (flags & IFF_BROADCAST)){
 
1173
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1171
1174
    if(debug){
1172
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1173
 
              if_entry->d_name);
 
1175
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1176
              "Rejecting non-broadcast interface \"%s\"\n", ifname);
1174
1177
    }
1175
 
    return 0;
 
1178
    return false;
1176
1179
  }
1177
1180
  /* Reject non-ARP interfaces (including dummy interfaces) */
1178
 
  if(flags & IFF_NOARP){
 
1181
  if(ifr->ifr_flags & IFF_NOARP){
1179
1182
    if(debug){
1180
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1181
 
              if_entry->d_name);
 
1183
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1184
              "Rejecting non-ARP interface \"%s\"\n", ifname);
1182
1185
    }
1183
 
    return 0;
 
1186
    return false;
1184
1187
  }
 
1188
  
1185
1189
  /* Accept this device */
1186
1190
  if(debug){
1187
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1188
 
            if_entry->d_name);
 
1191
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1192
            "Interface \"%s\" is good\n", ifname);
 
1193
  }
 
1194
  return true;
 
1195
}
 
1196
 
 
1197
/* 
 
1198
 * This function determines if a directory entry in /sys/class/net
 
1199
 * corresponds to an acceptable network device.
 
1200
 * (This function is passed to scandir(3) as a filter function.)
 
1201
 */
 
1202
int good_interface(const struct dirent *if_entry){
 
1203
  if(if_entry->d_name[0] == '.'){
 
1204
    return 0;
 
1205
  }
 
1206
  
 
1207
  struct ifreq ifr;
 
1208
  if(not get_flags(if_entry->d_name, &ifr)){
 
1209
    if(debug){
 
1210
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1211
              "Failed to get flags for interface \"%s\"\n",
 
1212
              if_entry->d_name);
 
1213
    }
 
1214
    return 0;
 
1215
  }
 
1216
  
 
1217
  if(not good_flags(if_entry->d_name, &ifr)){
 
1218
    return 0;
 
1219
  }
 
1220
  return 1;
 
1221
}
 
1222
 
 
1223
/* 
 
1224
 * This function determines if a directory entry in /sys/class/net
 
1225
 * corresponds to an acceptable network device which is up.
 
1226
 * (This function is passed to scandir(3) as a filter function.)
 
1227
 */
 
1228
int up_interface(const struct dirent *if_entry){
 
1229
  if(if_entry->d_name[0] == '.'){
 
1230
    return 0;
 
1231
  }
 
1232
  
 
1233
  struct ifreq ifr;
 
1234
  if(not get_flags(if_entry->d_name, &ifr)){
 
1235
    if(debug){
 
1236
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1237
              "Failed to get flags for interface \"%s\"\n",
 
1238
              if_entry->d_name);
 
1239
    }
 
1240
    return 0;
 
1241
  }
 
1242
  
 
1243
  /* Reject down interfaces */
 
1244
  if(not (ifr.ifr_flags & IFF_UP)){
 
1245
    if(debug){
 
1246
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1247
              "Rejecting down interface \"%s\"\n",
 
1248
              if_entry->d_name);
 
1249
    }
 
1250
    return 0;
 
1251
  }
 
1252
  
 
1253
  /* Reject non-running interfaces */
 
1254
  if(not (ifr.ifr_flags & IFF_RUNNING)){
 
1255
    if(debug){
 
1256
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1257
              "Rejecting non-running interface \"%s\"\n",
 
1258
              if_entry->d_name);
 
1259
    }
 
1260
    return 0;
 
1261
  }
 
1262
  
 
1263
  if(not good_flags(if_entry->d_name, &ifr)){
 
1264
    return 0;
1189
1265
  }
1190
1266
  return 1;
1191
1267
}
1201
1277
  return 1;
1202
1278
}
1203
1279
 
 
1280
/* Is this directory entry a runnable program? */
 
1281
int runnable_hook(const struct dirent *direntry){
 
1282
  int ret;
 
1283
  size_t sret;
 
1284
  struct stat st;
 
1285
  
 
1286
  if((direntry->d_name)[0] == '\0'){
 
1287
    /* Empty name? */
 
1288
    return 0;
 
1289
  }
 
1290
  
 
1291
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
1292
                "abcdefghijklmnopqrstuvwxyz"
 
1293
                "0123456789"
 
1294
                "_-");
 
1295
  if((direntry->d_name)[sret] != '\0'){
 
1296
    /* Contains non-allowed characters */
 
1297
    if(debug){
 
1298
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1299
              "Ignoring hook \"%s\" with bad name\n",
 
1300
              direntry->d_name);
 
1301
    }
 
1302
    return 0;
 
1303
  }
 
1304
  
 
1305
  char *fullname = NULL;
 
1306
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1307
  if(ret < 0){
 
1308
    perror_plus("asprintf");
 
1309
    return 0;
 
1310
  }
 
1311
  
 
1312
  ret = stat(fullname, &st);
 
1313
  if(ret == -1){
 
1314
    if(debug){
 
1315
      perror_plus("Could not stat hook");
 
1316
    }
 
1317
    return 0;
 
1318
  }
 
1319
  if(not (S_ISREG(st.st_mode))){
 
1320
    /* Not a regular file */
 
1321
    if(debug){
 
1322
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1323
              "Ignoring hook \"%s\" - not a file\n",
 
1324
              direntry->d_name);
 
1325
    }
 
1326
    return 0;
 
1327
  }
 
1328
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
 
1329
    /* Not executable */
 
1330
    if(debug){
 
1331
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1332
              "Ignoring hook \"%s\" - not executable\n",
 
1333
              direntry->d_name);
 
1334
    }
 
1335
    return 0;
 
1336
  }
 
1337
  return 1;
 
1338
}
 
1339
 
1204
1340
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1205
1341
  int ret;
1206
1342
  struct timespec now;
1210
1346
  while(true){
1211
1347
    if(mc.current_server == NULL){
1212
1348
      if (debug){
1213
 
        fprintf(stderr,
 
1349
        fprintf(stderr, "Mandos plugin mandos-client: "
1214
1350
                "Wait until first server is found. No timeout!\n");
1215
1351
      }
1216
1352
      ret = avahi_simple_poll_iterate(s, -1);
1217
1353
    } else {
1218
1354
      if (debug){
1219
 
        fprintf(stderr, "Check current_server if we should run it,"
 
1355
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1356
                "Check current_server if we should run it,"
1220
1357
                " or wait\n");
1221
1358
      }
1222
1359
      /* the current time */
1239
1376
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1240
1377
      
1241
1378
      if (debug){
1242
 
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
 
1379
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1380
                "Blocking for %" PRIdMAX " ms\n", block_time);
1243
1381
      }
1244
1382
      
1245
1383
      if(block_time <= 0){
1265
1403
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1266
1404
    }
1267
1405
    if(ret != 0){
1268
 
      if (ret > 0 or errno != EINTR) {
 
1406
      if (ret > 0 or errno != EINTR){
1269
1407
        return (ret != 1) ? ret : 0;
1270
1408
      }
1271
1409
    }
1359
1497
        .arg = "SECONDS",
1360
1498
        .doc = "Retry interval used when denied by the mandos server",
1361
1499
        .group = 2 },
 
1500
      { .name = "network-hook-dir", .key = 133,
 
1501
        .arg = "DIR",
 
1502
        .doc = "Directory where network hooks are located",
 
1503
        .group = 2 },
1362
1504
      /*
1363
1505
       * These reproduce what we would get without ARGP_NO_HELP
1364
1506
       */
1417
1559
          argp_error(state, "Bad retry interval");
1418
1560
        }
1419
1561
        break;
 
1562
      case 133:                 /* --network-hook-dir */
 
1563
        hookdir = arg;
 
1564
        break;
1420
1565
        /*
1421
1566
         * These reproduce what we would get without ARGP_NO_HELP
1422
1567
         */
1428
1573
        argp_state_help(state, state->out_stream,
1429
1574
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1430
1575
      case 'V':                 /* --version */
 
1576
        fprintf(state->out_stream, "Mandos plugin mandos-client: ");
1431
1577
        fprintf(state->out_stream, "%s\n", argp_program_version);
1432
1578
        exit(argp_err_exit_status);
1433
1579
        break;
1518
1664
    }
1519
1665
  }
1520
1666
  
 
1667
  /* Find network hooks and run them */
 
1668
  {
 
1669
    struct dirent **direntries;
 
1670
    struct dirent *direntry;
 
1671
    int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1672
                           alphasort);
 
1673
    if(numhooks == -1){
 
1674
      perror_plus("scandir");
 
1675
    } else {
 
1676
      int devnull = open("/dev/null", O_RDONLY);
 
1677
      for(int i = 0; i < numhooks; i++){
 
1678
        direntry = direntries[0];
 
1679
        char *fullname = NULL;
 
1680
        ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1681
        if(ret < 0){
 
1682
          perror_plus("asprintf");
 
1683
          continue;
 
1684
        }
 
1685
        pid_t hook_pid = fork();
 
1686
        if(hook_pid == 0){
 
1687
          /* Child */
 
1688
          dup2(devnull, STDIN_FILENO);
 
1689
          close(devnull);
 
1690
          dup2(STDERR_FILENO, STDOUT_FILENO);
 
1691
          ret = setenv("DEVICE", interface, 1);
 
1692
          if(ret == -1){
 
1693
            perror_plus("setenv");
 
1694
            exit(1);
 
1695
          }
 
1696
          ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1697
          if(ret == -1){
 
1698
            perror_plus("setenv");
 
1699
            exit(1);
 
1700
          }
 
1701
          ret = setenv("MODE", "start", 1);
 
1702
          if(ret == -1){
 
1703
            perror_plus("setenv");
 
1704
            exit(1);
 
1705
          }
 
1706
          char *delaystring;
 
1707
          ret = asprintf(&delaystring, "%f", delay);
 
1708
          if(ret == -1){
 
1709
            perror_plus("asprintf");
 
1710
            exit(1);
 
1711
          }
 
1712
          ret = setenv("DELAY", delaystring, 1);
 
1713
          if(ret == -1){
 
1714
            free(delaystring);
 
1715
            perror_plus("setenv");
 
1716
            exit(1);
 
1717
          }
 
1718
          free(delaystring);
 
1719
          ret = execl(fullname, direntry->d_name, "start", NULL);
 
1720
          perror_plus("execl");
 
1721
        } else {
 
1722
          int status;
 
1723
          if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1724
            perror_plus("waitpid");
 
1725
            free(fullname);
 
1726
            continue;
 
1727
          }
 
1728
          if(WIFEXITED(status)){
 
1729
            if(WEXITSTATUS(status) != 0){
 
1730
              fprintf(stderr, "Mandos plugin mandos-client: "
 
1731
                      "Warning: network hook \"%s\" exited"
 
1732
                      " with status %d\n", direntry->d_name,
 
1733
                      WEXITSTATUS(status));
 
1734
              free(fullname);
 
1735
              continue;
 
1736
            }
 
1737
          } else if(WIFSIGNALED(status)){
 
1738
            fprintf(stderr, "Mandos plugin mandos-client: "
 
1739
                    "Warning: network hook \"%s\" died by"
 
1740
                    " signal %d\n", direntry->d_name,
 
1741
                    WTERMSIG(status));
 
1742
            free(fullname);
 
1743
            continue;
 
1744
          } else {
 
1745
            fprintf(stderr, "Mandos plugin mandos-client: "
 
1746
                    "Warning: network hook \"%s\" crashed\n",
 
1747
                    direntry->d_name);
 
1748
            free(fullname);
 
1749
            continue;
 
1750
          }
 
1751
        }
 
1752
        free(fullname);
 
1753
        if(quit_now){
 
1754
          goto end;
 
1755
        }
 
1756
      }
 
1757
      close(devnull);
 
1758
    }
 
1759
  }
 
1760
  
1521
1761
  if(not debug){
1522
1762
    avahi_set_log_function(empty_log);
1523
1763
  }
1524
 
 
 
1764
  
1525
1765
  if(interface[0] == '\0'){
1526
1766
    struct dirent **direntries;
1527
 
    ret = scandir(sys_class_net, &direntries, good_interface,
 
1767
    /* First look for interfaces that are up */
 
1768
    ret = scandir(sys_class_net, &direntries, up_interface,
1528
1769
                  alphasort);
 
1770
    if(ret == 0){
 
1771
      /* No up interfaces, look for any good interfaces */
 
1772
      free(direntries);
 
1773
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1774
                    alphasort);
 
1775
    }
1529
1776
    if(ret >= 1){
1530
 
      /* Pick the first good interface */
 
1777
      /* Pick the first interface returned */
1531
1778
      interface = strdup(direntries[0]->d_name);
1532
1779
      if(debug){
1533
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
 
1780
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1781
                "Using interface \"%s\"\n", interface);
1534
1782
      }
1535
1783
      if(interface == NULL){
1536
1784
        perror_plus("malloc");
1541
1789
      free(direntries);
1542
1790
    } else {
1543
1791
      free(direntries);
1544
 
      fprintf(stderr, "Could not find a network interface\n");
 
1792
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1793
              "Could not find a network interface\n");
1545
1794
      exitcode = EXIT_FAILURE;
1546
1795
      goto end;
1547
1796
    }
1553
1802
  srand((unsigned int) time(NULL));
1554
1803
  mc.simple_poll = avahi_simple_poll_new();
1555
1804
  if(mc.simple_poll == NULL){
1556
 
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
 
1805
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1806
            "Avahi: Failed to create simple poll object.\n");
1557
1807
    exitcode = EX_UNAVAILABLE;
1558
1808
    goto end;
1559
1809
  }
1625
1875
  if(strcmp(interface, "none") != 0){
1626
1876
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1627
1877
    if(if_index == 0){
1628
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1878
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1879
              "No such interface: \"%s\"\n", interface);
1629
1880
      exitcode = EX_UNAVAILABLE;
1630
1881
      goto end;
1631
1882
    }
1772
2023
  
1773
2024
  ret = init_gnutls_global(pubkey, seckey);
1774
2025
  if(ret == -1){
1775
 
    fprintf(stderr, "init_gnutls_global failed\n");
 
2026
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2027
            "init_gnutls_global failed\n");
1776
2028
    exitcode = EX_UNAVAILABLE;
1777
2029
    goto end;
1778
2030
  } else {
1794
2046
  }
1795
2047
  
1796
2048
  if(not init_gpgme(pubkey, seckey, tempdir)){
1797
 
    fprintf(stderr, "init_gpgme failed\n");
 
2049
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2050
            "init_gpgme failed\n");
1798
2051
    exitcode = EX_UNAVAILABLE;
1799
2052
    goto end;
1800
2053
  } else {
1810
2063
    /* (Mainly meant for debugging) */
1811
2064
    char *address = strrchr(connect_to, ':');
1812
2065
    if(address == NULL){
1813
 
      fprintf(stderr, "No colon in address\n");
 
2066
      fprintf(stderr, "Mandos plugin mandos-client: "
 
2067
              "No colon in address\n");
1814
2068
      exitcode = EX_USAGE;
1815
2069
      goto end;
1816
2070
    }
1824
2078
    tmpmax = strtoimax(address+1, &tmp, 10);
1825
2079
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1826
2080
       or tmpmax != (uint16_t)tmpmax){
1827
 
      fprintf(stderr, "Bad port number\n");
 
2081
      fprintf(stderr, "Mandos plugin mandos-client: "
 
2082
              "Bad port number\n");
1828
2083
      exitcode = EX_USAGE;
1829
2084
      goto end;
1830
2085
    }
1860
2115
        break;
1861
2116
      }
1862
2117
      if(debug){
1863
 
        fprintf(stderr, "Retrying in %d seconds\n",
1864
 
                (int)retry_interval);
 
2118
        fprintf(stderr, "Mandos plugin mandos-client: "
 
2119
                "Retrying in %d seconds\n", (int)retry_interval);
1865
2120
      }
1866
2121
      sleep((int)retry_interval);
1867
2122
    }
1869
2124
    if (not quit_now){
1870
2125
      exitcode = EXIT_SUCCESS;
1871
2126
    }
1872
 
 
 
2127
    
1873
2128
    goto end;
1874
2129
  }
1875
2130
  
1897
2152
  
1898
2153
  /* Check if creating the Avahi server object succeeded */
1899
2154
  if(mc.server == NULL){
1900
 
    fprintf(stderr, "Failed to create Avahi server: %s\n",
 
2155
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2156
            "Failed to create Avahi server: %s\n",
1901
2157
            avahi_strerror(error));
1902
2158
    exitcode = EX_UNAVAILABLE;
1903
2159
    goto end;
1912
2168
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1913
2169
                                   NULL, 0, browse_callback, NULL);
1914
2170
  if(sb == NULL){
1915
 
    fprintf(stderr, "Failed to create service browser: %s\n",
 
2171
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2172
            "Failed to create service browser: %s\n",
1916
2173
            avahi_strerror(avahi_server_errno(mc.server)));
1917
2174
    exitcode = EX_UNAVAILABLE;
1918
2175
    goto end;
1925
2182
  /* Run the main loop */
1926
2183
  
1927
2184
  if(debug){
1928
 
    fprintf(stderr, "Starting Avahi loop search\n");
 
2185
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2186
            "Starting Avahi loop search\n");
1929
2187
  }
1930
2188
 
1931
2189
  ret = avahi_loop_with_timeout(mc.simple_poll,
1932
2190
                                (int)(retry_interval * 1000));
1933
2191
  if(debug){
1934
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
 
2192
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2193
            "avahi_loop_with_timeout exited %s\n",
1935
2194
            (ret == 0) ? "successfully" : "with error");
1936
2195
  }
1937
2196
  
1938
2197
 end:
1939
2198
  
1940
2199
  if(debug){
1941
 
    fprintf(stderr, "%s exiting\n", argv[0]);
 
2200
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2201
            "%s exiting\n", argv[0]);
1942
2202
  }
1943
2203
  
1944
2204
  /* Cleanup things */
1972
2232
    }
1973
2233
  }
1974
2234
  
 
2235
  /* XXX run network hooks "stop" here  */
 
2236
  
1975
2237
  /* Take down the network interface */
1976
2238
  if(take_down_interface){
1977
2239
    /* Re-raise priviliges */
1984
2246
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1985
2247
      if(ret == -1){
1986
2248
        perror_plus("ioctl SIOCGIFFLAGS");
1987
 
      } else if(network.ifr_flags & IFF_UP) {
 
2249
      } else if(network.ifr_flags & IFF_UP){
1988
2250
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1989
2251
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1990
2252
        if(ret == -1){
2022
2284
        }
2023
2285
        ret = remove(fullname);
2024
2286
        if(ret == -1){
2025
 
          fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2026
 
                  strerror(errno));
 
2287
          fprintf(stderr, "Mandos plugin mandos-client: "
 
2288
                  "remove(\"%s\"): %s\n", fullname, strerror(errno));
2027
2289
        }
2028
2290
        free(fullname);
2029
2291
      }