/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: Teddy Hogeborn
  • Date: 2012-05-26 22:48:45 UTC
  • Revision ID: teddy@recompile.se-20120526224845-imaxtrlh3j3yu1ga
* mandos: Change comment.
* mandos-clients.conf.xml (OPTIONS/checker): Rewrote without "you".
  (SEE ALSO): Reference fping(8).

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2011 Teddy Hogeborn
13
 
 * Copyright © 2008-2011 Björn Påhlsson
 
12
 * Copyright © 2008-2012 Teddy Hogeborn
 
13
 * Copyright © 2008-2012 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
41
41
 
42
42
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
43
                                   stdout, ferror(), remove() */
44
 
#include <stdint.h>             /* uint16_t, uint32_t */
 
44
#include <stdint.h>             /* uint16_t, uint32_t, intptr_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
47
                                   strtof(), abort() */
73
73
                                */
74
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
75
                                   getuid(), getgid(), seteuid(),
76
 
                                   setgid(), pause() */
 
76
                                   setgid(), pause(), _exit() */
77
77
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
78
78
#include <iso646.h>             /* not, or, and */
79
79
#include <argp.h>               /* struct argp_option, error_t, struct
87
87
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
88
88
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
89
89
                                   WEXITSTATUS(), WTERMSIG() */
 
90
#include <grp.h>                /* setgroups() */
90
91
 
91
92
#ifdef __linux__
92
93
#include <sys/klog.h>           /* klogctl() */
169
170
 
170
171
/* Function to use when printing errors */
171
172
void perror_plus(const char *print_text){
 
173
  int e = errno;
172
174
  fprintf(stderr, "Mandos plugin %s: ",
173
175
          program_invocation_short_name);
 
176
  errno = e;
174
177
  perror(print_text);
175
178
}
176
179
 
 
180
__attribute__((format (gnu_printf, 2, 3)))
177
181
int fprintf_plus(FILE *stream, const char *format, ...){
178
182
  va_list ap;
179
183
  va_start (ap, format);
180
184
  
181
 
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name));
 
185
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
 
186
                             program_invocation_short_name));
182
187
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
183
188
}
184
189
 
200
205
}
201
206
 
