2 * This file is part of secnet.
3 * See README for full list of copyright holders.
5 * secnet is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * secnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * version 3 along with secnet; if not, see
17 * https://www.gnu.org/licenses/gpl.html.
23 #define DEFAULT_SIZE 5
27 struct sigprivkey_if *sigpriv; /* 0 means none such */
32 struct privcache_if ops;
34 struct pathprefix_template path;
36 struct buffer_if databuf;
37 struct hash_if *defhash;
40 static struct sigprivkey_if *uncached_get(struct privcache *st,
41 const struct sigkeyid *id, struct log_if *log)
46 sprintf(st->path.write_here, SIGKEYID_PR_FMT, SIGKEYID_PR_VAL(id));
48 const char *path=st->path.buffer;
49 struct hash_if *defhash=st->defhash;
50 struct buffer_if *databuf=&st->databuf;
54 if (errno == ENOENT) {
55 slilog(log,M_DEBUG,"private key %s not found",
58 slilog(log,M_ERR,"failed to open private key file %s",
65 buffer_init(databuf,0);
66 ssize_t got=fread(databuf->base,1,databuf->alloclen,f);
68 slilog(log,M_ERR,"failed to read private-key file %s",
73 slilog(log,M_ERR,"private key file %s longer than max %d",
74 path, (int)databuf->alloclen);
79 struct sigprivkey_if *sigpriv=0;
80 const struct sigscheme_info *scheme;
81 for (scheme=sigschemes;
84 if (scheme->algid == id->b[GRPIDSZ])
87 slilog(log,M_ERR,"private key file %s not loaded (unknown algid)",
92 databuf->start=databuf->base;
94 struct cloc loc = { .file=path, .line=0 };
95 ok=scheme->loadpriv(scheme, databuf, &sigpriv, log, loc);
96 if (!ok) goto out; /* loadpriv will have logged */
98 if (sigpriv->sethash) {
101 "private key %s requires `hash' config key for privcache to load",
103 sigpriv->dispose(sigpriv->st);
107 sigpriv->sethash(sigpriv->st,defhash);
112 return ok ? sigpriv : 0;
115 static struct sigprivkey_if *privcache_lookup(void *sst,
116 const struct sigkeyid *id,
117 struct log_if *log) {
118 struct privcache *st = sst;
122 for (was=0; was<st->used; was++) {
123 if (sigkeyid_equal(id, &st->ents[was].id)) {
124 result = st->ents[was];
129 if (st->used < st->alloc) {
134 if (st->ents[was].sigpriv) {
135 st->ents[was].sigpriv->dispose(st->ents[was].sigpriv->st);
139 COPY_OBJ(result.id, *id);
140 result.sigpriv=uncached_get(st,id,log);
143 memmove(&st->ents[1], &st->ents[0], sizeof(st->ents[0]) * was);
144 st->ents[0] = result;
145 return result.sigpriv;
148 static list_t *privcache_apply(closure_t *self, struct cloc loc,
149 dict_t *context, list_t *args)
151 struct privcache *st;
156 st->cl.description="privcache";
157 st->cl.type=CL_PRIVCACHE;
159 st->cl.interface=&st->ops;
161 st->ops.lookup=privcache_lookup;
164 st->used=st->alloc=0;
167 item=list_elem(args,0);
168 if (!item || item->type!=t_dict)
169 cfgfatal(loc,"privcache","parameter must be a dictionary\n");
171 dict=item->data.dict;
173 st->alloc=dict_read_number(dict,"privcache-size",False,"privcache",loc,
175 NEW_ARY(st->ents,st->alloc);
178 int32_t buflen=dict_read_number(dict,"privkey-max",False,"privcache",loc,
180 buffer_new(&st->databuf,buflen+1);
182 const char *path=dict_read_string(dict,"privkeys",True,"privcache",loc);
183 pathprefix_template_init(&st->path,path,KEYIDSZ*2);
185 st->defhash=find_cl_if(dict,"hash",CL_HASH,False,"site",loc);
187 return new_closure(&st->cl);
190 void privcache_module(dict_t *dict)
192 add_closure(dict,"priv-cache",privcache_apply);