/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-06 02:29:14 UTC
  • mto: This revision was merged to the branch mainline in revision 686.
  • Revision ID: teddy@recompile.se-20140606022914-qc2q0bg0rtus8s2g
Tags: version-1.6.5-3
* debian/changelog (1.6.5-3): New entry.

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
 
42
42
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
 
                                   stdout, ferror() */
 
43
                                   stdout, ferror(), remove() */
44
44
#include <stdint.h>             /* uint16_t, uint32_t, intptr_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
58
                                   inet_pton(), connect(),
59
59
                                   getnameinfo() */
60
 
#include <fcntl.h>              /* open(), unlinkat() */
 
60
#include <fcntl.h>              /* open() */
61
61
#include <dirent.h>             /* opendir(), struct dirent, readdir()
62
62
                                 */
63
63
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
73
73
                                */
74
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
75
                                   getuid(), getgid(), seteuid(),
76
 
                                   setgid(), pause(), _exit(),
77
 
                                   unlinkat() */
 
76
                                   setgid(), pause(), _exit() */
78
77
#include <arpa/inet.h>          /* inet_pton(), htons() */
79
78
#include <iso646.h>             /* not, or, and */
80
79
#include <argp.h>               /* struct argp_option, error_t, struct
142
141
static const char sys_class_net[] = "/sys/class/net";
143
142
char *connect_to = NULL;
144
143
const char *hookdir = HOOKDIR;
145
 
int hookdir_fd = -1;
146
144
uid_t uid = 65534;
147
145
gid_t gid = 65534;
148
146
 
1337
1335
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1338
1336
                "abcdefghijklmnopqrstuvwxyz"
1339
1337
                "0123456789"
1340
 
                "_.-");
 
1338
                "_-");
1341
1339
  if((direntry->d_name)[sret] != '\0'){
1342
1340
    /* Contains non-allowed characters */
1343
1341
    if(debug){
1347
1345
    return 0;
1348
1346
  }
1349
1347
  
1350
 
  ret = fstatat(hookdir_fd, direntry->d_name, &st, 0);
 
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);
1351
1356
  if(ret == -1){
1352
1357
    if(debug){
1353
1358
      perror_plus("Could not stat hook");
1458
1463
  error_t ret_errno = 0;
1459
1464
  if(seteuid(0) == -1){
1460
1465
    ret_errno = errno;
 
1466
    perror_plus("seteuid");
1461
1467
  }
1462
1468
  errno = old_errno;
1463
1469
  return ret_errno;
1474
1480
  }
1475
1481
  if(setuid(0) == -1){
1476
1482
    ret_errno = errno;
 
1483
    perror_plus("seteuid");
1477
1484
  }
1478
1485
  errno = old_errno;
1479
1486
  return ret_errno;
1486
1493
  error_t ret_errno = 0;
1487
1494
  if(seteuid(uid) == -1){
1488
1495
    ret_errno = errno;
 
1496
    perror_plus("seteuid");
1489
1497
  }
1490
1498
  errno = old_errno;
1491
1499
  return ret_errno;
1498
1506
  error_t ret_errno = 0;
1499
1507
  if(setuid(uid) == -1){
1500
1508
    ret_errno = errno;
 
1509
    perror_plus("setuid");
1501
1510
  }
1502
1511
  errno = old_errno;
1503
1512
  return ret_errno;
1506
1515
__attribute__((nonnull))
1507
1516
void run_network_hooks(const char *mode, const char *interface,
1508
1517
                       const float delay){
1509
 
  struct dirent **direntries = NULL;
1510
 
  if(hookdir_fd == -1){
1511
 
    hookdir_fd = open(hookdir, O_RDONLY);
1512
 
    if(hookdir_fd == -1){
1513
 
      if(errno == ENOENT){
1514
 
        if(debug){
1515
 
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
1516
 
                       " found\n", hookdir);
1517
 
        }
1518
 
      } else {
1519
 
        perror_plus("open");
1520
 
      }
1521
 
      return;
1522
 
    }
1523
 
  }
1524
 
#ifdef __GLIBC__
1525
 
#if __GLIBC_PREREQ(2, 15)
1526
 
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1527
 
                           runnable_hook, alphasort);
1528
 
#else  /* not __GLIBC_PREREQ(2, 15) */
1529
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1530
 
                         alphasort);
1531
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
1532
 
#else   /* not __GLIBC__ */
1533
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1534
 
                         alphasort);
1535
 
#endif  /* not __GLIBC__ */
 
1518
  struct dirent **direntries;
 
1519
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1520
                         alphasort);
1536
1521
  if(numhooks == -1){
1537
 
    perror_plus("scandir");
1538
 
    return;
1539
 
  }
1540
 
  struct dirent *direntry;
1541
 
  int ret;
1542
 
  int devnull = open("/dev/null", O_RDONLY);
1543
 
  for(int i = 0; i < numhooks; i++){
1544
 
    direntry = direntries[i];
1545
 
    if(debug){
1546
 
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
1547
 
                   direntry->d_name);
 
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");
1548
1529
    }
