/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-23 19:24:40 UTC
  • Revision ID: teddy@recompile.se-20140323192440-d71iiqxebsxf9u2v
Update GCC warning flags and function attributes to GCC 4.7.

* Makefile (WARN): Update to include almost all warning flags.
* plugin-runner.c (getplugin, add_to_char_array, add_argument,
                   add_environment, set_cloexec_flag,
                   print_out_password): Add attribute
                                        "warn_unused_result".
  (main/parse_opt): Bug fix: Add error checking to --global-env,
                    --env-for, --plugin-dir, and --config-file, and
                    make sure errno does not "leak" from unrelated
                    functions.
* plugins.d/mandos-client.c
  (fprintf_plus, debuggnutls, resolve_callback): Add "nonnull"
                                                 attribute.
  (incbuffer, add_server, init_gpgme): Add "nonnull" and
                                       "warn_unused_result"
                                       attributes.
  (pgp_packet_decrypt, init_gnutls_global): - '' -
  (init_gnutls_session start_mandos_communication, get_flags): - '' -
  (good_flags, good_interface, interface_is_up): - '' -
  (interface_is_running, runnable_hook): - '' -
  (avahi_loop_with_timeout, bring_up_interface): : - '' -
  (safer_gnutls_strerror): Add "warn_unused_result" attribute.
  (notdotentries): Set "nonnull", "pure", and "warn_unused_result"
                   attributes.
  (raise_privileges, raise_privileges_permanently, lower_privileges,
  lower_privileges_permanently): Set "warn_unused_result" attribute.
  (run_network_hooks): Exit child process if it fails to do anything
                       it needs to do.  Make explicit cast to double
                       when passing float value to asprintf().  Change
                       return type to void - all callers changed.
  (bring_up_interface): Move variables "sd", "ret_errno", and
                        "ret_setflags" to innermost scope.  Bug fix:
                        Fail if could not get interface flags also in
                        non-debug mode, and restore old errno
                        correctly.  Print message if could not raise
                        (or later lower) privileges.
  (take_down_interface): Bug fix: When failing because it could not
                         get interface flags, restore old errno
                         correctly.  Print message if it could not
                         raise (or later lower) privileges.
  (main): Complain if failed to raise or lower privileges.  Only run
          network hooks or lower privileges if raising privileges was
          successful.

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
 
260
258
 * Initialize GPGME.
261
259
 */
