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
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
77
70
#define DH_BITS 1024
72
static const char *certdir = "/conf/conf.d/mandos";
73
static const char *certfile = "openpgp-client.txt";
74
static const char *certkey = "openpgp-client-key.txt";
79
76
bool debug = false;
85
82
} encrypted_session;
88
ssize_t pgp_packet_decrypt (char *packet, size_t packet_size,
89
char **new_packet, const char *homedir){
85
static ssize_t pgp_packet_decrypt (char *packet, size_t packet_size,
90
88
gpgme_data_t dh_crypto, dh_plain;
103
101
gpgme_check_version(NULL);
104
gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
102
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
103
if (rc != GPG_ERR_NO_ERROR){
104
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
105
gpgme_strsource(rc), gpgme_strerror(rc));
106
109
/* Set GPGME home directory */
107
110
rc = gpgme_get_engine_info (&engine_info);
193
196
gpgme_data_release(dh_crypto);
195
198
/* Seek back to the beginning of the GPGME plaintext data buffer */
196
gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET);
199
if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
200
perror("pgpme_data_seek");
200
205
if (new_packet_length + BUFFER_SIZE > new_packet_capacity){
244
void debuggnutls(__attribute__((unused)) int level,
249
static void debuggnutls(__attribute__((unused)) int level,
246
251
fprintf(stderr, "%s", string);
249
int initgnutls(encrypted_session *es){
254
static int initgnutls(encrypted_session *es){
254
259
fprintf(stderr, "Initializing GnuTLS\n");
257
262
if ((ret = gnutls_global_init ())
258
263
!= GNUTLS_E_SUCCESS) {
259
264
fprintf (stderr, "global_init: %s\n", safer_gnutls_strerror(ret));
277
282
fprintf(stderr, "Attempting to use OpenPGP certificate %s"
278
" and keyfile %s as GnuTLS credentials\n", CERTFILE,
283
" and keyfile %s as GnuTLS credentials\n", certfile,
282
287
ret = gnutls_certificate_set_openpgp_key_file
283
(es->cred, CERTFILE, KEYFILE, GNUTLS_OPENPGP_FMT_BASE64);
288
(es->cred, certfile, certkey, GNUTLS_OPENPGP_FMT_BASE64);
284
289
if (ret != GNUTLS_E_SUCCESS) {
286
291
(stderr, "Error[%d] while reading the OpenPGP key pair ('%s',"
288
ret, CERTFILE, KEYFILE);
293
ret, certfile, certkey);
289
294
fprintf(stdout, "The Error is: %s\n",
290
295
safer_gnutls_strerror(ret));
343
void empty_log(__attribute__((unused)) AvahiLogLevel level,
344
__attribute__((unused)) const char *txt){}
348
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
349
__attribute__((unused)) const char *txt){}
346
int start_mandos_communication(const char *ip, uint16_t port,
347
unsigned int if_index){
351
static int start_mandos_communication(const char *ip, uint16_t port,
352
AvahiIfIndex if_index){
349
354
struct sockaddr_in6 to;
350
355
encrypted_session es;
358
363
char interface[IF_NAMESIZE];
361
fprintf(stderr, "Setting up a tcp connection to %s\n", ip);
366
fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
364
370
tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
394
400
to.sin6_scope_id = (uint32_t)if_index;
397
fprintf(stderr, "Connection to: %s\n", ip);
403
fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
404
/* char addrstr[INET6_ADDRSTRLEN]; */
405
/* if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr, */
406
/* sizeof(addrstr)) == NULL){ */
407
/* perror("inet_ntop"); */
409
/* fprintf(stderr, "Really connecting to: %s, port %d\n", */
410
/* addrstr, ntohs(to.sin6_port)); */
400
414
ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
479
493
decrypted_buffer_size = pgp_packet_decrypt(buffer,
481
495
&decrypted_buffer,
483
497
if (decrypted_buffer_size >= 0){
484
while(written < decrypted_buffer_size){
498
while(written < (size_t) decrypted_buffer_size){
485
499
ret = (int)fwrite (decrypted_buffer + written, 1,
486
500
(size_t)decrypted_buffer_size - written,
556
570
fprintf(stderr, "Mandos server \"%s\" found on %s (%s) on"
557
571
" port %d\n", name, host_name, ip, port);
559
int ret = start_mandos_communication(ip, port,
560
(unsigned int) interface);
573
int ret = start_mandos_communication(ip, port, interface);
562
575
exit(EXIT_SUCCESS);
631
/* Combines file name and path and returns the malloced new
632
string. some sane checks could/should be added */
633
static const char *combinepath(const char *first, const char *second){
634
size_t f_len = strlen(first);
635
size_t s_len = strlen(second);
636
char *tmp = malloc(f_len + s_len + 2);
641
memcpy(tmp, first, f_len);
645
memcpy(tmp + f_len + 1, second, s_len);
647
tmp[f_len + 1 + s_len] = '\0';
618
652
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
619
653
AvahiServerConfig config;
620
654
AvahiSServiceBrowser *sb = NULL;
623
657
int returncode = EXIT_SUCCESS;
624
const char *interface = "eth0";
658
const char *interface = NULL;
659
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
660
char *connect_to = NULL;
627
663
static struct option long_options[] = {
628
664
{"debug", no_argument, (int *)&debug, 1},
665
{"connect", required_argument, 0, 'C'},
629
666
{"interface", required_argument, 0, 'i'},
667
{"certdir", required_argument, 0, 'd'},
668
{"certkey", required_argument, 0, 'c'},
669
{"certfile", required_argument, 0, 'k'},
632
672
int option_index = 0;
644
684
interface = optarg;
647
699
exit(EXIT_FAILURE);
703
certfile = combinepath(certdir, certfile);
704
if (certfile == NULL){
705
perror("combinepath");
709
if(interface != NULL){
710
if_index = (AvahiIfIndex) if_nametoindex(interface);
712
fprintf(stderr, "No such interface: \"%s\"\n", interface);
717
if(connect_to != NULL){
718
/* Connect directly, do not use Zeroconf */
719
/* (Mainly meant for debugging) */
720
char *address = strrchr(connect_to, ':');
722
fprintf(stderr, "No colon in address\n");
726
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
728
perror("Bad port number");
732
address = connect_to;
733
ret = start_mandos_communication(address, port, if_index);
741
certkey = combinepath(certdir, certkey);
742
if (certkey == NULL){
743
perror("combinepath");
652
748
avahi_set_log_function(empty_log);
687
783
/* Create the service browser */
688
sb = avahi_s_service_browser_new(server,
690
if_nametoindex(interface),
784
sb = avahi_s_service_browser_new(server, if_index,
691
785
AVAHI_PROTO_INET6,
692
786
"_mandos._tcp", NULL, 0,
693
787
browse_callback, server);