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
)
276 for (p
= data
->siggrip
; p
&& *p
; p
++)
278 rc
= test_agent_cache_once (data
, *p
);
279 if (!rc
|| rc
!= GPG_ERR_NO_DATA
)
288 extract_keygrip_once (struct crypto_s
*crypto
, char **keyids
, int sign
,
296 rc
= crypto_list_keys (crypto
, keyids
, sign
, &result
);
300 for (i
= 0; result
[i
]; i
++)
304 for (s
= result
[i
]->subkeys
; s
; s
= s
->next
)
308 if (sign
&& !s
->can_sign
)
311 for (k
= keyids
; *k
; k
++)
313 if (!strcmp (*k
, s
->keyid
) && s
->keygrip
)
316 size_t len
= strv_length (grips
);
318 tmp
= xrealloc (grips
, len
+2 * sizeof (char *));
326 grips
[len
] = str_dup (s
->keygrip
);
350 crypto_free_key_list (result
);
351 return rc
? rc
: grips
? rc
: GPG_ERR_NO_DATA
;
355 extract_keygrips (file_cache_t
*data
)
358 struct crypto_s
*crypto
;
359 char **grips
= NULL
, **siggrips
= NULL
;
361 if (!data
|| !data
->data
)
364 rc
= crypto_init (&crypto
, NULL
, NULL
, 0, NULL
);
368 pthread_cleanup_push ((void *)crypto_free
, crypto
);
370 if (data
->data
->pubkey
)
371 rc
= extract_keygrip_once (crypto
, data
->data
->pubkey
, 0, &grips
);
373 if (!rc
&& data
->data
->sigkey
)
374 rc
= extract_keygrip_once (crypto
, data
->data
->sigkey
, 1, &siggrips
);
378 strv_free (data
->grip
);
380 strv_free (data
->siggrip
);
381 data
->siggrip
= siggrips
;
386 strv_free (siggrips
);
389 pthread_cleanup_pop (1);
394 iscached (const char *filename
, int *defer
, int agent
, int sign
)
396 file_cache_t
*p
= get_entry (filename
);
402 return GPG_ERR_NO_DATA
;
405 *defer
= p
->defer_clear
;
411 return GPG_ERR_NO_DATA
;
414 *defer
= p
->defer_clear
;
416 if (!rc
&& (p
->grip
|| p
->siggrip
))
417 rc
= test_agent_cache (p
, sign
);
419 rc
= GPG_ERR_NO_DATA
;
427 MUTEX_LOCK (&cache_mutex
);
428 unsigned total
= 0, i
, n
;
430 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
431 n
= slist_length (key_cache
);
432 for (i
= 0; i
< n
; i
++)
434 file_cache_t
*p
= slist_nth_data (key_cache
, i
);
436 if (!iscached (p
->filename
, NULL
, 0, 0))
440 pthread_cleanup_pop (1);
445 cache_adjust_timeout ()
447 MUTEX_LOCK (&cache_mutex
);
448 int t
= slist_length (key_cache
);
451 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
453 for (i
= 0; i
< t
; i
++)
455 file_cache_t
*p
= slist_nth_data (key_cache
, i
);
460 if (!p
->timeout
|| (p
->defer_clear
&& p
->timeout
!= -1))
462 /* The file associated with this cache entry may be locked by a
463 * client. Defer freeing this cache entry until the next timeout
465 if (!clear_once (p
, 1))
466 send_status_all (STATUS_CACHE
, NULL
);
470 pthread_cleanup_pop (1);
474 set_timeout (const char *filename
, int timeout
, int defer
, int force
)
476 MUTEX_LOCK (&cache_mutex
);
477 file_cache_t
*p
= get_entry (filename
);
480 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
485 if (!p
->defer_clear
&& (p
->timeout
== -1 || force
))
486 p
->timeout
= timeout
;
488 if (!timeout
|| defer
)
490 rc
= clear_once (p
, 1);
492 send_status_all (STATUS_CACHE
, NULL
);
496 rc
= GPG_ERR_NOT_FOUND
;
498 pthread_cleanup_pop (1);
503 cache_set_data (const char *filename
, struct cache_data_s
* data
)
505 MUTEX_LOCK (&cache_mutex
);
506 gpg_error_t rc
= GPG_ERR_NO_DATA
;
507 file_cache_t
*p
= get_entry (filename
);
509 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
514 rc
= set_timeout (filename
, p
->reset
, p
->defer_clear
, 0);
515 if (!rc
&& !p
->reset
)
518 send_status_all (STATUS_CACHE
, NULL
);
521 rc
= extract_keygrips (p
);
524 pthread_cleanup_pop (1);
525 return p
&& !rc
? 0 : rc
;
528 struct cache_data_s
*
529 cache_get_data (const char *filename
)
531 MUTEX_LOCK (&cache_mutex
);
532 file_cache_t
*p
= get_entry (filename
);
534 MUTEX_UNLOCK (&cache_mutex
);
535 return p
? p
->data
: NULL
;
539 cache_add_file (const char *filename
, struct cache_data_s
*data
, int timeout
)
541 MUTEX_LOCK (&cache_mutex
);
542 file_cache_t
*p
= get_entry (filename
);
546 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
552 rc
= set_timeout (filename
, timeout
, p
->defer_clear
, 0);
556 p
= xcalloc (1, sizeof (file_cache_t
));
559 p
->mutex
= (pthread_mutex_t
*) xmalloc (sizeof (pthread_mutex_t
));
562 pthread_mutexattr_t attr
;
563 pthread_mutexattr_init (&attr
);
564 pthread_mutexattr_settype (&attr
, PTHREAD_MUTEX_RECURSIVE
);
565 pthread_mutex_init (p
->mutex
, &attr
);
566 pthread_mutexattr_destroy (&attr
);
567 p
->filename
= str_dup (filename
);
570 new = slist_append (key_cache
, p
);
574 rc
= cache_set_timeout(filename
, timeout
);
578 pthread_mutex_destroy (p
->mutex
);
598 rc
= extract_keygrips (p
);
600 pthread_cleanup_pop (1);
603 send_status_all (STATUS_CACHE
, NULL
);
609 cache_clear_agent_keys (const char *filename
, int decrypt
, int sign
)
611 MUTEX_LOCK (&cache_mutex
);
614 file_cache_t
*p
= get_entry (filename
);
616 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
620 for (key
= p
->grip
; decrypt
&& key
&& *key
; key
++)
622 rc
= agent_command (cache_agent
, NULL
, NULL
,
623 "CLEAR_PASSPHRASE --mode=normal %s", *key
);
628 for (key
= p
->siggrip
; sign
&& key
&& *key
; key
++)
630 rc
= agent_command (cache_agent
, NULL
, NULL
,
631 "CLEAR_PASSPHRASE --mode=normal %s", *key
);
637 rc
= GPG_ERR_NOT_FOUND
;
639 pthread_cleanup_pop (1);
644 clear_once (file_cache_t
*p
, int agent
)
646 gpg_error_t rc
= 0, trc
;
649 rc
= cache_clear_agent_keys (p
->filename
, 1, 1);
651 trc
= free_cache_data (p
);
652 return rc
? rc
: trc
;
656 free_entry (file_cache_t
*p
, int agent
)
663 rc
= clear_once (p
, agent
);
665 log_write ("%s(): %s", __FUNCTION__
, pwmd_strerror (rc
));
669 pthread_mutex_destroy (p
->mutex
);
674 strv_free (p
->siggrip
);
676 key_cache
= slist_remove (key_cache
, p
);
681 cache_clear (struct client_s
*client
, const char *filename
, int agent
)
684 gpg_error_t rc
= 0, all_rc
= 0;
687 MUTEX_LOCK (&cache_mutex
);
688 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
692 p
= get_entry (filename
);
695 rc
= clear_once (p
, agent
);
697 log_write ("%s(): %s", __FUNCTION__
, pwmd_strerror (rc
));
702 t
= slist_length (key_cache
);
703 for (i
= 0; i
< t
; i
++)
705 p
= slist_nth_data (key_cache
, i
);
709 assuan_peercred_t peer
;
710 int n
= client
->no_access_param
;
712 client
->no_access_param
= 1;
713 rc
= do_validate_peer (client
->ctx
, p
->filename
, &peer
);
714 client
->no_access_param
= n
;
715 if (rc
== GPG_ERR_FORBIDDEN
)
716 rc
= peer_is_invoker (client
);
720 all_rc
= !all_rc
? rc
: all_rc
;
725 clear_once (p
, agent
);
729 pthread_cleanup_pop (1);
730 return filename
? rc
: all_rc
;
734 cache_iscached (const char *filename
, int *defer
, int agent
, int sign
)
739 return GPG_ERR_INV_PARAMETER
;
741 MUTEX_LOCK (&cache_mutex
);
742 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
743 rc
= iscached (filename
, defer
, agent
, sign
);
745 /* Test if the data file disappeared from the filesystem. */
746 if ((!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
747 && access (filename
, R_OK
) == -1)
749 rc
= gpg_error_from_errno (errno
);
750 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
752 /* Fixes clearing the cache entry that was created during OPEN when
753 * SAVE'ing a new file. */
754 if ((defer
&& *defer
== 1) || !defer
)
756 rc
= cache_defer_clear (filename
);
765 pthread_cleanup_pop (1);
770 cache_defer_clear (const char *filename
)
772 MUTEX_LOCK (&cache_mutex
);
773 file_cache_t
*p
= get_entry (filename
);
777 rc
= GPG_ERR_NOT_FOUND
;
781 MUTEX_UNLOCK (&cache_mutex
);
786 cache_set_timeout (const char *filename
, int timeout
)
788 return set_timeout (filename
, timeout
, 0, 1);
795 MUTEX_LOCK (&cache_mutex
);
796 int i
, t
= slist_length (key_cache
);
798 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
799 for (i
= 0; i
< t
; i
++)
801 file_cache_t
*p
= slist_nth_data (key_cache
, i
);
804 t
= slist_length (key_cache
);
808 gcry_free (cache_key
);
812 agent_free (cache_agent
);
814 pthread_cleanup_pop (1);
815 pthread_mutex_destroy (&cache_mutex
);
821 pthread_mutexattr_t attr
;
823 pthread_mutexattr_init (&attr
);
824 pthread_mutexattr_settype (&attr
, PTHREAD_MUTEX_RECURSIVE
);
825 pthread_mutex_init (&cache_mutex
, &attr
);
826 pthread_mutexattr_destroy (&attr
);
834 rc
= agent_init (&cache_agent
);
843 rc
= gcry_cipher_algo_info (GCRY_CIPHER_AES
, GCRYCTL_GET_BLKLEN
, NULL
,
848 rc
= gcry_cipher_algo_info (GCRY_CIPHER_AES
, GCRYCTL_GET_KEYLEN
, NULL
,
853 cache_key
= gcry_malloc (cache_keysize
);
855 return GPG_ERR_ENOMEM
;
857 cache_iv
= xmalloc (cache_blocksize
);
860 gcry_free (cache_key
);
862 return GPG_ERR_ENOMEM
;
865 gcry_create_nonce (cache_key
, cache_keysize
);
866 gcry_create_nonce (cache_iv
, cache_blocksize
);
877 MUTEX_LOCK (&cache_mutex
);
883 MUTEX_UNLOCK (&cache_mutex
);
887 cache_free_data_once (struct cache_data_s
*data
)
892 strv_free (data
->pubkey
);
893 strv_free (data
->sigkey
);
900 release_cipher (void *arg
)
902 gcry_cipher_close ((gcry_cipher_hd_t
) arg
);
906 cache_encrypt (struct crypto_s
*crypto
)
910 size_t len
= crypto
->plaintext_size
;
912 rc
= gcry_cipher_open (&h
, GCRY_CIPHER_AES128
, GCRY_CIPHER_MODE_CBC
, 0);
916 if (len
% cache_blocksize
)
920 len
+= cache_blocksize
- (len
% cache_blocksize
);
921 p
= xrealloc (crypto
->plaintext
, len
* sizeof (unsigned char));
924 gcry_cipher_close (h
);
925 return GPG_ERR_ENOMEM
;
928 crypto
->plaintext
= p
;
929 memset (&crypto
->plaintext
[crypto
->plaintext_size
], 0,
930 len
- crypto
->plaintext_size
);
933 pthread_cleanup_push (release_cipher
, h
);
934 rc
= gcry_cipher_setiv (h
, cache_iv
, cache_blocksize
);
937 rc
= gcry_cipher_setkey (h
, cache_key
, cache_keysize
);
940 unsigned char *buf
= xmalloc (len
);
942 pthread_cleanup_push (xfree
, buf
);
949 rc
= gcry_cipher_encrypt (h
, buf
, len
, crypto
->plaintext
, len
);
952 xfree (crypto
->plaintext
);
953 crypto
->plaintext
= buf
;
954 crypto
->plaintext_size
= len
;
958 pthread_cleanup_pop (rc
!= 0);
962 pthread_cleanup_pop (1);
967 cache_decrypt (struct crypto_s
*crypto
)
969 gcry_cipher_hd_t h
= NULL
;
972 rc
= gcry_cipher_open (&h
, GCRY_CIPHER_AES
, GCRY_CIPHER_MODE_CBC
, 0);
977 rc
= gcry_cipher_setiv (h
, cache_iv
, cache_blocksize
);
980 rc
= gcry_cipher_setkey (h
, cache_key
, cache_keysize
);
982 pthread_cleanup_push (release_cipher
, h
);
986 rc
= gcry_cipher_decrypt (h
, crypto
->plaintext
, crypto
->plaintext_size
,
988 if (rc
|| strncmp ((char *)crypto
->plaintext
, "<?xml ", 6))
991 rc
= GPG_ERR_BAD_DATA
;
995 pthread_cleanup_pop (1);
1004 MUTEX_LOCK (&cache_mutex
);
1005 pthread_cleanup_push (release_mutex_cb
, &cache_mutex
);
1006 rc
= agent_kill_scd (cache_agent
);
1007 pthread_cleanup_pop (1);
1012 cache_agent_command (const char *cmd
)
1016 MUTEX_LOCK (&cache_mutex
);
1017 rc
= agent_command (cache_agent
, NULL
, NULL
, "%s", cmd
);
1018 MUTEX_UNLOCK (&cache_mutex
);
1023 cache_release_mutex ()
1025 MUTEX_UNLOCK (&cache_mutex
);