/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

Removed superflous white space.

* Makefile: - '' -
* initramfs-tools-hook: - '' -
* mandos: - '' -
* plugin-runner.xml: - '' -

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(), S_ISREG */
 
56
#include <sys/stat.h>           /* open() */
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() */
90
88
 
91
89
#ifdef __linux__
92
90
#include <sys/klog.h>           /* klogctl() */
110
108
                                   init_gnutls_session(),
111
109
                                   GNUTLS_* */
112
110
#include <gnutls/openpgp.h>
113
 
                         /* gnutls_certificate_set_openpgp_key_file(),
114
 
                            GNUTLS_OPENPGP_FMT_BASE64 */
 
111
                          /* gnutls_certificate_set_openpgp_key_file(),
 
112
                                   GNUTLS_OPENPGP_FMT_BASE64 */
115
113
 
116
114
/* GPGME */
117
115
#include <gpgme.h>              /* All GPGME types, constants and
125
123
#define PATHDIR "/conf/conf.d/mandos"
126
124
#define SECKEY "seckey.txt"
127
125
#define PUBKEY "pubkey.txt"
128
 
#define HOOKDIR "/lib/mandos/network-hooks.d"
129
126
 
130
127
bool debug = false;
131
128
static const char mandos_protocol_version[] = "1";
133
130
const char *argp_program_bug_address = "<mandos@recompile.se>";
134
131
static const char sys_class_net[] = "/sys/class/net";
135
132
char *connect_to = NULL;
136
 
const char *hookdir = HOOKDIR;
137
133
 
