72
71
INET_ADDRSTRLEN, INET6_ADDRSTRLEN
74
73
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
75
getuid(), getgid(), seteuid(),
77
#include <arpa/inet.h> /* inet_pton(), htons, inet_ntop() */
74
getuid(), getgid(), setuid(),
76
#include <arpa/inet.h> /* inet_pton(), htons */
78
77
#include <iso646.h> /* not, or, and */
79
78
#include <argp.h> /* struct argp_option, error_t, struct
80
79
argp_state, struct argp,
81
80
argp_parse(), ARGP_KEY_ARG,
82
81
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
83
82
#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 */
83
sigaction(), SIGTERM, sigaction,
90
87
#include <sys/klog.h> /* klogctl() */
1082
/* Signal handler that stops main loop after SIGTERM */
1083
static void handle_sigterm(int sig){
880
sig_atomic_t quit_now = 0;
882
/* stop main loop after sigterm has been called */
883
static void handle_sigterm(__attribute__((unused)) int sig){
1088
signal_received = sig;
1089
888
int old_errno = errno;
1090
/* set main loop to exit */
1091
889
if(mc.simple_poll != NULL){
1092
890
avahi_simple_poll_quit(mc.simple_poll);
1094
892
errno = old_errno;
1097
bool get_flags(const char *ifname, struct ifreq *ifr){
1100
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1102
perror_plus("socket");
1105
strcpy(ifr->ifr_name, ifname);
1106
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1109
perror_plus("ioctl SIOCGIFFLAGS");
1116
bool good_flags(const char *ifname, const struct ifreq *ifr){
1118
/* Reject the loopback device */
1119
if(ifr->ifr_flags & IFF_LOOPBACK){
1121
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1126
/* Accept point-to-point devices only if connect_to is specified */
1127
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1129
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1134
/* Otherwise, reject non-broadcast-capable devices */
1135
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1137
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1142
/* Reject non-ARP interfaces (including dummy interfaces) */
1143
if(ifr->ifr_flags & IFF_NOARP){
1145
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1150
/* Accept this device */
1152
fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1158
* This function determines if a directory entry in /sys/class/net
1159
* corresponds to an acceptable network device.
1160
* (This function is passed to scandir(3) as a filter function.)
1162
int good_interface(const struct dirent *if_entry){
1164
if(if_entry->d_name[0] == '.'){
1169
if(not get_flags(if_entry->d_name, &ifr)){
1173
if(not good_flags(if_entry->d_name, &ifr)){
1180
* This function determines if a directory entry in /sys/class/net
1181
* corresponds to an acceptable network device which is up.
1182
* (This function is passed to scandir(3) as a filter function.)
1184
int up_interface(const struct dirent *if_entry){
1186
char *flagname = NULL;
1187
if(if_entry->d_name[0] == '.'){
1190
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1193
perror_plus("asprintf");
1196
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1198
perror_plus("open");
1203
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1204
/* read line from flags_fd */
1205
ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1206
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1207
flagstring[(size_t)to_read] = '\0';
1208
if(flagstring == NULL){
1209
perror_plus("malloc");
1214
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1217
perror_plus("read");
1231
tmpmax = strtoimax(flagstring, &tmp, 0);
1232
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1233
and not (isspace(*tmp)))
1234
or tmpmax != (ifreq_flags)tmpmax){
1236
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1237
flagstring, if_entry->d_name);
1243
ifreq_flags flags = (ifreq_flags)tmpmax;
1244
/* Reject the loopback device */
1245
if(flags & IFF_LOOPBACK){
1247
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1253
/* Reject down interfaces */
1254
if(not (flags & IFF_UP)){
1258
/* Accept point-to-point devices only if connect_to is specified */
1259
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1261
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1266
/* Otherwise, reject non-broadcast-capable devices */
1267
if(not (flags & IFF_BROADCAST)){
1269
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1274
/* Reject non-ARP interfaces (including dummy interfaces) */
1275
if(flags & IFF_NOARP){
1277
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1282
/* Accept this device */
1284
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1290
int notdotentries(const struct dirent *direntry){
1291
/* Skip "." and ".." */
1292
if(direntry->d_name[0] == '.'
1293
and (direntry->d_name[1] == '\0'
1294
or (direntry->d_name[1] == '.'
1295
and direntry->d_name[2] == '\0'))){
1301
/* Is this directory entry a runnable program? */
1302
int runnable_hook(const struct dirent *direntry){
1306
if((direntry->d_name)[0] == '\0'){
1311
/* Save pointer to last character */
1312
char *end = strchr(direntry->d_name, '\0')-1;
1319
if(((direntry->d_name)[0] == '#')
1321
/* Temporary #name# */
1325
/* XXX more rules here */
1327
ret = stat(direntry->d_name, &st);
1330
perror_plus("Could not stat plugin");
1334
if(not (st.st_mode & S_ISREG)){
1335
/* Not a regular file */
1338
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1339
/* Not executable */
1345
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1347
struct timespec now;
1348
struct timespec waited_time;
1349
intmax_t block_time;
1352
if(mc.current_server == NULL){
1355
"Wait until first server is found. No timeout!\n");
1357
ret = avahi_simple_poll_iterate(s, -1);
1360
fprintf(stderr, "Check current_server if we should run it,"
1363
/* the current time */
1364
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1366
perror_plus("clock_gettime");
1369
/* Calculating in ms how long time between now and server
1370
who we visted longest time ago. Now - last seen. */
1371
waited_time.tv_sec = (now.tv_sec
1372
- mc.current_server->last_seen.tv_sec);
1373
waited_time.tv_nsec = (now.tv_nsec
1374
- mc.current_server->last_seen.tv_nsec);
1375
/* total time is 10s/10,000ms.
1376
Converting to s from ms by dividing by 1,000,
1377
and ns to ms by dividing by 1,000,000. */
1378
block_time = ((retry_interval
1379
- ((intmax_t)waited_time.tv_sec * 1000))
1380
- ((intmax_t)waited_time.tv_nsec / 1000000));
1383
fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1386
if(block_time <= 0){
1387
ret = start_mandos_communication(mc.current_server->ip,
1388
mc.current_server->port,
1389
mc.current_server->if_index,
1390
mc.current_server->af);
1392
avahi_simple_poll_quit(mc.simple_poll);
1395
ret = clock_gettime(CLOCK_MONOTONIC,
1396
&mc.current_server->last_seen);
1398
perror_plus("clock_gettime");
1401
mc.current_server = mc.current_server->next;
1402
block_time = 0; /* Call avahi to find new Mandos
1403
servers, but don't block */
1406
ret = avahi_simple_poll_iterate(s, (int)block_time);
1409
if (ret > 0 or errno != EINTR) {
1410
return (ret != 1) ? ret : 0;
1416
895
int main(int argc, char *argv[]){
1417
896
AvahiSServiceBrowser *sb = NULL;
1548
991
delay = strtof(arg, &tmp);
1549
992
if(errno != 0 or tmp == arg or *tmp != '\0'){
1550
argp_error(state, "Bad delay");
1552
case 132: /* --retry */
1554
retry_interval = strtod(arg, &tmp);
1555
if(errno != 0 or tmp == arg or *tmp != '\0'
1556
or (retry_interval * 1000) > INT_MAX
1557
or retry_interval < 0){
1558
argp_error(state, "Bad retry interval");
993
fprintf(stderr, "Bad delay\n");
1562
* These reproduce what we would get without ARGP_NO_HELP
1564
case '?': /* --help */
1565
argp_state_help(state, state->out_stream,
1566
(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1567
& ~(unsigned int)ARGP_HELP_EXIT_OK);
1568
case -3: /* --usage */
1569
argp_state_help(state, state->out_stream,
1570
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1571
case 'V': /* --version */
1572
fprintf(state->out_stream, "%s\n", argp_program_version);
1573
exit(argp_err_exit_status);
1576
1002
return ARGP_ERR_UNKNOWN;
1581
1007
struct argp argp = { .options = options, .parser = parse_opt,
1582
1008
.args_doc = "",
1583
1009
.doc = "Mandos client -- Get and decrypt"
1584
1010
" passwords from a Mandos server" };
1585
ret = argp_parse(&argp, argc, argv,
1586
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1593
perror_plus("argp_parse");
1594
exitcode = EX_OSERR;
1597
exitcode = EX_USAGE;
1603
/* Work around Debian bug #633582:
1604
<http://bugs.debian.org/633582> */
1607
/* Re-raise priviliges */
1611
perror_plus("seteuid");
1614
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1615
int seckey_fd = open(seckey, O_RDONLY);
1616
if(seckey_fd == -1){
1617
perror_plus("open");
1619
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1621
perror_plus("fstat");
1623
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1624
ret = fchown(seckey_fd, uid, gid);
1626
perror_plus("fchown");
1630
TEMP_FAILURE_RETRY(close(seckey_fd));
1634
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1635
int pubkey_fd = open(pubkey, O_RDONLY);
1636
if(pubkey_fd == -1){
1637
perror_plus("open");
1639
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1641
perror_plus("fstat");
1643
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1644
ret = fchown(pubkey_fd, uid, gid);
1646
perror_plus("fchown");
1650
TEMP_FAILURE_RETRY(close(pubkey_fd));
1654
/* Lower privileges */
1658
perror_plus("seteuid");
1662
/* Find network hooks and run them */
1664
struct dirent **direntries;
1665
struct dirent *direntry;
1666
int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
1668
int devnull = open("/dev/null", O_RDONLY);
1669
for(int i = 0; i < numhooks; i++){
1670
direntry = direntries[0];
1671
char *fullname = NULL;
1672
ret = asprintf(&fullname, "%s/%s", tempdir,
1675
perror_plus("asprintf");
1678
pid_t hook_pid = fork();
1681
dup2(devnull, STDIN_FILENO);
1683
dup2(STDERR_FILENO, STDOUT_FILENO);
1684
setenv("DEVICE", interface, 1);
1685
setenv("VERBOSE", debug ? "1" : "0", 1);
1686
setenv("MODE", "start", 1);
1687
/* setenv( XXX more here */
1688
ret = execl(fullname, direntry->d_name, "start");
1689
perror_plus("execl");
1011
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
1012
if(ret == ARGP_ERR_UNKNOWN){
1013
fprintf(stderr, "Unknown error while parsing arguments\n");
1014
exitcode = EXIT_FAILURE;
1700
1020
avahi_set_log_function(empty_log);
1703
if(interface[0] == '\0'){
1704
struct dirent **direntries;
1705
ret = scandir(sys_class_net, &direntries, good_interface,
1708
/* Pick the first good interface */
1709
interface = strdup(direntries[0]->d_name);
1711
fprintf(stderr, "Using interface \"%s\"\n", interface);
1713
if(interface == NULL){
1714
perror_plus("malloc");
1716
exitcode = EXIT_FAILURE;
1722
fprintf(stderr, "Could not find a network interface\n");
1723
exitcode = EXIT_FAILURE;
1728
1023
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1729
1024
from the signal handler */
1730
1025
/* Initialize the pseudo-RNG for Avahi */
1732
1027
mc.simple_poll = avahi_simple_poll_new();
1733
1028
if(mc.simple_poll == NULL){
1734
1029
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1735
exitcode = EX_UNAVAILABLE;
1030
exitcode = EXIT_FAILURE;
1739
1034
sigemptyset(&sigterm_action.sa_mask);
1740
1035
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1742
perror_plus("sigaddset");
1743
exitcode = EX_OSERR;
1037
perror("sigaddset");
1038
exitcode = EXIT_FAILURE;
1746
1041
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1748
perror_plus("sigaddset");
1749
exitcode = EX_OSERR;
1043
perror("sigaddset");
1044
exitcode = EXIT_FAILURE;
1752
1047
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1754
perror_plus("sigaddset");
1755
exitcode = EX_OSERR;
1758
/* Need to check if the handler is SIG_IGN before handling:
1759
| [[info:libc:Initial Signal Actions]] |
1760
| [[info:libc:Basic Signal Handling]] |
1762
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1764
perror_plus("sigaction");
1767
if(old_sigterm_action.sa_handler != SIG_IGN){
1768
ret = sigaction(SIGINT, &sigterm_action, NULL);
1770
perror_plus("sigaction");
1771
exitcode = EX_OSERR;
1775
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1777
perror_plus("sigaction");
1780
if(old_sigterm_action.sa_handler != SIG_IGN){
1781
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1783
perror_plus("sigaction");
1784
exitcode = EX_OSERR;
1788
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1790
perror_plus("sigaction");
1793
if(old_sigterm_action.sa_handler != SIG_IGN){
1794
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1796
perror_plus("sigaction");
1797
exitcode = EX_OSERR;
1049
perror("sigaddset");
1050
exitcode = EXIT_FAILURE;
1053
ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1055
perror("sigaction");
1056
exitcode = EXIT_FAILURE;
1802
1060
/* If the interface is down, bring it up */
1803
if(strcmp(interface, "none") != 0){
1804
if_index = (AvahiIfIndex) if_nametoindex(interface);
1806
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1807
exitcode = EX_UNAVAILABLE;
1815
/* Re-raise priviliges */
1819
perror_plus("seteuid");
1061
if(interface[0] != '\0'){
1822
1062
#ifdef __linux__
1823
1063
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1824
messages about the network interface to mess up the prompt */
1064
messages to mess up the prompt */
1825
1065
ret = klogctl(8, NULL, 5);
1826
1066
bool restore_loglevel = true;
1828
1068
restore_loglevel = false;
1829
perror_plus("klogctl");
1831
1071
#endif /* __linux__ */
1833
1073
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1835
perror_plus("socket");
1836
exitcode = EX_OSERR;
1076
exitcode = EXIT_FAILURE;
1837
1077
#ifdef __linux__
1838
1078
if(restore_loglevel){
1839
1079
ret = klogctl(7, NULL, 0);
1841
perror_plus("klogctl");
1844
1084
#endif /* __linux__ */
1845
/* Lower privileges */
1849
perror_plus("seteuid");
1853
1087
strcpy(network.ifr_name, interface);
1854
1088
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1856
perror_plus("ioctl SIOCGIFFLAGS");
1090
perror("ioctl SIOCGIFFLAGS");
1857
1091
#ifdef __linux__
1858
1092
if(restore_loglevel){
1859
1093
ret = klogctl(7, NULL, 0);
1861
perror_plus("klogctl");
1864
1098
#endif /* __linux__ */
1865
exitcode = EX_OSERR;
1866
/* Lower privileges */
1870
perror_plus("seteuid");
1099
exitcode = EXIT_FAILURE;
1874
1102
if((network.ifr_flags & IFF_UP) == 0){
1875
1103
network.ifr_flags |= IFF_UP;
1876
take_down_interface = true;
1877
1104
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1879
take_down_interface = false;
1880
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1881
exitcode = EX_OSERR;
1106
perror("ioctl SIOCSIFFLAGS");
1107
exitcode = EXIT_FAILURE;
1882
1108
#ifdef __linux__
1883
1109
if(restore_loglevel){
1884
1110
ret = klogctl(7, NULL, 0);
1886
perror_plus("klogctl");
1889
1115
#endif /* __linux__ */
1890
/* Lower privileges */
1894
perror_plus("seteuid");
1899
/* Sleep checking until interface is running.
1900
Check every 0.25s, up to total time of delay */
1119
/* sleep checking until interface is running */
1901
1120
for(int i=0; i < delay * 4; i++){
1902
1121
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1904
perror_plus("ioctl SIOCGIFFLAGS");
1123
perror("ioctl SIOCGIFFLAGS");
1905
1124
} else if(network.ifr_flags & IFF_RUNNING){
1908
1127
struct timespec sleeptime = { .tv_nsec = 250000000 };
1909
1128
ret = nanosleep(&sleeptime, NULL);
1910
1129
if(ret == -1 and errno != EINTR){
1911
perror_plus("nanosleep");
1130
perror("nanosleep");
1914
if(not take_down_interface){
1915
/* We won't need the socket anymore */
1916
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1918
perror_plus("close");
1133
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1921
1137
#ifdef __linux__
1922
1138
if(restore_loglevel){
1923
1139
/* Restores kernel loglevel to default */
1924
1140
ret = klogctl(7, NULL, 0);
1926
perror_plus("klogctl");
1929
1145
#endif /* __linux__ */
1930
/* Lower privileges */
1932
if(take_down_interface){
1933
/* Lower privileges */
1936
perror_plus("seteuid");
1939
/* Lower privileges permanently */
1942
perror_plus("setuid");
1951
1162
ret = init_gnutls_global(pubkey, seckey);
1953
1164
fprintf(stderr, "init_gnutls_global failed\n");
1954
exitcode = EX_UNAVAILABLE;
1165
exitcode = EXIT_FAILURE;
1957
1168
gnutls_initialized = true;
1964
1171
if(mkdtemp(tempdir) == NULL){
1965
perror_plus("mkdtemp");
1968
1175
tempdir_created = true;
1974
1177
if(not init_gpgme(pubkey, seckey, tempdir)){
1975
1178
fprintf(stderr, "init_gpgme failed\n");
1976
exitcode = EX_UNAVAILABLE;
1179
exitcode = EXIT_FAILURE;
1979
1182
gpgme_initialized = true;
1185
if(interface[0] != '\0'){
1186
if_index = (AvahiIfIndex) if_nametoindex(interface);
1188
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1189
exitcode = EXIT_FAILURE;
1986
1194
if(connect_to != NULL){
2138
1298
if(gpgme_initialized){
2139
1299
gpgme_release(mc.ctx);
2142
/* Cleans up the circular linked list of Mandos servers the client
2144
if(mc.current_server != NULL){
2145
mc.current_server->prev->next = NULL;
2146
while(mc.current_server != NULL){
2147
server *next = mc.current_server->next;
2148
free(mc.current_server);
2149
mc.current_server = next;
2153
/* XXX run network hooks "stop" here */
2155
/* Take down the network interface */
2156
if(take_down_interface){
2157
/* Re-raise priviliges */
2161
perror_plus("seteuid");
2164
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2166
perror_plus("ioctl SIOCGIFFLAGS");
2167
} else if(network.ifr_flags & IFF_UP) {
2168
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2169
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2171
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2174
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2176
perror_plus("close");
2178
/* Lower privileges permanently */
2182
perror_plus("setuid");
2187
/* Removes the GPGME temp directory and all files inside */
1302
/* Removes the temp directory used by GPGME */
2188
1303
if(tempdir_created){
2189
struct dirent **direntries = NULL;
2190
struct dirent *direntry = NULL;
2191
int numentries = scandir(tempdir, &direntries, notdotentries,
2193
if (numentries > 0){
2194
for(int i = 0; i < numentries; i++){
2195
direntry = direntries[i];
1305
struct dirent *direntry;
1306
d = opendir(tempdir);
1308
if(errno != ENOENT){
1313
direntry = readdir(d);
1314
if(direntry == NULL){
1317
/* Skip "." and ".." */
1318
if(direntry->d_name[0] == '.'
1319
and (direntry->d_name[1] == '\0'
1320
or (direntry->d_name[1] == '.'
1321
and direntry->d_name[2] == '\0'))){
2196
1324
char *fullname = NULL;
2197
1325
ret = asprintf(&fullname, "%s/%s", tempdir,
2198
1326
direntry->d_name);
2200
perror_plus("asprintf");
2203
1331
ret = remove(fullname);