202
207
/* Add server to set of servers to retry periodically */
203
 
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
204
 
               int af){
 
208
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
209
                int af){
205
210
  int ret;
206
211
  server *new_server = malloc(sizeof(server));
207
212
  if(new_server == NULL){
208
213
    perror_plus("malloc");
209
 
    return -1;
 
214
    return false;
210
215
  }
211
216
  *new_server = (server){ .ip = strdup(ip),
212
217
                          .port = port,
214
219
                          .af = af };
215
220
  if(new_server->ip == NULL){
216
221
    perror_plus("strdup");
217
 
    return -1;
 
222
    return false;
218
223
  }
219
224
  /* Special case of first server */
220
225
  if (mc.current_server == NULL){
231
236
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
232
237
  if(ret == -1){
233
238
    perror_plus("clock_gettime");
234
 
    return -1;
 
239
    return false;
235
240
  }
236
 
  return 0;
 
241
  return true;
237
242
}
238
243
 
239
244
/* 
262
267
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
263
268
    if(rc != GPG_ERR_NO_ERROR){
264
269
      fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
265
 
              gpgme_strsource(rc), gpgme_strerror(rc));
 
270
                   gpgme_strsource(rc), gpgme_strerror(rc));
266
271
      return false;
267
272
    }
268
273
    
269
274
    rc = gpgme_op_import(mc.ctx, pgp_data);
270
275
    if(rc != GPG_ERR_NO_ERROR){
271
276
      fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
272
 
              gpgme_strsource(rc), gpgme_strerror(rc));
 
277
                   gpgme_strsource(rc), gpgme_strerror(rc));
273
278
      return false;
274
279
    }
275
280
    
290
295
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
291
296
  if(rc != GPG_ERR_NO_ERROR){
292
297
    fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
293
 
            gpgme_strsource(rc), gpgme_strerror(rc));
 
298
                 gpgme_strsource(rc), gpgme_strerror(rc));
294
299
    return false;
295
300
  }
296
301
  
298
303
  rc = gpgme_get_engine_info(&engine_info);
299
304
  if(rc != GPG_ERR_NO_ERROR){
300
305
    fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
301
 
            gpgme_strsource(rc), gpgme_strerror(rc));
 
306
                 gpgme_strsource(rc), gpgme_strerror(rc));
302
307
    return false;
303
308
  }
304
309
  while(engine_info != NULL){
310
315
    engine_info = engine_info->next;
311
316
  }
312
317
  if(engine_info == NULL){
313
 
    fprintf_plus(stderr, "Could not set GPGME home dir to %s\n", tempdir);
 
318
    fprintf_plus(stderr, "Could not set GPGME home dir to %s\n",
 
319
                 tempdir);
314
320
    return false;
315
321
  }
316
322
  
318
324
  rc = gpgme_new(&(mc.ctx));
319
325
  if(rc != GPG_ERR_NO_ERROR){
320
326
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
321
 
            "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
322
 
            gpgme_strerror(rc));
 
327
                 "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
 
328
                 gpgme_strerror(rc));
323
329
    return false;
324
330
  }
325
331
  
352
358
                               0);
353
359
  if(rc != GPG_ERR_NO_ERROR){
354
360
    fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
355
 
            gpgme_strsource(rc), gpgme_strerror(rc));
 
361
                 gpgme_strsource(rc), gpgme_strerror(rc));
356
362
    return -1;
357
363
  }
358
364
  
360
366
  rc = gpgme_data_new(&dh_plain);
361
367
  if(rc != GPG_ERR_NO_ERROR){
362
368
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
363
 
            "bad gpgme_data_new: %s: %s\n",
364
 
            gpgme_strsource(rc), gpgme_strerror(rc));
 
369
                 "bad gpgme_data_new: %s: %s\n",
 
370
                 gpgme_strsource(rc), gpgme_strerror(rc));
365
371
    gpgme_data_release(dh_crypto);
366
372
    return -1;
367
373
  }
371
377
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
372
378
  if(rc != GPG_ERR_NO_ERROR){
373
379
    fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
374
 
            gpgme_strsource(rc), gpgme_strerror(rc));
 
380
                 gpgme_strsource(rc), gpgme_strerror(rc));
375
381
    plaintext_length = -1;
376
382
    if(debug){
377
383
      gpgme_decrypt_result_t result;
380
386
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
381
387
      } else {
382
388
        fprintf_plus(stderr, "Unsupported algorithm: %s\n",
383
 
                result->unsupported_algorithm);
 
389
                     result->unsupported_algorithm);
384
390
        fprintf_plus(stderr, "Wrong key usage: %u\n",
385
 
                result->wrong_key_usage);
 
391
                     result->wrong_key_usage);
386
392
        if(result->file_name != NULL){
387
393
          fprintf_plus(stderr, "File name: %s\n", result->file_name);
388
394
        }
390
396
        recipient = result->recipients;
391
397
        while(recipient != NULL){
392
398
          fprintf_plus(stderr, "Public key algorithm: %s\n",
393
 
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
399
                       gpgme_pubkey_algo_name
 
400
                       (recipient->pubkey_algo));
394
401
          fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
395
402
          fprintf_plus(stderr, "Secret key available: %s\n",
396
 
                  recipient->status == GPG_ERR_NO_SECKEY
397
 
                  ? "No" : "Yes");
 
403
                       recipient->status == GPG_ERR_NO_SECKEY
 
404
                       ? "No" : "Yes");
398
405
          recipient = recipient->next;
399
406
        }
400
407
      }
481
488
  
482
489
  ret = gnutls_global_init();
483
490
  if(ret != GNUTLS_E_SUCCESS){
484
 
    fprintf_plus(stderr, "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
 
491
    fprintf_plus(stderr, "GnuTLS global_init: %s\n",
 
492
                 safer_gnutls_strerror(ret));
485
493
    return -1;
486
494
  }
487
495
  
496
504
  /* OpenPGP credentials */
497
505
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
498
506
  if(ret != GNUTLS_E_SUCCESS){
499
 
    fprintf_plus(stderr, "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
 
507
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
 
508
                 safer_gnutls_strerror(ret));
500
509
    gnutls_global_deinit();
501
510
    return -1;
502
511
  }
503
512
  
504
513
  if(debug){
505
514
    fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
506
 
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
507
 
            seckeyfilename);
 
515
                 " secret key %s as GnuTLS credentials\n",
 
516
                 pubkeyfilename,
 
517
                 seckeyfilename);
508
518
  }
509
519
  
510
520
  ret = gnutls_certificate_set_openpgp_key_file
514
524
    fprintf_plus(stderr,
515
525
                 "Error[%d] while reading the OpenPGP key pair ('%s',"
516
526
                 " '%s')\n", ret, pubkeyfilename, seckeyfilename);
517
 
    fprintf_plus(stderr, "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
 
527
    fprintf_plus(stderr, "The GnuTLS error is: %s\n",
 
528
                 safer_gnutls_strerror(ret));
518
529
    goto globalfail;
519
530
  }
520
531
  
521
532
  /* GnuTLS server initialization */
522
533
  ret = gnutls_dh_params_init(&mc.dh_params);
