/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2014-06-14 02:55:24 UTC
  • mto: (237.7.272 trunk)
  • mto: This revision was merged to the branch mainline in revision 317.
  • Revision ID: teddy@recompile.se-20140614025524-k03150tjp4t89w1d
plugin-runner: Restore resources correctly if fork() fails.

* plugin-runner.c (main): If fork() fails, close pipe FD's and restore
                          signal mask for SIGCHLD.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2013 Teddy Hogeborn
13
 
 * Copyright © 2008-2013 Björn Påhlsson
 
12
 * Copyright © 2008-2014 Teddy Hogeborn
 
13
 * Copyright © 2008-2014 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
33
33
#ifndef _LARGEFILE_SOURCE
34
34
#define _LARGEFILE_SOURCE
35
 
#endif
 
35
#endif  /* not _LARGEFILE_SOURCE */
36
36
#ifndef _FILE_OFFSET_BITS
37
37
#define _FILE_OFFSET_BITS 64
38
 
#endif
 
38
#endif  /* not _FILE_OFFSET_BITS */
39
39
 
40
40
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
41
41
 
42
42
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
 
                                   stdout, ferror(), remove() */
 
43
                                   stdout, ferror() */
44
44
#include <stdint.h>             /* uint16_t, uint32_t, intptr_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
55
55
                                   opendir(), DIR */
56
56
#include <sys/stat.h>           /* open(), S_ISREG */
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
 
                                   inet_pton(), connect() */
59
 
#include <fcntl.h>              /* open() */
 
58
                                   inet_pton(), connect(),
 
59
                                   getnameinfo() */
 
60
#include <fcntl.h>              /* open(), unlinkat() */
60
61
#include <dirent.h>             /* opendir(), struct dirent, readdir()
61
62
                                 */
62
63
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
72
73
                                */
73
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
75
                                   getuid(), getgid(), seteuid(),
75
 
                                   setgid(), pause(), _exit() */
76
 
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
 
76
                                   setgid(), pause(), _exit(),
 
77
                                   unlinkat() */
 
78
#include <arpa/inet.h>          /* inet_pton(), htons() */
77
79
#include <iso646.h>             /* not, or, and */
78
80
#include <argp.h>               /* struct argp_option, error_t, struct
79
81
                                   argp_state, struct argp,
91
93
                                   argz_delete(), argz_append(),
92
94
                                   argz_stringify(), argz_add(),
93
95
                                   argz_count() */
 
96
#include <netdb.h>              /* getnameinfo(), NI_NUMERICHOST,
 
97
                                   EAI_SYSTEM, gai_strerror() */
94
98
 
95
99
#ifdef __linux__
96
100
#include <sys/klog.h>           /* klogctl() */
138
142
static const char sys_class_net[] = "/sys/class/net";
139
143
char *connect_to = NULL;
140
144
const char *hookdir = HOOKDIR;
 
145
int hookdir_fd = -1;
141
146
uid_t uid = 65534;
142
147
gid_t gid = 65534;
143
148
 
180
185
  perror(print_text);
181
186
}
182
187
 
183
 
__attribute__((format (gnu_printf, 2, 3)))
 
188
__attribute__((format (gnu_printf, 2, 3), nonnull))
184
189
int fprintf_plus(FILE *stream, const char *format, ...){
185
190
  va_list ap;
186
191
  va_start (ap, format);
195
200
 * bytes. "buffer_capacity" is how much is currently allocated,
196
201
 * "buffer_length" is how much is already used.
197
202
 */
 
203
__attribute__((nonnull, warn_unused_result))
198
204
size_t incbuffer(char **buffer, size_t buffer_length,
199
205
                 size_t buffer_capacity){
200
206
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
213
219
}
214
220
 
215
221
/* Add server to set of servers to retry periodically */
 
222
__attribute__((nonnull, warn_unused_result))
216
223
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
217
224
                int af, server **current_server){
218
225
  int ret;
229
236
    perror_plus("strdup");
230
237
    return false;
231
238
  }
 
239
  ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
 
240
  if(ret == -1){
 
241
    perror_plus("clock_gettime");
 
242
    return false;
 
243
  }
232
244
  /* Special case of first server */
233
245
  if(*current_server == NULL){
234
246
    new_server->next = new_server;
235
247
    new_server->prev = new_server;
236
248
    *current_server = new_server;
237
 
  /* Place the new server last in the list */
238
249
  } else {
 
250
    /* Place the new server last in the list */
239
251
    new_server->next = *current_server;
240
252
    new_server->prev = (*current_server)->prev;
241
253
    new_server->prev->next = new_server;
242
254
    (*current_server)->prev = new_server;
243
255
  }
244
 
  ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
245
 
  if(ret == -1){
246
 
    perror_plus("clock_gettime");
247
 
    return false;
248
 
  }
249
256
  return true;
250
257
}
251
258
 
252
259
/* 
253
260
 * Initialize GPGME.
254
261
 */
255
 
