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 © 2008,2009 Teddy Hogeborn
13
* Copyright © 2008,2009 Björn Påhlsson
12
* Copyright © 2008 Teddy Hogeborn & Björn Påhlsson
15
14
* This program is free software: you can redistribute it and/or
16
15
* modify it under the terms of the GNU General Public License as
36
35
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
38
37
#include <stdio.h> /* fprintf(), stderr, fwrite(),
39
stdout, ferror(), sscanf(),
41
39
#include <stdint.h> /* uint16_t, uint32_t */
42
40
#include <stddef.h> /* NULL, size_t, ssize_t */
43
41
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
49
47
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
50
48
sockaddr_in6, PF_INET6,
51
49
SOCK_STREAM, INET6_ADDRSTRLEN,
52
uid_t, gid_t, open(), opendir(),
50
uid_t, gid_t, open(), opendir(), DIR */
54
51
#include <sys/stat.h> /* open() */
55
52
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
56
53
struct in6_addr, inet_pton(),
58
55
#include <fcntl.h> /* open() */
59
#include <dirent.h> /* opendir(), struct dirent, readdir()
61
#include <inttypes.h> /* PRIu16, intmax_t, SCNdMAX */
56
#include <dirent.h> /* opendir(), struct dirent, readdir() */
57
#include <inttypes.h> /* PRIu16 */
62
58
#include <assert.h> /* assert() */
63
59
#include <errno.h> /* perror(), errno */
64
#include <time.h> /* nanosleep(), time() */
60
#include <time.h> /* time() */
65
61
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
66
62
SIOCSIFFLAGS, if_indextoname(),
67
63
if_nametoindex(), IF_NAMESIZE */
70
66
getuid(), getgid(), setuid(),
72
68
#include <arpa/inet.h> /* inet_pton(), htons */
73
#include <iso646.h> /* not, and, or */
69
#include <iso646.h> /* not, and */
74
70
#include <argp.h> /* struct argp_option, error_t, struct
75
71
argp_state, struct argp,
76
72
argp_parse(), ARGP_KEY_ARG,
77
73
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
78
#include <sys/klog.h> /* klogctl() */
81
76
/* All Avahi types, constants and functions
95
90
init_gnutls_session(),
97
#include <gnutls/openpgp.h>
98
/* gnutls_certificate_set_openpgp_key_file(),
92
#include <gnutls/openpgp.h> /* gnutls_certificate_set_openpgp_key_file(),
99
93
GNUTLS_OPENPGP_FMT_BASE64 */
108
102
#define BUFFER_SIZE 256
105
#define PATHDIR "/conf/conf.d/mandos"
110
108
#define PATHDIR "/conf/conf.d/mandos"
111
109
#define SECKEY "seckey.txt"
112
110
#define PUBKEY "pubkey.txt"
114
112
bool debug = false;
115
113
static const char mandos_protocol_version[] = "1";
116
const char *argp_program_version = "mandos-client " VERSION;
114
const char *argp_program_version = "mandos-client 1.0";
117
115
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
119
117
/* Used for passing in values through the Avahi callback functions */
135
133
size_t adjustbuffer(char **buffer, size_t buffer_length,
136
134
size_t buffer_capacity){
137
if(buffer_length + BUFFER_SIZE > buffer_capacity){
135
if (buffer_length + BUFFER_SIZE > buffer_capacity){
138
136
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
142
140
buffer_capacity += BUFFER_SIZE;
158
* Helper function to insert pub and seckey to the engine keyring.
156
* Helper function to insert pub and seckey to the enigne keyring.
160
158
bool import_key(const char *filename){
162
160
gpgme_data_t pgp_data;
164
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
162
fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
170
168
rc = gpgme_data_new_from_fd(&pgp_data, fd);
171
if(rc != GPG_ERR_NO_ERROR){
169
if (rc != GPG_ERR_NO_ERROR){
172
170
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
173
171
gpgme_strsource(rc), gpgme_strerror(rc));
177
175
rc = gpgme_op_import(mc->ctx, pgp_data);
178
if(rc != GPG_ERR_NO_ERROR){
176
if (rc != GPG_ERR_NO_ERROR){
179
177
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
180
178
gpgme_strsource(rc), gpgme_strerror(rc));
184
ret = (int)TEMP_FAILURE_RETRY(close(fd));
182
ret = TEMP_FAILURE_RETRY(close(fd));
193
191
fprintf(stderr, "Initialize gpgme\n");
197
195
gpgme_check_version(NULL);
198
196
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
199
if(rc != GPG_ERR_NO_ERROR){
197
if (rc != GPG_ERR_NO_ERROR){
200
198
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
201
199
gpgme_strsource(rc), gpgme_strerror(rc));
205
203
/* Set GPGME home directory for the OpenPGP engine only */
206
rc = gpgme_get_engine_info(&engine_info);
207
if(rc != GPG_ERR_NO_ERROR){
204
rc = gpgme_get_engine_info (&engine_info);
205
if (rc != GPG_ERR_NO_ERROR){
208
206
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
209
207
gpgme_strsource(rc), gpgme_strerror(rc));
225
223
/* Create new GPGME "context" */
226
224
rc = gpgme_new(&(mc->ctx));
227
if(rc != GPG_ERR_NO_ERROR){
225
if (rc != GPG_ERR_NO_ERROR){
228
226
fprintf(stderr, "bad gpgme_new: %s: %s\n",
229
227
gpgme_strsource(rc), gpgme_strerror(rc));
233
if(not import_key(pubkey) or not import_key(seckey)){
231
if (not import_key(pubkey) or not import_key(seckey)){
241
239
* Decrypt OpenPGP data.
242
240
* Returns -1 on error
244
static ssize_t pgp_packet_decrypt(const mandos_context *mc,
245
const char *cryptotext,
242
static ssize_t pgp_packet_decrypt (const mandos_context *mc,
243
const char *cryptotext,
248
246
gpgme_data_t dh_crypto, dh_plain;
249
247
gpgme_error_t rc;
251
249
size_t plaintext_capacity = 0;
252
250
ssize_t plaintext_length = 0;
255
253
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
258
256
/* Create new GPGME data buffer from memory cryptotext */
259
257
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
261
if(rc != GPG_ERR_NO_ERROR){
259
if (rc != GPG_ERR_NO_ERROR){
262
260
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
263
261
gpgme_strsource(rc), gpgme_strerror(rc));
267
265
/* Create new empty GPGME data buffer for the plaintext */
268
266
rc = gpgme_data_new(&dh_plain);
269
if(rc != GPG_ERR_NO_ERROR){
267
if (rc != GPG_ERR_NO_ERROR){
270
268
fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
271
269
gpgme_strsource(rc), gpgme_strerror(rc));
272
270
gpgme_data_release(dh_crypto);
276
274
/* Decrypt data from the cryptotext data buffer to the plaintext
278
276
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
279
if(rc != GPG_ERR_NO_ERROR){
277
if (rc != GPG_ERR_NO_ERROR){
280
278
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
281
279
gpgme_strsource(rc), gpgme_strerror(rc));
282
280
plaintext_length = -1;
284
282
gpgme_decrypt_result_t result;
285
283
result = gpgme_op_decrypt_result(mc->ctx);
287
285
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
289
287
fprintf(stderr, "Unsupported algorithm: %s\n",
318
316
/* Seek back to the beginning of the GPGME plaintext data buffer */
319
if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
317
if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
320
318
perror("gpgme_data_seek");
321
319
plaintext_length = -1;
322
320
goto decrypt_end;
327
325
plaintext_capacity = adjustbuffer(plaintext,
328
326
(size_t)plaintext_length,
329
327
plaintext_capacity);
330
if(plaintext_capacity == 0){
328
if (plaintext_capacity == 0){
331
329
perror("adjustbuffer");
332
330
plaintext_length = -1;
333
331
goto decrypt_end;
366
364
return plaintext_length;
369
static const char * safer_gnutls_strerror(int value) {
370
const char *ret = gnutls_strerror(value); /* Spurious warning from
371
-Wunreachable-code */
367
static const char * safer_gnutls_strerror (int value) {
368
const char *ret = gnutls_strerror (value); /* Spurious warning */
373
370
ret = "(unknown)";
392
389
ret = gnutls_global_init();
393
if(ret != GNUTLS_E_SUCCESS) {
394
fprintf(stderr, "GnuTLS global_init: %s\n",
395
safer_gnutls_strerror(ret));
390
if (ret != GNUTLS_E_SUCCESS) {
391
fprintf (stderr, "GnuTLS global_init: %s\n",
392
safer_gnutls_strerror(ret));
400
397
/* "Use a log level over 10 to enable all debugging options."
401
398
* - GnuTLS manual
407
404
/* OpenPGP credentials */
408
405
gnutls_certificate_allocate_credentials(&mc->cred);
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();
406
if (ret != GNUTLS_E_SUCCESS){
407
fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
409
safer_gnutls_strerror(ret));
410
gnutls_global_deinit ();
425
420
ret = gnutls_certificate_set_openpgp_key_file
426
421
(mc->cred, pubkeyfilename, seckeyfilename,
427
422
GNUTLS_OPENPGP_FMT_BASE64);
428
if(ret != GNUTLS_E_SUCCESS) {
423
if (ret != GNUTLS_E_SUCCESS) {
430
425
"Error[%d] while reading the OpenPGP key pair ('%s',"
431
426
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
437
432
/* GnuTLS server initialization */
438
433
ret = gnutls_dh_params_init(&mc->dh_params);
439
if(ret != GNUTLS_E_SUCCESS) {
440
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
441
" %s\n", safer_gnutls_strerror(ret));
434
if (ret != GNUTLS_E_SUCCESS) {
435
fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
436
" %s\n", safer_gnutls_strerror(ret));
444
439
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
445
if(ret != GNUTLS_E_SUCCESS) {
446
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
447
safer_gnutls_strerror(ret));
440
if (ret != GNUTLS_E_SUCCESS) {
441
fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
442
safer_gnutls_strerror(ret));
466
461
/* GnuTLS session creation */
467
462
ret = gnutls_init(session, GNUTLS_SERVER);
468
if(ret != GNUTLS_E_SUCCESS){
463
if (ret != GNUTLS_E_SUCCESS){
469
464
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
470
465
safer_gnutls_strerror(ret));
475
470
ret = gnutls_priority_set_direct(*session, mc->priority, &err);
476
if(ret != GNUTLS_E_SUCCESS) {
471
if (ret != GNUTLS_E_SUCCESS) {
477
472
fprintf(stderr, "Syntax error at: %s\n", err);
478
473
fprintf(stderr, "GnuTLS error: %s\n",
479
474
safer_gnutls_strerror(ret));
480
gnutls_deinit(*session);
475
gnutls_deinit (*session);
485
480
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
487
if(ret != GNUTLS_E_SUCCESS) {
482
if (ret != GNUTLS_E_SUCCESS) {
488
483
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
489
484
safer_gnutls_strerror(ret));
490
gnutls_deinit(*session);
485
gnutls_deinit (*session);
494
489
/* ignore client certificate if any. */
495
gnutls_certificate_server_set_request(*session,
490
gnutls_certificate_server_set_request (*session,
498
gnutls_dh_set_prime_bits(*session, mc->dh_bits);
493
gnutls_dh_set_prime_bits (*session, mc->dh_bits);
521
515
char interface[IF_NAMESIZE];
522
516
gnutls_session_t session;
524
ret = init_gnutls_session(mc, &session);
518
ret = init_gnutls_session (mc, &session);
558
552
fprintf(stderr, "Bad address: %s\n", ip);
561
to.in6.sin6_port = htons(port); /* Spurious warnings from
563
-Wunreachable-code */
555
to.in6.sin6_port = htons(port); /* Spurious warning */
565
557
to.in6.sin6_scope_id = (uint32_t)if_index;
581
573
ret = connect(tcp_sd, &to.in, sizeof(to));
583
575
perror("connect");
587
579
const char *out = mandos_protocol_version;
590
582
size_t out_size = strlen(out);
591
ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
583
ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
592
584
out_size - written));
612
604
fprintf(stderr, "Establishing TLS session with %s\n", ip);
615
gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
607
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
618
ret = gnutls_handshake(session);
610
ret = gnutls_handshake (session);
619
611
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
621
if(ret != GNUTLS_E_SUCCESS){
613
if (ret != GNUTLS_E_SUCCESS){
623
615
fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
638
630
buffer_capacity = adjustbuffer(&buffer, buffer_length,
639
631
buffer_capacity);
640
if(buffer_capacity == 0){
632
if (buffer_capacity == 0){
641
633
perror("adjustbuffer");
646
sret = gnutls_record_recv(session, buffer+buffer_length,
638
ret = gnutls_record_recv(session, buffer+buffer_length,
653
645
case GNUTLS_E_INTERRUPTED:
654
646
case GNUTLS_E_AGAIN:
656
648
case GNUTLS_E_REHANDSHAKE:
658
ret = gnutls_handshake(session);
650
ret = gnutls_handshake (session);
659
651
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
661
653
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
668
660
fprintf(stderr, "Unknown error while reading data from"
669
661
" encrypted session with Mandos server\n");
671
gnutls_bye(session, GNUTLS_SHUT_RDWR);
663
gnutls_bye (session, GNUTLS_SHUT_RDWR);
675
buffer_length += (size_t) sret;
667
buffer_length += (size_t) ret;
680
672
fprintf(stderr, "Closing TLS session\n");
683
gnutls_bye(session, GNUTLS_SHUT_RDWR);
675
gnutls_bye (session, GNUTLS_SHUT_RDWR);
685
if(buffer_length > 0){
677
if (buffer_length > 0){
686
678
decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
688
680
&decrypted_buffer);
689
if(decrypted_buffer_size >= 0){
681
if (decrypted_buffer_size >= 0){
691
683
while(written < (size_t) decrypted_buffer_size){
692
ret = (int)fwrite(decrypted_buffer + written, 1,
693
(size_t)decrypted_buffer_size - written,
684
ret = (int)fwrite (decrypted_buffer + written, 1,
685
(size_t)decrypted_buffer_size - written,
695
687
if(ret == 0 and ferror(stdout)){
697
689
fprintf(stderr, "Error writing encrypted data: %s\n",
756
748
avahi_address_snprint(ip, sizeof(ip), address);
758
750
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
759
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
760
ip, (intmax_t)interface, port);
751
PRIu16 ") on port %d\n", name, host_name, ip,
762
754
int ret = start_mandos_communication(ip, port, interface, mc);
764
756
avahi_simple_poll_quit(mc->simple_poll);
799
791
the callback function is called the Avahi server will free the
800
792
resolver for us. */
802
if(!(avahi_s_service_resolver_new(mc->server, interface,
794
if (!(avahi_s_service_resolver_new(mc->server, interface,
803
795
protocol, name, type, domain,
804
796
AVAHI_PROTO_INET6, 0,
805
797
resolve_callback, mc)))
834
824
char *connect_to = NULL;
835
825
char tempdir[] = "/tmp/mandosXXXXXX";
836
bool tempdir_created = false;
837
826
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
838
827
const char *seckey = PATHDIR "/" SECKEY;
839
828
const char *pubkey = PATHDIR "/" PUBKEY;
841
830
mandos_context mc = { .simple_poll = NULL, .server = NULL,
842
831
.dh_bits = 1024, .priority = "SECURE256"
843
832
":!CTYPE-X.509:+CTYPE-OPENPGP" };
844
bool gnutls_initialized = false;
845
bool gpgme_initialized = false;
833
bool gnutls_initalized = false;
834
bool gpgme_initalized = false;
849
837
struct argp_option options[] = {
876
864
.doc = "GnuTLS priority string for the TLS handshake",
878
{ .name = "delay", .key = 131,
880
.doc = "Maximum delay to wait for interface startup",
885
error_t parse_opt(int key, char *arg,
886
struct argp_state *state) {
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. */
888
874
case 128: /* --debug */
903
889
case 129: /* --dh-bits */
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");
891
mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
908
894
exit(EXIT_FAILURE);
910
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
912
897
case 130: /* --priority */
913
898
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");
922
900
case ARGP_KEY_ARG:
924
902
case ARGP_KEY_END:
934
912
.doc = "Mandos client -- Get and decrypt"
935
913
" passwords from a Mandos server" };
936
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
937
if(ret == ARGP_ERR_UNKNOWN){
914
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
915
if (ret == ARGP_ERR_UNKNOWN){
938
916
fprintf(stderr, "Unknown error while parsing arguments\n");
939
917
exitcode = EXIT_FAILURE;
944
922
/* 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);
953
924
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
955
926
perror("socket");
956
927
exitcode = EXIT_FAILURE;
957
ret = klogctl(7, NULL, 0);
963
930
strcpy(network.ifr_name, interface);
964
931
ret = ioctl(sd, SIOCGIFFLAGS, &network);
966
933
perror("ioctl SIOCGIFFLAGS");
967
ret = klogctl(7, NULL, 0);
971
934
exitcode = EXIT_FAILURE;
978
941
perror("ioctl SIOCSIFFLAGS");
979
942
exitcode = EXIT_FAILURE;
980
ret = klogctl(7, NULL, 0);
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));
946
ret = TEMP_FAILURE_RETRY(close(sd));
1000
948
perror("close");
1002
/* Restores kernel loglevel to default */
1003
ret = klogctl(7, NULL, 0);
1014
962
perror("setgid");
1022
965
ret = init_gnutls_global(&mc, pubkey, seckey);
1024
967
fprintf(stderr, "init_gnutls_global failed\n");
1025
968
exitcode = EXIT_FAILURE;
1028
gnutls_initialized = true;
971
gnutls_initalized = true;
1031
974
if(mkdtemp(tempdir) == NULL){
1032
975
perror("mkdtemp");
1035
tempdir_created = true;
1037
980
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
1038
fprintf(stderr, "init_gpgme failed\n");
981
fprintf(stderr, "gpgme_initalized failed\n");
1039
982
exitcode = EXIT_FAILURE;
1042
gpgme_initialized = true;
985
gpgme_initalized = true;
1045
988
if_index = (AvahiIfIndex) if_nametoindex(interface);
1046
989
if(if_index == 0){
1047
990
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1048
exitcode = EXIT_FAILURE;
1052
994
if(connect_to != NULL){
1058
1000
exitcode = EXIT_FAILURE;
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");
1004
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
1006
perror("Bad port number");
1066
1007
exitcode = EXIT_FAILURE;
1069
port = (uint16_t)tmpmax;
1070
1010
*address = '\0';
1071
1011
address = connect_to;
1072
1012
ret = start_mandos_communication(address, port, if_index, &mc);
1088
1028
/* Allocate main Avahi loop object */
1089
1029
mc.simple_poll = avahi_simple_poll_new();
1090
if(mc.simple_poll == NULL) {
1030
if (mc.simple_poll == NULL) {
1091
1031
fprintf(stderr, "Avahi: Failed to create simple poll"
1093
1033
exitcode = EXIT_FAILURE;
1135
1075
/* Run the main loop */
1138
1078
fprintf(stderr, "Starting Avahi loop search\n");
1141
1081
avahi_simple_poll_loop(mc.simple_poll);
1146
1086
fprintf(stderr, "%s exiting\n", argv[0]);
1149
1089
/* Cleanup things */
1151
1091
avahi_s_service_browser_free(sb);
1153
if(mc.server != NULL)
1093
if (mc.server != NULL)
1154
1094
avahi_server_free(mc.server);
1156
if(mc.simple_poll != NULL)
1096
if (mc.simple_poll != NULL)
1157
1097
avahi_simple_poll_free(mc.simple_poll);
1159
if(gnutls_initialized){
1099
if (gnutls_initalized){
1160
1100
gnutls_certificate_free_credentials(mc.cred);
1161
gnutls_global_deinit();
1101
gnutls_global_deinit ();
1162
1102
gnutls_dh_params_deinit(mc.dh_params);
1165
if(gpgme_initialized){
1105
if(gpgme_initalized){
1166
1106
gpgme_release(mc.ctx);
1169
1109
/* Removes the temp directory used by GPGME */
1170
if(tempdir_created){
1110
if(tempdir[0] != '\0'){
1172
1112
struct dirent *direntry;
1173
1113
d = opendir(tempdir);
1175
if(errno != ENOENT){
1180
1118
direntry = readdir(d);
1181
1119
if(direntry == NULL){
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,
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));
1207
1140
ret = rmdir(tempdir);
1208
if(ret == -1 and errno != ENOENT){
1209
1142
perror("rmdir");
1213
1146
return exitcode;