32
31
/* Needed by GPGME, specifically gpgme_data_seek() */
33
#ifndef _LARGEFILE_SOURCE
34
32
#define _LARGEFILE_SOURCE
36
#ifndef _FILE_OFFSET_BITS
37
33
#define _FILE_OFFSET_BITS 64
40
35
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
42
37
#include <stdio.h> /* fprintf(), stderr, fwrite(),
43
stdout, ferror(), remove() */
44
39
#include <stdint.h> /* uint16_t, uint32_t */
45
40
#include <stddef.h> /* NULL, size_t, ssize_t */
46
41
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
47
srand(), strtof(), abort() */
48
#include <stdbool.h> /* bool, false, true */
43
#include <stdbool.h> /* bool, true */
49
44
#include <string.h> /* memset(), strcmp(), strlen(),
50
45
strerror(), asprintf(), strcpy() */
51
#include <sys/ioctl.h> /* ioctl */
46
#include <sys/ioctl.h> /* ioctl */
52
47
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
53
48
sockaddr_in6, PF_INET6,
54
SOCK_STREAM, uid_t, gid_t, open(),
49
SOCK_STREAM, INET6_ADDRSTRLEN,
50
uid_t, gid_t, open(), opendir(), DIR */
56
51
#include <sys/stat.h> /* open() */
57
52
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
58
inet_pton(), connect() */
53
struct in6_addr, inet_pton(),
59
55
#include <fcntl.h> /* open() */
60
#include <dirent.h> /* opendir(), struct dirent, readdir()
62
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
56
#include <dirent.h> /* opendir(), struct dirent, readdir() */
57
#include <inttypes.h> /* PRIu16 */
64
58
#include <assert.h> /* assert() */
65
59
#include <errno.h> /* perror(), errno */
66
#include <time.h> /* nanosleep(), time() */
60
#include <time.h> /* time() */
67
61
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
62
SIOCSIFFLAGS, if_indextoname(),
69
63
if_nametoindex(), IF_NAMESIZE */
70
#include <netinet/in.h> /* IN6_IS_ADDR_LINKLOCAL,
71
INET_ADDRSTRLEN, INET6_ADDRSTRLEN
64
#include <netinet/in.h>
73
65
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
74
getuid(), getgid(), seteuid(),
66
getuid(), getgid(), setuid(),
76
68
#include <arpa/inet.h> /* inet_pton(), htons */
77
#include <iso646.h> /* not, or, and */
69
#include <iso646.h> /* not, and */
78
70
#include <argp.h> /* struct argp_option, error_t, struct
79
71
argp_state, struct argp,
80
72
argp_parse(), ARGP_KEY_ARG,
81
73
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
#include <signal.h> /* sigemptyset(), sigaddset(),
83
sigaction(), SIGTERM, sig_atomic_t,
87
#include <sys/klog.h> /* klogctl() */
88
#endif /* __linux__ */
91
76
/* All Avahi types, constants and functions
965
/* stop main loop after sigterm has been called */
966
static void handle_sigterm(int sig){
971
signal_received = sig;
972
int old_errno = errno;
973
if(mc.simple_poll != NULL){
974
avahi_simple_poll_quit(mc.simple_poll);
980
* This function determines if a directory entry in /sys/class/net
981
* corresponds to an acceptable network device.
982
* (This function is passed to scandir(3) as a filter function.)
984
int good_interface(const struct dirent *if_entry){
986
char *flagname = NULL;
987
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
993
if(if_entry->d_name[0] == '.'){
996
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1001
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1002
/* read line from flags_fd */
1003
ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1004
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1005
flagstring[(size_t)to_read] = '\0';
1006
if(flagstring == NULL){
1012
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1029
tmpmax = strtoimax(flagstring, &tmp, 0);
1030
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1031
and not (isspace(*tmp)))
1032
or tmpmax != (ifreq_flags)tmpmax){
1034
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1035
flagstring, if_entry->d_name);
1041
ifreq_flags flags = (ifreq_flags)tmpmax;
1042
/* Reject the loopback device */
1043
if(flags & IFF_LOOPBACK){
1045
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1050
/* Accept point-to-point devices only if connect_to is specified */
1051
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1053
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1058
/* Otherwise, reject non-broadcast-capable devices */
1059
if(not (flags & IFF_BROADCAST)){
1061
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1066
/* Accept this device */
1068
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1074
814
int main(int argc, char *argv[]){
1075
AvahiSServiceBrowser *sb = NULL;
1080
int exitcode = EXIT_SUCCESS;
1081
const char *interface = "";
1082
struct ifreq network;
1084
bool take_down_interface = false;
1087
char tempdir[] = "/tmp/mandosXXXXXX";
1088
bool tempdir_created = false;
1089
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1090
const char *seckey = PATHDIR "/" SECKEY;
1091
const char *pubkey = PATHDIR "/" PUBKEY;
1093
bool gnutls_initialized = false;
1094
bool gpgme_initialized = false;
1097
struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1098
struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1103
/* Lower any group privileges we might have, just to be safe */
1110
/* Lower user privileges (temporarily) */
1122
struct argp_option options[] = {
1123
{ .name = "debug", .key = 128,
1124
.doc = "Debug mode", .group = 3 },
1125
{ .name = "connect", .key = 'c',
1126
.arg = "ADDRESS:PORT",
1127
.doc = "Connect directly to a specific Mandos server",
1129
{ .name = "interface", .key = 'i',
1131
.doc = "Network interface that will be used to search for"
1134
{ .name = "seckey", .key = 's',
1136
.doc = "OpenPGP secret key file base name",
1138
{ .name = "pubkey", .key = 'p',
1140
.doc = "OpenPGP public key file base name",
1142
{ .name = "dh-bits", .key = 129,
1144
.doc = "Bit length of the prime number used in the"
1145
" Diffie-Hellman key exchange",
1147
{ .name = "priority", .key = 130,
1149
.doc = "GnuTLS priority string for the TLS handshake",
1151
{ .name = "delay", .key = 131,
1153
.doc = "Maximum delay to wait for interface startup",
1158
error_t parse_opt(int key, char *arg,
1159
struct argp_state *state){
1161
case 128: /* --debug */
1164
case 'c': /* --connect */
1167
case 'i': /* --interface */
1170
case 's': /* --seckey */
1173
case 'p': /* --pubkey */
1176
case 129: /* --dh-bits */
1178
tmpmax = strtoimax(arg, &tmp, 10);
1179
if(errno != 0 or tmp == arg or *tmp != '\0'
1180
or tmpmax != (typeof(mc.dh_bits))tmpmax){
1181
fprintf(stderr, "Bad number of DH bits\n");
1184
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1186
case 130: /* --priority */
1189
case 131: /* --delay */
1191
delay = strtof(arg, &tmp);
1192
if(errno != 0 or tmp == arg or *tmp != '\0'){
1193
fprintf(stderr, "Bad delay\n");
1202
return ARGP_ERR_UNKNOWN;
1207
struct argp argp = { .options = options, .parser = parse_opt,
1209
.doc = "Mandos client -- Get and decrypt"
1210
" passwords from a Mandos server" };
1211
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
1212
if(ret == ARGP_ERR_UNKNOWN){
1213
fprintf(stderr, "Unknown error while parsing arguments\n");
1214
exitcode = EXIT_FAILURE;
1220
avahi_set_log_function(empty_log);
1223
if(interface[0] == '\0'){
1224
struct dirent **direntries;
1225
ret = scandir(sys_class_net, &direntries, good_interface,
1228
/* Pick the first good interface */
1229
interface = strdup(direntries[0]->d_name);
1231
fprintf(stderr, "Using interface \"%s\"\n", interface);
1233
if(interface == NULL){
1236
exitcode = EXIT_FAILURE;
1242
fprintf(stderr, "Could not find a network interface\n");
1243
exitcode = EXIT_FAILURE;
1248
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1249
from the signal handler */
1250
/* Initialize the pseudo-RNG for Avahi */
1251
srand((unsigned int) time(NULL));
1252
mc.simple_poll = avahi_simple_poll_new();
1253
if(mc.simple_poll == NULL){
1254
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1255
exitcode = EXIT_FAILURE;
1259
sigemptyset(&sigterm_action.sa_mask);
1260
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1262
perror("sigaddset");
1263
exitcode = EXIT_FAILURE;
1266
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1268
perror("sigaddset");
1269
exitcode = EXIT_FAILURE;
1272
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1274
perror("sigaddset");
1275
exitcode = EXIT_FAILURE;
1278
/* Need to check if the handler is SIG_IGN before handling:
1279
| [[info:libc:Initial Signal Actions]] |
1280
| [[info:libc:Basic Signal Handling]] |
1282
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1284
perror("sigaction");
1285
return EXIT_FAILURE;
1287
if(old_sigterm_action.sa_handler != SIG_IGN){
1288
ret = sigaction(SIGINT, &sigterm_action, NULL);
1290
perror("sigaction");
1291
exitcode = EXIT_FAILURE;
1295
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1297
perror("sigaction");
1298
return EXIT_FAILURE;
1300
if(old_sigterm_action.sa_handler != SIG_IGN){
1301
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1303
perror("sigaction");
1304
exitcode = EXIT_FAILURE;
1308
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1310
perror("sigaction");
1311
return EXIT_FAILURE;
1313
if(old_sigterm_action.sa_handler != SIG_IGN){
1314
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1316
perror("sigaction");
1317
exitcode = EXIT_FAILURE;
1322
/* If the interface is down, bring it up */
1323
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;
1324
988
if_index = (AvahiIfIndex) if_nametoindex(interface);
1325
989
if(if_index == 0){
1326
990
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1327
exitcode = EXIT_FAILURE;
1335
/* Re-raise priviliges */
1343
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1344
messages to mess up the prompt */
1345
ret = klogctl(8, NULL, 5);
1346
bool restore_loglevel = true;
1348
restore_loglevel = false;
1351
#endif /* __linux__ */
1353
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1356
exitcode = EXIT_FAILURE;
1358
if(restore_loglevel){
1359
ret = klogctl(7, NULL, 0);
1364
#endif /* __linux__ */
1365
/* Lower privileges */
1373
strcpy(network.ifr_name, interface);
1374
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1376
perror("ioctl SIOCGIFFLAGS");
1378
if(restore_loglevel){
1379
ret = klogctl(7, NULL, 0);
1384
#endif /* __linux__ */
1385
exitcode = EXIT_FAILURE;
1386
/* Lower privileges */
1394
if((network.ifr_flags & IFF_UP) == 0){
1395
network.ifr_flags |= IFF_UP;
1396
take_down_interface = true;
1397
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1399
take_down_interface = false;
1400
perror("ioctl SIOCSIFFLAGS +IFF_UP");
1401
exitcode = EXIT_FAILURE;
1403
if(restore_loglevel){
1404
ret = klogctl(7, NULL, 0);
1409
#endif /* __linux__ */
1410
/* Lower privileges */
1419
/* sleep checking until interface is running */
1420
for(int i=0; i < delay * 4; i++){
1421
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1423
perror("ioctl SIOCGIFFLAGS");
1424
} else if(network.ifr_flags & IFF_RUNNING){
1427
struct timespec sleeptime = { .tv_nsec = 250000000 };
1428
ret = nanosleep(&sleeptime, NULL);
1429
if(ret == -1 and errno != EINTR){
1430
perror("nanosleep");
1433
if(not take_down_interface){
1434
/* We won't need the socket anymore */
1435
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1441
if(restore_loglevel){
1442
/* Restores kernel loglevel to default */
1443
ret = klogctl(7, NULL, 0);
1448
#endif /* __linux__ */
1449
/* Lower privileges */
1451
if(take_down_interface){
1452
/* Lower privileges */
1458
/* Lower privileges permanently */
1470
ret = init_gnutls_global(pubkey, seckey);
1472
fprintf(stderr, "init_gnutls_global failed\n");
1473
exitcode = EXIT_FAILURE;
1476
gnutls_initialized = true;
1483
tempdir_created = true;
1484
if(mkdtemp(tempdir) == NULL){
1485
tempdir_created = false;
1494
if(not init_gpgme(pubkey, seckey, tempdir)){
1495
fprintf(stderr, "init_gpgme failed\n");
1496
exitcode = EXIT_FAILURE;
1499
gpgme_initialized = true;
1506
if(connect_to != NULL){
1507
/* Connect directly, do not use Zeroconf */
1508
/* (Mainly meant for debugging) */
1509
char *address = strrchr(connect_to, ':');
1510
if(address == NULL){
1511
fprintf(stderr, "No colon in address\n");
1512
exitcode = EXIT_FAILURE;
1522
tmpmax = strtoimax(address+1, &tmp, 10);
1523
if(errno != 0 or tmp == address+1 or *tmp != '\0'
1524
or tmpmax != (uint16_t)tmpmax){
1525
fprintf(stderr, "Bad port number\n");
1526
exitcode = EXIT_FAILURE;
1534
port = (uint16_t)tmpmax;
1536
address = connect_to;
1537
/* Colon in address indicates IPv6 */
1539
if(strchr(address, ':') != NULL){
1549
ret = start_mandos_communication(address, port, if_index, af);
1551
exitcode = EXIT_FAILURE;
1553
exitcode = EXIT_SUCCESS;
1563
AvahiServerConfig config;
1564
/* Do not publish any local Zeroconf records */
1565
avahi_server_config_init(&config);
1566
config.publish_hinfo = 0;
1567
config.publish_addresses = 0;
1568
config.publish_workstation = 0;
1569
config.publish_domain = 0;
1571
/* Allocate a new server */
1572
mc.server = avahi_server_new(avahi_simple_poll_get
1573
(mc.simple_poll), &config, NULL,
1576
/* Free the Avahi configuration data */
1577
avahi_server_config_free(&config);
1580
/* Check if creating the Avahi server object succeeded */
1581
if(mc.server == NULL){
1582
fprintf(stderr, "Failed to create Avahi server: %s\n",
1583
avahi_strerror(error));
1584
exitcode = EXIT_FAILURE;
1592
/* Create the Avahi service browser */
1593
sb = avahi_s_service_browser_new(mc.server, if_index,
1594
AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1595
NULL, 0, browse_callback, NULL);
1597
fprintf(stderr, "Failed to create service browser: %s\n",
1598
avahi_strerror(avahi_server_errno(mc.server)));
1599
exitcode = EXIT_FAILURE;
1607
/* Run the main loop */
1610
fprintf(stderr, "Starting Avahi loop search\n");
1613
avahi_simple_poll_loop(mc.simple_poll);
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);
1618
fprintf(stderr, "%s exiting\n", argv[0]);
1621
/* Cleanup things */
1623
avahi_s_service_browser_free(sb);
1625
if(mc.server != NULL)
1626
avahi_server_free(mc.server);
1628
if(mc.simple_poll != NULL)
1629
avahi_simple_poll_free(mc.simple_poll);
1631
if(gnutls_initialized){
1632
gnutls_certificate_free_credentials(mc.cred);
1633
gnutls_global_deinit();
1634
gnutls_dh_params_deinit(mc.dh_params);
1637
if(gpgme_initialized){
1638
gpgme_release(mc.ctx);
1641
/* Take down the network interface */
1642
if(take_down_interface){
1643
/* Re-raise priviliges */
1650
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1652
perror("ioctl SIOCGIFFLAGS");
1653
} else if(network.ifr_flags & IFF_UP) {
1654
network.ifr_flags &= ~IFF_UP; /* clear flag */
1655
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1657
perror("ioctl SIOCSIFFLAGS -IFF_UP");
1660
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1664
/* Lower privileges permanently */
1673
/* Removes the temp directory used by GPGME */
1674
if(tempdir_created){
1676
struct dirent *direntry;
1677
d = opendir(tempdir);
1679
if(errno != ENOENT){
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);
1680
1115
perror("opendir");
1684
direntry = readdir(d);
1685
if(direntry == NULL){
1688
/* Skip "." and ".." */
1689
if(direntry->d_name[0] == '.'
1690
and (direntry->d_name[1] == '\0'
1691
or (direntry->d_name[1] == '.'
1692
and direntry->d_name[2] == '\0'))){
1695
char *fullname = NULL;
1696
ret = asprintf(&fullname, "%s/%s", tempdir,
1702
ret = remove(fullname);
1704
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
1711
ret = rmdir(tempdir);
1712
if(ret == -1 and errno != ENOENT){
1718
sigemptyset(&old_sigterm_action.sa_mask);
1719
old_sigterm_action.sa_handler = SIG_DFL;
1720
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
1721
&old_sigterm_action,
1724
perror("sigaction");
1727
ret = raise(signal_received);
1728
} while(ret != 0 and errno == EINTR);
1733
TEMP_FAILURE_RETRY(pause());
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);