/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:
178
178
  va_list ap;
179
179
  va_start (ap, format);
180
180
  
181
 
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
182
 
                             program_invocation_short_name));
 
181
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name));
183
182
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
184
183
}
185
184
 
263
262
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
264
263
    if(rc != GPG_ERR_NO_ERROR){
265
264
      fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
266
 
                   gpgme_strsource(rc), gpgme_strerror(rc));
 
265
              gpgme_strsource(rc), gpgme_strerror(rc));
267
266
      return false;
268
267
    }
269
268
    
270
269
    rc = gpgme_op_import(mc.ctx, pgp_data);
271
270
    if(rc != GPG_ERR_NO_ERROR){
272
271
      fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
273
 
                   gpgme_strsource(rc), gpgme_strerror(rc));
 
272
              gpgme_strsource(rc), gpgme_strerror(rc));
274
273
      return false;
275
274
    }
276
275
    
291
290
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
292
291
  if(rc != GPG_ERR_NO_ERROR){
293
292
    fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
294
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
293
            gpgme_strsource(rc), gpgme_strerror(rc));
295
294
    return false;
296
295
  }
297
296
  
299
298
  rc = gpgme_get_engine_info(&engine_info);
300
299
  if(rc != GPG_ERR_NO_ERROR){
301
300
    fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
302
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
301
            gpgme_strsource(rc), gpgme_strerror(rc));
303
302
    return false;
304
303
  }
305
304
  while(engine_info != NULL){
311
310
    engine_info = engine_info->next;
312
311
  }
313
312
  if(engine_info == NULL){
314
 
    fprintf_plus(stderr, "Could not set GPGME home dir to %s\n",
315
 
                 tempdir);
 
313
    fprintf_plus(stderr, "Could not set GPGME home dir to %s\n", tempdir);
316
314
    return false;
317
315
  }
318
316
  
320
318
  rc = gpgme_new(&(mc.ctx));
321
319
  if(rc != GPG_ERR_NO_ERROR){
322
320
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
323
 
                 "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
324
 
                 gpgme_strerror(rc));
 
321
            "bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
 
322
            gpgme_strerror(rc));
325
323
    return false;
326
324
  }
327
325
  
354
352
                               0);
355
353
  if(rc != GPG_ERR_NO_ERROR){
356
354
    fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
357
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
355
            gpgme_strsource(rc), gpgme_strerror(rc));
358
356
    return -1;
359
357
  }
360
358
  
362
360
  rc = gpgme_data_new(&dh_plain);
363
361
  if(rc != GPG_ERR_NO_ERROR){
364
362
    fprintf_plus(stderr, "Mandos plugin mandos-client: "
365
 
                 "bad gpgme_data_new: %s: %s\n",
366
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
363
            "bad gpgme_data_new: %s: %s\n",
 
364
            gpgme_strsource(rc), gpgme_strerror(rc));
367
365
    gpgme_data_release(dh_crypto);
368
366
    return -1;
369
367
  }
373
371
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
374
372
  if(rc != GPG_ERR_NO_ERROR){
375
373
    fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
376
 
                 gpgme_strsource(rc), gpgme_strerror(rc));
 
374
            gpgme_strsource(rc), gpgme_strerror(rc));
377
375
    plaintext_length = -1;
378
376
    if(debug){
379
377
      gpgme_decrypt_result_t result;
382
380
        fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
383
381
      } else {
384
382
        fprintf_plus(stderr, "Unsupported algorithm: %s\n",
385
 
                     result->unsupported_algorithm);
 
383
                result->unsupported_algorithm);
386
384
        fprintf_plus(stderr, "Wrong key usage: %u\n",
387
 
                     result->wrong_key_usage);
 
385
                result->wrong_key_usage);
388
386
        if(result->file_name != NULL){
389
387
          fprintf_plus(stderr, "File name: %s\n", result->file_name);
390
388
        }
392
390
        recipient = result->recipients;