523
534
  if(ret != GNUTLS_E_SUCCESS){
524
 
    fprintf_plus(stderr, "Error in GnuTLS DH parameter initialization:"
525
 
            " %s\n", safer_gnutls_strerror(ret));
 
535
    fprintf_plus(stderr, "Error in GnuTLS DH parameter"
 
536
                 " initialization: %s\n",
 
537
                 safer_gnutls_strerror(ret));
526
538
    goto globalfail;
527
539
  }
528
540
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
529
541
  if(ret != GNUTLS_E_SUCCESS){
530
542
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
531
 
            safer_gnutls_strerror(ret));
 
543
                 safer_gnutls_strerror(ret));
532
544
    goto globalfail;
533
545
  }
534
546
  
554
566
    }
555
567
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
556
568
  if(ret != GNUTLS_E_SUCCESS){
557
 
    fprintf_plus(stderr, "Error in GnuTLS session initialization: %s\n",
558
 
            safer_gnutls_strerror(ret));
 
569
    fprintf_plus(stderr,
 
570
                 "Error in GnuTLS session initialization: %s\n",
 
571
                 safer_gnutls_strerror(ret));
559
572
  }
560
573
  
561
574
  {
569
582
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
570
583
    if(ret != GNUTLS_E_SUCCESS){
571
584
      fprintf_plus(stderr, "Syntax error at: %s\n", err);
572
 
      fprintf_plus(stderr, "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
 
585
      fprintf_plus(stderr, "GnuTLS error: %s\n",
 
586
                   safer_gnutls_strerror(ret));
573
587
      gnutls_deinit(*session);
574
588
      return -1;
575
589
    }
585
599
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
586
600
  if(ret != GNUTLS_E_SUCCESS){
587
601
    fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
588
 
            safer_gnutls_strerror(ret));
 
602
                 safer_gnutls_strerror(ret));
589
603
    gnutls_deinit(*session);
590
604
    return -1;
591
605
  }
647
661
  }
648
662
  
649
663
  if(debug){
650
 
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %" PRIu16
651
 
            "\n", ip, port);
 
664
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
 
665
                 PRIu16 "\n", ip, port);
652
666
  }
653
667
  
654
668
  tcp_sd = socket(pf, SOCK_STREAM, 0);
693
707
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
694
708
                                -Wunreachable-code*/
695
709
      if(if_index == AVAHI_IF_UNSPEC){
696
 
        fprintf_plus(stderr, "An IPv6 link-local address is incomplete"
697
 
                " without a network interface\n");
 
710
        fprintf_plus(stderr, "An IPv6 link-local address is"
 
711
                     " incomplete without a network interface\n");
698
712
        errno = EINVAL;
699
713
        goto mandos_end;
700
714
      }
718
732
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
719
733
        perror_plus("if_indextoname");
720
734
      } else {
721
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
722
 
                ip, interface, port);
 
735
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
 
736
                     "\n", ip, interface, port);
723
737
      }
724
738
    } else {
725
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n", ip, port);
 
739
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
 
740
                   ip, port);
726
741
    }
727
742
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
728
743
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
806
821
    goto mandos_end;
807
822
  }
808
823
  
809
 
  /* Spurious warning from -Wint-to-pointer-cast */
810
 
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
 
824
  /* This casting via intptr_t is to eliminate warning about casting
 
825
     an int to a pointer type.  This is exactly how the GnuTLS Guile
 
826
     function "set-session-transport-fd!" does it. */
 
827
  gnutls_transport_set_ptr(session,
 
828
                           (gnutls_transport_ptr_t)(intptr_t)tcp_sd);
811
829
  
812
830
  if(quit_now){
813
831
    errno = EINTR;
834
852
  /* Read OpenPGP packet that contains the wanted password */
835
853
  
836
854
  if(debug){
837
 
    fprintf_plus(stderr, "Retrieving OpenPGP encrypted password from %s\n", ip);
 
855
    fprintf_plus(stderr, "Retrieving OpenPGP encrypted password from"
 
856
                 " %s\n", ip);
838
857
  }
839
858
  
840
859
  while(true){
878
897
          }
879
898
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
880
899
        if(ret < 0){
881
 
          fprintf_plus(stderr, "*** GnuTLS Re-handshake failed ***\n");
 
900
          fprintf_plus(stderr, "*** GnuTLS Re-handshake failed "
 
901
                       "***\n");
882
902
          gnutls_perror(ret);
883
903
          errno = EPROTO;
884
904
          goto mandos_end;
886
906
        break;
887
907
      default:
888
908
        fprintf_plus(stderr, "Unknown error while reading data from"
889
 
                " encrypted session with Mandos server\n");
 
909
                     " encrypted session with Mandos server\n");
890
910
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
891
911
        errno = EIO;
892
912
        goto mandos_end;
933
953
          int e = errno;
934
954
          if(debug){
935
955
            fprintf_plus(stderr, "Error writing encrypted data: %s\n",
936
 
                    strerror(errno));
 
956
                         strerror(errno));
937
957
          }
