4
 * This file demonstrates how to use Avahi's core API, this is
 
 
5
 * the embeddable mDNS stack for embedded applications.
 
 
7
 * End user applications should *not* use this API and should use
 
 
8
 * the D-Bus or C APIs, please see
 
 
9
 * client-browse-services.c and glib-integration.c
 
 
11
 * I repeat, you probably do *not* want to use this example.
 
 
15
  This file is part of avahi.
 
 
17
  avahi is free software; you can redistribute it and/or modify it
 
 
18
  under the terms of the GNU Lesser General Public License as
 
 
19
  published by the Free Software Foundation; either version 2.1 of the
 
 
20
  License, or (at your option) any later version.
 
 
22
  avahi is distributed in the hope that it will be useful, but WITHOUT
 
 
23
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
 
24
  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
 
 
25
  Public License for more details.
 
 
27
  You should have received a copy of the GNU Lesser General Public
 
 
28
  License along with avahi; if not, write to the Free Software
 
 
29
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 
 
33
#define _LARGEFILE_SOURCE
 
 
34
#define _FILE_OFFSET_BITS 64
 
 
40
#include <net/if.h>             /* if_nametoindex */
 
 
42
#include <avahi-core/core.h>
 
 
43
#include <avahi-core/lookup.h>
 
 
44
#include <avahi-core/log.h>
 
 
45
#include <avahi-common/simple-watch.h>
 
 
46
#include <avahi-common/malloc.h>
 
 
47
#include <avahi-common/error.h>
 
 
50
#include <sys/types.h>          /* socket(), setsockopt(), inet_pton() */
 
 
51
#include <sys/socket.h>         /* socket(), setsockopt(), struct sockaddr_in6, struct in6_addr, inet_pton() */
 
 
52
#include <gnutls/gnutls.h>      /* ALL GNUTLS STUFF */
 
 
53
#include <gnutls/openpgp.h>     /* gnutls with openpgp stuff */
 
 
55
#include <unistd.h>             /* close() */
 
 
56
#include <netinet/in.h>
 
 
57
#include <stdbool.h>            /* true */
 
 
58
#include <string.h>             /* memset */
 
 
59
#include <arpa/inet.h>          /* inet_pton() */
 
 
60
#include <iso646.h>             /* not */
 
 
63
#include <errno.h>              /* perror() */
 
 
68
#define CERT_ROOT "/conf/conf.d/cryptkeyreq/"
 
 
70
#define CERTFILE CERT_ROOT "openpgp-client.txt"
 
 
71
#define KEYFILE CERT_ROOT "openpgp-client-key.txt"
 
 
72
#define BUFFER_SIZE 256
 
 
76
  gnutls_session_t session;
 
 
77
  gnutls_certificate_credentials_t cred;
 
 
78
  gnutls_dh_params_t dh_params;
 
 
