36
36
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
38
38
#include <stdio.h> /* fprintf(), stderr, fwrite(),
39
stdout, ferror(), sscanf */
39
stdout, ferror(), sscanf(),
40
41
#include <stdint.h> /* uint16_t, uint32_t */
41
42
#include <stddef.h> /* NULL, size_t, ssize_t */
42
43
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
57
58
#include <fcntl.h> /* open() */
58
59
#include <dirent.h> /* opendir(), struct dirent, readdir()
60
#include <inttypes.h> /* PRIu16, SCNu16 */
61
#include <inttypes.h> /* PRIu16, intmax_t, SCNdMAX */
61
62
#include <assert.h> /* assert() */
62
63
#include <errno.h> /* perror(), errno */
63
#include <time.h> /* time() */
64
#include <time.h> /* nanosleep(), time() */
64
65
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
65
66
SIOCSIFFLAGS, if_indextoname(),
66
67
if_nametoindex(), IF_NAMESIZE */
74
75
argp_state, struct argp,
75
76
argp_parse(), ARGP_KEY_ARG,
76
77
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
78
#include <sys/klog.h> /* klogctl() */
79
81
/* All Avahi types, constants and functions
404
407
/* OpenPGP credentials */
405
408
gnutls_certificate_allocate_credentials(&mc->cred);
406
409
if(ret != GNUTLS_E_SUCCESS){
407
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious
410
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
409
414
safer_gnutls_strerror(ret));
410
415
gnutls_global_deinit();
553
558
fprintf(stderr, "Bad address: %s\n", ip);
556
to.in6.sin6_port = htons(port); /* Spurious warning */
561
to.in6.sin6_port = htons(port); /* Spurious warnings from
563
-Wunreachable-code */
558
565
to.in6.sin6_scope_id = (uint32_t)if_index;
749
756
avahi_address_snprint(ip, sizeof(ip), address);
751
758
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
752
PRIu16 ") on port %d\n", name, host_name, ip,
759
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
760
ip, (intmax_t)interface, port);
755
762
int ret = start_mandos_communication(ip, port, interface, mc);
825
834
char *connect_to = NULL;
826
835
char tempdir[] = "/tmp/mandosXXXXXX";
836
bool tempdir_created = false;
827
837
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
828
838
const char *seckey = PATHDIR "/" SECKEY;
829
839
const char *pubkey = PATHDIR "/" PUBKEY;
831
841
mandos_context mc = { .simple_poll = NULL, .server = NULL,
832
842
.dh_bits = 1024, .priority = "SECURE256"
833
843
":!CTYPE-X.509:+CTYPE-OPENPGP" };
834
bool gnutls_initalized = false;
835
bool gpgme_initalized = false;
844
bool gnutls_initialized = false;
845
bool gpgme_initialized = false;
838
849
struct argp_option options[] = {
865
876
.doc = "GnuTLS priority string for the TLS handshake",
878
{ .name = "delay", .key = 131,
880
.doc = "Maximum delay to wait for interface startup",
888
903
case 129: /* --dh-bits */
889
ret = sscanf(arg, "%u", &mc.dh_bits);
904
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
905
if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
906
or arg[numchars] != '\0'){
891
907
fprintf(stderr, "Bad number of DH bits\n");
892
908
exit(EXIT_FAILURE);
910
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
895
912
case 130: /* --priority */
896
913
mc.priority = arg;
915
case 131: /* --delay */
916
ret = sscanf(arg, "%lf%n", &delay, &numchars);
917
if(ret < 1 or arg[numchars] != '\0'){
918
fprintf(stderr, "Bad delay\n");
898
922
case ARGP_KEY_ARG:
899
923
argp_usage(state);
900
924
case ARGP_KEY_END:
920
944
/* If the interface is down, bring it up */
946
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
947
messages to mess up the prompt */
948
ret = klogctl(8, NULL, 5);
922
953
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
924
955
perror("socket");
925
956
exitcode = EXIT_FAILURE;
957
ret = klogctl(7, NULL, 0);
928
963
strcpy(network.ifr_name, interface);
929
964
ret = ioctl(sd, SIOCGIFFLAGS, &network);
931
966
perror("ioctl SIOCGIFFLAGS");
967
ret = klogctl(7, NULL, 0);
932
971
exitcode = EXIT_FAILURE;
939
978
perror("ioctl SIOCSIFFLAGS");
940
979
exitcode = EXIT_FAILURE;
980
ret = klogctl(7, NULL, 0);
987
/* sleep checking until interface is running */
988
for(int i=0; i < delay * 4; i++){
989
ret = ioctl(sd, SIOCGIFFLAGS, &network);
991
perror("ioctl SIOCGIFFLAGS");
992
} else if(network.ifr_flags & IFF_RUNNING){
995
struct timespec sleeptime = { .tv_nsec = 250000000 };
996
nanosleep(&sleeptime, NULL);
944
998
ret = (int)TEMP_FAILURE_RETRY(close(sd));
946
1000
perror("close");
1002
/* Restores kernel loglevel to default */
1003
ret = klogctl(7, NULL, 0);
953
1017
ret = setuid(uid);
955
1019
perror("setuid");
963
1022
ret = init_gnutls_global(&mc, pubkey, seckey);
965
1024
fprintf(stderr, "init_gnutls_global failed\n");
966
1025
exitcode = EXIT_FAILURE;
969
gnutls_initalized = true;
1028
gnutls_initialized = true;
972
1031
if(mkdtemp(tempdir) == NULL){
973
1032
perror("mkdtemp");
1035
tempdir_created = true;
978
1037
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
979
fprintf(stderr, "gpgme_initalized failed\n");
1038
fprintf(stderr, "init_gpgme failed\n");
980
1039
exitcode = EXIT_FAILURE;
983
gpgme_initalized = true;
1042
gpgme_initialized = true;
986
1045
if_index = (AvahiIfIndex) if_nametoindex(interface);
987
1046
if(if_index == 0){
988
1047
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1048
exitcode = EXIT_FAILURE;
992
1052
if(connect_to != NULL){
1002
ret = sscanf(address+1, "%" SCNu16, &port);
1062
ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1063
if(ret < 1 or tmpmax != (uint16_t)tmpmax
1064
or address[numchars+1] != '\0'){
1004
1065
fprintf(stderr, "Bad port number\n");
1005
1066
exitcode = EXIT_FAILURE;
1069
port = (uint16_t)tmpmax;
1008
1070
*address = '\0';
1009
1071
address = connect_to;
1010
1072
ret = start_mandos_communication(address, port, if_index, &mc);
1094
1156
if(mc.simple_poll != NULL)
1095
1157
avahi_simple_poll_free(mc.simple_poll);
1097
if(gnutls_initalized){
1159
if(gnutls_initialized){
1098
1160
gnutls_certificate_free_credentials(mc.cred);
1099
1161
gnutls_global_deinit();
1100
1162
gnutls_dh_params_deinit(mc.dh_params);
1103
if(gpgme_initalized){
1165
if(gpgme_initialized){
1104
1166
gpgme_release(mc.ctx);
1107
1169
/* Removes the temp directory used by GPGME */
1108
if(tempdir[0] != '\0'){
1170
if(tempdir_created){
1110
1172
struct dirent *direntry;
1111
1173
d = opendir(tempdir);
1119
1181
if(direntry == NULL){
1122
if(direntry->d_type == DT_REG){
1123
char *fullname = NULL;
1124
ret = asprintf(&fullname, "%s/%s", tempdir,
1130
ret = unlink(fullname);
1132
fprintf(stderr, "unlink(\"%s\"): %s",
1133
fullname, strerror(errno));
1184
/* Skip "." and ".." */
1185
if(direntry->d_name[0] == '.'
1186
and (direntry->d_name[1] == '\0'
1187
or (direntry->d_name[1] == '.'
1188
and direntry->d_name[2] == '\0'))){
1191
char *fullname = NULL;
1192
ret = asprintf(&fullname, "%s/%s", tempdir,
1198
ret = remove(fullname);
1200
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,