1
1
/* -*- coding: utf-8 -*- */
3
* Mandos client - get and decrypt data from a Mandos server
3
* Mandos-client - get and decrypt data from a Mandos server
5
5
* This program is partly derived from an example program for an Avahi
6
6
* service browser, downloaded from
9
9
* "browse_callback", and parts of "main".
11
11
* Everything else is
12
* Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
12
* Copyright © 2008,2009 Teddy Hogeborn
13
* Copyright © 2008,2009 Björn Påhlsson
14
15
* This program is free software: you can redistribute it and/or
15
16
* modify it under the terms of the GNU General Public License as
35
36
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
37
38
#include <stdio.h> /* fprintf(), stderr, fwrite(),
39
stdout, ferror(), sscanf(),
39
41
#include <stdint.h> /* uint16_t, uint32_t */
40
42
#include <stddef.h> /* NULL, size_t, ssize_t */
41
43
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
47
49
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
48
50
sockaddr_in6, PF_INET6,
49
51
SOCK_STREAM, INET6_ADDRSTRLEN,
50
uid_t, gid_t, open(), opendir(), DIR */
52
uid_t, gid_t, open(), opendir(),
51
54
#include <sys/stat.h> /* open() */
52
55
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
53
56
struct in6_addr, inet_pton(),
55
58
#include <fcntl.h> /* open() */
56
#include <dirent.h> /* opendir(), struct dirent, readdir() */
57
#include <inttypes.h> /* PRIu16 */
59
#include <dirent.h> /* opendir(), struct dirent, readdir()
61
#include <inttypes.h> /* PRIu16, intmax_t, SCNdMAX */
58
62
#include <assert.h> /* assert() */
59
63
#include <errno.h> /* perror(), errno */
60
#include <time.h> /* time() */
64
#include <time.h> /* nanosleep(), time() */
61
65
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
62
66
SIOCSIFFLAGS, if_indextoname(),
63
67
if_nametoindex(), IF_NAMESIZE */
66
70
getuid(), getgid(), setuid(),
68
72
#include <arpa/inet.h> /* inet_pton(), htons */
69
#include <iso646.h> /* not, and */
73
#include <iso646.h> /* not, and, or */
70
74
#include <argp.h> /* struct argp_option, error_t, struct
71
75
argp_state, struct argp,
72
76
argp_parse(), ARGP_KEY_ARG,
73
77
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
78
#include <sys/klog.h> /* klogctl() */
76
81
/* All Avahi types, constants and functions
90
95
init_gnutls_session(),
92
#include <gnutls/openpgp.h> /* gnutls_certificate_set_openpgp_key_file(),
97
#include <gnutls/openpgp.h>
98
/* gnutls_certificate_set_openpgp_key_file(),
93
99
GNUTLS_OPENPGP_FMT_BASE64 */
102
108
#define BUFFER_SIZE 256
105
#define PATHDIR "/conf/conf.d/mandos"
108
110
#define PATHDIR "/conf/conf.d/mandos"
109
111
#define SECKEY "seckey.txt"
110
112
#define PUBKEY "pubkey.txt"
112
114
bool debug = false;
113
115
static const char mandos_protocol_version[] = "1";
114
const char *argp_program_version = "mandos-client 1.0";
116
const char *argp_program_version = "mandos-client " VERSION;
115
117
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
117
119
/* Used for passing in values through the Avahi callback functions */
133
135
size_t adjustbuffer(char **buffer, size_t buffer_length,
134
136
size_t buffer_capacity){
135
if (buffer_length + BUFFER_SIZE > buffer_capacity){
137
if(buffer_length + BUFFER_SIZE > buffer_capacity){
136
138
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
140
142
buffer_capacity += BUFFER_SIZE;
156
* Helper function to insert pub and seckey to the enigne keyring.
158
* Helper function to insert pub and seckey to the engine keyring.
158
160
bool import_key(const char *filename){
160
162
gpgme_data_t pgp_data;
162
fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
164
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
168
170
rc = gpgme_data_new_from_fd(&pgp_data, fd);
169
if (rc != GPG_ERR_NO_ERROR){
171
if(rc != GPG_ERR_NO_ERROR){
170
172
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
171
173
gpgme_strsource(rc), gpgme_strerror(rc));
175
177
rc = gpgme_op_import(mc->ctx, pgp_data);
176
if (rc != GPG_ERR_NO_ERROR){
178
if(rc != GPG_ERR_NO_ERROR){
177
179
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
178
180
gpgme_strsource(rc), gpgme_strerror(rc));
182
ret = TEMP_FAILURE_RETRY(close(fd));
184
ret = (int)TEMP_FAILURE_RETRY(close(fd));
191
193
fprintf(stderr, "Initialize gpgme\n");
195
197
gpgme_check_version(NULL);
196
198
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
197
if (rc != GPG_ERR_NO_ERROR){
199
if(rc != GPG_ERR_NO_ERROR){
198
200
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
199
201
gpgme_strsource(rc), gpgme_strerror(rc));
203
205
/* Set GPGME home directory for the OpenPGP engine only */
204
rc = gpgme_get_engine_info (&engine_info);
205
if (rc != GPG_ERR_NO_ERROR){
206
rc = gpgme_get_engine_info(&engine_info);
207
if(rc != GPG_ERR_NO_ERROR){
206
208
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
207
209
gpgme_strsource(rc), gpgme_strerror(rc));
223
225
/* Create new GPGME "context" */
224
226
rc = gpgme_new(&(mc->ctx));
225
if (rc != GPG_ERR_NO_ERROR){
227
if(rc != GPG_ERR_NO_ERROR){
226
228
fprintf(stderr, "bad gpgme_new: %s: %s\n",
227
229
gpgme_strsource(rc), gpgme_strerror(rc));
231
if (not import_key(pubkey) or not import_key(seckey)){
233
if(not import_key(pubkey) or not import_key(seckey)){
239
241
* Decrypt OpenPGP data.
240
242
* Returns -1 on error
242
static ssize_t pgp_packet_decrypt (const mandos_context *mc,
243
const char *cryptotext,
244
static ssize_t pgp_packet_decrypt(const mandos_context *mc,
245
const char *cryptotext,
246
248
gpgme_data_t dh_crypto, dh_plain;
247
249
gpgme_error_t rc;
249
251
size_t plaintext_capacity = 0;
250
252
ssize_t plaintext_length = 0;
253
255
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
256
258
/* Create new GPGME data buffer from memory cryptotext */
257
259
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
259
if (rc != GPG_ERR_NO_ERROR){
261
if(rc != GPG_ERR_NO_ERROR){
260
262
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
261
263
gpgme_strsource(rc), gpgme_strerror(rc));
265
267
/* Create new empty GPGME data buffer for the plaintext */
266
268
rc = gpgme_data_new(&dh_plain);
267
if (rc != GPG_ERR_NO_ERROR){
269
if(rc != GPG_ERR_NO_ERROR){
268
270
fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
269
271
gpgme_strsource(rc), gpgme_strerror(rc));
270
272
gpgme_data_release(dh_crypto);
274
276
/* Decrypt data from the cryptotext data buffer to the plaintext
276
278
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
277
if (rc != GPG_ERR_NO_ERROR){
279
if(rc != GPG_ERR_NO_ERROR){
278
280
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
279
281
gpgme_strsource(rc), gpgme_strerror(rc));
280
282
plaintext_length = -1;
282
284
gpgme_decrypt_result_t result;
283
285
result = gpgme_op_decrypt_result(mc->ctx);
285
287
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
287
289
fprintf(stderr, "Unsupported algorithm: %s\n",
316
318
/* Seek back to the beginning of the GPGME plaintext data buffer */
317
if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
318
perror("pgpme_data_seek");
319
if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
320
perror("gpgme_data_seek");
319
321
plaintext_length = -1;
320
322
goto decrypt_end;
325
327
plaintext_capacity = adjustbuffer(plaintext,
326
328
(size_t)plaintext_length,
327
329
plaintext_capacity);
328
if (plaintext_capacity == 0){
330
if(plaintext_capacity == 0){
329
331
perror("adjustbuffer");
330
332
plaintext_length = -1;
331
333
goto decrypt_end;
364
366
return plaintext_length;
367
static const char * safer_gnutls_strerror (int value) {
368
const char *ret = gnutls_strerror (value); /* Spurious warning */
369
static const char * safer_gnutls_strerror(int value) {
370
const char *ret = gnutls_strerror(value); /* Spurious warning from
371
-Wunreachable-code */
370
373
ret = "(unknown)";
389
392
ret = gnutls_global_init();
390
if (ret != GNUTLS_E_SUCCESS) {
391
fprintf (stderr, "GnuTLS global_init: %s\n",
392
safer_gnutls_strerror(ret));
393
if(ret != GNUTLS_E_SUCCESS) {
394
fprintf(stderr, "GnuTLS global_init: %s\n",
395
safer_gnutls_strerror(ret));
397
400
/* "Use a log level over 10 to enable all debugging options."
398
401
* - GnuTLS manual
404
407
/* OpenPGP credentials */
405
408
gnutls_certificate_allocate_credentials(&mc->cred);
406
if (ret != GNUTLS_E_SUCCESS){
407
fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
409
safer_gnutls_strerror(ret));
410
gnutls_global_deinit ();
409
if(ret != GNUTLS_E_SUCCESS){
410
fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
414
safer_gnutls_strerror(ret));
415
gnutls_global_deinit();
420
425
ret = gnutls_certificate_set_openpgp_key_file
421
426
(mc->cred, pubkeyfilename, seckeyfilename,
422
427
GNUTLS_OPENPGP_FMT_BASE64);
423
if (ret != GNUTLS_E_SUCCESS) {
428
if(ret != GNUTLS_E_SUCCESS) {
425
430
"Error[%d] while reading the OpenPGP key pair ('%s',"
426
431
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
432
437
/* GnuTLS server initialization */
433
438
ret = gnutls_dh_params_init(&mc->dh_params);
434
if (ret != GNUTLS_E_SUCCESS) {
435
fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
436
" %s\n", safer_gnutls_strerror(ret));
439
if(ret != GNUTLS_E_SUCCESS) {
440
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
441
" %s\n", safer_gnutls_strerror(ret));
439
444
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
440
if (ret != GNUTLS_E_SUCCESS) {
441
fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
442
safer_gnutls_strerror(ret));
445
if(ret != GNUTLS_E_SUCCESS) {
446
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
447
safer_gnutls_strerror(ret));
460
466
/* GnuTLS session creation */
461
467
ret = gnutls_init(session, GNUTLS_SERVER);
462
if (ret != GNUTLS_E_SUCCESS){
468
if(ret != GNUTLS_E_SUCCESS){
463
469
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
464
470
safer_gnutls_strerror(ret));
469
475
ret = gnutls_priority_set_direct(*session, mc->priority, &err);
470
if (ret != GNUTLS_E_SUCCESS) {
476
if(ret != GNUTLS_E_SUCCESS) {
471
477
fprintf(stderr, "Syntax error at: %s\n", err);
472
478
fprintf(stderr, "GnuTLS error: %s\n",
473
479
safer_gnutls_strerror(ret));
474
gnutls_deinit (*session);
480
gnutls_deinit(*session);
479
485
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
481
if (ret != GNUTLS_E_SUCCESS) {
487
if(ret != GNUTLS_E_SUCCESS) {
482
488
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
483
489
safer_gnutls_strerror(ret));
484
gnutls_deinit (*session);
490
gnutls_deinit(*session);
488
494
/* ignore client certificate if any. */
489
gnutls_certificate_server_set_request (*session,
495
gnutls_certificate_server_set_request(*session,
492
gnutls_dh_set_prime_bits (*session, mc->dh_bits);
498
gnutls_dh_set_prime_bits(*session, mc->dh_bits);
514
521
char interface[IF_NAMESIZE];
515
522
gnutls_session_t session;
517
ret = init_gnutls_session (mc, &session);
524
ret = init_gnutls_session(mc, &session);
551
558
fprintf(stderr, "Bad address: %s\n", ip);
554
to.in6.sin6_port = htons(port); /* Spurious warning */
561
to.in6.sin6_port = htons(port); /* Spurious warnings from
563
-Wunreachable-code */
556
565
to.in6.sin6_scope_id = (uint32_t)if_index;
572
581
ret = connect(tcp_sd, &to.in, sizeof(to));
574
583
perror("connect");
578
587
const char *out = mandos_protocol_version;
581
590
size_t out_size = strlen(out);
582
ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
591
ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
583
592
out_size - written));
603
612
fprintf(stderr, "Establishing TLS session with %s\n", ip);
606
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
615
gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
609
ret = gnutls_handshake (session);
618
ret = gnutls_handshake(session);
610
619
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
612
if (ret != GNUTLS_E_SUCCESS){
621
if(ret != GNUTLS_E_SUCCESS){
614
623
fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
629
638
buffer_capacity = adjustbuffer(&buffer, buffer_length,
630
639
buffer_capacity);
631
if (buffer_capacity == 0){
640
if(buffer_capacity == 0){
632
641
perror("adjustbuffer");
637
ret = gnutls_record_recv(session, buffer+buffer_length,
646
sret = gnutls_record_recv(session, buffer+buffer_length,
644
653
case GNUTLS_E_INTERRUPTED:
645
654
case GNUTLS_E_AGAIN:
647
656
case GNUTLS_E_REHANDSHAKE:
649
ret = gnutls_handshake (session);
658
ret = gnutls_handshake(session);
650
659
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
652
661
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
659
668
fprintf(stderr, "Unknown error while reading data from"
660
669
" encrypted session with Mandos server\n");
662
gnutls_bye (session, GNUTLS_SHUT_RDWR);
671
gnutls_bye(session, GNUTLS_SHUT_RDWR);
666
buffer_length += (size_t) ret;
675
buffer_length += (size_t) sret;
671
680
fprintf(stderr, "Closing TLS session\n");
674
gnutls_bye (session, GNUTLS_SHUT_RDWR);
683
gnutls_bye(session, GNUTLS_SHUT_RDWR);
676
if (buffer_length > 0){
685
if(buffer_length > 0){
677
686
decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
679
688
&decrypted_buffer);
680
if (decrypted_buffer_size >= 0){
689
if(decrypted_buffer_size >= 0){
682
691
while(written < (size_t) decrypted_buffer_size){
683
ret = (int)fwrite (decrypted_buffer + written, 1,
684
(size_t)decrypted_buffer_size - written,
692
ret = (int)fwrite(decrypted_buffer + written, 1,
693
(size_t)decrypted_buffer_size - written,
686
695
if(ret == 0 and ferror(stdout)){
688
697
fprintf(stderr, "Error writing encrypted data: %s\n",
747
756
avahi_address_snprint(ip, sizeof(ip), address);
749
758
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
750
PRIu16 ") on port %d\n", name, host_name, ip,
759
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
760
ip, (intmax_t)interface, port);
753
762
int ret = start_mandos_communication(ip, port, interface, mc);
755
764
avahi_simple_poll_quit(mc->simple_poll);
790
799
the callback function is called the Avahi server will free the
791
800
resolver for us. */
793
if (!(avahi_s_service_resolver_new(mc->server, interface,
802
if(!(avahi_s_service_resolver_new(mc->server, interface,
794
803
protocol, name, type, domain,
795
804
AVAHI_PROTO_INET6, 0,
796
805
resolve_callback, mc)))
823
834
char *connect_to = NULL;
824
835
char tempdir[] = "/tmp/mandosXXXXXX";
836
bool tempdir_created = false;
825
837
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
826
838
const char *seckey = PATHDIR "/" SECKEY;
827
839
const char *pubkey = PATHDIR "/" PUBKEY;
829
841
mandos_context mc = { .simple_poll = NULL, .server = NULL,
830
842
.dh_bits = 1024, .priority = "SECURE256"
831
843
":!CTYPE-X.509:+CTYPE-OPENPGP" };
832
bool gnutls_initalized = false;
833
bool pgpme_initalized = false;
844
bool gnutls_initialized = false;
845
bool gpgme_initialized = false;
836
849
struct argp_option options[] = {
863
876
.doc = "GnuTLS priority string for the TLS handshake",
878
{ .name = "delay", .key = 131,
880
.doc = "Maximum delay to wait for interface startup",
868
error_t parse_opt (int key, char *arg,
869
struct argp_state *state) {
870
/* Get the INPUT argument from `argp_parse', which we know is
871
a pointer to our plugin list pointer. */
885
error_t parse_opt(int key, char *arg,
886
struct argp_state *state) {
873
888
case 128: /* --debug */
888
903
case 129: /* --dh-bits */
890
mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
904
ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
905
if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
906
or arg[numchars] != '\0'){
907
fprintf(stderr, "Bad number of DH bits\n");
893
908
exit(EXIT_FAILURE);
910
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
896
912
case 130: /* --priority */
897
913
mc.priority = arg;
915
case 131: /* --delay */
916
ret = sscanf(arg, "%lf%n", &delay, &numchars);
917
if(ret < 1 or arg[numchars] != '\0'){
918
fprintf(stderr, "Bad delay\n");
899
922
case ARGP_KEY_ARG:
901
924
case ARGP_KEY_END:
911
934
.doc = "Mandos client -- Get and decrypt"
912
935
" passwords from a Mandos server" };
913
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
914
if (ret == ARGP_ERR_UNKNOWN){
936
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
937
if(ret == ARGP_ERR_UNKNOWN){
915
938
fprintf(stderr, "Unknown error while parsing arguments\n");
916
939
exitcode = EXIT_FAILURE;
921
944
/* If the interface is down, bring it up */
946
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
947
messages to mess up the prompt */
948
ret = klogctl(8, NULL, 5);
923
953
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
925
955
perror("socket");
926
956
exitcode = EXIT_FAILURE;
957
ret = klogctl(7, NULL, 0);
929
963
strcpy(network.ifr_name, interface);
930
964
ret = ioctl(sd, SIOCGIFFLAGS, &network);
932
966
perror("ioctl SIOCGIFFLAGS");
967
ret = klogctl(7, NULL, 0);
933
971
exitcode = EXIT_FAILURE;
940
978
perror("ioctl SIOCSIFFLAGS");
941
979
exitcode = EXIT_FAILURE;
980
ret = klogctl(7, NULL, 0);
945
ret = TEMP_FAILURE_RETRY(close(sd));
987
/* sleep checking until interface is running */
988
for(int i=0; i < delay * 4; i++){
989
ret = ioctl(sd, SIOCGIFFLAGS, &network);
991
perror("ioctl SIOCGIFFLAGS");
992
} else if(network.ifr_flags & IFF_RUNNING){
995
struct timespec sleeptime = { .tv_nsec = 250000000 };
996
nanosleep(&sleeptime, NULL);
998
ret = (int)TEMP_FAILURE_RETRY(close(sd));
947
1000
perror("close");
1002
/* Restores kernel loglevel to default */
1003
ret = klogctl(7, NULL, 0);
954
1017
ret = setuid(uid);
956
1019
perror("setuid");
964
1022
ret = init_gnutls_global(&mc, pubkey, seckey);
966
1024
fprintf(stderr, "init_gnutls_global failed\n");
967
1025
exitcode = EXIT_FAILURE;
970
gnutls_initalized = true;
1028
gnutls_initialized = true;
973
1031
if(mkdtemp(tempdir) == NULL){
974
1032
perror("mkdtemp");
1035
tempdir_created = true;
979
1037
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
980
fprintf(stderr, "pgpme_initalized failed\n");
1038
fprintf(stderr, "init_gpgme failed\n");
981
1039
exitcode = EXIT_FAILURE;
984
pgpme_initalized = true;
1042
gpgme_initialized = true;
987
1045
if_index = (AvahiIfIndex) if_nametoindex(interface);
988
1046
if(if_index == 0){
989
1047
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1048
exitcode = EXIT_FAILURE;
993
1052
if(connect_to != NULL){
999
1058
exitcode = EXIT_FAILURE;
1003
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
1005
perror("Bad port number");
1062
ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1063
if(ret < 1 or tmpmax != (uint16_t)tmpmax
1064
or address[numchars+1] != '\0'){
1065
fprintf(stderr, "Bad port number\n");
1006
1066
exitcode = EXIT_FAILURE;
1069
port = (uint16_t)tmpmax;
1009
1070
*address = '\0';
1010
1071
address = connect_to;
1011
1072
ret = start_mandos_communication(address, port, if_index, &mc);
1027
1088
/* Allocate main Avahi loop object */
1028
1089
mc.simple_poll = avahi_simple_poll_new();
1029
if (mc.simple_poll == NULL) {
1090
if(mc.simple_poll == NULL) {
1030
1091
fprintf(stderr, "Avahi: Failed to create simple poll"
1032
1093
exitcode = EXIT_FAILURE;
1074
1135
/* Run the main loop */
1077
1138
fprintf(stderr, "Starting Avahi loop search\n");
1080
1141
avahi_simple_poll_loop(mc.simple_poll);
1085
1146
fprintf(stderr, "%s exiting\n", argv[0]);
1088
1149
/* Cleanup things */
1090
1151
avahi_s_service_browser_free(sb);
1092
if (mc.server != NULL)
1153
if(mc.server != NULL)
1093
1154
avahi_server_free(mc.server);
1095
if (mc.simple_poll != NULL)
1156
if(mc.simple_poll != NULL)
1096
1157
avahi_simple_poll_free(mc.simple_poll);
1098
if (gnutls_initalized){
1159
if(gnutls_initialized){
1099
1160
gnutls_certificate_free_credentials(mc.cred);
1100
gnutls_global_deinit ();
1161
gnutls_global_deinit();
1162
gnutls_dh_params_deinit(mc.dh_params);
1103
if(pgpme_initalized){
1165
if(gpgme_initialized){
1104
1166
gpgme_release(mc.ctx);
1107
1169
/* Removes the temp directory used by GPGME */
1108
if(tempdir[0] != '\0'){
1170
if(tempdir_created){
1110
1172
struct dirent *direntry;
1111
1173
d = opendir(tempdir);
1175
if(errno != ENOENT){
1116
1180
direntry = readdir(d);
1117
1181
if(direntry == NULL){
1120
if (direntry->d_type == DT_REG){
1121
char *fullname = NULL;
1122
ret = asprintf(&fullname, "%s/%s", tempdir,
1128
ret = unlink(fullname);
1130
fprintf(stderr, "unlink(\"%s\"): %s",
1131
fullname, strerror(errno));
1184
/* Skip "." and ".." */
1185
if(direntry->d_name[0] == '.'
1186
and (direntry->d_name[1] == '\0'
1187
or (direntry->d_name[1] == '.'
1188
and direntry->d_name[2] == '\0'))){
1191
char *fullname = NULL;
1192
ret = asprintf(&fullname, "%s/%s", tempdir,
1198
ret = remove(fullname);
1200
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
1137
1207
ret = rmdir(tempdir);
1208
if(ret == -1 and errno != ENOENT){
1139
1209
perror("rmdir");
1143
1213
return exitcode;