/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-03-29 02:38:15 UTC
  • Revision ID: teddy@recompile.se-20140329023815-bpgxi9v4ikrlcpst
Update copyright year.

* mandos-keygen: Update copyright year.
* plugin-runner.c: - '' -
* plugins.d/askpass-fifo.c: - '' -
* plugins.d/mandos-client.c: - '' -
* plugins.d/password-prompt.c: - '' -
* plugins.d/plymouth.c: - '' -
* plugins.d/splashy.c: - '' -
* plugins.d/usplash.c: - '' -

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
137
136
 
138
137
bool debug = false;
139
138
static const char mandos_protocol_version[] = "1";
140
 
const char *argp_program_version = "mandos-client " VERSION;
141
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
 
139
const char * argp_program_version = "mandos-client " VERSION;
 
140
const char * argp_program_bug_address = "<mandos@recompile.se>";
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");
1511
1516
void run_network_hooks(const char *mode, const char *interface,
1512
1517
                       const float delay){
1513
1518
  struct dirent **direntries;
1514
 
  if(hookdir_fd == -1){
1515
 
    hookdir_fd = open(hookdir, O_RDONLY);
1516
 
    if(hookdir_fd == -1){
1517
 
      if(errno == ENOENT){
1518
 
        if(debug){
1519
 
          fprintf_plus(stderr, "Network hook directory \"%s\" not"
1520
 
                       " found\n", hookdir);
1521
 
        }
1522
 
      } else {
1523
 
        perror_plus("open");
1524
 
      }
1525
 
      return;
1526
 
    }
1527
 
  }
1528
 
#ifdef __GLIBC__
1529
 
#if __GLIBC_PREREQ(2, 15)
1530
 
  int numhooks = scandirat(hookdir_fd, ".", &direntries,
1531
 
                           runnable_hook, alphasort);
1532
 
#else  /* not __GLIBC_PREREQ(2, 15) */
1533
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1534
 
                         alphasort);
1535
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
1536
 
#else   /* not __GLIBC__ */
1537
 
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1538
 
                         alphasort);
1539
 
#endif  /* not __GLIBC__ */
 
1519
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
 
1520
                         alphasort);
1540
1521
  if(numhooks == -1){
1541
 
    perror_plus("scandir");
1542
 
    return;
1543
 
  }
1544
 
  struct dirent *direntry;
1545
 
  int ret;
1546
 
  int devnull = open("/dev/null", O_RDONLY);
1547
 
  for(int i = 0; i < numhooks; i++){
1548
 
    direntry = direntries[i];
1549
 
    if(debug){
1550
 
      fprintf_plus(stderr, "Running network hook \"%s\"\n",
1551
 
                   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");
1552
1529
    }
1553
 
    pid_t hook_pid = fork();
1554
 
    if(hook_pid == 0){
1555
 
      /* Child */
1556
 
      /* Raise privileges */
1557
 
      if(raise_privileges_permanently() != 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){
 
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){
1613
1539
        perror_plus("asprintf");
1614
 
        _exit(EX_OSERR);
1615
 
      }
1616
 
      ret = setenv("DELAY", delaystring, 1);
1617
 
      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
        }
1618
1615
        free(delaystring);
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;
 
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
        }
1662
1627
      } else {
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
 
  if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1674
 
    perror_plus("close");
1675
 
  } else {
1676
 
    hookdir_fd = -1;
1677
 
  }
1678
 
  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
  }
1679
1663
}
1680
1664
 
1681
1665
__attribute__((nonnull, warn_unused_result))
1731
1715
    
1732
1716
    /* Raise privileges */
1733
1717
    ret_errno = raise_privileges();
 
1718
    bool restore_loglevel = false;
1734
1719
    if(ret_errno != 0){
1735
1720
      perror_plus("Failed to raise privileges");
1736
1721
    }
1737
 
    
1738
1722
#ifdef __linux__
1739
1723
    int ret_linux;
1740
 
    bool restore_loglevel = false;