938
958
          errno = e;
939
959
          goto mandos_end;
996
1016
  switch(event){
997
1017
  default:
998
1018
  case AVAHI_RESOLVER_FAILURE:
999
 
    fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
1000
 
            " of type '%s' in domain '%s': %s\n", name, type, domain,
1001
 
            avahi_strerror(avahi_server_errno(mc.server)));
 
1019
    fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
 
1020
                 "'%s' of type '%s' in domain '%s': %s\n", name, type,
 
1021
                 domain,
 
1022
                 avahi_strerror(avahi_server_errno(mc.server)));
1002
1023
    break;
1003
1024
    
1004
1025
  case AVAHI_RESOLVER_FOUND:
1007
1028
      avahi_address_snprint(ip, sizeof(ip), address);
1008
1029
      if(debug){
1009
1030
        fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
1010
 
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1011
 
                ip, (intmax_t)interface, port);
 
1031
                     PRIdMAX ") on port %" PRIu16 "\n", name,
 
1032
                     host_name, ip, (intmax_t)interface, port);
1012
1033
      }
1013
1034
      int ret = start_mandos_communication(ip, port, interface,
1014
1035
                                           avahi_proto_to_af(proto));
1015
1036
      if(ret == 0){
1016
1037
        avahi_simple_poll_quit(mc.simple_poll);
1017
1038
      } else {
1018
 
        ret = add_server(ip, port, interface,
1019
 
                         avahi_proto_to_af(proto));
 
1039
        if(not add_server(ip, port, interface,
 
1040
                          avahi_proto_to_af(proto))){
 
1041
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
 
1042
                       " list\n", name);
 
1043
        }
1020
1044
      }
1021
1045
    }
1022
1046
  }
1047
1071
  case AVAHI_BROWSER_FAILURE:
1048
1072
    
1049
1073
    fprintf_plus(stderr, "(Avahi browser) %s\n",
1050
 
            avahi_strerror(avahi_server_errno(mc.server)));
 
1074
                 avahi_strerror(avahi_server_errno(mc.server)));
1051
1075
    avahi_simple_poll_quit(mc.simple_poll);
1052
1076
    return;
1053
1077
    
1060
1084
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1061
1085
                                    name, type, domain, protocol, 0,
1062
1086
                                    resolve_callback, NULL) == NULL)
1063
 
      fprintf_plus(stderr, "Avahi: Failed to resolve service '%s': %s\n",
1064
 
              name, avahi_strerror(avahi_server_errno(mc.server)));
 
1087
      fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
 
1088
                   " %s\n", name,
 
1089
                   avahi_strerror(avahi_server_errno(mc.server)));
1065
1090
    break;
1066
1091
    
1067
1092
  case AVAHI_BROWSER_REMOVE:
1070
1095
  case AVAHI_BROWSER_ALL_FOR_NOW:
1071
1096
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1072
1097
    if(debug){
1073
 
      fprintf_plus(stderr, "No Mandos server found, still searching...\n");
 
1098
      fprintf_plus(stderr, "No Mandos server found, still"
 
1099
                   " searching...\n");
1074
1100
    }
1075
1101
    break;
1076
1102
  }
1115
1141
  /* Reject the loopback device */
1116
1142
  if(ifr->ifr_flags & IFF_LOOPBACK){
1117
1143
    if(debug){
1118
 
      fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n", ifname);
 
1144
      fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
 
1145
                   ifname);
1119
1146
    }
1120
1147
    return false;
1121
1148
  }
1122
1149
  /* Accept point-to-point devices only if connect_to is specified */
1123
1150
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1124
1151
    if(debug){
1125
 
      fprintf_plus(stderr, "Accepting point-to-point interface \"%s\"\n", ifname);
 
1152
      fprintf_plus(stderr, "Accepting point-to-point interface"
 
1153
                   " \"%s\"\n", ifname);
1126
1154
    }
1127
1155
    return true;
1128
1156
  }
1129
1157
  /* Otherwise, reject non-broadcast-capable devices */
1130
1158
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1131
1159
    if(debug){
1132
 
      fprintf_plus(stderr, "Rejecting non-broadcast interface \"%s\"\n", ifname);
 
1160
      fprintf_plus(stderr, "Rejecting non-broadcast interface"
 
1161
                   " \"%s\"\n", ifname);
1133
1162
    }
1134
1163
    return false;
