/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

merge

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(stderr, "Mandos plugin mandos-client: "
 
265
              "bad gpgme_data_new_from_fd: %s: %s\n",
254
266
              gpgme_strsource(rc), gpgme_strerror(rc));
255
267
      return false;
256
268
    }
257
269
    
258
270
    rc = gpgme_op_import(mc.ctx, pgp_data);
259
271
    if(rc != GPG_ERR_NO_ERROR){
260
 
      fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
 
272
      fprintf(stderr, "Mandos plugin mandos-client: "
 
273
              "bad gpgme_op_import: %s: %s\n",
261
274
              gpgme_strsource(rc), gpgme_strerror(rc));
262
275
      return false;
263
276
    }
271
284
  }
272
285
  
273
286
  if(debug){
274
 
    fprintf(stderr, "Initializing GPGME\n");
 
287
    fprintf(stderr, "Mandos plugin mandos-client: "
 
288
            "Initializing GPGME\n");
275
289
  }
276
290
  
277
291
  /* Init GPGME */
278
292
  gpgme_check_version(NULL);
279
293
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
280
294
  if(rc != GPG_ERR_NO_ERROR){
281
 
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
 
295
    fprintf(stderr, "Mandos plugin mandos-client: "
 
296
            "bad gpgme_engine_check_version: %s: %s\n",
282
297
            gpgme_strsource(rc), gpgme_strerror(rc));
283
298
    return false;
284
299
  }
286
301
  /* Set GPGME home directory for the OpenPGP engine only */
287
302
  rc = gpgme_get_engine_info(&engine_info);
288
303
  if(rc != GPG_ERR_NO_ERROR){
289
 
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
 
304
    fprintf(stderr, "Mandos plugin mandos-client: "
 
305
            "bad gpgme_get_engine_info: %s: %s\n",
290
306
            gpgme_strsource(rc), gpgme_strerror(rc));
291
307
    return false;
292
308
  }
299
315
    engine_info = engine_info->next;
300
316
  }
301
317
  if(engine_info == NULL){
302
 
    fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
 
318
    fprintf(stderr, "Mandos plugin mandos-client: "
 
319
            "Could not set GPGME home dir to %s\n", tempdir);
303
320
    return false;
304
321
  }
305
322
  
306
323
  /* Create new GPGME "context" */
307
324
  rc = gpgme_new(&(mc.ctx));
308
325
  if(rc != GPG_ERR_NO_ERROR){
309
 
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
310
 
            gpgme_strsource(rc), gpgme_strerror(rc));
 
326
    fprintf(stderr, "Mandos plugin mandos-client: "
 
327
            "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
 
328
            gpgme_strerror(rc));
311
329
    return false;
312
330
  }
313
331
  
332
350
  ssize_t plaintext_length = 0;
333
351
  
334
352
  if(debug){
335
 
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
 
353
    fprintf(stderr, "Mandos plugin mandos-client: "
 
354
            "Trying to decrypt OpenPGP data\n");
336
355
  }
337
356
  
338
357
  /* Create new GPGME data buffer from memory cryptotext */
339
358
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
340
359
                               0);
341
360
  if(rc != GPG_ERR_NO_ERROR){
342
 
    fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
 
361
    fprintf(stderr, "Mandos plugin mandos-client: "
 
362
            "bad gpgme_data_new_from_mem: %s: %s\n",
343
363
            gpgme_strsource(rc), gpgme_strerror(rc));
344
364
    return -1;
345
365
  }
347
367
  /* Create new empty GPGME data buffer for the plaintext */
348
368
  rc = gpgme_data_new(&dh_plain);
