122
129
#define PATHDIR "/conf/conf.d/mandos"
123
130
#define SECKEY "seckey.txt"
124
131
#define PUBKEY "pubkey.txt"
132
#define HOOKDIR "/lib/mandos/network-hooks.d"
126
134
bool debug = false;
127
135
static const char mandos_protocol_version[] = "1";
128
136
const char *argp_program_version = "mandos-client " VERSION;
129
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
137
const char *argp_program_bug_address = "<mandos@recompile.se>";
138
static const char sys_class_net[] = "/sys/class/net";
139
char *connect_to = NULL;
140
const char *hookdir = HOOKDIR;
144
/* Doubly linked list that need to be circularly linked when used */
145
typedef struct server{
148
AvahiIfIndex if_index;
150
struct timespec last_seen;
131
155
/* Used for passing in values through the Avahi callback functions */
133
AvahiSimplePoll *simple_poll;
134
157
AvahiServer *server;
135
158
gnutls_certificate_credentials_t cred;
136
159
unsigned int dh_bits;
137
160
gnutls_dh_params_t dh_params;
138
161
const char *priority;
163
server *current_server;
140
164
} mandos_context;
142
/* global context so signal handler can reach it*/
143
mandos_context mc = { .simple_poll = NULL, .server = NULL,
144
.dh_bits = 1024, .priority = "SECURE256"
145
":!CTYPE-X.509:+CTYPE-OPENPGP" };
166
/* global so signal handler can reach it*/
167
AvahiSimplePoll *simple_poll;
168
mandos_context mc = { .server = NULL, .dh_bits = 1024,
169
.priority = "SECURE256:!CTYPE-X.509:"
170
"+CTYPE-OPENPGP", .current_server = NULL };
147
172
sig_atomic_t quit_now = 0;
148
173
int signal_received = 0;
175
/* Function to use when printing errors */
176
void perror_plus(const char *print_text){
178
fprintf(stderr, "Mandos plugin %s: ",
179
program_invocation_short_name);
184
__attribute__((format (gnu_printf, 2, 3)))
185
int fprintf_plus(FILE *stream, const char *format, ...){
187
va_start (ap, format);
189
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
190
program_invocation_short_name));
191
return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
151
195
* Make additional room in "buffer" for at least BUFFER_SIZE more
152
196
* bytes. "buffer_capacity" is how much is currently allocated,
153
197
* "buffer_length" is how much is already used.
155
199
size_t incbuffer(char **buffer, size_t buffer_length,
156
size_t buffer_capacity){
200
size_t buffer_capacity){
157
201
if(buffer_length + BUFFER_SIZE > buffer_capacity){
158
202
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
159
203
if(buffer == NULL){
297
380
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
298
381
if(rc != GPG_ERR_NO_ERROR){
299
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
300
gpgme_strsource(rc), gpgme_strerror(rc));
382
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
383
gpgme_strsource(rc), gpgme_strerror(rc));
301
384
plaintext_length = -1;
303
386
gpgme_decrypt_result_t result;
304
387
result = gpgme_op_decrypt_result(mc.ctx);
305
388
if(result == NULL){
306
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
389
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
308
fprintf(stderr, "Unsupported algorithm: %s\n",
309
result->unsupported_algorithm);
310
fprintf(stderr, "Wrong key usage: %u\n",
311
result->wrong_key_usage);
391
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
392
result->unsupported_algorithm);
393
fprintf_plus(stderr, "Wrong key usage: %u\n",
394
result->wrong_key_usage);
312
395
if(result->file_name != NULL){
313
fprintf(stderr, "File name: %s\n", result->file_name);
396
fprintf_plus(stderr, "File name: %s\n", result->file_name);
315
398
gpgme_recipient_t recipient;
316
399
recipient = result->recipients;
317
400
while(recipient != NULL){
318
fprintf(stderr, "Public key algorithm: %s\n",
319
gpgme_pubkey_algo_name(recipient->pubkey_algo));
320
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
321
fprintf(stderr, "Secret key available: %s\n",
322
recipient->status == GPG_ERR_NO_SECKEY
401
fprintf_plus(stderr, "Public key algorithm: %s\n",
402
gpgme_pubkey_algo_name
403
(recipient->pubkey_algo));
404
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
405
fprintf_plus(stderr, "Secret key available: %s\n",
406
recipient->status == GPG_ERR_NO_SECKEY
324
408
recipient = recipient->next;
423
506
/* OpenPGP credentials */
424
gnutls_certificate_allocate_credentials(&mc.cred);
507
ret = gnutls_certificate_allocate_credentials(&mc.cred);
425
508
if(ret != GNUTLS_E_SUCCESS){
426
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
430
safer_gnutls_strerror(ret));
509
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
510
safer_gnutls_strerror(ret));
431
511
gnutls_global_deinit();
436
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
437
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
516
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
517
" secret key %s as GnuTLS credentials\n",
441
522
ret = gnutls_certificate_set_openpgp_key_file
442
523
(mc.cred, pubkeyfilename, seckeyfilename,
443
524
GNUTLS_OPENPGP_FMT_BASE64);
444
525
if(ret != GNUTLS_E_SUCCESS){
446
"Error[%d] while reading the OpenPGP key pair ('%s',"
447
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
448
fprintf(stderr, "The GnuTLS error is: %s\n",
449
safer_gnutls_strerror(ret));
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));
453
534
/* GnuTLS server initialization */
454
535
ret = gnutls_dh_params_init(&mc.dh_params);
455
536
if(ret != GNUTLS_E_SUCCESS){
456
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
457
" %s\n", safer_gnutls_strerror(ret));
537
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
538
" initialization: %s\n",
539
safer_gnutls_strerror(ret));
460
542
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
461
543
if(ret != GNUTLS_E_SUCCESS){
462
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
463
safer_gnutls_strerror(ret));
544
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
545
safer_gnutls_strerror(ret));
1015
1115
signal_received = sig;
1016
1116
int old_errno = errno;
1017
if(mc.simple_poll != NULL){
1018
avahi_simple_poll_quit(mc.simple_poll);
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",
1023
1748
int main(int argc, char *argv[]){
1024
1749
AvahiSServiceBrowser *sb = NULL;
1027
1752
intmax_t tmpmax;
1029
1754
int exitcode = EXIT_SUCCESS;
1030
const char *interface = "eth0";
1031
struct ifreq network;
1033
bool take_down_interface = false;
1036
char *connect_to = NULL;
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;
1037
1759
char tempdir[] = "/tmp/mandosXXXXXX";
1038
1760
bool tempdir_created = false;
1039
1761
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1040
1762
const char *seckey = PATHDIR "/" SECKEY;
1041
1763
const char *pubkey = PATHDIR "/" PUBKEY;
1764
char *interfaces_hooks = NULL;
1765
size_t interfaces_hooks_size = 0;
1043
1767
bool gnutls_initialized = false;
1044
1768
bool gpgme_initialized = false;
1045
1769
float delay = 2.5f;
1770
double retry_interval = 10; /* 10s between trying a server and
1771
retrying the same server again */
1047
1773
struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1048
1774
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1234
2079
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1236
perror("sigaction");
2081
perror_plus("sigaction");
1237
2082
return EX_OSERR;
1239
2084
if(old_sigterm_action.sa_handler != SIG_IGN){
1240
2085
ret = sigaction(SIGINT, &sigterm_action, NULL);
1242
perror("sigaction");
2087
perror_plus("sigaction");
1243
2088
exitcode = EX_OSERR;
1247
2092
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1249
perror("sigaction");
2094
perror_plus("sigaction");
1250
2095
return EX_OSERR;
1252
2097
if(old_sigterm_action.sa_handler != SIG_IGN){
1253
2098
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1255
perror("sigaction");
2100
perror_plus("sigaction");
1256
2101
exitcode = EX_OSERR;
1260
2105
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1262
perror("sigaction");
2107
perror_plus("sigaction");
1263
2108
return EX_OSERR;
1265
2110
if(old_sigterm_action.sa_handler != SIG_IGN){
1266
2111
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1268
perror("sigaction");
2113
perror_plus("sigaction");
1269
2114
exitcode = EX_OSERR;
1274
/* If the interface is down, bring it up */
1275
if(interface[0] != '\0'){
1276
if_index = (AvahiIfIndex) if_nametoindex(interface);
1278
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1279
exitcode = EX_UNAVAILABLE;
1287
/* Re-raise priviliges */
1295
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1296
messages about the network interface to mess up the prompt */
1297
ret = klogctl(8, NULL, 5);
1298
bool restore_loglevel = true;
1300
restore_loglevel = false;
1303
#endif /* __linux__ */
1305
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1308
exitcode = EX_OSERR;
1310
if(restore_loglevel){
1311
ret = klogctl(7, NULL, 0);
1316
#endif /* __linux__ */
1317
/* Lower privileges */
1325
strcpy(network.ifr_name, interface);
1326
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1328
perror("ioctl SIOCGIFFLAGS");
1330
if(restore_loglevel){
1331
ret = klogctl(7, NULL, 0);
1336
#endif /* __linux__ */
1337
exitcode = EX_OSERR;
1338
/* Lower privileges */
1346
if((network.ifr_flags & IFF_UP) == 0){
1347
network.ifr_flags |= IFF_UP;
1348
take_down_interface = true;
1349
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1351
take_down_interface = false;
1352
perror("ioctl SIOCSIFFLAGS");
1353
exitcode = EX_OSERR;
1355
if(restore_loglevel){
1356
ret = klogctl(7, NULL, 0);
1361
#endif /* __linux__ */
1362
/* Lower privileges */
1371
/* sleep checking until interface is running */
1372
for(int i=0; i < delay * 4; i++){
1373
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1375
perror("ioctl SIOCGIFFLAGS");
1376
} else if(network.ifr_flags & IFF_RUNNING){
1379
struct timespec sleeptime = { .tv_nsec = 250000000 };
1380
ret = nanosleep(&sleeptime, NULL);
1381
if(ret == -1 and errno != EINTR){
1382
perror("nanosleep");
1385
if(not take_down_interface){
1386
/* We won't need the socket anymore */
1387
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1393
if(restore_loglevel){
1394
/* Restores kernel loglevel to default */
1395
ret = klogctl(7, NULL, 0);
1400
#endif /* __linux__ */
1401
/* Lower privileges */
1403
if(take_down_interface){
1404
/* Lower privileges */
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);
1410
/* Lower privileges permanently */
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");
1608
2376
gpgme_release(mc.ctx);
1611
/* Take down the network interface */
1612
if(take_down_interface){
1613
/* Re-raise priviliges */
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;
1620
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1622
perror("ioctl SIOCGIFFLAGS");
1623
} else if(network.ifr_flags & IFF_UP) {
1624
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1625
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1627
perror("ioctl SIOCSIFFLAGS");
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");
1630
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1634
/* Lower privileges permanently */
2410
if(debug and (interfaces_to_take_down == NULL)){
2411
fprintf_plus(stderr, "No interfaces needed to be taken"
2416
lower_privileges_permanently();
1643
/* Removes the temp directory used by GPGME */
2419
free(interfaces_to_take_down);
2420
free(interfaces_hooks);
2422
/* Removes the GPGME temp directory and all files inside */
1644
2423
if(tempdir_created){
1646
struct dirent *direntry;
1647
d = opendir(tempdir);
1649
if(errno != ENOENT){
1654
direntry = readdir(d);
1655
if(direntry == NULL){
1658
/* Skip "." and ".." */
1659
if(direntry->d_name[0] == '.'
1660
and (direntry->d_name[1] == '\0'
1661
or (direntry->d_name[1] == '.'
1662
and direntry->d_name[2] == '\0'))){
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];
1665
2431
char *fullname = NULL;
1666
2432
ret = asprintf(&fullname, "%s/%s", tempdir,
1667
2433
direntry->d_name);
2435
perror_plus("asprintf");
1672
2438
ret = remove(fullname);
1674
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2440
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
1677
2443
free(fullname);
2447
/* need to clean even if 0 because man page doesn't specify */
2449
if (numentries == -1){
2450
perror_plus("scandir");
1681
2452
ret = rmdir(tempdir);
1682
2453
if(ret == -1 and errno != ENOENT){
2454
perror_plus("rmdir");