/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: teddy at bsnet
  • Date: 2010-09-08 19:25:13 UTC
  • Revision ID: teddy@fukt.bsnet.se-20100908192513-ccso0jpam3qg4yze
* TODO: Clarifications.

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
                                   strtoimax() */
64
64
#include <assert.h>             /* assert() */
65
65
#include <errno.h>              /* perror(), errno */
66
 
#include <time.h>               /* nanosleep(), time(), sleep() */
 
66
#include <time.h>               /* nanosleep(), time() */
67
67
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
68
                                   SIOCSIFFLAGS, if_indextoname(),
69
69
                                   if_nametoindex(), IF_NAMESIZE */
127
127
static const char mandos_protocol_version[] = "1";
128
128
const char *argp_program_version = "mandos-client " VERSION;
129
129
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
130
 
static const char sys_class_net[] = "/sys/class/net";
131
 
char *connect_to = NULL;
132
130
 
133
131
/* Used for passing in values through the Avahi callback functions */
134
132
typedef struct {
690
688
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
691
689
  }
692
690
  if(ret < 0){
693
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
694
 
      int e = errno;
695
 
      perror("connect");
696
 
      errno = e;
697
 
    }
 
691
    int e = errno;
 
692
    perror("connect");
 
693
    errno = e;
698
694
    goto mandos_end;
699
695
  }
700
696
  
1024
1020
  errno = old_errno;
1025
1021
}
1026
1022
 
1027
 
/* 
1028
 
 * This function determines if a directory entry in /sys/class/net
1029
 
 * corresponds to an acceptable network device.
1030
 
 * (This function is passed to scandir(3) as a filter function.)
1031
 
 */
1032
 
int good_interface(const struct dirent *if_entry){
1033
 
  ssize_t ssret;
1034
 
  char *flagname = NULL;
1035
 
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1036
 
                     if_entry->d_name);
1037
 
  if(ret < 0){
1038
 
    perror("asprintf");
1039
 
    return 0;
1040
 
  }
1041
 
  if(if_entry->d_name[0] == '.'){
1042
 
    return 0;
1043
 
  }
1044
 
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1045
 
  if(flags_fd == -1){
1046
 
    perror("open");
1047
 
    return 0;
1048
 
  }
1049
 
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1050
 
  /* read line from flags_fd */
1051
 
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1052
 
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1053
 
  flagstring[(size_t)to_read] = '\0';
1054
 
  if(flagstring == NULL){
1055
 
    perror("malloc");
1056
 
    close(flags_fd);
1057
 
    return 0;
1058
 
  }
1059
 
  while(to_read > 0){
1060
 
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1061
 
                                             (size_t)to_read));
1062
 
    if(ssret == -1){
1063
 
      perror("read");
1064
 
      free(flagstring);
1065
 
      close(flags_fd);
1066
 
      return 0;
1067
 
    }
1068
 
    to_read -= ssret;
1069
 
    if(ssret == 0){
1070
 
      break;
1071
 
    }
1072
 
  }
1073
 
  close(flags_fd);
1074
 
  intmax_t tmpmax;
1075
 
  char *tmp;
1076
 
  errno = 0;
1077
 
  tmpmax = strtoimax(flagstring, &tmp, 0);
1078
 
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1079
 
                                         and not (isspace(*tmp)))
1080
 
     or tmpmax != (ifreq_flags)tmpmax){
1081
 
    if(debug){
1082
 
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1083
 
              flagstring, if_entry->d_name);
1084
 
    }
1085
 
    free(flagstring);
1086
 
    return 0;
1087
 
  }
1088
 
  free(flagstring);
1089
 
  ifreq_flags flags = (ifreq_flags)tmpmax;
1090
 
  /* Reject the loopback device */
1091
 
  if(flags & IFF_LOOPBACK){
1092
 
    if(debug){
1093
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1094
 
              if_entry->d_name);
1095
 
    }
1096
 
    return 0;
1097
 
  }
1098
 
  /* Accept point-to-point devices only if connect_to is specified */
1099
 
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1100
 
    if(debug){
1101
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1102
 
              if_entry->d_name);
1103
 
    }
1104
 
    return 1;
1105
 
  }
1106
 
  /* Otherwise, reject non-broadcast-capable devices */
1107
 
  if(not (flags & IFF_BROADCAST)){
1108
 
    if(debug){
1109
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1110
 
              if_entry->d_name);
1111
 
    }
