1509
1516
__attribute__((nonnull))
 
1510
1517
void run_network_hooks(const char *mode, const char *interface,
 
1511
1518
                       const float delay){
 
1512
 
  struct dirent **direntries = NULL;
 
1513
 
  if(hookdir_fd == -1){
 
1514
 
    hookdir_fd = open(hookdir, O_RDONLY);
 
1515
 
    if(hookdir_fd == -1){
 
1516
 
      if(errno == ENOENT){
 
1518
 
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1519
 
                       " found\n", hookdir);
 
1522
 
        perror_plus("open");
 
1528
 
#if __GLIBC_PREREQ(2, 15)
 
1529
 
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
 
1530
 
                           runnable_hook, alphasort);
 
1531
 
#else  /* not __GLIBC_PREREQ(2, 15) */
 
1532
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1534
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
1535
 
#else   /* not __GLIBC__ */
 
1536
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1538
 
#endif  /* not __GLIBC__ */
 
 
1519
  struct dirent **direntries;
 
 
1520
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1539
1522
  if(numhooks == -1){
 
1540
 
    perror_plus("scandir");
 
1543
 
  struct dirent *direntry;
 
1545
 
  int devnull = open("/dev/null", O_RDONLY);
 
1546
 
  for(int i = 0; i < numhooks; i++){
 
1547
 
    direntry = direntries[i];
 
1549
 
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
 
1523
    if(errno == ENOENT){
 
 
1525
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
 
1526
                     " found\n", hookdir);
 
 
1529
      perror_plus("scandir");
 
1552
 
    pid_t hook_pid = fork();
 
1555
 
      /* Raise privileges */
 
1556
 
      errno = raise_privileges_permanently();
 
1558
 
        perror_plus("Failed to raise privileges");
 
1565
 
        perror_plus("setgid");
 
1568
 
      /* Reset supplementary groups */
 
1570
 
      ret = setgroups(0, NULL);
 
1572
 
        perror_plus("setgroups");
 
1575
 
      ret = dup2(devnull, STDIN_FILENO);
 
1577
 
        perror_plus("dup2(devnull, STDIN_FILENO)");
 
1580
 
      ret = close(devnull);
 
1582
 
        perror_plus("close");
 
1585
 
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
1587
 
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
1590
 
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1592
 
        perror_plus("setenv");
 
1595
 
      ret = setenv("DEVICE", interface, 1);
 
1597
 
        perror_plus("setenv");
 
1600
 
      ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1602
 
        perror_plus("setenv");
 
1605
 
      ret = setenv("MODE", mode, 1);
 
1607
 
        perror_plus("setenv");
 
1611
 
      ret = asprintf(&delaystring, "%f", (double)delay);
 
 
1532
    struct dirent *direntry;
 
 
1534
    int devnull = open("/dev/null", O_RDONLY);
 
 
1535
    for(int i = 0; i < numhooks; i++){
 
 
1536
      direntry = direntries[i];
 
 
1537
      char *fullname = NULL;
 
 
1538
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1613
1540
        perror_plus("asprintf");
 
1616
 
      ret = setenv("DELAY", delaystring, 1);
 
 
1544
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
 
1547
      pid_t hook_pid = fork();
 
 
1550
        /* Raise privileges */
 
 
1551
        if(raise_privileges_permanently() != 0){
 
 
1552
          perror_plus("Failed to raise privileges");
 
 
1559
          perror_plus("setgid");
 
 
1562
        /* Reset supplementary groups */
 
 
1564
        ret = setgroups(0, NULL);
 
 
1566
          perror_plus("setgroups");
 
 
1569
        ret = dup2(devnull, STDIN_FILENO);
 
 
1571
          perror_plus("dup2(devnull, STDIN_FILENO)");
 
 
1574
        ret = close(devnull);
 
 
1576
          perror_plus("close");
 
 
1579
        ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
 
1581
          perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
 
1584
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
 
1586
          perror_plus("setenv");
 
 
1589
        ret = setenv("DEVICE", interface, 1);
 
 
1591
          perror_plus("setenv");
 
 
1594
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
 
1596
          perror_plus("setenv");
 
 
1599
        ret = setenv("MODE", mode, 1);
 
 
1601
          perror_plus("setenv");
 
 
1605
        ret = asprintf(&delaystring, "%f", (double)delay);
 
 
1607
          perror_plus("asprintf");
 
 
1610
        ret = setenv("DELAY", delaystring, 1);
 
 
1613
          perror_plus("setenv");
 
1618
1616
        free(delaystring);
 
1619
 
        perror_plus("setenv");
 
1623
 
      if(connect_to != NULL){
 
1624
 
        ret = setenv("CONNECT", connect_to, 1);
 
1626
 
          perror_plus("setenv");
 
1630
 
      int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
 
1632
 
        perror_plus("openat");
 
1633
 
        _exit(EXIT_FAILURE);
 
1635
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
1636
 
        perror_plus("close");
 
1637
 
        _exit(EXIT_FAILURE);
 
1639
 
      if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
 
1641
 
        perror_plus("fexecve");
 
1642
 
        _exit(EXIT_FAILURE);
 
1646
 
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1647
 
        perror_plus("waitpid");
 
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));
 
1657
 
      } else if(WIFSIGNALED(status)){
 
1658
 
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1659
 
                     " signal %d\n", direntry->d_name,
 
 
1617
        if(connect_to != NULL){
 
 
1618
          ret = setenv("CONNECT", connect_to, 1);
 
 
1620
            perror_plus("setenv");
 
 
1624
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
 
1625
          perror_plus("execl");
 
 
1626
          _exit(EXIT_FAILURE);
 
1663
 
        fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1664
 
                     " crashed\n", direntry->d_name);
 
1669
 
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1674
 
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
1675
 
    perror_plus("close");
 
 
1630
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
 
1631
          perror_plus("waitpid");
 
 
1635
        if(WIFEXITED(status)){
 
 
1636
          if(WEXITSTATUS(status) != 0){
 
 
1637
            fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
 
1638
                         " with status %d\n", direntry->d_name,
 
 
1639
                         WEXITSTATUS(status));
 
 
1643
        } else if(WIFSIGNALED(status)){
 
 
1644
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
 
1645
                       " signal %d\n", direntry->d_name,
 
 
1650
          fprintf_plus(stderr, "Warning: network hook \"%s\""
 
 
1651
                       " crashed\n", direntry->d_name);
 
 
1658
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1682
1666
__attribute__((nonnull, warn_unused_result))
 
 
2598
2576
  /* Removes the GPGME temp directory and all files inside */
 
2599
2577
  if(tempdir != NULL){
 
2600
2578
    struct dirent **direntries = NULL;
 
2601
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
 
2603
 
    if(tempdir_fd == -1){
 
2604
 
      perror_plus("open");
 
2607
 
#if __GLIBC_PREREQ(2, 15)
 
2608
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
 
2609
 
                                 notdotentries, alphasort);
 
2610
 
#else  /* not __GLIBC_PREREQ(2, 15) */
 
2611
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
2613
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
2614
 
#else   /* not __GLIBC__ */
 
2615
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
2617
 
#endif  /* not __GLIBC__ */
 
2618
 
      if(numentries >= 0){
 
2619
 
        for(int i = 0; i < numentries; i++){
 
2620
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
 
2622
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
 
2623
 
                         " \"%s\", 0): %s\n", tempdir,
 
2624
 
                         direntries[i]->d_name, strerror(errno));
 
2628
 
        /* need to clean even if 0 because man page doesn't specify */
 
2630
 
        if(numentries == -1){
 
2631
 
          perror_plus("scandir");
 
2633
 
        ret = rmdir(tempdir);
 
2634
 
        if(ret == -1 and errno != ENOENT){
 
2635
 
          perror_plus("rmdir");
 
 
2579
    struct dirent *direntry = NULL;
 
 
2580
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
 
2583
      for(int i = 0; i < numentries; i++){
 
 
2584
        direntry = direntries[i];
 
 
2585
        char *fullname = NULL;
 
 
2586
        ret = asprintf(&fullname, "%s/%s", tempdir,
 
 
2589
          perror_plus("asprintf");
 
 
2592
        ret = remove(fullname);
 
 
2594
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
2638
 
      TEMP_FAILURE_RETRY(close(tempdir_fd));
 
 
2601
    /* need to clean even if 0 because man page doesn't specify */
 
 
2603
    if(numentries == -1){
 
 
2604
      perror_plus("scandir");
 
 
2606
    ret = rmdir(tempdir);
 
 
2607
    if(ret == -1 and errno != ENOENT){
 
 
2608
      perror_plus("rmdir");