138
134
/* Doubly linked list that need to be circularly linked when used */
139
135
typedef struct server{
180
176
 * "buffer_length" is how much is already used.
181
177
 */
182
178
size_t incbuffer(char **buffer, size_t buffer_length,
183
 
                 size_t buffer_capacity){
 
179
                  size_t buffer_capacity){
184
180
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
185
181
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
186
182
    if(buffer == NULL){
192
188
}
193
189
 
194
190
/* Add server to set of servers to retry periodically */
195
 
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
196
 
               int af){
 
191
int add_server(const char *ip, uint16_t port,
 
192
                 AvahiIfIndex if_index,
 
193
                 int af){
197
194
  int ret;
198
195
  server *new_server = malloc(sizeof(server));
199
196
  if(new_server == NULL){
201
198
    return -1;
202
199
  }
203
200
  *new_server = (server){ .ip = strdup(ip),
204
 
                          .port = port,
205
 
                          .if_index = if_index,
206
 
                          .af = af };
 
201
                         .port = port,
 
202
                         .if_index = if_index,
 
203
                         .af = af };
207
204
  if(new_server->ip == NULL){
208
205
    perror_plus("strdup");
209
206
    return -1;
231
228
/* 
232
229
 * Initialize GPGME.
233
230
 */
234
 
static bool init_gpgme(const char *seckey, const char *pubkey,
235
 
                       const char *tempdir){
 
231
static bool init_gpgme(const char *seckey,
 
232
                       const char *pubkey, const char *tempdir){
236
233
  gpgme_error_t rc;
237
234
  gpgme_engine_info_t engine_info;
238
235
  
253
250
    
254
251
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
255
252
    if(rc != GPG_ERR_NO_ERROR){
256
 
      fprintf(stderr, "Mandos plugin mandos-client: "
257
 
              "bad gpgme_data_new_from_fd: %s: %s\n",
 
253
      fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
258
254
              gpgme_strsource(rc), gpgme_strerror(rc));
259
255
      return false;
260
256
    }
261
257
    
262
258
    rc = gpgme_op_import(mc.ctx, pgp_data);
263
259
    if(rc != GPG_ERR_NO_ERROR){
264
 
      fprintf(stderr, "Mandos plugin mandos-client: "
265
 
              "bad gpgme_op_import: %s: %s\n",
 
260
      fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
266
261
              gpgme_strsource(rc), gpgme_strerror(rc));
267
262
      return false;
268
263
    }
276
271
  }
277
272
  
278
273
  if(debug){
279
 
    fprintf(stderr, "Mandos plugin mandos-client: "
280
 
            "Initializing GPGME\n");
 
274
    fprintf(stderr, "Initializing GPGME\n");
281
275
  }
282
276
  
283
277
  /* Init GPGME */
284
278
  gpgme_check_version(NULL);
285
279
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
286
280
  if(rc != GPG_ERR_NO_ERROR){
287
 
    fprintf(stderr, "Mandos plugin mandos-client: "
288
 
            "bad gpgme_engine_check_version: %s: %s\n",
 
281
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
289
282
            gpgme_strsource(rc), gpgme_strerror(rc));
290
283
    return false;
291
284
  }
293
286
  /* Set GPGME home directory for the OpenPGP engine only */
294
287
  rc = gpgme_get_engine_info(&engine_info);
295
288
  if(rc != GPG_ERR_NO_ERROR){
296
 
    fprintf(stderr, "Mandos plugin mandos-client: "
297
 
            "bad gpgme_get_engine_info: %s: %s\n",
 
289
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
298
290
            gpgme_strsource(rc), gpgme_strerror(rc));
299
291
    return false;
300
292
  }
307
299
    engine_info = engine_info->next;
308
300
  }
309
301
  if(engine_info == NULL){
310
 
    fprintf(stderr, "Mandos plugin mandos-client: "
311
 
            "Could not set GPGME home dir to %s\n", tempdir);
 
302
    fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
312
303
    return false;
313
304
  }
314
305
  
315
306
  /* Create new GPGME "context" */
316
307
  rc = gpgme_new(&(mc.ctx));
317
308
  if(rc != GPG_ERR_NO_ERROR){
318
 
    fprintf(stderr, "Mandos plugin mandos-client: "
319
 
            "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
320
 
            gpgme_strerror(rc));
 
309
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
 
310
            gpgme_strsource(rc), gpgme_strerror(rc));
321
311
    return false;
322
312
  }
323
313
  
342
332
  ssize_t plaintext_length = 0;
343
333
  
344
334
  if(debug){
345
 
    fprintf(stderr, "Mandos plugin mandos-client: "
346
 
            "Trying to decrypt OpenPGP data\n");
 
335
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
347
336
  }
348
337
  
349
338
  /* Create new GPGME data buffer from memory cryptotext */
350
339
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
351
340
                               0);
352
341
  if(rc != GPG_ERR_NO_ERROR){
353
 
    fprintf(stderr, "Mandos plugin mandos-client: "
354
 
            "bad gpgme_data_new_from_mem: %s: %s\n",
 
342
    fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
355
343
            gpgme_strsource(rc), gpgme_strerror(rc));
356
344
    return -1;
357
345
  }
359
347
  /* Create new empty GPGME data buffer for the plaintext */
360
348
  rc = gpgme_data_new(&dh_plain);
361
349
  if(rc != GPG_ERR_NO_ERROR){
362
 
    fprintf(stderr, "Mandos plugin mandos-client: "
363
 
            "bad gpgme_data_new: %s: %s\n",
 
350
    fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
364
351
            gpgme_strsource(rc), gpgme_strerror(rc));
365
352
    gpgme_data_release(dh_crypto);
366
353
    return -1;
370
357
     data buffer */
371
358
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
372
359
  if(rc != GPG_ERR_NO_ERROR){
373
 
    fprintf(stderr, "Mandos plugin mandos-client: "
374
 
            "bad gpgme_op_decrypt: %s: %s\n",
 
360
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
375
361
            gpgme_strsource(rc), gpgme_strerror(rc));
376
362
    plaintext_length = -1;
377
363
    if(debug){
378
364
      gpgme_decrypt_result_t result;
379
365
      result = gpgme_op_decrypt_result(mc.ctx);
380
366
      if(result == NULL){
381
 
        fprintf(stderr, "Mandos plugin mandos-client: "
382
 
                "gpgme_op_decrypt_result failed\n");
 
367
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
383
368
      } else {
384
 
        fprintf(stderr, "Mandos plugin mandos-client: "
385
 
                "Unsupported algorithm: %s\n",
 
369
        fprintf(stderr, "Unsupported algorithm: %s\n",
386
370
                result->unsupported_algorithm);
387
 
        fprintf(stderr, "Mandos plugin mandos-client: "
388
 
                "Wrong key usage: %u\n",
 
371
        fprintf(stderr, "Wrong key usage: %u\n",
389
372
                result->wrong_key_usage);
390
373
        if(result->file_name != NULL){
391
 
          fprintf(stderr, "Mandos plugin mandos-client: "
392
 
                  "File name: %s\n", result->file_name);
 
374
          fprintf(stderr, "File name: %s\n", result->file_name);
393
375
        }
394
376
        gpgme_recipient_t recipient;
395
377
        recipient = result->recipients;
396
378
        while(recipient != NULL){
397
 
          fprintf(stderr, "Mandos plugin mandos-client: "
398
 
                  "Public key algorithm: %s\n",
 
379
          fprintf(stderr, "Public key algorithm: %s\n",
399
380
                  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",
 
381
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
382
          fprintf(stderr, "Secret key available: %s\n",
404
383
                  recipient->status == GPG_ERR_NO_SECKEY
405
384
                  ? "No" : "Yes");
406
385
          recipient = recipient->next;
411
390
  }
412
391
  
413
392
  if(debug){
414
 
    fprintf(stderr, "Mandos plugin mandos-client: "
415
 
            "Decryption of OpenPGP data succeeded\n");
 
393
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
416
394
  }
417
395
  
418
396
  /* Seek back to the beginning of the GPGME plaintext data buffer */
425
403
  *plaintext = NULL;
426
404
  while(true){
427
405
    plaintext_capacity = incbuffer(plaintext,
428
 
                                   (size_t)plaintext_length,
429
 
                                   plaintext_capacity);
 
406
                                      (size_t)plaintext_length,
 
407
                                      plaintext_capacity);
430
408
    if(plaintext_capacity == 0){
431
 
      perror_plus("incbuffer");
432
 
      plaintext_length = -1;
433
 
      goto decrypt_end;
 
409
        perror_plus("incbuffer");
 
410
        plaintext_length = -1;
 
411
        goto decrypt_end;
434
412
    }
435
413
    
436
414
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
449
427
  }
450
428
  
451
429
  if(debug){
452
 
    fprintf(stderr, "Mandos plugin mandos-client: "
453
 
            "Decrypted password is: ");
 
430
    fprintf(stderr, "Decrypted password is: ");
454
431
    for(ssize_t i = 0; i < plaintext_length; i++){
455
432
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
456
433
    }
478
455
/* GnuTLS log function callback */
479
456
static void debuggnutls(__attribute__((unused)) int level,
480
457
                        const char* string){
481
 
  fprintf(stderr, "Mandos plugin mandos-client: GnuTLS: %s", string);
 
458
  fprintf(stderr, "GnuTLS: %s", string);
482
459
}
483
460
 
484
461
static int init_gnutls_global(const char *pubkeyfilename,
486
463
  int ret;
487
464
  
488
465
  if(debug){
489
 
    fprintf(stderr, "Mandos plugin mandos-client: "
490
 
            "Initializing GnuTLS\n");
 
466
    fprintf(stderr, "Initializing GnuTLS\n");
491
467
  }
492
468
  
493
469
  ret = gnutls_global_init();
494
470
  if(ret != GNUTLS_E_SUCCESS){
495
 
    fprintf(stderr, "Mandos plugin mandos-client: "
496
 
            "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
 
471
    fprintf(stderr, "GnuTLS global_init: %s\n",
 
472
            safer_gnutls_strerror(ret));
497
473
    return -1;
498
474
  }
499
475
  
508
484
  /* OpenPGP credentials */
509
485
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
510
486
  if(ret != GNUTLS_E_SUCCESS){
511
 
    fprintf(stderr, "Mandos plugin mandos-client: "
512
 
            "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
 
487
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
488
            safer_gnutls_strerror(ret));
513
489
    gnutls_global_deinit();
514
490
    return -1;
515
491
  }
516
492
  
517
493
  if(debug){
518
 
    fprintf(stderr, "Mandos plugin mandos-client: "
519
 
            "Attempting to use OpenPGP public key %s and"
 
494
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
520
495
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
521
496
            seckeyfilename);
522
497
  }
526
501
     GNUTLS_OPENPGP_FMT_BASE64);
527
502
  if(ret != GNUTLS_E_SUCCESS){
528
503
    fprintf(stderr,
529
 
            "Mandos plugin mandos-client: "
530
504
            "Error[%d] while reading the OpenPGP key pair ('%s',"
531
505
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
532
 
    fprintf(stderr, "Mandos plugin mandos-client: "
533
 
            "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
 
506
    fprintf(stderr, "The GnuTLS error is: %s\n",
 
507
            safer_gnutls_strerror(ret));
534
508
    goto globalfail;
535
509
  }
536
510
  
537
511
  /* GnuTLS server initialization */
538
512
  ret = gnutls_dh_params_init(&mc.dh_params);
539
513
  if(ret != GNUTLS_E_SUCCESS){
540
 
    fprintf(stderr, "Mandos plugin mandos-client: "
541
 
            "Error in GnuTLS DH parameter initialization:"
 
514
    fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
542
515
            " %s\n", safer_gnutls_strerror(ret));
543
516
    goto globalfail;
544
517
  }
545
518
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
546
519
  if(ret != GNUTLS_E_SUCCESS){
547
 
    fprintf(stderr, "Mandos plugin mandos-client: "
548
 
            "Error in GnuTLS prime generation: %s\n",
 
520
    fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
549
521
            safer_gnutls_strerror(ret));
550
522
    goto globalfail;
551
523
  }
572
544
    }
573
545
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
574
546
  if(ret != GNUTLS_E_SUCCESS){
575
 
    fprintf(stderr, "Mandos plugin mandos-client: "
576
 
            "Error in GnuTLS session initialization: %s\n",
 
547
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
577
548
            safer_gnutls_strerror(ret));
578
549
  }
579
550
  
587
558
      }
588
559
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
589
560
    if(ret != GNUTLS_E_SUCCESS){
590
 
      fprintf(stderr, "Mandos plugin mandos-client: "
591
 
              "Syntax error at: %s\n", err);
592
 
      fprintf(stderr, "Mandos plugin mandos-client: "
593
 
              "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
 
561
      fprintf(stderr, "Syntax error at: %s\n", err);
 
562
      fprintf(stderr, "GnuTLS error: %s\n",
 
563
              safer_gnutls_strerror(ret));
594
564
      gnutls_deinit(*session);
595
565
      return -1;
596
566
    }
605
575
    }
606
576
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
607
577
  if(ret != GNUTLS_E_SUCCESS){
608
 
    fprintf(stderr, "Mandos plugin mandos-client: "
609
 
            "Error setting GnuTLS credentials: %s\n",
 
578
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
610
579
            safer_gnutls_strerror(ret));
611
580
    gnutls_deinit(*session);
612
581
    return -1;
658
627
    pf = PF_INET;
659
628
    break;
660
629
  default:
661
 
    fprintf(stderr, "Mandos plugin mandos-client: "
662
 
            "Bad address family: %d\n", af);
 
630
    fprintf(stderr, "Bad address family: %d\n", af);
663
631
    errno = EINVAL;
664
632
    return -1;
665
633
  }
670
638
  }
671
639
  
672
640
  if(debug){
673
 
    fprintf(stderr, "Mandos plugin mandos-client: "
674
 
            "Setting up a TCP connection to %s, port %" PRIu16
 
641
    fprintf(stderr, "Setting up a TCP connection to %s, port %" PRIu16
675
642
            "\n", ip, port);
676
643
  }
677
644
  
704
671
  }
705
672
  if(ret == 0){
706
673
    int e = errno;
707
 
    fprintf(stderr, "Mandos plugin mandos-client: "
708
 
            "Bad address: %s\n", ip);
 
674
    fprintf(stderr, "Bad address: %s\n", ip);
709
675
    errno = e;
710
676
    goto mandos_end;
711
677
  }
716
682
    
717
683
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
718
684
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
719
 
                                -Wunreachable-code*/
 
685
                              -Wunreachable-code*/
720
686
      if(if_index == AVAHI_IF_UNSPEC){
721
 
        fprintf(stderr, "Mandos plugin mandos-client: "
722
 
                "An IPv6 link-local address is incomplete"
 
687
        fprintf(stderr, "An IPv6 link-local address is incomplete"
723
688
                " without a network interface\n");
724
689
        errno = EINVAL;
725
690
        goto mandos_end;
744
709
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
745
710
        perror_plus("if_indextoname");
746
711
      } else {
747
 
        fprintf(stderr, "Mandos plugin mandos-client: "
748
 
                "Connection to: %s%%%s, port %" PRIu16 "\n",
 
712
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
749
713
                ip, interface, port);
750
714
      }
751
715
    } else {
752
 
      fprintf(stderr, "Mandos plugin mandos-client: "
753
 
              "Connection to: %s, port %" PRIu16 "\n", ip, port);
 
716
      fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
 
717
              port);
754
718
    }
755
719
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
756
720
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
766
730
      perror_plus("inet_ntop");
767
731
    } else {
768
732
      if(strcmp(addrstr, ip) != 0){
769
 
        fprintf(stderr, "Mandos plugin mandos-client: "
770
 
                "Canonical address form: %s\n", addrstr);
 
733
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
771
734
      }
772
735
    }
773
736
  }
801
764
  while(true){
802
765
    size_t out_size = strlen(out);
803
766
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
804
 
                                        out_size - written));
 
767
                                   out_size - written));
805
768
    if(ret == -1){
806
769
      int e = errno;
807
770
      perror_plus("write");
827
790
  }
828
791
  
829
792
  if(debug){
830
 
    fprintf(stderr, "Mandos plugin mandos-client: "
831
 
            "Establishing TLS session with %s\n", ip);
 
793
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
832
794
  }
833
795
  
834
796
  if(quit_now){
854
816
  
855
817
  if(ret != GNUTLS_E_SUCCESS){
856
818
    if(debug){
857
 
      fprintf(stderr, "Mandos plugin mandos-client: "
858
 
              "*** GnuTLS Handshake failed ***\n");
 
819
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
859
820
      gnutls_perror(ret);
860
821
    }
861
822
    errno = EPROTO;
865
826
  /* Read OpenPGP packet that contains the wanted password */
866
827
  
867
828
  if(debug){
868
 
    fprintf(stderr, "Mandos plugin mandos-client: "
869
 
            "Retrieving OpenPGP encrypted password from %s\n", ip);
 
829
    fprintf(stderr, "Retrieving OpenPGP encrypted password from %s\n",
 
830
            ip);
870
831
  }
871
832
  
872
833
  while(true){
877
838
    }
878
839
    
879
840
    buffer_capacity = incbuffer(&buffer, buffer_length,
880
 
                                buffer_capacity);
 
841
                                   buffer_capacity);
881
842
    if(buffer_capacity == 0){
882
843
      int e = errno;
883
844
      perror_plus("incbuffer");
910
871
          }
911
872
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
912
873
        if(ret < 0){
913
 
          fprintf(stderr, "Mandos plugin mandos-client: "
914
 
                  "*** GnuTLS Re-handshake failed ***\n");
 
874
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
915
875
          gnutls_perror(ret);
916
876
          errno = EPROTO;
917
877
          goto mandos_end;
918
878
        }
919
879
        break;
920
880
      default:
921
 
        fprintf(stderr, "Mandos plugin mandos-client: "
922
 
                "Unknown error while reading data from"
 
881
        fprintf(stderr, "Unknown error while reading data from"
923
882
                " encrypted session with Mandos server\n");
924
883
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
925
884
        errno = EIO;
931
890
  }
932
891
  
933
892
  if(debug){
934
 
    fprintf(stderr, "Mandos plugin mandos-client: "
935
 
            "Closing TLS session\n");
 
893
    fprintf(stderr, "Closing TLS session\n");
936
894
  }
937
895
  
938
896
  if(quit_now){
950
908
  
951
909
  if(buffer_length > 0){
952
910
    ssize_t decrypted_buffer_size;
953
 
    decrypted_buffer_size = pgp_packet_decrypt(buffer, buffer_length,
 
911
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
 
912
                                               buffer_length,
954
913
                                               &decrypted_buffer);
955
914
    if(decrypted_buffer_size >= 0){
956
915
      
967
926
        if(ret == 0 and ferror(stdout)){
968
927
          int e = errno;
969
928
          if(debug){
970
 
            fprintf(stderr, "Mandos plugin mandos-client: "
971
 
                    "Error writing encrypted data: %s\n",
 
929
            fprintf(stderr, "Error writing encrypted data: %s\n",
972
930
                    strerror(errno));
973
931
          }
974
932
          errno = e;
1032
990
  switch(event){
1033
991
  default:
1034
992
  case AVAHI_RESOLVER_FAILURE:
1035
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1036
 
            "(Avahi Resolver) Failed to resolve service '%s'"
 
993
    fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
1037
994
            " of type '%s' in domain '%s': %s\n", name, type, domain,
1038
995
            avahi_strerror(avahi_server_errno(mc.server)));
1039
996
    break;
1043
1000
      char ip[AVAHI_ADDRESS_STR_MAX];
1044
1001
      avahi_address_snprint(ip, sizeof(ip), address);
1045
1002
      if(debug){
1046
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1047
 
                "Mandos server \"%s\" found on %s (%s, %"
 
1003
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
1048
1004
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1049
1005
                ip, (intmax_t)interface, port);
1050
1006
      }
1084
1040
  default:
1085
1041
  case AVAHI_BROWSER_FAILURE:
1086
1042
    
1087
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1088
 
            "(Avahi browser) %s\n",
 
1043
    fprintf(stderr, "(Avahi browser) %s\n",
1089
1044
            avahi_strerror(avahi_server_errno(mc.server)));
1090
1045
    avahi_simple_poll_quit(mc.simple_poll);
1091
1046
    return;
1099
1054
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1100
1055
                                    name, type, domain, protocol, 0,
1101
1056
                                    resolve_callback, NULL) == NULL)
1102
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1103
 
              "Avahi: Failed to resolve service '%s': %s\n",
 
1057
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
1104
1058
              name, avahi_strerror(avahi_server_errno(mc.server)));
1105
1059
    break;
1106
1060
    
1110
1064
  case AVAHI_BROWSER_ALL_FOR_NOW:
1111
1065
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1112
1066
    if(debug){
1113
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1114
 
              "No Mandos server found, still searching...\n");
 
1067
      fprintf(stderr, "No Mandos server found, still searching...\n");
1115
1068
    }
1116
1069
    break;
1117
1070
  }
1132
1085
  errno = old_errno;
1133
1086
}
1134
1087
 
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){
 
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){
1146
1144
    if(debug){
1147
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1145
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
 
1146
              flagstring, if_entry->d_name);
1148
1147
    }
1149
 
    return false;
 
1148
    free(flagstring);
 
1149
    return 0;
1150
1150
  }