1112
 
    return 0;
1113
 
  }
1114
 
  /* Accept this device */
1115
 
  if(debug){
1116
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1117
 
            if_entry->d_name);
1118
 
  }
1119
 
  return 1;
1120
 
}
1121
 
 
1122
1023
int main(int argc, char *argv[]){
1123
1024
  AvahiSServiceBrowser *sb = NULL;
1124
1025
  int error;
1126
1027
  intmax_t tmpmax;
1127
1028
  char *tmp;
1128
1029
  int exitcode = EXIT_SUCCESS;
1129
 
  const char *interface = "";
 
1030
  const char *interface = "eth0";
1130
1031
  struct ifreq network;
1131
1032
  int sd = -1;
1132
1033
  bool take_down_interface = false;
1133
1034
  uid_t uid;
1134
1035
  gid_t gid;
 
1036
  char *connect_to = NULL;
1135
1037
  char tempdir[] = "/tmp/mandosXXXXXX";
1136
1038
  bool tempdir_created = false;
1137
1039
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1294
1196
  if(not debug){
1295
1197
    avahi_set_log_function(empty_log);
1296
1198
  }
1297
 
 
1298
 
  if(interface[0] == '\0'){
1299
 
    struct dirent **direntries;
1300
 
    ret = scandir(sys_class_net, &direntries, good_interface,
1301
 
                  alphasort);
1302
 
    if(ret >= 1){
1303
 
      /* Pick the first good interface */
1304
 
      interface = strdup(direntries[0]->d_name);
1305
 
      if(debug){
1306
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1307
 
      }
1308
 
      if(interface == NULL){
1309
 
        perror("malloc");
1310
 
        free(direntries);
1311
 
        exitcode = EXIT_FAILURE;
1312
 
        goto end;
1313
 
      }
1314
 
      free(direntries);
1315
 
    } else {
1316
 
      free(direntries);
1317
 
      fprintf(stderr, "Could not find a network interface\n");
1318
 
      exitcode = EXIT_FAILURE;
1319
 
      goto end;
1320
 
    }
1321
 
  }
1322
1199
  
1323
1200
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1324
1201
     from the signal handler */
1395
1272
  }
1396
1273
  
1397
1274
  /* If the interface is down, bring it up */
1398
 
  if(strcmp(interface, "none") != 0){
 
1275
  if(interface[0] != '\0'){
1399
1276
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1400
1277
    if(if_index == 0){
1401
1278
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1472
1349
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1473
1350
      if(ret == -1){
1474
1351
        take_down_interface = false;
1475
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1352
        perror("ioctl SIOCSIFFLAGS");
1476
1353
        exitcode = EX_OSERR;
1477
1354
#ifdef __linux__
1478
1355
        if(restore_loglevel){
1620
1497
    if(quit_now){
1621
1498
      goto end;
1622
1499
    }
1623
 
 
1624
 
    while(not quit_now){
1625
 
      ret = start_mandos_communication(address, port, if_index, af);
1626
 
      if(quit_now or ret == 0){
 
1500
    
 
1501
    ret = start_mandos_communication(address, port, if_index, af);
 
1502
    if(ret < 0){
 
1503
      switch(errno){
 
1504
      case ENETUNREACH:
 
1505
      case EHOSTDOWN:
 
1506
      case EHOSTUNREACH:
 
1507
        exitcode = EX_NOHOST;
 
1508
        break;
 
1509
      case EINVAL:
 
1510
        exitcode = EX_USAGE;
 
1511
        break;
 
1512
      case EIO:
 
1513
        exitcode = EX_IOERR;
 
1514
        break;
 
1515
      case EPROTO:
 
1516
        exitcode = EX_PROTOCOL;
 
1517
        break;
 
1518
      default:
 
1519
        exitcode = EX_OSERR;
1627
1520
        break;
1628
1521
      }
1629
 
      sleep(15);
1630
 
    };
1631
 
 
1632
 
    if (not quit_now){
 
1522
    } else {
1633
1523
      exitcode = EXIT_SUCCESS;
1634
1524
    }
1635
 
 
1636
1525
    goto end;
1637
1526
  }
1638
1527
  
1735
1624
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1736
1625
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1737
1626
        if(ret == -1){
1738
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1627
          perror("ioctl SIOCSIFFLAGS");
1739
1628
        }
1740
1629
      }
1741
1630
      ret = (int)TEMP_FAILURE_RETRY(close(sd));