32
32
#define _LARGEFILE_SOURCE
33
33
#define _FILE_OFFSET_BITS 64
35
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY() */
35
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
37
#include <stdio.h> /* fprintf(), stderr, fwrite(), stdout, ferror() */
37
#include <stdio.h> /* fprintf(), stderr, fwrite(),
38
39
#include <stdint.h> /* uint16_t, uint32_t */
39
40
#include <stddef.h> /* NULL, size_t, ssize_t */
40
#include <stdlib.h> /* free() */
41
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
41
43
#include <stdbool.h> /* bool, true */
42
#include <string.h> /* memset(), strcmp(), strlen, strerror() */
43
#include <sys/ioctl.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
44
#include <string.h> /* memset(), strcmp(), strlen(),
45
strerror(), asprintf(), strcpy() */
46
#include <sys/ioctl.h> /* ioctl */
45
47
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
46
sockaddr_in6, PF_INET6, SOCK_STREAM, INET6_ADDRSTRLEN */
48
sockaddr_in6, PF_INET6,
49
SOCK_STREAM, INET6_ADDRSTRLEN,
51
#include <inttypes.h> /* PRIu16 */
47
52
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
48
53
struct in6_addr, inet_pton(),
51
#include <errno.h> /* perror() */
55
#include <assert.h> /* assert() */
56
#include <errno.h> /* perror(), errno */
57
#include <time.h> /* time() */
53
58
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
54
59
SIOCSIFFLAGS, if_indextoname(),
55
60
if_nametoindex(), IF_NAMESIZE */
56
#include <unistd.h> /* close(), SEEK_SET, off_t, write()*/
61
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
62
getuid(), getgid(), setuid(),
57
64
#include <netinet/in.h>
58
65
#include <arpa/inet.h> /* inet_pton(), htons */
59
#include <iso646.h> /* not */
60
#include <argp.h> /* struct argp_option,
61
struct argp_state, struct argp,
66
#include <iso646.h> /* not, and */
67
#include <argp.h> /* struct argp_option, error_t, struct
68
argp_state, struct argp,
69
argp_parse(), ARGP_KEY_ARG,
70
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
65
#include <avahi-core/core.h> /* AvahiSimplePoll, AvahiServer,
73
/* All Avahi types, constants and functions
76
#include <avahi-core/core.h>
67
77
#include <avahi-core/lookup.h>
68
#include <avahi-core/log.h> /* AvahiLogLevel */
78
#include <avahi-core/log.h>
69
79
#include <avahi-common/simple-watch.h>
70
80
#include <avahi-common/malloc.h>
71
81
#include <avahi-common/error.h>
74
#include <gnutls/gnutls.h> /* gnutls_certificate_credentials_t,
78
gnutls_global_set_log_level(),
79
gnutls_global_set_log_function(),
80
gnutls_certificate_allocate_credentials(),
81
gnutls_global_deinit(),
82
gnutls_dh_params_init(),
83
gnutls_dh_params_generate(),
84
gnutls_certificate_set_dh_params(),
85
gnutls_certificate_free_credentials(),
86
gnutls_session_t, gnutls_init(),
87
gnutls_priority_set_direct(),
89
gnutls_credentials_set(),
90
gnutls_certificate_server_set_request(),
91
gnutls_dh_set_prime_bits(),
92
gnutls_transport_set_ptr(),
93
gnutls_transport_ptr_t,
96
gnutls_perror(), gnutls_bye(),
84
#include <gnutls/gnutls.h> /* All GnuTLS types, constants and
97
87
init_gnutls_session(),
99
GNUTLS_CRD_CERTIFICATE,
101
GNUTLS_E_INTERRUPTED,
103
GNUTLS_E_REHANDSHAKE,
105
#include <gnutls/openpgp.h> /* gnutls_certificate_set_openpgp_key_file(),
106
GNUTLS_OPENPGP_FMT_BASE64 */
89
#include <gnutls/openpgp.h> /* gnutls_certificate_set_openpgp_key_file(),
90
GNUTLS_OPENPGP_FMT_BASE64 */
109
#include <gpgme.h> /* gpgme_data_t, gpgme_ctx_t,
110
gpgme_error_t, gpgme_engine_info_t,
111
gpgme_check_version(),
112
gpgme_engine_check_version(),
115
gpgme_get_engine_info(),
116
gpgme_set_engine_info(),
117
gpgme_data_new_from_mem(),
118
gpgme_data_new(), gpgme_new(),
120
gpgme_decrypt_result_t,
121
gpgme_op_decrypt_result(),
123
gpgme_pubkey_algo_name(),
93
#include <gpgme.h> /* All GPGME types, constants and
127
96
GPGME_PROTOCOL_OpenPGP,
129
GPG_ERR_NO_SECKEY, */
131
99
#define BUFFER_SIZE 256
133
101
bool debug = false;
134
102
static const char *keydir = "/conf/conf.d/mandos";
135
103
static const char mandos_protocol_version[] = "1";
136
const char *argp_program_version = "mandosclient 0.9";
104
const char *argp_program_version = "password-request 1.0";
137
105
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
139
107
/* Used for passing in values through the Avahi callback functions */
383
351
fprintf(stderr, "Attempting to use OpenPGP certificate %s"
384
" and keyfile %s as GnuTLS credentials\n", pubkeyfile,
352
" and keyfile %s as GnuTLS credentials\n", pubkeyfilename,
388
356
ret = gnutls_certificate_set_openpgp_key_file
389
(mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
357
(mc->cred, pubkeyfilename, seckeyfilename,
358
GNUTLS_OPENPGP_FMT_BASE64);
390
359
if (ret != GNUTLS_E_SUCCESS) {
392
361
"Error[%d] while reading the OpenPGP key pair ('%s',"
393
" '%s')\n", ret, pubkeyfile, seckeyfile);
362
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
394
363
fprintf(stdout, "The GnuTLS error is: %s\n",
395
364
safer_gnutls_strerror(ret));
892
859
.doc = "Mandos client -- Get and decrypt"
893
860
" passwords from mandos server" };
894
argp_parse (&argp, argc, argv, 0, 0, NULL);
861
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
862
if (ret == ARGP_ERR_UNKNOWN){
863
fprintf(stderr, "Unknown error while parsing arguments\n");
864
exitcode = EXIT_FAILURE;
897
pubkeyfile = combinepath(keydir, pubkeyfile);
898
if (pubkeyfile == NULL){
869
pubkeyfilename = combinepath(keydir, pubkeyname);
870
if (pubkeyfilename == NULL){
899
871
perror("combinepath");
900
872
exitcode = EXIT_FAILURE;
904
seckeyfile = combinepath(keydir, seckeyfile);
905
if (seckeyfile == NULL){
876
seckeyfilename = combinepath(keydir, seckeyname);
877
if (seckeyfilename == NULL){
906
878
perror("combinepath");
879
exitcode = EXIT_FAILURE;
910
ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
883
ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
912
fprintf(stderr, "init_gnutls_global\n");
885
fprintf(stderr, "init_gnutls_global failed\n");
886
exitcode = EXIT_FAILURE;
915
889
gnutls_initalized = true;
892
/* If the interface is down, bring it up */
894
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
897
exitcode = EXIT_FAILURE;
900
strcpy(network.ifr_name, interface); /* Spurious warning */
901
ret = ioctl(sd, SIOCGIFFLAGS, &network);
903
perror("ioctl SIOCGIFFLAGS");
904
exitcode = EXIT_FAILURE;
907
if((network.ifr_flags & IFF_UP) == 0){
908
network.ifr_flags |= IFF_UP;
909
ret = ioctl(sd, SIOCSIFFLAGS, &network);
911
perror("ioctl SIOCSIFFLAGS");
912
exitcode = EXIT_FAILURE;
921
922
ret = setuid(uid);
923
924
perror("setuid");