1549
 
    pid_t hook_pid = fork();
1550
 
    if(hook_pid == 0){
1551
 
      /* Child */
1552
 
      /* Raise privileges */
1553
 
      errno = raise_privileges_permanently();
1554
 
      if(errno != 0){
1555
 
        perror_plus("Failed to raise privileges");
1556
 
        _exit(EX_NOPERM);
1557
 
      }
1558
 
      /* Set group */
1559
 
      errno = 0;
1560
 
      ret = setgid(0);
1561
 
      if(ret == -1){
1562
 
        perror_plus("setgid");
1563
 
        _exit(EX_NOPERM);
1564
 
      }
1565
 
      /* Reset supplementary groups */
1566
 
      errno = 0;
1567
 
      ret = setgroups(0, NULL);
1568
 
      if(ret == -1){
1569
 
        perror_plus("setgroups");
1570
 
        _exit(EX_NOPERM);
1571
 
      }
1572
 
      ret = dup2(devnull, STDIN_FILENO);
1573
 
      if(ret == -1){
1574
 
        perror_plus("dup2(devnull, STDIN_FILENO)");
1575
 
        _exit(EX_OSERR);
1576
 
      }
1577
 
      ret = close(devnull);
1578
 
      if(ret == -1){
1579
 
        perror_plus("close");
1580
 
        _exit(EX_OSERR);
1581
 
      }
1582
 
      ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1583
 
      if(ret == -1){
1584
 
        perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1585
 
        _exit(EX_OSERR);
1586
 
      }
1587
 
      ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1588
 
      if(ret == -1){
1589
 
        perror_plus("setenv");
1590
 
        _exit(EX_OSERR);
1591
 
      }
1592
 
      ret = setenv("DEVICE", interface, 1);
1593
 
      if(ret == -1){
1594
 
        perror_plus("setenv");
1595
 
        _exit(EX_OSERR);
1596
 
      }
1597
 
      ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1598
 
      if(ret == -1){
1599
 
        perror_plus("setenv");
1600
 
        _exit(EX_OSERR);
1601
 
      }
1602
 
      ret = setenv("MODE", mode, 1);
1603
 
      if(ret == -1){
1604
 
        perror_plus("setenv");
1605
 
        _exit(EX_OSERR);
1606
 
      }
1607
 
      char *delaystring;
1608
 
      ret = asprintf(&delaystring, "%f", (double)delay);
1609
 
      if(ret == -1){
 
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){
1610
1539
        perror_plus("asprintf");
1611
 
        _exit(EX_OSERR);
1612
 
      }
1613
 
      ret = setenv("DELAY", delaystring, 1);
1614
 
      if(ret == -1){
 
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
        }
1615
1615
        free(delaystring);
1616
 
        perror_plus("setenv");
1617
 
        _exit(EX_OSERR);
1618
 
      }
1619
 
      free(delaystring);
1620
 
      if(connect_to != NULL){
1621
 
        ret = setenv("CONNECT", connect_to, 1);
1622
 
        if(ret == -1){
1623
 
          perror_plus("setenv");
1624
 
          _exit(EX_OSERR);
1625
 
        }
1626
 
      }
1627
 
      int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
1628
 
      if(hook_fd == -1){
1629
 
        perror_plus("openat");
1630
 
        _exit(EXIT_FAILURE);
1631
 
      }
1632
 
      if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1633
 
        perror_plus("close");
1634
 
        _exit(EXIT_FAILURE);
1635
 
      }
1636
 
      if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
1637
 
                 environ) == -1){
1638
 
        perror_plus("fexecve");
1639
 
        _exit(EXIT_FAILURE);
1640
 
      }
1641
 
    } else {
1642
 
      int status;
1643
 
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1644
 
        perror_plus("waitpid");
1645
 
        continue;
1646
 
      }
1647
 
      if(WIFEXITED(status)){
1648
 
        if(WEXITSTATUS(status) != 0){
1649
 
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1650
 
                       " with status %d\n", direntry->d_name,
1651
 
                       WEXITSTATUS(status));
1652
 
          continue;
1653
 
        }
1654
 
      } else if(WIFSIGNALED(status)){
1655
 
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1656
 
                     " signal %d\n", direntry->d_name,
1657
 
                     WTERMSIG(status));
1658
 
        continue;
 
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
        }
1659
1627
      } else {
1660
 
        fprintf_plus(stderr, "Warning: network hook \"%s\""
1661
 
                     " crashed\n", direntry->d_name);
1662
 
        continue;
1663
 
      }
1664
 
    }
1665
 
    if(debug){
1666
 
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1667
 
                   direntry->d_name);
1668
 
    }
1669
 
  }
1670
 
  free(direntries);
1671
 
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1672
 
    perror_plus("close");
1673
 
  } else {
1674
 
    hookdir_fd = -1;
1675
 
  }
1676
 
  close(devnull);
 
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
  }
1677
1663
}
1678
1664
 
1679
1665
__attribute__((nonnull, warn_unused_result))
1730
1716
    /* Raise privileges */
