Blame
Date:
Sat Mar 19 11:02:42 2022 UTC
Message:
const-ify some tables

matches found with

% grep -R '=[ ]*{' . | fgrep -v const
001
2021-05-09
op
/*
002
2021-05-09
op
* Copyright (c) 2021 Omar Polo <op@omarpolo.com>
003
2021-05-09
op
*
004
2021-05-09
op
* Permission to use, copy, modify, and distribute this software for any
005
2021-05-09
op
* purpose with or without fee is hereby granted, provided that the above
006
2021-05-09
op
* copyright notice and this permission notice appear in all copies.
007
2021-05-09
op
*
008
2021-05-09
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
009
2021-05-09
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
010
2021-05-09
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
011
2021-05-09
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
012
2021-05-09
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
013
2021-05-09
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
014
2021-05-09
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
015
2021-05-09
op
*/
016
2021-05-09
op
017
2021-05-09
op
#include "gmid.h"
018
2021-05-09
op
019
2021-05-09
op
#include <assert.h>
020
2021-05-09
op
#include <errno.h>
021
2021-05-09
op
#include <string.h>
022
2021-05-09
op
023
2021-05-09
op
/*
024
2021-05-09
op
* Sometimes it can be useful to inspect the fastcgi traffic as
025
2021-05-09
op
* received by gmid.
026
2021-05-09
op
*
027
2021-05-09
op
* This will make gmid connect to a `debug.sock' socket (that must
028
2021-05-09
op
* exists) in the current directory and send there a copy of what gets
029
2021-05-09
op
* read. The socket can be created and monitored e.g. with
030
2021-05-09
op
*
031
2021-05-09
op
* rm -f debug.sock ; nc -Ulk ./debug.sock | hexdump -C
032
2021-05-09
op
*
033
2021-05-09
op
* NB: the sandbox must be disabled for this to work.
034
2021-05-09
op
*/
035
2021-05-09
op
#define DEBUG_FCGI 0
036
2021-05-09
op
037
2021-06-11
op
#if DEBUG_FCGI
038
2021-05-09
op
# include <sys/un.h>
039
2021-05-09
op
static int debug_socket = -1;
040
2021-05-09
op
#endif
041
2021-05-09
op
042
2021-05-09
op
struct fcgi_header {
043
2021-05-09
op
unsigned char version;
044
2021-05-09
op
unsigned char type;
045
2021-05-09
op
unsigned char req_id1;
046
2021-05-09
op
unsigned char req_id0;
047
2021-05-09
op
unsigned char content_len1;
048
2021-05-09
op
unsigned char content_len0;
049
2021-05-09
op
unsigned char padding;
050
2021-05-09
op
unsigned char reserved;
051
2021-05-09
op
};
052
2021-05-09
op
053
2021-05-09
op
/*
054
2021-05-09
op
* number of bytes in a FCGI_HEADER. Future version of the protocol
055
2021-05-09
op
* will not reduce this number.
056
2021-05-09
op
*/
057
2021-05-09
op
#define FCGI_HEADER_LEN 8
058
2021-05-09
op
059
2021-05-09
op
/*
060
2021-05-09
op
* values for the version component
061
2021-05-09
op
*/
062
2021-05-09
op
#define FCGI_VERSION_1 1
063
2021-05-09
op
064
2021-05-09
op
/*
065
2021-05-09
op
* values for the type component
066
2021-05-09
op
*/
067
2021-05-09
op
#define FCGI_BEGIN_REQUEST 1
068
2021-05-09
op
#define FCGI_ABORT_REQUEST 2
069
2021-05-09
op
#define FCGI_END_REQUEST 3
070
2021-05-09
op
#define FCGI_PARAMS 4
071
2021-05-09
op
#define FCGI_STDIN 5
072
2021-05-09
op
#define FCGI_STDOUT 6
073
2021-05-09
op
#define FCGI_STDERR 7
074
2021-05-09
op
#define FCGI_DATA 8
075
2021-05-09
op
#define FCGI_GET_VALUES 9
076
2021-05-09
op
#define FCGI_GET_VALUES_RESULT 10
077
2021-05-09
op
#define FCGI_UNKNOWN_TYPE 11
078
2021-05-09
op
#define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
079
2021-05-09
op
080
2021-05-09
op
struct fcgi_begin_req {
081
2021-05-09
op
unsigned char role1;
082
2021-05-09
op
unsigned char role0;
083
2021-05-09
op
unsigned char flags;
084
2021-05-09
op
unsigned char reserved[5];
085
2021-05-09
op
};
086
2021-05-09
op
087
2021-05-09
op
struct fcgi_begin_req_record {
088
2021-05-09
op
struct fcgi_header header;
089
2021-05-09
op
struct fcgi_begin_req body;
090
2021-05-09
op
};
091
2021-05-09
op
092
2021-05-09
op
/*
093
2021-05-09
op
* mask for flags;
094
2021-05-09
op
*/
095
2021-05-09
op
#define FCGI_KEEP_CONN 1
096
2021-05-09
op
097
2021-05-09
op
/*
098
2021-05-09
op
* values for the role
099
2021-05-09
op
*/
100
2021-05-09
op
#define FCGI_RESPONDER 1
101
2021-05-09
op
#define FCGI_AUTHORIZER 2
102
2021-05-09
op
#define FCGI_FILTER 3
103
2021-05-09
op
104
2021-05-09
op
struct fcgi_end_req_body {
105
2021-05-09
op
unsigned char app_status3;
106
2021-05-09
op
unsigned char app_status2;
107
2021-05-09
op
unsigned char app_status1;
108
2021-05-09
op
unsigned char app_status0;
109
2021-05-09
op
unsigned char proto_status;
110
2021-05-09
op
unsigned char reserved[3];
111
2021-05-09
op
};
112
2021-05-09
op
113
2021-05-09
op
/*
114
2021-05-09
op
* values for proto_status
115
2021-05-09
op
*/
116
2021-05-09
op
#define FCGI_REQUEST_COMPLETE 0
117
2021-05-09
op
#define FCGI_CANT_MPX_CONN 1
118
2021-05-09
op
#define FCGI_OVERLOADED 2
119
2021-05-09
op
#define FCGI_UNKNOWN_ROLE 3
120
2021-05-09
op
121
2021-05-09
op
/*
122
2021-05-09
op
* Variable names for FCGI_GET_VALUES / FCGI_GET_VALUES_RESULT
123
2021-05-09
op
* records.
124
2021-05-09
op
*/
125
2021-05-09
op
#define FCGI_MAX_CONNS "FCGI_MAX_CONNS"
126
2021-05-09
op
#define FCGI_MAX_REQS "FCGI_MAX_REQS"
127
2021-05-09
op
#define FCGI_MPXS_CONNS "FCGI_MPXS_CONNS"
128
2021-05-09
op
129
2021-05-09
op
static int
130
2021-10-07
op
prepare_header(struct fcgi_header *h, int type, size_t size,
131
2021-05-09
op
size_t padding)
132
2021-05-09
op
{
133
2021-10-07
op
int id = 1;
134
2021-10-07
op
135
2021-05-09
op
memset(h, 0, sizeof(*h));
136
2021-05-09
op
137
2021-05-09
op
h->version = FCGI_VERSION_1;
138
2021-10-18
op
h->type = type;
139
2021-05-09
op
h->req_id1 = (id >> 8);
140
2021-05-09
op
h->req_id0 = (id & 0xFF);
141
2021-05-09
op
h->content_len1 = (size >> 8);
142
2021-05-09
op
h->content_len0 = (size & 0xFF);
143
2021-05-09
op
h->padding = padding;
144
2021-05-09
op
145
2021-05-09
op
return 0;
146
2021-05-09
op
}
147
2021-05-09
op
148
2021-05-09
op
static int
149
2021-10-07
op
fcgi_begin_request(struct bufferevent *bev)
150
2021-05-09
op
{
151
2021-05-09
op
struct fcgi_begin_req_record r;
152
2021-05-09
op
153
2021-05-09
op
memset(&r, 0, sizeof(r));
154
2021-10-07
op
prepare_header(&r.header, FCGI_BEGIN_REQUEST, sizeof(r.body), 0);
155
2021-05-09
op
assert(sizeof(r.body) == FCGI_HEADER_LEN);
156
2021-05-09
op
157
2021-05-09
op
r.body.role1 = 0;
158
2021-05-09
op
r.body.role0 = FCGI_RESPONDER;
159
2021-05-09
op
r.body.flags = FCGI_KEEP_CONN;
160
2021-05-09
op
161
2021-09-26
op
if (bufferevent_write(bev, &r, sizeof(r)) == -1)
162
2021-05-09
op
return -1;
163
2021-05-09
op
return 0;
164
2021-05-09
op
}
165
2021-05-09
op
166
2021-05-09
op
static int
167
2021-10-07
op
fcgi_send_param(struct bufferevent *bev, const char *name,
168
2021-09-26
op
const char *value)
169
2021-05-09
op
{
170
2021-05-09
op
struct fcgi_header h;
171
2021-10-18
op
uint32_t namlen, vallen, padlen;
172
2021-05-09
op
uint8_t s[8];
173
2021-05-09
op
size_t size;
174
2022-03-19
op
const char padding[8] = { 0 };
175
2021-05-09
op
176
2021-05-09
op
namlen = strlen(name);
177
2021-05-09
op
vallen = strlen(value);
178
2021-05-09
op
size = namlen + vallen + 8; /* 4 for the sizes */
179
2021-05-09
op
padlen = (8 - (size & 0x7)) & 0x7;
180
2021-05-09
op
181
2021-05-09
op
s[0] = ( namlen >> 24) | 0x80;
182
2021-05-09
op
s[1] = ((namlen >> 16) & 0xFF);
183
2021-05-09
op
s[2] = ((namlen >> 8) & 0xFF);
184
2021-05-09
op
s[3] = ( namlen & 0xFF);
185
2021-05-09
op
186
2021-05-09
op
s[4] = ( vallen >> 24) | 0x80;
187
2021-05-09
op
s[5] = ((vallen >> 16) & 0xFF);
188
2021-05-09
op
s[6] = ((vallen >> 8) & 0xFF);
189
2021-05-09
op
s[7] = ( vallen & 0xFF);
190
2021-05-09
op
191
2021-10-07
op
prepare_header(&h, FCGI_PARAMS, size, padlen);
192
2021-05-09
op
193
2021-09-26
op
if (bufferevent_write(bev, &h, sizeof(h)) == -1 ||
194
2021-09-26
op
bufferevent_write(bev, s, sizeof(s)) == -1 ||
195
2021-09-26
op
bufferevent_write(bev, name, namlen) == -1 ||
196
2021-09-26
op
bufferevent_write(bev, value, vallen) == -1 ||
197
2021-09-26
op
bufferevent_write(bev, padding, padlen) == -1)
198
2021-05-09
op
return -1;
199
2021-05-09
op
200
2021-05-09
op
return 0;
201
2021-05-09
op
}
202
2021-05-09
op
203
2021-05-09
op
static int
204
2021-10-07
op
fcgi_end_param(struct bufferevent *bev)
205
2021-05-09
op
{
206
2021-05-09
op
struct fcgi_header h;
207
2021-05-09
op
208
2021-10-07
op
prepare_header(&h, FCGI_PARAMS, 0, 0);
209
2021-09-26
op
if (bufferevent_write(bev, &h, sizeof(h)) == -1)
210
2021-05-09
op
return -1;
211
2021-05-09
op
212
2021-10-07
op
prepare_header(&h, FCGI_STDIN, 0, 0);
213
2021-09-26
op
if (bufferevent_write(bev, &h, sizeof(h)) == -1)
214
2021-05-09
op
return -1;
215
2021-05-09
op
216
2021-05-09
op
return 0;
217
2021-05-09
op
}
218
2021-05-09
op
219
2021-05-09
op
static inline int
220
2021-05-09
op
recid(struct fcgi_header *h)
221
2021-05-09
op
{
222
2021-10-07
op
return h->req_id0 + (h->req_id1 << 8);
223
2021-05-09
op
}
224
2021-05-09
op
225
2021-05-09
op
static inline int
226
2021-05-09
op
reclen(struct fcgi_header *h)
227
2021-05-09
op
{
228
2021-05-09
op
return h->content_len0 + (h->content_len1 << 8);
229
2021-05-09
op
}
230
2021-05-09
op
231
2021-07-06
op
void
232
2021-09-26
op
fcgi_read(struct bufferevent *bev, void *d)
233
2021-05-09
op
{
234
2021-10-07
op
struct client *c = d;
235
2021-09-26
op
struct evbuffer *src = EVBUFFER_INPUT(bev);
236
2021-09-26
op
struct fcgi_header hdr;
237
2021-05-09
op
struct fcgi_end_req_body end;
238
2021-05-09
op
size_t len;
239
2021-05-09
op
240
2021-09-26
op
#if DEBUG_FCGI
241
2021-09-26
op
if (debug_socket == -1) {
242
2021-09-26
op
struct sockaddr_un addr;
243
2021-05-09
op
244
2021-09-26
op
if ((debug_socket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
245
2021-09-26
op
err(1, "socket");
246
2021-05-09
op
247
2021-09-26
op
memset(&addr, 0, sizeof(addr));
248
2021-09-26
op
addr.sun_family = AF_UNIX;
249
2021-09-26
op
strlcpy(addr.sun_path, "./debug.sock", sizeof(addr.sun_path));
250
2021-09-26
op
if (connect(debug_socket, (struct sockaddr*)&addr, sizeof(addr))
251
2021-09-26
op
== -1)
252
2021-09-26
op
err(1, "connect");
253
2021-09-26
op
}
254
2021-09-26
op
#endif
255
2021-05-09
op
256
2021-09-26
op
for (;;) {
257
2021-09-26
op
if (EVBUFFER_LENGTH(src) < sizeof(hdr))
258
2021-09-26
op
return;
259
2021-07-06
op
260
2021-09-26
op
memcpy(&hdr, EVBUFFER_DATA(src), sizeof(hdr));
261
2021-05-09
op
262
2021-10-07
op
if (recid(&hdr) != 1) {
263
2021-09-26
op
log_err(NULL,
264
2021-10-07
op
"got invalid client id %d from fcgi backend",
265
2021-10-07
op
recid(&hdr));
266
2021-05-09
op
goto err;
267
2021-09-26
op
}
268
2021-09-26
op
269
2021-09-26
op
len = reclen(&hdr);
270
2021-05-09
op
271
2021-09-26
op
if (EVBUFFER_LENGTH(src) < sizeof(hdr) + len + hdr.padding)
272
2021-09-26
op
return;
273
2021-05-09
op
274
2021-09-26
op
#if DEBUG_FCGI
275
2021-10-02
op
write(debug_socket, EVBUFFER_DATA(src),
276
2021-09-26
op
sizeof(hdr) + len + hdr.padding);
277
2021-09-26
op
#endif
278
2021-09-26
op
279
2021-09-26
op
evbuffer_drain(src, sizeof(hdr));
280
2021-09-26
op
281
2021-09-26
op
switch (hdr.type) {
282
2021-09-26
op
case FCGI_END_REQUEST:
283
2021-09-26
op
if (len != sizeof(end)) {
284
2021-09-26
op
log_err(NULL,
285
2021-09-26
op
"got invalid end request record size");
286
2021-09-26
op
goto err;
287
2021-09-26
op
}
288
2021-09-26
op
bufferevent_read(bev, &end, sizeof(end));
289
2021-09-26
op
290
2021-09-26
op
/* TODO: do something with the status? */
291
2021-10-02
op
c->type = REQUEST_DONE;
292
2021-10-02
op
client_write(c->bev, c);
293
2021-09-26
op
break;
294
2021-09-26
op
295
2021-09-26
op
case FCGI_STDERR:
296
2021-09-26
op
/* discard stderr (for now) */
297
2021-09-26
op
evbuffer_drain(src, len);
298
2021-09-26
op
break;
299
2021-09-26
op
300
2021-09-26
op
case FCGI_STDOUT:
301
2021-10-04
op
bufferevent_write(c->bev, EVBUFFER_DATA(src), len);
302
2021-10-04
op
evbuffer_drain(src, len);
303
2021-09-26
op
break;
304
2021-05-09
op
305
2021-09-26
op
default:
306
2021-09-26
op
log_err(NULL, "got invalid fcgi record (type=%d)",
307
2021-09-26
op
hdr.type);
308
2021-09-26
op
goto err;
309
2021-09-26
op
}
310
2021-05-09
op
311
2021-09-26
op
evbuffer_drain(src, hdr.padding);
312
2021-09-26
op
}
313
2021-07-06
op
314
2021-09-26
op
err:
315
2021-10-07
op
fcgi_error(bev, EVBUFFER_ERROR, c);
316
2021-09-26
op
}
317
2021-09-26
op
318
2021-09-26
op
void
319
2021-09-26
op
fcgi_write(struct bufferevent *bev, void *d)
320
2021-09-26
op
{
321
2021-09-26
op
/*
322
2021-09-26
op
* There's no much use for the write callback.
323
2021-09-26
op
*/
324
2021-05-09
op
return;
325
2021-09-26
op
}
326
2021-05-09
op
327
2021-09-26
op
void
328
2021-09-26
op
fcgi_error(struct bufferevent *bev, short err, void *d)
329
2021-09-26
op
{
330
2021-10-07
op
struct client *c = d;
331
2021-09-26
op
332
2021-10-02
op
if (!(err & (EVBUFFER_ERROR|EVBUFFER_EOF)))
333
2021-10-02
op
log_warn(NULL, "unknown event error (%x): %s",
334
2021-10-02
op
err, strerror(errno));
335
2021-09-26
op
336
2021-10-07
op
c->type = REQUEST_DONE;
337
2021-10-07
op
if (c->code != 0)
338
2021-10-07
op
client_close(c);
339
2021-10-07
op
else
340
2021-10-07
op
start_reply(c, CGI_ERROR, "CGI error");
341
2021-05-09
op
}
342
2021-05-09
op
343
2021-05-09
op
void
344
2021-10-07
op
fcgi_req(struct client *c)
345
2021-05-09
op
{
346
2021-06-11
op
char addr[NI_MAXHOST], buf[22];
347
2021-05-15
op
int e;
348
2021-06-11
op
time_t tim;
349
2021-06-11
op
struct tm tminfo;
350
2021-06-11
op
struct envlist *p;
351
2021-05-15
op
352
2021-07-06
op
e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
353
2021-05-15
op
addr, sizeof(addr),
354
2021-05-15
op
NULL, 0,
355
2021-05-15
op
NI_NUMERICHOST);
356
2021-05-15
op
if (e != 0)
357
2021-10-02
op
fatal("getnameinfo failed: %s (%s)",
358
2021-10-02
op
gai_strerror(e), strerror(errno));
359
2021-05-15
op
360
2021-10-07
op
fcgi_begin_request(c->cgibev);
361
2021-10-07
op
fcgi_send_param(c->cgibev, "GATEWAY_INTERFACE", "CGI/1.1");
362
2021-10-07
op
fcgi_send_param(c->cgibev, "GEMINI_URL_PATH", c->iri.path);
363
2021-10-07
op
fcgi_send_param(c->cgibev, "QUERY_STRING", c->iri.query);
364
2021-10-07
op
fcgi_send_param(c->cgibev, "REMOTE_ADDR", addr);
365
2021-10-07
op
fcgi_send_param(c->cgibev, "REMOTE_HOST", addr);
366
2021-10-07
op
fcgi_send_param(c->cgibev, "REQUEST_METHOD", "");
367
2021-10-07
op
fcgi_send_param(c->cgibev, "SERVER_NAME", c->iri.host);
368
2021-10-07
op
fcgi_send_param(c->cgibev, "SERVER_PROTOCOL", "GEMINI");
369
2021-10-07
op
fcgi_send_param(c->cgibev, "SERVER_SOFTWARE", GMID_VERSION);
370
2021-06-11
op
371
2021-06-11
op
if (tls_peer_cert_provided(c->ctx)) {
372
2021-10-07
op
fcgi_send_param(c->cgibev, "AUTH_TYPE", "CERTIFICATE");
373
2021-10-07
op
fcgi_send_param(c->cgibev, "REMOTE_USER",
374
2021-06-11
op
tls_peer_cert_subject(c->ctx));
375
2021-10-07
op
fcgi_send_param(c->cgibev, "TLS_CLIENT_ISSUER",
376
2021-06-11
op
tls_peer_cert_issuer(c->ctx));
377
2021-10-07
op
fcgi_send_param(c->cgibev, "TLS_CLIENT_HASH",
378
2021-06-11
op
tls_peer_cert_hash(c->ctx));
379
2021-10-07
op
fcgi_send_param(c->cgibev, "TLS_VERSION",
380
2021-06-11
op
tls_conn_version(c->ctx));
381
2021-10-07
op
fcgi_send_param(c->cgibev, "TLS_CIPHER",
382
2021-06-11
op
tls_conn_cipher(c->ctx));
383
2021-05-09
op
384
2021-06-11
op
snprintf(buf, sizeof(buf), "%d",
385
2021-06-11
op
tls_conn_cipher_strength(c->ctx));
386
2021-10-07
op
fcgi_send_param(c->cgibev, "TLS_CIPHER_STRENGTH", buf);
387
2021-06-11
op
388
2021-06-11
op
tim = tls_peer_cert_notbefore(c->ctx);
389
2021-06-11
op
strftime(buf, sizeof(buf), "%FT%TZ",
390
2021-06-11
op
gmtime_r(&tim, &tminfo));
391
2021-10-07
op
fcgi_send_param(c->cgibev, "TLS_CLIENT_NOT_BEFORE", buf);
392
2021-06-11
op
393
2021-06-11
op
tim = tls_peer_cert_notafter(c->ctx);
394
2021-06-11
op
strftime(buf, sizeof(buf), "%FT%TZ",
395
2021-06-11
op
gmtime_r(&tim, &tminfo));
396
2021-10-07
op
fcgi_send_param(c->cgibev, "TLS_CLIENT_NOT_AFTER", buf);
397
2021-06-11
op
398
2021-06-11
op
TAILQ_FOREACH(p, &c->host->params, envs) {
399
2021-10-07
op
fcgi_send_param(c->cgibev, p->name, p->value);
400
2021-06-11
op
}
401
2021-06-11
op
} else
402
2021-10-07
op
fcgi_send_param(c->cgibev, "AUTH_TYPE", "");
403
2021-06-11
op
404
2021-10-07
op
if (fcgi_end_param(c->cgibev) == -1)
405
2021-10-07
op
fcgi_error(c->cgibev, EVBUFFER_ERROR, c);
406
2021-05-09
op
}
Omar Polo