349
369
  if(rc != GPG_ERR_NO_ERROR){
350
 
    fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
 
370
    fprintf(stderr, "Mandos plugin mandos-client: "
 
371
            "bad gpgme_data_new: %s: %s\n",
351
372
            gpgme_strsource(rc), gpgme_strerror(rc));
352
373
    gpgme_data_release(dh_crypto);
353
374
    return -1;
357
378
     data buffer */
358
379
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
359
380
  if(rc != GPG_ERR_NO_ERROR){
360
 
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
 
381
    fprintf(stderr, "Mandos plugin mandos-client: "
 
382
            "bad gpgme_op_decrypt: %s: %s\n",
361
383
            gpgme_strsource(rc), gpgme_strerror(rc));
362
384
    plaintext_length = -1;
363
385
    if(debug){
364
386
      gpgme_decrypt_result_t result;
365
387
      result = gpgme_op_decrypt_result(mc.ctx);
366
388
      if(result == NULL){
367
 
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
 
389
        fprintf(stderr, "Mandos plugin mandos-client: "
 
390
                "gpgme_op_decrypt_result failed\n");
368
391
      } else {
369
 
        fprintf(stderr, "Unsupported algorithm: %s\n",
 
392
        fprintf(stderr, "Mandos plugin mandos-client: "
 
393
                "Unsupported algorithm: %s\n",
370
394
                result->unsupported_algorithm);
371
 
        fprintf(stderr, "Wrong key usage: %u\n",
 
395
        fprintf(stderr, "Mandos plugin mandos-client: "
 
396
                "Wrong key usage: %u\n",
372
397
                result->wrong_key_usage);
373
398
        if(result->file_name != NULL){
374
 
          fprintf(stderr, "File name: %s\n", result->file_name);
 
399
          fprintf(stderr, "Mandos plugin mandos-client: "
 
400
                  "File name: %s\n", result->file_name);
375
401
        }
376
402
        gpgme_recipient_t recipient;
377
403
        recipient = result->recipients;
378
404
        while(recipient != NULL){
379
 
          fprintf(stderr, "Public key algorithm: %s\n",
 
405
          fprintf(stderr, "Mandos plugin mandos-client: "
 
406
                  "Public key algorithm: %s\n",
380
407
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
381
 
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
382
 
          fprintf(stderr, "Secret key available: %s\n",
 
408
          fprintf(stderr, "Mandos plugin mandos-client: "
 
409
                  "Key ID: %s\n", recipient->keyid);
 
410
          fprintf(stderr, "Mandos plugin mandos-client: "
 
411
                  "Secret key available: %s\n",
383
412
                  recipient->status == GPG_ERR_NO_SECKEY
384
413
                  ? "No" : "Yes");
385
414
          recipient = recipient->next;
390
419
  }
391
420
  
392
421
  if(debug){
393
 
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
 
422
    fprintf(stderr, "Mandos plugin mandos-client: "
 
423
            "Decryption of OpenPGP data succeeded\n");
394
424
  }
395
425
  
396
426
  /* Seek back to the beginning of the GPGME plaintext data buffer */
403
433
  *plaintext = NULL;
404
434
  while(true){
405
435
    plaintext_capacity = incbuffer(plaintext,
406
 
                                      (size_t)plaintext_length,
407
 
                                      plaintext_capacity);
 
436
                                   (size_t)plaintext_length,
 
437
                                   plaintext_capacity);
408
438
    if(plaintext_capacity == 0){
409
 
        perror_plus("incbuffer");
410
 
        plaintext_length = -1;
411
 
        goto decrypt_end;
 
439
      perror_plus("incbuffer");
 
440
      plaintext_length = -1;
 
441
      goto decrypt_end;
412
442
    }
413
443
    
414
444
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
427
457
  }
428
458
  
429
459
  if(debug){
430
 
    fprintf(stderr, "Decrypted password is: ");
 
460
    fprintf(stderr, "Mandos plugin mandos-client: "
 
461
            "Decrypted password is: ");
431
462
    for(ssize_t i = 0; i < plaintext_length; i++){
432
463
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
433
464
    }
455
486
/* GnuTLS log function callback */
456
487
static void debuggnutls(__attribute__((unused)) int level,
457
488
                        const char* string){
458
 
  fprintf(stderr, "GnuTLS: %s", string);
 
489
  fprintf(stderr, "Mandos plugin mandos-client: GnuTLS: %s", string);
459
490
}
460
491
 
461
492
static int init_gnutls_global(const char *pubkeyfilename,
463
494
  int ret;
464
495
  
465
496
  if(debug){
466
 
    fprintf(stderr, "Initializing GnuTLS\n");
 
497
    fprintf(stderr, "Mandos plugin mandos-client: "
 
498
            "Initializing GnuTLS\n");
467
499
  }
468
500
  
469
501
  ret = gnutls_global_init();
470
502
  if(ret != GNUTLS_E_SUCCESS){
471
 
    fprintf(stderr, "GnuTLS global_init: %s\n",
472
 
            safer_gnutls_strerror(ret));
 
503
    fprintf(stderr, "Mandos plugin mandos-client: "
 
504
            "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
473
505
    return -1;
474
506
  }
475
507
  
484
516
  /* OpenPGP credentials */
485
517
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
486
518
  if(ret != GNUTLS_E_SUCCESS){
487
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
488
 
            safer_gnutls_strerror(ret));
 
519
    fprintf(stderr, "Mandos plugin mandos-client: "
 
520
            "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
489
521
    gnutls_global_deinit();
490
522
    return -1;
491
523
  }
492
524
  
493
525
  if(debug){
494
 
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
 
526
    fprintf(stderr, "Mandos plugin mandos-client: "
 
527
            "Attempting to use OpenPGP public key %s and"
495
528
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
496
529
            seckeyfilename);
497
530
  }
501
534
     GNUTLS_OPENPGP_FMT_BASE64);
502
535
  if(ret != GNUTLS_E_SUCCESS){
503
536
    fprintf(stderr,
 
537
            "Mandos plugin mandos-client: "
504
538
            "Error[%d] while reading the OpenPGP key pair ('%s',"
505
539
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
506
 
    fprintf(stderr, "The GnuTLS error is: %s\n",
507
 
            safer_gnutls_strerror(ret));
 
540
    fprintf(stderr, "Mandos plugin mandos-client: "
 
541
            "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
508
542
    goto globalfail;
509
543
  }
510
544
  
511
545
  /* GnuTLS server initialization */
512
546
  ret = gnutls_dh_params_init(&mc.dh_params);
513
547
  if(ret != GNUTLS_E_SUCCESS){
514
 
    fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
 
548
    fprintf(stderr, "Mandos plugin mandos-client: "
 
549
            "Error in GnuTLS DH parameter initialization:"
515
550
            " %s\n", safer_gnutls_strerror(ret));
516
551
    goto globalfail;
517
552
  }
518
553
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
519
554
  if(ret != GNUTLS_E_SUCCESS){
520
 
    fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
 
555
    fprintf(stderr, "Mandos plugin mandos-client: "
 
556
            "Error in GnuTLS prime generation: %s\n",
521
557
            safer_gnutls_strerror(ret));
522
558
    goto globalfail;
523
559
  }
544
580
    }
545
581
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
546
582
  if(ret != GNUTLS_E_SUCCESS){
547
 
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
 
583
    fprintf(stderr, "Mandos plugin mandos-client: "
 
584
            "Error in GnuTLS session initialization: %s\n",
548
585
            safer_gnutls_strerror(ret));
549
586
  }
550
587
  
558
595
      }
559
596
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
560
597
    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));
 
598
      fprintf(stderr, "Mandos plugin mandos-client: "
 
599
              "Syntax error at: %s\n", err);
 
600
      fprintf(stderr, "Mandos plugin mandos-client: "
 
601
              "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
564
602
      gnutls_deinit(*session);
565
603
      return -1;
566
604
    }
575
613
    }
576
614
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
577
615
  if(ret != GNUTLS_E_SUCCESS){
578
 
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
 
616
    fprintf(stderr, "Mandos plugin mandos-client: "
 
617
            "Error setting GnuTLS credentials: %s\n",
579
618
            safer_gnutls_strerror(ret));
580
619
    gnutls_deinit(*session);
581
620
    return -1;
627
666
    pf = PF_INET;
628
667
    break;
629
668
  default:
630
 
    fprintf(stderr, "Bad address family: %d\n", af);
 
669
    fprintf(stderr, "Mandos plugin mandos-client: "
 
670
            "Bad address family: %d\n", af);
631
671
    errno = EINVAL;
632
672
    return -1;
633
673
  }
638
678
  }
639
679
  
640
680
  if(debug){
641
 
    fprintf(stderr, "Setting up a TCP connection to %s, port %" PRIu16
 
681
    fprintf(stderr, "Mandos plugin mandos-client: "
 
682
            "Setting up a TCP connection to %s, port %" PRIu16
642
683
            "\n", ip, port);
643
684
  }
644
685
  
671
712
  }
672
713
  if(ret == 0){
673
714
    int e = errno;
674
 
    fprintf(stderr, "Bad address: %s\n", ip);
 
715
    fprintf(stderr, "Mandos plugin mandos-client: "
 
716
            "Bad address: %s\n", ip);
675
717
    errno = e;
676
718
    goto mandos_end;
677
719
  }
682
724
    
683
725
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
684
726
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
685
 
                              -Wunreachable-code*/
 
727
                                -Wunreachable-code*/
686
728
      if(if_index == AVAHI_IF_UNSPEC){
687
 
        fprintf(stderr, "An IPv6 link-local address is incomplete"
 
729
        fprintf(stderr, "Mandos plugin mandos-client: "
 
730
                "An IPv6 link-local address is incomplete"
688
731
                " without a network interface\n");
689
732
        errno = EINVAL;
690
733
        goto mandos_end;
709
752
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
710
753
        perror_plus("if_indextoname");
711
754
      } else {
712
 
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
 
755
        fprintf(stderr, "Mandos plugin mandos-client: "
 
756
                "Connection to: %s%%%s, port %" PRIu16 "\n",
713
757
                ip, interface, port);
714
758
      }
715
759
    } else {
716
 
      fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
717
 
              port);
 
760
      fprintf(stderr, "Mandos plugin mandos-client: "
 
761
              "Connection to: %s, port %" PRIu16 "\n", ip, port);
718
762
    }
719
763
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
720
764
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
730
774
      perror_plus("inet_ntop");
731
775
    } else {
732
776
      if(strcmp(addrstr, ip) != 0){
733
 
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
 
777
        fprintf(stderr, "Mandos plugin mandos-client: "
 
778
                "Canonical address form: %s\n", addrstr);
734
779
      }
735
780
    }
736
781
  }
764
809
  while(true){
765
810
    size_t out_size = strlen(out);
766
811
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
767
 
                                   out_size - written));
 
812
                                        out_size - written));
