9
9
* "browse_callback", and parts of "main".
11
11
* Everything else is
12
* Copyright © 2008-2013 Teddy Hogeborn
13
* Copyright © 2008-2013 Björn Påhlsson
12
* Copyright © 2008-2012 Teddy Hogeborn
13
* Copyright © 2008-2012 Björn Påhlsson
15
15
* This program is free software: you can redistribute it and/or
16
16
* modify it under the terms of the GNU General Public License as
56
56
#include <sys/stat.h> /* open(), S_ISREG */
57
57
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
58
inet_pton(), connect(),
58
inet_pton(), connect() */
60
59
#include <fcntl.h> /* open() */
61
60
#include <dirent.h> /* opendir(), struct dirent, readdir()
74
73
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
75
74
getuid(), getgid(), seteuid(),
76
75
setgid(), pause(), _exit() */
77
#include <arpa/inet.h> /* inet_pton(), htons() */
76
#include <arpa/inet.h> /* inet_pton(), htons, inet_ntop() */
78
77
#include <iso646.h> /* not, or, and */
79
78
#include <argp.h> /* struct argp_option, error_t, struct
80
79
argp_state, struct argp,
92
91
argz_delete(), argz_append(),
93
92
argz_stringify(), argz_add(),
95
#include <netdb.h> /* getnameinfo(), NI_NUMERICHOST,
96
EAI_SYSTEM, gai_strerror() */
99
96
#include <sys/klog.h> /* klogctl() */
191
186
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
192
187
program_invocation_short_name));
193
return (int)TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
188
return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
201
196
size_t incbuffer(char **buffer, size_t buffer_length,
202
197
size_t buffer_capacity){
203
198
if(buffer_length + BUFFER_SIZE > buffer_capacity){
204
char *new_buf = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
206
int old_errno = errno;
199
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
213
203
buffer_capacity += BUFFER_SIZE;
215
205
return buffer_capacity;
232
222
perror_plus("strdup");
235
ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
237
perror_plus("clock_gettime");
240
225
/* Special case of first server */
241
226
if(*current_server == NULL){
242
227
new_server->next = new_server;
249
234
new_server->prev->next = new_server;
250
235
(*current_server)->prev = new_server;
237
ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
239
perror_plus("clock_gettime");
634
624
int af, mandos_context *mc){
635
625
int ret, tcp_sd = -1;
637
struct sockaddr_storage to;
628
struct sockaddr_in in;
629
struct sockaddr_in6 in6;
638
631
char *buffer = NULL;
639
632
char *decrypted_buffer = NULL;
640
633
size_t buffer_length = 0;
667
/* If the interface is specified and we have a list of interfaces */
668
if(if_index != AVAHI_IF_UNSPEC and mc->interfaces != NULL){
669
/* Check if the interface is one of the interfaces we are using */
672
char *interface = NULL;
673
while((interface=argz_next(mc->interfaces, mc->interfaces_size,
675
if(if_nametoindex(interface) == (unsigned int)if_index){
682
/* This interface does not match any in the list, so we don't
683
connect to the server */
685
char interface[IF_NAMESIZE];
686
if(if_indextoname((unsigned int)if_index, interface) == NULL){
687
perror_plus("if_indextoname");
689
fprintf_plus(stderr, "Skipping server on non-used interface"
691
if_indextoname((unsigned int)if_index,
699
660
ret = init_gnutls_session(&session, mc);
722
683
memset(&to, 0, sizeof(to));
723
684
if(af == AF_INET6){
724
((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
725
ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
685
to.in6.sin6_family = (sa_family_t)af;
686
ret = inet_pton(af, ip, &to.in6.sin6_addr);
726
687
} else { /* IPv4 */
727
((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
728
ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
688
to.in.sin_family = (sa_family_t)af;
689
ret = inet_pton(af, ip, &to.in.sin_addr);
742
703
if(af == AF_INET6){
743
((struct sockaddr_in6 *)&to)->sin6_port = htons(port);
744
if(IN6_IS_ADDR_LINKLOCAL
745
(&((struct sockaddr_in6 *)&to)->sin6_addr)){
704
to.in6.sin6_port = htons(port);
705
if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
706
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
746
708
if(if_index == AVAHI_IF_UNSPEC){
747
709
fprintf_plus(stderr, "An IPv6 link-local address is"
748
710
" incomplete without a network interface\n");
752
714
/* Set the network interface number as scope */
753
((struct sockaddr_in6 *)&to)->sin6_scope_id = (uint32_t)if_index;
715
to.in6.sin6_scope_id = (uint32_t)if_index;
756
((struct sockaddr_in *)&to)->sin_port = htons(port);
718
to.in.sin_port = htons(port); /* Spurious warnings from
720
-Wunreachable-code */
777
741
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
778
742
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
779
744
if(af == AF_INET6){
780
ret = getnameinfo((struct sockaddr *)&to,
781
sizeof(struct sockaddr_in6),
782
addrstr, sizeof(addrstr), NULL, 0,
745
pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
785
ret = getnameinfo((struct sockaddr *)&to,
786
sizeof(struct sockaddr_in),
787
addrstr, sizeof(addrstr), NULL, 0,
748
pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
790
if(ret == EAI_SYSTEM){
791
perror_plus("getnameinfo");
792
} else if(ret != 0) {
793
fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
794
} else if(strcmp(addrstr, ip) != 0){
795
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
752
perror_plus("inet_ntop");
754
if(strcmp(addrstr, ip) != 0){
755
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
804
765
if(af == AF_INET6){
805
ret = connect(tcp_sd, (struct sockaddr *)&to,
806
sizeof(struct sockaddr_in6));
766
ret = connect(tcp_sd, &to.in6, sizeof(to));
808
ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
809
sizeof(struct sockaddr_in));
768
ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
812
771
if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
1491
1450
bool run_network_hooks(const char *mode, const char *interface,
1492
1451
const float delay){
1493
1452
struct dirent **direntries;
1453
struct dirent *direntry;
1494
1455
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1496
1457
if(numhooks == -1){
1749
1710
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1751
int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1712
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1753
1714
ret_errno = errno;
1754
1715
perror_plus("socket");
1764
1725
/* Raise priviliges */
1765
1726
raise_privileges();
1767
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1728
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1768
1729
ret_errno = errno;
1770
1731
/* Lower privileges */
1771
1732
lower_privileges();
1773
1734
/* Close the socket */
1774
int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1735
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1776
1737
perror_plus("close");
1794
1755
int main(int argc, char *argv[]){
1795
1756
mandos_context mc = { .server = NULL, .dh_bits = 1024,
1796
1757
.priority = "SECURE256:!CTYPE-X.509:"
1797
"+CTYPE-OPENPGP", .current_server = NULL,
1798
.interfaces = NULL, .interfaces_size = 0 };
1758
"+CTYPE-OPENPGP", .current_server = NULL };
1799
1759
AvahiSServiceBrowser *sb = NULL;
1800
1760
error_t ret_errno;
1802
1762
intmax_t tmpmax;
1804
1764
int exitcode = EXIT_SUCCESS;
1765
char *interfaces = NULL;
1766
size_t interfaces_size = 0;
1805
1767
char *interfaces_to_take_down = NULL;
1806
1768
size_t interfaces_to_take_down_size = 0;
1807
1769
char tempdir[] = "/tmp/mandosXXXXXX";
1810
1772
const char *seckey = PATHDIR "/" SECKEY;
1811
1773
const char *pubkey = PATHDIR "/" PUBKEY;
1812
1774
char *interfaces_hooks = NULL;
1775
size_t interfaces_hooks_size = 0;
1814
1777
bool gnutls_initialized = false;
1815
1778
bool gpgme_initialized = false;
1906
1869
connect_to = arg;
1908
1871
case 'i': /* --interface */
1909
ret_errno = argz_add_sep(&mc.interfaces, &mc.interfaces_size,
1872
ret_errno = argz_add_sep(&interfaces, &interfaces_size, arg,
1911
1874
if(ret_errno != 0){
1912
1875
argp_error(state, "%s", strerror(ret_errno));
2042
2005
/* Lower privileges */
2009
perror_plus("seteuid");
2047
/* Remove invalid interface names (except "none") */
2014
/* Remove empty interface names */
2049
2016
char *interface = NULL;
2050
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2017
while((interface = argz_next(interfaces, interfaces_size,
2052
if(strcmp(interface, "none") != 0
2053
and if_nametoindex(interface) == 0){
2054
if(interface[0] != '\0'){
2019
if(if_nametoindex(interface) == 0){
2020
if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2055
2021
fprintf_plus(stderr, "Not using nonexisting interface"
2056
2022
" \"%s\"\n", interface);
2058
argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
2024
argz_delete(&interfaces, &interfaces_size, interface);
2059
2025
interface = NULL;
2064
2030
/* Run network hooks */
2066
if(mc.interfaces != NULL){
2067
interfaces_hooks = malloc(mc.interfaces_size);
2033
if(interfaces != NULL){
2034
interfaces_hooks = malloc(interfaces_size);
2068
2035
if(interfaces_hooks == NULL){
2069
2036
perror_plus("malloc");
2072
memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2073
argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2039
memcpy(interfaces_hooks, interfaces, interfaces_size);
2040
interfaces_hooks_size = interfaces_size;
2041
argz_stringify(interfaces_hooks, interfaces_hooks_size,
2075
2044
if(not run_network_hooks("start", interfaces_hooks != NULL ?
2076
2045
interfaces_hooks : "", delay)){
2160
2129
/* If no interfaces were specified, make a list */
2161
if(mc.interfaces == NULL){
2130
if(interfaces == NULL){
2162
2131
struct dirent **direntries;
2163
2132
/* Look for any good interfaces */
2164
2133
ret = scandir(sys_class_net, &direntries, good_interface,
2167
2136
/* Add all found interfaces to interfaces list */
2168
2137
for(int i = 0; i < ret; ++i){
2169
ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2138
ret_errno = argz_add(&interfaces, &interfaces_size,
2170
2139
direntries[i]->d_name);
2171
2140
if(ret_errno != 0){
2173
2141
perror_plus("argz_add");
2190
/* Bring up interfaces which are down, and remove any "none"s */
2158
/* If we only got one interface, explicitly use only that one */
2159
if(argz_count(interfaces, interfaces_size) == 1){
2161
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2164
if_index = (AvahiIfIndex)if_nametoindex(interfaces);
2167
/* Bring up interfaces which are down */
2168
if(not (argz_count(interfaces, interfaces_size) == 1
2169
and strcmp(interfaces, "none") == 0)){
2192
2170
char *interface = NULL;
2193
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2171
while((interface = argz_next(interfaces, interfaces_size,
2195
/* If interface name is "none", stop bringing up interfaces.
2196
Also remove all instances of "none" from the list */
2197
if(strcmp(interface, "none") == 0){
2198
argz_delete(&mc.interfaces, &mc.interfaces_size,
2201
while((interface = argz_next(mc.interfaces,
2202
mc.interfaces_size, interface))){
2203
if(strcmp(interface, "none") == 0){
2204
argz_delete(&mc.interfaces, &mc.interfaces_size,
2211
2173
bool interface_was_up = interface_is_up(interface);
2212
2174
ret = bring_up_interface(interface, delay);
2213
2175
if(not interface_was_up){
2218
2180
ret_errno = argz_add(&interfaces_to_take_down,
2219
2181
&interfaces_to_take_down_size,
2223
perror_plus("argz_add");
2188
interfaces_size = 0;
2228
2189
if(debug and (interfaces_to_take_down == NULL)){
2229
2190
fprintf_plus(stderr, "No interfaces were brought up\n");
2233
/* If we only got one interface, explicitly use only that one */
2234
if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
2236
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2239
if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);