static bool init_gpgme(const char *seckey, const char *pubkey,
256
 
                       const char *tempdir, mandos_context *mc){
 
262
__attribute__((nonnull, warn_unused_result))
 
263
static bool init_gpgme(const char * const seckey,
 
264
                       const char * const pubkey,
 
265
                       const char * const tempdir,
 
266
                       mandos_context *mc){
257
267
  gpgme_error_t rc;
258
268
  gpgme_engine_info_t engine_info;
259
269
  
260
270
  /*
261
271
   * Helper function to insert pub and seckey to the engine keyring.
262
272
   */
263
 
  bool import_key(const char *filename){
 
273
  bool import_key(const char * const filename){
264
274
    int ret;
265
275
    int fd;
266
276
    gpgme_data_t pgp_data;
347
357
 * Decrypt OpenPGP data.
348
358
 * Returns -1 on error
349
359
 */
 
360
__attribute__((nonnull, warn_unused_result))
350
361
static ssize_t pgp_packet_decrypt(const char *cryptotext,
351
362
                                  size_t crypto_size,
352
363
                                  char **plaintext,
472
483
  return plaintext_length;
473
484
}
474
485
 
475
 
static const char * safer_gnutls_strerror(int value){
 
486
__attribute__((warn_unused_result))
 
487
static const char *safer_gnutls_strerror(int value){
476
488
  const char *ret = gnutls_strerror(value);
477
489
  if(ret == NULL)
478
490
    ret = "(unknown)";
480
492
}
481
493
 
482
494
/* GnuTLS log function callback */
 
495
__attribute__((nonnull))
483
496
static void debuggnutls(__attribute__((unused)) int level,
484
497
                        const char* string){
485
498
  fprintf_plus(stderr, "GnuTLS: %s", string);
486
499
}
487
500
 
 
501
__attribute__((nonnull, warn_unused_result))
488
502
static int init_gnutls_global(const char *pubkeyfilename,
489
503
                              const char *seckeyfilename,
490
504
                              mandos_context *mc){
564
578
  return -1;
565
579
}
566
580
 
 
581
__attribute__((nonnull, warn_unused_result))
567
582
static int init_gnutls_session(gnutls_session_t *session,
568
583
                               mandos_context *mc){
569
584
  int ret;
626
641
                      __attribute__((unused)) const char *txt){}
627
642
 
628
643
/* Called when a Mandos server is found */
 
644
__attribute__((nonnull, warn_unused_result))
629
645
static int start_mandos_communication(const char *ip, in_port_t port,
630
646
                                      AvahiIfIndex if_index,
631
647
                                      int af, mandos_context *mc){
632
648
  int ret, tcp_sd = -1;
633
649
  ssize_t sret;
634
 
  union {
635
 
    struct sockaddr_in in;
636
 
    struct sockaddr_in6 in6;
637
 
  } to;
 
650
  struct sockaddr_storage to;
638
651
  char *buffer = NULL;
639
652
  char *decrypted_buffer = NULL;
640
653
  size_t buffer_length = 0;
721
734
  
722
735
  memset(&to, 0, sizeof(to));
723
736
  if(af == AF_INET6){
724
 
    to.in6.sin6_family = (sa_family_t)af;
725
 
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
 
737
    ((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
 
738
    ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
726
739
  } else {                      /* IPv4 */
727
 
    to.in.sin_family = (sa_family_t)af;
728
 
    ret = inet_pton(af, ip, &to.in.sin_addr);
 
740
    ((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
 
741
    ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
729
742
  }
730
743
  if(ret < 0 ){
731
744
    int e = errno;
740
753
    goto mandos_end;
741
754
  }
742
755
  if(af == AF_INET6){
743
 
    to.in6.sin6_port = htons(port);    
744
 
#ifdef __GNUC__
745
 
#pragma GCC diagnostic push
746
 
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
747
 
#endif
748
 
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
749
 
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower */
750
 
#ifdef __GNUC__
751
 
#pragma GCC diagnostic pop
752
 
#endif
 
756
    ((struct sockaddr_in6 *)&to)->sin6_port = htons(port);
 
757
    if(IN6_IS_ADDR_LINKLOCAL
 
758
       (&((struct sockaddr_in6 *)&to)->sin6_addr)){
753
759
      if(if_index == AVAHI_IF_UNSPEC){
754
760
        fprintf_plus(stderr, "An IPv6 link-local address is"
755
761
                     " incomplete without a network interface\n");
757
763
        goto mandos_end;
758
764
      }
759
765
      /* Set the network interface number as scope */
760
 
      to.in6.sin6_scope_id = (uint32_t)if_index;
 
766
      ((struct sockaddr_in6 *)&to)->sin6_scope_id = (uint32_t)if_index;
761
767
    }
762
768
  } else {
763
 
    to.in.sin_port = htons(port);
 
769
    ((struct sockaddr_in *)&to)->sin_port = htons(port);
764
770
  }
765
771
  
766
772
  if(quit_now){
783
789
    }
784
790
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
785
791
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
786
 
    const char *pcret;
787
792
    if(af == AF_INET6){
788
 
      pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
789
 
                        sizeof(addrstr));
 
793
      ret = getnameinfo((struct sockaddr *)&to,
 
794
                        sizeof(struct sockaddr_in6),
 
795
                        addrstr, sizeof(addrstr), NULL, 0,
 
796
                        NI_NUMERICHOST);
790
797
    } else {
791
 
      pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
792
 
                        sizeof(addrstr));
 
798
      ret = getnameinfo((struct sockaddr *)&to,
 
799
                        sizeof(struct sockaddr_in),
 
800
                        addrstr, sizeof(addrstr), NULL, 0,
 
801
                        NI_NUMERICHOST);
793
802
    }
794
 
    if(pcret == NULL){
795
 
      perror_plus("inet_ntop");
796
 
    } else {
797
 
      if(strcmp(addrstr, ip) != 0){
798
 
        fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
799
 
      }
 
803
    if(ret == EAI_SYSTEM){
 
804
      perror_plus("getnameinfo");
 
805
    } else if(ret != 0) {
 
806
      fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
 
807
    } else if(strcmp(addrstr, ip) != 0){
 
808
      fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
800
809
    }
801
810
  }
802
811
  
806
815
  }
807
816
  
808
817
  if(af == AF_INET6){
809
 
    ret = connect(tcp_sd, &to.in6, sizeof(to));
 
818
    ret = connect(tcp_sd, (struct sockaddr *)&to,
 
819
                  sizeof(struct sockaddr_in6));
810
820
  } else {
811
 
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
 
821
    ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
 
822
                  sizeof(struct sockaddr_in));
812
823
  }
813
824
  if(ret < 0){
814
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
825
    if((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
815
826
      int e = errno;
816
827
      perror_plus("connect");
817
828
      errno = e;
1032
1043
  return retval;
1033
1044
}
1034
1045
 
 
1046
__attribute__((nonnull))
1035
1047
static void resolve_callback(AvahiSServiceResolver *r,
1036
1048
                             AvahiIfIndex interface,
1037
1049
                             AvahiProtocol proto,
1045
1057
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
1046
1058
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1047
1059
                             flags,
1048
 
                             void* mc){
 
1060
                             void *mc){
1049
1061
  if(r == NULL){
1050
1062
    return;
1051
1063
  }
1104
1116
                            const char *domain,
1105
1117
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1106
1118
                            flags,
1107
 
                            void* mc){
 
1119
                            void *mc){
1108
1120
  if(b == NULL){
1109
1121
    return;
1110
1122
  }
1170
1182
  errno = old_errno;
1171
1183
}
1172
1184
 
 
1185
__attribute__((nonnull, warn_unused_result))
1173
1186
bool get_flags(const char *ifname, struct ifreq *ifr){
1174
1187
  int ret;
1175
1188
  error_t ret_errno;
1194
1207
  return true;
1195
1208
}
1196
1209
 
 
1210
__attribute__((nonnull, warn_unused_result))
1197
1211
bool good_flags(const char *ifname, const struct ifreq *ifr){
1198
1212
  
1199
1213
  /* Reject the loopback device */
1241
1255
 * corresponds to an acceptable network device.
1242
1256
 * (This function is passed to scandir(3) as a filter function.)
1243
1257
 */
 
1258
__attribute__((nonnull, warn_unused_result))
1244
1259
int good_interface(const struct dirent *if_entry){
1245
1260
  if(if_entry->d_name[0] == '.'){
1246
1261
    return 0;
1264
1279
/* 
1265
1280
 * This function determines if a network interface is up.
1266
1281
 */
 
1282
__attribute__((nonnull, warn_unused_result))
1267
1283
bool interface_is_up(const char *interface){
1268
1284
  struct ifreq ifr;
1269
1285
  if(not get_flags(interface, &ifr)){
1280
1296
/* 
1281
1297
 * This function determines if a network interface is running
1282
1298
 */
 
1299
__attribute__((nonnull, warn_unused_result))
1283
1300
bool interface_is_running(const char *interface){
1284
1301
  struct ifreq ifr;
1285
1302
  if(not get_flags(interface, &ifr)){
1293
1310
  return (bool)(ifr.ifr_flags & IFF_RUNNING);
1294
1311
}
1295
1312
 
 
1313
__attribute__((nonnull, pure, warn_unused_result))
1296
1314
int notdotentries(const struct dirent *direntry){
1297
1315
  /* Skip "." and ".." */
1298
1316
  if(direntry->d_name[0] == '.'
1305
1323
}
1306
1324
 
1307
1325
/* Is this directory entry a runnable program? */
 
1326
__attribute__((nonnull, warn_unused_result))
1308
1327
int runnable_hook(const struct dirent *direntry){
1309
1328
  int ret;
1310
1329
  size_t sret;
1318
1337
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1319
1338
                "abcdefghijklmnopqrstuvwxyz"
1320
1339
                "0123456789"
1321
 
                "_-");
 
1340
                "_.-");
1322
1341
  if((direntry->d_name)[sret] != '\0'){
1323
1342
    /* Contains non-allowed characters */
1324
1343
    if(debug){
1328
1347
    return 0;
1329
1348
  }
1330
1349
  
1331
 
  char *fullname = NULL;
1332
 
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1333
 
  if(ret < 0){
1334
 
    perror_plus("asprintf");
1335
 
    return 0;
1336
 
  }
1337
 
  
1338
 
  ret = stat(fullname, &st);
 
1350
  ret = fstatat(hookdir_fd, direntry->d_name, &st, 0);
1339
1351
  if(ret == -1){
1340
1352
    if(debug){
1341
1353
      perror_plus("Could not stat hook");
1365
1377
  return 1;
1366
1378
}
1367
1379
 
 
1380
__attribute__((nonnull, warn_unused_result))
1368
1381
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1369
1382
                            mandos_context *mc){
1370
1383
  int ret;
1374
1387
  
1375
1388
  while(true){
1376
1389
    if(mc->current_server == NULL){
1377
 
      if (debug){
 
1390
      if(debug){
1378
1391
        fprintf_plus(stderr, "Wait until first server is found."
1379
1392
                     " No timeout!\n");
1380
1393
      }
1381
1394
      ret = avahi_simple_poll_iterate(s, -1);
1382
1395
    } else {
1383
 
      if (debug){
 
1396
      if(debug){
1384
1397
        fprintf_plus(stderr, "Check current_server if we should run"
1385
1398
                     " it, or wait\n");
1386
1399
      }
1403
1416
                     - ((intmax_t)waited_time.tv_sec * 1000))
1404
1417
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1405
1418
      
1406
 
      if (debug){
 
1419
      if(debug){
1407
1420
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1408
1421
                     block_time);
1409
1422
      }
1431
1444
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1432
1445
    }
1433
1446
    if(ret != 0){
1434
 
      if (ret > 0 or errno != EINTR){
 
1447
      if(ret > 0 or errno != EINTR){
1435
1448
        return (ret != 1) ? ret : 0;
1436
1449
      }
1437
1450
    }
1439
1452
}
1440
1453
 
1441
1454
/* Set effective uid to 0, return errno */
 
1455
__attribute__((warn_unused_result))
1442
1456
error_t raise_privileges(void){
1443
1457
  error_t old_errno = errno;
1444
1458
  error_t ret_errno = 0;
1451
1465
}
1452
1466
 
1453
1467
/* Set effective and real user ID to 0.  Return errno. */
 
1468
__attribute__((warn_unused_result))
1454
1469
error_t raise_privileges_permanently(void){
1455
1470
  error_t old_errno = errno;
1456
1471
  error_t ret_errno = raise_privileges();
1467
1482
}
1468
1483
 
1469
1484
/* Set effective user ID to unprivileged saved user ID */
 
1485
__attribute__((warn_unused_result))
1470
1486
error_t lower_privileges(void){
1471
1487
  error_t old_errno = errno;
1472
1488
  error_t ret_errno = 0;
1479
1495
}
1480
1496
 
1481
1497
/* Lower privileges permanently */
 
1498
__attribute__((warn_unused_result))
1482
1499
error_t lower_privileges_permanently(void){
1483
1500
  error_t old_errno = errno;
1484
1501
  error_t ret_errno = 0;
1490
1507
  return ret_errno;
1491
1508
}
1492
1509
 
1493
 
bool run_network_hooks(const char *mode, const char *interface,
 
1510
__attribute__((nonnull))
 
1511
void run_network_hooks(const char *mode, const char *interface,
1494
1512
                       const float delay){
1495
1513
  struct dirent **direntries;
1496
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1497
 
                         alphasort);
 
1514
  if(hookdir_fd == -1){
 
1515
    hookdir_fd = open(hookdir, O_RDONLY);
 
1516
    if(hookdir_fd == -1){
 
1517
      if(errno == ENOENT){
 
1518
        if(debug){
 
1519
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1520
                       " found\n", hookdir);
 
1521
        }
 
1522
      } else {
 
1523
        perror_plus("open");
 
1524
      }
 
1525
      return;
 
1526
    }
 
1527
  }
 
1528
#ifdef __GLIBC__
 
1529
#if __GLIBC_PREREQ(2, 15)
 
1530
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
 
1531
                           runnable_hook, alphasort);
 
1532
#else  /* not __GLIBC_PREREQ(2, 15) */
 
1533
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1534
                         alphasort);
 
1535
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
1536
#else   /* not __GLIBC__ */
 
1537
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1538
                         alphasort);
 
1539
#endif  /* not __GLIBC__ */
1498
1540
  if(numhooks == -1){
1499
 
    if(errno == ENOENT){
1500
 
      if(debug){
1501
 
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
1502
 
                     " found\n", hookdir);
1503
 
      }
1504
 
    } else {
1505
 
      perror_plus("scandir");
 
1541
    perror_plus("scandir");
 
1542
    return;
 
1543
  }
 
1544
  struct dirent *direntry;
 
1545
  int ret;
 
1546
  int devnull = open("/dev/null", O_RDONLY);
 
1547
  for(int i = 0; i < numhooks; i++){
 
1548
    direntry = direntries[i];
 
1549
    if(debug){
 
1550
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1551
                   direntry->d_name);
1506
1552
    }
1507
 
  } else {
1508
 
    struct dirent *direntry;
1509
 
    int ret;
1510
 
    int devnull = open("/dev/null", O_RDONLY);
1511
 
    for(int i = 0; i < numhooks; i++){
1512
 
      direntry = direntries[i];
1513
 
      char *fullname = NULL;
1514
 
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1515
 
      if(ret < 0){
 
1553
    pid_t hook_pid = fork();
 
1554
    if(hook_pid == 0){
 
1555
      /* Child */
 
1556
      /* Raise privileges */
 
1557
      if(raise_privileges_permanently() != 0){
 
1558
        perror_plus("Failed to raise privileges");
 
1559
        _exit(EX_NOPERM);
 
1560
      }
 
1561
      /* Set group */
 
1562
      errno = 0;
 
1563
      ret = setgid(0);
 
1564
      if(ret == -1){
 
1565
        perror_plus("setgid");
 
1566
        _exit(EX_NOPERM);
 
1567
      }
 
1568
      /* Reset supplementary groups */
 
1569
      errno = 0;
 
1570
      ret = setgroups(0, NULL);
 
1571
      if(ret == -1){
 
1572
        perror_plus("setgroups");
 
1573
        _exit(EX_NOPERM);
 
1574
      }
 
1575
      ret = dup2(devnull, STDIN_FILENO);
 
1576
      if(ret == -1){
 
1577
        perror_plus("dup2(devnull, STDIN_FILENO)");
 
1578
        _exit(EX_OSERR);
 
1579
      }
 
1580
      ret = close(devnull);
 
1581
      if(ret == -1){
 
1582
        perror_plus("close");
 
1583
        _exit(EX_OSERR);
 
1584
      }
 
1585
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
1586
      if(ret == -1){
 
1587
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
1588
        _exit(EX_OSERR);
 
1589
      }
 
1590
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1591
      if(ret == -1){
 
1592
        perror_plus("setenv");
 
1593
        _exit(EX_OSERR);
 
1594
      }
 
1595
      ret = setenv("DEVICE", interface, 1);
 
1596
      if(ret == -1){
 
1597
        perror_plus("setenv");
 
1598
        _exit(EX_OSERR);
 
1599
      }
 
1600
      ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1601
      if(ret == -1){
 
1602
        perror_plus("setenv");
 
1603
        _exit(EX_OSERR);
 
1604
      }
 
1605
      ret = setenv("MODE", mode, 1);
 
1606
      if(ret == -1){
 
1607
        perror_plus("setenv");
 
1608
        _exit(EX_OSERR);
 
1609
      }
 
1610
      char *delaystring;
 
1611
      ret = asprintf(&delaystring, "%f", (double)delay);
 
1612
      if(ret == -1){
1516
1613
        perror_plus("asprintf");
1517
 
        continue;
1518
 
      }
1519
 
      if(debug){
1520
 
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
1521
 
                     direntry->d_name);
1522
 
      }
1523
 
      pid_t hook_pid = fork();
1524
 
      if(hook_pid == 0){
1525
 
        /* Child */
1526
 
        /* Raise privileges */
1527
 
        raise_privileges_permanently();
1528
 
        /* Set group */
1529
 
        errno = 0;
1530
 
        ret = setgid(0);
1531
 
        if(ret == -1){
1532
 
          perror_plus("setgid");
1533
 
        }
1534
 
        /* Reset supplementary groups */
1535
 
        errno = 0;
1536
 
        ret = setgroups(0, NULL);
1537
 
        if(ret == -1){
1538
 
          perror_plus("setgroups");
1539
 
        }
1540
 
        dup2(devnull, STDIN_FILENO);
1541
 
        close(devnull);
1542
 
        dup2(STDERR_FILENO, STDOUT_FILENO);
1543
 
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1544
 
        if(ret == -1){
1545
 
          perror_plus("setenv");
1546
 
          _exit(EX_OSERR);
1547
 
        }
1548
 
        ret = setenv("DEVICE", interface, 1);
1549
 
        if(ret == -1){
1550
 
          perror_plus("setenv");
1551
 
          _exit(EX_OSERR);
1552
 
        }
1553
 
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1554
 
        if(ret == -1){
1555
 
          perror_plus("setenv");
1556
 
          _exit(EX_OSERR);
1557
 
        }
1558
 
        ret = setenv("MODE", mode, 1);
1559
 
        if(ret == -1){
1560
 
          perror_plus("setenv");
1561
 
          _exit(EX_OSERR);
1562
 
        }
1563
 
        char *delaystring;
1564
 
        ret = asprintf(&delaystring, "%f", delay);
1565
 
        if(ret == -1){
1566
 
          perror_plus("asprintf");
1567
 
          _exit(EX_OSERR);
1568
 
        }
1569
 
        ret = setenv("DELAY", delaystring, 1);
1570
 
        if(ret == -1){
1571
 
          free(delaystring);
1572
 
          perror_plus("setenv");
1573
 
          _exit(EX_OSERR);
1574
 
        }
 
1614
        _exit(EX_OSERR);
 
1615
      }
 
1616
      ret = setenv("DELAY", delaystring, 1);
 
1617
      if(ret == -1){
1575
1618
        free(delaystring);
1576
 
        if(connect_to != NULL){
1577
 
          ret = setenv("CONNECT", connect_to, 1);
1578
 
          if(ret == -1){
1579
 
            perror_plus("setenv");
1580
 
            _exit(EX_OSERR);
1581
 
          }
1582
 
        }
1583
 
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1584
 
          perror_plus("execl");
1585
 
          _exit(EXIT_FAILURE);
1586
 
        }
 
1619
        perror_plus("setenv");
 
1620
        _exit(EX_OSERR);
 
1621
      }
 
1622
      free(delaystring);
 
1623
      if(connect_to != NULL){
 
1624
        ret = setenv("CONNECT", connect_to, 1);
 
1625
        if(ret == -1){
 
1626
          perror_plus("setenv");
 
1627
          _exit(EX_OSERR);
 
1628
        }
 
1629
      }
 
1630
      int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
 
1631
      if(hook_fd == -1){
 
1632
        perror_plus("openat");
 
1633
        _exit(EXIT_FAILURE);
 
1634
      }
 
1635
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
1636
        perror_plus("close");
 
1637
        _exit(EXIT_FAILURE);
 
1638
      }
 
1639
      if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
 
1640
                 environ) == -1){
 
1641
        perror_plus("fexecve");
 
1642
        _exit(EXIT_FAILURE);
 
1643
      }
 
1644
    } else {
 
1645
      int status;
 
1646
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1647
        perror_plus("waitpid");
 
1648
        continue;
 
1649
      }
 
1650
      if(WIFEXITED(status)){
 
1651
        if(WEXITSTATUS(status) != 0){
 
1652
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1653
                       " with status %d\n", direntry->d_name,
 
1654
                       WEXITSTATUS(status));
 
1655
          continue;
 
1656
        }
 
1657
      } else if(WIFSIGNALED(status)){
 
1658
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1659
                     " signal %d\n", direntry->d_name,
 
1660
                     WTERMSIG(status));
 
1661
        continue;
1587
1662
      } else {
1588
 
        int status;
1589
 
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1590
 
          perror_plus("waitpid");
1591
 
          free(fullname);
1592
 
          continue;
1593
 
        }
1594
 
        if(WIFEXITED(status)){
1595
 
          if(WEXITSTATUS(status) != 0){
1596
 
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1597
 
                         " with status %d\n", direntry->d_name,
1598
 
                         WEXITSTATUS(status));
1599
 
            free(fullname);
1600
 
            continue;
1601
 
          }
1602
 
        } else if(WIFSIGNALED(status)){
1603
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1604
 
                       " signal %d\n", direntry->d_name,
1605
 
                       WTERMSIG(status));
1606
 
          free(fullname);
1607
 
          continue;
1608
 
        } else {
1609
 
          fprintf_plus(stderr, "Warning: network hook \"%s\""
1610
 
                       " crashed\n", direntry->d_name);
1611
 
          free(fullname);
1612
 
          continue;
1613
 
        }
1614
 
      }
1615
 
      free(fullname);
1616
 
      if(debug){
1617
 
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1618
 
                     direntry->d_name);
1619
 
      }
1620
 
    }
1621
 
    close(devnull);
1622
 
  }
1623
 
  return true;
 
1663
        fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1664
                     " crashed\n", direntry->d_name);
 
1665
        continue;
 
1666
      }
 
1667
    }
 
1668
    if(debug){
 
1669
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1670
                   direntry->d_name);
 
1671
    }
 
