/mandos/release

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

« back to all changes in this revision

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

  • Committer: Björn Påhlsson
  • Date: 2008-08-10 19:14:12 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: belorn@braxen-20080810191412-0c8xxvnh5nalhdfq
updated mandos-client sections and added see also stuff

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>
38
 
#include <assert.h>
39
 
#include <stdlib.h>
40
 
#include <time.h>
41
 
#include <net/if.h>             /* if_nametoindex */
42
 
#include <sys/ioctl.h>          /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
43
 
                                   SIOCSIFFLAGS */
 
37
#include <stdio.h>              /* fprintf(), stderr, fwrite(), stdout,
 
38
                                   ferror() */
 
39
#include <stdint.h>             /* uint16_t, uint32_t */
 
40
#include <stddef.h>             /* NULL, size_t, ssize_t */
 
41
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
 
42
                                   srand() */
 
43
#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 */
 
49
#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 */
 
54
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
 
55
                                   struct in6_addr, inet_pton(),
 
56
                                   connect() */
 
57
#include <assert.h>             /* assert() */
 
58
#include <errno.h>              /* perror(), errno */
 
59
#include <time.h>               /* time() */
44
60
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
45
 
                                   SIOCSIFFLAGS */
 
61
                                   SIOCSIFFLAGS, if_indextoname(),
 
62
                                   if_nametoindex(), IF_NAMESIZE */
 
63
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
 
64
                                   getuid(), getgid(), setuid(),
 
65
                                   setgid() */
 
66
#include <netinet/in.h>
 
67
#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 */
46
73
 
 
74
/* Avahi */
 
75
/* All Avahi types, constants and functions
 
76
 Avahi*, avahi_*,
 
77
 AVAHI_* */
47
78
#include <avahi-core/core.h>
48
79
#include <avahi-core/lookup.h>
49
80
#include <avahi-core/log.h>
51
82
#include <avahi-common/malloc.h>
52
83
#include <avahi-common/error.h>
53
84
 
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 */
 
85
/* GnuTLS */
 
86
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and functions
 
87
                                   gnutls_*
 
88
                                   init_gnutls_session(),
 
89
                                   GNUTLS_* */
 
90
#include <gnutls/openpgp.h>     /* gnutls_certificate_set_openpgp_key_file(),
 
91
                                   GNUTLS_OPENPGP_FMT_BASE64 */
60
92
 
61
 
#include <unistd.h>             /* close() */
62
 
#include <netinet/in.h>
63
 
#include <stdbool.h>            /* true */
64
 
#include <string.h>             /* memset */
65
 
#include <arpa/inet.h>          /* inet_pton() */
66
 
#include <iso646.h>             /* not */
67
 
#include <net/if.h>             /* IF_NAMESIZE */
68
 
#include <argp.h>               /* struct argp_option,
69
 
                                   struct argp_state, struct argp,
70
 
                                   argp_parse() */
71
93
/* GPGME */
72
 
#include <errno.h>              /* perror() */
73
 
#include <gpgme.h>
 
94
#include <gpgme.h>              /* All GPGME types, constants and functions
 
95
                                   gpgme_*
 
96
                                   GPGME_PROTOCOL_OpenPGP,
 
97
                                   GPG_ERR_NO_* */
74
98
 
75
99
#define BUFFER_SIZE 256
76
100
 
77
101
bool debug = false;
78
102
static const char *keydir = "/conf/conf.d/mandos";
79
103
static const char mandos_protocol_version[] = "1";
80
 
const char *argp_program_version = "mandosclient 0.9";
 
104
const char *argp_program_version = "password-request 1.0";
81
105
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
82
106
 
83
107
/* Used for passing in values through the Avahi callback functions */
205
229
    } else {
206
230
      fprintf(stderr, "Unsupported algorithm: %s\n",
207
231
              result->unsupported_algorithm);
208
 
      fprintf(stderr, "Wrong key usage: %d\n",
 
232
      fprintf(stderr, "Wrong key usage: %u\n",
209
233
              result->wrong_key_usage);
210
234
      if(result->file_name != NULL){
211
235
        fprintf(stderr, "File name: %s\n", result->file_name);
298
322
  if(debug){
299
323
    fprintf(stderr, "Initializing GnuTLS\n");
300
324
  }
301
 
 
302
 
  if ((ret = gnutls_global_init ())
303
 
      != GNUTLS_E_SUCCESS) {
 
325
  
 
326
  ret = gnutls_global_init();
 
327
  if (ret != GNUTLS_E_SUCCESS) {
304
328
    fprintf (stderr, "GnuTLS global_init: %s\n",
305
329
             safer_gnutls_strerror(ret));
306
330
    return -1;
315
339
  }
316
340
  
317
341
  /* OpenPGP credentials */
318
 
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
319
 
      != GNUTLS_E_SUCCESS) {
 
342
  gnutls_certificate_allocate_credentials(&mc->cred);
 
343
  if (ret != GNUTLS_E_SUCCESS){
320
344
    fprintf (stderr, "GnuTLS memory error: %s\n",
321
345
             safer_gnutls_strerror(ret));
322
346
    gnutls_global_deinit ();
360
384
 
361
385
 globalfail:
362
386
 
363
 
  gnutls_certificate_free_credentials (mc->cred);
364
 
  gnutls_global_deinit ();
 
387
  gnutls_certificate_free_credentials(mc->cred);
 
388
  gnutls_global_deinit();
365
389
  return -1;
366
390
 
367
391
}
432
456
  }
433
457
  
434
458
  if(debug){
435
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
436
 
            ip, port);
 
459
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
 
460
            "\n", ip, port);
437
461
  }
438
462
  
439
463
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
468
492
  to.in6.sin6_scope_id = (uint32_t)if_index;
469
493
  
470
494
  if(debug){
471
 
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
 
495
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
 
496
            port);
472
497
    char addrstr[INET6_ADDRSTRLEN] = "";
473
498
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
474
499
                 sizeof(addrstr)) == NULL){
515
540
  }
516
541
  
517
542
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
518
 
  
519
 
  ret = gnutls_handshake (session);
 
543
 
 
544
  do{
 
545
    ret = gnutls_handshake (session);
 
546
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
520
547
  
521
548
  if (ret != GNUTLS_E_SUCCESS){
522
549
    if(debug){
554
581
      case GNUTLS_E_AGAIN:
555
582
        break;
556
583
      case GNUTLS_E_REHANDSHAKE:
557
 
        ret = gnutls_handshake (session);
 
584
        do{
 
585
          ret = gnutls_handshake (session);
 
586
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
558
587
        if (ret < 0){
559
588
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
560
589
          gnutls_perror (ret);
649
678
      char ip[AVAHI_ADDRESS_STR_MAX];
650
679
      avahi_address_snprint(ip, sizeof(ip), address);
651
680
      if(debug){
652
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
653
 
                " 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);
654
684
      }
655
685
      int ret = start_mandos_communication(ip, port, interface, mc);
656
686
      if (ret == 0){
835
865
                           .args_doc = "",
836
866
                           .doc = "Mandos client -- Get and decrypt"
837
867
                           " passwords from mandos server" };
838
 
      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
      }
839
874
    }
840
875
      
841
876
    pubkeyfile = combinepath(keydir, pubkeyfile);
1013
1048
    free(seckeyfile);
1014
1049
 
1015
1050
    if (gnutls_initalized){
1016
 
      gnutls_certificate_free_credentials (mc.cred);
 
1051
      gnutls_certificate_free_credentials(mc.cred);
1017
1052
      gnutls_global_deinit ();
1018
1053
    }
1019
1054