1151
 
  return true;
1152
 
}
1153
 
 
1154
 
bool good_flags(const char *ifname, const struct ifreq *ifr){
1155
 
  
 
1151
  free(flagstring);
 
1152
  ifreq_flags flags = (ifreq_flags)tmpmax;
1156
1153
  /* Reject the loopback device */
1157
 
  if(ifr->ifr_flags & IFF_LOOPBACK){
 
1154
  if(flags & IFF_LOOPBACK){
1158
1155
    if(debug){
1159
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1160
 
              "Rejecting loopback interface \"%s\"\n", ifname);
 
1156
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
 
1157
              if_entry->d_name);
1161
1158
    }
1162
 
    return false;
 
1159
    return 0;
1163
1160
  }
1164
1161
  /* Accept point-to-point devices only if connect_to is specified */
1165
 
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
 
1162
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1166
1163
    if(debug){
1167
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1168
 
              "Accepting point-to-point interface \"%s\"\n", ifname);
 
1164
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
 
1165
              if_entry->d_name);
1169
1166
    }
1170
 
    return true;
 
1167
    return 1;
1171
1168
  }
1172
1169
  /* Otherwise, reject non-broadcast-capable devices */
1173
 
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
 
1170
  if(not (flags & IFF_BROADCAST)){
1174
1171
    if(debug){
1175
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1176
 
              "Rejecting non-broadcast interface \"%s\"\n", ifname);
 
1172
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
 
1173
              if_entry->d_name);
