1073
/* Signal handler that stops main loop after SIGTERM */
1074
static void handle_sigterm(int sig){
1079
signal_received = sig;
1080
int old_errno = errno;
1081
/* set main loop to exit */
1082
if(mc.simple_poll != NULL){
1083
avahi_simple_poll_quit(mc.simple_poll);
1088
bool get_flags(const char *ifname, struct ifreq *ifr){
1091
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1093
perror_plus("socket");
1096
strcpy(ifr->ifr_name, ifname);
1097
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1100
perror_plus("ioctl SIOCGIFFLAGS");
1107
bool good_flags(const char *ifname, const struct ifreq *ifr){
1109
/* Reject the loopback device */
1110
if(ifr->ifr_flags & IFF_LOOPBACK){
1112
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1117
/* Accept point-to-point devices only if connect_to is specified */
1118
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1120
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1125
/* Otherwise, reject non-broadcast-capable devices */
1126
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1128
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1133
/* Reject non-ARP interfaces (including dummy interfaces) */
1134
if(ifr->ifr_flags & IFF_NOARP){
1136
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1141
/* Accept this device */
1143
fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1149
* This function determines if a directory entry in /sys/class/net
1150
* corresponds to an acceptable network device.
1151
* (This function is passed to scandir(3) as a filter function.)
1153
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)){
1164
if(not good_flags(if_entry->d_name, &ifr)){
1170
int notdotentries(const struct dirent *direntry){
1171
/* Skip "." and ".." */
1172
if(direntry->d_name[0] == '.'
1173
and (direntry->d_name[1] == '\0'
1174
or (direntry->d_name[1] == '.'
1175
and direntry->d_name[2] == '\0'))){
1181
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1183
struct timespec now;
1184
struct timespec waited_time;
1185
intmax_t block_time;
1188
if(mc.current_server == NULL){
1191
"Wait until first server is found. No timeout!\n");
1193
ret = avahi_simple_poll_iterate(s, -1);
1196
fprintf(stderr, "Check current_server if we should run it,"
1199
/* the current time */
1200
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1202
perror_plus("clock_gettime");
1205
/* Calculating in ms how long time between now and server
1206
who we visted longest time ago. Now - last seen. */
1207
waited_time.tv_sec = (now.tv_sec
1208
- mc.current_server->last_seen.tv_sec);
1209
waited_time.tv_nsec = (now.tv_nsec
1210
- mc.current_server->last_seen.tv_nsec);
1211
/* total time is 10s/10,000ms.
1212
Converting to s from ms by dividing by 1,000,
1213
and ns to ms by dividing by 1,000,000. */
1214
block_time = ((retry_interval
1215
- ((intmax_t)waited_time.tv_sec * 1000))
1216
- ((intmax_t)waited_time.tv_nsec / 1000000));
1219
fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1222
if(block_time <= 0){
1223
ret = start_mandos_communication(mc.current_server->ip,
1224
mc.current_server->port,
1225
mc.current_server->if_index,
1226
mc.current_server->af);
1228
avahi_simple_poll_quit(mc.simple_poll);
1231
ret = clock_gettime(CLOCK_MONOTONIC,
1232
&mc.current_server->last_seen);
1234
perror_plus("clock_gettime");
1237
mc.current_server = mc.current_server->next;
1238
block_time = 0; /* Call avahi to find new Mandos
1239
servers, but don't block */
1242
ret = avahi_simple_poll_iterate(s, (int)block_time);
1245
if (ret > 0 or errno != EINTR) {
1246
return (ret != 1) ? ret : 0;
1252
814
int main(int argc, char *argv[]){
1253
AvahiSServiceBrowser *sb = NULL;
1258
int exitcode = EXIT_SUCCESS;
1259
const char *interface = "";
1260
struct ifreq network;
1262
bool take_down_interface = false;
1265
char tempdir[] = "/tmp/mandosXXXXXX";
1266
bool tempdir_created = false;
1267
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1268
const char *seckey = PATHDIR "/" SECKEY;
1269
const char *pubkey = PATHDIR "/" PUBKEY;
1271
bool gnutls_initialized = false;
1272
bool gpgme_initialized = false;
1274
double retry_interval = 10; /* 10s between trying a server and
1275
retrying the same server again */
1277
struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1278
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1283
/* Lower any group privileges we might have, just to be safe */
1287
perror_plus("setgid");
1290
/* Lower user privileges (temporarily) */
1294
perror_plus("seteuid");
1302
struct argp_option options[] = {
1303
{ .name = "debug", .key = 128,
1304
.doc = "Debug mode", .group = 3 },
1305
{ .name = "connect", .key = 'c',
1306
.arg = "ADDRESS:PORT",
1307
.doc = "Connect directly to a specific Mandos server",
1309
{ .name = "interface", .key = 'i',
1311
.doc = "Network interface that will be used to search for"
1314
{ .name = "seckey", .key = 's',
1316
.doc = "OpenPGP secret key file base name",
1318
{ .name = "pubkey", .key = 'p',
1320
.doc = "OpenPGP public key file base name",
1322
{ .name = "dh-bits", .key = 129,
1324
.doc = "Bit length of the prime number used in the"
1325
" Diffie-Hellman key exchange",
1327
{ .name = "priority", .key = 130,
1329
.doc = "GnuTLS priority string for the TLS handshake",
1331
{ .name = "delay", .key = 131,
1333
.doc = "Maximum delay to wait for interface startup",
1335
{ .name = "retry", .key = 132,
1337
.doc = "Retry interval used when denied by the mandos server",
1340
* These reproduce what we would get without ARGP_NO_HELP
1342
{ .name = "help", .key = '?',
1343
.doc = "Give this help list", .group = -1 },
1344
{ .name = "usage", .key = -3,
1345
.doc = "Give a short usage message", .group = -1 },
1346
{ .name = "version", .key = 'V',
1347
.doc = "Print program version", .group = -1 },
1351
error_t parse_opt(int key, char *arg,
1352
struct argp_state *state){
1355
case 128: /* --debug */
1358
case 'c': /* --connect */
1361
case 'i': /* --interface */
1364
case 's': /* --seckey */
1367
case 'p': /* --pubkey */
1370
case 129: /* --dh-bits */
1372
tmpmax = strtoimax(arg, &tmp, 10);
1373
if(errno != 0 or tmp == arg or *tmp != '\0'
1374
or tmpmax != (typeof(mc.dh_bits))tmpmax){
1375
argp_error(state, "Bad number of DH bits");
1377
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1379
case 130: /* --priority */
1382
case 131: /* --delay */
1384
delay = strtof(arg, &tmp);
1385
if(errno != 0 or tmp == arg or *tmp != '\0'){
1386
argp_error(state, "Bad delay");
1388
case 132: /* --retry */
1390
retry_interval = strtod(arg, &tmp);
1391
if(errno != 0 or tmp == arg or *tmp != '\0'
1392
or (retry_interval * 1000) > INT_MAX
1393
or retry_interval < 0){
1394
argp_error(state, "Bad retry interval");
1398
* These reproduce what we would get without ARGP_NO_HELP
1400
case '?': /* --help */
1401
argp_state_help(state, state->out_stream,
1402
(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1403
& ~(unsigned int)ARGP_HELP_EXIT_OK);
1404
case -3: /* --usage */
1405
argp_state_help(state, state->out_stream,
1406
ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1407
case 'V': /* --version */
1408
fprintf(state->out_stream, "%s\n", argp_program_version);
1409
exit(argp_err_exit_status);
1412
return ARGP_ERR_UNKNOWN;
1417
struct argp argp = { .options = options, .parser = parse_opt,
1419
.doc = "Mandos client -- Get and decrypt"
1420
" passwords from a Mandos server" };
1421
ret = argp_parse(&argp, argc, argv,
1422
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1429
perror_plus("argp_parse");
1430
exitcode = EX_OSERR;
1433
exitcode = EX_USAGE;
1439
/* Work around Debian bug #633582:
1440
<http://bugs.debian.org/633582> */
1443
/* Re-raise priviliges */
1447
perror_plus("seteuid");
1450
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1451
int seckey_fd = open(seckey, O_RDONLY);
1452
if(seckey_fd == -1){
1453
perror_plus("open");
1455
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1457
perror_plus("fstat");
1459
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1460
ret = fchown(seckey_fd, uid, gid);
1462
perror_plus("fchown");
1466
TEMP_FAILURE_RETRY(close(seckey_fd));
1470
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1471
int pubkey_fd = open(pubkey, O_RDONLY);
1472
if(pubkey_fd == -1){
1473
perror_plus("open");
1475
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1477
perror_plus("fstat");
1479
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1480
ret = fchown(pubkey_fd, uid, gid);
1482
perror_plus("fchown");
1486
TEMP_FAILURE_RETRY(close(pubkey_fd));
1490
/* Lower privileges */
1494
perror_plus("seteuid");
1499
avahi_set_log_function(empty_log);
1502
if(interface[0] == '\0'){
1503
struct dirent **direntries;
1504
ret = scandir(sys_class_net, &direntries, good_interface,
1507
/* Pick the first good interface */
1508
interface = strdup(direntries[0]->d_name);
1510
fprintf(stderr, "Using interface \"%s\"\n", interface);
1512
if(interface == NULL){
1513
perror_plus("malloc");
1515
exitcode = EXIT_FAILURE;
1521
fprintf(stderr, "Could not find a network interface\n");
1522
exitcode = EXIT_FAILURE;
1527
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1528
from the signal handler */
1529
/* Initialize the pseudo-RNG for Avahi */
1530
srand((unsigned int) time(NULL));
1531
mc.simple_poll = avahi_simple_poll_new();
1532
if(mc.simple_poll == NULL){
1533
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1534
exitcode = EX_UNAVAILABLE;
1538
sigemptyset(&sigterm_action.sa_mask);
1539
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1541
perror_plus("sigaddset");
1542
exitcode = EX_OSERR;
1545
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1547
perror_plus("sigaddset");
1548
exitcode = EX_OSERR;
1551
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1553
perror_plus("sigaddset");
1554
exitcode = EX_OSERR;
1557
/* Need to check if the handler is SIG_IGN before handling:
1558
| [[info:libc:Initial Signal Actions]] |
1559
| [[info:libc:Basic Signal Handling]] |
1561
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1563
perror_plus("sigaction");
1566
if(old_sigterm_action.sa_handler != SIG_IGN){
1567
ret = sigaction(SIGINT, &sigterm_action, NULL);
1569
perror_plus("sigaction");
1570
exitcode = EX_OSERR;
1574
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1576
perror_plus("sigaction");
1579
if(old_sigterm_action.sa_handler != SIG_IGN){
1580
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1582
perror_plus("sigaction");
1583
exitcode = EX_OSERR;
1587
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1589
perror_plus("sigaction");
1592
if(old_sigterm_action.sa_handler != SIG_IGN){
1593
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1595
perror_plus("sigaction");
1596
exitcode = EX_OSERR;
1601
/* If the interface is down, bring it up */
1602
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;
1603
988
if_index = (AvahiIfIndex) if_nametoindex(interface);
1604
989
if(if_index == 0){
1605
990
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1606
exitcode = EX_UNAVAILABLE;
1614
/* Re-raise priviliges */
1618
perror_plus("seteuid");
1622
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1623
messages about the network interface to mess up the prompt */
1624
ret = klogctl(8, NULL, 5);
1625
bool restore_loglevel = true;
1627
restore_loglevel = false;
1628
perror_plus("klogctl");
1630
#endif /* __linux__ */
1632
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1634
perror_plus("socket");
1635
exitcode = EX_OSERR;
1637
if(restore_loglevel){
1638
ret = klogctl(7, NULL, 0);
1640
perror_plus("klogctl");
1643
#endif /* __linux__ */
1644
/* Lower privileges */
1648
perror_plus("seteuid");
1652
strcpy(network.ifr_name, interface);
1653
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1655
perror_plus("ioctl SIOCGIFFLAGS");
1657
if(restore_loglevel){
1658
ret = klogctl(7, NULL, 0);
1660
perror_plus("klogctl");
1663
#endif /* __linux__ */
1664
exitcode = EX_OSERR;
1665
/* Lower privileges */
1669
perror_plus("seteuid");
1673
if((network.ifr_flags & IFF_UP) == 0){
1674
network.ifr_flags |= IFF_UP;
1675
take_down_interface = true;
1676
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1678
take_down_interface = false;
1679
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1680
exitcode = EX_OSERR;
1682
if(restore_loglevel){
1683
ret = klogctl(7, NULL, 0);
1685
perror_plus("klogctl");
1688
#endif /* __linux__ */
1689
/* Lower privileges */
1693
perror_plus("seteuid");
1698
/* Sleep checking until interface is running.
1699
Check every 0.25s, up to total time of delay */
1700
for(int i=0; i < delay * 4; i++){
1701
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1703
perror_plus("ioctl SIOCGIFFLAGS");
1704
} else if(network.ifr_flags & IFF_RUNNING){
1707
struct timespec sleeptime = { .tv_nsec = 250000000 };
1708
ret = nanosleep(&sleeptime, NULL);
1709
if(ret == -1 and errno != EINTR){
1710
perror_plus("nanosleep");
1713
if(not take_down_interface){
1714
/* We won't need the socket anymore */
1715
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1717
perror_plus("close");
1721
if(restore_loglevel){
1722
/* Restores kernel loglevel to default */
1723
ret = klogctl(7, NULL, 0);
1725
perror_plus("klogctl");
1728
#endif /* __linux__ */
1729
/* Lower privileges */
1731
if(take_down_interface){
1732
/* Lower privileges */
1735
perror_plus("seteuid");
1738
/* Lower privileges permanently */
1741
perror_plus("setuid");
1750
ret = init_gnutls_global(pubkey, seckey);
1752
fprintf(stderr, "init_gnutls_global failed\n");
1753
exitcode = EX_UNAVAILABLE;
1756
gnutls_initialized = true;
1763
if(mkdtemp(tempdir) == NULL){
1764
perror_plus("mkdtemp");
1767
tempdir_created = true;
1773
if(not init_gpgme(pubkey, seckey, tempdir)){
1774
fprintf(stderr, "init_gpgme failed\n");
1775
exitcode = EX_UNAVAILABLE;
1778
gpgme_initialized = true;
1785
if(connect_to != NULL){
1786
/* Connect directly, do not use Zeroconf */
1787
/* (Mainly meant for debugging) */
1788
char *address = strrchr(connect_to, ':');
1789
if(address == NULL){
1790
fprintf(stderr, "No colon in address\n");
1791
exitcode = EX_USAGE;
1801
tmpmax = strtoimax(address+1, &tmp, 10);
1802
if(errno != 0 or tmp == address+1 or *tmp != '\0'
1803
or tmpmax != (uint16_t)tmpmax){
1804
fprintf(stderr, "Bad port number\n");
1805
exitcode = EX_USAGE;
1813
port = (uint16_t)tmpmax;
1815
/* Colon in address indicates IPv6 */
1817
if(strchr(connect_to, ':') != NULL){
1819
/* Accept [] around IPv6 address - see RFC 5952 */
1820
if(connect_to[0] == '[' and address[-1] == ']')
1828
address = connect_to;
1834
while(not quit_now){
1835
ret = start_mandos_communication(address, port, if_index, af);
1836
if(quit_now or ret == 0){
1840
fprintf(stderr, "Retrying in %d seconds\n",
1841
(int)retry_interval);
1843
sleep((int)retry_interval);
1847
exitcode = EXIT_SUCCESS;
1858
AvahiServerConfig config;
1859
/* Do not publish any local Zeroconf records */
1860
avahi_server_config_init(&config);
1861
config.publish_hinfo = 0;
1862
config.publish_addresses = 0;
1863
config.publish_workstation = 0;
1864
config.publish_domain = 0;
1866
/* Allocate a new server */
1867
mc.server = avahi_server_new(avahi_simple_poll_get
1868
(mc.simple_poll), &config, NULL,
1871
/* Free the Avahi configuration data */
1872
avahi_server_config_free(&config);
1875
/* Check if creating the Avahi server object succeeded */
1876
if(mc.server == NULL){
1877
fprintf(stderr, "Failed to create Avahi server: %s\n",
1878
avahi_strerror(error));
1879
exitcode = EX_UNAVAILABLE;
1887
/* Create the Avahi service browser */
1888
sb = avahi_s_service_browser_new(mc.server, if_index,
1889
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1890
NULL, 0, browse_callback, NULL);
1892
fprintf(stderr, "Failed to create service browser: %s\n",
1893
avahi_strerror(avahi_server_errno(mc.server)));
1894
exitcode = EX_UNAVAILABLE;
1902
/* Run the main loop */
1905
fprintf(stderr, "Starting Avahi loop search\n");
1908
ret = avahi_loop_with_timeout(mc.simple_poll,
1909
(int)(retry_interval * 1000));
1911
fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1912
(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);
1918
fprintf(stderr, "%s exiting\n", argv[0]);
1921
/* Cleanup things */
1923
avahi_s_service_browser_free(sb);
1925
if(mc.server != NULL)
1926
avahi_server_free(mc.server);
1928
if(mc.simple_poll != NULL)
1929
avahi_simple_poll_free(mc.simple_poll);
1931
if(gnutls_initialized){
1932
gnutls_certificate_free_credentials(mc.cred);
1933
gnutls_global_deinit();
1934
gnutls_dh_params_deinit(mc.dh_params);
1937
if(gpgme_initialized){
1938
gpgme_release(mc.ctx);
1941
/* Cleans up the circular linked list of Mandos servers the client
1943
if(mc.current_server != NULL){
1944
mc.current_server->prev->next = NULL;
1945
while(mc.current_server != NULL){
1946
server *next = mc.current_server->next;
1947
free(mc.current_server);
1948
mc.current_server = next;
1952
/* Take down the network interface */
1953
if(take_down_interface){
1954
/* Re-raise priviliges */
1958
perror_plus("seteuid");
1961
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1963
perror_plus("ioctl SIOCGIFFLAGS");
1964
} else if(network.ifr_flags & IFF_UP) {
1965
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1966
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1968
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1971
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1973
perror_plus("close");
1975
/* Lower privileges permanently */
1979
perror_plus("setuid");
1984
/* Removes the GPGME temp directory and all files inside */
1985
if(tempdir_created){
1986
struct dirent **direntries = NULL;
1987
struct dirent *direntry = NULL;
1988
int numentries = scandir(tempdir, &direntries, notdotentries,
1990
if (numentries > 0){
1991
for(int i = 0; i < numentries; i++){
1992
direntry = direntries[i];
1993
char *fullname = NULL;
1994
ret = asprintf(&fullname, "%s/%s", tempdir,
1997
perror_plus("asprintf");
2000
ret = remove(fullname);
2002
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2009
/* need to clean even if 0 because man page doesn't specify */
2011
if (numentries == -1){
2012
perror_plus("scandir");
2014
ret = rmdir(tempdir);
2015
if(ret == -1 and errno != ENOENT){
2016
perror_plus("rmdir");
2021
sigemptyset(&old_sigterm_action.sa_mask);
2022
old_sigterm_action.sa_handler = SIG_DFL;
2023
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
2024
&old_sigterm_action,
2027
perror_plus("sigaction");
2030
ret = raise(signal_received);
2031
} while(ret != 0 and errno == EINTR);
2033
perror_plus("raise");
2036
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);