/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-10 17:52:54 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080810175254-pxjzwws523ji49n7
* Makefile (DOCBOOKTOMAN): Do not generate AUTHORS section.

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