/mandos/release

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

« back to all changes in this revision

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

  • Committer: teddy at bsnet
  • Date: 2010-08-10 19:08:24 UTC
  • mto: (237.2.182 mandos-local)
  • mto: This revision was merged to the branch mainline in revision 270.
  • Revision ID: teddy@fukt.bsnet.se-20100810190824-5yquozxy4kh6py3f
* plugins.d/mandos-client.c: An empty interface name now means to
                             autodetect an interface; to specify no
                             particular interface, use "none".
  (sys_class_net): New global variable for the "/sys/class/net" path.
  (good_interface): New function to determine the suitability of an
                    interface.  Used by a scandir() call in main().
  (main): Changed default value for "interface" to the empty string.
          Moved "connect_to" to be a global variable.  Only take down
          and up interface if its name is not "none".
* plugins.d/mandos-client.xml (OPTIONS): Update documentation for the
                                         "--interface" option.

Show diffs side-by-side

added added

removed removed

Lines of Context:
82
82
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
83
                                   sigaction(), SIGTERM, sig_atomic_t,
84
84
                                   raise() */
85
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE */
86
85
 
87
86
#ifdef __linux__
88
87
#include <sys/klog.h>           /* klogctl() */
126
125
static const char mandos_protocol_version[] = "1";
127
126
const char *argp_program_version = "mandos-client " VERSION;
128
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
130
 
130
131
/* Used for passing in values through the Avahi callback functions */
131
132
typedef struct {
975
976
  errno = old_errno;
976
977
}
977
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);
 
1005
  if(flagstring == NULL){
 
1006
    perror("malloc");
 
1007
    close(flags_fd);
 
1008
    return 0;
 
1009
  }
 
1010
  while(to_read > 0){
 
1011
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
 
1012
                                             (size_t)to_read));
 
1013
    if(ssret == -1){
 
1014
      perror("read");
 
1015
      free(flagstring);
 
1016
      close(flags_fd);
 
1017
      return 0;
 
1018
    }
 
1019
    to_read -= ssret;
 
1020
    if(ssret == 0){
 
1021
      break;
 
1022
    }
 
1023
  }
 
1024
  close(flags_fd);
 
1025
  intmax_t tmpmax;
 
1026
  char *tmp;
 
1027
  errno = 0;
 
1028
  tmpmax = strtoimax(flagstring, &tmp, 0);
 
1029
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
 
1030
                                         and not (isspace(*tmp)))
 
1031
     or tmpmax != (ifreq_flags)tmpmax){
 
1032
    free(flagstring);
 
1033
    return 0;
 
1034
  }
 
1035
  free(flagstring);
 
1036
  ifreq_flags flags = (ifreq_flags)tmpmax;
 
1037
  /* Reject the loopback device */
 
1038
  if(flags & IFF_LOOPBACK){
 
1039
    return 0;
 
1040
  }
 
1041
  /* Accept point-to-point devices only if connect_to is specified */
 
1042
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
 
1043
    return 1;
 
1044
  }
 
1045
  /* Otherwise, reject non-broadcast-capable devices */
 
1046
  if(not (flags & IFF_BROADCAST)){
 
1047
    return 0;
 
1048
  }
 
1049
  /* Accept this device */
 
1050
  return 1;
 
1051
}
 
1052
 
978
1053
int main(int argc, char *argv[]){
979
1054
  AvahiSServiceBrowser *sb = NULL;
980
1055
  int error;
982
1057
  intmax_t tmpmax;
983
1058
  char *tmp;
984
1059
  int exitcode = EXIT_SUCCESS;
985
 
  const char *interface = "eth0";
 
1060
  const char *interface = "";
986
1061
  struct ifreq network;
987
1062
  int sd = -1;
988
1063
  bool take_down_interface = false;
989
1064
  uid_t uid;
990
1065
  gid_t gid;
991
 
  char *connect_to = NULL;
992
1066
  char tempdir[] = "/tmp/mandosXXXXXX";
993
1067
  bool tempdir_created = false;
994
1068
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1057
1131
        .arg = "SECONDS",
1058
1132
        .doc = "Maximum delay to wait for interface startup",
1059
1133
        .group = 2 },
1060
 
      /*
1061
 
       * These reproduce what we would get without ARGP_NO_HELP
1062
 
       */
1063
 
      { .name = "help", .key = '?',
1064
 
        .doc = "Give this help list", .group = -1 },
1065
 
      { .name = "usage", .key = -3,
1066
 
        .doc = "Give a short usage message", .group = -1 },
1067
 
      { .name = "version", .key = 'V',
1068
 
        .doc = "Print program version", .group = -1 },
1069
1134
      { .name = NULL }
1070
1135
    };
1071
1136
    
