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