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 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 */
129
135
size_t adjustbuffer(char **buffer, size_t buffer_length,
130
136
size_t buffer_capacity){
131
if (buffer_length + BUFFER_SIZE > buffer_capacity){
137
if(buffer_length + BUFFER_SIZE > buffer_capacity){
132
138
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
136
142
buffer_capacity += BUFFER_SIZE;
152
* Helper function to insert pub and seckey to the enigne keyring.
158
* Helper function to insert pub and seckey to the engine keyring.
154
160
bool import_key(const char *filename){
156
162
gpgme_data_t pgp_data;
158
fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
164
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
164
170
rc = gpgme_data_new_from_fd(&pgp_data, fd);
165
if (rc != GPG_ERR_NO_ERROR){
171
if(rc != GPG_ERR_NO_ERROR){
166
172
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
167
173
gpgme_strsource(rc), gpgme_strerror(rc));
171
177
rc = gpgme_op_import(mc->ctx, pgp_data);
172
if (rc != GPG_ERR_NO_ERROR){
178
if(rc != GPG_ERR_NO_ERROR){
173
179
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
174
180
gpgme_strsource(rc), gpgme_strerror(rc));
178
ret = TEMP_FAILURE_RETRY(close(fd));
184
ret = (int)TEMP_FAILURE_RETRY(close(fd));
187
193
fprintf(stderr, "Initialize gpgme\n");
191
197
gpgme_check_version(NULL);
192
198
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
193
if (rc != GPG_ERR_NO_ERROR){
199
if(rc != GPG_ERR_NO_ERROR){
194
200
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
195
201
gpgme_strsource(rc), gpgme_strerror(rc));
199
205
/* Set GPGME home directory for the OpenPGP engine only */
200
rc = gpgme_get_engine_info (&engine_info);
201
if (rc != GPG_ERR_NO_ERROR){
206
rc = gpgme_get_engine_info(&engine_info);
207
if(rc != GPG_ERR_NO_ERROR){
202
208
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
203
209
gpgme_strsource(rc), gpgme_strerror(rc));
219
225
/* Create new GPGME "context" */
220
226
rc = gpgme_new(&(mc->ctx));
221
if (rc != GPG_ERR_NO_ERROR){
227
if(rc != GPG_ERR_NO_ERROR){
222
228
fprintf(stderr, "bad gpgme_new: %s: %s\n",
223
229
gpgme_strsource(rc), gpgme_strerror(rc));
227
if (not import_key(pubkey) or not import_key(seckey)){
233
if(not import_key(pubkey) or not import_key(seckey)){
235
241
* Decrypt OpenPGP data.
236
242
* Returns -1 on error
238
static ssize_t pgp_packet_decrypt (const mandos_context *mc,
239
const char *cryptotext,
244
static ssize_t pgp_packet_decrypt(const mandos_context *mc,
245
const char *cryptotext,
242
248
gpgme_data_t dh_crypto, dh_plain;
243
249
gpgme_error_t rc;
245
251
size_t plaintext_capacity = 0;
246
252
ssize_t plaintext_length = 0;
249
255
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
252
258
/* Create new GPGME data buffer from memory cryptotext */
253
259
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
255
if (rc != GPG_ERR_NO_ERROR){
261
if(rc != GPG_ERR_NO_ERROR){
256
262
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
257
263
gpgme_strsource(rc), gpgme_strerror(rc));
261
267
/* Create new empty GPGME data buffer for the plaintext */
262
268
rc = gpgme_data_new(&dh_plain);
263
if (rc != GPG_ERR_NO_ERROR){
269
if(rc != GPG_ERR_NO_ERROR){
264
270
fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
265
271
gpgme_strsource(rc), gpgme_strerror(rc));
266
272
gpgme_data_release(dh_crypto);
270
276
/* Decrypt data from the cryptotext data buffer to the plaintext
272
278
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
273
if (rc != GPG_ERR_NO_ERROR){
279
if(rc != GPG_ERR_NO_ERROR){
274
280
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
275
281
gpgme_strsource(rc), gpgme_strerror(rc));
276
282
plaintext_length = -1;
278
284
gpgme_decrypt_result_t result;
279
285
result = gpgme_op_decrypt_result(mc->ctx);
281
287
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
283
289
fprintf(stderr, "Unsupported algorithm: %s\n",
312
318
/* Seek back to the beginning of the GPGME plaintext data buffer */
313
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){
314
320
perror("gpgme_data_seek");
315
321
plaintext_length = -1;
316
322
goto decrypt_end;
321
327
plaintext_capacity = adjustbuffer(plaintext,
322
328
(size_t)plaintext_length,
323
329
plaintext_capacity);
324
if (plaintext_capacity == 0){
330
if(plaintext_capacity == 0){
325
331
perror("adjustbuffer");
326
332
plaintext_length = -1;
327
333
goto decrypt_end;
360
366
return plaintext_length;
363
static const char * safer_gnutls_strerror (int value) {
364
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 */
366
373
ret = "(unknown)";
385
392
ret = gnutls_global_init();
386
if (ret != GNUTLS_E_SUCCESS) {
387
fprintf (stderr, "GnuTLS global_init: %s\n",
388
safer_gnutls_strerror(ret));
393
if(ret != GNUTLS_E_SUCCESS) {
394
fprintf(stderr, "GnuTLS global_init: %s\n",
395
safer_gnutls_strerror(ret));
393
400
/* "Use a log level over 10 to enable all debugging options."
394
401
* - GnuTLS manual
400
407
/* OpenPGP credentials */
401
408
gnutls_certificate_allocate_credentials(&mc->cred);
402
if (ret != GNUTLS_E_SUCCESS){
403
fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
405
safer_gnutls_strerror(ret));
406
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();
416
425
ret = gnutls_certificate_set_openpgp_key_file
417
426
(mc->cred, pubkeyfilename, seckeyfilename,
418
427
GNUTLS_OPENPGP_FMT_BASE64);
419
if (ret != GNUTLS_E_SUCCESS) {
428
if(ret != GNUTLS_E_SUCCESS) {
421
430
"Error[%d] while reading the OpenPGP key pair ('%s',"
422
431
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
428
437
/* GnuTLS server initialization */
429
438
ret = gnutls_dh_params_init(&mc->dh_params);
430
if (ret != GNUTLS_E_SUCCESS) {
431
fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
432
" %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));
435
444
ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
436
if (ret != GNUTLS_E_SUCCESS) {
437
fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
438
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));
457
466
/* GnuTLS session creation */
458
467
ret = gnutls_init(session, GNUTLS_SERVER);
459
if (ret != GNUTLS_E_SUCCESS){
468
if(ret != GNUTLS_E_SUCCESS){
460
469
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
461
470
safer_gnutls_strerror(ret));
466
475
ret = gnutls_priority_set_direct(*session, mc->priority, &err);
467
if (ret != GNUTLS_E_SUCCESS) {
476
if(ret != GNUTLS_E_SUCCESS) {
468
477
fprintf(stderr, "Syntax error at: %s\n", err);
469
478
fprintf(stderr, "GnuTLS error: %s\n",
470
479
safer_gnutls_strerror(ret));
471
gnutls_deinit (*session);
480
gnutls_deinit(*session);
476
485
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
478
if (ret != GNUTLS_E_SUCCESS) {
487
if(ret != GNUTLS_E_SUCCESS) {
479
488
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
480
489
safer_gnutls_strerror(ret));
481
gnutls_deinit (*session);
490
gnutls_deinit(*session);
485
494
/* ignore client certificate if any. */
486
gnutls_certificate_server_set_request (*session,
495
gnutls_certificate_server_set_request(*session,
489
gnutls_dh_set_prime_bits (*session, mc->dh_bits);
498
gnutls_dh_set_prime_bits(*session, mc->dh_bits);
511
521
char interface[IF_NAMESIZE];
512
522
gnutls_session_t session;
514
ret = init_gnutls_session (mc, &session);
524
ret = init_gnutls_session(mc, &session);
548
558
fprintf(stderr, "Bad address: %s\n", ip);
551
to.in6.sin6_port = htons(port); /* Spurious warning */
561
to.in6.sin6_port = htons(port); /* Spurious warnings from
563
-Wunreachable-code */
553
565
to.in6.sin6_scope_id = (uint32_t)if_index;
569
581
ret = connect(tcp_sd, &to.in, sizeof(to));
571
583
perror("connect");
575
587
const char *out = mandos_protocol_version;
578
590
size_t out_size = strlen(out);
579
ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
591
ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
580
592
out_size - written));
600
612
fprintf(stderr, "Establishing TLS session with %s\n", ip);
603
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
615
gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
606
ret = gnutls_handshake (session);
618
ret = gnutls_handshake(session);
607
619
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
609
if (ret != GNUTLS_E_SUCCESS){
621
if(ret != GNUTLS_E_SUCCESS){
611
623
fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
626
638
buffer_capacity = adjustbuffer(&buffer, buffer_length,
627
639
buffer_capacity);
628
if (buffer_capacity == 0){
640
if(buffer_capacity == 0){
629
641
perror("adjustbuffer");
634
ret = gnutls_record_recv(session, buffer+buffer_length,
646
sret = gnutls_record_recv(session, buffer+buffer_length,
641
653
case GNUTLS_E_INTERRUPTED:
642
654
case GNUTLS_E_AGAIN:
644
656
case GNUTLS_E_REHANDSHAKE:
646
ret = gnutls_handshake (session);
658
ret = gnutls_handshake(session);
647
659
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
649
661
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
656
668
fprintf(stderr, "Unknown error while reading data from"
657
669
" encrypted session with Mandos server\n");
659
gnutls_bye (session, GNUTLS_SHUT_RDWR);
671
gnutls_bye(session, GNUTLS_SHUT_RDWR);
663
buffer_length += (size_t) ret;
675
buffer_length += (size_t) sret;
668
680
fprintf(stderr, "Closing TLS session\n");
671
gnutls_bye (session, GNUTLS_SHUT_RDWR);
683
gnutls_bye(session, GNUTLS_SHUT_RDWR);
673
if (buffer_length > 0){
685
if(buffer_length > 0){
674
686
decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
676
688
&decrypted_buffer);
677
if (decrypted_buffer_size >= 0){
689
if(decrypted_buffer_size >= 0){
679
691
while(written < (size_t) decrypted_buffer_size){
680
ret = (int)fwrite (decrypted_buffer + written, 1,
681
(size_t)decrypted_buffer_size - written,
692
ret = (int)fwrite(decrypted_buffer + written, 1,
693
(size_t)decrypted_buffer_size - written,
683
695
if(ret == 0 and ferror(stdout)){
685
697
fprintf(stderr, "Error writing encrypted data: %s\n",
744
756
avahi_address_snprint(ip, sizeof(ip), address);
746
758
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
747
PRIu16 ") on port %d\n", name, host_name, ip,
759
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
760
ip, (intmax_t)interface, port);
750
762
int ret = start_mandos_communication(ip, port, interface, mc);
752
764
avahi_simple_poll_quit(mc->simple_poll);
787
799
the callback function is called the Avahi server will free the
788
800
resolver for us. */
790
if (!(avahi_s_service_resolver_new(mc->server, interface,
802
if(!(avahi_s_service_resolver_new(mc->server, interface,
791
803
protocol, name, type, domain,
792
804
AVAHI_PROTO_INET6, 0,
793
805
resolve_callback, mc)))
820
834
char *connect_to = NULL;
821
835
char tempdir[] = "/tmp/mandosXXXXXX";
836
bool tempdir_created = false;
822
837
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
823
838
const char *seckey = PATHDIR "/" SECKEY;
824
839
const char *pubkey = PATHDIR "/" PUBKEY;
826
841
mandos_context mc = { .simple_poll = NULL, .server = NULL,
827
842
.dh_bits = 1024, .priority = "SECURE256"
828
843
":!CTYPE-X.509:+CTYPE-OPENPGP" };
829
bool gnutls_initalized = false;
830
bool gpgme_initalized = false;
844
bool gnutls_initialized = false;
845
bool gpgme_initialized = false;
833
849
struct argp_option options[] = {
860
876
.doc = "GnuTLS priority string for the TLS handshake",
878
{ .name = "delay", .key = 131,
880
.doc = "Maximum delay to wait for interface startup",
865
error_t parse_opt (int key, char *arg,
866
struct argp_state *state) {
867
/* Get the INPUT argument from `argp_parse', which we know is
868
a pointer to our plugin list pointer. */
885
error_t parse_opt(int key, char *arg,
886
struct argp_state *state) {
870
888
case 128: /* --debug */
885
903
case 129: /* --dh-bits */
887
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");
890
908
exit(EXIT_FAILURE);
910
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
893
912
case 130: /* --priority */
894
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");
896
922
case ARGP_KEY_ARG:
898
924
case ARGP_KEY_END:
908
934
.doc = "Mandos client -- Get and decrypt"
909
935
" passwords from a Mandos server" };
910
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
911
if (ret == ARGP_ERR_UNKNOWN){
936
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
937
if(ret == ARGP_ERR_UNKNOWN){
912
938
fprintf(stderr, "Unknown error while parsing arguments\n");
913
939
exitcode = EXIT_FAILURE;
918
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);
920
953
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
922
955
perror("socket");
923
956
exitcode = EXIT_FAILURE;
957
ret = klogctl(7, NULL, 0);
926
963
strcpy(network.ifr_name, interface);
927
964
ret = ioctl(sd, SIOCGIFFLAGS, &network);
929
966
perror("ioctl SIOCGIFFLAGS");
967
ret = klogctl(7, NULL, 0);
930
971
exitcode = EXIT_FAILURE;
937
978
perror("ioctl SIOCSIFFLAGS");
938
979
exitcode = EXIT_FAILURE;
980
ret = klogctl(7, NULL, 0);
942
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));
944
1000
perror("close");
1002
/* Restores kernel loglevel to default */
1003
ret = klogctl(7, NULL, 0);
951
1017
ret = setuid(uid);
953
1019
perror("setuid");
961
1022
ret = init_gnutls_global(&mc, pubkey, seckey);
963
1024
fprintf(stderr, "init_gnutls_global failed\n");
964
1025
exitcode = EXIT_FAILURE;
967
gnutls_initalized = true;
1028
gnutls_initialized = true;
970
1031
if(mkdtemp(tempdir) == NULL){
971
1032
perror("mkdtemp");
1035
tempdir_created = true;
976
1037
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
977
fprintf(stderr, "gpgme_initalized failed\n");
1038
fprintf(stderr, "init_gpgme failed\n");
978
1039
exitcode = EXIT_FAILURE;
981
gpgme_initalized = true;
1042
gpgme_initialized = true;
984
1045
if_index = (AvahiIfIndex) if_nametoindex(interface);
985
1046
if(if_index == 0){
986
1047
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1048
exitcode = EXIT_FAILURE;
990
1052
if(connect_to != NULL){
996
1058
exitcode = EXIT_FAILURE;
1000
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
1002
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");
1003
1066
exitcode = EXIT_FAILURE;
1069
port = (uint16_t)tmpmax;
1006
1070
*address = '\0';
1007
1071
address = connect_to;
1008
1072
ret = start_mandos_communication(address, port, if_index, &mc);
1024
1088
/* Allocate main Avahi loop object */
1025
1089
mc.simple_poll = avahi_simple_poll_new();
1026
if (mc.simple_poll == NULL) {
1090
if(mc.simple_poll == NULL) {
1027
1091
fprintf(stderr, "Avahi: Failed to create simple poll"
1029
1093
exitcode = EXIT_FAILURE;
1071
1135
/* Run the main loop */
1074
1138
fprintf(stderr, "Starting Avahi loop search\n");
1077
1141
avahi_simple_poll_loop(mc.simple_poll);
1082
1146
fprintf(stderr, "%s exiting\n", argv[0]);
1085
1149
/* Cleanup things */
1087
1151
avahi_s_service_browser_free(sb);
1089
if (mc.server != NULL)
1153
if(mc.server != NULL)
1090
1154
avahi_server_free(mc.server);
1092
if (mc.simple_poll != NULL)
1156
if(mc.simple_poll != NULL)
1093
1157
avahi_simple_poll_free(mc.simple_poll);
1095
if (gnutls_initalized){
1159
if(gnutls_initialized){
1096
1160
gnutls_certificate_free_credentials(mc.cred);
1097
gnutls_global_deinit ();
1161
gnutls_global_deinit();
1098
1162
gnutls_dh_params_deinit(mc.dh_params);
1101
if(gpgme_initalized){
1165
if(gpgme_initialized){
1102
1166
gpgme_release(mc.ctx);
1105
1169
/* Removes the temp directory used by GPGME */
1106
if(tempdir[0] != '\0'){
1170
if(tempdir_created){
1108
1172
struct dirent *direntry;
1109
1173
d = opendir(tempdir);
1175
if(errno != ENOENT){
1114
1180
direntry = readdir(d);
1115
1181
if(direntry == NULL){
1118
if (direntry->d_type == DT_REG){
1119
char *fullname = NULL;
1120
ret = asprintf(&fullname, "%s/%s", tempdir,
1126
ret = unlink(fullname);
1128
fprintf(stderr, "unlink(\"%s\"): %s",
1129
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,
1136
1207
ret = rmdir(tempdir);
1208
if(ret == -1 and errno != ENOENT){
1138
1209
perror("rmdir");
1142
1213
return exitcode;