65
65
#include <arpa/inet.h> /* inet_pton() */
66
66
#include <iso646.h> /* not */
67
67
#include <net/if.h> /* IF_NAMESIZE */
68
#include <argp.h> /* struct argp_option,
69
struct argp_state, struct argp,
72
70
#include <errno.h> /* perror() */
75
76
#define BUFFER_SIZE 256
78
static const char *keydir = "/conf/conf.d/mandos";
79
static const char *pubkeyfile = "pubkey.txt";
80
static const char *seckeyfile = "seckey.txt";
77
82
bool debug = false;
78
static const char *keydir = "/conf/conf.d/mandos";
79
const char *argp_program_version = "mandosclient 0.9";
80
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
81
static const char mandos_protocol_version[] = "1";
83
/* Used for passing in values through the Avahi callback functions */
84
const char mandos_protocol_version[] = "1";
86
/* Used for passing in values through all the callback functions */
85
88
AvahiSimplePoll *simple_poll;
86
89
AvahiServer *server;
87
90
gnutls_certificate_credentials_t cred;
88
91
unsigned int dh_bits;
89
gnutls_dh_params_t dh_params;
90
92
const char *priority;
93
/* Make room in "buffer" for at least BUFFER_SIZE additional bytes.
94
* "buffer_capacity" is how much is currently allocated,
95
* "buffer_length" is how much is already used. */
96
95
size_t adjustbuffer(char **buffer, size_t buffer_length,
97
96
size_t buffer_capacity){
98
97
if (buffer_length + BUFFER_SIZE > buffer_capacity){
340
337
/* GnuTLS server initialization */
341
ret = gnutls_dh_params_init(&mc->dh_params);
338
ret = gnutls_dh_params_init(dh_params);
342
339
if (ret != GNUTLS_E_SUCCESS) {
343
340
fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
344
341
" %s\n", safer_gnutls_strerror(ret));
347
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
344
ret = gnutls_dh_params_generate2(*dh_params, mc->dh_bits);
348
345
if (ret != GNUTLS_E_SUCCESS) {
349
346
fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
350
347
safer_gnutls_strerror(ret));
354
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
359
static int init_gnutls_session(mandos_context *mc,
360
gnutls_session_t *session){
351
gnutls_certificate_set_dh_params(mc->cred, *dh_params);
362
353
/* GnuTLS session creation */
363
354
ret = gnutls_init(session, GNUTLS_SERVER);
364
355
if (ret != GNUTLS_E_SUCCESS){
451
443
fprintf(stderr, "Bad address: %s\n", ip);
454
to.in6.sin6_port = htons(port); /* Spurious warning */
446
to.sin6_port = htons(port); /* Spurious warning */
456
to.in6.sin6_scope_id = (uint32_t)if_index;
448
to.sin6_scope_id = (uint32_t)if_index;
459
451
fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
460
452
char addrstr[INET6_ADDRSTRLEN] = "";
461
if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
453
if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
462
454
sizeof(addrstr)) == NULL){
463
455
perror("inet_ntop");
731
722
const char *interface = "eth0";
732
723
struct ifreq network;
736
725
char *connect_to = NULL;
737
726
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
738
const char *pubkeyfile = "pubkey.txt";
739
const char *seckeyfile = "seckey.txt";
740
727
mandos_context mc = { .simple_poll = NULL, .server = NULL,
741
728
.dh_bits = 1024, .priority = "SECURE256"};
744
struct argp_option options[] = {
745
{ .name = "debug", .key = 128,
746
.doc = "Debug mode", .group = 3 },
747
{ .name = "connect", .key = 'c',
749
.doc = "Connect directly to a sepcified mandos server",
751
{ .name = "interface", .key = 'i',
753
.doc = "Interface that Avahi will conntect through",
755
{ .name = "keydir", .key = 'd',
757
.doc = "Directory where the openpgp keyring is",
759
{ .name = "seckey", .key = 's',
761
.doc = "Secret openpgp key for gnutls authentication",
763
{ .name = "pubkey", .key = 'p',
765
.doc = "Public openpgp key for gnutls authentication",
767
{ .name = "dh-bits", .key = 129,
769
.doc = "dh-bits to use in gnutls communication",
771
{ .name = "priority", .key = 130,
773
.doc = "GNUTLS priority", .group = 1 },
778
error_t parse_opt (int key, char *arg,
779
struct argp_state *state) {
780
/* Get the INPUT argument from `argp_parse', which we know is
781
a pointer to our plugin list pointer. */
731
/* Temporary int to get the address of for getopt_long */
732
int debug_int = debug ? 1 : 0;
734
struct option long_options[] = {
735
{"debug", no_argument, &debug_int, 1},
736
{"connect", required_argument, NULL, 'c'},
737
{"interface", required_argument, NULL, 'i'},
738
{"keydir", required_argument, NULL, 'd'},
739
{"seckey", required_argument, NULL, 's'},
740
{"pubkey", required_argument, NULL, 'p'},
741
{"dh-bits", required_argument, NULL, 'D'},
742
{"priority", required_argument, NULL, 'P'},
745
int option_index = 0;
746
ret = getopt_long (argc, argv, "i:", long_options,
803
mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
773
mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
805
775
perror("strtol");
806
776
exit(EXIT_FAILURE);
780
mc.priority = optarg;
818
return ARGP_ERR_UNKNOWN;
784
/* getopt_long() has already printed a message about the
785
unrcognized option, so just exit. */
823
struct argp argp = { .options = options, .parser = parse_opt,
825
.doc = "Mandos client -- Get and decrypt"
826
" passwords from mandos server" };
827
argp_parse (&argp, argc, argv, 0, 0, NULL);
789
/* Set the global debug flag from the temporary int */
790
debug = debug_int ? true : false;
830
793
pubkeyfile = combinepath(keydir, pubkeyfile);
831
794
if (pubkeyfile == NULL){
832
795
perror("combinepath");