159
199
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");
163
240
* Initialize GPGME.
165
static bool init_gpgme(const char *seckey,
166
const char *pubkey, const char *tempdir){
242
static bool init_gpgme(const char *seckey, const char *pubkey,
243
const char *tempdir){
168
244
gpgme_error_t rc;
169
245
gpgme_engine_info_t engine_info;
173
249
* Helper function to insert pub and seckey to the engine keyring.
175
251
bool import_key(const char *filename){
177
254
gpgme_data_t pgp_data;
179
256
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
185
262
rc = gpgme_data_new_from_fd(&pgp_data, fd);
186
263
if(rc != GPG_ERR_NO_ERROR){
187
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
264
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
188
265
gpgme_strsource(rc), gpgme_strerror(rc));
192
269
rc = gpgme_op_import(mc.ctx, pgp_data);
193
270
if(rc != GPG_ERR_NO_ERROR){
194
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
271
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
195
272
gpgme_strsource(rc), gpgme_strerror(rc));
199
276
ret = (int)TEMP_FAILURE_RETRY(close(fd));
278
perror_plus("close");
203
280
gpgme_data_release(pgp_data);
208
fprintf(stderr, "Initializing GPGME\n");
285
fprintf_plus(stderr, "Initializing GPGME\n");
212
289
gpgme_check_version(NULL);
213
290
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
214
291
if(rc != GPG_ERR_NO_ERROR){
215
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
292
fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
216
293
gpgme_strsource(rc), gpgme_strerror(rc));
220
/* Set GPGME home directory for the OpenPGP engine only */
297
/* Set GPGME home directory for the OpenPGP engine only */
221
298
rc = gpgme_get_engine_info(&engine_info);
222
299
if(rc != GPG_ERR_NO_ERROR){
223
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
300
fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
224
301
gpgme_strsource(rc), gpgme_strerror(rc));
292
371
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
293
372
if(rc != GPG_ERR_NO_ERROR){
294
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
373
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
295
374
gpgme_strsource(rc), gpgme_strerror(rc));
296
375
plaintext_length = -1;
298
377
gpgme_decrypt_result_t result;
299
378
result = gpgme_op_decrypt_result(mc.ctx);
300
379
if(result == NULL){
301
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
380
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
303
fprintf(stderr, "Unsupported algorithm: %s\n",
382
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
304
383
result->unsupported_algorithm);
305
fprintf(stderr, "Wrong key usage: %u\n",
384
fprintf_plus(stderr, "Wrong key usage: %u\n",
306
385
result->wrong_key_usage);
307
386
if(result->file_name != NULL){
308
fprintf(stderr, "File name: %s\n", result->file_name);
387
fprintf_plus(stderr, "File name: %s\n", result->file_name);
310
389
gpgme_recipient_t recipient;
311
390
recipient = result->recipients;
312
391
while(recipient != NULL){
313
fprintf(stderr, "Public key algorithm: %s\n",
392
fprintf_plus(stderr, "Public key algorithm: %s\n",
314
393
gpgme_pubkey_algo_name(recipient->pubkey_algo));
315
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
316
fprintf(stderr, "Secret key available: %s\n",
394
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
395
fprintf_plus(stderr, "Secret key available: %s\n",
317
396
recipient->status == GPG_ERR_NO_SECKEY
319
398
recipient = recipient->next;
437
511
(mc.cred, pubkeyfilename, seckeyfilename,
438
512
GNUTLS_OPENPGP_FMT_BASE64);
439
513
if(ret != GNUTLS_E_SUCCESS){
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));
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));
448
521
/* GnuTLS server initialization */
449
522
ret = gnutls_dh_params_init(&mc.dh_params);
450
523
if(ret != GNUTLS_E_SUCCESS){
451
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
524
fprintf_plus(stderr, "Error in GnuTLS DH parameter initialization:"
452
525
" %s\n", safer_gnutls_strerror(ret));
455
528
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
456
529
if(ret != GNUTLS_E_SUCCESS){
457
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
530
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
458
531
safer_gnutls_strerror(ret));
474
547
static int init_gnutls_session(gnutls_session_t *session){
476
549
/* GnuTLS session creation */
477
ret = gnutls_init(session, GNUTLS_SERVER);
551
ret = gnutls_init(session, GNUTLS_SERVER);
555
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
478
556
if(ret != GNUTLS_E_SUCCESS){
479
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
557
fprintf_plus(stderr, "Error in GnuTLS session initialization: %s\n",
480
558
safer_gnutls_strerror(ret));
485
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
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);
486
570
if(ret != GNUTLS_E_SUCCESS){
487
fprintf(stderr, "Syntax error at: %s\n", err);
488
fprintf(stderr, "GnuTLS error: %s\n",
489
safer_gnutls_strerror(ret));
571
fprintf_plus(stderr, "Syntax error at: %s\n", err);
572
fprintf_plus(stderr, "GnuTLS error: %s\n", safer_gnutls_strerror(ret));
490
573
gnutls_deinit(*session);
495
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
579
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
582
gnutls_deinit(*session);
585
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
497
586
if(ret != GNUTLS_E_SUCCESS){
498
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
587
fprintf_plus(stderr, "Error setting GnuTLS credentials: %s\n",
499
588
safer_gnutls_strerror(ret));
500
589
gnutls_deinit(*session);
504
593
/* ignore client certificate if any. */
505
gnutls_certificate_server_set_request(*session,
594
gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
508
596
gnutls_dh_set_prime_bits(*session, mc.dh_bits);
965
1085
signal_received = sig;
966
1086
int old_errno = errno;
1087
/* set main loop to exit */
967
1088
if(mc.simple_poll != NULL){
968
1089
avahi_simple_poll_quit(mc.simple_poll);
970
1091
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;
973
1357
int main(int argc, char *argv[]){
974
1358
AvahiSServiceBrowser *sb = NULL;
1031
1437
.arg = "SECONDS",
1032
1438
.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 },
1034
1457
{ .name = NULL }
1037
1460
error_t parse_opt(int key, char *arg,
1038
1461
struct argp_state *state){
1040
1464
case 128: /* --debug */
1070
1493
delay = strtof(arg, &tmp);
1071
1494
if(errno != 0 or tmp == arg or *tmp != '\0'){
1072
fprintf(stderr, "Bad delay\n");
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);
1081
1525
return ARGP_ERR_UNKNOWN;
1086
1530
struct argp argp = { .options = options, .parser = parse_opt,
1087
1531
.args_doc = "",
1088
1532
.doc = "Mandos client -- Get and decrypt"
1089
1533
" passwords from a Mandos server" };
1090
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
1091
if(ret == ARGP_ERR_UNKNOWN){
1092
fprintf(stderr, "Unknown error while parsing arguments\n");
1093
exitcode = EXIT_FAILURE;
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",
1099
1708
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;
1102
1743
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1103
1744
from the signal handler */
1104
1745
/* Initialize the pseudo-RNG for Avahi */
1105
1746
srand((unsigned int) time(NULL));
1106
1747
mc.simple_poll = avahi_simple_poll_new();
1107
1748
if(mc.simple_poll == NULL){
1108
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1109
exitcode = EXIT_FAILURE;
1749
fprintf_plus(stderr, "Avahi: Failed to create simple poll object.\n");
1750
exitcode = EX_UNAVAILABLE;
1113
1754
sigemptyset(&sigterm_action.sa_mask);
1114
1755
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1116
perror("sigaddset");
1117
exitcode = EXIT_FAILURE;
1757
perror_plus("sigaddset");
1758
exitcode = EX_OSERR;
1120
1761
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1122
perror("sigaddset");
1123
exitcode = EXIT_FAILURE;
1763
perror_plus("sigaddset");
1764
exitcode = EX_OSERR;
1126
1767
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1128
perror("sigaddset");
1129
exitcode = EXIT_FAILURE;
1769
perror_plus("sigaddset");
1770
exitcode = EX_OSERR;
1132
1773
/* Need to check if the handler is SIG_IGN before handling:
1136
1777
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1138
perror("sigaction");
1139
return EXIT_FAILURE;
1779
perror_plus("sigaction");
1141
1782
if(old_sigterm_action.sa_handler != SIG_IGN){
1142
1783
ret = sigaction(SIGINT, &sigterm_action, NULL);
1144
perror("sigaction");
1145
exitcode = EXIT_FAILURE;
1785
perror_plus("sigaction");
1786
exitcode = EX_OSERR;
1149
1790
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1151
perror("sigaction");
1152
return EXIT_FAILURE;
1792
perror_plus("sigaction");
1154
1795
if(old_sigterm_action.sa_handler != SIG_IGN){
1155
1796
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1157
perror("sigaction");
1158
exitcode = EXIT_FAILURE;
1798
perror_plus("sigaction");
1799
exitcode = EX_OSERR;
1162
1803
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1164
perror("sigaction");
1165
return EXIT_FAILURE;
1805
perror_plus("sigaction");
1167
1808
if(old_sigterm_action.sa_handler != SIG_IGN){
1168
1809
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1170
perror("sigaction");
1171
exitcode = EXIT_FAILURE;
1811
perror_plus("sigaction");
1812
exitcode = EX_OSERR;
1176
1817
/* If the interface is down, bring it up */
1177
if(interface[0] != '\0'){
1818
if(strcmp(interface, "none") != 0){
1178
1819
if_index = (AvahiIfIndex) if_nametoindex(interface);
1179
1820
if(if_index == 0){
1180
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1181
exitcode = EXIT_FAILURE;
1821
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1822
exitcode = EX_UNAVAILABLE;
1232
1892
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1234
1894
take_down_interface = false;
1235
perror("ioctl SIOCSIFFLAGS");
1236
exitcode = EXIT_FAILURE;
1895
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1896
exitcode = EX_OSERR;
1237
1897
#ifdef __linux__
1238
1898
if(restore_loglevel){
1239
1899
ret = klogctl(7, NULL, 0);
1901
perror_plus("klogctl");
1244
1904
#endif /* __linux__ */
1905
/* Lower privileges */
1909
perror_plus("seteuid");
1248
/* sleep checking until interface is running */
1914
/* Sleep checking until interface is running.
1915
Check every 0.25s, up to total time of delay */
1249
1916
for(int i=0; i < delay * 4; i++){
1250
1917
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1252
perror("ioctl SIOCGIFFLAGS");
1919
perror_plus("ioctl SIOCGIFFLAGS");
1253
1920
} else if(network.ifr_flags & IFF_RUNNING){
1256
1923
struct timespec sleeptime = { .tv_nsec = 250000000 };
1257
1924
ret = nanosleep(&sleeptime, NULL);
1258
1925
if(ret == -1 and errno != EINTR){
1259
perror("nanosleep");
1926
perror_plus("nanosleep");
1262
1929
if(not take_down_interface){
1263
1930
/* We won't need the socket anymore */
1264
1931
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1933
perror_plus("close");
1269
1936
#ifdef __linux__
1469
2152
if(gpgme_initialized){
1470
2153
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 */
1473
2169
/* Take down the network interface */
1474
2170
if(take_down_interface){
1475
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2171
/* Re-raise priviliges */
1477
perror("ioctl SIOCGIFFLAGS");
1478
} else if(network.ifr_flags & IFF_UP) {
1479
network.ifr_flags &= ~IFF_UP; /* clear flag */
1480
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1482
perror("ioctl SIOCSIFFLAGS");
2175
perror_plus("seteuid");
1485
ret = (int)TEMP_FAILURE_RETRY(close(sd));
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");
1491
/* Removes the temp directory used by GPGME */
2201
/* Removes the GPGME temp directory and all files inside */
1492
2202
if(tempdir_created){
1494
struct dirent *direntry;
1495
d = opendir(tempdir);
1497
if(errno != ENOENT){
1502
direntry = readdir(d);
1503
if(direntry == NULL){
1506
/* Skip "." and ".." */
1507
if(direntry->d_name[0] == '.'
1508
and (direntry->d_name[1] == '\0'
1509
or (direntry->d_name[1] == '.'
1510
and direntry->d_name[2] == '\0'))){
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];
1513
2210
char *fullname = NULL;
1514
2211
ret = asprintf(&fullname, "%s/%s", tempdir,
1515
2212
direntry->d_name);
2214
perror_plus("asprintf");
1520
2217
ret = remove(fullname);
1522
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2219
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname, strerror(errno));
1525
2221
free(fullname);
2225
/* need to clean even if 0 because man page doesn't specify */
2227
if (numentries == -1){
2228
perror_plus("scandir");
1529
2230
ret = rmdir(tempdir);
1530
2231
if(ret == -1 and errno != ENOENT){
2232
perror_plus("rmdir");
1536
2237
sigemptyset(&old_sigterm_action.sa_mask);
1537
2238
old_sigterm_action.sa_handler = SIG_DFL;
1538
ret = sigaction(signal_received, &old_sigterm_action, NULL);
2239
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
2240
&old_sigterm_action,
1540
perror("sigaction");
1542
raise(signal_received);
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());
1545
2255
return exitcode;