/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-27 21:24:33 UTC
  • Revision ID: teddy@recompile.se-20140327212433-oxdvfg1vsaagc6v0
Use fnmatch()in plugin-runner.

* plugin-runner.c (main): Use fnmatch().

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
137
136
 
138
137
bool debug = false;
139
138
static const char mandos_protocol_version[] = "1";
140
 
const char *argp_program_version = "mandos-client " VERSION;
141
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
 
139
const char * argp_program_version = "mandos-client " VERSION;
 
140
const char * argp_program_bug_address = "<mandos@recompile.se>";
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
 
234
232
                          .af = af };
235
233
  if(new_server->ip == NULL){
236
234
    perror_plus("strdup");
237
 
    free(new_server);
238
235
    return false;
239
236
  }
240
237
  ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
241
238
  if(ret == -1){
242
239
    perror_plus("clock_gettime");
243
 
#ifdef __GNUC__
244
 
#pragma GCC diagnostic push
245
 
#pragma GCC diagnostic ignored "-Wcast-qual"
246
 
#endif
247
 
    free((char *)(new_server->ip));
248
 
#ifdef __GNUC__
249
 
#pragma GCC diagnostic pop
250
 
#endif
251
 
    free(new_server);
252
240
    return false;
253
241
  }
254
242
  /* Special case of first server */
1347
1335
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1348
1336
                "abcdefghijklmnopqrstuvwxyz"
1349
1337
                "0123456789"
1350
 
                "_.-");
 
1338
                "_-");
1351
1339
  if((direntry->d_name)[sret] != '\0'){
1352
1340
    /* Contains non-allowed characters */
1353
1341
    if(debug){
1357
1345
    return 0;
1358
1346
  }
1359
1347
  
1360
 
  ret = fstatat(hookdir_fd, direntry->d_name, &st, 0);
 
1348
  char *fullname = NULL;
 
1349
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1350
  if(ret < 0){
 
1351
    perror_plus("asprintf");
 
1352
    return 0;
 
1353
  }
 
1354
  
 
1355
  ret = stat(fullname, &st);
1361
1356
  if(ret == -1){
1362
1357
    if(debug){
1363
1358
      perror_plus("Could not stat hook");
1468
1463
  error_t ret_errno = 0;
1469
1464
  if(seteuid(0) == -1){
1470
1465
    ret_errno = errno;
 
1466
    perror_plus("seteuid");
1471
1467
  }
1472
1468
  errno = old_errno;
1473
1469
  return ret_errno;
1484
1480
  }
1485
1481
  if(setuid(0) == -1){
1486
1482
    ret_errno = errno;
 
1483
    perror_plus("seteuid");
1487
1484
  }
1488
1485
  errno = old_errno;
1489
1486
  return ret_errno;
1496
1493
  error_t ret_errno = 0;
1497
1494
  if(seteuid(uid) == -1){
1498
1495
    ret_errno = errno;
 
1496
    perror_plus("seteuid");
1499
1497
  }
1500
1498
  errno = old_errno;
1501
1499
  return ret_errno;
1508
1506
  error_t ret_errno = 0;
1509
1507
  if(setuid(uid) == -1){
1510
1508
    ret_errno = errno;
 
1509
    perror_plus("setuid");
1511
1510
  }
1512
1511
  errno = old_errno;
1513
1512
  return ret_errno;
1516
1515
__attribute__((nonnull))
1517
1516
void run_network_hooks(const char *mode, const char *interface,
1518
1517
                       const float delay){
1519
 
  struct dirent **direntries = NULL;
1520
 
  if(hookdir_fd == -1){
1521
 
    hookdir_fd = open(hookdir, O_RDONLY);
1522
 
    if(hookdir_fd == -1){
1523
 
      if(errno == ENOENT){
1524
 
        if(debug){
1525
 
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
1526
 
                       " found\n", hookdir);
1527
 
        }
1528
 
      } else {
1529
 
        perror_plus("open");
1530
 
      }
1531
 
      return;
1532
 
    }
1533
 
  }
1534
 
#ifdef __GLIBC__
1535
 
#if __GLIBC_PREREQ(2, 15)
1536
 
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1537
 
                           runnable_hook, alphasort);
1538
 
#else  /* not __GLIBC_PREREQ(2, 15) */
1539
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1540
 
                         alphasort);
