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