32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
33
#ifndef _LARGEFILE_SOURCE
34
33
#define _LARGEFILE_SOURCE
36
#ifndef _FILE_OFFSET_BITS
37
34
#define _FILE_OFFSET_BITS 64
40
36
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
42
38
#include <stdio.h> /* fprintf(), stderr, fwrite(),
43
stdout, ferror(), remove() */
39
stdout, ferror(), sscanf(),
44
41
#include <stdint.h> /* uint16_t, uint32_t */
45
42
#include <stddef.h> /* NULL, size_t, ssize_t */
46
#include <stdlib.h> /* free(), EXIT_SUCCESS, srand(),
48
#include <stdbool.h> /* bool, false, true */
43
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
45
#include <stdbool.h> /* bool, true */
49
46
#include <string.h> /* memset(), strcmp(), strlen(),
50
47
strerror(), asprintf(), strcpy() */
51
#include <sys/ioctl.h> /* ioctl */
48
#include <sys/ioctl.h> /* ioctl */
52
49
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
53
50
sockaddr_in6, PF_INET6,
54
SOCK_STREAM, uid_t, gid_t, open(),
51
SOCK_STREAM, INET6_ADDRSTRLEN,
52
uid_t, gid_t, open(), opendir(),
56
54
#include <sys/stat.h> /* open() */
57
55
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
58
inet_pton(), connect() */
56
struct in6_addr, inet_pton(),
59
58
#include <fcntl.h> /* open() */
60
59
#include <dirent.h> /* opendir(), struct dirent, readdir()
62
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
61
#include <inttypes.h> /* PRIu16, intmax_t, SCNdMAX */
64
62
#include <assert.h> /* assert() */
65
#include <errno.h> /* perror(), errno,
66
program_invocation_short_name */
67
#include <time.h> /* nanosleep(), time(), sleep() */
63
#include <errno.h> /* perror(), errno */
64
#include <time.h> /* nanosleep(), time() */
68
65
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
69
66
SIOCSIFFLAGS, if_indextoname(),
70
67
if_nametoindex(), IF_NAMESIZE */
71
#include <netinet/in.h> /* IN6_IS_ADDR_LINKLOCAL,
72
INET_ADDRSTRLEN, INET6_ADDRSTRLEN
68
#include <netinet/in.h>
74
69
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
75
getuid(), getgid(), seteuid(),
77
#include <arpa/inet.h> /* inet_pton(), htons, inet_ntop() */
78
#include <iso646.h> /* not, or, and */
70
getuid(), getgid(), setuid(),
72
#include <arpa/inet.h> /* inet_pton(), htons */
73
#include <iso646.h> /* not, and, or */
79
74
#include <argp.h> /* struct argp_option, error_t, struct
80
75
argp_state, struct argp,
81
76
argp_parse(), ARGP_KEY_ARG,
82
77
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
83
#include <signal.h> /* sigemptyset(), sigaddset(),
84
sigaction(), SIGTERM, sig_atomic_t,
86
#include <sysexits.h> /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
87
EX_NOHOST, EX_IOERR, EX_PROTOCOL */
90
79
#include <sys/klog.h> /* klogctl() */
91
#endif /* __linux__ */
94
83
/* All Avahi types, constants and functions
510
439
/* GnuTLS server initialization */
511
ret = gnutls_dh_params_init(&mc.dh_params);
440
ret = gnutls_dh_params_init(&mc->dh_params);
512
441
if(ret != GNUTLS_E_SUCCESS){
513
442
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
514
443
" %s\n", safer_gnutls_strerror(ret));
517
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
446
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
518
447
if(ret != GNUTLS_E_SUCCESS){
519
448
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
520
449
safer_gnutls_strerror(ret));
524
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
453
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
530
gnutls_certificate_free_credentials(mc.cred);
459
gnutls_certificate_free_credentials(mc->cred);
531
460
gnutls_global_deinit();
532
gnutls_dh_params_deinit(mc.dh_params);
461
gnutls_dh_params_deinit(mc->dh_params);
536
static int init_gnutls_session(gnutls_session_t *session){
465
static int init_gnutls_session(mandos_context *mc,
466
gnutls_session_t *session){
538
468
/* GnuTLS session creation */
540
ret = gnutls_init(session, GNUTLS_SERVER);
544
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
469
ret = gnutls_init(session, GNUTLS_SERVER);
545
470
if(ret != GNUTLS_E_SUCCESS){
546
471
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
547
472
safer_gnutls_strerror(ret));
595
509
/* Called when a Mandos server is found */
596
510
static int start_mandos_communication(const char *ip, uint16_t port,
597
511
AvahiIfIndex if_index,
599
int ret, tcp_sd = -1;
602
struct sockaddr_in in;
603
struct sockaddr_in6 in6;
515
union { struct sockaddr in; struct sockaddr_in6 in6; } to;
605
516
char *buffer = NULL;
606
char *decrypted_buffer = NULL;
517
char *decrypted_buffer;
607
518
size_t buffer_length = 0;
608
519
size_t buffer_capacity = 0;
520
ssize_t decrypted_buffer_size;
523
char interface[IF_NAMESIZE];
611
524
gnutls_session_t session;
612
int pf; /* Protocol family */
629
fprintf(stderr, "Bad address family: %d\n", af);
634
ret = init_gnutls_session(&session);
526
ret = init_gnutls_session(mc, &session);
640
fprintf(stderr, "Setting up a TCP connection to %s, port %" PRIu16
532
fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
644
tcp_sd = socket(pf, SOCK_STREAM, 0);
536
tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
647
perror_plus("socket");
543
if(if_indextoname((unsigned int)if_index, interface) == NULL){
544
perror("if_indextoname");
547
fprintf(stderr, "Binding to interface %s\n", interface);
657
550
memset(&to, 0, sizeof(to));
659
to.in6.sin6_family = (sa_family_t)af;
660
ret = inet_pton(af, ip, &to.in6.sin6_addr);
662
to.in.sin_family = (sa_family_t)af;
663
ret = inet_pton(af, ip, &to.in.sin_addr);
551
to.in6.sin6_family = AF_INET6;
552
/* It would be nice to have a way to detect if we were passed an
553
IPv4 address here. Now we assume an IPv6 address. */
554
ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
667
perror_plus("inet_pton");
673
560
fprintf(stderr, "Bad address: %s\n", ip);
678
to.in6.sin6_port = htons(port); /* Spurious warnings from
680
-Wunreachable-code */
682
if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
683
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
685
if(if_index == AVAHI_IF_UNSPEC){
686
fprintf(stderr, "An IPv6 link-local address is incomplete"
687
" without a network interface\n");
691
/* Set the network interface number as scope */
692
to.in6.sin6_scope_id = (uint32_t)if_index;
695
to.in.sin_port = htons(port); /* Spurious warnings from
563
to.in6.sin6_port = htons(port); /* Spurious warnings from
697
565
-Wunreachable-code */
567
to.in6.sin6_scope_id = (uint32_t)if_index;
706
if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
707
char interface[IF_NAMESIZE];
708
if(if_indextoname((unsigned int)if_index, interface) == NULL){
709
perror_plus("if_indextoname");
711
fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
712
ip, interface, port);
715
fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
718
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
719
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
722
pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
725
pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
729
perror_plus("inet_ntop");
570
fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
572
char addrstr[INET6_ADDRSTRLEN] = "";
573
if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
574
sizeof(addrstr)) == NULL){
731
577
if(strcmp(addrstr, ip) != 0){
732
578
fprintf(stderr, "Canonical address form: %s\n", addrstr);
1072
/* Signal handler that stops main loop after SIGTERM */
1073
static void handle_sigterm(int sig){
1078
signal_received = sig;
1079
int old_errno = errno;
1080
/* set main loop to exit */
1081
if(mc.simple_poll != NULL){
1082
avahi_simple_poll_quit(mc.simple_poll);
1088
* This function determines if a directory entry in /sys/class/net
1089
* corresponds to an acceptable network device.
1090
* (This function is passed to scandir(3) as a filter function.)
1092
int good_interface(const struct dirent *if_entry){
1094
char *flagname = NULL;
1095
if(if_entry->d_name[0] == '.'){
1098
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1101
perror_plus("asprintf");
1104
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1106
perror_plus("open");
1111
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1112
/* read line from flags_fd */
1113
ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1114
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1115
flagstring[(size_t)to_read] = '\0';
1116
if(flagstring == NULL){
1117
perror_plus("malloc");
1122
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1125
perror_plus("read");
1139
tmpmax = strtoimax(flagstring, &tmp, 0);
1140
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1141
and not (isspace(*tmp)))
1142
or tmpmax != (ifreq_flags)tmpmax){
1144
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1145
flagstring, if_entry->d_name);
1151
ifreq_flags flags = (ifreq_flags)tmpmax;
1152
/* Reject the loopback device */
1153
if(flags & IFF_LOOPBACK){
1155
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1160
/* Accept point-to-point devices only if connect_to is specified */
1161
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1163
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1168
/* Otherwise, reject non-broadcast-capable devices */
1169
if(not (flags & IFF_BROADCAST)){
1171
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1176
/* Reject non-ARP interfaces (including dummy interfaces) */
1177
if(flags & IFF_NOARP){
1179
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1184
/* Accept this device */
1186
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1192
int notdotentries(const struct dirent *direntry){
1193
/* Skip "." and ".." */
1194
if(direntry->d_name[0] == '.'
1195
and (direntry->d_name[1] == '\0'
1196
or (direntry->d_name[1] == '.'
1197
and direntry->d_name[2] == '\0'))){
1203
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1205
struct timespec now;
1206
struct timespec waited_time;
1207
intmax_t block_time;
1210
if(mc.current_server == NULL){
1213
"Wait until first server is found. No timeout!\n");
1215
ret = avahi_simple_poll_iterate(s, -1);
1218
fprintf(stderr, "Check current_server if we should run it,"
1221
/* the current time */
1222
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1224
perror_plus("clock_gettime");
1227
/* Calculating in ms how long time between now and server
1228
who we visted longest time ago. Now - last seen. */
1229
waited_time.tv_sec = (now.tv_sec
1230
- mc.current_server->last_seen.tv_sec);
1231
waited_time.tv_nsec = (now.tv_nsec
1232
- mc.current_server->last_seen.tv_nsec);
1233
/* total time is 10s/10,000ms.
1234
Converting to s from ms by dividing by 1,000,
1235
and ns to ms by dividing by 1,000,000. */
1236
block_time = ((retry_interval
1237
- ((intmax_t)waited_time.tv_sec * 1000))
1238
- ((intmax_t)waited_time.tv_nsec / 1000000));
1241
fprintf(stderr, "Blocking for %ld ms\n", block_time);
1244
if(block_time <= 0){
1245
ret = start_mandos_communication(mc.current_server->ip,
1246
mc.current_server->port,
1247
mc.current_server->if_index,
1248
mc.current_server->af);
1250
avahi_simple_poll_quit(mc.simple_poll);
1253
ret = clock_gettime(CLOCK_MONOTONIC,
1254
&mc.current_server->last_seen);
1256
perror_plus("clock_gettime");
1259
mc.current_server = mc.current_server->next;
1260
block_time = 0; /* Call avahi to find new Mandos
1261
servers, but don't block */
1264
ret = avahi_simple_poll_iterate(s, (int)block_time);
1267
if (ret > 0 or errno != EINTR) {
1268
return (ret != 1) ? ret : 0;
1274
824
int main(int argc, char *argv[]){
1275
825
AvahiSServiceBrowser *sb = NULL;
1278
828
intmax_t tmpmax;
1280
830
int exitcode = EXIT_SUCCESS;
1281
const char *interface = "";
831
const char *interface = "eth0";
1282
832
struct ifreq network;
1284
bool take_down_interface = false;
836
char *connect_to = NULL;
1287
837
char tempdir[] = "/tmp/mandosXXXXXX";
1288
838
bool tempdir_created = false;
1289
839
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1290
840
const char *seckey = PATHDIR "/" SECKEY;
1291
841
const char *pubkey = PATHDIR "/" PUBKEY;
843
mandos_context mc = { .simple_poll = NULL, .server = NULL,
844
.dh_bits = 1024, .priority = "SECURE256"
845
":!CTYPE-X.509:+CTYPE-OPENPGP" };
1293
846
bool gnutls_initialized = false;
1294
847
bool gpgme_initialized = false;
1296
double retry_interval = 10; /* 10s between trying a server and
1297
retrying the same server again */
1299
struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1300
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1305
/* Lower any group privileges we might have, just to be safe */
1309
perror_plus("setgid");
1312
/* Lower user privileges (temporarily) */
1316
perror_plus("seteuid");
1324
851
struct argp_option options[] = {
1402
915
mc.priority = arg;
1404
917
case 131: /* --delay */
1406
delay = strtof(arg, &tmp);
1407
if(errno != 0 or tmp == arg or *tmp != '\0'){
1408
argp_error(state, "Bad delay");
1410
case 132: /* --retry */
1412
retry_interval = strtod(arg, &tmp);
1413
if(errno != 0 or tmp == arg or *tmp != '\0'
1414
or (retry_interval * 1000) > INT_MAX){
1415
argp_error(state, "Bad retry interval");
918
ret = sscanf(arg, "%lf%n", &delay, &numchars);
919
if(ret < 1 or arg[numchars] != '\0'){
920
fprintf(stderr, "Bad delay\n");
1419
* These reproduce what we would get without ARGP_NO_HELP
1421
case '?': /* --help */
1422
argp_state_help(state, state->out_stream,
1423
(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1424
& ~(unsigned int)ARGP_HELP_EXIT_OK);
1425
case -3: /* --usage */
1426
argp_state_help(state, state->out_stream,
1427
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1428
case 'V': /* --version */
1429
fprintf(state->out_stream, "%s\n", argp_program_version);
1430
exit(argp_err_exit_status);
1433
929
return ARGP_ERR_UNKNOWN;
1438
934
struct argp argp = { .options = options, .parser = parse_opt,
1440
936
.doc = "Mandos client -- Get and decrypt"
1441
937
" passwords from a Mandos server" };
1442
ret = argp_parse(&argp, argc, argv,
1443
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1450
perror_plus("argp_parse");
1451
exitcode = EX_OSERR;
1454
exitcode = EX_USAGE;
1460
avahi_set_log_function(empty_log);
1463
if(interface[0] == '\0'){
1464
struct dirent **direntries;
1465
ret = scandir(sys_class_net, &direntries, good_interface,
1468
/* Pick the first good interface */
1469
interface = strdup(direntries[0]->d_name);
1471
fprintf(stderr, "Using interface \"%s\"\n", interface);
1473
if(interface == NULL){
1474
perror_plus("malloc");
1476
exitcode = EXIT_FAILURE;
1482
fprintf(stderr, "Could not find a network interface\n");
938
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
939
if(ret == ARGP_ERR_UNKNOWN){
940
fprintf(stderr, "Unknown error while parsing arguments\n");
1483
941
exitcode = EXIT_FAILURE;
1488
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1489
from the signal handler */
1490
/* Initialize the pseudo-RNG for Avahi */
1491
srand((unsigned int) time(NULL));
1492
mc.simple_poll = avahi_simple_poll_new();
1493
if(mc.simple_poll == NULL){
1494
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1495
exitcode = EX_UNAVAILABLE;
1499
sigemptyset(&sigterm_action.sa_mask);
1500
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1502
perror_plus("sigaddset");
1503
exitcode = EX_OSERR;
1506
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1508
perror_plus("sigaddset");
1509
exitcode = EX_OSERR;
1512
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1514
perror_plus("sigaddset");
1515
exitcode = EX_OSERR;
1518
/* Need to check if the handler is SIG_IGN before handling:
1519
| [[info:libc:Initial Signal Actions]] |
1520
| [[info:libc:Basic Signal Handling]] |
1522
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1524
perror_plus("sigaction");
1527
if(old_sigterm_action.sa_handler != SIG_IGN){
1528
ret = sigaction(SIGINT, &sigterm_action, NULL);
1530
perror_plus("sigaction");
1531
exitcode = EX_OSERR;
1535
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1537
perror_plus("sigaction");
1540
if(old_sigterm_action.sa_handler != SIG_IGN){
1541
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1543
perror_plus("sigaction");
1544
exitcode = EX_OSERR;
1548
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1550
perror_plus("sigaction");
1553
if(old_sigterm_action.sa_handler != SIG_IGN){
1554
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1556
perror_plus("sigaction");
1557
exitcode = EX_OSERR;
1562
946
/* If the interface is down, bring it up */
1563
if(strcmp(interface, "none") != 0){
1564
if_index = (AvahiIfIndex) if_nametoindex(interface);
1566
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1567
exitcode = EX_UNAVAILABLE;
1575
/* Re-raise priviliges */
1579
perror_plus("seteuid");
1582
948
#ifdef __linux__
1583
949
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1584
messages about the network interface to mess up the prompt */
950
messages to mess up the prompt */
1585
951
ret = klogctl(8, NULL, 5);
1586
bool restore_loglevel = true;
1588
restore_loglevel = false;
1589
perror_plus("klogctl");
1591
#endif /* __linux__ */
1593
957
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1595
perror_plus("socket");
1596
exitcode = EX_OSERR;
960
exitcode = EXIT_FAILURE;
1597
961
#ifdef __linux__
1598
if(restore_loglevel){
1599
ret = klogctl(7, NULL, 0);
1601
perror_plus("klogctl");
1604
#endif /* __linux__ */
1605
/* Lower privileges */
962
ret = klogctl(7, NULL, 0);
1609
perror_plus("seteuid");
1613
969
strcpy(network.ifr_name, interface);
1614
970
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1616
perror_plus("ioctl SIOCGIFFLAGS");
972
perror("ioctl SIOCGIFFLAGS");
1617
973
#ifdef __linux__
1618
if(restore_loglevel){
1619
ret = klogctl(7, NULL, 0);
1621
perror_plus("klogctl");
1624
#endif /* __linux__ */
1625
exitcode = EX_OSERR;
1626
/* Lower privileges */
974
ret = klogctl(7, NULL, 0);
1630
perror_plus("seteuid");
979
exitcode = EXIT_FAILURE;
1634
982
if((network.ifr_flags & IFF_UP) == 0){
1635
983
network.ifr_flags |= IFF_UP;
1636
take_down_interface = true;
1637
984
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1639
take_down_interface = false;
1640
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1641
exitcode = EX_OSERR;
986
perror("ioctl SIOCSIFFLAGS");
987
exitcode = EXIT_FAILURE;
1642
988
#ifdef __linux__
1643
if(restore_loglevel){
1644
ret = klogctl(7, NULL, 0);
1646
perror_plus("klogctl");
1649
#endif /* __linux__ */
1650
/* Lower privileges */
989
ret = klogctl(7, NULL, 0);
1654
perror_plus("seteuid");
1659
/* Sleep checking until interface is running.
1660
Check every 0.25s, up to total time of delay */
997
/* sleep checking until interface is running */
1661
998
for(int i=0; i < delay * 4; i++){
1662
999
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1664
perror_plus("ioctl SIOCGIFFLAGS");
1001
perror("ioctl SIOCGIFFLAGS");
1665
1002
} else if(network.ifr_flags & IFF_RUNNING){
1668
1005
struct timespec sleeptime = { .tv_nsec = 250000000 };
1669
1006
ret = nanosleep(&sleeptime, NULL);
1670
1007
if(ret == -1 and errno != EINTR){
1671
perror_plus("nanosleep");
1008
perror("nanosleep");
1674
if(not take_down_interface){
1675
/* We won't need the socket anymore */
1676
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1678
perror_plus("close");
1011
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1681
1015
#ifdef __linux__
1682
if(restore_loglevel){
1683
/* Restores kernel loglevel to default */
1684
ret = klogctl(7, NULL, 0);
1686
perror_plus("klogctl");
1689
#endif /* __linux__ */
1690
/* Lower privileges */
1692
if(take_down_interface){
1693
/* Lower privileges */
1696
perror_plus("seteuid");
1699
/* Lower privileges permanently */
1702
perror_plus("setuid");
1711
ret = init_gnutls_global(pubkey, seckey);
1016
/* Restores kernel loglevel to default */
1017
ret = klogctl(7, NULL, 0);
1037
ret = init_gnutls_global(&mc, pubkey, seckey);
1713
1039
fprintf(stderr, "init_gnutls_global failed\n");
1714
exitcode = EX_UNAVAILABLE;
1040
exitcode = EXIT_FAILURE;
1717
1043
gnutls_initialized = true;
1724
1046
if(mkdtemp(tempdir) == NULL){
1725
perror_plus("mkdtemp");
1728
1050
tempdir_created = true;
1734
if(not init_gpgme(pubkey, seckey, tempdir)){
1052
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
1735
1053
fprintf(stderr, "init_gpgme failed\n");
1736
exitcode = EX_UNAVAILABLE;
1054
exitcode = EXIT_FAILURE;
1739
1057
gpgme_initialized = true;
1060
if_index = (AvahiIfIndex) if_nametoindex(interface);
1062
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1063
exitcode = EXIT_FAILURE;
1888
1178
if(gpgme_initialized){
1889
1179
gpgme_release(mc.ctx);
1892
/* Cleans up the circular linked list of Mandos servers the client
1894
if(mc.current_server != NULL){
1895
mc.current_server->prev->next = NULL;
1896
while(mc.current_server != NULL){
1897
server *next = mc.current_server->next;
1898
free(mc.current_server);
1899
mc.current_server = next;
1903
/* Take down the network interface */
1904
if(take_down_interface){
1905
/* Re-raise priviliges */
1909
perror_plus("seteuid");
1912
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1914
perror_plus("ioctl SIOCGIFFLAGS");
1915
} else if(network.ifr_flags & IFF_UP) {
1916
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1917
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1919
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1922
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1924
perror_plus("close");
1926
/* Lower privileges permanently */
1930
perror_plus("setuid");
1935
/* Removes the GPGME temp directory and all files inside */
1182
/* Removes the temp directory used by GPGME */
1936
1183
if(tempdir_created){
1937
struct dirent **direntries = NULL;
1938
struct dirent *direntry = NULL;
1939
ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1941
for(int i = 0; i < ret; i++){
1942
direntry = direntries[i];
1185
struct dirent *direntry;
1186
d = opendir(tempdir);
1188
if(errno != ENOENT){
1193
direntry = readdir(d);
1194
if(direntry == NULL){
1197
/* Skip "." and ".." */
1198
if(direntry->d_name[0] == '.'
1199
and (direntry->d_name[1] == '\0'
1200
or (direntry->d_name[1] == '.'
1201
and direntry->d_name[2] == '\0'))){
1943
1204
char *fullname = NULL;
1944
1205
ret = asprintf(&fullname, "%s/%s", tempdir,
1945
1206
direntry->d_name);
1947
perror_plus("asprintf");
1950
1211
ret = remove(fullname);