summaryrefslogtreecommitdiffstats
path: root/src/squark-auth-ip.c
blob: 20c7d3577342e22b8e68430a59df13bf499ab12f (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
/* squark-auth-ip.c - Squid User Authentication and Rating Kit
 *   An external acl helper for Squid which collects authentication
 *   information about an IP-address from local shared memory database.
 *
 * Copyright (C) 2010 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation. See http://www.gnu.org/ for details.
 */

#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

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

#define DO_LOGIN	-1
#define DO_OVERRIDE	-2
#define DO_PRINT	-3
#define DO_LOGOUT	-4
#define DO_REFRESH	-5

static int running = 1;
static struct sqdb db;
static struct authdb adb;
static struct authdb_config adbc;
static blob_t space = BLOB_STR_INIT(" ");
static blob_t lf = BLOB_STR_INIT("\n");
static time_t now;

static void handle_line(blob_t line)
{
	char reply[128];
	blob_t b, id, ipaddr;
	struct authdb_entry entry;
	sockaddr_any addr;
	void *token;
	int auth_ok = 0;

	id = blob_pull_cspn(&line, space);
	blob_pull_spn(&line, space);
	ipaddr = blob_pull_cspn(&line, space);

	if (addr_parse(ipaddr, &addr)) {
		token = authdb_get(&adb, &addr, &entry, 1);

		if (token != NULL && authdb_check_login(token, &entry, BLOB_NULL, now, &adbc))
			auth_ok = 1;
	}

	b = BLOB_BUF(reply);
	blob_push(&b, id);
	if (auth_ok) {
		blob_push(&b, BLOB_STR(" OK user="));
		blob_push(&b, BLOB_STRLEN(entry.p.login_name));
		blob_push(&b, BLOB_PTR_LEN("\n", 1));
	} else {
		blob_push(&b, BLOB_STR(" ERR\n"));
	}

	b = blob_pushed(BLOB_BUF(reply), b);
	if(write(STDOUT_FILENO, b.ptr, b.len) < 0)
		report_error("Error occurred while writing to stdout: %s", strerror(errno));
}

static void read_input(void)
{
	static char buffer[256];
	static blob_t left;

	blob_t b, line;
	int r;

	if (blob_is_null(left))
		left = BLOB_BUF(buffer);

	r = read(STDIN_FILENO, left.ptr, left.len);
	if (r < 0)
		return;
	if (r == 0) {
		running = 0;
		return;
	}
	left.ptr += r;
	left.len -= r;

	now = time(NULL);
	adbc_refresh(&adbc, now);

	b = blob_pushed(BLOB_BUF(buffer), left);
	do {
		line = blob_pull_cspn(&b, lf);
		if (!blob_pull_matching(&b, lf))
			return;

		handle_line(line);

		if (b.len) {
			memcpy(buffer, b.ptr, b.len);
			b.ptr = buffer;
		}
		left = BLOB_PTR_LEN(buffer + b.len, sizeof(buffer) - b.len);
	} while (b.len);
}

#define DUMPPAR(b, name, fn) \
	do {							\
		blob_push(b, BLOB_STR("squark_" name "='"));	\
		fn;						\
		blob_push(b, BLOB_STR("'; "));			\
	} while (0)

int main(int argc, char **argv)
{
	int opt, rc = 1;
	sockaddr_any ipaddr = { .any.sa_family = AF_UNSPEC };
	blob_t ip = BLOB_NULL, username = BLOB_NULL;

	reporting_init("squark-auth-ip");

	while ((opt = getopt(argc, argv, "Vi:u:olpLrsqv::")) != -1) {
		switch (opt) {
		case 'V':
			fprintf(stderr, "squark-auth-ip %s\n", squark_version);
			return 0;
		case 'i':
			ip = BLOB_STRLEN(optarg);
			if (!addr_parse(ip, &ipaddr)) {
				report_error("'%s' does not look like IP-address\n", optarg);
				return 1;
			}
			break;
		case 'u':
			username = BLOB_STRLEN(optarg);
			break;
		case 'o':
			running = DO_OVERRIDE;
			break;
		case 'l':
			running = DO_LOGIN;
			break;
		case 'p':
			running = DO_PRINT;
			break;
		case 'L':
			running = DO_LOGOUT;
			break;
		case 'r':
			running = DO_REFRESH;
			break;
		case 's':
			reporting_use_syslog(1);
			break;
		case 'q':
			reporting_verbosity(REPORT_ALERT);
			break;
		case 'v':
			if (optarg == 0)
				reporting_verbosity(REPORT_INFO);
			else if (*optarg == 'v')
				reporting_verbosity(REPORT_DEBUG);
			break;
		}
	}

	now = time(NULL);
	if (sqdb_open(&db, squark_dbname) < 0) {
		report_error("%s: failed to open squarkdb\n", squark_dbname);
		goto err_sqdb;
	}
	if (authdb_open(&adb, &adbc, &db) < 0) {
		report_error("Failed to initialize authdb\n");
		goto err_adb;
	}

	rc = 0;
	if (running < 0) {
		struct authdb_entry entry;
		void *token;

		if (ipaddr.any.sa_family == AF_UNSPEC) {
			report_error("IP-address not specified\n");
			return 2;
		}

		token = authdb_get(&adb, &ipaddr, &entry, 1);
		if (token == NULL) {
			report_error("Failed to get authdb record\n");
			return 3;
		}

		switch (running) {
		case DO_LOGIN:
			if (blob_is_null(username)) {
				report_error("Username not specified\n");
				return 2;
			}
			authdb_clear_entry(&entry);
			memcpy(entry.p.login_name, username.ptr, username.len);
			authdb_commit_login(token, &entry, now, &adbc);
			break;
		case DO_REFRESH:
			if (!authdb_check_login(token, &entry, username, now, &adbc))
				rc = 3;
			break;
		case DO_OVERRIDE:
			if (authdb_check_login(token, &entry, username, now, &adbc))
				authdb_commit_override(token, &entry, now);
			break;
		case DO_PRINT: {
			char buf[512];
			blob_t b = BLOB_BUF(buf);

			DUMPPAR(&b, "ip_address",
				addr_push_hostaddr(&b, &ipaddr));
			DUMPPAR(&b, "username",
				blob_push(&b, BLOB_BUF(entry.p.login_name)));
			DUMPPAR(&b, "mac_address",
				blob_push_hexdump(&b, BLOB_BUF(entry.p.mac_address)));
			DUMPPAR(&b, "login_time",
				blob_push_ctime(&b, entry.p.login_time));
			DUMPPAR(&b, "activity_time",
				blob_push_ctime(&b, entry.last_activity_time));
			DUMPPAR(&b, "override_time",
				blob_push_ctime(&b, entry.override_time));
			DUMPPAR(&b, "block_categories",
				blob_push_hexdump(&b, BLOB_BUF(&entry.p.block_categories)));
			DUMPPAR(&b, "hard_block_categories",
				blob_push_hexdump(&b, BLOB_BUF(&entry.p.hard_block_categories)));
			blob_push(&b, BLOB_STR("\n"));
			b = blob_pushed(BLOB_BUF(buf), b);
			fwrite(b.ptr, b.len, 1, stdout);
			break;
			}
		case DO_LOGOUT:
			if (authdb_check_login(token, &entry, username, now, &adbc))
				authdb_commit_logout(token);
			break;
		}
	} else {
		while (running)
			read_input();
	}

	authdb_close(&adb);
err_adb:
	sqdb_close(&db);
err_sqdb:
	return rc;
}