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