199
159
return buffer_capacity;
202
/* Add server to set of servers to retry periodically */
203
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
206
server *new_server = malloc(sizeof(server));
207
if(new_server == NULL){
208
perror_plus("malloc");
211
*new_server = (server){ .ip = strdup(ip),
213
.if_index = if_index,
215
if(new_server->ip == NULL){
216
perror_plus("strdup");
219
/* Special case of first server */
220
if (mc.current_server == NULL){
221
new_server->next = new_server;
222
new_server->prev = new_server;
223
mc.current_server = new_server;
224
/* Place the new server last in the list */
226
new_server->next = mc.current_server;
227
new_server->prev = mc.current_server->prev;
228
new_server->prev->next = new_server;
229
mc.current_server->prev = new_server;
231
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
233
perror_plus("clock_gettime");
240
163
* Initialize GPGME.
242
static bool init_gpgme(const char *seckey, const char *pubkey,
243
const char *tempdir){
165
static bool init_gpgme(const char *seckey,
166
const char *pubkey, const char *tempdir){
244
168
gpgme_error_t rc;
245
169
gpgme_engine_info_t engine_info;
249
173
* Helper function to insert pub and seckey to the engine keyring.
251
175
bool import_key(const char *filename){
254
177
gpgme_data_t pgp_data;
256
179
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
262
185
rc = gpgme_data_new_from_fd(&pgp_data, fd);
263
186
if(rc != GPG_ERR_NO_ERROR){
264
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
187
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
265
188
gpgme_strsource(rc), gpgme_strerror(rc));
269
192
rc = gpgme_op_import(mc.ctx, pgp_data);
270
193
if(rc != GPG_ERR_NO_ERROR){
271
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
194
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
272
195
gpgme_strsource(rc), gpgme_strerror(rc));
276
199
ret = (int)TEMP_FAILURE_RETRY(close(fd));
278
perror_plus("close");
280
203
gpgme_data_release(pgp_data);
285
fprintf_plus(stderr, "Initializing GPGME\n");
208
fprintf(stderr, "Initializing GPGME\n");
289
212
gpgme_check_version(NULL);
290
213
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
291
214
if(rc != GPG_ERR_NO_ERROR){
292
fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
215
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
293
216
gpgme_strsource(rc), gpgme_strerror(rc));
297
/* Set GPGME home directory for the OpenPGP engine only */
220
/* Set GPGME home directory for the OpenPGP engine only */
298
221
rc = gpgme_get_engine_info(&engine_info);
299
222
if(rc != GPG_ERR_NO_ERROR){
300
fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
223
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
301
224
gpgme_strsource(rc), gpgme_strerror(rc));
371
292
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
372
293
if(rc != GPG_ERR_NO_ERROR){
373
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
294
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
374
295
gpgme_strsource(rc), gpgme_strerror(rc));
375
296
plaintext_length = -1;
377
298
gpgme_decrypt_result_t result;
378
299
result = gpgme_op_decrypt_result(mc.ctx);
379
300
if(result == NULL){
380
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
301
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
382
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
303
fprintf(stderr, "Unsupported algorithm: %s\n",
383
304
result->unsupported_algorithm);
384
fprintf_plus(stderr, "Wrong key usage: %u\n",
305
fprintf(stderr, "Wrong key usage: %u\n",
385
306
result->wrong_key_usage);
386
307
if(result->file_name != NULL){
387
fprintf_plus(stderr, "File name: %s\n", result->file_name);
308
fprintf(stderr, "File name: %s\n", result->file_name);
389
310
gpgme_recipient_t recipient;
390
311
recipient = result->recipients;
391
312
while(recipient != NULL){
392
fprintf_plus(stderr, "Public key algorithm: %s\n",
313
fprintf(stderr, "Public key algorithm: %s\n",
393
314
gpgme_pubkey_algo_name(recipient->pubkey_algo));
394
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
395
fprintf_plus(stderr, "Secret key available: %s\n",
315
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
316
fprintf(stderr, "Secret key available: %s\n",
396
317
recipient->status == GPG_ERR_NO_SECKEY
398
319
recipient = recipient->next;
511
437
(mc.cred, pubkeyfilename, seckeyfilename,
512
438
GNUTLS_OPENPGP_FMT_BASE64);
513
439
if(ret != GNUTLS_E_SUCCESS){
515
"Error[%d] while reading the OpenPGP key pair ('%s',"
516
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
517
fprintf_plus(stderr, "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
441
"Error[%d] while reading the OpenPGP key pair ('%s',"
442
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
443
fprintf(stderr, "The GnuTLS error is: %s\n",
444
safer_gnutls_strerror(ret));
521
448
/* GnuTLS server initialization */
522
449
ret = gnutls_dh_params_init(&mc.dh_params);
523
450
if(ret != GNUTLS_E_SUCCESS){
524
fprintf_plus(stderr, "Error in GnuTLS DH parameter initialization:"
451
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
525
452
" %s\n", safer_gnutls_strerror(ret));
528
455
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
529
456
if(ret != GNUTLS_E_SUCCESS){
530
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
457
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
531
458
safer_gnutls_strerror(ret));
547
474
static int init_gnutls_session(gnutls_session_t *session){
549
476
/* GnuTLS session creation */
551
ret = gnutls_init(session, GNUTLS_SERVER);
555
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
477
ret = gnutls_init(session, GNUTLS_SERVER);
556
478
if(ret != GNUTLS_E_SUCCESS){
557
fprintf_plus(stderr, "Error in GnuTLS session initialization: %s\n",
479
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
558
480
safer_gnutls_strerror(ret));
564
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
566
gnutls_deinit(*session);
569
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
485
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
570
486
if(ret != GNUTLS_E_SUCCESS){
571
fprintf_plus(stderr, "Syntax error at: %s\n", err);
572
fprintf_plus(stderr, "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
487
fprintf(stderr, "Syntax error at: %s\n", err);
488
fprintf(stderr, "GnuTLS error: %s\n",
489
safer_gnutls_strerror(ret));
573
490
gnutls_deinit(*session);
579
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
582
gnutls_deinit(*session);
585
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
495
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
586
497
if(ret != GNUTLS_E_SUCCESS){
587
fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
498
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
588
499
safer_gnutls_strerror(ret));
589
500
gnutls_deinit(*session);
593
504
/* ignore client certificate if any. */
594
gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
505
gnutls_certificate_server_set_request(*session,
596
508
gnutls_dh_set_prime_bits(*session, mc.dh_bits);
900
fprintf_plus(stderr, "Closing TLS session\n");
909
ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
914
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
741
fprintf(stderr, "Closing TLS session\n");
744
gnutls_bye(session, GNUTLS_SHUT_RDWR);
916
746
if(buffer_length > 0){
917
ssize_t decrypted_buffer_size;
918
decrypted_buffer_size = pgp_packet_decrypt(buffer, buffer_length,
747
decrypted_buffer_size = pgp_packet_decrypt(buffer,
919
749
&decrypted_buffer);
920
750
if(decrypted_buffer_size >= 0){
923
752
while(written < (size_t) decrypted_buffer_size){
929
753
ret = (int)fwrite(decrypted_buffer + written, 1,
930
754
(size_t)decrypted_buffer_size - written,
932
756
if(ret == 0 and ferror(stdout)){
935
fprintf_plus(stderr, "Error writing encrypted data: %s\n",
758
fprintf(stderr, "Error writing encrypted data: %s\n",
936
759
strerror(errno));
941
764
written += (size_t)ret;
766
free(decrypted_buffer);
947
774
/* Shutdown procedure */
952
free(decrypted_buffer);
955
ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
961
perror_plus("close");
963
gnutls_deinit(session);
778
ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
782
gnutls_deinit(session);
1085
891
signal_received = sig;
1086
892
int old_errno = errno;
1087
/* set main loop to exit */
1088
893
if(mc.simple_poll != NULL){
1089
894
avahi_simple_poll_quit(mc.simple_poll);
1091
896
errno = old_errno;
1094
bool get_flags(const char *ifname, struct ifreq *ifr){
1097
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1099
perror_plus("socket");
1102
strcpy(ifr->ifr_name, ifname);
1103
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1106
perror_plus("ioctl SIOCGIFFLAGS");
1113
bool good_flags(const char *ifname, const struct ifreq *ifr){
1115
/* Reject the loopback device */
1116
if(ifr->ifr_flags & IFF_LOOPBACK){
1118
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n", ifname);
1122
/* Accept point-to-point devices only if connect_to is specified */
1123
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1125
fprintf_plus(stderr, "Accepting point-to-point interface \"%s\"\n", ifname);
1129
/* Otherwise, reject non-broadcast-capable devices */
1130
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1132
fprintf_plus(stderr, "Rejecting non-broadcast interface \"%s\"\n", ifname);
1136
/* Reject non-ARP interfaces (including dummy interfaces) */
1137
if(ifr->ifr_flags & IFF_NOARP){
1139
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1144
/* Accept this device */
1146
fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1152
* This function determines if a directory entry in /sys/class/net
1153
* corresponds to an acceptable network device.
1154
* (This function is passed to scandir(3) as a filter function.)
1156
int good_interface(const struct dirent *if_entry){
1157
if(if_entry->d_name[0] == '.'){
1162
if(not get_flags(if_entry->d_name, &ifr)){
1164
fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
1170
if(not good_flags(if_entry->d_name, &ifr)){
1177
* This function determines if a directory entry in /sys/class/net
1178
* corresponds to an acceptable network device which is up.
1179
* (This function is passed to scandir(3) as a filter function.)
1181
int up_interface(const struct dirent *if_entry){
1182
if(if_entry->d_name[0] == '.'){
1187
if(not get_flags(if_entry->d_name, &ifr)){
1189
fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
1195
/* Reject down interfaces */
1196
if(not (ifr.ifr_flags & IFF_UP)){
1198
fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1204
/* Reject non-running interfaces */
1205
if(not (ifr.ifr_flags & IFF_RUNNING)){
1207
fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1213
if(not good_flags(if_entry->d_name, &ifr)){
1219
int notdotentries(const struct dirent *direntry){
1220
/* Skip "." and ".." */
1221
if(direntry->d_name[0] == '.'
1222
and (direntry->d_name[1] == '\0'
1223
or (direntry->d_name[1] == '.'
1224
and direntry->d_name[2] == '\0'))){
1230
/* Is this directory entry a runnable program? */
1231
int runnable_hook(const struct dirent *direntry){
1236
if((direntry->d_name)[0] == '\0'){
1241
sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1242
"abcdefghijklmnopqrstuvwxyz"
1245
if((direntry->d_name)[sret] != '\0'){
1246
/* Contains non-allowed characters */
1248
fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1254
char *fullname = NULL;
1255
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1257
perror_plus("asprintf");
1261
ret = stat(fullname, &st);
1264
perror_plus("Could not stat hook");
1268
if(not (S_ISREG(st.st_mode))){
1269
/* Not a regular file */
1271
fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1276
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1277
/* Not executable */
1279
fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1287
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1289
struct timespec now;
1290
struct timespec waited_time;
1291
intmax_t block_time;
1294
if(mc.current_server == NULL){
1296
fprintf_plus(stderr, "Wait until first server is found. No timeout!\n");
1298
ret = avahi_simple_poll_iterate(s, -1);
1301
fprintf_plus(stderr, "Check current_server if we should run it,"
1304
/* the current time */
1305
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1307
perror_plus("clock_gettime");
1310
/* Calculating in ms how long time between now and server
1311
who we visted longest time ago. Now - last seen. */
1312
waited_time.tv_sec = (now.tv_sec
1313
- mc.current_server->last_seen.tv_sec);
1314
waited_time.tv_nsec = (now.tv_nsec
1315
- mc.current_server->last_seen.tv_nsec);
1316
/* total time is 10s/10,000ms.
1317
Converting to s from ms by dividing by 1,000,
1318
and ns to ms by dividing by 1,000,000. */
1319
block_time = ((retry_interval
1320
- ((intmax_t)waited_time.tv_sec * 1000))
1321
- ((intmax_t)waited_time.tv_nsec / 1000000));
1324
fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1327
if(block_time <= 0){
1328
ret = start_mandos_communication(mc.current_server->ip,
1329
mc.current_server->port,
1330
mc.current_server->if_index,
1331
mc.current_server->af);
1333
avahi_simple_poll_quit(mc.simple_poll);
1336
ret = clock_gettime(CLOCK_MONOTONIC,
1337
&mc.current_server->last_seen);
1339
perror_plus("clock_gettime");
1342
mc.current_server = mc.current_server->next;
1343
block_time = 0; /* Call avahi to find new Mandos
1344
servers, but don't block */
1347
ret = avahi_simple_poll_iterate(s, (int)block_time);
1350
if (ret > 0 or errno != EINTR){
1351
return (ret != 1) ? ret : 0;
1357
899
int main(int argc, char *argv[]){
1358
900
AvahiSServiceBrowser *sb = NULL;
1437
957
.arg = "SECONDS",
1438
958
.doc = "Maximum delay to wait for interface startup",
1440
{ .name = "retry", .key = 132,
1442
.doc = "Retry interval used when denied by the mandos server",
1444
{ .name = "network-hook-dir", .key = 133,
1446
.doc = "Directory where network hooks are located",
1449
* These reproduce what we would get without ARGP_NO_HELP
1451
{ .name = "help", .key = '?',
1452
.doc = "Give this help list", .group = -1 },
1453
{ .name = "usage", .key = -3,
1454
.doc = "Give a short usage message", .group = -1 },
1455
{ .name = "version", .key = 'V',
1456
.doc = "Print program version", .group = -1 },
1457
960
{ .name = NULL }
1460
963
error_t parse_opt(int key, char *arg,
1461
964
struct argp_state *state){
1464
966
case 128: /* --debug */
1493
996
delay = strtof(arg, &tmp);
1494
997
if(errno != 0 or tmp == arg or *tmp != '\0'){
1495
argp_error(state, "Bad delay");
1497
case 132: /* --retry */
1499
retry_interval = strtod(arg, &tmp);
1500
if(errno != 0 or tmp == arg or *tmp != '\0'
1501
or (retry_interval * 1000) > INT_MAX
1502
or retry_interval < 0){
1503
argp_error(state, "Bad retry interval");
1506
case 133: /* --network-hook-dir */
1510
* These reproduce what we would get without ARGP_NO_HELP
1512
case '?': /* --help */
1513
argp_state_help(state, state->out_stream,
1514
(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1515
& ~(unsigned int)ARGP_HELP_EXIT_OK);
1516
case -3: /* --usage */
1517
argp_state_help(state, state->out_stream,
1518
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1519
case 'V': /* --version */
1520
fprintf_plus(state->out_stream, "Mandos plugin mandos-client: ");
1521
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1522
exit(argp_err_exit_status);
998
fprintf(stderr, "Bad delay\n");
1525
1007
return ARGP_ERR_UNKNOWN;
1530
1012
struct argp argp = { .options = options, .parser = parse_opt,
1531
1013
.args_doc = "",
1532
1014
.doc = "Mandos client -- Get and decrypt"
1533
1015
" passwords from a Mandos server" };
1534
ret = argp_parse(&argp, argc, argv,
1535
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1542
perror_plus("argp_parse");
1543
exitcode = EX_OSERR;
1546
exitcode = EX_USAGE;
1552
/* Work around Debian bug #633582:
1553
<http://bugs.debian.org/633582> */
1556
/* Re-raise priviliges */
1560
perror_plus("seteuid");
1563
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1564
int seckey_fd = open(seckey, O_RDONLY);
1565
if(seckey_fd == -1){
1566
perror_plus("open");
1568
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1570
perror_plus("fstat");
1572
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1573
ret = fchown(seckey_fd, uid, gid);
1575
perror_plus("fchown");
1579
TEMP_FAILURE_RETRY(close(seckey_fd));
1583
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1584
int pubkey_fd = open(pubkey, O_RDONLY);
1585
if(pubkey_fd == -1){
1586
perror_plus("open");
1588
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1590
perror_plus("fstat");
1592
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1593
ret = fchown(pubkey_fd, uid, gid);
1595
perror_plus("fchown");
1599
TEMP_FAILURE_RETRY(close(pubkey_fd));
1603
/* Lower privileges */
1607
perror_plus("seteuid");
1611
/* Find network hooks and run them */
1613
struct dirent **direntries;
1614
struct dirent *direntry;
1615
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1618
perror_plus("scandir");
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);
1626
perror_plus("asprintf");
1629
pid_t hook_pid = fork();
1632
dup2(devnull, STDIN_FILENO);
1634
dup2(STDERR_FILENO, STDOUT_FILENO);
1635
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1637
perror_plus("setenv");
1640
ret = setenv("DEVICE", interface, 1);
1642
perror_plus("setenv");
1645
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1647
perror_plus("setenv");
1650
ret = setenv("MODE", "start", 1);
1652
perror_plus("setenv");
1656
ret = asprintf(&delaystring, "%f", delay);
1658
perror_plus("asprintf");
1661
ret = setenv("DELAY", delaystring, 1);
1664
perror_plus("setenv");
1668
ret = execl(fullname, direntry->d_name, "start", NULL);
1669
perror_plus("execl");
1672
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1673
perror_plus("waitpid");
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));
1685
} else if(WIFSIGNALED(status)){
1686
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1687
" signal %d\n", direntry->d_name,
1692
fprintf_plus(stderr, "Warning: network hook \"%s\" crashed\n",
1016
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
1017
if(ret == ARGP_ERR_UNKNOWN){
1018
fprintf(stderr, "Unknown error while parsing arguments\n");
1019
exitcode = EXIT_FAILURE;
1708
1025
avahi_set_log_function(empty_log);
1711
if(interface[0] == '\0'){
1712
struct dirent **direntries;
1713
/* First look for interfaces that are up */
1714
ret = scandir(sys_class_net, &direntries, up_interface,
1717
/* No up interfaces, look for any good interfaces */
1719
ret = scandir(sys_class_net, &direntries, good_interface,
1723
/* Pick the first interface returned */
1724
interface = strdup(direntries[0]->d_name);
1726
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1728
if(interface == NULL){
1729
perror_plus("malloc");
1731
exitcode = EXIT_FAILURE;
1737
fprintf_plus(stderr, "Could not find a network interface\n");
1738
exitcode = EXIT_FAILURE;
1743
1028
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1744
1029
from the signal handler */
1745
1030
/* Initialize the pseudo-RNG for Avahi */
1746
1031
srand((unsigned int) time(NULL));
1747
1032
mc.simple_poll = avahi_simple_poll_new();
1748
1033
if(mc.simple_poll == NULL){
1749
fprintf_plus(stderr, "Avahi: Failed to create simple poll object.\n");
1750
exitcode = EX_UNAVAILABLE;
1034
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1035
exitcode = EXIT_FAILURE;
1754
1039
sigemptyset(&sigterm_action.sa_mask);
1755
1040
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1757
perror_plus("sigaddset");
1758
exitcode = EX_OSERR;
1042
perror("sigaddset");
1043
exitcode = EXIT_FAILURE;
1761
1046
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1763
perror_plus("sigaddset");
1764
exitcode = EX_OSERR;
1048
perror("sigaddset");
1049
exitcode = EXIT_FAILURE;
1767
1052
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1769
perror_plus("sigaddset");
1770
exitcode = EX_OSERR;
1054
perror("sigaddset");
1055
exitcode = EXIT_FAILURE;
1773
1058
/* Need to check if the handler is SIG_IGN before handling:
1777
1062
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1779
perror_plus("sigaction");
1064
perror("sigaction");
1065
return EXIT_FAILURE;
1782
1067
if(old_sigterm_action.sa_handler != SIG_IGN){
1783
1068
ret = sigaction(SIGINT, &sigterm_action, NULL);
1785
perror_plus("sigaction");
1786
exitcode = EX_OSERR;
1070
perror("sigaction");
1071
exitcode = EXIT_FAILURE;
1790
1075
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1792
perror_plus("sigaction");
1077
perror("sigaction");
1078
return EXIT_FAILURE;
1795
1080
if(old_sigterm_action.sa_handler != SIG_IGN){
1796
1081
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1798
perror_plus("sigaction");
1799
exitcode = EX_OSERR;
1083
perror("sigaction");
1084
exitcode = EXIT_FAILURE;
1803
1088
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1805
perror_plus("sigaction");
1090
perror("sigaction");
1091
return EXIT_FAILURE;
1808
1093
if(old_sigterm_action.sa_handler != SIG_IGN){
1809
1094
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1811
perror_plus("sigaction");
1812
exitcode = EX_OSERR;
1096
perror("sigaction");
1097
exitcode = EXIT_FAILURE;
1817
1102
/* If the interface is down, bring it up */
1818
if(strcmp(interface, "none") != 0){
1103
if(interface[0] != '\0'){
1819
1104
if_index = (AvahiIfIndex) if_nametoindex(interface);
1820
1105
if(if_index == 0){
1821
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1822
exitcode = EX_UNAVAILABLE;
1106
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1107
exitcode = EXIT_FAILURE;
1892
1158
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1894
1160
take_down_interface = false;
1895
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1896
exitcode = EX_OSERR;
1161
perror("ioctl SIOCSIFFLAGS");
1162
exitcode = EXIT_FAILURE;
1897
1163
#ifdef __linux__
1898
1164
if(restore_loglevel){
1899
1165
ret = klogctl(7, NULL, 0);
1901
perror_plus("klogctl");
1904
1170
#endif /* __linux__ */
1905
/* Lower privileges */
1909
perror_plus("seteuid");
1914
/* Sleep checking until interface is running.
1915
Check every 0.25s, up to total time of delay */
1174
/* sleep checking until interface is running */
1916
1175
for(int i=0; i < delay * 4; i++){
1917
1176
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1919
perror_plus("ioctl SIOCGIFFLAGS");
1178
perror("ioctl SIOCGIFFLAGS");
1920
1179
} else if(network.ifr_flags & IFF_RUNNING){
1923
1182
struct timespec sleeptime = { .tv_nsec = 250000000 };
1924
1183
ret = nanosleep(&sleeptime, NULL);
1925
1184
if(ret == -1 and errno != EINTR){
1926
perror_plus("nanosleep");
1185
perror("nanosleep");
1929
1188
if(not take_down_interface){
1930
1189
/* We won't need the socket anymore */
1931
1190
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1933
perror_plus("close");
1936
1195
#ifdef __linux__
2152
1395
if(gpgme_initialized){
2153
1396
gpgme_release(mc.ctx);
2156
/* Cleans up the circular linked list of Mandos servers the client
2158
if(mc.current_server != NULL){
2159
mc.current_server->prev->next = NULL;
2160
while(mc.current_server != NULL){
2161
server *next = mc.current_server->next;
2162
free(mc.current_server);
2163
mc.current_server = next;
2167
/* XXX run network hooks "stop" here */
2169
1399
/* Take down the network interface */
2170
1400
if(take_down_interface){
2171
/* Re-raise priviliges */
1401
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2175
perror_plus("seteuid");
1403
perror("ioctl SIOCGIFFLAGS");
1404
} else if(network.ifr_flags & IFF_UP) {
1405
network.ifr_flags &= ~IFF_UP; /* clear flag */
1406
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1408
perror("ioctl SIOCSIFFLAGS");
2178
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2180
perror_plus("ioctl SIOCGIFFLAGS");
2181
} else if(network.ifr_flags & IFF_UP){
2182
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2183
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2185
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2188
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2190
perror_plus("close");
2192
/* Lower privileges permanently */
2196
perror_plus("setuid");
1411
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2201
/* Removes the GPGME temp directory and all files inside */
1417
/* Removes the temp directory used by GPGME */
2202
1418
if(tempdir_created){
2203
struct dirent **direntries = NULL;
2204
struct dirent *direntry = NULL;
2205
int numentries = scandir(tempdir, &direntries, notdotentries,
2207
if (numentries > 0){
2208
for(int i = 0; i < numentries; i++){
2209
direntry = direntries[i];
1420
struct dirent *direntry;
1421
d = opendir(tempdir);
1423
if(errno != ENOENT){
1428
direntry = readdir(d);
1429
if(direntry == NULL){
1432
/* Skip "." and ".." */
1433
if(direntry->d_name[0] == '.'
1434
and (direntry->d_name[1] == '\0'
1435
or (direntry->d_name[1] == '.'
1436
and direntry->d_name[2] == '\0'))){
2210
1439
char *fullname = NULL;
2211
1440
ret = asprintf(&fullname, "%s/%s", tempdir,
2212
1441
direntry->d_name);
2214
perror_plus("asprintf");
2217
1446
ret = remove(fullname);
2219
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname, strerror(errno));
1448
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2221
1451
free(fullname);
2225
/* need to clean even if 0 because man page doesn't specify */
2227
if (numentries == -1){
2228
perror_plus("scandir");
2230
1455
ret = rmdir(tempdir);
2231
1456
if(ret == -1 and errno != ENOENT){
2232
perror_plus("rmdir");
2237
1462
sigemptyset(&old_sigterm_action.sa_mask);
2238
1463
old_sigterm_action.sa_handler = SIG_DFL;
2239
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
2240
&old_sigterm_action,
1464
ret = sigaction(signal_received, &old_sigterm_action, NULL);
2243
perror_plus("sigaction");
2246
ret = raise(signal_received);
2247
} while(ret != 0 and errno == EINTR);
2249
perror_plus("raise");
2252
TEMP_FAILURE_RETRY(pause());
1466
perror("sigaction");
1468
raise(signal_received);
2255
1471
return exitcode;