82
82
#include <signal.h> /* sigemptyset(), sigaddset(),
83
83
sigaction(), SIGTERM, sig_atomic_t,
85
#include <sysexits.h> /* EX_OSERR, EX_USAGE */
88
87
#include <sys/klog.h> /* klogctl() */
126
125
static const char mandos_protocol_version[] = "1";
127
126
const char *argp_program_version = "mandos-client " VERSION;
128
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;
130
131
/* Used for passing in values through the Avahi callback functions */
975
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);
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 */
978
1053
int main(int argc, char *argv[]){
979
1054
AvahiSServiceBrowser *sb = NULL;
982
1057
intmax_t tmpmax;
984
1059
int exitcode = EXIT_SUCCESS;
985
const char *interface = "eth0";
1060
const char *interface = "";
986
1061
struct ifreq network;
988
1063
bool take_down_interface = false;
991
char *connect_to = NULL;
992
1066
char tempdir[] = "/tmp/mandosXXXXXX";
993
1067
bool tempdir_created = false;
994
1068
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1057
1131
.arg = "SECONDS",
1058
1132
.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 },
1069
1134
{ .name = NULL }
1072
1137
error_t parse_opt(int key, char *arg,
1073
1138
struct argp_state *state){
1076
1140
case 128: /* --debug */
1093
1157
tmpmax = strtoimax(arg, &tmp, 10);
1094
1158
if(errno != 0 or tmp == arg or *tmp != '\0'
1095
1159
or tmpmax != (typeof(mc.dh_bits))tmpmax){
1096
argp_error(state, "Bad number of DH bits");
1160
fprintf(stderr, "Bad number of DH bits\n");
1098
1163
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1105
1170
delay = strtof(arg, &tmp);
1106
1171
if(errno != 0 or tmp == arg or *tmp != '\0'){
1107
argp_error(state, "Bad delay");
1172
fprintf(stderr, "Bad delay\n");
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);
1125
1181
return ARGP_ERR_UNKNOWN;
1130
1186
struct argp argp = { .options = options, .parser = parse_opt,
1131
1187
.args_doc = "",
1132
1188
.doc = "Mandos client -- Get and decrypt"
1133
1189
" passwords from a Mandos server" };
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;
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;
1152
1199
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;
1155
1224
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1156
1225
from the signal handler */
1229
1298
/* If the interface is down, bring it up */
1230
if(interface[0] != '\0'){
1299
if(strcmp(interface, "none") != 0){
1231
1300
if_index = (AvahiIfIndex) if_nametoindex(interface);
1232
1301
if(if_index == 0){
1233
1302
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1249
1318
#ifdef __linux__
1250
1319
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1251
messages about the network interface to mess up the prompt */
1320
messages to mess up the prompt */
1252
1321
ret = klogctl(8, NULL, 5);
1253
1322
bool restore_loglevel = true;
1559
1628
perror("ioctl SIOCGIFFLAGS");
1560
1629
} else if(network.ifr_flags & IFF_UP) {
1561
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1630
network.ifr_flags &= ~IFF_UP; /* clear flag */
1562
1631
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1564
1633
perror("ioctl SIOCSIFFLAGS");