aboutsummaryrefslogtreecommitdiff
path: root/ctl.c
blob: cd90f4fe18ac52a5df4533574e8abe1a4cc6a55a (plain)
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
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "cforum.h"

#define MAXMSG 256

/* Print UNIX timestamp as written date. */
void
printdate(int timestamp)
{
	char buf[20];
	struct tm *t;
	
	t = localtime((time_t *)&timestamp);
	strftime(buf, 20, "%Y-%m-%d %H:%M", t);
	
	printf(buf);
}

/* Print HTML-escaped string. */
void
printhtml(char *s)
{
	char *t;
	
	for(t = s; *t; t++)
		switch(*t){
		case '&': printf("&amp;"); break;
		case '\"': printf("&quot;"); break;
		case '\'': printf("&apos;"); break;
		case '<': printf("&lt;"); break;
		case '>': printf("&gt;"); break;
		default: printf("%c", *t);
		}
}

void
login()
{
	char *hlite, msg[MAXMSG], *name;
	char title[] = "Log In";
	
	*msg = 0;
	hlite = name = NULL;
	
	printf("Content-Type: text/html\n\n");
	#include "t/login.tc"
}

/*
 * The `new' functions provide a way to add a new attachment/post/user.
 * On GET, they show a form. On POST, they insert the posted information
 * into the database.
 */
void
newatt()
{}

void
newpost()
{}

void
newuser()
{
	char *captcha, *confirm, *name, *full, *pass;
	char *hlite, msg[MAXMSG], *p, *v;
	char title[] = "New User";
	int i;
	struct user *user;
	
	*msg = 0;
	captcha = confirm = hlite = name = full = pass = NULL;
	
	if(query.method == GET){
		printf("Content-Type: text/html\n\n");
		#include "t/newuser.tc"
		return;
	}
	
	if(query.length > MAXUSERDATA){
		snprintf(msg, MAXMSG, "Input exceeded server limitations");
		printf("Status: 431 Request Header Fields Too Large\n");
		printf("Content-Type: text/html\n\n");
		#include "t/newuser.tc"
		return;
	}
	
	while(p = nextparam(POST, NULL, MAXUSERDATA)){
		if(!(v = split(p))) continue;
		
		if(!captcha && strcmp(p, "captcha") == 0)
			captcha = strdup(v);
		if(!confirm && strcmp(p, "confirm") == 0)
			confirm = strdup(v);
		else if(!name && strcmp(p, "name") == 0)
			name = strdup(v);
		else if(!full && strcmp(p, "full") == 0)
		 	full = strdup(v);
		else if(!pass && strcmp(p, "pass") == 0)
		 	pass = strdup(v);
		else
			continue;
	}
	
	/* Decode URL-encoded fields. */
	if(captcha && *captcha)
		captcha[urldecode(captcha, -1)] = 0;
	if(confirm && *confirm)
		confirm[urldecode(confirm, -1)] = 0;
	if(name && *name)
		name[urldecode(name, -1)] = 0;
	if(full && *full)
		full[urldecode(full, -1)] = 0;
	if(pass && *pass)
		pass[urldecode(pass, -1)] = 0;
	
	/* Constrain lengths of decoded fields. */
	if(name && *name && strlen(name)-1 > MAXUSERNAME){
		hlite = strdup("name");
		snprintf(msg, MAXMSG, "Username longer than %d characters",
		    MAXUSERNAME-1);
		goto err;
	}
	if(full && *full && strlen(full)-1 > MAXUSERFULL){
		hlite = strdup("full");
		snprintf(msg, MAXMSG, "Full name longer than %d characters",
		    MAXUSERFULL-1);
		goto err;
	}
	if(pass && *pass && strlen(pass)-1 > MAXUSERPASS){
		hlite = strdup("pass");
		snprintf(msg, MAXMSG, "Password longer than %d characters",
		    MAXUSERPASS-1);
		goto err;
	}
	
#define ISVIS(c) ((unsigned char)c >= 32)
#define ISALNUM(c) (c>='A' && c<='Z' || c>='a' && c<='z' || c>='0' && c<='9')
	
	/* Constrain character sets. */
	for(i = 0; name[i]; i++)
		if(!name[i] == '_' && !ISALNUM(name[i])){
			hlite = strdup("name");
			snprintf(msg, MAXMSG,
			    "Username may only contain ASCII letters, "
			    "numbers and underscores.");
			goto err;
		}
	for(i = 0; full[i]; i++)
		if(!ISVIS(full[i])){
			fprintf(stderr, "%d\n", full[i]);
			hlite = strdup("full");
			snprintf(msg, MAXMSG,
			    "Full name may only contain visible characters");
			goto err;
		}
	
	/* Validate captcha. */
	if(captcha && strcmp(captcha, "9") != 0){
		hlite = strdup("captcha");
		snprintf(msg, MAXMSG, "Incorrectly answered captcha");
		goto err;
	}

	/* Ensure all required fields are there. */
	if(!name || !*name){
		hlite = strdup("name");
		snprintf(msg, MAXMSG, "Username is required");
		goto err;
	}
	if(!pass || !*pass){
		hlite = strdup("pass");
		snprintf(msg, MAXMSG, "Password is required");
		goto err;
	}

	if(pass && confirm && strcmp(pass, confirm) != 0){
		snprintf(msg, MAXMSG, "Passwords do not match");
		goto err;
	}
	
	if(!(user = malloc(sizeof(struct user))))
		err(1, "malloc");
	
	user->name = name;
	user->full = *full? full: NULL;
	makehash(pass, &user->hash, &user->salt);
	
	if(!adduser(user)){
		if(strcmp(sqlite3_errmsg(db), "column name is not unique")==0)
			snprintf(msg, MAXMSG, "Username already exists.");
		else
			snprintf(msg, MAXMSG, "Could not create user: %s. ",
			    sqlite3_errmsg(db));
		goto err;
	}
	
	printf("Content-Type: text/html\n\n");
	snprintf(msg, MAXMSG, "SUCCESS: User was added to database");
	#include "t/newuser.tc"
	return;
err:
	printf("Status: 400 Bad Request\n");
	printf("Content-Type: text/html\n\n");
	#include "t/newuser.tc"
	return;
}

