2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
31 #include "pwmd-error.h"
37 #include "util-misc.h"
38 #include "util-string.h"
46 static pthread_mutex_t cache_mutex
;
47 static struct slist_s
*key_cache
;
48 static unsigned char *cache_iv
;
49 static unsigned char *cache_key
;
50 static size_t cache_blocksize
;
51 static size_t cache_keysize
;
52 struct agent_s
*cache_agent
;
55 extern void log_write (const char *fmt
, ...);
57 static gpg_error_t
clear_once (file_cache_t
* p
, int agent
);
58 static int remove_entry (const char *);
59 static void free_entry (file_cache_t
* p
, int agent
);
62 get_entry (const char *filename
)
64 int t
= slist_length (key_cache
);
70 for (i
= 0; i
< t
; i
++)
72 file_cache_t
*p
= slist_nth_data (key_cache
, i
);
74 if (!strcmp (p
->filename
, filename
))
82 cache_lock_mutex (void *ctx
, const char *filename
, long lock_timeout
, int add
,
85 MUTEX_LOCK (&cache_mutex
);
87 file_cache_t
*p
= get_entry (filename
);
89 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
93 MUTEX_UNLOCK (&cache_mutex
);
94 MUTEX_TRYLOCK (ctx
, p
->mutex
, rc
, lock_timeout
);
98 rc
= cache_add_file (filename
, NULL
, timeout
);
101 p
= get_entry (filename
);
102 MUTEX_UNLOCK (&cache_mutex
);
104 MUTEX_TRYLOCK (ctx
, p
->mutex
, rc
, lock_timeout
);
108 MUTEX_UNLOCK (&cache_mutex
);
113 MUTEX_UNLOCK (&cache_mutex
);
116 pthread_cleanup_pop (0);
117 return !p
&& !rc
? GPG_ERR_NO_DATA
: rc
;
121 remove_entry (const char *filename
)
123 MUTEX_LOCK (&cache_mutex
);
124 file_cache_t
*p
= get_entry (filename
);
126 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
129 /* Keep a refcount because another client maybe editing this same new
130 * file. The entry needs to be kept in case the other client SAVE's.
137 pthread_cleanup_pop (1);
142 free_cache_data (file_cache_t
* cache
)
144 gpg_error_t rc
= GPG_ERR_NO_DATA
;
146 struct client_thread_s
*found
= NULL
;
152 MUTEX_LOCK (&cache_mutex
);
153 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
154 MUTEX_LOCK (&cn_mutex
);
155 pthread_cleanup_push (release_mutex_cb
, &cn_mutex
);
157 t
= slist_length (cn_thread_list
);
158 for (i
= 0; i
< t
; i
++)
160 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
165 self
= pthread_equal (pthread_self (), thd
->tid
);
167 if (thd
->cl
->filename
&& !strcmp (thd
->cl
->filename
, cache
->filename
))
175 /* Continue trying to find a client who has the same file open and
176 * also has a lock. */
177 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->filename
, -1, 0, -1);
186 if (self
&& (!rc
|| rc
== GPG_ERR_NO_DATA
) && found
)
187 rc
= cache_lock_mutex (found
->cl
->ctx
, found
->cl
->filename
, -1, 0, -1);
189 if (exiting
|| !rc
|| rc
== GPG_ERR_NO_DATA
)
191 cache_free_data_once (cache
->data
);
193 cache
->defer_clear
= 0;
197 cache_unlock_mutex (found
->cl
->filename
, 0);
203 cache
->defer_clear
= 1;
205 pthread_cleanup_pop (1);
206 pthread_cleanup_pop (1);
211 cache_unlock_mutex (const char *filename
, int remove
)
213 MUTEX_LOCK (&cache_mutex
);
214 file_cache_t
*p
= get_entry (filename
);
216 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
220 MUTEX_UNLOCK (p
->mutex
);
222 remove_entry (filename
);
225 pthread_cleanup_pop (1);
226 return p
? 0 : GPG_ERR_NO_DATA
;
230 test_agent_cache_once (file_cache_t
*p
, const char *grip
)
236 rc
= agent_command (cache_agent
, &line
, NULL
, "KEYINFO --data %s", grip
);
239 char **fields
= str_split (line
, " ", 0);
242 if (*fields
[1] == 'T')
243 rc
= GPG_ERR_NO_DATA
;
245 rc
= isdigit (*fields
[4]) || *fields
[5] == 'C' ? 0 : GPG_ERR_NO_DATA
;
254 /* Test each keygrip in data->(sig)grip for gpg-agent cache status. The file is
255 * considered cached if at least one grip is cached in the agent or one siggrip
256 * is cached in the case of 'sign'.
259 test_agent_cache (file_cache_t
*data
, int sign
)
264 if ((!data
->grip
&& !sign
) || (!data
->siggrip
&& sign
))
265 return GPG_ERR_NO_DATA
;
267 for (p
= data
->grip
; !sign
&& p
&& *p
; p
++)
269 rc
= test_agent_cache_once (data
, *p
);
270 if (!rc
|| rc
!= GPG_ERR_NO_DATA
)
275 rc
= test_agent_cache_once (data
, data
->siggrip
);
281 extract_keygrip_once (struct crypto_s
*crypto
, char **keyids
, int sign
,
289 rc
= crypto_list_keys (crypto
, keyids
, sign
, &result
);
293 for (i
= 0; result
[i
]; i
++)
297 for (s
= result
[i
]->subkeys
; s
; s
= s
->next
)
301 if (sign
&& !s
->can_sign
)
304 for (k
= keyids
; *k
; k
++)
306 if (!strcmp (*k
, s
->keyid
) && s
->keygrip
)
309 size_t len
= strv_length (grips
);
311 tmp
= xrealloc (grips
, len
+2 * sizeof (char *));
319 grips
[len
] = str_dup (s
->keygrip
);
343 crypto_free_key_list (result
);
344 return rc
? rc
: grips
? rc
: GPG_ERR_NO_DATA
;
348 extract_keygrips (file_cache_t
*data
)
351 struct crypto_s
*crypto
;
352 char **grips
= NULL
, **siggrips
= NULL
;
354 if (!data
|| !data
->data
)
357 rc
= crypto_init (&crypto
, NULL
, NULL
, 0, NULL
);
361 pthread_cleanup_push ((void *)crypto_free
, crypto
);
363 if (data
->data
->pubkey
)
364 rc
= extract_keygrip_once (crypto
, data
->data
->pubkey
, 0, &grips
);
366 if (!rc
&& data
->data
->sigkey
)
370 strv_printf (&tmp
, "%s", data
->data
->sigkey
);
371 rc
= extract_keygrip_once (crypto
, tmp
, 1, &siggrips
);
377 strv_free (data
->grip
);
379 xfree (data
->siggrip
);
380 data
->siggrip
= NULL
;
381 if (siggrips
&& *siggrips
)
382 data
->siggrip
= str_dup (*siggrips
);
383 strv_free (siggrips
);
388 strv_free (siggrips
);
391 pthread_cleanup_pop (1);
396 iscached (const char *filename
, int *defer
, int agent
, int sign
)
398 file_cache_t
*p
= get_entry (filename
);
404 return GPG_ERR_NO_DATA
;
407 *defer
= p
->defer_clear
;
413 return GPG_ERR_NO_DATA
;
416 *defer
= p
->defer_clear
;
418 if (!rc
&& (p
->grip
|| p
->siggrip
))
419 rc
= test_agent_cache (p
, sign
);
421 rc
= GPG_ERR_NO_DATA
;
429 MUTEX_LOCK (&cache_mutex
);
430 unsigned total
= 0, i
, n
;
432 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
433 n
= slist_length (key_cache
);
434 for (i
= 0; i
< n
; i
++)
436 file_cache_t
*p
= slist_nth_data (key_cache
, i
);
438 if (!iscached (p
->filename
, NULL
, 0, 0))
442 pthread_cleanup_pop (1);
447 cache_adjust_timeout ()
449 MUTEX_LOCK (&cache_mutex
);
450 int t
= slist_length (key_cache
);
453 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
455 for (i
= 0; i
< t
; i
++)
457 file_cache_t
*p
= slist_nth_data (key_cache
, i
);
462 if (!p
->timeout
|| (p
->defer_clear
&& p
->timeout
!= -1))
464 /* The file associated with this cache entry may be locked by a
465 * client. Defer freeing this cache entry until the next timeout
467 if (!clear_once (p
, 1))
468 send_status_all (STATUS_CACHE
, NULL
);
472 pthread_cleanup_pop (1);
476 set_timeout (const char *filename
, int timeout
, int defer
, int force
)
478 MUTEX_LOCK (&cache_mutex
);
479 file_cache_t
*p
= get_entry (filename
);
482 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
487 if (!p
->defer_clear
&& (p
->timeout
== -1 || force
))
488 p
->timeout
= timeout
;
490 if (!timeout
|| defer
)
492 rc
= clear_once (p
, 1);
494 send_status_all (STATUS_CACHE
, NULL
);
498 rc
= GPG_ERR_NOT_FOUND
;
500 pthread_cleanup_pop (1);
505 cache_set_data (const char *filename
, struct cache_data_s
* data
)
507 MUTEX_LOCK (&cache_mutex
);
508 gpg_error_t rc
= GPG_ERR_NO_DATA
;
509 file_cache_t
*p
= get_entry (filename
);
511 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
516 rc
= set_timeout (filename
, p
->reset
, p
->defer_clear
, 0);
517 if (!rc
&& !p
->reset
)
520 send_status_all (STATUS_CACHE
, NULL
);
523 rc
= extract_keygrips (p
);
526 pthread_cleanup_pop (1);
527 return p
&& !rc
? 0 : rc
;
530 struct cache_data_s
*
531 cache_get_data (const char *filename
)
533 MUTEX_LOCK (&cache_mutex
);
534 file_cache_t
*p
= get_entry (filename
);
536 MUTEX_UNLOCK (&cache_mutex
);
537 return p
? p
->data
: NULL
;
541 cache_add_file (const char *filename
, struct cache_data_s
*data
, int timeout
)
543 MUTEX_LOCK (&cache_mutex
);
544 file_cache_t
*p
= get_entry (filename
);
548 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
554 rc
= set_timeout (filename
, timeout
, p
->defer_clear
, 0);
558 p
= xcalloc (1, sizeof (file_cache_t
));
561 p
->mutex
= (pthread_mutex_t
*) xmalloc (sizeof (pthread_mutex_t
));
564 pthread_mutexattr_t attr
;
565 pthread_mutexattr_init (&attr
);
566 pthread_mutexattr_settype (&attr
, PTHREAD_MUTEX_RECURSIVE
);
567 pthread_mutex_init (p
->mutex
, &attr
);
568 pthread_mutexattr_destroy (&attr
);
569 p
->filename
= str_dup (filename
);
572 new = slist_append (key_cache
, p
);
576 rc
= cache_set_timeout(filename
, timeout
);
580 pthread_mutex_destroy (p
->mutex
);
600 rc
= extract_keygrips (p
);
602 pthread_cleanup_pop (1);
605 send_status_all (STATUS_CACHE
, NULL
);
611 cache_clear_agent_keys (const char *filename
, int decrypt
, int sign
)
613 MUTEX_LOCK (&cache_mutex
);
616 file_cache_t
*p
= get_entry (filename
);
618 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
622 for (key
= p
->grip
; decrypt
&& key
&& *key
; key
++)
624 rc
= agent_command (cache_agent
, NULL
, NULL
,
625 "CLEAR_PASSPHRASE --mode=normal %s", *key
);
630 if (p
->siggrip
&& sign
)
632 rc
= agent_command (cache_agent
, NULL
, NULL
,
633 "CLEAR_PASSPHRASE --mode=normal %s", p
->siggrip
);
639 rc
= GPG_ERR_NOT_FOUND
;
641 pthread_cleanup_pop (1);
646 clear_once (file_cache_t
*p
, int agent
)
648 gpg_error_t rc
= 0, trc
;
651 rc
= cache_clear_agent_keys (p
->filename
, 1, 1);
653 trc
= free_cache_data (p
);
654 return rc
? rc
: trc
;
658 free_entry (file_cache_t
*p
, int agent
)
665 rc
= clear_once (p
, agent
);
667 log_write ("%s(): %s", __FUNCTION__
, pwmd_strerror (rc
));
671 pthread_mutex_destroy (p
->mutex
);
678 key_cache
= slist_remove (key_cache
, p
);
683 cache_clear (struct client_s
*client
, const char *filename
, int agent
)
686 gpg_error_t rc
= 0, all_rc
= 0;
689 MUTEX_LOCK (&cache_mutex
);
690 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
694 p
= get_entry (filename
);
697 rc
= clear_once (p
, agent
);
699 log_write ("%s(): %s", __FUNCTION__
, pwmd_strerror (rc
));
704 t
= slist_length (key_cache
);
705 for (i
= 0; i
< t
; i
++)
707 p
= slist_nth_data (key_cache
, i
);
711 assuan_peercred_t peer
;
712 int n
= client
->no_access_param
;
714 client
->no_access_param
= 1;
715 rc
= do_validate_peer (client
->ctx
, p
->filename
, &peer
);
716 client
->no_access_param
= n
;
717 if (rc
== GPG_ERR_FORBIDDEN
)
718 rc
= peer_is_invoker (client
);
722 all_rc
= !all_rc
? rc
: all_rc
;
727 clear_once (p
, agent
);
731 pthread_cleanup_pop (1);
732 return filename
? rc
: all_rc
;
736 cache_iscached (const char *filename
, int *defer
, int agent
, int sign
)
741 return GPG_ERR_INV_PARAMETER
;
743 MUTEX_LOCK (&cache_mutex
);
744 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
745 rc
= iscached (filename
, defer
, agent
, sign
);
747 /* Test if the data file disappeared from the filesystem. */
748 if ((!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
749 && access (filename
, R_OK
) == -1)
751 rc
= gpg_error_from_errno (errno
);
752 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
754 /* Fixes clearing the cache entry that was created during OPEN when
755 * SAVE'ing a new file. */
756 if ((defer
&& *defer
== 1) || !defer
)
758 rc
= cache_defer_clear (filename
);
767 pthread_cleanup_pop (1);
772 cache_defer_clear (const char *filename
)
774 MUTEX_LOCK (&cache_mutex
);
775 file_cache_t
*p
= get_entry (filename
);
779 rc
= GPG_ERR_NOT_FOUND
;
783 MUTEX_UNLOCK (&cache_mutex
);
788 cache_set_timeout (const char *filename
, int timeout
)
790 return set_timeout (filename
, timeout
, 0, 1);
797 MUTEX_LOCK (&cache_mutex
);
798 int i
, t
= slist_length (key_cache
);
800 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
801 for (i
= 0; i
< t
; i
++)
803 file_cache_t
*p
= slist_nth_data (key_cache
, i
);
806 t
= slist_length (key_cache
);
810 gcry_free (cache_key
);
814 agent_free (cache_agent
);
816 pthread_cleanup_pop (1);
817 pthread_mutex_destroy (&cache_mutex
);
823 pthread_mutexattr_t attr
;
825 pthread_mutexattr_init (&attr
);
826 pthread_mutexattr_settype (&attr
, PTHREAD_MUTEX_RECURSIVE
);
827 pthread_mutex_init (&cache_mutex
, &attr
);
828 pthread_mutexattr_destroy (&attr
);
836 rc
= agent_init (&cache_agent
);
845 rc
= gcry_cipher_algo_info (GCRY_CIPHER_AES
, GCRYCTL_GET_BLKLEN
, NULL
,
850 rc
= gcry_cipher_algo_info (GCRY_CIPHER_AES
, GCRYCTL_GET_KEYLEN
, NULL
,
855 cache_key
= gcry_malloc (cache_keysize
);
857 return GPG_ERR_ENOMEM
;
859 cache_iv
= xmalloc (cache_blocksize
);
862 gcry_free (cache_key
);
864 return GPG_ERR_ENOMEM
;
867 gcry_create_nonce (cache_key
, cache_keysize
);
868 gcry_create_nonce (cache_iv
, cache_blocksize
);
879 MUTEX_LOCK (&cache_mutex
);
885 MUTEX_UNLOCK (&cache_mutex
);
889 cache_free_data_once (struct cache_data_s
*data
)
894 strv_free (data
->pubkey
);
895 xfree (data
->sigkey
);
902 release_cipher (void *arg
)
904 gcry_cipher_close ((gcry_cipher_hd_t
) arg
);
908 cache_encrypt (struct crypto_s
*crypto
)
912 size_t len
= crypto
->plaintext_size
;
914 rc
= gcry_cipher_open (&h
, GCRY_CIPHER_AES128
, GCRY_CIPHER_MODE_CBC
, 0);
918 if (len
% cache_blocksize
)
922 len
+= cache_blocksize
- (len
% cache_blocksize
);
923 p
= xrealloc (crypto
->plaintext
, len
* sizeof (unsigned char));
926 gcry_cipher_close (h
);
927 return GPG_ERR_ENOMEM
;
930 crypto
->plaintext
= p
;
931 memset (&crypto
->plaintext
[crypto
->plaintext_size
], 0,
932 len
- crypto
->plaintext_size
);
935 pthread_cleanup_push (release_cipher
, h
);
936 rc
= gcry_cipher_setiv (h
, cache_iv
, cache_blocksize
);
939 rc
= gcry_cipher_setkey (h
, cache_key
, cache_keysize
);
942 unsigned char *buf
= xmalloc (len
);
944 pthread_cleanup_push (xfree
, buf
);
951 rc
= gcry_cipher_encrypt (h
, buf
, len
, crypto
->plaintext
, len
);
954 xfree (crypto
->plaintext
);
955 crypto
->plaintext
= buf
;
956 crypto
->plaintext_size
= len
;
960 pthread_cleanup_pop (rc
!= 0);
964 pthread_cleanup_pop (1);
969 cache_decrypt (struct crypto_s
*crypto
)
971 gcry_cipher_hd_t h
= NULL
;
974 rc
= gcry_cipher_open (&h
, GCRY_CIPHER_AES
, GCRY_CIPHER_MODE_CBC
, 0);
979 rc
= gcry_cipher_setiv (h
, cache_iv
, cache_blocksize
);
982 rc
= gcry_cipher_setkey (h
, cache_key
, cache_keysize
);
984 pthread_cleanup_push (release_cipher
, h
);
988 rc
= gcry_cipher_decrypt (h
, crypto
->plaintext
, crypto
->plaintext_size
,
990 if (rc
|| strncmp ((char *)crypto
->plaintext
, "<?xml ", 6))
993 rc
= GPG_ERR_BAD_DATA
;
997 pthread_cleanup_pop (1);
1006 MUTEX_LOCK (&cache_mutex
);
1007 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
1008 rc
= agent_kill_scd (cache_agent
);
1009 pthread_cleanup_pop (1);
1014 cache_agent_command (const char *cmd
)
1018 MUTEX_LOCK (&cache_mutex
);
1019 rc
= agent_command (cache_agent
, NULL
, NULL
, "%s", cmd
);
1020 MUTEX_UNLOCK (&cache_mutex
);
1025 cache_release_mutex ()
1027 MUTEX_UNLOCK (&cache_mutex
);