1072
1137
    error_t parse_opt(int key, char *arg,
1073
1138
                      struct argp_state *state){
1074
 
      errno = 0;
1075
1139
      switch(key){
1076
1140
      case 128:                 /* --debug */
1077
1141
        debug = true;
1093
1157
        tmpmax = strtoimax(arg, &tmp, 10);
1094
1158
        if(errno != 0 or tmp == arg or *tmp != '\0'
1095
1159
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1096
 
          argp_error(state, "Bad number of DH bits");
 
1160
          fprintf(stderr, "Bad number of DH bits\n");
 
1161
          exit(EXIT_FAILURE);
1097
1162
        }
1098
1163
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1099
1164
        break;
1104
1169
        errno = 0;
1105
1170
        delay = strtof(arg, &tmp);
1106
1171
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1107
 
          argp_error(state, "Bad delay");
 
1172
          fprintf(stderr, "Bad delay\n");
 
1173
          exit(EXIT_FAILURE);
1108
1174
        }
1109
1175
        break;
1110
 
        /*
1111
 
         * These reproduce what we would get without ARGP_NO_HELP
1112
 
         */
1113
 
      case '?':                 /* --help */
1114
 
        argp_state_help(state, state->out_stream,
1115
 
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1116
 
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
1117
 
      case -3:                  /* --usage */
1118
 
        argp_state_help(state, state->out_stream,
1119
 
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1120
 
      case 'V':                 /* --version */
1121
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
1122
 
        exit(argp_err_exit_status);
 
1176
      case ARGP_KEY_ARG:
 
1177
        argp_usage(state);
 
1178
      case ARGP_KEY_END:
1123
1179
        break;
1124
1180
      default:
1125
1181
        return ARGP_ERR_UNKNOWN;
1126
1182
      }
1127
 
      return errno;
 
1183
      return 0;
1128
1184
    }
1129
1185
    
1130
1186
    struct argp argp = { .options = options, .parser = parse_opt,
1131
1187
                         .args_doc = "",
1132
1188
                         .doc = "Mandos client -- Get and decrypt"
1133
1189
                         " passwords from a Mandos server" };
1134
 
    ret = argp_parse(&argp, argc, argv,
1135
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1136
 
    switch(ret){
1137
 
    case 0:
1138
 
      break;
1139
 
    case ENOMEM:
1140
 
    default:
1141
 
      errno = ret;
1142
 
      perror("argp_parse");
1143
 
      exitcode = EX_OSERR;
1144
 
      goto end;
1145
 
    case EINVAL:
1146
 
      exitcode = EX_USAGE;
 
1190
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
1191
    if(ret == ARGP_ERR_UNKNOWN){
 
1192
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
1193
      exitcode = EXIT_FAILURE;
1147
1194
      goto end;
1148
1195
    }
1149
1196
  }
1151
1198
  if(not debug){
1152
1199
    avahi_set_log_function(empty_log);
1153
1200
  }
 
1201
 
 
1202
  if(interface[0] == '\0'){
 
1203
    struct dirent **direntries;
 
1204
    ret = scandir(sys_class_net, &direntries, good_interface,
 
1205
                  alphasort);
 
1206
    if(ret >= 1){
 
1207
      /* Pick the first good interface */
 
1208
      interface = strdup(direntries[0]->d_name);
 
1209
      if(interface == NULL){
 
1210
        perror("malloc");
 
1211
        free(direntries);
 
1212
        exitcode = EXIT_FAILURE;
 
1213
        goto end;
 
1214
      }
 
1215
      free(direntries);
 
1216
    } else {
 
1217
      free(direntries);
 
1218
      fprintf(stderr, "Could not find a network interface\n");
 
1219
      exitcode = EXIT_FAILURE;
 
1220
      goto end;
 
1221
    }
 
1222
  }
1154
1223
  
1155
1224
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1156
1225
     from the signal handler */
1227
1296
  }
1228
1297
  
1229
1298
  /* If the interface is down, bring it up */
1230
 
  if(interface[0] != '\0'){
 
1299
  if(strcmp(interface, "none") != 0){
1231
1300
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1232
1301
    if(if_index == 0){
1233
1302
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1248
1317
    
1249
1318
#ifdef __linux__
1250
1319
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1251
 
       messages about the network interface to mess up the prompt */
 
1320
       messages to mess up the prompt */
1252
1321
    ret = klogctl(8, NULL, 5);
1253
1322
    bool restore_loglevel = true;
1254
1323
    if(ret == -1){
1558
1627
      if(ret == -1){
1559
1628
        perror("ioctl SIOCGIFFLAGS");
1560
1629
      } else if(network.ifr_flags & IFF_UP) {
1561
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
1630
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1562
1631
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1563
1632
        if(ret == -1){
1564
1633
          perror("ioctl SIOCSIFFLAGS");