262
260
__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){
 
261
static bool init_gpgme(const char *seckey, const char *pubkey,
 
262
                       const char *tempdir, mandos_context *mc){
267
263
  gpgme_error_t rc;
268
264
  gpgme_engine_info_t engine_info;
269
265
  
270
266
  /*
271
267
   * Helper function to insert pub and seckey to the engine keyring.
272
268
   */
273
 
  bool import_key(const char * const filename){
 
269
  bool import_key(const char *filename){
274
270
    int ret;
275
271
    int fd;
276
272
    gpgme_data_t pgp_data;
1337
1333
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1338
1334
                "abcdefghijklmnopqrstuvwxyz"
1339
1335
                "0123456789"
1340
 
                "_.-");
 
1336
                "_-");
1341
1337
  if((direntry->d_name)[sret] != '\0'){
1342
1338
    /* Contains non-allowed characters */
1343
1339
    if(debug){
1347
1343
    return 0;
1348
1344
  }
1349
1345
  
1350
 
  ret = fstatat(hookdir_fd, direntry->d_name, &st, 0);
 
1346
  char *fullname = NULL;
 
1347
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1348
  if(ret < 0){
 
1349
    perror_plus("asprintf");
 
1350
    return 0;
 
1351
  }
 
1352
  
 
1353
  ret = stat(fullname, &st);
1351
1354
  if(ret == -1){
1352
1355
    if(debug){
1353
1356
      perror_plus("Could not stat hook");
1458
1461
  error_t ret_errno = 0;
1459
1462
  if(seteuid(0) == -1){
1460
1463
    ret_errno = errno;
 
1464
    perror_plus("seteuid");
1461
1465
  }
1462
1466
  errno = old_errno;
1463
1467
  return ret_errno;
1474
1478
  }
1475
1479
  if(setuid(0) == -1){
1476
1480
    ret_errno = errno;
 
1481
    perror_plus("seteuid");
1477
1482
  }
1478
1483
  errno = old_errno;
1479
1484
  return ret_errno;
1486
1491
  error_t ret_errno = 0;
1487
1492
  if(seteuid(uid) == -1){
1488
1493
    ret_errno = errno;
 
1494
    perror_plus("seteuid");
1489
1495
  }
1490
1496
  errno = old_errno;
1491
1497
  return ret_errno;
1498
1504
  error_t ret_errno = 0;
1499
1505
  if(setuid(uid) == -1){
1500
1506
    ret_errno = errno;
 
1507
    perror_plus("setuid");
1501
1508
  }
1502
1509
  errno = old_errno;
1503
1510
  return ret_errno;
1506
1513
__attribute__((nonnull))
1507
1514
void run_network_hooks(const char *mode, const char *interface,
1508
1515
                       const float delay){
1509
 
  struct dirent **direntries = NULL;
1510
 
  if(hookdir_fd == -1){
1511
 
    hookdir_fd = open(hookdir, O_RDONLY);
1512
 
    if(hookdir_fd == -1){
1513
 
      if(errno == ENOENT){
1514
 
        if(debug){
1515
 
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
1516
 
                       " found\n", hookdir);
1517
 
        }
1518
 
      } else {
1519
 
        perror_plus("open");
1520
 
      }
1521
 
      return;
1522
 
    }
1523
 
  }
1524
 
#ifdef __GLIBC__
1525
 
#if __GLIBC_PREREQ(2, 15)
1526
 
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1527
 
                           runnable_hook, alphasort);
1528
 
#else  /* not __GLIBC_PREREQ(2, 15) */
1529
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1530
 
                         alphasort);
1531
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
1532
 
#else   /* not __GLIBC__ */
1533
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1534
 
                         alphasort);
1535
 
#endif  /* not __GLIBC__ */
 
1516
  struct dirent **direntries;
 
1517
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1518
                         alphasort);
1536
1519
  if(numhooks == -1){
1537
 
    perror_plus("scandir");
1538
 
    return;
1539
 
  }
1540
 
  struct dirent *direntry;
1541
 
  int ret;
1542
 
  int devnull = open("/dev/null", O_RDONLY);
1543
 
  for(int i = 0; i < numhooks; i++){
1544
 
    direntry = direntries[i];
1545
 
    if(debug){
1546
 
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
1547
 
                   direntry->d_name);
 
1520
    if(errno == ENOENT){
 
1521
      if(debug){
 
1522
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1523
                     " found\n", hookdir);
 
1524
      }
 
1525
    } else {
 
1526
      perror_plus("scandir");
1548
1527
    }
1549
 
    pid_t hook_pid = fork();