393
391
        while(recipient != NULL){
394
392
          fprintf_plus(stderr, "Public key algorithm: %s\n",
395
 
                       gpgme_pubkey_algo_name
396
 
                       (recipient->pubkey_algo));
 
393
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
397
394
          fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
398
395
          fprintf_plus(stderr, "Secret key available: %s\n",
399
 
                       recipient->status == GPG_ERR_NO_SECKEY
400
 
                       ? "No" : "Yes");
 
396
                  recipient->status == GPG_ERR_NO_SECKEY
 
397
                  ? "No" : "Yes");
401
398
          recipient = recipient->next;
402
399
        }
403
400
      }
484
481
  
485
482
  ret = gnutls_global_init();
486
483
  if(ret != GNUTLS_E_SUCCESS){
487
 
    fprintf_plus(stderr, "GnuTLS global_init: %s\n",
488
 
                 safer_gnutls_strerror(ret));
 
484
    fprintf_plus(stderr, "GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
489
485
    return -1;
490
486
  }
491
487
  
500
496
  /* OpenPGP credentials */
501
497
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
502
498
  if(ret != GNUTLS_E_SUCCESS){
503
 
    fprintf_plus(stderr, "GnuTLS memory error: %s\n",
504
 
                 safer_gnutls_strerror(ret));
 
499
    fprintf_plus(stderr, "GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
505
500
    gnutls_global_deinit();
506
501
    return -1;
507
502
  }
508
503
  
509
504
  if(debug){
510
505
    fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
511
 
                 " secret key %s as GnuTLS credentials\n",
512
 
                 pubkeyfilename,
513
 
                 seckeyfilename);
 
506
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
 
507
            seckeyfilename);
514
508
  }
515
509
  
516
510
  ret = gnutls_certificate_set_openpgp_key_file
520
514
    fprintf_plus(stderr,
521
515
                 "Error[%d] while reading the OpenPGP key pair ('%s',"
522
516
                 " '%s')\n", ret, pubkeyfilename, seckeyfilename);
523
 
    fprintf_plus(stderr, "The GnuTLS error is: %s\n",
524
 
                 safer_gnutls_strerror(ret));
 
517
    fprintf_plus(stderr, "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
525
518
    goto globalfail;
526
519
  }
527
520
  
528
521
  /* GnuTLS server initialization */
529
522
  ret = gnutls_dh_params_init(&mc.dh_params);
530
523
  if(ret != GNUTLS_E_SUCCESS){
531
 
    fprintf_plus(stderr, "Error in GnuTLS DH parameter"
532
 
                 " initialization: %s\n",
533
 
                 safer_gnutls_strerror(ret));
 
524
    fprintf_plus(stderr, "Error in GnuTLS DH parameter initialization:"
 
525
            " %s\n", safer_gnutls_strerror(ret));
534
526
    goto globalfail;
535
527
  }
536
528
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
537
529
  if(ret != GNUTLS_E_SUCCESS){
538
530
    fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
539
 
                 safer_gnutls_strerror(ret));
 
531
            safer_gnutls_strerror(ret));
540
532
    goto globalfail;
541
533
  }
542
534
  
562
554
    }
563
555
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
564
556
  if(ret != GNUTLS_E_SUCCESS){
565
 
    fprintf_plus(stderr,
566
 
                 "Error in GnuTLS session initialization: %s\n",
567
 
                 safer_gnutls_strerror(ret));
 
557
    fprintf_plus(stderr, "Error in GnuTLS session initialization: %s\n",
 
558
            safer_gnutls_strerror(ret));
568
559
  }
569
560
  
570
561
  {
578
569
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
579
570
    if(ret != GNUTLS_E_SUCCESS){
580
571
      fprintf_plus(stderr, "Syntax error at: %s\n", err);
581
 
      fprintf_plus(stderr, "GnuTLS error: %s\n",
582
 
                   safer_gnutls_strerror(ret));
 
572
      fprintf_plus(stderr, "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
583
573
      gnutls_deinit(*session);
584
574
      return -1;
585
575
    }
595
585
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
596
586
  if(ret != GNUTLS_E_SUCCESS){
597
587
    fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
598
 
                 safer_gnutls_strerror(ret));
 
588
            safer_gnutls_strerror(ret));
599
589
    gnutls_deinit(*session);
600
590
    return -1;
601
591
  }
