63
63
#include <errno.h> /* perror() */
67
67
#include <getopt.h>
69
69
#define BUFFER_SIZE 256
72
const char *certdir = "/conf/conf.d/cryptkeyreq/";
73
const char *certfile = "openpgp-client.txt";
74
const char *certkey = "openpgp-client-key.txt";
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";
76
77
bool debug = false;
79
81
gnutls_session_t session;
80
82
gnutls_certificate_credentials_t cred;
82
84
} encrypted_session;
85
ssize_t pgp_packet_decrypt (char *packet, size_t packet_size,
86
char **new_packet, const char *homedir){
87
static ssize_t pgp_packet_decrypt (char *packet, size_t packet_size,
87
90
gpgme_data_t dh_crypto, dh_plain;
248
void debuggnutls(__attribute__((unused)) int level,
251
static void debuggnutls(__attribute__((unused)) int level,
250
253
fprintf(stderr, "%s", string);
253
int initgnutls(encrypted_session *es){
256
static int initgnutls(encrypted_session *es){
281
284
fprintf(stderr, "Attempting to use OpenPGP certificate %s"
282
" and keyfile %s as GnuTLS credentials\n", certfile,
285
" and keyfile %s as GnuTLS credentials\n", pubkeyfile,
286
289
ret = gnutls_certificate_set_openpgp_key_file
287
(es->cred, certfile, certkey, GNUTLS_OPENPGP_FMT_BASE64);
290
(es->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
288
291
if (ret != GNUTLS_E_SUCCESS) {
290
293
(stderr, "Error[%d] while reading the OpenPGP key pair ('%s',"
292
ret, certfile, certkey);
295
ret, pubkeyfile, seckeyfile);
293
296
fprintf(stdout, "The Error is: %s\n",
294
297
safer_gnutls_strerror(ret));
306
if ((ret = gnutls_dh_params_generate2 (es->dh_params, DH_BITS))
309
if ((ret = gnutls_dh_params_generate2 (es->dh_params, dh_bits))
307
310
!= GNUTLS_E_SUCCESS) {
308
311
fprintf (stderr, "Error in prime generation: %s\n",
309
312
safer_gnutls_strerror(ret));
339
342
gnutls_certificate_server_set_request (es->session,
340
343
GNUTLS_CERT_IGNORE);
342
gnutls_dh_set_prime_bits (es->session, DH_BITS);
345
gnutls_dh_set_prime_bits (es->session, dh_bits);
347
void empty_log(__attribute__((unused)) AvahiLogLevel level,
348
__attribute__((unused)) const char *txt){}
350
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
351
__attribute__((unused)) const char *txt){}
350
int start_mandos_communication(const char *ip, uint16_t port,
351
AvahiIfIndex if_index){
353
static int start_mandos_communication(const char *ip, uint16_t port,
354
AvahiIfIndex if_index){
353
356
struct sockaddr_in6 to;
354
357
encrypted_session es;
402
405
fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
403
/* char addrstr[INET6_ADDRSTRLEN]; */
404
/* if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr, */
405
/* sizeof(addrstr)) == NULL){ */
406
/* perror("inet_ntop"); */
408
/* fprintf(stderr, "Really connecting to: %s, port %d\n", */
409
/* addrstr, ntohs(to.sin6_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));
413
418
ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
492
497
decrypted_buffer_size = pgp_packet_decrypt(buffer,
494
499
&decrypted_buffer,
496
501
if (decrypted_buffer_size >= 0){
497
502
while(written < (size_t) decrypted_buffer_size){
498
503
ret = (int)fwrite (decrypted_buffer + written, 1,
630
/* combinds file name and path and returns the malloced new string. som sane checks could/should be added */
631
const char *combinepath(const char *first, const char *second){
633
tmp = malloc(strlen(first) + strlen(second) + 2);
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);
634
641
if (tmp == NULL){
639
if (first[0] != '\0' and first[strlen(first) - 1] != '/'){
645
memcpy(tmp, first, f_len);
649
memcpy(tmp + f_len + 1, second, s_len);
651
tmp[f_len + 1 + s_len] = '\0';
649
658
AvahiSServiceBrowser *sb = NULL;
652
662
int returncode = EXIT_SUCCESS;
653
663
const char *interface = NULL;
654
664
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
655
665
char *connect_to = NULL;
667
debug_int = debug ? 1 : 0;
658
669
static struct option long_options[] = {
659
{"debug", no_argument, (int *)&debug, 1},
660
{"connect", required_argument, 0, 'C'},
661
{"interface", required_argument, 0, 'i'},
662
{"certdir", required_argument, 0, 'd'},
663
{"certkey", required_argument, 0, 'c'},
664
{"certfile", required_argument, 0, 'k'},
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'},
667
679
int option_index = 0;