8
8
* includes the following functions: "resolve_callback",
9
9
* "browse_callback", and parts of "main".
11
* Everything else is Copyright © 2007-2008 Teddy Hogeborn and Björn
12
* Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
14
14
* This program is free software: you can redistribute it and/or
15
15
* modify it under the terms of the GNU General Public License as
25
25
* along with this program. If not, see
26
26
* <http://www.gnu.org/licenses/>.
28
* Contact the authors at <https://www.fukt.bsnet.se/~belorn/> and
29
* <https://www.fukt.bsnet.se/~teddy/>.
28
* Contact the authors at <mandos@fukt.bsnet.se>.
32
#define _FORTIFY_SOURCE 2
31
/* Needed by GPGME, specifically gpgme_data_seek() */
34
32
#define _LARGEFILE_SOURCE
35
33
#define _FILE_OFFSET_BITS 64
65
63
#include <errno.h> /* perror() */
69
67
#include <getopt.h>
72
#define CERT_ROOT "/conf/conf.d/cryptkeyreq/"
74
#define CERTFILE CERT_ROOT "openpgp-client.txt"
75
#define KEYFILE CERT_ROOT "openpgp-client-key.txt"
76
69
#define BUFFER_SIZE 256
71
static int dh_bits = 1024;
73
static const char *keydir = "/conf/conf.d/mandos";
74
static const char *pubkeyfile = "pubkey.txt";
75
static const char *seckeyfile = "seckey.txt";
79
77
bool debug = false;
82
81
gnutls_session_t session;
83
82
gnutls_certificate_credentials_t cred;
85
84
} encrypted_session;
88
ssize_t pgp_packet_decrypt (char *packet, size_t packet_size,
89
char **new_packet, const char *homedir){
87
static ssize_t pgp_packet_decrypt (char *packet, size_t packet_size,
90
90
gpgme_data_t dh_crypto, dh_plain;
103
103
gpgme_check_version(NULL);
104
gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
104
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
105
if (rc != GPG_ERR_NO_ERROR){
106
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
107
gpgme_strsource(rc), gpgme_strerror(rc));
106
111
/* Set GPGME home directory */
107
112
rc = gpgme_get_engine_info (&engine_info);
193
198
gpgme_data_release(dh_crypto);
195
200
/* Seek back to the beginning of the GPGME plaintext data buffer */
196
gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET);
201
if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
202
perror("pgpme_data_seek");
200
207
if (new_packet_length + BUFFER_SIZE > new_packet_capacity){
244
void debuggnutls(__attribute__((unused)) int level,
251
static void debuggnutls(__attribute__((unused)) int level,
246
253
fprintf(stderr, "%s", string);
249
int initgnutls(encrypted_session *es){
256
static int initgnutls(encrypted_session *es){
254
261
fprintf(stderr, "Initializing GnuTLS\n");
257
264
if ((ret = gnutls_global_init ())
258
265
!= GNUTLS_E_SUCCESS) {
259
266
fprintf (stderr, "global_init: %s\n", safer_gnutls_strerror(ret));
277
284
fprintf(stderr, "Attempting to use OpenPGP certificate %s"
278
" and keyfile %s as GnuTLS credentials\n", CERTFILE,
285
" and keyfile %s as GnuTLS credentials\n", pubkeyfile,
282
289
ret = gnutls_certificate_set_openpgp_key_file
283
(es->cred, CERTFILE, KEYFILE, GNUTLS_OPENPGP_FMT_BASE64);
290
(es->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
284
291
if (ret != GNUTLS_E_SUCCESS) {
286
293
(stderr, "Error[%d] while reading the OpenPGP key pair ('%s',"
288
ret, CERTFILE, KEYFILE);
295
ret, pubkeyfile, seckeyfile);
289
296
fprintf(stdout, "The Error is: %s\n",
290
297
safer_gnutls_strerror(ret));
302
if ((ret = gnutls_dh_params_generate2 (es->dh_params, DH_BITS))
309
if ((ret = gnutls_dh_params_generate2 (es->dh_params, dh_bits))
303
310
!= GNUTLS_E_SUCCESS) {
304
311
fprintf (stderr, "Error in prime generation: %s\n",
305
312
safer_gnutls_strerror(ret));
335
342
gnutls_certificate_server_set_request (es->session,
336
343
GNUTLS_CERT_IGNORE);
338
gnutls_dh_set_prime_bits (es->session, DH_BITS);
345
gnutls_dh_set_prime_bits (es->session, dh_bits);
343
void empty_log(__attribute__((unused)) AvahiLogLevel level,
344
__attribute__((unused)) const char *txt){}
350
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
351
__attribute__((unused)) const char *txt){}
346
int start_mandos_communication(const char *ip, uint16_t port,
347
unsigned int if_index){
353
static int start_mandos_communication(const char *ip, uint16_t port,
354
AvahiIfIndex if_index){
349
356
struct sockaddr_in6 to;
350
357
encrypted_session es;
358
365
char interface[IF_NAMESIZE];
361
fprintf(stderr, "Setting up a tcp connection to %s\n", ip);
368
fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
364
372
tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
394
402
to.sin6_scope_id = (uint32_t)if_index;
397
fprintf(stderr, "Connection to: %s\n", ip);
405
fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
406
char addrstr[INET6_ADDRSTRLEN] = "";
407
if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
408
sizeof(addrstr)) == NULL){
411
if(strcmp(addrstr, ip) != 0){
412
fprintf(stderr, "Canonical address form: %s\n",
413
addrstr, ntohs(to.sin6_port));
400
418
ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
479
497
decrypted_buffer_size = pgp_packet_decrypt(buffer,
481
499
&decrypted_buffer,
483
501
if (decrypted_buffer_size >= 0){
484
while(written < decrypted_buffer_size){
502
while(written < (size_t) decrypted_buffer_size){
485
503
ret = (int)fwrite (decrypted_buffer + written, 1,
486
504
(size_t)decrypted_buffer_size - written,
556
574
fprintf(stderr, "Mandos server \"%s\" found on %s (%s) on"
557
575
" port %d\n", name, host_name, ip, port);
559
int ret = start_mandos_communication(ip, port,
560
(unsigned int) interface);
577
int ret = start_mandos_communication(ip, port, interface);
562
579
exit(EXIT_SUCCESS);
635
/* Combines file name and path and returns the malloced new
636
string. some sane checks could/should be added */
637
static const char *combinepath(const char *first, const char *second){
638
size_t f_len = strlen(first);
639
size_t s_len = strlen(second);
640
char *tmp = malloc(f_len + s_len + 2);
645
memcpy(tmp, first, f_len);
649
memcpy(tmp + f_len + 1, second, s_len);
651
tmp[f_len + 1 + s_len] = '\0';
618
656
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
619
657
AvahiServerConfig config;
620
658
AvahiSServiceBrowser *sb = NULL;
623
662
int returncode = EXIT_SUCCESS;
624
const char *interface = "eth0";
663
const char *interface = NULL;
664
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
665
char *connect_to = NULL;
667
debug_int = debug ? 1 : 0;
627
669
static struct option long_options[] = {
628
{"debug", no_argument, (int *)&debug, 1},
629
{"interface", required_argument, 0, 'i'},
670
{"debug", no_argument, &debug_int, 1},
671
{"connect", required_argument, NULL, 'C'},
672
{"interface", required_argument, NULL, 'i'},
673
{"keydir", required_argument, NULL, 'd'},
674
{"seckey", required_argument, NULL, 'c'},
675
{"pubkey", required_argument, NULL, 'k'},
676
{"dh-bits", required_argument, NULL, 'D'},
632
679
int option_index = 0;
644
691
interface = optarg;
706
dh_bits = atoi(optarg);
647
711
exit(EXIT_FAILURE);
714
debug = debug_int ? true : false;
716
pubkeyfile = combinepath(keydir, pubkeyfile);
717
if (pubkeyfile == NULL){
718
perror("combinepath");
722
if(interface != NULL){
723
if_index = (AvahiIfIndex) if_nametoindex(interface);
725
fprintf(stderr, "No such interface: \"%s\"\n", interface);
730
if(connect_to != NULL){
731
/* Connect directly, do not use Zeroconf */
732
/* (Mainly meant for debugging) */
733
char *address = strrchr(connect_to, ':');
735
fprintf(stderr, "No colon in address\n");
739
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
741
perror("Bad port number");
745
address = connect_to;
746
ret = start_mandos_communication(address, port, if_index);
754
seckeyfile = combinepath(keydir, seckeyfile);
755
if (seckeyfile == NULL){
756
perror("combinepath");
652
761
avahi_set_log_function(empty_log);
687
796
/* Create the service browser */
688
sb = avahi_s_service_browser_new(server,
690
if_nametoindex(interface),
797
sb = avahi_s_service_browser_new(server, if_index,
691
798
AVAHI_PROTO_INET6,
692
799
"_mandos._tcp", NULL, 0,
693
800
browse_callback, server);