/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

* README (FAQ): Fix typo.
* mandos (main): Try to always do cleanup() before exit, since
                 otherwise the D-Bus bus name gets unregistered first.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2011 Teddy Hogeborn
13
 
 * Copyright © 2008-2011 Björn Påhlsson
 
12
 * Copyright © 2008,2009 Teddy Hogeborn
 
13
 * Copyright © 2008,2009 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
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 */
73
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
74
                                   getuid(), getgid(), seteuid(),
75
75
                                   setgid(), pause() */
76
 
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
 
76
#include <arpa/inet.h>          /* inet_pton(), htons */
77
77
#include <iso646.h>             /* not, or, and */
78
78
#include <argp.h>               /* struct argp_option, error_t, struct
79
79
                                   argp_state, struct argp,
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 {
423
421
  }
424
422
  
425
423
  /* OpenPGP credentials */
426
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
424
  gnutls_certificate_allocate_credentials(&mc.cred);
427
425
  if(ret != GNUTLS_E_SUCCESS){
428
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
426
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
427
                                                    from
 
428
                                                    -Wunreachable-code
 
429
                                                 */
429
430
            safer_gnutls_strerror(ret));
430
431
    gnutls_global_deinit();
431
432
    return -1;
687
688
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
688
689
  }
689
690
  if(ret < 0){
690
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
691
 
      int e = errno;
692
 
      perror("connect");
693
 
      errno = e;
694
 
    }
 
691
    int e = errno;
 
692
    perror("connect");
 
693
    errno = e;
695
694
    goto mandos_end;
696
695
  }
697
696
  
1021
1020
  errno = old_errno;
1022
1021
}
1023
1022
 
1024
 
/* 
1025
 
 * This function determines if a directory entry in /sys/class/net
1026
 
 * corresponds to an acceptable network device.
1027
 
 * (This function is passed to scandir(3) as a filter function.)
1028
 
 */
1029
 
int good_interface(const struct dirent *if_entry){
1030
 
  ssize_t ssret;
1031
 
  char *flagname = NULL;
1032
 
  if(if_entry->d_name[0] == '.'){
1033
 
    return 0;
1034
 
  }
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
 
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1042
 
  if(flags_fd == -1){
1043
 
    perror("open");
1044
 
    free(flagname);
1045
 
    return 0;
1046
 
  }
1047
 
  free(flagname);
1048
 
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1049
 
  /* read line from flags_fd */
1050
 
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1051
 
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1052
 
  flagstring[(size_t)to_read] = '\0';
1053
 
  if(flagstring == NULL){
1054
 
    perror("malloc");
1055
 
    close(flags_fd);
1056
 
    return 0;
1057
 
  }
1058
 
  while(to_read > 0){
1059
 
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1060
 
                                             (size_t)to_read));
1061
 
    if(ssret == -1){
1062
 
      perror("read");
1063
 
      free(flagstring);
1064
 
      close(flags_fd);
1065
 
      return 0;
1066
 
    }
1067
 
    to_read -= ssret;
1068
 
    if(ssret == 0){
1069
 
      break;
1070
 
    }
1071
 
  }
1072
 
  close(flags_fd);
1073
 
  intmax_t tmpmax;
1074
 
  char *tmp;
1075
 
  errno = 0;
1076
 
  tmpmax = strtoimax(flagstring, &tmp, 0);
1077
 
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1078
 
                                         and not (isspace(*tmp)))
1079
 
     or tmpmax != (ifreq_flags)tmpmax){
1080
 
    if(debug){
1081
 
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1082
 
              flagstring, if_entry->d_name);
1083
 
    }
1084
 
    free(flagstring);
1085
 
    return 0;
1086
 
  }
1087
 
  free(flagstring);
1088
 
  ifreq_flags flags = (ifreq_flags)tmpmax;
1089
 
  /* Reject the loopback device */
1090
 
  if(flags & IFF_LOOPBACK){
1091
 
    if(debug){
1092
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1093
 
              if_entry->d_name);
1094
 
    }
1095
 
    return 0;
1096
 
  }
1097
 
  /* Accept point-to-point devices only if connect_to is specified */
1098
 
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1099
 
    if(debug){
1100
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1101
 
              if_entry->d_name);
