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, EXIT_FAILURE,
47
srand(), strtof(), abort() */
46
#include <stdlib.h> /* free(), EXIT_SUCCESS, srand(),
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 */
87
89
#include <sys/klog.h> /* klogctl() */
125
127
static const char mandos_protocol_version[] = "1";
126
128
const char *argp_program_version = "mandos-client " VERSION;
127
129
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 */
976
1020
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
1023
int main(int argc, char *argv[]){
1075
1024
AvahiSServiceBrowser *sb = NULL;
1078
1027
intmax_t tmpmax;
1080
1029
int exitcode = EXIT_SUCCESS;
1081
const char *interface = "";
1030
const char *interface = "eth0";
1082
1031
struct ifreq network;
1084
1033
bool take_down_interface = false;
1036
char *connect_to = NULL;
1087
1037
char tempdir[] = "/tmp/mandosXXXXXX";
1088
1038
bool tempdir_created = false;
1089
1039
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1152
1102
.arg = "SECONDS",
1153
1103
.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 },
1155
1114
{ .name = NULL }
1158
1117
error_t parse_opt(int key, char *arg,
1159
1118
struct argp_state *state){
1161
1121
case 128: /* --debug */
1178
1138
tmpmax = strtoimax(arg, &tmp, 10);
1179
1139
if(errno != 0 or tmp == arg or *tmp != '\0'
1180
1140
or tmpmax != (typeof(mc.dh_bits))tmpmax){
1181
fprintf(stderr, "Bad number of DH bits\n");
1141
argp_error(state, "Bad number of DH bits");
1184
1143
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1191
1150
delay = strtof(arg, &tmp);
1192
1151
if(errno != 0 or tmp == arg or *tmp != '\0'){
1193
fprintf(stderr, "Bad delay\n");
1152
argp_error(state, "Bad delay");
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);
1202
1170
return ARGP_ERR_UNKNOWN;
1207
1175
struct argp argp = { .options = options, .parser = parse_opt,
1208
1176
.args_doc = "",
1209
1177
.doc = "Mandos client -- Get and decrypt"
1210
1178
" 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;
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;
1220
1197
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
1200
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1249
1201
from the signal handler */
1260
1212
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1262
1214
perror("sigaddset");
1263
exitcode = EXIT_FAILURE;
1215
exitcode = EX_OSERR;
1266
1218
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1268
1220
perror("sigaddset");
1269
exitcode = EXIT_FAILURE;
1221
exitcode = EX_OSERR;
1272
1224
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1274
1226
perror("sigaddset");
1275
exitcode = EXIT_FAILURE;
1227
exitcode = EX_OSERR;
1278
1230
/* Need to check if the handler is SIG_IGN before handling:
1282
1234
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1284
1236
perror("sigaction");
1285
return EXIT_FAILURE;
1287
1239
if(old_sigterm_action.sa_handler != SIG_IGN){
1288
1240
ret = sigaction(SIGINT, &sigterm_action, NULL);
1290
1242
perror("sigaction");
1291
exitcode = EXIT_FAILURE;
1243
exitcode = EX_OSERR;
1295
1247
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1297
1249
perror("sigaction");
1298
return EXIT_FAILURE;
1300
1252
if(old_sigterm_action.sa_handler != SIG_IGN){
1301
1253
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1303
1255
perror("sigaction");
1304
exitcode = EXIT_FAILURE;
1256
exitcode = EX_OSERR;
1308
1260
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1310
1262
perror("sigaction");
1311
return EXIT_FAILURE;
1313
1265
if(old_sigterm_action.sa_handler != SIG_IGN){
1314
1266
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1316
1268
perror("sigaction");
1317
exitcode = EXIT_FAILURE;
1269
exitcode = EX_OSERR;
1322
1274
/* If the interface is down, bring it up */
1323
if(strcmp(interface, "none") != 0){
1275
if(interface[0] != '\0'){
1324
1276
if_index = (AvahiIfIndex) if_nametoindex(interface);
1325
1277
if(if_index == 0){
1326
1278
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1327
exitcode = EXIT_FAILURE;
1279
exitcode = EX_UNAVAILABLE;
1342
1294
#ifdef __linux__
1343
1295
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1344
messages to mess up the prompt */
1296
messages about the network interface to mess up the prompt */
1345
1297
ret = klogctl(8, NULL, 5);
1346
1298
bool restore_loglevel = true;
1397
1349
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1399
1351
take_down_interface = false;
1400
perror("ioctl SIOCSIFFLAGS +IFF_UP");
1401
exitcode = EXIT_FAILURE;
1352
perror("ioctl SIOCSIFFLAGS");
1353
exitcode = EX_OSERR;
1402
1354
#ifdef __linux__
1403
1355
if(restore_loglevel){
1404
1356
ret = klogctl(7, NULL, 0);
1494
1446
if(not init_gpgme(pubkey, seckey, tempdir)){
1495
1447
fprintf(stderr, "init_gpgme failed\n");
1496
exitcode = EXIT_FAILURE;
1448
exitcode = EX_UNAVAILABLE;
1499
1451
gpgme_initialized = true;
1652
1622
perror("ioctl SIOCGIFFLAGS");
1653
1623
} else if(network.ifr_flags & IFF_UP) {
1654
network.ifr_flags &= ~IFF_UP; /* clear flag */
1624
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1655
1625
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1657
perror("ioctl SIOCSIFFLAGS -IFF_UP");
1627
perror("ioctl SIOCSIFFLAGS");
1660
1630
ret = (int)TEMP_FAILURE_RETRY(close(sd));