1177
1174
    }
1178
 
    return false;
 
1175
    return 0;
1179
1176
  }
1180
1177
  /* Reject non-ARP interfaces (including dummy interfaces) */
1181
 
  if(ifr->ifr_flags & IFF_NOARP){
 
1178
  if(flags & IFF_NOARP){
1182
1179
    if(debug){
1183
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1184
 
              "Rejecting non-ARP interface \"%s\"\n", ifname);
 
1180
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
 
1181
              if_entry->d_name);
1185
1182
    }
1186
 
    return false;
 
1183
    return 0;
1187
1184
  }
1188
 
  
1189
1185
  /* Accept this device */
1190
1186
  if(debug){
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;
 
1187
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
 
1188
            if_entry->d_name);
1265
1189
  }
1266
1190
  return 1;
1267
1191
}
1277
1201
  return 1;
1278
1202
}
1279
1203
 
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
 
 
1340
1204
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1341
1205
  int ret;
1342
1206
  struct timespec now;
1346
1210
  while(true){
1347
1211
    if(mc.current_server == NULL){
1348
1212
      if (debug){
1349
 
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1213
        fprintf(stderr,
1350
1214
                "Wait until first server is found. No timeout!\n");
1351
1215
      }
1352
1216
      ret = avahi_simple_poll_iterate(s, -1);
1353
1217
    } else {
1354
1218
      if (debug){
1355
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1356
 
                "Check current_server if we should run it,"
 
1219
        fprintf(stderr, "Check current_server if we should run it,"
1357
1220
                " or wait\n");
1358
1221
      }
1359
1222
      /* the current time */
1376
1239
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1377
1240
      
1378
1241
      if (debug){
1379
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1380
 
                "Blocking for %" PRIdMAX " ms\n", block_time);
 
1242
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1381
1243
      }
1382
1244
      
1383
1245
      if(block_time <= 0){
1403
1265
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1404
1266
    }
1405
1267
    if(ret != 0){
1406
 
      if (ret > 0 or errno != EINTR){
 
1268
      if (ret > 0 or errno != EINTR) {
1407
1269
        return (ret != 1) ? ret : 0;
1408
1270
      }
1409
1271
    }
1497
1359
        .arg = "SECONDS",
1498
1360
        .doc = "Retry interval used when denied by the mandos server",
1499
1361
        .group = 2 },
1500
 
      { .name = "network-hook-dir", .key = 133,
1501
 
        .arg = "DIR",
1502
 
        .doc = "Directory where network hooks are located",
1503
 
        .group = 2 },
1504
1362
      /*
1505
1363
       * These reproduce what we would get without ARGP_NO_HELP
1506
1364
       */
1559
1417
          argp_error(state, "Bad retry interval");
1560
1418
        }