1731
1717
    ret_errno = raise_privileges();
1732
1718
    if(ret_errno != 0){
1733
 
      errno = ret_errno;
1734
1719
      perror_plus("Failed to raise privileges");
1735
1720
    }
1736
1721
    
1840
1825
    /* Raise privileges */
1841
1826
    ret_errno = raise_privileges();
1842
1827
    if(ret_errno != 0){
1843
 
      errno = ret_errno;
1844
1828
      perror_plus("Failed to raise privileges");
1845
1829
    }
1846
1830
    
2253
2237
  
2254
2238
  /* If no interfaces were specified, make a list */
2255
2239
  if(mc.interfaces == NULL){
2256
 
    struct dirent **direntries = NULL;
 
2240
    struct dirent **direntries;
2257
2241
    /* Look for any good interfaces */
2258
2242
    ret = scandir(sys_class_net, &direntries, good_interface,
2259
2243
                  alphasort);
2274
2258
      }
2275
2259
      free(direntries);
2276
2260
    } else {
2277
 
      if(ret == 0){
2278
 
        free(direntries);
2279
 
      }
 
2261
      free(direntries);
2280
2262
      fprintf_plus(stderr, "Could not find a network interface\n");
2281
2263
      exitcode = EXIT_FAILURE;
2282
2264
      goto end;
2504
2486
  if(debug){
2505
2487
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2506
2488
  }
2507
 
  
 
2489
 
2508
2490
  ret = avahi_loop_with_timeout(simple_poll,
2509
2491
                                (int)(retry_interval * 1000), &mc);
2510
2492
  if(debug){
2555
2537
  {
2556
2538
    ret_errno = raise_privileges();
2557
2539
    if(ret_errno != 0){
2558
 
      errno = ret_errno;
2559
2540
      perror_plus("Failed to raise privileges");
2560
2541
    } else {
2561
2542
      
2584
2565
    
2585
2566
    ret_errno = lower_privileges_permanently();
2586
2567
    if(ret_errno != 0){
2587
 
      errno = ret_errno;
2588
2568
      perror_plus("Failed to lower privileges permanently");
2589
2569
    }
2590
2570
  }
2595
2575
  /* Removes the GPGME temp directory and all files inside */
2596
2576
  if(tempdir != NULL){
2597
2577
    struct dirent **direntries = NULL;
2598
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2599
 
                                                  O_NOFOLLOW));
2600
 
    if(tempdir_fd == -1){
2601
 
      perror_plus("open");
2602
 
    } else {
2603
 
#ifdef __GLIBC__
2604
 
#if __GLIBC_PREREQ(2, 15)
2605
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
2606
 
                                 notdotentries, alphasort);
2607
 
#else  /* not __GLIBC_PREREQ(2, 15) */
2608
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2609
 
                               alphasort);
2610
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
2611
 
#else   /* not __GLIBC__ */
2612
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2613
 
                               alphasort);
2614
 
#endif  /* not __GLIBC__ */
2615
 
      if(numentries >= 0){
2616
 
        for(int i = 0; i < numentries; i++){
2617
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2618
 
          if(ret == -1){
2619
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2620
 
                         " \"%s\", 0): %s\n", tempdir,
2621
 
                         direntries[i]->d_name, strerror(errno));
2622
 
          }
2623
 
        }
2624
 
        
2625
 
        /* need to clean even if 0 because man page doesn't specify */
2626
 
        free(direntries);
2627
 
        if(numentries == -1){
2628
 
          perror_plus("scandir");
2629
 
        }
2630
 
        ret = rmdir(tempdir);
2631
 
        if(ret == -1 and errno != ENOENT){
2632
 
          perror_plus("rmdir");
2633
 
        }
 
2578
    struct dirent *direntry = NULL;
 
2579
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2580
                             alphasort);
 
2581
    if(numentries > 0){
 
2582
      for(int i = 0; i < numentries; i++){
 
2583
        direntry = direntries[i];
 
2584
        char *fullname = NULL;
 
2585
        ret = asprintf(&fullname, "%s/%s", tempdir,
 
2586
                       direntry->d_name);
 
2587
        if(ret < 0){
 
2588
          perror_plus("asprintf");
 
2589
          continue;
 
2590
        }
 
2591
        ret = remove(fullname);
 
2592
        if(ret == -1){
 
2593
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
2594
                       strerror(errno));
 
2595
        }
 
2596
        free(fullname);
2634
2597
      }
2635
 
      TEMP_FAILURE_RETRY(close(tempdir_fd));
 
2598
    }
 
2599
 
 
2600
    /* need to clean even if 0 because man page doesn't specify */
 
2601
    free(direntries);
 
2602
    if(numentries == -1){
 
2603
      perror_plus("scandir");
 
2604
    }
 
2605
    ret = rmdir(tempdir);
 
2606
    if(ret == -1 and errno != ENOENT){
 
2607
      perror_plus("rmdir");
2636
2608
    }
2637
2609
  }
2638
2610