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
188
186
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ",
189
187
program_invocation_short_name));
190
return (int)TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
188
return 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,
694
660
ret = init_gnutls_session(&session, mc);
737
703
if(af == AF_INET6){
738
704
to.in6.sin6_port = htons(port);
740
#pragma GCC diagnostic push
741
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
743
705
if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
744
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower */
746
#pragma GCC diagnostic pop
706
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
748
708
if(if_index == AVAHI_IF_UNSPEC){
749
709
fprintf_plus(stderr, "An IPv6 link-local address is"
750
710
" incomplete without a network interface\n");
1793
1755
int main(int argc, char *argv[]){
1794
1756
mandos_context mc = { .server = NULL, .dh_bits = 1024,
1795
1757
.priority = "SECURE256:!CTYPE-X.509:"
1796
"+CTYPE-OPENPGP", .current_server = NULL,
1797
.interfaces = NULL, .interfaces_size = 0 };
1758
"+CTYPE-OPENPGP", .current_server = NULL };
1798
1759
AvahiSServiceBrowser *sb = NULL;
1799
1760
error_t ret_errno;
1801
1762
intmax_t tmpmax;
1803
1764
int exitcode = EXIT_SUCCESS;
1765
char *interfaces = NULL;
1766
size_t interfaces_size = 0;
1804
1767
char *interfaces_to_take_down = NULL;
1805
1768
size_t interfaces_to_take_down_size = 0;
1806
1769
char tempdir[] = "/tmp/mandosXXXXXX";
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
interfaces_hooks_size = mc.interfaces_size;
2039
memcpy(interfaces_hooks, interfaces, interfaces_size);
2040
interfaces_hooks_size = interfaces_size;
2074
2041
argz_stringify(interfaces_hooks, interfaces_hooks_size,
2162
2129
/* If no interfaces were specified, make a list */
2163
if(mc.interfaces == NULL){
2130
if(interfaces == NULL){
2164
2131
struct dirent **direntries;
2165
2132
/* Look for any good interfaces */
2166
2133
ret = scandir(sys_class_net, &direntries, good_interface,
2169
2136
/* Add all found interfaces to interfaces list */
2170
2137
for(int i = 0; i < ret; ++i){
2171
ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2138
ret_errno = argz_add(&interfaces, &interfaces_size,
2172
2139
direntries[i]->d_name);
2173
2140
if(ret_errno != 0){
2174
2141
perror_plus("argz_add");
2191
/* 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)){
2193
2170
char *interface = NULL;
2194
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2171
while((interface = argz_next(interfaces, 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,
2212
2173
bool interface_was_up = interface_is_up(interface);
2213
2174
ret = bring_up_interface(interface, delay);
2214
2175
if(not interface_was_up){
2188
interfaces_size = 0;
2225
2189
if(debug and (interfaces_to_take_down == NULL)){
2226
2190
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);