/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

  • Committer: Björn Påhlsson
  • Date: 2011-11-14 19:10:50 UTC
  • mto: (505.3.9 client-network-hooks)
  • mto: This revision was merged to the branch mainline in revision 525.
  • Revision ID: belorn@fukt.bsnet.se-20111114191050-g11po6084bl9j5kf
replace calls to fprintf with fprintf_plus.

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{
170
174
  perror(print_text);
171
175
}
172
176
 
 
177
int fprintf_plus(FILE *stream, const char *format, ...){
 
178
  va_list ap;
 
179
  va_start (ap, format);
 
180
  
 
181
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name));
 
182
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
 
183
}
 
184
 
173
185
/*
174
186
 * Make additional room in "buffer" for at least BUFFER_SIZE more
175
187
 * bytes. "buffer_capacity" is how much is currently allocated,
176
188
 * "buffer_length" is how much is already used.
177
189
 */
178
190
size_t incbuffer(char **buffer, size_t buffer_length,
179
 
                  size_t buffer_capacity){
 
191
                 size_t buffer_capacity){
180
192
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
181
193
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
182
194
    if(buffer == NULL){
188
200
}
189
201
 
190
202
/* 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){
 
203
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
204
               int af){
194
205
  int ret;
195
206
  server *new_server = malloc(sizeof(server));
196
207
  if(new_server == NULL){
198
209
    return -1;
199
210
  }
200
211
  *new_server = (server){ .ip = strdup(ip),
201
 
                         .port = port,
202
 
                         .if_index = if_index,
203
 
                         .af = af };
 
212
                          .port = port,
 
213
                          .if_index = if_index,
 
214
                          .af = af };
204
215
  if(new_server->ip == NULL){
205
216
    perror_plus("strdup");
206
217
    return -1;
228
239
/* 
229
240
 * Initialize GPGME.
230
241
 */
231
 
static bool init_gpgme(const char *seckey,
232
 
                       const char *pubkey, const char *tempdir){
 
242
static bool init_gpgme(const char *seckey, const char *pubkey,
 
243
                       const char *tempdir){
233
244
  gpgme_error_t rc;
234
245
  gpgme_engine_info_t engine_info;
235
246
  
250
261
    
251
262
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
252
263
    if(rc != GPG_ERR_NO_ERROR){
253
 
      fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
 
264
      fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
254
265
              gpgme_strsource(rc), gpgme_strerror(rc));
255
266
      return false;
256
267
    }
257
268
    
258
269
    rc = gpgme_op_import(mc.ctx, pgp_data);
259
270
    if(rc != GPG_ERR_NO_ERROR){
260
 
      fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
 
271
      fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
261
272
              gpgme_strsource(rc), gpgme_strerror(rc));
262
273
      return false;
263
274
    }
271
282
  }
272
283
  
273
284
  if(debug){
274
 
    fprintf(stderr, "Initializing GPGME\n");
 
285
    fprintf_plus(stderr, "Initializing GPGME\n");
275
286
  }
276
287
  
277
288
  /* Init GPGME */
278
289
  gpgme_check_version(NULL);
279
290
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
280
291
  if(rc != GPG_ERR_NO_ERROR){
281
 
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
 
292
    fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
282
293
            gpgme_strsource(rc), gpgme_strerror(rc));
283
294
    return false;
284
295
  }
286
297
  /* Set GPGME home directory for the OpenPGP engine only */
287
298
  rc = gpgme_get_engine_info(&engine_info);
288
299
  if(rc != GPG_ERR_NO_ERROR){
289
 
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
 
300
    fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
290
301
            gpgme_strsource(rc), gpgme_strerror(rc));
291
302
    return false;
292
303
  }
299
310
    engine_info = engine_info->next;
300
311
  }
301
312
  if(engine_info == NULL){
302
 
    fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
 
313
    fprintf_plus(stderr, "Could not set GPGME home dir to %s\n", tempdir);
303
314
    return false;
304
315
  }
305
316
  
306
317
  /* Create new GPGME "context" */
307
318
  rc = gpgme_new(&(mc.ctx));
308
319
  if(rc != GPG_ERR_NO_ERROR){
309
 
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
310
 
            gpgme_strsource(rc), gpgme_strerror(rc));
 
320
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
 
321
            "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
 
322
            gpgme_strerror(rc));
311
323
    return false;
312
324
  }
313
325
  
332
344
  ssize_t plaintext_length = 0;
333
345
  
334
346
  if(debug){
335
 
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
 
347
    fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
336
348
  }
337
349
  
338
350
  /* Create new GPGME data buffer from memory cryptotext */
339
351
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
340
352
                               0);