1541
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
1542
 
#else   /* not __GLIBC__ */
1543
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1544
 
                         alphasort);
1545
 
#endif  /* not __GLIBC__ */
 
1518
  struct dirent **direntries;
 
1519
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1520
                         alphasort);
1546
1521
  if(numhooks == -1){
1547
 
    perror_plus("scandir");
1548
 
    return;
1549
 
  }
1550
 
  struct dirent *direntry;
1551
 
  int ret;
1552
 
  int devnull = open("/dev/null", O_RDONLY);
1553
 
  for(int i = 0; i < numhooks; i++){
1554
 
    direntry = direntries[i];
1555
 
    if(debug){
1556
 
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
1557
 
                   direntry->d_name);
 
1522
    if(errno == ENOENT){
 
1523
      if(debug){
 
1524
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1525
                     " found\n", hookdir);
 
1526
      }
 
1527
    } else {
 
1528
      perror_plus("scandir");
1558
1529
    }
1559
 
    pid_t hook_pid = fork();
1560
 
    if(hook_pid == 0){
1561
 
      /* Child */
1562
 
      /* Raise privileges */
1563
 
      errno = raise_privileges_permanently();
1564
 
      if(errno != 0){
1565
 
        perror_plus("Failed to raise privileges");
1566
 
        _exit(EX_NOPERM);
1567
 
      }
1568
 
      /* Set group */
1569
 
      errno = 0;
1570
 
      ret = setgid(0);
1571
 
      if(ret == -1){
1572
 
        perror_plus("setgid");
1573
 
        _exit(EX_NOPERM);
1574
 
      }
1575
 
      /* Reset supplementary groups */
1576
 
      errno = 0;
1577
 
      ret = setgroups(0, NULL);
1578
 
      if(ret == -1){
1579
 
        perror_plus("setgroups");
1580
 
        _exit(EX_NOPERM);
1581
 
      }
1582
 
      ret = dup2(devnull, STDIN_FILENO);
1583
 
      if(ret == -1){
1584
 
        perror_plus("dup2(devnull, STDIN_FILENO)");
1585
 
        _exit(EX_OSERR);
1586
 
      }
1587
 
      ret = close(devnull);
1588
 
      if(ret == -1){
1589
 
        perror_plus("close");
1590
 
        _exit(EX_OSERR);
1591
 
      }
1592
 
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1593
 
      if(ret == -1){
1594
 
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1595
 
        _exit(EX_OSERR);
1596
 
      }
1597
 
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1598
 
      if(ret == -1){
1599
 
        perror_plus("setenv");
1600
 
        _exit(EX_OSERR);
1601
 
      }
1602
 
      ret = setenv("DEVICE", interface, 1);
1603
 
      if(ret == -1){
1604
 
        perror_plus("setenv");
1605
 
        _exit(EX_OSERR);
1606
 
      }
1607
 
      ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1608
 
      if(ret == -1){
1609
 
        perror_plus("setenv");
1610
 
        _exit(EX_OSERR);
1611
 
      }
1612
 
      ret = setenv("MODE", mode, 1);
1613
 
      if(ret == -1){
1614
 
        perror_plus("setenv");
1615
 
        _exit(EX_OSERR);
1616
 
      }
1617
 
      char *delaystring;
1618
 
      ret = asprintf(&delaystring, "%f", (double)delay);
1619
 
      if(ret == -1){
 
1530
  } else {
 
1531
    struct dirent *direntry;
 
1532
    int ret;
 
1533
    int devnull = open("/dev/null", O_RDONLY);
 
1534
    for(int i = 0; i < numhooks; i++){
 
1535
      direntry = direntries[i];
 
1536
      char *fullname = NULL;
 
1537
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1538
      if(ret < 0){
1620
1539
        perror_plus("asprintf");
1621
 
        _exit(EX_OSERR);
1622
 
      }
1623
 
      ret = setenv("DELAY", delaystring, 1);
1624
 
      if(ret == -1){
 
1540
        continue;
 
1541
      }
 
1542
      if(debug){
 
1543
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1544
                     direntry->d_name);
 
1545
      }
 
1546
      pid_t hook_pid = fork();
 
1547
      if(hook_pid == 0){
 
1548
        /* Child */
 
1549
        /* Raise privileges */
 
1550
        if(raise_privileges_permanently() != 0){
 
1551
          perror_plus("Failed to raise privileges");
 
1552
          _exit(EX_NOPERM);
 
1553
        }
 
1554
        /* Set group */
 
1555
        errno = 0;
 
1556
        ret = setgid(0);
 
1557
        if(ret == -1){
 
1558
          perror_plus("setgid");
 
1559
          _exit(EX_NOPERM);
 
1560
        }
 
1561
        /* Reset supplementary groups */
 
1562
        errno = 0;
 
1563
        ret = setgroups(0, NULL);
 
1564
        if(ret == -1){
 
1565
          perror_plus("setgroups");
 
1566
          _exit(EX_NOPERM);
 
1567
        }
 
1568
        ret = dup2(devnull, STDIN_FILENO);
 
1569
        if(ret == -1){
 
1570
          perror_plus("dup2(devnull, STDIN_FILENO)");
 
1571
          _exit(EX_OSERR);
 
1572
        }
 
1573
        ret = close(devnull);
 
1574
        if(ret == -1){
 
1575
          perror_plus("close");
 
1576
          _exit(EX_OSERR);
 
1577
        }
 
1578
        ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
1579
        if(ret == -1){
 
1580
          perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
1581
          _exit(EX_OSERR);
 
1582
        }
 
1583
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1584
        if(ret == -1){
 
1585
          perror_plus("setenv");
 
1586
          _exit(EX_OSERR);
 
1587
        }
 
1588
        ret = setenv("DEVICE", interface, 1);
 
1589
        if(ret == -1){
 
1590
          perror_plus("setenv");
 
1591
          _exit(EX_OSERR);
 
1592
        }
 
1593
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1594
        if(ret == -1){
 
1595
          perror_plus("setenv");
 
1596
          _exit(EX_OSERR);
 
1597
        }
 
1598
        ret = setenv("MODE", mode, 1);
 
1599
        if(ret == -1){
 
1600
          perror_plus("setenv");
 
1601
          _exit(EX_OSERR);
 
1602
        }
 
1603
        char *delaystring;
 
1604
        ret = asprintf(&delaystring, "%f", (double)delay);
 
1605
        if(ret == -1){
 
1606
          perror_plus("asprintf");
 
1607
          _exit(EX_OSERR);
 
1608
        }
 
1609
        ret = setenv("DELAY", delaystring, 1);
 
1610
        if(ret == -1){
 
1611
          free(delaystring);
 
1612
          perror_plus("setenv");
 
1613
          _exit(EX_OSERR);
 
1614
        }
1625
1615
        free(delaystring);
1626
 
        perror_plus("setenv");
1627
 
        _exit(EX_OSERR);
1628
 
      }
1629
 
      free(delaystring);
1630
 
      if(connect_to != NULL){
1631
 
        ret = setenv("CONNECT", connect_to, 1);
1632
 
        if(ret == -1){
1633
 
          perror_plus("setenv");
1634
 
          _exit(EX_OSERR);
1635
 
        }
1636
 
      }
1637
 
      int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
1638
 
      if(hook_fd == -1){
1639
 
        perror_plus("openat");
1640
 
        _exit(EXIT_FAILURE);
1641
 
      }
1642
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1643
 
        perror_plus("close");
1644
 
        _exit(EXIT_FAILURE);
1645
 
      }
1646
 
      if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
1647
 
                 environ) == -1){
1648
 
        perror_plus("fexecve");
1649
 
        _exit(EXIT_FAILURE);
1650
 
      }
