139
155
gnutls_dh_params_t dh_params;
140
156
const char *priority;
158
server *current_server;
142
159
} mandos_context;
144
161
/* global context so signal handler can reach it*/
145
162
mandos_context mc = { .simple_poll = NULL, .server = NULL,
146
163
.dh_bits = 1024, .priority = "SECURE256"
147
":!CTYPE-X.509:+CTYPE-OPENPGP" };
164
":!CTYPE-X.509:+CTYPE-OPENPGP",
165
.current_server = NULL };
149
167
sig_atomic_t quit_now = 0;
150
168
int signal_received = 0;
170
/* Function to use when printing errors */
171
void perror_plus(const char *print_text){
172
fprintf(stderr, "Mandos plugin %s: ",
173
program_invocation_short_name);
177
int fprintf_plus(FILE *stream, const char *format, ...){
179
va_start (ap, format);
181
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
182
program_invocation_short_name));
183
return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
153
187
* Make additional room in "buffer" for at least BUFFER_SIZE more
154
188
* bytes. "buffer_capacity" is how much is currently allocated,
155
189
* "buffer_length" is how much is already used.
157
191
size_t incbuffer(char **buffer, size_t buffer_length,
158
size_t buffer_capacity){
192
size_t buffer_capacity){
159
193
if(buffer_length + BUFFER_SIZE > buffer_capacity){
160
194
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
161
195
if(buffer == NULL){
166
200
return buffer_capacity;
203
/* Add server to set of servers to retry periodically */
204
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
207
server *new_server = malloc(sizeof(server));
208
if(new_server == NULL){
209
perror_plus("malloc");
212
*new_server = (server){ .ip = strdup(ip),
214
.if_index = if_index,
216
if(new_server->ip == NULL){
217
perror_plus("strdup");
220
/* Special case of first server */
221
if (mc.current_server == NULL){
222
new_server->next = new_server;
223
new_server->prev = new_server;
224
mc.current_server = new_server;
225
/* Place the new server last in the list */
227
new_server->next = mc.current_server;
228
new_server->prev = mc.current_server->prev;
229
new_server->prev->next = new_server;
230
mc.current_server->prev = new_server;
232
ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
234
perror_plus("clock_gettime");
170
241
* Initialize GPGME.
172
static bool init_gpgme(const char *seckey,
173
const char *pubkey, const char *tempdir){
243
static bool init_gpgme(const char *seckey, const char *pubkey,
244
const char *tempdir){
174
245
gpgme_error_t rc;
175
246
gpgme_engine_info_t engine_info;
186
257
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
192
263
rc = gpgme_data_new_from_fd(&pgp_data, fd);
193
264
if(rc != GPG_ERR_NO_ERROR){
194
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
195
gpgme_strsource(rc), gpgme_strerror(rc));
265
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
266
gpgme_strsource(rc), gpgme_strerror(rc));
199
270
rc = gpgme_op_import(mc.ctx, pgp_data);
200
271
if(rc != GPG_ERR_NO_ERROR){
201
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
202
gpgme_strsource(rc), gpgme_strerror(rc));
272
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
273
gpgme_strsource(rc), gpgme_strerror(rc));
206
277
ret = (int)TEMP_FAILURE_RETRY(close(fd));
279
perror_plus("close");
210
281
gpgme_data_release(pgp_data);
215
fprintf(stderr, "Initializing GPGME\n");
286
fprintf_plus(stderr, "Initializing GPGME\n");
219
290
gpgme_check_version(NULL);
220
291
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
221
292
if(rc != GPG_ERR_NO_ERROR){
222
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
223
gpgme_strsource(rc), gpgme_strerror(rc));
293
fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
294
gpgme_strsource(rc), gpgme_strerror(rc));
227
/* Set GPGME home directory for the OpenPGP engine only */
298
/* Set GPGME home directory for the OpenPGP engine only */
228
299
rc = gpgme_get_engine_info(&engine_info);
229
300
if(rc != GPG_ERR_NO_ERROR){
230
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
231
gpgme_strsource(rc), gpgme_strerror(rc));
301
fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
302
gpgme_strsource(rc), gpgme_strerror(rc));
234
305
while(engine_info != NULL){
273
346
ssize_t plaintext_length = 0;
276
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
349
fprintf_plus(stderr, "Trying to decrypt OpenPGP data\n");
279
352
/* Create new GPGME data buffer from memory cryptotext */
280
353
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
282
355
if(rc != GPG_ERR_NO_ERROR){
283
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
284
gpgme_strsource(rc), gpgme_strerror(rc));
356
fprintf_plus(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
357
gpgme_strsource(rc), gpgme_strerror(rc));
288
361
/* Create new empty GPGME data buffer for the plaintext */
289
362
rc = gpgme_data_new(&dh_plain);
290
363
if(rc != GPG_ERR_NO_ERROR){
291
fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
292
gpgme_strsource(rc), gpgme_strerror(rc));
364
fprintf_plus(stderr, "Mandos plugin mandos-client: "
365
"bad gpgme_data_new: %s: %s\n",
366
gpgme_strsource(rc), gpgme_strerror(rc));
293
367
gpgme_data_release(dh_crypto);
299
373
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
300
374
if(rc != GPG_ERR_NO_ERROR){
301
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
302
gpgme_strsource(rc), gpgme_strerror(rc));
375
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
376
gpgme_strsource(rc), gpgme_strerror(rc));
303
377
plaintext_length = -1;
305
379
gpgme_decrypt_result_t result;
306
380
result = gpgme_op_decrypt_result(mc.ctx);
307
381
if(result == NULL){
308
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
382
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
310
fprintf(stderr, "Unsupported algorithm: %s\n",
311
result->unsupported_algorithm);
312
fprintf(stderr, "Wrong key usage: %u\n",
313
result->wrong_key_usage);
384
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
385
result->unsupported_algorithm);
386
fprintf_plus(stderr, "Wrong key usage: %u\n",
387
result->wrong_key_usage);
314
388
if(result->file_name != NULL){
315
fprintf(stderr, "File name: %s\n", result->file_name);
389
fprintf_plus(stderr, "File name: %s\n", result->file_name);
317
391
gpgme_recipient_t recipient;
318
392
recipient = result->recipients;
319
393
while(recipient != NULL){
320
fprintf(stderr, "Public key algorithm: %s\n",
321
gpgme_pubkey_algo_name(recipient->pubkey_algo));
322
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
323
fprintf(stderr, "Secret key available: %s\n",
324
recipient->status == GPG_ERR_NO_SECKEY
394
fprintf_plus(stderr, "Public key algorithm: %s\n",
395
gpgme_pubkey_algo_name
396
(recipient->pubkey_algo));
397
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
398
fprintf_plus(stderr, "Secret key available: %s\n",
399
recipient->status == GPG_ERR_NO_SECKEY
326
401
recipient = recipient->next;
425
500
/* OpenPGP credentials */
426
gnutls_certificate_allocate_credentials(&mc.cred);
501
ret = gnutls_certificate_allocate_credentials(&mc.cred);
427
502
if(ret != GNUTLS_E_SUCCESS){
428
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
432
safer_gnutls_strerror(ret));
503
fprintf_plus(stderr, "GnuTLS memory error: %s\n",
504
safer_gnutls_strerror(ret));
433
505
gnutls_global_deinit();
438
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
439
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
510
fprintf_plus(stderr, "Attempting to use OpenPGP public key %s and"
511
" secret key %s as GnuTLS credentials\n",
443
516
ret = gnutls_certificate_set_openpgp_key_file
444
517
(mc.cred, pubkeyfilename, seckeyfilename,
445
518
GNUTLS_OPENPGP_FMT_BASE64);
446
519
if(ret != GNUTLS_E_SUCCESS){
448
"Error[%d] while reading the OpenPGP key pair ('%s',"
449
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
450
fprintf(stderr, "The GnuTLS error is: %s\n",
451
safer_gnutls_strerror(ret));
521
"Error[%d] while reading the OpenPGP key pair ('%s',"
522
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
523
fprintf_plus(stderr, "The GnuTLS error is: %s\n",
524
safer_gnutls_strerror(ret));
455
528
/* GnuTLS server initialization */
456
529
ret = gnutls_dh_params_init(&mc.dh_params);
457
530
if(ret != GNUTLS_E_SUCCESS){
458
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
459
" %s\n", safer_gnutls_strerror(ret));
531
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
532
" initialization: %s\n",
533
safer_gnutls_strerror(ret));
462
536
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
463
537
if(ret != GNUTLS_E_SUCCESS){
464
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
465
safer_gnutls_strerror(ret));
538
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
539
safer_gnutls_strerror(ret));
651
726
if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
652
727
char interface[IF_NAMESIZE];
653
728
if(if_indextoname((unsigned int)if_index, interface) == NULL){
654
perror("if_indextoname");
729
perror_plus("if_indextoname");
656
fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
657
ip, interface, port);
731
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
732
"\n", ip, interface, port);
660
fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
735
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
663
738
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
664
739
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
944
1020
char ip[AVAHI_ADDRESS_STR_MAX];
945
1021
avahi_address_snprint(ip, sizeof(ip), address);
947
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
948
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
949
ip, (intmax_t)interface, port);
1023
fprintf_plus(stderr, "Mandos server \"%s\" found on %s (%s, %"
1024
PRIdMAX ") on port %" PRIu16 "\n", name,
1025
host_name, ip, (intmax_t)interface, port);
951
1027
int ret = start_mandos_communication(ip, port, interface,
952
1028
avahi_proto_to_af(proto));
954
1030
avahi_simple_poll_quit(mc.simple_poll);
1032
ret = add_server(ip, port, interface,
1033
avahi_proto_to_af(proto));
1020
1101
signal_received = sig;
1021
1102
int old_errno = errno;
1103
/* set main loop to exit */
1022
1104
if(mc.simple_poll != NULL){
1023
1105
avahi_simple_poll_quit(mc.simple_poll);
1025
1107
errno = old_errno;
1029
* This function determines if a directory entry in /sys/class/net
1030
* corresponds to an acceptable network device.
1031
* (This function is passed to scandir(3) as a filter function.)
1033
int good_interface(const struct dirent *if_entry){
1035
char *flagname = NULL;
1036
if(if_entry->d_name[0] == '.'){
1039
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1045
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1052
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1053
/* read line from flags_fd */
1054
ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1055
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1056
flagstring[(size_t)to_read] = '\0';
1057
if(flagstring == NULL){
1063
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1080
tmpmax = strtoimax(flagstring, &tmp, 0);
1081
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1082
and not (isspace(*tmp)))
1083
or tmpmax != (ifreq_flags)tmpmax){
1110
bool get_flags(const char *ifname, struct ifreq *ifr){
1113
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1115
perror_plus("socket");
1118
strcpy(ifr->ifr_name, ifname);
1119
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1085
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1086
flagstring, if_entry->d_name);
1122
perror_plus("ioctl SIOCGIFFLAGS");
1092
ifreq_flags flags = (ifreq_flags)tmpmax;
1129
bool good_flags(const char *ifname, const struct ifreq *ifr){
1093
1131
/* Reject the loopback device */
1094
if(flags & IFF_LOOPBACK){
1132
if(ifr->ifr_flags & IFF_LOOPBACK){
1096
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1134
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n",
1101
1139
/* Accept point-to-point devices only if connect_to is specified */
1102
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1140
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1104
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1142
fprintf_plus(stderr, "Accepting point-to-point interface"
1143
" \"%s\"\n", ifname);
1109
1147
/* Otherwise, reject non-broadcast-capable devices */
1110
if(not (flags & IFF_BROADCAST)){
1148
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1112
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1150
fprintf_plus(stderr, "Rejecting non-broadcast interface"
1151
" \"%s\"\n", ifname);
1117
1155
/* Reject non-ARP interfaces (including dummy interfaces) */
1118
if(flags & IFF_NOARP){
1156
if(ifr->ifr_flags & IFF_NOARP){
1120
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1158
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n",
1125
1164
/* Accept this device */
1127
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1166
fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1172
* This function determines if a directory entry in /sys/class/net
1173
* corresponds to an acceptable network device.
1174
* (This function is passed to scandir(3) as a filter function.)
1176
int good_interface(const struct dirent *if_entry){
1177
if(if_entry->d_name[0] == '.'){
1182
if(not get_flags(if_entry->d_name, &ifr)){
1184
fprintf_plus(stderr, "Failed to get flags for interface "
1185
"\"%s\"\n", if_entry->d_name);
1190
if(not good_flags(if_entry->d_name, &ifr)){
1197
* This function determines if a directory entry in /sys/class/net
1198
* corresponds to an acceptable network device which is up.
1199
* (This function is passed to scandir(3) as a filter function.)
1201
int up_interface(const struct dirent *if_entry){
1202
if(if_entry->d_name[0] == '.'){
1207
if(not get_flags(if_entry->d_name, &ifr)){
1209
fprintf_plus(stderr, "Failed to get flags for interface "
1210
"\"%s\"\n", if_entry->d_name);
1215
/* Reject down interfaces */
1216
if(not (ifr.ifr_flags & IFF_UP)){
1218
fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1224
/* Reject non-running interfaces */
1225
if(not (ifr.ifr_flags & IFF_RUNNING)){
1227
fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1233
if(not good_flags(if_entry->d_name, &ifr)){
1250
/* Is this directory entry a runnable program? */
1251
int runnable_hook(const struct dirent *direntry){
1256
if((direntry->d_name)[0] == '\0'){
1261
sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1262
"abcdefghijklmnopqrstuvwxyz"
1265
if((direntry->d_name)[sret] != '\0'){
1266
/* Contains non-allowed characters */
1268
fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1274
char *fullname = NULL;
1275
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1277
perror_plus("asprintf");
1281
ret = stat(fullname, &st);
1284
perror_plus("Could not stat hook");
1288
if(not (S_ISREG(st.st_mode))){
1289
/* Not a regular file */
1291
fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1296
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1297
/* Not executable */
1299
fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1307
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1309
struct timespec now;
1310
struct timespec waited_time;
1311
intmax_t block_time;
1314
if(mc.current_server == NULL){
1316
fprintf_plus(stderr, "Wait until first server is found."
1319
ret = avahi_simple_poll_iterate(s, -1);
1322
fprintf_plus(stderr, "Check current_server if we should run"
1325
/* the current time */
1326
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1328
perror_plus("clock_gettime");
1331
/* Calculating in ms how long time between now and server
1332
who we visted longest time ago. Now - last seen. */
1333
waited_time.tv_sec = (now.tv_sec
1334
- mc.current_server->last_seen.tv_sec);
1335
waited_time.tv_nsec = (now.tv_nsec
1336
- mc.current_server->last_seen.tv_nsec);
1337
/* total time is 10s/10,000ms.
1338
Converting to s from ms by dividing by 1,000,
1339
and ns to ms by dividing by 1,000,000. */
1340
block_time = ((retry_interval
1341
- ((intmax_t)waited_time.tv_sec * 1000))
1342
- ((intmax_t)waited_time.tv_nsec / 1000000));
1345
fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1349
if(block_time <= 0){
1350
ret = start_mandos_communication(mc.current_server->ip,
1351
mc.current_server->port,
1352
mc.current_server->if_index,
1353
mc.current_server->af);
1355
avahi_simple_poll_quit(mc.simple_poll);
1358
ret = clock_gettime(CLOCK_MONOTONIC,
1359
&mc.current_server->last_seen);
1361
perror_plus("clock_gettime");
1364
mc.current_server = mc.current_server->next;
1365
block_time = 0; /* Call avahi to find new Mandos
1366
servers, but don't block */
1369
ret = avahi_simple_poll_iterate(s, (int)block_time);
1372
if (ret > 0 or errno != EINTR){
1373
return (ret != 1) ? ret : 0;
1379
bool run_network_hooks(const char *mode, const char *interface,
1381
struct dirent **direntries;
1382
struct dirent *direntry;
1384
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1387
perror_plus("scandir");
1389
int devnull = open("/dev/null", O_RDONLY);
1390
for(int i = 0; i < numhooks; i++){
1391
direntry = direntries[i];
1392
char *fullname = NULL;
1393
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1395
perror_plus("asprintf");
1399
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1402
pid_t hook_pid = fork();
1405
dup2(devnull, STDIN_FILENO);
1407
dup2(STDERR_FILENO, STDOUT_FILENO);
1408
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1410
perror_plus("setenv");
1413
ret = setenv("DEVICE", interface, 1);
1415
perror_plus("setenv");
1418
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1420
perror_plus("setenv");
1423
ret = setenv("MODE", mode, 1);
1425
perror_plus("setenv");
1429
ret = asprintf(&delaystring, "%f", delay);
1431
perror_plus("asprintf");
1434
ret = setenv("DELAY", delaystring, 1);
1437
perror_plus("setenv");
1441
ret = execl(fullname, direntry->d_name, mode, NULL);
1442
perror_plus("execl");
1445
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1446
perror_plus("waitpid");
1450
if(WIFEXITED(status)){
1451
if(WEXITSTATUS(status) != 0){
1452
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1453
" with status %d\n", direntry->d_name,
1454
WEXITSTATUS(status));
1458
} else if(WIFSIGNALED(status)){
1459
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1460
" signal %d\n", direntry->d_name,
1465
fprintf_plus(stderr, "Warning: network hook \"%s\""
1466
" crashed\n", direntry->d_name);
1144
1481
int main(int argc, char *argv[]){
1145
1482
AvahiSServiceBrowser *sb = NULL;
1677
/* Work around Debian bug #633582:
1678
<http://bugs.debian.org/633582> */
1681
/* Re-raise priviliges */
1685
perror_plus("seteuid");
1688
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1689
int seckey_fd = open(seckey, O_RDONLY);
1690
if(seckey_fd == -1){
1691
perror_plus("open");
1693
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1695
perror_plus("fstat");
1697
if(S_ISREG(st.st_mode)
1698
and st.st_uid == 0 and st.st_gid == 0){
1699
ret = fchown(seckey_fd, uid, gid);
1701
perror_plus("fchown");
1705
TEMP_FAILURE_RETRY(close(seckey_fd));
1709
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1710
int pubkey_fd = open(pubkey, O_RDONLY);
1711
if(pubkey_fd == -1){
1712
perror_plus("open");
1714
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1716
perror_plus("fstat");
1718
if(S_ISREG(st.st_mode)
1719
and st.st_uid == 0 and st.st_gid == 0){
1720
ret = fchown(pubkey_fd, uid, gid);
1722
perror_plus("fchown");
1726
TEMP_FAILURE_RETRY(close(pubkey_fd));
1730
/* Lower privileges */
1734
perror_plus("seteuid");
1738
/* Run network hooks */
1741
/* Re-raise priviliges */
1745
perror_plus("seteuid");
1748
if(not run_network_hooks("start", interface, delay)){
1752
/* Lower privileges */
1756
perror_plus("seteuid");
1317
1762
avahi_set_log_function(empty_log);
1320
1765
if(interface[0] == '\0'){
1321
1766
struct dirent **direntries;
1322
ret = scandir(sys_class_net, &direntries, good_interface,
1767
/* First look for interfaces that are up */
1768
ret = scandir(sys_class_net, &direntries, up_interface,
1771
/* No up interfaces, look for any good interfaces */
1773
ret = scandir(sys_class_net, &direntries, good_interface,
1325
/* Pick the first good interface */
1777
/* Pick the first interface returned */
1326
1778
interface = strdup(direntries[0]->d_name);
1328
fprintf(stderr, "Using interface \"%s\"\n", interface);
1780
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1330
1782
if(interface == NULL){
1783
perror_plus("malloc");
1332
1784
free(direntries);
1333
1785
exitcode = EXIT_FAILURE;
1379
1832
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1381
perror("sigaction");
1834
perror_plus("sigaction");
1382
1835
return EX_OSERR;
1384
1837
if(old_sigterm_action.sa_handler != SIG_IGN){
1385
1838
ret = sigaction(SIGINT, &sigterm_action, NULL);
1387
perror("sigaction");
1840
perror_plus("sigaction");
1388
1841
exitcode = EX_OSERR;
1392
1845
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1394
perror("sigaction");
1847
perror_plus("sigaction");
1395
1848
return EX_OSERR;
1397
1850
if(old_sigterm_action.sa_handler != SIG_IGN){
1398
1851
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1400
perror("sigaction");
1853
perror_plus("sigaction");
1401
1854
exitcode = EX_OSERR;
1405
1858
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1407
perror("sigaction");
1860
perror_plus("sigaction");
1408
1861
return EX_OSERR;
1410
1863
if(old_sigterm_action.sa_handler != SIG_IGN){
1411
1864
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1413
perror("sigaction");
1866
perror_plus("sigaction");
1414
1867
exitcode = EX_OSERR;
1739
2200
if(gpgme_initialized){
1740
2201
gpgme_release(mc.ctx);
2204
/* Cleans up the circular linked list of Mandos servers the client
2206
if(mc.current_server != NULL){
2207
mc.current_server->prev->next = NULL;
2208
while(mc.current_server != NULL){
2209
server *next = mc.current_server->next;
2210
free(mc.current_server);
2211
mc.current_server = next;
1743
/* Take down the network interface */
1744
if(take_down_interface){
1745
/* Re-raise priviliges */
2215
/* Re-raise priviliges */
2221
perror_plus("seteuid");
2224
/* Run network hooks */
2225
if(not run_network_hooks("stop", interface, delay)){
2229
/* Take down the network interface */
2230
if(take_down_interface and geteuid() == 0){
1752
2231
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1754
perror("ioctl SIOCGIFFLAGS");
1755
} else if(network.ifr_flags & IFF_UP) {
2233
perror_plus("ioctl SIOCGIFFLAGS");
2234
} else if(network.ifr_flags & IFF_UP){
1756
2235
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1757
2236
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1759
perror("ioctl SIOCSIFFLAGS -IFF_UP");
2238
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1762
2241
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1766
/* Lower privileges permanently */
2243
perror_plus("close");
2248
/* Lower privileges permanently */
2252
perror_plus("setuid");
1776
2257
if(tempdir_created){
1777
2258
struct dirent **direntries = NULL;
1778
2259
struct dirent *direntry = NULL;
1779
ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1781
for(int i = 0; i < ret; i++){
2260
int numentries = scandir(tempdir, &direntries, notdotentries,
2262
if (numentries > 0){
2263
for(int i = 0; i < numentries; i++){
1782
2264
direntry = direntries[i];
1783
2265
char *fullname = NULL;
1784
2266
ret = asprintf(&fullname, "%s/%s", tempdir,
1785
2267
direntry->d_name);
2269
perror_plus("asprintf");
1790
2272
ret = remove(fullname);
1792
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2274
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
1795
2277
free(fullname);
1799
/* need to be cleaned even if ret == 0 because man page dont specify */
2281
/* need to clean even if 0 because man page doesn't specify */
1800
2282
free(direntries);
2283
if (numentries == -1){
2284
perror_plus("scandir");
1804
2286
ret = rmdir(tempdir);
1805
2287
if(ret == -1 and errno != ENOENT){
2288
perror_plus("rmdir");