1561
1419
        break;
1562
 
      case 133:                 /* --network-hook-dir */
1563
 
        hookdir = arg;
1564
 
        break;
1565
1420
        /*
1566
1421
         * These reproduce what we would get without ARGP_NO_HELP
1567
1422
         */
1573
1428
        argp_state_help(state, state->out_stream,
1574
1429
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1575
1430
      case 'V':                 /* --version */
1576
 
        fprintf(state->out_stream, "Mandos plugin mandos-client: ");
1577
1431
        fprintf(state->out_stream, "%s\n", argp_program_version);
1578
1432
        exit(argp_err_exit_status);
1579
1433
        break;
1664
1518
    }
1665
1519
  }
1666
1520
  
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("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);
1763
 
    }
1764
 
  }
1765
 
  
1766
1521
  if(not debug){
1767
1522
    avahi_set_log_function(empty_log);
1768
1523
  }
1769
 
  
 
1524
 
1770
1525
  if(interface[0] == '\0'){
1771
1526
    struct dirent **direntries;
1772
 
    /* First look for interfaces that are up */
1773
 
    ret = scandir(sys_class_net, &direntries, up_interface,
 
1527
    ret = scandir(sys_class_net, &direntries, good_interface,
1774
1528
                  alphasort);
1775
 
    if(ret == 0){
1776
 
      /* No up interfaces, look for any good interfaces */
1777
 
      free(direntries);
1778
 
      ret = scandir(sys_class_net, &direntries, good_interface,
1779
 
                    alphasort);
1780
 
    }
1781
1529
    if(ret >= 1){
1782
 
      /* Pick the first interface returned */
 
1530
      /* Pick the first good interface */
1783
1531
      interface = strdup(direntries[0]->d_name);
1784
1532
      if(debug){
1785
 
        fprintf(stderr, "Mandos plugin mandos-client: "
1786
 
                "Using interface \"%s\"\n", interface);
 
1533
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1787
1534
      }
1788
1535
      if(interface == NULL){
1789
1536
        perror_plus("malloc");
1794
1541
      free(direntries);
1795
1542
    } else {
1796
1543
      free(direntries);
1797
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1798
 
              "Could not find a network interface\n");
 
1544
      fprintf(stderr, "Could not find a network interface\n");
1799
1545
      exitcode = EXIT_FAILURE;
1800
1546
      goto end;
1801
1547
    }
1807
1553
  srand((unsigned int) time(NULL));
1808
1554
  mc.simple_poll = avahi_simple_poll_new();
1809
1555
  if(mc.simple_poll == NULL){
1810
 
    fprintf(stderr, "Mandos plugin mandos-client: "
1811
 
            "Avahi: Failed to create simple poll object.\n");
 
1556
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1812
1557
    exitcode = EX_UNAVAILABLE;
1813
1558
    goto end;
1814
1559
  }
1880
1625
  if(strcmp(interface, "none") != 0){
1881
1626
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1882
1627
    if(if_index == 0){
1883
 
      fprintf(stderr, "Mandos plugin mandos-client: "
1884
 
              "No such interface: \"%s\"\n", interface);
 
1628
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1885
1629
      exitcode = EX_UNAVAILABLE;
1886
1630
      goto end;
1887
1631
    }
2028
1772
  
2029
1773
  ret = init_gnutls_global(pubkey, seckey);
2030
1774
  if(ret == -1){
2031
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2032
 
            "init_gnutls_global failed\n");
 
1775
    fprintf(stderr, "init_gnutls_global failed\n");
2033
1776
    exitcode = EX_UNAVAILABLE;
2034
1777
    goto end;
2035
1778
  } else {
2051
1794
  }
2052
1795
  
2053
1796
  if(not init_gpgme(pubkey, seckey, tempdir)){
2054
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2055
 
            "init_gpgme failed\n");
 
1797
    fprintf(stderr, "init_gpgme failed\n");
2056
1798
    exitcode = EX_UNAVAILABLE;
2057
1799
    goto end;
2058
1800
  } else {
2068
1810
    /* (Mainly meant for debugging) */
2069
1811
    char *address = strrchr(connect_to, ':');
2070
1812
    if(address == NULL){
2071
 
      fprintf(stderr, "Mandos plugin mandos-client: "
2072
 
              "No colon in address\n");
 
1813
      fprintf(stderr, "No colon in address\n");
2073
1814
      exitcode = EX_USAGE;
2074
1815
      goto end;
2075
1816
    }
2083
1824
    tmpmax = strtoimax(address+1, &tmp, 10);
2084
1825
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2085
1826
       or tmpmax != (uint16_t)tmpmax){
2086
 
      fprintf(stderr, "Mandos plugin mandos-client: "
2087
 
              "Bad port number\n");
 
1827
      fprintf(stderr, "Bad port number\n");
2088
1828
      exitcode = EX_USAGE;
2089
1829
      goto end;
2090
1830
    }
2120
1860
        break;
2121
1861
      }
