/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 22:37:22 UTC
  • mto: (237.7.272 trunk)
  • mto: This revision was merged to the branch mainline in revision 317.
  • Revision ID: teddy@recompile.se-20140607223722-55qmdr3n9x39pvx4
Make mandos-client use fstatat().

* plugins.d/mandos-client.d (runnable_hook): Use fstatat().

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
 
1335
1336
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1336
1337
                "abcdefghijklmnopqrstuvwxyz"
1337
1338
                "0123456789"
1338
 
                "_-");
 
1339
                "_.-");
1339
1340
  if((direntry->d_name)[sret] != '\0'){
1340
1341
    /* Contains non-allowed characters */
1341
1342
    if(debug){
1345
1346
    return 0;
1346
1347
  }
1347
1348
  
1348
 
  char *fullname = NULL;
1349
 
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1350
 
  if(ret < 0){
1351
 
    perror_plus("asprintf");
1352
 
    return 0;
1353
 
  }
1354
 
  
1355
 
  ret = stat(fullname, &st);
 
1349
  ret = fstatat(hookdir_fd, direntry->d_name, &st, 0);
1356
1350
  if(ret == -1){
1357
1351
    if(debug){
1358
1352
      perror_plus("Could not stat hook");
1512
1506
  return ret_errno;
1513
1507
}
1514
1508
 
 
1509
#ifndef O_CLOEXEC
 
1510
/*
 
1511
 * Based on the example in the GNU LibC manual chapter 13.13 "File
 
1512
 * Descriptor Flags".
 
1513
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
 
1514
 */
 
1515
__attribute__((warn_unused_result))
 
1516
static int set_cloexec_flag(int fd){
 
1517
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
 
1518
  /* If reading the flags failed, return error indication now. */
 
1519
  if(ret < 0){
 
1520
    return ret;
 
1521
  }
 
1522
  /* Store modified flag word in the descriptor. */
 
1523
  return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
 
1524
                                       ret | FD_CLOEXEC));
 
1525
}
 
1526
#endif  /* not O_CLOEXEC */
 
1527
 
1515
1528
__attribute__((nonnull))
1516
1529
void run_network_hooks(const char *mode, const char *interface,
1517
1530
                       const float delay){
1518
1531
  struct dirent **direntries;
1519
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1520
 
                         alphasort);
 
1532
  if(hookdir_fd == -1){
 
1533
    hookdir_fd = open(hookdir, O_RDONLY |
 
1534
#ifdef O_CLOEXEC
 
1535
                      O_CLOEXEC
 
1536
#else  /* not O_CLOEXEC */
 
1537
                      0
 
1538
#endif  /* not O_CLOEXEC */
 
1539
                      );
 
1540
    if(hookdir_fd == -1){
 
1541
      if(errno == ENOENT){
 
1542
        if(debug){
 
1543
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1544
                       " found\n", hookdir);
 
1545
        }
 
1546
      } else {
 
1547
        perror_plus("open");
 
1548
      }
 
1549
      return;
 
1550
    }
 
1551
#ifndef O_CLOEXEC
 
1552
    if(set_cloexec_flag(hookdir_fd) < 0){
 
1553
      perror_plus("set_cloexec_flag");
 
1554
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
1555
        perror_plus("close");
 
1556
      } else {
 
1557
        hookdir_fd = -1;
 
1558
      }
 
1559
      return;
 
1560
    }
 
1561
#endif  /* not O_CLOEXEC */
 
1562
  }
 
1563
#ifdef __GLIBC__
 
1564
#if __GLIBC_PREREQ(2, 15)
 
1565
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
 
1566
                           runnable_hook, alphasort);
 
1567
#else  /* not __GLIBC_PREREQ(2, 15) */
 
1568
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1569
                         alphasort);
 
1570
#endif  /* not __GLIBC_PREREQ(2, 15) */
 
1571
#else   /* not __GLIBC__ */
 
1572
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1573
                         alphasort);
 
1574
#endif  /* not __GLIBC__ */
1521
1575
  if(numhooks == -1){
1522
 
    if(errno == ENOENT){
1523
 
      if(debug){
1524
 
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
1525
 
                     " found\n", hookdir);
1526
 
      }
1527
 
    } else {
1528
 
      perror_plus("scandir");
 
1576
    perror_plus("scandir");
 
1577
    return;
 
1578
  }
 
1579
  struct dirent *direntry;
 
1580
  int ret;
 
1581
  int devnull = open("/dev/null", O_RDONLY);
 
1582
  for(int i = 0; i < numhooks; i++){
 
1583
    direntry = direntries[i];
 
1584
    if(debug){
 
1585
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1586
                   direntry->d_name);
1529
1587
    }
