/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-15 02:48:49 UTC
  • Revision ID: teddy@recompile.se-20140615024849-68x3q8g4yzadl33s
mandos: New "--no-zeroconf" option.  Also make "--socket=0" work.

* mandos (main): New "--no-zeroconf" option.  Also do not interpret
                 socket=0 as no socket.
* mandos-options.xml (zeroconf): New.
* mandos.conf (socket, zeroconf): New.
* mandos.xml (SYNOPSIS, OPTIONS): Added "--no-zeroconf".

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
1346
1347
    return 0;
1347
1348
  }
1348
1349
  
1349
 
  char *fullname = NULL;
1350
 
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1351
 
  if(ret < 0){
1352
 
    perror_plus("asprintf");
1353
 
    return 0;
1354
 
  }
1355
 
  
1356
 
  ret = stat(fullname, &st);
 
1350
  ret = fstatat(hookdir_fd, direntry->d_name, &st, 0);
1357
1351
  if(ret == -1){
1358
1352
    if(debug){
1359
1353
      perror_plus("Could not stat hook");
1360
1354
    }
1361
1355
    return 0;
1362
1356
  }
1363
 
  free(fullname);
1364
1357
  if(not (S_ISREG(st.st_mode))){
1365
1358
    /* Not a regular file */
1366
1359
    if(debug){
1465
1458
  error_t ret_errno = 0;
1466
1459
  if(seteuid(0) == -1){
1467
1460
    ret_errno = errno;
1468
 
    perror_plus("seteuid");
1469
1461
  }
1470
1462
  errno = old_errno;
1471
1463
  return ret_errno;
1482
1474
  }
1483
1475
  if(setuid(0) == -1){
1484
1476
    ret_errno = errno;
1485
 
    perror_plus("seteuid");
1486
1477
  }
1487
1478
  errno = old_errno;
1488
1479
  return ret_errno;
1495
1486
  error_t ret_errno = 0;
1496
1487
  if(seteuid(uid) == -1){
1497
1488
    ret_errno = errno;
1498
 
    perror_plus("seteuid");
1499
1489
  }
1500
1490
  errno = old_errno;
1501
1491
  return ret_errno;
1508
1498
  error_t ret_errno = 0;
1509
1499
  if(setuid(uid) == -1){
1510
1500
    ret_errno = errno;
1511
 
    perror_plus("setuid");
1512
1501
  }
1513
1502
  errno = old_errno;
1514
1503
  return ret_errno;
1515
1504
}
1516
1505
 
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
1506
__attribute__((nonnull))
1537
1507
void run_network_hooks(const char *mode, const char *interface,
1538
1508
                       const float delay){
1539
 
  struct dirent **direntries;
 
1509
  struct dirent **direntries = NULL;
1540
1510
  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
 
                      );
 
1511
    hookdir_fd = open(hookdir, O_RDONLY);
1548
1512
    if(hookdir_fd == -1){
1549
1513
      if(errno == ENOENT){
1550
1514
        if(debug){
1556
1520
      }
1557
1521
      return;
1558
1522
    }
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
1523
  }
1571
1524
#ifdef __GLIBC__
1572
1525
#if __GLIBC_PREREQ(2, 15)
1589
1542
  int devnull = open("/dev/null", O_RDONLY);
1590
1543
  for(int i = 0; i < numhooks; i++){
1591
1544
    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
1545
    if(debug){
1599
1546
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
1600
1547
                   direntry->d_name);
1603
1550
    if(hook_pid == 0){
1604
1551
      /* Child */
1605
1552
      /* Raise privileges */
1606
 
      if(raise_privileges_permanently() != 0){
 
1553
      errno = raise_privileges_permanently();
 
1554
      if(errno != 0){
1607
1555
        perror_plus("Failed to raise privileges");
1608
1556
        _exit(EX_NOPERM);
1609
1557
      }
1676
1624
          _exit(EX_OSERR);
1677
1625
        }
1678
1626
      }
1679
 
      if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1680
 
        perror_plus("execl");
 
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");
1681
1639
        _exit(EXIT_FAILURE);
1682
1640
      }
1683
1641
    } else {
1684
1642
      int status;
1685
1643
      if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1686
1644
        perror_plus("waitpid");
1687
 
        free(fullname);
1688
1645
        continue;
1689
1646
      }