1672
  }
 
1673
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
1674
    perror_plus("close");
 
1675
  } else {
 
1676
    hookdir_fd = -1;
 
1677
  }
 
1678
  close(devnull);
1624
1679
}
1625
1680
 
 
1681
__attribute__((nonnull, warn_unused_result))
1626
1682
error_t bring_up_interface(const char *const interface,
1627
1683
                           const float delay){
1628
 
  int sd = -1;
1629
1684
  error_t old_errno = errno;
1630
 
  error_t ret_errno = 0;
1631
 
  int ret, ret_setflags;
 
1685
  int ret;
1632
1686
  struct ifreq network;
1633
1687
  unsigned int if_index = if_nametoindex(interface);
1634
1688
  if(if_index == 0){
1643
1697
  }
1644
1698
  
1645
1699
  if(not interface_is_up(interface)){
1646
 
    if(not get_flags(interface, &network) and debug){
 
1700
    error_t ret_errno = 0, ioctl_errno = 0;
 
1701
    if(not get_flags(interface, &network)){
1647
1702
      ret_errno = errno;
1648
1703
      fprintf_plus(stderr, "Failed to get flags for interface "
1649
1704
                   "\"%s\"\n", interface);
 
1705
      errno = old_errno;
1650
1706
      return ret_errno;
1651
1707
    }
1652
 
    network.ifr_flags |= IFF_UP;
 
1708
    network.ifr_flags |= IFF_UP; /* set flag */
1653
1709
    
1654
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1655
 
    if(sd < 0){
 
1710
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1711
    if(sd == -1){
1656
1712
      ret_errno = errno;
1657
1713
      perror_plus("socket");
1658
1714
      errno = old_errno;
1659
1715
      return ret_errno;
1660
1716
    }
1661
 
  
 
1717
    
1662
1718
    if(quit_now){
1663
 
      close(sd);
 
1719
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1720
      if(ret == -1){
 
1721
        perror_plus("close");
 
1722
      }
1664
1723
      errno = old_errno;
1665
1724
      return EINTR;
1666
1725
    }
1670
1729
                   interface);
1671
1730
    }
1672
1731
    
1673
 
    /* Raise priviliges */
1674
 
    raise_privileges();
 
1732
    /* Raise privileges */
 
1733
    ret_errno = raise_privileges();
 
1734
    if(ret_errno != 0){
 
1735
      perror_plus("Failed to raise privileges");
 
1736
    }
1675
1737
    
1676
1738
#ifdef __linux__
1677
 
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1678
 
       messages about the network interface to mess up the prompt */
1679
 
    int ret_linux = klogctl(8, NULL, 5);
1680
 
    bool restore_loglevel = true;
1681
 
    if(ret_linux == -1){
1682
 
      restore_loglevel = false;
1683
 
      perror_plus("klogctl");
 
1739
    int ret_linux;
 
1740
    bool restore_loglevel = false;
 
1741
    if(ret_errno == 0){
 
1742
      /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1743
         messages about the network interface to mess up the prompt */
 
1744
      ret_linux = klogctl(8, NULL, 5);
 
1745
      if(ret_linux == -1){
 
1746
        perror_plus("klogctl");
 
1747
      } else {
 
1748
        restore_loglevel = true;
 
1749
      }
1684
1750
    }
1685
1751
#endif  /* __linux__ */
1686
 
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1687
 
    ret_errno = errno;
 
1752
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1753
    ioctl_errno = errno;
1688
1754
#ifdef __linux__
1689
1755
    if(restore_loglevel){
1690
1756
      ret_linux = klogctl(7, NULL, 0);
1694
1760
    }
1695
1761
#endif  /* __linux__ */
1696
1762
    
1697
 
    /* Lower privileges */
1698
 
    lower_privileges();
 
1763
    /* If raise_privileges() succeeded above */
 
1764
    if(ret_errno == 0){
 
1765
      /* Lower privileges */
 
1766
      ret_errno = lower_privileges();
 
1767
      if(ret_errno != 0){
 
1768
        errno = ret_errno;
 
1769
        perror_plus("Failed to lower privileges");
 
1770
      }
 
1771
    }
1699
1772
    
1700
1773
    /* Close the socket */
1701
1774
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1704
1777
    }
1705
1778
    
1706
1779
    if(ret_setflags == -1){
1707
 
      errno = ret_errno;
 
1780
      errno = ioctl_errno;
1708
1781
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1709
1782
      errno = old_errno;
1710
 
      return ret_errno;
 
1783
      return ioctl_errno;
1711
1784
    }
1712
1785
  } else if(debug){
1713
1786
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1731
1804
  return 0;
1732
1805
}
1733
1806
 
 
1807
__attribute__((nonnull, warn_unused_result))
1734
1808
error_t take_down_interface(const char *const interface){
1735
1809
  error_t old_errno = errno;
1736
1810
  struct ifreq network;
1741
1815
    return ENXIO;
1742
1816
  }
1743
1817
  if(interface_is_up(interface)){
1744
 
    error_t ret_errno = 0;
 
1818
    error_t ret_errno = 0, ioctl_errno = 0;
1745
1819
    if(not get_flags(interface, &network) and debug){
1746
1820
      ret_errno = errno;
1747
1821
      fprintf_plus(stderr, "Failed to get flags for interface "
1748
1822
                   "\"%s\"\n", interface);
 
1823
      errno = old_errno;
1749
1824
      return ret_errno;
1750
1825
    }
1751
1826
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1752
1827
    
1753
1828
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1754
 
    if(sd < 0){
 
1829
    if(sd == -1){
1755
1830
      ret_errno = errno;
1756
1831
      perror_plus("socket");
1757
1832
      errno = old_errno;
1763
1838
                   interface);
1764
1839
    }
1765
1840
    
1766
 
    /* Raise priviliges */
1767
 
    raise_privileges();
 
1841
    /* Raise privileges */
 
1842
    ret_errno = raise_privileges();
 
1843
    if(ret_errno != 0){
 
1844
      perror_plus("Failed to raise privileges");
 
1845
    }
1768
1846
    
1769
1847
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1770
 
    ret_errno = errno;
 
1848
    ioctl_errno = errno;
1771
1849
    
1772
 
    /* Lower privileges */
1773
 
    lower_privileges();
 
1850
    /* If raise_privileges() succeeded above */
 
1851
    if(ret_errno == 0){
 
1852
      /* Lower privileges */
 
1853
      ret_errno = lower_privileges();
 
1854
      if(ret_errno != 0){
 
1855
        errno = ret_errno;
 
1856
        perror_plus("Failed to lower privileges");
 
1857
      }
 
1858
    }
1774
1859
    
1775
1860
    /* Close the socket */
1776
1861
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1779
1864
    }
1780
1865
    
1781
1866
    if(ret_setflags == -1){
1782
 
      errno = ret_errno;
 
1867
      errno = ioctl_errno;
1783
1868
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1784
1869
      errno = old_errno;
1785
 
      return ret_errno;
 
1870
      return ioctl_errno;
1786
1871
    }
1787
1872
  } else if(debug){
1788
1873
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1796
1881
int main(int argc, char *argv[]){
1797
1882
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
1798
1883
                        .priority = "SECURE256:!CTYPE-X.509:"
1799
 
                        "+CTYPE-OPENPGP", .current_server = NULL, 
 
1884
                        "+CTYPE-OPENPGP", .current_server = NULL,
1800
1885
                        .interfaces = NULL, .interfaces_size = 0 };
1801
1886
  AvahiSServiceBrowser *sb = NULL;
1802
1887
  error_t ret_errno;
1806
1891
  int exitcode = EXIT_SUCCESS;
1807
1892
  char *interfaces_to_take_down = NULL;
1808
1893
  size_t interfaces_to_take_down_size = 0;
1809
 
  char tempdir[] = "/tmp/mandosXXXXXX";
1810
 
  bool tempdir_created = false;
 
1894
  char run_tempdir[] = "/run/tmp/mandosXXXXXX";
 
1895
  char old_tempdir[] = "/tmp/mandosXXXXXX";
 
1896
  char *tempdir = NULL;
1811
1897
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1812
1898
  const char *seckey = PATHDIR "/" SECKEY;
1813
1899
  const char *pubkey = PATHDIR "/" PUBKEY;
1995
2081
    /* Work around Debian bug #633582:
1996
2082
       <http://bugs.debian.org/633582> */
1997
2083
    
1998
 
    /* Re-raise priviliges */
1999
 
    if(raise_privileges() == 0){
 
2084
    /* Re-raise privileges */
 
2085
    ret_errno = raise_privileges();
 
2086
    if(ret_errno != 0){
 
2087
      errno = ret_errno;
 
2088
      perror_plus("Failed to raise privileges");
 
2089
    } else {
2000
2090
      struct stat st;
2001
2091
      
2002
2092
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2042
2132
      }
2043
2133
    
2044
2134
      /* Lower privileges */
2045
 
      lower_privileges();
 
2135
      ret_errno = lower_privileges();
 
2136
      if(ret_errno != 0){
 
2137
        errno = ret_errno;
 
2138
        perror_plus("Failed to lower privileges");
 
2139
      }
2046
2140
    }
2047
2141
  }