341
353
  if(rc != GPG_ERR_NO_ERROR){
342
 
    fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
 
354
    fprintf_plus(stderr, "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_plus(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_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
361
374
            gpgme_strsource(rc), gpgme_strerror(rc));
362
375
    plaintext_length = -1;
363
376
    if(debug){
364
377
      gpgme_decrypt_result_t result;
365
378
      result = gpgme_op_decrypt_result(mc.ctx);
366
379
      if(result == NULL){
367
 
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
 
380
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
368
381
      } else {
369
 
        fprintf(stderr, "Unsupported algorithm: %s\n",
 
382
        fprintf_plus(stderr, "Unsupported algorithm: %s\n",
370
383
                result->unsupported_algorithm);
371
 
        fprintf(stderr, "Wrong key usage: %u\n",
 
384
        fprintf_plus(stderr, "Wrong key usage: %u\n",
372
385
                result->wrong_key_usage);
373
386
        if(result->file_name != NULL){
374
 
          fprintf(stderr, "File name: %s\n", result->file_name);
 
387
          fprintf_plus(stderr, "File name: %s\n", result->file_name);
375
388
        }
376
389
        gpgme_recipient_t recipient;
377
390
        recipient = result->recipients;
378
391
        while(recipient != NULL){
379
 
          fprintf(stderr, "Public key algorithm: %s\n",
 
392
          fprintf_plus(stderr, "Public key algorithm: %s\n",
380
393
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
381
 
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
382
 
          fprintf(stderr, "Secret key available: %s\n",
 
394
          fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
 
395
          fprintf_plus(stderr, "Secret key available: %s\n",
383
396
                  recipient->status == GPG_ERR_NO_SECKEY
384
397
                  ? "No" : "Yes");
385
398
          recipient = recipient->next;
390
403
  }
391
404
  
392
405
  if(debug){
393
 
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
 
406
    fprintf_plus(stderr, "Decryption of OpenPGP data succeeded\n");
394
407
  }
395
408
  
396
409
  /* Seek back to the beginning of the GPGME plaintext data buffer */
403
416
  *plaintext = NULL;
404
417
  while(true){
405
418
    plaintext_capacity = incbuffer(plaintext,
406
 
                                      (size_t)plaintext_length,
407
 
                                      plaintext_capacity);
 
419
                                   (size_t)plaintext_length,
 
420
                                   plaintext_capacity);
408
421
    if(plaintext_capacity == 0){
409
 
        perror_plus("incbuffer");
410
 
        plaintext_length = -1;
411
 
        goto decrypt_end;
 
422
      perror_plus("incbuffer");
 
423
      plaintext_length = -1;
 
424
      goto decrypt_end;
412
425
    }
413
426
    
414
427
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
427
440
  }
428
441
  
429
442
  if(debug){
430
 
    fprintf(stderr, "Decrypted password is: ");
 
443
    fprintf_plus(stderr, "Decrypted password is: ");
431
444
    for(ssize_t i = 0; i < plaintext_length; i++){
432
445
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
433
446
    }
455
468
/* GnuTLS log function callback */
456
469
static void debuggnutls(__attribute__((unused)) int level,
457
470
                        const char* string){
458
 
  fprintf(stderr, "GnuTLS: %s", string);
 
471
  fprintf_plus(stderr, "GnuTLS: %s", string);
459
472
}
460
473
 
461
474
static int init_gnutls_global(const char *pubkeyfilename,
463
476
  int ret;
464
477
  
465
478
  if(debug){
466
 
    fprintf(stderr, "Initializing GnuTLS\n");
 
479
    fprintf_plus(stderr, "Initializing GnuTLS\n");
467
480
  }
468
481
  
469
482
  ret = gnutls_global_init();
470
483
  if(ret != GNUTLS_E_SUCCESS){
471
 
    fprintf(stderr, "GnuTLS global_init: %s\n",
472
 
            safer_gnutls_strerror(ret));
 
484
    fprintf_plus(stderr, "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
473
485
    return -1;
474
486
  }
475
487
  
484
496
  /* OpenPGP credentials */
485
497
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
486
498
  if(ret != GNUTLS_E_SUCCESS){
487
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
488
 
            safer_gnutls_strerror(ret));
 
499
    fprintf_plus(stderr, "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
489
500
    gnutls_global_deinit();
490
501
    return -1;
491
502
  }
492
503
  
493
504
  if(debug){
494
 
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
 
505
    fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
495
506
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
496
507
            seckeyfilename);
497
508
  }
500
511
    (mc.cred, pubkeyfilename, seckeyfilename,
501
512
     GNUTLS_OPENPGP_FMT_BASE64);
502
513
  if(ret != GNUTLS_E_SUCCESS){
503
 
    fprintf(stderr,
504
 
            "Error[%d] while reading the OpenPGP key pair ('%s',"
505
 
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
506
 
    fprintf(stderr, "The GnuTLS error is: %s\n",
507
 
            safer_gnutls_strerror(ret));
 
514
    fprintf_plus(stderr,
 
515
                 "Error[%d] while reading the OpenPGP key pair ('%s',"
 
516
                 " '%s')\n", ret, pubkeyfilename, seckeyfilename);
 
517
    fprintf_plus(stderr, "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
508
518
    goto globalfail;
509
519
  }
510
520
  
511
521
  /* GnuTLS server initialization */
512
522
  ret = gnutls_dh_params_init(&mc.dh_params);
513
523
  if(ret != GNUTLS_E_SUCCESS){
514
 
    fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
 
524
    fprintf_plus(stderr, "Error in GnuTLS DH parameter initialization:"
515
525
            " %s\n", safer_gnutls_strerror(ret));
516
526
    goto globalfail;
517
527
  }
518
528
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
519
529
  if(ret != GNUTLS_E_SUCCESS){
520
 
    fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
 
530
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
521
531
            safer_gnutls_strerror(ret));
522
532
    goto globalfail;
523
533
  }
544
554
    }
545
555
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
546
556
  if(ret != GNUTLS_E_SUCCESS){
547
 
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
 
557
    fprintf_plus(stderr, "Error in GnuTLS session initialization: %s\n",
548
558
            safer_gnutls_strerror(ret));
549
559
  }
550
560
  
558
568
      }
559
569
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
560
570
    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));
 
571
      fprintf_plus(stderr, "Syntax error at: %s\n", err);
 
572
      fprintf_plus(stderr, "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
564
573
      gnutls_deinit(*session);
565
574
      return -1;
566
575
    }
575
584
    }
576
585
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
577
586
  if(ret != GNUTLS_E_SUCCESS){
578
 
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
 
587
    fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
579
588
            safer_gnutls_strerror(ret));
580
589
    gnutls_deinit(*session);
581
590
    return -1;
627
636
    pf = PF_INET;
628
637
    break;
629
638
  default:
630
 
    fprintf(stderr, "Bad address family: %d\n", af);
 
639
    fprintf_plus(stderr, "Bad address family: %d\n", af);
631
640
    errno = EINVAL;
632
641
    return -1;
633
642
  }
638
647
  }
639
648
  
640
649
  if(debug){
641
 
    fprintf(stderr, "Setting up a TCP connection to %s, port %" PRIu16
 
650
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %" PRIu16
642
651
            "\n", ip, port);
643
652
  }
644
653
  
671
680
  }
672
681
  if(ret == 0){
673
682
    int e = errno;
674
 
    fprintf(stderr, "Bad address: %s\n", ip);
 
683
    fprintf_plus(stderr, "Bad address: %s\n", ip);
675
684
    errno = e;
676
685
    goto mandos_end;
677
686
  }
682
691
    
683
692
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
684
693
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
685
 
                              -Wunreachable-code*/
 
694
                                -Wunreachable-code*/
686
695
      if(if_index == AVAHI_IF_UNSPEC){
687
 
        fprintf(stderr, "An IPv6 link-local address is incomplete"
 
696
        fprintf_plus(stderr, "An IPv6 link-local address is incomplete"
688
697
                " without a network interface\n");
689
698
        errno = EINVAL;
690
699
        goto mandos_end;
709
718
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
710
719
        perror_plus("if_indextoname");
711
720
      } else {
712
 
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
 
721
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
713
722
                ip, interface, port);
714
723
      }
715
724
    } else {
716
 
      fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
717
 
              port);
 
725
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n", ip, port);
718
726
    }
