chiark / gitweb /
pubkeys: Print file path in some log messages
[secnet.git] / pubkeys.fl.pl
1 #!/usr/bin/perl -w
2 # -*- C -*-
3 #
4 # secnet - pubkeys.fl.pl
5 #
6 # This file is part of secnet.
7 # See README for full list of copyright holders.
8 #
9 # secnet is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13
14 # secnet is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # General Public License for more details.
18
19 # You should have received a copy of the GNU General Public License
20 # version 3 along with secnet; if not, see
21 # https://www.gnu.org/licenses/gpl.html.
22
23 # We process __DATA__ of this file first through the perl code,
24 # and then through flex.  We do it like this because directives
25 # with positional arguments are otherwise rather tedious to specify
26 # in flex.  Of course we could have used bison too but this seems
27 # better overall.
28
29 use strict;
30
31 our $do = '';
32 our $co = '';
33 our $kw;
34 our $kwid;
35 our @next_kw;
36 our $in_s;
37 our $data_off;
38
39 our %subst = (GRPIDSZ => 4, SERIALSZ => 4);
40
41 our $last_lno = -1;
42 sub lineno (;$$) {
43     my ($always, $delta) = @_;
44     my $o = '';
45     $delta //= 0;
46     if ($always || $. != $last_lno+1) {
47         $o .= sprintf "#line %d \"%s\"\n", $delta+$data_off+$., $0;
48     }
49     $last_lno = $.;
50     $o;
51 }
52
53 while (<DATA>) {
54         last if m/^\%\%\s*$/;
55         if (m/^!SUBSTCHECKS\s*$/) {
56                 foreach (keys %subst) {
57                         $do .= <<END
58 #if $_ != $subst{$_}
59 # error $_ value disagrees between pubkeys.fl.pl and C headers
60 #endif
61 END
62                 }
63                 next;
64         }
65         $do .= lineno();
66         $do .= $_;
67 }
68
69 sub inst ($) {
70         $do .= "%x $_[0]\n";
71         "<$_[0]>";
72 }
73
74 while (<DATA>) {
75     s#\{!2(\w+)\}# '{'.(2 * ($subst{$1}//die "$1 ?")).'}' #ge;
76     if (m/^!(KEYWORD|KWALIAS) ([-0-9a-z]+)(\s*\{.*\})?$/) {
77         my $kwt=$2;
78         if ($1 eq 'KEYWORD') {
79             die if $kw;
80             $kw = $kwt;
81         } else {
82             die if @next_kw;
83             die unless $kw;
84         }
85         my $xact = $3 // '';
86         $kwid = $kw; $kwid =~ y/-/_/;
87         $in_s = "HK_${kwid}";
88         $co .= "{L}$kwt { BEGIN($in_s); $xact }\n";
89         next;
90     }
91     if (m/^!ARG (\w+) (\S.*\S) \{\s*$/) {
92         die unless $kw;
93         die if @next_kw;
94         $co .= inst("$in_s")."{S} { BEGIN(D_${kwid}_$1); }\n";
95         $co .= inst("D_${kwid}_$1")."$2 {\n";
96         $in_s = "HA_${kwid}_$1";
97         $co .= "\tBEGIN($in_s);\n";
98         @next_kw = ($kw);
99         $co .= lineno(1,1);
100         next;
101     }
102     if (m/^!\}\s*$/) {
103         die unless @next_kw;
104         $co .= lineno(1,0);
105         $co .= "}\n";
106         $kw = shift @next_kw;
107         next;
108     }
109     if (m/^!FINAL \{\s*$/) {
110         die unless $kw;
111         die if @next_kw;
112         $co .= inst("FIN_$kwid")."\\n { BEGIN(0); c->lno++; }\n";
113         $co .= inst("$in_s")."{L}/\\n {\n";
114         $co .= "\tBEGIN(FIN_$kwid);\n";
115         $co .= lineno(1,1);
116         @next_kw = (undef);
117         next;
118     }
119     if (m/^!/) {
120         die;
121     }
122     $co .= $_;
123     if (m/^\%\%\s*$/) {
124         $co .= lineno(1,1);
125     }
126 }
127
128 print $do, "%%\n", $co or die $!;
129
130 BEGIN { $data_off = __LINE__ + 1; }
131 __DATA__
132
133 L       [ \t]*
134 S       [ \t]+
135 BASE91S []-~!#-&(-[]+
136 %x SKIPNL
137
138 %option yylineno
139 %option noyywrap
140 %option batch
141 %option 8bit
142 %option nodefault
143 %option never-interactive
144
145 %option prefix="pkyy"
146
147 %option warn
148
149 %{
150
151 #include "secnet.h"
152 #include "pubkeys.h"
153 #include "util.h"
154 #include "unaligned.h"
155 #include "base91s/base91.h"
156
157 !SUBSTCHECKS
158
159 struct pubkeyset_context {
160     /* filled in during setup: */
161     const char *path;
162     struct log_if *log;
163     struct buffer_if *data_buf;
164     struct peer_keyset *building;
165     /* runtime: */
166     bool_t had_serial;
167     int lno;
168     bool_t fallback_skip;
169     const struct sigscheme_info *scheme;
170     uint8_t grpid[GRPIDSZ];
171     serialt serial;
172 };
173
174 static struct pubkeyset_context c[1];
175
176 #define LI (c->log)
177 #define HEX2BIN(v,l) ({                                                 \
178         int32_t outlen;                                                 \
179         bool_t ok=hex_decode((v), ((l)), &outlen, yytext, False);       \
180         assert(ok);                                                     \
181         assert(outlen==((l)));                                          \
182     })
183 #define HEX2BIN_ARRAY(v) HEX2BIN((v),sizeof((v)))
184 #define DOSKIPQ ({                                      \
185         BEGIN(SKIPNL);                                  \
186         break;                                          \
187     })
188 #define DOSKIP(m) ({                                    \
189         slilog(LI,M_INFO,"%s:%d: " m, c->path, c->lno); \
190         DOSKIPQ;                                        \
191     })
192 #define FAIL(m) do{                                     \
193         slilog(LI,M_ERR,"%s:%d: " m, c->path, c->lno);  \
194         return -1;                                      \
195     }while(0)
196
197 %}
198
199 %%
200
201 !KEYWORD pkg  { c->fallback_skip=0; }
202 !KWALIAS pkgf { c->fallback_skip=!!c->building->nkeys; }
203 !ARG id [0-9a-f]{!2GRPIDSZ} {
204     HEX2BIN_ARRAY(c->grpid);
205 !}
206 !FINAL {
207 !}
208 !KEYWORD pub
209 !ARG algo [-0-9a-z]+ {
210     if (c->fallback_skip) DOSKIP("fallback not needed");
211     c->scheme = sigscheme_lookup(yytext);
212     if (!c->scheme) DOSKIP("unknown pk algorithm");
213 !}
214 !ARG data {BASE91S} {
215     /* baseE91 and thus base91s can sometimes store 14 bits per
216      * character pair, so the max decode ratio is 14/16. */
217     size_t maxl = base91s_decode_maxlen(yyleng);
218     buffer_init(c->data_buf,0);
219     if (buf_remaining_space(c->data_buf) < maxl) DOSKIP("pk data too long");
220     struct base91s b91;
221     base91s_init(&b91);
222     size_t l = base91s_decode(&b91, yytext, yyleng, c->data_buf->start);
223     l += base91s_decode_end(&b91, c->data_buf->start + l);
224     assert(l <= maxl);
225     buf_append(c->data_buf,l);
226 !}
227 !FINAL {
228     if (c->building->nkeys >= MAX_SIG_KEYS) DOSKIP("too many public keys");
229     struct sigpubkey_if *pubkey;
230     bool_t ok=c->scheme->loadpub(c->scheme,c->data_buf,
231                                  &pubkey,c->log);
232     if (!ok) break;
233     memcpy(c->building->keys[c->building->nkeys].id.b,
234            c->grpid,
235            GRPIDSZ);
236     assert(ALGIDSZ==1); /* otherwise need htons or htonl or something */
237     c->building->keys[c->building->nkeys].id.b[GRPIDSZ]=
238       c->scheme->algid;
239     c->building->keys[c->building->nkeys++].pubkey=pubkey;
240 !}
241
242 !KEYWORD serial
243 !ARG id [0-9a-f]{!2SERIALSZ} {
244     if (c->had_serial) FAIL("`serial' repeated");
245     c->had_serial = 1;
246     uint8_t sb[SERIALSZ];
247     HEX2BIN_ARRAY(sb);
248     c->serial=get_uint32(sb);
249 !}
250 !FINAL {
251 !}
252
253 {L}[-0-9a-z]+ {
254     DOSKIP("unknown directive");
255 }
256
257 {L}\# {
258     BEGIN(SKIPNL);
259 }
260 {L}\n {
261     c->lno++;
262 }
263
264 <SKIPNL>.*\n {
265     c->lno++;
266     BEGIN(0);
267 }
268
269 <INITIAL><<EOF>>        { return 0; }
270
271 <*>. { FAIL("syntax error"); }
272 <*>\n { FAIL("syntax error - unexpected newline"); }
273 <<EOF>> { FAIL("syntax error - unexpected eof"); }
274
275 %%
276
277 extern struct peer_keyset *
278 keyset_load(const char *path, struct buffer_if *data_buf,
279             struct log_if *log, int logcl_enoent) {
280     assert(!c->building);
281     c->log=log;
282     c->path=path;
283     pkyyin = fopen(path, "r");
284     if (!pkyyin) {
285         slilog(LI,
286                errno==ENOENT ? logcl_enoent : M_ERR,
287                "could not open keyset file %s: %s",
288                path,strerror(errno));
289         goto err;
290     }
291
292     pkyyrestart(pkyyin);
293     BEGIN(0);
294     c->data_buf=data_buf;
295     NEW(c->building);
296     c->building->nkeys=0;
297     c->building->refcount=1;
298     c->fallback_skip=0;
299     c->had_serial=0;
300     c->lno=1;
301     FILLZERO(c->grpid);
302     FILLZERO(c->serial);
303     int r=pkyylex();
304     if (r) goto err_bad;
305
306     if (!c->had_serial) {
307         slilog(LI,M_ERR,"missing serial number in %s",path);
308         goto err_bad;
309     }
310     if (!c->building->nkeys) {
311         slilog(LI,M_ERR,"no useable keys in %s",path);
312         goto err_bad;
313     }
314     fclose(pkyyin);
315     struct peer_keyset *built=c->building;
316     c->building=0;
317     return built;
318
319  err_bad:
320     errno=EBADMSG;
321  err:
322     if (c->building) { free(c->building); c->building=0; }
323     if (pkyyin) { fclose(pkyyin); pkyyin=0; }
324     return 0;
325 }
326