summaryrefslogtreecommitdiff
path: root/src/medsrv/controller/user_controller.c
blob: 8bdd86a4244fd39e187d73e54eb3f1649bf484a5 (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
/*
 * Copyright (C) 2008 Martin Willi
 * Copyright (C) 2008 Philip Boetschi, Adrian Doerig
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#define _GNU_SOURCE
#include <string.h>

#include "user_controller.h"

#include <library.h>

typedef struct private_user_controller_t private_user_controller_t;

/**
 * private data of the user_controller
 */
struct private_user_controller_t {

	/**
	 * public functions
	 */
	user_controller_t public;

	/**
	 * database connection
	 */
	database_t *db;

	/**
	 * user session
	 */
	user_t *user;

	/**
	 * minimum required password length
	 */
	u_int password_length;
};

/**
 * hash the password for database storage
 */
static chunk_t hash_password(char *login, char *password)
{
	hasher_t *hasher;
	chunk_t hash, data;

	hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
	if (!hasher)
	{
		return chunk_empty;
	}
	data = chunk_cata("cc",	chunk_create(login, strlen(login)),
							chunk_create(password, strlen(password)));
	if (!hasher->allocate_hash(hasher, data, &hash))
	{
		hasher->destroy(hasher);
		return chunk_empty;
	}
	hasher->destroy(hasher);
	return hash;
}

/**
 * Login a user.
 */
static void login(private_user_controller_t *this, fast_request_t *request)
{
	if (request->get_query_data(request, "submit"))
	{
		char *login, *password;

		login = request->get_query_data(request, "login");
		password = request->get_query_data(request, "password");

		if (login && password)
		{
			enumerator_t *query;
			u_int id = 0;
			chunk_t hash;

			hash = hash_password(login, password);
			query = this->db->query(this->db,
						"SELECT id FROM user WHERE login = ? AND password = ?",
						DB_TEXT, login, DB_BLOB, hash, DB_UINT);
			if (query)
			{
				query->enumerate(query, &id);
				query->destroy(query);
			}
			free(hash.ptr);
			if (id)
			{
				this->user->set_user(this->user, id);
				return request->redirect(request, "peer/list");
			}
		}
		request->setf(request, "error=Invalid username or password.");
	}
	request->render(request, "templates/user/login.cs");
}

/**
 * Logout a user.
 */
static void logout(private_user_controller_t *this, fast_request_t *request)
{
	request->redirect(request, "user/login");
	request->close_session(request);
}

/**
 * verify a user entered username for validity
 */
static bool verify_login(private_user_controller_t *this,
						 fast_request_t *request, char *login)
{
	if (!login || *login == '\0')
	{
		request->setf(request, "error=Username is missing.");
		return FALSE;
	}
	while (*login != '\0')
	{
		switch (*login)
		{
			case 'a' ... 'z':
			case 'A' ... 'Z':
			case '0' ... '9':
			case '-':
			case '_':
			case '@':
			case '.':
				login++;
				continue;
			default:
				request->setf(request, "error=Username invalid, "
							  "valid characters: A-Z a-z 0-9 - _ @ .");
		}
	}
	return TRUE;
}

/**
 * verify a user entered password for validity
 */
static bool verify_password(private_user_controller_t *this,
							fast_request_t *request,
							char *password, char *confirm)
{
	if (!password || *password == '\0')
	{
		request->setf(request, "error=Password is missing.");
		return FALSE;
	}
	if (strlen(password) < this->password_length)
	{
		request->setf(request, "error=Password requires at least %d characters.",
					  this->password_length);
		return FALSE;
	}
	if (!confirm || !streq(password, confirm))
	{
		request->setf(request, "error=Password not confirmed.");
		return FALSE;
	}
	return TRUE;
}

/**
 * Register a user.
 */
static void add(private_user_controller_t *this, fast_request_t *request)
{
	char *login = "";

	while (request->get_query_data(request, "register"))
	{
		char *password, *confirm;
		chunk_t hash;
		u_int id;

		login = request->get_query_data(request, "new_login");
		password = request->get_query_data(request, "new_password");
		confirm = request->get_query_data(request, "confirm_password");

		if (!verify_login(this, request, login) ||
			!verify_password(this, request, password, confirm))
		{
			break;
		}

		hash = hash_password(login, password);
		if (!hash.ptr || this->db->execute(this->db, &id,
							"INSERT INTO user (login, password) VALUES (?, ?)",
							DB_TEXT, login, DB_BLOB, hash) < 0)
		{
			request->setf(request, "error=Username already exists.");
			free(hash.ptr);
			break;
		}
		free(hash.ptr);
		this->user->set_user(this->user, id);
		return request->redirect(request, "peer/list");
	}
	request->set(request, "new_login", login);
	request->setf(request, "password_length=%d", this->password_length);
	request->render(request, "templates/user/add.cs");
}

/**
 * Edit the logged in user
 */
static void edit(private_user_controller_t *this, fast_request_t *request)
{
	enumerator_t *query;
	char *old_login;

	/* lookup old login */
	query = this->db->query(this->db, "SELECT login FROM user WHERE id = ?",
							DB_INT, this->user->get_user(this->user),
							DB_TEXT);
	if (!query || !query->enumerate(query, &old_login))
	{
		DESTROY_IF(query);
		request->close_session(request);
		return request->redirect(request, "user/login");
	}
	old_login = strdupa(old_login);
	query->destroy(query);

	/* back pressed */
	if (request->get_query_data(request, "back"))
	{
		return request->redirect(request, "peer/list");
	}
	/* delete pressed */
	if (request->get_query_data(request, "delete"))
	{
		this->db->execute(this->db, NULL, "DELETE FROM user WHERE id = ?",
						  DB_UINT, this->user->get_user(this->user));
		this->db->execute(this->db, NULL,
						  "DELETE FROM peer WHERE user = ?",
						  DB_UINT, this->user->get_user(this->user));
		return logout(this, request);
	}
	/* save pressed */
	while (request->get_query_data(request, "save"))
	{
		char *new_login, *old_pass, *new_pass, *confirm;
		chunk_t old_hash, new_hash;

		new_login = request->get_query_data(request, "old_login");
		old_pass = request->get_query_data(request, "old_password");
		new_pass = request->get_query_data(request, "new_password");
		confirm = request->get_query_data(request, "confirm_password");

		if (!verify_login(this, request, new_login) ||
			!verify_password(this, request, new_pass, confirm))
		{
			old_login = new_login;
			break;
		}
		old_hash = hash_password(old_login, old_pass);
		new_hash = hash_password(new_login, new_pass);

		if (this->db->execute(this->db, NULL,
			"UPDATE user SET login = ?, password = ? "
			"WHERE id = ? AND password = ?",
			DB_TEXT, new_login, DB_BLOB, new_hash,
			DB_UINT, this->user->get_user(this->user), DB_BLOB, old_hash) <= 0)
		{
			free(new_hash.ptr);
			free(old_hash.ptr);
			old_login = new_login;
			request->setf(request, "error=Password verification failed.");
			break;
		}
		free(new_hash.ptr);
		free(old_hash.ptr);
		return request->redirect(request, "peer/list");
	}
	/* on error/template rendering */
	request->set(request, "old_login", old_login);
	request->setf(request, "password_length=%d", this->password_length);
	request->render(request, "templates/user/edit.cs");
}

METHOD(fast_controller_t, get_name, char*,
	private_user_controller_t *this)
{
	return "user";
}

METHOD(fast_controller_t, handle, void,
	private_user_controller_t *this, fast_request_t *request, char *action,
	char *p2, char *p3, char *p4, char *p5)
{
	if (action)
	{
		if (streq(action, "add"))
		{
			return add(this, request);
		}
		if (streq(action, "login"))
		{
			return login(this, request);
		}
		else if (streq(action, "logout"))
		{
			return logout(this, request);
		}
		else if (streq(action, "edit"))
		{
			return edit(this, request);
		}
		else if (streq(action, "help"))
		{
			return request->render(request, "templates/user/help.cs");
		}
	}
	request->redirect(request, "user/login");
}

METHOD(fast_controller_t, destroy, void,
	private_user_controller_t *this)
{
	free(this);
}

/*
 * see header file
 */
fast_controller_t *user_controller_create(user_t *user, database_t *db)
{
	private_user_controller_t *this;

	INIT(this,
		.public = {
			.controller = {
				.get_name = _get_name,
				.handle = _handle,
				.destroy = _destroy,
			},
		},
		.user = user,
		.db = db,
		.password_length = lib->settings->get_int(lib->settings,
												  "medsrv.password_length", 6),
	);

	return &this->public.controller;
}