1517
1515
__attribute__((nonnull))
 
1518
1516
void run_network_hooks(const char *mode, const char *interface,
 
1519
1517
                       const float delay){
 
1520
 
  struct dirent **direntries = NULL;
 
1521
 
  if(hookdir_fd == -1){
 
1522
 
    hookdir_fd = open(hookdir, O_RDONLY);
 
1523
 
    if(hookdir_fd == -1){
 
1524
 
      if(errno == ENOENT){
 
1526
 
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1527
 
                       " found\n", hookdir);
 
1530
 
        perror_plus("open");
 
1536
 
#if __GLIBC_PREREQ(2, 15)
 
1537
 
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
 
1538
 
                           runnable_hook, alphasort);
 
1539
 
#else  /* not __GLIBC_PREREQ(2, 15) */
 
1540
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1542
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
1543
 
#else   /* not __GLIBC__ */
 
1544
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1546
 
#endif  /* not __GLIBC__ */
 
 
1518
  struct dirent **direntries;
 
 
1519
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1547
1521
  if(numhooks == -1){
 
1548
 
    perror_plus("scandir");
 
1551
 
  struct dirent *direntry;
 
1553
 
  int devnull = open("/dev/null", O_RDONLY);
 
1554
 
  for(int i = 0; i < numhooks; i++){
 
1555
 
    direntry = direntries[i];
 
1557
 
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
 
1522
    if(errno == ENOENT){
 
 
1524
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
 
1525
                     " found\n", hookdir);
 
 
1528
      perror_plus("scandir");
 
1560
 
    pid_t hook_pid = fork();
 
1563
 
      /* Raise privileges */
 
1564
 
      errno = raise_privileges_permanently();
 
1566
 
        perror_plus("Failed to raise privileges");
 
1573
 
        perror_plus("setgid");
 
1576
 
      /* Reset supplementary groups */
 
1578
 
      ret = setgroups(0, NULL);
 
1580
 
        perror_plus("setgroups");
 
1583
 
      ret = dup2(devnull, STDIN_FILENO);
 
1585
 
        perror_plus("dup2(devnull, STDIN_FILENO)");
 
1588
 
      ret = close(devnull);
 
1590
 
        perror_plus("close");
 
1593
 
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
1595
 
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
1598
 
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1600
 
        perror_plus("setenv");
 
1603
 
      ret = setenv("DEVICE", interface, 1);
 
1605
 
        perror_plus("setenv");
 
1608
 
      ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1610
 
        perror_plus("setenv");
 
1613
 
      ret = setenv("MODE", mode, 1);
 
1615
 
        perror_plus("setenv");
 
1619
 
      ret = asprintf(&delaystring, "%f", (double)delay);
 
 
1531
    struct dirent *direntry;
 
 
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);
 
1621
1539
        perror_plus("asprintf");
 
1624
 
      ret = setenv("DELAY", delaystring, 1);
 
 
1543
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
 
1546
      pid_t hook_pid = fork();
 
 
1549
        /* Raise privileges */
 
 
1550
        if(raise_privileges_permanently() != 0){
 
 
1551
          perror_plus("Failed to raise privileges");
 
 
1558
          perror_plus("setgid");
 
 
1561
        /* Reset supplementary groups */
 
 
1563
        ret = setgroups(0, NULL);
 
 
1565
          perror_plus("setgroups");
 
 
1568
        ret = dup2(devnull, STDIN_FILENO);
 
 
1570
          perror_plus("dup2(devnull, STDIN_FILENO)");
 
 
1573
        ret = close(devnull);
 
 
1575
          perror_plus("close");
 
 
1578
        ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
 
1580
          perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
 
1583
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
 
1585
          perror_plus("setenv");
 
 
1588
        ret = setenv("DEVICE", interface, 1);
 
 
1590
          perror_plus("setenv");
 
 
1593
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
 
1595
          perror_plus("setenv");
 
 
1598
        ret = setenv("MODE", mode, 1);
 
 
1600
          perror_plus("setenv");
 
 
1604
        ret = asprintf(&delaystring, "%f", (double)delay);
 
 
1606
          perror_plus("asprintf");
 
 
1609
        ret = setenv("DELAY", delaystring, 1);
 
 
1612
          perror_plus("setenv");
 
1626
1615
        free(delaystring);
 
1627
 
        perror_plus("setenv");
 
1631
 
      if(connect_to != NULL){
 
1632
 
        ret = setenv("CONNECT", connect_to, 1);
 
1634
 
          perror_plus("setenv");
 
1638
 
      int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
 
1640
 
        perror_plus("openat");
 
1641
 
        _exit(EXIT_FAILURE);
 
1643
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
1644
 
        perror_plus("close");
 
1645
 
        _exit(EXIT_FAILURE);
 
1647
 
      if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
 
1649
 
        perror_plus("fexecve");
 
1650
 
        _exit(EXIT_FAILURE);
 
1654
 
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1655
 
        perror_plus("waitpid");
 
1659
 
      if(WIFEXITED(status)){
 
1660
 
        if(WEXITSTATUS(status) != 0){
 
1661
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1662
 
                       " with status %d\n", direntry->d_name,
 
1663
 
                       WEXITSTATUS(status));
 
1667
 
      } else if(WIFSIGNALED(status)){
 
1668
 
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1669
 
                     " signal %d\n", direntry->d_name,
 
 
1616
        if(connect_to != NULL){
 
 
1617
          ret = setenv("CONNECT", connect_to, 1);
 
 
1619
            perror_plus("setenv");
 
 
1623
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
 
1624
          perror_plus("execl");
 
 
1625
          _exit(EXIT_FAILURE);
 
1674
 
        fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1675
 
                     " crashed\n", direntry->d_name);
 
1681
 
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1687
 
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
1688
 
    perror_plus("close");
 
 
1629
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
 
1630
          perror_plus("waitpid");
 
 
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));
 
 
1642
        } else if(WIFSIGNALED(status)){
 
 
1643
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
 
1644
                       " signal %d\n", direntry->d_name,
 
 
1649
          fprintf_plus(stderr, "Warning: network hook \"%s\""
 
 
1650
                       " crashed\n", direntry->d_name);
 
 
1657
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1695
1665
__attribute__((nonnull, warn_unused_result))
 
 
2621
2572
  /* Removes the GPGME temp directory and all files inside */
 
2622
2573
  if(tempdir != NULL){
 
2623
2574
    struct dirent **direntries = NULL;
 
2624
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
 
2626
 
    if(tempdir_fd == -1){
 
2627
 
      perror_plus("open");
 
2630
 
#if __GLIBC_PREREQ(2, 15)
 
2631
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
 
2632
 
                                 notdotentries, alphasort);
 
2633
 
#else  /* not __GLIBC_PREREQ(2, 15) */
 
2634
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
2636
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
2637
 
#else   /* not __GLIBC__ */
 
2638
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
2640
 
#endif  /* not __GLIBC__ */
 
2641
 
      if(numentries >= 0){
 
2642
 
        for(int i = 0; i < numentries; i++){
 
2643
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
 
2645
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
 
2646
 
                         " \"%s\", 0): %s\n", tempdir,
 
2647
 
                         direntries[i]->d_name, strerror(errno));
 
2649
 
          free(direntries[i]);
 
2652
 
        /* need to clean even if 0 because man page doesn't specify */
 
2654
 
        if(numentries == -1){
 
2655
 
          perror_plus("scandir");
 
2657
 
        ret = rmdir(tempdir);
 
2658
 
        if(ret == -1 and errno != ENOENT){
 
2659
 
          perror_plus("rmdir");
 
 
2575
    struct dirent *direntry = NULL;
 
 
2576
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
 
2579
      for(int i = 0; i < numentries; i++){
 
 
2580
        direntry = direntries[i];
 
 
2581
        char *fullname = NULL;
 
 
2582
        ret = asprintf(&fullname, "%s/%s", tempdir,
 
 
2585
          perror_plus("asprintf");
 
 
2588
        ret = remove(fullname);
 
 
2590
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
2662
 
      TEMP_FAILURE_RETRY(close(tempdir_fd));
 
 
2597
    /* need to clean even if 0 because man page doesn't specify */
 
 
2599
    if(numentries == -1){
 
 
2600
      perror_plus("scandir");
 
 
2602
    ret = rmdir(tempdir);
 
 
2603
    if(ret == -1 and errno != ENOENT){
 
 
2604
      perror_plus("rmdir");