719
727
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
720
728
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
730
738
      perror_plus("inet_ntop");
731
739
    } else {
732
740
      if(strcmp(addrstr, ip) != 0){
733
 
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
 
741
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
734
742
      }
735
743
    }
736
744
  }
764
772
  while(true){
765
773
    size_t out_size = strlen(out);
766
774
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
767
 
                                   out_size - written));
 
775
                                        out_size - written));
768
776
    if(ret == -1){
769
777
      int e = errno;
770
778
      perror_plus("write");
790
798
  }
791
799
  
792
800
  if(debug){
793
 
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
 
801
    fprintf_plus(stderr, "Establishing TLS session with %s\n", ip);
794
802
  }
795
803
  
796
804
  if(quit_now){
816
824
  
817
825
  if(ret != GNUTLS_E_SUCCESS){
818
826
    if(debug){
819
 
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
 
827
      fprintf_plus(stderr, "*** GnuTLS Handshake failed ***\n");
820
828
      gnutls_perror(ret);
821
829
    }
822
830
    errno = EPROTO;
826
834
  /* Read OpenPGP packet that contains the wanted password */
827
835
  
828
836
  if(debug){
829
 
    fprintf(stderr, "Retrieving OpenPGP encrypted password from %s\n",
830
 
            ip);
 
837
    fprintf_plus(stderr, "Retrieving OpenPGP encrypted password from %s\n", ip);
831
838
  }
832
839
  
833
840
  while(true){
838
845
    }
839
846
    
840
847
    buffer_capacity = incbuffer(&buffer, buffer_length,
841
 
                                   buffer_capacity);
 
848
                                buffer_capacity);
842
849
    if(buffer_capacity == 0){
843
850
      int e = errno;
844
851
      perror_plus("incbuffer");
871
878
          }
872
879
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
873
880
        if(ret < 0){
874
 
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
 
881
          fprintf_plus(stderr, "*** GnuTLS Re-handshake failed ***\n");
875
882
          gnutls_perror(ret);
876
883
          errno = EPROTO;
877
884
          goto mandos_end;
878
885
        }
879
886
        break;
880
887
      default:
881
 
        fprintf(stderr, "Unknown error while reading data from"
 
888
        fprintf_plus(stderr, "Unknown error while reading data from"
882
889
                " encrypted session with Mandos server\n");
883
890
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
884
891
        errno = EIO;
890
897
  }
891
898
  
892
899
  if(debug){
893
 
    fprintf(stderr, "Closing TLS session\n");
 
900
    fprintf_plus(stderr, "Closing TLS session\n");
894
901
  }
895
902
  
896
903
  if(quit_now){
908
915
  
909
916
  if(buffer_length > 0){
910
917
    ssize_t decrypted_buffer_size;
911
 
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
912
 
                                               buffer_length,
 
918
    decrypted_buffer_size = pgp_packet_decrypt(buffer, buffer_length,
913
919
                                               &decrypted_buffer);
914
920
    if(decrypted_buffer_size >= 0){
915
921
      
926
932
        if(ret == 0 and ferror(stdout)){
927
933
          int e = errno;
928
934
          if(debug){
929
 
            fprintf(stderr, "Error writing encrypted data: %s\n",
 
935
            fprintf_plus(stderr, "Error writing encrypted data: %s\n",
930
936
                    strerror(errno));
931
937
          }
932
938
          errno = e;
990
996
  switch(event){
991
997
  default:
992
998
  case AVAHI_RESOLVER_FAILURE:
993
 
    fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
 
999
    fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
994
1000
            " of type '%s' in domain '%s': %s\n", name, type, domain,
995
1001
            avahi_strerror(avahi_server_errno(mc.server)));
996
1002
    break;
1000
1006
      char ip[AVAHI_ADDRESS_STR_MAX];
1001
1007
      avahi_address_snprint(ip, sizeof(ip), address);
1002
1008
      if(debug){
1003
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
 
1009
        fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
1004
1010
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1005
1011
                ip, (intmax_t)interface, port);
1006
1012
      }
1040
1046
  default:
1041
1047
  case AVAHI_BROWSER_FAILURE:
1042
1048
    
1043
 
    fprintf(stderr, "(Avahi browser) %s\n",
 
1049
    fprintf_plus(stderr, "(Avahi browser) %s\n",
1044
1050
            avahi_strerror(avahi_server_errno(mc.server)));
1045
1051
    avahi_simple_poll_quit(mc.simple_poll);
1046
1052
    return;
1054
1060
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1055
1061
                                    name, type, domain, protocol, 0,
1056
1062
                                    resolve_callback, NULL) == NULL)
1057
 
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
 
1063
      fprintf_plus(stderr, "Avahi: Failed to resolve service '%s': %s\n",
1058
1064
              name, avahi_strerror(avahi_server_errno(mc.server)));
1059
1065
    break;
1060
1066
    
1064
1070
  case AVAHI_BROWSER_ALL_FOR_NOW:
1065
1071
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1066
1072
    if(debug){
1067
 
      fprintf(stderr, "No Mandos server found, still searching...\n");
 
1073
      fprintf_plus(stderr, "No Mandos server found, still searching...\n");
1068
1074
    }
1069
1075
    break;
1070
1076
  }
1085
1091
  errno = old_errno;
1086
1092
}
1087
1093
 
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){
 
1094
bool get_flags(const char *ifname, struct ifreq *ifr){
 
1095
  int ret;
 
1096
  
 
1097
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1098
  if(s < 0){
 
1099
    perror_plus("socket");
 
1100
    return false;
 
1101
  }
 
1102
  strcpy(ifr->ifr_name, ifname);
 
1103
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
 
1104
  if(ret == -1){
1144
1105
    if(debug){
1145
 
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1146
 
              flagstring, if_entry->d_name);
 
1106
      perror_plus("ioctl SIOCGIFFLAGS");
1147
1107
    }
1148
 
    free(flagstring);
1149
 
    return 0;
 
1108
    return false;
1150
1109
  }
1151
 
  free(flagstring);
1152
 
  ifreq_flags flags = (ifreq_flags)tmpmax;
 
1110
  return true;
 
1111
}
 
1112
 
 
1113
bool good_flags(const char *ifname, const struct ifreq *ifr){
 
1114
  
1153
1115
  /* Reject the loopback device */
1154
 
  if(flags & IFF_LOOPBACK){
 
1116
  if(ifr->ifr_flags & IFF_LOOPBACK){
1155
1117
    if(debug){
1156
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1157
 
              if_entry->d_name);
 
1118
      fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n", ifname);
1158
1119
    }
1159
 
    return 0;
 
1120
    return false;
1160
1121
  }
1161
1122
  /* Accept point-to-point devices only if connect_to is specified */
1162
 
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
 
1123
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1163
1124
    if(debug){
1164
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1165
 
              if_entry->d_name);
 
1125
      fprintf_plus(stderr, "Accepting point-to-point interface \"%s\"\n", ifname);
1166
1126
    }
1167
 
    return 1;
 
1127
    return true;
1168
1128
  }
