/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-07 22:37:22 UTC
  • 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:
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
1507
1506
  return ret_errno;
1508
1507
}
1509
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
 
1510
1528
__attribute__((nonnull))
1511
1529
void run_network_hooks(const char *mode, const char *interface,
1512
1530
                       const float delay){
1513
 
  struct dirent **direntries = NULL;
 
1531
  struct dirent **direntries;
1514
1532
  if(hookdir_fd == -1){
1515
 
    hookdir_fd = open(hookdir, O_RDONLY);
 
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
                      );
1516
1540
    if(hookdir_fd == -1){
1517
1541
      if(errno == ENOENT){
1518
1542
        if(debug){
1524
1548
      }
1525
1549
      return;
1526
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 */
1527
1562
  }
1528
1563
#ifdef __GLIBC__
1529
1564
#if __GLIBC_PREREQ(2, 15)
1627
1662
          _exit(EX_OSERR);
1628
1663
        }
1629
1664
      }
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){
 
1665
      if(fexecve(hookdir_fd, (char *const [])
 
1666
                 { direntry->d_name, NULL }, environ) == -1){
1641
1667
        perror_plus("fexecve");
1642
1668
        _exit(EXIT_FAILURE);
1643
1669
      }
1670
1696
                   direntry->d_name);
1671
1697
    }
1672
1698
  }
1673
 
  free(direntries);
1674
1699
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1675
1700
    perror_plus("close");
1676
1701
  } else {
2254
2279
  
2255
2280
  /* If no interfaces were specified, make a list */
2256
2281
  if(mc.interfaces == NULL){
2257
 
    struct dirent **direntries = NULL;
 
2282
    struct dirent **direntries;
2258
2283
    /* Look for any good interfaces */
2259
2284
    ret = scandir(sys_class_net, &direntries, good_interface,
2260
2285
                  alphasort);
2275
2300
      }
2276
2301
      free(direntries);
2277
2302
    } else {
2278
 
      if(ret == 0){
2279
 
        free(direntries);
2280
 
      }
 
2303
      free(direntries);
2281
2304
      fprintf_plus(stderr, "Could not find a network interface\n");
2282
2305
      exitcode = EXIT_FAILURE;
2283
2306
      goto end;
2594
2617
  /* Removes the GPGME temp directory and all files inside */
2595
2618
  if(tempdir != NULL){
2596
2619
    struct dirent **direntries = NULL;
2597
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2598
 
                                                  O_NOFOLLOW));
2599
 
    if(tempdir_fd == -1){
2600
 
      perror_plus("open");
2601
 
    } else {
2602
 
#ifdef __GLIBC__
2603
 
#if __GLIBC_PREREQ(2, 15)
2604
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
2605
 
                                 notdotentries, alphasort);
2606
 
#else  /* not __GLIBC_PREREQ(2, 15) */
2607
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2608
 
                               alphasort);
2609
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
2610
 
#else   /* not __GLIBC__ */
2611
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2612
 
                               alphasort);
2613
 
#endif  /* not __GLIBC__ */
2614
 
      if(numentries >= 0){
2615
 
        for(int i = 0; i < numentries; i++){
2616
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2617
 
          if(ret == -1){
2618
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2619
 
                         " \"%s\", 0): %s\n", tempdir,
2620
 
                         direntries[i]->d_name, strerror(errno));
2621
 
          }
2622
 
        }
2623
 
        
2624
 
        /* need to clean even if 0 because man page doesn't specify */
2625
 
        free(direntries);
2626
 
        if(numentries == -1){
2627
 
          perror_plus("scandir");
2628
 
        }
2629
 
        ret = rmdir(tempdir);
2630
 
        if(ret == -1 and errno != ENOENT){
2631
 
          perror_plus("rmdir");
2632
 
        }
 
2620
    struct dirent *direntry = NULL;
 
2621
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2622
                             alphasort);
 
2623
    if(numentries > 0){
 
2624
      for(int i = 0; i < numentries; i++){
 
2625
        direntry = direntries[i];
 
2626
        char *fullname = NULL;
 
2627
        ret = asprintf(&fullname, "%s/%s", tempdir,
 
2628
                       direntry->d_name);
 
2629
        if(ret < 0){
 
2630
          perror_plus("asprintf");
 
2631
          continue;
 
2632
        }
 
2633
        ret = remove(fullname);
 
2634
        if(ret == -1){
 
2635
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
2636
                       strerror(errno));
 
2637
        }
 
2638
        free(fullname);
2633
2639
      }
2634
 
      TEMP_FAILURE_RETRY(close(tempdir_fd));
 
2640
    }
 
2641
    
 
2642
    /* need to clean even if 0 because man page doesn't specify */
 
2643
    free(direntries);
 
2644
    if(numentries == -1){
 
2645
      perror_plus("scandir");
 
2646
    }
 
2647
    ret = rmdir(tempdir);
 
2648
    if(ret == -1 and errno != ENOENT){
 
2649
      perror_plus("rmdir");
2635
2650
    }
2636
2651
  }
2637
2652