1651
 
    } else {
1652
 
      int status;
1653
 
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1654
 
        perror_plus("waitpid");
1655
 
        continue;
1656
 
      }
1657
 
      if(WIFEXITED(status)){
1658
 
        if(WEXITSTATUS(status) != 0){
1659
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1660
 
                       " with status %d\n", direntry->d_name,
1661
 
                       WEXITSTATUS(status));
1662
 
          continue;
1663
 
        }
1664
 
      } else if(WIFSIGNALED(status)){
1665
 
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1666
 
                     " signal %d\n", direntry->d_name,
1667
 
                     WTERMSIG(status));
1668
 
        continue;
 
1616
        if(connect_to != NULL){
 
1617
          ret = setenv("CONNECT", connect_to, 1);
 
1618
          if(ret == -1){
 
1619
            perror_plus("setenv");
 
1620
            _exit(EX_OSERR);
 
1621
          }
 
1622
        }
 
1623
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1624
          perror_plus("execl");
 
1625
          _exit(EXIT_FAILURE);
 
1626
        }
1669
1627
      } else {
1670
 
        fprintf_plus(stderr, "Warning: network hook \"%s\""
1671
 
                     " crashed\n", direntry->d_name);
1672
 
        continue;
1673
 
      }
1674
 
    }
1675
 
    if(debug){
1676
 
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1677
 
                   direntry->d_name);
