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
255
rc = gpgme_data_new_from_fd(&pgp_data, fd);
252
256
if(rc != GPG_ERR_NO_ERROR){
253
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
257
fprintf(stderr, "Mandos plugin mandos-client: "
258
"bad gpgme_data_new_from_fd: %s: %s\n",
254
259
gpgme_strsource(rc), gpgme_strerror(rc));
258
263
rc = gpgme_op_import(mc.ctx, pgp_data);
259
264
if(rc != GPG_ERR_NO_ERROR){
260
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
265
fprintf(stderr, "Mandos plugin mandos-client: "
266
"bad gpgme_op_import: %s: %s\n",
261
267
gpgme_strsource(rc), gpgme_strerror(rc));
274
fprintf(stderr, "Initializing GPGME\n");
280
fprintf(stderr, "Mandos plugin mandos-client: "
281
"Initializing GPGME\n");
278
285
gpgme_check_version(NULL);
279
286
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
280
287
if(rc != GPG_ERR_NO_ERROR){
281
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
288
fprintf(stderr, "Mandos plugin mandos-client: "
289
"bad gpgme_engine_check_version: %s: %s\n",
282
290
gpgme_strsource(rc), gpgme_strerror(rc));
286
294
/* Set GPGME home directory for the OpenPGP engine only */
287
295
rc = gpgme_get_engine_info(&engine_info);
288
296
if(rc != GPG_ERR_NO_ERROR){
289
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
297
fprintf(stderr, "Mandos plugin mandos-client: "
298
"bad gpgme_get_engine_info: %s: %s\n",
290
299
gpgme_strsource(rc), gpgme_strerror(rc));
299
308
engine_info = engine_info->next;
301
310
if(engine_info == NULL){
302
fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
311
fprintf(stderr, "Mandos plugin mandos-client: "
312
"Could not set GPGME home dir to %s\n", tempdir);
306
316
/* Create new GPGME "context" */
307
317
rc = gpgme_new(&(mc.ctx));
308
318
if(rc != GPG_ERR_NO_ERROR){
309
fprintf(stderr, "bad gpgme_new: %s: %s\n",
310
gpgme_strsource(rc), gpgme_strerror(rc));
319
fprintf(stderr, "Mandos plugin mandos-client: "
320
"bad gpgme_new: %s: %s\n", gpgme_strsource(rc),
332
343
ssize_t plaintext_length = 0;
335
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
346
fprintf(stderr, "Mandos plugin mandos-client: "
347
"Trying to decrypt OpenPGP data\n");
338
350
/* Create new GPGME data buffer from memory cryptotext */
339
351
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
341
353
if(rc != GPG_ERR_NO_ERROR){
342
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
354
fprintf(stderr, "Mandos plugin mandos-client: "
355
"bad gpgme_data_new_from_mem: %s: %s\n",
343
356
gpgme_strsource(rc), gpgme_strerror(rc));
347
360
/* Create new empty GPGME data buffer for the plaintext */
348
361
rc = gpgme_data_new(&dh_plain);
349
362
if(rc != GPG_ERR_NO_ERROR){
350
fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
363
fprintf(stderr, "Mandos plugin mandos-client: "
364
"bad gpgme_data_new: %s: %s\n",
351
365
gpgme_strsource(rc), gpgme_strerror(rc));
352
366
gpgme_data_release(dh_crypto);
358
372
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
359
373
if(rc != GPG_ERR_NO_ERROR){
360
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
374
fprintf(stderr, "Mandos plugin mandos-client: "
375
"bad gpgme_op_decrypt: %s: %s\n",
361
376
gpgme_strsource(rc), gpgme_strerror(rc));
362
377
plaintext_length = -1;
364
379
gpgme_decrypt_result_t result;
365
380
result = gpgme_op_decrypt_result(mc.ctx);
366
381
if(result == NULL){
367
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
382
fprintf(stderr, "Mandos plugin mandos-client: "
383
"gpgme_op_decrypt_result failed\n");
369
fprintf(stderr, "Unsupported algorithm: %s\n",
385
fprintf(stderr, "Mandos plugin mandos-client: "
386
"Unsupported algorithm: %s\n",
370
387
result->unsupported_algorithm);
371
fprintf(stderr, "Wrong key usage: %u\n",
388
fprintf(stderr, "Mandos plugin mandos-client: "
389
"Wrong key usage: %u\n",
372
390
result->wrong_key_usage);
373
391
if(result->file_name != NULL){
374
fprintf(stderr, "File name: %s\n", result->file_name);
392
fprintf(stderr, "Mandos plugin mandos-client: "
393
"File name: %s\n", result->file_name);
376
395
gpgme_recipient_t recipient;
377
396
recipient = result->recipients;
378
397
while(recipient != NULL){
379
fprintf(stderr, "Public key algorithm: %s\n",
398
fprintf(stderr, "Mandos plugin mandos-client: "
399
"Public key algorithm: %s\n",
380
400
gpgme_pubkey_algo_name(recipient->pubkey_algo));
381
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
382
fprintf(stderr, "Secret key available: %s\n",
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",
383
405
recipient->status == GPG_ERR_NO_SECKEY
385
407
recipient = recipient->next;
466
fprintf(stderr, "Initializing GnuTLS\n");
490
fprintf(stderr, "Mandos plugin mandos-client: "
491
"Initializing GnuTLS\n");
469
494
ret = gnutls_global_init();
470
495
if(ret != GNUTLS_E_SUCCESS){
471
fprintf(stderr, "GnuTLS global_init: %s\n",
472
safer_gnutls_strerror(ret));
496
fprintf(stderr, "Mandos plugin mandos-client: "
497
"GnuTLS global_init: %s\n", safer_gnutls_strerror(ret));
484
509
/* OpenPGP credentials */
485
510
ret = gnutls_certificate_allocate_credentials(&mc.cred);
486
511
if(ret != GNUTLS_E_SUCCESS){
487
fprintf(stderr, "GnuTLS memory error: %s\n",
488
safer_gnutls_strerror(ret));
512
fprintf(stderr, "Mandos plugin mandos-client: "
513
"GnuTLS memory error: %s\n", safer_gnutls_strerror(ret));
489
514
gnutls_global_deinit();
494
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
519
fprintf(stderr, "Mandos plugin mandos-client: "
520
"Attempting to use OpenPGP public key %s and"
495
521
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
501
527
GNUTLS_OPENPGP_FMT_BASE64);
502
528
if(ret != GNUTLS_E_SUCCESS){
530
"Mandos plugin mandos-client: "
504
531
"Error[%d] while reading the OpenPGP key pair ('%s',"
505
532
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
506
fprintf(stderr, "The GnuTLS error is: %s\n",
507
safer_gnutls_strerror(ret));
533
fprintf(stderr, "Mandos plugin mandos-client: "
534
"The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
511
538
/* GnuTLS server initialization */
512
539
ret = gnutls_dh_params_init(&mc.dh_params);
513
540
if(ret != GNUTLS_E_SUCCESS){
514
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
541
fprintf(stderr, "Mandos plugin mandos-client: "
542
"Error in GnuTLS DH parameter initialization:"
515
543
" %s\n", safer_gnutls_strerror(ret));
518
546
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
519
547
if(ret != GNUTLS_E_SUCCESS){
520
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
548
fprintf(stderr, "Mandos plugin mandos-client: "
549
"Error in GnuTLS prime generation: %s\n",
521
550
safer_gnutls_strerror(ret));
559
589
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
560
590
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));
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));
564
595
gnutls_deinit(*session);
709
745
if(if_indextoname((unsigned int)if_index, interface) == NULL){
710
746
perror_plus("if_indextoname");
712
fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
748
fprintf(stderr, "Mandos plugin mandos-client: "
749
"Connection to: %s%%%s, port %" PRIu16 "\n",
713
750
ip, interface, port);
716
fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
753
fprintf(stderr, "Mandos plugin mandos-client: "
754
"Connection to: %s, port %" PRIu16 "\n", ip, port);
719
756
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
720
757
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
872
912
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
874
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
914
fprintf(stderr, "Mandos plugin mandos-client: "
915
"*** GnuTLS Re-handshake failed ***\n");
875
916
gnutls_perror(ret);
881
fprintf(stderr, "Unknown error while reading data from"
922
fprintf(stderr, "Mandos plugin mandos-client: "
923
"Unknown error while reading data from"
882
924
" encrypted session with Mandos server\n");
883
925
gnutls_bye(session, GNUTLS_SHUT_RDWR);
992
1036
case AVAHI_RESOLVER_FAILURE:
993
fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
1037
fprintf(stderr, "Mandos plugin mandos-client: "
1038
"(Avahi Resolver) Failed to resolve service '%s'"
994
1039
" of type '%s' in domain '%s': %s\n", name, type, domain,
995
1040
avahi_strerror(avahi_server_errno(mc.server)));
1000
1045
char ip[AVAHI_ADDRESS_STR_MAX];
1001
1046
avahi_address_snprint(ip, sizeof(ip), address);
1003
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
1048
fprintf(stderr, "Mandos plugin mandos-client: "
1049
"Mandos server \"%s\" found on %s (%s, %"
1004
1050
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1005
1051
ip, (intmax_t)interface, port);
1054
1101
if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1055
1102
name, type, domain, protocol, 0,
1056
1103
resolve_callback, NULL) == NULL)
1057
fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
1104
fprintf(stderr, "Mandos plugin mandos-client: "
1105
"Avahi: Failed to resolve service '%s': %s\n",
1058
1106
name, avahi_strerror(avahi_server_errno(mc.server)));
1109
1158
/* Reject the loopback device */
1110
1159
if(ifr->ifr_flags & IFF_LOOPBACK){
1112
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1161
fprintf(stderr, "Mandos plugin mandos-client: "
1162
"Rejecting loopback interface \"%s\"\n", ifname);
1117
1166
/* Accept point-to-point devices only if connect_to is specified */
1118
1167
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1120
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1169
fprintf(stderr, "Mandos plugin mandos-client: "
1170
"Accepting point-to-point interface \"%s\"\n", ifname);
1125
1174
/* Otherwise, reject non-broadcast-capable devices */
1126
1175
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1128
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1177
fprintf(stderr, "Mandos plugin mandos-client: "
1178
"Rejecting non-broadcast interface \"%s\"\n", ifname);
1133
1182
/* Reject non-ARP interfaces (including dummy interfaces) */
1134
1183
if(ifr->ifr_flags & IFF_NOARP){
1136
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1185
fprintf(stderr, "Mandos plugin mandos-client: "
1186
"Rejecting non-ARP interface \"%s\"\n", ifname);
1141
1191
/* Accept this device */
1143
fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1193
fprintf(stderr, "Mandos plugin mandos-client: "
1194
"Interface \"%s\" is good\n", ifname);
1151
1202
* (This function is passed to scandir(3) as a filter function.)
1153
1204
int good_interface(const struct dirent *if_entry){
1155
1205
if(if_entry->d_name[0] == '.'){
1158
1209
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] == '.'){
1160
1236
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 */
1181
1334
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1183
1336
struct timespec now;
1188
1341
if(mc.current_server == NULL){
1343
fprintf(stderr, "Mandos plugin mandos-client: "
1191
1344
"Wait until first server is found. No timeout!\n");
1193
1346
ret = avahi_simple_poll_iterate(s, -1);
1196
fprintf(stderr, "Check current_server if we should run it,"
1349
fprintf(stderr, "Mandos plugin mandos-client: "
1350
"Check current_server if we should run it,"
1199
1353
/* 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",
1499
1756
avahi_set_log_function(empty_log);
1502
1759
if(interface[0] == '\0'){
1503
1760
struct dirent **direntries;
1504
ret = scandir(sys_class_net, &direntries, good_interface,
1761
/* First look for interfaces that are up */
1762
ret = scandir(sys_class_net, &direntries, up_interface,
1765
/* No up interfaces, look for any good interfaces */
1767
ret = scandir(sys_class_net, &direntries, good_interface,
1507
/* Pick the first good interface */
1771
/* Pick the first interface returned */
1508
1772
interface = strdup(direntries[0]->d_name);
1510
fprintf(stderr, "Using interface \"%s\"\n", interface);
1774
fprintf(stderr, "Mandos plugin mandos-client: "
1775
"Using interface \"%s\"\n", interface);
1512
1777
if(interface == NULL){
1513
1778
perror_plus("malloc");
1889
2162
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1890
2163
NULL, 0, browse_callback, NULL);
1891
2164
if(sb == NULL){
1892
fprintf(stderr, "Failed to create service browser: %s\n",
2165
fprintf(stderr, "Mandos plugin mandos-client: "
2166
"Failed to create service browser: %s\n",
1893
2167
avahi_strerror(avahi_server_errno(mc.server)));
1894
2168
exitcode = EX_UNAVAILABLE;
1902
2176
/* Run the main loop */
1905
fprintf(stderr, "Starting Avahi loop search\n");
2179
fprintf(stderr, "Mandos plugin mandos-client: "
2180
"Starting Avahi loop search\n");
1908
2183
ret = avahi_loop_with_timeout(mc.simple_poll,
1909
2184
(int)(retry_interval * 1000));
1911
fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
2186
fprintf(stderr, "Mandos plugin mandos-client: "
2187
"avahi_loop_with_timeout exited %s\n",
1912
2188
(ret == 0) ? "successfully" : "with error");
1918
fprintf(stderr, "%s exiting\n", argv[0]);
2194
fprintf(stderr, "Mandos plugin mandos-client: "
2195
"%s exiting\n", argv[0]);
1921
2198
/* Cleanup things */