1530
 
  } else {
1531
 
    struct dirent *direntry;
1532
 
    int ret;
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);
1538
 
      if(ret < 0){
 
1588
    pid_t hook_pid = fork();
 
1589
    if(hook_pid == 0){
 
1590
      /* Child */
 
1591
      /* Raise privileges */
 
1592
      if(raise_privileges_permanently() != 0){
 
1593
        perror_plus("Failed to raise privileges");
 
1594
        _exit(EX_NOPERM);
 
1595
      }
 
1596
      /* Set group */
 
1597
      errno = 0;
 
1598
      ret = setgid(0);
 
1599
      if(ret == -1){
 
1600
        perror_plus("setgid");
 
1601
        _exit(EX_NOPERM);
 
1602
      }
 
1603
      /* Reset supplementary groups */
 
1604
      errno = 0;
 
1605
      ret = setgroups(0, NULL);
 
1606
      if(ret == -1){
 
1607
        perror_plus("setgroups");
 
1608
        _exit(EX_NOPERM);
 
1609
      }
 
1610
      ret = dup2(devnull, STDIN_FILENO);
 
1611
      if(ret == -1){
 
1612
        perror_plus("dup2(devnull, STDIN_FILENO)");
 
1613
        _exit(EX_OSERR);
 
1614
      }
 
1615
      ret = close(devnull);
 
1616
      if(ret == -1){
 
1617
        perror_plus("close");
 
1618
        _exit(EX_OSERR);
 
1619
      }
 
1620
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
 
1621
      if(ret == -1){
 
1622
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
 
1623
        _exit(EX_OSERR);
 
1624
      }
 
1625
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
 
1626
      if(ret == -1){
 
1627
        perror_plus("setenv");
 
1628
        _exit(EX_OSERR);
 
1629
      }
 
1630
      ret = setenv("DEVICE", interface, 1);
 
1631
      if(ret == -1){
 
1632
        perror_plus("setenv");
 
1633
        _exit(EX_OSERR);
 
1634
      }
 
1635
      ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1636
      if(ret == -1){
 
1637
        perror_plus("setenv");
 
1638
        _exit(EX_OSERR);
 
1639
      }
 
1640
      ret = setenv("MODE", mode, 1);
 
1641
      if(ret == -1){
 
1642
        perror_plus("setenv");
 
1643
        _exit(EX_OSERR);
 
1644
      }
 
1645
      char *delaystring;
 
1646
      ret = asprintf(&delaystring, "%f", (double)delay);
 
1647
      if(ret == -1){
1539
1648
        perror_plus("asprintf");
1540
 
        continue;
1541
 
      }
1542
 
      if(debug){
1543
 
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
1544
 
                     direntry->d_name);
1545
 
      }
1546
 
      pid_t hook_pid = fork();
1547
 
      if(hook_pid == 0){
1548
 
        /* Child */
1549
 
        /* Raise privileges */
1550
 
        if(raise_privileges_permanently() != 0){
1551
 
          perror_plus("Failed to raise privileges");
1552
 
          _exit(EX_NOPERM);
1553
 
        }
1554
 
        /* Set group */
1555
 
        errno = 0;
1556
 
        ret = setgid(0);
1557
 
        if(ret == -1){
1558
 
          perror_plus("setgid");
1559
 
          _exit(EX_NOPERM);
1560
 
        }
1561
 
        /* Reset supplementary groups */
1562
 
        errno = 0;
1563
 
        ret = setgroups(0, NULL);
1564
 
        if(ret == -1){
1565
 
          perror_plus("setgroups");
1566
 
          _exit(EX_NOPERM);
1567
 
        }
1568
 
        ret = dup2(devnull, STDIN_FILENO);
1569
 
        if(ret == -1){
1570
 
          perror_plus("dup2(devnull, STDIN_FILENO)");
1571
 
          _exit(EX_OSERR);
1572
 
        }
1573
 
        ret = close(devnull);
1574
 
        if(ret == -1){
1575
 
          perror_plus("close");
1576
 
          _exit(EX_OSERR);
1577
 
        }
1578
 
        ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1579
 
        if(ret == -1){
1580
 
          perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1581
 
          _exit(EX_OSERR);
1582
 
        }
1583
 
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1584
 
        if(ret == -1){
1585
 
          perror_plus("setenv");
1586
 
          _exit(EX_OSERR);
1587
 
        }
1588
 
        ret = setenv("DEVICE", interface, 1);
1589
 
        if(ret == -1){
1590
 
          perror_plus("setenv");
1591
 
          _exit(EX_OSERR);
1592
 
        }
1593
 
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1594
 
        if(ret == -1){
1595
 
          perror_plus("setenv");
1596
 
          _exit(EX_OSERR);
1597
 
        }
1598
 
        ret = setenv("MODE", mode, 1);
1599
 
        if(ret == -1){
1600
 
          perror_plus("setenv");
1601
 
          _exit(EX_OSERR);
1602
 
        }
1603
 
        char *delaystring;
1604
 
        ret = asprintf(&delaystring, "%f", (double)delay);
1605
 
        if(ret == -1){
1606
 
          perror_plus("asprintf");
1607
 
          _exit(EX_OSERR);
1608
 
        }
1609
 
        ret = setenv("DELAY", delaystring, 1);
1610
 
        if(ret == -1){
1611
 
          free(delaystring);
1612
 
          perror_plus("setenv");
1613
 
          _exit(EX_OSERR);
1614
 
        }
 
1649
        _exit(EX_OSERR);
 
1650
      }
 
1651
      ret = setenv("DELAY", delaystring, 1);
 
1652
      if(ret == -1){
1615
1653
        free(delaystring);
1616
 
        if(connect_to != NULL){
1617
 
          ret = setenv("CONNECT", connect_to, 1);
1618
 
          if(ret == -1){
1619
 
            perror_plus("setenv");
1620
 
            _exit(EX_OSERR);
1621
 
          }
1622
 
        }
1623
 
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1624
 
          perror_plus("execl");
1625
 
          _exit(EXIT_FAILURE);
1626
 
        }
 
1654
        perror_plus("setenv");
 
1655
        _exit(EX_OSERR);
 
1656
      }
 