1678
 
    }
1679
 
  }
1680
 
  free(direntries);
1681
 
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1682
 
    perror_plus("close");
1683
 
  } else {
1684
 
    hookdir_fd = -1;
1685
 
  }
1686
 
  close(devnull);
 
1628
        int status;
 
1629
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1630
          perror_plus("waitpid");
 
1631
          free(fullname);
 
1632
          continue;
 
1633
        }
 
1634
        if(WIFEXITED(status)){
 
1635
          if(WEXITSTATUS(status) != 0){
 
1636
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1637
                         " with status %d\n", direntry->d_name,
 
1638
                         WEXITSTATUS(status));
 
1639
            free(fullname);
 
1640
            continue;
 
1641
          }
 
1642
        } else if(WIFSIGNALED(status)){
 
1643
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1644
                       " signal %d\n", direntry->d_name,
 
1645
                       WTERMSIG(status));
 
1646
          free(fullname);
 
1647
          continue;
 
1648
        } else {
 
1649
          fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1650
                       " crashed\n", direntry->d_name);
 
1651
          free(fullname);
 
1652
          continue;
 
1653
        }
 
1654
      }
 
1655
      free(fullname);
 
1656
      if(debug){
 
1657
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1658
                     direntry->d_name);
 
1659
      }
 
1660
    }
 
1661
    close(devnull);
 
1662
  }
1687
1663
}
1688
1664
 
1689
1665
__attribute__((nonnull, warn_unused_result))
1739
1715
    
1740
1716
    /* Raise privileges */
1741
1717
    ret_errno = raise_privileges();
 
1718
    bool restore_loglevel = false;
1742
1719
    if(ret_errno != 0){
1743
 
      errno = ret_errno;
1744
1720
      perror_plus("Failed to raise privileges");
1745
1721
    }
1746
 
    
1747
1722
#ifdef __linux__
1748
1723
    int ret_linux;
1749
 
    bool restore_loglevel = false;
1750
1724
    if(ret_errno == 0){
1751
1725
      /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1752
1726
         messages about the network interface to mess up the prompt */
1850
1824
    /* Raise privileges */
1851
1825
    ret_errno = raise_privileges();
1852
1826
    if(ret_errno != 0){
1853
 
      errno = ret_errno;
1854
1827
      perror_plus("Failed to raise privileges");
1855
1828
    }
1856
 
    
1857
1829
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1858
1830
    ioctl_errno = errno;
1859
1831
    
2263
2235
  
2264
2236
  /* If no interfaces were specified, make a list */
2265
2237
  if(mc.interfaces == NULL){
2266
 
    struct dirent **direntries = NULL;
 
2238
    struct dirent **direntries;
2267
2239
    /* Look for any good interfaces */
2268
2240
    ret = scandir(sys_class_net, &direntries, good_interface,
2269
2241
                  alphasort);
2284
2256
      }
2285
2257
      free(direntries);
2286
2258
    } else {
2287
 
      if(ret == 0){
2288
 
        free(direntries);
2289
 
      }
 
2259
      free(direntries);
2290
2260
      fprintf_plus(stderr, "Could not find a network interface\n");
2291
2261
      exitcode = EXIT_FAILURE;
2292
2262
      goto end;
2514
2484
  if(debug){
2515
2485
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2516
2486
  }
2517
 
  
 
2487
 
2518
2488
  ret = avahi_loop_with_timeout(simple_poll,
2519
2489
                                (int)(retry_interval * 1000), &mc);
2520
2490
  if(debug){
2556
2526
    mc.current_server->prev->next = NULL;
2557
2527
    while(mc.current_server != NULL){
2558
2528
      server *next = mc.current_server->next;
2559
 
#ifdef __GNUC__
2560
 
#pragma GCC diagnostic push
2561
 
#pragma GCC diagnostic ignored "-Wcast-qual"
2562
 
#endif
2563
 
      free((char *)(mc.current_server->ip));
2564
 
#ifdef __GNUC__
2565
 
#pragma GCC diagnostic pop
2566
 
#endif
2567
2529
      free(mc.current_server);
2568
2530
      mc.current_server = next;
2569
2531
    }
2573
2535
  {
2574
2536
    ret_errno = raise_privileges();
2575
2537
    if(ret_errno != 0){
2576
 
      errno = ret_errno;
2577
2538
      perror_plus("Failed to raise privileges");
2578
2539
    } else {
2579
2540
      
2599
2560
        }
2600
2561
      }
2601
2562
    }
2602
 
    
2603
2563
    ret_errno = lower_privileges_permanently();
2604
2564
    if(ret_errno != 0){
2605
 
      errno = ret_errno;
2606
2565
      perror_plus("Failed to lower privileges permanently");
2607
2566
    }
2608
2567
  }
