44
44
#include <stdint.h> /* uint16_t, uint32_t */
45
45
#include <stddef.h> /* NULL, size_t, ssize_t */
46
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() */
73
73
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
74
74
getuid(), getgid(), seteuid(),
76
76
#include <arpa/inet.h> /* inet_pton(), htons */
77
77
#include <iso646.h> /* not, or, and */
78
78
#include <argp.h> /* struct argp_option, error_t, struct
125
125
static const char mandos_protocol_version[] = "1";
126
126
const char *argp_program_version = "mandos-client " VERSION;
127
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;
129
131
/* Used for passing in values through the Avahi callback functions */
142
144
.dh_bits = 1024, .priority = "SECURE256"
143
145
":!CTYPE-X.509:+CTYPE-OPENPGP" };
147
sig_atomic_t quit_now = 0;
148
int signal_received = 0;
146
151
* Make additional room in "buffer" for at least BUFFER_SIZE more
147
152
* bytes. "buffer_capacity" is how much is currently allocated,
476
481
/* GnuTLS session creation */
478
483
ret = gnutls_init(session, GNUTLS_SERVER);
479
487
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
480
488
if(ret != GNUTLS_E_SUCCESS){
481
489
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
488
496
ret = gnutls_priority_set_direct(*session, mc.priority, &err);
498
gnutls_deinit(*session);
489
501
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
490
502
if(ret != GNUTLS_E_SUCCESS){
491
503
fprintf(stderr, "Syntax error at: %s\n", err);
500
512
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
515
gnutls_deinit(*session);
502
518
} while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
503
519
if(ret != GNUTLS_E_SUCCESS){
504
520
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
519
535
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
520
536
__attribute__((unused)) const char *txt){}
522
sig_atomic_t quit_now = 0;
523
int signal_received = 0;
525
538
/* Called when a Mandos server is found */
526
539
static int start_mandos_communication(const char *ip, uint16_t port,
527
540
AvahiIfIndex if_index,
533
546
struct sockaddr_in6 in6;
535
548
char *buffer = NULL;
536
char *decrypted_buffer;
549
char *decrypted_buffer = NULL;
537
550
size_t buffer_length = 0;
538
551
size_t buffer_capacity = 0;
541
554
gnutls_session_t session;
542
555
int pf; /* Protocol family */
607
617
if(if_index == AVAHI_IF_UNSPEC){
608
618
fprintf(stderr, "An IPv6 link-local address is incomplete"
609
619
" without a network interface\n");
613
622
/* Set the network interface number as scope */
779
784
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
780
785
gnutls_perror(ret);
786
790
fprintf(stderr, "Unknown error while reading data from"
787
791
" encrypted session with Mandos server\n");
789
792
gnutls_bye(session, GNUTLS_SHUT_RDWR);
805
gnutls_bye(session, GNUTLS_SHUT_RDWR);
809
ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
813
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
811
815
if(buffer_length > 0){
812
816
ssize_t decrypted_buffer_size;
976
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 */
979
1053
int main(int argc, char *argv[]){
980
1054
AvahiSServiceBrowser *sb = NULL;
983
1057
intmax_t tmpmax;
985
1059
int exitcode = EXIT_SUCCESS;
986
const char *interface = "eth0";
1060
const char *interface = "";
987
1061
struct ifreq network;
989
1063
bool take_down_interface = false;
992
char *connect_to = NULL;
993
1066
char tempdir[] = "/tmp/mandosXXXXXX";
994
1067
bool tempdir_created = false;
995
1068
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1126
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;
1129
1224
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1130
1225
from the signal handler */
1203
1298
/* If the interface is down, bring it up */
1204
if(interface[0] != '\0'){
1299
if(strcmp(interface, "none") != 0){
1205
1300
if_index = (AvahiIfIndex) if_nametoindex(interface);
1206
1301
if(if_index == 0){
1207
1302
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1599
1694
sigemptyset(&old_sigterm_action.sa_mask);
1600
1695
old_sigterm_action.sa_handler = SIG_DFL;
1601
ret = sigaction(signal_received, &old_sigterm_action, NULL);
1696
ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
1697
&old_sigterm_action,
1603
1700
perror("sigaction");
1605
raise(signal_received);
1703
ret = raise(signal_received);
1704
} while(ret != 0 and errno == EINTR);
1709
TEMP_FAILURE_RETRY(pause());
1608
1712
return exitcode;