125
125
static const char mandos_protocol_version[] = "1";
126
126
const char *argp_program_version = "mandos-client " VERSION;
127
127
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
128
static const char sys_class_net[] = "/sys/class/net";
129
char *connect_to = NULL;
129
131
/* Used for passing in values through the Avahi callback functions */
974
976
errno = old_errno;
980
* This function determines if a directory entry in /sys/class/net
981
* corresponds to an acceptable network device.
982
* (This function is passed to scandir(3) as a filter function.)
984
int good_interface(const struct dirent *if_entry){
986
char *flagname = NULL;
987
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
993
if(if_entry->d_name[0] == '.'){
996
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1001
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1002
/* read line from flags_fd */
1003
ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1004
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1005
flagstring[(size_t)to_read] = '\0';
1006
if(flagstring == NULL){
1012
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1029
tmpmax = strtoimax(flagstring, &tmp, 0);
1030
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1031
and not (isspace(*tmp)))
1032
or tmpmax != (ifreq_flags)tmpmax){
1034
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1035
flagstring, if_entry->d_name);
1041
ifreq_flags flags = (ifreq_flags)tmpmax;
1042
/* Reject the loopback device */
1043
if(flags & IFF_LOOPBACK){
1045
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1050
/* Accept point-to-point devices only if connect_to is specified */
1051
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1053
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1058
/* Otherwise, reject non-broadcast-capable devices */
1059
if(not (flags & IFF_BROADCAST)){
1061
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1066
/* Accept this device */
1068
fprintf(stderr, "Interface \"%s\" is acceptable\n",
977
1074
int main(int argc, char *argv[]){
978
1075
AvahiSServiceBrowser *sb = NULL;
981
1078
intmax_t tmpmax;
983
1080
int exitcode = EXIT_SUCCESS;
984
const char *interface = "eth0";
1081
const char *interface = "";
985
1082
struct ifreq network;
987
1084
bool take_down_interface = false;
990
char *connect_to = NULL;
991
1087
char tempdir[] = "/tmp/mandosXXXXXX";
992
1088
bool tempdir_created = false;
993
1089
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1124
1220
avahi_set_log_function(empty_log);
1223
if(interface[0] == '\0'){
1224
struct dirent **direntries;
1225
ret = scandir(sys_class_net, &direntries, good_interface,
1228
/* Pick the first good interface */
1229
interface = strdup(direntries[0]->d_name);
1231
fprintf(stderr, "Using interface \"%s\"\n", interface);
1233
if(interface == NULL){
1236
exitcode = EXIT_FAILURE;
1242
fprintf(stderr, "Could not find a network interface\n");
1243
exitcode = EXIT_FAILURE;
1127
1248
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1128
1249
from the signal handler */
1201
1322
/* If the interface is down, bring it up */
1202
if(interface[0] != '\0'){
1323
if(strcmp(interface, "none") != 0){
1203
1324
if_index = (AvahiIfIndex) if_nametoindex(interface);
1204
1325
if(if_index == 0){
1205
1326
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1533
1654
network.ifr_flags &= ~IFF_UP; /* clear flag */
1534
1655
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1536
perror("ioctl SIOCSIFFLAGS");
1657
perror("ioctl SIOCSIFFLAGS -IFF_UP");
1539
1660
ret = (int)TEMP_FAILURE_RETRY(close(sd));