1741
1724
    if(ret_errno == 0){
1742
1725
      /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1743
1726
         messages about the network interface to mess up the prompt */
1843
1826
    if(ret_errno != 0){
1844
1827
      perror_plus("Failed to raise privileges");
1845
1828
    }
1846
 
    
1847
1829
    int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1848
1830
    ioctl_errno = errno;
1849
1831
    
2502
2484
  if(debug){
2503
2485
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2504
2486
  }
2505
 
  
 
2487
 
2506
2488
  ret = avahi_loop_with_timeout(simple_poll,
2507
2489
                                (int)(retry_interval * 1000), &mc);
2508
2490
  if(debug){
2578
2560
        }
2579
2561
      }
2580
2562
    }
2581
 
    
2582
2563
    ret_errno = lower_privileges_permanently();
2583
2564
    if(ret_errno != 0){
2584
2565
      perror_plus("Failed to lower privileges permanently");
2591
2572
  /* Removes the GPGME temp directory and all files inside */
2592
2573
  if(tempdir != NULL){
2593
2574
    struct dirent **direntries = NULL;
2594
 
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2595
 
                                                  O_NOFOLLOW));
2596
 
    if(tempdir_fd == -1){
2597
 
      perror_plus("open");
2598
 
    } else {
2599
 
#ifdef __GLIBC__
2600
 
#if __GLIBC_PREREQ(2, 15)
2601
 
      int numentries = scandirat(tempdir_fd, ".", &direntries,
2602
 
                                 notdotentries, alphasort);
2603
 
#else  /* not __GLIBC_PREREQ(2, 15) */
2604
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2605
 
                               alphasort);
2606
 
#endif  /* not __GLIBC_PREREQ(2, 15) */
2607
 
#else   /* not __GLIBC__ */
2608
 
      int numentries = scandir(tempdir, &direntries, notdotentries,
2609
 
                               alphasort);
2610
 
#endif  /* not __GLIBC__ */
2611
 
      if(numentries > 0){
2612
 
        for(int i = 0; i < numentries; i++){
2613
 
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2614
 
          if(ret == -1){
2615
 
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2616
 
                         " \"%s\", 0): %s\n", tempdir,
2617
 
                         direntries[i]->d_name, strerror(errno));
2618
 
          }
2619
 
        }
2620
 
        
2621
 
        /* need to clean even if 0 because man page doesn't specify */
2622
 
        free(direntries);
2623
 
        if(numentries == -1){
2624
 
          perror_plus("scandir");
2625
 
        }
2626
 
        ret = rmdir(tempdir);
2627
 
        if(ret == -1 and errno != ENOENT){
2628
 
          perror_plus("rmdir");
2629
 
        }
 
2575
    struct dirent *direntry = NULL;
 
2576
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2577
                             alphasort);
 
2578
    if(numentries > 0){
 
2579
      for(int i = 0; i < numentries; i++){
 
2580
        direntry = direntries[i];
 
2581
        char *fullname = NULL;
 
2582
        ret = asprintf(&fullname, "%s/%s", tempdir,
 
2583
                       direntry->d_name);
 
2584
        if(ret < 0){
 
2585
          perror_plus("asprintf");
 
2586
          continue;
 
2587
        }
 
2588
        ret = remove(fullname);
 
2589
        if(ret == -1){
 
2590
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
 
2591
                       strerror(errno));
 
2592
        }
 
2593
        free(fullname);
2630
2594
      }
2631
 
      TEMP_FAILURE_RETRY(close(tempdir_fd));
 
2595
    }
 
2596
 
 
2597
    /* need to clean even if 0 because man page doesn't specify */
 
2598
    free(direntries);
 
2599
    if(numentries == -1){
 
2600
      perror_plus("scandir");
 
2601
    }
 
2602
    ret = rmdir(tempdir);
 
2603
    if(ret == -1 and errno != ENOENT){
 
2604
      perror_plus("rmdir");
2632
2605
    }
2633
2606
  }
2634
2607