82
ssize_t gpg_packet_decrypt (char *packet, size_t packet_size, char **new_packet, char *homedir){
 
 
83
  gpgme_data_t dh_crypto, dh_plain;
 
 
87
  size_t new_packet_capacity = 0;
 
 
88
  size_t new_packet_length = 0;
 
 
89
  gpgme_engine_info_t engine_info;
 
 
92
  gpgme_check_version(NULL);
 
 
93
  gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
 
 
95
  /* Set GPGME home directory */
 
 
96
  rc = gpgme_get_engine_info (&engine_info);
 
 
97
  if (rc != GPG_ERR_NO_ERROR){
 
 
98
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
 
 
99
            gpgme_strsource(rc), gpgme_strerror(rc));
 
 
102
  while(engine_info != NULL){
 
 
103
    if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
 
 
104
      gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
 
 
105
                            engine_info->file_name, homedir);
 
 
108
    engine_info = engine_info->next;
 
 
110
  if(engine_info == NULL){
 
 
111
    fprintf(stderr, "Could not set home dir to %s\n", homedir);
 
 
115
  /* Create new GPGME data buffer from packet buffer */
 
 
116
  rc = gpgme_data_new_from_mem(&dh_crypto, packet, packet_size, 0);
 
 
117
  if (rc != GPG_ERR_NO_ERROR){
 
 
118
    fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
 
 
119
            gpgme_strsource(rc), gpgme_strerror(rc));
 
 
123
  /* Create new empty GPGME data buffer for the plaintext */
 
 
124
  rc = gpgme_data_new(&dh_plain);
 
 
125
  if (rc != GPG_ERR_NO_ERROR){
 
 
126
    fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
 
 
127
            gpgme_strsource(rc), gpgme_strerror(rc));
 
 
131
  /* Create new GPGME "context" */
 
 
132
  rc = gpgme_new(&ctx);
 
 
133
  if (rc != GPG_ERR_NO_ERROR){
 
 
134
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
 
 
135
            gpgme_strsource(rc), gpgme_strerror(rc));
 
 
139
  /* Decrypt data from the FILE pointer to the plaintext data buffer */
 
 
140
  rc = gpgme_op_decrypt(ctx, dh_crypto, dh_plain);
 
 
141
  if (rc != GPG_ERR_NO_ERROR){
 
 
142
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
 
 
143
            gpgme_strsource(rc), gpgme_strerror(rc));
 
 
147
/*   gpgme_decrypt_result_t result; */
 
 
148
/*   result = gpgme_op_decrypt_result(ctx); */
 
 
149
/*   fprintf(stderr, "Unsupported algorithm: %s\n", result->unsupported_algorithm); */
 
 
150
/*   fprintf(stderr, "Wrong key usage: %d\n", result->wrong_key_usage); */
 
 
151
/*   if(result->file_name != NULL){ */
 
 
152
/*     fprintf(stderr, "File name: %s\n", result->file_name); */
 
 
154
/*   gpgme_recipient_t recipient; */
 
 
155
/*   recipient = result->recipients; */
 
 
157
/*     while(recipient != NULL){ */
 
 
158
/*       fprintf(stderr, "Public key algorithm: %s\n", */
 
 
159
/*            gpgme_pubkey_algo_name(recipient->pubkey_algo)); */
 
 
160
/*       fprintf(stderr, "Key ID: %s\n", recipient->keyid); */
 
 
161
/*       fprintf(stderr, "Secret key available: %s\n", */
 
 
162
/*            recipient->status == GPG_ERR_NO_SECKEY ? "No" : "Yes"); */
 
 
163
/*       recipient = recipient->next; */
 
 
167
  /* Delete the GPGME FILE pointer cryptotext data buffer */
 
 
168
  gpgme_data_release(dh_crypto);
 
 
170
  /* Seek back to the beginning of the GPGME plaintext data buffer */
 
 
171
  gpgme_data_seek(dh_plain, 0, SEEK_SET);
 
 
175
    if (new_packet_length + BUFFER_SIZE > new_packet_capacity){
 
 
176
      *new_packet = realloc(*new_packet, new_packet_capacity + BUFFER_SIZE);
 
 
177
      if (*new_packet == NULL){
 
 
181
      new_packet_capacity += BUFFER_SIZE;
 
 
184
    ret = gpgme_data_read(dh_plain, *new_packet + new_packet_length, BUFFER_SIZE);
 
 
185
    /* Print the data, if any */
 
 
187
      /* If password is empty, then a incorrect error will be printed */
 
 
191
      perror("gpgme_data_read");
 
 
194
    new_packet_length += ret;
 
 
197
   /* Delete the GPGME plaintext data buffer */
 
 
198
  gpgme_data_release(dh_plain);
 
 
199
  return new_packet_length;
 
 
202
static const char * safer_gnutls_strerror (int value) {
 
 
203
  const char *ret = gnutls_strerror (value);
 
 
209
void debuggnutls(int level, const char* string){
 
 
210
  fprintf(stderr, "%s", string);
 
 
213
int initgnutls(encrypted_session *es){
 
 
217
  if ((ret = gnutls_global_init ())
 
 
218
      != GNUTLS_E_SUCCESS) {
 
 
219
    fprintf (stderr, "global_init: %s\n", safer_gnutls_strerror(ret));
 
 
223
  /* Uncomment to enable full debuggin on the gnutls library */
 
 
224
  /*   gnutls_global_set_log_level(11); */
 
 
225
  /*   gnutls_global_set_log_function(debuggnutls); */
 
 
228
  /* openpgp credentials */
 
 
229
  if ((ret = gnutls_certificate_allocate_credentials (&es->cred))
 
 
230
      != GNUTLS_E_SUCCESS) {
 
 
231
    fprintf (stderr, "memory error: %s\n", safer_gnutls_strerror(ret));
 
 
235
  ret = gnutls_certificate_set_openpgp_key_file
 
 
236
    (es->cred, CERTFILE, KEYFILE, GNUTLS_OPENPGP_FMT_BASE64);
 
 
237
  if (ret != GNUTLS_E_SUCCESS) {
 
 
239
      (stderr, "Error[%d] while reading the OpenPGP key pair ('%s', '%s')\n",
 
 
240
       ret, CERTFILE, KEYFILE);
 
 
241
    fprintf(stdout, "The Error is: %s\n",
 
 
242
            safer_gnutls_strerror(ret));
 
 
246
  //Gnutls server initialization
 
 
247
  if ((ret = gnutls_dh_params_init (&es->dh_params))
 
 
248
      != GNUTLS_E_SUCCESS) {
 
 
249
    fprintf (stderr, "Error in dh parameter initialization: %s\n",
 
 
250
             safer_gnutls_strerror(ret));
 
 
254
  if ((ret = gnutls_dh_params_generate2 (es->dh_params, DH_BITS))
 
 
255
      != GNUTLS_E_SUCCESS) {
 
 
256
    fprintf (stderr, "Error in prime generation: %s\n",
 
 
257
             safer_gnutls_strerror(ret));
 
 
261
  gnutls_certificate_set_dh_params (es->cred, es->dh_params);
 
 
263
  // Gnutls session creation
 
 
264
  if ((ret = gnutls_init (&es->session, GNUTLS_SERVER))
 
 
265
      != GNUTLS_E_SUCCESS){
 
 
266
    fprintf(stderr, "Error in gnutls session initialization: %s\n",
 
 
267
            safer_gnutls_strerror(ret));
 
 
270
  if ((ret = gnutls_priority_set_direct (es->session, "NORMAL", &err))
 
 
271
      != GNUTLS_E_SUCCESS) {
 
 
272
    fprintf(stderr, "Syntax error at: %s\n", err);
 
 
273
    fprintf(stderr, "Gnutls error: %s\n",
 
 
274
            safer_gnutls_strerror(ret));
 
 
278
  if ((ret = gnutls_credentials_set
 
 
279
       (es->session, GNUTLS_CRD_CERTIFICATE, es->cred))
 
 
280
      != GNUTLS_E_SUCCESS) {
 
 
281
    fprintf(stderr, "Error setting a credentials set: %s\n",
 
 
282
            safer_gnutls_strerror(ret));
 
 
286
  /* ignore client certificate if any. */
 
 
287
  gnutls_certificate_server_set_request (es->session, GNUTLS_CERT_IGNORE);
 
 
289
  gnutls_dh_set_prime_bits (es->session, DH_BITS);
 
 
294
void empty_log(AvahiLogLevel level, const char *txt){}
 
 
296
int start_mandos_communcation(char *ip, uint16_t port){
 
 
298
  struct sockaddr_in6 to;
 
 
299
  struct in6_addr ip_addr;
 
 
300
  encrypted_session es;
 
 
302
  char *decrypted_buffer;
 
 
303
  size_t buffer_length = 0;
 
 
304
  size_t buffer_capacity = 0;
 
 
305
  ssize_t decrypted_buffer_size;
 
 
309
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
 
 
315
  ret = setsockopt(tcp_sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
 
 
317
    perror("setsockopt bindtodevice");
 
 
321
  memset(&to,0,sizeof(to));
 
 
322
  to.sin6_family = AF_INET6;
 
 
323
  ret = inet_pton(AF_INET6, ip, &ip_addr);
 
 
329
    fprintf(stderr, "Bad address: %s\n", ip);
 
 
332
  to.sin6_port = htons(port);
 
 
333
  to.sin6_scope_id = if_nametoindex("eth0");
 
 
335
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
 
 
341
  ret = initgnutls (&es);
 
 
348
  gnutls_transport_set_ptr (es.session, (gnutls_transport_ptr_t) tcp_sd);
 
 
350
  ret = gnutls_handshake (es.session);
 
 
352
  if (ret != GNUTLS_E_SUCCESS){
 
 
353
    fprintf(stderr, "\n*** Handshake failed ***\n");
 
 
361
    if (buffer_length + BUFFER_SIZE > buffer_capacity){
 
 
362
      buffer = realloc(buffer, buffer_capacity + BUFFER_SIZE);
 
 
367
      buffer_capacity += BUFFER_SIZE;
 
 
370
    ret = gnutls_record_recv
 
 
371
      (es.session, buffer+buffer_length, BUFFER_SIZE);
 
 
377
      case GNUTLS_E_INTERRUPTED:
 
 
380
      case GNUTLS_E_REHANDSHAKE:
 
 
381
        ret = gnutls_handshake (es.session);
 
 
383
          fprintf(stderr, "\n*** Handshake failed ***\n");
 
 
390
        fprintf(stderr, "Unknown error while reading data from encrypted session with mandos server\n");
 
 
392
        gnutls_bye (es.session, GNUTLS_SHUT_RDWR);
 
 
396
      buffer_length += ret;
 
 
400
  if (buffer_length > 0){
 
 
401
    if ((decrypted_buffer_size = gpg_packet_decrypt(buffer, buffer_length, &decrypted_buffer, CERT_ROOT)) == 0){
 
 
404
      fwrite (decrypted_buffer, 1, decrypted_buffer_size, stdout);
 
 
405
      free(decrypted_buffer);
 
 
412
  gnutls_bye (es.session, GNUTLS_SHUT_RDWR);
 
 
415
  gnutls_deinit (es.session);
 
 
416
  gnutls_certificate_free_credentials (es.cred);
 
 
417
  gnutls_global_deinit ();
 
 
421
static AvahiSimplePoll *simple_poll = NULL;
 
 
422
static AvahiServer *server = NULL;
 
 
424
static void resolve_callback(
 
 
425
    AvahiSServiceResolver *r,
 
 
426
    AVAHI_GCC_UNUSED AvahiIfIndex interface,
 
 
427
    AVAHI_GCC_UNUSED AvahiProtocol protocol,
 
 
428
    AvahiResolverEvent event,
 
 
432
    const char *host_name,
 
 
433
    const AvahiAddress *address,
 
 
435
    AvahiStringList *txt,
 
 
436
    AvahiLookupResultFlags flags,
 
 
437
    AVAHI_GCC_UNUSED void* userdata) {
 
 
441
    /* Called whenever a service has been resolved successfully or timed out */
 
 
444
        case AVAHI_RESOLVER_FAILURE:
 
 
445
            fprintf(stderr, "(Resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_server_errno(server)));
 
 
448
        case AVAHI_RESOLVER_FOUND: {
 
 
449
          char ip[AVAHI_ADDRESS_STR_MAX];
 
 
450
            avahi_address_snprint(ip, sizeof(ip), address);
 
 
451
            int ret = start_mandos_communcation(ip, port);
 
 
459
    avahi_s_service_resolver_free(r);
 
 
462
static void browse_callback(
 
 
463
    AvahiSServiceBrowser *b,
 
 
464
    AvahiIfIndex interface,
 
 
465
    AvahiProtocol protocol,
 
 
466
    AvahiBrowserEvent event,
 
 
470
    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
 
 
473
    AvahiServer *s = userdata;
 
 
476
    /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
 
 
480
        case AVAHI_BROWSER_FAILURE:
 
 
482
            fprintf(stderr, "(Browser) %s\n", avahi_strerror(avahi_server_errno(server)));
 
 
483
            avahi_simple_poll_quit(simple_poll);
 
 
486
        case AVAHI_BROWSER_NEW:
 
 
487
            /* We ignore the returned resolver object. In the callback
 
 
488
               function we free it. If the server is terminated before
 
 
489
               the callback function is called the server will free
 
 
490
               the resolver for us. */
 
 
492
            if (!(avahi_s_service_resolver_new(s, interface, protocol, name, type, domain, AVAHI_PROTO_INET6, 0, resolve_callback, s)))
 
 
493
                fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_server_errno(s)));
 
 
497
        case AVAHI_BROWSER_REMOVE:
 
 
500
        case AVAHI_BROWSER_ALL_FOR_NOW:
 
 
501
        case AVAHI_BROWSER_CACHE_EXHAUSTED:
 
 
506
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
 
 
507
    AvahiServerConfig config;
 
 
508
    AvahiSServiceBrowser *sb = NULL;
 
 
512
    avahi_set_log_function(empty_log);
 
 
514
    /* Initialize the psuedo-RNG */
 
 
517
    /* Allocate main loop object */
 
 
518
    if (!(simple_poll = avahi_simple_poll_new())) {
 
 
519
        fprintf(stderr, "Failed to create simple poll object.\n");
 
 
523
    /* Do not publish any local records */
 
 
524
    avahi_server_config_init(&config);
 
 
525
    config.publish_hinfo = 0;
 
 
526
    config.publish_addresses = 0;
 
 
527
    config.publish_workstation = 0;
 
 
528
    config.publish_domain = 0;
 
 
530
/*     /\* Set a unicast DNS server for wide area DNS-SD *\/ */
 
 
531
/*     avahi_address_parse("193.11.177.11", AVAHI_PROTO_UNSPEC, &config.wide_area_servers[0]); */
 
 
532
/*     config.n_wide_area_servers = 1; */
 
 
533
/*     config.enable_wide_area = 1; */
 
 
535
    /* Allocate a new server */
 
 
536
    server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, NULL, NULL, &error);
 
 
538
    /* Free the configuration data */
 
 
539
    avahi_server_config_free(&config);
 
 
541
    /* Check wether creating the server object succeeded */
 
 
543
        fprintf(stderr, "Failed to create server: %s\n", avahi_strerror(error));
 
 
547
    /* Create the service browser */
 
 
548
    if (!(sb = avahi_s_service_browser_new(server, if_nametoindex("eth0"), AVAHI_PROTO_INET6, "_mandos._tcp", NULL, 0, browse_callback, server))) {
 
 
549
        fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_server_errno(server)));
 
 
553
    /* Run the main loop */
 
 
554
    avahi_simple_poll_loop(simple_poll);
 
 
562
        avahi_s_service_browser_free(sb);
 
 
565
        avahi_server_free(server);
 
 
568
        avahi_simple_poll_free(simple_poll);