merging kernel_pfkey plugin back from kernel-interface branch
[strongswan.git] / src / medsrv / filter / auth_filter.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Copyright (C) 2008 Philip Boetschi, Adrian Doerig
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 #include "auth_filter.h"
20
21 #include <debug.h>
22
23 typedef struct private_auth_filter_t private_auth_filter_t;
24
25 /**
26 * private data of auth_filter
27 */
28 struct private_auth_filter_t {
29 /**
30 * public functions
31 */
32 auth_filter_t public;
33
34 /**
35 * user session
36 */
37 user_t *user;
38
39 /**
40 * database connection
41 */
42 database_t *db;
43 };
44
45 /**
46 * Implementation of filter_t.run
47 */
48 static bool run(private_auth_filter_t *this, request_t *request,
49 char *controller, char *action)
50 {
51 if (this->user->get_user(this->user))
52 {
53 enumerator_t *query;
54 char *login;
55
56 query = this->db->query(this->db, "SELECT login FROM user WHERE id = ?",
57 DB_INT, this->user->get_user(this->user),
58 DB_TEXT);
59 if (query && query->enumerate(query, &login))
60 {
61 request->set(request, "login", login);
62 query->destroy(query);
63 return TRUE;
64 }
65 DESTROY_IF(query);
66 this->user->set_user(this->user, 0);
67 }
68 if (controller && streq(controller, "user") && action &&
69 (streq(action, "add") || streq(action, "login") || streq(action, "help")))
70 { /* add/login allowed */
71 return TRUE;
72 }
73 request->redirect(request, "user/login");
74 return FALSE;
75 }
76
77 /**
78 * Implementation of filter_t.destroy
79 */
80 static void destroy(private_auth_filter_t *this)
81 {
82 free(this);
83 }
84
85 /*
86 * see header file
87 */
88 filter_t *auth_filter_create(user_t *user, database_t *db)
89 {
90 private_auth_filter_t *this= malloc_thing(private_auth_filter_t);
91
92 this->public.filter.destroy = (void(*)(filter_t*))destroy;
93 this->public.filter.run = (bool(*)(filter_t*, request_t*,char*,char*,char*,char*,char*,char*))run;
94
95 this->user = user;
96 this->db = db;
97
98 return &this->public.filter;
99 }
100