1550
 
    if(hook_pid == 0){
1551
 
      /* Child */
1552
 
      /* Raise privileges */
1553
 
      errno = raise_privileges_permanently();
1554
 
      if(errno != 0){
1555
 
        perror_plus("Failed to raise privileges");
1556
 
        _exit(EX_NOPERM);
1557
 
      }
1558
 
      /* Set group */
1559
 
      errno = 0;
1560
 
      ret = setgid(0);
1561
 
      if(ret == -1){
1562
 
        perror_plus("setgid");
1563
 
        _exit(EX_NOPERM);
1564
 
      }
1565
 
      /* Reset supplementary groups */
1566
 
      errno = 0;
1567
 
      ret = setgroups(0, NULL);
1568
 
      if(ret == -1){
1569
 
        perror_plus("setgroups");
1570
 
        _exit(EX_NOPERM);
1571
 
      }
1572
 
      ret = dup2(devnull, STDIN_FILENO);
1573
 
      if(ret == -1){
1574
 
        perror_plus("dup2(devnull, STDIN_FILENO)");
1575
 
        _exit(EX_OSERR);
1576
 
      }
1577
 
      ret = close(devnull);
1578
 
      if(ret == -1){
1579
 
        perror_plus("close");
1580
 
        _exit(EX_OSERR);
1581
 
      }
1582
 
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1583
 
      if(ret == -1){
1584
 
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1585
 
        _exit(EX_OSERR);
1586
 
      }
1587
 
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1588
 
      if(ret == -1){
1589
 
        perror_plus("setenv");
1590
 
        _exit(EX_OSERR);
1591
 
      }
1592
 
      ret = setenv("DEVICE", interface, 1);
1593
 
      if(ret == -1){
1594
 
        perror_plus("setenv");
1595
 
        _exit(EX_OSERR);
1596
 
      }
1597
 
      ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1598
 
      if(ret == -1){
1599
 
        perror_plus("setenv");
1600
 
        _exit(EX_OSERR);
1601
 
      }
1602
 
      ret = setenv("MODE", mode, 1);
1603
 
      if(ret == -1){
1604
 
        perror_plus("setenv");
1605
 
        _exit(EX_OSERR);
1606
 
      }
1607
 
      char *delaystring;
1608
 
      ret = asprintf(&delaystring, "%f", (double)delay);
1609
 
      if(ret == -1){
 
1528
  } else {
 
1529
    struct dirent *direntry;
 
1530
    int ret;
 
1531
    int devnull = open("/dev/null", O_RDONLY);
 
1532
    for(int i = 0; i < numhooks; i++){
 
1533
      direntry = direntries[i];
 
1534
      char *fullname = NULL;
 
1535
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1536
      if(ret < 0){
1610
1537
        perror_plus("asprintf");
1611
 
        _exit(EX_OSERR);
1612
 
      }
1613
 
      ret = setenv("DELAY", delaystring, 1);
1614
 
      if(ret == -1){
 
1538
        continue;
 
1539
      }
 
1540
      if(debug){
 
1541
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1542
                     direntry->d_name);
 
1543
      }
 
1544
      pid_t hook_pid = fork();
 
1545
      if(hook_pid == 0){
 
1546
        /* Child */
 
1547
        /* Raise privileges */
 
1548
        if(raise_privileges_permanently() != 0){
 
1549
          perror_plus("Failed to raise privileges");
 
1550
          _exit(EX_NOPERM);
 
1551
        }
 
1552
        /* Set group */
 
1553
        errno = 0;
 
1554
        ret = setgid(0);
 
1555
        if(ret == -1){
 
1556
          perror_plus("setgid");
 
1557
          _exit(EX_NOPERM);
 
1558
        }
 
1559
        /* Reset supplementary groups */
 
1560
        errno = 0;
 
1561
        ret = setgroups(0, NULL);
 
1562
        if(ret == -1){
 
1563
          perror_plus("setgroups");
 
1564
          _exit(EX_NOPERM);
 
1565
        }
 
1566
        ret = dup2(devnull, STDIN_FILENO);
 
1567
        if(ret == -1){
 
1568
          perror_plus("dup2(devnull, STDIN_FILENO)");
 
1569
          _exit(EX_OSERR);
 
1570
        }
 
1571
        ret = close(devnull);
 
1572
        if(ret == -1){
 
1573
          perror_plus("close");
 
1574
          _exit(EX_OSERR);
 
1575
        }
 
1576
        ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
1577
        if(ret == -1){
 
1578
          perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
1579
          _exit(EX_OSERR);
 
1580
        }
 
1581
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1582
        if(ret == -1){
 
1583
          perror_plus("setenv");
 
1584
          _exit(EX_OSERR);
 
1585
        }
 
1586
        ret = setenv("DEVICE", interface, 1);
 
1587
        if(ret == -1){
 
1588
          perror_plus("setenv");
 
1589
          _exit(EX_OSERR);
 
1590
        }
 
1591
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1592
        if(ret == -1){
 
1593
          perror_plus("setenv");
 
1594
          _exit(EX_OSERR);
 
1595
        }
 
1596
        ret = setenv("MODE", mode, 1);
 
1597
        if(ret == -1){
 
1598
          perror_plus("setenv");
 
1599
          _exit(EX_OSERR);
 
1600
        }
 
1601
        char *delaystring;
 
1602
        ret = asprintf(&delaystring, "%f", (double)delay);
 
1603
        if(ret == -1){
 
1604
          perror_plus("asprintf");
 
1605
          _exit(EX_OSERR);
 
1606
        }
 
1607
        ret = setenv("DELAY", delaystring, 1);
 
1608
        if(ret == -1){
 
1609
          free(delaystring);
 
1610
          perror_plus("setenv");
 
1611
          _exit(EX_OSERR);
 
1612
        }
1615
1613
        free(delaystring);
1616
 
        perror_plus("setenv");
1617
 
        _exit(EX_OSERR);
1618
 
      }
1619
 
      free(delaystring);
1620
 
      if(connect_to != NULL){
1621
 
        ret = setenv("CONNECT", connect_to, 1);
1622
 
        if(ret == -1){
1623
 
          perror_plus("setenv");
1624
 
          _exit(EX_OSERR);
1625
 
        }
1626
 
      }
1627
 
      int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
1628
 
      if(hook_fd == -1){
1629
 
        perror_plus("openat");
1630
 
        _exit(EXIT_FAILURE);
1631
 
      }
1632
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1633
 
        perror_plus("close");
1634
 
        _exit(EXIT_FAILURE);
1635
 
      }
1636
 
      if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
1637
 
                 environ) == -1){
1638
 
        perror_plus("fexecve");
1639
 
        _exit(EXIT_FAILURE);
1640
 
      }
