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 */
41
40
#include <stdint.h> /* uint16_t, uint32_t */
42
41
#include <stddef.h> /* NULL, size_t, ssize_t */
43
42
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
61
60
#include <inttypes.h> /* PRIu16, intmax_t, SCNdMAX */
62
61
#include <assert.h> /* assert() */
63
62
#include <errno.h> /* perror(), errno */
64
#include <time.h> /* nanosleep(), time() */
63
#include <time.h> /* time() */
65
64
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
66
65
SIOCSIFFLAGS, if_indextoname(),
67
66
if_nametoindex(), IF_NAMESIZE */
75
74
argp_state, struct argp,
76
75
argp_parse(), ARGP_KEY_ARG,
77
76
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
78
#include <sys/klog.h> /* klogctl() */
81
79
/* All Avahi types, constants and functions
834
832
char *connect_to = NULL;
835
833
char tempdir[] = "/tmp/mandosXXXXXX";
836
bool tempdir_created = false;
837
834
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
838
835
const char *seckey = PATHDIR "/" SECKEY;
839
836
const char *pubkey = PATHDIR "/" PUBKEY;
841
838
mandos_context mc = { .simple_poll = NULL, .server = NULL,
842
839
.dh_bits = 1024, .priority = "SECURE256"
843
840
":!CTYPE-X.509:+CTYPE-OPENPGP" };
844
bool gnutls_initialized = false;
845
bool gpgme_initialized = false;
841
bool gnutls_initalized = false;
842
bool gpgme_initalized = false;
849
845
struct argp_option options[] = {
876
872
.doc = "GnuTLS priority string for the TLS handshake",
878
{ .name = "delay", .key = 131,
880
.doc = "Maximum delay to wait for interface startup",
912
904
case 130: /* --priority */
913
905
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");
922
907
case ARGP_KEY_ARG:
923
908
argp_usage(state);
924
909
case ARGP_KEY_END:
944
929
/* 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);
953
931
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
955
933
perror("socket");
956
934
exitcode = EXIT_FAILURE;
957
ret = klogctl(7, NULL, 0);
963
937
strcpy(network.ifr_name, interface);
964
938
ret = ioctl(sd, SIOCGIFFLAGS, &network);
966
940
perror("ioctl SIOCGIFFLAGS");
967
ret = klogctl(7, NULL, 0);
971
941
exitcode = EXIT_FAILURE;
978
948
perror("ioctl SIOCSIFFLAGS");
979
949
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);
998
953
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1000
955
perror("close");
1002
/* Restores kernel loglevel to default */
1003
ret = klogctl(7, NULL, 0);
1014
969
perror("setgid");
1022
972
ret = init_gnutls_global(&mc, pubkey, seckey);
1024
974
fprintf(stderr, "init_gnutls_global failed\n");
1025
975
exitcode = EXIT_FAILURE;
1028
gnutls_initialized = true;
978
gnutls_initalized = true;
1031
981
if(mkdtemp(tempdir) == NULL){
1032
982
perror("mkdtemp");
1035
tempdir_created = true;
1037
987
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
1038
fprintf(stderr, "init_gpgme failed\n");
988
fprintf(stderr, "gpgme_initalized failed\n");
1039
989
exitcode = EXIT_FAILURE;
1042
gpgme_initialized = true;
992
gpgme_initalized = true;
1045
995
if_index = (AvahiIfIndex) if_nametoindex(interface);
1046
996
if(if_index == 0){
1047
997
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1048
exitcode = EXIT_FAILURE;
1052
1001
if(connect_to != NULL){
1156
1105
if(mc.simple_poll != NULL)
1157
1106
avahi_simple_poll_free(mc.simple_poll);
1159
if(gnutls_initialized){
1108
if(gnutls_initalized){
1160
1109
gnutls_certificate_free_credentials(mc.cred);
1161
1110
gnutls_global_deinit();
1162
1111
gnutls_dh_params_deinit(mc.dh_params);
1165
if(gpgme_initialized){
1114
if(gpgme_initalized){
1166
1115
gpgme_release(mc.ctx);
1169
1118
/* Removes the temp directory used by GPGME */
1170
if(tempdir_created){
1119
if(tempdir[0] != '\0'){
1172
1121
struct dirent *direntry;
1173
1122
d = opendir(tempdir);
1181
1130
if(direntry == NULL){
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,
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));