208
159
return buffer_capacity;
211
/* Add server to set of servers to retry periodically */
212
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
215
server *new_server = malloc(sizeof(server));
216
if(new_server == NULL){
217
perror_plus("malloc");
220
*new_server = (server){ .ip = strdup(ip),
222
.if_index = if_index,
224
if(new_server->ip == NULL){
225
perror_plus("strdup");
228
/* Special case of first server */
229
if (mc.current_server == NULL){
230
new_server->next = new_server;
231
new_server->prev = new_server;
232
mc.current_server = new_server;
233
/* Place the new server last in the list */
235
new_server->next = mc.current_server;
236
new_server->prev = mc.current_server->prev;
237
new_server->prev->next = new_server;
238
mc.current_server->prev = new_server;
240
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
242
perror_plus("clock_gettime");
249
163
* Initialize GPGME.
251
static bool init_gpgme(const char *seckey, const char *pubkey,
252
const char *tempdir){
165
static bool init_gpgme(const char *seckey,
166
const char *pubkey, const char *tempdir){
253
168
gpgme_error_t rc;
254
169
gpgme_engine_info_t engine_info;
257
173
* Helper function to insert pub and seckey to the engine keyring.
259
175
bool import_key(const char *filename){
262
177
gpgme_data_t pgp_data;
264
179
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
270
185
rc = gpgme_data_new_from_fd(&pgp_data, fd);
271
186
if(rc != GPG_ERR_NO_ERROR){
272
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
273
gpgme_strsource(rc), gpgme_strerror(rc));
187
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
188
gpgme_strsource(rc), gpgme_strerror(rc));
277
192
rc = gpgme_op_import(mc.ctx, pgp_data);
278
193
if(rc != GPG_ERR_NO_ERROR){
279
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
280
gpgme_strsource(rc), gpgme_strerror(rc));
194
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
195
gpgme_strsource(rc), gpgme_strerror(rc));
284
199
ret = (int)TEMP_FAILURE_RETRY(close(fd));
286
perror_plus("close");
288
203
gpgme_data_release(pgp_data);
293
fprintf_plus(stderr, "Initializing GPGME\n");
208
fprintf(stderr, "Initializing GPGME\n");
297
212
gpgme_check_version(NULL);
298
213
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
299
214
if(rc != GPG_ERR_NO_ERROR){
300
fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
301
gpgme_strsource(rc), gpgme_strerror(rc));
215
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
216
gpgme_strsource(rc), gpgme_strerror(rc));
305
/* Set GPGME home directory for the OpenPGP engine only */
220
/* Set GPGME home directory for the OpenPGP engine only */
306
221
rc = gpgme_get_engine_info(&engine_info);
307
222
if(rc != GPG_ERR_NO_ERROR){
308
fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
309
gpgme_strsource(rc), gpgme_strerror(rc));
223
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
224
gpgme_strsource(rc), gpgme_strerror(rc));
312
227
while(engine_info != NULL){
506
418
/* OpenPGP credentials */
507
ret = gnutls_certificate_allocate_credentials(&mc.cred);
419
gnutls_certificate_allocate_credentials(&mc.cred);
508
420
if(ret != GNUTLS_E_SUCCESS){
509
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
510
safer_gnutls_strerror(ret));
421
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
425
safer_gnutls_strerror(ret));
511
426
gnutls_global_deinit();
516
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
517
" secret key %s as GnuTLS credentials\n",
431
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
432
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
522
436
ret = gnutls_certificate_set_openpgp_key_file
523
437
(mc.cred, pubkeyfilename, seckeyfilename,
524
438
GNUTLS_OPENPGP_FMT_BASE64);
525
439
if(ret != GNUTLS_E_SUCCESS){
527
"Error[%d] while reading the OpenPGP key pair ('%s',"
528
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
529
fprintf_plus(stderr, "The GnuTLS error is: %s\n",
530
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));
534
448
/* GnuTLS server initialization */
535
449
ret = gnutls_dh_params_init(&mc.dh_params);
536
450
if(ret != GNUTLS_E_SUCCESS){
537
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
538
" initialization: %s\n",
539
safer_gnutls_strerror(ret));
451
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
452
" %s\n", safer_gnutls_strerror(ret));
542
455
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
543
456
if(ret != GNUTLS_E_SUCCESS){
544
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
545
safer_gnutls_strerror(ret));
457
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
458
safer_gnutls_strerror(ret));
1115
891
signal_received = sig;
1116
892
int old_errno = errno;
1117
/* set main loop to exit */
1118
if(simple_poll != NULL){
1119
avahi_simple_poll_quit(simple_poll);
1124
bool get_flags(const char *ifname, struct ifreq *ifr){
1128
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1131
perror_plus("socket");
1135
strcpy(ifr->ifr_name, ifname);
1136
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1140
perror_plus("ioctl SIOCGIFFLAGS");
1148
bool good_flags(const char *ifname, const struct ifreq *ifr){
1150
/* Reject the loopback device */
1151
if(ifr->ifr_flags & IFF_LOOPBACK){
1153
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1158
/* Accept point-to-point devices only if connect_to is specified */
1159
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1161
fprintf_plus(stderr, "Accepting point-to-point interface"
1162
" \"%s\"\n", ifname);
1166
/* Otherwise, reject non-broadcast-capable devices */
1167
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1169
fprintf_plus(stderr, "Rejecting non-broadcast interface"
1170
" \"%s\"\n", ifname);
1174
/* Reject non-ARP interfaces (including dummy interfaces) */
1175
if(ifr->ifr_flags & IFF_NOARP){
1177
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1183
/* Accept this device */
1185
fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1191
* This function determines if a directory entry in /sys/class/net
1192
* corresponds to an acceptable network device.
1193
* (This function is passed to scandir(3) as a filter function.)
1195
int good_interface(const struct dirent *if_entry){
1196
if(if_entry->d_name[0] == '.'){
1201
if(not get_flags(if_entry->d_name, &ifr)){
1203
fprintf_plus(stderr, "Failed to get flags for interface "
1204
"\"%s\"\n", if_entry->d_name);
1209
if(not good_flags(if_entry->d_name, &ifr)){
1216
* This function determines if a network interface is up.
1218
bool interface_is_up(const char *interface){
1220
if(not get_flags(interface, &ifr)){
1222
fprintf_plus(stderr, "Failed to get flags for interface "
1223
"\"%s\"\n", interface);
1228
return (bool)(ifr.ifr_flags & IFF_UP);
1232
* This function determines if a network interface is running
1234
bool interface_is_running(const char *interface){
1236
if(not get_flags(interface, &ifr)){
1238
fprintf_plus(stderr, "Failed to get flags for interface "
1239
"\"%s\"\n", interface);
1244
return (bool)(ifr.ifr_flags & IFF_RUNNING);
1247
int notdotentries(const struct dirent *direntry){
1248
/* Skip "." and ".." */
1249
if(direntry->d_name[0] == '.'
1250
and (direntry->d_name[1] == '\0'
1251
or (direntry->d_name[1] == '.'
1252
and direntry->d_name[2] == '\0'))){
1258
/* Is this directory entry a runnable program? */
1259
int runnable_hook(const struct dirent *direntry){
1264
if((direntry->d_name)[0] == '\0'){
1269
sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1270
"abcdefghijklmnopqrstuvwxyz"
1273
if((direntry->d_name)[sret] != '\0'){
1274
/* Contains non-allowed characters */
1276
fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1282
char *fullname = NULL;
1283
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1285
perror_plus("asprintf");
1289
ret = stat(fullname, &st);
1292
perror_plus("Could not stat hook");
1296
if(not (S_ISREG(st.st_mode))){
1297
/* Not a regular file */
1299
fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1304
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1305
/* Not executable */
1307
fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1313
fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
1319
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1321
struct timespec now;
1322
struct timespec waited_time;
1323
intmax_t block_time;
1326
if(mc.current_server == NULL){
1328
fprintf_plus(stderr, "Wait until first server is found."
1331
ret = avahi_simple_poll_iterate(s, -1);
1334
fprintf_plus(stderr, "Check current_server if we should run"
1337
/* the current time */
1338
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1340
perror_plus("clock_gettime");
1343
/* Calculating in ms how long time between now and server
1344
who we visted longest time ago. Now - last seen. */
1345
waited_time.tv_sec = (now.tv_sec
1346
- mc.current_server->last_seen.tv_sec);
1347
waited_time.tv_nsec = (now.tv_nsec
1348
- mc.current_server->last_seen.tv_nsec);
1349
/* total time is 10s/10,000ms.
1350
Converting to s from ms by dividing by 1,000,
1351
and ns to ms by dividing by 1,000,000. */
1352
block_time = ((retry_interval
1353
- ((intmax_t)waited_time.tv_sec * 1000))
1354
- ((intmax_t)waited_time.tv_nsec / 1000000));
1357
fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1361
if(block_time <= 0){
1362
ret = start_mandos_communication(mc.current_server->ip,
1363
mc.current_server->port,
1364
mc.current_server->if_index,
1365
mc.current_server->af);
1367
avahi_simple_poll_quit(simple_poll);
1370
ret = clock_gettime(CLOCK_MONOTONIC,
1371
&mc.current_server->last_seen);
1373
perror_plus("clock_gettime");
1376
mc.current_server = mc.current_server->next;
1377
block_time = 0; /* Call avahi to find new Mandos
1378
servers, but don't block */
1381
ret = avahi_simple_poll_iterate(s, (int)block_time);
1384
if (ret > 0 or errno != EINTR){
1385
return (ret != 1) ? ret : 0;
1391
/* Set effective uid to 0, return errno */
1392
error_t raise_privileges(void){
1393
error_t old_errno = errno;
1394
error_t ret_errno = 0;
1395
if(seteuid(0) == -1){
1397
perror_plus("seteuid");
1403
/* Set effective and real user ID to 0. Return errno. */
1404
error_t raise_privileges_permanently(void){
1405
error_t old_errno = errno;
1406
error_t ret_errno = raise_privileges();
1411
if(setuid(0) == -1){
1413
perror_plus("seteuid");
1419
/* Set effective user ID to unprivileged saved user ID */
1420
error_t lower_privileges(void){
1421
error_t old_errno = errno;
1422
error_t ret_errno = 0;
1423
if(seteuid(uid) == -1){
1425
perror_plus("seteuid");
1431
/* Lower privileges permanently */
1432
error_t lower_privileges_permanently(void){
1433
error_t old_errno = errno;
1434
error_t ret_errno = 0;
1435
if(setuid(uid) == -1){
1437
perror_plus("setuid");
1443
bool run_network_hooks(const char *mode, const char *interface,
1445
struct dirent **direntries;
1446
struct dirent *direntry;
1448
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1451
if(errno == ENOENT){
1453
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1454
" found\n", hookdir);
1457
perror_plus("scandir");
1460
int devnull = open("/dev/null", O_RDONLY);
1461
for(int i = 0; i < numhooks; i++){
1462
direntry = direntries[i];
1463
char *fullname = NULL;
1464
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1466
perror_plus("asprintf");
1470
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1473
pid_t hook_pid = fork();
1476
/* Raise privileges */
1477
raise_privileges_permanently();
1482
perror_plus("setgid");
1484
/* Reset supplementary groups */
1486
ret = setgroups(0, NULL);
1488
perror_plus("setgroups");
1490
dup2(devnull, STDIN_FILENO);
1492
dup2(STDERR_FILENO, STDOUT_FILENO);
1493
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1495
perror_plus("setenv");
1498
ret = setenv("DEVICE", interface, 1);
1500
perror_plus("setenv");
1503
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1505
perror_plus("setenv");
1508
ret = setenv("MODE", mode, 1);
1510
perror_plus("setenv");
1514
ret = asprintf(&delaystring, "%f", delay);
1516
perror_plus("asprintf");
1519
ret = setenv("DELAY", delaystring, 1);
1522
perror_plus("setenv");
1526
if(connect_to != NULL){
1527
ret = setenv("CONNECT", connect_to, 1);
1529
perror_plus("setenv");
1533
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1534
perror_plus("execl");
1535
_exit(EXIT_FAILURE);
1539
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1540
perror_plus("waitpid");
1544
if(WIFEXITED(status)){
1545
if(WEXITSTATUS(status) != 0){
1546
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1547
" with status %d\n", direntry->d_name,
1548
WEXITSTATUS(status));
1552
} else if(WIFSIGNALED(status)){
1553
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1554
" signal %d\n", direntry->d_name,
1559
fprintf_plus(stderr, "Warning: network hook \"%s\""
1560
" crashed\n", direntry->d_name);
1567
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1576
error_t bring_up_interface(const char *const interface,
1579
error_t old_errno = errno;
1580
error_t ret_errno = 0;
1581
int ret, ret_setflags;
1582
struct ifreq network;
1583
unsigned int if_index = if_nametoindex(interface);
1585
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1595
if(not interface_is_up(interface)){
1596
if(not get_flags(interface, &network) and debug){
1598
fprintf_plus(stderr, "Failed to get flags for interface "
1599
"\"%s\"\n", interface);
1602
network.ifr_flags |= IFF_UP;
1604
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1607
perror_plus("socket");
1619
fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
1623
/* Raise priviliges */
1627
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1628
messages about the network interface to mess up the prompt */
1629
int ret_linux = klogctl(8, NULL, 5);
1630
bool restore_loglevel = true;
1631
if(ret_linux == -1){
1632
restore_loglevel = false;
1633
perror_plus("klogctl");
1635
#endif /* __linux__ */
1636
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1639
if(restore_loglevel){
1640
ret_linux = klogctl(7, NULL, 0);
1641
if(ret_linux == -1){
1642
perror_plus("klogctl");
1645
#endif /* __linux__ */
1647
/* Lower privileges */
1650
/* Close the socket */
1651
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1653
perror_plus("close");
1656
if(ret_setflags == -1){
1658
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1663
fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1667
/* Sleep checking until interface is running.
1668
Check every 0.25s, up to total time of delay */
1669
for(int i=0; i < delay * 4; i++){
1670
if(interface_is_running(interface)){
1673
struct timespec sleeptime = { .tv_nsec = 250000000 };
1674
ret = nanosleep(&sleeptime, NULL);
1675
if(ret == -1 and errno != EINTR){
1676
perror_plus("nanosleep");
1684
error_t take_down_interface(const char *const interface){
1686
error_t old_errno = errno;
1687
error_t ret_errno = 0;
1688
int ret, ret_setflags;
1689
struct ifreq network;
1690
unsigned int if_index = if_nametoindex(interface);
1692
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1696
if(interface_is_up(interface)){
1697
if(not get_flags(interface, &network) and debug){
1699
fprintf_plus(stderr, "Failed to get flags for interface "
1700
"\"%s\"\n", interface);
1703
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1705
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1708
perror_plus("socket");
1714
fprintf_plus(stderr, "Taking down interface \"%s\"\n",
1718
/* Raise priviliges */
1721
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1724
/* Lower privileges */
1727
/* Close the socket */
1728
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1730
perror_plus("close");
1733
if(ret_setflags == -1){
1735
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1740
fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
893
if(mc.simple_poll != NULL){
894
avahi_simple_poll_quit(mc.simple_poll);
1748
899
int main(int argc, char *argv[]){
1749
900
AvahiSServiceBrowser *sb = NULL;
1752
903
intmax_t tmpmax;
1754
905
int exitcode = EXIT_SUCCESS;
1755
char *interfaces = NULL;
1756
size_t interfaces_size = 0;
1757
char *interfaces_to_take_down = NULL;
1758
size_t interfaces_to_take_down_size = 0;
906
const char *interface = "eth0";
907
struct ifreq network;
909
bool take_down_interface = false;
912
char *connect_to = NULL;
1759
913
char tempdir[] = "/tmp/mandosXXXXXX";
1760
914
bool tempdir_created = false;
1761
915
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1762
916
const char *seckey = PATHDIR "/" SECKEY;
1763
917
const char *pubkey = PATHDIR "/" PUBKEY;
1764
char *interfaces_hooks = NULL;
1765
size_t interfaces_hooks_size = 0;
1767
919
bool gnutls_initialized = false;
1768
920
bool gpgme_initialized = false;
1769
921
float delay = 2.5f;
1770
double retry_interval = 10; /* 10s between trying a server and
1771
retrying the same server again */
1773
struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
923
struct sigaction old_sigterm_action;
1774
924
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1779
/* Lower any group privileges we might have, just to be safe */
1783
perror_plus("setgid");
1786
/* Lower user privileges (temporarily) */
1790
perror_plus("seteuid");
1798
927
struct argp_option options[] = {
1799
928
{ .name = "debug", .key = 128,
1888
996
delay = strtof(arg, &tmp);
1889
997
if(errno != 0 or tmp == arg or *tmp != '\0'){
1890
argp_error(state, "Bad delay");
1892
case 132: /* --retry */
1894
retry_interval = strtod(arg, &tmp);
1895
if(errno != 0 or tmp == arg or *tmp != '\0'
1896
or (retry_interval * 1000) > INT_MAX
1897
or retry_interval < 0){
1898
argp_error(state, "Bad retry interval");
1901
case 133: /* --network-hook-dir */
1905
* These reproduce what we would get without ARGP_NO_HELP
1907
case '?': /* --help */
1908
argp_state_help(state, state->out_stream,
1909
(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1910
& ~(unsigned int)ARGP_HELP_EXIT_OK);
1911
case -3: /* --usage */
1912
argp_state_help(state, state->out_stream,
1913
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1914
case 'V': /* --version */
1915
fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1916
exit(argp_err_exit_status);
998
fprintf(stderr, "Bad delay\n");
1919
1007
return ARGP_ERR_UNKNOWN;
1924
1012
struct argp argp = { .options = options, .parser = parse_opt,
1925
1013
.args_doc = "",
1926
1014
.doc = "Mandos client -- Get and decrypt"
1927
1015
" passwords from a Mandos server" };
1928
ret = argp_parse(&argp, argc, argv,
1929
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1936
perror_plus("argp_parse");
1937
exitcode = EX_OSERR;
1940
exitcode = EX_USAGE;
1946
/* Work around Debian bug #633582:
1947
<http://bugs.debian.org/633582> */
1949
/* Re-raise priviliges */
1950
if(raise_privileges() == 0){
1953
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1954
int seckey_fd = open(seckey, O_RDONLY);
1955
if(seckey_fd == -1){
1956
perror_plus("open");
1958
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1960
perror_plus("fstat");
1962
if(S_ISREG(st.st_mode)
1963
and st.st_uid == 0 and st.st_gid == 0){
1964
ret = fchown(seckey_fd, uid, gid);
1966
perror_plus("fchown");
1970
TEMP_FAILURE_RETRY(close(seckey_fd));
1974
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1975
int pubkey_fd = open(pubkey, O_RDONLY);
1976
if(pubkey_fd == -1){
1977
perror_plus("open");
1979
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1981
perror_plus("fstat");
1983
if(S_ISREG(st.st_mode)
1984
and st.st_uid == 0 and st.st_gid == 0){
1985
ret = fchown(pubkey_fd, uid, gid);
1987
perror_plus("fchown");
1991
TEMP_FAILURE_RETRY(close(pubkey_fd));
1995
/* Lower privileges */
1999
perror_plus("seteuid");
2004
/* Remove empty interface names */
2006
char *interface = NULL;
2007
while((interface = argz_next(interfaces, interfaces_size,
2009
if(if_nametoindex(interface) == 0){
2010
if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2011
fprintf_plus(stderr, "Not using nonexisting interface"
2012
" \"%s\"\n", interface);
2014
argz_delete(&interfaces, &interfaces_size, interface);
2020
/* Run network hooks */
2023
if(interfaces != NULL){
2024
interfaces_hooks = malloc(interfaces_size);
2025
if(interfaces_hooks == NULL){
2026
perror_plus("malloc");
2029
memcpy(interfaces_hooks, interfaces, interfaces_size);
2030
interfaces_hooks_size = interfaces_size;
2031
argz_stringify(interfaces_hooks, interfaces_hooks_size,
2034
if(not run_network_hooks("start", interfaces_hooks != NULL ?
2035
interfaces_hooks : "", delay)){
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;
2045
1029
from the signal handler */
2046
1030
/* Initialize the pseudo-RNG for Avahi */
2047
1031
srand((unsigned int) time(NULL));
2048
simple_poll = avahi_simple_poll_new();
2049
if(simple_poll == NULL){
2050
fprintf_plus(stderr,
2051
"Avahi: Failed to create simple poll object.\n");
2052
exitcode = EX_UNAVAILABLE;
1032
mc.simple_poll = avahi_simple_poll_new();
1033
if(mc.simple_poll == NULL){
1034
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1035
exitcode = EXIT_FAILURE;
2056
1039
sigemptyset(&sigterm_action.sa_mask);
2057
1040
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
2059
perror_plus("sigaddset");
2060
exitcode = EX_OSERR;
1042
perror("sigaddset");
1043
exitcode = EXIT_FAILURE;
2063
1046
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
2065
perror_plus("sigaddset");
2066
exitcode = EX_OSERR;
1048
perror("sigaddset");
1049
exitcode = EXIT_FAILURE;
2069
1052
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
2071
perror_plus("sigaddset");
2072
exitcode = EX_OSERR;
2075
/* Need to check if the handler is SIG_IGN before handling:
2076
| [[info:libc:Initial Signal Actions]] |
2077
| [[info:libc:Basic Signal Handling]] |
2079
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
2081
perror_plus("sigaction");
2084
if(old_sigterm_action.sa_handler != SIG_IGN){
2085
ret = sigaction(SIGINT, &sigterm_action, NULL);
2087
perror_plus("sigaction");
2088
exitcode = EX_OSERR;
2092
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
2094
perror_plus("sigaction");
2097
if(old_sigterm_action.sa_handler != SIG_IGN){
2098
ret = sigaction(SIGHUP, &sigterm_action, NULL);
2100
perror_plus("sigaction");
2101
exitcode = EX_OSERR;
2105
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
2107
perror_plus("sigaction");
2110
if(old_sigterm_action.sa_handler != SIG_IGN){
2111
ret = sigaction(SIGTERM, &sigterm_action, NULL);
2113
perror_plus("sigaction");
2114
exitcode = EX_OSERR;
2119
/* If no interfaces were specified, make a list */
2120
if(interfaces == NULL){
2121
struct dirent **direntries;
2122
/* Look for any good interfaces */
2123
ret = scandir(sys_class_net, &direntries, good_interface,
2126
/* Add all found interfaces to interfaces list */
2127
for(int i = 0; i < ret; ++i){
2128
ret_errno = argz_add(&interfaces, &interfaces_size,
2129
direntries[i]->d_name);
2131
perror_plus("argz_add");
2135
fprintf_plus(stderr, "Will use interface \"%s\"\n",
2136
direntries[i]->d_name);
2142
fprintf_plus(stderr, "Could not find a network interface\n");
2143
exitcode = EXIT_FAILURE;
2148
/* If we only got one interface, explicitly use only that one */
2149
if(argz_count(interfaces, interfaces_size) == 1){
2151
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2154
if_index = (AvahiIfIndex)if_nametoindex(interfaces);
2157
/* Bring up interfaces which are down */
2158
if(not (argz_count(interfaces, interfaces_size) == 1
2159
and strcmp(interfaces, "none") == 0)){
2160
char *interface = NULL;
2161
while((interface = argz_next(interfaces, interfaces_size,
2163
bool interface_was_up = interface_is_up(interface);
2164
ret = bring_up_interface(interface, delay);
2165
if(not interface_was_up){
2168
perror_plus("Failed to bring up interface");
2170
ret_errno = argz_add(&interfaces_to_take_down,
2171
&interfaces_to_take_down_size,
2178
interfaces_size = 0;
2179
if(debug and (interfaces_to_take_down == NULL)){
2180
fprintf_plus(stderr, "No interfaces were brought up\n");
1054
perror("sigaddset");
1055
exitcode = EXIT_FAILURE;
1058
ret = sigaction(SIGINT, &sigterm_action, &old_sigterm_action);
1060
perror("sigaction");
1061
exitcode = EXIT_FAILURE;
1064
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1066
perror("sigaction");
1067
exitcode = EXIT_FAILURE;
1070
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1072
perror("sigaction");
1073
exitcode = EXIT_FAILURE;
1077
/* If the interface is down, bring it up */
1078
if(interface[0] != '\0'){
1079
if_index = (AvahiIfIndex) if_nametoindex(interface);
1081
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1082
exitcode = EXIT_FAILURE;
1091
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1092
messages to mess up the prompt */
1093
ret = klogctl(8, NULL, 5);
1094
bool restore_loglevel = true;
1096
restore_loglevel = false;
1099
#endif /* __linux__ */
1101
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1104
exitcode = EXIT_FAILURE;
1106
if(restore_loglevel){
1107
ret = klogctl(7, NULL, 0);
1112
#endif /* __linux__ */
1115
strcpy(network.ifr_name, interface);
1116
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1118
perror("ioctl SIOCGIFFLAGS");
1120
if(restore_loglevel){
1121
ret = klogctl(7, NULL, 0);
1126
#endif /* __linux__ */
1127
exitcode = EXIT_FAILURE;
1130
if((network.ifr_flags & IFF_UP) == 0){
1131
network.ifr_flags |= IFF_UP;
1132
take_down_interface = true;
1133
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1135
take_down_interface = false;
1136
perror("ioctl SIOCSIFFLAGS");
1137
exitcode = EXIT_FAILURE;
1139
if(restore_loglevel){
1140
ret = klogctl(7, NULL, 0);
1145
#endif /* __linux__ */
1149
/* sleep checking until interface is running */
1150
for(int i=0; i < delay * 4; i++){
1151
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1153
perror("ioctl SIOCGIFFLAGS");
1154
} else if(network.ifr_flags & IFF_RUNNING){
1157
struct timespec sleeptime = { .tv_nsec = 250000000 };
1158
ret = nanosleep(&sleeptime, NULL);
1159
if(ret == -1 and errno != EINTR){
1160
perror("nanosleep");
1163
if(not take_down_interface){
1164
/* We won't need the socket anymore */
1165
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1171
if(restore_loglevel){
1172
/* Restores kernel loglevel to default */
1173
ret = klogctl(7, NULL, 0);
1178
#endif /* __linux__ */
2376
1371
gpgme_release(mc.ctx);
2379
/* Cleans up the circular linked list of Mandos servers the client
2381
if(mc.current_server != NULL){
2382
mc.current_server->prev->next = NULL;
2383
while(mc.current_server != NULL){
2384
server *next = mc.current_server->next;
2385
free(mc.current_server);
2386
mc.current_server = next;
2390
/* Re-raise priviliges */
2394
/* Run network hooks */
2395
run_network_hooks("stop", interfaces_hooks != NULL ?
2396
interfaces_hooks : "", delay);
2398
/* Take down the network interfaces which were brought up */
2400
char *interface = NULL;
2401
while((interface=argz_next(interfaces_to_take_down,
2402
interfaces_to_take_down_size,
2404
ret_errno = take_down_interface(interface);
2407
perror_plus("Failed to take down interface");
2410
if(debug and (interfaces_to_take_down == NULL)){
2411
fprintf_plus(stderr, "No interfaces needed to be taken"
2416
lower_privileges_permanently();
2419
free(interfaces_to_take_down);
2420
free(interfaces_hooks);
2422
/* Removes the GPGME temp directory and all files inside */
1374
/* Take down the network interface */
1375
if(take_down_interface){
1376
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1378
perror("ioctl SIOCGIFFLAGS");
1379
} else if(network.ifr_flags & IFF_UP) {
1380
network.ifr_flags &= ~IFF_UP; /* clear flag */
1381
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1383
perror("ioctl SIOCSIFFLAGS");
1386
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1392
/* Removes the temp directory used by GPGME */
2423
1393
if(tempdir_created){
2424
struct dirent **direntries = NULL;
2425
struct dirent *direntry = NULL;
2426
int numentries = scandir(tempdir, &direntries, notdotentries,
2428
if (numentries > 0){
2429
for(int i = 0; i < numentries; i++){
2430
direntry = direntries[i];
1395
struct dirent *direntry;
1396
d = opendir(tempdir);
1398
if(errno != ENOENT){
1403
direntry = readdir(d);
1404
if(direntry == NULL){
1407
/* Skip "." and ".." */
1408
if(direntry->d_name[0] == '.'
1409
and (direntry->d_name[1] == '\0'
1410
or (direntry->d_name[1] == '.'
1411
and direntry->d_name[2] == '\0'))){
2431
1414
char *fullname = NULL;
2432
1415
ret = asprintf(&fullname, "%s/%s", tempdir,
2433
1416
direntry->d_name);
2435
perror_plus("asprintf");
2438
1421
ret = remove(fullname);
2440
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
1423
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2443
1426
free(fullname);
2447
/* need to clean even if 0 because man page doesn't specify */
2449
if (numentries == -1){
2450
perror_plus("scandir");
2452
1430
ret = rmdir(tempdir);
2453
1431
if(ret == -1 and errno != ENOENT){
2454
perror_plus("rmdir");
2459
sigemptyset(&old_sigterm_action.sa_mask);
2460
old_sigterm_action.sa_handler = SIG_DFL;
2461
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
2462
&old_sigterm_action,
1437
ret = sigaction(signal_received, &old_sigterm_action, NULL);
2465
perror_plus("sigaction");
2468
ret = raise(signal_received);
2469
} while(ret != 0 and errno == EINTR);
2471
perror_plus("raise");
2474
TEMP_FAILURE_RETRY(pause());
1439
perror("sigaction");
1441
raise(signal_received);
2477
1444
return exitcode;