529
571
/* GnuTLS server initialization */
530
ret = gnutls_dh_params_init(&mc.dh_params);
572
ret = gnutls_dh_params_init(&mc->dh_params);
531
573
if(ret != GNUTLS_E_SUCCESS){
532
574
fprintf_plus(stderr, "Error in GnuTLS DH parameter"
533
575
" initialization: %s\n",
534
576
safer_gnutls_strerror(ret));
537
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
538
if(ret != GNUTLS_E_SUCCESS){
539
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
540
safer_gnutls_strerror(ret));
544
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);
550
gnutls_certificate_free_credentials(mc.cred);
757
gnutls_certificate_free_credentials(mc->cred);
551
758
gnutls_global_deinit();
552
gnutls_dh_params_deinit(mc.dh_params);
759
gnutls_dh_params_deinit(mc->dh_params);
556
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,
558
767
/* GnuTLS session creation */
613
820
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
614
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);
616
1041
/* Called when a Mandos server is found */
617
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,
618
1044
AvahiIfIndex if_index,
1045
int af, mandos_context *mc){
620
1046
int ret, tcp_sd = -1;
623
struct sockaddr_in in;
624
struct sockaddr_in6 in6;
1048
struct sockaddr_storage to;
626
1049
char *buffer = NULL;
627
1050
char *decrypted_buffer = NULL;
628
1051
size_t buffer_length = 0;
729
1180
if(if_indextoname((unsigned int)if_index, interface) == NULL){
730
1181
perror_plus("if_indextoname");
732
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
733
"\n", ip, interface, port);
1183
fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
1184
"\n", ip, interface, (uintmax_t)port);
736
fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
1187
fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
1188
ip, (uintmax_t)port);
739
1190
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
740
1191
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
743
pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
746
pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
750
perror_plus("inet_ntop");
752
if(strcmp(addrstr, ip) != 0){
753
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
764
ret = connect(tcp_sd, &to.in6, sizeof(to));
766
ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
769
if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
771
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");
782
1274
const char *out = mandos_protocol_version;
1374
1888
ret = avahi_simple_poll_iterate(s, (int)block_time);
1377
if (ret > 0 or errno != EINTR){
1891
if(ret > 0 or errno != EINTR){
1378
1892
return (ret != 1) ? ret : 0;
1384
bool run_network_hooks(const char *mode, const char *interface,
1898
__attribute__((nonnull))
1899
void run_network_hooks(const char *mode, const char *interface,
1385
1900
const float delay){
1386
struct dirent **direntries;
1387
struct dirent *direntry;
1389
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);
1391
1919
if(numhooks == -1){
1392
1920
perror_plus("scandir");
1394
int devnull = open("/dev/null", O_RDONLY);
1395
for(int i = 0; i < numhooks; i++){
1396
direntry = direntries[i];
1397
char *fullname = NULL;
1398
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);
1400
1982
perror_plus("asprintf");
1404
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1407
pid_t hook_pid = fork();
1410
/* Raise privileges */
1414
perror_plus("seteuid");
1416
/* Raise privileges even more */
1420
perror_plus("setuid");
1426
perror_plus("setgid");
1428
/* Reset supplementary groups */
1430
ret = setgroups(0, NULL);
1432
perror_plus("setgroups");
1434
dup2(devnull, STDIN_FILENO);
1436
dup2(STDERR_FILENO, STDOUT_FILENO);
1437
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1439
perror_plus("setenv");
1442
ret = setenv("DEVICE", interface, 1);
1444
perror_plus("setenv");
1447
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1449
perror_plus("setenv");
1452
ret = setenv("MODE", mode, 1);
1454
perror_plus("setenv");
1458
ret = asprintf(&delaystring, "%f", delay);
1460
perror_plus("asprintf");
1463
ret = setenv("DELAY", delaystring, 1);
1466
perror_plus("setenv");
1985
ret = setenv("DELAY", delaystring, 1);
1469
1987
free(delaystring);
1470
ret = execl(fullname, direntry->d_name, mode, NULL);
1471
perror_plus("execl");
1474
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1475
perror_plus("waitpid");
1479
if(WIFEXITED(status)){
1480
if(WEXITSTATUS(status) != 0){
1481
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1482
" with status %d\n", direntry->d_name,
1483
WEXITSTATUS(status));
1487
} else if(WIFSIGNALED(status)){
1488
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1489
" signal %d\n", direntry->d_name,
1494
fprintf_plus(stderr, "Warning: network hook \"%s\""
1495
" crashed\n", direntry->d_name);
1502
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",
1511
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 };
1512
2286
AvahiSServiceBrowser *sb = NULL;
1515
2289
intmax_t tmpmax;
1517
2291
int exitcode = EXIT_SUCCESS;
1518
const char *interface = "";
1519
struct ifreq network;
1521
bool take_down_interface = false;
1524
char tempdir[] = "/tmp/mandosXXXXXX";
1525
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;
1526
2297
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1527
2298
const char *seckey = PATHDIR "/" SECKEY;
1528
2299
const char *pubkey = PATHDIR "/" PUBKEY;
2300
const char *dh_params_file = NULL;
2301
char *interfaces_hooks = NULL;
1530
2303
bool gnutls_initialized = false;
1531
2304
bool gpgme_initialized = false;
1756
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");
1760
2565
/* Lower privileges */
1764
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);
1769
2591
/* Run network hooks */
1770
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);
1775
2607
avahi_set_log_function(empty_log);
1778
if(interface[0] == '\0'){
1779
struct dirent **direntries;
1780
/* First look for interfaces that are up */
1781
ret = scandir(sys_class_net, &direntries, up_interface,
1784
/* No up interfaces, look for any good interfaces */
1786
ret = scandir(sys_class_net, &direntries, good_interface,
1790
/* Pick the first interface returned */
1791
interface = strdup(direntries[0]->d_name);
1793
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1795
if(interface == NULL){
1796
perror_plus("malloc");
1798
exitcode = EXIT_FAILURE;
1804
fprintf_plus(stderr, "Could not find a network interface\n");
1805
exitcode = EXIT_FAILURE;
1810
2610
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1811
2611
from the signal handler */
1812
2612
/* Initialize the pseudo-RNG for Avahi */
1813
2613
srand((unsigned int) time(NULL));
1814
mc.simple_poll = avahi_simple_poll_new();
1815
if(mc.simple_poll == NULL){
2614
simple_poll = avahi_simple_poll_new();
2615
if(simple_poll == NULL){
1816
2616
fprintf_plus(stderr,
1817
2617
"Avahi: Failed to create simple poll object.\n");
1818
2618
exitcode = EX_UNAVAILABLE;
1885
/* If the interface is down, bring it up */
1886
if(strcmp(interface, "none") != 0){
1887
if_index = (AvahiIfIndex) if_nametoindex(interface);
1889
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1890
exitcode = EX_UNAVAILABLE;
1898
/* Re-raise priviliges */
1902
perror_plus("seteuid");
1906
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1907
messages about the network interface to mess up the prompt */
1908
ret = klogctl(8, NULL, 5);
1909
bool restore_loglevel = true;
1911
restore_loglevel = false;
1912
perror_plus("klogctl");
1914
#endif /* __linux__ */
1916
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1918
perror_plus("socket");
1919
exitcode = EX_OSERR;
1921
if(restore_loglevel){
1922
ret = klogctl(7, NULL, 0);
1924
perror_plus("klogctl");
1927
#endif /* __linux__ */
1928
/* Lower privileges */
1932
perror_plus("seteuid");
1936
strcpy(network.ifr_name, interface);
1937
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1939
perror_plus("ioctl SIOCGIFFLAGS");
1941
if(restore_loglevel){
1942
ret = klogctl(7, NULL, 0);
1944
perror_plus("klogctl");
1947
#endif /* __linux__ */
1948
exitcode = EX_OSERR;
1949
/* Lower privileges */
1953
perror_plus("seteuid");
1957
if((network.ifr_flags & IFF_UP) == 0){
1958
network.ifr_flags |= IFF_UP;
1959
take_down_interface = true;
1960
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1962
take_down_interface = false;
1963
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1964
exitcode = EX_OSERR;
1966
if(restore_loglevel){
1967
ret = klogctl(7, NULL, 0);
1969
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,
1972
#endif /* __linux__ */
1973
/* Lower privileges */
1977
perror_plus("seteuid");
1982
/* Sleep checking until interface is running.
1983
Check every 0.25s, up to total time of delay */
1984
for(int i=0; i < delay * 4; i++){
1985
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1987
perror_plus("ioctl SIOCGIFFLAGS");
1988
} else if(network.ifr_flags & IFF_RUNNING){
1991
struct timespec sleeptime = { .tv_nsec = 250000000 };
1992
ret = nanosleep(&sleeptime, NULL);
1993
if(ret == -1 and errno != EINTR){
1994
perror_plus("nanosleep");
1997
if(not take_down_interface){
1998
/* We won't need the socket anymore */
1999
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2001
perror_plus("close");
2005
if(restore_loglevel){
2006
/* Restores kernel loglevel to default */
2007
ret = klogctl(7, NULL, 0);
2009
perror_plus("klogctl");
2012
#endif /* __linux__ */
2013
/* Lower privileges */
2015
/* Lower privileges */
2018
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);
2026
ret = init_gnutls_global(pubkey, seckey);
2774
ret = init_gnutls_global(pubkey, seckey, dh_params_file, &mc);
2028
2776
fprintf_plus(stderr, "init_gnutls_global failed\n");
2029
2777
exitcode = EX_UNAVAILABLE;
2220
2980
mc.current_server->prev->next = NULL;
2221
2981
while(mc.current_server != NULL){
2222
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
2223
2991
free(mc.current_server);
2224
2992
mc.current_server = next;
2228
/* Run network hooks */
2229
run_network_hooks("stop", interface, delay);
2231
/* Re-raise priviliges */
2996
/* Re-raise privileges */
2236
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"
2239
/* Take down the network interface */
2240
if(take_down_interface and geteuid() == 0){
2241
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2243
perror_plus("ioctl SIOCGIFFLAGS");
2244
} else if(network.ifr_flags & IFF_UP){
2245
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2246
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2248
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2251
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2253
perror_plus("close");
3027
ret_errno = lower_privileges_permanently();
3030
perror_plus("Failed to lower privileges permanently");
2257
/* Lower privileges permanently */
2261
perror_plus("setuid");
3034
free(interfaces_to_take_down);
3035
free(interfaces_hooks);
2264
3037
/* Removes the GPGME temp directory and all files inside */
2265
if(tempdir_created){
3038
if(tempdir != NULL){
2266
3039
struct dirent **direntries = NULL;
2267
struct dirent *direntry = NULL;
2268
int numentries = scandir(tempdir, &direntries, notdotentries,
2270
if (numentries > 0){
2271
for(int i = 0; i < numentries; i++){
2272
direntry = direntries[i];
2273
char *fullname = NULL;
2274
ret = asprintf(&fullname, "%s/%s", tempdir,
2277
perror_plus("asprintf");
2280
ret = remove(fullname);
2282
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");
2289
/* need to clean even if 0 because man page doesn't specify */
2291
if (numentries == -1){
2292
perror_plus("scandir");
2294
ret = rmdir(tempdir);
2295
if(ret == -1 and errno != ENOENT){
2296
perror_plus("rmdir");