768
813
    if(ret == -1){
769
814
      int e = errno;
770
815
      perror_plus("write");
790
835
  }
791
836
  
792
837
  if(debug){
793
 
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
 
838
    fprintf(stderr, "Mandos plugin mandos-client: "
 
839
            "Establishing TLS session with %s\n", ip);
794
840
  }
795
841
  
796
842
  if(quit_now){
816
862
  
817
863
  if(ret != GNUTLS_E_SUCCESS){
818
864
    if(debug){
819
 
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
 
865
      fprintf(stderr, "Mandos plugin mandos-client: "
 
866
              "*** GnuTLS Handshake failed ***\n");
820
867
      gnutls_perror(ret);
821
868
    }
822
869
    errno = EPROTO;
826
873
  /* Read OpenPGP packet that contains the wanted password */
827
874
  
828
875
  if(debug){
829
 
    fprintf(stderr, "Retrieving OpenPGP encrypted password from %s\n",
830
 
            ip);
 
876
    fprintf(stderr, "Mandos plugin mandos-client: "
 
877
            "Retrieving OpenPGP encrypted password from %s\n", ip);
831
878
  }
832
879
  
833
880
  while(true){
838
885
    }
839
886
    
840
887
    buffer_capacity = incbuffer(&buffer, buffer_length,
841
 
                                   buffer_capacity);
 
888
                                buffer_capacity);
842
889
    if(buffer_capacity == 0){
843
890
      int e = errno;
844
891
      perror_plus("incbuffer");
871
918
          }
872
919
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
873
920
        if(ret < 0){
874
 
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
 
921
          fprintf(stderr, "Mandos plugin mandos-client: "
 
922
                  "*** GnuTLS Re-handshake failed ***\n");
875
923
          gnutls_perror(ret);
876
924
          errno = EPROTO;
877
925
          goto mandos_end;
878
926
        }
879
927
        break;
880
928
      default:
881
 
        fprintf(stderr, "Unknown error while reading data from"
 
929
        fprintf(stderr, "Mandos plugin mandos-client: "
 
930
                "Unknown error while reading data from"
882
931
                " encrypted session with Mandos server\n");
883
932
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
884
933
        errno = EIO;
890
939
  }
891
940
  
892
941
  if(debug){
893
 
    fprintf(stderr, "Closing TLS session\n");
 
942
    fprintf(stderr, "Mandos plugin mandos-client: "
 
943
            "Closing TLS session\n");
894
944
  }
895
945
  
896
946
  if(quit_now){
908
958
  
909
959
  if(buffer_length > 0){
910
960
    ssize_t decrypted_buffer_size;
911
 
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
912
 
                                               buffer_length,
 
961
    decrypted_buffer_size = pgp_packet_decrypt(buffer, buffer_length,
913
962
                                               &decrypted_buffer);
914
963
    if(decrypted_buffer_size >= 0){
915
964
      
926
975
        if(ret == 0 and ferror(stdout)){
927
976
          int e = errno;
928
977
          if(debug){
929
 
            fprintf(stderr, "Error writing encrypted data: %s\n",
 
978
            fprintf(stderr, "Mandos plugin mandos-client: "
 
979
                    "Error writing encrypted data: %s\n",
930
980
                    strerror(errno));
931
981
          }
932
982
          errno = e;
990
1040
  switch(event){
991
1041
  default:
992
1042
  case AVAHI_RESOLVER_FAILURE:
993
 
    fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
 
1043
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1044
            "(Avahi Resolver) Failed to resolve service '%s'"
994
1045
            " of type '%s' in domain '%s': %s\n", name, type, domain,
995
1046
            avahi_strerror(avahi_server_errno(mc.server)));
996
1047
    break;
1000
1051
      char ip[AVAHI_ADDRESS_STR_MAX];
1001
1052
      avahi_address_snprint(ip, sizeof(ip), address);
1002
1053
      if(debug){
1003
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
 
1054
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1055
                "Mandos server \"%s\" found on %s (%s, %"
1004
1056
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1005
1057
                ip, (intmax_t)interface, port);
1006
1058
      }
1040
1092
  default:
1041
1093
  case AVAHI_BROWSER_FAILURE:
1042
1094
    
1043
 
    fprintf(stderr, "(Avahi browser) %s\n",
 
1095
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1096
            "(Avahi browser) %s\n",
1044
1097
            avahi_strerror(avahi_server_errno(mc.server)));
1045
1098
    avahi_simple_poll_quit(mc.simple_poll);
1046
1099
    return;
1054
1107
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1055
1108
                                    name, type, domain, protocol, 0,
1056
1109
                                    resolve_callback, NULL) == NULL)
1057
 
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
 
1110
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1111
              "Avahi: Failed to resolve service '%s': %s\n",
1058
1112
              name, avahi_strerror(avahi_server_errno(mc.server)));
1059
1113
    break;
1060
1114
    
1064
1118
  case AVAHI_BROWSER_ALL_FOR_NOW:
1065
1119
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1066
1120
    if(debug){
1067
 
      fprintf(stderr, "No Mandos server found, still searching...\n");
 
1121
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1122
              "No Mandos server found, still searching...\n");
1068
1123
    }
1069
1124
    break;
1070
1125
  }
