43
43
                                   stdout, ferror(), remove() */
 
44
44
#include <stdint.h>             /* uint16_t, uint32_t */
 
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
 
46
 
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
 
 
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
 
 
47
                                   srand(), strtof(), abort() */
 
48
48
#include <stdbool.h>            /* bool, false, true */
 
49
49
#include <string.h>             /* memset(), strcmp(), strlen(),
 
50
50
                                   strerror(), asprintf(), strcpy() */
 
 
82
82
#include <signal.h>             /* sigemptyset(), sigaddset(),
 
83
83
                                   sigaction(), SIGTERM, sig_atomic_t,
 
85
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
 
86
 
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
 
89
87
#include <sys/klog.h>           /* klogctl() */
 
 
127
125
static const char mandos_protocol_version[] = "1";
 
128
126
const char *argp_program_version = "mandos-client " VERSION;
 
129
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;
 
131
131
/* Used for passing in values through the Avahi callback functions */
 
 
628
617
      if(if_index == AVAHI_IF_UNSPEC){
 
629
618
        fprintf(stderr, "An IPv6 link-local address is incomplete"
 
630
619
                " without a network interface\n");
 
634
622
      /* Set the network interface number as scope */
 
 
863
829
                          (size_t)decrypted_buffer_size - written,
 
865
831
        if(ret == 0 and ferror(stdout)){
 
868
833
            fprintf(stderr, "Error writing encrypted data: %s\n",
 
869
834
                    strerror(errno));
 
874
838
        written += (size_t)ret;
 
 
1020
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);
 
 
1005
  if(flagstring == NULL){
 
 
1011
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
 
 
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){
 
 
1036
  ifreq_flags flags = (ifreq_flags)tmpmax;
 
 
1037
  /* Reject the loopback device */
 
 
1038
  if(flags & IFF_LOOPBACK){
 
 
1041
  /* Accept point-to-point devices only if connect_to is specified */
 
 
1042
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
 
 
1045
  /* Otherwise, reject non-broadcast-capable devices */
 
 
1046
  if(not (flags & IFF_BROADCAST)){
 
 
1049
  /* Accept this device */
 
1023
1053
int main(int argc, char *argv[]){
 
1024
1054
  AvahiSServiceBrowser *sb = NULL;
 
 
1027
1057
  intmax_t tmpmax;
 
1029
1059
  int exitcode = EXIT_SUCCESS;
 
1030
 
  const char *interface = "eth0";
 
 
1060
  const char *interface = "";
 
1031
1061
  struct ifreq network;
 
1033
1063
  bool take_down_interface = false;
 
1036
 
  char *connect_to = NULL;
 
1037
1066
  char tempdir[] = "/tmp/mandosXXXXXX";
 
1038
1067
  bool tempdir_created = false;
 
1039
1068
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
 
 
1102
1131
        .arg = "SECONDS",
 
1103
1132
        .doc = "Maximum delay to wait for interface startup",
 
1106
 
       * These reproduce what we would get without ARGP_NO_HELP
 
1108
 
      { .name = "help", .key = '?',
 
1109
 
        .doc = "Give this help list", .group = -1 },
 
1110
 
      { .name = "usage", .key = -3,
 
1111
 
        .doc = "Give a short usage message", .group = -1 },
 
1112
 
      { .name = "version", .key = 'V',
 
1113
 
        .doc = "Print program version", .group = -1 },
 
1114
1134
      { .name = NULL }
 
1117
1137
    error_t parse_opt(int key, char *arg,
 
1118
1138
                      struct argp_state *state){
 
1121
1140
      case 128:                 /* --debug */
 
 
1138
1157
        tmpmax = strtoimax(arg, &tmp, 10);
 
1139
1158
        if(errno != 0 or tmp == arg or *tmp != '\0'
 
1140
1159
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
 
1141
 
          argp_error(state, "Bad number of DH bits");
 
 
1160
          fprintf(stderr, "Bad number of DH bits\n");
 
1143
1163
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
 
 
1150
1170
        delay = strtof(arg, &tmp);
 
1151
1171
        if(errno != 0 or tmp == arg or *tmp != '\0'){
 
1152
 
          argp_error(state, "Bad delay");
 
 
1172
          fprintf(stderr, "Bad delay\n");
 
1156
 
         * These reproduce what we would get without ARGP_NO_HELP
 
1158
 
      case '?':                 /* --help */
 
1159
 
        argp_state_help(state, state->out_stream,
 
1160
 
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
 
1161
 
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
 
1162
 
      case -3:                  /* --usage */
 
1163
 
        argp_state_help(state, state->out_stream,
 
1164
 
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
 
1165
 
      case 'V':                 /* --version */
 
1166
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
 
1167
 
        exit(argp_err_exit_status);
 
1170
1181
        return ARGP_ERR_UNKNOWN;
 
1175
1186
    struct argp argp = { .options = options, .parser = parse_opt,
 
1176
1187
                         .args_doc = "",
 
1177
1188
                         .doc = "Mandos client -- Get and decrypt"
 
1178
1189
                         " passwords from a Mandos server" };
 
1179
 
    ret = argp_parse(&argp, argc, argv,
 
1180
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
 
1187
 
      perror("argp_parse");
 
1188
 
      exitcode = EX_OSERR;
 
1191
 
      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;
 
 
1197
1199
    avahi_set_log_function(empty_log);
 
 
1202
  if(interface[0] == '\0'){
 
 
1203
    struct dirent **direntries;
 
 
1204
    ret = scandir(sys_class_net, &direntries, good_interface,
 
 
1207
      /* Pick the first good interface */
 
 
1208
      interface = strdup(direntries[0]->d_name);
 
 
1209
      if(interface == NULL){
 
 
1212
        exitcode = EXIT_FAILURE;
 
 
1218
      fprintf(stderr, "Could not find a network interface\n");
 
 
1219
      exitcode = EXIT_FAILURE;
 
1200
1224
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
 
1201
1225
     from the signal handler */
 
 
1204
1228
  mc.simple_poll = avahi_simple_poll_new();
 
1205
1229
  if(mc.simple_poll == NULL){
 
1206
1230
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
 
1207
 
    exitcode = EX_UNAVAILABLE;
 
 
1231
    exitcode = EXIT_FAILURE;
 
 
1212
1236
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
 
1214
1238
    perror("sigaddset");
 
1215
 
    exitcode = EX_OSERR;
 
 
1239
    exitcode = EXIT_FAILURE;
 
1218
1242
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
 
1220
1244
    perror("sigaddset");
 
1221
 
    exitcode = EX_OSERR;
 
 
1245
    exitcode = EXIT_FAILURE;
 
1224
1248
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
 
1226
1250
    perror("sigaddset");
 
1227
 
    exitcode = EX_OSERR;
 
 
1251
    exitcode = EXIT_FAILURE;
 
1230
1254
  /* Need to check if the handler is SIG_IGN before handling:
 
 
1234
1258
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
 
1236
1260
    perror("sigaction");
 
 
1261
    return EXIT_FAILURE;
 
1239
1263
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1240
1264
    ret = sigaction(SIGINT, &sigterm_action, NULL);
 
1242
1266
      perror("sigaction");
 
1243
 
      exitcode = EX_OSERR;
 
 
1267
      exitcode = EXIT_FAILURE;
 
1247
1271
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
 
1249
1273
    perror("sigaction");
 
 
1274
    return EXIT_FAILURE;
 
1252
1276
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1253
1277
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
 
1255
1279
      perror("sigaction");
 
1256
 
      exitcode = EX_OSERR;
 
 
1280
      exitcode = EXIT_FAILURE;
 
1260
1284
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
 
1262
1286
    perror("sigaction");
 
 
1287
    return EXIT_FAILURE;
 
1265
1289
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1266
1290
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
 
1268
1292
      perror("sigaction");
 
1269
 
      exitcode = EX_OSERR;
 
 
1293
      exitcode = EXIT_FAILURE;
 
1274
1298
  /* If the interface is down, bring it up */
 
1275
 
  if(interface[0] != '\0'){
 
 
1299
  if(strcmp(interface, "none") != 0){
 
1276
1300
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1277
1301
    if(if_index == 0){
 
1278
1302
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1279
 
      exitcode = EX_UNAVAILABLE;
 
 
1303
      exitcode = EXIT_FAILURE;
 
 
1294
1318
#ifdef __linux__
 
1295
1319
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1296
 
       messages about the network interface to mess up the prompt */
 
 
1320
       messages to mess up the prompt */
 
1297
1321
    ret = klogctl(8, NULL, 5);
 
1298
1322
    bool restore_loglevel = true;
 
 
1422
1446
  ret = init_gnutls_global(pubkey, seckey);
 
1424
1448
    fprintf(stderr, "init_gnutls_global failed\n");
 
1425
 
    exitcode = EX_UNAVAILABLE;
 
 
1449
    exitcode = EXIT_FAILURE;
 
1428
1452
    gnutls_initialized = true;
 
 
1446
1470
  if(not init_gpgme(pubkey, seckey, tempdir)){
 
1447
1471
    fprintf(stderr, "init_gpgme failed\n");
 
1448
 
    exitcode = EX_UNAVAILABLE;
 
 
1472
    exitcode = EXIT_FAILURE;
 
1451
1475
    gpgme_initialized = true;
 
 
1622
1628
        perror("ioctl SIOCGIFFLAGS");
 
1623
1629
      } else if(network.ifr_flags & IFF_UP) {
 
1624
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
 
1630
        network.ifr_flags &= ~IFF_UP; /* clear flag */
 
1625
1631
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1627
1633
          perror("ioctl SIOCSIFFLAGS");