1169
1129
  /* Otherwise, reject non-broadcast-capable devices */
1170
 
  if(not (flags & IFF_BROADCAST)){
 
1130
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1171
1131
    if(debug){
1172
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1173
 
              if_entry->d_name);
 
1132
      fprintf_plus(stderr, "Rejecting non-broadcast interface \"%s\"\n", ifname);
1174
1133
    }
1175
 
    return 0;
 
1134
    return false;
1176
1135
  }
1177
1136
  /* Reject non-ARP interfaces (including dummy interfaces) */
1178
 
  if(flags & IFF_NOARP){
 
1137
  if(ifr->ifr_flags & IFF_NOARP){
1179
1138
    if(debug){
1180
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1181
 
              if_entry->d_name);
 
1139
      fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1182
1140
    }
1183
 
    return 0;
 
1141
    return false;
1184
1142
  }
 
1143
  
1185
1144
  /* Accept this device */
1186
1145
  if(debug){
1187
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1188
 
            if_entry->d_name);
 
1146
    fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
 
1147
  }
 
1148
  return true;
 
1149
}
 
1150
 
 
1151
/* 
 
1152
 * This function determines if a directory entry in /sys/class/net
 
1153
 * corresponds to an acceptable network device.
 
1154
 * (This function is passed to scandir(3) as a filter function.)
 
1155
 */
 
