/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2014-06-07 20:29:36 UTC
  • mto: (237.7.272 trunk)
  • mto: This revision was merged to the branch mainline in revision 317.
  • Revision ID: teddy@recompile.se-20140607202936-crh5fxrdd804oora
Fix typo in code comment.

* debian/mandos.postinst: Fix typo in comment.

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  /* 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
 
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;
145
144
uid_t uid = 65534;
146
145
gid_t gid = 65534;
147
146
 
1514
1513
  return ret_errno;
1515
1514
}
1516
1515
 
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
 
 
1536
1516
__attribute__((nonnull))
1537
1517
void run_network_hooks(const char *mode, const char *interface,
1538
1518
                       const float delay){
1539
1519
  struct dirent **direntries;
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__ */
 
1520
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1521
                         alphasort);
1583
1522
  if(numhooks == -1){
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
 
    if(debug){
1593
 
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
1594
 
                   direntry->d_name);
 
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");
1595
1530
    }
1596
 
    pid_t hook_pid = fork();
1597
 
    if(hook_pid == 0){
1598
 
      /* Child */
1599
 
      /* Raise privileges */
1600
 
      if(raise_privileges_permanently() != 0){
1601
 
        perror_plus("Failed to raise privileges");
1602
 
        _exit(EX_NOPERM);
1603
 
      }
1604
 
      /* Set group */
1605
 
      errno = 0;
1606
 
      ret = setgid(0);
1607
 
      if(ret == -1){
1608
 
        perror_plus("setgid");
1609
 
        _exit(EX_NOPERM);
1610
 
      }
1611
 
      /* Reset supplementary groups */
1612
 
      errno = 0;
1613
 
      ret = setgroups(0, NULL);
1614
 
      if(ret == -1){
1615
 
        perror_plus("setgroups");
1616
 
        _exit(EX_NOPERM);
1617
 
      }
1618
 
      ret = dup2(devnull, STDIN_FILENO);
1619
 
      if(ret == -1){
1620
 
        perror_plus("dup2(devnull, STDIN_FILENO)");
1621
 
        _exit(EX_OSERR);
1622
 
      }
1623
 
      ret = close(devnull);
1624
 
      if(ret == -1){
1625
 
        perror_plus("close");
1626
 
        _exit(EX_OSERR);
1627
 
      }
1628
 
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1629
 
      if(ret == -1){
1630
 
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1631
 
        _exit(EX_OSERR);
1632
 
      }
1633
 
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1634
 
      if(ret == -1){
1635
 
        perror_plus("setenv");
1636
 
        _exit(EX_OSERR);
1637
 
      }
1638
 
      ret = setenv("DEVICE", interface, 1);
1639
 
      if(ret == -1){
1640
 
        perror_plus("setenv");
1641
 
        _exit(EX_OSERR);
1642
 
      }
1643
 
      ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1644
 
      if(ret == -1){
1645
 
        perror_plus("setenv");
1646
 
        _exit(EX_OSERR);
1647
 
      }
1648
 
      ret = setenv("MODE", mode, 1);
1649
 
      if(ret == -1){
1650
 
        perror_plus("setenv");
1651
 
        _exit(EX_OSERR);
1652
 
      }
1653
 
      char *delaystring;
1654
 
      ret = asprintf(&delaystring, "%f", (double)delay);
1655
 
      if(ret == -1){
 
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){
1656
1540
        perror_plus("asprintf");
1657
 
        _exit(EX_OSERR);
1658
 
      }
1659
 
      ret = setenv("DELAY", delaystring, 1);
1660
 
      if(ret == -1){
 
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
        }
1661
1616
        free(delaystring);
1662
 
        perror_plus("setenv");
1663
 
        _exit(EX_OSERR);
1664
 
      }
1665
 
      free(delaystring);
1666
 
      if(connect_to != NULL){
1667
 
        ret = setenv("CONNECT", connect_to, 1);
1668
 
        if(ret == -1){
1669
 
          perror_plus("setenv");
1670
 
          _exit(EX_OSERR);
1671
 
        }
1672
 
      }
1673
 
      if(fexecve(hookdir_fd, (char *const [])
1674
 
                 { direntry->d_name, NULL }, environ) == -1){
1675
 
        perror_plus("fexecve");
1676
 
        _exit(EXIT_FAILURE);
1677
 
      }
1678
 
    } else {
1679
 
      int status;
1680
 
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1681
 
        perror_plus("waitpid");
1682
 
        continue;
1683
 
      }
1684
 
      if(WIFEXITED(status)){
1685
 
        if(WEXITSTATUS(status) != 0){
1686
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1687
 
                       " with status %d\n", direntry->d_name,
1688
 
                       WEXITSTATUS(status));
1689
 
          continue;
1690
 
        }
1691
 
      } else if(WIFSIGNALED(status)){
1692
 
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1693
 
                     " signal %d\n", direntry->d_name,
1694
 
                     WTERMSIG(status));
1695
 
        continue;
 
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
        }
1696
1628
      } else {
1697
 
        fprintf_plus(stderr, "Warning: network hook \"%s\""
1698
 
                     " crashed\n", direntry->d_name);
1699
 
        continue;
1700
 
      }
1701
 
    }
1702
 
    if(debug){
1703
 
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1704
 
                   direntry->d_name);
1705
 
    }
1706
 
  }
1707
 
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1708
 
    perror_plus("close");
1709
 
  } else {
1710
 
    hookdir_fd = -1;
1711
 
  }
1712
 
  close(devnull);
 
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
  }
1713
1664
}
1714
1665
 
1715
1666
__attribute__((nonnull, warn_unused_result))