/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>              /* fprintf(), stderr, fwrite(), stdout,
38
 
                                   ferror() */
 
37
#include <stdio.h>              /* fprintf(), stderr, fwrite(), stdout, ferror() */
39
38
#include <stdint.h>             /* uint16_t, uint32_t */
40
39
#include <stddef.h>             /* NULL, size_t, ssize_t */
41
 
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
42
 
                                   srand() */
 
40
#include <stdlib.h>             /* free() */
43
41
#include <stdbool.h>            /* bool, true */
44
 
#include <string.h>             /* memset(), strcmp(), strlen(),
45
 
                                   strerror(), memcpy(), strcpy() */
46
 
#include <sys/ioctl.h>          /* ioctl */
47
 
#include <net/if.h>             /* ifreq, SIOCGIFFLAGS, SIOCSIFFLAGS,
48
 
                                   IFF_UP */
 
42
#include <string.h>             /* memset(), strcmp(), strlen, strerror() */
 
43
#include <sys/ioctl.h>          /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
 
44
                                   SIOCSIFFLAGS */
49
45
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
50
 
                                   sockaddr_in6, PF_INET6,
51
 
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
52
 
                                   uid_t, gid_t */
53
 
#include <inttypes.h>           /* PRIu16 */
 
46
                                   sockaddr_in6, PF_INET6, SOCK_STREAM, INET6_ADDRSTRLEN */
54
47
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
55
48
                                   struct in6_addr, inet_pton(),
56
49
                                   connect() */
57
 
#include <assert.h>             /* assert() */
58
 
#include <errno.h>              /* perror(), errno */
59
 
#include <time.h>               /* time() */
 
50
#include <assert.h>
 
51
#include <errno.h>              /* perror() */
 
52
#include <time.h>
60
53
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
61
54
                                   SIOCSIFFLAGS, if_indextoname(),
62
55
                                   if_nametoindex(), IF_NAMESIZE */
63
 
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
64
 
                                   getuid(), getgid(), setuid(),
65
 
                                   setgid() */
 
56
#include <unistd.h>             /* close(), SEEK_SET, off_t, write()*/
66
57
#include <netinet/in.h>
67
58
#include <arpa/inet.h>          /* inet_pton(), htons */
68
 
#include <iso646.h>             /* not, and */
69
 
#include <argp.h>               /* struct argp_option, error_t, struct
70
 
                                   argp_state, struct argp,
71
 
                                   argp_parse(), ARGP_KEY_ARG,
72
 
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
 
59
#include <iso646.h>             /* not */
 
60
#include <argp.h>               /* struct argp_option,
 
61
                                   struct argp_state, struct argp,
 
62
                                   argp_parse() */
73
63
 
74
64
/* Avahi */
75
 
/* All Avahi types, constants and functions
76
 
 Avahi*, avahi_*,
77
 
 AVAHI_* */
78
 
#include <avahi-core/core.h>
 
65
#include <avahi-core/core.h>    /* AvahiSimplePoll, AvahiServer,
 
66
                                   AvahiIfIndex */
79
67
#include <avahi-core/lookup.h>
80
 
#include <avahi-core/log.h>
 
68
#include <avahi-core/log.h>     /* AvahiLogLevel */
81
69
#include <avahi-common/simple-watch.h>
82
70
#include <avahi-common/malloc.h>
83
71
#include <avahi-common/error.h>
84
72
 
85
73
/* GnuTLS */
86
 
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and functions
87
 
                                   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(),
88
97
                                   init_gnutls_session(),
89
 
                                   GNUTLS_* */
90
 
#include <gnutls/openpgp.h>     /* gnutls_certificate_set_openpgp_key_file(),
91
 
                                   GNUTLS_OPENPGP_FMT_BASE64 */
 
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 */
92
107
 
93
108
/* GPGME */
94
 
#include <gpgme.h>              /* All GPGME types, constants and functions
95
 
                                   gpgme_*
 
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()
96
127
                                   GPGME_PROTOCOL_OpenPGP,
97
 
                                   GPG_ERR_NO_* */
 
128
                                   GPG_ERR_NO_ERROR,
 
129
                                   GPG_ERR_NO_SECKEY, */
98
130
 
99
131
#define BUFFER_SIZE 256
100
132
 
101
133
bool debug = false;
102
134
static const char *keydir = "/conf/conf.d/mandos";
103
135
static const char mandos_protocol_version[] = "1";
104
 