1156
int good_interface(const struct dirent *if_entry){
 
1157
  if(if_entry->d_name[0] == '.'){
 
1158
    return 0;
 
1159
  }
 
1160
  
 
1161
  struct ifreq ifr;
 
1162
  if(not get_flags(if_entry->d_name, &ifr)){
 
1163
    if(debug){
 
1164
      fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
 
1165
              if_entry->d_name);
 
1166
    }
 
1167
    return 0;
 
1168
  }
 
1169
  
 
1170
  if(not good_flags(if_entry->d_name, &ifr)){
 
1171
    return 0;
 
1172
  }
 
1173
  return 1;
 
1174
}
 
1175
 
 
1176
/* 
 
1177
 * This function determines if a directory entry in /sys/class/net
 
1178
 * corresponds to an acceptable network device which is up.
 
1179
 * (This function is passed to scandir(3) as a filter function.)
 
1180
 */
 
1181
int up_interface(const struct dirent *if_entry){
 
1182
  if(if_entry->d_name[0] == '.'){
 
1183
    return 0;
 
1184
  }
 
1185
  
 
1186
  struct ifreq ifr;
 
1187
  if(not get_flags(if_entry->d_name, &ifr)){
 
1188
    if(debug){
 
1189
      fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
 
1190
              if_entry->d_name);
 
1191
    }
 
1192
    return 0;
 
1193
  }
 
1194
  
 
1195
  /* Reject down interfaces */
 
1196
  if(not (ifr.ifr_flags & IFF_UP)){
 
1197
    if(debug){
 
1198
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
 
1199
              if_entry->d_name);
 
1200
    }
 
1201
    return 0;
 
1202
  }
 
1203
  
 
1204
  /* Reject non-running interfaces */
 
1205
  if(not (ifr.ifr_flags & IFF_RUNNING)){
 
1206
    if(debug){
 
1207
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
 
1208
              if_entry->d_name);
 
1209
    }
 
1210
    return 0;
 
1211
  }
 
1212
  
 
1213
  if(not good_flags(if_entry->d_name, &ifr)){
 
1214
    return 0;
1189
1215
  }
1190
1216
  return 1;
1191
1217
}
1201
1227
  return 1;
1202
1228
}
1203
1229
 
 
1230
/* Is this directory entry a runnable program? */
 
1231
int runnable_hook(const struct dirent *direntry){
 
1232
  int ret;
 
1233
  size_t sret;
 
1234
  struct stat st;
 
1235
  
 
1236
  if((direntry->d_name)[0] == '\0'){
 
1237
    /* Empty name? */
 
1238
    return 0;
 
1239
  }
 
1240
  
 
1241
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
1242
                "abcdefghijklmnopqrstuvwxyz"
 
1243
                "0123456789"
 
1244
                "_-");
 