1641
 
    } else {
1642
 
      int status;
1643
 
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1644
 
        perror_plus("waitpid");
1645
 
        continue;
1646
 
      }
1647
 
      if(WIFEXITED(status)){
1648
 
        if(WEXITSTATUS(status) != 0){
1649
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1650
 
                       " with status %d\n", direntry->d_name,
1651
 
                       WEXITSTATUS(status));
1652
 
          continue;
1653
 
        }
1654
 
      } else if(WIFSIGNALED(status)){
1655
 
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1656
 
                     " signal %d\n", direntry->d_name,
1657
 
                     WTERMSIG(status));
1658
 
        continue;
 
1614
        if(connect_to != NULL){
 
1615
          ret = setenv("CONNECT", connect_to, 1);
 
1616
          if(ret == -1){
 
1617
            perror_plus("setenv");
 
1618
            _exit(EX_OSERR);
 
1619
          }
 
1620
        }
 
1621
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1622
          perror_plus("execl");
 
1623
          _exit(EXIT_FAILURE);
 
1624
        }
1659
1625
      } else {
1660
 
        fprintf_plus(stderr, "Warning: network hook \"%s\""
1661
 
                     " crashed\n", direntry->d_name);
1662
 
        continue;
1663
 
      }
1664
 
    }
1665
 
    if(debug){
1666
 
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1667
 
                   direntry->d_name);
1668
 
    }
1669
 
  }
1670
 
  free(direntries);
1671
 
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1672
 
    perror_plus("close");
1673
 
  } else {
1674
 
    hookdir_fd = -1;
1675
 
  }
1676
 
  close(devnull);
 
1626
        int status;
 
1627
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1628
          perror_plus("waitpid");
 
1629
          free(fullname);
 
1630
          continue;
 
1631
        }
 
1632
        if(WIFEXITED(status)){
 
1633
          if(WEXITSTATUS(status) != 0){
 
1634
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1635
                         " with status %d\n", direntry->d_name,
 
1636
                         WEXITSTATUS(status));
 
1637
            free(fullname);
 
1638
            continue;
 
1639
          }
 
