64
62
#include <string.h> /* memset */
65
63
#include <arpa/inet.h> /* inet_pton() */
66
64
#include <iso646.h> /* not */
67
#include <net/if.h> /* IF_NAMESIZE */
68
#include <argp.h> /* struct argp_option,
69
struct argp_state, struct argp,
72
67
#include <errno.h> /* perror() */
74
#define CERT_ROOT "/conf/conf.d/cryptkeyreq/"
76
#define CERTFILE CERT_ROOT "openpgp-client.txt"
77
#define KEYFILE CERT_ROOT "openpgp-client-key.txt"
75
78
#define BUFFER_SIZE 256
77
81
bool debug = false;
78
static const char *keydir = "/conf/conf.d/mandos";
79
static const char mandos_protocol_version[] = "1";
80
const char *argp_program_version = "mandosclient 0.9";
81
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
83
/* Used for passing in values through the Avahi callback functions */
85
AvahiSimplePoll *simple_poll;
84
gnutls_session_t session;
87
85
gnutls_certificate_credentials_t cred;
89
86
gnutls_dh_params_t dh_params;
94
* Make room in "buffer" for at least BUFFER_SIZE additional bytes.
95
* "buffer_capacity" is how much is currently allocated,
96
* "buffer_length" is how much is already used.
98
size_t adjustbuffer(char **buffer, size_t buffer_length,
99
size_t buffer_capacity){
100
if (buffer_length + BUFFER_SIZE > buffer_capacity){
101
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
105
buffer_capacity += BUFFER_SIZE;
107
return buffer_capacity;
111
* Decrypt OpenPGP data using keyrings in HOMEDIR.
112
* Returns -1 on error
114
static ssize_t pgp_packet_decrypt (const char *cryptotext,
117
const char *homedir){
90
ssize_t pgp_packet_decrypt (char *packet, size_t packet_size,
91
char **new_packet, const char *homedir){
118
92
gpgme_data_t dh_crypto, dh_plain;
122
size_t plaintext_capacity = 0;
123
ssize_t plaintext_length = 0;
96
ssize_t new_packet_capacity = 0;
97
ssize_t new_packet_length = 0;
124
98
gpgme_engine_info_t engine_info;
127
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
101
fprintf(stderr, "Trying to decrypt OpenPGP packet\n");
131
105
gpgme_check_version(NULL);
132
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
133
if (rc != GPG_ERR_NO_ERROR){
134
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
135
gpgme_strsource(rc), gpgme_strerror(rc));
106
gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
139
/* Set GPGME home directory for the OpenPGP engine only */
108
/* Set GPGME home directory */
140
109
rc = gpgme_get_engine_info (&engine_info);
141
110
if (rc != GPG_ERR_NO_ERROR){
142
111
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
287
/* GnuTLS log function callback */
288
static void debuggnutls(__attribute__((unused)) int level,
290
fprintf(stderr, "GnuTLS: %s", string);
246
void debuggnutls(__attribute__((unused)) int level,
248
fprintf(stderr, "%s", string);
293
static int init_gnutls_global(mandos_context *mc,
294
const char *pubkeyfile,
295
const char *seckeyfile){
251
int initgnutls(encrypted_session *es){
299
256
fprintf(stderr, "Initializing GnuTLS\n");
302
259
if ((ret = gnutls_global_init ())
303
260
!= GNUTLS_E_SUCCESS) {
304
fprintf (stderr, "GnuTLS global_init: %s\n",
305
safer_gnutls_strerror(ret));
261
fprintf (stderr, "global_init: %s\n", safer_gnutls_strerror(ret));
310
/* "Use a log level over 10 to enable all debugging options."
313
266
gnutls_global_set_log_level(11);
314
267
gnutls_global_set_log_function(debuggnutls);
317
/* OpenPGP credentials */
318
if ((ret = gnutls_certificate_allocate_credentials (&mc->cred))
270
/* openpgp credentials */
271
if ((ret = gnutls_certificate_allocate_credentials (&es->cred))
319
272
!= GNUTLS_E_SUCCESS) {
320
fprintf (stderr, "GnuTLS memory error: %s\n",
273
fprintf (stderr, "memory error: %s\n",
321
274
safer_gnutls_strerror(ret));
322
gnutls_global_deinit ();
327
279
fprintf(stderr, "Attempting to use OpenPGP certificate %s"
328
" and keyfile %s as GnuTLS credentials\n", pubkeyfile,
280
" and keyfile %s as GnuTLS credentials\n", CERTFILE,
332
284
ret = gnutls_certificate_set_openpgp_key_file
333
(mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
285
(es->cred, CERTFILE, KEYFILE, GNUTLS_OPENPGP_FMT_BASE64);
334
286
if (ret != GNUTLS_E_SUCCESS) {
336
"Error[%d] while reading the OpenPGP key pair ('%s',"
337
" '%s')\n", ret, pubkeyfile, seckeyfile);
338
fprintf(stdout, "The GnuTLS error is: %s\n",
288
(stderr, "Error[%d] while reading the OpenPGP key pair ('%s',"
290
ret, CERTFILE, KEYFILE);
291
fprintf(stdout, "The Error is: %s\n",
339
292
safer_gnutls_strerror(ret));
343
/* GnuTLS server initialization */
344
ret = gnutls_dh_params_init(&mc->dh_params);
345
if (ret != GNUTLS_E_SUCCESS) {
346
fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
347
" %s\n", safer_gnutls_strerror(ret));
350
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
351
if (ret != GNUTLS_E_SUCCESS) {
352
fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
353
safer_gnutls_strerror(ret));
357
gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
363
gnutls_certificate_free_credentials (mc->cred);
364
gnutls_global_deinit ();
369
static int init_gnutls_session(mandos_context *mc,
370
gnutls_session_t *session){
372
/* GnuTLS session creation */
373
ret = gnutls_init(session, GNUTLS_SERVER);
374
if (ret != GNUTLS_E_SUCCESS){
296
//GnuTLS server initialization
297
if ((ret = gnutls_dh_params_init (&es->dh_params))
298
!= GNUTLS_E_SUCCESS) {
299
fprintf (stderr, "Error in dh parameter initialization: %s\n",
300
safer_gnutls_strerror(ret));
304
if ((ret = gnutls_dh_params_generate2 (es->dh_params, DH_BITS))
305
!= GNUTLS_E_SUCCESS) {
306
fprintf (stderr, "Error in prime generation: %s\n",
307
safer_gnutls_strerror(ret));
311
gnutls_certificate_set_dh_params (es->cred, es->dh_params);
313
// GnuTLS session creation
314
if ((ret = gnutls_init (&es->session, GNUTLS_SERVER))
315
!= GNUTLS_E_SUCCESS){
375
316
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
376
317
safer_gnutls_strerror(ret));
381
ret = gnutls_priority_set_direct(*session, mc->priority, &err);
382
if (ret != GNUTLS_E_SUCCESS) {
383
fprintf(stderr, "Syntax error at: %s\n", err);
384
fprintf(stderr, "GnuTLS error: %s\n",
385
safer_gnutls_strerror(ret));
386
gnutls_deinit (*session);
320
if ((ret = gnutls_priority_set_direct (es->session, "NORMAL", &err))
321
!= GNUTLS_E_SUCCESS) {
322
fprintf(stderr, "Syntax error at: %s\n", err);
323
fprintf(stderr, "GnuTLS error: %s\n",
324
safer_gnutls_strerror(ret));
391
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
393
if (ret != GNUTLS_E_SUCCESS) {
394
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
328
if ((ret = gnutls_credentials_set
329
(es->session, GNUTLS_CRD_CERTIFICATE, es->cred))
330
!= GNUTLS_E_SUCCESS) {
331
fprintf(stderr, "Error setting a credentials set: %s\n",
395
332
safer_gnutls_strerror(ret));
396
gnutls_deinit (*session);
400
336
/* ignore client certificate if any. */
401
gnutls_certificate_server_set_request (*session,
337
gnutls_certificate_server_set_request (es->session,
402
338
GNUTLS_CERT_IGNORE);
404
gnutls_dh_set_prime_bits (*session, mc->dh_bits);
340
gnutls_dh_set_prime_bits (es->session, DH_BITS);
409
/* Avahi log function callback */
410
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
411
__attribute__((unused)) const char *txt){}
345
void empty_log(__attribute__((unused)) AvahiLogLevel level,
346
__attribute__((unused)) const char *txt){}
413
/* Called when a Mandos server is found */
414
static int start_mandos_communication(const char *ip, uint16_t port,
415
AvahiIfIndex if_index,
348
int start_mandos_communication(char *ip, uint16_t port,
349
unsigned int if_index){
418
union { struct sockaddr in; struct sockaddr_in6 in6; } to;
351
struct sockaddr_in6 to;
352
encrypted_session es;
419
353
char *buffer = NULL;
420
354
char *decrypted_buffer;
421
355
size_t buffer_length = 0;
422
356
size_t buffer_capacity = 0;
423
357
ssize_t decrypted_buffer_size;
426
359
char interface[IF_NAMESIZE];
427
gnutls_session_t session;
429
ret = init_gnutls_session (mc, &session);
435
fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
362
fprintf(stderr, "Setting up a tcp connection to %s\n", ip);
439
365
tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
441
367
perror("socket");
446
if(if_indextoname((unsigned int)if_index, interface) == NULL){
371
if(if_indextoname(if_index, interface) == NULL){
447
373
perror("if_indextoname");
450
379
fprintf(stderr, "Binding to interface %s\n", interface);
453
memset(&to,0,sizeof(to)); /* Spurious warning */
454
to.in6.sin6_family = AF_INET6;
455
/* It would be nice to have a way to detect if we were passed an
456
IPv4 address here. Now we assume an IPv6 address. */
457
ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
382
ret = setsockopt(tcp_sd, SOL_SOCKET, SO_BINDTODEVICE, interface, 5);
384
perror("setsockopt bindtodevice");
388
memset(&to,0,sizeof(to));
389
to.sin6_family = AF_INET6;
390
ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
459
392
perror("inet_pton");
463
396
fprintf(stderr, "Bad address: %s\n", ip);
466
to.in6.sin6_port = htons(port); /* Spurious warning */
399
/* Spurious warnings for the next line, see for instance
400
<http://bugs.debian.org/488884> */
401
to.sin6_port = htons(port);
468
to.in6.sin6_scope_id = (uint32_t)if_index;
403
to.sin6_scope_id = (uint32_t)if_index;
471
fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
472
char addrstr[INET6_ADDRSTRLEN] = "";
473
if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
474
sizeof(addrstr)) == NULL){
477
if(strcmp(addrstr, ip) != 0){
478
fprintf(stderr, "Canonical address form: %s\n", addrstr);
406
fprintf(stderr, "Connection to: %s\n", ip);
483
ret = connect(tcp_sd, &to.in, sizeof(to));
409
ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
485
411
perror("connect");
489
const char *out = mandos_protocol_version;
492
size_t out_size = strlen(out);
493
ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
494
out_size - written));
500
written += (size_t)ret;
501
if(written < out_size){
504
if (out == mandos_protocol_version){
415
ret = initgnutls (&es);
421
gnutls_transport_set_ptr (es.session,
422
(gnutls_transport_ptr_t) tcp_sd);
514
425
fprintf(stderr, "Establishing TLS session with %s\n", ip);
517
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
519
ret = gnutls_handshake (session);
428
ret = gnutls_handshake (es.session);
521
430
if (ret != GNUTLS_E_SUCCESS){
523
fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
431
fprintf(stderr, "\n*** Handshake failed ***\n");
530
/* Read OpenPGP packet that contains the wanted password */
437
//Retrieve OpenPGP packet that contains the wanted password
533
440
fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
602
written += (size_t)ret;
502
decrypted_buffer += ret;
503
decrypted_buffer_size -= ret;
604
505
free(decrypted_buffer);
610
/* Shutdown procedure */
514
fprintf(stderr, "Closing TLS session\n");
518
gnutls_bye (es.session, GNUTLS_SHUT_RDWR);
615
gnutls_deinit (session);
521
gnutls_deinit (es.session);
522
gnutls_certificate_free_credentials (es.cred);
523
gnutls_global_deinit ();
619
static void resolve_callback(AvahiSServiceResolver *r,
620
AvahiIfIndex interface,
621
AVAHI_GCC_UNUSED AvahiProtocol protocol,
622
AvahiResolverEvent event,
626
const char *host_name,
627
const AvahiAddress *address,
629
AVAHI_GCC_UNUSED AvahiStringList *txt,
630
AVAHI_GCC_UNUSED AvahiLookupResultFlags
633
mandos_context *mc = userdata;
634
assert(r); /* Spurious warning */
527
static AvahiSimplePoll *simple_poll = NULL;
528
static AvahiServer *server = NULL;
530
static void resolve_callback(
531
AvahiSServiceResolver *r,
532
AVAHI_GCC_UNUSED AvahiIfIndex interface,
533
AVAHI_GCC_UNUSED AvahiProtocol protocol,
534
AvahiResolverEvent event,
538
const char *host_name,
539
const AvahiAddress *address,
541
AVAHI_GCC_UNUSED AvahiStringList *txt,
542
AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
543
AVAHI_GCC_UNUSED void* userdata) {
636
547
/* Called whenever a service has been resolved successfully or
641
552
case AVAHI_RESOLVER_FAILURE:
642
fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
643
" of type '%s' in domain '%s': %s\n", name, type, domain,
644
avahi_strerror(avahi_server_errno(mc->server)));
553
fprintf(stderr, "(Resolver) Failed to resolve service '%s' of"
554
" type '%s' in domain '%s': %s\n", name, type, domain,
555
avahi_strerror(avahi_server_errno(server)));
647
558
case AVAHI_RESOLVER_FOUND:
649
560
char ip[AVAHI_ADDRESS_STR_MAX];
650
561
avahi_address_snprint(ip, sizeof(ip), address);
652
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
653
" port %d\n", name, host_name, ip, interface, port);
563
fprintf(stderr, "Mandos server found on %s (%s) on port %d\n",
564
host_name, ip, port);
655
int ret = start_mandos_communication(ip, port, interface, mc);
566
int ret = start_mandos_communication(ip, port,
657
570
exit(EXIT_SUCCESS);
661
576
avahi_s_service_resolver_free(r);
664
static void browse_callback( AvahiSServiceBrowser *b,
665
AvahiIfIndex interface,
666
AvahiProtocol protocol,
667
AvahiBrowserEvent event,
671
AVAHI_GCC_UNUSED AvahiLookupResultFlags
674
mandos_context *mc = userdata;
675
assert(b); /* Spurious warning */
677
/* Called whenever a new services becomes available on the LAN or
678
is removed from the LAN */
682
case AVAHI_BROWSER_FAILURE:
684
fprintf(stderr, "(Avahi browser) %s\n",
685
avahi_strerror(avahi_server_errno(mc->server)));
686
avahi_simple_poll_quit(mc->simple_poll);
689
case AVAHI_BROWSER_NEW:
690
/* We ignore the returned Avahi resolver object. In the callback
691
function we free it. If the Avahi server is terminated before
692
the callback function is called the Avahi server will free the
695
if (!(avahi_s_service_resolver_new(mc->server, interface,
696
protocol, name, type, domain,
697
AVAHI_PROTO_INET6, 0,
698
resolve_callback, mc)))
699
fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
700
name, avahi_strerror(avahi_server_errno(mc->server)));
703
case AVAHI_BROWSER_REMOVE:
706
case AVAHI_BROWSER_ALL_FOR_NOW:
707
case AVAHI_BROWSER_CACHE_EXHAUSTED:
709
fprintf(stderr, "No Mandos server found, still searching...\n");
579
static void browse_callback(
580
AvahiSServiceBrowser *b,
581
AvahiIfIndex interface,
582
AvahiProtocol protocol,
583
AvahiBrowserEvent event,
587
AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
590
AvahiServer *s = userdata;
593
/* Called whenever a new services becomes available on the LAN or
594
is removed from the LAN */
598
case AVAHI_BROWSER_FAILURE:
600
fprintf(stderr, "(Browser) %s\n",
601
avahi_strerror(avahi_server_errno(server)));
602
avahi_simple_poll_quit(simple_poll);
605
case AVAHI_BROWSER_NEW:
606
/* We ignore the returned resolver object. In the callback
607
function we free it. If the server is terminated before
608
the callback function is called the server will free
609
the resolver for us. */
611
if (!(avahi_s_service_resolver_new(s, interface, protocol, name,
613
AVAHI_PROTO_INET6, 0,
614
resolve_callback, s)))
615
fprintf(stderr, "Failed to resolve service '%s': %s\n", name,
616
avahi_strerror(avahi_server_errno(s)));
619
case AVAHI_BROWSER_REMOVE:
622
case AVAHI_BROWSER_ALL_FOR_NOW:
623
case AVAHI_BROWSER_CACHE_EXHAUSTED:
715
/* Combines file name and path and returns the malloced new
716
string. some sane checks could/should be added */
717
static const char *combinepath(const char *first, const char *second){
718
size_t f_len = strlen(first);
719
size_t s_len = strlen(second);
720
char *tmp = malloc(f_len + s_len + 2);
725
memcpy(tmp, first, f_len); /* Spurious warning */
729
memcpy(tmp + f_len + 1, second, s_len); /* Spurious warning */
731
tmp[f_len + 1 + s_len] = '\0';
736
int main(int argc, char *argv[]){
628
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
629
AvahiServerConfig config;
737
630
AvahiSServiceBrowser *sb = NULL;
740
int exitcode = EXIT_SUCCESS;
633
int returncode = EXIT_SUCCESS;
741
634
const char *interface = "eth0";
742
struct ifreq network;
746
char *connect_to = NULL;
747
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
748
const char *pubkeyfile = "pubkey.txt";
749
const char *seckeyfile = "seckey.txt";
750
mandos_context mc = { .simple_poll = NULL, .server = NULL,
751
.dh_bits = 1024, .priority = "SECURE256"};
752
bool gnutls_initalized = false;
755
struct argp_option options[] = {
756
{ .name = "debug", .key = 128,
757
.doc = "Debug mode", .group = 3 },
758
{ .name = "connect", .key = 'c',
760
.doc = "Connect directly to a sepcified mandos server",
762
{ .name = "interface", .key = 'i',
764
.doc = "Interface that Avahi will conntect through",
766
{ .name = "keydir", .key = 'd',
768
.doc = "Directory where the openpgp keyring is",
770
{ .name = "seckey", .key = 's',
772
.doc = "Secret openpgp key for gnutls authentication",
774
{ .name = "pubkey", .key = 'p',
776
.doc = "Public openpgp key for gnutls authentication",
778
{ .name = "dh-bits", .key = 129,
780
.doc = "dh-bits to use in gnutls communication",
782
{ .name = "priority", .key = 130,
784
.doc = "GNUTLS priority", .group = 1 },
789
error_t parse_opt (int key, char *arg,
790
struct argp_state *state) {
791
/* Get the INPUT argument from `argp_parse', which we know is
792
a pointer to our plugin list pointer. */
814
mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
829
return ARGP_ERR_UNKNOWN;
834
struct argp argp = { .options = options, .parser = parse_opt,
836
.doc = "Mandos client -- Get and decrypt"
837
" passwords from mandos server" };
838
argp_parse (&argp, argc, argv, 0, 0, NULL);
841
pubkeyfile = combinepath(keydir, pubkeyfile);
842
if (pubkeyfile == NULL){
843
perror("combinepath");
844
exitcode = EXIT_FAILURE;
848
seckeyfile = combinepath(keydir, seckeyfile);
849
if (seckeyfile == NULL){
850
perror("combinepath");
854
ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
856
fprintf(stderr, "init_gnutls_global\n");
859
gnutls_initalized = true;
875
if_index = (AvahiIfIndex) if_nametoindex(interface);
877
fprintf(stderr, "No such interface: \"%s\"\n", interface);
881
if(connect_to != NULL){
882
/* Connect directly, do not use Zeroconf */
883
/* (Mainly meant for debugging) */
884
char *address = strrchr(connect_to, ':');
886
fprintf(stderr, "No colon in address\n");
887
exitcode = EXIT_FAILURE;
891
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
893
perror("Bad port number");
894
exitcode = EXIT_FAILURE;
898
address = connect_to;
899
ret = start_mandos_communication(address, port, if_index, &mc);
901
exitcode = EXIT_FAILURE;
903
exitcode = EXIT_SUCCESS;
908
/* If the interface is down, bring it up */
910
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
913
exitcode = EXIT_FAILURE;
916
strcpy(network.ifr_name, interface); /* Spurious warning */
917
ret = ioctl(sd, SIOCGIFFLAGS, &network);
919
perror("ioctl SIOCGIFFLAGS");
920
exitcode = EXIT_FAILURE;
923
if((network.ifr_flags & IFF_UP) == 0){
924
network.ifr_flags |= IFF_UP;
925
ret = ioctl(sd, SIOCSIFFLAGS, &network);
927
perror("ioctl SIOCSIFFLAGS");
928
exitcode = EXIT_FAILURE;
637
static struct option long_options[] = {
638
{"debug", no_argument, (int *)&debug, 1},
639
{"interface", required_argument, 0, 'i'},
642
int option_index = 0;
643
ret = getopt_long (argc, argv, "i:", long_options,
936
662
avahi_set_log_function(empty_log);
939
/* Initialize the pseudo-RNG for Avahi */
665
/* Initialize the psuedo-RNG */
940
666
srand((unsigned int) time(NULL));
942
/* Allocate main Avahi loop object */
943
mc.simple_poll = avahi_simple_poll_new();
944
if (mc.simple_poll == NULL) {
945
fprintf(stderr, "Avahi: Failed to create simple poll"
947
exitcode = EXIT_FAILURE;
952
AvahiServerConfig config;
953
/* Do not publish any local Zeroconf records */
954
avahi_server_config_init(&config);
955
config.publish_hinfo = 0;
956
config.publish_addresses = 0;
957
config.publish_workstation = 0;
958
config.publish_domain = 0;
960
/* Allocate a new server */
961
mc.server = avahi_server_new(avahi_simple_poll_get
962
(mc.simple_poll), &config, NULL,
965
/* Free the Avahi configuration data */
966
avahi_server_config_free(&config);
969
/* Check if creating the Avahi server object succeeded */
970
if (mc.server == NULL) {
971
fprintf(stderr, "Failed to create Avahi server: %s\n",
668
/* Allocate main loop object */
669
if (!(simple_poll = avahi_simple_poll_new())) {
670
fprintf(stderr, "Failed to create simple poll object.\n");
675
/* Do not publish any local records */
676
avahi_server_config_init(&config);
677
config.publish_hinfo = 0;
678
config.publish_addresses = 0;
679
config.publish_workstation = 0;
680
config.publish_domain = 0;
682
/* Allocate a new server */
683
server = avahi_server_new(avahi_simple_poll_get(simple_poll),
684
&config, NULL, NULL, &error);
686
/* Free the configuration data */
687
avahi_server_config_free(&config);
689
/* Check if creating the server object succeeded */
691
fprintf(stderr, "Failed to create server: %s\n",
972
692
avahi_strerror(error));
973
exitcode = EXIT_FAILURE;
693
returncode = EXIT_FAILURE;
977
/* Create the Avahi service browser */
978
sb = avahi_s_service_browser_new(mc.server, if_index,
697
/* Create the service browser */
698
sb = avahi_s_service_browser_new(server,
700
if_nametoindex(interface),
979
701
AVAHI_PROTO_INET6,
980
702
"_mandos._tcp", NULL, 0,
981
browse_callback, &mc);
703
browse_callback, server);
983
705
fprintf(stderr, "Failed to create service browser: %s\n",
984
avahi_strerror(avahi_server_errno(mc.server)));
985
exitcode = EXIT_FAILURE;
706
avahi_strerror(avahi_server_errno(server)));
707
returncode = EXIT_FAILURE;
989
711
/* Run the main loop */
992
fprintf(stderr, "Starting Avahi loop search\n");
714
fprintf(stderr, "Starting avahi loop search\n");
995
avahi_simple_poll_loop(mc.simple_poll);
717
avahi_simple_poll_loop(simple_poll);
1000
722
fprintf(stderr, "%s exiting\n", argv[0]);
1003
725
/* Cleanup things */
1005
727
avahi_s_service_browser_free(sb);
1007
if (mc.server != NULL)
1008
avahi_server_free(mc.server);
1010
if (mc.simple_poll != NULL)
1011
avahi_simple_poll_free(mc.simple_poll);
1015
if (gnutls_initalized){
1016
gnutls_certificate_free_credentials (mc.cred);
1017
gnutls_global_deinit ();
730
avahi_server_free(server);
733
avahi_simple_poll_free(simple_poll);