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