1657
      free(delaystring);
 
1658
      if(connect_to != NULL){
 
1659
        ret = setenv("CONNECT", connect_to, 1);
 
1660
        if(ret == -1){
 
1661
          perror_plus("setenv");
 
1662
          _exit(EX_OSERR);
 
1663
        }
 
1664
      }
 
1665
      if(fexecve(hookdir_fd, (char *const [])
 
1666
                 { direntry->d_name, NULL }, environ) == -1){
 
1667
        perror_plus("fexecve");
 
1668
        _exit(EXIT_FAILURE);
 
1669
      }
 
1670
    } else {
 
1671
      int status;
 
1672
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
 
1673
        perror_plus("waitpid");
 
1674
        continue;
 
1675
      }
 
1676
      if(WIFEXITED(status)){
 
1677
        if(WEXITSTATUS(status) != 0){
 
1678
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
 
1679
                       " with status %d\n", direntry->d_name,
 
1680
                       WEXITSTATUS(status));
 
1681
          continue;
 
1682
        }
 
1683
      } else if(WIFSIGNALED(status)){
 
1684
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
 
1685
                     " signal %d\n", direntry->d_name,
 
1686
                     WTERMSIG(status));
 
1687
        continue;
1627
1688
      } else {
1628
 
        int status;
1629
 
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1630
 
          perror_plus("waitpid");
1631
 
          free(fullname);
1632
 
          continue;
1633
 
        }
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));
1639
 
            free(fullname);
1640
 
            continue;
1641
 
          }
1642
 
        } else if(WIFSIGNALED(status)){
1643
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1644
 
                       " signal %d\n", direntry->d_name,
1645
 
                       WTERMSIG(status));
1646
 
          free(fullname);
1647
 
          continue;
1648
 
        } else {
1649
 
          fprintf_plus(stderr, "Warning: network hook \"%s\""
1650
 
                       " crashed\n", direntry->d_name);
1651
 
          free(fullname);
1652
 
          continue;
1653
 
        }
1654
 
      }
1655
 
      free(fullname);
1656
 
      if(debug){
1657
 
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1658
 
                     direntry->d_name);
1659
 
      }
1660
 
    }
1661
 
    close(devnull);
1662
 
  }
 
1689
        fprintf_plus(stderr, "Warning: network hook \"%s\""
 
1690
                     " crashed\n", direntry->d_name);
 
1691
        continue;
 
1692
      }
 
1693
    }
 
1694
    if(debug){
 
1695
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1696
                   direntry->d_name);
 
1697
    }
 
1698
  }
 
1699
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
 
1700
    perror_plus("close");
 
1701
  } else {
 
1702
    hookdir_fd = -1;
 
1703
  }
 
1704
  close(devnull);
1663
1705
}
1664
1706
 
1665
1707
__attribute__((nonnull, warn_unused_result))
2486
2528
  if(debug){
2487
2529
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2488
2530
  }
2489
 
 
 
2531
  
2490
2532
  ret = avahi_loop_with_timeout(simple_poll,
2491
2533
                                (int)(retry_interval * 1000), &mc);
2492
2534
  if(debug){
2596
2638
        free(fullname);
2597
2639
      }
2598
2640
    }
2599
 
 
 
2641
    
2600
2642
    /* need to clean even if 0 because man page doesn't specify */
2601
2643
    free(direntries);
2602
2644
    if(numentries == -1){