1135
1164
  }
1136
1165
  /* Reject non-ARP interfaces (including dummy interfaces) */
1137
1166
  if(ifr->ifr_flags & IFF_NOARP){
1138
1167
    if(debug){
1139
 
      fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
 
1168
      fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
 
1169
                   ifname);
1140
1170
    }
1141
1171
    return false;
1142
1172
  }
1161
1191
  struct ifreq ifr;
1162
1192
  if(not get_flags(if_entry->d_name, &ifr)){
1163
1193
    if(debug){
1164
 
      fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
1165
 
              if_entry->d_name);
 
1194
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1195
                   "\"%s\"\n", if_entry->d_name);
1166
1196
    }
1167
1197
    return 0;
1168
1198
  }
1186
1216
  struct ifreq ifr;
1187
1217
  if(not get_flags(if_entry->d_name, &ifr)){
1188
1218
    if(debug){
1189
 
      fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
1190
 
              if_entry->d_name);
 
1219
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1220
                   "\"%s\"\n", if_entry->d_name);
1191
1221
    }
1192
1222
    return 0;
1193
1223
  }
1196
1226
  if(not (ifr.ifr_flags & IFF_UP)){
1197
1227
    if(debug){
1198
1228
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1199
 
              if_entry->d_name);
 
1229
                   if_entry->d_name);
1200
1230
    }
1201
1231
    return 0;
1202
1232
  }
1205
1235
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1206
1236
    if(debug){
1207
1237
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1208
 
              if_entry->d_name);
 
1238
                   if_entry->d_name);
1209
1239
    }
1210
1240
    return 0;
1211
1241
  }
1246
1276
    /* Contains non-allowed characters */
1247
1277
    if(debug){
1248
1278
      fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1249
 
              direntry->d_name);
 
1279
                   direntry->d_name);
1250
1280
    }
1251
1281
    return 0;
1252
1282
  }
1269
1299
    /* Not a regular file */
1270
1300
    if(debug){
1271
1301
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1272
 
              direntry->d_name);
 
1302
                   direntry->d_name);
1273
1303
    }
1274
1304
    return 0;
1275
1305
  }
1277
1307
    /* Not executable */
1278
1308
    if(debug){
1279
1309
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1280
 
              direntry->d_name);
 
1310
                   direntry->d_name);
1281
1311
    }
1282
1312
    return 0;
1283
1313
  }
 
1314
  if(debug){
 
1315
    fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
 
1316
                 direntry->d_name);
 
1317
  }
1284
1318
  return 1;
1285
1319
}
1286
1320
 
1293
1327
  while(true){
1294
1328
    if(mc.current_server == NULL){
1295
1329
      if (debug){
1296
 
        fprintf_plus(stderr, "Wait until first server is found. No timeout!\n");
 
1330
        fprintf_plus(stderr, "Wait until first server is found."
 
1331
                     " No timeout!\n");
1297
1332
      }
1298
1333
      ret = avahi_simple_poll_iterate(s, -1);
1299
1334
    } else {
1300
1335
      if (debug){
1301
 
        fprintf_plus(stderr, "Check current_server if we should run it,"
1302
 
                " or wait\n");
 
1336
        fprintf_plus(stderr, "Check current_server if we should run"
 
1337
                     " it, or wait\n");
1303
1338
      }
1304
1339
      /* the current time */
1305
1340
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1321
1356
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1322
1357
      
1323
1358
      if (debug){
1324
 
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
 
1359
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
 
1360
                     block_time);
1325
1361
      }
1326
1362
      
1327
1363
      if(block_time <= 0){
1354
1390
  }
1355
1391
}
1356
1392
 
 
1393
bool run_network_hooks(const char *mode, const char *interface,
 
1394
                       const float delay){
 
1395
  struct dirent **direntries;
 
1396
  struct dirent *direntry;
 
1397
  int ret;
 
1398
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1399
                         alphasort);
 