1245
  if((direntry->d_name)[sret] != '\0'){
 
1246
    /* Contains non-allowed characters */
 
1247
    if(debug){
 
1248
      fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
 
1249
              direntry->d_name);
 
1250
    }
 
1251
    return 0;
 
1252
  }
 
1253
  
 
1254
  char *fullname = NULL;
 
1255
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1256
  if(ret < 0){
 
1257
    perror_plus("asprintf");
 
1258
    return 0;
 
1259
  }
 
1260
  
 
1261
  ret = stat(fullname, &st);
 
1262
  if(ret == -1){
 
1263
    if(debug){
 
1264
      perror_plus("Could not stat hook");
 
1265
    }
 
1266
    return 0;
 
1267
  }
 
1268
  if(not (S_ISREG(st.st_mode))){
 
1269
    /* Not a regular file */
 
1270
    if(debug){
 
1271
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
 
1272
              direntry->d_name);
 
1273
    }
 
1274
    return 0;
 
1275
  }
 
1276
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
 
1277
    /* Not executable */
 
1278
    if(debug){
 
1279
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
 
1280
              direntry->d_name);
 
1281
    }
 
1282
    return 0;
 
1283
  }
 
1284
  return 1;
 
1285
}
 
1286
 
1204
1287
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1205
1288
  int ret;
1206
1289
  struct timespec now;
1210
1293
  while(true){
1211
1294
    if(mc.current_server == NULL){
1212
1295
      if (debug){
1213
 
        fprintf(stderr,
1214
 
                "Wait until first server is found. No timeout!\n");
 
1296
        fprintf_plus(stderr, "Wait until first server is found. No timeout!\n");
1215
1297
      }
1216
1298
      ret = avahi_simple_poll_iterate(s, -1);
1217
1299
    } else {
1218
1300
      if (debug){
1219
 
        fprintf(stderr, "Check current_server if we should run it,"
 
1301
        fprintf_plus(stderr, "Check current_server if we should run it,"
1220
1302
                " or wait\n");
1221
1303
      }
1222
1304
      /* the current time */
1239
1321
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1240
1322
      
1241
1323
      if (debug){
1242
 
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
 
1324
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1243
1325
      }
1244
1326
      
1245
1327
      if(block_time <= 0){
1265
1347
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1266
1348
    }
1267
1349
    if(ret != 0){
1268
 
      if (ret > 0 or errno != EINTR) {
 
1350
      if (ret > 0 or errno != EINTR){
1269
1351
        return (ret != 1) ? ret : 0;
1270
1352
      }
1271
1353
    }
1359
1441
        .arg = "SECONDS",
1360
1442
        .doc = "Retry interval used when denied by the mandos server",
1361
1443
        .group = 2 },
 
1444
      { .name = "network-hook-dir", .key = 133,
 
1445
        .arg = "DIR",
 
1446
        .doc = "Directory where network hooks are located",
 
1447
        .group = 2 },
1362
1448
      /*
1363
1449
       * These reproduce what we would get without ARGP_NO_HELP
1364
1450
       */
1417
1503
          argp_error(state, "Bad retry interval");
1418
1504
        }
1419
1505
        break;
 
1506
      case 133:                 /* --network-hook-dir */
 
1507
        hookdir = arg;
 
1508
        break;
1420
1509
        /*
1421
1510
         * These reproduce what we would get without ARGP_NO_HELP
1422
1511
         */
1428
1517
        argp_state_help(state, state->out_stream,
1429
1518
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1430
1519
      case 'V':                 /* --version */
1431
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
 
1520
        fprintf_plus(state->out_stream, "Mandos plugin mandos-client: ");
 
1521
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1432
1522
        exit(argp_err_exit_status);
1433
1523
        break;
1434
1524
      default:
1518
1608
    }
1519
1609
  }
1520
1610
  
 
1611
  /* Find network hooks and run them */
 
1612
  {
 
1613
    struct dirent **direntries;
 
1614
    struct dirent *direntry;
 
1615
    int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1616
                           alphasort);
 
1617
    if(numhooks == -1){
 
1618
      perror_plus("scandir");
 
1619
    } else {
 
1620
      int devnull = open("/dev/null", O_RDONLY);
 
1621
      for(int i = 0; i < numhooks; i++){
 
1622
        direntry = direntries[0];
 
1623
        char *fullname = NULL;
 
1624
        ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1625
        if(ret < 0){
 
1626
          perror_plus("asprintf");
 
1627
          continue;
 
1628
        }
 
1629
        pid_t hook_pid = fork();
 
1630
        if(hook_pid == 0){
 
1631
          /* Child */
 
1632
          dup2(devnull, STDIN_FILENO);
 
1633
          close(devnull);
 
1634
          dup2(STDERR_FILENO, STDOUT_FILENO);
 
1635
          ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1636
          if(ret == -1){
 
1637
            perror_plus("setenv");
 
1638
            exit(1);
 
1639
          }
 
1640
          ret = setenv("DEVICE", interface, 1);
 
1641
          if(ret == -1){
 
1642
            perror_plus("setenv");
 
1643
            exit(1);
 
1644
          }
 
1645
          ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1646
          if(ret == -1){
 
1647
            perror_plus("setenv");
 
1648
            exit(1);
 
1649
          }
 
1650
          ret = setenv("MODE", "start", 1);
 
1651
          if(ret == -1){
 
1652
            perror_plus("setenv");
 
1653
            exit(1);
 
1654
          }
 
1655
          char *delaystring;
 
1656
          ret = asprintf(&delaystring, "%f", delay);
 
1657
          if(ret == -1){
 
1658
            perror_plus("asprintf");
 
1659
            exit(1);
 
1660
          }
 
1661
          ret = setenv("DELAY", delaystring, 1);
 
1662
          if(ret == -1){
 
1663
            free(delaystring);
 
1664
            perror_plus("setenv");
 
1665
            exit(1);
 
1666
          }
 
1667
          free(delaystring);
 
1668
          ret = execl(fullname, direntry->d_name, "start", NULL);
 
1669
          perror_plus("execl");
 
1670
        } else {
 
1671
          int status;
 
1672
          if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1673
            perror_plus("waitpid");
 
1674
            free(fullname);
 
1675
            continue;
 
1676
          }
 
1677
          if(WIFEXITED(status)){
 
1678
            if(WEXITSTATUS(status) != 0){
 
1679
              fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1680
                      " with status %d\n", direntry->d_name,
 
1681
                      WEXITSTATUS(status));
 
1682
              free(fullname);
 
1683
              continue;
 
1684
            }
 
1685
          } else if(WIFSIGNALED(status)){
 
1686
            fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1687
                    " signal %d\n", direntry->d_name,
 
1688
                    WTERMSIG(status));
 
1689
            free(fullname);
 
1690
            continue;
 
1691
          } else {
 
1692
            fprintf_plus(stderr, "Warning: network hook \"%s\" crashed\n",
 
1693
                    direntry->d_name);
 
1694
            free(fullname);
 
1695
            continue;
 
1696
          }
 
1697
        }
 
