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

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() */
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";
102
79
static const char mandos_protocol_version[] = "1";
103
 
const char *argp_program_version = "password-request 1.0";
 
80
const char *argp_program_version = "mandosclient 0.9";
104
81
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
105
82
 
106
83
/* Used for passing in values through the Avahi callback functions */
321
298
  if(debug){
322
299
    fprintf(stderr, "Initializing GnuTLS\n");
323
300
  }
324
 
  
325
 
  ret = gnutls_global_init();
326
 
  if (ret != GNUTLS_E_SUCCESS) {
 
301
 
 
302
  if ((ret = gnutls_global_init ())
 
303
      != GNUTLS_E_SUCCESS) {
327
304
    fprintf (stderr, "GnuTLS global_init: %s\n",
328
305
             safer_gnutls_strerror(ret));
329
306
    return -1;
338
315
  }
339
316
  
340
317
  /* OpenPGP credentials */
341
 
  gnutls_certificate_allocate_credentials(&mc->cred);
342
 
  if (ret != GNUTLS_E_SUCCESS){
 
318
  if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
 
319
      != GNUTLS_E_SUCCESS) {
343
320
    fprintf (stderr, "GnuTLS memory error: %s\n",
344
321
             safer_gnutls_strerror(ret));
345
 
    gnutls_global_deinit ();
346
322
    return -1;
347
323
  }
348
324
  
360
336
            " '%s')\n", ret, pubkeyfile, seckeyfile);
361
337
    fprintf(stdout, "The GnuTLS error is: %s\n",
362
338
            safer_gnutls_strerror(ret));
363
 
    goto globalfail;
 
339
    return -1;
364
340
  }
365
341
  
366
342
  /* GnuTLS server initialization */
368
344
  if (ret != GNUTLS_E_SUCCESS) {
369
345
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
370
346
             " %s\n", safer_gnutls_strerror(ret));
371
 
    goto globalfail;
 
347
    return -1;
372
348
  }
373
349
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
374
350
  if (ret != GNUTLS_E_SUCCESS) {
375
351
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
376
352
             safer_gnutls_strerror(ret));
377
 
    goto globalfail;
 
353
    return -1;
378
354
  }
379
355
  
380
356
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
381
357
 
382
358
  return 0;
383
 
 
384
 
 globalfail:
385
 
 
386
 
  gnutls_certificate_free_credentials(mc->cred);
387
 
  gnutls_global_deinit();
388
 
  return -1;
389
 
 
390
359
}
391
360
 
392
361
static int init_gnutls_session(mandos_context *mc,
406
375
      fprintf(stderr, "Syntax error at: %s\n", err);
407
376
      fprintf(stderr, "GnuTLS error: %s\n",
408
377
              safer_gnutls_strerror(ret));
409
 
      gnutls_deinit (*session);
410
378
      return -1;
411
379
    }
412
380
  }
416
384
  if (ret != GNUTLS_E_SUCCESS) {
417
385
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
418
386
            safer_gnutls_strerror(ret));
419
 
    gnutls_deinit (*session);
420
387
    return -1;
421
388
  }
422
389
  
538
505
  }
539
506
  
540
507
  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);
 
508
  
 
509
  ret = gnutls_handshake (session);
545
510
  
546
511
  if (ret != GNUTLS_E_SUCCESS){
547
512
    if(debug){
579
544
      case GNUTLS_E_AGAIN:
580
545
        break;
581
546
      case GNUTLS_E_REHANDSHAKE:
582
 
        do{
583
 
          ret = gnutls_handshake (session);
584
 
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
 
547
        ret = gnutls_handshake (session);
585
548
        if (ret < 0){
586
549
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
587
550
          gnutls_perror (ret);
640
603
  free(buffer);
641
604
  close(tcp_sd);
642
605
  gnutls_deinit (session);
 
606
  gnutls_certificate_free_credentials (mc->cred);
 
607
  gnutls_global_deinit ();
643
608
  return retval;
644
609
}
645
610
 
776
741
    const char *seckeyfile = "seckey.txt";
777
742
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
778
743
                          .dh_bits = 1024, .priority = "SECURE256"};
779
 
    bool gnutls_initalized = false;
780
744
    
781
745
    {
782
746
      struct argp_option options[] = {
862
826
                           .args_doc = "",
863
827
                           .doc = "Mandos client -- Get and decrypt"
864
828
                           " 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
 
      }
 
829
      argp_parse (&argp, argc, argv, 0, 0, NULL);
871
830
    }
872
831
      
873
832
    pubkeyfile = combinepath(keydir, pubkeyfile);
887
846
    if (ret == -1){
888
847
      fprintf(stderr, "init_gnutls_global\n");
889
848
      goto end;
890
 
    } else {
891
 
      gnutls_initalized = true;
892
849
    }
893
850
 
894
851
    uid = getuid();
1043
1000
        avahi_simple_poll_free(mc.simple_poll);
1044
1001
    free(pubkeyfile);
1045
1002
    free(seckeyfile);
1046
 
 
1047
 
    if (gnutls_initalized){
1048
 
      gnutls_certificate_free_credentials(mc.cred);
1049
 
      gnutls_global_deinit ();
1050
 
    }
1051
1003
    
1052
1004
    return exitcode;
1053
1005
}