summaryrefslogtreecommitdiffstats
path: root/src/authdb.c
blob: d16ba3a7421fe29ba05360331daf6e1d6546954a (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sched.h>
#include <fcntl.h>
#include <ctype.h>
#include <time.h>
#include <grp.h>
#include <errno.h>
#include <string.h>

#include "config.h"
#include "authdb.h"
#include "filterdb.h"
#include "addr.h"
#include "blob.h"
#include "reporting.h"

#define ALIGN(s,a)		(((s) + a - 1) & ~(a - 1))

#define AUTHDB_IP_PER_ME		256
#define AUTHDB_SHM_SIZE			ALIGN(sizeof(struct authdb_entry[AUTHDB_IP_PER_ME]), 4096)

static struct authdb_map_entry *authdb_me_open(sockaddr_any *addr, int create)
{
	int oflag, fd;
	char name[64], buf[256];
	blob_t b = BLOB_BUF(name);
	void *base;
	struct authdb_map_entry *me;
	struct group grp, *res;

	blob_push(&b, BLOB_STR("squark-auth-"));
	blob_push_hexdump(&b, addr_get_hostaddr_blob(addr));
	blob_push_byte(&b, 0);

	oflag = O_RDWR;
	if (create)
		oflag |= O_CREAT;

	fd = shm_open(name, oflag, 0660);
	if (fd < 0) {
		report_error("Couldn't create shared memory object. Error: %s\n", strerror(errno));
		return NULL;
	}

	if (ftruncate(fd, AUTHDB_SHM_SIZE) < 0) {
		close(fd);
		report_error("Couldn't truncate shared memory object. Error: %s\n", strerror(errno));
		return NULL;
	}

	getgrnam_r("squark", &grp, buf, sizeof(buf), &res);
	if (res != NULL) {
		if (fchown(fd, -1, res->gr_gid) < 0)
			report_error("Couldn't set group ownership of shared memory object. Error: %s\n",
				strerror(errno));
		fchmod(fd, 0660);
	}

	base = mmap(NULL, AUTHDB_SHM_SIZE,
		    PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	close(fd);

	if (base == MAP_FAILED) {
		report_error("Couldn't map shared memory object into address space. Error: %s\n",
			strerror(errno));
		return NULL;
	}

	me = malloc(sizeof(*me));
	if (me == NULL) {
		munmap(base, AUTHDB_SHM_SIZE);
		report_error("Couldn't allocate memory for authentication database entry. Error: %s\n",
			strerror(errno));
		return NULL;
	}

	me->next = NULL;
	me->baseaddr = *addr;
	me->entries = base;

	return me;
}

static void authdb_me_free(struct authdb_map_entry *me)
{
	munmap(me->entries, AUTHDB_SHM_SIZE);
	free(me);
}

int authdb_open(struct authdb *adb, struct authdb_config *cfg, struct sqdb *db)
{
	memset(adb, 0, sizeof(*adb));
	memset(cfg, 0, sizeof(*cfg));
	cfg->db = db;
	return adbc_refresh(cfg, time(NULL));
}

void authdb_close(struct authdb *adb)
{
	struct authdb_map_entry *c, *n;
	int i;

	for (i = 0; i < ARRAY_SIZE(adb->hash_bucket); i++) {
		for (c = adb->hash_bucket[i]; c != NULL; c = n) {
			n = c->next;
			authdb_me_free(c);
		}
	}
}

static unsigned int MurmurHash2(const void * key, int len, unsigned int seed)
{
	// 'm' and 'r' are mixing constants generated offline.
	// They're not really 'magic', they just happen to work well.
	const unsigned int m = 0x5bd1e995;
	const int r = 24;
	unsigned int h = seed ^ len;
	const unsigned char * data = (const unsigned char *)key;

	while(len >= 4)
	{
		unsigned int k = *(unsigned int *)data;

		k *= m;
		k ^= k >> r;
		k *= m;

		h *= m;
		h ^= k;

		data += 4;
		len -= 4;
	}

	switch(len)
	{
	case 3:	h ^= data[2] << 16;
	case 2:	h ^= data[1] << 8;
	case 1:	h ^= data[0];
		h *= m;
	}

	h ^= h >> 13;
	h *= m;
	h ^= h >> 15;

	return h;
}

static uint32_t authdb_entry_checksum(struct authdb_entry *entry)
{
	return MurmurHash2(&entry->p, sizeof(entry->p), 0);
}

void *authdb_get(struct authdb *adb, sockaddr_any *addr, struct authdb_entry *entry, int create)
{
	struct authdb_map_entry *me;
	unsigned int hash, e, i;
	sockaddr_any baseaddr;
	blob_t b;

	baseaddr = *addr;
	b = addr_get_hostaddr_blob(&baseaddr);
	if (b.len < 4)
		return NULL;

	e = (unsigned char) b.ptr[3];
	b.ptr[3] = 0x00;

	hash = b.ptr[0] + b.ptr[1] + b.ptr[2];
	hash %= ARRAY_SIZE(adb->hash_bucket);

	for (me = adb->hash_bucket[hash]; me != NULL; me = me->next) {
		if (addr_cmp(&baseaddr, &me->baseaddr) == 0)
			break;
	}
	if (me == NULL) {
		me = authdb_me_open(&baseaddr, create);
		if (me == NULL)
			return NULL;
		me->next = adb->hash_bucket[hash];
		adb->hash_bucket[hash] = me;
	}

	for (i = 0; i < 3; i++) {
		memcpy(entry, &me->entries[e], sizeof(struct authdb_entry));
		if (entry->checksum == 0 && entry->p.login_time == 0)
			return &me->entries[e];
		if (entry->checksum == authdb_entry_checksum(entry))
			return &me->entries[e];
		sched_yield();
	}

	authdb_clear_entry(entry);

	return &me->entries[e];
}

int authdb_set(void *token, struct authdb_entry *entry)
{
	struct authdb_entry *mme = token;
	uint32_t checksum = entry->checksum;

	entry->checksum = authdb_entry_checksum(entry);
	if (mme->checksum != checksum)
		return 0;

	mme->checksum = ~0;
	memcpy(mme, entry, sizeof(*entry));

	return 1;
}

int authdb_check_login(void *token, struct authdb_entry *e,
		       blob_t username, time_t now,
		       struct authdb_config *adbc)
{
	struct authdb_entry *mme = token;

	/* check username */
	if (!blob_is_null(username) &&
	    blob_cmp(username, BLOB_CHAR_ARRAY(e->p.login_name)) != 0)
		return 0;

	/* and dates */
	if (now > e->last_activity_time + adbc->logout_timeout)
		return 0;

	/* and that no one clobbered the entry */
	if (mme->checksum != e->checksum)
		return 0;

	/* refresh last activity -- avoid writes to page so
	 * caches don't get invalidated too often */
	if (now > mme->last_activity_time + 2)
		mme->last_activity_time = now;

	return 1;
}

void authdb_clear_entry(struct authdb_entry *entry)
{
	uint32_t checksum = entry->checksum;

	memset(entry, 0, sizeof(*entry));
	entry->checksum = checksum;
}

void authdb_commit_login(void *token, struct authdb_entry *e, time_t now, struct authdb_config *cfg)
{
	e->p.block_categories = cfg->block_categories;
	e->p.hard_block_categories = cfg->hard_block_categories;
	e->p.login_time = now;
	e->last_activity_time = now;
	e->override_time = 0;

	authdb_set(token, e);
}

void authdb_commit_logout(void *token)
{
	memset(token, 0, sizeof(struct authdb_entry));
}

void authdb_commit_override(void *token, struct authdb_entry *e, time_t now)
{
	struct authdb_entry *mme = token;

	mme->override_time = now;
}

static blob_t read_word(FILE *in, int *lineno, blob_t b)
{
	int ch, i, comment = 0;
	blob_t r;

	ch = fgetc(in);
	while (1) {
		if (ch == EOF)
			return BLOB_NULL;
		if (ch == '#')
			comment = 1;
		if (!comment && !isspace(ch))
			break;
		if (ch == '\n') {
			(*lineno)++;
			comment = 0;
		}
		ch = fgetc(in);
	}

	r.ptr = b.ptr;
	r.len = 0;
	for (i = 0; i < b.len-1 && !isspace(ch); i++) {
		r.ptr[i] = ch;
		r.len++;
		ch = fgetc(in);
		if (ch == EOF)
			break;
		if (ch == '\n')
			(*lineno)++;
	}

	return r;
}

static int find_category_id(struct sqdb *db, blob_t cat)
{
	uint32_t size, *ptr;
	int i;

	ptr = sqdb_section_get(db, SQDB_SECTION_CATEGORIES, &size);
	if (ptr == NULL)
		return -1;

	size /= sizeof(uint32_t);
	for (i = 0; i < size; i++)
		if (blob_cmp(cat, sqdb_get_string_literal(db, ptr[i])) == 0)
			return i;

	return -1;
}

static inline uint64_t to_category(struct sqdb *db, blob_t c)
{
	int category;

	category = find_category_id(db, c);
	if (category >= 0)
		return 1ULL << category;

	report_warning("Unknown category '%.*s'\n", c.len, c.ptr);
	return 0;
}

static int is_true(blob_t p)
{
	if (blob_icmp(p, BLOB_STR("yes")) == 0 ||
	    blob_icmp(p, BLOB_STR("true")) == 0 ||
	    blob_icmp(p, BLOB_STR("1")) == 0)
		return 1;

	return 0;
}

int adbc_refresh(struct authdb_config *cfg, time_t now)
{
	FILE *in;
	int lineno = 1;
	char word1[64], word2[64];
	blob_t b, p;
	struct stat st;

	if (cfg->last_check != 0 && cfg->last_check + 2*60 > now)
		return 0;

	if (stat(squark_config, &st) != 0) {
		report_error("Error occurred while checking attributes of config file: %s\n", strerror(errno));
		return -1;
	}

	cfg->last_check = now;
	if (cfg->last_change == st.st_ctime)
		return 0;

	in = fopen(squark_config, "r");
	if (in == NULL) {
		report_error("Error occurred while opening config file: %s\n", strerror(errno));
		return -1;
	}

	cfg->last_change = st.st_ctime;
	cfg->block_categories = 0;
	cfg->hard_block_categories = 0;
	cfg->logout_timeout = DEFAULT_LOGOUT_TIMEOUT;
	cfg->require_auth = 1;

	while (1) {
		b = read_word(in, &lineno, BLOB_BUF(word1));
		if (blob_is_null(b))
			break;

		p = read_word(in, &lineno, BLOB_BUF(word2));
		if (blob_cmp(b, BLOB_STR("redirect_path")) == 0) {
			cfg->redirect_url_base = blob_dup(p);
		} else if (blob_cmp(b, BLOB_STR("forbid")) == 0) {
			cfg->hard_block_categories |= to_category(cfg->db, p);
		} else if (blob_cmp(b, BLOB_STR("warn")) == 0) {
			cfg->block_categories |= to_category(cfg->db, p);
		} else if (blob_cmp(b, BLOB_STR("logout_timeout")) == 0) {
			cfg->logout_timeout = blob_pull_uint(&p, 10);
		} else if (blob_cmp(b, BLOB_STR("require_auth")) == 0) {
			cfg->require_auth = is_true(p);
		}
	}
	cfg->block_categories |= cfg->hard_block_categories;

	fclose(in);

	return 1;
}