1102
 
    }
1103
 
    return 1;
1104
 
  }
1105
 
  /* Otherwise, reject non-broadcast-capable devices */
1106
 
  if(not (flags & IFF_BROADCAST)){
1107
 
    if(debug){
1108
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1109
 
              if_entry->d_name);
1110
 
    }
1111
 
    return 0;
1112
 
  }
1113
 
  /* Reject non-ARP interfaces (including dummy interfaces) */
1114
 
  if(flags & IFF_NOARP){
1115
 
    if(debug){
1116
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1117
 
              if_entry->d_name);
1118
 
    }
1119
 
    return 0;
1120
 
  }
1121
 
  /* Accept this device */
1122
 
  if(debug){
1123
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1124
 
            if_entry->d_name);
1125
 
  }
1126
 
  return 1;
1127
 
}
1128
 
 
1129
1023
int main(int argc, char *argv[]){
1130
1024
  AvahiSServiceBrowser *sb = NULL;
1131
1025
  int error;
1133
1027
  intmax_t tmpmax;
1134
1028
  char *tmp;
1135
1029
  int exitcode = EXIT_SUCCESS;
1136
 
  const char *interface = "";
 
1030
  const char *interface = "eth0";
1137
1031
  struct ifreq network;
1138
1032
  int sd = -1;
1139
1033
  bool take_down_interface = false;
1140
1034
  uid_t uid;
1141
1035
  gid_t gid;
 
1036
  char *connect_to = NULL;
1142
1037
  char tempdir[] = "/tmp/mandosXXXXXX";
1143
1038
  bool tempdir_created = false;
1144
1039
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1301
1196
  if(not debug){
1302
1197
    avahi_set_log_function(empty_log);
1303
1198
  }
1304
 
 
1305
 
  if(interface[0] == '\0'){
1306
 
    struct dirent **direntries;
1307
 
    ret = scandir(sys_class_net, &direntries, good_interface,
1308
 
                  alphasort);
1309
 
    if(ret >= 1){
1310
 
      /* Pick the first good interface */
1311
 
      interface = strdup(direntries[0]->d_name);
1312
 
      if(debug){
1313
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1314
 
      }
1315
 
      if(interface == NULL){
1316
 
        perror("malloc");
1317
 
        free(direntries);
1318
 
        exitcode = EXIT_FAILURE;
1319
 
        goto end;
1320
 
      }
1321
 
      free(direntries);
1322
 
    } else {
1323
 
      free(direntries);
1324
 
      fprintf(stderr, "Could not find a network interface\n");
1325
 
      exitcode = EXIT_FAILURE;
1326
 
      goto end;
1327
 
    }
1328
 
  }
1329
1199
  
1330
1200
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1331
1201
     from the signal handler */
1402
1272
  }
1403
1273
  
1404
1274
  /* If the interface is down, bring it up */
1405
 
  if(strcmp(interface, "none") != 0){
 
1275
  if(interface[0] != '\0'){
1406
1276
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1407
1277
    if(if_index == 0){
1408
1278
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1479
1349
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1480
1350
      if(ret == -1){
1481
1351
        take_down_interface = false;
1482
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1352
        perror("ioctl SIOCSIFFLAGS");
1483
1353
        exitcode = EX_OSERR;
1484
1354
#ifdef __linux__
1485
1355
        if(restore_loglevel){
1627
1497
    if(quit_now){
1628
1498
      goto end;
1629
1499
    }
1630
 
 
1631
 
    while(not quit_now){
1632
 
      ret = start_mandos_communication(address, port, if_index, af);
1633
 
      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;
1634
1520
        break;
1635
1521
      }
1636
 
      sleep(15);
1637
 
    };
1638
 
 
1639
 
    if (not quit_now){
 
1522
    } else {
1640
1523
      exitcode = EXIT_SUCCESS;
1641
1524
    }
1642
 
 
1643
1525
    goto end;
1644
1526
  }
1645
1527
  
1742
1624
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1743
1625
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1744
1626
        if(ret == -1){
1745
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1627
          perror("ioctl SIOCSIFFLAGS");
1746
1628
        }
1747
1629
      }
1748
1630
      ret = (int)TEMP_FAILURE_RETRY(close(sd));