1085
1140
  errno = old_errno;
1086
1141
}
1087
1142
 
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;
 
1143
bool get_flags(const char *ifname, struct ifreq *ifr){
1095
1144
  int ret;
1096
 
  if(if_entry->d_name[0] == '.'){
1097
 
    return 0;
1098
 
  }
 
1145
  
1099
1146
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1100
1147
  if(s < 0){
1101
1148
    perror_plus("socket");
1102
 
    return 0;
 
1149
    return false;
1103
1150
  }
1104
 
  struct ifreq ifr;
1105
 
  strcpy(ifr.ifr_name, if_entry->d_name);
1106
 
  ret = ioctl(s, SIOCGIFFLAGS, &ifr);
 
1151
  strcpy(ifr->ifr_name, ifname);
 
1152
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1107
1153
  if(ret == -1){
1108
1154
    if(debug){
1109
1155
      perror_plus("ioctl SIOCGIFFLAGS");
1110
1156
    }
1111
 
    return 0;
 
1157
    return false;
1112
1158
  }
 
1159
  return true;
 
1160
}
 
1161
 
 
1162
bool good_flags(const char *ifname, const struct ifreq *ifr){
 
1163
  
1113
1164
  /* Reject the loopback device */
1114
 
  if(ifr.ifr_flags & IFF_LOOPBACK){
 
1165
  if(ifr->ifr_flags & IFF_LOOPBACK){
1115
1166
    if(debug){
1116
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1117
 
              if_entry->d_name);
 
1167
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1168
              "Rejecting loopback interface \"%s\"\n", ifname);
1118
1169
    }
1119
 
    return 0;
 
1170
    return false;
1120
1171
  }
