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