657
647
  }
658
648
  
659
649
  if(debug){
660
 
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
661
 
                 PRIu16 "\n", ip, port);
 
650
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %" PRIu16
 
651
            "\n", ip, port);
662
652
  }
663
653
  
664
654
  tcp_sd = socket(pf, SOCK_STREAM, 0);
703
693
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
704
694
                                -Wunreachable-code*/
705
695
      if(if_index == AVAHI_IF_UNSPEC){
706
 
        fprintf_plus(stderr, "An IPv6 link-local address is"
707
 
                     " incomplete without a network interface\n");
 
696
        fprintf_plus(stderr, "An IPv6 link-local address is incomplete"
 
697
                " without a network interface\n");
708
698
        errno = EINVAL;
709
699
        goto mandos_end;
710
700
      }
728
718
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
729
719
        perror_plus("if_indextoname");
730
720
      } else {
731
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
732
 
                     "\n", ip, interface, port);
 
721
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
 
722
                ip, interface, port);
733
723
      }
734
724
    } else {
735
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
736
 
                   ip, port);
 
725
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n", ip, port);
737
726
    }
738
727
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
739
728
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
845
834
  /* Read OpenPGP packet that contains the wanted password */
846
835
  
847
836
  if(debug){
848
 
    fprintf_plus(stderr, "Retrieving OpenPGP encrypted password from"
849
 
                 " %s\n", ip);
 
837
    fprintf_plus(stderr, "Retrieving OpenPGP encrypted password from %s\n", ip);
850
838
  }
851
839
  
852
840
  while(true){
890
878
          }
891
879
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
892
880
        if(ret < 0){
893
 
          fprintf_plus(stderr, "*** GnuTLS Re-handshake failed "
894
 
                       "***\n");
 
881
          fprintf_plus(stderr, "*** GnuTLS Re-handshake failed ***\n");
895
882
          gnutls_perror(ret);
896
883
          errno = EPROTO;
897
884
          goto mandos_end;
899
886
        break;
900
887
      default:
901
888
        fprintf_plus(stderr, "Unknown error while reading data from"
902
 
                     " encrypted session with Mandos server\n");
 
889
                " encrypted session with Mandos server\n");
903
890
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
904
891
        errno = EIO;
905
892
        goto mandos_end;
946
933
          int e = errno;
947
934
          if(debug){
948
935
            fprintf_plus(stderr, "Error writing encrypted data: %s\n",
949
 
                         strerror(errno));
 
936
                    strerror(errno));
950
937
          }
951
938
          errno = e;
952
939
          goto mandos_end;
1009
996
  switch(event){
1010
997
  default:
1011
998
  case AVAHI_RESOLVER_FAILURE:
1012
 
    fprintf_plus(stderr, "(Avahi Resolver) Failed to resolve service "
1013
 
                 "'%s' of type '%s' in domain '%s': %s\n", name, type,
1014
 
                 domain,
1015
 
                 avahi_strerror(avahi_server_errno(mc.server)));
 
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)));
1016
1002
    break;
1017
1003
    
1018
1004
  case AVAHI_RESOLVER_FOUND:
1021
1007
      avahi_address_snprint(ip, sizeof(ip), address);
1022
1008
      if(debug){
1023
1009
        fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
1024
 
                     PRIdMAX ") on port %" PRIu16 "\n", name,
1025
 
                     host_name, ip, (intmax_t)interface, port);
 
1010
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
 
1011
                ip, (intmax_t)interface, port);
1026
1012
      }
1027
1013
      int ret = start_mandos_communication(ip, port, interface,
1028
1014
                                           avahi_proto_to_af(proto));
1061
1047
  case AVAHI_BROWSER_FAILURE:
1062
1048
    
1063
1049
    fprintf_plus(stderr, "(Avahi browser) %s\n",
1064
 
                 avahi_strerror(avahi_server_errno(mc.server)));
 
