32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
33
#ifndef _LARGEFILE_SOURCE
33
34
#define _LARGEFILE_SOURCE
36
#ifndef _FILE_OFFSET_BITS
34
37
#define _FILE_OFFSET_BITS 64
36
40
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
38
42
#include <stdio.h> /* fprintf(), stderr, fwrite(),
39
stdout, ferror(), sscanf */
43
stdout, ferror(), remove() */
40
44
#include <stdint.h> /* uint16_t, uint32_t */
41
45
#include <stddef.h> /* NULL, size_t, ssize_t */
42
46
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
44
#include <stdbool.h> /* bool, true */
47
srand(), strtof(), abort() */
48
#include <stdbool.h> /* bool, false, true */
45
49
#include <string.h> /* memset(), strcmp(), strlen(),
46
50
strerror(), asprintf(), strcpy() */
47
#include <sys/ioctl.h> /* ioctl */
51
#include <sys/ioctl.h> /* ioctl */
48
52
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
49
53
sockaddr_in6, PF_INET6,
50
SOCK_STREAM, INET6_ADDRSTRLEN,
51
uid_t, gid_t, open(), opendir(),
54
SOCK_STREAM, uid_t, gid_t, open(),
53
56
#include <sys/stat.h> /* open() */
54
57
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
55
struct in6_addr, inet_pton(),
58
inet_pton(), connect() */
57
59
#include <fcntl.h> /* open() */
58
60
#include <dirent.h> /* opendir(), struct dirent, readdir()
60
#include <inttypes.h> /* PRIu16, intmax_t, SCNdMAX */
62
#include <inttypes.h> /* PRIu16, PRIdMAX, intmax_t,
61
64
#include <assert.h> /* assert() */
62
65
#include <errno.h> /* perror(), errno */
63
#include <time.h> /* time() */
66
#include <time.h> /* nanosleep(), time() */
64
67
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
65
68
SIOCSIFFLAGS, if_indextoname(),
66
69
if_nametoindex(), IF_NAMESIZE */
67
#include <netinet/in.h>
70
#include <netinet/in.h> /* IN6_IS_ADDR_LINKLOCAL,
71
INET_ADDRSTRLEN, INET6_ADDRSTRLEN
68
73
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
69
getuid(), getgid(), setuid(),
74
getuid(), getgid(), seteuid(),
71
76
#include <arpa/inet.h> /* inet_pton(), htons */
72
#include <iso646.h> /* not, and, or */
77
#include <iso646.h> /* not, or, and */
73
78
#include <argp.h> /* struct argp_option, error_t, struct
74
79
argp_state, struct argp,
75
80
argp_parse(), ARGP_KEY_ARG,
76
81
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__ */
79
91
/* 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",
820
1074
int main(int argc, char *argv[]){
821
AvahiSServiceBrowser *sb = NULL;
826
int exitcode = EXIT_SUCCESS;
827
const char *interface = "eth0";
828
struct ifreq network;
832
char *connect_to = NULL;
833
char tempdir[] = "/tmp/mandosXXXXXX";
834
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
835
const char *seckey = PATHDIR "/" SECKEY;
836
const char *pubkey = PATHDIR "/" PUBKEY;
838
mandos_context mc = { .simple_poll = NULL, .server = NULL,
839
.dh_bits = 1024, .priority = "SECURE256"
840
":!CTYPE-X.509:+CTYPE-OPENPGP" };
841
bool gnutls_initalized = false;
842
bool gpgme_initalized = false;
845
struct argp_option options[] = {
846
{ .name = "debug", .key = 128,
847
.doc = "Debug mode", .group = 3 },
848
{ .name = "connect", .key = 'c',
849
.arg = "ADDRESS:PORT",
850
.doc = "Connect directly to a specific Mandos server",
852
{ .name = "interface", .key = 'i',
854
.doc = "Interface that will be used to search for Mandos"
857
{ .name = "seckey", .key = 's',
859
.doc = "OpenPGP secret key file base name",
861
{ .name = "pubkey", .key = 'p',
863
.doc = "OpenPGP public key file base name",
865
{ .name = "dh-bits", .key = 129,
867
.doc = "Bit length of the prime number used in the"
868
" Diffie-Hellman key exchange",
870
{ .name = "priority", .key = 130,
872
.doc = "GnuTLS priority string for the TLS handshake",
877
error_t parse_opt(int key, char *arg,
878
struct argp_state *state) {
880
case 128: /* --debug */
883
case 'c': /* --connect */
886
case 'i': /* --interface */
889
case 's': /* --seckey */
892
case 'p': /* --pubkey */
895
case 129: /* --dh-bits */
896
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
897
if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
898
or arg[numchars] != '\0'){
899
fprintf(stderr, "Bad number of DH bits\n");
902
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
904
case 130: /* --priority */
912
return ARGP_ERR_UNKNOWN;
917
struct argp argp = { .options = options, .parser = parse_opt,
919
.doc = "Mandos client -- Get and decrypt"
920
" passwords from a Mandos server" };
921
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
922
if(ret == ARGP_ERR_UNKNOWN){
923
fprintf(stderr, "Unknown error while parsing arguments\n");
924
exitcode = EXIT_FAILURE;
929
/* If the interface is down, bring it up */
931
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
934
exitcode = EXIT_FAILURE;
937
strcpy(network.ifr_name, interface);
938
ret = ioctl(sd, SIOCGIFFLAGS, &network);
940
perror("ioctl SIOCGIFFLAGS");
941
exitcode = EXIT_FAILURE;
944
if((network.ifr_flags & IFF_UP) == 0){
945
network.ifr_flags |= IFF_UP;
946
ret = ioctl(sd, SIOCSIFFLAGS, &network);
948
perror("ioctl SIOCSIFFLAGS");
949
exitcode = EXIT_FAILURE;
953
ret = (int)TEMP_FAILURE_RETRY(close(sd));
972
ret = init_gnutls_global(&mc, pubkey, seckey);
974
fprintf(stderr, "init_gnutls_global failed\n");
975
exitcode = EXIT_FAILURE;
978
gnutls_initalized = true;
981
if(mkdtemp(tempdir) == NULL){
987
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
988
fprintf(stderr, "gpgme_initalized failed\n");
989
exitcode = EXIT_FAILURE;
992
gpgme_initalized = true;
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){
995
1324
if_index = (AvahiIfIndex) if_nametoindex(interface);
996
1325
if(if_index == 0){
997
1326
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1001
if(connect_to != NULL){
1002
/* Connect directly, do not use Zeroconf */
1003
/* (Mainly meant for debugging) */
1004
char *address = strrchr(connect_to, ':');
1005
if(address == NULL){
1006
fprintf(stderr, "No colon in address\n");
1007
exitcode = EXIT_FAILURE;
1011
ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1012
if(ret < 1 or tmpmax != (uint16_t)tmpmax
1013
or address[numchars+1] != '\0'){
1014
fprintf(stderr, "Bad port number\n");
1015
exitcode = EXIT_FAILURE;
1018
port = (uint16_t)tmpmax;
1020
address = connect_to;
1021
ret = start_mandos_communication(address, port, if_index, &mc);
1023
exitcode = EXIT_FAILURE;
1025
exitcode = EXIT_SUCCESS;
1031
avahi_set_log_function(empty_log);
1034
/* Initialize the pseudo-RNG for Avahi */
1035
srand((unsigned int) time(NULL));
1037
/* Allocate main Avahi loop object */
1038
mc.simple_poll = avahi_simple_poll_new();
1039
if(mc.simple_poll == NULL) {
1040
fprintf(stderr, "Avahi: Failed to create simple poll"
1042
exitcode = EXIT_FAILURE;
1047
AvahiServerConfig config;
1048
/* Do not publish any local Zeroconf records */
1049
avahi_server_config_init(&config);
1050
config.publish_hinfo = 0;
1051
config.publish_addresses = 0;
1052
config.publish_workstation = 0;
1053
config.publish_domain = 0;
1055
/* Allocate a new server */
1056
mc.server = avahi_server_new(avahi_simple_poll_get
1057
(mc.simple_poll), &config, NULL,
1060
/* Free the Avahi configuration data */
1061
avahi_server_config_free(&config);
1064
/* Check if creating the Avahi server object succeeded */
1065
if(mc.server == NULL) {
1066
fprintf(stderr, "Failed to create Avahi server: %s\n",
1067
avahi_strerror(error));
1068
exitcode = EXIT_FAILURE;
1072
/* Create the Avahi service browser */
1073
sb = avahi_s_service_browser_new(mc.server, if_index,
1075
"_mandos._tcp", NULL, 0,
1076
browse_callback, &mc);
1078
fprintf(stderr, "Failed to create service browser: %s\n",
1079
avahi_strerror(avahi_server_errno(mc.server)));
1080
exitcode = EXIT_FAILURE;
1084
/* Run the main loop */
1087
fprintf(stderr, "Starting Avahi loop search\n");
1090
avahi_simple_poll_loop(mc.simple_poll);
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);
1095
fprintf(stderr, "%s exiting\n", argv[0]);
1098
/* Cleanup things */
1100
avahi_s_service_browser_free(sb);
1102
if(mc.server != NULL)
1103
avahi_server_free(mc.server);
1105
if(mc.simple_poll != NULL)
1106
avahi_simple_poll_free(mc.simple_poll);
1108
if(gnutls_initalized){
1109
gnutls_certificate_free_credentials(mc.cred);
1110
gnutls_global_deinit();
1111
gnutls_dh_params_deinit(mc.dh_params);
1114
if(gpgme_initalized){
1115
gpgme_release(mc.ctx);
1118
/* Removes the temp directory used by GPGME */
1119
if(tempdir[0] != '\0'){
1121
struct dirent *direntry;
1122
d = opendir(tempdir);
1124
if(errno != ENOENT){
1129
direntry = readdir(d);
1130
if(direntry == NULL){
1133
if(direntry->d_type == DT_REG){
1134
char *fullname = NULL;
1135
ret = asprintf(&fullname, "%s/%s", tempdir,
1141
ret = unlink(fullname);
1143
fprintf(stderr, "unlink(\"%s\"): %s",
1144
fullname, strerror(errno));
1151
ret = rmdir(tempdir);
1152
if(ret == -1 and errno != ENOENT){
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){
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());