1121
1172
  /* Accept point-to-point devices only if connect_to is specified */
1122
 
  if(connect_to != NULL and (ifr.ifr_flags & IFF_POINTOPOINT)){
 
1173
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1123
1174
    if(debug){
1124
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1125
 
              if_entry->d_name);
 
1175
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1176
              "Accepting point-to-point interface \"%s\"\n", ifname);
1126
1177
    }
1127
 
    return 1;
 
1178
    return true;
1128
1179
  }
1129
1180
  /* Otherwise, reject non-broadcast-capable devices */
1130
 
  if(not (ifr.ifr_flags & IFF_BROADCAST)){
 
1181
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1131
1182
    if(debug){
1132
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1133
 
              if_entry->d_name);
 
1183
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1184
              "Rejecting non-broadcast interface \"%s\"\n", ifname);
1134
1185
    }
1135
 
    return 0;
 
1186
    return false;
1136
1187
  }
1137
1188
  /* Reject non-ARP interfaces (including dummy interfaces) */
1138
 
  if(ifr.ifr_flags & IFF_NOARP){
 
1189
  if(ifr->ifr_flags & IFF_NOARP){
1139
1190
    if(debug){
1140
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1141
 
              if_entry->d_name);
 
1191
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1192
              "Rejecting non-ARP interface \"%s\"\n", ifname);
1142
1193
    }
1143
 
    return 0;
 
1194
    return false;
1144
1195
  }
 
1196
  
1145
1197
  /* Accept this device */
1146
1198
  if(debug){
1147
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1148
 
            if_entry->d_name);
 
1199
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1200
            "Interface \"%s\" is good\n", ifname);
 
1201
  }
 
1202
  return true;
 
1203
}
 
1204
 
 
1205
/* 
 
1206
 * This function determines if a directory entry in /sys/class/net
 
1207
 * corresponds to an acceptable network device.
 
1208
 * (This function is passed to scandir(3) as a filter function.)
 
1209
 */
 
1210
int good_interface(const struct dirent *if_entry){
 
1211
  if(if_entry->d_name[0] == '.'){
 
1212
    return 0;
 
1213
  }
 
1214
  
 
1215
  struct ifreq ifr;
 
1216
  if(not get_flags(if_entry->d_name, &ifr)){
 
1217
    if(debug){
 
1218
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1219
              "Failed to get flags for interface \"%s\"\n",
 
1220
              if_entry->d_name);
 
1221
    }
 
1222
    return 0;
 
1223
  }
 
1224
  
 
1225
  if(not good_flags(if_entry->d_name, &ifr)){
 
1226
    return 0;
 
1227
  }
 
1228
  return 1;
 
1229
}
 
1230
 
 
1231
/* 
 
1232
 * This function determines if a directory entry in /sys/class/net
 
1233
 * corresponds to an acceptable network device which is up.
 
1234
 * (This function is passed to scandir(3) as a filter function.)
 
1235
 */
 
1236
int up_interface(const struct dirent *if_entry){
 
1237
  if(if_entry->d_name[0] == '.'){
 
1238
    return 0;
 
1239
  }
 
1240
  
 
1241
  struct ifreq ifr;
 
1242
  if(not get_flags(if_entry->d_name, &ifr)){
 
1243
    if(debug){
 
1244
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1245
              "Failed to get flags for interface \"%s\"\n",
 
1246
              if_entry->d_name);
 
1247
    }
 
1248
    return 0;
 
1249
  }
 
1250
  
 
1251
  /* Reject down interfaces */
 
1252
  if(not (ifr.ifr_flags & IFF_UP)){
 
1253
    if(debug){
 
1254
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1255
              "Rejecting down interface \"%s\"\n",
 
1256
              if_entry->d_name);
 
1257
    }
 
1258
    return 0;
 
1259
  }
 
1260
  
 
1261
  /* Reject non-running interfaces */
 
1262
  if(not (ifr.ifr_flags & IFF_RUNNING)){
 
1263
    if(debug){
 
1264
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1265
              "Rejecting non-running interface \"%s\"\n",
 
1266
              if_entry->d_name);
 
1267
    }
 
1268
    return 0;
 
1269
  }
 
1270
  
 
1271
  if(not good_flags(if_entry->d_name, &ifr)){
 
1272
    return 0;
1149
1273
  }
1150
1274
  return 1;
1151
1275
}
1161
1285
  return 1;
1162
1286
}
1163
1287
 
 
1288
/* Is this directory entry a runnable program? */
 
1289
int runnable_hook(const struct dirent *direntry){
 
1290
  int ret;
 
1291
  size_t sret;
 
1292
  struct stat st;
 
1293
  
 
1294
  if((direntry->d_name)[0] == '\0'){
 
1295
    /* Empty name? */
 
1296
    return 0;
 
1297
  }
 
1298
  
 
1299
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
1300
                "abcdefghijklmnopqrstuvwxyz"
 
1301
                "0123456789"
 
1302
                "_-");
 