1050
            avahi_strerror(avahi_server_errno(mc.server)));
1065
1051
    avahi_simple_poll_quit(mc.simple_poll);
1066
1052
    return;
1067
1053
    
1074
1060
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1075
1061
                                    name, type, domain, protocol, 0,
1076
1062
                                    resolve_callback, NULL) == NULL)
1077
 
      fprintf_plus(stderr, "Avahi: Failed to resolve service '%s':"
1078
 
                   " %s\n", name,
1079
 
                   avahi_strerror(avahi_server_errno(mc.server)));
 
1063
      fprintf_plus(stderr, "Avahi: Failed to resolve service '%s': %s\n",
 
1064
              name, avahi_strerror(avahi_server_errno(mc.server)));
1080
1065
    break;
1081
1066
    
1082
1067
  case AVAHI_BROWSER_REMOVE:
1085
1070
  case AVAHI_BROWSER_ALL_FOR_NOW:
1086
1071
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
1087
1072
    if(debug){
1088
 
      fprintf_plus(stderr, "No Mandos server found, still"
1089
 
                   " searching...\n");
 
1073
      fprintf_plus(stderr, "No Mandos server found, still searching...\n");
1090
1074
    }
1091
1075
    break;
1092
1076
  }
1131
1115
  /* Reject the loopback device */
1132
1116
  if(ifr->ifr_flags & IFF_LOOPBACK){
1133
1117
    if(debug){
1134
 
      fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1135
 
                   ifname);
 
1118
      fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n", ifname);
1136
1119
    }
1137
1120
    return false;
1138
1121
  }
1139
1122
  /* Accept point-to-point devices only if connect_to is specified */
1140
1123
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1141
1124
    if(debug){
1142
 
      fprintf_plus(stderr, "Accepting point-to-point interface"
1143
 
                   " \"%s\"\n", ifname);
 
1125
      fprintf_plus(stderr, "Accepting point-to-point interface \"%s\"\n", ifname);
1144
1126
    }
1145
1127
    return true;
1146
1128
  }
1147
1129
  /* Otherwise, reject non-broadcast-capable devices */
1148
1130
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1149
1131
    if(debug){
1150
 
      fprintf_plus(stderr, "Rejecting non-broadcast interface"
1151
 
                   " \"%s\"\n", ifname);
 
1132
      fprintf_plus(stderr, "Rejecting non-broadcast interface \"%s\"\n", ifname);
1152
1133
    }
1153
1134
    return false;
1154
1135
  }
1155
1136
  /* Reject non-ARP interfaces (including dummy interfaces) */
1156
1137
  if(ifr->ifr_flags & IFF_NOARP){
1157
1138
    if(debug){
1158
 
      fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1159
 
                   ifname);
 
1139
      fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1160
1140
    }
1161
1141
    return false;
1162
1142
  }
1181
1161
  struct ifreq ifr;
1182
1162
  if(not get_flags(if_entry->d_name, &ifr)){
1183
1163
    if(debug){
1184
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1185
 
                   "\"%s\"\n", if_entry->d_name);
 
1164
      fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
 
1165
              if_entry->d_name);
1186
1166
    }
1187
1167
    return 0;
1188
1168
  }
1206
1186
  struct ifreq ifr;
1207
1187
  if(not get_flags(if_entry->d_name, &ifr)){
1208
1188
    if(debug){
1209
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1210
 
                   "\"%s\"\n", if_entry->d_name);
 
1189
      fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
 
1190
              if_entry->d_name);
1211
1191
    }
1212
1192
    return 0;
1213
1193
  }
1216
1196
  if(not (ifr.ifr_flags & IFF_UP)){
1217
1197
    if(debug){
1218
1198
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1219
 
                   if_entry->d_name);
 
1199
              if_entry->d_name);
1220
1200
    }
1221
1201
    return 0;
1222
1202
  }
1225
1205
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1226
1206
    if(debug){
1227
1207
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1228
 
                   if_entry->d_name);
 
1208
              if_entry->d_name);