1690
1647
      if(WIFEXITED(status)){
1692
1649
          fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1693
1650
                       " with status %d\n", direntry->d_name,
1694
1651
                       WEXITSTATUS(status));
1695
 
          free(fullname);
1696
1652
          continue;
1697
1653
        }
1698
1654
      } else if(WIFSIGNALED(status)){
1699
1655
        fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1700
1656
                     " signal %d\n", direntry->d_name,
1701
1657
                     WTERMSIG(status));
1702
 
        free(fullname);
1703
1658
        continue;
1704
1659
      } else {
1705
1660
        fprintf_plus(stderr, "Warning: network hook \"%s\""
1706
1661
                     " crashed\n", direntry->d_name);
1707
 
        free(fullname);
1708
1662
        continue;
1709
1663
      }
1710
1664
    }
1711
 
    free(fullname);
1712
1665
    if(debug){
1713
1666
      fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1714
1667
                   direntry->d_name);
1715
1668
    }
1716
1669
  }
 
1670
  free(direntries);
1717
1671
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1718
1672
    perror_plus("close");
1719
1673
  } else {
1776
1730
    /* Raise privileges */
1777
1731
    ret_errno = raise_privileges();
1778
1732
    if(ret_errno != 0){
 
1733
      errno = ret_errno;
1779
1734
      perror_plus("Failed to raise privileges");
1780
1735
    }
1781
1736
    
1885
1840
    /* Raise privileges */
1886
1841
    ret_errno = raise_privileges();
1887
1842
    if(ret_errno != 0){
 
1843
      errno = ret_errno;
1888
1844
      perror_plus("Failed to raise privileges");
1889
1845
    }
1890
1846
    
2297
2253
  
2298
2254
  /* If no interfaces were specified, make a list */
2299
2255
  if(mc.interfaces == NULL){
2300
 
    struct dirent **direntries;
 
2256
    struct dirent **direntries = NULL;
2301
2257
    /* Look for any good interfaces */
2302
2258
    ret = scandir(sys_class_net, &direntries, good_interface,
2303
2259
                  alphasort);
2318
2274
      }
2319
2275
      free(direntries);
2320
2276
    } else {
2321
 
      free(direntries);
 
2277
      if(ret == 0){
 
2278
        free(direntries);
 
2279
      }
2322
2280
      fprintf_plus(stderr, "Could not find a network interface\n");
2323
2281
      exitcode = EXIT_FAILURE;
2324
2282
      goto end;
2597
2555
  {
2598
2556
    ret_errno = raise_privileges();
2599
2557
    if(ret_errno != 0){
 
2558
      errno = ret_errno;
2600
2559
      perror_plus("Failed to raise privileges");
2601
2560
    } else {
2602
2561
      
2625
2584
    
2626
2585
    ret_errno = lower_privileges_permanently();
2627
2586
    if(ret_errno != 0){
 
2587
      errno = ret_errno;
2628
2588
      perror_plus("Failed to lower privileges permanently");
2629
2589
    }
2630
2590
  }
2635
2595
  /* Removes the GPGME temp directory and all files inside */
2636
2596
  if(tempdir != NULL){
2637
2597
    struct dirent **direntries = NULL;
2638
 
    struct dirent *direntry = NULL;
2639
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
2640
 
                             alphasort);
2641
 
    if(numentries > 0){
2642
 
      for(int i = 0; i < numentries; i++){
2643
 
        direntry = direntries[i];
2644
 
        char *fullname = NULL;
2645
 
        ret = asprintf(&fullname, "%s/%s", tempdir,
2646
 
                       direntry->d_name);
2647
 
        if(ret < 0){
2648
 
          perror_plus("asprintf");
2649
 
          continue;
2650
 
        }
2651
 
        ret = remove(fullname);
2652
 
        if(ret == -1){
2653
 
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2654
 
                       strerror(errno));
2655
 
        }
2656
 
        free(fullname);
 
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
        }
2657
2634
      }
2658
 
    }
2659
 
    
2660
 
    /* need to clean even if 0 because man page doesn't specify */
2661
 
    free(direntries);
2662
 
    if(numentries == -1){
2663
 
      perror_plus("scandir");
2664
 
    }
2665
 
    ret = rmdir(tempdir);
2666
 
    if(ret == -1 and errno != ENOENT){
2667
 
      perror_plus("rmdir");
 
2635
      TEMP_FAILURE_RETRY(close(tempdir_fd));
2668
2636
    }
2669
2637
  }
2670
2638