/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-07-13 20:37:44 UTC
  • mfrom: (237.7.258 trunk)
  • Revision ID: teddy@recompile.se-20140713203744-538hqhk81vw9dixv
MergeĀ fromĀ trunk.

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