/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-06-07 21:58:56 UTC
  • Revision ID: teddy@recompile.se-20140607215856-9q31dfsc68uuvzo4
Make mandos-client use scandirat() if it exists.

* plugins.d/mandos-client.d (hookdir_fd): File descriptor for hookdir.
  (set_cloexec_flag): Define this function if O_CLOEXEC is undefined.
  (run_network_hooks): Open hookdir_fd.  Use scandirat() if it exists.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
33
33
#ifndef _LARGEFILE_SOURCE
34
34
#define _LARGEFILE_SOURCE
35
 
#endif
 
35
#endif  /* not _LARGEFILE_SOURCE */
36
36
#ifndef _FILE_OFFSET_BITS
37
37
#define _FILE_OFFSET_BITS 64
38
 
#endif
 
38
#endif  /* not _FILE_OFFSET_BITS */
39
39
 
40
40
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
41
41
 
141
141
static const char sys_class_net[] = "/sys/class/net";
142
142
char *connect_to = NULL;
143
143
const char *hookdir = HOOKDIR;
 
144
int hookdir_fd = -1;
144
145
uid_t uid = 65534;
145
146
gid_t gid = 65534;
146
147
 
1513
1514
  return ret_errno;
1514
1515
}
1515
1516
 
 
1517
#ifndef O_CLOEXEC
 
1518
/*
 
1519
 * Based on the example in the GNU LibC manual chapter 13.13 "File
 
1520
 * Descriptor Flags".
 
1521
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
 
1522
 */
 
1523
__attribute__((warn_unused_result))
 
1524
static int set_cloexec_flag(int fd){
 
1525
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
 
1526
  /* If reading the flags failed, return error indication now. */
 
1527
  if(ret < 0){
 
1528
    return ret;
 
1529
  }
 
1530
  /* Store modified flag word in the descriptor. */
 
1531
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
 
1532
                                       ret | FD_CLOEXEC));
 
1533
}
 
1534
#endif  /* not O_CLOEXEC */
 
1535
 
1516
1536
__attribute__((nonnull))
1517
1537
void run_network_hooks(const char *mode, const char *interface,
1518
1538
                       const float delay){
1519
1539
  struct dirent **direntries;
1520
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1521
 
                         alphasort);
 
1540
  if(hookdir_fd == -1){
 
1541
    hookdir_fd = open(hookdir, O_RDONLY |
 
1542
#ifdef O_CLOEXEC
 
1543
                      O_CLOEXEC
 
1544
#else  /* not O_CLOEXEC */
 
1545
                      0
 
1546
#endif  /* not O_CLOEXEC */
 
1547
                      );
 
1548
    if(hookdir_fd == -1){
 
1549
      if(errno == ENOENT){
 
1550
        if(debug){
 
1551
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1552
                       " found\n", hookdir);
 
1553
        }
 
1554
      } else {
 
1555
        perror_plus("open");
 
1556
      }
 
1557
      return;
 
1558
    }
 
1559
#ifndef O_CLOEXEC
 
1560
    if(set_cloexec_flag(hookdir_fd) < 0){
 
1561
      perror_plus("set_cloexec_flag");
 
1562
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
1563
        perror_plus("close");
 
1564
      } else {
 
1565
        hookdir_fd = -1;
 
1566
      }
 
1567
      return;
 
1568
    }
 
1569
#endif  /* not O_CLOEXEC */
 
1570
  }
 
1571
#ifdef __GLIBC__
 
1572
#if __GLIBC_PREREQ(2, 15)
 
1573
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
 
1574
                           runnable_hook, alphasort);
 
1575
#else  /* not __GLIBC_PREREQ(2, 15) */
 
1576
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1577
                         alphasort);
 
1578
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
1579
#else   /* not __GLIBC__ */
 
1580
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1581
                         alphasort);
 
1582
#endif  /* not __GLIBC__ */
1522
1583
  if(numhooks == -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("scandir");
1530
 
    }
1531
 
  } else {
1532
 
    struct dirent *direntry;
1533
 
    int ret;
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);
1539
 
      if(ret < 0){
 
1584
    perror_plus("scandir");
 
1585
    return;
 
1586
  }
 
1587
  struct dirent *direntry;
 
1588
  int ret;
 
1589
  int devnull = open("/dev/null", O_RDONLY);
 