1640
        } else if(WIFSIGNALED(status)){
 
1641
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1642
                       " signal %d\n", direntry->d_name,
 
1643
                       WTERMSIG(status));
 
1644
          free(fullname);
 
1645
          continue;
 
1646
        } else {
 
1647
          fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1648
                       " crashed\n", direntry->d_name);
 
1649
          free(fullname);
 
1650
          continue;
 
1651
        }
 
1652
      }
 
1653
      free(fullname);
 
1654
      if(debug){
 
1655
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1656
                     direntry->d_name);
 
1657
      }
 
1658
    }
 
1659
    close(devnull);
 
1660
  }
1677
1661
}
1678
1662
 
1679
1663
__attribute__((nonnull, warn_unused_result))
1729
1713
    
1730
1714
    /* Raise privileges */
1731
1715
    ret_errno = raise_privileges();
 
1716
    bool restore_loglevel = false;
1732
1717
    if(ret_errno != 0){
1733
 
      errno = ret_errno;
1734
1718
      perror_plus("Failed to raise privileges");
1735
1719
    }
1736
 
    
1737
1720
#ifdef __linux__
1738
1721
    int ret_linux;
1739
 
    bool restore_loglevel = false;
1740
1722
    if(ret_errno == 0){
1741
1723
      /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1742
1724
         messages about the network interface to mess up the prompt */
1840
1822
    /* Raise privileges */
1841
1823
    ret_errno = raise_privileges();
1842
1824
    if(ret_errno != 0){
1843
 
      errno = ret_errno;
1844
1825
      perror_plus("Failed to raise privileges");
1845
1826
    }
1846
 
    
1847
1827
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1848
1828
    ioctl_errno = errno;
1849
1829
    
1891
1871
  int exitcode = EXIT_SUCCESS;
1892
1872
  char *interfaces_to_take_down = NULL;
1893
1873
  size_t interfaces_to_take_down_size = 0;
1894
 
  char run_tempdir[] = "/run/tmp/mandosXXXXXX";
1895
 
  char old_tempdir[] = "/tmp/mandosXXXXXX";
1896
 
  char *tempdir = NULL;
 
1874
  char tempdir[] = "/tmp/mandosXXXXXX";
 
1875
  bool tempdir_created = false;
1897
1876
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1898
1877
  const char *seckey = PATHDIR "/" SECKEY;
1899
1878
  const char *pubkey = PATHDIR "/" PUBKEY;
2253
2232
  
2254
2233
  /* If no interfaces were specified, make a list */
2255
2234
  if(mc.interfaces == NULL){
2256
 
    struct dirent **direntries = NULL;
 
2235
    struct dirent **direntries;
2257
2236
    /* Look for any good interfaces */
2258
2237
    ret = scandir(sys_class_net, &direntries, good_interface,
2259
2238
                  alphasort);
2274
2253
      }
2275
2254
      free(direntries);
2276
2255
    } else {
2277
 
      if(ret == 0){
2278
 
        free(direntries);
2279
 
      }
 
2256
      free(direntries);
2280
2257
      fprintf_plus(stderr, "Could not find a network interface\n");
2281
2258
      exitcode = EXIT_FAILURE;
2282
2259
      goto end;
2350
2327
    goto end;
2351
2328
  }
2352
2329
  
2353
 
  /* Try /run/tmp before /tmp */
2354
 
  tempdir = mkdtemp(run_tempdir);
2355
 
  if(tempdir == NULL and errno == ENOENT){
2356
 
      if(debug){
2357
 
        fprintf_plus(stderr, "Tempdir %s did not work, trying %s\n",
2358
 
                     run_tempdir, old_tempdir);
2359
 
      }
2360
 
      tempdir = mkdtemp(old_tempdir);
2361
 
  }