/*
 * The `show' functions show an existing attachment/post/user
 * or some other type of page.
 */
void
showatt(id)
{
	struct att *att;
	
	if(!(att = getatt(selectbyint("atts", "oid", id)))){
		srverr("Could not retrieve attachment");
		return;
	}
	
	printf("Content-Type: %s\n\n", att->mime);
	printf("%.*s", att->bytes, att->data);
	free(att);
}

void
showfront()
{
	char *title;
	struct post *post;
	struct user *user;
	sqlite3_stmt *pstmt, *ustmt;
	
	if(sqlite3_prepare(db,
	    "SELECT oid, * from posts ORDER BY created DESC",
	    -1, &pstmt, 0) != SQLITE_OK){
		srverr("Could not retrieve posts");
		return;
	}
	
	if(sqlite3_prepare(db,
	    "SELECT oid, * from users ORDER BY created DESC",
	    -1, &ustmt, 0) != SQLITE_OK){
		srverr("Could not retrieve users");
		return;
	}
	
	title = site.name;
	printf("Content-Type: text/html\n\n");
	#include "t/front.tc"
}

void
showpost(int id)
{
	char *title;
	struct att *att;
	struct post *post;
	struct user *user;
	sqlite3_stmt *stmt;
	
	if(!(post = getpost(selectbyint("posts", "oid", id)))){
		srverr("Could not retrieve post");
		return;
	}
	
	if(!(user = getuser(selectbyint("users", "oid", post->user)))){
		srverr("Could not retrieve post author");
		return;
	}
	
	stmt = selectbyint("atts", "post", id);
	
	title = post->subject;
	printf("Content-Type: text/html\n\n");
	#include "t/post.tc"
	free(user);
	free(post);
}

void
showuser(int id)
{
	char title[120];
	sqlite3_stmt *stmt;
	struct post *post;
	struct user *user;
	
	if(!(user = getuser(selectbyint("users", "oid", id)))){
		srverr("Could not retrieve user");
		return;
	}
	
	if(sqlite3_prepare(db,
	    "SELECT oid, * from posts WHERE user = ? ORDER BY created DESC",
	    -1, &stmt, 0) != SQLITE_OK)
		goto err;
	
	if(sqlite3_bind_int(stmt, 1, id) != SQLITE_OK)
		goto err;
	
	snprintf(title, 120, "User: %s", user->full? user->full: user->name);
	printf("Content-Type: text/html\n\n");
	#include "t/user.tc"
	free(user);
	return;
	
err:
	srverr("Could not retrieve posts");
	return;
}