532
571
/* GnuTLS server initialization */
533
ret = gnutls_dh_params_init(&mc.dh_params);
572
ret = gnutls_dh_params_init(&mc->dh_params);
534
573
if(ret != GNUTLS_E_SUCCESS){
535
574
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
536
575
" initialization: %s\n",
537
576
safer_gnutls_strerror(ret));
540
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
541
if(ret != GNUTLS_E_SUCCESS){
542
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
543
safer_gnutls_strerror(ret));
547
gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
579
/* If a Diffie-Hellman parameters file was given, try to use it */
580
if(dhparamsfilename != NULL){
581
gnutls_datum_t params = { .data = NULL, .size = 0 };
583
int dhpfile = open(dhparamsfilename, O_RDONLY);
586
dhparamsfilename = NULL;
589
size_t params_capacity = 0;
591
params_capacity = incbuffer((char **)¶ms.data,
593
(size_t)params_capacity);
594
if(params_capacity == 0){
595
perror_plus("incbuffer");
598
dhparamsfilename = NULL;
601
ssize_t bytes_read = read(dhpfile,
602
params.data + params.size,
608
/* check bytes_read for failure */
613
dhparamsfilename = NULL;
616
params.size += (unsigned int)bytes_read;
618
if(params.data == NULL){
619
dhparamsfilename = NULL;
621
if(dhparamsfilename == NULL){
624
ret = gnutls_dh_params_import_pkcs3(mc->dh_params, ¶ms,
625
GNUTLS_X509_FMT_PEM);
626
if(ret != GNUTLS_E_SUCCESS){
627
fprintf_plus(stderr, "Failed to parse DH parameters in file"
628
" \"%s\": %s\n", dhparamsfilename,
629
safer_gnutls_strerror(ret));
630
dhparamsfilename = NULL;
634
if(dhparamsfilename == NULL){
635
if(mc->dh_bits == 0){
636
/* Find out the optimal number of DH bits */
637
/* Try to read the private key file */
638
gnutls_datum_t buffer = { .data = NULL, .size = 0 };
640
int secfile = open(seckeyfilename, O_RDONLY);
645
size_t buffer_capacity = 0;
647
buffer_capacity = incbuffer((char **)&buffer.data,
649
(size_t)buffer_capacity);
650
if(buffer_capacity == 0){
651
perror_plus("incbuffer");
656
ssize_t bytes_read = read(secfile,
657
buffer.data + buffer.size,
663
/* check bytes_read for failure */
670
buffer.size += (unsigned int)bytes_read;
674
/* If successful, use buffer to parse private key */
675
gnutls_sec_param_t sec_param = GNUTLS_SEC_PARAM_ULTRA;
676
if(buffer.data != NULL){
678
gnutls_openpgp_privkey_t privkey = NULL;
679
ret = gnutls_openpgp_privkey_init(&privkey);
680
if(ret != GNUTLS_E_SUCCESS){
681
fprintf_plus(stderr, "Error initializing OpenPGP key"
683
safer_gnutls_strerror(ret));
687
ret = gnutls_openpgp_privkey_import
688
(privkey, &buffer, GNUTLS_OPENPGP_FMT_BASE64, "", 0);
689
if(ret != GNUTLS_E_SUCCESS){
690
fprintf_plus(stderr, "Error importing OpenPGP key : %s",
691
safer_gnutls_strerror(ret));
697
/* Use private key to suggest an appropriate
699
sec_param = gnutls_openpgp_privkey_sec_param(privkey);
700
gnutls_openpgp_privkey_deinit(privkey);
702
fprintf_plus(stderr, "This OpenPGP key implies using"
703
" a GnuTLS security parameter \"%s\".\n",
704
safe_string(gnutls_sec_param_get_name
710
if(sec_param == GNUTLS_SEC_PARAM_UNKNOWN){
711
/* Err on the side of caution */
712
sec_param = GNUTLS_SEC_PARAM_ULTRA;
714
fprintf_plus(stderr, "Falling back to security parameter"
716
safe_string(gnutls_sec_param_get_name
721
uret = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, sec_param);
725
fprintf_plus(stderr, "A \"%s\" GnuTLS security parameter"
726
" implies %u DH bits; using that.\n",
727
safe_string(gnutls_sec_param_get_name
732
fprintf_plus(stderr, "Failed to get implied number of DH"
733
" bits for security parameter \"%s\"): %s\n",
734
safe_string(gnutls_sec_param_get_name
736
safer_gnutls_strerror(ret));
740
fprintf_plus(stderr, "DH bits explicitly set to %u\n",
743
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
744
if(ret != GNUTLS_E_SUCCESS){
745
fprintf_plus(stderr, "Error in GnuTLS prime generation (%u"
746
" bits): %s\n", mc->dh_bits,
747
safer_gnutls_strerror(ret));
751
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
553
gnutls_certificate_free_credentials(mc.cred);
757
gnutls_certificate_free_credentials(mc->cred);
554
758
gnutls_global_deinit();
555
gnutls_dh_params_deinit(mc.dh_params);
759
gnutls_dh_params_deinit(mc->dh_params);
559
static int init_gnutls_session(gnutls_session_t *session){
763
__attribute__((nonnull, warn_unused_result))
764
static int init_gnutls_session(gnutls_session_t *session,
561
767
/* GnuTLS session creation */
616
820
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
617
821
__attribute__((unused)) const char *txt){}
823
/* Set effective uid to 0, return errno */
824
__attribute__((warn_unused_result))
825
error_t raise_privileges(void){
826
error_t old_errno = errno;
827
error_t ret_errno = 0;
828
if(seteuid(0) == -1){
835
/* Set effective and real user ID to 0. Return errno. */
836
__attribute__((warn_unused_result))
837
error_t raise_privileges_permanently(void){
838
error_t old_errno = errno;
839
error_t ret_errno = raise_privileges();
851
/* Set effective user ID to unprivileged saved user ID */
852
__attribute__((warn_unused_result))
853
error_t lower_privileges(void){
854
error_t old_errno = errno;
855
error_t ret_errno = 0;
856
if(seteuid(uid) == -1){
863
/* Lower privileges permanently */
864
__attribute__((warn_unused_result))
865
error_t lower_privileges_permanently(void){
866
error_t old_errno = errno;
867
error_t ret_errno = 0;
868
if(setuid(uid) == -1){
875
/* Helper function to add_local_route() and delete_local_route() */
876
__attribute__((nonnull, warn_unused_result))
877
static bool add_delete_local_route(const bool add,
879
AvahiIfIndex if_index){
881
char helper[] = "mandos-client-iprouteadddel";
882
char add_arg[] = "add";
883
char delete_arg[] = "delete";
884
char debug_flag[] = "--debug";
885
char *pluginhelperdir = getenv("MANDOSPLUGINHELPERDIR");
886
if(pluginhelperdir == NULL){
888
fprintf_plus(stderr, "MANDOSPLUGINHELPERDIR environment"
889
" variable not set; cannot run helper\n");
894
char interface[IF_NAMESIZE];
895
if(if_indextoname((unsigned int)if_index, interface) == NULL){
896
perror_plus("if_indextoname");
900
int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
902
perror_plus("open(\"/dev/null\", O_RDONLY)");
908
/* Raise privileges */
909
errno = raise_privileges_permanently();
911
perror_plus("Failed to raise privileges");
912
/* _exit(EX_NOPERM); */
918
perror_plus("setgid");
921
/* Reset supplementary groups */
923
ret = setgroups(0, NULL);
925
perror_plus("setgroups");
929
ret = dup2(devnull, STDIN_FILENO);
931
perror_plus("dup2(devnull, STDIN_FILENO)");
934
ret = close(devnull);
936
perror_plus("close");
939
ret = dup2(STDERR_FILENO, STDOUT_FILENO);
941
perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
944
int helperdir_fd = (int)TEMP_FAILURE_RETRY(open(pluginhelperdir,
949
if(helperdir_fd == -1){
951
_exit(EX_UNAVAILABLE);
953
int helper_fd = (int)TEMP_FAILURE_RETRY(openat(helperdir_fd,
956
perror_plus("openat");
958
_exit(EX_UNAVAILABLE);
962
#pragma GCC diagnostic push
963
#pragma GCC diagnostic ignored "-Wcast-qual"
965
if(fexecve(helper_fd, (char *const [])
966
{ helper, add ? add_arg : delete_arg, (char *)address,
967
interface, debug ? debug_flag : NULL, NULL },
970
#pragma GCC diagnostic pop
972
perror_plus("fexecve");
984
pret = waitpid(pid, &status, 0);
985
if(pret == -1 and errno == EINTR and quit_now){
986
int errno_raising = 0;
987
if((errno = raise_privileges()) != 0){
988
errno_raising = errno;
989
perror_plus("Failed to raise privileges in order to"
990
" kill helper program");
992
if(kill(pid, SIGTERM) == -1){
995
if((errno_raising == 0) and (errno = lower_privileges()) != 0){
996
perror_plus("Failed to lower privileges after killing"
1001
} while(pret == -1 and errno == EINTR);
1003
perror_plus("waitpid");
1006
if(WIFEXITED(status)){
1007
if(WEXITSTATUS(status) != 0){
1008
fprintf_plus(stderr, "Error: iprouteadddel exited"
1009
" with status %d\n", WEXITSTATUS(status));
1014
if(WIFSIGNALED(status)){
1015
fprintf_plus(stderr, "Error: iprouteadddel died by"
1016
" signal %d\n", WTERMSIG(status));
1019
fprintf_plus(stderr, "Error: iprouteadddel crashed\n");
1023
__attribute__((nonnull, warn_unused_result))
1024
static bool add_local_route(const char *address,
1025
AvahiIfIndex if_index){
1027
fprintf_plus(stderr, "Adding route to %s\n", address);
1029
return add_delete_local_route(true, address, if_index);
1032
__attribute__((nonnull, warn_unused_result))
1033
static bool delete_local_route(const char *address,
1034
AvahiIfIndex if_index){
1036
fprintf_plus(stderr, "Removing route to %s\n", address);
1038
return add_delete_local_route(false, address, if_index);
619
1041
/* Called when a Mandos server is found */
620
static int start_mandos_communication(const char *ip, uint16_t port,
1042
__attribute__((nonnull, warn_unused_result))
1043
static int start_mandos_communication(const char *ip, in_port_t port,
621
1044
AvahiIfIndex if_index,
1045
int af, mandos_context *mc){
623
1046
int ret, tcp_sd = -1;
626
struct sockaddr_in in;
627
struct sockaddr_in6 in6;
1048
struct sockaddr_storage to;
629
1049
char *buffer = NULL;
630
1050
char *decrypted_buffer = NULL;
631
1051
size_t buffer_length = 0;
732
1180
if(if_indextoname((unsigned int)if_index, interface) == NULL){
733
1181
perror_plus("if_indextoname");
735
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
736
"\n", ip, interface, port);
1183
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
1184
"\n", ip, interface, (uintmax_t)port);
739
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
1187
fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
1188
ip, (uintmax_t)port);
742
1190
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
743
1191
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
746
pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
749
pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
753
perror_plus("inet_ntop");
755
if(strcmp(addrstr, ip) != 0){
756
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
767
ret = connect(tcp_sd, &to.in6, sizeof(to));
769
ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
772
if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
774
perror_plus("connect");
1193
ret = getnameinfo((struct sockaddr *)&to,
1194
sizeof(struct sockaddr_in6),
1195
addrstr, sizeof(addrstr), NULL, 0,
1198
ret = getnameinfo((struct sockaddr *)&to,
1199
sizeof(struct sockaddr_in),
1200
addrstr, sizeof(addrstr), NULL, 0,
1203
if(ret == EAI_SYSTEM){
1204
perror_plus("getnameinfo");
1205
} else if(ret != 0) {
1206
fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
1207
} else if(strcmp(addrstr, ip) != 0){
1208
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
1219
ret = connect(tcp_sd, (struct sockaddr *)&to,
1220
sizeof(struct sockaddr_in6));
1222
ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
1223
sizeof(struct sockaddr_in));
1226
if(((errno == ENETUNREACH) or (errno == EHOSTUNREACH))
1227
and if_index != AVAHI_IF_UNSPEC
1228
and connect_to == NULL
1229
and not route_added and
1230
((af == AF_INET6 and not
1231
IN6_IS_ADDR_LINKLOCAL(&(((struct sockaddr_in6 *)
1233
or (af == AF_INET and
1234
/* Not a a IPv4LL address */
1235
(ntohl(((struct sockaddr_in *)&to)->sin_addr.s_addr)
1236
& 0xFFFF0000L) != 0xA9FE0000L))){
1237
/* Work around Avahi bug - Avahi does not announce link-local
1238
addresses if it has a global address, so local hosts with
1239
*only* a link-local address (e.g. Mandos clients) cannot
1240
connect to a Mandos server announced by Avahi on a server
1241
host with a global address. Work around this by retrying
1242
with an explicit route added with the server's address.
1244
Avahi bug reference:
1245
http://lists.freedesktop.org/archives/avahi/2010-February/001833.html
1246
https://bugs.debian.org/587961
1249
fprintf_plus(stderr, "Mandos server unreachable, trying"
1253
route_added = add_local_route(ip, if_index);
1259
if(errno != ECONNREFUSED or debug){
1261
perror_plus("connect");
785
1274
const char *out = mandos_protocol_version;
1380
1888
ret = avahi_simple_poll_iterate(s, (int)block_time);
1383
if (ret > 0 or errno != EINTR){
1891
if(ret > 0 or errno != EINTR){
1384
1892
return (ret != 1) ? ret : 0;
1390
bool run_network_hooks(const char *mode, const char *interface,
1898
__attribute__((nonnull))
1899
void run_network_hooks(const char *mode, const char *interface,
1391
1900
const float delay){
1392
struct dirent **direntries;
1393
struct dirent *direntry;
1395
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1901
struct dirent **direntries = NULL;
1902
if(hookdir_fd == -1){
1903
hookdir_fd = open(hookdir, O_RDONLY | O_DIRECTORY | O_PATH
1905
if(hookdir_fd == -1){
1906
if(errno == ENOENT){
1908
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1909
" found\n", hookdir);
1912
perror_plus("open");
1917
int numhooks = scandirat(hookdir_fd, ".", &direntries,
1918
runnable_hook, alphasort);
1397
1919
if(numhooks == -1){
1398
1920
perror_plus("scandir");
1400
int devnull = open("/dev/null", O_RDONLY);
1401
for(int i = 0; i < numhooks; i++){
1402
direntry = direntries[i];
1403
char *fullname = NULL;
1404
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1923
struct dirent *direntry;
1925
int devnull = (int)TEMP_FAILURE_RETRY(open("/dev/null", O_RDONLY));
1927
perror_plus("open(\"/dev/null\", O_RDONLY)");
1930
for(int i = 0; i < numhooks; i++){
1931
direntry = direntries[i];
1933
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1936
pid_t hook_pid = fork();
1939
/* Raise privileges */
1940
errno = raise_privileges_permanently();
1942
perror_plus("Failed to raise privileges");
1949
perror_plus("setgid");
1952
/* Reset supplementary groups */
1954
ret = setgroups(0, NULL);
1956
perror_plus("setgroups");
1959
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1961
perror_plus("setenv");
1964
ret = setenv("DEVICE", interface, 1);
1966
perror_plus("setenv");
1969
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1971
perror_plus("setenv");
1974
ret = setenv("MODE", mode, 1);
1976
perror_plus("setenv");
1980
ret = asprintf(&delaystring, "%f", (double)delay);
1406
1982
perror_plus("asprintf");
1410
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1413
pid_t hook_pid = fork();
1416
/* Raise privileges */
1420
perror_plus("seteuid");
1422
/* Raise privileges even more */
1426
perror_plus("setuid");
1432
perror_plus("setgid");
1434
/* Reset supplementary groups */
1436
ret = setgroups(0, NULL);
1438
perror_plus("setgroups");
1440
dup2(devnull, STDIN_FILENO);
1442
dup2(STDERR_FILENO, STDOUT_FILENO);
1443
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1445
perror_plus("setenv");
1448
ret = setenv("DEVICE", interface, 1);
1450
perror_plus("setenv");
1453
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1455
perror_plus("setenv");
1458
ret = setenv("MODE", mode, 1);
1460
perror_plus("setenv");
1464
ret = asprintf(&delaystring, "%f", delay);
1466
perror_plus("asprintf");
1469
ret = setenv("DELAY", delaystring, 1);
1472
perror_plus("setenv");
1985
ret = setenv("DELAY", delaystring, 1);
1475
1987
free(delaystring);
1476
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1477
perror_plus("execl");
1478
_exit(EXIT_FAILURE);
1482
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1483
perror_plus("waitpid");
1487
if(WIFEXITED(status)){
1488
if(WEXITSTATUS(status) != 0){
1489
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1490
" with status %d\n", direntry->d_name,
1491
WEXITSTATUS(status));
1495
} else if(WIFSIGNALED(status)){
1496
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1497
" signal %d\n", direntry->d_name,
1502
fprintf_plus(stderr, "Warning: network hook \"%s\""
1503
" crashed\n", direntry->d_name);
1510
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1988
perror_plus("setenv");
1992
if(connect_to != NULL){
1993
ret = setenv("CONNECT", connect_to, 1);
1995
perror_plus("setenv");
1999
int hook_fd = (int)TEMP_FAILURE_RETRY(openat(hookdir_fd,
2003
perror_plus("openat");
2004
_exit(EXIT_FAILURE);
2006
if(close(hookdir_fd) == -1){
2007
perror_plus("close");
2008
_exit(EXIT_FAILURE);
2010
ret = dup2(devnull, STDIN_FILENO);
2012
perror_plus("dup2(devnull, STDIN_FILENO)");
2015
ret = close(devnull);
2017
perror_plus("close");
2020
ret = dup2(STDERR_FILENO, STDOUT_FILENO);
2022
perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
2025
if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
2027
perror_plus("fexecve");
2028
_exit(EXIT_FAILURE);
2032
perror_plus("fork");
2037
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
2038
perror_plus("waitpid");
2042
if(WIFEXITED(status)){
2043
if(WEXITSTATUS(status) != 0){
2044
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
2045
" with status %d\n", direntry->d_name,
2046
WEXITSTATUS(status));
2050
} else if(WIFSIGNALED(status)){
2051
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
2052
" signal %d\n", direntry->d_name,
2057
fprintf_plus(stderr, "Warning: network hook \"%s\""
2058
" crashed\n", direntry->d_name);
2064
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
2070
if(close(hookdir_fd) == -1){
2071
perror_plus("close");
2078
__attribute__((nonnull, warn_unused_result))
2079
error_t bring_up_interface(const char *const interface,
2081
error_t old_errno = errno;
2083
struct ifreq network;
2084
unsigned int if_index = if_nametoindex(interface);
2086
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
2096
if(not interface_is_up(interface)){
2097
error_t ret_errno = 0, ioctl_errno = 0;
2098
if(not get_flags(interface, &network)){
2100
fprintf_plus(stderr, "Failed to get flags for interface "
2101
"\"%s\"\n", interface);
2105
network.ifr_flags |= IFF_UP; /* set flag */
2107
int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
2110
perror_plus("socket");
2118
perror_plus("close");
2125
fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
2129
/* Raise privileges */
2130
ret_errno = raise_privileges();
2133
perror_plus("Failed to raise privileges");
2138
bool restore_loglevel = false;
2140
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
2141
messages about the network interface to mess up the prompt */
2142
ret_linux = klogctl(8, NULL, 5);
2143
if(ret_linux == -1){
2144
perror_plus("klogctl");
2146
restore_loglevel = true;
2149
#endif /* __linux__ */
2150
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
2151
ioctl_errno = errno;
2153
if(restore_loglevel){
2154
ret_linux = klogctl(7, NULL, 0);
2155
if(ret_linux == -1){
2156
perror_plus("klogctl");
2159
#endif /* __linux__ */
2161
/* If raise_privileges() succeeded above */
2163
/* Lower privileges */
2164
ret_errno = lower_privileges();
2167
perror_plus("Failed to lower privileges");
2171
/* Close the socket */
2174
perror_plus("close");
2177
if(ret_setflags == -1){
2178
errno = ioctl_errno;
2179
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
2184
fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
2188
/* Sleep checking until interface is running.
2189
Check every 0.25s, up to total time of delay */
2190
for(int i=0; i < delay * 4; i++){
2191
if(interface_is_running(interface)){
2194
struct timespec sleeptime = { .tv_nsec = 250000000 };
2195
ret = nanosleep(&sleeptime, NULL);
2196
if(ret == -1 and errno != EINTR){
2197
perror_plus("nanosleep");
2205
__attribute__((nonnull, warn_unused_result))
2206
error_t take_down_interface(const char *const interface){
2207
error_t old_errno = errno;
2208
struct ifreq network;
2209
unsigned int if_index = if_nametoindex(interface);
2211
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
2215
if(interface_is_up(interface)){
2216
error_t ret_errno = 0, ioctl_errno = 0;
2217
if(not get_flags(interface, &network) and debug){
2219
fprintf_plus(stderr, "Failed to get flags for interface "
2220
"\"%s\"\n", interface);
2224
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2226
int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
2229
perror_plus("socket");
2235
fprintf_plus(stderr, "Taking down interface \"%s\"\n",
2239
/* Raise privileges */
2240
ret_errno = raise_privileges();
2243
perror_plus("Failed to raise privileges");
2246
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
2247
ioctl_errno = errno;
2249
/* If raise_privileges() succeeded above */
2251
/* Lower privileges */
2252
ret_errno = lower_privileges();
2255
perror_plus("Failed to lower privileges");
2259
/* Close the socket */
2260
int ret = close(sd);
2262
perror_plus("close");
2265
if(ret_setflags == -1){
2266
errno = ioctl_errno;
2267
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2272
fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1519
2280
int main(int argc, char *argv[]){
2281
mandos_context mc = { .server = NULL, .dh_bits = 0,
2282
.priority = "SECURE256:!CTYPE-X.509"
2283
":+CTYPE-OPENPGP:!RSA:+SIGN-DSA-SHA256",
2284
.current_server = NULL, .interfaces = NULL,
2285
.interfaces_size = 0 };
1520
2286
AvahiSServiceBrowser *sb = NULL;
1523
2289
intmax_t tmpmax;
1525
2291
int exitcode = EXIT_SUCCESS;
1526
const char *interface = "";
1527
struct ifreq network;
1529
bool take_down_interface = false;
1532
char tempdir[] = "/tmp/mandosXXXXXX";
1533
bool tempdir_created = false;
2292
char *interfaces_to_take_down = NULL;
2293
size_t interfaces_to_take_down_size = 0;
2294
char run_tempdir[] = "/run/tmp/mandosXXXXXX";
2295
char old_tempdir[] = "/tmp/mandosXXXXXX";
2296
char *tempdir = NULL;
1534
2297
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1535
2298
const char *seckey = PATHDIR "/" SECKEY;
1536
2299
const char *pubkey = PATHDIR "/" PUBKEY;
2300
const char *dh_params_file = NULL;
2301
char *interfaces_hooks = NULL;
1538
2303
bool gnutls_initialized = false;
1539
2304
bool gpgme_initialized = false;
1762
TEMP_FAILURE_RETRY(close(pubkey_fd));
2543
if(dh_params_file != NULL
2544
and strcmp(dh_params_file, PATHDIR "/dhparams.pem" ) == 0){
2545
int dhparams_fd = open(dh_params_file, O_RDONLY);
2546
if(dhparams_fd == -1){
2547
perror_plus("open");
2549
ret = (int)TEMP_FAILURE_RETRY(fstat(dhparams_fd, &st));
2551
perror_plus("fstat");
2553
if(S_ISREG(st.st_mode)
2554
and st.st_uid == 0 and st.st_gid == 0){
2555
ret = fchown(dhparams_fd, uid, gid);
2557
perror_plus("fchown");
1766
2565
/* Lower privileges */
1770
perror_plus("seteuid");
2566
ret_errno = lower_privileges();
2569
perror_plus("Failed to lower privileges");
2574
/* Remove invalid interface names (except "none") */
2576
char *interface = NULL;
2577
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2579
if(strcmp(interface, "none") != 0
2580
and if_nametoindex(interface) == 0){
2581
if(interface[0] != '\0'){
2582
fprintf_plus(stderr, "Not using nonexisting interface"
2583
" \"%s\"\n", interface);
2585
argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
1775
2591
/* Run network hooks */
1776
if(not run_network_hooks("start", interface, delay)){
2593
if(mc.interfaces != NULL){
2594
interfaces_hooks = malloc(mc.interfaces_size);
2595
if(interfaces_hooks == NULL){
2596
perror_plus("malloc");
2599
memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2600
argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2602
run_network_hooks("start", interfaces_hooks != NULL ?
2603
interfaces_hooks : "", delay);
1781
2607
avahi_set_log_function(empty_log);
1784
if(interface[0] == '\0'){
1785
struct dirent **direntries;
1786
/* First look for interfaces that are up */
1787
ret = scandir(sys_class_net, &direntries, up_interface,
1790
/* No up interfaces, look for any good interfaces */
1792
ret = scandir(sys_class_net, &direntries, good_interface,
1796
/* Pick the first interface returned */
1797
interface = strdup(direntries[0]->d_name);
1799
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1801
if(interface == NULL){
1802
perror_plus("malloc");
1804
exitcode = EXIT_FAILURE;
1810
fprintf_plus(stderr, "Could not find a network interface\n");
1811
exitcode = EXIT_FAILURE;
1816
2610
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1817
2611
from the signal handler */
1818
2612
/* Initialize the pseudo-RNG for Avahi */
1819
2613
srand((unsigned int) time(NULL));
1820
mc.simple_poll = avahi_simple_poll_new();
1821
if(mc.simple_poll == NULL){
2614
simple_poll = avahi_simple_poll_new();
2615
if(simple_poll == NULL){
1822
2616
fprintf_plus(stderr,
1823
2617
"Avahi: Failed to create simple poll object.\n");
1824
2618
exitcode = EX_UNAVAILABLE;
1891
/* If the interface is down, bring it up */
1892
if(strcmp(interface, "none") != 0){
1893
if_index = (AvahiIfIndex) if_nametoindex(interface);
1895
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1896
exitcode = EX_UNAVAILABLE;
1904
/* Re-raise priviliges */
1908
perror_plus("seteuid");
1912
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1913
messages about the network interface to mess up the prompt */
1914
ret = klogctl(8, NULL, 5);
1915
bool restore_loglevel = true;
1917
restore_loglevel = false;
1918
perror_plus("klogctl");
1920
#endif /* __linux__ */
1922
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1924
perror_plus("socket");
1925
exitcode = EX_OSERR;
1927
if(restore_loglevel){
1928
ret = klogctl(7, NULL, 0);
1930
perror_plus("klogctl");
1933
#endif /* __linux__ */
1934
/* Lower privileges */
1938
perror_plus("seteuid");
1942
strcpy(network.ifr_name, interface);
1943
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1945
perror_plus("ioctl SIOCGIFFLAGS");
1947
if(restore_loglevel){
1948
ret = klogctl(7, NULL, 0);
1950
perror_plus("klogctl");
1953
#endif /* __linux__ */
1954
exitcode = EX_OSERR;
1955
/* Lower privileges */
1959
perror_plus("seteuid");
1963
if((network.ifr_flags & IFF_UP) == 0){
1964
network.ifr_flags |= IFF_UP;
1965
take_down_interface = true;
1966
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1968
take_down_interface = false;
1969
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1970
exitcode = EX_OSERR;
1972
if(restore_loglevel){
1973
ret = klogctl(7, NULL, 0);
1975
perror_plus("klogctl");
2685
/* If no interfaces were specified, make a list */
2686
if(mc.interfaces == NULL){
2687
struct dirent **direntries = NULL;
2688
/* Look for any good interfaces */
2689
ret = scandir(sys_class_net, &direntries, good_interface,
2692
/* Add all found interfaces to interfaces list */
2693
for(int i = 0; i < ret; ++i){
2694
ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2695
direntries[i]->d_name);
2698
perror_plus("argz_add");
2699
free(direntries[i]);
2703
fprintf_plus(stderr, "Will use interface \"%s\"\n",
2704
direntries[i]->d_name);
2706
free(direntries[i]);
2713
fprintf_plus(stderr, "Could not find a network interface\n");
2714
exitcode = EXIT_FAILURE;
2719
/* Bring up interfaces which are down, and remove any "none"s */
2721
char *interface = NULL;
2722
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2724
/* If interface name is "none", stop bringing up interfaces.
2725
Also remove all instances of "none" from the list */
2726
if(strcmp(interface, "none") == 0){
2727
argz_delete(&mc.interfaces, &mc.interfaces_size,
2730
while((interface = argz_next(mc.interfaces,
2731
mc.interfaces_size, interface))){
2732
if(strcmp(interface, "none") == 0){
2733
argz_delete(&mc.interfaces, &mc.interfaces_size,
1978
#endif /* __linux__ */
1979
/* Lower privileges */
1983
perror_plus("seteuid");
1988
/* Sleep checking until interface is running.
1989
Check every 0.25s, up to total time of delay */
1990
for(int i=0; i < delay * 4; i++){
1991
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1993
perror_plus("ioctl SIOCGIFFLAGS");
1994
} else if(network.ifr_flags & IFF_RUNNING){
1997
struct timespec sleeptime = { .tv_nsec = 250000000 };
1998
ret = nanosleep(&sleeptime, NULL);
1999
if(ret == -1 and errno != EINTR){
2000
perror_plus("nanosleep");
2003
if(not take_down_interface){
2004
/* We won't need the socket anymore */
2005
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2007
perror_plus("close");
2011
if(restore_loglevel){
2012
/* Restores kernel loglevel to default */
2013
ret = klogctl(7, NULL, 0);
2015
perror_plus("klogctl");
2018
#endif /* __linux__ */
2019
/* Lower privileges */
2021
/* Lower privileges */
2024
perror_plus("seteuid");
2740
bool interface_was_up = interface_is_up(interface);
2741
errno = bring_up_interface(interface, delay);
2742
if(not interface_was_up){
2744
fprintf_plus(stderr, "Failed to bring up interface \"%s\":"
2745
" %s\n", interface, strerror(errno));
2747
errno = argz_add(&interfaces_to_take_down,
2748
&interfaces_to_take_down_size,
2751
perror_plus("argz_add");
2756
if(debug and (interfaces_to_take_down == NULL)){
2757
fprintf_plus(stderr, "No interfaces were brought up\n");
2761
/* If we only got one interface, explicitly use only that one */
2762
if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
2764
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2767
if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
2032
ret = init_gnutls_global(pubkey, seckey);
2774
ret = init_gnutls_global(pubkey, seckey, dh_params_file, &mc);
2034
2776
fprintf_plus(stderr, "init_gnutls_global failed\n");
2035
2777
exitcode = EX_UNAVAILABLE;
2226
2980
mc.current_server->prev->next = NULL;
2227
2981
while(mc.current_server != NULL){
2228
2982
server *next = mc.current_server->next;
2984
#pragma GCC diagnostic push
2985
#pragma GCC diagnostic ignored "-Wcast-qual"
2987
free((char *)(mc.current_server->ip));
2989
#pragma GCC diagnostic pop
2229
2991
free(mc.current_server);
2230
2992
mc.current_server = next;
2234
/* Run network hooks */
2235
run_network_hooks("stop", interface, delay);
2237
/* Re-raise priviliges */
2996
/* Re-raise privileges */
2242
perror_plus("seteuid");
2998
ret_errno = raise_privileges();
3001
perror_plus("Failed to raise privileges");
3004
/* Run network hooks */
3005
run_network_hooks("stop", interfaces_hooks != NULL ?
3006
interfaces_hooks : "", delay);
3008
/* Take down the network interfaces which were brought up */
3010
char *interface = NULL;
3011
while((interface=argz_next(interfaces_to_take_down,
3012
interfaces_to_take_down_size,
3014
ret_errno = take_down_interface(interface);
3017
perror_plus("Failed to take down interface");
3020
if(debug and (interfaces_to_take_down == NULL)){
3021
fprintf_plus(stderr, "No interfaces needed to be taken"
2245
/* Take down the network interface */
2246
if(take_down_interface and geteuid() == 0){
2247
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2249
perror_plus("ioctl SIOCGIFFLAGS");
2250
} else if(network.ifr_flags & IFF_UP){
2251
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2252
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2254
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2257
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2259
perror_plus("close");
3027
ret_errno = lower_privileges_permanently();
3030
perror_plus("Failed to lower privileges permanently");
2263
/* Lower privileges permanently */
2267
perror_plus("setuid");
3034
free(interfaces_to_take_down);
3035
free(interfaces_hooks);
2270
3037
/* Removes the GPGME temp directory and all files inside */
2271
if(tempdir_created){
3038
if(tempdir != NULL){
2272
3039
struct dirent **direntries = NULL;
2273
struct dirent *direntry = NULL;
2274
int numentries = scandir(tempdir, &direntries, notdotentries,
2276
if (numentries > 0){
2277
for(int i = 0; i < numentries; i++){
2278
direntry = direntries[i];
2279
char *fullname = NULL;
2280
ret = asprintf(&fullname, "%s/%s", tempdir,
2283
perror_plus("asprintf");
2286
ret = remove(fullname);
2288
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
3040
int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY
3044
if(tempdir_fd == -1){
3045
perror_plus("open");
3047
int numentries = scandirat(tempdir_fd, ".", &direntries,
3048
notdotentries, alphasort);
3049
if(numentries >= 0){
3050
for(int i = 0; i < numentries; i++){
3051
ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
3053
fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
3054
" \"%s\", 0): %s\n", tempdir,
3055
direntries[i]->d_name, strerror(errno));
3057
free(direntries[i]);
3060
/* need to clean even if 0 because man page doesn't specify */
3062
if(numentries == -1){
3063
perror_plus("scandir");
3065
ret = rmdir(tempdir);
3066
if(ret == -1 and errno != ENOENT){
3067
perror_plus("rmdir");
2295
/* need to clean even if 0 because man page doesn't specify */
2297
if (numentries == -1){
2298
perror_plus("scandir");
2300
ret = rmdir(tempdir);
2301
if(ret == -1 and errno != ENOENT){
2302
perror_plus("rmdir");