/mandos/trunk

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

« back to all changes in this revision

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

  • Committer: Teddy Hogeborn
  • Date: 2014-03-10 06:34:36 UTC
  • Revision ID: teddy@recompile.se-20140310063436-zksdghqvv6k1pbyg
Do not add a new server to server list if clock_gettime() fails

* plugins.d/mandos-client.c (add_server): Set server.last_seen before
                                          adding new server, in case
                                          clock_gettime() fails.

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-2014 Teddy Hogeborn
13
 
 * Copyright © 2008-2014 Björn Påhlsson
 
12
 * Copyright © 2008-2013 Teddy Hogeborn
 
13
 * Copyright © 2008-2013 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  /* not _LARGEFILE_SOURCE */
 
35
#endif
36
36
#ifndef _FILE_OFFSET_BITS
37
37
#define _FILE_OFFSET_BITS 64
38
 
#endif  /* not _FILE_OFFSET_BITS */
 
38
#endif
39
39
 
40
40
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
41
41
 
42
42
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
 
                                   stdout, ferror() */
 
43
                                   stdout, ferror(), remove() */
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(),
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
58
                                   inet_pton(), connect(),
59
59
                                   getnameinfo() */
60
 
#include <fcntl.h>              /* open(), unlinkat() */
 
60
#include <fcntl.h>              /* open() */
61
61
#include <dirent.h>             /* opendir(), struct dirent, readdir()
62
62
                                 */
63
63
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
73
73
                                */
74
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
75
                                   getuid(), getgid(), seteuid(),
76
 
                                   setgid(), pause(), _exit(),
77
 
                                   unlinkat() */
 
76
                                   setgid(), pause(), _exit() */
78
77
#include <arpa/inet.h>          /* inet_pton(), htons() */
79
78
#include <iso646.h>             /* not, or, and */
80
79
#include <argp.h>               /* struct argp_option, error_t, struct
142
141
static const char sys_class_net[] = "/sys/class/net";
143
142
char *connect_to = NULL;
144
143
const char *hookdir = HOOKDIR;
145
 
int hookdir_fd = -1;
146
144
uid_t uid = 65534;
147
145
gid_t gid = 65534;
148
146
 
185
183
  perror(print_text);
186
184
}
187
185
 
188
 
__attribute__((format (gnu_printf, 2, 3), nonnull))
 
186
__attribute__((format (gnu_printf, 2, 3)))
189
187
int fprintf_plus(FILE *stream, const char *format, ...){
190
188
  va_list ap;
191
189
  va_start (ap, format);
200
198
 * bytes. "buffer_capacity" is how much is currently allocated,
201
199
 * "buffer_length" is how much is already used.
202
200
 */
203
 
__attribute__((nonnull, warn_unused_result))
204
201
size_t incbuffer(char **buffer, size_t buffer_length,
205
202
                 size_t buffer_capacity){
206
203
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
219
216
}
220
217
 
221
218
/* Add server to set of servers to retry periodically */
222
 
__attribute__((nonnull, warn_unused_result))
223
219
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
224
220
                int af, server **current_server){
225
221
  int ret;
234
230
                          .af = af };
235
231
  if(new_server->ip == NULL){
236
232
    perror_plus("strdup");
237
 
    free(new_server);
238
233
    return false;
239
234
  }
240
235
  ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
241
236
  if(ret == -1){
242
237
    perror_plus("clock_gettime");
243
 
    free(new_server->ip);
244
 
    free(new_server);
245
238
    return false;
246
239
  }
247
240
  /* Special case of first server */
249
242
    new_server->next = new_server;
250
243
    new_server->prev = new_server;
251
244
    *current_server = new_server;
 
245
  /* Place the new server last in the list */
