71
72
INET_ADDRSTRLEN, INET6_ADDRSTRLEN
73
74
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
74
getuid(), getgid(), setuid(),
76
#include <arpa/inet.h> /* inet_pton(), htons */
75
getuid(), getgid(), seteuid(),
77
#include <arpa/inet.h> /* inet_pton(), htons, inet_ntop() */
77
78
#include <iso646.h> /* not, or, and */
78
79
#include <argp.h> /* struct argp_option, error_t, struct
79
80
argp_state, struct argp,
80
81
argp_parse(), ARGP_KEY_ARG,
81
82
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
83
#include <signal.h> /* sigemptyset(), sigaddset(),
83
sigaction(), SIGTERM, sigaction,
84
sigaction(), SIGTERM, sig_atomic_t,
86
#include <sysexits.h> /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
87
EX_NOHOST, EX_IOERR, EX_PROTOCOL */
87
90
#include <sys/klog.h> /* klogctl() */
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){
1074
/* Signal handler that stops main loop after SIGTERM */
1075
static void handle_sigterm(int sig){
1080
signal_received = sig;
888
1081
int old_errno = errno;
1082
/* set main loop to exit */
889
1083
if(mc.simple_poll != NULL){
890
1084
avahi_simple_poll_quit(mc.simple_poll);
892
1086
errno = old_errno;
1089
bool get_flags(const char *ifname, struct ifreq *ifr){
1092
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1094
perror_plus("socket");
1097
strcpy(ifr->ifr_name, ifname);
1098
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1101
perror_plus("ioctl SIOCGIFFLAGS");
1108
bool good_flags(const char *ifname, const struct ifreq *ifr){
1110
/* Reject the loopback device */
1111
if(ifr->ifr_flags & IFF_LOOPBACK){
1113
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1118
/* Accept point-to-point devices only if connect_to is specified */
1119
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1121
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1126
/* Otherwise, reject non-broadcast-capable devices */
1127
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1129
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1134
/* Reject non-ARP interfaces (including dummy interfaces) */
1135
if(ifr->ifr_flags & IFF_NOARP){
1137
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1142
/* Accept this device */
1144
fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1150
* This function determines if a directory entry in /sys/class/net
1151
* corresponds to an acceptable network device.
1152
* (This function is passed to scandir(3) as a filter function.)
1154
int good_interface(const struct dirent *if_entry){
1156
if(if_entry->d_name[0] == '.'){
1161
if(not get_flags(if_entry->d_name, &ifr)){
1165
if(not good_flags(if_entry->d_name, &ifr)){
1172
* This function determines if a directory entry in /sys/class/net
1173
* corresponds to an acceptable network device which is up.
1174
* (This function is passed to scandir(3) as a filter function.)
1176
int up_interface(const struct dirent *if_entry){
1178
char *flagname = NULL;
1179
if(if_entry->d_name[0] == '.'){
1182
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1185
perror_plus("asprintf");
1188
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1190
perror_plus("open");
1195
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1196
/* read line from flags_fd */
1197
ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1198
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1199
flagstring[(size_t)to_read] = '\0';
1200
if(flagstring == NULL){
1201
perror_plus("malloc");
1206
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1209
perror_plus("read");
1223
tmpmax = strtoimax(flagstring, &tmp, 0);
1224
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1225
and not (isspace(*tmp)))
1226
or tmpmax != (ifreq_flags)tmpmax){
1228
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1229
flagstring, if_entry->d_name);
1235
ifreq_flags flags = (ifreq_flags)tmpmax;
1236
/* Reject the loopback device */
1237
if(flags & IFF_LOOPBACK){
1239
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1245
/* Reject down interfaces */
1246
if(not (flags & IFF_UP)){
1250
/* Accept point-to-point devices only if connect_to is specified */
1251
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1253
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1258
/* Otherwise, reject non-broadcast-capable devices */
1259
if(not (flags & IFF_BROADCAST)){
1261
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1266
/* Reject non-ARP interfaces (including dummy interfaces) */
1267
if(flags & IFF_NOARP){
1269
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1274
/* Accept this device */
1276
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1282
int notdotentries(const struct dirent *direntry){
1283
/* Skip "." and ".." */
1284
if(direntry->d_name[0] == '.'
1285
and (direntry->d_name[1] == '\0'
1286
or (direntry->d_name[1] == '.'
1287
and direntry->d_name[2] == '\0'))){
1293
/* Is this directory entry a runnable program? */
1294
int runnable_hook(const struct dirent *direntry){
1298
if((direntry->d_name)[0] == '\0'){
1303
/* Save pointer to last character */
1304
char *end = strchr(direntry->d_name, '\0')-1;
1311
if(((direntry->d_name)[0] == '#')
1313
/* Temporary #name# */
1317
/* XXX more rules here */
1319
ret = stat(direntry->d_name, &st);
1322
perror_plus("Could not stat plugin");
1326
if(not (st.st_mode & S_ISREG)){
1327
/* Not a regular file */
1330
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1331
/* Not executable */
1337
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1339
struct timespec now;
1340
struct timespec waited_time;
1341
intmax_t block_time;
1344
if(mc.current_server == NULL){
1347
"Wait until first server is found. No timeout!\n");
1349
ret = avahi_simple_poll_iterate(s, -1);
1352
fprintf(stderr, "Check current_server if we should run it,"
1355
/* the current time */
1356
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1358
perror_plus("clock_gettime");
1361
/* Calculating in ms how long time between now and server
1362
who we visted longest time ago. Now - last seen. */
1363
waited_time.tv_sec = (now.tv_sec
1364
- mc.current_server->last_seen.tv_sec);
1365
waited_time.tv_nsec = (now.tv_nsec
1366
- mc.current_server->last_seen.tv_nsec);
1367
/* total time is 10s/10,000ms.
1368
Converting to s from ms by dividing by 1,000,
1369
and ns to ms by dividing by 1,000,000. */
1370
block_time = ((retry_interval
1371
- ((intmax_t)waited_time.tv_sec * 1000))
1372
- ((intmax_t)waited_time.tv_nsec / 1000000));
1375
fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1378
if(block_time <= 0){
1379
ret = start_mandos_communication(mc.current_server->ip,
1380
mc.current_server->port,
1381
mc.current_server->if_index,
1382
mc.current_server->af);
1384
avahi_simple_poll_quit(mc.simple_poll);
1387
ret = clock_gettime(CLOCK_MONOTONIC,
1388
&mc.current_server->last_seen);
1390
perror_plus("clock_gettime");
1393
mc.current_server = mc.current_server->next;
1394
block_time = 0; /* Call avahi to find new Mandos
1395
servers, but don't block */
1398
ret = avahi_simple_poll_iterate(s, (int)block_time);
1401
if (ret > 0 or errno != EINTR) {
1402
return (ret != 1) ? ret : 0;
895
1408
int main(int argc, char *argv[]){
896
1409
AvahiSServiceBrowser *sb = NULL;
991
1540
delay = strtof(arg, &tmp);
992
1541
if(errno != 0 or tmp == arg or *tmp != '\0'){
993
fprintf(stderr, "Bad delay\n");
1542
argp_error(state, "Bad delay");
1544
case 132: /* --retry */
1546
retry_interval = strtod(arg, &tmp);
1547
if(errno != 0 or tmp == arg or *tmp != '\0'
1548
or (retry_interval * 1000) > INT_MAX
1549
or retry_interval < 0){
1550
argp_error(state, "Bad retry interval");
1554
* These reproduce what we would get without ARGP_NO_HELP
1556
case '?': /* --help */
1557
argp_state_help(state, state->out_stream,
1558
(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1559
& ~(unsigned int)ARGP_HELP_EXIT_OK);
1560
case -3: /* --usage */
1561
argp_state_help(state, state->out_stream,
1562
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1563
case 'V': /* --version */
1564
fprintf(state->out_stream, "%s\n", argp_program_version);
1565
exit(argp_err_exit_status);
1002
1568
return ARGP_ERR_UNKNOWN;
1007
1573
struct argp argp = { .options = options, .parser = parse_opt,
1008
1574
.args_doc = "",
1009
1575
.doc = "Mandos client -- Get and decrypt"
1010
1576
" passwords from a Mandos server" };
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;
1577
ret = argp_parse(&argp, argc, argv,
1578
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1585
perror_plus("argp_parse");
1586
exitcode = EX_OSERR;
1589
exitcode = EX_USAGE;
1595
/* Work around Debian bug #633582:
1596
<http://bugs.debian.org/633582> */
1599
/* Re-raise priviliges */
1603
perror_plus("seteuid");
1606
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1607
int seckey_fd = open(seckey, O_RDONLY);
1608
if(seckey_fd == -1){
1609
perror_plus("open");
1611
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1613
perror_plus("fstat");
1615
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1616
ret = fchown(seckey_fd, uid, gid);
1618
perror_plus("fchown");
1622
TEMP_FAILURE_RETRY(close(seckey_fd));
1626
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1627
int pubkey_fd = open(pubkey, O_RDONLY);
1628
if(pubkey_fd == -1){
1629
perror_plus("open");
1631
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1633
perror_plus("fstat");
1635
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1636
ret = fchown(pubkey_fd, uid, gid);
1638
perror_plus("fchown");
1642
TEMP_FAILURE_RETRY(close(pubkey_fd));
1646
/* Lower privileges */
1650
perror_plus("seteuid");
1654
/* Find network hooks and run them */
1656
struct dirent **direntries;
1657
struct dirent *direntry;
1658
int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
1660
int devnull = open("/dev/null", O_RDONLY);
1661
for(int i = 0; i < numhooks; i++){
1662
direntry = direntries[0];
1663
char *fullname = NULL;
1664
ret = asprintf(&fullname, "%s/%s", tempdir,
1667
perror_plus("asprintf");
1670
pid_t hook_pid = fork();
1673
dup2(devnull, STDIN_FILENO);
1675
dup2(STDERR_FILENO, STDOUT_FILENO);
1676
setenv("DEVICE", interface, 1);
1677
setenv("VERBOSE", debug ? "1" : "0", 1);
1678
setenv("MODE", "start", 1);
1679
/* setenv( XXX more here */
1680
ret = execl(fullname, direntry->d_name, "start");
1681
perror_plus("execl");
1020
1692
avahi_set_log_function(empty_log);
1695
if(interface[0] == '\0'){
1696
struct dirent **direntries;
1697
ret = scandir(sys_class_net, &direntries, good_interface,
1700
/* Pick the first good interface */
1701
interface = strdup(direntries[0]->d_name);
1703
fprintf(stderr, "Using interface \"%s\"\n", interface);
1705
if(interface == NULL){
1706
perror_plus("malloc");
1708
exitcode = EXIT_FAILURE;
1714
fprintf(stderr, "Could not find a network interface\n");
1715
exitcode = EXIT_FAILURE;
1023
1720
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1024
1721
from the signal handler */
1025
1722
/* Initialize the pseudo-RNG for Avahi */
1027
1724
mc.simple_poll = avahi_simple_poll_new();
1028
1725
if(mc.simple_poll == NULL){
1029
1726
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1030
exitcode = EXIT_FAILURE;
1727
exitcode = EX_UNAVAILABLE;
1034
1731
sigemptyset(&sigterm_action.sa_mask);
1035
1732
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1037
perror("sigaddset");
1038
exitcode = EXIT_FAILURE;
1734
perror_plus("sigaddset");
1735
exitcode = EX_OSERR;
1041
1738
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1043
perror("sigaddset");
1044
exitcode = EXIT_FAILURE;
1740
perror_plus("sigaddset");
1741
exitcode = EX_OSERR;
1047
1744
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1049
perror("sigaddset");
1050
exitcode = EXIT_FAILURE;
1053
ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1055
perror("sigaction");
1056
exitcode = EXIT_FAILURE;
1746
perror_plus("sigaddset");
1747
exitcode = EX_OSERR;
1750
/* Need to check if the handler is SIG_IGN before handling:
1751
| [[info:libc:Initial Signal Actions]] |
1752
| [[info:libc:Basic Signal Handling]] |
1754
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1756
perror_plus("sigaction");
1759
if(old_sigterm_action.sa_handler != SIG_IGN){
1760
ret = sigaction(SIGINT, &sigterm_action, NULL);
1762
perror_plus("sigaction");
1763
exitcode = EX_OSERR;
1767
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1769
perror_plus("sigaction");
1772
if(old_sigterm_action.sa_handler != SIG_IGN){
1773
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1775
perror_plus("sigaction");
1776
exitcode = EX_OSERR;
1780
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1782
perror_plus("sigaction");
1785
if(old_sigterm_action.sa_handler != SIG_IGN){
1786
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1788
perror_plus("sigaction");
1789
exitcode = EX_OSERR;
1060
1794
/* If the interface is down, bring it up */
1061
if(interface[0] != '\0'){
1795
if(strcmp(interface, "none") != 0){
1796
if_index = (AvahiIfIndex) if_nametoindex(interface);
1798
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1799
exitcode = EX_UNAVAILABLE;
1807
/* Re-raise priviliges */
1811
perror_plus("seteuid");
1062
1814
#ifdef __linux__
1063
1815
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1064
messages to mess up the prompt */
1816
messages about the network interface to mess up the prompt */
1065
1817
ret = klogctl(8, NULL, 5);
1066
1818
bool restore_loglevel = true;
1068
1820
restore_loglevel = false;
1821
perror_plus("klogctl");
1071
1823
#endif /* __linux__ */
1073
1825
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1076
exitcode = EXIT_FAILURE;
1827
perror_plus("socket");
1828
exitcode = EX_OSERR;
1077
1829
#ifdef __linux__
1078
1830
if(restore_loglevel){
1079
1831
ret = klogctl(7, NULL, 0);
1833
perror_plus("klogctl");
1084
1836
#endif /* __linux__ */
1837
/* Lower privileges */
1841
perror_plus("seteuid");
1087
1845
strcpy(network.ifr_name, interface);
1088
1846
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1090
perror("ioctl SIOCGIFFLAGS");
1848
perror_plus("ioctl SIOCGIFFLAGS");
1091
1849
#ifdef __linux__
1092
1850
if(restore_loglevel){
1093
1851
ret = klogctl(7, NULL, 0);
1853
perror_plus("klogctl");
1098
1856
#endif /* __linux__ */
1099
exitcode = EXIT_FAILURE;
1857
exitcode = EX_OSERR;
1858
/* Lower privileges */
1862
perror_plus("seteuid");
1102
1866
if((network.ifr_flags & IFF_UP) == 0){
1103
1867
network.ifr_flags |= IFF_UP;
1868
take_down_interface = true;
1104
1869
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1106
perror("ioctl SIOCSIFFLAGS");
1107
exitcode = EXIT_FAILURE;
1871
take_down_interface = false;
1872
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1873
exitcode = EX_OSERR;
1108
1874
#ifdef __linux__
1109
1875
if(restore_loglevel){
1110
1876
ret = klogctl(7, NULL, 0);
1878
perror_plus("klogctl");
1115
1881
#endif /* __linux__ */
1882
/* Lower privileges */
1886
perror_plus("seteuid");
1119
/* sleep checking until interface is running */
1891
/* Sleep checking until interface is running.
1892
Check every 0.25s, up to total time of delay */
1120
1893
for(int i=0; i < delay * 4; i++){
1121
1894
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1123
perror("ioctl SIOCGIFFLAGS");
1896
perror_plus("ioctl SIOCGIFFLAGS");
1124
1897
} else if(network.ifr_flags & IFF_RUNNING){
1127
1900
struct timespec sleeptime = { .tv_nsec = 250000000 };
1128
1901
ret = nanosleep(&sleeptime, NULL);
1129
1902
if(ret == -1 and errno != EINTR){
1130
perror("nanosleep");
1903
perror_plus("nanosleep");
1133
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1906
if(not take_down_interface){
1907
/* We won't need the socket anymore */
1908
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1910
perror_plus("close");
1137
1913
#ifdef __linux__
1138
1914
if(restore_loglevel){
1139
1915
/* Restores kernel loglevel to default */
1140
1916
ret = klogctl(7, NULL, 0);
1918
perror_plus("klogctl");
1145
1921
#endif /* __linux__ */
1922
/* Lower privileges */
1924
if(take_down_interface){
1925
/* Lower privileges */
1928
perror_plus("seteuid");
1931
/* Lower privileges permanently */
1934
perror_plus("setuid");
1162
1943
ret = init_gnutls_global(pubkey, seckey);
1164
1945
fprintf(stderr, "init_gnutls_global failed\n");
1165
exitcode = EXIT_FAILURE;
1946
exitcode = EX_UNAVAILABLE;
1168
1949
gnutls_initialized = true;
1171
1956
if(mkdtemp(tempdir) == NULL){
1957
perror_plus("mkdtemp");
1175
1960
tempdir_created = true;
1177
1966
if(not init_gpgme(pubkey, seckey, tempdir)){
1178
1967
fprintf(stderr, "init_gpgme failed\n");
1179
exitcode = EXIT_FAILURE;
1968
exitcode = EX_UNAVAILABLE;
1182
1971
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;
1194
1978
if(connect_to != NULL){
1298
2130
if(gpgme_initialized){
1299
2131
gpgme_release(mc.ctx);
1302
/* Removes the temp directory used by GPGME */
2134
/* Cleans up the circular linked list of Mandos servers the client
2136
if(mc.current_server != NULL){
2137
mc.current_server->prev->next = NULL;
2138
while(mc.current_server != NULL){
2139
server *next = mc.current_server->next;
2140
free(mc.current_server);
2141
mc.current_server = next;
2145
/* XXX run network hooks "stop" here */
2147
/* Take down the network interface */
2148
if(take_down_interface){
2149
/* Re-raise priviliges */
2153
perror_plus("seteuid");
2156
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2158
perror_plus("ioctl SIOCGIFFLAGS");
2159
} else if(network.ifr_flags & IFF_UP) {
2160
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2161
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2163
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2166
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2168
perror_plus("close");
2170
/* Lower privileges permanently */
2174
perror_plus("setuid");
2179
/* Removes the GPGME temp directory and all files inside */
1303
2180
if(tempdir_created){
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'))){
2181
struct dirent **direntries = NULL;
2182
struct dirent *direntry = NULL;
2183
int numentries = scandir(tempdir, &direntries, notdotentries,
2185
if (numentries > 0){
2186
for(int i = 0; i < numentries; i++){
2187
direntry = direntries[i];
1324
2188
char *fullname = NULL;
1325
2189
ret = asprintf(&fullname, "%s/%s", tempdir,
1326
2190
direntry->d_name);
2192
perror_plus("asprintf");
1331
2195
ret = remove(fullname);