2613
2572
  /* Removes the GPGME temp directory and all files inside */
2614
2573
  if(tempdir != NULL){
2615
2574
    struct dirent **direntries = NULL;
2616
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2617
 
                                                  O_NOFOLLOW));
2618
 
    if(tempdir_fd == -1){
2619
 
      perror_plus("open");
2620
 
    } else {
2621
 
#ifdef __GLIBC__
2622
 
#if __GLIBC_PREREQ(2, 15)
2623
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
2624
 
                                 notdotentries, alphasort);
2625
 
#else  /* not __GLIBC_PREREQ(2, 15) */
2626
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2627
 
                               alphasort);
2628
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
2629
 
#else   /* not __GLIBC__ */
2630
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2631
 
                               alphasort);
2632
 
#endif  /* not __GLIBC__ */
2633
 
      if(numentries >= 0){
2634
 
        for(int i = 0; i < numentries; i++){
2635
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2636
 
          if(ret == -1){
2637
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2638
 
                         " \"%s\", 0): %s\n", tempdir,
2639
 
                         direntries[i]->d_name, strerror(errno));
2640
 
          }
2641
 
        }
2642
 
        
2643
 
        /* need to clean even if 0 because man page doesn't specify */
2644
 
        free(direntries);
2645
 
        if(numentries == -1){
2646
 
          perror_plus("scandir");
2647
 
        }
2648
 
        ret = rmdir(tempdir);
2649
 
        if(ret == -1 and errno != ENOENT){
2650
 
          perror_plus("rmdir");
2651
 
        }
 
2575
    struct dirent *direntry = NULL;
 
2576
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2577
                             alphasort);
 
2578
    if(numentries > 0){
 
2579
      for(int i = 0; i < numentries; i++){
 
2580
        direntry = direntries[i];
 
2581
        char *fullname = NULL;
 
2582
        ret = asprintf(&fullname, "%s/%s", tempdir,
 
2583
                       direntry->d_name);
 
2584
        if(ret < 0){
 
2585
          perror_plus("asprintf");
 
2586
          continue;
 
2587
        }
 
2588
        ret = remove(fullname);
 
2589
        if(ret == -1){
 
2590
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
2591
                       strerror(errno));
 
2592
        }
 
2593
        free(fullname);
2652
2594
      }
2653
 
      TEMP_FAILURE_RETRY(close(tempdir_fd));
 
2595
    }
 
2596
 
 
2597
    /* need to clean even if 0 because man page doesn't specify */
 
2598
    free(direntries);
 
2599
    if(numentries == -1){
 
2600
      perror_plus("scandir");
 
2601
    }
 
2602
    ret = rmdir(tempdir);
 
2603
    if(ret == -1 and errno != ENOENT){
 
2604
      perror_plus("rmdir");
2654
2605
    }
2655
2606
  }
2656
2607