1400
  if(numhooks == -1){
 
1401
    perror_plus("scandir");
 
1402
  } else {
 
1403
    int devnull = open("/dev/null", O_RDONLY);
 
1404
    for(int i = 0; i < numhooks; i++){
 
1405
      direntry = direntries[i];
 
1406
      char *fullname = NULL;
 
1407
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1408
      if(ret < 0){
 
1409
        perror_plus("asprintf");
 
1410
        continue;
 
1411
      }
 
1412
      if(debug){
 
1413
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1414
                     direntry->d_name);
 
1415
      }
 
1416
      pid_t hook_pid = fork();
 
1417
      if(hook_pid == 0){
 
1418
        /* Child */
 
1419
        /* Raise privileges */
 
1420
        errno = 0;
 
1421
        ret = seteuid(0);
 
1422
        if(ret == -1){
 
1423
          perror_plus("seteuid");
 
1424
        }
 
1425
        /* Raise privileges even more */
 
1426
        errno = 0;
 
1427
        ret = setuid(0);
 
1428
        if(ret == -1){
 
1429
          perror_plus("setuid");
 
1430
        }
 
1431
        /* Set group */
 
1432
        errno = 0;
 
1433
        ret = setgid(0);
 
1434
        if(ret == -1){
 
1435
          perror_plus("setgid");
 
1436
        }
 
1437
        /* Reset supplementary groups */
 
1438
        errno = 0;
 
1439
        ret = setgroups(0, NULL);
 
1440
        if(ret == -1){
 
1441
          perror_plus("setgroups");
 
1442
        }
 
1443
        dup2(devnull, STDIN_FILENO);
 
1444
        close(devnull);
 
1445
        dup2(STDERR_FILENO, STDOUT_FILENO);
 
1446
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1447
        if(ret == -1){
 
1448
          perror_plus("setenv");
 
1449
          _exit(EX_OSERR);
 
1450
        }
 
1451
        ret = setenv("DEVICE", interface, 1);
 
1452
        if(ret == -1){
 
1453
          perror_plus("setenv");
 
1454
          _exit(EX_OSERR);
 
1455
        }
 
1456
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1457
        if(ret == -1){
 
1458
          perror_plus("setenv");
 
1459
          _exit(EX_OSERR);
 
1460
        }
 
1461
        ret = setenv("MODE", mode, 1);
 
1462
        if(ret == -1){
 
1463
          perror_plus("setenv");
 
1464
          _exit(EX_OSERR);
 
1465
        }
 
1466
        char *delaystring;
 
1467
        ret = asprintf(&delaystring, "%f", delay);
 
1468
        if(ret == -1){
 
1469
          perror_plus("asprintf");
 
1470
          _exit(EX_OSERR);
 
1471
        }
 
1472
        ret = setenv("DELAY", delaystring, 1);
 
1473
        if(ret == -1){
 
1474
          free(delaystring);
 
1475
          perror_plus("setenv");
 
1476
          _exit(EX_OSERR);
 
1477
        }
 
1478
        free(delaystring);
 
1479
        if(connect_to != NULL){
 
1480
          ret = setenv("CONNECT", connect_to, 1);
 
1481
          if(ret == -1){
 
1482
            perror_plus("setenv");
 
1483
            _exit(EX_OSERR);
 
1484
          }
 
1485
        }
 
1486
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1487
          perror_plus("execl");
 
1488
          _exit(EXIT_FAILURE);
 
1489
        }
 
1490
      } else {
 
1491
        int status;
 
1492
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1493
          perror_plus("waitpid");
 
1494
          free(fullname);
 
1495
          continue;
 
1496
        }
 
1497
        if(WIFEXITED(status)){
 
1498
          if(WEXITSTATUS(status) != 0){
 
1499
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1500
                         " with status %d\n", direntry->d_name,
 
1501
                         WEXITSTATUS(status));
 
1502
            free(fullname);
 
1503
            continue;
 
1504
          }
 
1505
        } else if(WIFSIGNALED(status)){
 
1506
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1507
                       " signal %d\n", direntry->d_name,
 
1508
                       WTERMSIG(status));
 
1509
          free(fullname);
 
1510
          continue;
 
1511
        } else {
 
1512
          fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1513
                       " crashed\n", direntry->d_name);
 
1514
          free(fullname);
 
1515
          continue;
 
1516
        }
 
1517
      }
 
1518
      free(fullname);
 
1519
      if(debug){
 
1520
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1521
                     direntry->d_name);
 
1522
      }
 
1523
    }
 
1524
    close(devnull);
 
1525
  }
 
1526
  return true;
 
1527
}
 
1528
 
1357
1529
int main(int argc, char *argv[]){
1358
1530
  AvahiSServiceBrowser *sb = NULL;
1359
1531
  int error;
1439
1611
        .group = 2 },
1440
1612
      { .name = "retry", .key = 132,
1441
1613
        .arg = "SECONDS",
1442
 
        .doc = "Retry interval used when denied by the mandos server",
 
1614
        .doc = "Retry interval used when denied by the Mandos server",
1443
1615
        .group = 2 },