252
246
  } else {
253
 
    /* Place the new server last in the list */
254
247
    new_server->next = *current_server;
255
248
    new_server->prev = (*current_server)->prev;
256
249
    new_server->prev->next = new_server;
262
255
/* 
263
256
 * Initialize GPGME.
264
257
 */
265
 
__attribute__((nonnull, warn_unused_result))
266
 
static bool init_gpgme(const char * const seckey,
267
 
                       const char * const pubkey,
268
 
                       const char * const tempdir,
269
 
                       mandos_context *mc){
 
258
static bool init_gpgme(const char *seckey, const char *pubkey,
 
259
                       const char *tempdir, mandos_context *mc){
270
260
  gpgme_error_t rc;
271
261
  gpgme_engine_info_t engine_info;
272
262
  
273
263
  /*
274
264
   * Helper function to insert pub and seckey to the engine keyring.
275
265
   */
276
 
  bool import_key(const char * const filename){
 
266
  bool import_key(const char *filename){
277
267
    int ret;
278
268
    int fd;
279
269
    gpgme_data_t pgp_data;
360
350
 * Decrypt OpenPGP data.
361
351
 * Returns -1 on error
362
352
 */
363
 
__attribute__((nonnull, warn_unused_result))
364
353
static ssize_t pgp_packet_decrypt(const char *cryptotext,
365
354
                                  size_t crypto_size,
366
355
                                  char **plaintext,
486
475
  return plaintext_length;
487
476
}
488
477
 
489
 
__attribute__((warn_unused_result))
490
 
static const char *safer_gnutls_strerror(int value){
 
478
static const char * safer_gnutls_strerror(int value){
491
479
  const char *ret = gnutls_strerror(value);
492
480
  if(ret == NULL)
493
481
    ret = "(unknown)";
495
483
}
496
484
 
497
485
/* GnuTLS log function callback */
498
 
__attribute__((nonnull))
499
486
static void debuggnutls(__attribute__((unused)) int level,
500
487
                        const char* string){
501
488
  fprintf_plus(stderr, "GnuTLS: %s", string);
502
489
}
503
490
 
504
 
__attribute__((nonnull, warn_unused_result))
505
491
static int init_gnutls_global(const char *pubkeyfilename,
506
492
                              const char *seckeyfilename,
507
493
                              mandos_context *mc){
581
567
  return -1;
582
568
}
583
569
 
584
 
__attribute__((nonnull, warn_unused_result))
585
570
static int init_gnutls_session(gnutls_session_t *session,
586
571
                               mandos_context *mc){
587
572
  int ret;
644
629
                      __attribute__((unused)) const char *txt){}
645
630
 
646
631
/* Called when a Mandos server is found */
647
 
__attribute__((nonnull, warn_unused_result))
648
632
static int start_mandos_communication(const char *ip, in_port_t port,
649
633
                                      AvahiIfIndex if_index,
650
634
                                      int af, mandos_context *mc){
651
635
  int ret, tcp_sd = -1;
652
636
  ssize_t sret;
653
 
  struct sockaddr_storage to;
 
637
  union {
 
638
    struct sockaddr_in in;
 
639
    struct sockaddr_in6 in6;
 
640
  } to;
654
641
  char *buffer = NULL;
655
642
  char *decrypted_buffer = NULL;
656
643
  size_t buffer_length = 0;
737
724
  
738
725
  memset(&to, 0, sizeof(to));
739
726
  if(af == AF_INET6){
740
 
    ((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
741
 
    ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
 
727
    to.in6.sin6_family = (sa_family_t)af;
 
728
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
742
729
  } else {                      /* IPv4 */
743
 
    ((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
744
 
    ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
 
730
    to.in.sin_family = (sa_family_t)af;
 
731
    ret = inet_pton(af, ip, &to.in.sin_addr);
745
732
  }
746
733
  if(ret < 0 ){
747
734
    int e = errno;
756
743
    goto mandos_end;
757
744
  }
758
745
  if(af == AF_INET6){
759
 
    ((struct sockaddr_in6 *)&to)->sin6_port = htons(port);
760
 
    if(IN6_IS_ADDR_LINKLOCAL
761
 
       (&((struct sockaddr_in6 *)&to)->sin6_addr)){
 
746
    to.in6.sin6_port = htons(port);    
 
747
#ifdef __GNUC__
 
748
#pragma GCC diagnostic push
 
749
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
 
750
#endif
 
751
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
 
752
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower */
 
753
#ifdef __GNUC__
 
754
#pragma GCC diagnostic pop
 
755
#endif
762
756
      if(if_index == AVAHI_IF_UNSPEC){
763
757
        fprintf_plus(stderr, "An IPv6 link-local address is"
764
758
                     " incomplete without a network interface\n");
766
760
        goto mandos_end;
767
761
      }
768
762
      /* Set the network interface number as scope */
769
 
      ((struct sockaddr_in6 *)&to)->sin6_scope_id = (uint32_t)if_index;
 
763
      to.in6.sin6_scope_id = (uint32_t)if_index;
770
764
    }
771
765
  } else {
772
 
    ((struct sockaddr_in *)&to)->sin_port = htons(port);
 
766
    to.in.sin_port = htons(port);
773
767
  }
774
768
  
775
769
  if(quit_now){
793
787
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
794
788
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
795
789
    if(af == AF_INET6){
796
 
      ret = getnameinfo((struct sockaddr *)&to,
797
 
                        sizeof(struct sockaddr_in6),
 
790
      ret = getnameinfo((struct sockaddr *)&(to.in6), sizeof(to.in6),
798
791
                        addrstr, sizeof(addrstr), NULL, 0,
799
792
                        NI_NUMERICHOST);
800
793
    } else {
801
 
      ret = getnameinfo((struct sockaddr *)&to,
802
 
                        sizeof(struct sockaddr_in),
 
794
      ret = getnameinfo((struct sockaddr *)&(to.in), sizeof(to.in),
803
795
                        addrstr, sizeof(addrstr), NULL, 0,
804
796
                        NI_NUMERICHOST);
805
797
    }
818
810
  }
819
811
  
820
812
  if(af == AF_INET6){
821
 
    ret = connect(tcp_sd, (struct sockaddr *)&to,
822
 
                  sizeof(struct sockaddr_in6));
 
813
    ret = connect(tcp_sd, &to.in6, sizeof(to));
823
814
  } else {
824
 
    ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
825
 
                  sizeof(struct sockaddr_in));
 
815
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
826
816
  }
827
817
  if(ret < 0){
828
 
    if((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
818
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
829
819
      int e = errno;
830
820
      perror_plus("connect");
831
821
      errno = e;
1046
1036
  return retval;
1047
1037
}
1048
1038
 
1049
 
__attribute__((nonnull))
1050
1039
static void resolve_callback(AvahiSServiceResolver *r,
1051
1040
                             AvahiIfIndex interface,
1052
1041
                             AvahiProtocol proto,
1060
1049
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
1061
1050
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1062
1051
                             flags,
1063
 
                             void *mc){
 
1052
                             void* mc){
1064
1053
  if(r == NULL){
1065
1054
    return;
1066
1055
  }
1119
1108
                            const char *domain,
1120
1109
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1121
1110
                            flags,
1122
 
                            void *mc){
 
1111
                            void* mc){
1123
1112
  if(b == NULL){
1124
1113
    return;
1125
1114
  }
1185
1174
  errno = old_errno;
1186
1175
}
1187
1176
 
1188
 
__attribute__((nonnull, warn_unused_result))
1189
1177
bool get_flags(const char *ifname, struct ifreq *ifr){
1190
1178
  int ret;
1191
1179
  error_t ret_errno;
1210
1198
  return true;
1211
1199
}
1212
1200
 
1213
 
__attribute__((nonnull, warn_unused_result))
1214
1201
bool good_flags(const char *ifname, const struct ifreq *ifr){
1215
1202
  
1216
1203
  /* Reject the loopback device */
1258
1245
 * corresponds to an acceptable network device.
1259
1246
 * (This function is passed to scandir(3) as a filter function.)
1260
1247
 */
1261
 
__attribute__((nonnull, warn_unused_result))
1262
1248
int good_interface(const struct dirent *if_entry){
1263
1249
  if(if_entry->d_name[0] == '.'){
1264
1250
    return 0;
1282
1268
/* 
1283
1269
 * This function determines if a network interface is up.
1284
1270
 */
1285
 
__attribute__((nonnull, warn_unused_result))
1286
1271
bool interface_is_up(const char *interface){
1287
1272
  struct ifreq ifr;
1288
1273
  if(not get_flags(interface, &ifr)){
1299
1284
/* 
1300
1285
 * This function determines if a network interface is running
1301
1286
 */
1302
 
__attribute__((nonnull, warn_unused_result))
1303
1287
bool interface_is_running(const char *interface){
1304
1288
  struct ifreq ifr;
1305
1289
  if(not get_flags(interface, &ifr)){
1313
1297
  return (bool)(ifr.ifr_flags & IFF_RUNNING);
1314
1298
}
1315
1299
 
1316
 
__attribute__((nonnull, pure, warn_unused_result))
1317
1300
int notdotentries(const struct dirent *direntry){
1318
1301
  /* Skip "." and ".." */
1319
1302
  if(direntry->d_name[0] == '.'
1326
1309
}
1327
1310
 
1328
1311
/* Is this directory entry a runnable program? */
1329
 
__attribute__((nonnull, warn_unused_result))
1330
1312
int runnable_hook(const struct dirent *direntry){
1331
1313
  int ret;
1332
1314
  size_t sret;
1340
1322
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1341
1323
                "abcdefghijklmnopqrstuvwxyz"
1342
1324
                "0123456789"
1343
 
                "_.-");
 
1325
                "_-");
1344
1326
  if((direntry->d_name)[sret] != '\0'){
1345
1327
    /* Contains non-allowed characters */
1346
1328
    if(debug){
1350
1332
    return 0;
1351
1333
  }
1352
1334
  
1353
 
  ret = fstatat(hookdir_fd, direntry->d_name, &st, 0);
 
1335
  char *fullname = NULL;
 
1336
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1337
  if(ret < 0){
 
1338
    perror_plus("asprintf");
 
1339
    return 0;
 
1340
  }
 
1341
  
 
1342
  ret = stat(fullname, &st);
1354
1343
  if(ret == -1){
1355
1344
    if(debug){
1356
1345
      perror_plus("Could not stat hook");
1380
1369
  return 1;
1381
1370
}
1382
1371
 
1383
 
__attribute__((nonnull, warn_unused_result))
1384
1372
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1385
1373
                            mandos_context *mc){
1386
1374
  int ret;
1390
1378
  
1391
1379
  while(true){
1392
1380
    if(mc->current_server == NULL){
1393
 
      if(debug){
 
1381
      if (debug){
1394
1382
        fprintf_plus(stderr, "Wait until first server is found."
1395
1383
                     " No timeout!\n");
1396
1384
      }
1397
1385
      ret = avahi_simple_poll_iterate(s, -1);
1398
1386
    } else {
1399
 
      if(debug){
 
1387
      if (debug){
1400
1388
        fprintf_plus(stderr, "Check current_server if we should run"
1401
1389
                     " it, or wait\n");
1402
1390
      }
1419
1407
                     - ((intmax_t)waited_time.tv_sec * 1000))
1420
1408
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1421
1409
      
1422
 
      if(debug){
 
1410
      if (debug){
1423
1411
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1424
1412
                     block_time);
1425
1413
      }
1447
1435
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1448
1436
    }
1449
1437
    if(ret != 0){
1450
 
      if(ret > 0 or errno != EINTR){
 
1438
      if (ret > 0 or errno != EINTR){
1451
1439
        return (ret != 1) ? ret : 0;
1452
1440
      }
1453
1441
    }
1455
1443
}
1456
1444
 
1457
1445
/* Set effective uid to 0, return errno */
1458
 
__attribute__((warn_unused_result))
1459
1446
error_t raise_privileges(void){
1460
1447
  error_t old_errno = errno;
1461
1448
  error_t ret_errno = 0;
1462
1449
  if(seteuid(0) == -1){
1463
1450
    ret_errno = errno;
 
1451
    perror_plus("seteuid");
1464
1452
  }
1465
1453
  errno = old_errno;
1466
1454
  return ret_errno;
1467
1455
}
1468
1456
 
1469
1457
/* Set effective and real user ID to 0.  Return errno. */
1470
 
__attribute__((warn_unused_result))
1471
1458
error_t raise_privileges_permanently(void){
1472
1459
  error_t old_errno = errno;
1473
1460
  error_t ret_errno = raise_privileges();
1477
1464
  }
1478
1465
  if(setuid(0) == -1){
1479
1466
    ret_errno = errno;
 
1467
    perror_plus("seteuid");
1480
1468
  }
1481
1469
  errno = old_errno;
1482
1470
  return ret_errno;
1483
1471
}
1484
1472
 
1485
1473
/* Set effective user ID to unprivileged saved user ID */
1486
 
__attribute__((warn_unused_result))
1487
1474
error_t lower_privileges(void){
1488
1475
  error_t old_errno = errno;
1489
1476
  error_t ret_errno = 0;
1490
1477
  if(seteuid(uid) == -1){
1491
1478
    ret_errno = errno;
 
1479
    perror_plus("seteuid");
1492
1480
  }
1493
1481
  errno = old_errno;
1494
1482
  return ret_errno;
1495
1483
}
1496
1484
 
1497
1485
/* Lower privileges permanently */
1498
 
__attribute__((warn_unused_result))
1499
1486
error_t lower_privileges_permanently(void){
1500
1487
  error_t old_errno = errno;
1501
1488
  error_t ret_errno = 0;
1502
1489
  if(setuid(uid) == -1){
1503
1490
    ret_errno = errno;
 
1491
    perror_plus("setuid");
1504
1492
  }
1505
1493
  errno = old_errno;
1506
1494
  return ret_errno;
1507
1495
}
1508
1496
 
1509
 
__attribute__((nonnull))
1510
 
void run_network_hooks(const char *mode, const char *interface,
 
1497
bool run_network_hooks(const char *mode, const char *interface,
1511
1498
                       const float delay){
1512
 
  struct dirent **direntries = NULL;
1513
 
  if(hookdir_fd == -1){
1514
 
    hookdir_fd = open(hookdir, O_RDONLY);
1515
 
    if(hookdir_fd == -1){
1516
 
      if(errno == ENOENT){
1517
 
        if(debug){
1518
 
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
1519
 
                       " found\n", hookdir);
1520
 
        }
1521
 
      } else {
1522
 
        perror_plus("open");
1523
 
      }
1524
 
      return;
1525
 
    }
1526
 
  }
1527
 
#ifdef __GLIBC__
1528
 
#if __GLIBC_PREREQ(2, 15)
1529
 
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1530
 
                           runnable_hook, alphasort);
1531
 
#else  /* not __GLIBC_PREREQ(2, 15) */
1532
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1533
 
                         alphasort);
1534
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
1535
 
#else   /* not __GLIBC__ */
1536
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1537
 
                         alphasort);
1538
 
#endif  /* not __GLIBC__ */
 
1499
  struct dirent **direntries;
 
1500
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1501
                         alphasort);
1539
1502
  if(numhooks == -1){
1540
 
    perror_plus("scandir");
1541
 
    return;
1542
 
  }
1543
 
  struct dirent *direntry;
1544
 
  int ret;
1545
 
  int devnull = open("/dev/null", O_RDONLY);
1546
 
  for(int i = 0; i < numhooks; i++){
1547
 
    direntry = direntries[i];
1548
 
    if(debug){
1549
 
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
1550
 
                   direntry->d_name);
 
1503
    if(errno == ENOENT){
 
1504
      if(debug){
 
1505
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1506
                     " found\n", hookdir);
 
1507
      }
 
1508
    } else {
 
1509
      perror_plus("scandir");
1551
1510
    }
1552
 
    pid_t hook_pid = fork();
1553
 
    if(hook_pid == 0){
1554
 
      /* Child */
1555
 
      /* Raise privileges */
1556
 
      errno = raise_privileges_permanently();
1557
 
      if(errno != 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){
 
1511
  } else {
 
1512
    struct dirent *direntry;
 
1513
    int ret;
 
1514
    int devnull = open("/dev/null", O_RDONLY);
 
1515
    for(int i = 0; i < numhooks; i++){
 
1516
      direntry = direntries[i];
 
1517
      char *fullname = NULL;
 
1518
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1519
      if(ret < 0){
1613
1520
        perror_plus("asprintf");
1614
 
        _exit(EX_OSERR);
1615
 
      }
1616
 
      ret = setenv("DELAY", delaystring, 1);
1617
 
      if(ret == -1){
 
1521
        continue;
 
1522
      }
 
1523
      if(debug){
 
1524
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1525
                     direntry->d_name);
 
1526
      }
 
1527
      pid_t hook_pid = fork();
 
1528
      if(hook_pid == 0){
 
1529
        /* Child */
 
1530
        /* Raise privileges */
 
1531
        raise_privileges_permanently();
 
1532
        /* Set group */
 
1533
        errno = 0;
 
1534
        ret = setgid(0);
 
1535
        if(ret == -1){
 
1536
          perror_plus("setgid");
 
1537
        }
 
1538
        /* Reset supplementary groups */
 
1539
        errno = 0;
 
1540
        ret = setgroups(0, NULL);
 
1541
        if(ret == -1){
 
1542
          perror_plus("setgroups");
 
1543
        }
 
1544
        dup2(devnull, STDIN_FILENO);
 
1545
        close(devnull);
 
1546
        dup2(STDERR_FILENO, STDOUT_FILENO);
 
1547
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1548
        if(ret == -1){
 
1549
          perror_plus("setenv");
 
1550
          _exit(EX_OSERR);
 
1551
        }
 
1552
        ret = setenv("DEVICE", interface, 1);
 
1553
        if(ret == -1){
 
1554
          perror_plus("setenv");
 
1555
          _exit(EX_OSERR);
 
1556
        }
 
1557
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1558
        if(ret == -1){
 
1559
          perror_plus("setenv");
 
1560
          _exit(EX_OSERR);
 
1561
        }
 
1562
        ret = setenv("MODE", mode, 1);
 
1563
        if(ret == -1){
 
1564
          perror_plus("setenv");
 
1565
          _exit(EX_OSERR);
 
1566
        }
 
1567
        char *delaystring;
 
1568
        ret = asprintf(&delaystring, "%f", delay);
 
1569
        if(ret == -1){
 
1570
          perror_plus("asprintf");
 
1571
          _exit(EX_OSERR);
 
1572
        }
 
1573
        ret = setenv("DELAY", delaystring, 1);
 
1574
        if(ret == -1){
 
1575
          free(delaystring);
 
1576
          perror_plus("setenv");
 
1577
          _exit(EX_OSERR);
 
1578
        }
1618
1579
        free(delaystring);
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;
 
1580
        if(connect_to != NULL){
 
1581
          ret = setenv("CONNECT", connect_to, 1);
 
1582
          if(ret == -1){
 
1583
            perror_plus("setenv");
 
1584
            _exit(EX_OSERR);
 
1585
          }
 
1586
        }
 
1587
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1588
          perror_plus("execl");
 
1589
          _exit(EXIT_FAILURE);
 
1590
        }
1662
1591
      } else {
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
 
  free(direntries);
1674
 
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1675
 
    perror_plus("close");
1676
 
  } else {
1677
 
    hookdir_fd = -1;
1678
 
  }
1679
 
  close(devnull);
 
1592
        int status;
 
1593
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1594
          perror_plus("waitpid");
 
1595
          free(fullname);
 
1596
          continue;
 
1597
        }
 
1598
        if(WIFEXITED(status)){
 
1599
          if(WEXITSTATUS(status) != 0){
 
1600
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1601
                         " with status %d\n", direntry->d_name,
 
1602
                         WEXITSTATUS(status));
 
1603
            free(fullname);
 
1604
            continue;
 
1605
          }
 
1606
        } else if(WIFSIGNALED(status)){
 
1607
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1608
                       " signal %d\n", direntry->d_name,
 
1609
                       WTERMSIG(status));
 
1610
          free(fullname);
 
1611
          continue;
 
1612
        } else {
 
1613
          fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1614
                       " crashed\n", direntry->d_name);
 
1615
          free(fullname);
 
1616
          continue;
 
1617
        }
 
1618
      }
 
1619
      free(fullname);
 
1620
      if(debug){
 
1621
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1622
                     direntry->d_name);
 
1623
      }
 
1624
    }
 
1625
    close(devnull);
 
1626
  }
 
1627
  return true;
1680
1628
}
1681
1629
 
1682
 
__attribute__((nonnull, warn_unused_result))
1683
1630
error_t bring_up_interface(const char *const interface,
1684
1631
                           const float delay){
 
1632
  int sd = -1;
1685
1633
  error_t old_errno = errno;
1686
 
  int ret;
 
1634
  error_t ret_errno = 0;
 
1635
  int ret, ret_setflags;
1687
1636
  struct ifreq network;
1688
1637
  unsigned int if_index = if_nametoindex(interface);
1689
1638
  if(if_index == 0){
1698
1647
  }
1699
1648
  
1700
1649
  if(not interface_is_up(interface)){
1701
 
    error_t ret_errno = 0, ioctl_errno = 0;
1702
 
    if(not get_flags(interface, &network)){
 
1650
    if(not get_flags(interface, &network) and debug){
1703
1651
      ret_errno = errno;
1704
1652
      fprintf_plus(stderr, "Failed to get flags for interface "
1705
1653
                   "\"%s\"\n", interface);
1706
 
      errno = old_errno;
1707
1654
      return ret_errno;
1708
1655
    }
1709
 
    network.ifr_flags |= IFF_UP; /* set flag */
 
1656
    network.ifr_flags |= IFF_UP;
1710
1657
    
1711
 
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1712
 
    if(sd == -1){
 
1658
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1659
    if(sd < 0){
1713
1660
      ret_errno = errno;
1714
1661
      perror_plus("socket");
1715
1662
      errno = old_errno;
1716
1663
      return ret_errno;
1717
1664
    }
1718
 
    
 
1665
  
1719
1666
    if(quit_now){
1720
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1721
 
      if(ret == -1){
1722
 
        perror_plus("close");
1723
 
      }
 
1667
      close(sd);
1724
1668
      errno = old_errno;
1725
1669
      return EINTR;
1726
1670
    }
1730
1674
                   interface);
1731
1675
    }
1732
1676
    
1733
 
    /* Raise privileges */
1734
 
    ret_errno = raise_privileges();
1735
 
    if(ret_errno != 0){
1736
 
      errno = ret_errno;
1737
 
      perror_plus("Failed to raise privileges");
1738
 
    }
 
1677
    /* Raise priviliges */
 
1678
    raise_privileges();
1739
1679
    
1740
1680
#ifdef __linux__
1741
 
    int ret_linux;
1742
 
    bool restore_loglevel = false;
1743
 
    if(ret_errno == 0){
1744
 
      /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1745
 
         messages about the network interface to mess up the prompt */
1746
 
      ret_linux = klogctl(8, NULL, 5);
1747
 
      if(ret_linux == -1){
1748
 
        perror_plus("klogctl");
1749
 
      } else {
1750
 
        restore_loglevel = true;
1751
 
      }
 
1681
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1682
       messages about the network interface to mess up the prompt */
 
1683
    int ret_linux = klogctl(8, NULL, 5);
 
1684
    bool restore_loglevel = true;
 
1685
    if(ret_linux == -1){
 
1686
      restore_loglevel = false;
 
1687
      perror_plus("klogctl");
1752
1688
    }
1753
1689
#endif  /* __linux__ */
1754
 
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1755
 
    ioctl_errno = errno;
 
1690
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1691
    ret_errno = errno;
1756
1692
#ifdef __linux__
1757
1693
    if(restore_loglevel){
1758
1694
      ret_linux = klogctl(7, NULL, 0);
1762
1698
    }
1763
1699
#endif  /* __linux__ */
1764
1700
    
1765
 
    /* If raise_privileges() succeeded above */
1766
 
    if(ret_errno == 0){
1767
 
      /* Lower privileges */
1768
 
      ret_errno = lower_privileges();
1769
 
      if(ret_errno != 0){
1770
 
        errno = ret_errno;
1771
 
        perror_plus("Failed to lower privileges");
1772
 
      }
1773
 
    }
 
1701
    /* Lower privileges */
 
1702
    lower_privileges();
1774
1703
    
1775
1704
    /* Close the socket */
1776
1705
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1779
1708
    }
1780
1709
    
1781
1710
    if(ret_setflags == -1){
1782
 
      errno = ioctl_errno;
 
1711
      errno = ret_errno;
1783
1712
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1784
1713
      errno = old_errno;
1785
 
      return ioctl_errno;
 
1714
      return ret_errno;
1786
1715
    }
1787
1716
  } else if(debug){
1788
1717
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1806
1735
  return 0;
1807
1736
}
1808
1737
 
1809
 
__attribute__((nonnull, warn_unused_result))
1810
1738
error_t take_down_interface(const char *const interface){
1811
1739
  error_t old_errno = errno;
1812
1740
  struct ifreq network;
1817
1745
    return ENXIO;
1818
1746
  }
1819
1747
  if(interface_is_up(interface)){
1820
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
1748
    error_t ret_errno = 0;
1821
1749
    if(not get_flags(interface, &network) and debug){
1822
1750
      ret_errno = errno;
1823
1751
      fprintf_plus(stderr, "Failed to get flags for interface "
1824
1752
                   "\"%s\"\n", interface);
1825
 
      errno = old_errno;
1826
1753
      return ret_errno;
1827
1754
    }
1828
1755
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1829
1756
    
1830
1757
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1831
 
    if(sd == -1){
 
1758
    if(sd < 0){
1832
1759
      ret_errno = errno;
1833
1760
      perror_plus("socket");
1834
1761
      errno = old_errno;
1840
1767
                   interface);
1841
1768
    }
1842
1769
    
1843
 
    /* Raise privileges */
1844
 
    ret_errno = raise_privileges();
1845
 
    if(ret_errno != 0){
1846
 
      errno = ret_errno;
1847
 
      perror_plus("Failed to raise privileges");
1848
 
    }
 
1770
    /* Raise priviliges */
 
1771
    raise_privileges();
1849
1772
    
1850
1773
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1851
 
    ioctl_errno = errno;
 
1774
    ret_errno = errno;
1852
1775
    
1853
 
    /* If raise_privileges() succeeded above */
1854
 
    if(ret_errno == 0){
1855
 
      /* Lower privileges */
1856
 
      ret_errno = lower_privileges();
1857
 
      if(ret_errno != 0){
1858
 
        errno = ret_errno;
1859
 
        perror_plus("Failed to lower privileges");
1860
 
      }
1861
 
    }
 
1776
    /* Lower privileges */
 
1777
    lower_privileges();
1862
1778
    
1863
1779
    /* Close the socket */
1864
1780
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1867
1783
    }
1868
1784
    
1869
1785
    if(ret_setflags == -1){
1870
 
      errno = ioctl_errno;
 
1786
      errno = ret_errno;
1871
1787
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1872
1788
      errno = old_errno;
1873
 
      return ioctl_errno;
 
1789
      return ret_errno;
1874
1790
    }
1875
1791
  } else if(debug){
1876
1792
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1884
1800
int main(int argc, char *argv[]){
1885
1801
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
1886
1802
                        .priority = "SECURE256:!CTYPE-X.509:"
1887
 
                        "+CTYPE-OPENPGP", .current_server = NULL,
 
1803
                        "+CTYPE-OPENPGP", .current_server = NULL, 
1888
1804
                        .interfaces = NULL, .interfaces_size = 0 };
1889
1805
  AvahiSServiceBrowser *sb = NULL;
1890
1806
  error_t ret_errno;
1894
1810
  int exitcode = EXIT_SUCCESS;
1895
1811
  char *interfaces_to_take_down = NULL;
1896
1812
  size_t interfaces_to_take_down_size = 0;
1897
 
  char run_tempdir[] = "/run/tmp/mandosXXXXXX";
1898
 
  char old_tempdir[] = "/tmp/mandosXXXXXX";
1899
 
  char *tempdir = NULL;
 
1813
  char tempdir[] = "/tmp/mandosXXXXXX";
 
1814
  bool tempdir_created = false;
1900
1815
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1901
1816
  const char *seckey = PATHDIR "/" SECKEY;
1902
1817
  const char *pubkey = PATHDIR "/" PUBKEY;
2084
1999
    /* Work around Debian bug #633582:
2085
2000
       <http://bugs.debian.org/633582> */
2086
2001
    
2087
 
    /* Re-raise privileges */
2088
 
    ret_errno = raise_privileges();
2089
 
    if(ret_errno != 0){
2090
 
      errno = ret_errno;
2091
 
      perror_plus("Failed to raise privileges");
2092
 
    } else {
 
2002
    /* Re-raise priviliges */
 
2003
    if(raise_privileges() == 0){
2093
2004
      struct stat st;
2094
2005
      
2095
2006
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2135
2046
      }
2136
2047
    
2137
2048
      /* Lower privileges */
2138
 
      ret_errno = lower_privileges();
2139
 
      if(ret_errno != 0){
2140
 
        errno = ret_errno;
2141
 
        perror_plus("Failed to lower privileges");
2142
 
      }
 
2049
      lower_privileges();
2143
2050
    }
2144
2051
  }
2145
2052
  
2171
2078
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2172
2079
      argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2173
2080
    }
2174
 
    run_network_hooks("start", interfaces_hooks != NULL ?
2175
 
                      interfaces_hooks : "", delay);
 
2081
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
 
2082
                             interfaces_hooks : "", delay)){
 
2083
      goto end;
 
2084
    }
2176
2085
  }
2177
2086
  
2178
2087
  if(not debug){
2256
2165
  
2257
2166
  /* If no interfaces were specified, make a list */
2258
2167
  if(mc.interfaces == NULL){
2259
 
    struct dirent **direntries = NULL;
 
2168
    struct dirent **direntries;
2260
2169
    /* Look for any good interfaces */
2261
2170
    ret = scandir(sys_class_net, &direntries, good_interface,
2262
2171
                  alphasort);
2277
2186
      }
2278
2187
      free(direntries);
2279
2188
    } else {
2280
 
      if(ret == 0){
2281
 
        free(direntries);
2282
 
      }
 
2189
      free(direntries);
2283
2190
      fprintf_plus(stderr, "Could not find a network interface\n");
2284
2191
      exitcode = EXIT_FAILURE;
2285
2192
      goto end;
2308
2215
        break;
2309
2216
      }
2310
2217
      bool interface_was_up = interface_is_up(interface);
2311
 
      errno = bring_up_interface(interface, delay);
 
2218
      ret = bring_up_interface(interface, delay);
2312
2219
      if(not interface_was_up){
2313
 
        if(errno != 0){
 
2220
        if(ret != 0){
 
2221
          errno = ret;
2314
2222
          perror_plus("Failed to bring up interface");
2315
2223
        } else {
2316
 
          errno = argz_add(&interfaces_to_take_down,
2317
 
                           &interfaces_to_take_down_size,
2318
 
                           interface);
2319
 
          if(errno != 0){
 
2224
          ret_errno = argz_add(&interfaces_to_take_down,
 
2225
                               &interfaces_to_take_down_size,
 
2226
                               interface);
 
2227
          if(ret_errno != 0){
 
2228
            errno = ret_errno;
2320
2229
            perror_plus("argz_add");
2321
2230
          }
2322
2231
        }
2353
2262
    goto end;
2354
2263
  }
2355
2264
  
2356
 
  /* Try /run/tmp before /tmp */
2357
 
  tempdir = mkdtemp(run_tempdir);
2358
 
  if(tempdir == NULL and errno == ENOENT){
2359
 
      if(debug){
2360
 
        fprintf_plus(stderr, "Tempdir %s did not work, trying %s\n",
2361
 
                     run_tempdir, old_tempdir);
2362
 
      }
2363
 
      tempdir = mkdtemp(old_tempdir);
2364
 
  }
2365
 
  if(tempdir == NULL){
 
2265
  if(mkdtemp(tempdir) == NULL){
2366
2266
    perror_plus("mkdtemp");
2367
2267
    goto end;
2368
2268
  }
 
2269
  tempdir_created = true;
2369
2270
  
2370
2271
  if(quit_now){
2371
2272
    goto end;
2446
2347
      sleep((unsigned int)retry_interval);
2447
2348
    }
2448
2349
    
2449
 
    if(not quit_now){
 
2350
    if (not quit_now){
2450
2351
      exitcode = EXIT_SUCCESS;
2451
2352
    }
2452
2353
    
2507
2408
  if(debug){
2508
2409
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2509
2410
  }
2510
 
  
 
2411
 
2511
2412
  ret = avahi_loop_with_timeout(simple_poll,
2512
2413
                                (int)(retry_interval * 1000), &mc);
2513
2414
  if(debug){
2554
2455
    }
2555
2456
  }
2556
2457
  
2557
 
  /* Re-raise privileges */
 
2458
  /* Re-raise priviliges */
2558
2459
  {
2559
 
    ret_errno = raise_privileges();
2560
 
    if(ret_errno != 0){
2561
 
      errno = ret_errno;
2562
 
      perror_plus("Failed to raise privileges");
2563
 
    } else {
2564
 
      
2565
 
      /* Run network hooks */
2566
 
      run_network_hooks("stop", interfaces_hooks != NULL ?
2567
 
                        interfaces_hooks : "", delay);
2568
 
      
2569
 
      /* Take down the network interfaces which were brought up */
2570
 
      {
2571
 
        char *interface = NULL;
2572
 
        while((interface=argz_next(interfaces_to_take_down,
2573
 
                                   interfaces_to_take_down_size,
2574
 
                                   interface))){
2575
 
          ret_errno = take_down_interface(interface);
2576
 
          if(ret_errno != 0){
2577
 
            errno = ret_errno;
2578
 
            perror_plus("Failed to take down interface");
2579
 
          }
2580
 
        }
2581
 
        if(debug and (interfaces_to_take_down == NULL)){
2582
 
          fprintf_plus(stderr, "No interfaces needed to be taken"
2583
 
                       " down\n");
2584
 
        }
2585
 
      }
2586
 
    }
2587
 
    
2588
 
    ret_errno = lower_privileges_permanently();
2589
 
    if(ret_errno != 0){
2590
 
      errno = ret_errno;
2591
 
      perror_plus("Failed to lower privileges permanently");
2592
 
    }
 
2460
    raise_privileges();
 
2461
    
 
2462
    /* Run network hooks */
 
2463
    run_network_hooks("stop", interfaces_hooks != NULL ?
 
2464
                      interfaces_hooks : "", delay);
 
2465
    
 
2466
    /* Take down the network interfaces which were brought up */
 
2467
    {
 
2468
      char *interface = NULL;
 
2469
      while((interface=argz_next(interfaces_to_take_down,
 
2470
                                 interfaces_to_take_down_size,
 
2471
                                 interface))){
 
2472
        ret_errno = take_down_interface(interface);
 
2473
        if(ret_errno != 0){
 
2474
          errno = ret_errno;
 
2475
          perror_plus("Failed to take down interface");
 
2476
        }
 
2477
      }
 
2478
      if(debug and (interfaces_to_take_down == NULL)){
 
2479
        fprintf_plus(stderr, "No interfaces needed to be taken"
 
2480
                     " down\n");
 
2481
      }
 
2482
    }
 
2483
    
 
2484
    lower_privileges_permanently();
2593
2485
  }
2594
2486
  
2595
2487
  free(interfaces_to_take_down);
2596
2488
  free(interfaces_hooks);
2597
2489
  
2598
2490
  /* Removes the GPGME temp directory and all files inside */
2599
 
  if(tempdir != NULL){
 
2491
  if(tempdir_created){
2600
2492
    struct dirent **direntries = NULL;
2601
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2602
 
                                                  O_NOFOLLOW));
2603
 
    if(tempdir_fd == -1){
2604
 
      perror_plus("open");
2605
 
    } else {
2606
 
#ifdef __GLIBC__
2607
 
#if __GLIBC_PREREQ(2, 15)
2608
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
2609
 
                                 notdotentries, alphasort);
2610
 
#else  /* not __GLIBC_PREREQ(2, 15) */
2611
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2612
 
                               alphasort);
2613
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
2614
 
#else   /* not __GLIBC__ */
2615
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2616
 
                               alphasort);
2617
 
#endif  /* not __GLIBC__ */
2618
 
      if(numentries >= 0){
2619
 
        for(int i = 0; i < numentries; i++){
2620
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2621
 
          if(ret == -1){
2622
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2623
 
                         " \"%s\", 0): %s\n", tempdir,
2624
 
                         direntries[i]->d_name, strerror(errno));
2625
 
          }
2626
 
        }
2627
 
        
2628
 
        /* need to clean even if 0 because man page doesn't specify */
2629
 
        free(direntries);
2630
 
        if(numentries == -1){
2631
 
          perror_plus("scandir");
2632
 
        }
2633
 
        ret = rmdir(tempdir);
2634
 
        if(ret == -1 and errno != ENOENT){
2635
 
          perror_plus("rmdir");
2636
 
        }
 
2493
    struct dirent *direntry = NULL;
 
2494
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2495
                             alphasort);
 
2496
    if (numentries > 0){
 
2497
      for(int i = 0; i < numentries; i++){
 
2498
        direntry = direntries[i];
 
2499
        char *fullname = NULL;
 
2500
        ret = asprintf(&fullname, "%s/%s", tempdir,
 
2501
                       direntry->d_name);
 
2502
        if(ret < 0){
 
2503
          perror_plus("asprintf");
 
2504
          continue;
 
2505
        }
 
2506
        ret = remove(fullname);
 
2507
        if(ret == -1){
 
2508
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
2509
                       strerror(errno));
 
2510
        }
 
2511
        free(fullname);
2637
2512
      }
2638
 
      TEMP_FAILURE_RETRY(close(tempdir_fd));
 
2513
    }
 
2514
 
 
2515
    /* need to clean even if 0 because man page doesn't specify */
 
2516
    free(direntries);
 
2517
    if (numentries == -1){
 
2518
      perror_plus("scandir");
 
2519
    }
 
2520
    ret = rmdir(tempdir);
 
2521
    if(ret == -1 and errno != ENOENT){
 
2522
      perror_plus("rmdir");
2639
2523
    }
2640
2524
  }
2641
2525