/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/mandosclient.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-04 16:05:01 UTC
  • mfrom: (24.1.15 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080804160501-woej7nf8amgrbc2e
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
                                    to be a union.  All users changed.

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() */
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 <sys/socket.h>         /* socket(), struct sockaddr_in6,
54
 
                                   struct in6_addr, inet_pton(),
55
 
                                   connect() */
56
 
#include <assert.h>             /* assert() */
57
 
#include <errno.h>              /* perror(), errno */
58
 
#include <time.h>               /* time() */
 
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 */
59
44
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
60
 
                                   SIOCSIFFLAGS, if_indextoname(),
61
 
                                   if_nametoindex(), IF_NAMESIZE */
62
 
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
63
 
                                   getuid(), getgid(), setuid(),
64
 
                                   setgid() */
65
 
#include <netinet/in.h>
66
 
#include <arpa/inet.h>          /* inet_pton(), htons */
67
 
#include <iso646.h>             /* not, and */
68
 
#include <argp.h>               /* struct argp_option, error_t, struct
69
 
                                   argp_state, struct argp,
70
 
                                   argp_parse(), ARGP_KEY_ARG,
71
 
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
 
45
                                   SIOCSIFFLAGS */
72
46
 
73
 
/* Avahi */
74
 
/* All Avahi types, constants and functions
75
 
 Avahi*, avahi_*,
76
 
 AVAHI_* */
77
47
#include <avahi-core/core.h>
78
48
#include <avahi-core/lookup.h>
79
49
#include <avahi-core/log.h>
81
51
#include <avahi-common/malloc.h>
82
52
#include <avahi-common/error.h>
83
53
 
84
 
/* GnuTLS */
85
 
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and functions
86
 
                                   gnutls_*
87
 
                                   init_gnutls_session(),
88
 
                                   GNUTLS_* */
89
 
#include <gnutls/openpgp.h>     /* gnutls_certificate_set_openpgp_key_file(),
90
 
                                   GNUTLS_OPENPGP_FMT_BASE64 */
 
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 */
91
60
 
 
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() */
92
71
/* GPGME */
93
 
#include <gpgme.h>              /* All GPGME types, constants and functions
94
 
                                   gpgme_*
95
 
                                   GPGME_PROTOCOL_OpenPGP,
96
 
                                   GPG_ERR_NO_* */
 
72
#include <errno.h>              /* perror() */
 
73
#include <gpgme.h>
97
74
 
98
75
#define BUFFER_SIZE 256
99
76
 
100
77
bool debug = false;
101
78
static const char *keydir = "/conf/conf.d/mandos";
 
79
const char *argp_program_version = "mandosclient 0.9";
 
80
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
102
81
static const char mandos_protocol_version[] = "1";
103
 
const char *argp_program_version = "mandosclient 1.0";
104
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
105
82
 
106
83
/* Used for passing in values through the Avahi callback functions */
107
84
typedef struct {
113
90
  const char *priority;
114
91
} mandos_context;
115
92
 
116
 
/*
117
 
 * Make room in "buffer" for at least BUFFER_SIZE additional bytes.
 
93
/* Make room in "buffer" for at least BUFFER_SIZE additional bytes.
118
94
 * "buffer_capacity" is how much is currently allocated,
119
 
 * "buffer_length" is how much is already used.
120
 
 */
 
95
 * "buffer_length" is how much is already used. */
121
96
size_t adjustbuffer(char **buffer, size_t buffer_length,
122
97
                  size_t buffer_capacity){
123
98
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
321
296
  if(debug){
322
297
    fprintf(stderr, "Initializing GnuTLS\n");
323
298
  }
324
 
  
325
 
  ret = gnutls_global_init();
326
 
  if (ret != GNUTLS_E_SUCCESS) {
 
299
 
 
300
  if ((ret = gnutls_global_init ())
 
301
      != GNUTLS_E_SUCCESS) {
327
302
    fprintf (stderr, "GnuTLS global_init: %s\n",
328
303
             safer_gnutls_strerror(ret));
329
304
    return -1;
338
313
  }
339
314
  
340
315
  /* OpenPGP credentials */
341
 
  gnutls_certificate_allocate_credentials(&mc->cred);
342
 
  if (ret != GNUTLS_E_SUCCESS){
 
316
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
 
317
      != GNUTLS_E_SUCCESS) {
343
318
    fprintf (stderr, "GnuTLS memory error: %s\n",
344
319
             safer_gnutls_strerror(ret));
345
 
    gnutls_global_deinit ();
346
320
    return -1;
347
321
  }
348
322
  
360
334
            " '%s')\n", ret, pubkeyfile, seckeyfile);
361
335
    fprintf(stdout, "The GnuTLS error is: %s\n",
362
336
            safer_gnutls_strerror(ret));
363
 
    goto globalfail;
 
337
    return -1;
364
338
  }
365
339
  
366
340
  /* GnuTLS server initialization */
368
342
  if (ret != GNUTLS_E_SUCCESS) {
369
343
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
370
344
             " %s\n", safer_gnutls_strerror(ret));
371
 
    goto globalfail;
 
345
    return -1;
372
346
  }
