: asapien/signet

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201 
202 
203 
204 
205 
206 
207 
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

#include "http.h"
#include "util.h"

const char *
http_header(struct kreq *r, const char *name)
{
	size_t i;

	for (i = 0; i < r->reqsz; i++)
		if (strcasecmp(r->reqs[i].key, name) == 0)
			return r->reqs[i].val;
	return NULL;
}

void
ident_read(struct kreq *r, const struct conf *cf, struct ident *id)
{
	const char *groups;

	id->user = http_header(r, conf_get(cf, "auth.user_header", "Remote-User"));
	id->groups = "";
	/* No development environment variable may manufacture an identity or role. */
	if (id->user == NULL || !is_safe_name(id->user)) {
		id->user = NULL;
		return;
	}
	groups = http_header(r, conf_get(cf, "auth.groups_header", "Remote-Groups"));
	if (groups != NULL)
		id->groups = groups;
}

int
ident_has_role(const struct ident *id, const struct conf *cf, const char *role)
{
	char key[64];

	snprintf(key, sizeof(key), "role.%s", role);
	return id->user != NULL &&
	    list_intersects(id->groups, conf_get(cf, key, ""));
}

static void
free_pair(struct kpair *p)
{
	free(p->key);
	free(p->val);
	free(p->file);
	free(p->ctype);
	free(p->xcode);
}

/* Unused form/cookie arrays must not compete with qpdf while a handler waits
 * on PostgreSQL. Keep first values, as http_field() does, before borrowing them.
 * Both APIs use unmapped kcgi requests; maps would point into the old array. */
int
http_keep_fields(struct kreq *r, const char *const *names, size_t count)
{
	struct kpair *p, *fields;
	size_t i, j, n = 0, oldsz = r->fieldsz;

	if (r->keysz != 0)
		return -1;
	for (i = 0; i < oldsz; i++) {
		p = &r->fields[i];
		for (j = 0; j < count; j++)
			if (strcmp(p->key, names[j]) == 0)
				break;
		if (j == count) {
			free_pair(p);
			continue;
		}
		for (j = 0; j < n; j++)
			if (strcmp(p->key, r->fields[j].key) == 0)
				break;
		if (j != n) {
			free_pair(p);
			continue;
		}
		r->fields[n] = *p;
		r->fields[n++].next = NULL;
	}
	r->fieldsz = n;
	/* Identity is forwarded in headers, never in cookies. */
	for (i = 0; i < r->cookiesz; i++)
		free_pair(&r->cookies[i]);
	free(r->cookies);
	r->cookies = NULL;
	r->cookiesz = 0;
	if (n == 0) {
		free(r->fields);
		r->fields = NULL;
		return 0;
	}
	if (n == oldsz)
		return 0;
	fields = realloc(r->fields, n * sizeof(*fields));
	if (fields == NULL)
		return -1;
	r->fields = fields;
	return 0;
}

struct kpair *
http_field(struct kreq *r, const char *key)
{
	size_t i;

	for (i = 0; i < r->fieldsz; i++)
		if (strcmp(r->fields[i].key, key) == 0)
			return &r->fields[i];
	return NULL;
}

const char *
http_field_str(struct kreq *r, const char *key)
{
	struct kpair *p = http_field(r, key);

	/* kcgi NUL-terminates val; reject embedded NULs */
	if (p == NULL || strlen(p->val) != p->valsz)
		return NULL;
	return p->val;
}

int
http_post_ok(struct kreq *r)
{
	const char *h = http_header(r, "X-Requested-With");

	return r->method == KMETHOD_POST && h != NULL && strcmp(h, "signet") == 0;
}

int
http_path(struct kreq *r, char *buf, size_t bufsz, char **seg, int max)
{
	char *p;
	int n = 0;

	snprintf(buf, bufsz, "%s", r->fullpath);
	for (p = strtok(buf, "/"); p != NULL && n < max; p = strtok(NULL, "/"))
		seg[n++] = p;
	return n;
}

void
send_json(struct kreq *r, enum khttp code, const char *body)
{
	khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[code]);
	khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", "application/json");
	khttp_head(r, kresps[KRESP_CACHE_CONTROL], "%s", "no-store");
	khttp_body(r);
	khttp_puts(r, body);
}

void
send_err_at(struct kreq *r, enum khttp code, const char *msg,
    const char *function, int line)
{
	struct ident id = { NULL, NULL };
	char buf[1024], site[96];
	size_t o;

	if (r->arg != NULL)
		ident_read(r, r->arg, &id);
	/* Error text may contain resolved attributes, filenames or DB details. */
	snprintf(site, sizeof(site), "%s:%d", function, line);
	log_request(http_method_str(r->method), r->fullpath, id.user,
	    atoi(khttps[code]), site);
	o = (size_t)snprintf(buf, sizeof(buf), "{\"error\":\"");
	for (; *msg && o + 8 < sizeof(buf); msg++) {
		if (*msg == '"' || *msg == '\\') {
			buf[o++] = '\\';
			buf[o++] = *msg;
		} else if ((unsigned char)*msg < 0x20)
			buf[o++] = ' ';
		else
			buf[o++] = *msg;
	}
	snprintf(buf + o, sizeof(buf) - o, "\"}");
	send_json(r, code, buf);
}

void
send_file(struct kreq *r, const char *path, const char *mime, const char *name)
{
	char buf[16384];
	size_t n;
	FILE *f;

	if ((f = fopen(path, "rb")) == NULL) {
		send_err(r, KHTTP_404, "file not available");
		return;
	}
	khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
	khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", mime);
	khttp_head(r, kresps[KRESP_CACHE_CONTROL], "%s", "private, no-store");
	if (name != NULL)
		khttp_head(r, kresps[KRESP_CONTENT_DISPOSITION],
		    "inline; filename=\"%s\"", name);
	khttp_body(r);
	while ((n = fread(buf, 1, sizeof(buf), f)) > 0)
		khttp_write(r, buf, n);
	fclose(f);
}

const char *
http_method_str(enum kmethod m)
{
	switch (m) {
	case KMETHOD_GET: return "GET";
	case KMETHOD_POST: return "POST";
	case KMETHOD_HEAD: return "HEAD";
	case KMETHOD_PUT: return "PUT";
	case KMETHOD_DELETE: return "DELETE";
	case KMETHOD_OPTIONS: return "OPTIONS";
	default: return "?";
	}
}