1229
1209
    }
1230
1210
    return 0;
1231
1211
  }
1266
1246
    /* Contains non-allowed characters */
1267
1247
    if(debug){
1268
1248
      fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1269
 
                   direntry->d_name);
 
1249
              direntry->d_name);
1270
1250
    }
1271
1251
    return 0;
1272
1252
  }
1289
1269
    /* Not a regular file */
1290
1270
    if(debug){
1291
1271
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1292
 
                   direntry->d_name);
 
1272
              direntry->d_name);
1293
1273
    }
1294
1274
    return 0;
1295
1275
  }
1297
1277
    /* Not executable */
1298
1278
    if(debug){
1299
1279
      fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1300
 
                   direntry->d_name);
 
1280
              direntry->d_name);
1301
1281
    }
1302
1282
    return 0;
1303
1283
  }
1313
1293
  while(true){
1314
1294
    if(mc.current_server == NULL){
1315
1295
      if (debug){
1316
 
        fprintf_plus(stderr, "Wait until first server is found."
1317
 
                     " No timeout!\n");
 
1296
        fprintf_plus(stderr, "Wait until first server is found. No timeout!\n");
1318
1297
      }
1319
1298
      ret = avahi_simple_poll_iterate(s, -1);
1320
1299
    } else {
1321
1300
      if (debug){
1322
 
        fprintf_plus(stderr, "Check current_server if we should run"
1323
 
                     " it, or wait\n");
 
1301
        fprintf_plus(stderr, "Check current_server if we should run it,"
 
1302
                " or wait\n");
1324
1303
      }
1325
1304
      /* the current time */
1326
1305
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1342
1321
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1343
1322
      
1344
1323
      if (debug){
1345
 
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1346
 
                     block_time);
 
1324
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1347
1325
      }
1348
1326
      
1349
1327
      if(block_time <= 0){
1376
1354
  }
1377
1355
}
1378
1356
 
1379
 
bool run_network_hooks(const char *mode, const char *interface,
1380
 
                       const float delay){
1381
 
  struct dirent **direntries;
1382
 
  struct dirent *direntry;
1383
 
  int ret;
1384
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1385
 
                         alphasort);
1386
 
  if(numhooks == -1){
1387
 
    perror_plus("scandir");
1388
 
  } else {
1389
 
    int devnull = open("/dev/null", O_RDONLY);
1390
 
    for(int i = 0; i < numhooks; i++){
1391
 
      direntry = direntries[0];
1392
 
      char *fullname = NULL;
1393
 
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1394
 
      if(ret < 0){
1395
 
        perror_plus("asprintf");
1396
 
        continue;
1397
 
      }
1398
 
      pid_t hook_pid = fork();
1399
 
      if(hook_pid == 0){
1400
 
        /* Child */
1401
 
        dup2(devnull, STDIN_FILENO);
1402
 
        close(devnull);
1403
 
        dup2(STDERR_FILENO, STDOUT_FILENO);
1404
 
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1405
 
        if(ret == -1){
1406
 
          perror_plus("setenv");
1407
 
          return false;
1408
 
        }
1409
 
        ret = setenv("DEVICE", interface, 1);
1410
 
        if(ret == -1){
1411
 
          perror_plus("setenv");
1412
 
          return false;
1413
 
        }
1414
 
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1415
 
        if(ret == -1){
1416
 
          perror_plus("setenv");
1417
 
          return false;
1418
 
        }
1419
 
        ret = setenv("MODE", mode, 1);
1420
 
        if(ret == -1){
1421
 
          perror_plus("setenv");
1422
 
          return false;
1423
 
        }
1424
 
        char *delaystring;
1425
 
        ret = asprintf(&delaystring, "%f", delay);
1426
 
        if(ret == -1){
1427
 
          perror_plus("asprintf");
1428
 
          return false;
1429
 
        }
1430
 
        ret = setenv("DELAY", delaystring, 1);
1431
 
        if(ret == -1){
1432
 
          free(delaystring);
1433
 
          perror_plus("setenv");
1434
 
          return false;
1435
 
        }
1436
 
        free(delaystring);
1437
 
        ret = execl(fullname, direntry->d_name, mode, NULL);
1438
 
        perror_plus("execl");
1439
 
      } else {
1440
 
        int status;
1441
 
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1442
 
          perror_plus("waitpid");
1443
 
          free(fullname);
1444
 
          continue;
1445
 
        }
1446
 
        if(WIFEXITED(status)){
1447
 
          if(WEXITSTATUS(status) != 0){
1448
 
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1449
 
                         " with status %d\n", direntry->d_name,
1450
 
                         WEXITSTATUS(status));
1451
 
            free(fullname);
1452
 
            continue;
1453
 
          }
1454
 
        } else if(WIFSIGNALED(status)){
1455
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1456
 
                       " signal %d\n", direntry->d_name,
1457
 
                       WTERMSIG(status));
1458
 
          free(fullname);
1459
 
          continue;
1460
 
        } else {
1461
 
          fprintf_plus(stderr, "Warning: network hook \"%s\""
1462
 
                       " crashed\n", direntry->d_name);
1463
 
          free(fullname);
1464
 
          continue;
1465
 
        }
