82
82
#include <signal.h> /* sigemptyset(), sigaddset(),
83
83
sigaction(), SIGTERM, sig_atomic_t,
85
#include <sysexits.h> /* EX_OSERR, EX_USAGE */
87
88
#include <sys/klog.h> /* klogctl() */
125
126
static const char mandos_protocol_version[] = "1";
126
127
const char *argp_program_version = "mandos-client " VERSION;
127
128
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
130
/* Used for passing in values through the Avahi callback functions */
976
975
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+1); /* +1 for final \0 */
1005
flagstring[(size_t)to_read] = '\0';
1006
if(flagstring == NULL){
1012
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
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){
1034
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1035
flagstring, if_entry->d_name);
1041
ifreq_flags flags = (ifreq_flags)tmpmax;
1042
/* Reject the loopback device */
1043
if(flags & IFF_LOOPBACK){
1045
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1050
/* Accept point-to-point devices only if connect_to is specified */
1051
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1053
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1058
/* Otherwise, reject non-broadcast-capable devices */
1059
if(not (flags & IFF_BROADCAST)){
1061
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1066
/* Accept this device */
1068
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1074
978
int main(int argc, char *argv[]){
1075
979
AvahiSServiceBrowser *sb = NULL;
1078
982
intmax_t tmpmax;
1080
984
int exitcode = EXIT_SUCCESS;
1081
const char *interface = "";
985
const char *interface = "eth0";
1082
986
struct ifreq network;
1084
988
bool take_down_interface = false;
991
char *connect_to = NULL;
1087
992
char tempdir[] = "/tmp/mandosXXXXXX";
1088
993
bool tempdir_created = false;
1089
994
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1152
1057
.arg = "SECONDS",
1153
1058
.doc = "Maximum delay to wait for interface startup",
1061
* These reproduce what we would get without ARGP_NO_HELP
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 },
1155
1069
{ .name = NULL }
1158
1072
error_t parse_opt(int key, char *arg,
1159
1073
struct argp_state *state){
1161
1076
case 128: /* --debug */
1178
1093
tmpmax = strtoimax(arg, &tmp, 10);
1179
1094
if(errno != 0 or tmp == arg or *tmp != '\0'
1180
1095
or tmpmax != (typeof(mc.dh_bits))tmpmax){
1181
fprintf(stderr, "Bad number of DH bits\n");
1096
argp_error(state, "Bad number of DH bits");
1184
1098
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1191
1105
delay = strtof(arg, &tmp);
1192
1106
if(errno != 0 or tmp == arg or *tmp != '\0'){
1193
fprintf(stderr, "Bad delay\n");
1107
argp_error(state, "Bad delay");
1111
* These reproduce what we would get without ARGP_NO_HELP
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);
1202
1125
return ARGP_ERR_UNKNOWN;
1207
1130
struct argp argp = { .options = options, .parser = parse_opt,
1208
1131
.args_doc = "",
1209
1132
.doc = "Mandos client -- Get and decrypt"
1210
1133
" passwords from a Mandos server" };
1211
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
1212
if(ret == ARGP_ERR_UNKNOWN){
1213
fprintf(stderr, "Unknown error while parsing arguments\n");
1214
exitcode = EXIT_FAILURE;
1134
ret = argp_parse(&argp, argc, argv,
1135
ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1142
perror("argp_parse");
1143
exitcode = EX_OSERR;
1146
exitcode = EX_USAGE;
1220
1152
avahi_set_log_function(empty_log);
1223
if(interface[0] == '\0'){
1224
struct dirent **direntries;
1225
ret = scandir(sys_class_net, &direntries, good_interface,
1228
/* Pick the first good interface */
1229
interface = strdup(direntries[0]->d_name);
1231
fprintf(stderr, "Using interface \"%s\"\n", interface);
1233
if(interface == NULL){
1236
exitcode = EXIT_FAILURE;
1242
fprintf(stderr, "Could not find a network interface\n");
1243
exitcode = EXIT_FAILURE;
1248
1155
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1249
1156
from the signal handler */
1322
1229
/* If the interface is down, bring it up */
1323
if(strcmp(interface, "none") != 0){
1230
if(interface[0] != '\0'){
1324
1231
if_index = (AvahiIfIndex) if_nametoindex(interface);
1325
1232
if(if_index == 0){
1326
1233
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1342
1249
#ifdef __linux__
1343
1250
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1344
messages to mess up the prompt */
1251
messages about the network interface to mess up the prompt */
1345
1252
ret = klogctl(8, NULL, 5);
1346
1253
bool restore_loglevel = true;
1652
1559
perror("ioctl SIOCGIFFLAGS");
1653
1560
} else if(network.ifr_flags & IFF_UP) {
1654
network.ifr_flags &= ~IFF_UP; /* clear flag */
1561
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1655
1562
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1657
perror("ioctl SIOCSIFFLAGS -IFF_UP");
1564
perror("ioctl SIOCSIFFLAGS");
1660
1567
ret = (int)TEMP_FAILURE_RETRY(close(sd));