/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/password-request.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-09 04:56:00 UTC
  • mfrom: (24.1.27 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080809045600-m7jx3tj4wwunaffa
Merge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
35
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
36
36
 
37
 
#include <stdio.h>
 
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,
 
44
                                   SIOCSIFFLAGS */
 
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(),
 
49
                                   connect() */
38
50
#include <assert.h>
39
 
#include <stdlib.h>
 
51
#include <errno.h>              /* perror() */
40
52
#include <time.h>
41
 
#include <net/if.h>             /* if_nametoindex */
42
 
#include <sys/ioctl.h>          /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
43
 
                                   SIOCSIFFLAGS */
44
53
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
45
 
                                   SIOCSIFFLAGS */
46
 
 
47
 
#include <avahi-core/core.h>
48
 
#include <avahi-core/lookup.h>
49
 
#include <avahi-core/log.h>
50
 
#include <avahi-common/simple-watch.h>
51
 
#include <avahi-common/malloc.h>
52
 
#include <avahi-common/error.h>
53
 
 
54
 
/* Mandos client part */
55
 
#include <sys/types.h>          /* socket(), inet_pton() */
56
 
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
57
 
                                   struct in6_addr, inet_pton() */
58
 
#include <gnutls/gnutls.h>      /* All GnuTLS stuff */
59
 
#include <gnutls/openpgp.h>     /* GnuTLS with openpgp stuff */
60
 
 
61
 
#include <unistd.h>             /* close() */
 
54
                                   SIOCSIFFLAGS, if_indextoname(),
 
55
                                   if_nametoindex(), IF_NAMESIZE */
 
56
#include <unistd.h>             /* close(), SEEK_SET, off_t, write()*/
62
57
#include <netinet/in.h>
63
 
#include <stdbool.h>            /* true */
64
 
#include <string.h>             /* memset */
65
 
#include <arpa/inet.h>          /* inet_pton() */
 
58
#include <arpa/inet.h>          /* inet_pton(), htons */
66
59
#include <iso646.h>             /* not */
67
 
#include <net/if.h>             /* IF_NAMESIZE */
68
60
#include <argp.h>               /* struct argp_option,
69
61
                                   struct argp_state, struct argp,
70
62
                                   argp_parse() */
 
63
 
 
64
/* Avahi */
 
65
#include <avahi-core/core.h>    /* AvahiSimplePoll, AvahiServer,
 
66
                                   AvahiIfIndex */
 
67
#include <avahi-core/lookup.h>
 
68
#include <avahi-core/log.h>     /* AvahiLogLevel */
 
69
#include <avahi-common/simple-watch.h>
 
70
#include <avahi-common/malloc.h>
 
71
#include <avahi-common/error.h>
 
72
 
 
73
/* GnuTLS */
 
74
#include <gnutls/gnutls.h>      /* gnutls_certificate_credentials_t,
 
75
                                   gnutls_dh_params_t,
 
76
                                   gnutls_strerror(),
 
77
                                   gnutls_global_init(),
 
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(),
 
88
                                   gnutls_deinit(),
 
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,
 
94
                                   gnutls_handshake(),
 
95
                                   gnutls_record_recv()
 
96
                                   gnutls_perror(), gnutls_bye(),
 
97
                                   init_gnutls_session(),
 
98
                                   GNUTLS_E_SUCCESS,
 
99
                                   GNUTLS_CRD_CERTIFICATE,
 
100
                                   GNUTLS_CERT_IGNORE,
 
101
                                   GNUTLS_E_INTERRUPTED,
 
102
                                   GNUTLS_E_AGAIN,
 
103
                                   GNUTLS_E_REHANDSHAKE,
 
104
                                   GNUTLS_SHUT_RDWR, */
 
105
#include <gnutls/openpgp.h> /* gnutls_certificate_set_openpgp_key_file(),
 
106
                               GNUTLS_OPENPGP_FMT_BASE64 */
 
107
 
71
108
/* GPGME */
72
 
#include <errno.h>              /* perror() */
73
 
#include <gpgme.h>
 
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(),
 
113
                                   gpgme_strsource(),
 
114
                                   gpgme_strerror(),
 
115
                                   gpgme_get_engine_info(),
 
116
                                   gpgme_set_engine_info(),
 
117
                                   gpgme_data_new_from_mem(),
 
118
                                   gpgme_data_new(), gpgme_new(),
 
119
                                   gpgme_op_decrypt(),
 
120
                                   gpgme_decrypt_result_t,
 
121
                                   gpgme_op_decrypt_result(),
 
122
                                   gpgme_recipient_t,
 
123
                                   gpgme_pubkey_algo_name(),
 
124
                                   gpgme_data_seek(),
 
125
                                   gpgme_data_read(),
 
126
                                   gpgme_data_release()
 
127
                                   GPGME_PROTOCOL_OpenPGP,
 
128
                                   GPG_ERR_NO_ERROR,
 
129
                                   GPG_ERR_NO_SECKEY, */
74
130
 
75
131
#define BUFFER_SIZE 256
76
132
 
77
133
bool debug = false;
78
134
static const char *keydir = "/conf/conf.d/mandos";
 
135
static const char mandos_protocol_version[] = "1";
79
136
const char *argp_program_version = "mandosclient 0.9";
80
137
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
81
 
static const char mandos_protocol_version[] = "1";
82
138
 
83
139
/* Used for passing in values through the Avahi callback functions */
84
140
typedef struct {
90
146
  const char *priority;
91
147
} mandos_context;
92
148
 
93
 
/* Make room in "buffer" for at least BUFFER_SIZE additional bytes.
 
149
/*
 
150
 * Make room in "buffer" for at least BUFFER_SIZE additional bytes.
94
151
 * "buffer_capacity" is how much is currently allocated,
95
 
 * "buffer_length" is how much is already used. */
 
152
 * "buffer_length" is how much is already used.
 
153
 */
96
154
size_t adjustbuffer(char **buffer, size_t buffer_length,
97
155
                  size_t buffer_capacity){
98
156
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
317
375
      != GNUTLS_E_SUCCESS) {
318
376
    fprintf (stderr, "GnuTLS memory error: %s\n",
319
377
             safer_gnutls_strerror(ret));
 
378
    gnutls_global_deinit ();
320
379
    return -1;
321
380
  }
322
381
  
334
393
            " '%s')\n", ret, pubkeyfile, seckeyfile);
