1 /* cache.c - keep a cache of passphrases
2 * Copyright (C) 2002 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG 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 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG 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 this program; if not, see <http://www.gnu.org/licenses/>.
30 struct secret_data_s
{
31 int totallen
; /* this includes the padding */
32 int datalen
; /* actual data length */
36 typedef struct cache_item_s
*ITEM
;
41 int ttl
; /* max. lifetime given in seconds, -1 one means infinite */
43 struct secret_data_s
*pw
;
44 cache_mode_t cache_mode
;
53 release_data (struct secret_data_s
*data
)
58 static struct secret_data_s
*
59 new_data (const void *data
, size_t length
)
61 struct secret_data_s
*d
;
64 /* we pad the data to 32 bytes so that it get more complicated
65 finding something out by watching allocation patterns. This is
66 usally not possible but we better assume nothing about our
67 secure storage provider*/
68 total
= length
+ 32 - (length
% 32);
70 d
= gcry_malloc_secure (sizeof *d
+ total
- 1);
75 memcpy (d
->data
, data
, length
);
82 /* check whether there are items to expire */
87 time_t current
= gnupg_get_time ();
89 /* First expire the actual data */
90 for (r
=thecache
; r
; r
= r
->next
)
92 if (!r
->lockcount
&& r
->pw
93 && r
->ttl
>= 0 && r
->accessed
+ r
->ttl
< current
)
96 log_debug (" expired `%s' (%ds after last access)\n",
100 r
->accessed
= current
;
104 /* Second, make sure that we also remove them based on the created stamp so
105 that the user has to enter it from time to time. */
106 for (r
=thecache
; r
; r
= r
->next
)
108 unsigned long maxttl
;
110 switch (r
->cache_mode
)
112 case CACHE_MODE_SSH
: maxttl
= opt
.max_cache_ttl_ssh
; break;
113 default: maxttl
= opt
.max_cache_ttl
; break;
115 if (!r
->lockcount
&& r
->pw
&& r
->created
+ maxttl
< current
)
118 log_debug (" expired `%s' (%lus after creation)\n",
119 r
->key
, opt
.max_cache_ttl
);
120 release_data (r
->pw
);
122 r
->accessed
= current
;
126 /* Third, make sure that we don't have too many items in the list.
127 Expire old and unused entries after 30 minutes */
128 for (rprev
=NULL
, r
=thecache
; r
; )
130 if (!r
->pw
&& r
->ttl
>= 0 && r
->accessed
+ 60*30 < current
)
134 log_error ("can't remove unused cache entry `%s' due to"
136 r
->key
, r
->lockcount
);
137 r
->accessed
+= 60*10; /* next error message in 10 minutes */
145 log_debug (" removed `%s' (slot not used for 30m)\n", r
->key
);
164 agent_flush_cache (void)
169 log_debug ("agent_flush_cache\n");
171 for (r
=thecache
; r
; r
= r
->next
)
173 if (!r
->lockcount
&& r
->pw
)
176 log_debug (" flushing `%s'\n", r
->key
);
177 release_data (r
->pw
);
181 else if (r
->lockcount
&& r
->pw
)
184 log_debug (" marked `%s' for flushing\n", r
->key
);
193 /* Store DATA of length DATALEN in the cache under KEY and mark it
194 with a maximum lifetime of TTL seconds. If there is already data
195 under this key, it will be replaced. Using a DATA of NULL deletes
196 the entry. A TTL of 0 is replaced by the default TTL and a TTL of
197 -1 set infinite timeout. CACHE_MODE is stored with the cache entry
198 and used to select different timeouts. */
200 agent_put_cache (const char *key
, cache_mode_t cache_mode
,
201 const char *data
, int ttl
)
206 log_debug ("agent_put_cache `%s' requested ttl=%d mode=%d\n",
207 key
, ttl
, cache_mode
);
214 case CACHE_MODE_SSH
: ttl
= opt
.def_cache_ttl_ssh
; break;
215 default: ttl
= opt
.def_cache_ttl
; break;
218 if (!ttl
|| cache_mode
== CACHE_MODE_IGNORE
)
221 for (r
=thecache
; r
; r
= r
->next
)
223 if (!r
->lockcount
&& !strcmp (r
->key
, key
))
230 release_data (r
->pw
);
235 r
->created
= r
->accessed
= gnupg_get_time ();
237 r
->cache_mode
= cache_mode
;
238 r
->pw
= new_data (data
, strlen (data
)+1);
240 log_error ("out of core while allocating new cache item\n");
244 { /* simply insert */
245 r
= xtrycalloc (1, sizeof *r
+ strlen (key
));
247 log_error ("out of core while allocating new cache control\n");
250 strcpy (r
->key
, key
);
251 r
->created
= r
->accessed
= gnupg_get_time ();
253 r
->cache_mode
= cache_mode
;
254 r
->pw
= new_data (data
, strlen (data
)+1);
257 log_error ("out of core while allocating new cache item\n");
271 /* Try to find an item in the cache. Note that we currently don't
272 make use of CACHE_MODE. */
274 agent_get_cache (const char *key
, cache_mode_t cache_mode
, void **cache_id
)
278 if (cache_mode
== CACHE_MODE_IGNORE
)
282 log_debug ("agent_get_cache `%s'...\n", key
);
285 /* first try to find one with no locks - this is an updated cache
286 entry: We might have entries with a lockcount and without a
288 for (r
=thecache
; r
; r
= r
->next
)
290 if (!r
->lockcount
&& r
->pw
&& !strcmp (r
->key
, key
))
292 /* put_cache does only put strings into the cache, so we
293 don't need the lengths */
294 r
->accessed
= gnupg_get_time ();
296 log_debug ("... hit\n");
302 /* again, but this time get even one with a lockcount set */
303 for (r
=thecache
; r
; r
= r
->next
)
305 if (r
->pw
&& !strcmp (r
->key
, key
))
307 r
->accessed
= gnupg_get_time ();
309 log_debug ("... hit (locked)\n");
316 log_debug ("... miss\n");
324 agent_unlock_cache_entry (void **cache_id
)
328 for (r
=thecache
; r
; r
= r
->next
)
333 log_error ("trying to unlock non-locked cache entry `%s'\n",