373
347
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
374
348
  if (ret != GNUTLS_E_SUCCESS) {
375
349
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
376
350
             safer_gnutls_strerror(ret));
377
 
    goto globalfail;
 
351
    return -1;
378
352
  }
379
353
  
380
354
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
381
355
 
382
356
  return 0;
383
 
 
384
 
 globalfail:
385
 
 
386
 
  gnutls_certificate_free_credentials(mc->cred);
387
 
  gnutls_global_deinit();
388
 
  return -1;
389
 
 
390
357
}
391
358
 
392
359
static int init_gnutls_session(mandos_context *mc,
406
373
      fprintf(stderr, "Syntax error at: %s\n", err);
407
374
      fprintf(stderr, "GnuTLS error: %s\n",
408
375
              safer_gnutls_strerror(ret));
409
 
      gnutls_deinit (*session);
410
376
      return -1;
411
377
    }
412
378
  }
416
382
  if (ret != GNUTLS_E_SUCCESS) {
417
383
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
418
384
            safer_gnutls_strerror(ret));
419
 
    gnutls_deinit (*session);
420
385
    return -1;
421
386
  }
422
387
  
538
503
  }
539
504
  
540
505
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
541
 
 
542
 
  do{
543
 
    ret = gnutls_handshake (session);
544
 
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
506
  
 
507
  ret = gnutls_handshake (session);
545
508
  
546
509
  if (ret != GNUTLS_E_SUCCESS){
547
510
    if(debug){
579
542
      case GNUTLS_E_AGAIN:
580
543
        break;
581
544
      case GNUTLS_E_REHANDSHAKE:
582
 
        do{
583
 
          ret = gnutls_handshake (session);
584
 
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
545
        ret = gnutls_handshake (session);
585
546
        if (ret < 0){
586
547
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
587
548
          gnutls_perror (ret);
640
601
  free(buffer);
641
602
  close(tcp_sd);
642
603
  gnutls_deinit (session);
 
604
  gnutls_certificate_free_credentials (mc->cred);
 
605
  gnutls_global_deinit ();
643
606
  return retval;
644
607
}
645
608
 
776
739
    const char *seckeyfile = "seckey.txt";
777
740
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
778
741
                          .dh_bits = 1024, .priority = "SECURE256"};
779
 
    bool gnutls_initalized = false;
780
742
    
781
743
    {
782
744
      struct argp_option options[] = {
862
824
                           .args_doc = "",
863
825
                           .doc = "Mandos client -- Get and decrypt"
864
826
                           " passwords from mandos server" };
865
 
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
866
 
      if (ret == ARGP_ERR_UNKNOWN){
867
 
        fprintf(stderr, "Unkown error while parsing arguments\n");
868
 
        exitcode = EXIT_FAILURE;
869
 
        goto end;
870
 
      }
 
827
      argp_parse (&argp, argc, argv, 0, 0, NULL);
871
828
    }
872
829
      
873
830
    pubkeyfile = combinepath(keydir, pubkeyfile);
887
844
    if (ret == -1){
888
845
      fprintf(stderr, "init_gnutls_global\n");
889
846
      goto end;
890
 
    } else {
891
 
      gnutls_initalized = true;
892
847
    }
893
848
 
894
849
    uid = getuid();
1043
998
        avahi_simple_poll_free(mc.simple_poll);
1044
999
    free(pubkeyfile);
1045
1000
    free(seckeyfile);
1046
 
 
1047
 
    if (gnutls_initalized){
1048
 
      gnutls_certificate_free_credentials(mc.cred);
1049
 
      gnutls_global_deinit ();
1050
 
    }
1051
1001
    
1052
1002
    return exitcode;
1053
1003
}