575
559
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",
679
562
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
680
563
if(ret != GNUTLS_E_SUCCESS){
681
fprintf_plus(stderr, "Error in GnuTLS prime generation (%u bits):"
682
" %s\n", mc->dh_bits, safer_gnutls_strerror(ret));
564
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
565
safer_gnutls_strerror(ret));
1616
1501
error_t ret_errno = 0;
1617
1502
if(setuid(uid) == -1){
1618
1503
ret_errno = errno;
1504
perror_plus("setuid");
1620
1506
errno = old_errno;
1621
1507
return ret_errno;
1512
* Based on the example in the GNU LibC manual chapter 13.13 "File
1513
* Descriptor Flags".
1514
| [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
1516
__attribute__((warn_unused_result))
1517
static int set_cloexec_flag(int fd){
1518
int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
1519
/* If reading the flags failed, return error indication now. */
1523
/* Store modified flag word in the descriptor. */
1524
return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
1527
#endif /* not O_CLOEXEC */
1624
1529
__attribute__((nonnull))
1625
1530
void run_network_hooks(const char *mode, const char *interface,
1626
1531
const float delay){
1627
struct dirent **direntries = NULL;
1532
struct dirent **direntries;
1628
1533
if(hookdir_fd == -1){
1629
hookdir_fd = open(hookdir, O_RDONLY);
1534
hookdir_fd = open(hookdir, O_RDONLY |
1537
#else /* not O_CLOEXEC */
1539
#endif /* not O_CLOEXEC */
1630
1541
if(hookdir_fd == -1){
1631
1542
if(errno == ENOENT){
1742
1663
_exit(EX_OSERR);
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 },
1666
if(fexecve(hookdir_fd, (char *const [])
1667
{ direntry->d_name, NULL }, environ) == -1){
1756
1668
perror_plus("fexecve");
1757
1669
_exit(EXIT_FAILURE);
1761
perror_plus("fork");
1766
1673
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1767
1674
perror_plus("waitpid");
1771
1677
if(WIFEXITED(status)){
1773
1679
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1774
1680
" with status %d\n", direntry->d_name,
1775
1681
WEXITSTATUS(status));
1779
1684
} else if(WIFSIGNALED(status)){
1780
1685
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1781
1686
" signal %d\n", direntry->d_name,
1782
1687
WTERMSIG(status));
1786
1690
fprintf_plus(stderr, "Warning: network hook \"%s\""
1787
1691
" crashed\n", direntry->d_name);
2009
1908
int main(int argc, char *argv[]){
2010
mandos_context mc = { .server = NULL, .dh_bits = 0,
1909
mandos_context mc = { .server = NULL, .dh_bits = 1024,
2011
1910
.priority = "SECURE256:!CTYPE-X.509:"
2012
"+CTYPE-OPENPGP:!RSA", .current_server = NULL,
1911
"+CTYPE-OPENPGP", .current_server = NULL,
2013
1912
.interfaces = NULL, .interfaces_size = 0 };
2014
1913
AvahiSServiceBrowser *sb = NULL;
2015
1914
error_t ret_errno;