1303
  if((direntry->d_name)[sret] != '\0'){
 
1304
    /* Contains non-allowed characters */
 
1305
    if(debug){
 
1306
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1307
              "Ignoring hook \"%s\" with bad name\n",
 
1308
              direntry->d_name);
 
1309
    }
 
1310
    return 0;
 
1311
  }
 
1312
  
 
1313
  char *fullname = NULL;
 
1314
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1315
  if(ret < 0){
 
1316
    perror_plus("asprintf");
 
1317
    return 0;
 
1318
  }
 
1319
  
 
1320
  ret = stat(fullname, &st);
 
1321
  if(ret == -1){
 
1322
    if(debug){
 
1323
      perror_plus("Could not stat hook");
 
1324
    }
 
1325
    return 0;
 
1326
  }
 
1327
  if(not (S_ISREG(st.st_mode))){
 
1328
    /* Not a regular file */
 
1329
    if(debug){
 
1330
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1331
              "Ignoring hook \"%s\" - not a file\n",
 
1332
              direntry->d_name);
 
1333
    }
 
1334
    return 0;
 
1335
  }
 
1336
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
 
1337
    /* Not executable */
 
1338
    if(debug){
 
1339
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1340
              "Ignoring hook \"%s\" - not executable\n",
 
1341
              direntry->d_name);
 
1342
    }
 
1343
    return 0;
 
1344
  }
 
1345
  return 1;
 
1346
}
 
1347
 
1164
1348
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1165
1349
  int ret;
1166
1350
  struct timespec now;
1170
1354
  while(true){
1171
1355
    if(mc.current_server == NULL){
1172
1356
      if (debug){
1173
 
        fprintf(stderr,
 
1357
        fprintf(stderr, "Mandos plugin mandos-client: "
1174
1358
                "Wait until first server is found. No timeout!\n");
1175
1359
      }
1176
1360
      ret = avahi_simple_poll_iterate(s, -1);
1177
1361
    } else {
1178
1362
      if (debug){
1179
 
        fprintf(stderr, "Check current_server if we should run it,"
 
1363
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1364
                "Check current_server if we should run it,"
1180
1365
                " or wait\n");
1181
1366
      }
1182
1367
      /* the current time */
1199
1384
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1200
1385
      
1201
1386
      if (debug){
1202
 
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
 
1387
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1388
                "Blocking for %" PRIdMAX " ms\n", block_time);
1203
1389
      }
1204
1390
      
1205
1391
      if(block_time <= 0){
1225
1411
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1226
1412
    }
1227
1413
    if(ret != 0){
1228
 
      if (ret > 0 or errno != EINTR) {
 
1414
      if (ret > 0 or errno != EINTR){
1229
1415
        return (ret != 1) ? ret : 0;
1230
1416
      }
1231
1417
    }
1319
1505
        .arg = "SECONDS",
1320
1506
        .doc = "Retry interval used when denied by the mandos server",
1321
1507
        .group = 2 },
 
1508
      { .name = "network-hook-dir", .key = 133,
 
1509
        .arg = "DIR",
 
1510
        .doc = "Directory where network hooks are located",
 
1511
        .group = 2 },
1322
1512
      /*
1323
1513
       * These reproduce what we would get without ARGP_NO_HELP
1324
1514
       */
1377
1567
          argp_error(state, "Bad retry interval");
1378
1568
        }
1379
1569
        break;
 
1570
      case 133:                 /* --network-hook-dir */
 
1571
        hookdir = arg;
 
1572
        break;
1380
1573
        /*
1381
1574
         * These reproduce what we would get without ARGP_NO_HELP
1382
1575
         */
1388
1581
        argp_state_help(state, state->out_stream,
1389
1582
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1390
1583
      case 'V':                 /* --version */
 
1584
        fprintf(state->out_stream, "Mandos plugin mandos-client: ");
1391
1585
        fprintf(state->out_stream, "%s\n", argp_program_version);
1392
1586
        exit(argp_err_exit_status);
1393
1587
        break;
1478
1672
    }
1479
1673
  }
1480
1674
  
 
1675
  /* Find network hooks and run them */
 
1676
  {
 
1677
    struct dirent **direntries;
 
1678
    struct dirent *direntry;
 
1679
    int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1680
                           alphasort);
 