1466
 
      }
1467
 
      free(fullname);
1468
 
      if(quit_now){
1469
 
        break;
1470
 
      }
1471
 
    }
1472
 
    close(devnull);
1473
 
  }
1474
 
  return true;
1475
 
}
1476
 
 
1477
1357
int main(int argc, char *argv[]){
1478
1358
  AvahiSServiceBrowser *sb = NULL;
1479
1359
  int error;
1637
1517
        argp_state_help(state, state->out_stream,
1638
1518
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1639
1519
      case 'V':                 /* --version */
1640
 
        fprintf_plus(state->out_stream,
1641
 
                     "Mandos plugin mandos-client: ");
 
1520
        fprintf_plus(state->out_stream, "Mandos plugin mandos-client: ");
1642
1521
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1643
1522
        exit(argp_err_exit_status);
1644
1523
        break;
1690
1569
        if(ret == -1){
1691
1570
          perror_plus("fstat");
1692
1571
        } else {
1693
 
          if(S_ISREG(st.st_mode)
1694
 
             and st.st_uid == 0 and st.st_gid == 0){
 
1572
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1695
1573
            ret = fchown(seckey_fd, uid, gid);
1696
1574
            if(ret == -1){
1697
1575
              perror_plus("fchown");
1711
1589
        if(ret == -1){
1712
1590
          perror_plus("fstat");
1713
1591
        } else {
1714
 
          if(S_ISREG(st.st_mode)
1715
 
             and st.st_uid == 0 and st.st_gid == 0){
 
1592
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1716
1593
            ret = fchown(pubkey_fd, uid, gid);
1717
1594
            if(ret == -1){
1718
1595
              perror_plus("fchown");
1731
1608
    }
1732
1609
  }
1733
1610
  
1734
 
  /* Run network hooks */
 
1611
  /* Find network hooks and run them */
1735
1612
  {
1736
 
    /* Re-raise priviliges */
1737
 
    errno = 0;
1738
 
    ret = seteuid(0);
1739
 
    if(ret == -1){
1740
 
      perror_plus("seteuid");
1741
 
    }
1742
 
    if(not run_network_hooks("start", interface, delay)){
1743
 
      goto end;
1744
 
    }
1745
 
    /* Lower privileges */
1746
 
    errno = 0;
1747
 
    ret = seteuid(uid);
1748
 
    if(ret == -1){
1749
 
      perror_plus("seteuid");
 
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);
1750
1704
    }
1751
1705
  }
1752
1706
  
1792
1746
  srand((unsigned int) time(NULL));
1793
1747
  mc.simple_poll = avahi_simple_poll_new();
1794
1748
  if(mc.simple_poll == NULL){
1795
 
    fprintf_plus(stderr,
1796
 
                 "Avahi: Failed to create simple poll object.\n");
 
1749
    fprintf_plus(stderr, "Avahi: Failed to create simple poll object.\n");
1797
1750
    exitcode = EX_UNAVAILABLE;
1798
1751
    goto end;
1799
1752
  }
1991
1944
#endif  /* __linux__ */
1992
1945
    /* Lower privileges */
1993
1946
    errno = 0;
1994
 
    /* Lower privileges */
1995
 
    ret = seteuid(uid);
1996
 
    if(ret == -1){
1997
 
      perror_plus("seteuid");
 
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
      }
1998
1959
    }
1999
1960
  }
2000
1961
  
2092
2053
        break;
2093
2054
      }
2094
2055
      if(debug){
2095
 
        fprintf_plus(stderr, "Retrying in %d seconds\n",
2096
 
                     (int)retry_interval);
 
2056
        fprintf_plus(stderr, "Retrying in %d seconds\n", (int)retry_interval);
2097
2057
      }
2098
2058
      sleep((int)retry_interval);
2099
2059
    }
2130
2090
  /* Check if creating the Avahi server object succeeded */
2131
2091
  if(mc.server == NULL){
2132
2092
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2133
 
                 avahi_strerror(error));
 
2093
            avahi_strerror(error));