2122
1862
      if(debug){
2123
 
        fprintf(stderr, "Mandos plugin mandos-client: "
2124
 
                "Retrying in %d seconds\n", (int)retry_interval);
 
1863
        fprintf(stderr, "Retrying in %d seconds\n",
 
1864
                (int)retry_interval);
2125
1865
      }
2126
1866
      sleep((int)retry_interval);
2127
1867
    }
2129
1869
    if (not quit_now){
2130
1870
      exitcode = EXIT_SUCCESS;
2131
1871
    }
2132
 
    
 
1872
 
2133
1873
    goto end;
2134
1874
  }
2135
1875
  
2157
1897
  
2158
1898
  /* Check if creating the Avahi server object succeeded */
2159
1899
  if(mc.server == NULL){
2160
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2161
 
            "Failed to create Avahi server: %s\n",
 
1900
    fprintf(stderr, "Failed to create Avahi server: %s\n",
2162
1901
            avahi_strerror(error));
2163
1902
    exitcode = EX_UNAVAILABLE;
2164
1903
    goto end;
2173
1912
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2174
1913
                                   NULL, 0, browse_callback, NULL);
2175
1914
  if(sb == NULL){
2176
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2177
 
            "Failed to create service browser: %s\n",
 
1915
    fprintf(stderr, "Failed to create service browser: %s\n",
2178
1916
            avahi_strerror(avahi_server_errno(mc.server)));
2179
1917
    exitcode = EX_UNAVAILABLE;
2180
1918
    goto end;
2187
1925
  /* Run the main loop */
2188
1926
  
2189
1927
  if(debug){
2190
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2191
 
            "Starting Avahi loop search\n");
 
1928
    fprintf(stderr, "Starting Avahi loop search\n");
2192
1929
  }