1681
    if(numhooks == -1){
 
1682
      perror_plus("scandir");
 
1683
    } else {
 
1684
      int devnull = open("/dev/null", O_RDONLY);
 
1685
      for(int i = 0; i < numhooks; i++){
 
1686
        direntry = direntries[0];
 
1687
        char *fullname = NULL;
 
1688
        ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1689
        if(ret < 0){
 
1690
          perror_plus("asprintf");
 
1691
          continue;
 
1692
        }
 
1693
        pid_t hook_pid = fork();
 
1694
        if(hook_pid == 0){
 
1695
          /* Child */
 
1696
          dup2(devnull, STDIN_FILENO);
 
1697
          close(devnull);
 
1698
          dup2(STDERR_FILENO, STDOUT_FILENO);
 
1699
          ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1700
          if(ret == -1){
 
1701
            perror_plus("setenv");
 
1702
            exit(1);
 
1703
          }
 
1704
          ret = setenv("DEVICE", interface, 1);
 
1705
          if(ret == -1){
 
1706
            perror_plus("setenv");
 
1707
            exit(1);
 
1708
          }
 
1709
          ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1710
          if(ret == -1){
 
1711
            perror_plus("setenv");
 
1712
            exit(1);
 
1713
          }
 
1714
          ret = setenv("MODE", "start", 1);
 
1715
          if(ret == -1){
 
1716
            perror_plus("setenv");
 
1717
            exit(1);
 
1718
          }
 
1719
          char *delaystring;
 
1720
          ret = asprintf(&delaystring, "%f", delay);
 
1721
          if(ret == -1){
 
1722
            perror_plus("asprintf");
 
1723
            exit(1);
 
1724
          }
 
1725
          ret = setenv("DELAY", delaystring, 1);
 
1726
          if(ret == -1){
 
1727
            free(delaystring);
 
1728
            perror_plus("setenv");
 
1729
            exit(1);
 
1730
          }
 
1731
          free(delaystring);
 
1732
          ret = execl(fullname, direntry->d_name, "start", NULL);
 
1733
          perror_plus("execl");
 
1734
        } else {
 
1735
          int status;
 
1736
          if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1737
            perror_plus("waitpid");
 
1738
            free(fullname);
 
1739
            continue;
 
1740
          }
 
1741
          if(WIFEXITED(status)){
 
1742
            if(WEXITSTATUS(status) != 0){
 
1743
              fprintf(stderr, "Mandos plugin mandos-client: "
 
1744
                      "Warning: network hook \"%s\" exited"
 
1745
                      " with status %d\n", direntry->d_name,
 
1746
                      WEXITSTATUS(status));
 
1747
              free(fullname);
 
1748
              continue;
 
1749
            }
 
1750
          } else if(WIFSIGNALED(status)){
 
1751
            fprintf(stderr, "Mandos plugin mandos-client: "
 
1752
                    "Warning: network hook \"%s\" died by"
 
1753
                    " signal %d\n", direntry->d_name,
 
1754
                    WTERMSIG(status));
 
1755
            free(fullname);
 
1756
            continue;
 
1757
          } else {
 
1758
            fprintf(stderr, "Mandos plugin mandos-client: "
 
1759
                    "Warning: network hook \"%s\" crashed\n",
 
1760
                    direntry->d_name);
 
1761
            free(fullname);
 
1762
            continue;
 
1763
          }
 
1764
        }
 
1765
        free(fullname);
 
1766
        if(quit_now){
 
1767
          goto end;
 
1768
        }
 
1769
      }
 
1770
      close(devnull);
 
1771
    }
 
1772
  }
 
1773
  
1481
1774
  if(not debug){
1482
1775
    avahi_set_log_function(empty_log);
1483
1776
  }
1484
 
 
 
1777
  
1485
1778
  if(interface[0] == '\0'){
1486
1779
    struct dirent **direntries;
1487
 
    ret = scandir(sys_class_net, &direntries, good_interface,
 
1780
    /* First look for interfaces that are up */
 
1781
    ret = scandir(sys_class_net, &direntries, up_interface,
1488
1782
                  alphasort);
 
1783
    if(ret == 0){
 
1784
      /* No up interfaces, look for any good interfaces */
 
1785
      free(direntries);
 
1786
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1787
                    alphasort);
 
1788
    }
1489
1789
    if(ret >= 1){
1490
 
      /* Pick the first good interface */
 
1790
      /* Pick the first interface returned */
1491
1791
      interface = strdup(direntries[0]->d_name);
1492
1792
      if(debug){
1493
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
 
1793
        fprintf(stderr, "Mandos plugin mandos-client: "
 
1794
                "Using interface \"%s\"\n", interface);
1494
1795
      }
1495
1796
      if(interface == NULL){
1496
1797
        perror_plus("malloc");
1501
1802
      free(direntries);
1502
1803
    } else {
1503
1804
      free(direntries);
1504
 
      fprintf(stderr, "Could not find a network interface\n");
 
1805
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1806
              "Could not find a network interface\n");
1505
1807
      exitcode = EXIT_FAILURE;
1506
1808
      goto end;
1507
1809
    }
1513
1815
  srand((unsigned int) time(NULL));
1514
1816
  mc.simple_poll = avahi_simple_poll_new();
1515
1817
  if(mc.simple_poll == NULL){
1516
 
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
 
1818
    fprintf(stderr, "Mandos plugin mandos-client: "
 
1819
            "Avahi: Failed to create simple poll object.\n");
1517
1820
    exitcode = EX_UNAVAILABLE;
1518
1821
    goto end;
1519
1822
  }
1585
1888
  if(strcmp(interface, "none") != 0){
1586
1889
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1587
1890
    if(if_index == 0){
1588
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1891
      fprintf(stderr, "Mandos plugin mandos-client: "
 
1892
              "No such interface: \"%s\"\n", interface);
1589
1893
      exitcode = EX_UNAVAILABLE;
1590
1894
      goto end;
1591
1895
    }
1732
2036
  
1733
2037
  ret = init_gnutls_global(pubkey, seckey);
1734
2038
  if(ret == -1){
1735
 
    fprintf(stderr, "init_gnutls_global failed\n");
 
2039
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2040
            "init_gnutls_global failed\n");
1736
2041
    exitcode = EX_UNAVAILABLE;
1737
2042
    goto end;
1738
2043
  } else {
1754
2059
  }
1755
2060
  
1756
2061
  if(not init_gpgme(pubkey, seckey, tempdir)){
1757
 
    fprintf(stderr, "init_gpgme failed\n");
 
2062
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2063
            "init_gpgme failed\n");
1758
2064
    exitcode = EX_UNAVAILABLE;
1759
2065
    goto end;
1760
2066
  } else {
1770
2076
    /* (Mainly meant for debugging) */
1771
2077
    char *address = strrchr(connect_to, ':');
1772
2078
    if(address == NULL){
1773
 
      fprintf(stderr, "No colon in address\n");
 
2079
      fprintf(stderr, "Mandos plugin mandos-client: "
 
2080
              "No colon in address\n");
1774
2081
      exitcode = EX_USAGE;
1775
2082
      goto end;
1776
2083
    }
1784
2091
    tmpmax = strtoimax(address+1, &tmp, 10);
1785
2092
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1786
2093
       or tmpmax != (uint16_t)tmpmax){
1787
 
      fprintf(stderr, "Bad port number\n");
 
2094
      fprintf(stderr, "Mandos plugin mandos-client: "
 
2095
              "Bad port number\n");
1788
2096
      exitcode = EX_USAGE;
1789
2097
      goto end;
1790
2098
    }
