/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-24 19:29:27 UTC
  • mfrom: (505.3.9 client-network-hooks)
  • mto: This revision was merged to the branch mainline in revision 525.
  • Revision ID: belorn@fukt.bsnet.se-20111124192927-drkq3qjxu48aniri
merge

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