1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2011 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 gboolean
reset_entry(file_cache_t
*p
)
186 if (p
&& !p
->refcount
) {
191 memset(p
->key
, 0, sizeof(p
->key
));
196 gboolean
cache_clear(const guchar
*md5file
, gint which
)
198 file_cache_t
*p
= md5file
? get_entry(md5file
) : NULL
;
206 send_status_all(STATUS_CACHE
);
211 for (i
= 0, t
= g_slist_length(key_cache
); i
< t
; i
++) {
212 p
= g_slist_nth_data(key_cache
, i
);
218 send_status_all(STATUS_CACHE
);
222 gboolean
cache_iscached(const guchar
*md5file
)
224 file_cache_t
*p
= get_entry(md5file
);
226 return p
? valid_key(p
->key
, sizeof(p
->key
)) : FALSE
;
229 gboolean
cache_reset_timeout(const guchar
*md5file
, glong timeout
)
231 file_cache_t
*p
= get_entry(md5file
);
237 return cache_set_timeout(md5file
, timeout
);
239 return cache_set_timeout(md5file
, p
->reset
);
242 gboolean
cache_set_timeout(const guchar
*md5file
, glong timeout
)
244 file_cache_t
*p
= get_entry(md5file
);
250 p
->timeout
= p
->reset
;
252 p
->reset
= p
->timeout
= timeout
;
257 gboolean
cache_update_key(const guchar
*md5file
, const guchar
*shakey
)
259 file_cache_t
*p
= get_entry(md5file
);
262 return cache_add_file(md5file
, shakey
);
265 memcpy(p
->key
, shakey
, sizeof(p
->key
));
270 gboolean
cache_get_key(const guchar
*md5file
, guchar
*shakey
)
272 file_cache_t
*p
= get_entry(md5file
);
274 if (!p
|| valid_key(p
->key
, sizeof(p
->key
)) == FALSE
)
277 memcpy(shakey
, p
->key
, sizeof(p
->key
));
281 gboolean
cache_has_file(const guchar
*md5file
)
283 file_cache_t
*p
= get_entry(md5file
);
285 return p
? TRUE
: FALSE
;
288 gboolean
cache_decr_refcount(const guchar
*md5file
)
290 file_cache_t
*p
= get_entry(md5file
);
303 gboolean
cache_incr_refcount(const guchar
*md5file
)
305 file_cache_t
*p
= get_entry(md5file
);