35
36
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
37
38
#include <stdio.h> /* fprintf(), stderr, fwrite(),
39
stdout, ferror(), sscanf */
39
40
#include <stdint.h> /* uint16_t, uint32_t */
40
41
#include <stddef.h> /* NULL, size_t, ssize_t */
41
42
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
47
48
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
48
49
sockaddr_in6, PF_INET6,
49
50
SOCK_STREAM, INET6_ADDRSTRLEN,
50
uid_t, gid_t, open(), opendir(), DIR */
51
uid_t, gid_t, open(), opendir(),
51
53
#include <sys/stat.h> /* open() */
52
54
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
53
55
struct in6_addr, inet_pton(),
55
57
#include <fcntl.h> /* open() */
56
#include <dirent.h> /* opendir(), struct dirent, readdir() */
57
#include <inttypes.h> /* PRIu16 */
58
#include <dirent.h> /* opendir(), struct dirent, readdir()
60
#include <inttypes.h> /* PRIu16, intmax_t, SCNdMAX */
58
61
#include <assert.h> /* assert() */
59
62
#include <errno.h> /* perror(), errno */
60
63
#include <time.h> /* time() */
66
69
getuid(), getgid(), setuid(),
68
71
#include <arpa/inet.h> /* inet_pton(), htons */
69
#include <iso646.h> /* not, and */
72
#include <iso646.h> /* not, and, or */
70
73
#include <argp.h> /* struct argp_option, error_t, struct
71
74
argp_state, struct argp,
72
75
argp_parse(), ARGP_KEY_ARG,
102
106
#define BUFFER_SIZE 256
105
#define PATHDIR "/conf/conf.d/mandos"
108
108
#define PATHDIR "/conf/conf.d/mandos"
109
109
#define SECKEY "seckey.txt"
110
#define PUBKEY "pupkey.txt"
110
#define PUBKEY "pubkey.txt"
112
112
bool debug = false;
113
113
static const char mandos_protocol_version[] = "1";
114
const char *argp_program_version = "password-request 1.0";
114
const char *argp_program_version = "mandos-client " VERSION;
115
115
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
117
117
/* Used for passing in values through the Avahi callback functions */
160
160
gpgme_data_t pgp_data;
162
fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
162
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
168
168
rc = gpgme_data_new_from_fd(&pgp_data, fd);
169
if (rc != GPG_ERR_NO_ERROR){
169
if(rc != GPG_ERR_NO_ERROR){
170
170
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
171
171
gpgme_strsource(rc), gpgme_strerror(rc));
175
175
rc = gpgme_op_import(mc->ctx, pgp_data);
176
if (rc != GPG_ERR_NO_ERROR){
176
if(rc != GPG_ERR_NO_ERROR){
177
177
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
178
178
gpgme_strsource(rc), gpgme_strerror(rc));
182
ret = TEMP_FAILURE_RETRY(close(fd));
182
ret = (int)TEMP_FAILURE_RETRY(close(fd));
191
191
fprintf(stderr, "Initialize gpgme\n");
195
195
gpgme_check_version(NULL);
196
196
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
197
if (rc != GPG_ERR_NO_ERROR){
197
if(rc != GPG_ERR_NO_ERROR){
198
198
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
199
199
gpgme_strsource(rc), gpgme_strerror(rc));
203
203
/* Set GPGME home directory for the OpenPGP engine only */
204
rc = gpgme_get_engine_info (&engine_info);
205
if (rc != GPG_ERR_NO_ERROR){
204
rc = gpgme_get_engine_info(&engine_info);
205
if(rc != GPG_ERR_NO_ERROR){
206
206
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
207
207
gpgme_strsource(rc), gpgme_strerror(rc));
219
219
fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
223
223
/* Create new GPGME "context" */
224
224
rc = gpgme_new(&(mc->ctx));
225
if (rc != GPG_ERR_NO_ERROR){
225
if(rc != GPG_ERR_NO_ERROR){
226
226
fprintf(stderr, "bad gpgme_new: %s: %s\n",
227
227
gpgme_strsource(rc), gpgme_strerror(rc));
231
if (not import_key(pubkey) or not import_key(seckey)){
231
if(not import_key(pubkey) or not import_key(seckey)){
239
239
* Decrypt OpenPGP data.
240
240
* Returns -1 on error
242
static ssize_t pgp_packet_decrypt (const mandos_context *mc,
243
const char *cryptotext,
242
static ssize_t pgp_packet_decrypt(const mandos_context *mc,
243
const char *cryptotext,
246
246
gpgme_data_t dh_crypto, dh_plain;
247
247
gpgme_error_t rc;
249
249
size_t plaintext_capacity = 0;
250
250
ssize_t plaintext_length = 0;
253
253
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
256
256
/* Create new GPGME data buffer from memory cryptotext */
257
257
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
259
if (rc != GPG_ERR_NO_ERROR){
259
if(rc != GPG_ERR_NO_ERROR){
260
260
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
261
261
gpgme_strsource(rc), gpgme_strerror(rc));
274
274
/* Decrypt data from the cryptotext data buffer to the plaintext
276
276
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
277
if (rc != GPG_ERR_NO_ERROR){
277
if(rc != GPG_ERR_NO_ERROR){
278
278
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
279
279
gpgme_strsource(rc), gpgme_strerror(rc));
280
280
plaintext_length = -1;
282
282
gpgme_decrypt_result_t result;
283
283
result = gpgme_op_decrypt_result(mc->ctx);
285
285
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
287
287
fprintf(stderr, "Unsupported algorithm: %s\n",
389
390
ret = gnutls_global_init();
390
if (ret != GNUTLS_E_SUCCESS) {
391
fprintf (stderr, "GnuTLS global_init: %s\n",
392
safer_gnutls_strerror(ret));
391
if(ret != GNUTLS_E_SUCCESS) {
392
fprintf(stderr, "GnuTLS global_init: %s\n",
393
safer_gnutls_strerror(ret));
397
398
/* "Use a log level over 10 to enable all debugging options."
398
399
* - GnuTLS manual
404
405
/* OpenPGP credentials */
405
406
gnutls_certificate_allocate_credentials(&mc->cred);
406
if (ret != GNUTLS_E_SUCCESS){
407
fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
409
safer_gnutls_strerror(ret));
410
gnutls_global_deinit ();
407
if(ret != GNUTLS_E_SUCCESS){
408
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
412
safer_gnutls_strerror(ret));
413
gnutls_global_deinit();
415
fprintf(stderr, "Attempting to use OpenPGP certificate %s"
416
" and keyfile %s as GnuTLS credentials\n", pubkeyfilename,
418
fprintf(stderr, "Attempting to use OpenPGP public key %s and"
419
" secret key %s as GnuTLS credentials\n", pubkeyfilename,
420
423
ret = gnutls_certificate_set_openpgp_key_file
421
424
(mc->cred, pubkeyfilename, seckeyfilename,
422
425
GNUTLS_OPENPGP_FMT_BASE64);
423
if (ret != GNUTLS_E_SUCCESS) {
426
if(ret != GNUTLS_E_SUCCESS) {
425
428
"Error[%d] while reading the OpenPGP key pair ('%s',"
426
429
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
432
435
/* GnuTLS server initialization */
433
436
ret = gnutls_dh_params_init(&mc->dh_params);
434
if (ret != GNUTLS_E_SUCCESS) {
435
fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
436
" %s\n", safer_gnutls_strerror(ret));
437
if(ret != GNUTLS_E_SUCCESS) {
438
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
439
" %s\n", safer_gnutls_strerror(ret));
439
442
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
440
if (ret != GNUTLS_E_SUCCESS) {
441
fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
442
safer_gnutls_strerror(ret));
443
if(ret != GNUTLS_E_SUCCESS) {
444
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
445
safer_gnutls_strerror(ret));
446
449
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
452
455
gnutls_certificate_free_credentials(mc->cred);
453
456
gnutls_global_deinit();
457
gnutls_dh_params_deinit(mc->dh_params);
458
461
static int init_gnutls_session(mandos_context *mc,
470
473
ret = gnutls_priority_set_direct(*session, mc->priority, &err);
471
if (ret != GNUTLS_E_SUCCESS) {
474
if(ret != GNUTLS_E_SUCCESS) {
472
475
fprintf(stderr, "Syntax error at: %s\n", err);
473
476
fprintf(stderr, "GnuTLS error: %s\n",
474
477
safer_gnutls_strerror(ret));
475
gnutls_deinit (*session);
478
gnutls_deinit(*session);
480
483
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
482
if (ret != GNUTLS_E_SUCCESS) {
485
if(ret != GNUTLS_E_SUCCESS) {
483
486
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
484
487
safer_gnutls_strerror(ret));
485
gnutls_deinit (*session);
488
gnutls_deinit(*session);
489
492
/* ignore client certificate if any. */
490
gnutls_certificate_server_set_request (*session,
493
gnutls_certificate_server_set_request(*session,
493
gnutls_dh_set_prime_bits (*session, mc->dh_bits);
496
gnutls_dh_set_prime_bits(*session, mc->dh_bits);
604
610
fprintf(stderr, "Establishing TLS session with %s\n", ip);
607
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
613
gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
610
ret = gnutls_handshake (session);
616
ret = gnutls_handshake(session);
611
617
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
613
if (ret != GNUTLS_E_SUCCESS){
619
if(ret != GNUTLS_E_SUCCESS){
615
621
fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
625
631
fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
630
636
buffer_capacity = adjustbuffer(&buffer, buffer_length,
631
637
buffer_capacity);
632
if (buffer_capacity == 0){
638
if(buffer_capacity == 0){
633
639
perror("adjustbuffer");
638
ret = gnutls_record_recv(session, buffer+buffer_length,
644
sret = gnutls_record_recv(session, buffer+buffer_length,
645
651
case GNUTLS_E_INTERRUPTED:
646
652
case GNUTLS_E_AGAIN:
648
654
case GNUTLS_E_REHANDSHAKE:
650
ret = gnutls_handshake (session);
656
ret = gnutls_handshake(session);
651
657
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
653
659
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
672
678
fprintf(stderr, "Closing TLS session\n");
675
gnutls_bye (session, GNUTLS_SHUT_RDWR);
681
gnutls_bye(session, GNUTLS_SHUT_RDWR);
677
if (buffer_length > 0){
683
if(buffer_length > 0){
678
684
decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
680
686
&decrypted_buffer);
681
if (decrypted_buffer_size >= 0){
687
if(decrypted_buffer_size >= 0){
683
689
while(written < (size_t) decrypted_buffer_size){
684
ret = (int)fwrite (decrypted_buffer + written, 1,
685
(size_t)decrypted_buffer_size - written,
690
ret = (int)fwrite(decrypted_buffer + written, 1,
691
(size_t)decrypted_buffer_size - written,
687
693
if(ret == 0 and ferror(stdout)){
689
695
fprintf(stderr, "Error writing encrypted data: %s\n",
748
754
avahi_address_snprint(ip, sizeof(ip), address);
750
756
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
751
PRIu16 ") on port %d\n", name, host_name, ip,
757
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
758
ip, (intmax_t)interface, port);
754
760
int ret = start_mandos_communication(ip, port, interface, mc);
756
762
avahi_simple_poll_quit(mc->simple_poll);
828
836
const char *pubkey = PATHDIR "/" PUBKEY;
830
838
mandos_context mc = { .simple_poll = NULL, .server = NULL,
831
.dh_bits = 1024, .priority = "SECURE256"};
839
.dh_bits = 1024, .priority = "SECURE256"
840
":!CTYPE-X.509:+CTYPE-OPENPGP" };
832
841
bool gnutls_initalized = false;
833
bool pgpme_initalized = false;
842
bool gpgme_initalized = false;
836
845
struct argp_option options[] = {
837
846
{ .name = "debug", .key = 128,
838
847
.doc = "Debug mode", .group = 3 },
839
848
{ .name = "connect", .key = 'c',
841
.doc = "Connect directly to a sepcified mandos server",
849
.arg = "ADDRESS:PORT",
850
.doc = "Connect directly to a specific Mandos server",
843
852
{ .name = "interface", .key = 'i',
845
.doc = "Interface that Avahi will conntect through",
854
.doc = "Interface that will be used to search for Mandos"
847
857
{ .name = "seckey", .key = 's',
849
.doc = "Secret openpgp key for gnutls authentication",
859
.doc = "OpenPGP secret key file base name",
851
861
{ .name = "pubkey", .key = 'p',
853
.doc = "Public openpgp key for gnutls authentication",
863
.doc = "OpenPGP public key file base name",
855
865
{ .name = "dh-bits", .key = 129,
857
.doc = "dh-bits to use in gnutls communication",
867
.doc = "Bit length of the prime number used in the"
868
" Diffie-Hellman key exchange",
859
870
{ .name = "priority", .key = 130,
861
.doc = "GNUTLS priority", .group = 1 },
872
.doc = "GnuTLS priority string for the TLS handshake",
866
error_t parse_opt (int key, char *arg,
867
struct argp_state *state) {
868
/* Get the INPUT argument from `argp_parse', which we know is
869
a pointer to our plugin list pointer. */
877
error_t parse_opt(int key, char *arg,
878
struct argp_state *state) {
880
case 128: /* --debug */
883
case 'c': /* --connect */
875
884
connect_to = arg;
886
case 'i': /* --interface */
889
case 's': /* --seckey */
892
case 'p': /* --pubkey */
888
mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
895
case 129: /* --dh-bits */
896
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
897
if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
898
or arg[numchars] != '\0'){
899
fprintf(stderr, "Bad number of DH bits\n");
891
900
exit(EXIT_FAILURE);
902
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
904
case 130: /* --priority */
895
905
mc.priority = arg;
897
907
case ARGP_KEY_ARG:
899
909
case ARGP_KEY_END:
907
917
struct argp argp = { .options = options, .parser = parse_opt,
909
919
.doc = "Mandos client -- Get and decrypt"
910
" passwords from mandos server" };
911
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
912
if (ret == ARGP_ERR_UNKNOWN){
920
" passwords from a Mandos server" };
921
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
922
if(ret == ARGP_ERR_UNKNOWN){
913
923
fprintf(stderr, "Unknown error while parsing arguments\n");
914
924
exitcode = EXIT_FAILURE;
919
ret = init_gnutls_global(&mc, pubkey, seckey);
921
fprintf(stderr, "init_gnutls_global failed\n");
922
exitcode = EXIT_FAILURE;
925
gnutls_initalized = true;
928
if(mkdtemp(tempdir) == NULL){
934
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
935
fprintf(stderr, "pgpme_initalized failed\n");
936
exitcode = EXIT_FAILURE;
939
pgpme_initalized = true;
942
929
/* If the interface is down, bring it up */
975
962
ret = setuid(uid);
977
964
perror("setuid");
982
969
perror("setgid");
972
ret = init_gnutls_global(&mc, pubkey, seckey);
974
fprintf(stderr, "init_gnutls_global failed\n");
975
exitcode = EXIT_FAILURE;
978
gnutls_initalized = true;
981
if(mkdtemp(tempdir) == NULL){
987
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
988
fprintf(stderr, "gpgme_initalized failed\n");
989
exitcode = EXIT_FAILURE;
992
gpgme_initalized = true;
985
995
if_index = (AvahiIfIndex) if_nametoindex(interface);
986
996
if(if_index == 0){
987
997
fprintf(stderr, "No such interface: \"%s\"\n", interface);
997
1007
exitcode = EXIT_FAILURE;
1001
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
1003
perror("Bad port number");
1011
ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1012
if(ret < 1 or tmpmax != (uint16_t)tmpmax
1013
or address[numchars+1] != '\0'){
1014
fprintf(stderr, "Bad port number\n");
1004
1015
exitcode = EXIT_FAILURE;
1018
port = (uint16_t)tmpmax;
1007
1019
*address = '\0';
1008
1020
address = connect_to;
1009
1021
ret = start_mandos_communication(address, port, if_index, &mc);
1039
1051
config.publish_addresses = 0;
1040
1052
config.publish_workstation = 0;
1041
1053
config.publish_domain = 0;
1043
1055
/* Allocate a new server */
1044
1056
mc.server = avahi_server_new(avahi_simple_poll_get
1045
1057
(mc.simple_poll), &config, NULL,
1048
1060
/* Free the Avahi configuration data */
1049
1061
avahi_server_config_free(&config);
1052
1064
/* Check if creating the Avahi server object succeeded */
1053
if (mc.server == NULL) {
1065
if(mc.server == NULL) {
1054
1066
fprintf(stderr, "Failed to create Avahi server: %s\n",
1055
1067
avahi_strerror(error));
1056
1068
exitcode = EXIT_FAILURE;
1072
1084
/* Run the main loop */
1075
1087
fprintf(stderr, "Starting Avahi loop search\n");
1078
1090
avahi_simple_poll_loop(mc.simple_poll);
1083
1095
fprintf(stderr, "%s exiting\n", argv[0]);
1086
1098
/* Cleanup things */
1088
1100
avahi_s_service_browser_free(sb);
1090
if (mc.server != NULL)
1102
if(mc.server != NULL)
1091
1103
avahi_server_free(mc.server);
1093
if (mc.simple_poll != NULL)
1105
if(mc.simple_poll != NULL)
1094
1106
avahi_simple_poll_free(mc.simple_poll);
1096
if (gnutls_initalized){
1108
if(gnutls_initalized){
1097
1109
gnutls_certificate_free_credentials(mc.cred);
1098
gnutls_global_deinit ();
1110
gnutls_global_deinit();
1111
gnutls_dh_params_deinit(mc.dh_params);
1101
if(pgpme_initalized){
1114
if(gpgme_initalized){
1102
1115
gpgme_release(mc.ctx);
1105
1118
/* Removes the temp directory used by GPGME */
1106
1119
if(tempdir[0] != '\0'){
1108
1121
struct dirent *direntry;
1109
1122
d = opendir(tempdir);
1124
if(errno != ENOENT){
1114
1129
direntry = readdir(d);
1115
1130
if(direntry == NULL){
1118
if (direntry->d_type == DT_REG){
1133
if(direntry->d_type == DT_REG){
1119
1134
char *fullname = NULL;
1120
1135
ret = asprintf(&fullname, "%s/%s", tempdir,
1121
1136
direntry->d_name);