/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-22 02:19:30 UTC
  • mto: (237.7.272 trunk)
  • mto: This revision was merged to the branch mainline in revision 317.
  • Revision ID: teddy@recompile.se-20140622021930-icl7h4cm97blhjml
mandos-keygen: Generate "checker" option to use SSH fingerprints.

To turn this off, use a new "--no-ssh" option to mandos-keygen.

* INSTALL (Mandos Server, Mandos Client): Document new suggested
                                          installation of SSH.
* Makefile (confdir/clients.conf): Use new "--no-ssh" option to
                                   "mandos-keygen".
* debian/control (mandos/Depends): Changed to "fping | ssh-client".
  (mandos-client/Recommends): New; set to "ssh".
* intro.xml (FREQUENTLY ASKED QUESTIONS): Rename and rewrite section
                                          called "Faking ping
                                          replies?" to address new
                                          default behavior.
* mandos-clients.conf.xml (OPTIONS/checker): Briefly discuss new
                                             behavior of
                                             mandos-keygen.
* mandos-keygen: Bug fix: Suppress failure output of "shred" to remove
                 "sec*", since no such files may exist.
 (password mode): Scan for SSH key fingerprints and output as new
                  "checker" and "ssh_fingerprint" options, unless new
                  "--no-ssh" option is given.
* mandos-keygen.xml (SYNOPSIS/--force): Bug fix: Document short form.
  (OPTIONS/--no-ssh): New.
  (SEE ALSO): Add reference "ssh-keyscan(1)".
* plugins.d/mandos-client.xml (SECURITY): Briefly mention the
                                          possibility of using SSH key
                                          fingerprints for checking.

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
1457
1458
  error_t ret_errno = 0;
1458
1459
  if(seteuid(0) == -1){
1459
1460
    ret_errno = errno;
1460
 
    perror_plus("seteuid");
1461
1461
  }
1462
1462
  errno = old_errno;
1463
1463
  return ret_errno;
1474
1474
  }
1475
1475
  if(setuid(0) == -1){
1476
1476
    ret_errno = errno;
1477
 
    perror_plus("seteuid");
1478
1477
  }
1479
1478
  errno = old_errno;
1480
1479
  return ret_errno;
1487
1486
  error_t ret_errno = 0;
1488
1487
  if(seteuid(uid) == -1){
1489
1488
    ret_errno = errno;
1490
 
    perror_plus("seteuid");
1491
1489
  }
1492
1490
  errno = old_errno;
1493
1491
  return ret_errno;
1500
1498
  error_t ret_errno = 0;
1501
1499
  if(setuid(uid) == -1){
1502
1500
    ret_errno = errno;
1503
 
    perror_plus("setuid");
1504
1501
  }
1505
1502
  errno = old_errno;
1506
1503
  return ret_errno;
1507
1504
}
1508
1505
 
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
 
 
1528
1506
__attribute__((nonnull))
1529
1507
void run_network_hooks(const char *mode, const char *interface,
1530
1508
                       const float delay){
1531
 
  struct dirent **direntries;
 
1509
  struct dirent **direntries = NULL;
1532
1510
  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
 
                      );
 
1511
    hookdir_fd = open(hookdir, O_RDONLY);
1540
1512
    if(hookdir_fd == -1){
1541
1513
      if(errno == ENOENT){
1542
1514
        if(debug){
1548
1520
      }
1549
1521
      return;
1550
1522
    }
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
1523
  }
1563
1524
#ifdef __GLIBC__
1564
1525
#if __GLIBC_PREREQ(2, 15)
1589
1550
    if(hook_pid == 0){
1590
1551
      /* Child */
1591
1552
      /* Raise privileges */
1592
 
      if(raise_privileges_permanently() != 0){
 
1553
      errno = raise_privileges_permanently();
 
1554
      if(errno != 0){
1593
1555
        perror_plus("Failed to raise privileges");
1594
1556
        _exit(EX_NOPERM);
1595
1557
      }
1662
1624
          _exit(EX_OSERR);
1663
1625
        }
1664
1626
      }
1665
 
      if(fexecve(hookdir_fd, (char *const [])
1666
 
                 { direntry->d_name, NULL }, environ) == -1){
 
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){
1667
1638
        perror_plus("fexecve");
1668
1639
        _exit(EXIT_FAILURE);
1669
1640
      }
1696
1667
                   direntry->d_name);
1697
1668
    }
1698
1669
  }
 
1670
  free(direntries);
1699
1671
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1700
1672
    perror_plus("close");
1701
1673
  } else {
1758
1730
    /* Raise privileges */
1759
1731
    ret_errno = raise_privileges();
1760
1732
    if(ret_errno != 0){
 
1733
      errno = ret_errno;
1761
1734
      perror_plus("Failed to raise privileges");
1762
1735
    }
1763
1736
    
1867
1840
    /* Raise privileges */
1868
1841
    ret_errno = raise_privileges();
1869
1842
    if(ret_errno != 0){
 
1843
      errno = ret_errno;
1870
1844
      perror_plus("Failed to raise privileges");
1871
1845
    }
1872
1846
    
2279
2253
  
2280
2254
  /* If no interfaces were specified, make a list */
2281
2255
  if(mc.interfaces == NULL){
2282
 
    struct dirent **direntries;
 
2256
    struct dirent **direntries = NULL;
2283
2257
    /* Look for any good interfaces */
2284
2258
    ret = scandir(sys_class_net, &direntries, good_interface,
2285
2259
                  alphasort);
2300
2274
      }
2301
2275
      free(direntries);
2302
2276
    } else {
2303
 
      free(direntries);
 
2277
      if(ret == 0){
 
2278
        free(direntries);
 
2279
      }
2304
2280
      fprintf_plus(stderr, "Could not find a network interface\n");
2305
2281
      exitcode = EXIT_FAILURE;
2306
2282
      goto end;
2579
2555
  {
2580
2556
    ret_errno = raise_privileges();
2581
2557
    if(ret_errno != 0){
 
2558
      errno = ret_errno;
2582
2559
      perror_plus("Failed to raise privileges");
2583
2560
    } else {
2584
2561
      
2607
2584
    
2608
2585
    ret_errno = lower_privileges_permanently();
2609
2586
    if(ret_errno != 0){
 
2587
      errno = ret_errno;
2610
2588
      perror_plus("Failed to lower privileges permanently");
2611
2589
    }
2612
2590
  }
2617
2595
  /* Removes the GPGME temp directory and all files inside */
2618
2596
  if(tempdir != NULL){
2619
2597
    struct dirent **direntries = NULL;
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);
 
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
        }
2639
2634
      }
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
      TEMP_FAILURE_RETRY(close(tempdir_fd));
2650
2636
    }
2651
2637
  }
2652
2638