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