2362
 
  if(tempdir == NULL){
 
2330
  if(mkdtemp(tempdir) == NULL){
2363
2331
    perror_plus("mkdtemp");
2364
2332
    goto end;
2365
2333
  }
 
2334
  tempdir_created = true;
2366
2335
  
2367
2336
  if(quit_now){
2368
2337
    goto end;
2504
2473
  if(debug){
2505
2474
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2506
2475
  }
2507
 
  
 
2476
 
2508
2477
  ret = avahi_loop_with_timeout(simple_poll,
2509
2478
                                (int)(retry_interval * 1000), &mc);
2510
2479
  if(debug){
2555
2524
  {
2556
2525
    ret_errno = raise_privileges();
2557
2526
    if(ret_errno != 0){
2558
 
      errno = ret_errno;
2559
2527
      perror_plus("Failed to raise privileges");
2560
2528
    } else {
2561
2529
      
2581
2549
        }
2582
2550
      }
2583
2551
    }
2584
 
    
2585
2552
    ret_errno = lower_privileges_permanently();
2586
2553
    if(ret_errno != 0){
2587
 
      errno = ret_errno;
2588
2554
      perror_plus("Failed to lower privileges permanently");
2589
2555
    }
2590
2556
  }
2593
2559
  free(interfaces_hooks);
2594
2560
  
2595
2561
  /* Removes the GPGME temp directory and all files inside */
2596
 
  if(tempdir != NULL){
 
2562
  if(tempdir_created){
2597
2563
    struct dirent **direntries = NULL;
2598
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2599
 
                                                  O_NOFOLLOW));
2600
 
    if(tempdir_fd == -1){
2601
 
      perror_plus("open");
2602
 
    } else {
2603
 
#ifdef __GLIBC__
2604
 
#if __GLIBC_PREREQ(2, 15)
2605
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
2606
 
                                 notdotentries, alphasort);
2607
 
#else  /* not __GLIBC_PREREQ(2, 15) */
2608
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2609
 
                               alphasort);
2610
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
2611
 
#else   /* not __GLIBC__ */
2612
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2613
 
                               alphasort);
2614
 
#endif  /* not __GLIBC__ */
2615
 
      if(numentries >= 0){
2616
 
        for(int i = 0; i < numentries; i++){
2617
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2618
 
          if(ret == -1){
2619
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2620
 
                         " \"%s\", 0): %s\n", tempdir,
2621
 
                         direntries[i]->d_name, strerror(errno));
2622
 
          }
2623
 
        }
2624
 
        
2625
 
        /* need to clean even if 0 because man page doesn't specify */
2626
 
        free(direntries);
2627
 
        if(numentries == -1){
2628
 
          perror_plus("scandir");
2629
 
        }
2630
 
        ret = rmdir(tempdir);
2631
 
        if(ret == -1 and errno != ENOENT){
2632
 
          perror_plus("rmdir");
2633
 
        }
 
2564
    struct dirent *direntry = NULL;
 
2565
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2566
                             alphasort);
 
2567
    if(numentries > 0){
 
2568
      for(int i = 0; i < numentries; i++){
 
2569
        direntry = direntries[i];
 
2570
        char *fullname = NULL;
 
2571
        ret = asprintf(&fullname, "%s/%s", tempdir,
 
2572
                       direntry->d_name);
 
2573
        if(ret < 0){
 
2574
          perror_plus("asprintf");
 
2575
          continue;
 
2576
        }
 
2577
        ret = remove(fullname);
 
2578
        if(ret == -1){
 
2579
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
2580
                       strerror(errno));
 
2581
        }
 
2582
        free(fullname);
2634
2583
      }
2635
 
      TEMP_FAILURE_RETRY(close(tempdir_fd));
 
2584
    }
 
2585
 
 
2586
    /* need to clean even if 0 because man page doesn't specify */
 
2587
    free(direntries);
 
2588
    if(numentries == -1){
 
2589
      perror_plus("scandir");
 
2590
    }
 
2591
    ret = rmdir(tempdir);
 
2592
    if(ret == -1 and errno != ENOENT){
 
2593
      perror_plus("rmdir");
2636
2594
    }
2637
2595
  }
2638
2596