9
9
* "browse_callback", and parts of "main".
11
11
* Everything else is
12
* Copyright © 2008,2009 Teddy Hogeborn
13
* Copyright © 2008,2009 Björn Påhlsson
12
* Copyright © 2008-2011 Teddy Hogeborn
13
* Copyright © 2008-2011 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
64
64
#include <assert.h> /* assert() */
65
65
#include <errno.h> /* perror(), errno */
66
#include <time.h> /* nanosleep(), time() */
66
#include <time.h> /* nanosleep(), time(), sleep() */
67
67
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
68
SIOCSIFFLAGS, if_indextoname(),
69
69
if_nametoindex(), IF_NAMESIZE */
73
73
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
74
74
getuid(), getgid(), seteuid(),
75
75
setgid(), pause() */
76
#include <arpa/inet.h> /* inet_pton(), htons */
76
#include <arpa/inet.h> /* inet_pton(), htons, inet_ntop() */
77
77
#include <iso646.h> /* not, or, and */
78
78
#include <argp.h> /* struct argp_option, error_t, struct
79
79
argp_state, struct argp,
423
425
/* OpenPGP credentials */
424
gnutls_certificate_allocate_credentials(&mc.cred);
426
ret = gnutls_certificate_allocate_credentials(&mc.cred);
425
427
if(ret != GNUTLS_E_SUCCESS){
426
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
428
fprintf(stderr, "GnuTLS memory error: %s\n",
430
429
safer_gnutls_strerror(ret));
431
430
gnutls_global_deinit();
1020
1021
errno = old_errno;
1025
* This function determines if a directory entry in /sys/class/net
1026
* corresponds to an acceptable network device.
1027
* (This function is passed to scandir(3) as a filter function.)
1029
int good_interface(const struct dirent *if_entry){
1031
char *flagname = NULL;
1032
if(if_entry->d_name[0] == '.'){
1035
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1041
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1048
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1049
/* read line from flags_fd */
1050
ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1051
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1052
flagstring[(size_t)to_read] = '\0';
1053
if(flagstring == NULL){
1059
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1076
tmpmax = strtoimax(flagstring, &tmp, 0);
1077
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1078
and not (isspace(*tmp)))
1079
or tmpmax != (ifreq_flags)tmpmax){
1081
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1082
flagstring, if_entry->d_name);
1088
ifreq_flags flags = (ifreq_flags)tmpmax;
1089
/* Reject the loopback device */
1090
if(flags & IFF_LOOPBACK){
1092
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1097
/* Accept point-to-point devices only if connect_to is specified */
1098
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1100
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1105
/* Otherwise, reject non-broadcast-capable devices */
1106
if(not (flags & IFF_BROADCAST)){
1108
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1113
/* Reject non-ARP interfaces (including dummy interfaces) */
1114
if(flags & IFF_NOARP){
1116
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1121
/* Accept this device */
1123
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1023
1129
int main(int argc, char *argv[]){
1024
1130
AvahiSServiceBrowser *sb = NULL;
1027
1133
intmax_t tmpmax;
1029
1135
int exitcode = EXIT_SUCCESS;
1030
const char *interface = "eth0";
1136
const char *interface = "";
1031
1137
struct ifreq network;
1033
1139
bool take_down_interface = false;
1036
char *connect_to = NULL;
1037
1142
char tempdir[] = "/tmp/mandosXXXXXX";
1038
1143
bool tempdir_created = false;
1039
1144
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1197
1302
avahi_set_log_function(empty_log);
1305
if(interface[0] == '\0'){
1306
struct dirent **direntries;
1307
ret = scandir(sys_class_net, &direntries, good_interface,
1310
/* Pick the first good interface */
1311
interface = strdup(direntries[0]->d_name);
1313
fprintf(stderr, "Using interface \"%s\"\n", interface);
1315
if(interface == NULL){
1318
exitcode = EXIT_FAILURE;
1324
fprintf(stderr, "Could not find a network interface\n");
1325
exitcode = EXIT_FAILURE;
1200
1330
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1201
1331
from the signal handler */
1274
1404
/* If the interface is down, bring it up */
1275
if(interface[0] != '\0'){
1405
if(strcmp(interface, "none") != 0){
1276
1406
if_index = (AvahiIfIndex) if_nametoindex(interface);
1277
1407
if(if_index == 0){
1278
1408
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1501
ret = start_mandos_communication(address, port, if_index, af);
1507
exitcode = EX_NOHOST;
1510
exitcode = EX_USAGE;
1513
exitcode = EX_IOERR;
1516
exitcode = EX_PROTOCOL;
1519
exitcode = EX_OSERR;
1631
while(not quit_now){
1632
ret = start_mandos_communication(address, port, if_index, af);
1633
if(quit_now or ret == 0){
1523
1640
exitcode = EXIT_SUCCESS;
1624
1742
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1625
1743
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1627
perror("ioctl SIOCSIFFLAGS");
1745
perror("ioctl SIOCSIFFLAGS -IFF_UP");
1630
1748
ret = (int)TEMP_FAILURE_RETRY(close(sd));