1590
  for(int i = 0; i < numhooks; i++){
 
1591
    direntry = direntries[i];
 
1592
    char *fullname = NULL;
 
1593
    ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
 
1594
    if(ret < 0){
 
1595
      perror_plus("asprintf");
 
1596
      continue;
 
1597
    }
 
1598
    if(debug){
 
1599
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1600
                   direntry->d_name);
 
1601
    }
 
1602
    pid_t hook_pid = fork();
 
1603
    if(hook_pid == 0){
 
1604
      /* Child */
 
1605
      /* Raise privileges */
 
1606
      if(raise_privileges_permanently() != 0){
 
1607
        perror_plus("Failed to raise privileges");
 
1608
        _exit(EX_NOPERM);
 
1609
      }
 
1610
      /* Set group */
 
1611
      errno = 0;
 
1612
      ret = setgid(0);
 
1613
      if(ret == -1){
 
1614
        perror_plus("setgid");
 
1615
        _exit(EX_NOPERM);
 
1616
      }
 
1617
      /* Reset supplementary groups */
 
1618
      errno = 0;
 
1619
      ret = setgroups(0, NULL);
 
1620
      if(ret == -1){
 
1621
        perror_plus("setgroups");
 
1622
        _exit(EX_NOPERM);
 
1623
      }
 
1624
      ret = dup2(devnull, STDIN_FILENO);
 
1625
      if(ret == -1){
 
1626
        perror_plus("dup2(devnull, STDIN_FILENO)");
 
1627
        _exit(EX_OSERR);
 
1628
      }
 
1629
      ret = close(devnull);
 
1630
      if(ret == -1){
 
1631
        perror_plus("close");
 
1632
        _exit(EX_OSERR);
 
1633
      }
 
1634
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
1635
      if(ret == -1){
 
1636
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
1637
        _exit(EX_OSERR);
 
1638
      }
 
1639
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1640
      if(ret == -1){
 
1641
        perror_plus("setenv");
 
1642
        _exit(EX_OSERR);
 
1643
      }
 
1644
      ret = setenv("DEVICE", interface, 1);
 
1645
      if(ret == -1){
 
1646
        perror_plus("setenv");
 
1647
        _exit(EX_OSERR);
 
1648
      }
 
1649
      ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1650
      if(ret == -1){
 
1651
        perror_plus("setenv");
 
1652
        _exit(EX_OSERR);
 
1653
      }
 
1654
      ret = setenv("MODE", mode, 1);
 
1655
      if(ret == -1){
 
1656
        perror_plus("setenv");
 
1657
        _exit(EX_OSERR);
 
1658
      }
 
1659
      char *delaystring;
 
1660
      ret = asprintf(&delaystring, "%f", (double)delay);
 
1661
      if(ret == -1){
1540
1662
        perror_plus("asprintf");
1541
 
        continue;
1542
 
      }
1543
 
      if(debug){
1544
 
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
1545
 
                     direntry->d_name);
1546
 
      }
1547
 
      pid_t hook_pid = fork();
1548
 
      if(hook_pid == 0){
1549
 
        /* Child */
1550
 
        /* Raise privileges */
1551
 
        if(raise_privileges_permanently() != 0){
1552
 
          perror_plus("Failed to raise privileges");
1553
 
          _exit(EX_NOPERM);
1554
 
        }
1555
 
        /* Set group */
1556
 
        errno = 0;
1557
 
        ret = setgid(0);
1558
 
        if(ret == -1){
1559
 
          perror_plus("setgid");
1560
 
          _exit(EX_NOPERM);
1561
 
        }
1562
 
        /* Reset supplementary groups */
1563
 
        errno = 0;
1564
 
        ret = setgroups(0, NULL);
1565
 
        if(ret == -1){
1566
 
          perror_plus("setgroups");
1567
 
          _exit(EX_NOPERM);
1568
 
        }
1569
 
        ret = dup2(devnull, STDIN_FILENO);
1570
 
        if(ret == -1){
1571
 
          perror_plus("dup2(devnull, STDIN_FILENO)");
1572
 
          _exit(EX_OSERR);
1573
 
        }
1574
 
        ret = close(devnull);
1575
 
        if(ret == -1){
1576
 
          perror_plus("close");
1577
 
          _exit(EX_OSERR);
1578
 
        }
1579
 
        ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1580
 
        if(ret == -1){
1581
 
          perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1582
 
          _exit(EX_OSERR);
1583
 
        }
1584
 
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1585
 
        if(ret == -1){
1586
 
          perror_plus("setenv");
1587
 
          _exit(EX_OSERR);
1588
 
        }
1589
 
        ret = setenv("DEVICE", interface, 1);
1590
 
        if(ret == -1){
1591
 
          perror_plus("setenv");
1592
 
          _exit(EX_OSERR);
1593
 
        }
1594
 
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1595
 
        if(ret == -1){
1596
 
          perror_plus("setenv");
1597
 
          _exit(EX_OSERR);
1598
 
        }
1599
 
        ret = setenv("MODE", mode, 1);
1600
 
        if(ret == -1){
1601
 
          perror_plus("setenv");
1602
 
          _exit(EX_OSERR);
1603
 
        }
1604
 
        char *delaystring;
1605
 
        ret = asprintf(&delaystring, "%f", (double)delay);
1606
 
        if(ret == -1){
1607
 
          perror_plus("asprintf");
1608
 
          _exit(EX_OSERR);
1609
 
        }
1610
 
        ret = setenv("DELAY", delaystring, 1);
1611
 
        if(ret == -1){
1612
 
          free(delaystring);
1613
 
          perror_plus("setenv");
1614
 
          _exit(EX_OSERR);
1615
 
        }
 
1663
        _exit(EX_OSERR);
 
1664
      }
 
