53
53
sockaddr_in6, PF_INET6,
54
54
SOCK_STREAM, uid_t, gid_t, open(),
56
#include <sys/stat.h> /* open(), S_ISREG */
56
#include <sys/stat.h> /* open() */
57
57
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
58
58
inet_pton(), connect() */
59
59
#include <fcntl.h> /* open() */
255
251
rc = gpgme_data_new_from_fd(&pgp_data, fd);
256
252
if(rc != GPG_ERR_NO_ERROR){
257
fprintf(stderr, "Mandos plugin mandos-client: "
258
"bad gpgme_data_new_from_fd: %s: %s\n",
253
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
259
254
gpgme_strsource(rc), gpgme_strerror(rc));
263
258
rc = gpgme_op_import(mc.ctx, pgp_data);
264
259
if(rc != GPG_ERR_NO_ERROR){
265
fprintf(stderr, "Mandos plugin mandos-client: "
266
"bad gpgme_op_import: %s: %s\n",
260
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
267
261
gpgme_strsource(rc), gpgme_strerror(rc));
280
fprintf(stderr, "Mandos plugin mandos-client: "
281
"Initializing GPGME\n");
274
fprintf(stderr, "Initializing GPGME\n");
285
278
gpgme_check_version(NULL);
286
279
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
287
280
if(rc != GPG_ERR_NO_ERROR){
288
fprintf(stderr, "Mandos plugin mandos-client: "
289
"bad gpgme_engine_check_version: %s: %s\n",
281
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
290
282
gpgme_strsource(rc), gpgme_strerror(rc));
294
286
/* Set GPGME home directory for the OpenPGP engine only */
295
287
rc = gpgme_get_engine_info(&engine_info);
296
288
if(rc != GPG_ERR_NO_ERROR){
297
fprintf(stderr, "Mandos plugin mandos-client: "
298
"bad gpgme_get_engine_info: %s: %s\n",
289
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
299
290
gpgme_strsource(rc), gpgme_strerror(rc));
308
299
engine_info = engine_info->next;
310
301
if(engine_info == NULL){
311
fprintf(stderr, "Mandos plugin mandos-client: "
312
"Could not set GPGME home dir to %s\n", tempdir);
302
fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
316
306
/* Create new GPGME "context" */
317
307
rc = gpgme_new(&(mc.ctx));
318
308
if(rc != GPG_ERR_NO_ERROR){
319
fprintf(stderr, "Mandos plugin mandos-client: "
320
"bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
309
fprintf(stderr, "bad gpgme_new: %s: %s\n",
310
gpgme_strsource(rc), gpgme_strerror(rc));
343
332
ssize_t plaintext_length = 0;
346
fprintf(stderr, "Mandos plugin mandos-client: "
347
"Trying to decrypt OpenPGP data\n");
335
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
350
338
/* Create new GPGME data buffer from memory cryptotext */
351
339
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
353
341
if(rc != GPG_ERR_NO_ERROR){
354
fprintf(stderr, "Mandos plugin mandos-client: "
355
"bad gpgme_data_new_from_mem: %s: %s\n",
342
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
356
343
gpgme_strsource(rc), gpgme_strerror(rc));
360
347
/* Create new empty GPGME data buffer for the plaintext */
361
348
rc = gpgme_data_new(&dh_plain);
362
349
if(rc != GPG_ERR_NO_ERROR){
363
fprintf(stderr, "Mandos plugin mandos-client: "
364
"bad gpgme_data_new: %s: %s\n",
350
fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
365
351
gpgme_strsource(rc), gpgme_strerror(rc));
366
352
gpgme_data_release(dh_crypto);
372
358
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
373
359
if(rc != GPG_ERR_NO_ERROR){
374
fprintf(stderr, "Mandos plugin mandos-client: "
375
"bad gpgme_op_decrypt: %s: %s\n",
360
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
376
361
gpgme_strsource(rc), gpgme_strerror(rc));
377
362
plaintext_length = -1;
379
364
gpgme_decrypt_result_t result;
380
365
result = gpgme_op_decrypt_result(mc.ctx);
381
366
if(result == NULL){
382
fprintf(stderr, "Mandos plugin mandos-client: "
383
"gpgme_op_decrypt_result failed\n");
367
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
385
fprintf(stderr, "Mandos plugin mandos-client: "
386
"Unsupported algorithm: %s\n",
369
fprintf(stderr, "Unsupported algorithm: %s\n",
387
370
result->unsupported_algorithm);
388
fprintf(stderr, "Mandos plugin mandos-client: "
389
"Wrong key usage: %u\n",
371
fprintf(stderr, "Wrong key usage: %u\n",
390
372
result->wrong_key_usage);
391
373
if(result->file_name != NULL){
392
fprintf(stderr, "Mandos plugin mandos-client: "
393
"File name: %s\n", result->file_name);
374
fprintf(stderr, "File name: %s\n", result->file_name);
395
376
gpgme_recipient_t recipient;
396
377
recipient = result->recipients;
397
378
while(recipient != NULL){
398
fprintf(stderr, "Mandos plugin mandos-client: "
399
"Public key algorithm: %s\n",
379
fprintf(stderr, "Public key algorithm: %s\n",
400
380
gpgme_pubkey_algo_name(recipient->pubkey_algo));
401
fprintf(stderr, "Mandos plugin mandos-client: "
402
"Key ID: %s\n", recipient->keyid);
403
fprintf(stderr, "Mandos plugin mandos-client: "
404
"Secret key available: %s\n",
381
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
382
fprintf(stderr, "Secret key available: %s\n",
405
383
recipient->status == GPG_ERR_NO_SECKEY
407
385
recipient = recipient->next;
490
fprintf(stderr, "Mandos plugin mandos-client: "
491
"Initializing GnuTLS\n");
466
fprintf(stderr, "Initializing GnuTLS\n");
494
469
ret = gnutls_global_init();
495
470
if(ret != GNUTLS_E_SUCCESS){
496
fprintf(stderr, "Mandos plugin mandos-client: "
497
"GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
471
fprintf(stderr, "GnuTLS global_init: %s\n",
472
safer_gnutls_strerror(ret));
509
484
/* OpenPGP credentials */
510
485
ret = gnutls_certificate_allocate_credentials(&mc.cred);
511
486
if(ret != GNUTLS_E_SUCCESS){
512
fprintf(stderr, "Mandos plugin mandos-client: "
513
"GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
487
fprintf(stderr, "GnuTLS memory error: %s\n",
488
safer_gnutls_strerror(ret));
514
489
gnutls_global_deinit();
519
fprintf(stderr, "Mandos plugin mandos-client: "
520
"Attempting to use OpenPGP public key %s and"
494
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
521
495
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
527
501
GNUTLS_OPENPGP_FMT_BASE64);
528
502
if(ret != GNUTLS_E_SUCCESS){
530
"Mandos plugin mandos-client: "
531
504
"Error[%d] while reading the OpenPGP key pair ('%s',"
532
505
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
533
fprintf(stderr, "Mandos plugin mandos-client: "
534
"The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
506
fprintf(stderr, "The GnuTLS error is: %s\n",
507
safer_gnutls_strerror(ret));
538
511
/* GnuTLS server initialization */
539
512
ret = gnutls_dh_params_init(&mc.dh_params);
540
513
if(ret != GNUTLS_E_SUCCESS){
541
fprintf(stderr, "Mandos plugin mandos-client: "
542
"Error in GnuTLS DH parameter initialization:"
514
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
543
515
" %s\n", safer_gnutls_strerror(ret));
546
518
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
547
519
if(ret != GNUTLS_E_SUCCESS){
548
fprintf(stderr, "Mandos plugin mandos-client: "
549
"Error in GnuTLS prime generation: %s\n",
520
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
550
521
safer_gnutls_strerror(ret));
589
559
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
590
560
if(ret != GNUTLS_E_SUCCESS){
591
fprintf(stderr, "Mandos plugin mandos-client: "
592
"Syntax error at: %s\n", err);
593
fprintf(stderr, "Mandos plugin mandos-client: "
594
"GnuTLS error: %s\n", safer_gnutls_strerror(ret));
561
fprintf(stderr, "Syntax error at: %s\n", err);
562
fprintf(stderr, "GnuTLS error: %s\n",
563
safer_gnutls_strerror(ret));
595
564
gnutls_deinit(*session);
745
709
if(if_indextoname((unsigned int)if_index, interface) == NULL){
746
710
perror_plus("if_indextoname");
748
fprintf(stderr, "Mandos plugin mandos-client: "
749
"Connection to: %s%%%s, port %" PRIu16 "\n",
712
fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
750
713
ip, interface, port);
753
fprintf(stderr, "Mandos plugin mandos-client: "
754
"Connection to: %s, port %" PRIu16 "\n", ip, port);
716
fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
756
719
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
757
720
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
912
872
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
914
fprintf(stderr, "Mandos plugin mandos-client: "
915
"*** GnuTLS Re-handshake failed ***\n");
874
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
916
875
gnutls_perror(ret);
922
fprintf(stderr, "Mandos plugin mandos-client: "
923
"Unknown error while reading data from"
881
fprintf(stderr, "Unknown error while reading data from"
924
882
" encrypted session with Mandos server\n");
925
883
gnutls_bye(session, GNUTLS_SHUT_RDWR);
1036
992
case AVAHI_RESOLVER_FAILURE:
1037
fprintf(stderr, "Mandos plugin mandos-client: "
1038
"(Avahi Resolver) Failed to resolve service '%s'"
993
fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
1039
994
" of type '%s' in domain '%s': %s\n", name, type, domain,
1040
995
avahi_strerror(avahi_server_errno(mc.server)));
1045
1000
char ip[AVAHI_ADDRESS_STR_MAX];
1046
1001
avahi_address_snprint(ip, sizeof(ip), address);
1048
fprintf(stderr, "Mandos plugin mandos-client: "
1049
"Mandos server \"%s\" found on %s (%s, %"
1003
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
1050
1004
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1051
1005
ip, (intmax_t)interface, port);
1101
1054
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1102
1055
name, type, domain, protocol, 0,
1103
1056
resolve_callback, NULL) == NULL)
1104
fprintf(stderr, "Mandos plugin mandos-client: "
1105
"Avahi: Failed to resolve service '%s': %s\n",
1057
fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
1106
1058
name, avahi_strerror(avahi_server_errno(mc.server)));
1158
1109
/* Reject the loopback device */
1159
1110
if(ifr->ifr_flags & IFF_LOOPBACK){
1161
fprintf(stderr, "Mandos plugin mandos-client: "
1162
"Rejecting loopback interface \"%s\"\n", ifname);
1112
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1166
1117
/* Accept point-to-point devices only if connect_to is specified */
1167
1118
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1169
fprintf(stderr, "Mandos plugin mandos-client: "
1170
"Accepting point-to-point interface \"%s\"\n", ifname);
1120
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1174
1125
/* Otherwise, reject non-broadcast-capable devices */
1175
1126
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1177
fprintf(stderr, "Mandos plugin mandos-client: "
1178
"Rejecting non-broadcast interface \"%s\"\n", ifname);
1128
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1182
1133
/* Reject non-ARP interfaces (including dummy interfaces) */
1183
1134
if(ifr->ifr_flags & IFF_NOARP){
1185
fprintf(stderr, "Mandos plugin mandos-client: "
1186
"Rejecting non-ARP interface \"%s\"\n", ifname);
1136
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1191
1141
/* Accept this device */
1193
fprintf(stderr, "Mandos plugin mandos-client: "
1194
"Interface \"%s\" is good\n", ifname);
1143
fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1202
1151
* (This function is passed to scandir(3) as a filter function.)
1204
1153
int good_interface(const struct dirent *if_entry){
1205
1155
if(if_entry->d_name[0] == '.'){
1209
1158
struct ifreq ifr;
1210
if(not get_flags(if_entry->d_name, &ifr)){
1212
fprintf(stderr, "Mandos plugin mandos-client: "
1213
"Failed to get flags for interface \"%s\"\n",
1219
if(not good_flags(if_entry->d_name, &ifr)){
1226
* This function determines if a directory entry in /sys/class/net
1227
* corresponds to an acceptable network device which is up.
1228
* (This function is passed to scandir(3) as a filter function.)
1230
int up_interface(const struct dirent *if_entry){
1231
if(if_entry->d_name[0] == '.'){
1236
1160
if(not get_flags(if_entry->d_name, &ifr)){
1238
fprintf(stderr, "Mandos plugin mandos-client: "
1239
"Failed to get flags for interface \"%s\"\n",
1245
/* Reject down interfaces */
1246
if(not (ifr.ifr_flags & IFF_UP)){
1248
fprintf(stderr, "Mandos plugin mandos-client: "
1249
"Rejecting down interface \"%s\"\n",
1255
/* Reject non-running interfaces */
1256
if(not (ifr.ifr_flags & IFF_RUNNING)){
1258
fprintf(stderr, "Mandos plugin mandos-client: "
1259
"Rejecting non-running interface \"%s\"\n",
1282
/* Is this directory entry a runnable program? */
1283
int runnable_hook(const struct dirent *direntry){
1287
if((direntry->d_name)[0] == '\0'){
1292
/* Save pointer to last character */
1293
char *end = strchr(direntry->d_name, '\0')-1;
1300
if(((direntry->d_name)[0] == '#')
1302
/* Temporary #name# */
1306
/* XXX more rules here */
1308
char *fullname = NULL;
1309
ret = asprintf(&fullname, "%s/%s", hookdir,
1312
perror_plus("asprintf");
1316
ret = stat(fullname, &st);
1319
perror_plus("Could not stat plugin");
1323
if(not (S_ISREG(st.st_mode))){
1324
/* Not a regular file */
1327
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1328
/* Not executable */
1334
1181
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1336
1183
struct timespec now;
1341
1188
if(mc.current_server == NULL){
1343
fprintf(stderr, "Mandos plugin mandos-client: "
1344
1191
"Wait until first server is found. No timeout!\n");
1346
1193
ret = avahi_simple_poll_iterate(s, -1);
1349
fprintf(stderr, "Mandos plugin mandos-client: "
1350
"Check current_server if we should run it,"
1196
fprintf(stderr, "Check current_server if we should run it,"
1353
1199
/* the current time */
1661
/* Find network hooks and run them */
1663
struct dirent **direntries;
1664
struct dirent *direntry;
1665
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1668
perror_plus("scandir");
1670
int devnull = open("/dev/null", O_RDONLY);
1671
for(int i = 0; i < numhooks; i++){
1672
direntry = direntries[0];
1673
char *fullname = NULL;
1674
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1676
perror_plus("asprintf");
1679
pid_t hook_pid = fork();
1682
dup2(devnull, STDIN_FILENO);
1684
dup2(STDERR_FILENO, STDOUT_FILENO);
1685
ret = setenv("DEVICE", interface, 1);
1687
perror_plus("setenv");
1690
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1692
perror_plus("setenv");
1695
ret = setenv("MODE", "start", 1);
1697
perror_plus("setenv");
1701
ret = asprintf(&delaystring, "%f", delay);
1703
perror_plus("asprintf");
1706
ret = setenv("DELAY", delaystring, 1);
1709
perror_plus("setenv");
1713
ret = execl(fullname, direntry->d_name, "start", NULL);
1714
perror_plus("execl");
1717
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1718
perror_plus("waitpid");
1722
if(WIFEXITED(status)){
1723
if(WEXITSTATUS(status) != 0){
1724
fprintf(stderr, "Mandos plugin mandos-client: "
1725
"Warning: network hook \"%s\" exited"
1726
" with status %d\n", direntry->d_name,
1727
WEXITSTATUS(status));
1731
} else if(WIFSIGNALED(status)){
1732
fprintf(stderr, "Mandos plugin mandos-client: "
1733
"Warning: network hook \"%s\" died by"
1734
" signal %d\n", direntry->d_name,
1739
fprintf(stderr, "Mandos plugin mandos-client: "
1740
"Warning: network hook \"%s\" crashed\n",
1756
1499
avahi_set_log_function(empty_log);
1759
1502
if(interface[0] == '\0'){
1760
1503
struct dirent **direntries;
1761
/* First look for interfaces that are up */
1762
ret = scandir(sys_class_net, &direntries, up_interface,
1504
ret = scandir(sys_class_net, &direntries, good_interface,
1765
/* No up interfaces, look for any good interfaces */
1767
ret = scandir(sys_class_net, &direntries, good_interface,
1771
/* Pick the first interface returned */
1507
/* Pick the first good interface */
1772
1508
interface = strdup(direntries[0]->d_name);
1774
fprintf(stderr, "Mandos plugin mandos-client: "
1775
"Using interface \"%s\"\n", interface);
1510
fprintf(stderr, "Using interface \"%s\"\n", interface);
1777
1512
if(interface == NULL){
1778
1513
perror_plus("malloc");
2162
1889
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2163
1890
NULL, 0, browse_callback, NULL);
2164
1891
if(sb == NULL){
2165
fprintf(stderr, "Mandos plugin mandos-client: "
2166
"Failed to create service browser: %s\n",
1892
fprintf(stderr, "Failed to create service browser: %s\n",
2167
1893
avahi_strerror(avahi_server_errno(mc.server)));
2168
1894
exitcode = EX_UNAVAILABLE;
2176
1902
/* Run the main loop */
2179
fprintf(stderr, "Mandos plugin mandos-client: "
2180
"Starting Avahi loop search\n");
1905
fprintf(stderr, "Starting Avahi loop search\n");
2183
1908
ret = avahi_loop_with_timeout(mc.simple_poll,
2184
1909
(int)(retry_interval * 1000));
2186
fprintf(stderr, "Mandos plugin mandos-client: "
2187
"avahi_loop_with_timeout exited %s\n",
1911
fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
2188
1912
(ret == 0) ? "successfully" : "with error");
2194
fprintf(stderr, "Mandos plugin mandos-client: "
2195
"%s exiting\n", argv[0]);
1918
fprintf(stderr, "%s exiting\n", argv[0]);
2198
1921
/* Cleanup things */