/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-08-11 20:06:18 UTC
  • mto: This revision was merged to the branch mainline in revision 419.
  • Revision ID: teddy@fukt.bsnet.se-20100811200618-1uuv7p6cmrb2cmxu
* plugins.d/mandos-client.c: Added debug output.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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;
128
130
 
129
131
/* Used for passing in values through the Avahi callback functions */
130
132
typedef struct {
974
976
  errno = old_errno;
975
977
}
976
978
 
 
979
/* 
 
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.)
 
983
 */
 
984
int good_interface(const struct dirent *if_entry){
 
985
  ssize_t ssret;
 
986
  char *flagname = NULL;
 
987
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
 
988
                     if_entry->d_name);
 
989
  if(ret < 0){
 
990
    perror("asprintf");
 
991
    return 0;
 
992
  }
 
993
  if(if_entry->d_name[0] == '.'){
 
994
    return 0;
 
995
  }
 
996
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
 
997
  if(flags_fd == -1){
 
998
    perror("open");
 
999
    return 0;
 
1000
  }
 
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){
 
1007
    perror("malloc");
 
1008
    close(flags_fd);
 
1009
    return 0;
 
1010
  }
 
1011
  while(to_read > 0){
 
1012
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
 
1013
                                             (size_t)to_read));
 
1014
    if(ssret == -1){
 
1015
      perror("read");
 
1016
      free(flagstring);
 
1017
      close(flags_fd);
 
1018
      return 0;
 
1019
    }
 
1020
    to_read -= ssret;
 
1021
    if(ssret == 0){
 
1022
      break;
 
1023
    }
 
1024
  }
 
1025
  close(flags_fd);
 
1026
  intmax_t tmpmax;
 
1027
  char *tmp;
 
1028
  errno = 0;
 
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){
 
1033
    if(debug){
 
1034
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
 
1035
              flagstring, if_entry->d_name);
 
1036
    }
 
1037
    free(flagstring);
 
1038
    return 0;
 
1039
  }
 
1040
  free(flagstring);
 
1041
  ifreq_flags flags = (ifreq_flags)tmpmax;
 
1042
  /* Reject the loopback device */
 
1043
  if(flags & IFF_LOOPBACK){
 
1044
    if(debug){
 
1045
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
 
1046
              if_entry->d_name);
 
1047
    }
 
1048
    return 0;
 
1049
  }
 
1050
  /* Accept point-to-point devices only if connect_to is specified */
 
1051
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
 
1052
    if(debug){
 
1053
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
 
1054
              if_entry->d_name);
 
1055
    }
 
1056
    return 1;
 
1057
  }
 
1058
  /* Otherwise, reject non-broadcast-capable devices */
 
1059
  if(not (flags & IFF_BROADCAST)){
 
1060
    if(debug){
 
1061
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
 
1062
              if_entry->d_name);
 
1063
    }
 
1064
    return 0;
 
1065
  }
 
1066
  /* Accept this device */
 
1067
  if(debug){
 
1068
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
 
1069
            if_entry->d_name);
 
1070
  }
 
1071
  return 1;
 
1072
}
 
1073
 
977
1074
int main(int argc, char *argv[]){
978
1075
  AvahiSServiceBrowser *sb = NULL;
979
1076
  int error;
981
1078
  intmax_t tmpmax;
982
1079
  char *tmp;
983
1080
  int exitcode = EXIT_SUCCESS;
984
 
  const char *interface = "eth0";
 
1081
  const char *interface = "";
985
1082
  struct ifreq network;
986
1083
  int sd = -1;
987
1084
  bool take_down_interface = false;
988
1085
  uid_t uid;
989
1086
  gid_t gid;
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;
1123
1219
  if(not debug){
1124
1220
    avahi_set_log_function(empty_log);
1125
1221
  }
 
1222
 
 
1223
  if(interface[0] == '\0'){
 
1224
    struct dirent **direntries;
 
1225
    ret = scandir(sys_class_net, &direntries, good_interface,
 
1226
                  alphasort);
 
1227
    if(ret >= 1){
 
1228
      /* Pick the first good interface */
 
1229
      interface = strdup(direntries[0]->d_name);
 
1230
      if(debug){
 
1231
        fprintf(stderr, "Using interface \"%s\"\n", interface);
 
1232
      }
 
1233
      if(interface == NULL){
 
1234
        perror("malloc");
 
1235
        free(direntries);
 
1236
        exitcode = EXIT_FAILURE;
 
1237
        goto end;
 
1238
      }
 
1239
      free(direntries);
 
1240
    } else {
 
1241
      free(direntries);
 
1242
      fprintf(stderr, "Could not find a network interface\n");
 
1243
      exitcode = EXIT_FAILURE;
 
1244
      goto end;
 
1245
    }
 
1246
  }
1126
1247
  
1127
1248
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1128
1249
     from the signal handler */
1199
1320
  }
1200
1321
  
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);
1276
1397
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1277
1398
      if(ret == -1){
1278
1399
        take_down_interface = false;
1279
 
        perror("ioctl SIOCSIFFLAGS");
 
1400
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1280
1401
        exitcode = EXIT_FAILURE;
1281
1402
#ifdef __linux__
1282
1403
        if(restore_loglevel){
1533
1654
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1534
1655
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1535
1656
        if(ret == -1){
1536
 
          perror("ioctl SIOCSIFFLAGS");
 
1657
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
1537
1658
        }
1538
1659
      }
1539
1660
      ret = (int)TEMP_FAILURE_RETRY(close(sd));