32
32
#define _LARGEFILE_SOURCE
33
33
#define _FILE_OFFSET_BITS 64
35
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY() */
37
#include <stdio.h> /* fprintf(), stderr, fwrite(), stdout, ferror() */
38
#include <stdint.h> /* uint16_t, uint32_t */
39
#include <stddef.h> /* NULL, size_t, ssize_t */
40
#include <stdlib.h> /* free() */
41
#include <stdbool.h> /* bool, true */
42
#include <string.h> /* memset(), strcmp(), strlen, strerror() */
43
#include <sys/ioctl.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
45
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
46
sockaddr_in6, PF_INET6, SOCK_STREAM, INET6_ADDRSTRLEN */
47
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
48
struct in6_addr, inet_pton(),
50
36
#include <assert.h>
51
#include <errno.h> /* perror() */
39
#include <net/if.h> /* if_nametoindex */
40
#include <sys/ioctl.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
53
42
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
54
SIOCSIFFLAGS, if_indextoname(),
55
if_nametoindex(), IF_NAMESIZE */
56
#include <unistd.h> /* close(), SEEK_SET, off_t, write()*/
57
#include <netinet/in.h>
58
#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,
65
#include <avahi-core/core.h> /* AvahiSimplePoll, AvahiServer,
45
#include <avahi-core/core.h>
67
46
#include <avahi-core/lookup.h>
68
#include <avahi-core/log.h> /* AvahiLogLevel */
47
#include <avahi-core/log.h>
69
48
#include <avahi-common/simple-watch.h>
70
49
#include <avahi-common/malloc.h>
71
50
#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(),
97
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 */
52
/* Mandos client part */
53
#include <sys/types.h> /* socket(), inet_pton() */
54
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
55
struct in6_addr, inet_pton() */
56
#include <gnutls/gnutls.h> /* All GnuTLS stuff */
57
#include <gnutls/openpgp.h> /* GnuTLS with openpgp stuff */
59
#include <unistd.h> /* close() */
60
#include <netinet/in.h>
61
#include <stdbool.h> /* true */
62
#include <string.h> /* memset */
63
#include <arpa/inet.h> /* inet_pton() */
64
#include <iso646.h> /* not */
65
#include <net/if.h> /* IF_NAMESIZE */
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(),
127
GPGME_PROTOCOL_OpenPGP,
129
GPG_ERR_NO_SECKEY, */
68
#include <errno.h> /* perror() */
131
74
#define BUFFER_SIZE 256
76
static const char *keydir = "/conf/conf.d/mandos";
77
static const char *pubkeyfile = "pubkey.txt";
78
static const char *seckeyfile = "seckey.txt";
133
80
bool debug = false;
134
static const char *keydir = "/conf/conf.d/mandos";
135
static const char mandos_protocol_version[] = "1";
136
const char *argp_program_version = "mandosclient 0.9";
137
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
139
/* Used for passing in values through the Avahi callback functions */
82
/* Used for passing in values through all the callback functions */
141
84
AvahiSimplePoll *simple_poll;
142
85
AvahiServer *server;
143
86
gnutls_certificate_credentials_t cred;
144
87
unsigned int dh_bits;
145
gnutls_dh_params_t dh_params;
146
88
const char *priority;
150
* Make room in "buffer" for at least BUFFER_SIZE additional bytes.
151
* "buffer_capacity" is how much is currently allocated,
152
* "buffer_length" is how much is already used.
154
size_t adjustbuffer(char **buffer, size_t buffer_length,
155
size_t buffer_capacity){
156
if (buffer_length + BUFFER_SIZE > buffer_capacity){
157
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
161
buffer_capacity += BUFFER_SIZE;
163
return buffer_capacity;
167
92
* Decrypt OpenPGP data using keyrings in HOMEDIR.
168
93
* Returns -1 on error
393
319
" '%s')\n", ret, pubkeyfile, seckeyfile);
394
320
fprintf(stdout, "The GnuTLS error is: %s\n",
395
321
safer_gnutls_strerror(ret));
399
325
/* GnuTLS server initialization */
400
ret = gnutls_dh_params_init(&mc->dh_params);
326
ret = gnutls_dh_params_init(dh_params);
401
327
if (ret != GNUTLS_E_SUCCESS) {
402
328
fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
403
329
" %s\n", safer_gnutls_strerror(ret));
406
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
332
ret = gnutls_dh_params_generate2(*dh_params, mc->dh_bits);
407
333
if (ret != GNUTLS_E_SUCCESS) {
408
334
fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
409
335
safer_gnutls_strerror(ret));
413
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
419
gnutls_certificate_free_credentials(mc->cred);
420
gnutls_global_deinit();
425
static int init_gnutls_session(mandos_context *mc,
426
gnutls_session_t *session){
339
gnutls_certificate_set_dh_params(mc->cred, *dh_params);
428
341
/* GnuTLS session creation */
429
342
ret = gnutls_init(session, GNUTLS_SERVER);
430
343
if (ret != GNUTLS_E_SUCCESS){
797
688
const char *interface = "eth0";
798
689
struct ifreq network;
802
691
char *connect_to = NULL;
803
692
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
804
const char *pubkeyfile = "pubkey.txt";
805
const char *seckeyfile = "seckey.txt";
806
693
mandos_context mc = { .simple_poll = NULL, .server = NULL,
807
694
.dh_bits = 1024, .priority = "SECURE256"};
808
bool gnutls_initalized = false;
811
struct argp_option options[] = {
812
{ .name = "debug", .key = 128,
813
.doc = "Debug mode", .group = 3 },
814
{ .name = "connect", .key = 'c',
816
.doc = "Connect directly to a sepcified mandos server",
818
{ .name = "interface", .key = 'i',
820
.doc = "Interface that Avahi will conntect through",
822
{ .name = "keydir", .key = 'd',
824
.doc = "Directory where the openpgp keyring is",
826
{ .name = "seckey", .key = 's',
828
.doc = "Secret openpgp key for gnutls authentication",
830
{ .name = "pubkey", .key = 'p',
832
.doc = "Public openpgp key for gnutls authentication",
834
{ .name = "dh-bits", .key = 129,
836
.doc = "dh-bits to use in gnutls communication",
838
{ .name = "priority", .key = 130,
840
.doc = "GNUTLS priority", .group = 1 },
845
error_t parse_opt (int key, char *arg,
846
struct argp_state *state) {
847
/* Get the INPUT argument from `argp_parse', which we know is
848
a pointer to our plugin list pointer. */
697
/* Temporary int to get the address of for getopt_long */
698
int debug_int = debug ? 1 : 0;
700
struct option long_options[] = {
701
{"debug", no_argument, &debug_int, 1},
702
{"connect", required_argument, NULL, 'c'},
703
{"interface", required_argument, NULL, 'i'},
704
{"keydir", required_argument, NULL, 'd'},
705
{"seckey", required_argument, NULL, 's'},
706
{"pubkey", required_argument, NULL, 'p'},
707
{"dh-bits", required_argument, NULL, 'D'},
708
{"priority", required_argument, NULL, 'P'},
711
int option_index = 0;
712
ret = getopt_long (argc, argv, "i:", long_options,
870
mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
739
mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
872
741
perror("strtol");
873
742
exit(EXIT_FAILURE);
746
mc.priority = optarg;
885
return ARGP_ERR_UNKNOWN;
750
/* getopt_long() has already printed a message about the
751
unrcognized option, so just exit. */
890
struct argp argp = { .options = options, .parser = parse_opt,
892
.doc = "Mandos client -- Get and decrypt"
893
" passwords from mandos server" };
894
argp_parse (&argp, argc, argv, 0, 0, NULL);
755
/* Set the global debug flag from the temporary int */
756
debug = debug_int ? true : false;
897
759
pubkeyfile = combinepath(keydir, pubkeyfile);
898
760
if (pubkeyfile == NULL){
899
761
perror("combinepath");