aboutsummaryrefslogtreecommitdiff
path: root/db.c
blob: 30c255b6f7394c50858f4d5feaa47ecd5b63f9e0 (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
#include <err.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sqlite3.h>
#include <time.h>
#include <unistd.h>
#include "cforum.h"
#include "crypt/ow-crypt.h"

static char * strdupn(const unsigned char *);

/*
 * The `add' functions insert an att/post/user struct into the database.
 */
int
addatt(struct att *att)
{
	sqlite3_stmt *stmt;

	if(sqlite3_prepare(db, "INSERT INTO atts"
	    " (post, name, desc, mime, data)"
	    " VALUES (?, ?, ?, ?, ?)",
	    -1, &stmt, 0) != SQLITE_OK)
		goto err;
	
	if(sqlite3_bind_int(stmt, 1, att->post)
	    != SQLITE_OK)
		goto err;
	
	if(sqlite3_bind_text(stmt, 2, att->name, -1, SQLITE_STATIC)
	    != SQLITE_OK)
		goto err;
	
	if(sqlite3_bind_text(stmt, 3, att->desc, -1, SQLITE_STATIC)
	    != SQLITE_OK)
		goto err;
	
	if(sqlite3_bind_text(stmt, 4, att->mime, -1, SQLITE_STATIC)
	    != SQLITE_OK)
		goto err;
	
	if(sqlite3_bind_blob(stmt, 5, att->data, att->bytes, SQLITE_STATIC)
	    != SQLITE_OK)
		goto err;
	
	if(sqlite3_step(stmt) != SQLITE_DONE)
		goto err;
	
	sqlite3_finalize(stmt);
	return 1;
err:
	sqlite3_finalize(stmt);
	return 0;
}

int
addpost(struct post *post)
{
	sqlite3_stmt *stmt;

	if(sqlite3_prepare(db, "INSERT INTO posts"
	    " (parent, user, created, subject, text)"
	    " VALUES (?, ?, ?, ?, ?)",
	    -1, &stmt, 0) != SQLITE_OK)
		goto err;
	
	if(sqlite3_bind_int(stmt, 1, post->parent) != SQLITE_OK)
		goto err;
	
	if(sqlite3_bind_int(stmt, 2, post->user) != SQLITE_OK)
		goto err;
	
	if(sqlite3_bind_int(stmt, 3, post->created) != SQLITE_OK)
		goto err;
	
	if(sqlite3_bind_text(stmt, 4, post->subject, -1, SQLITE_STATIC)
	    != SQLITE_OK)
		goto err;
	
	if(sqlite3_bind_text(stmt, 5, post->text, -1, SQLITE_STATIC)
	    != SQLITE_OK)
		goto err;
	
	if(sqlite3_step(stmt) != SQLITE_DONE)
		goto err;
	
	sqlite3_finalize(stmt);
	return 1;
err:
	sqlite3_finalize(stmt);
	return 0;
}

int
adduser(struct user *user)
{
	sqlite3_stmt *stmt;

	if(sqlite3_prepare(db, "INSERT INTO users"
	    " (name, full, hash, salt, created)"
	    " VALUES (?, ?, ?, ?, ?)",
	    -1, &stmt, 0) != SQLITE_OK)
		goto err;
	
	if(sqlite3_bind_text(stmt, 1, user->name, -1, SQLITE_STATIC)
	    != SQLITE_OK)
		goto err;
	
	if(sqlite3_bind_text(stmt, 2, user->full, -1, SQLITE_STATIC)
	    != SQLITE_OK)
		goto err;
	
	if(sqlite3_bind_text(stmt, 3, user->hash, -1, SQLITE_STATIC)
	    != SQLITE_OK)
		goto err;

	if(sqlite3_bind_text(stmt, 4, user->salt, -1, SQLITE_STATIC)
	    != SQLITE_OK)
		goto err;
	
	if(sqlite3_bind_int(stmt, 5, time(NULL)) != SQLITE_OK)
		goto err;
	
	if(sqlite3_step(stmt) != SQLITE_DONE)
		goto err;
	
	sqlite3_finalize(stmt);
	return 1;
err:
	sqlite3_finalize(stmt);
	return 0;
}

/*
 * The `get' functions retrieve an att/post/user struct once,
 * after which the statement is automatically finalized.
 */
struct att *
getatt(sqlite3_stmt *stmt)
{
	struct att *att;
	att = nextatt(stmt);
	sqlite3_finalize(stmt);
	return att;
}

struct post *
getpost(sqlite3_stmt *stmt)
{
	struct post *post;
	post = nextpost(stmt);
	sqlite3_finalize(stmt);
	return post;
}

struct user *
getuser(sqlite3_stmt *stmt)
{
	struct user *user;
	user = nextuser(stmt);
	sqlite3_finalize(stmt);
	return user;
}

/* Return true if user has given password. */
int
haspass(struct user *user, char *pass)
{
	char *newhash;
	
	newhash = crypt(pass, user->salt);
	return strcmp(user->hash, newhash) == 0;
}

/* Generate new salt and hash for password. */
void
makehash(char *pass, char **hash, char **salt)
{
	char data[50];
	int fd;
	
	if(fd = open("/dev/urandom", O_RDONLY) == -1)
		err(1, "open");

	if(read(fd, data, sizeof(data)) == -1)
		err(1, "read");
	
	if(!(*salt = crypt_gensalt("$2b$", 10, data, sizeof(data))))
		err(1, "crypt_gensalt");
	
	*hash = crypt(pass, *salt);
	close(fd);
}

/*
 * The `next' functions create an att/post/user struct by querying
 * the database with the given stmt. They may be called multiple times
 * with the same stmt to retrieve multiple rows.
 */
struct att *
nextatt(sqlite3_stmt *stmt)
{
	struct att *att;
	
	if(!stmt)
		return NULL;
	
	if(sqlite3_step(stmt) != SQLITE_ROW)
		return NULL;
	
	if(!(att = malloc(sizeof(struct att))))
		err(1, "malloc");
	
	att->id = sqlite3_column_int(stmt, 0);
	att->post = sqlite3_column_int(stmt, 1);
	att->name = strdupn(sqlite3_column_text(stmt, 2));
	att->desc = strdupn(sqlite3_column_text(stmt, 3));
	att->mime = strdupn(sqlite3_column_text(stmt, 4));
	
	att->bytes = sqlite3_column_bytes(stmt, 5);
	if(!(att->data = malloc(att->bytes)))
		err(1, "malloc");
	memcpy(att->data, sqlite3_column_blob(stmt, 5),
	    att->bytes);
	
	return att;
}

struct post *
nextpost(sqlite3_stmt *stmt)
{
	struct post *post;
	
	if(!stmt)
		return NULL;
	
	if(sqlite3_step(stmt) != SQLITE_ROW)
		return NULL;
	
	if(!(post = malloc(sizeof(struct post))))
		err(1, "malloc");
	
	post->id = sqlite3_column_int(stmt, 0);
	post->parent = sqlite3_column_int(stmt, 1);
	post->user = sqlite3_column_int(stmt, 2);
	post->created = sqlite3_column_int(stmt, 3);
	post->edited = sqlite3_column_int(stmt, 4);
	post->subject = strdupn(sqlite3_column_text(stmt, 5));
	post->text = strdupn(sqlite3_column_text(stmt, 6));
	
	return post;
}

struct user *
nextuser(sqlite3_stmt *stmt)
{
	struct user *user;
	
	if(!stmt)
		return NULL;
	
	if(sqlite3_step(stmt) != SQLITE_ROW)
		return NULL;
	
	if(!(user = malloc(sizeof(struct user))))
		err(1, "malloc");
	
	user->id = sqlite3_column_int(stmt, 0);
	user->name = strdupn(sqlite3_column_text(stmt, 1));
	user->full = strdupn(sqlite3_column_text(stmt, 2));
	user->hash = strdupn(sqlite3_column_text(stmt, 3));
	user->created = sqlite3_column_int(stmt, 4);
	
	return user;
}

/*
 * Create a statement that selects from a given table on a given integer.
 * The returned statement must eventually be finalized by calling
 * sqlite3_finalize(sqlite3_stmt *).
 */
sqlite3_stmt *
selectbyint(char *table, char *field, int i)
{
	char sql[100];
	sqlite3_stmt *stmt;
	
	snprintf(sql, 100, "SELECT oid, * FROM %s WHERE %s = ?", table, field);
	
	if(sqlite3_prepare(db, sql, -1, &stmt, 0) != SQLITE_OK)
		return NULL;
		
	if(sqlite3_bind_int(stmt, 1, i) != SQLITE_OK)
		return NULL;
	
	return stmt;
}

/* Return an allocated copy of string unless NULL. */
char *
strdupn(const unsigned char *s)
{
	return s? strdup((char *)s): NULL;
}