32
32
#define _LARGEFILE_SOURCE
33
33
#define _FILE_OFFSET_BITS 64
35
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
35
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY() */
37
#include <stdio.h> /* fprintf(), stderr, fwrite(),
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,
43
#include <stdbool.h> /* bool, true */
44
#include <string.h> /* memset(), strcmp(), strlen(),
45
strerror(), asprintf(), strcpy() */
46
#include <sys/ioctl.h> /* ioctl */
47
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
48
sockaddr_in6, PF_INET6,
49
SOCK_STREAM, INET6_ADDRSTRLEN,
51
#include <inttypes.h> /* PRIu16 */
52
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
53
struct in6_addr, inet_pton(),
55
#include <assert.h> /* assert() */
56
#include <errno.h> /* perror(), errno */
57
#include <time.h> /* time() */
41
#include <net/if.h> /* if_nametoindex */
42
#include <sys/ioctl.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
58
44
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
59
SIOCSIFFLAGS, if_indextoname(),
60
if_nametoindex(), IF_NAMESIZE */
61
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
62
getuid(), getgid(), setuid(),
64
#include <netinet/in.h>
65
#include <arpa/inet.h> /* inet_pton(), htons */
66
#include <iso646.h> /* not, and */
67
#include <argp.h> /* struct argp_option, error_t, struct
68
argp_state, struct argp,
69
argp_parse(), ARGP_KEY_ARG,
70
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
73
/* All Avahi types, constants and functions
76
47
#include <avahi-core/core.h>
77
48
#include <avahi-core/lookup.h>
78
49
#include <avahi-core/log.h>
80
51
#include <avahi-common/malloc.h>
81
52
#include <avahi-common/error.h>
84
#include <gnutls/gnutls.h> /* All GnuTLS types, constants and
87
init_gnutls_session(),
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 */
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 */
93
#include <gpgme.h> /* All GPGME types, constants and
96
GPGME_PROTOCOL_OpenPGP,
70
#include <errno.h> /* perror() */
99
76
#define BUFFER_SIZE 256
78
static const char *keydir = "/conf/conf.d/mandos";
79
static const char *pubkeyfile = "pubkey.txt";
80
static const char *seckeyfile = "seckey.txt";
101
82
bool debug = false;
102
static const char *keydir = "/conf/conf.d/mandos";
103
static const char mandos_protocol_version[] = "1";
104
const char *argp_program_version = "password-request 1.0";
105
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
107
/* Used for passing in values through the Avahi callback functions */
84
const char mandos_protocol_version[] = "1";
86
/* Used for passing in values through all the callback functions */
109
88
AvahiSimplePoll *simple_poll;
110
89
AvahiServer *server;
111
90
gnutls_certificate_credentials_t cred;
112
91
unsigned int dh_bits;
113
gnutls_dh_params_t dh_params;
114
92
const char *priority;
118
* Make room in "buffer" for at least BUFFER_SIZE additional bytes.
119
* "buffer_capacity" is how much is currently allocated,
120
* "buffer_length" is how much is already used.
122
95
size_t adjustbuffer(char **buffer, size_t buffer_length,
123
96
size_t buffer_capacity){
124
97
if (buffer_length + BUFFER_SIZE > buffer_capacity){
214
187
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
215
188
gpgme_strsource(rc), gpgme_strerror(rc));
216
189
plaintext_length = -1;
218
gpgme_decrypt_result_t result;
219
result = gpgme_op_decrypt_result(ctx);
221
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
223
fprintf(stderr, "Unsupported algorithm: %s\n",
224
result->unsupported_algorithm);
225
fprintf(stderr, "Wrong key usage: %u\n",
226
result->wrong_key_usage);
227
if(result->file_name != NULL){
228
fprintf(stderr, "File name: %s\n", result->file_name);
230
gpgme_recipient_t recipient;
231
recipient = result->recipients;
233
while(recipient != NULL){
234
fprintf(stderr, "Public key algorithm: %s\n",
235
gpgme_pubkey_algo_name(recipient->pubkey_algo));
236
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
237
fprintf(stderr, "Secret key available: %s\n",
238
recipient->status == GPG_ERR_NO_SECKEY
240
recipient = recipient->next;
245
190
goto decrypt_end;
249
194
fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
198
gpgme_decrypt_result_t result;
199
result = gpgme_op_decrypt_result(ctx);
201
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
203
fprintf(stderr, "Unsupported algorithm: %s\n",
204
result->unsupported_algorithm);
205
fprintf(stderr, "Wrong key usage: %d\n",
206
result->wrong_key_usage);
207
if(result->file_name != NULL){
208
fprintf(stderr, "File name: %s\n", result->file_name);
210
gpgme_recipient_t recipient;
211
recipient = result->recipients;
213
while(recipient != NULL){
214
fprintf(stderr, "Public key algorithm: %s\n",
215
gpgme_pubkey_algo_name(recipient->pubkey_algo));
216
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
217
fprintf(stderr, "Secret key available: %s\n",
218
recipient->status == GPG_ERR_NO_SECKEY
220
recipient = recipient->next;
252
226
/* Seek back to the beginning of the GPGME plaintext data buffer */
253
227
if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
254
228
perror("pgpme_data_seek");
340
312
/* OpenPGP credentials */
341
gnutls_certificate_allocate_credentials(&mc->cred);
342
if (ret != GNUTLS_E_SUCCESS){
343
fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
313
if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
314
!= GNUTLS_E_SUCCESS) {
315
fprintf (stderr, "GnuTLS memory error: %s\n",
345
316
safer_gnutls_strerror(ret));
346
gnutls_global_deinit ();
351
321
fprintf(stderr, "Attempting to use OpenPGP certificate %s"
352
" and keyfile %s as GnuTLS credentials\n", pubkeyfilename,
322
" and keyfile %s as GnuTLS credentials\n", pubkeyfile,
356
326
ret = gnutls_certificate_set_openpgp_key_file
357
(mc->cred, pubkeyfilename, seckeyfilename,
358
GNUTLS_OPENPGP_FMT_BASE64);
327
(mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
359
328
if (ret != GNUTLS_E_SUCCESS) {
361
330
"Error[%d] while reading the OpenPGP key pair ('%s',"
362
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
331
" '%s')\n", ret, pubkeyfile, seckeyfile);
363
332
fprintf(stdout, "The GnuTLS error is: %s\n",
364
333
safer_gnutls_strerror(ret));
368
337
/* GnuTLS server initialization */
369
ret = gnutls_dh_params_init(&mc->dh_params);
338
ret = gnutls_dh_params_init(dh_params);
370
339
if (ret != GNUTLS_E_SUCCESS) {
371
340
fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
372
341
" %s\n", safer_gnutls_strerror(ret));
375
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
344
ret = gnutls_dh_params_generate2(*dh_params, mc->dh_bits);
376
345
if (ret != GNUTLS_E_SUCCESS) {
377
346
fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
378
347
safer_gnutls_strerror(ret));
382
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
388
gnutls_certificate_free_credentials(mc->cred);
389
gnutls_global_deinit();
394
static int init_gnutls_session(mandos_context *mc,
395
gnutls_session_t *session){
351
gnutls_certificate_set_dh_params(mc->cred, *dh_params);
397
353
/* GnuTLS session creation */
398
354
ret = gnutls_init(session, GNUTLS_SERVER);
399
355
if (ret != GNUTLS_E_SUCCESS){
765
722
const char *interface = "eth0";
766
723
struct ifreq network;
770
725
char *connect_to = NULL;
771
726
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
772
char *pubkeyfilename = NULL;
773
char *seckeyfilename = NULL;
774
const char *pubkeyname = "pubkey.txt";
775
const char *seckeyname = "seckey.txt";
776
727
mandos_context mc = { .simple_poll = NULL, .server = NULL,
777
728
.dh_bits = 1024, .priority = "SECURE256"};
778
bool gnutls_initalized = false;
781
struct argp_option options[] = {
782
{ .name = "debug", .key = 128,
783
.doc = "Debug mode", .group = 3 },
784
{ .name = "connect", .key = 'c',
786
.doc = "Connect directly to a sepcified mandos server",
788
{ .name = "interface", .key = 'i',
790
.doc = "Interface that Avahi will conntect through",
792
{ .name = "keydir", .key = 'd',
794
.doc = "Directory where the openpgp keyring is",
796
{ .name = "seckey", .key = 's',
798
.doc = "Secret openpgp key for gnutls authentication",
800
{ .name = "pubkey", .key = 'p',
802
.doc = "Public openpgp key for gnutls authentication",
804
{ .name = "dh-bits", .key = 129,
806
.doc = "dh-bits to use in gnutls communication",
808
{ .name = "priority", .key = 130,
810
.doc = "GNUTLS priority", .group = 1 },
815
error_t parse_opt (int key, char *arg,
816
struct argp_state *state) {
817
/* Get the INPUT argument from `argp_parse', which we know is
818
a pointer to our plugin list pointer. */
731
/* Temporary int to get the address of for getopt_long */
732
int debug_int = debug ? 1 : 0;
734
struct option long_options[] = {
735
{"debug", no_argument, &debug_int, 1},
736
{"connect", required_argument, NULL, 'c'},
737
{"interface", required_argument, NULL, 'i'},
738
{"keydir", required_argument, NULL, 'd'},
739
{"seckey", required_argument, NULL, 's'},
740
{"pubkey", required_argument, NULL, 'p'},
741
{"dh-bits", required_argument, NULL, 'D'},
742
{"priority", required_argument, NULL, 'P'},
745
int option_index = 0;
746
ret = getopt_long (argc, argv, "i:", long_options,
840
mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
773
mc.dh_bits = (unsigned int) strtol(optarg, NULL, 10);
842
775
perror("strtol");
843
776
exit(EXIT_FAILURE);
780
mc.priority = optarg;
854
return ARGP_ERR_UNKNOWN;
859
struct argp argp = { .options = options, .parser = parse_opt,
861
.doc = "Mandos client -- Get and decrypt"
862
" passwords from mandos server" };
863
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
864
if (ret == ARGP_ERR_UNKNOWN){
865
fprintf(stderr, "Unknown error while parsing arguments\n");
866
exitcode = EXIT_FAILURE;
871
pubkeyfilename = combinepath(keydir, pubkeyname);
872
if (pubkeyfilename == NULL){
873
perror("combinepath");
874
exitcode = EXIT_FAILURE;
878
seckeyfilename = combinepath(keydir, seckeyname);
879
if (seckeyfilename == NULL){
880
perror("combinepath");
881
exitcode = EXIT_FAILURE;
885
ret = init_gnutls_global(&mc, pubkeyfilename, seckeyfilename);
887
fprintf(stderr, "init_gnutls_global failed\n");
888
exitcode = EXIT_FAILURE;
891
gnutls_initalized = true;
894
/* If the interface is down, bring it up */
896
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
899
exitcode = EXIT_FAILURE;
902
strcpy(network.ifr_name, interface);
903
ret = ioctl(sd, SIOCGIFFLAGS, &network);
905
perror("ioctl SIOCGIFFLAGS");
906
exitcode = EXIT_FAILURE;
909
if((network.ifr_flags & IFF_UP) == 0){
910
network.ifr_flags |= IFF_UP;
911
ret = ioctl(sd, SIOCSIFFLAGS, &network);
913
perror("ioctl SIOCSIFFLAGS");
914
exitcode = EXIT_FAILURE;
784
/* getopt_long() has already printed a message about the
785
unrcognized option, so just exit. */
789
/* Set the global debug flag from the temporary int */
790
debug = debug_int ? true : false;
793
pubkeyfile = combinepath(keydir, pubkeyfile);
794
if (pubkeyfile == NULL){
795
perror("combinepath");
796
exitcode = EXIT_FAILURE;
800
seckeyfile = combinepath(keydir, seckeyfile);
801
if (seckeyfile == NULL){
802
perror("combinepath");
934
806
if_index = (AvahiIfIndex) if_nametoindex(interface);
943
815
char *address = strrchr(connect_to, ':');
944
816
if(address == NULL){
945
817
fprintf(stderr, "No colon in address\n");
946
exitcode = EXIT_FAILURE;
950
821
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
952
823
perror("Bad port number");
953
exitcode = EXIT_FAILURE;
957
827
address = connect_to;
958
828
ret = start_mandos_communication(address, port, if_index, &mc);
960
exitcode = EXIT_FAILURE;
962
exitcode = EXIT_SUCCESS;
836
/* If the interface is down, bring it up */
838
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
841
exitcode = EXIT_FAILURE;
844
strcpy(network.ifr_name, interface); /* Spurious warning */
845
ret = ioctl(sd, SIOCGIFFLAGS, &network);
847
perror("ioctl SIOCGIFFLAGS");
848
exitcode = EXIT_FAILURE;
851
if((network.ifr_flags & IFF_UP) == 0){
852
network.ifr_flags |= IFF_UP;
853
ret = ioctl(sd, SIOCSIFFLAGS, &network);
855
perror("ioctl SIOCSIFFLAGS");
856
exitcode = EXIT_FAILURE;