1665
      ret = setenv("DELAY", delaystring, 1);
 
1666
      if(ret == -1){
1616
1667
        free(delaystring);
1617
 
        if(connect_to != NULL){
1618
 
          ret = setenv("CONNECT", connect_to, 1);
1619
 
          if(ret == -1){
1620
 
            perror_plus("setenv");
1621
 
            _exit(EX_OSERR);
1622
 
          }
1623
 
        }
1624
 
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1625
 
          perror_plus("execl");
1626
 
          _exit(EXIT_FAILURE);
1627
 
        }
 
1668
        perror_plus("setenv");
 
1669
        _exit(EX_OSERR);
 
1670
      }
 
1671
      free(delaystring);
 
1672
      if(connect_to != NULL){
 
1673
        ret = setenv("CONNECT", connect_to, 1);
 
1674
        if(ret == -1){
 
1675
          perror_plus("setenv");
 
1676
          _exit(EX_OSERR);
 
1677
        }
 
1678
      }
 
1679
      if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1680
        perror_plus("execl");
 
1681
        _exit(EXIT_FAILURE);
 
1682
      }
 
1683
    } else {
 
1684
      int status;
 
1685
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1686
        perror_plus("waitpid");
 
1687
        free(fullname);
 
1688
        continue;
 
1689
      }
 
1690
      if(WIFEXITED(status)){
 
1691
        if(WEXITSTATUS(status) != 0){
 
1692
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1693
                       " with status %d\n", direntry->d_name,
 
1694
                       WEXITSTATUS(status));
 
1695
          free(fullname);
 
1696
          continue;
 
1697
        }
 
1698
      } else if(WIFSIGNALED(status)){
 
1699
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1700
                     " signal %d\n", direntry->d_name,
 
1701
                     WTERMSIG(status));
 
1702
        free(fullname);
 
1703
        continue;
1628
1704
      } else {
1629
 
        int status;
1630
 
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1631
 
          perror_plus("waitpid");
1632
 
          free(fullname);
1633
 
          continue;
1634
 
        }
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));
1640
 
            free(fullname);
1641
 
            continue;
1642
 
          }
1643
 
        } else if(WIFSIGNALED(status)){
1644
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1645
 
                       " signal %d\n", direntry->d_name,
1646
 
                       WTERMSIG(status));
1647
 
          free(fullname);
1648
 
          continue;
1649
 
        } else {
1650
 
          fprintf_plus(stderr, "Warning: network hook \"%s\""
1651
 
                       " crashed\n", direntry->d_name);
1652
 
          free(fullname);
1653
 
          continue;
1654
 
        }
1655
 
      }
1656
 
      free(fullname);
1657
 
      if(debug){
1658
 
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1659
 
                     direntry->d_name);
1660
 
      }
1661
 
    }
1662
 
    close(devnull);
1663
 
  }
 
1705
        fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1706
                     " crashed\n", direntry->d_name);
 
1707
        free(fullname);
 
1708
        continue;
 
1709
      }
 
1710
    }
 
1711
    free(fullname);
 
1712
    if(debug){
 
1713
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1714
                   direntry->d_name);
 
1715
    }
 
1716
  }
 
1717
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
1718
    perror_plus("close");
 
1719
  } else {
 
1720
    hookdir_fd = -1;
 
1721
  }
 
1722
  close(devnull);
1664
1723
}
1665
1724
 
1666
1725
__attribute__((nonnull, warn_unused_result))