65
63
#include <arpa/inet.h> /* inet_pton() */
66
64
#include <iso646.h> /* not */
67
65
#include <net/if.h> /* IF_NAMESIZE */
68
#include <argp.h> /* struct argp_option,
69
struct argp_state, struct argp,
72
68
#include <errno.h> /* perror() */
75
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";
77
80
bool debug = false;
78
static const char *keydir = "/conf/conf.d/mandos";
79
static const char mandos_protocol_version[] = "1";
80
const char *argp_program_version = "mandosclient 0.9";
81
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
83
/* Used for passing in values through the Avahi callback functions */
82
/* Used for passing in values through all the callback functions */
85
84
AvahiSimplePoll *simple_poll;
86
85
AvahiServer *server;
87
86
gnutls_certificate_credentials_t cred;
88
87
unsigned int dh_bits;
89
gnutls_dh_params_t dh_params;
90
88
const char *priority;
94
* Make room in "buffer" for at least BUFFER_SIZE additional bytes.
95
* "buffer_capacity" is how much is currently allocated,
96
* "buffer_length" is how much is already used.
98
size_t adjustbuffer(char **buffer, size_t buffer_length,
99
size_t buffer_capacity){
100
if (buffer_length + BUFFER_SIZE > buffer_capacity){
101
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
105
buffer_capacity += BUFFER_SIZE;
107
return buffer_capacity;
111
92
* Decrypt OpenPGP data using keyrings in HOMEDIR.
112
93
* Returns -1 on error
342
325
/* GnuTLS server initialization */
343
ret = gnutls_dh_params_init(&mc->dh_params);
326
ret = gnutls_dh_params_init(dh_params);
344
327
if (ret != GNUTLS_E_SUCCESS) {
345
328
fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
346
329
" %s\n", safer_gnutls_strerror(ret));
349
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
332
ret = gnutls_dh_params_generate2(*dh_params, mc->dh_bits);
350
333
if (ret != GNUTLS_E_SUCCESS) {
351
334
fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
352
335
safer_gnutls_strerror(ret));
356
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
361
static int init_gnutls_session(mandos_context *mc,
362
gnutls_session_t *session){
339
gnutls_certificate_set_dh_params(mc->cred, *dh_params);
364
341
/* GnuTLS session creation */
365
342
ret = gnutls_init(session, GNUTLS_SERVER);
366
343
if (ret != GNUTLS_E_SUCCESS){
453
431
fprintf(stderr, "Bad address: %s\n", ip);
456
to.in6.sin6_port = htons(port); /* Spurious warning */
434
to.sin6_port = htons(port); /* Spurious warning */
458
to.in6.sin6_scope_id = (uint32_t)if_index;
436
to.sin6_scope_id = (uint32_t)if_index;
461
439
fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
462
440
char addrstr[INET6_ADDRSTRLEN] = "";
463
if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
441
if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
464
442
sizeof(addrstr)) == NULL){
465
443
perror("inet_ntop");
733
688
const char *interface = "eth0";
734
689
struct ifreq network;
738
691
char *connect_to = NULL;
739
692
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
740
const char *pubkeyfile = "pubkey.txt";
741
const char *seckeyfile = "seckey.txt";
742
693
mandos_context mc = { .simple_poll = NULL, .server = NULL,
743
694
.dh_bits = 1024, .priority = "SECURE256"};
746
struct argp_option options[] = {
747
{ .name = "debug", .key = 128,
748
.doc = "Debug mode", .group = 3 },
749
{ .name = "connect", .key = 'c',
751
.doc = "Connect directly to a sepcified mandos server",
753
{ .name = "interface", .key = 'i',
755
.doc = "Interface that Avahi will conntect through",
757
{ .name = "keydir", .key = 'd',
759
.doc = "Directory where the openpgp keyring is",
761
{ .name = "seckey", .key = 's',
763
.doc = "Secret openpgp key for gnutls authentication",
765
{ .name = "pubkey", .key = 'p',
767
.doc = "Public openpgp key for gnutls authentication",
769
{ .name = "dh-bits", .key = 129,
771
.doc = "dh-bits to use in gnutls communication",
773
{ .name = "priority", .key = 130,
775
.doc = "GNUTLS priority", .group = 1 },
780
error_t parse_opt (int key, char *arg,
781
struct argp_state *state) {
782
/* Get the INPUT argument from `argp_parse', which we know is
783
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,
805
mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
739
mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
807
741
perror("strtol");
808
742
exit(EXIT_FAILURE);
746
mc.priority = optarg;
820
return ARGP_ERR_UNKNOWN;
750
/* getopt_long() has already printed a message about the
751
unrcognized option, so just exit. */
825
struct argp argp = { .options = options, .parser = parse_opt,
827
.doc = "Mandos client -- Get and decrypt"
828
" passwords from mandos server" };
829
argp_parse (&argp, argc, argv, 0, 0, NULL);
755
/* Set the global debug flag from the temporary int */
756
debug = debug_int ? true : false;
832
759
pubkeyfile = combinepath(keydir, pubkeyfile);
833
760
if (pubkeyfile == NULL){
834
761
perror("combinepath");