1698
        free(fullname);
 
1699
        if(quit_now){
 
1700
          goto end;
 
1701
        }
 
1702
      }
 
1703
      close(devnull);
 
1704
    }
 
1705
  }
 
1706
  
1521
1707
  if(not debug){
1522
1708
    avahi_set_log_function(empty_log);
1523
1709
  }
1524
 
 
 
1710
  
1525
1711
  if(interface[0] == '\0'){
1526
1712
    struct dirent **direntries;
1527
 
    ret = scandir(sys_class_net, &direntries, good_interface,
 
1713
    /* First look for interfaces that are up */
 
1714
    ret = scandir(sys_class_net, &direntries, up_interface,
1528
1715
                  alphasort);
 
1716
    if(ret == 0){
 
1717
      /* No up interfaces, look for any good interfaces */
 
1718
      free(direntries);
 
1719
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1720
                    alphasort);
 
1721
    }
1529
1722
    if(ret >= 1){
1530
 
      /* Pick the first good interface */
 
1723
      /* Pick the first interface returned */
1531
1724
      interface = strdup(direntries[0]->d_name);
1532
1725
      if(debug){
1533
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
 
1726
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1534
1727
      }
1535
1728
      if(interface == NULL){
1536
1729
        perror_plus("malloc");
1541
1734
      free(direntries);
1542
1735
    } else {
1543
1736
      free(direntries);
1544
 
      fprintf(stderr, "Could not find a network interface\n");
 
1737
      fprintf_plus(stderr, "Could not find a network interface\n");
1545
1738
      exitcode = EXIT_FAILURE;
1546
1739
      goto end;
1547
1740
    }
1553
1746
  srand((unsigned int) time(NULL));
1554
1747
  mc.simple_poll = avahi_simple_poll_new();
1555
1748
  if(mc.simple_poll == NULL){
1556
 
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
 
1749
    fprintf_plus(stderr, "Avahi: Failed to create simple poll object.\n");
1557
1750
    exitcode = EX_UNAVAILABLE;
1558
1751
    goto end;
1559
1752
  }
1625
1818
  if(strcmp(interface, "none") != 0){
1626
1819
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1627
1820
    if(if_index == 0){
1628
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1821
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1629
1822
      exitcode = EX_UNAVAILABLE;
1630
1823
      goto end;
1631
1824
    }
1772
1965
  
1773
1966
  ret = init_gnutls_global(pubkey, seckey);
1774
1967
  if(ret == -1){
1775
 
    fprintf(stderr, "init_gnutls_global failed\n");
 
1968
    fprintf_plus(stderr, "init_gnutls_global failed\n");
1776
1969
    exitcode = EX_UNAVAILABLE;
1777
1970
    goto end;
1778
1971
  } else {
1794
1987
  }
1795
1988
  
1796
1989
  if(not init_gpgme(pubkey, seckey, tempdir)){
1797
 
    fprintf(stderr, "init_gpgme failed\n");
 
1990
    fprintf_plus(stderr, "init_gpgme failed\n");
1798
1991
    exitcode = EX_UNAVAILABLE;
1799
1992
    goto end;
1800
1993
  } else {
1810
2003
    /* (Mainly meant for debugging) */
1811
2004
    char *address = strrchr(connect_to, ':');
1812
2005
    if(address == NULL){
1813
 
      fprintf(stderr, "No colon in address\n");
 
2006
      fprintf_plus(stderr, "No colon in address\n");
1814
2007
      exitcode = EX_USAGE;
1815
2008
      goto end;
1816
2009
    }
1824
2017
    tmpmax = strtoimax(address+1, &tmp, 10);
1825
2018
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1826
2019
       or tmpmax != (uint16_t)tmpmax){
1827
 
      fprintf(stderr, "Bad port number\n");
 
2020
      fprintf_plus(stderr, "Bad port number\n");
1828
2021
      exitcode = EX_USAGE;
1829
2022
      goto end;
1830
2023
    }
1860
2053
        break;
1861
2054
      }
1862
2055
      if(debug){
1863
 
        fprintf(stderr, "Retrying in %d seconds\n",
1864
 
                (int)retry_interval);
 
2056
        fprintf_plus(stderr, "Retrying in %d seconds\n", (int)retry_interval);
1865
2057
      }
1866
2058
      sleep((int)retry_interval);
1867
2059
    }
1869
2061
    if (not quit_now){
1870
2062
      exitcode = EXIT_SUCCESS;
1871
2063
    }
1872
 
 
 
2064
    
1873
2065
    goto end;
1874
2066
  }