1444
1616
      { .name = "network-hook-dir", .key = 133,
1445
1617
        .arg = "DIR",
1517
1689
        argp_state_help(state, state->out_stream,
1518
1690
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1519
1691
      case 'V':                 /* --version */
1520
 
        fprintf_plus(state->out_stream, "Mandos plugin mandos-client: ");
1521
1692
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1522
1693
        exit(argp_err_exit_status);
1523
1694
        break;
1551
1722
  {
1552
1723
    /* Work around Debian bug #633582:
1553
1724
       <http://bugs.debian.org/633582> */
1554
 
    struct stat st;
1555
1725
    
1556
1726
    /* Re-raise priviliges */
1557
1727
    errno = 0;
1558
1728
    ret = seteuid(0);
1559
1729
    if(ret == -1){
1560
1730
      perror_plus("seteuid");
1561
 
    }
1562
 
    
1563
 
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1564
 
      int seckey_fd = open(seckey, O_RDONLY);
1565
 
      if(seckey_fd == -1){
1566
 
        perror_plus("open");
1567
 
      } else {
1568
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1569
 
        if(ret == -1){
1570
 
          perror_plus("fstat");
1571
 
        } else {
1572
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1573
 
            ret = fchown(seckey_fd, uid, gid);
1574
 
            if(ret == -1){
1575
 
              perror_plus("fchown");
1576
 
            }
1577
 
          }
1578
 
        }
1579
 
        TEMP_FAILURE_RETRY(close(seckey_fd));
1580
 
      }
1581
 
    }
1582
 
    
1583
 
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1584
 
      int pubkey_fd = open(pubkey, O_RDONLY);
1585
 
      if(pubkey_fd == -1){
1586
 
        perror_plus("open");
1587
 
      } else {
1588
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1589
 
        if(ret == -1){
1590
 
          perror_plus("fstat");
1591
 
        } else {
1592
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1593
 
            ret = fchown(pubkey_fd, uid, gid);
1594
 
            if(ret == -1){
1595
 
              perror_plus("fchown");
1596
 
            }
1597
 
          }
1598
 
        }
1599
 
        TEMP_FAILURE_RETRY(close(pubkey_fd));
1600
 
      }
1601
 
    }
1602
 
    
1603
 
    /* Lower privileges */
1604
 
    errno = 0;
1605
 
    ret = seteuid(uid);
1606
 
    if(ret == -1){
1607
 
      perror_plus("seteuid");
1608
 
    }
1609
 
  }
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
1731
    } 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);
 
1732
      struct stat st;
 
1733
      
 
1734
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1735
        int seckey_fd = open(seckey, O_RDONLY);
 
1736
        if(seckey_fd == -1){
 
1737
          perror_plus("open");
 
1738
        } else {
 
1739
          ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1740
          if(ret == -1){
 
1741
            perror_plus("fstat");
 
1742
          } else {
 
1743
            if(S_ISREG(st.st_mode)
 
1744
               and st.st_uid == 0 and st.st_gid == 0){
 
1745
              ret = fchown(seckey_fd, uid, gid);
 
1746
              if(ret == -1){
 
1747
                perror_plus("fchown");
 
1748
              }
 
1749
            }
 
1750
          }
 
1751
          TEMP_FAILURE_RETRY(close(seckey_fd));
 
1752
        }
 
1753
      }
 
1754
    
 
1755
      if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
 
1756
        int pubkey_fd = open(pubkey, O_RDONLY);
 
1757
        if(pubkey_fd == -1){
 
1758
          perror_plus("open");
 
1759
        } else {
 
1760
          ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1761
          if(ret == -1){
 
1762
            perror_plus("fstat");
 
1763
          } else {
 
1764
            if(S_ISREG(st.st_mode)
 
1765
               and st.st_uid == 0 and st.st_gid == 0){
 
1766
              ret = fchown(pubkey_fd, uid, gid);
 
1767
              if(ret == -1){
 
1768
                perror_plus("fchown");
 
1769
              }
 
1770
            }
 
1771
          }
 
1772
          TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1773
        }
 
1774
      }
 
1775
    
 
1776
      /* Lower privileges */
 
1777
      errno = 0;
 
1778
      ret = seteuid(uid);
 
1779
      if(ret == -1){
 
1780
        perror_plus("seteuid");
 
1781
      }
1704
1782
    }
1705
1783
  }
1706
1784
  
 
1785
  /* Run network hooks */
 
1786
  if(not run_network_hooks("start", interface, delay)){
 
1787
    goto end;
 
1788
  }
 
1789
  
1707
1790
  if(not debug){
1708
1791
    avahi_set_log_function(empty_log);
1709
1792
  }
1746
1829
  srand((unsigned int) time(NULL));
1747
1830
  mc.simple_poll = avahi_simple_poll_new();
1748
1831
  if(mc.simple_poll == NULL){
1749
 
    fprintf_plus(stderr, "Avahi: Failed to create simple poll object.\n");
 
1832
    fprintf_plus(stderr,
 
1833
                 "Avahi: Failed to create simple poll object.\n");
1750
1834
    exitcode = EX_UNAVAILABLE;
1751
1835
    goto end;
1752
1836
  }
