/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;
246
242
    new_server->next = new_server;
247
243
    new_server->prev = new_server;
248
244
    *current_server = new_server;
 
245
  /* Place the new server last in the list */
249
246
  } else {
250
 
    /* Place the new server last in the list */
251
247
    new_server->next = *current_server;
252
248
    new_server->prev = (*current_server)->prev;
253
249
    new_server->prev->next = new_server;
259
255
/* 
260
256
 * Initialize GPGME.
261
257
 */
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){
 
258
static bool init_gpgme(const char *seckey, const char *pubkey,
 
259
                       const char *tempdir, mandos_context *mc){
267
260
  gpgme_error_t rc;
268
261
  gpgme_engine_info_t engine_info;
269
262
  
270
263
  /*
271
264
   * Helper function to insert pub and seckey to the engine keyring.
272
265
   */
273
 
  bool import_key(const char * const filename){
 
266
  bool import_key(const char *filename){
274
267
    int ret;
275
268
    int fd;
276
269
    gpgme_data_t pgp_data;
357
350
 * Decrypt OpenPGP data.
358
351
 * Returns -1 on error
359
352
 */
360
 
__attribute__((nonnull, warn_unused_result))
361
353
static ssize_t pgp_packet_decrypt(const char *cryptotext,
362
354
                                  size_t crypto_size,
363
355
                                  char **plaintext,
483
475
  return plaintext_length;
484
476
}
485
477
 
486
 
__attribute__((warn_unused_result))
487
 
static const char *safer_gnutls_strerror(int value){
 
478
static const char * safer_gnutls_strerror(int value){
488
479
  const char *ret = gnutls_strerror(value);
489
480
  if(ret == NULL)
490
481
    ret = "(unknown)";
492
483
}
493
484
 
494
485
/* GnuTLS log function callback */
495
 
__attribute__((nonnull))
496
486
static void debuggnutls(__attribute__((unused)) int level,
497
487
                        const char* string){
498
488
  fprintf_plus(stderr, "GnuTLS: %s", string);
499
489
}
500
490
 
501
 
__attribute__((nonnull, warn_unused_result))
502
491
static int init_gnutls_global(const char *pubkeyfilename,
503
492
                              const char *seckeyfilename,
504
493
                              mandos_context *mc){
578
567
  return -1;
579
568
}
580
569
 
581
 
__attribute__((nonnull, warn_unused_result))
582
570
static int init_gnutls_session(gnutls_session_t *session,
583
571
                               mandos_context *mc){
584
572
  int ret;
641
629
                      __attribute__((unused)) const char *txt){}
642
630
 
643
631
/* Called when a Mandos server is found */
644
 
__attribute__((nonnull, warn_unused_result))
645
632
static int start_mandos_communication(const char *ip, in_port_t port,
646
633
                                      AvahiIfIndex if_index,
647
634
                                      int af, mandos_context *mc){
648
635
  int ret, tcp_sd = -1;
649
636
  ssize_t sret;
650
 
  struct sockaddr_storage to;
 
637
  union {
 
638
    struct sockaddr_in in;
 
639
    struct sockaddr_in6 in6;
 
640
  } to;
651
641
  char *buffer = NULL;
652
642
  char *decrypted_buffer = NULL;
653
643
  size_t buffer_length = 0;
734
724
  
735
725
  memset(&to, 0, sizeof(to));
736
726
  if(af == AF_INET6){
737
 
    ((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
738
 
    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);
739
729
  } else {                      /* IPv4 */
740
 
    ((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
741
 
    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);
742
732
  }
743
733
  if(ret < 0 ){
744
734
    int e = errno;
753
743
    goto mandos_end;
754
744
  }
755
745
  if(af == AF_INET6){
756
 
    ((struct sockaddr_in6 *)&to)->sin6_port = htons(port);
757
 
    if(IN6_IS_ADDR_LINKLOCAL
758
 
       (&((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
759
756
      if(if_index == AVAHI_IF_UNSPEC){
760
757
        fprintf_plus(stderr, "An IPv6 link-local address is"
761
758
                     " incomplete without a network interface\n");
763
760
        goto mandos_end;
764
761
      }
765
762
      /* Set the network interface number as scope */
766
 
      ((struct sockaddr_in6 *)&to)->sin6_scope_id = (uint32_t)if_index;
 
763
      to.in6.sin6_scope_id = (uint32_t)if_index;
767
764
    }
768
765
  } else {
769
 
    ((struct sockaddr_in *)&to)->sin_port = htons(port);
 
766
    to.in.sin_port = htons(port);
770
767
  }
771
768
  
772
769
  if(quit_now){
790
787
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
791
788
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
792
789
    if(af == AF_INET6){
793
 
      ret = getnameinfo((struct sockaddr *)&to,
794
 
                        sizeof(struct sockaddr_in6),
 
790
      ret = getnameinfo((struct sockaddr *)&(to.in6), sizeof(to.in6),
795
791
                        addrstr, sizeof(addrstr), NULL, 0,
796
792
                        NI_NUMERICHOST);
797
793
    } else {
798
 
      ret = getnameinfo((struct sockaddr *)&to,
799
 
                        sizeof(struct sockaddr_in),
 
794
      ret = getnameinfo((struct sockaddr *)&(to.in), sizeof(to.in),
800
795
                        addrstr, sizeof(addrstr), NULL, 0,
801
796
                        NI_NUMERICHOST);
802
797
    }
815
810
  }
816
811
  
817
812
  if(af == AF_INET6){
818
 
    ret = connect(tcp_sd, (struct sockaddr *)&to,
819
 
                  sizeof(struct sockaddr_in6));
 
813
    ret = connect(tcp_sd, &to.in6, sizeof(to));
820
814
  } else {
821
 
    ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
822
 
                  sizeof(struct sockaddr_in));
 
815
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
823
816
  }
824
817
  if(ret < 0){
825
 
    if((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
818
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
826
819
      int e = errno;
827
820
      perror_plus("connect");
828
821
      errno = e;
1043
1036
  return retval;
1044
1037
}
1045
1038
 
1046
 
__attribute__((nonnull))
1047
1039
static void resolve_callback(AvahiSServiceResolver *r,
1048
1040
                             AvahiIfIndex interface,
1049
1041
                             AvahiProtocol proto,
1057
1049
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
1058
1050
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
1059
1051
                             flags,
1060
 
                             void *mc){
 
1052
                             void* mc){
1061
1053
  if(r == NULL){
1062
1054
    return;
1063
1055
  }
1116
1108
                            const char *domain,
1117
1109
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1118
1110
                            flags,
1119
 
                            void *mc){
 
1111
                            void* mc){
1120
1112
  if(b == NULL){
1121
1113
    return;
1122
1114
  }
1182
1174
  errno = old_errno;
1183
1175
}
1184
1176
 
1185
 
__attribute__((nonnull, warn_unused_result))
1186
1177
bool get_flags(const char *ifname, struct ifreq *ifr){
1187
1178
  int ret;
1188
1179
  error_t ret_errno;
1207
1198
  return true;
1208
1199
}
1209
1200
 
1210
 
__attribute__((nonnull, warn_unused_result))
1211
1201
bool good_flags(const char *ifname, const struct ifreq *ifr){
1212
1202
  
1213
1203
  /* Reject the loopback device */
1255
1245
 * corresponds to an acceptable network device.
1256
1246
 * (This function is passed to scandir(3) as a filter function.)
1257
1247
 */
1258
 
__attribute__((nonnull, warn_unused_result))
1259
1248
int good_interface(const struct dirent *if_entry){
1260
1249
  if(if_entry->d_name[0] == '.'){
1261
1250
    return 0;
1279
1268
/* 
1280
1269
 * This function determines if a network interface is up.
1281
1270
 */
1282
 
__attribute__((nonnull, warn_unused_result))
1283
1271
bool interface_is_up(const char *interface){
1284
1272
  struct ifreq ifr;
1285
1273
  if(not get_flags(interface, &ifr)){
1296
1284
/* 
1297
1285
 * This function determines if a network interface is running
1298
1286
 */
1299
 
__attribute__((nonnull, warn_unused_result))
1300
1287
bool interface_is_running(const char *interface){
1301
1288
  struct ifreq ifr;
1302
1289
  if(not get_flags(interface, &ifr)){
1310
1297
  return (bool)(ifr.ifr_flags & IFF_RUNNING);
1311
1298
}
1312
1299
 
1313
 
__attribute__((nonnull, pure, warn_unused_result))
1314
1300
int notdotentries(const struct dirent *direntry){
1315
1301
  /* Skip "." and ".." */
1316
1302
  if(direntry->d_name[0] == '.'
1323
1309
}
1324
1310
 
1325
1311
/* Is this directory entry a runnable program? */
1326
 
__attribute__((nonnull, warn_unused_result))
1327
1312
int runnable_hook(const struct dirent *direntry){
1328
1313
  int ret;
1329
1314
  size_t sret;
1337
1322
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1338
1323
                "abcdefghijklmnopqrstuvwxyz"
1339
1324
                "0123456789"
1340
 
                "_.-");
 
1325
                "_-");
1341
1326
  if((direntry->d_name)[sret] != '\0'){
1342
1327
    /* Contains non-allowed characters */
1343
1328
    if(debug){
1347
1332
    return 0;
1348
1333
  }
1349
1334
  
1350
 
  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);
1351
1343
  if(ret == -1){
1352
1344
    if(debug){
1353
1345
      perror_plus("Could not stat hook");
1377
1369
  return 1;
1378
1370
}
1379
1371
 
1380
 
__attribute__((nonnull, warn_unused_result))
1381
1372
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval,
1382
1373
                            mandos_context *mc){
1383
1374
  int ret;
1387
1378
  
1388
1379
  while(true){
1389
1380
    if(mc->current_server == NULL){
1390
 
      if(debug){
 
1381
      if (debug){
1391
1382
        fprintf_plus(stderr, "Wait until first server is found."
1392
1383
                     " No timeout!\n");
1393
1384
      }
1394
1385
      ret = avahi_simple_poll_iterate(s, -1);
1395
1386
    } else {
1396
 
      if(debug){
 
1387
      if (debug){
1397
1388
        fprintf_plus(stderr, "Check current_server if we should run"
1398
1389
                     " it, or wait\n");
1399
1390
      }
1416
1407
                     - ((intmax_t)waited_time.tv_sec * 1000))
1417
1408
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1418
1409
      
1419
 
      if(debug){
 
1410
      if (debug){
1420
1411
        fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n",
1421
1412
                     block_time);
1422
1413
      }
1444
1435
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1445
1436
    }
1446
1437
    if(ret != 0){
1447
 
      if(ret > 0 or errno != EINTR){
 
1438
      if (ret > 0 or errno != EINTR){
1448
1439
        return (ret != 1) ? ret : 0;
1449
1440
      }
1450
1441
    }
1452
1443
}
1453
1444
 
1454
1445
/* Set effective uid to 0, return errno */
1455
 
__attribute__((warn_unused_result))
1456
1446
error_t raise_privileges(void){
1457
1447
  error_t old_errno = errno;
1458
1448
  error_t ret_errno = 0;
1465
1455
}
1466
1456
 
1467
1457
/* Set effective and real user ID to 0.  Return errno. */
1468
 
__attribute__((warn_unused_result))
1469
1458
error_t raise_privileges_permanently(void){
1470
1459
  error_t old_errno = errno;
1471
1460
  error_t ret_errno = raise_privileges();
1482
1471
}
1483
1472
 
1484
1473
/* Set effective user ID to unprivileged saved user ID */
1485
 
__attribute__((warn_unused_result))
1486
1474
error_t lower_privileges(void){
1487
1475
  error_t old_errno = errno;
1488
1476
  error_t ret_errno = 0;
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;
1507
1494
  return ret_errno;
1508
1495
}
1509
1496
 
1510
 
__attribute__((nonnull))
1511
 
void run_network_hooks(const char *mode, const char *interface,
 
1497
bool run_network_hooks(const char *mode, const char *interface,
1512
1498
                       const float delay){
1513
 
  struct dirent **direntries = NULL;
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__ */
 
1499
  struct dirent **direntries;
 
1500
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1501
                         alphasort);
1540
1502
  if(numhooks == -1){
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);
 
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");
1552
1510
    }
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){
 
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
 
      perror_plus("Failed to raise privileges");
1737
 
    }
 
1677
    /* Raise priviliges */
 
1678
    raise_privileges();
1738
1679
    
1739
1680
#ifdef __linux__
1740
 
    int ret_linux;
1741
 
    bool restore_loglevel = false;
1742
 
    if(ret_errno == 0){
1743
 
      /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1744
 
         messages about the network interface to mess up the prompt */
1745
 
      ret_linux = klogctl(8, NULL, 5);
1746
 
      if(ret_linux == -1){
1747
 
        perror_plus("klogctl");
1748
 
      } else {
1749
 
        restore_loglevel = true;
1750
 
      }
 
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");
1751
1688
    }
1752
1689
#endif  /* __linux__ */
1753
 
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1754
 
    ioctl_errno = errno;
 
1690
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1691
    ret_errno = errno;
1755
1692
#ifdef __linux__
1756
1693
    if(restore_loglevel){
1757
1694
      ret_linux = klogctl(7, NULL, 0);
1761
1698
    }
1762
1699
#endif  /* __linux__ */
1763
1700
    
1764
 
    /* If raise_privileges() succeeded above */
1765
 
    if(ret_errno == 0){
1766
 
      /* Lower privileges */
1767
 
      ret_errno = lower_privileges();
1768
 
      if(ret_errno != 0){
1769
 
        errno = ret_errno;
1770
 
        perror_plus("Failed to lower privileges");
1771
 
      }
1772
 
    }
 
1701
    /* Lower privileges */
 
1702
    lower_privileges();
1773
1703
    
1774
1704
    /* Close the socket */
1775
1705
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1778
1708
    }
1779
1709
    
1780
1710
    if(ret_setflags == -1){
1781
 
      errno = ioctl_errno;
 
1711
      errno = ret_errno;
1782
1712
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1783
1713
      errno = old_errno;
1784
 
      return ioctl_errno;
 
1714
      return ret_errno;
1785
1715
    }
1786
1716
  } else if(debug){
1787
1717
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1805
1735
  return 0;
1806
1736
}
1807
1737
 
1808
 
__attribute__((nonnull, warn_unused_result))
1809
1738
error_t take_down_interface(const char *const interface){
1810
1739
  error_t old_errno = errno;
1811
1740
  struct ifreq network;
1816
1745
    return ENXIO;
1817
1746
  }
1818
1747
  if(interface_is_up(interface)){
1819
 
    error_t ret_errno = 0, ioctl_errno = 0;
 
1748
    error_t ret_errno = 0;
1820
1749
    if(not get_flags(interface, &network) and debug){
1821
1750
      ret_errno = errno;
1822
1751
      fprintf_plus(stderr, "Failed to get flags for interface "
1823
1752
                   "\"%s\"\n", interface);
1824
 
      errno = old_errno;
1825
1753
      return ret_errno;
1826
1754
    }
1827
1755
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1828
1756
    
1829
1757
    int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1830
 
    if(sd == -1){
 
1758
    if(sd < 0){
1831
1759
      ret_errno = errno;
1832
1760
      perror_plus("socket");
1833
1761
      errno = old_errno;
1839
1767
                   interface);
1840
1768
    }
1841
1769
    
1842
 
    /* Raise privileges */
1843
 
    ret_errno = raise_privileges();
1844
 
    if(ret_errno != 0){
1845
 
      perror_plus("Failed to raise privileges");
1846
 
    }
 
1770
    /* Raise priviliges */
 
1771
    raise_privileges();
1847
1772
    
1848
1773
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1849
 
    ioctl_errno = errno;
 
1774
    ret_errno = errno;
1850
1775
    
1851
 
    /* If raise_privileges() succeeded above */
1852
 
    if(ret_errno == 0){
1853
 
      /* Lower privileges */
1854
 
      ret_errno = lower_privileges();
1855
 
      if(ret_errno != 0){
1856
 
        errno = ret_errno;
1857
 
        perror_plus("Failed to lower privileges");
1858
 
      }
1859
 
    }
 
1776
    /* Lower privileges */
 
1777
    lower_privileges();
1860
1778
    
1861
1779
    /* Close the socket */
1862
1780
    int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1865
1783
    }
1866
1784
    
1867
1785
    if(ret_setflags == -1){
1868
 
      errno = ioctl_errno;
 
1786
      errno = ret_errno;
1869
1787
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1870
1788
      errno = old_errno;
1871
 
      return ioctl_errno;
 
1789
      return ret_errno;
1872
1790
    }
1873
1791
  } else if(debug){
1874
1792
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1882
1800
int main(int argc, char *argv[]){
1883
1801
  mandos_context mc = { .server = NULL, .dh_bits = 1024,
1884
1802
                        .priority = "SECURE256:!CTYPE-X.509:"
1885
 
                        "+CTYPE-OPENPGP", .current_server = NULL,
 
1803
                        "+CTYPE-OPENPGP", .current_server = NULL, 
1886
1804
                        .interfaces = NULL, .interfaces_size = 0 };
1887
1805
  AvahiSServiceBrowser *sb = NULL;
1888
1806
  error_t ret_errno;
1892
1810
  int exitcode = EXIT_SUCCESS;
1893
1811
  char *interfaces_to_take_down = NULL;
1894
1812
  size_t interfaces_to_take_down_size = 0;
1895
 
  char run_tempdir[] = "/run/tmp/mandosXXXXXX";
1896
 
  char old_tempdir[] = "/tmp/mandosXXXXXX";
1897
 
  char *tempdir = NULL;
 
1813
  char tempdir[] = "/tmp/mandosXXXXXX";
 
1814
  bool tempdir_created = false;
1898
1815
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1899
1816
  const char *seckey = PATHDIR "/" SECKEY;
1900
1817
  const char *pubkey = PATHDIR "/" PUBKEY;
2082
1999
    /* Work around Debian bug #633582:
2083
2000
       <http://bugs.debian.org/633582> */
2084
2001
    
2085
 
    /* Re-raise privileges */
2086
 
    ret_errno = raise_privileges();
2087
 
    if(ret_errno != 0){
2088
 
      errno = ret_errno;
2089
 
      perror_plus("Failed to raise privileges");
2090
 
    } else {
 
2002
    /* Re-raise priviliges */
 
2003
    if(raise_privileges() == 0){
2091
2004
      struct stat st;
2092
2005
      
2093
2006
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
2133
2046
      }
2134
2047
    
2135
2048
      /* Lower privileges */
2136
 
      ret_errno = lower_privileges();
2137
 
      if(ret_errno != 0){
2138
 
        errno = ret_errno;
2139
 
        perror_plus("Failed to lower privileges");
2140
 
      }
 
2049
      lower_privileges();
2141
2050
    }
2142
2051
  }
2143
2052
  
2169
2078
      memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2170
2079
      argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2171
2080
    }
2172
 
    run_network_hooks("start", interfaces_hooks != NULL ?
2173
 
                      interfaces_hooks : "", delay);
 
2081
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
 
2082
                             interfaces_hooks : "", delay)){
 
2083
      goto end;
 
2084
    }
2174
2085
  }
2175
2086
  
2176
2087
  if(not debug){
2254
2165
  
2255
2166
  /* If no interfaces were specified, make a list */
2256
2167
  if(mc.interfaces == NULL){
2257
 
    struct dirent **direntries = NULL;
 
2168
    struct dirent **direntries;
2258
2169
    /* Look for any good interfaces */
2259
2170
    ret = scandir(sys_class_net, &direntries, good_interface,
2260
2171
                  alphasort);
2275
2186
      }
2276
2187
      free(direntries);
2277
2188
    } else {
2278
 
      if(ret == 0){
2279
 
        free(direntries);
2280
 
      }
 
2189
      free(direntries);
2281
2190
      fprintf_plus(stderr, "Could not find a network interface\n");
2282
2191
      exitcode = EXIT_FAILURE;
2283
2192
      goto end;
2306
2215
        break;
2307
2216
      }
2308
2217
      bool interface_was_up = interface_is_up(interface);
2309
 
      errno = bring_up_interface(interface, delay);
 
2218
      ret = bring_up_interface(interface, delay);
2310
2219
      if(not interface_was_up){
2311
 
        if(errno != 0){
 
2220
        if(ret != 0){
 
2221
          errno = ret;
2312
2222
          perror_plus("Failed to bring up interface");
2313
2223
        } else {
2314
 
          errno = argz_add(&interfaces_to_take_down,
2315
 
                           &interfaces_to_take_down_size,
2316
 
                           interface);
2317
 
          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;
2318
2229
            perror_plus("argz_add");
2319
2230
          }
2320
2231
        }
2351
2262
    goto end;
2352
2263
  }
2353
2264
  
2354
 
  /* Try /run/tmp before /tmp */
2355
 
  tempdir = mkdtemp(run_tempdir);
2356
 
  if(tempdir == NULL and errno == ENOENT){
2357
 
      if(debug){
2358
 
        fprintf_plus(stderr, "Tempdir %s did not work, trying %s\n",
2359
 
                     run_tempdir, old_tempdir);
2360
 
      }
2361
 
      tempdir = mkdtemp(old_tempdir);
2362
 
  }
2363
 
  if(tempdir == NULL){
 
2265
  if(mkdtemp(tempdir) == NULL){
2364
2266
    perror_plus("mkdtemp");
2365
2267
    goto end;
2366
2268
  }
 
2269
  tempdir_created = true;
2367
2270
  
2368
2271
  if(quit_now){
2369
2272
    goto end;
2444
2347
      sleep((unsigned int)retry_interval);
2445
2348
    }
2446
2349
    
2447
 
    if(not quit_now){
 
2350
    if (not quit_now){
2448
2351
      exitcode = EXIT_SUCCESS;
2449
2352
    }
2450
2353
    
2505
2408
  if(debug){
2506
2409
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2507
2410
  }
2508
 
  
 
2411
 
2509
2412
  ret = avahi_loop_with_timeout(simple_poll,
2510
2413
                                (int)(retry_interval * 1000), &mc);
2511
2414
  if(debug){
2552
2455
    }
2553
2456
  }
2554
2457
  
2555
 
  /* Re-raise privileges */
 
2458
  /* Re-raise priviliges */
2556
2459
  {
2557
 
    ret_errno = raise_privileges();
2558
 
    if(ret_errno != 0){
2559
 
      perror_plus("Failed to raise privileges");
2560
 
    } else {
2561
 
      
2562
 
      /* Run network hooks */
2563
 
      run_network_hooks("stop", interfaces_hooks != NULL ?
2564
 
                        interfaces_hooks : "", delay);
2565
 
      
2566
 
      /* Take down the network interfaces which were brought up */
2567
 
      {
2568
 
        char *interface = NULL;
2569
 
        while((interface=argz_next(interfaces_to_take_down,
2570
 
                                   interfaces_to_take_down_size,
2571
 
                                   interface))){
2572
 
          ret_errno = take_down_interface(interface);
2573
 
          if(ret_errno != 0){
2574
 
            errno = ret_errno;
2575
 
            perror_plus("Failed to take down interface");
2576
 
          }
2577
 
        }
2578
 
        if(debug and (interfaces_to_take_down == NULL)){
2579
 
          fprintf_plus(stderr, "No interfaces needed to be taken"
2580
 
                       " down\n");
2581
 
        }
2582
 
      }
2583
 
    }
2584
 
    
2585
 
    ret_errno = lower_privileges_permanently();
2586
 
    if(ret_errno != 0){
2587
 
      perror_plus("Failed to lower privileges permanently");
2588
 
    }
 
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();
2589
2485
  }
2590
2486
  
2591
2487
  free(interfaces_to_take_down);
2592
2488
  free(interfaces_hooks);
2593
2489
  
2594
2490
  /* Removes the GPGME temp directory and all files inside */
2595
 
  if(tempdir != NULL){
 
2491
  if(tempdir_created){
2596
2492
    struct dirent **direntries = NULL;
2597
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2598
 
                                                  O_NOFOLLOW));
2599
 
    if(tempdir_fd == -1){
2600
 
      perror_plus("open");
2601
 
    } else {
2602
 
#ifdef __GLIBC__
2603
 
#if __GLIBC_PREREQ(2, 15)
2604
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
2605
 
                                 notdotentries, alphasort);
2606
 
#else  /* not __GLIBC_PREREQ(2, 15) */
2607
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2608
 
                               alphasort);
2609
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
2610
 
#else   /* not __GLIBC__ */
2611
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2612
 
                               alphasort);
2613
 
#endif  /* not __GLIBC__ */
2614
 
      if(numentries >= 0){
2615
 
        for(int i = 0; i < numentries; i++){
2616
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2617
 
          if(ret == -1){
2618
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2619
 
                         " \"%s\", 0): %s\n", tempdir,
2620
 
                         direntries[i]->d_name, strerror(errno));
2621
 
          }
2622
 
        }
2623
 
        
2624
 
        /* need to clean even if 0 because man page doesn't specify */
2625
 
        free(direntries);
2626
 
        if(numentries == -1){
2627
 
          perror_plus("scandir");
2628
 
        }
2629
 
        ret = rmdir(tempdir);
2630
 
        if(ret == -1 and errno != ENOENT){
2631
 
          perror_plus("rmdir");
2632
 
        }
 
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);
2633
2512
      }
2634
 
      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");
2635
2523
    }
2636
2524
  }
2637
2525