2048
2142
  
2074
2168
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2075
2169
      argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2076
2170
    }
2077
 
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
2078
 
                             interfaces_hooks : "", delay)){
2079
 
      goto end;
2080
 
    }
 
2171
    run_network_hooks("start", interfaces_hooks != NULL ?
 
2172
                      interfaces_hooks : "", delay);
2081
2173
  }
2082
2174
  
2083
2175
  if(not debug){
2161
2253
  
2162
2254
  /* If no interfaces were specified, make a list */
2163
2255
  if(mc.interfaces == NULL){
2164
 
    struct dirent **direntries;
 
2256
    struct dirent **direntries = NULL;
2165
2257
    /* Look for any good interfaces */
2166
2258
    ret = scandir(sys_class_net, &direntries, good_interface,
2167
2259
                  alphasort);
2211
2303
        break;
2212
2304
      }
2213
2305
      bool interface_was_up = interface_is_up(interface);
2214
 
      ret = bring_up_interface(interface, delay);
 
2306
      errno = bring_up_interface(interface, delay);
2215
2307
      if(not interface_was_up){
2216
 
        if(ret != 0){
2217
 
          errno = ret;
 
2308
        if(errno != 0){
2218
2309
          perror_plus("Failed to bring up interface");
2219
2310
        } else {
2220
 
          ret_errno = argz_add(&interfaces_to_take_down,
2221
 
                               &interfaces_to_take_down_size,
2222
 
                               interface);
2223
 
          if(ret_errno != 0){
2224
 
            errno = ret_errno;
 
2311
          errno = argz_add(&interfaces_to_take_down,
 
2312
                           &interfaces_to_take_down_size,
 
2313
                           interface);
 
2314
          if(errno != 0){
2225
2315
            perror_plus("argz_add");
2226
2316
          }
2227
2317
        }
2258
2348
    goto end;
2259
2349
  }
2260
2350
  
2261
 
  if(mkdtemp(tempdir) == NULL){
 
2351
  /* Try /run/tmp before /tmp */
 
2352
  tempdir = mkdtemp(run_tempdir);
 
2353
  if(tempdir == NULL and errno == ENOENT){
 
2354
      if(debug){
 
2355
        fprintf_plus(stderr, "Tempdir %s did not work, trying %s\n",
 
2356
                     run_tempdir, old_tempdir);
 
2357
      }
 
2358
      tempdir = mkdtemp(old_tempdir);
 
2359
  }
 
2360
  if(tempdir == NULL){
2262
2361
    perror_plus("mkdtemp");
2263
2362
    goto end;
2264
2363
  }
2265
 
  tempdir_created = true;
2266
2364
  
2267
2365
  if(quit_now){
2268
2366
    goto end;
2343
2441
      sleep((unsigned int)retry_interval);
2344
2442
    }
2345
2443
    
2346
 
    if (not quit_now){
 
2444
    if(not quit_now){
2347
2445
      exitcode = EXIT_SUCCESS;
2348
2446
    }
2349
2447
    
2404
2502
  if(debug){
2405
2503
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2406
2504
  }
2407
 
 
 
2505
  
2408
2506
  ret = avahi_loop_with_timeout(simple_poll,
2409
2507
                                (int)(retry_interval * 1000), &mc);
2410
2508
  if(debug){
2451
2549
    }
2452
2550
  }
2453
2551
  
2454
 
  /* Re-raise priviliges */
 
2552
  /* Re-raise privileges */
2455
2553
  {
2456
 
    raise_privileges();
2457
 
    
2458
 
    /* Run network hooks */
2459
 
    run_network_hooks("stop", interfaces_hooks != NULL ?
2460
 
                      interfaces_hooks : "", delay);
2461
 
    
2462
 
    /* Take down the network interfaces which were brought up */
2463
 
    {
2464
 
      char *interface = NULL;
2465
 
      while((interface=argz_next(interfaces_to_take_down,
2466
 
                                 interfaces_to_take_down_size,
2467
 
                                 interface))){
2468
 
        ret_errno = take_down_interface(interface);
2469
 
        if(ret_errno != 0){
2470
 
          errno = ret_errno;
2471
 
          perror_plus("Failed to take down interface");
2472
 
        }
2473
 
      }
2474
 
      if(debug and (interfaces_to_take_down == NULL)){
2475
 
        fprintf_plus(stderr, "No interfaces needed to be taken"
2476
 
                     " down\n");
2477
 
      }
2478
 
    }
2479
 
    
2480
 
    lower_privileges_permanently();
 
2554
    ret_errno = raise_privileges();
 
2555
    if(ret_errno != 0){
 
2556
      perror_plus("Failed to raise privileges");
 
2557
    } else {
 
2558
      
 
2559
      /* Run network hooks */
 
2560
      run_network_hooks("stop", interfaces_hooks != NULL ?
 
2561
                        interfaces_hooks : "", delay);
 
2562
      
 
2563
      /* Take down the network interfaces which were brought up */
 
2564
      {
 
2565
        char *interface = NULL;
 
2566
        while((interface=argz_next(interfaces_to_take_down,
 
2567
                                   interfaces_to_take_down_size,
 
2568
                                   interface))){
 
2569
          ret_errno = take_down_interface(interface);
 
2570
          if(ret_errno != 0){
 
2571
            errno = ret_errno;
 
2572
            perror_plus("Failed to take down interface");
 
2573
          }
 
2574
        }
 
2575
        if(debug and (interfaces_to_take_down == NULL)){
 
2576
          fprintf_plus(stderr, "No interfaces needed to be taken"
 
2577
                       " down\n");
 
2578
        }
 
2579
      }
 
2580
    }
 
2581
    
 
2582
    ret_errno = lower_privileges_permanently();
 
2583
    if(ret_errno != 0){
 
2584
      perror_plus("Failed to lower privileges permanently");
 
2585
    }
2481
2586
  }
2482
2587
  
2483
2588
  free(interfaces_to_take_down);
2484
2589
  free(interfaces_hooks);
2485
2590
  
2486
2591
  /* Removes the GPGME temp directory and all files inside */
2487
 
  if(tempdir_created){
 
2592
  if(tempdir != NULL){
2488
2593
    struct dirent **direntries = NULL;
2489
 
    struct dirent *direntry = NULL;
2490
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
2491
 
                             alphasort);
2492
 
    if (numentries > 0){
2493
 
      for(int i = 0; i < numentries; i++){
2494
 
        direntry = direntries[i];
2495
 
        char *fullname = NULL;
2496
 
        ret = asprintf(&fullname, "%s/%s", tempdir,
2497
 
                       direntry->d_name);
2498
 
        if(ret < 0){
2499
 
          perror_plus("asprintf");
2500
 
          continue;
2501
 
        }
2502
 
        ret = remove(fullname);
2503
 
        if(ret == -1){
2504
 
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2505
 
                       strerror(errno));
2506
 
        }
2507
 
        free(fullname);
 
2594
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
 
2595
                                                  O_NOFOLLOW));
 
2596
    if(tempdir_fd == -1){
 
2597
      perror_plus("open");
 
2598
    } else {
 
2599
#ifdef __GLIBC__
 
2600
#if __GLIBC_PREREQ(2, 15)
 
2601
      int numentries = scandirat(tempdir_fd, ".", &direntries,
 
2602
                                 notdotentries, alphasort);
 
2603
#else  /* not __GLIBC_PREREQ(2, 15) */
 
2604
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
2605
                               alphasort);
 
2606
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
2607
#else   /* not __GLIBC__ */
 
2608
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
2609
                               alphasort);
 
2610
#endif  /* not __GLIBC__ */
 
2611
      if(numentries > 0){
 
2612
        for(int i = 0; i < numentries; i++){
 
2613
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
 
2614
          if(ret == -1){
 
2615
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
 
2616
                         " \"%s\", 0): %s\n", tempdir,
 
2617
                         direntries[i]->d_name, strerror(errno));
 
2618
          }
 
2619
        }
 
2620
        
 
2621
        /* need to clean even if 0 because man page doesn't specify */
 
2622
        free(direntries);
 
2623
        if(numentries == -1){
 
2624
          perror_plus("scandir");
 
2625
        }
 
2626
        ret = rmdir(tempdir);
 
2627
        if(ret == -1 and errno != ENOENT){
 
2628
          perror_plus("rmdir");
 
2629
        }
2508
2630
      }
2509
 
    }
2510
 
 
2511
 
    /* need to clean even if 0 because man page doesn't specify */
2512
 
    free(direntries);
2513
 
    if (numentries == -1){
2514
 
      perror_plus("scandir");
2515
 
    }
2516
 
    ret = rmdir(tempdir);
2517
 
    if(ret == -1 and errno != ENOENT){
2518
 
      perror_plus("rmdir");
 
2631
      TEMP_FAILURE_RETRY(close(tempdir_fd));
2519
2632
    }
2520
2633
  }
2521
2634