1944
2028
#endif  /* __linux__ */
1945
2029
    /* Lower privileges */
1946
2030
    errno = 0;
1947
 
    if(take_down_interface){
1948
 
      /* Lower privileges */
1949
 
      ret = seteuid(uid);
1950
 
      if(ret == -1){
1951
 
        perror_plus("seteuid");
1952
 
      }
1953
 
    } else {
1954
 
      /* Lower privileges permanently */
1955
 
      ret = setuid(uid);
1956
 
      if(ret == -1){
1957
 
        perror_plus("setuid");
1958
 
      }
 
2031
    /* Lower privileges */
 
2032
    ret = seteuid(uid);
 
2033
    if(ret == -1){
 
2034
      perror_plus("seteuid");
1959
2035
    }
1960
2036
  }
1961
2037
  
2053
2129
        break;
2054
2130
      }
2055
2131
      if(debug){
2056
 
        fprintf_plus(stderr, "Retrying in %d seconds\n", (int)retry_interval);
 
2132
        fprintf_plus(stderr, "Retrying in %d seconds\n",
 
2133
                     (int)retry_interval);
2057
2134
      }
2058
2135
      sleep((int)retry_interval);
2059
2136
    }
2090
2167
  /* Check if creating the Avahi server object succeeded */
2091
2168
  if(mc.server == NULL){
2092
2169
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2093
 
            avahi_strerror(error));
 
2170
                 avahi_strerror(error));
2094
2171
    exitcode = EX_UNAVAILABLE;
2095
2172
    goto end;
2096
2173
  }
2105
2182
                                   NULL, 0, browse_callback, NULL);
2106
2183
  if(sb == NULL){
2107
2184
    fprintf_plus(stderr, "Failed to create service browser: %s\n",
2108
 
            avahi_strerror(avahi_server_errno(mc.server)));
 
2185
                 avahi_strerror(avahi_server_errno(mc.server)));
2109
2186
    exitcode = EX_UNAVAILABLE;
2110
2187
    goto end;
2111
2188
  }
2124
2201
                                (int)(retry_interval * 1000));
2125
2202
  if(debug){
2126
2203
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2127
 
            (ret == 0) ? "successfully" : "with error");
 
2204
                 (ret == 0) ? "successfully" : "with error");
2128
2205
  }
2129
2206
  
2130
2207
 end:
2152
2229
  if(gpgme_initialized){
2153
2230
    gpgme_release(mc.ctx);
2154
2231
  }
2155
 
 
 
2232
  
2156
2233
  /* Cleans up the circular linked list of Mandos servers the client
2157
2234
     has seen */
2158
2235
  if(mc.current_server != NULL){
2164
2241
    }
2165
2242
  }
2166
2243
  
2167
 
  /* XXX run network hooks "stop" here  */
 
2244
  /* Run network hooks */
 
2245
  run_network_hooks("stop", interface, delay);
2168
2246
  
2169
 
  /* Take down the network interface */
2170
 
  if(take_down_interface){
2171
 
    /* Re-raise priviliges */
 
2247
  /* Re-raise priviliges */
 
2248
  {
2172
2249
    errno = 0;
2173
2250
    ret = seteuid(0);
2174
2251
    if(ret == -1){
2175
2252
      perror_plus("seteuid");
2176
2253
    }
2177
 
    if(geteuid() == 0){
 
2254
    
 
2255
    /* Take down the network interface */
 
2256
    if(take_down_interface and geteuid() == 0){
2178
2257
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2179
2258
      if(ret == -1){
2180
2259
        perror_plus("ioctl SIOCGIFFLAGS");
2189
2268
      if(ret == -1){
2190
2269
        perror_plus("close");
2191
2270
      }
2192
 
      /* Lower privileges permanently */
2193
 
      errno = 0;
2194
 
      ret = setuid(uid);
2195
 
      if(ret == -1){
2196
 
        perror_plus("setuid");
2197
 
      }
2198
2271
    }
2199
2272
  }
 
2273
  /* Lower privileges permanently */
 
2274
  errno = 0;
 
2275
  ret = setuid(uid);
 
2276
  if(ret == -1){
 
2277
    perror_plus("setuid");
 
2278
  }
2200
2279
  
2201
2280
  /* Removes the GPGME temp directory and all files inside */
2202
2281
  if(tempdir_created){
2216
2295
        }
2217
2296
        ret = remove(fullname);
2218
2297
        if(ret == -1){
2219
 
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname, strerror(errno));
 
2298
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
2299
                       strerror(errno));
2220
2300
        }
2221
2301
        free(fullname);
2222
2302
      }