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,
60
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
832
834
char *connect_to = NULL;
833
835
char tempdir[] = "/tmp/mandosXXXXXX";
836
bool tempdir_created = false;
834
837
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
835
838
const char *seckey = PATHDIR "/" SECKEY;
836
839
const char *pubkey = PATHDIR "/" PUBKEY;
838
841
mandos_context mc = { .simple_poll = NULL, .server = NULL,
839
842
.dh_bits = 1024, .priority = "SECURE256"
840
843
":!CTYPE-X.509:+CTYPE-OPENPGP" };
841
bool gnutls_initalized = false;
842
bool gpgme_initalized = false;
844
bool gnutls_initialized = false;
845
bool gpgme_initialized = false;
845
849
struct argp_option options[] = {
872
876
.doc = "GnuTLS priority string for the TLS handshake",
878
{ .name = "delay", .key = 131,
880
.doc = "Maximum delay to wait for interface startup",
904
912
case 130: /* --priority */
905
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");
907
922
case ARGP_KEY_ARG:
908
923
argp_usage(state);
909
924
case ARGP_KEY_END:
929
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);
931
953
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
933
955
perror("socket");
934
956
exitcode = EXIT_FAILURE;
957
ret = klogctl(7, NULL, 0);
937
963
strcpy(network.ifr_name, interface);
938
964
ret = ioctl(sd, SIOCGIFFLAGS, &network);
940
966
perror("ioctl SIOCGIFFLAGS");
967
ret = klogctl(7, NULL, 0);
941
971
exitcode = EXIT_FAILURE;
948
978
perror("ioctl SIOCSIFFLAGS");
949
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);
953
998
ret = (int)TEMP_FAILURE_RETRY(close(sd));
955
1000
perror("close");
1002
/* Restores kernel loglevel to default */
1003
ret = klogctl(7, NULL, 0);
962
1017
ret = setuid(uid);
964
1019
perror("setuid");
972
1022
ret = init_gnutls_global(&mc, pubkey, seckey);
974
1024
fprintf(stderr, "init_gnutls_global failed\n");
975
1025
exitcode = EXIT_FAILURE;
978
gnutls_initalized = true;
1028
gnutls_initialized = true;
981
1031
if(mkdtemp(tempdir) == NULL){
982
1032
perror("mkdtemp");
1035
tempdir_created = true;
987
1037
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
988
fprintf(stderr, "gpgme_initalized failed\n");
1038
fprintf(stderr, "init_gpgme failed\n");
989
1039
exitcode = EXIT_FAILURE;
992
gpgme_initalized = true;
1042
gpgme_initialized = true;
995
1045
if_index = (AvahiIfIndex) if_nametoindex(interface);
996
1046
if(if_index == 0){
997
1047
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1048
exitcode = EXIT_FAILURE;
1001
1052
if(connect_to != NULL){
1105
1156
if(mc.simple_poll != NULL)
1106
1157
avahi_simple_poll_free(mc.simple_poll);
1108
if(gnutls_initalized){
1159
if(gnutls_initialized){
1109
1160
gnutls_certificate_free_credentials(mc.cred);
1110
1161
gnutls_global_deinit();
1111
1162
gnutls_dh_params_deinit(mc.dh_params);
1114
if(gpgme_initalized){
1165
if(gpgme_initialized){
1115
1166
gpgme_release(mc.ctx);
1118
1169
/* Removes the temp directory used by GPGME */
1119
if(tempdir[0] != '\0'){
1170
if(tempdir_created){
1121
1172
struct dirent *direntry;
1122
1173
d = opendir(tempdir);
1130
1181
if(direntry == NULL){
1133
if(direntry->d_type == DT_REG){
1134
char *fullname = NULL;
1135
ret = asprintf(&fullname, "%s/%s", tempdir,
1141
ret = unlink(fullname);
1143
fprintf(stderr, "unlink(\"%s\"): %s",
1144
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,