const char *argp_program_version = "password-request 1.0";
 
136
const char *argp_program_version = "mandosclient 0.9";
105
137
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
106
138
 
107
139
/* Used for passing in values through the Avahi callback functions */
229
261
    } else {
230
262
      fprintf(stderr, "Unsupported algorithm: %s\n",
231
263
              result->unsupported_algorithm);
232
 
      fprintf(stderr, "Wrong key usage: %u\n",
 
264
      fprintf(stderr, "Wrong key usage: %d\n",
233
265
              result->wrong_key_usage);
234
266
      if(result->file_name != NULL){
235
267
        fprintf(stderr, "File name: %s\n", result->file_name);
322
354
  if(debug){
323
355
    fprintf(stderr, "Initializing GnuTLS\n");
324
356
  }
325
 
  
326
 
  ret = gnutls_global_init();
327
 
  if (ret != GNUTLS_E_SUCCESS) {
 
357
 
 
358
  if ((ret = gnutls_global_init ())
 
359
      != GNUTLS_E_SUCCESS) {
328
360
    fprintf (stderr, "GnuTLS global_init: %s\n",
329
361
             safer_gnutls_strerror(ret));
330
362
    return -1;
339
371
  }
340
372
  
341
373
  /* OpenPGP credentials */
342
 
  gnutls_certificate_allocate_credentials(&mc->cred);
343
 
  if (ret != GNUTLS_E_SUCCESS){
 
374
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
 
375
      != GNUTLS_E_SUCCESS) {
344
376
    fprintf (stderr, "GnuTLS memory error: %s\n",
345
377
             safer_gnutls_strerror(ret));
346
378
    gnutls_global_deinit ();
456
488
  }
457
489
  
458
490
  if(debug){
459
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
460
 
            "\n", ip, port);
 
491
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
 
492
            ip, port);
461
493
  }
462
494
  
463
495
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
492
524
  to.in6.sin6_scope_id = (uint32_t)if_index;
493
525
  
494
526
  if(debug){
495
 
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
496
 
            port);
 
527
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
497
528
    char addrstr[INET6_ADDRSTRLEN] = "";
498
529
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
499
530
                 sizeof(addrstr)) == NULL){
540
571
  }
541
572
  
542
573
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
543
 
 
544
 
  do{
545
 
    ret = gnutls_handshake (session);
546
 
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
574
  
 
575
  ret = gnutls_handshake (session);
547
576
  
548
577
  if (ret != GNUTLS_E_SUCCESS){
549
578
    if(debug){
581
610
      case GNUTLS_E_AGAIN:
582
611
        break;
583
612
      case GNUTLS_E_REHANDSHAKE:
584
 
        do{
585
 
          ret = gnutls_handshake (session);
586
 
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
613
        ret = gnutls_handshake (session);
587
614
        if (ret < 0){
588
615
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
589
616
          gnutls_perror (ret);
678
705
      char ip[AVAHI_ADDRESS_STR_MAX];
679
706
      avahi_address_snprint(ip, sizeof(ip), address);
680
707
      if(debug){
681
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
682
 
                PRIu16 ") on port %d\n", name, host_name, ip,
683
 
                interface, port);
 
708
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
 
709
                " port %d\n", name, host_name, ip, interface, port);
684
710
      }
685
711
      int ret = start_mandos_communication(ip, port, interface, mc);
686
712
      if (ret == 0){
865
891
                           .args_doc = "",
866
892
                           .doc = "Mandos client -- Get and decrypt"
867
893
                           " passwords from mandos server" };
868
 
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
869
 
      if (ret == ARGP_ERR_UNKNOWN){
870
 
        fprintf(stderr, "Unkown error while parsing arguments\n");
871
 
        exitcode = EXIT_FAILURE;
872
 
        goto end;
873
 
      }
 
894
      argp_parse (&argp, argc, argv, 0, 0, NULL);
874
895
    }
875
896
      
876
897
    pubkeyfile = combinepath(keydir, pubkeyfile);
1048
1069
    free(seckeyfile);
1049
1070
 
1050
1071
    if (gnutls_initalized){
1051
 
      gnutls_certificate_free_credentials(mc.cred);
 
1072
      gnutls_certificate_free_credentials (mc.cred);
1052
1073
      gnutls_global_deinit ();
1053
1074
    }
1054
1075