-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
332 lines (290 loc) · 7.28 KB
/
main.c
File metadata and controls
332 lines (290 loc) · 7.28 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* $Id$ */
/*
* Copyright (c) 2016, 2018, 2020 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#if HAVE_ERR
# include <err.h>
#endif
#include <inttypes.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <kcgi.h>
#include <kcgijson.h>
#include "extern.h"
/*
* Start with five pages.
* As you add more pages, you'll start by giving them an identifier key
* in this enum.
*/
enum page {
PAGE_INDEX,
PAGE_LOGIN,
PAGE_LOGOUT,
PAGE_USER_MOD_EMAIL,
PAGE_USER_MOD_PASS,
PAGE__MAX
};
static const char *const pages[PAGE__MAX] = {
"index", /* PAGE_INDEX */
"login", /* PAGE_LOGIN */
"logout", /* PAGE_LOGOUT */
"usermodemail", /* PAGE_USER_MOD_EMAIL */
"usermodpass", /* PAGE_USER_MOD_PASS */
};
/*
* Fill out all headers then start the HTTP document body.
* No more headers after this point!
*/
static void
http_open(struct kreq *r, enum khttp code)
{
khttp_head(r, kresps[KRESP_STATUS],
"%s", khttps[code]);
khttp_head(r, kresps[KRESP_CONTENT_TYPE],
"%s", kmimetypes[r->mime]);
khttp_head(r, "X-Content-Type-Options", "nosniff");
khttp_head(r, "X-Frame-Options", "DENY");
khttp_head(r, "X-XSS-Protection", "1; mode=block");
khttp_body(r);
}
static void
json_emptydoc(struct kreq *r)
{
struct kjsonreq req;
kjson_open(&req, r);
kjson_obj_open(&req);
kjson_obj_close(&req);
kjson_close(&req);
}
/*
* Process an e-mail address change.
* Raises HTTP 400 if not all fields exist or if the e-mail address is
* already taken by the system.
* Raises HTTP 200 on success.
*/
static void
sendmodemail(struct kreq *r, const struct user *u)
{
struct kpair *kp;
if ((kp = r->fieldmap[VALID_USER_EMAIL]) != NULL) {
db_user_update_email(r->arg, kp->parsed.s, u->id);
http_open(r, KHTTP_200);
} else
http_open(r, KHTTP_400);
json_emptydoc(r);
}
/*
* Process a password change.
* Raises HTTP 400 if not all fields exist.
* Raises HTTP 200 on success.
*/
static void
sendmodpass(struct kreq *r, const struct user *u)
{
struct kpair *kp;
if ((kp = r->fieldmap[VALID_USER_HASH]) != NULL) {
db_user_update_pass(r->arg, kp->parsed.s, u->id);
http_open(r, KHTTP_200);
} else
http_open(r, KHTTP_400);
json_emptydoc(r);
}
/*
* Retrieve user information.
* Raises HTTP 200 on success and the JSON of the user.
*/
static void
sendindex(struct kreq *r, const struct user *u)
{
struct kjsonreq req;
http_open(r, KHTTP_200);
kjson_open(&req, r);
kjson_obj_open(&req);
json_user_obj(&req, u);
kjson_obj_close(&req);
kjson_close(&req);
}
/*
* Log in the given user by their e-mail and password.
* Creates a new session.
* Returns HTTP 400 if missing parameters, bad user, bad password, etc.
* Returns HTTP 200 with empty JSON body and cookie headers.
*/
static void
sendlogin(struct kreq *r)
{
int64_t sid, token;
struct kpair *kpi, *kpp;
char buf[64];
struct user *u;
const char *secure;
if ((kpi = r->fieldmap[VALID_USER_EMAIL]) == NULL ||
(kpp = r->fieldmap[VALID_USER_HASH]) == NULL) {
http_open(r, KHTTP_400);
json_emptydoc(r);
return;
}
u = db_user_get_creds(r->arg, kpi->parsed.s, kpp->parsed.s);
if (u == NULL) {
http_open(r, KHTTP_400);
json_emptydoc(r);
return;
}
#if HAVE_ARC4RANDOM
token = arc4random();
#else
token = random();
#endif
sid = db_sess_insert(r->arg, u->id, token);
khttp_epoch2str
(time(NULL) + 60 * 60 * 24 * 365,
buf, sizeof(buf));
#ifdef SECURE
secure = " secure;";
#else
secure = "";
#endif
khttp_head(r, kresps[KRESP_SET_COOKIE],
"%s=%" PRId64 ";%s HttpOnly; path=/; expires=%s",
valid_keys[VALID_SESS_TOKEN].name, token, secure, buf);
khttp_head(r, kresps[KRESP_SET_COOKIE],
"%s=%" PRId64 ";%s HttpOnly; path=/; expires=%s",
valid_keys[VALID_SESS_ID].name, sid, secure, buf);
http_open(r, KHTTP_200);
json_emptydoc(r);
db_user_free(u);
}
/*
* Log out the given user by deleting the user's session (if found) and
* invalidating the client-side session.
* Returns HTTP 200 with empty JSON body.
*/
static void
sendlogout(struct kreq *r, const struct sess *s)
{
const char *secure;
char buf[32];
khttp_epoch2str(0, buf, sizeof(buf));
#ifdef SECURE
secure = " secure;";
#else
secure = "";
#endif
khttp_head(r, kresps[KRESP_SET_COOKIE],
"%s=; path=/;%s HttpOnly; expires=%s",
valid_keys[VALID_SESS_TOKEN].name, secure, buf);
khttp_head(r, kresps[KRESP_SET_COOKIE],
"%s=; path=/;%s HttpOnly; expires=%s",
valid_keys[VALID_SESS_ID].name, secure, buf);
http_open(r, KHTTP_200);
json_emptydoc(r);
db_sess_delete_id(r->arg, s->id, s->token);
}
int
main(void)
{
struct kreq r;
enum kcgi_err er;
struct sess *s;
/* If this applies to your OS, you've made bad decisions. */
#if !HAVE_ARC4RANDOM
srandom(time(NULL) + getpid());
#endif
er = khttp_parse(&r, valid_keys, VALID__MAX,
pages, PAGE__MAX, PAGE_INDEX);
if (er != KCGI_OK) {
kutil_warnx(NULL, NULL, "%s", kcgi_strerror(er));
return EXIT_FAILURE;
}
/*
* Front line of defence: make sure we're a proper method, make
* sure we're a page, make sure we're a JSON file.
*/
if (r.method != KMETHOD_GET &&
r.method != KMETHOD_POST) {
http_open(&r, KHTTP_405);
khttp_free(&r);
return EXIT_SUCCESS;
} else if (r.page == PAGE__MAX ||
r.mime != KMIME_APP_JSON) {
http_open(&r, KHTTP_404);
khttp_puts(&r, "Page not found.");
khttp_free(&r);
return EXIT_SUCCESS;
}
r.arg = db_open_logging
(DATADIR "/yourprog.db", NULL, warnx, NULL);
if (r.arg == NULL) {
http_open(&r, KHTTP_500);
json_emptydoc(&r);
khttp_free(&r);
return EXIT_SUCCESS;
}
#if HAVE_PLEDGE
if (pledge("stdio", NULL) == -1) {
kutil_warn(NULL, NULL, "pledge");
db_close(r.arg);
khttp_free(&r);
return EXIT_FAILURE;
}
#endif
/*
* Assume we're logging in with a session and grab the session
* from the database.
* This is our first database access.
*/
s = db_sess_get_creds(r.arg,
r.cookiemap[VALID_SESS_ID] != NULL ?
r.cookiemap[VALID_SESS_ID]->parsed.i : -1,
r.cookiemap[VALID_SESS_TOKEN] != NULL ?
r.cookiemap[VALID_SESS_TOKEN]->parsed.i : -1);
/* User authorisation. */
if (r.page != PAGE_LOGIN && s == NULL) {
http_open(&r, KHTTP_403);
json_emptydoc(&r);
db_close(r.arg);
khttp_free(&r);
return EXIT_SUCCESS;
}
switch (r.page) {
case PAGE_INDEX:
sendindex(&r, &s->user);
break;
case PAGE_LOGIN:
sendlogin(&r);
break;
case PAGE_LOGOUT:
sendlogout(&r, s);
break;
case PAGE_USER_MOD_EMAIL:
sendmodemail(&r, &s->user);
break;
case PAGE_USER_MOD_PASS:
sendmodpass(&r, &s->user);
break;
default:
abort();
}
db_sess_free(s);
db_close(r.arg);
khttp_free(&r);
return EXIT_SUCCESS;
}