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
186
188
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
187
189
program_invocation_short_name));
188
return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
190
return (int)TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
662
/* If the interface is specified and we have a list of interfaces */
663
if(if_index != AVAHI_IF_UNSPEC and mc->interfaces != NULL){
664
/* Check if the interface is one of the interfaces we are using */
667
char *interface = NULL;
668
while((interface=argz_next(mc->interfaces, mc->interfaces_size,
670
if(if_nametoindex(interface) == (unsigned int)if_index){
677
/* This interface does not match any in the list, so we don't
678
connect to the server */
680
char interface[IF_NAMESIZE];
681
if(if_indextoname((unsigned int)if_index, interface) == NULL){
682
perror_plus("if_indextoname");
684
fprintf_plus(stderr, "Skipping server on non-used interface"
686
if_indextoname((unsigned int)if_index,
660
694
ret = init_gnutls_session(&session, mc);
703
737
if(af == AF_INET6){
704
738
to.in6.sin6_port = htons(port);
740
#pragma GCC diagnostic push
741
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
705
743
if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
706
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
744
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower */
746
#pragma GCC diagnostic pop
708
748
if(if_index == AVAHI_IF_UNSPEC){
709
749
fprintf_plus(stderr, "An IPv6 link-local address is"
710
750
" incomplete without a network interface\n");
1755
1793
int main(int argc, char *argv[]){
1756
1794
mandos_context mc = { .server = NULL, .dh_bits = 1024,
1757
1795
.priority = "SECURE256:!CTYPE-X.509:"
1758
"+CTYPE-OPENPGP", .current_server = NULL };
1796
"+CTYPE-OPENPGP", .current_server = NULL,
1797
.interfaces = NULL, .interfaces_size = 0 };
1759
1798
AvahiSServiceBrowser *sb = NULL;
1760
1799
error_t ret_errno;
1762
1801
intmax_t tmpmax;
1764
1803
int exitcode = EXIT_SUCCESS;
1765
char *interfaces = NULL;
1766
size_t interfaces_size = 0;
1767
1804
char *interfaces_to_take_down = NULL;
1768
1805
size_t interfaces_to_take_down_size = 0;
1769
1806
char tempdir[] = "/tmp/mandosXXXXXX";
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;
2072
memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2073
interfaces_hooks_size = mc.interfaces_size;
2041
2074
argz_stringify(interfaces_hooks, interfaces_hooks_size,
2129
2162
/* If no interfaces were specified, make a list */
2130
if(interfaces == NULL){
2163
if(mc.interfaces == NULL){
2131
2164
struct dirent **direntries;
2132
2165
/* Look for any good interfaces */
2133
2166
ret = scandir(sys_class_net, &direntries, good_interface,
2136
2169
/* Add all found interfaces to interfaces list */
2137
2170
for(int i = 0; i < ret; ++i){
2138
ret_errno = argz_add(&interfaces, &interfaces_size,
2171
ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2139
2172
direntries[i]->d_name);
2140
2173
if(ret_errno != 0){
2141
2174
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)){
2191
/* Bring up interfaces which are down, and remove any "none"s */
2170
2193
char *interface = NULL;
2171
while((interface = argz_next(interfaces, interfaces_size,
2194
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2196
/* If interface name is "none", stop bringing up interfaces.
2197
Also remove all instances of "none" from the list */
2198
if(strcmp(interface, "none") == 0){
2199
argz_delete(&mc.interfaces, &mc.interfaces_size,
2202
while((interface = argz_next(mc.interfaces,
2203
mc.interfaces_size, interface))){
2204
if(strcmp(interface, "none") == 0){
2205
argz_delete(&mc.interfaces, &mc.interfaces_size,
2173
2212
bool interface_was_up = interface_is_up(interface);
2174
2213
ret = bring_up_interface(interface, delay);
2175
2214
if(not interface_was_up){
2188
interfaces_size = 0;
2189
2225
if(debug and (interfaces_to_take_down == NULL)){
2190
2226
fprintf_plus(stderr, "No interfaces were brought up\n");
2230
/* If we only got one interface, explicitly use only that one */
2231
if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
2233
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2236
if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);