1875
2067
  
1897
2089
  
1898
2090
  /* Check if creating the Avahi server object succeeded */
1899
2091
  if(mc.server == NULL){
1900
 
    fprintf(stderr, "Failed to create Avahi server: %s\n",
 
2092
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
1901
2093
            avahi_strerror(error));
1902
2094
    exitcode = EX_UNAVAILABLE;
1903
2095
    goto end;
1912
2104
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1913
2105
                                   NULL, 0, browse_callback, NULL);
1914
2106
  if(sb == NULL){
1915
 
    fprintf(stderr, "Failed to create service browser: %s\n",
 
2107
    fprintf_plus(stderr, "Failed to create service browser: %s\n",
1916
2108
            avahi_strerror(avahi_server_errno(mc.server)));
1917
2109
    exitcode = EX_UNAVAILABLE;
1918
2110
    goto end;
1925
2117
  /* Run the main loop */
1926
2118
  
1927
2119
  if(debug){
1928
 
    fprintf(stderr, "Starting Avahi loop search\n");
 
2120
    fprintf_plus(stderr, "Starting Avahi loop search\n");
1929
2121
  }
1930
2122
 
1931
2123
  ret = avahi_loop_with_timeout(mc.simple_poll,
1932
2124
                                (int)(retry_interval * 1000));
1933
2125
  if(debug){
1934
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
 
2126
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
1935
2127
            (ret == 0) ? "successfully" : "with error");
1936
2128
  }
1937
2129
  
1938
2130
 end:
1939
2131
  
1940
2132
  if(debug){
1941
 
    fprintf(stderr, "%s exiting\n", argv[0]);
 
2133
    fprintf_plus(stderr, "%s exiting\n", argv[0]);
1942
2134
  }
1943
2135
  
1944
2136
  /* Cleanup things */
1972
2164
    }
1973
2165
  }
1974
2166
  
 
2167
  /* XXX run network hooks "stop" here  */
 
2168
  
1975
2169
  /* Take down the network interface */
1976
2170
  if(take_down_interface){
1977
2171
    /* Re-raise priviliges */
1984
2178
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1985
2179
      if(ret == -1){
1986
2180
        perror_plus("ioctl SIOCGIFFLAGS");
1987
 
      } else if(network.ifr_flags & IFF_UP) {
 
2181
      } else if(network.ifr_flags & IFF_UP){
1988
2182
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1989
2183
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1990
2184
        if(ret == -1){
2022
2216
        }
2023
2217
        ret = remove(fullname);
2024
2218
        if(ret == -1){
2025
 
          fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2026
 
                  strerror(errno));
 
2219
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname, strerror(errno));
2027
2220
        }
2028
2221
        free(fullname);
2029
2222
      }