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){
319
if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
318
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));
461
466
/* GnuTLS session creation */
462
467
ret = gnutls_init(session, GNUTLS_SERVER);
463
if (ret != GNUTLS_E_SUCCESS){
468
if(ret != GNUTLS_E_SUCCESS){
464
469
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
465
470
safer_gnutls_strerror(ret));
470
475
ret = gnutls_priority_set_direct(*session, mc->priority, &err);
471
if (ret != GNUTLS_E_SUCCESS) {
476
if(ret != GNUTLS_E_SUCCESS) {
472
477
fprintf(stderr, "Syntax error at: %s\n", err);
473
478
fprintf(stderr, "GnuTLS error: %s\n",
474
479
safer_gnutls_strerror(ret));
475
gnutls_deinit (*session);
480
gnutls_deinit(*session);
480
485
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
482
if (ret != GNUTLS_E_SUCCESS) {
487
if(ret != GNUTLS_E_SUCCESS) {
483
488
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
484
489
safer_gnutls_strerror(ret));
485
gnutls_deinit (*session);
490
gnutls_deinit(*session);
489
494
/* ignore client certificate if any. */
490
gnutls_certificate_server_set_request (*session,
495
gnutls_certificate_server_set_request(*session,
493
gnutls_dh_set_prime_bits (*session, mc->dh_bits);
498
gnutls_dh_set_prime_bits(*session, mc->dh_bits);
515
521
char interface[IF_NAMESIZE];
516
522
gnutls_session_t session;
518
ret = init_gnutls_session (mc, &session);
524
ret = init_gnutls_session(mc, &session);
552
558
fprintf(stderr, "Bad address: %s\n", ip);
555
to.in6.sin6_port = htons(port); /* Spurious warning */
561
to.in6.sin6_port = htons(port); /* Spurious warnings from
563
-Wunreachable-code */
557
565
to.in6.sin6_scope_id = (uint32_t)if_index;
573
581
ret = connect(tcp_sd, &to.in, sizeof(to));
575
583
perror("connect");
579
587
const char *out = mandos_protocol_version;
582
590
size_t out_size = strlen(out);
583
ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
591
ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
584
592
out_size - written));
604
612
fprintf(stderr, "Establishing TLS session with %s\n", ip);
607
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
615
gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
610
ret = gnutls_handshake (session);
618
ret = gnutls_handshake(session);
611
619
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
613
if (ret != GNUTLS_E_SUCCESS){
621
if(ret != GNUTLS_E_SUCCESS){
615
623
fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
630
638
buffer_capacity = adjustbuffer(&buffer, buffer_length,
631
639
buffer_capacity);
632
if (buffer_capacity == 0){
640
if(buffer_capacity == 0){
633
641
perror("adjustbuffer");
638
ret = gnutls_record_recv(session, buffer+buffer_length,
646
sret = gnutls_record_recv(session, buffer+buffer_length,
645
653
case GNUTLS_E_INTERRUPTED:
646
654
case GNUTLS_E_AGAIN:
648
656
case GNUTLS_E_REHANDSHAKE:
650
ret = gnutls_handshake (session);
658
ret = gnutls_handshake(session);
651
659
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
653
661
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
660
668
fprintf(stderr, "Unknown error while reading data from"
661
669
" encrypted session with Mandos server\n");
663
gnutls_bye (session, GNUTLS_SHUT_RDWR);
671
gnutls_bye(session, GNUTLS_SHUT_RDWR);
667
buffer_length += (size_t) ret;
675
buffer_length += (size_t) sret;
672
680
fprintf(stderr, "Closing TLS session\n");
675
gnutls_bye (session, GNUTLS_SHUT_RDWR);
683
gnutls_bye(session, GNUTLS_SHUT_RDWR);
677
if (buffer_length > 0){
685
if(buffer_length > 0){
678
686
decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
680
688
&decrypted_buffer);
681
if (decrypted_buffer_size >= 0){
689
if(decrypted_buffer_size >= 0){
683
691
while(written < (size_t) decrypted_buffer_size){
684
ret = (int)fwrite (decrypted_buffer + written, 1,
685
(size_t)decrypted_buffer_size - written,
692
ret = (int)fwrite(decrypted_buffer + written, 1,
693
(size_t)decrypted_buffer_size - written,
687
695
if(ret == 0 and ferror(stdout)){
689
697
fprintf(stderr, "Error writing encrypted data: %s\n",
748
756
avahi_address_snprint(ip, sizeof(ip), address);
750
758
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
751
PRIu16 ") on port %d\n", name, host_name, ip,
759
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
760
ip, (intmax_t)interface, port);
754
762
int ret = start_mandos_communication(ip, port, interface, mc);
756
764
avahi_simple_poll_quit(mc->simple_poll);
791
799
the callback function is called the Avahi server will free the
792
800
resolver for us. */
794
if (!(avahi_s_service_resolver_new(mc->server, interface,
802
if(!(avahi_s_service_resolver_new(mc->server, interface,
795
803
protocol, name, type, domain,
796
804
AVAHI_PROTO_INET6, 0,
797
805
resolve_callback, mc)))
824
834
char *connect_to = NULL;
825
835
char tempdir[] = "/tmp/mandosXXXXXX";
836
bool tempdir_created = false;
826
837
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
827
838
const char *seckey = PATHDIR "/" SECKEY;
828
839
const char *pubkey = PATHDIR "/" PUBKEY;
830
841
mandos_context mc = { .simple_poll = NULL, .server = NULL,
831
842
.dh_bits = 1024, .priority = "SECURE256"
832
843
":!CTYPE-X.509:+CTYPE-OPENPGP" };
833
bool gnutls_initalized = false;
834
bool gpgme_initalized = false;
844
bool gnutls_initialized = false;
845
bool gpgme_initialized = false;
837
849
struct argp_option options[] = {
864
876
.doc = "GnuTLS priority string for the TLS handshake",
878
{ .name = "delay", .key = 131,
880
.doc = "Maximum delay to wait for interface startup",
869
error_t parse_opt (int key, char *arg,
870
struct argp_state *state) {
871
/* Get the INPUT argument from `argp_parse', which we know is
872
a pointer to our plugin list pointer. */
885
error_t parse_opt(int key, char *arg,
886
struct argp_state *state) {
874
888
case 128: /* --debug */
889
903
case 129: /* --dh-bits */
891
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");
894
908
exit(EXIT_FAILURE);
910
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
897
912
case 130: /* --priority */
898
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");
900
922
case ARGP_KEY_ARG:
902
924
case ARGP_KEY_END:
912
934
.doc = "Mandos client -- Get and decrypt"
913
935
" passwords from a Mandos server" };
914
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
915
if (ret == ARGP_ERR_UNKNOWN){
936
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
937
if(ret == ARGP_ERR_UNKNOWN){
916
938
fprintf(stderr, "Unknown error while parsing arguments\n");
917
939
exitcode = EXIT_FAILURE;
922
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);
924
953
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
926
955
perror("socket");
927
956
exitcode = EXIT_FAILURE;
957
ret = klogctl(7, NULL, 0);
930
963
strcpy(network.ifr_name, interface);
931
964
ret = ioctl(sd, SIOCGIFFLAGS, &network);
933
966
perror("ioctl SIOCGIFFLAGS");
967
ret = klogctl(7, NULL, 0);
934
971
exitcode = EXIT_FAILURE;
941
978
perror("ioctl SIOCSIFFLAGS");
942
979
exitcode = EXIT_FAILURE;
980
ret = klogctl(7, NULL, 0);
946
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));
948
1000
perror("close");
1002
/* Restores kernel loglevel to default */
1003
ret = klogctl(7, NULL, 0);
955
1017
ret = setuid(uid);
957
1019
perror("setuid");
965
1022
ret = init_gnutls_global(&mc, pubkey, seckey);
967
1024
fprintf(stderr, "init_gnutls_global failed\n");
968
1025
exitcode = EXIT_FAILURE;
971
gnutls_initalized = true;
1028
gnutls_initialized = true;
974
1031
if(mkdtemp(tempdir) == NULL){
975
1032
perror("mkdtemp");
1035
tempdir_created = true;
980
1037
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
981
fprintf(stderr, "gpgme_initalized failed\n");
1038
fprintf(stderr, "init_gpgme failed\n");
982
1039
exitcode = EXIT_FAILURE;
985
gpgme_initalized = true;
1042
gpgme_initialized = true;
988
1045
if_index = (AvahiIfIndex) if_nametoindex(interface);
989
1046
if(if_index == 0){
990
1047
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1048
exitcode = EXIT_FAILURE;
994
1052
if(connect_to != NULL){
1000
1058
exitcode = EXIT_FAILURE;
1004
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
1006
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");
1007
1066
exitcode = EXIT_FAILURE;
1069
port = (uint16_t)tmpmax;
1010
1070
*address = '\0';
1011
1071
address = connect_to;
1012
1072
ret = start_mandos_communication(address, port, if_index, &mc);
1028
1088
/* Allocate main Avahi loop object */
1029
1089
mc.simple_poll = avahi_simple_poll_new();
1030
if (mc.simple_poll == NULL) {
1090
if(mc.simple_poll == NULL) {
1031
1091
fprintf(stderr, "Avahi: Failed to create simple poll"
1033
1093
exitcode = EXIT_FAILURE;
1075
1135
/* Run the main loop */
1078
1138
fprintf(stderr, "Starting Avahi loop search\n");
1081
1141
avahi_simple_poll_loop(mc.simple_poll);
1086
1146
fprintf(stderr, "%s exiting\n", argv[0]);
1089
1149
/* Cleanup things */
1091
1151
avahi_s_service_browser_free(sb);
1093
if (mc.server != NULL)
1153
if(mc.server != NULL)
1094
1154
avahi_server_free(mc.server);
1096
if (mc.simple_poll != NULL)
1156
if(mc.simple_poll != NULL)
1097
1157
avahi_simple_poll_free(mc.simple_poll);
1099
if (gnutls_initalized){
1159
if(gnutls_initialized){
1100
1160
gnutls_certificate_free_credentials(mc.cred);
1101
gnutls_global_deinit ();
1161
gnutls_global_deinit();
1102
1162
gnutls_dh_params_deinit(mc.dh_params);
1105
if(gpgme_initalized){
1165
if(gpgme_initialized){
1106
1166
gpgme_release(mc.ctx);
1109
1169
/* Removes the temp directory used by GPGME */
1110
if(tempdir[0] != '\0'){
1170
if(tempdir_created){
1112
1172
struct dirent *direntry;
1113
1173
d = opendir(tempdir);
1175
if(errno != ENOENT){
1118
1180
direntry = readdir(d);
1119
1181
if(direntry == NULL){
1122
if (direntry->d_type == DT_REG){
1123
char *fullname = NULL;
1124
ret = asprintf(&fullname, "%s/%s", tempdir,
1130
ret = unlink(fullname);
1132
fprintf(stderr, "unlink(\"%s\"): %s",
1133
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,
1140
1207
ret = rmdir(tempdir);
1208
if(ret == -1 and errno != ENOENT){
1142
1209
perror("rmdir");
1146
1213
return exitcode;