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