/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 20:43:31 UTC
  • mfrom: (24.1.17 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080804204331-zgu42g8ii997h1l2
* ca.pem: Removed.
* cert.pem: - '' -
* client-cert.pem: - '' -
* client-key.pem: - '' -
* crl.pem: - '' -
* key.pem: - '' -

* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
                                    to be a union.  All users changed.

* server.py: Changed log severity for many log messages.
  (Client.__init__): Take all config file settins as a dict instead of
                     as keyword arguments.

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 <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() */
 
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 */
60
44
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
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 */
 
45
                                   SIOCSIFFLAGS */
73
46
 
74
 
/* Avahi */
75
 
/* All Avahi types, constants and functions
76
 
 Avahi*, avahi_*,
77
 
 AVAHI_* */
78
47
#include <avahi-core/core.h>
79
48
#include <avahi-core/lookup.h>
80
49
#include <avahi-core/log.h>
82
51
#include <avahi-common/malloc.h>
83
52
#include <avahi-common/error.h>
84
53
 
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 */
 
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 */
92
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() */
93
71
/* GPGME */
94
 
#include <gpgme.h>              /* All GPGME types, constants and functions
95
 
                                   gpgme_*
96
 
                                   GPGME_PROTOCOL_OpenPGP,
97
 
                                   GPG_ERR_NO_* */
 
72
#include <errno.h>              /* perror() */
 
73
#include <gpgme.h>
98
74
 
99
75
#define BUFFER_SIZE 256
100
76
 
101
77
bool debug = false;
102
78
static const char *keydir = "/conf/conf.d/mandos";
103
79
static const char mandos_protocol_version[] = "1";
104
 
const char *argp_program_version = "password-request 1.0";
 
80
const char *argp_program_version = "mandosclient 0.9";
105
81
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
106
82
 
107
83
/* Used for passing in values through the Avahi callback functions */
229
205
    } else {
230
206
      fprintf(stderr, "Unsupported algorithm: %s\n",
231
207
              result->unsupported_algorithm);
232
 
      fprintf(stderr, "Wrong key usage: %u\n",
 
208
      fprintf(stderr, "Wrong key usage: %d\n",
233
209
              result->wrong_key_usage);
234
210
      if(result->file_name != NULL){
235
211
        fprintf(stderr, "File name: %s\n", result->file_name);
322
298
  if(debug){
323
299
    fprintf(stderr, "Initializing GnuTLS\n");
324
300
  }
325
 
  
326
 
  ret = gnutls_global_init();
327
 
  if (ret != GNUTLS_E_SUCCESS) {
 
301
 
 
302
  if ((ret = gnutls_global_init ())
 
303
      != GNUTLS_E_SUCCESS) {
328
304
    fprintf (stderr, "GnuTLS global_init: %s\n",
329
305
             safer_gnutls_strerror(ret));
330
306
    return -1;
339
315
  }
340
316
  
341
317
  /* OpenPGP credentials */
342
 
  gnutls_certificate_allocate_credentials(&mc->cred);
343
 
  if (ret != GNUTLS_E_SUCCESS){
 
318
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
 
319
      != GNUTLS_E_SUCCESS) {
344
320
    fprintf (stderr, "GnuTLS memory error: %s\n",
345
321
             safer_gnutls_strerror(ret));
346
 
    gnutls_global_deinit ();
347
322
    return -1;
348
323
  }
349
324
  
361
336
            " '%s')\n", ret, pubkeyfile, seckeyfile);
362
337
    fprintf(stdout, "The GnuTLS error is: %s\n",
363
338
            safer_gnutls_strerror(ret));
364
 
    goto globalfail;
 
339
    return -1;
365
340
  }
366
341
  
367
342
  /* GnuTLS server initialization */
369
344
  if (ret != GNUTLS_E_SUCCESS) {
370
345
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
371
346
             " %s\n", safer_gnutls_strerror(ret));
372
 
    goto globalfail;
 
347
    return -1;
373
348
  }
374
349
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
375
350
  if (ret != GNUTLS_E_SUCCESS) {
376
351
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
377
352
             safer_gnutls_strerror(ret));
378
 
    goto globalfail;
 
353
    return -1;
379
354
  }
