chiark / gitweb /
a5a67a520d36301bbc27d57485d14458de41987e
[secnet.git] / privcache.c
1 /*
2  * This file is part of secnet.
3  * See README for full list of copyright holders.
4  *
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.
9  * 
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.
14  * 
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.
18  */
19
20 #include "secnet.h"
21 #include "util.h"
22
23 #define DEFAULT_SIZE 5
24
25 struct ent {
26     struct sigkeyid id;
27     struct sigprivkey_if *sigpriv; /* 0 means none such */
28 };
29
30 struct privcache {
31     closure_t cl;
32     struct privcache_if ops;
33     int used, alloc;
34     struct pathprefix_template path;
35     struct ent *ents;
36     struct buffer_if databuf;
37     struct hash_if *defhash;
38 };
39
40 static struct sigprivkey_if *uncached_get(struct privcache *st,
41                            const struct sigkeyid *id, struct log_if *log)
42 {
43     bool_t ok=False;
44     FILE *f=0;
45
46     sprintf(st->path.write_here, SIGKEYID_PR_FMT, SIGKEYID_PR_VAL(id));
47
48     const char *path=st->path.buffer;
49     struct hash_if *defhash=st->defhash;
50
51     f = fopen(path,"rb");
52     if (!f) {
53         if (errno == ENOENT) {
54             slilog(log,M_DEBUG,"private key %s not found",
55                    path);
56         } else {
57             slilog(log,M_ERR,"failed to open private key file %s",
58                    path);
59         }
60         goto out;
61     }
62
63     setbuf(f,0);
64     buffer_init(&st->databuf,0);
65     ssize_t got=fread(st->databuf.base,1,st->databuf.alloclen,f);
66     if (ferror(f)) {
67         slilog(log,M_ERR,"failed to read private-key file %s",
68                path);
69         goto out;
70     }
71     if (!feof(f)) {
72         slilog(log,M_ERR,"private key file %s longer than max %d",
73                path, (int)st->databuf.alloclen);
74         goto out;
75     }
76     fclose(f); f=0;
77
78     struct sigprivkey_if *sigpriv=0;
79     const struct sigscheme_info *scheme;
80     for (scheme=sigschemes;
81          scheme->name;
82          scheme++)
83         if (scheme->algid == id->b[GRPIDSZ])
84             goto found;
85
86     slilog(log,M_ERR,"private key file %s not loaded (unknown algid)",
87            path);
88     goto out;
89
90  found:
91     st->databuf.start=st->databuf.base;
92     st->databuf.size=got;
93     struct cloc loc = { .file=path, .line=0 };
94     ok=scheme->loadpriv(scheme, &st->databuf, &sigpriv, log, loc);
95     if (!ok) goto out; /* loadpriv will have logged */
96
97     if (sigpriv->sethash) {
98         if (!defhash) {
99             slilog(log,M_ERR,
100  "private key %s requires `hash' config key for privcache to load",
101                    path);
102             sigpriv->dispose(sigpriv->st);
103             sigpriv=0;
104             goto out;
105         }
106         sigpriv->sethash(sigpriv->st,defhash);
107     }
108
109   out:
110     if (f) fclose(f);
111     return ok ? sigpriv : 0;
112 }
113
114 static struct sigprivkey_if *privcache_lookup(void *sst,
115                                               const struct sigkeyid *id,
116                                               struct log_if *log) {
117     struct privcache *st = sst;
118     int was;
119     struct ent result;
120
121     for (was=0; was<st->used; was++) {
122         if (sigkeyid_equal(id, &st->ents[was].id)) {
123             result = st->ents[was];
124             goto found;
125         }
126     }
127
128     if (st->used < st->alloc) {
129         was = st->used;
130         st->used++;
131     } else {
132         was = st->used-1;
133         if (st->ents[was].sigpriv) {
134             st->ents[was].sigpriv->dispose(st->ents[was].sigpriv->st);
135         }
136     }
137
138     COPY_OBJ(result.id, *id);
139     result.sigpriv=uncached_get(st,id,log);
140
141  found:
142     memmove(&st->ents[1], &st->ents[0], sizeof(st->ents[0]) * was);
143     st->ents[0] = result;
144     return result.sigpriv;
145 }
146
147 static list_t *privcache_apply(closure_t *self, struct cloc loc,
148                                dict_t *context, list_t *args)
149 {
150     struct privcache *st;
151     item_t *item;
152     dict_t *dict;
153
154     NEW(st);
155     st->cl.description="privcache";
156     st->cl.type=CL_PRIVCACHE;
157     st->cl.apply=NULL;
158     st->cl.interface=&st->ops;
159     st->ops.st=st;
160     st->ops.lookup=privcache_lookup;
161     st->ents=0;
162     st->path.buffer=0;
163     st->used=st->alloc=0;
164     st->defhash=0;
165
166     item=list_elem(args,0);
167     if (!item || item->type!=t_dict)
168         cfgfatal(loc,"privcache","parameter must be a dictionary\n");
169     
170     dict=item->data.dict;
171
172     st->alloc=dict_read_number(dict,"privcache-size",False,"privcache",loc,
173                                DEFAULT_SIZE);
174     NEW_ARY(st->ents,st->alloc);
175     st->used=0;
176
177     int32_t buflen=dict_read_number(dict,"privkey-max",False,"privcache",loc,
178                                     4095);
179     buffer_new(&st->databuf,buflen+1);
180
181     const char *path=dict_read_string(dict,"privkeys",True,"privcache",loc);
182     pathprefix_template_init(&st->path,path,KEYIDSZ*2);
183
184     st->defhash=find_cl_if(dict,"hash",CL_HASH,False,"site",loc);
185
186     return new_closure(&st->cl);
187 }
188
189 void privcache_module(dict_t *dict)
190 {
191     add_closure(dict,"priv-cache",privcache_apply);
192 }