1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2010 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
31 #include "pwmd_error.h"
33 extern void log_write(const gchar
*fmt
, ...);
35 static gboolean
valid_key(const guchar
*key
, gsize len
)
39 for (b
= 0; b
< len
; b
++) {
47 static file_cache_t
*get_entry(const guchar
*md5file
)
49 gint t
= g_slist_length(key_cache
);
55 for (i
= 0; i
< t
; i
++) {
56 file_cache_t
*p
= g_slist_nth_data(key_cache
, i
);
58 if (!memcmp(p
->filename
, md5file
, sizeof(p
->filename
)))
65 gboolean
cache_get_mutex(const guchar
*md5file
, pth_mutex_t
**result
)
67 file_cache_t
*p
= get_entry(md5file
);
72 return p
? TRUE
: FALSE
;
75 gint
cache_file_count()
77 gint t
= g_slist_length(key_cache
);
81 for (i
= 0; i
< t
; i
++) {
82 file_cache_t
*p
= g_slist_nth_data(key_cache
, i
);
84 if (valid_key(p
->key
, sizeof(p
->key
)) == TRUE
)
91 void cache_adjust_timer()
93 gint t
= g_slist_length(key_cache
);
96 for (i
= 0; i
< t
; i
++) {
97 file_cache_t
*p
= g_slist_nth_data(key_cache
, i
);
103 if (p
->timeout
== 0) {
104 if (valid_key(p
->key
, sizeof(p
->key
)) == TRUE
) {
105 memset(&p
->key
, 0, sizeof(p
->key
));
106 send_status_all(STATUS_CACHE
);
115 gboolean
cache_add_file(const guchar
*md5file
, const guchar
*shakey
)
117 file_cache_t
*p
= g_malloc0(sizeof(file_cache_t
));
121 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, pwmd_strerror(GPG_ERR_ENOMEM
));
125 p
->mutex
= g_malloc(sizeof(pth_mutex_t
));
128 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, pwmd_strerror(GPG_ERR_ENOMEM
));
133 pth_mutex_init(p
->mutex
);
134 memcpy(p
->filename
, md5file
, sizeof(p
->filename
));
137 memcpy(p
->key
, shakey
, sizeof(p
->key
));
139 p
->reset
= p
->timeout
= -2;
141 if (mlock(p
, sysconf(_SC_PAGESIZE
)))
142 log_write("mlock(): %s", pwmd_strerror(gpg_error_from_syserror()));
144 new = g_slist_append(key_cache
, p
);
147 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, pwmd_strerror(GPG_ERR_ENOMEM
));
149 memset(p
, 0, sizeof(file_cache_t
));
158 static void free_entry(file_cache_t
*p
)
166 memset(p
, 0, sizeof(file_cache_t
));
167 munlock(p
, sysconf(_SC_PAGESIZE
));
168 key_cache
= g_slist_remove(key_cache
, p
);
175 file_cache_t
*p
= g_slist_nth_data(key_cache
, 0);
184 static void reset_entry(file_cache_t
*p
)
186 if (p
&& !p
->refcount
)
189 memset(p
->key
, 0, sizeof(p
->key
));
192 gboolean
cache_clear(const guchar
*md5file
, gint which
)
194 file_cache_t
*p
= md5file
? get_entry(md5file
) : NULL
;
201 send_status_all(STATUS_CACHE
);
206 file_cache_t
*p
= g_slist_nth_data(key_cache
, 0);
212 } while (cache_file_count());
214 send_status_all(STATUS_CACHE
);
218 gboolean
cache_iscached(const guchar
*md5file
)
220 file_cache_t
*p
= get_entry(md5file
);
222 return p
? valid_key(p
->key
, sizeof(p
->key
)) : FALSE
;
225 gboolean
cache_reset_timeout(const guchar
*md5file
, glong timeout
)
227 file_cache_t
*p
= get_entry(md5file
);
233 return cache_set_timeout(md5file
, timeout
);
235 return cache_set_timeout(md5file
, p
->reset
);
238 gboolean
cache_set_timeout(const guchar
*md5file
, glong timeout
)
240 file_cache_t
*p
= get_entry(md5file
);
246 p
->timeout
= p
->reset
;
248 p
->reset
= p
->timeout
= timeout
;
253 gboolean
cache_update_key(const guchar
*md5file
, const guchar
*shakey
)
255 file_cache_t
*p
= get_entry(md5file
);
258 return cache_add_file(md5file
, shakey
);
261 memcpy(p
->key
, shakey
, sizeof(p
->key
));
266 gboolean
cache_get_key(const guchar
*md5file
, guchar
*shakey
)
268 file_cache_t
*p
= get_entry(md5file
);
270 if (!p
|| valid_key(p
->key
, sizeof(p
->key
)) == FALSE
)
273 memcpy(shakey
, p
->key
, sizeof(p
->key
));
277 gboolean
cache_has_file(const guchar
*md5file
)
279 file_cache_t
*p
= get_entry(md5file
);
281 return p
? TRUE
: FALSE
;
284 gboolean
cache_decr_refcount(const guchar
*md5file
)
286 file_cache_t
*p
= get_entry(md5file
);
299 gboolean
cache_incr_refcount(const guchar
*md5file
)
301 file_cache_t
*p
= get_entry(md5file
);