380
355
  
381
356
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
382
357
 
383
358
  return 0;
384
 
 
385
 
 globalfail:
386
 
 
387
 
  gnutls_certificate_free_credentials(mc->cred);
388
 
  gnutls_global_deinit();
389
 
  return -1;
390
 
 
391
359
}
392
360
 
393
361
static int init_gnutls_session(mandos_context *mc,
407
375
      fprintf(stderr, "Syntax error at: %s\n", err);
408
376
      fprintf(stderr, "GnuTLS error: %s\n",
409
377
              safer_gnutls_strerror(ret));
410
 
      gnutls_deinit (*session);
411
378
      return -1;
412
379
    }
413
380
  }
417
384
  if (ret != GNUTLS_E_SUCCESS) {
418
385
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
419
386
            safer_gnutls_strerror(ret));
420
 
    gnutls_deinit (*session);
421
387
    return -1;
422
388
  }
423
389
  
456
422
  }
457
423
  
458
424
  if(debug){
459
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
460
 
            "\n", ip, port);
 
425
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
 
426
            ip, port);
461
427
  }
462
428
  
463
429
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
492
458
  to.in6.sin6_scope_id = (uint32_t)if_index;
493
459
  
494
460
  if(debug){
495
 
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
496
 
            port);
 
461
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
497
462
    char addrstr[INET6_ADDRSTRLEN] = "";
498
463
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
499
464
                 sizeof(addrstr)) == NULL){
540
505
  }
541
506
  
542
507
  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);
 
508
  
 
509
  ret = gnutls_handshake (session);
547
510
  
548
511
  if (ret != GNUTLS_E_SUCCESS){
549
512
    if(debug){
581
544
      case GNUTLS_E_AGAIN:
582
545
        break;
583
546
      case GNUTLS_E_REHANDSHAKE:
584
 
        do{
585
 
          ret = gnutls_handshake (session);
586
 
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
547
        ret = gnutls_handshake (session);
587
548
        if (ret < 0){
588
549
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
589
550
          gnutls_perror (ret);
642
603
  free(buffer);
643
604
  close(tcp_sd);
644
605
  gnutls_deinit (session);
 
606
  gnutls_certificate_free_credentials (mc->cred);
 
607
  gnutls_global_deinit ();
645
608
  return retval;
646
609
}
647
610
 
678
641
      char ip[AVAHI_ADDRESS_STR_MAX];
679
642
      avahi_address_snprint(ip, sizeof(ip), address);
680
643
      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);
 
644
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
 
645
                " port %d\n", name, host_name, ip, interface, port);
684
646
      }
685
647
      int ret = start_mandos_communication(ip, port, interface, mc);
686
648
      if (ret == 0){
779
741
    const char *seckeyfile = "seckey.txt";
780
742
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
781
743
                          .dh_bits = 1024, .priority = "SECURE256"};
782
 
    bool gnutls_initalized = false;
783
744
    
784
745
    {
785
746
      struct argp_option options[] = {
865
826
                           .args_doc = "",
866
827
                           .doc = "Mandos client -- Get and decrypt"
867
828
                           " 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
 
      }
 
829
      argp_parse (&argp, argc, argv, 0, 0, NULL);
874
830
    }
875
831
      
876
832
    pubkeyfile = combinepath(keydir, pubkeyfile);
890
846
    if (ret == -1){
891
847
      fprintf(stderr, "init_gnutls_global\n");
892
848
      goto end;
893
 
    } else {
894
 
      gnutls_initalized = true;
895
849
    }
896
850
 
897
851
    uid = getuid();
1046
1000
        avahi_simple_poll_free(mc.simple_poll);
1047
1001
    free(pubkeyfile);
1048
1002
    free(seckeyfile);
1049
 
 
1050
 
    if (gnutls_initalized){
1051
 
      gnutls_certificate_free_credentials(mc.cred);
1052
 
      gnutls_global_deinit ();
1053
 
    }
1054
1003
    
1055
1004
    return exitcode;
1056
1005
}