75
75
#define BUFFER_SIZE 256
77
77
bool debug = false;
78
static const char *keydir = "/conf/conf.d/mandos";
79
static const char mandos_protocol_version[] = "1";
78
const char *keydir = "/conf/conf.d/mandos";
80
79
const char *argp_program_version = "mandosclient 0.9";
81
80
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
81
const char mandos_protocol_version[] = "1";
83
/* Used for passing in values through the Avahi callback functions */
83
/* Used for passing in values through all the callback functions */
85
85
AvahiSimplePoll *simple_poll;
86
86
AvahiServer *server;
90
90
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
93
size_t adjustbuffer(char **buffer, size_t buffer_length,
99
94
size_t buffer_capacity){
100
95
if (buffer_length + BUFFER_SIZE > buffer_capacity){
236
231
*plaintext = NULL;
238
plaintext_capacity = adjustbuffer(plaintext,
239
(size_t)plaintext_length,
233
plaintext_capacity = adjustbuffer(plaintext, (size_t)plaintext_length,
240
234
plaintext_capacity);
241
235
if (plaintext_capacity == 0){
242
236
perror("adjustbuffer");
337
330
" '%s')\n", ret, pubkeyfile, seckeyfile);
338
331
fprintf(stdout, "The GnuTLS error is: %s\n",
339
332
safer_gnutls_strerror(ret));
343
336
/* GnuTLS server initialization */
345
338
if (ret != GNUTLS_E_SUCCESS) {
346
339
fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
347
340
" %s\n", safer_gnutls_strerror(ret));
350
343
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
351
344
if (ret != GNUTLS_E_SUCCESS) {
352
345
fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
353
346
safer_gnutls_strerror(ret));
357
350
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
363
gnutls_certificate_free_credentials (mc->cred);
364
gnutls_global_deinit ();
369
static int init_gnutls_session(mandos_context *mc,
370
gnutls_session_t *session){
355
static int init_gnutls_session(mandos_context *mc, gnutls_session_t *session){
372
357
/* GnuTLS session creation */
373
358
ret = gnutls_init(session, GNUTLS_SERVER);
383
368
fprintf(stderr, "Syntax error at: %s\n", err);
384
369
fprintf(stderr, "GnuTLS error: %s\n",
385
370
safer_gnutls_strerror(ret));
386
gnutls_deinit (*session);
393
377
if (ret != GNUTLS_E_SUCCESS) {
394
378
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
395
379
safer_gnutls_strerror(ret));
396
gnutls_deinit (*session);
426
409
char interface[IF_NAMESIZE];
427
410
gnutls_session_t session;
411
gnutls_dh_params_t dh_params;
429
413
ret = init_gnutls_session (mc, &session);
453
437
memset(&to,0,sizeof(to)); /* Spurious warning */
454
to.in6.sin6_family = AF_INET6;
438
to.sin6_family = AF_INET6;
455
439
/* It would be nice to have a way to detect if we were passed an
456
440
IPv4 address here. Now we assume an IPv6 address. */
457
ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
441
ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
459
443
perror("inet_pton");
463
447
fprintf(stderr, "Bad address: %s\n", ip);
466
to.in6.sin6_port = htons(port); /* Spurious warning */
450
to.sin6_port = htons(port); /* Spurious warning */
468
to.in6.sin6_scope_id = (uint32_t)if_index;
452
to.sin6_scope_id = (uint32_t)if_index;
471
455
fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
472
456
char addrstr[INET6_ADDRSTRLEN] = "";
473
if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
457
if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
474
458
sizeof(addrstr)) == NULL){
475
459
perror("inet_ntop");
538
buffer_capacity = adjustbuffer(&buffer, buffer_length,
522
buffer_capacity = adjustbuffer(&buffer, buffer_length, buffer_capacity);
540
523
if (buffer_capacity == 0){
541
524
perror("adjustbuffer");
749
734
const char *seckeyfile = "seckey.txt";
750
735
mandos_context mc = { .simple_poll = NULL, .server = NULL,
751
736
.dh_bits = 1024, .priority = "SECURE256"};
752
bool gnutls_initalized = false;
755
739
struct argp_option options[] = {
757
741
.doc = "Debug mode", .group = 3 },
758
742
{ .name = "connect", .key = 'c',
760
.doc = "Connect directly to a sepcified mandos server",
744
.doc = "Connect directly to a sepcified mandos server", .group = 1 },
762
745
{ .name = "interface", .key = 'i',
763
746
.arg = "INTERFACE",
764
.doc = "Interface that Avahi will conntect through",
747
.doc = "Interface that Avahi will conntect through", .group = 1 },
766
748
{ .name = "keydir", .key = 'd',
768
.doc = "Directory where the openpgp keyring is",
750
.doc = "Directory where the openpgp keyring is", .group = 1 },
770
751
{ .name = "seckey", .key = 's',
772
.doc = "Secret openpgp key for gnutls authentication",
753
.doc = "Secret openpgp key for gnutls authentication", .group = 1 },
774
754
{ .name = "pubkey", .key = 'p',
776
.doc = "Public openpgp key for gnutls authentication",
756
.doc = "Public openpgp key for gnutls authentication", .group = 2 },
778
757
{ .name = "dh-bits", .key = 129,
780
.doc = "dh-bits to use in gnutls communication",
759
.doc = "dh-bits to use in gnutls communication", .group = 2 },
782
760
{ .name = "priority", .key = 130,
783
761
.arg = "PRIORITY",
784
762
.doc = "GNUTLS priority", .group = 1 },
789
error_t parse_opt (int key, char *arg,
790
struct argp_state *state) {
791
/* Get the INPUT argument from `argp_parse', which we know is
792
a pointer to our plugin list pointer. */
767
error_t parse_opt (int key, char *arg, struct argp_state *state) {
768
/* Get the INPUT argument from `argp_parse', which we know is a
769
pointer to our plugin list pointer. */
834
811
struct argp argp = { .options = options, .parser = parse_opt,
836
.doc = "Mandos client -- Get and decrypt"
837
" passwords from mandos server" };
813
.doc = "Mandos client -- Get and decrypt passwords from mandos server" };
838
814
argp_parse (&argp, argc, argv, 0, 0, NULL);