86
86
#include <sysexits.h> /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
87
87
EX_NOHOST, EX_IOERR, EX_PROTOCOL */
88
#include <sys/wait.h> /* waitpid(), WIFEXITED(),
89
WEXITSTATUS(), WTERMSIG() */
90
92
#include <sys/klog.h> /* klogctl() */
252
254
rc = gpgme_data_new_from_fd(&pgp_data, fd);
253
255
if(rc != GPG_ERR_NO_ERROR){
254
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
256
fprintf(stderr, "Mandos plugin mandos-client: "
257
"bad gpgme_data_new_from_fd: %s: %s\n",
255
258
gpgme_strsource(rc), gpgme_strerror(rc));
259
262
rc = gpgme_op_import(mc.ctx, pgp_data);
260
263
if(rc != GPG_ERR_NO_ERROR){
261
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
264
fprintf(stderr, "Mandos plugin mandos-client: "
265
"bad gpgme_op_import: %s: %s\n",
262
266
gpgme_strsource(rc), gpgme_strerror(rc));
275
fprintf(stderr, "Initializing GPGME\n");
279
fprintf(stderr, "Mandos plugin mandos-client: "
280
"Initializing GPGME\n");
279
284
gpgme_check_version(NULL);
280
285
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
281
286
if(rc != GPG_ERR_NO_ERROR){
282
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
287
fprintf(stderr, "Mandos plugin mandos-client: "
288
"bad gpgme_engine_check_version: %s: %s\n",
283
289
gpgme_strsource(rc), gpgme_strerror(rc));
287
293
/* Set GPGME home directory for the OpenPGP engine only */
288
294
rc = gpgme_get_engine_info(&engine_info);
289
295
if(rc != GPG_ERR_NO_ERROR){
290
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
296
fprintf(stderr, "Mandos plugin mandos-client: "
297
"bad gpgme_get_engine_info: %s: %s\n",
291
298
gpgme_strsource(rc), gpgme_strerror(rc));
300
307
engine_info = engine_info->next;
302
309
if(engine_info == NULL){
303
fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
310
fprintf(stderr, "Mandos plugin mandos-client: "
311
"Could not set GPGME home dir to %s\n", tempdir);
307
315
/* Create new GPGME "context" */
308
316
rc = gpgme_new(&(mc.ctx));
309
317
if(rc != GPG_ERR_NO_ERROR){
310
fprintf(stderr, "bad gpgme_new: %s: %s\n",
311
gpgme_strsource(rc), gpgme_strerror(rc));
318
fprintf(stderr, "Mandos plugin mandos-client: "
319
"bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
333
342
ssize_t plaintext_length = 0;
336
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
345
fprintf(stderr, "Mandos plugin mandos-client: "
346
"Trying to decrypt OpenPGP data\n");
339
349
/* Create new GPGME data buffer from memory cryptotext */
340
350
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
342
352
if(rc != GPG_ERR_NO_ERROR){
343
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
353
fprintf(stderr, "Mandos plugin mandos-client: "
354
"bad gpgme_data_new_from_mem: %s: %s\n",
344
355
gpgme_strsource(rc), gpgme_strerror(rc));
348
359
/* Create new empty GPGME data buffer for the plaintext */
349
360
rc = gpgme_data_new(&dh_plain);
350
361
if(rc != GPG_ERR_NO_ERROR){
351
fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
362
fprintf(stderr, "Mandos plugin mandos-client: "
363
"bad gpgme_data_new: %s: %s\n",
352
364
gpgme_strsource(rc), gpgme_strerror(rc));
353
365
gpgme_data_release(dh_crypto);
359
371
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
360
372
if(rc != GPG_ERR_NO_ERROR){
361
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
373
fprintf(stderr, "Mandos plugin mandos-client: "
374
"bad gpgme_op_decrypt: %s: %s\n",
362
375
gpgme_strsource(rc), gpgme_strerror(rc));
363
376
plaintext_length = -1;
365
378
gpgme_decrypt_result_t result;
366
379
result = gpgme_op_decrypt_result(mc.ctx);
367
380
if(result == NULL){
368
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
381
fprintf(stderr, "Mandos plugin mandos-client: "
382
"gpgme_op_decrypt_result failed\n");
370
fprintf(stderr, "Unsupported algorithm: %s\n",
384
fprintf(stderr, "Mandos plugin mandos-client: "
385
"Unsupported algorithm: %s\n",
371
386
result->unsupported_algorithm);
372
fprintf(stderr, "Wrong key usage: %u\n",
387
fprintf(stderr, "Mandos plugin mandos-client: "
388
"Wrong key usage: %u\n",
373
389
result->wrong_key_usage);
374
390
if(result->file_name != NULL){
375
fprintf(stderr, "File name: %s\n", result->file_name);
391
fprintf(stderr, "Mandos plugin mandos-client: "
392
"File name: %s\n", result->file_name);
377
394
gpgme_recipient_t recipient;
378
395
recipient = result->recipients;
379
396
while(recipient != NULL){
380
fprintf(stderr, "Public key algorithm: %s\n",
397
fprintf(stderr, "Mandos plugin mandos-client: "
398
"Public key algorithm: %s\n",
381
399
gpgme_pubkey_algo_name(recipient->pubkey_algo));
382
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
383
fprintf(stderr, "Secret key available: %s\n",
400
fprintf(stderr, "Mandos plugin mandos-client: "
401
"Key ID: %s\n", recipient->keyid);
402
fprintf(stderr, "Mandos plugin mandos-client: "
403
"Secret key available: %s\n",
384
404
recipient->status == GPG_ERR_NO_SECKEY
386
406
recipient = recipient->next;
467
fprintf(stderr, "Initializing GnuTLS\n");
489
fprintf(stderr, "Mandos plugin mandos-client: "
490
"Initializing GnuTLS\n");
470
493
ret = gnutls_global_init();
471
494
if(ret != GNUTLS_E_SUCCESS){
472
fprintf(stderr, "GnuTLS global_init: %s\n",
473
safer_gnutls_strerror(ret));
495
fprintf(stderr, "Mandos plugin mandos-client: "
496
"GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
485
508
/* OpenPGP credentials */
486
509
ret = gnutls_certificate_allocate_credentials(&mc.cred);
487
510
if(ret != GNUTLS_E_SUCCESS){
488
fprintf(stderr, "GnuTLS memory error: %s\n",
489
safer_gnutls_strerror(ret));
511
fprintf(stderr, "Mandos plugin mandos-client: "
512
"GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
490
513
gnutls_global_deinit();
495
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
518
fprintf(stderr, "Mandos plugin mandos-client: "
519
"Attempting to use OpenPGP public key %s and"
496
520
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
502
526
GNUTLS_OPENPGP_FMT_BASE64);
503
527
if(ret != GNUTLS_E_SUCCESS){
529
"Mandos plugin mandos-client: "
505
530
"Error[%d] while reading the OpenPGP key pair ('%s',"
506
531
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
507
fprintf(stderr, "The GnuTLS error is: %s\n",
508
safer_gnutls_strerror(ret));
532
fprintf(stderr, "Mandos plugin mandos-client: "
533
"The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
512
537
/* GnuTLS server initialization */
513
538
ret = gnutls_dh_params_init(&mc.dh_params);
514
539
if(ret != GNUTLS_E_SUCCESS){
515
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
540
fprintf(stderr, "Mandos plugin mandos-client: "
541
"Error in GnuTLS DH parameter initialization:"
516
542
" %s\n", safer_gnutls_strerror(ret));
519
545
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
520
546
if(ret != GNUTLS_E_SUCCESS){
521
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
547
fprintf(stderr, "Mandos plugin mandos-client: "
548
"Error in GnuTLS prime generation: %s\n",
522
549
safer_gnutls_strerror(ret));
560
588
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
561
589
if(ret != GNUTLS_E_SUCCESS){
562
fprintf(stderr, "Syntax error at: %s\n", err);
563
fprintf(stderr, "GnuTLS error: %s\n",
564
safer_gnutls_strerror(ret));
590
fprintf(stderr, "Mandos plugin mandos-client: "
591
"Syntax error at: %s\n", err);
592
fprintf(stderr, "Mandos plugin mandos-client: "
593
"GnuTLS error: %s\n", safer_gnutls_strerror(ret));
565
594
gnutls_deinit(*session);
710
744
if(if_indextoname((unsigned int)if_index, interface) == NULL){
711
745
perror_plus("if_indextoname");
713
fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
747
fprintf(stderr, "Mandos plugin mandos-client: "
748
"Connection to: %s%%%s, port %" PRIu16 "\n",
714
749
ip, interface, port);
717
fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
752
fprintf(stderr, "Mandos plugin mandos-client: "
753
"Connection to: %s, port %" PRIu16 "\n", ip, port);
720
755
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
721
756
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
873
911
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
875
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
913
fprintf(stderr, "Mandos plugin mandos-client: "
914
"*** GnuTLS Re-handshake failed ***\n");
876
915
gnutls_perror(ret);
882
fprintf(stderr, "Unknown error while reading data from"
921
fprintf(stderr, "Mandos plugin mandos-client: "
922
"Unknown error while reading data from"
883
923
" encrypted session with Mandos server\n");
884
924
gnutls_bye(session, GNUTLS_SHUT_RDWR);
993
1035
case AVAHI_RESOLVER_FAILURE:
994
fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
1036
fprintf(stderr, "Mandos plugin mandos-client: "
1037
"(Avahi Resolver) Failed to resolve service '%s'"
995
1038
" of type '%s' in domain '%s': %s\n", name, type, domain,
996
1039
avahi_strerror(avahi_server_errno(mc.server)));
1001
1044
char ip[AVAHI_ADDRESS_STR_MAX];
1002
1045
avahi_address_snprint(ip, sizeof(ip), address);
1004
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
1047
fprintf(stderr, "Mandos plugin mandos-client: "
1048
"Mandos server \"%s\" found on %s (%s, %"
1005
1049
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1006
1050
ip, (intmax_t)interface, port);
1055
1100
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1056
1101
name, type, domain, protocol, 0,
1057
1102
resolve_callback, NULL) == NULL)
1058
fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
1103
fprintf(stderr, "Mandos plugin mandos-client: "
1104
"Avahi: Failed to resolve service '%s': %s\n",
1059
1105
name, avahi_strerror(avahi_server_errno(mc.server)));
1110
1157
/* Reject the loopback device */
1111
1158
if(ifr->ifr_flags & IFF_LOOPBACK){
1113
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1160
fprintf(stderr, "Mandos plugin mandos-client: "
1161
"Rejecting loopback interface \"%s\"\n", ifname);
1118
1165
/* Accept point-to-point devices only if connect_to is specified */
1119
1166
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1121
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1168
fprintf(stderr, "Mandos plugin mandos-client: "
1169
"Accepting point-to-point interface \"%s\"\n", ifname);
1126
1173
/* Otherwise, reject non-broadcast-capable devices */
1127
1174
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1129
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1176
fprintf(stderr, "Mandos plugin mandos-client: "
1177
"Rejecting non-broadcast interface \"%s\"\n", ifname);
1134
1181
/* Reject non-ARP interfaces (including dummy interfaces) */
1135
1182
if(ifr->ifr_flags & IFF_NOARP){
1137
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1184
fprintf(stderr, "Mandos plugin mandos-client: "
1185
"Rejecting non-ARP interface \"%s\"\n", ifname);
1142
1190
/* Accept this device */
1144
fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1192
fprintf(stderr, "Mandos plugin mandos-client: "
1193
"Interface \"%s\" is good\n", ifname);
1279
1332
if(mc.current_server == NULL){
1334
fprintf(stderr, "Mandos plugin mandos-client: "
1282
1335
"Wait until first server is found. No timeout!\n");
1284
1337
ret = avahi_simple_poll_iterate(s, -1);
1287
fprintf(stderr, "Check current_server if we should run it,"
1340
fprintf(stderr, "Mandos plugin mandos-client: "
1341
"Check current_server if we should run it,"
1290
1344
/* the current time */
1496
1551
argp_state_help(state, state->out_stream,
1497
1552
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1498
1553
case 'V': /* --version */
1554
fprintf(state->out_stream, "Mandos plugin mandos-client: ");
1499
1555
fprintf(state->out_stream, "%s\n", argp_program_version);
1500
1556
exit(argp_err_exit_status);
1592
1648
struct dirent *direntry;
1593
1649
int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
1595
int devnull = open("/dev/null", O_RDONLY);
1596
for(int i = 0; i < numhooks; i++){
1597
direntry = direntries[0];
1598
char *fullname = NULL;
1599
ret = asprintf(&fullname, "%s/%s", tempdir,
1602
perror_plus("asprintf");
1605
pid_t hook_pid = fork();
1608
dup2(devnull, STDIN_FILENO);
1610
dup2(STDERR_FILENO, STDOUT_FILENO);
1611
ret = setenv("DEVICE", interface, 1);
1613
perror_plus("setenv");
1616
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1618
perror_plus("setenv");
1621
ret = setenv("MODE", "start", 1);
1623
perror_plus("setenv");
1627
ret = asprintf(&delaystring, "%f", delay);
1652
perror_plus("scandir");
1654
int devnull = open("/dev/null", O_RDONLY);
1655
for(int i = 0; i < numhooks; i++){
1656
direntry = direntries[0];
1657
char *fullname = NULL;
1658
ret = asprintf(&fullname, "%s/%s", tempdir,
1629
1661
perror_plus("asprintf");
1632
ret = setenv("DELAY", delaystring, 1);
1664
pid_t hook_pid = fork();
1667
dup2(devnull, STDIN_FILENO);
1669
dup2(STDERR_FILENO, STDOUT_FILENO);
1670
ret = setenv("DEVICE", interface, 1);
1672
perror_plus("setenv");
1675
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1677
perror_plus("setenv");
1680
ret = setenv("MODE", "start", 1);
1682
perror_plus("setenv");
1686
ret = asprintf(&delaystring, "%f", delay);
1688
perror_plus("asprintf");
1691
ret = setenv("DELAY", delaystring, 1);
1694
perror_plus("setenv");
1634
1697
free(delaystring);
1635
perror_plus("setenv");
1639
ret = execl(fullname, direntry->d_name, "start", NULL);
1640
perror_plus("execl");
1698
ret = execl(fullname, direntry->d_name, "start", NULL);
1699
perror_plus("execl");
1702
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1703
perror_plus("waitpid");
1707
if(WIFEXITED(status)){
1708
if(WEXITSTATUS(status) != 0){
1709
fprintf(stderr, "Mandos plugin mandos-client: "
1710
"Warning: network hook \"%s\" exited"
1711
" with status %d\n", direntry->d_name,
1712
WEXITSTATUS(status));
1716
} else if(WIFSIGNALED(status)){
1717
fprintf(stderr, "Mandos plugin mandos-client: "
1718
"Warning: network hook \"%s\" died by"
1719
" signal %d\n", direntry->d_name,
1724
fprintf(stderr, "Mandos plugin mandos-client: "
1725
"Warning: network hook \"%s\" crashed\n",
1761
1854
if(strcmp(interface, "none") != 0){
1762
1855
if_index = (AvahiIfIndex) if_nametoindex(interface);
1763
1856
if(if_index == 0){
1764
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1857
fprintf(stderr, "Mandos plugin mandos-client: "
1858
"No such interface: \"%s\"\n", interface);
1765
1859
exitcode = EX_UNAVAILABLE;
2048
2147
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2049
2148
NULL, 0, browse_callback, NULL);
2050
2149
if(sb == NULL){
2051
fprintf(stderr, "Failed to create service browser: %s\n",
2150
fprintf(stderr, "Mandos plugin mandos-client: "
2151
"Failed to create service browser: %s\n",
2052
2152
avahi_strerror(avahi_server_errno(mc.server)));
2053
2153
exitcode = EX_UNAVAILABLE;
2061
2161
/* Run the main loop */
2064
fprintf(stderr, "Starting Avahi loop search\n");
2164
fprintf(stderr, "Mandos plugin mandos-client: "
2165
"Starting Avahi loop search\n");
2067
2168
ret = avahi_loop_with_timeout(mc.simple_poll,
2068
2169
(int)(retry_interval * 1000));
2070
fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
2171
fprintf(stderr, "Mandos plugin mandos-client: "
2172
"avahi_loop_with_timeout exited %s\n",
2071
2173
(ret == 0) ? "successfully" : "with error");
2077
fprintf(stderr, "%s exiting\n", argv[0]);
2179
fprintf(stderr, "Mandos plugin mandos-client: "
2180
"%s exiting\n", argv[0]);
2080
2183
/* Cleanup things */