2 Copyright (C) 2006-2019 Ben Kibbey <bjk@luxsci.net>
4 This file is part of pwmd.
6 Pwmd is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 Pwmd is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
29 #include "pwmd-error.h"
35 #include "util-misc.h"
36 #include "util-string.h"
52 static pthread_mutex_t cache_mutex
;
53 static struct slist_s
*key_cache
;
54 static struct slist_s
*plaintext_list
;
55 static unsigned char *cache_iv
;
56 static unsigned char *cache_key
;
57 static size_t cache_blocksize
;
58 static size_t cache_keysize
;
59 static struct agent_s
*cache_agent
;
62 void log_write (const char *fmt
, ...);
64 static gpg_error_t
clear_once (file_cache_t
* p
, int agent
, int force
);
65 static int remove_entry (const char *);
66 static void free_entry (file_cache_t
* p
, int agent
);
69 get_entry (const char *filename
)
71 int t
= slist_length (key_cache
);
77 for (i
= 0; i
< t
; i
++)
79 file_cache_t
*p
= slist_nth_data (key_cache
, i
);
81 if (!strcmp (p
->filename
, filename
))
89 cache_lock_mutex (void *ctx
, const char *filename
, long lock_timeout
, int add
,
92 MUTEX_LOCK (&cache_mutex
);
94 file_cache_t
*p
= get_entry (filename
);
96 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
100 MUTEX_UNLOCK (&cache_mutex
);
101 MUTEX_TRYLOCK (ctx
, p
->mutex
, rc
, lock_timeout
);
102 p
->flags
|= CACHE_FLAG_LOCKED
;
106 rc
= cache_add_file (filename
, NULL
, timeout
);
109 p
= get_entry (filename
);
110 MUTEX_UNLOCK (&cache_mutex
);
113 MUTEX_TRYLOCK (ctx
, p
->mutex
, rc
, lock_timeout
);
114 p
->flags
|= CACHE_FLAG_LOCKED
;
119 MUTEX_UNLOCK (&cache_mutex
);
124 MUTEX_UNLOCK (&cache_mutex
);
127 pthread_cleanup_pop (0);
128 return !p
&& !rc
? GPG_ERR_NO_DATA
: rc
;
132 remove_entry (const char *filename
)
134 MUTEX_LOCK (&cache_mutex
);
135 file_cache_t
*p
= get_entry (filename
);
137 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
140 /* Keep a refcount because another client maybe editing this same new
141 * file. The entry needs to be kept in case the other client SAVE's.
148 pthread_cleanup_pop (1);
153 free_cache_data (file_cache_t
*cache
, int force
)
155 gpg_error_t rc
= GPG_ERR_NO_DATA
;
157 struct client_thread_s
*found
= NULL
;
163 MUTEX_LOCK (&cn_mutex
);
164 t
= slist_length (cn_thread_list
);
166 /* Prevent clearing the cache entry when there are any connected clients
167 * using this cache entry to prevent clearing the plaintext which would cause
168 * cache_plaintext_set() to return GPG_ERR_NO_DATA and prevent CoW commands
170 for (i
= 0; i
< t
; i
++)
172 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
177 if (thd
->cl
->filename
&& !strcmp (thd
->cl
->filename
, cache
->filename
)
180 cache
->defer_clear
= 1;
181 MUTEX_UNLOCK (&cn_mutex
);
186 pthread_cleanup_push (release_mutex_cb
, &cn_mutex
);
187 MUTEX_LOCK (&cache_mutex
);
188 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
190 for (i
= 0; i
< t
; i
++)
192 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
197 self
= pthread_equal (pthread_self (), thd
->tid
);
199 if (thd
->cl
->filename
&& !strcmp (thd
->cl
->filename
, cache
->filename
))
207 /* Continue trying to find a client who has the same file open and
208 * also has a lock. */
209 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->filename
, -1, 0, -1);
218 if (self
&& (!rc
|| rc
== GPG_ERR_NO_DATA
) && found
)
219 rc
= cache_lock_mutex (found
->cl
->ctx
, found
->cl
->filename
, -1, 0, -1);
221 if (exiting
|| !rc
|| rc
== GPG_ERR_NO_DATA
)
223 cache_free_data_once (cache
->data
);
225 cache
->defer_clear
= 0;
226 cache
->timeout
= (long)-1;
229 cache_unlock_mutex (found
->cl
->filename
, 0);
235 cache
->defer_clear
= 1;
237 pthread_cleanup_pop (1);
238 pthread_cleanup_pop (1);
243 cache_unlock_mutex (const char *filename
, int remove
)
245 MUTEX_LOCK (&cache_mutex
);
246 file_cache_t
*p
= get_entry (filename
);
248 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
252 MUTEX_UNLOCK (p
->mutex
);
253 p
->flags
&= ~CACHE_FLAG_LOCKED
;
255 remove_entry (filename
);
258 pthread_cleanup_pop (1);
259 return p
? 0 : GPG_ERR_NO_DATA
;
263 get_keyinfo (const char *grip
, char ***r_fields
)
268 rc
= agent_command (cache_agent
, &line
, NULL
, "KEYINFO --data %s", grip
);
272 *r_fields
= str_split (line
, " ", 0);
274 return !(*r_fields
) ? GPG_ERR_ENOMEM
: rc
;
278 cache_is_shadowed (const char *filename
)
280 MUTEX_LOCK (&cache_mutex
);
281 file_cache_t
*f
= get_entry (filename
);
283 char **fields
= NULL
;
284 gpg_error_t rc
= GPG_ERR_NO_DATA
, trc
;
287 return GPG_ERR_NOT_FOUND
;
289 for (p
= f
->grip
; p
&& *p
; p
++)
291 trc
= get_keyinfo (*p
, &fields
);
292 if (!trc
&& *fields
[1] == 'T')
307 if (rc
== GPG_ERR_NO_DATA
&& f
->siggrip
)
309 trc
= get_keyinfo (f
->siggrip
, &fields
);
310 if (!trc
&& *fields
[1] == 'T')
317 MUTEX_UNLOCK (&cache_mutex
);
322 test_agent_cache_once (file_cache_t
*p
, const char *grip
)
325 char **fields
= NULL
;
328 rc
= get_keyinfo (grip
, &fields
);
333 if (*fields
[1] == 'T')
334 rc
= GPG_ERR_NO_DATA
;
336 rc
= isdigit (*fields
[4]) || *fields
[5] == 'C' ? 0 : GPG_ERR_NO_DATA
;
342 /* Test each keygrip in data->(sig)grip for gpg-agent cache status. The file is
343 * considered cached if at least one grip is cached in the agent or one siggrip
344 * is cached in the case of 'sign'.
347 test_agent_cache (file_cache_t
*data
, int sign
)
352 if ((!data
->grip
&& !sign
) || (!data
->siggrip
&& sign
))
353 return GPG_ERR_NO_DATA
;
355 for (p
= data
->grip
; !sign
&& p
&& *p
; p
++)
357 rc
= test_agent_cache_once (data
, *p
);
358 if (!rc
|| rc
!= GPG_ERR_NO_DATA
)
363 rc
= test_agent_cache_once (data
, data
->siggrip
);
369 extract_keygrip_once (struct crypto_s
*crypto
, char **keyids
, int sign
,
377 rc
= crypto_list_keys (crypto
, keyids
, sign
, &result
);
381 for (i
= 0; result
[i
]; i
++)
385 for (s
= result
[i
]->subkeys
; s
; s
= s
->next
)
389 if (sign
&& !s
->can_sign
)
392 for (k
= keyids
; *k
; k
++)
394 if (!strcmp (*k
, s
->keyid
) && s
->keygrip
)
397 size_t len
= strv_length (grips
);
399 tmp
= xrealloc (grips
, len
+2 * sizeof (char *));
407 grips
[len
] = str_dup (s
->keygrip
);
431 crypto_free_key_list (result
);
432 return rc
? rc
: grips
? rc
: GPG_ERR_NO_DATA
;
436 extract_keygrips (file_cache_t
*data
)
439 struct crypto_s
*crypto
;
440 char **grips
= NULL
, **siggrips
= NULL
;
442 if (!data
|| !data
->data
)
445 rc
= crypto_init (&crypto
, NULL
, NULL
, 0, NULL
);
449 pthread_cleanup_push ((void *)crypto_free
, crypto
);
451 if (data
->data
->pubkey
)
452 rc
= extract_keygrip_once (crypto
, data
->data
->pubkey
, 0, &grips
);
454 if (!rc
&& data
->data
->sigkey
)
458 strv_printf (&tmp
, "%s", data
->data
->sigkey
);
459 rc
= extract_keygrip_once (crypto
, tmp
, 1, &siggrips
);
465 strv_free (data
->grip
);
467 xfree (data
->siggrip
);
468 data
->siggrip
= NULL
;
469 if (siggrips
&& *siggrips
)
470 data
->siggrip
= str_dup (*siggrips
);
471 strv_free (siggrips
);
476 strv_free (siggrips
);
479 pthread_cleanup_pop (1);
484 iscached (const char *filename
, int *defer
, int agent
, int sign
)
486 file_cache_t
*p
= get_entry (filename
);
492 return GPG_ERR_NO_DATA
;
495 *defer
= p
->defer_clear
;
501 return GPG_ERR_NO_DATA
;
504 *defer
= p
->defer_clear
;
506 if (!rc
&& (p
->grip
|| p
->siggrip
))
507 rc
= test_agent_cache (p
, sign
);
509 rc
= GPG_ERR_NO_DATA
;
517 MUTEX_LOCK (&cache_mutex
);
518 unsigned total
= 0, i
, n
;
520 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
521 n
= slist_length (key_cache
);
522 for (i
= 0; i
< n
; i
++)
524 file_cache_t
*p
= slist_nth_data (key_cache
, i
);
526 if (!iscached (p
->filename
, NULL
, 0, 0))
530 pthread_cleanup_pop (1);
535 cache_adjust_timeout ()
537 MUTEX_LOCK (&cache_mutex
);
538 int t
= slist_length (key_cache
);
541 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
543 for (i
= 0; i
< t
; i
++)
545 file_cache_t
*p
= slist_nth_data (key_cache
, i
);
550 /* Test the cache entry timeout for expiration and also whether the file
551 * is a new one (no crc) and without any associated clients (refcount ==
552 * 1) and remove the cache entry accordingly. */
553 if (!p
->timeout
|| (p
->defer_clear
&& p
->timeout
!= (long)-1)
554 || (p
->timeout
> 0 && p
->refcount
== 1
555 && (!p
->data
|| !p
->data
->crc
)
556 && !(p
->flags
& CACHE_FLAG_LOCKED
)))
558 /* The file associated with this cache entry may be locked by a
559 * client. Defer freeing this cache entry until the next timeout
561 gpg_error_t rc
= clear_once (p
, 1, 0);
563 if (!rc
&& !p
->defer_clear
)
566 t
= slist_length (key_cache
);
567 send_status_all (STATUS_CACHE
, NULL
);
572 pthread_cleanup_pop (1);
576 set_timeout (const char *filename
, long timeout
, int defer
, int force
)
578 MUTEX_LOCK (&cache_mutex
);
579 file_cache_t
*p
= get_entry (filename
);
582 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
587 if (!p
->defer_clear
&& (p
->timeout
== -1 || force
))
588 p
->timeout
= timeout
;
590 if (!timeout
|| defer
)
592 rc
= clear_once (p
, 1, 0);
594 send_status_all (STATUS_CACHE
, NULL
);
598 rc
= GPG_ERR_NOT_FOUND
;
600 pthread_cleanup_pop (1);
605 cache_set_data (const char *filename
, struct cache_data_s
*data
)
607 MUTEX_LOCK (&cache_mutex
);
608 gpg_error_t rc
= GPG_ERR_NO_DATA
;
609 file_cache_t
*p
= get_entry (filename
);
611 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
615 xfree (data
->filename
);
616 data
->filename
= NULL
;
618 data
->filename
= str_dup (p
->filename
);
621 rc
= set_timeout (filename
, p
->reset
, p
->defer_clear
, 0);
622 if (!rc
&& !p
->reset
)
624 clear_once (p
, 0, 0);
625 send_status_all (STATUS_CACHE
, NULL
);
628 rc
= extract_keygrips (p
);
631 pthread_cleanup_pop (1);
632 return p
&& !rc
? 0 : rc
;
635 struct cache_data_s
*
636 cache_get_data (const char *filename
, int *defer
)
638 MUTEX_LOCK (&cache_mutex
);
639 file_cache_t
*p
= get_entry (filename
);
642 *defer
= p
->defer_clear
;
643 MUTEX_UNLOCK (&cache_mutex
);
644 return p
? p
->data
: NULL
;
648 cache_add_file (const char *filename
, struct cache_data_s
*data
, long timeout
)
650 MUTEX_LOCK (&cache_mutex
);
651 file_cache_t
*p
= get_entry (filename
);
655 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
661 char *str
= p
->filename
? str_dup (p
->filename
) : NULL
;
663 if (p
->filename
&& !str
)
667 xfree (data
->filename
);
668 data
->filename
= str
;
676 rc
= set_timeout (filename
, timeout
, p
->defer_clear
, 0);
681 p
= xcalloc (1, sizeof (file_cache_t
));
684 p
->mutex
= (pthread_mutex_t
*) xmalloc (sizeof (pthread_mutex_t
));
687 pthread_mutexattr_t attr
;
688 pthread_mutexattr_init (&attr
);
689 pthread_mutexattr_settype (&attr
, PTHREAD_MUTEX_RECURSIVE
);
690 pthread_mutex_init (p
->mutex
, &attr
);
691 pthread_mutexattr_destroy (&attr
);
692 p
->filename
= str_dup (filename
);
696 xfree (data
->filename
);
697 data
->filename
= NULL
;
698 data
->filename
= str_dup (p
->filename
);
703 new = slist_append (key_cache
, p
);
707 rc
= cache_set_timeout(filename
, timeout
);
711 pthread_mutex_destroy (p
->mutex
);
731 rc
= extract_keygrips (p
);
733 pthread_cleanup_pop (1);
736 send_status_all (STATUS_CACHE
, NULL
);
742 cache_clear_agent_keys (const char *filename
, int decrypt
, int sign
)
744 MUTEX_LOCK (&cache_mutex
);
747 file_cache_t
*p
= get_entry (filename
);
749 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
753 for (key
= p
->grip
; decrypt
&& key
&& *key
; key
++)
755 rc
= agent_command (cache_agent
, NULL
, NULL
,
756 "CLEAR_PASSPHRASE --mode=normal %s", *key
);
761 if (p
->siggrip
&& sign
)
762 rc
= agent_command (cache_agent
, NULL
, NULL
,
763 "CLEAR_PASSPHRASE --mode=normal %s", p
->siggrip
);
766 rc
= GPG_ERR_NOT_FOUND
;
768 pthread_cleanup_pop (1);
773 clear_once (file_cache_t
*p
, int agent
, int force
)
775 gpg_error_t rc
= 0, trc
;
778 rc
= cache_clear_agent_keys (p
->filename
, 1, 1);
780 trc
= free_cache_data (p
, force
);
781 return rc
? rc
: trc
;
785 free_entry (file_cache_t
*p
, int agent
)
792 rc
= clear_once (p
, agent
, 0);
794 log_write ("%s(): %s", __FUNCTION__
, pwmd_strerror (rc
));
798 pthread_mutex_destroy (p
->mutex
);
805 key_cache
= slist_remove (key_cache
, p
);
810 cache_clear (struct client_s
*client
, const char *filename
, int agent
,
814 gpg_error_t rc
= 0, all_rc
= 0;
817 MUTEX_LOCK (&cache_mutex
);
818 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
822 p
= get_entry (filename
);
825 rc
= clear_once (p
, agent
, force
);
827 log_write ("%s(): %s", __FUNCTION__
, pwmd_strerror (rc
));
832 t
= slist_length (key_cache
);
833 for (i
= 0; i
< t
; i
++)
835 p
= slist_nth_data (key_cache
, i
);
839 assuan_peercred_t peer
;
840 int n
= client
->no_access_param
;
842 client
->no_access_param
= 1;
843 rc
= do_validate_peer (client
->ctx
, p
->filename
, &peer
);
844 client
->no_access_param
= n
;
845 if (rc
== GPG_ERR_FORBIDDEN
)
846 rc
= peer_is_invoker (client
);
850 all_rc
= !all_rc
? rc
: all_rc
;
855 clear_once (p
, agent
, 0);
859 pthread_cleanup_pop (1);
860 return filename
? rc
: all_rc
;
864 cache_iscached (const char *filename
, int *defer
, int agent
, int sign
)
869 return GPG_ERR_INV_PARAMETER
;
871 MUTEX_LOCK (&cache_mutex
);
872 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
873 rc
= iscached (filename
, defer
, agent
, sign
);
875 /* Test if the data file disappeared from the filesystem. */
876 if ((!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
877 && access (filename
, R_OK
) == -1)
879 rc
= gpg_error_from_errno (errno
);
880 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
882 /* Fixes clearing the cache entry that was created during OPEN when
883 * SAVE'ing a new file. */
884 if ((defer
&& *defer
== 1) || !defer
)
886 rc
= cache_defer_clear (filename
);
895 pthread_cleanup_pop (1);
900 cache_defer_clear (const char *filename
)
902 MUTEX_LOCK (&cache_mutex
);
903 file_cache_t
*p
= get_entry (filename
);
907 rc
= GPG_ERR_NOT_FOUND
;
911 MUTEX_UNLOCK (&cache_mutex
);
916 cache_set_timeout (const char *filename
, long timeout
)
918 return set_timeout (filename
, timeout
, 0, 1);
925 MUTEX_LOCK (&cache_mutex
);
926 int i
, t
= slist_length (key_cache
);
928 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
929 for (i
= 0; i
< t
; i
++)
931 file_cache_t
*p
= slist_nth_data (key_cache
, i
);
934 t
= slist_length (key_cache
);
938 gcry_free (cache_key
);
942 agent_free (cache_agent
);
944 pthread_cleanup_pop (1);
945 pthread_mutex_destroy (&cache_mutex
);
951 pthread_mutexattr_t attr
;
953 pthread_mutexattr_init (&attr
);
954 pthread_mutexattr_settype (&attr
, PTHREAD_MUTEX_RECURSIVE
);
955 pthread_mutex_init (&cache_mutex
, &attr
);
956 pthread_mutexattr_destroy (&attr
);
964 rc
= agent_init (&cache_agent
);
973 rc
= gcry_cipher_algo_info (GCRY_CIPHER_AES
, GCRYCTL_GET_BLKLEN
, NULL
,
978 rc
= gcry_cipher_algo_info (GCRY_CIPHER_AES
, GCRYCTL_GET_KEYLEN
, NULL
,
983 cache_key
= gcry_malloc (cache_keysize
);
985 return GPG_ERR_ENOMEM
;
987 cache_iv
= xmalloc (cache_blocksize
);
990 gcry_free (cache_key
);
992 return GPG_ERR_ENOMEM
;
995 gcry_create_nonce (cache_key
, cache_keysize
);
996 gcry_create_nonce (cache_iv
, cache_blocksize
);
1000 cache_mutex_init ();
1007 MUTEX_LOCK (&cache_mutex
);
1013 MUTEX_UNLOCK (&cache_mutex
);
1017 update_plaintext_pointer (xmlDocPtr plaintext
)
1022 t
= slist_length (key_cache
);
1023 for (i
= 0; i
< t
; i
++)
1025 file_cache_t
*p
= slist_nth_data (key_cache
, i
);
1027 if (p
->data
&& p
->data
->plaintext
== plaintext
)
1028 p
->data
->plaintext
= NULL
;
1033 cache_free_data_once (struct cache_data_s
*data
)
1038 xfree (data
->filename
);
1039 strv_free (data
->pubkey
);
1040 xfree (data
->sigkey
);
1047 release_cipher (void *arg
)
1049 gcry_cipher_close ((gcry_cipher_hd_t
) arg
);
1053 cache_encrypt (struct crypto_s
*crypto
)
1057 size_t len
= crypto
->plaintext_size
;
1059 rc
= gcry_cipher_open (&h
, GCRY_CIPHER_AES128
, GCRY_CIPHER_MODE_CBC
, 0);
1063 if (len
% cache_blocksize
)
1067 len
+= cache_blocksize
- (len
% cache_blocksize
);
1068 p
= xrealloc (crypto
->plaintext
, len
* sizeof (unsigned char));
1071 gcry_cipher_close (h
);
1072 return GPG_ERR_ENOMEM
;
1075 crypto
->plaintext
= p
;
1076 memset (&crypto
->plaintext
[crypto
->plaintext_size
], 0,
1077 len
- crypto
->plaintext_size
);
1080 pthread_cleanup_push (release_cipher
, h
);
1081 rc
= gcry_cipher_setiv (h
, cache_iv
, cache_blocksize
);
1084 rc
= gcry_cipher_setkey (h
, cache_key
, cache_keysize
);
1087 unsigned char *buf
= xmalloc (len
);
1089 pthread_cleanup_push (xfree
, buf
);
1092 rc
= GPG_ERR_ENOMEM
;
1096 rc
= gcry_cipher_encrypt (h
, buf
, len
, crypto
->plaintext
, len
);
1099 xfree (crypto
->plaintext
);
1100 crypto
->plaintext
= buf
;
1101 crypto
->plaintext_size
= len
;
1105 pthread_cleanup_pop (rc
!= 0);
1109 pthread_cleanup_pop (1);
1114 cache_decrypt (struct crypto_s
*crypto
)
1116 gcry_cipher_hd_t h
= NULL
;
1119 rc
= gcry_cipher_open (&h
, GCRY_CIPHER_AES
, GCRY_CIPHER_MODE_CBC
, 0);
1124 rc
= gcry_cipher_setiv (h
, cache_iv
, cache_blocksize
);
1127 rc
= gcry_cipher_setkey (h
, cache_key
, cache_keysize
);
1129 pthread_cleanup_push (release_cipher
, h
);
1133 rc
= gcry_cipher_decrypt (h
, crypto
->plaintext
, crypto
->plaintext_size
,
1135 if (rc
|| strncmp ((char *)crypto
->plaintext
, "<?xml ", 6))
1138 rc
= GPG_ERR_BAD_DATA
;
1142 pthread_cleanup_pop (1);
1151 MUTEX_LOCK (&cache_mutex
);
1152 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
1153 rc
= agent_kill_scd (cache_agent
);
1154 pthread_cleanup_pop (1);
1159 cache_agent_command (const char *cmd
)
1163 MUTEX_LOCK (&cache_mutex
);
1164 rc
= agent_command (cache_agent
, NULL
, NULL
, "%s", cmd
);
1165 MUTEX_UNLOCK (&cache_mutex
);
1170 cache_release_mutex ()
1172 MUTEX_UNLOCK (&cache_mutex
);
1176 plaintext_get (struct cache_data_s
*data
, xmlDocPtr
*r_doc
)
1179 unsigned t
= slist_length (plaintext_list
);
1181 assert (r_doc
!= NULL
);
1183 for (i
= 0; i
< t
; i
++)
1185 struct plaintext_s
*p
= slist_nth_data (plaintext_list
, i
);
1187 if (!strcmp (p
->filename
, data
->filename
) && !p
->modified
)
1189 *r_doc
= p
->plaintext
;
1195 return GPG_ERR_NO_DATA
;
1199 cache_plaintext_get (const char *filename
, xmlDocPtr
*r_doc
)
1204 MUTEX_LOCK (&cache_mutex
);
1205 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
1207 p
= get_entry (filename
);
1209 rc
= plaintext_get (p
->data
, r_doc
);
1211 rc
= GPG_ERR_NO_DATA
;
1213 pthread_cleanup_pop (1);
1218 release_plaintext (xmlDocPtr
*plaintext
)
1224 return GPG_ERR_NO_DATA
;
1226 t
= slist_length (plaintext_list
);
1227 for (i
= 0; i
< t
; i
++)
1229 struct plaintext_s
*p
= slist_nth_data (plaintext_list
, i
);
1231 if (p
->plaintext
== *plaintext
)
1233 assert (p
->refcount
> 0);
1234 if (--p
->refcount
== 0)
1236 xmlFreeDoc (p
->plaintext
);
1237 xfree (p
->filename
);
1238 plaintext_list
= slist_remove (plaintext_list
, p
);
1240 update_plaintext_pointer (*plaintext
);
1248 return GPG_ERR_NO_DATA
;
1252 cache_plaintext_release (xmlDocPtr
*plaintext
)
1256 MUTEX_LOCK (&cache_mutex
);
1257 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
1258 rc
= release_plaintext (plaintext
);
1259 pthread_cleanup_pop (1);
1264 cache_plaintext_set (const char *filename
, xmlDocPtr doc
, int modified
)
1269 MUTEX_LOCK (&cache_mutex
);
1270 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
1272 p
= get_entry (filename
);
1275 struct plaintext_s
*s
= xcalloc (1, sizeof (struct plaintext_s
));
1279 s
->filename
= str_dup (p
->data
->filename
);
1283 int i
, t
= slist_length (plaintext_list
);
1285 /* Prevent cache_plaintext_get() from returning the other
1286 * client's document copy. */
1287 for (i
= 0; i
< t
; i
++)
1289 struct plaintext_s
*x
= slist_nth_data (plaintext_list
, i
);
1291 if (!strcmp (s
->filename
, x
->filename
))
1295 l
= slist_append (plaintext_list
, s
);
1299 s
->plaintext
= p
->data
->plaintext
= doc
;
1300 s
->modified
= modified
;
1305 xfree (s
->filename
);
1306 rc
= GPG_ERR_ENOMEM
;
1310 rc
= GPG_ERR_ENOMEM
;
1316 rc
= GPG_ERR_ENOMEM
;
1319 rc
= GPG_ERR_NO_DATA
;
1321 pthread_cleanup_pop (1);