2134
2094
    exitcode = EX_UNAVAILABLE;
2135
2095
    goto end;
2136
2096
  }
2145
2105
                                   NULL, 0, browse_callback, NULL);
2146
2106
  if(sb == NULL){
2147
2107
    fprintf_plus(stderr, "Failed to create service browser: %s\n",
2148
 
                 avahi_strerror(avahi_server_errno(mc.server)));
 
2108
            avahi_strerror(avahi_server_errno(mc.server)));
2149
2109
    exitcode = EX_UNAVAILABLE;
2150
2110
    goto end;
2151
2111
  }
2164
2124
                                (int)(retry_interval * 1000));
2165
2125
  if(debug){
2166
2126
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2167
 
                 (ret == 0) ? "successfully" : "with error");
 
2127
            (ret == 0) ? "successfully" : "with error");
2168
2128
  }
2169
2129
  
2170
2130
 end:
2204
2164
    }
2205
2165
  }
2206
2166
  
2207
 
  /* Re-raise priviliges */
2208
 
  {
 
2167
  /* XXX run network hooks "stop" here  */
 
2168
  
 
2169
  /* Take down the network interface */
 
2170
  if(take_down_interface){
 
2171
    /* Re-raise priviliges */
2209
2172
    errno = 0;
2210
2173
    ret = seteuid(0);
2211
2174
    if(ret == -1){
2212
2175
      perror_plus("seteuid");
2213
2176
    }
2214
 
    /* Run network hooks */
2215
 
    if(not run_network_hooks("stop", interface, delay)){
2216
 
      goto end;
2217
 
    }
2218
 
    
2219
 
    /* Take down the network interface */
2220
 
    if(take_down_interface and geteuid() == 0){
 
2177
    if(geteuid() == 0){
2221
2178
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2222
2179
      if(ret == -1){
2223
2180
        perror_plus("ioctl SIOCGIFFLAGS");
2232
2189
      if(ret == -1){
2233
2190
        perror_plus("close");
2234
2191
      }
 
2192
      /* Lower privileges permanently */
 
2193
      errno = 0;
 
2194
      ret = setuid(uid);
 
2195
      if(ret == -1){
 
2196
        perror_plus("setuid");
 
2197
      }
2235
2198
    }
2236
2199
  }
2237
 
  /* Lower privileges permanently */
2238
 
  errno = 0;
2239
 
  ret = setuid(uid);
2240
 
  if(ret == -1){
2241
 
    perror_plus("setuid");
2242
 
  }
2243
2200
  
2244
2201
  /* Removes the GPGME temp directory and all files inside */
2245
2202
  if(tempdir_created){
2259
2216
        }
2260
2217
        ret = remove(fullname);
2261
2218
        if(ret == -1){
2262
 
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2263
 
                       strerror(errno));
 
2219
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname, strerror(errno));
2264
2220
        }
2265
2221
        free(fullname);
2266
2222
      }