added subnets of CHILD_SAs to xml interface
[strongswan.git] / src / manager / controller / auth_controller.c
1 /**
2 * @file auth_controller.c
3 *
4 * @brief Implementation of auth_controller_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "auth_controller.h"
24 #include "../manager.h"
25
26 #include <template.h>
27
28 #include <library.h>
29
30
31 typedef struct private_auth_controller_t private_auth_controller_t;
32
33 /**
34 * private data of the task manager
35 */
36 struct private_auth_controller_t {
37
38 /**
39 * public functions
40 */
41 auth_controller_t public;
42
43 /**
44 * manager instance
45 */
46 manager_t *manager;
47 };
48
49 static void login(private_auth_controller_t *this,
50 request_t *request, response_t *response)
51 {
52 template_t *t = template_create("templates/auth/login.cs");
53 t->set(t, "action", "check");
54 t->set(t, "title", "Login");
55 t->render(t, response);
56 t->destroy(t);
57 }
58
59 static void check(private_auth_controller_t *this,
60 request_t *request, response_t *response)
61 {
62 char *username, *password;
63
64 username = request->get_post_data(request, "username");
65 password = request->get_post_data(request, "password");
66 if (username && password &&
67 this->manager->login(this->manager, username, password))
68 {
69 response->redirect(response, "status/ikesalist");
70 }
71 else
72 {
73 response->redirect(response, "auth/login");
74 }
75 }
76
77 static void logout(private_auth_controller_t *this,
78 request_t *request, response_t *response)
79 {
80 this->manager->logout(this->manager);
81 response->redirect(response, "auth/login");
82 }
83
84 /**
85 * Implementation of controller_t.get_name
86 */
87 static char* get_name(private_auth_controller_t *this)
88 {
89 return "auth";
90 }
91
92 /**
93 * Implementation of controller_t.handle
94 */
95 static void handle(private_auth_controller_t *this,
96 request_t *request, response_t *response, char *action)
97 {
98 if (action)
99 {
100 if (streq(action, "login"))
101 {
102 return login(this, request, response);
103 }
104 else if (streq(action, "check"))
105 {
106 return check(this, request, response);
107 }
108 else if (streq(action, "logout"))
109 {
110 return logout(this, request, response);
111 }
112 }
113 response->redirect(response, "auth/login");
114 }
115
116 /**
117 * Implementation of controller_t.destroy
118 */
119 static void destroy(private_auth_controller_t *this)
120 {
121 free(this);
122 }
123
124 /*
125 * see header file
126 */
127 controller_t *auth_controller_create(context_t *context, void *param)
128 {
129 private_auth_controller_t *this = malloc_thing(private_auth_controller_t);
130
131 this->public.controller.get_name = (char*(*)(controller_t*))get_name;
132 this->public.controller.handle = (void(*)(controller_t*,request_t*,response_t*,char*,char*,char*,char*,char*))handle;
133 this->public.controller.destroy = (void(*)(controller_t*))destroy;
134
135 this->manager = (manager_t*)context;
136
137 return &this->public.controller;
138 }
139