53
53
sockaddr_in6, PF_INET6,
54
54
SOCK_STREAM, uid_t, gid_t, open(),
56
#include <sys/stat.h> /* open() */
56
#include <sys/stat.h> /* open(), S_ISREG */
57
57
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
58
58
inet_pton(), connect() */
59
59
#include <fcntl.h> /* open() */
251
254
rc = gpgme_data_new_from_fd(&pgp_data, fd);
252
255
if(rc != GPG_ERR_NO_ERROR){
253
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",
254
258
gpgme_strsource(rc), gpgme_strerror(rc));
258
262
rc = gpgme_op_import(mc.ctx, pgp_data);
259
263
if(rc != GPG_ERR_NO_ERROR){
260
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
264
fprintf(stderr, "Mandos plugin mandos-client: "
265
"bad gpgme_op_import: %s: %s\n",
261
266
gpgme_strsource(rc), gpgme_strerror(rc));
274
fprintf(stderr, "Initializing GPGME\n");
279
fprintf(stderr, "Mandos plugin mandos-client: "
280
"Initializing GPGME\n");
278
284
gpgme_check_version(NULL);
279
285
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
280
286
if(rc != GPG_ERR_NO_ERROR){
281
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",
282
289
gpgme_strsource(rc), gpgme_strerror(rc));
286
293
/* Set GPGME home directory for the OpenPGP engine only */
287
294
rc = gpgme_get_engine_info(&engine_info);
288
295
if(rc != GPG_ERR_NO_ERROR){
289
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",
290
298
gpgme_strsource(rc), gpgme_strerror(rc));
299
307
engine_info = engine_info->next;
301
309
if(engine_info == NULL){
302
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);
306
315
/* Create new GPGME "context" */
307
316
rc = gpgme_new(&(mc.ctx));
308
317
if(rc != GPG_ERR_NO_ERROR){
309
fprintf(stderr, "bad gpgme_new: %s: %s\n",
310
gpgme_strsource(rc), gpgme_strerror(rc));
318
fprintf(stderr, "Mandos plugin mandos-client: "
319
"bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
332
342
ssize_t plaintext_length = 0;
335
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
345
fprintf(stderr, "Mandos plugin mandos-client: "
346
"Trying to decrypt OpenPGP data\n");
338
349
/* Create new GPGME data buffer from memory cryptotext */
339
350
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
341
352
if(rc != GPG_ERR_NO_ERROR){
342
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",
343
355
gpgme_strsource(rc), gpgme_strerror(rc));
347
359
/* Create new empty GPGME data buffer for the plaintext */
348
360
rc = gpgme_data_new(&dh_plain);
349
361
if(rc != GPG_ERR_NO_ERROR){
350
fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
362
fprintf(stderr, "Mandos plugin mandos-client: "
363
"bad gpgme_data_new: %s: %s\n",
351
364
gpgme_strsource(rc), gpgme_strerror(rc));
352
365
gpgme_data_release(dh_crypto);
358
371
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
359
372
if(rc != GPG_ERR_NO_ERROR){
360
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
373
fprintf(stderr, "Mandos plugin mandos-client: "
374
"bad gpgme_op_decrypt: %s: %s\n",
361
375
gpgme_strsource(rc), gpgme_strerror(rc));
362
376
plaintext_length = -1;
364
378
gpgme_decrypt_result_t result;
365
379
result = gpgme_op_decrypt_result(mc.ctx);
366
380
if(result == NULL){
367
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
381
fprintf(stderr, "Mandos plugin mandos-client: "
382
"gpgme_op_decrypt_result failed\n");
369
fprintf(stderr, "Unsupported algorithm: %s\n",
384
fprintf(stderr, "Mandos plugin mandos-client: "
385
"Unsupported algorithm: %s\n",
370
386
result->unsupported_algorithm);
371
fprintf(stderr, "Wrong key usage: %u\n",
387
fprintf(stderr, "Mandos plugin mandos-client: "
388
"Wrong key usage: %u\n",
372
389
result->wrong_key_usage);
373
390
if(result->file_name != NULL){
374
fprintf(stderr, "File name: %s\n", result->file_name);
391
fprintf(stderr, "Mandos plugin mandos-client: "
392
"File name: %s\n", result->file_name);
376
394
gpgme_recipient_t recipient;
377
395
recipient = result->recipients;
378
396
while(recipient != NULL){
379
fprintf(stderr, "Public key algorithm: %s\n",
397
fprintf(stderr, "Mandos plugin mandos-client: "
398
"Public key algorithm: %s\n",
380
399
gpgme_pubkey_algo_name(recipient->pubkey_algo));
381
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
382
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",
383
404
recipient->status == GPG_ERR_NO_SECKEY
385
406
recipient = recipient->next;
466
fprintf(stderr, "Initializing GnuTLS\n");
489
fprintf(stderr, "Mandos plugin mandos-client: "
490
"Initializing GnuTLS\n");
469
493
ret = gnutls_global_init();
470
494
if(ret != GNUTLS_E_SUCCESS){
471
fprintf(stderr, "GnuTLS global_init: %s\n",
472
safer_gnutls_strerror(ret));
495
fprintf(stderr, "Mandos plugin mandos-client: "
496
"GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
484
508
/* OpenPGP credentials */
485
509
ret = gnutls_certificate_allocate_credentials(&mc.cred);
486
510
if(ret != GNUTLS_E_SUCCESS){
487
fprintf(stderr, "GnuTLS memory error: %s\n",
488
safer_gnutls_strerror(ret));
511
fprintf(stderr, "Mandos plugin mandos-client: "
512
"GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
489
513
gnutls_global_deinit();
494
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"
495
520
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
501
526
GNUTLS_OPENPGP_FMT_BASE64);
502
527
if(ret != GNUTLS_E_SUCCESS){
529
"Mandos plugin mandos-client: "
504
530
"Error[%d] while reading the OpenPGP key pair ('%s',"
505
531
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
506
fprintf(stderr, "The GnuTLS error is: %s\n",
507
safer_gnutls_strerror(ret));
532
fprintf(stderr, "Mandos plugin mandos-client: "
533
"The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
511
537
/* GnuTLS server initialization */
512
538
ret = gnutls_dh_params_init(&mc.dh_params);
513
539
if(ret != GNUTLS_E_SUCCESS){
514
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
540
fprintf(stderr, "Mandos plugin mandos-client: "
541
"Error in GnuTLS DH parameter initialization:"
515
542
" %s\n", safer_gnutls_strerror(ret));
518
545
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
519
546
if(ret != GNUTLS_E_SUCCESS){
520
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
547
fprintf(stderr, "Mandos plugin mandos-client: "
548
"Error in GnuTLS prime generation: %s\n",
521
549
safer_gnutls_strerror(ret));
559
588
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
560
589
if(ret != GNUTLS_E_SUCCESS){
561
fprintf(stderr, "Syntax error at: %s\n", err);
562
fprintf(stderr, "GnuTLS error: %s\n",
563
safer_gnutls_strerror(ret));
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));
564
594
gnutls_deinit(*session);
709
744
if(if_indextoname((unsigned int)if_index, interface) == NULL){
710
745
perror_plus("if_indextoname");
712
fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
747
fprintf(stderr, "Mandos plugin mandos-client: "
748
"Connection to: %s%%%s, port %" PRIu16 "\n",
713
749
ip, interface, port);
716
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);
719
755
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
720
756
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
872
911
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
874
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
913
fprintf(stderr, "Mandos plugin mandos-client: "
914
"*** GnuTLS Re-handshake failed ***\n");
875
915
gnutls_perror(ret);
881
fprintf(stderr, "Unknown error while reading data from"
921
fprintf(stderr, "Mandos plugin mandos-client: "
922
"Unknown error while reading data from"
882
923
" encrypted session with Mandos server\n");
883
924
gnutls_bye(session, GNUTLS_SHUT_RDWR);
992
1035
case AVAHI_RESOLVER_FAILURE:
993
fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
1036
fprintf(stderr, "Mandos plugin mandos-client: "
1037
"(Avahi Resolver) Failed to resolve service '%s'"
994
1038
" of type '%s' in domain '%s': %s\n", name, type, domain,
995
1039
avahi_strerror(avahi_server_errno(mc.server)));
1000
1044
char ip[AVAHI_ADDRESS_STR_MAX];
1001
1045
avahi_address_snprint(ip, sizeof(ip), address);
1003
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
1047
fprintf(stderr, "Mandos plugin mandos-client: "
1048
"Mandos server \"%s\" found on %s (%s, %"
1004
1049
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1005
1050
ip, (intmax_t)interface, port);
1054
1100
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1055
1101
name, type, domain, protocol, 0,
1056
1102
resolve_callback, NULL) == NULL)
1057
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",
1058
1105
name, avahi_strerror(avahi_server_errno(mc.server)));
1109
1157
/* Reject the loopback device */
1110
1158
if(ifr->ifr_flags & IFF_LOOPBACK){
1112
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1160
fprintf(stderr, "Mandos plugin mandos-client: "
1161
"Rejecting loopback interface \"%s\"\n", ifname);
1117
1165
/* Accept point-to-point devices only if connect_to is specified */
1118
1166
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1120
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);
1125
1173
/* Otherwise, reject non-broadcast-capable devices */
1126
1174
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1128
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1176
fprintf(stderr, "Mandos plugin mandos-client: "
1177
"Rejecting non-broadcast interface \"%s\"\n", ifname);
1133
1181
/* Reject non-ARP interfaces (including dummy interfaces) */
1134
1182
if(ifr->ifr_flags & IFF_NOARP){
1136
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1184
fprintf(stderr, "Mandos plugin mandos-client: "
1185
"Rejecting non-ARP interface \"%s\"\n", ifname);
1141
1190
/* Accept this device */
1143
fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1192
fprintf(stderr, "Mandos plugin mandos-client: "
1193
"Interface \"%s\" is good\n", ifname);
1151
1201
* (This function is passed to scandir(3) as a filter function.)
1153
1203
int good_interface(const struct dirent *if_entry){
1155
1204
if(if_entry->d_name[0] == '.'){
1158
1208
struct ifreq ifr;
1209
if(not get_flags(if_entry->d_name, &ifr)){
1211
fprintf(stderr, "Mandos plugin mandos-client: "
1212
"Failed to get flags for interface \"%s\"\n",
1218
if(not good_flags(if_entry->d_name, &ifr)){
1225
* This function determines if a directory entry in /sys/class/net
1226
* corresponds to an acceptable network device which is up.
1227
* (This function is passed to scandir(3) as a filter function.)
1229
int up_interface(const struct dirent *if_entry){
1230
if(if_entry->d_name[0] == '.'){
1160
1235
if(not get_flags(if_entry->d_name, &ifr)){
1237
fprintf(stderr, "Mandos plugin mandos-client: "
1238
"Failed to get flags for interface \"%s\"\n",
1244
/* Reject down interfaces */
1245
if(not (ifr.ifr_flags & IFF_UP)){
1247
fprintf(stderr, "Mandos plugin mandos-client: "
1248
"Rejecting down interface \"%s\"\n",
1254
/* Reject non-running interfaces */
1255
if(not (ifr.ifr_flags & IFF_RUNNING)){
1257
fprintf(stderr, "Mandos plugin mandos-client: "
1258
"Rejecting non-running interface \"%s\"\n",
1281
/* Is this directory entry a runnable program? */
1282
int runnable_hook(const struct dirent *direntry){
1286
if((direntry->d_name)[0] == '\0'){
1291
/* Save pointer to last character */
1292
char *end = strchr(direntry->d_name, '\0')-1;
1299
if(((direntry->d_name)[0] == '#')
1301
/* Temporary #name# */
1305
/* XXX more rules here */
1307
ret = stat(direntry->d_name, &st);
1310
perror_plus("Could not stat plugin");
1314
if(not (S_ISREG(st.st_mode))){
1315
/* Not a regular file */
1318
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1319
/* Not executable */
1181
1325
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1183
1327
struct timespec now;
1188
1332
if(mc.current_server == NULL){
1334
fprintf(stderr, "Mandos plugin mandos-client: "
1191
1335
"Wait until first server is found. No timeout!\n");
1193
1337
ret = avahi_simple_poll_iterate(s, -1);
1196
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,"
1199
1344
/* the current time */
1645
/* Find network hooks and run them */
1647
struct dirent **direntries;
1648
struct dirent *direntry;
1649
int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
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,
1661
perror_plus("asprintf");
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");
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",
1499
1741
avahi_set_log_function(empty_log);
1502
1744
if(interface[0] == '\0'){
1503
1745
struct dirent **direntries;
1504
ret = scandir(sys_class_net, &direntries, good_interface,
1746
/* First look for interfaces that are up */
1747
ret = scandir(sys_class_net, &direntries, up_interface,
1750
/* No up interfaces, look for any good interfaces */
1752
ret = scandir(sys_class_net, &direntries, good_interface,
1507
/* Pick the first good interface */
1756
/* Pick the first interface returned */
1508
1757
interface = strdup(direntries[0]->d_name);
1510
fprintf(stderr, "Using interface \"%s\"\n", interface);
1759
fprintf(stderr, "Mandos plugin mandos-client: "
1760
"Using interface \"%s\"\n", interface);
1512
1762
if(interface == NULL){
1513
1763
perror_plus("malloc");
1889
2147
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1890
2148
NULL, 0, browse_callback, NULL);
1891
2149
if(sb == NULL){
1892
fprintf(stderr, "Failed to create service browser: %s\n",
2150
fprintf(stderr, "Mandos plugin mandos-client: "
2151
"Failed to create service browser: %s\n",
1893
2152
avahi_strerror(avahi_server_errno(mc.server)));
1894
2153
exitcode = EX_UNAVAILABLE;
1902
2161
/* Run the main loop */
1905
fprintf(stderr, "Starting Avahi loop search\n");
2164
fprintf(stderr, "Mandos plugin mandos-client: "
2165
"Starting Avahi loop search\n");
1908
2168
ret = avahi_loop_with_timeout(mc.simple_poll,
1909
2169
(int)(retry_interval * 1000));
1911
fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
2171
fprintf(stderr, "Mandos plugin mandos-client: "
2172
"avahi_loop_with_timeout exited %s\n",
1912
2173
(ret == 0) ? "successfully" : "with error");
1918
fprintf(stderr, "%s exiting\n", argv[0]);
2179
fprintf(stderr, "Mandos plugin mandos-client: "
2180
"%s exiting\n", argv[0]);
1921
2183
/* Cleanup things */