1820
2128
        break;
1821
2129
      }
1822
2130
      if(debug){
1823
 
        fprintf(stderr, "Retrying in %d seconds\n",
1824
 
                (int)retry_interval);
 
2131
        fprintf(stderr, "Mandos plugin mandos-client: "
 
2132
                "Retrying in %d seconds\n", (int)retry_interval);
1825
2133
      }
1826
2134
      sleep((int)retry_interval);
1827
2135
    }
1829
2137
    if (not quit_now){
1830
2138
      exitcode = EXIT_SUCCESS;
1831
2139
    }
1832
 
 
 
2140
    
1833
2141
    goto end;
1834
2142
  }
1835
2143
  
1857
2165
  
1858
2166
  /* Check if creating the Avahi server object succeeded */
1859
2167
  if(mc.server == NULL){
1860
 
    fprintf(stderr, "Failed to create Avahi server: %s\n",
 
2168
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2169
            "Failed to create Avahi server: %s\n",
1861
2170
            avahi_strerror(error));
1862
2171
    exitcode = EX_UNAVAILABLE;
1863
2172
    goto end;
1872
2181
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1873
2182
                                   NULL, 0, browse_callback, NULL);
1874
2183
  if(sb == NULL){
1875
 
    fprintf(stderr, "Failed to create service browser: %s\n",
 
2184
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2185
            "Failed to create service browser: %s\n",
1876
2186
            avahi_strerror(avahi_server_errno(mc.server)));
1877
2187
    exitcode = EX_UNAVAILABLE;
1878
2188
    goto end;
1885
2195
  /* Run the main loop */
1886
2196
  
1887
2197
  if(debug){
1888
 
    fprintf(stderr, "Starting Avahi loop search\n");
 
2198
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2199
            "Starting Avahi loop search\n");
1889
2200
  }
1890
2201
 
1891
2202
  ret = avahi_loop_with_timeout(mc.simple_poll,
1892
2203
                                (int)(retry_interval * 1000));
1893
2204
  if(debug){
1894
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
 
2205
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2206
            "avahi_loop_with_timeout exited %s\n",
1895
2207
            (ret == 0) ? "successfully" : "with error");
1896
2208
  }
1897
2209
  
1898
2210
 end:
1899
2211
  
1900
2212
  if(debug){
1901
 
    fprintf(stderr, "%s exiting\n", argv[0]);
 
2213
    fprintf(stderr, "Mandos plugin mandos-client: "
 
2214
            "%s exiting\n", argv[0]);
1902
2215
  }
1903
2216
  
1904
2217
  /* Cleanup things */
1932
2245
    }
1933
2246
  }
1934
2247
  
 
2248
  /* XXX run network hooks "stop" here  */
 
2249
  
1935
2250
  /* Take down the network interface */
1936
2251
  if(take_down_interface){
1937
2252
    /* Re-raise priviliges */
1944
2259
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1945
2260
      if(ret == -1){
1946
2261
        perror_plus("ioctl SIOCGIFFLAGS");
1947
 
      } else if(network.ifr_flags & IFF_UP) {
 
2262
      } else if(network.ifr_flags & IFF_UP){
1948
2263
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1949
2264
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1950
2265
        if(ret == -1){
1982
2297
        }
1983
2298
        ret = remove(fullname);
1984
2299
        if(ret == -1){
1985
 
          fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
1986
 
                  strerror(errno));
 
2300
          fprintf(stderr, "Mandos plugin mandos-client: "
 
2301
                  "remove(\"%s\"): %s\n", fullname, strerror(errno));
1987
2302
        }
1988
2303
        free(fullname);
1989
2304
      }