1514
1511
void run_network_hooks(const char *mode, const char *interface,
 
1515
1512
                       const float delay){
 
1516
1513
  struct dirent **direntries;
 
1517
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
 
1514
  if(hookdir_fd == -1){
 
 
1515
    hookdir_fd = open(hookdir, O_RDONLY);
 
 
1516
    if(hookdir_fd == -1){
 
 
1517
      if(errno == ENOENT){
 
 
1519
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
 
1520
                       " found\n", hookdir);
 
 
1523
        perror_plus("open");
 
 
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,
 
 
1535
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
 
1536
#else   /* not __GLIBC__ */
 
 
1537
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
 
1539
#endif  /* not __GLIBC__ */
 
1519
1540
  if(numhooks == -1){
 
1520
 
    if(errno == ENOENT){
 
1522
 
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1523
 
                     " found\n", hookdir);
 
1526
 
      perror_plus("scandir");
 
 
1541
    perror_plus("scandir");
 
 
1544
  struct dirent *direntry;
 
 
1546
  int devnull = open("/dev/null", O_RDONLY);
 
 
1547
  for(int i = 0; i < numhooks; i++){
 
 
1548
    direntry = direntries[i];
 
 
1550
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1529
 
    struct dirent *direntry;
 
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);
 
 
1553
    pid_t hook_pid = fork();
 
 
1556
      /* Raise privileges */
 
 
1557
      if(raise_privileges_permanently() != 0){
 
 
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);
 
1537
1613
        perror_plus("asprintf");
 
1541
 
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1544
 
      pid_t hook_pid = fork();
 
1547
 
        /* Raise privileges */
 
1548
 
        if(raise_privileges_permanently() != 0){
 
1549
 
          perror_plus("Failed to raise privileges");
 
1556
 
          perror_plus("setgid");
 
1559
 
        /* Reset supplementary groups */
 
1561
 
        ret = setgroups(0, NULL);
 
1563
 
          perror_plus("setgroups");
 
1566
 
        ret = dup2(devnull, STDIN_FILENO);
 
1568
 
          perror_plus("dup2(devnull, STDIN_FILENO)");
 
1571
 
        ret = close(devnull);
 
1573
 
          perror_plus("close");
 
1576
 
        ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
1578
 
          perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
1581
 
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1583
 
          perror_plus("setenv");
 
1586
 
        ret = setenv("DEVICE", interface, 1);
 
1588
 
          perror_plus("setenv");
 
1591
 
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1593
 
          perror_plus("setenv");
 
1596
 
        ret = setenv("MODE", mode, 1);
 
1598
 
          perror_plus("setenv");
 
1602
 
        ret = asprintf(&delaystring, "%f", (double)delay);
 
1604
 
          perror_plus("asprintf");
 
1607
 
        ret = setenv("DELAY", delaystring, 1);
 
1610
 
          perror_plus("setenv");
 
 
1616
      ret = setenv("DELAY", delaystring, 1);
 
1613
1618
        free(delaystring);
 
1614
 
        if(connect_to != NULL){
 
1615
 
          ret = setenv("CONNECT", connect_to, 1);
 
1617
 
            perror_plus("setenv");
 
1621
 
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1622
 
          perror_plus("execl");
 
1623
 
          _exit(EXIT_FAILURE);
 
 
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,
 
1627
 
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1628
 
          perror_plus("waitpid");
 
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));
 
1640
 
        } else if(WIFSIGNALED(status)){
 
1641
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1642
 
                       " signal %d\n", direntry->d_name,
 
1647
 
          fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1648
 
                       " crashed\n", direntry->d_name);
 
1655
 
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
 
1663
        fprintf_plus(stderr, "Warning: network hook \"%s\""
 
 
1664
                     " crashed\n", direntry->d_name);
 
 
1669
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
 
1673
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
 
1674
    perror_plus("close");
 
1663
1681
__attribute__((nonnull, warn_unused_result))
 
 
2559
2589
  free(interfaces_hooks);
 
2561
2591
  /* Removes the GPGME temp directory and all files inside */
 
2562
 
  if(tempdir_created){
 
 
2592
  if(tempdir != NULL){
 
2563
2593
    struct dirent **direntries = NULL;
 
2564
 
    struct dirent *direntry = NULL;
 
2565
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2568
 
      for(int i = 0; i < numentries; i++){
 
2569
 
        direntry = direntries[i];
 
2570
 
        char *fullname = NULL;
 
2571
 
        ret = asprintf(&fullname, "%s/%s", tempdir,
 
2574
 
          perror_plus("asprintf");
 
2577
 
        ret = remove(fullname);
 
2579
 
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
 
2594
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
 
 
2596
    if(tempdir_fd == -1){
 
 
2597
      perror_plus("open");
 
 
2600
#if __GLIBC_PREREQ(2, 15)
 
 
2601
      int numentries = scandirat(tempdir_fd, ".", &direntries,
 
 
2602
                                 notdotentries, alphasort);
 
 
2603
#else  /* not __GLIBC_PREREQ(2, 15) */
 
 
2604
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
 
2606
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
 
2607
#else   /* not __GLIBC__ */
 
 
2608
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
 
2610
#endif  /* not __GLIBC__ */
 
 
2612
        for(int i = 0; i < numentries; i++){
 
 
2613
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
 
 
2615
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
 
 
2616
                         " \"%s\", 0): %s\n", tempdir,
 
 
2617
                         direntries[i]->d_name, strerror(errno));
 
 
2621
        /* need to clean even if 0 because man page doesn't specify */
 
 
2623
        if(numentries == -1){
 
 
2624
          perror_plus("scandir");
 
 
2626
        ret = rmdir(tempdir);
 
 
2627
        if(ret == -1 and errno != ENOENT){
 
 
2628
          perror_plus("rmdir");
 
2586
 
    /* need to clean even if 0 because man page doesn't specify */
 
2588
 
    if(numentries == -1){
 
2589
 
      perror_plus("scandir");
 
2591
 
    ret = rmdir(tempdir);
 
2592
 
    if(ret == -1 and errno != ENOENT){
 
2593
 
      perror_plus("rmdir");
 
 
2631
      TEMP_FAILURE_RETRY(close(tempdir_fd));