1074
/* Signal handler that stops main loop after SIGTERM */
1075
static void handle_sigterm(int sig){
1080
signal_received = sig;
1081
int old_errno = errno;
1082
/* set main loop to exit */
1083
if(mc.simple_poll != NULL){
1084
avahi_simple_poll_quit(mc.simple_poll);
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){
1155
if(if_entry->d_name[0] == '.'){
1160
if(not get_flags(if_entry->d_name, &ifr)){
1162
fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
1168
if(not good_flags(if_entry->d_name, &ifr)){
1175
* This function determines if a directory entry in /sys/class/net
1176
* corresponds to an acceptable network device which is up.
1177
* (This function is passed to scandir(3) as a filter function.)
1179
int up_interface(const struct dirent *if_entry){
1180
if(if_entry->d_name[0] == '.'){
1185
if(not get_flags(if_entry->d_name, &ifr)){
1187
fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
1193
/* Reject down interfaces */
1194
if(not (ifr.ifr_flags & IFF_UP)){
1196
fprintf(stderr, "Rejecting down interface \"%s\"\n",
1202
/* Reject non-running interfaces */
1203
if(not (ifr.ifr_flags & IFF_RUNNING)){
1205
fprintf(stderr, "Rejecting non-running interface \"%s\"\n",
1211
if(not good_flags(if_entry->d_name, &ifr)){
1217
int notdotentries(const struct dirent *direntry){
1218
/* Skip "." and ".." */
1219
if(direntry->d_name[0] == '.'
1220
and (direntry->d_name[1] == '\0'
1221
or (direntry->d_name[1] == '.'
1222
and direntry->d_name[2] == '\0'))){
1228
/* Is this directory entry a runnable program? */
1229
int runnable_hook(const struct dirent *direntry){
1233
if((direntry->d_name)[0] == '\0'){
1238
/* Save pointer to last character */
1239
char *end = strchr(direntry->d_name, '\0')-1;
1246
if(((direntry->d_name)[0] == '#')
1248
/* Temporary #name# */
1252
/* XXX more rules here */
1254
ret = stat(direntry->d_name, &st);
1257
perror_plus("Could not stat plugin");
1261
if(not (S_ISREG(st.st_mode))){
1262
/* Not a regular file */
1265
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1266
/* Not executable */
1272
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1274
struct timespec now;
1275
struct timespec waited_time;
1276
intmax_t block_time;
1279
if(mc.current_server == NULL){
1282
"Wait until first server is found. No timeout!\n");
1284
ret = avahi_simple_poll_iterate(s, -1);
1287
fprintf(stderr, "Check current_server if we should run it,"
1290
/* the current time */
1291
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1293
perror_plus("clock_gettime");
1296
/* Calculating in ms how long time between now and server
1297
who we visted longest time ago. Now - last seen. */
1298
waited_time.tv_sec = (now.tv_sec
1299
- mc.current_server->last_seen.tv_sec);
1300
waited_time.tv_nsec = (now.tv_nsec
1301
- mc.current_server->last_seen.tv_nsec);
1302
/* total time is 10s/10,000ms.
1303
Converting to s from ms by dividing by 1,000,
1304
and ns to ms by dividing by 1,000,000. */
1305
block_time = ((retry_interval
1306
- ((intmax_t)waited_time.tv_sec * 1000))
1307
- ((intmax_t)waited_time.tv_nsec / 1000000));
1310
fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1313
if(block_time <= 0){
1314
ret = start_mandos_communication(mc.current_server->ip,
1315
mc.current_server->port,
1316
mc.current_server->if_index,
1317
mc.current_server->af);
1319
avahi_simple_poll_quit(mc.simple_poll);
1322
ret = clock_gettime(CLOCK_MONOTONIC,
1323
&mc.current_server->last_seen);
1325
perror_plus("clock_gettime");
1328
mc.current_server = mc.current_server->next;
1329
block_time = 0; /* Call avahi to find new Mandos
1330
servers, but don't block */
1333
ret = avahi_simple_poll_iterate(s, (int)block_time);
1336
if (ret > 0 or errno != EINTR) {
1337
return (ret != 1) ? ret : 0;
1343
814
int main(int argc, char *argv[]){
1344
AvahiSServiceBrowser *sb = NULL;
1349
int exitcode = EXIT_SUCCESS;
1350
const char *interface = "";
1351
struct ifreq network;
1353
bool take_down_interface = false;
1356
char tempdir[] = "/tmp/mandosXXXXXX";
1357
bool tempdir_created = false;
1358
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1359
const char *seckey = PATHDIR "/" SECKEY;
1360
const char *pubkey = PATHDIR "/" PUBKEY;
1362
bool gnutls_initialized = false;
1363
bool gpgme_initialized = false;
1365
double retry_interval = 10; /* 10s between trying a server and
1366
retrying the same server again */
1368
struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1369
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1374
/* Lower any group privileges we might have, just to be safe */
1378
perror_plus("setgid");
1381
/* Lower user privileges (temporarily) */
1385
perror_plus("seteuid");
1393
struct argp_option options[] = {
1394
{ .name = "debug", .key = 128,
1395
.doc = "Debug mode", .group = 3 },
1396
{ .name = "connect", .key = 'c',
1397
.arg = "ADDRESS:PORT",
1398
.doc = "Connect directly to a specific Mandos server",
1400
{ .name = "interface", .key = 'i',
1402
.doc = "Network interface that will be used to search for"
1405
{ .name = "seckey", .key = 's',
1407
.doc = "OpenPGP secret key file base name",
1409
{ .name = "pubkey", .key = 'p',
1411
.doc = "OpenPGP public key file base name",
1413
{ .name = "dh-bits", .key = 129,
1415
.doc = "Bit length of the prime number used in the"
1416
" Diffie-Hellman key exchange",
1418
{ .name = "priority", .key = 130,
1420
.doc = "GnuTLS priority string for the TLS handshake",
1422
{ .name = "delay", .key = 131,
1424
.doc = "Maximum delay to wait for interface startup",
1426
{ .name = "retry", .key = 132,
1428
.doc = "Retry interval used when denied by the mandos server",
1431
* These reproduce what we would get without ARGP_NO_HELP
1433
{ .name = "help", .key = '?',
1434
.doc = "Give this help list", .group = -1 },
1435
{ .name = "usage", .key = -3,
1436
.doc = "Give a short usage message", .group = -1 },
1437
{ .name = "version", .key = 'V',
1438
.doc = "Print program version", .group = -1 },
1442
error_t parse_opt(int key, char *arg,
1443
struct argp_state *state){
1446
case 128: /* --debug */
1449
case 'c': /* --connect */
1452
case 'i': /* --interface */
1455
case 's': /* --seckey */
1458
case 'p': /* --pubkey */
1461
case 129: /* --dh-bits */
1463
tmpmax = strtoimax(arg, &tmp, 10);
1464
if(errno != 0 or tmp == arg or *tmp != '\0'
1465
or tmpmax != (typeof(mc.dh_bits))tmpmax){
1466
argp_error(state, "Bad number of DH bits");
1468
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1470
case 130: /* --priority */
1473
case 131: /* --delay */
1475
delay = strtof(arg, &tmp);
1476
if(errno != 0 or tmp == arg or *tmp != '\0'){
1477
argp_error(state, "Bad delay");
1479
case 132: /* --retry */
1481
retry_interval = strtod(arg, &tmp);
1482
if(errno != 0 or tmp == arg or *tmp != '\0'
1483
or (retry_interval * 1000) > INT_MAX
1484
or retry_interval < 0){
1485
argp_error(state, "Bad retry interval");
1489
* These reproduce what we would get without ARGP_NO_HELP
1491
case '?': /* --help */
1492
argp_state_help(state, state->out_stream,
1493
(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1494
& ~(unsigned int)ARGP_HELP_EXIT_OK);
1495
case -3: /* --usage */
1496
argp_state_help(state, state->out_stream,
1497
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1498
case 'V': /* --version */
1499
fprintf(state->out_stream, "%s\n", argp_program_version);
1500
exit(argp_err_exit_status);
1503
return ARGP_ERR_UNKNOWN;
1508
struct argp argp = { .options = options, .parser = parse_opt,
1510
.doc = "Mandos client -- Get and decrypt"
1511
" passwords from a Mandos server" };
1512
ret = argp_parse(&argp, argc, argv,
1513
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1520
perror_plus("argp_parse");
1521
exitcode = EX_OSERR;
1524
exitcode = EX_USAGE;
1530
/* Work around Debian bug #633582:
1531
<http://bugs.debian.org/633582> */
1534
/* Re-raise priviliges */
1538
perror_plus("seteuid");
1541
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1542
int seckey_fd = open(seckey, O_RDONLY);
1543
if(seckey_fd == -1){
1544
perror_plus("open");
1546
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1548
perror_plus("fstat");
1550
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1551
ret = fchown(seckey_fd, uid, gid);
1553
perror_plus("fchown");
1557
TEMP_FAILURE_RETRY(close(seckey_fd));
1561
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1562
int pubkey_fd = open(pubkey, O_RDONLY);
1563
if(pubkey_fd == -1){
1564
perror_plus("open");
1566
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1568
perror_plus("fstat");
1570
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1571
ret = fchown(pubkey_fd, uid, gid);
1573
perror_plus("fchown");
1577
TEMP_FAILURE_RETRY(close(pubkey_fd));
1581
/* Lower privileges */
1585
perror_plus("seteuid");
1589
/* Find network hooks and run them */
1591
struct dirent **direntries;
1592
struct dirent *direntry;
1593
int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
1595
int devnull = open("/dev/null", O_RDONLY);
1596
for(int i = 0; i < numhooks; i++){
1597
direntry = direntries[0];
1598
char *fullname = NULL;
1599
ret = asprintf(&fullname, "%s/%s", tempdir,
1602
perror_plus("asprintf");
1605
pid_t hook_pid = fork();
1608
dup2(devnull, STDIN_FILENO);
1610
dup2(STDERR_FILENO, STDOUT_FILENO);
1611
ret = setenv("DEVICE", interface, 1);
1613
perror_plus("setenv");
1616
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1618
perror_plus("setenv");
1621
ret = setenv("MODE", "start", 1);
1623
perror_plus("setenv");
1627
ret = asprintf(&delaystring, "%f", delay);
1629
perror_plus("asprintf");
1632
ret = setenv("DELAY", delaystring, 1);
1635
perror_plus("setenv");
1639
ret = execl(fullname, direntry->d_name, "start", NULL);
1640
perror_plus("execl");
1651
avahi_set_log_function(empty_log);
1654
if(interface[0] == '\0'){
1655
struct dirent **direntries;
1656
/* First look for interfaces that are up */
1657
ret = scandir(sys_class_net, &direntries, up_interface,
1660
/* No up interfaces, look for any good interfaces */
1662
ret = scandir(sys_class_net, &direntries, good_interface,
1666
/* Pick the first interface returned */
1667
interface = strdup(direntries[0]->d_name);
1669
fprintf(stderr, "Using interface \"%s\"\n", interface);
1671
if(interface == NULL){
1672
perror_plus("malloc");
1674
exitcode = EXIT_FAILURE;
1680
fprintf(stderr, "Could not find a network interface\n");
1681
exitcode = EXIT_FAILURE;
1686
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1687
from the signal handler */
1688
/* Initialize the pseudo-RNG for Avahi */
1689
srand((unsigned int) time(NULL));
1690
mc.simple_poll = avahi_simple_poll_new();
1691
if(mc.simple_poll == NULL){
1692
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1693
exitcode = EX_UNAVAILABLE;
1697
sigemptyset(&sigterm_action.sa_mask);
1698
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1700
perror_plus("sigaddset");
1701
exitcode = EX_OSERR;
1704
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1706
perror_plus("sigaddset");
1707
exitcode = EX_OSERR;
1710
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1712
perror_plus("sigaddset");
1713
exitcode = EX_OSERR;
1716
/* Need to check if the handler is SIG_IGN before handling:
1717
| [[info:libc:Initial Signal Actions]] |
1718
| [[info:libc:Basic Signal Handling]] |
1720
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1722
perror_plus("sigaction");
1725
if(old_sigterm_action.sa_handler != SIG_IGN){
1726
ret = sigaction(SIGINT, &sigterm_action, NULL);
1728
perror_plus("sigaction");
1729
exitcode = EX_OSERR;
1733
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1735
perror_plus("sigaction");
1738
if(old_sigterm_action.sa_handler != SIG_IGN){
1739
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1741
perror_plus("sigaction");
1742
exitcode = EX_OSERR;
1746
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1748
perror_plus("sigaction");
1751
if(old_sigterm_action.sa_handler != SIG_IGN){
1752
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1754
perror_plus("sigaction");
1755
exitcode = EX_OSERR;
1760
/* If the interface is down, bring it up */
1761
if(strcmp(interface, "none") != 0){
815
AvahiSServiceBrowser *sb = NULL;
818
int exitcode = EXIT_SUCCESS;
819
const char *interface = "eth0";
820
struct ifreq network;
824
char *connect_to = NULL;
825
char tempdir[] = "/tmp/mandosXXXXXX";
826
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
827
const char *seckey = PATHDIR "/" SECKEY;
828
const char *pubkey = PATHDIR "/" PUBKEY;
830
mandos_context mc = { .simple_poll = NULL, .server = NULL,
831
.dh_bits = 1024, .priority = "SECURE256"
832
":!CTYPE-X.509:+CTYPE-OPENPGP" };
833
bool gnutls_initalized = false;
834
bool gpgme_initalized = false;
837
struct argp_option options[] = {
838
{ .name = "debug", .key = 128,
839
.doc = "Debug mode", .group = 3 },
840
{ .name = "connect", .key = 'c',
841
.arg = "ADDRESS:PORT",
842
.doc = "Connect directly to a specific Mandos server",
844
{ .name = "interface", .key = 'i',
846
.doc = "Interface that will be used to search for Mandos"
849
{ .name = "seckey", .key = 's',
851
.doc = "OpenPGP secret key file base name",
853
{ .name = "pubkey", .key = 'p',
855
.doc = "OpenPGP public key file base name",
857
{ .name = "dh-bits", .key = 129,
859
.doc = "Bit length of the prime number used in the"
860
" Diffie-Hellman key exchange",
862
{ .name = "priority", .key = 130,
864
.doc = "GnuTLS priority string for the TLS handshake",
869
error_t parse_opt (int key, char *arg,
870
struct argp_state *state) {
871
/* Get the INPUT argument from `argp_parse', which we know is
872
a pointer to our plugin list pointer. */
874
case 128: /* --debug */
877
case 'c': /* --connect */
880
case 'i': /* --interface */
883
case 's': /* --seckey */
886
case 'p': /* --pubkey */
889
case 129: /* --dh-bits */
891
mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
897
case 130: /* --priority */
905
return ARGP_ERR_UNKNOWN;
910
struct argp argp = { .options = options, .parser = parse_opt,
912
.doc = "Mandos client -- Get and decrypt"
913
" passwords from a Mandos server" };
914
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
915
if (ret == ARGP_ERR_UNKNOWN){
916
fprintf(stderr, "Unknown error while parsing arguments\n");
917
exitcode = EXIT_FAILURE;
922
/* If the interface is down, bring it up */
924
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
927
exitcode = EXIT_FAILURE;
930
strcpy(network.ifr_name, interface);
931
ret = ioctl(sd, SIOCGIFFLAGS, &network);
933
perror("ioctl SIOCGIFFLAGS");
934
exitcode = EXIT_FAILURE;
937
if((network.ifr_flags & IFF_UP) == 0){
938
network.ifr_flags |= IFF_UP;
939
ret = ioctl(sd, SIOCSIFFLAGS, &network);
941
perror("ioctl SIOCSIFFLAGS");
942
exitcode = EXIT_FAILURE;
946
ret = TEMP_FAILURE_RETRY(close(sd));
965
ret = init_gnutls_global(&mc, pubkey, seckey);
967
fprintf(stderr, "init_gnutls_global failed\n");
968
exitcode = EXIT_FAILURE;
971
gnutls_initalized = true;
974
if(mkdtemp(tempdir) == NULL){
980
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
981
fprintf(stderr, "gpgme_initalized failed\n");
982
exitcode = EXIT_FAILURE;
985
gpgme_initalized = true;
1762
988
if_index = (AvahiIfIndex) if_nametoindex(interface);
1763
989
if(if_index == 0){
1764
990
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1765
exitcode = EX_UNAVAILABLE;
1773
/* Re-raise priviliges */
1777
perror_plus("seteuid");
1781
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1782
messages about the network interface to mess up the prompt */
1783
ret = klogctl(8, NULL, 5);
1784
bool restore_loglevel = true;
1786
restore_loglevel = false;
1787
perror_plus("klogctl");
1789
#endif /* __linux__ */
1791
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1793
perror_plus("socket");
1794
exitcode = EX_OSERR;
1796
if(restore_loglevel){
1797
ret = klogctl(7, NULL, 0);
1799
perror_plus("klogctl");
1802
#endif /* __linux__ */
1803
/* Lower privileges */
1807
perror_plus("seteuid");
1811
strcpy(network.ifr_name, interface);
1812
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1814
perror_plus("ioctl SIOCGIFFLAGS");
1816
if(restore_loglevel){
1817
ret = klogctl(7, NULL, 0);
1819
perror_plus("klogctl");
1822
#endif /* __linux__ */
1823
exitcode = EX_OSERR;
1824
/* Lower privileges */
1828
perror_plus("seteuid");
1832
if((network.ifr_flags & IFF_UP) == 0){
1833
network.ifr_flags |= IFF_UP;
1834
take_down_interface = true;
1835
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1837
take_down_interface = false;
1838
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1839
exitcode = EX_OSERR;
1841
if(restore_loglevel){
1842
ret = klogctl(7, NULL, 0);
1844
perror_plus("klogctl");
1847
#endif /* __linux__ */
1848
/* Lower privileges */
1852
perror_plus("seteuid");
1857
/* Sleep checking until interface is running.
1858
Check every 0.25s, up to total time of delay */
1859
for(int i=0; i < delay * 4; i++){
1860
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1862
perror_plus("ioctl SIOCGIFFLAGS");
1863
} else if(network.ifr_flags & IFF_RUNNING){
1866
struct timespec sleeptime = { .tv_nsec = 250000000 };
1867
ret = nanosleep(&sleeptime, NULL);
1868
if(ret == -1 and errno != EINTR){
1869
perror_plus("nanosleep");
1872
if(not take_down_interface){
1873
/* We won't need the socket anymore */
1874
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1876
perror_plus("close");
1880
if(restore_loglevel){
1881
/* Restores kernel loglevel to default */
1882
ret = klogctl(7, NULL, 0);
1884
perror_plus("klogctl");
1887
#endif /* __linux__ */
1888
/* Lower privileges */
1890
if(take_down_interface){
1891
/* Lower privileges */
1894
perror_plus("seteuid");
1897
/* Lower privileges permanently */
1900
perror_plus("setuid");
1909
ret = init_gnutls_global(pubkey, seckey);
1911
fprintf(stderr, "init_gnutls_global failed\n");
1912
exitcode = EX_UNAVAILABLE;
1915
gnutls_initialized = true;
1922
if(mkdtemp(tempdir) == NULL){
1923
perror_plus("mkdtemp");
1926
tempdir_created = true;
1932
if(not init_gpgme(pubkey, seckey, tempdir)){
1933
fprintf(stderr, "init_gpgme failed\n");
1934
exitcode = EX_UNAVAILABLE;
1937
gpgme_initialized = true;
1944
if(connect_to != NULL){
1945
/* Connect directly, do not use Zeroconf */
1946
/* (Mainly meant for debugging) */
1947
char *address = strrchr(connect_to, ':');
1948
if(address == NULL){
1949
fprintf(stderr, "No colon in address\n");
1950
exitcode = EX_USAGE;
1960
tmpmax = strtoimax(address+1, &tmp, 10);
1961
if(errno != 0 or tmp == address+1 or *tmp != '\0'
1962
or tmpmax != (uint16_t)tmpmax){
1963
fprintf(stderr, "Bad port number\n");
1964
exitcode = EX_USAGE;
1972
port = (uint16_t)tmpmax;
1974
/* Colon in address indicates IPv6 */
1976
if(strchr(connect_to, ':') != NULL){
1978
/* Accept [] around IPv6 address - see RFC 5952 */
1979
if(connect_to[0] == '[' and address[-1] == ']')
1987
address = connect_to;
1993
while(not quit_now){
1994
ret = start_mandos_communication(address, port, if_index, af);
1995
if(quit_now or ret == 0){
1999
fprintf(stderr, "Retrying in %d seconds\n",
2000
(int)retry_interval);
2002
sleep((int)retry_interval);
2006
exitcode = EXIT_SUCCESS;
2017
AvahiServerConfig config;
2018
/* Do not publish any local Zeroconf records */
2019
avahi_server_config_init(&config);
2020
config.publish_hinfo = 0;
2021
config.publish_addresses = 0;
2022
config.publish_workstation = 0;
2023
config.publish_domain = 0;
2025
/* Allocate a new server */
2026
mc.server = avahi_server_new(avahi_simple_poll_get
2027
(mc.simple_poll), &config, NULL,
2030
/* Free the Avahi configuration data */
2031
avahi_server_config_free(&config);
2034
/* Check if creating the Avahi server object succeeded */
2035
if(mc.server == NULL){
2036
fprintf(stderr, "Failed to create Avahi server: %s\n",
2037
avahi_strerror(error));
2038
exitcode = EX_UNAVAILABLE;
2046
/* Create the Avahi service browser */
2047
sb = avahi_s_service_browser_new(mc.server, if_index,
2048
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
2049
NULL, 0, browse_callback, NULL);
2051
fprintf(stderr, "Failed to create service browser: %s\n",
2052
avahi_strerror(avahi_server_errno(mc.server)));
2053
exitcode = EX_UNAVAILABLE;
2061
/* Run the main loop */
2064
fprintf(stderr, "Starting Avahi loop search\n");
2067
ret = avahi_loop_with_timeout(mc.simple_poll,
2068
(int)(retry_interval * 1000));
2070
fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
2071
(ret == 0) ? "successfully" : "with error");
994
if(connect_to != NULL){
995
/* Connect directly, do not use Zeroconf */
996
/* (Mainly meant for debugging) */
997
char *address = strrchr(connect_to, ':');
999
fprintf(stderr, "No colon in address\n");
1000
exitcode = EXIT_FAILURE;
1004
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
1006
perror("Bad port number");
1007
exitcode = EXIT_FAILURE;
1011
address = connect_to;
1012
ret = start_mandos_communication(address, port, if_index, &mc);
1014
exitcode = EXIT_FAILURE;
1016
exitcode = EXIT_SUCCESS;
1022
avahi_set_log_function(empty_log);
1025
/* Initialize the pseudo-RNG for Avahi */
1026
srand((unsigned int) time(NULL));
1028
/* Allocate main Avahi loop object */
1029
mc.simple_poll = avahi_simple_poll_new();
1030
if (mc.simple_poll == NULL) {
1031
fprintf(stderr, "Avahi: Failed to create simple poll"
1033
exitcode = EXIT_FAILURE;
1038
AvahiServerConfig config;
1039
/* Do not publish any local Zeroconf records */
1040
avahi_server_config_init(&config);
1041
config.publish_hinfo = 0;
1042
config.publish_addresses = 0;
1043
config.publish_workstation = 0;
1044
config.publish_domain = 0;
1046
/* Allocate a new server */
1047
mc.server = avahi_server_new(avahi_simple_poll_get
1048
(mc.simple_poll), &config, NULL,
1051
/* Free the Avahi configuration data */
1052
avahi_server_config_free(&config);
1055
/* Check if creating the Avahi server object succeeded */
1056
if (mc.server == NULL) {
1057
fprintf(stderr, "Failed to create Avahi server: %s\n",
1058
avahi_strerror(error));
1059
exitcode = EXIT_FAILURE;
1063
/* Create the Avahi service browser */
1064
sb = avahi_s_service_browser_new(mc.server, if_index,
1066
"_mandos._tcp", NULL, 0,
1067
browse_callback, &mc);
1069
fprintf(stderr, "Failed to create service browser: %s\n",
1070
avahi_strerror(avahi_server_errno(mc.server)));
1071
exitcode = EXIT_FAILURE;
1075
/* Run the main loop */
1078
fprintf(stderr, "Starting Avahi loop search\n");
1081
avahi_simple_poll_loop(mc.simple_poll);
2077
fprintf(stderr, "%s exiting\n", argv[0]);
2080
/* Cleanup things */
2082
avahi_s_service_browser_free(sb);
2084
if(mc.server != NULL)
2085
avahi_server_free(mc.server);
2087
if(mc.simple_poll != NULL)
2088
avahi_simple_poll_free(mc.simple_poll);
2090
if(gnutls_initialized){
2091
gnutls_certificate_free_credentials(mc.cred);
2092
gnutls_global_deinit();
2093
gnutls_dh_params_deinit(mc.dh_params);
2096
if(gpgme_initialized){
2097
gpgme_release(mc.ctx);
2100
/* Cleans up the circular linked list of Mandos servers the client
2102
if(mc.current_server != NULL){
2103
mc.current_server->prev->next = NULL;
2104
while(mc.current_server != NULL){
2105
server *next = mc.current_server->next;
2106
free(mc.current_server);
2107
mc.current_server = next;
2111
/* XXX run network hooks "stop" here */
2113
/* Take down the network interface */
2114
if(take_down_interface){
2115
/* Re-raise priviliges */
2119
perror_plus("seteuid");
2122
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2124
perror_plus("ioctl SIOCGIFFLAGS");
2125
} else if(network.ifr_flags & IFF_UP) {
2126
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2127
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2129
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2132
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2134
perror_plus("close");
2136
/* Lower privileges permanently */
2140
perror_plus("setuid");
2145
/* Removes the GPGME temp directory and all files inside */
2146
if(tempdir_created){
2147
struct dirent **direntries = NULL;
2148
struct dirent *direntry = NULL;
2149
int numentries = scandir(tempdir, &direntries, notdotentries,
2151
if (numentries > 0){
2152
for(int i = 0; i < numentries; i++){
2153
direntry = direntries[i];
2154
char *fullname = NULL;
2155
ret = asprintf(&fullname, "%s/%s", tempdir,
2158
perror_plus("asprintf");
2161
ret = remove(fullname);
2163
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2170
/* need to clean even if 0 because man page doesn't specify */
2172
if (numentries == -1){
2173
perror_plus("scandir");
2175
ret = rmdir(tempdir);
2176
if(ret == -1 and errno != ENOENT){
2177
perror_plus("rmdir");
2182
sigemptyset(&old_sigterm_action.sa_mask);
2183
old_sigterm_action.sa_handler = SIG_DFL;
2184
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
2185
&old_sigterm_action,
2188
perror_plus("sigaction");
2191
ret = raise(signal_received);
2192
} while(ret != 0 and errno == EINTR);
2194
perror_plus("raise");
2197
TEMP_FAILURE_RETRY(pause());
1086
fprintf(stderr, "%s exiting\n", argv[0]);
1089
/* Cleanup things */
1091
avahi_s_service_browser_free(sb);
1093
if (mc.server != NULL)
1094
avahi_server_free(mc.server);
1096
if (mc.simple_poll != NULL)
1097
avahi_simple_poll_free(mc.simple_poll);
1099
if (gnutls_initalized){
1100
gnutls_certificate_free_credentials(mc.cred);
1101
gnutls_global_deinit ();
1102
gnutls_dh_params_deinit(mc.dh_params);
1105
if(gpgme_initalized){
1106
gpgme_release(mc.ctx);
1109
/* Removes the temp directory used by GPGME */
1110
if(tempdir[0] != '\0'){
1112
struct dirent *direntry;
1113
d = opendir(tempdir);
1118
direntry = readdir(d);
1119
if(direntry == NULL){
1122
if (direntry->d_type == DT_REG){
1123
char *fullname = NULL;
1124
ret = asprintf(&fullname, "%s/%s", tempdir,
1130
ret = unlink(fullname);
1132
fprintf(stderr, "unlink(\"%s\"): %s",
1133
fullname, strerror(errno));
1140
ret = rmdir(tempdir);