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
13
* Copyright © 2008 Björn Påhlsson
15
15
* This program is free software: you can redistribute it and/or
16
16
* modify it under the terms of the GNU General Public License as
36
36
#define _GNU_SOURCE /* TEMP_FAILURE_RETRY(), asprintf() */
38
38
#include <stdio.h> /* fprintf(), stderr, fwrite(),
39
stdout, ferror(), sscanf(),
41
40
#include <stdint.h> /* uint16_t, uint32_t */
42
41
#include <stddef.h> /* NULL, size_t, ssize_t */
43
42
#include <stdlib.h> /* free(), EXIT_SUCCESS, EXIT_FAILURE,
49
48
#include <sys/types.h> /* socket(), inet_pton(), sockaddr,
50
49
sockaddr_in6, PF_INET6,
51
50
SOCK_STREAM, INET6_ADDRSTRLEN,
52
uid_t, gid_t, open(), opendir(),
51
uid_t, gid_t, open(), opendir(), DIR */
54
52
#include <sys/stat.h> /* open() */
55
53
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
56
54
struct in6_addr, inet_pton(),
58
56
#include <fcntl.h> /* open() */
59
#include <dirent.h> /* opendir(), struct dirent, readdir()
61
#include <inttypes.h> /* PRIu16, intmax_t, SCNdMAX */
57
#include <dirent.h> /* opendir(), struct dirent, readdir() */
58
#include <inttypes.h> /* PRIu16 */
62
59
#include <assert.h> /* assert() */
63
60
#include <errno.h> /* perror(), errno */
64
#include <time.h> /* nanosleep(), time() */
61
#include <time.h> /* time() */
65
62
#include <net/if.h> /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
66
63
SIOCSIFFLAGS, if_indextoname(),
67
64
if_nametoindex(), IF_NAMESIZE */
70
67
getuid(), getgid(), setuid(),
72
69
#include <arpa/inet.h> /* inet_pton(), htons */
73
#include <iso646.h> /* not, and, or */
70
#include <iso646.h> /* not, and */
74
71
#include <argp.h> /* struct argp_option, error_t, struct
75
72
argp_state, struct argp,
76
73
argp_parse(), ARGP_KEY_ARG,
77
74
ARGP_KEY_END, ARGP_ERR_UNKNOWN */
78
#include <sys/klog.h> /* klogctl() */
81
77
/* All Avahi types, constants and functions
95
91
init_gnutls_session(),
97
#include <gnutls/openpgp.h>
98
/* gnutls_certificate_set_openpgp_key_file(),
93
#include <gnutls/openpgp.h> /* gnutls_certificate_set_openpgp_key_file(),
99
94
GNUTLS_OPENPGP_FMT_BASE64 */
135
130
size_t adjustbuffer(char **buffer, size_t buffer_length,
136
131
size_t buffer_capacity){
137
if(buffer_length + BUFFER_SIZE > buffer_capacity){
132
if (buffer_length + BUFFER_SIZE > buffer_capacity){
138
133
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
142
137
buffer_capacity += BUFFER_SIZE;
158
* Helper function to insert pub and seckey to the engine keyring.
153
* Helper function to insert pub and seckey to the enigne keyring.
160
155
bool import_key(const char *filename){
162
157
gpgme_data_t pgp_data;
164
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
159
fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
170
165
rc = gpgme_data_new_from_fd(&pgp_data, fd);
171
if(rc != GPG_ERR_NO_ERROR){
166
if (rc != GPG_ERR_NO_ERROR){
172
167
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
173
168
gpgme_strsource(rc), gpgme_strerror(rc));
177
172
rc = gpgme_op_import(mc->ctx, pgp_data);
178
if(rc != GPG_ERR_NO_ERROR){
173
if (rc != GPG_ERR_NO_ERROR){
179
174
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
180
175
gpgme_strsource(rc), gpgme_strerror(rc));
184
ret = (int)TEMP_FAILURE_RETRY(close(fd));
179
ret = TEMP_FAILURE_RETRY(close(fd));
193
188
fprintf(stderr, "Initialize gpgme\n");
197
192
gpgme_check_version(NULL);
198
193
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
199
if(rc != GPG_ERR_NO_ERROR){
194
if (rc != GPG_ERR_NO_ERROR){
200
195
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
201
196
gpgme_strsource(rc), gpgme_strerror(rc));
205
200
/* Set GPGME home directory for the OpenPGP engine only */
206
rc = gpgme_get_engine_info(&engine_info);
207
if(rc != GPG_ERR_NO_ERROR){
201
rc = gpgme_get_engine_info (&engine_info);
202
if (rc != GPG_ERR_NO_ERROR){
208
203
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
209
204
gpgme_strsource(rc), gpgme_strerror(rc));
225
220
/* Create new GPGME "context" */
226
221
rc = gpgme_new(&(mc->ctx));
227
if(rc != GPG_ERR_NO_ERROR){
222
if (rc != GPG_ERR_NO_ERROR){
228
223
fprintf(stderr, "bad gpgme_new: %s: %s\n",
229
224
gpgme_strsource(rc), gpgme_strerror(rc));
233
if(not import_key(pubkey) or not import_key(seckey)){
228
if (not import_key(pubkey) or not import_key(seckey)){
241
236
* Decrypt OpenPGP data.
242
237
* Returns -1 on error
244
static ssize_t pgp_packet_decrypt(const mandos_context *mc,
245
const char *cryptotext,
239
static ssize_t pgp_packet_decrypt (const mandos_context *mc,
240
const char *cryptotext,
248
243
gpgme_data_t dh_crypto, dh_plain;
249
244
gpgme_error_t rc;
251
246
size_t plaintext_capacity = 0;
252
247
ssize_t plaintext_length = 0;
255
250
fprintf(stderr, "Trying to decrypt OpenPGP data\n");
258
253
/* Create new GPGME data buffer from memory cryptotext */
259
254
rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
261
if(rc != GPG_ERR_NO_ERROR){
256
if (rc != GPG_ERR_NO_ERROR){
262
257
fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
263
258
gpgme_strsource(rc), gpgme_strerror(rc));
267
262
/* Create new empty GPGME data buffer for the plaintext */
268
263
rc = gpgme_data_new(&dh_plain);
269
if(rc != GPG_ERR_NO_ERROR){
264
if (rc != GPG_ERR_NO_ERROR){
270
265
fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
271
266
gpgme_strsource(rc), gpgme_strerror(rc));
272
267
gpgme_data_release(dh_crypto);
276
271
/* Decrypt data from the cryptotext data buffer to the plaintext
278
273
rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
279
if(rc != GPG_ERR_NO_ERROR){
274
if (rc != GPG_ERR_NO_ERROR){
280
275
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
281
276
gpgme_strsource(rc), gpgme_strerror(rc));
282
277
plaintext_length = -1;
284
279
gpgme_decrypt_result_t result;
285
280
result = gpgme_op_decrypt_result(mc->ctx);
287
282
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
289
284
fprintf(stderr, "Unsupported algorithm: %s\n",
318
313
/* Seek back to the beginning of the GPGME plaintext data buffer */
319
if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
314
if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
320
315
perror("gpgme_data_seek");
321
316
plaintext_length = -1;
322
317
goto decrypt_end;
327
322
plaintext_capacity = adjustbuffer(plaintext,
328
323
(size_t)plaintext_length,
329
324
plaintext_capacity);
330
if(plaintext_capacity == 0){
325
if (plaintext_capacity == 0){
331
326
perror("adjustbuffer");
332
327
plaintext_length = -1;
333
328
goto decrypt_end;
366
361
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 */
364
static const char * safer_gnutls_strerror (int value) {
365
const char *ret = gnutls_strerror (value); /* Spurious warning */
373
367
ret = "(unknown)";
392
386
ret = gnutls_global_init();
393
if(ret != GNUTLS_E_SUCCESS) {
394
fprintf(stderr, "GnuTLS global_init: %s\n",
395
safer_gnutls_strerror(ret));
387
if (ret != GNUTLS_E_SUCCESS) {
388
fprintf (stderr, "GnuTLS global_init: %s\n",
389
safer_gnutls_strerror(ret));
400
394
/* "Use a log level over 10 to enable all debugging options."
401
395
* - GnuTLS manual
407
401
/* OpenPGP credentials */
408
402
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();
403
if (ret != GNUTLS_E_SUCCESS){
404
fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
406
safer_gnutls_strerror(ret));
407
gnutls_global_deinit ();
425
417
ret = gnutls_certificate_set_openpgp_key_file
426
418
(mc->cred, pubkeyfilename, seckeyfilename,
427
419
GNUTLS_OPENPGP_FMT_BASE64);
428
if(ret != GNUTLS_E_SUCCESS) {
420
if (ret != GNUTLS_E_SUCCESS) {
430
422
"Error[%d] while reading the OpenPGP key pair ('%s',"
431
423
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
437
429
/* GnuTLS server initialization */
438
430
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));
431
if (ret != GNUTLS_E_SUCCESS) {
432
fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
433
" %s\n", safer_gnutls_strerror(ret));
444
436
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));
437
if (ret != GNUTLS_E_SUCCESS) {
438
fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
439
safer_gnutls_strerror(ret));
466
458
/* GnuTLS session creation */
467
459
ret = gnutls_init(session, GNUTLS_SERVER);
468
if(ret != GNUTLS_E_SUCCESS){
460
if (ret != GNUTLS_E_SUCCESS){
469
461
fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
470
462
safer_gnutls_strerror(ret));
475
467
ret = gnutls_priority_set_direct(*session, mc->priority, &err);
476
if(ret != GNUTLS_E_SUCCESS) {
468
if (ret != GNUTLS_E_SUCCESS) {
477
469
fprintf(stderr, "Syntax error at: %s\n", err);
478
470
fprintf(stderr, "GnuTLS error: %s\n",
479
471
safer_gnutls_strerror(ret));
480
gnutls_deinit(*session);
472
gnutls_deinit (*session);
485
477
ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
487
if(ret != GNUTLS_E_SUCCESS) {
479
if (ret != GNUTLS_E_SUCCESS) {
488
480
fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
489
481
safer_gnutls_strerror(ret));
490
gnutls_deinit(*session);
482
gnutls_deinit (*session);
494
486
/* ignore client certificate if any. */
495
gnutls_certificate_server_set_request(*session,
487
gnutls_certificate_server_set_request (*session,
498
gnutls_dh_set_prime_bits(*session, mc->dh_bits);
490
gnutls_dh_set_prime_bits (*session, mc->dh_bits);
521
512
char interface[IF_NAMESIZE];
522
513
gnutls_session_t session;
524
ret = init_gnutls_session(mc, &session);
515
ret = init_gnutls_session (mc, &session);
558
549
fprintf(stderr, "Bad address: %s\n", ip);
561
to.in6.sin6_port = htons(port); /* Spurious warnings from
563
-Wunreachable-code */
552
to.in6.sin6_port = htons(port); /* Spurious warning */
565
554
to.in6.sin6_scope_id = (uint32_t)if_index;
581
570
ret = connect(tcp_sd, &to.in, sizeof(to));
583
572
perror("connect");
587
576
const char *out = mandos_protocol_version;
590
579
size_t out_size = strlen(out);
591
ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
580
ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
592
581
out_size - written));
612
601
fprintf(stderr, "Establishing TLS session with %s\n", ip);
615
gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
604
gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
618
ret = gnutls_handshake(session);
607
ret = gnutls_handshake (session);
619
608
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
621
if(ret != GNUTLS_E_SUCCESS){
610
if (ret != GNUTLS_E_SUCCESS){
623
612
fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
638
627
buffer_capacity = adjustbuffer(&buffer, buffer_length,
639
628
buffer_capacity);
640
if(buffer_capacity == 0){
629
if (buffer_capacity == 0){
641
630
perror("adjustbuffer");
646
sret = gnutls_record_recv(session, buffer+buffer_length,
635
ret = gnutls_record_recv(session, buffer+buffer_length,
653
642
case GNUTLS_E_INTERRUPTED:
654
643
case GNUTLS_E_AGAIN:
656
645
case GNUTLS_E_REHANDSHAKE:
658
ret = gnutls_handshake(session);
647
ret = gnutls_handshake (session);
659
648
} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
661
650
fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
668
657
fprintf(stderr, "Unknown error while reading data from"
669
658
" encrypted session with Mandos server\n");
671
gnutls_bye(session, GNUTLS_SHUT_RDWR);
660
gnutls_bye (session, GNUTLS_SHUT_RDWR);
675
buffer_length += (size_t) sret;
664
buffer_length += (size_t) ret;
680
669
fprintf(stderr, "Closing TLS session\n");
683
gnutls_bye(session, GNUTLS_SHUT_RDWR);
672
gnutls_bye (session, GNUTLS_SHUT_RDWR);
685
if(buffer_length > 0){
674
if (buffer_length > 0){
686
675
decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
688
677
&decrypted_buffer);
689
if(decrypted_buffer_size >= 0){
678
if (decrypted_buffer_size >= 0){
691
680
while(written < (size_t) decrypted_buffer_size){
692
ret = (int)fwrite(decrypted_buffer + written, 1,
693
(size_t)decrypted_buffer_size - written,
681
ret = (int)fwrite (decrypted_buffer + written, 1,
682
(size_t)decrypted_buffer_size - written,
695
684
if(ret == 0 and ferror(stdout)){
697
686
fprintf(stderr, "Error writing encrypted data: %s\n",
756
745
avahi_address_snprint(ip, sizeof(ip), address);
758
747
fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
759
PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
760
ip, (intmax_t)interface, port);
748
PRIu16 ") on port %d\n", name, host_name, ip,
762
751
int ret = start_mandos_communication(ip, port, interface, mc);
764
753
avahi_simple_poll_quit(mc->simple_poll);
799
788
the callback function is called the Avahi server will free the
800
789
resolver for us. */
802
if(!(avahi_s_service_resolver_new(mc->server, interface,
791
if (!(avahi_s_service_resolver_new(mc->server, interface,
803
792
protocol, name, type, domain,
804
793
AVAHI_PROTO_INET6, 0,
805
794
resolve_callback, mc)))
834
821
char *connect_to = NULL;
835
822
char tempdir[] = "/tmp/mandosXXXXXX";
836
bool tempdir_created = false;
837
823
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
838
824
const char *seckey = PATHDIR "/" SECKEY;
839
825
const char *pubkey = PATHDIR "/" PUBKEY;
841
827
mandos_context mc = { .simple_poll = NULL, .server = NULL,
842
828
.dh_bits = 1024, .priority = "SECURE256"
843
829
":!CTYPE-X.509:+CTYPE-OPENPGP" };
844
bool gnutls_initialized = false;
845
bool gpgme_initialized = false;
830
bool gnutls_initalized = false;
831
bool gpgme_initalized = false;
849
834
struct argp_option options[] = {
876
861
.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) {
866
error_t parse_opt (int key, char *arg,
867
struct argp_state *state) {
868
/* Get the INPUT argument from `argp_parse', which we know is
869
a pointer to our plugin list pointer. */
888
871
case 128: /* --debug */
903
886
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");
888
mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
908
891
exit(EXIT_FAILURE);
910
mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
912
894
case 130: /* --priority */
913
895
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
897
case ARGP_KEY_ARG:
924
899
case ARGP_KEY_END:
934
909
.doc = "Mandos client -- Get and decrypt"
935
910
" passwords from a Mandos server" };
936
ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
937
if(ret == ARGP_ERR_UNKNOWN){
911
ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
912
if (ret == ARGP_ERR_UNKNOWN){
938
913
fprintf(stderr, "Unknown error while parsing arguments\n");
939
914
exitcode = EXIT_FAILURE;
944
919
/* 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
921
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
955
923
perror("socket");
956
924
exitcode = EXIT_FAILURE;
957
ret = klogctl(7, NULL, 0);
963
927
strcpy(network.ifr_name, interface);
964
928
ret = ioctl(sd, SIOCGIFFLAGS, &network);
966
930
perror("ioctl SIOCGIFFLAGS");
967
ret = klogctl(7, NULL, 0);
971
931
exitcode = EXIT_FAILURE;
978
938
perror("ioctl SIOCSIFFLAGS");
979
939
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));
943
ret = TEMP_FAILURE_RETRY(close(sd));
1000
945
perror("close");
1002
/* Restores kernel loglevel to default */
1003
ret = klogctl(7, NULL, 0);
1014
959
perror("setgid");
1022
962
ret = init_gnutls_global(&mc, pubkey, seckey);
1024
964
fprintf(stderr, "init_gnutls_global failed\n");
1025
965
exitcode = EXIT_FAILURE;
1028
gnutls_initialized = true;
968
gnutls_initalized = true;
1031
971
if(mkdtemp(tempdir) == NULL){
1032
972
perror("mkdtemp");
1035
tempdir_created = true;
1037
977
if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
1038
fprintf(stderr, "init_gpgme failed\n");
978
fprintf(stderr, "gpgme_initalized failed\n");
1039
979
exitcode = EXIT_FAILURE;
1042
gpgme_initialized = true;
982
gpgme_initalized = true;
1045
985
if_index = (AvahiIfIndex) if_nametoindex(interface);
1046
986
if(if_index == 0){
1047
987
fprintf(stderr, "No such interface: \"%s\"\n", interface);
1048
exitcode = EXIT_FAILURE;
1052
991
if(connect_to != NULL){
1058
997
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");
1001
uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
1003
perror("Bad port number");
1066
1004
exitcode = EXIT_FAILURE;
1069
port = (uint16_t)tmpmax;
1070
1007
*address = '\0';
1071
1008
address = connect_to;
1072
1009
ret = start_mandos_communication(address, port, if_index, &mc);
1088
1025
/* Allocate main Avahi loop object */
1089
1026
mc.simple_poll = avahi_simple_poll_new();
1090
if(mc.simple_poll == NULL) {
1027
if (mc.simple_poll == NULL) {
1091
1028
fprintf(stderr, "Avahi: Failed to create simple poll"
1093
1030
exitcode = EXIT_FAILURE;
1135
1072
/* Run the main loop */
1138
1075
fprintf(stderr, "Starting Avahi loop search\n");
1141
1078
avahi_simple_poll_loop(mc.simple_poll);
1146
1083
fprintf(stderr, "%s exiting\n", argv[0]);
1149
1086
/* Cleanup things */
1151
1088
avahi_s_service_browser_free(sb);
1153
if(mc.server != NULL)
1090
if (mc.server != NULL)
1154
1091
avahi_server_free(mc.server);
1156
if(mc.simple_poll != NULL)
1093
if (mc.simple_poll != NULL)
1157
1094
avahi_simple_poll_free(mc.simple_poll);
1159
if(gnutls_initialized){
1096
if (gnutls_initalized){
1160
1097
gnutls_certificate_free_credentials(mc.cred);
1161
gnutls_global_deinit();
1098
gnutls_global_deinit ();
1162
1099
gnutls_dh_params_deinit(mc.dh_params);
1165
if(gpgme_initialized){
1102
if(gpgme_initalized){
1166
1103
gpgme_release(mc.ctx);
1169
1106
/* Removes the temp directory used by GPGME */
1170
if(tempdir_created){
1107
if(tempdir[0] != '\0'){
1172
1109
struct dirent *direntry;
1173
1110
d = opendir(tempdir);
1175
if(errno != ENOENT){
1180
1115
direntry = readdir(d);
1181
1116
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,
1119
if (direntry->d_type == DT_REG){
1120
char *fullname = NULL;
1121
ret = asprintf(&fullname, "%s/%s", tempdir,
1127
ret = unlink(fullname);
1129
fprintf(stderr, "unlink(\"%s\"): %s",
1130
fullname, strerror(errno));
1207
1137
ret = rmdir(tempdir);
1208
if(ret == -1 and errno != ENOENT){
1209
1139
perror("rmdir");
1213
1143
return exitcode;