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
754
756
avahi_address_snprint(ip, sizeof(ip), address);
756
758
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
757
PRIu16 ") on port %d\n", name, host_name, ip,
759
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
760
ip, (intmax_t)interface, port);
760
762
int ret = start_mandos_communication(ip, port, interface, mc);
830
834
char *connect_to = NULL;
831
835
char tempdir[] = "/tmp/mandosXXXXXX";
836
bool tempdir_created = false;
832
837
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
833
838
const char *seckey = PATHDIR "/" SECKEY;
834
839
const char *pubkey = PATHDIR "/" PUBKEY;
836
841
mandos_context mc = { .simple_poll = NULL, .server = NULL,
837
842
.dh_bits = 1024, .priority = "SECURE256"
838
843
":!CTYPE-X.509:+CTYPE-OPENPGP" };
839
bool gnutls_initalized = false;
840
bool gpgme_initalized = false;
844
bool gnutls_initialized = false;
845
bool gpgme_initialized = false;
843
849
struct argp_option options[] = {
870
876
.doc = "GnuTLS priority string for the TLS handshake",
878
{ .name = "delay", .key = 131,
880
.doc = "Maximum delay to wait for interface startup",
893
903
case 129: /* --dh-bits */
894
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'){
896
907
fprintf(stderr, "Bad number of DH bits\n");
897
908
exit(EXIT_FAILURE);
910
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
900
912
case 130: /* --priority */
901
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");
903
922
case ARGP_KEY_ARG:
904
923
argp_usage(state);
905
924
case ARGP_KEY_END:
925
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);
927
953
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
929
955
perror("socket");
930
956
exitcode = EXIT_FAILURE;
957
ret = klogctl(7, NULL, 0);
933
963
strcpy(network.ifr_name, interface);
934
964
ret = ioctl(sd, SIOCGIFFLAGS, &network);
936
966
perror("ioctl SIOCGIFFLAGS");
967
ret = klogctl(7, NULL, 0);
937
971
exitcode = EXIT_FAILURE;
944
978
perror("ioctl SIOCSIFFLAGS");
945
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);
949
998
ret = (int)TEMP_FAILURE_RETRY(close(sd));
951
1000
perror("close");
1002
/* Restores kernel loglevel to default */
1003
ret = klogctl(7, NULL, 0);
958
1017
ret = setuid(uid);
960
1019
perror("setuid");
968
1022
ret = init_gnutls_global(&mc, pubkey, seckey);
970
1024
fprintf(stderr, "init_gnutls_global failed\n");
971
1025
exitcode = EXIT_FAILURE;
974
gnutls_initalized = true;
1028
gnutls_initialized = true;
977
1031
if(mkdtemp(tempdir) == NULL){
978
1032
perror("mkdtemp");
1035
tempdir_created = true;
983
1037
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
984
fprintf(stderr, "gpgme_initalized failed\n");
1038
fprintf(stderr, "init_gpgme failed\n");
985
1039
exitcode = EXIT_FAILURE;
988
gpgme_initalized = true;
1042
gpgme_initialized = true;
991
1045
if_index = (AvahiIfIndex) if_nametoindex(interface);
992
1046
if(if_index == 0){
993
1047
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1048
exitcode = EXIT_FAILURE;
997
1052
if(connect_to != NULL){
1007
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'){
1009
1065
fprintf(stderr, "Bad port number\n");
1010
1066
exitcode = EXIT_FAILURE;
1069
port = (uint16_t)tmpmax;
1013
1070
*address = '\0';
1014
1071
address = connect_to;
1015
1072
ret = start_mandos_communication(address, port, if_index, &mc);
1099
1156
if(mc.simple_poll != NULL)
1100
1157
avahi_simple_poll_free(mc.simple_poll);
1102
if(gnutls_initalized){
1159
if(gnutls_initialized){
1103
1160
gnutls_certificate_free_credentials(mc.cred);
1104
1161
gnutls_global_deinit();
1105
1162
gnutls_dh_params_deinit(mc.dh_params);
1108
if(gpgme_initalized){
1165
if(gpgme_initialized){
1109
1166
gpgme_release(mc.ctx);
1112
1169
/* Removes the temp directory used by GPGME */
1113
if(tempdir[0] != '\0'){
1170
if(tempdir_created){
1115
1172
struct dirent *direntry;
1116
1173
d = opendir(tempdir);
1124
1181
if(direntry == NULL){
1127
if(direntry->d_type == DT_REG){
1128
char *fullname = NULL;
1129
ret = asprintf(&fullname, "%s/%s", tempdir,
1135
ret = unlink(fullname);
1137
fprintf(stderr, "unlink(\"%s\"): %s",
1138
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,