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 */
617
628
if(if_index == AVAHI_IF_UNSPEC){
618
629
fprintf(stderr, "An IPv6 link-local address is incomplete"
619
630
" without a network interface\n");
622
634
/* Set the network interface number as scope */
829
863
(size_t)decrypted_buffer_size - written,
831
865
if(ret == 0 and ferror(stdout)){
833
868
fprintf(stderr, "Error writing encrypted data: %s\n",
834
869
strerror(errno));
838
874
written += (size_t)ret;
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);
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 */
1053
1023
int main(int argc, char *argv[]){
1054
1024
AvahiSServiceBrowser *sb = NULL;
1057
1027
intmax_t tmpmax;
1059
1029
int exitcode = EXIT_SUCCESS;
1060
const char *interface = "";
1030
const char *interface = "eth0";
1061
1031
struct ifreq network;
1063
1033
bool take_down_interface = false;
1036
char *connect_to = NULL;
1066
1037
char tempdir[] = "/tmp/mandosXXXXXX";
1067
1038
bool tempdir_created = false;
1068
1039
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1131
1102
.arg = "SECONDS",
1132
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 },
1134
1114
{ .name = NULL }
1137
1117
error_t parse_opt(int key, char *arg,
1138
1118
struct argp_state *state){
1140
1121
case 128: /* --debug */
1157
1138
tmpmax = strtoimax(arg, &tmp, 10);
1158
1139
if(errno != 0 or tmp == arg or *tmp != '\0'
1159
1140
or tmpmax != (typeof(mc.dh_bits))tmpmax){
1160
fprintf(stderr, "Bad number of DH bits\n");
1141
argp_error(state, "Bad number of DH bits");
1163
1143
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1170
1150
delay = strtof(arg, &tmp);
1171
1151
if(errno != 0 or tmp == arg or *tmp != '\0'){
1172
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);
1181
1170
return ARGP_ERR_UNKNOWN;
1186
1175
struct argp argp = { .options = options, .parser = parse_opt,
1187
1176
.args_doc = "",
1188
1177
.doc = "Mandos client -- Get and decrypt"
1189
1178
" passwords from a Mandos server" };
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;
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;
1199
1197
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;
1224
1200
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1225
1201
from the signal handler */
1228
1204
mc.simple_poll = avahi_simple_poll_new();
1229
1205
if(mc.simple_poll == NULL){
1230
1206
fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1231
exitcode = EXIT_FAILURE;
1207
exitcode = EX_UNAVAILABLE;
1236
1212
ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1238
1214
perror("sigaddset");
1239
exitcode = EXIT_FAILURE;
1215
exitcode = EX_OSERR;
1242
1218
ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1244
1220
perror("sigaddset");
1245
exitcode = EXIT_FAILURE;
1221
exitcode = EX_OSERR;
1248
1224
ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1250
1226
perror("sigaddset");
1251
exitcode = EXIT_FAILURE;
1227
exitcode = EX_OSERR;
1254
1230
/* Need to check if the handler is SIG_IGN before handling:
1258
1234
ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1260
1236
perror("sigaction");
1261
return EXIT_FAILURE;
1263
1239
if(old_sigterm_action.sa_handler != SIG_IGN){
1264
1240
ret = sigaction(SIGINT, &sigterm_action, NULL);
1266
1242
perror("sigaction");
1267
exitcode = EXIT_FAILURE;
1243
exitcode = EX_OSERR;
1271
1247
ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1273
1249
perror("sigaction");
1274
return EXIT_FAILURE;
1276
1252
if(old_sigterm_action.sa_handler != SIG_IGN){
1277
1253
ret = sigaction(SIGHUP, &sigterm_action, NULL);
1279
1255
perror("sigaction");
1280
exitcode = EXIT_FAILURE;
1256
exitcode = EX_OSERR;
1284
1260
ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1286
1262
perror("sigaction");
1287
return EXIT_FAILURE;
1289
1265
if(old_sigterm_action.sa_handler != SIG_IGN){
1290
1266
ret = sigaction(SIGTERM, &sigterm_action, NULL);
1292
1268
perror("sigaction");
1293
exitcode = EXIT_FAILURE;
1269
exitcode = EX_OSERR;
1298
1274
/* If the interface is down, bring it up */
1299
if(strcmp(interface, "none") != 0){
1275
if(interface[0] != '\0'){
1300
1276
if_index = (AvahiIfIndex) if_nametoindex(interface);
1301
1277
if(if_index == 0){
1302
1278
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1303
exitcode = EXIT_FAILURE;
1279
exitcode = EX_UNAVAILABLE;
1318
1294
#ifdef __linux__
1319
1295
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1320
messages to mess up the prompt */
1296
messages about the network interface to mess up the prompt */
1321
1297
ret = klogctl(8, NULL, 5);
1322
1298
bool restore_loglevel = true;
1446
1422
ret = init_gnutls_global(pubkey, seckey);
1448
1424
fprintf(stderr, "init_gnutls_global failed\n");
1449
exitcode = EXIT_FAILURE;
1425
exitcode = EX_UNAVAILABLE;
1452
1428
gnutls_initialized = true;
1470
1446
if(not init_gpgme(pubkey, seckey, tempdir)){
1471
1447
fprintf(stderr, "init_gpgme failed\n");
1472
exitcode = EXIT_FAILURE;
1448
exitcode = EX_UNAVAILABLE;
1475
1451
gpgme_initialized = true;
1628
1622
perror("ioctl SIOCGIFFLAGS");
1629
1623
} else if(network.ifr_flags & IFF_UP) {
1630
network.ifr_flags &= ~IFF_UP; /* clear flag */
1624
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1631
1625
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1633
1627
perror("ioctl SIOCSIFFLAGS");