40
40
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
42
42
#include <stdio.h> /* fprintf(), stderr, fwrite(),
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(),
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
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
558
575
safer_gnutls_strerror(ret));
578
if(mc->dh_bits == 0){
579
/* Find out the optimal number of DH bits */
580
/* Try to read the private key file */
581
gnutls_datum_t buffer = { .data = NULL, .size = 0 };
583
int secfile = open(seckeyfilename, O_RDONLY);
584
size_t buffer_capacity = 0;
586
buffer_capacity = incbuffer((char **)&buffer.data,
588
(size_t)buffer_capacity);
589
if(buffer_capacity == 0){
590
perror_plus("incbuffer");
595
ssize_t bytes_read = read(secfile, buffer.data + buffer.size,
601
/* check bytes_read for failure */
608
buffer.size += (unsigned int)bytes_read;
612
/* If successful, use buffer to parse private key */
613
gnutls_sec_param_t sec_param = GNUTLS_SEC_PARAM_ULTRA;
614
if(buffer.data != NULL){
616
gnutls_openpgp_privkey_t privkey = NULL;
617
ret = gnutls_openpgp_privkey_init(&privkey);
618
if(ret != GNUTLS_E_SUCCESS){
619
fprintf_plus(stderr, "Error initializing OpenPGP key"
620
" structure: %s", safer_gnutls_strerror(ret));
624
ret = gnutls_openpgp_privkey_import(privkey, &buffer,
625
GNUTLS_OPENPGP_FMT_BASE64,
627
if(ret != GNUTLS_E_SUCCESS){
628
fprintf_plus(stderr, "Error importing OpenPGP key : %s",
629
safer_gnutls_strerror(ret));
635
/* Use private key to suggest an appropriate sec_param */
636
sec_param = gnutls_openpgp_privkey_sec_param(privkey);
637
gnutls_openpgp_privkey_deinit(privkey);
639
fprintf_plus(stderr, "This OpenPGP key implies using a"
640
" GnuTLS security parameter \"%s\".\n",
641
safe_string(gnutls_sec_param_get_name
647
if(sec_param == GNUTLS_SEC_PARAM_UNKNOWN){
648
/* Err on the side of caution */
649
sec_param = GNUTLS_SEC_PARAM_ULTRA;
651
fprintf_plus(stderr, "Falling back to security parameter"
653
safe_string(gnutls_sec_param_get_name
658
uret = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, sec_param);
662
fprintf_plus(stderr, "A \"%s\" GnuTLS security parameter"
663
" implies %u DH bits; using that.\n",
664
safe_string(gnutls_sec_param_get_name
669
fprintf_plus(stderr, "Failed to get implied number of DH"
670
" bits for security parameter \"%s\"): %s\n",
671
safe_string(gnutls_sec_param_get_name(sec_param)),
672
safer_gnutls_strerror(ret));
676
fprintf_plus(stderr, "DH bits explicitly set to %u\n",
561
679
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
562
680
if(ret != GNUTLS_E_SUCCESS){
563
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
564
safer_gnutls_strerror(ret));
681
fprintf_plus(stderr, "Error in GnuTLS prime generation (%u bits):"
682
" %s\n", mc->dh_bits, safer_gnutls_strerror(ret));
1500
1616
error_t ret_errno = 0;
1501
1617
if(setuid(uid) == -1){
1502
1618
ret_errno = errno;
1503
perror_plus("setuid");
1505
1620
errno = old_errno;
1506
1621
return ret_errno;
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]] |
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. */
1522
/* Store modified flag word in the descriptor. */
1523
return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
1526
#endif /* not O_CLOEXEC */
1528
1624
__attribute__((nonnull))
1529
1625
void run_network_hooks(const char *mode, const char *interface,
1530
1626
const float delay){
1531
struct dirent **direntries;
1627
struct dirent **direntries = NULL;
1532
1628
if(hookdir_fd == -1){
1533
hookdir_fd = open(hookdir, O_RDONLY |
1536
#else /* not O_CLOEXEC */
1538
#endif /* not O_CLOEXEC */
1629
hookdir_fd = open(hookdir, O_RDONLY);
1540
1630
if(hookdir_fd == -1){
1541
1631
if(errno == ENOENT){
1662
1742
_exit(EX_OSERR);
1665
if(fexecve(hookdir_fd, (char *const [])
1666
{ direntry->d_name, NULL }, environ) == -1){
1745
int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
1747
perror_plus("openat");
1748
_exit(EXIT_FAILURE);
1750
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1751
perror_plus("close");
1752
_exit(EXIT_FAILURE);
1754
if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
1667
1756
perror_plus("fexecve");
1668
1757
_exit(EXIT_FAILURE);
1761
perror_plus("fork");
1672
1766
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1673
1767
perror_plus("waitpid");
1676
1771
if(WIFEXITED(status)){
1678
1773
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1679
1774
" with status %d\n", direntry->d_name,
1680
1775
WEXITSTATUS(status));
1683
1779
} else if(WIFSIGNALED(status)){
1684
1780
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1685
1781
" signal %d\n", direntry->d_name,
1686
1782
WTERMSIG(status));
1689
1786
fprintf_plus(stderr, "Warning: network hook \"%s\""
1690
1787
" crashed\n", direntry->d_name);
1907
2009
int main(int argc, char *argv[]){
1908
mandos_context mc = { .server = NULL, .dh_bits = 1024,
2010
mandos_context mc = { .server = NULL, .dh_bits = 0,
1909
2011
.priority = "SECURE256:!CTYPE-X.509:"
1910
"+CTYPE-OPENPGP", .current_server = NULL,
2012
"+CTYPE-OPENPGP:!RSA", .current_server = NULL,
1911
2013
.interfaces = NULL, .interfaces_size = 0 };
1912
2014
AvahiSServiceBrowser *sb = NULL;
1913
2015
error_t ret_errno;
2617
2733
/* Removes the GPGME temp directory and all files inside */
2618
2734
if(tempdir != NULL){
2619
2735
struct dirent **direntries = NULL;
2620
struct dirent *direntry = NULL;
2621
int numentries = scandir(tempdir, &direntries, notdotentries,
2624
for(int i = 0; i < numentries; i++){
2625
direntry = direntries[i];
2626
char *fullname = NULL;
2627
ret = asprintf(&fullname, "%s/%s", tempdir,
2630
perror_plus("asprintf");
2633
ret = remove(fullname);
2635
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2736
int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2738
if(tempdir_fd == -1){
2739
perror_plus("open");
2742
#if __GLIBC_PREREQ(2, 15)
2743
int numentries = scandirat(tempdir_fd, ".", &direntries,
2744
notdotentries, alphasort);
2745
#else /* not __GLIBC_PREREQ(2, 15) */
2746
int numentries = scandir(tempdir, &direntries, notdotentries,
2748
#endif /* not __GLIBC_PREREQ(2, 15) */
2749
#else /* not __GLIBC__ */
2750
int numentries = scandir(tempdir, &direntries, notdotentries,
2752
#endif /* not __GLIBC__ */
2753
if(numentries >= 0){
2754
for(int i = 0; i < numentries; i++){
2755
ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2757
fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2758
" \"%s\", 0): %s\n", tempdir,
2759
direntries[i]->d_name, strerror(errno));
2761
free(direntries[i]);
2764
/* need to clean even if 0 because man page doesn't specify */
2766
if(numentries == -1){
2767
perror_plus("scandir");
2769
ret = rmdir(tempdir);
2770
if(ret == -1 and errno != ENOENT){
2771
perror_plus("rmdir");
2642
/* need to clean even if 0 because man page doesn't specify */
2644
if(numentries == -1){
2645
perror_plus("scandir");
2647
ret = rmdir(tempdir);
2648
if(ret == -1 and errno != ENOENT){
2649
perror_plus("rmdir");
2774
TEMP_FAILURE_RETRY(close(tempdir_fd));