335
394
    fprintf(stdout, "The GnuTLS error is: %s\n",
336
395
            safer_gnutls_strerror(ret));
337
 
    return -1;
 
396
    goto globalfail;
338
397
  }
339
398
  
340
399
  /* GnuTLS server initialization */
342
401
  if (ret != GNUTLS_E_SUCCESS) {
343
402
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
344
403
             " %s\n", safer_gnutls_strerror(ret));
345
 
    return -1;
 
404
    goto globalfail;
346
405
  }
347
406
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
348
407
  if (ret != GNUTLS_E_SUCCESS) {
349
408
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
350
409
             safer_gnutls_strerror(ret));
351
 
    return -1;
 
410
    goto globalfail;
352
411
  }
353
412
  
354
413
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
355
414
 
356
415
  return 0;
 
416
 
 
417
 globalfail:
 
418
 
 
419
  gnutls_certificate_free_credentials(mc->cred);
 
420
  gnutls_global_deinit();
 
421
  return -1;
 
422
 
357
423
}
358
424
 
359
425
static int init_gnutls_session(mandos_context *mc,
373
439
      fprintf(stderr, "Syntax error at: %s\n", err);
374
440
      fprintf(stderr, "GnuTLS error: %s\n",
375
441
              safer_gnutls_strerror(ret));
 
442
      gnutls_deinit (*session);
376
443
      return -1;
377
444
    }
378
445
  }
382
449
  if (ret != GNUTLS_E_SUCCESS) {
383
450
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
384
451
            safer_gnutls_strerror(ret));
 
452
    gnutls_deinit (*session);
385
453
    return -1;
386
454
  }
387
455
  
601
669
  free(buffer);
602
670
  close(tcp_sd);
603
671
  gnutls_deinit (session);
604
 
  gnutls_certificate_free_credentials (mc->cred);
605
 
  gnutls_global_deinit ();
606
672
  return retval;
607
673
}
608
674
 
739
805
    const char *seckeyfile = "seckey.txt";
740
806
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
741
807
                          .dh_bits = 1024, .priority = "SECURE256"};
 
808
    bool gnutls_initalized = false;
742
809
    
743
810
    {
744
811
      struct argp_option options[] = {
844
911
    if (ret == -1){
845
912
      fprintf(stderr, "init_gnutls_global\n");
846
913
      goto end;
 
914
    } else {
 
915
      gnutls_initalized = true;
847
916
    }
848
917
 
849
918
    uid = getuid();
998
1067
        avahi_simple_poll_free(mc.simple_poll);
999
1068
    free(pubkeyfile);
1000
1069
    free(seckeyfile);
 
1070
 
 
1071
    if (gnutls_initalized){
 
1072
      gnutls_certificate_free_credentials (mc.cred);
 
1073
      gnutls_global_deinit ();
 
1074
    }
1001
1075
    
1002
1076
    return exitcode;
1003
1077
}