2193
1930
 
2194
1931
  ret = avahi_loop_with_timeout(mc.simple_poll,
2195
1932
                                (int)(retry_interval * 1000));
2196
1933
  if(debug){
2197
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2198
 
            "avahi_loop_with_timeout exited %s\n",
 
1934
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
2199
1935
            (ret == 0) ? "successfully" : "with error");
2200
1936
  }
2201
1937
  
2202
1938
 end:
2203
1939
  
2204
1940
  if(debug){
2205
 
    fprintf(stderr, "Mandos plugin mandos-client: "
2206
 
            "%s exiting\n", argv[0]);
 
1941
    fprintf(stderr, "%s exiting\n", argv[0]);
2207
1942
  }
2208
1943
  
2209
1944
  /* Cleanup things */
2237
1972
    }
2238
1973
  }
2239
1974
  
2240
 
  /* XXX run network hooks "stop" here  */
2241
 
  
2242
1975
  /* Take down the network interface */
2243
1976
  if(take_down_interface){
2244
1977
    /* Re-raise priviliges */
2251
1984
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2252
1985
      if(ret == -1){
2253
1986
        perror_plus("ioctl SIOCGIFFLAGS");
2254
 
      } else if(network.ifr_flags & IFF_UP){
 
1987
      } else if(network.ifr_flags & IFF_UP) {
2255
1988
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2256
1989
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
2257
1990
        if(ret == -1){
2289
2022
        }
2290
2023
        ret = remove(fullname);
2291
2024
        if(ret == -1){
2292
 
          fprintf(stderr, "Mandos plugin mandos-client: "
2293
 
                  "remove(\"%s\"): %s\n", fullname, strerror(errno));
 
2025
          fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
 
2026
                  strerror(errno));
2294
2027
        }
2295
2028
        free(fullname);
2296
2029
      }