manager can query and list IKE_SA status (no layout yet)
[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->render(t, response);
55 t->destroy(t);
56 }
57
58 static void check(private_auth_controller_t *this,
59 request_t *request, response_t *response)
60 {
61 char *username, *password;
62
63 username = request->get_post_data(request, "username");
64 password = request->get_post_data(request, "password");
65 if (username && password &&
66 this->manager->login(this->manager, username, password))
67 {
68 response->redirect(response, "status/test");
69 }
70 else
71 {
72 response->redirect(response, "auth/login");
73 }
74 }
75
76 static void logout(private_auth_controller_t *this,
77 request_t *request, response_t *response)
78 {
79 this->manager->logout(this->manager);
80 response->redirect(response, "auth/login");
81 }
82
83 /**
84 * Implementation of controller_t.get_name
85 */
86 static char* get_name(private_auth_controller_t *this)
87 {
88 return "auth";
89 }
90
91 /**
92 * Implementation of controller_t.get_handler
93 */
94 static controller_handler_t get_handler(private_auth_controller_t *this, char *name)
95 {
96 if (streq(name, "login")) return (controller_handler_t)login;
97 if (streq(name, "check")) return (controller_handler_t)check;
98 if (streq(name, "logout")) return (controller_handler_t)logout;
99 return NULL;
100 }
101
102 /**
103 * Implementation of controller_t.destroy
104 */
105 static void destroy(private_auth_controller_t *this)
106 {
107 free(this);
108 }
109
110 /*
111 * see header file
112 */
113 controller_t *auth_controller_create(context_t *context, void *param)
114 {
115 private_auth_controller_t *this = malloc_thing(private_auth_controller_t);
116
117 this->public.controller.get_name = (char*(*)(controller_t*))get_name;
118 this->public.controller.get_handler = (controller_handler_t(*)(controller_t*,char*))get_handler;
119 this->public.controller.destroy = (void(*)(controller_t*))destroy;
120
121 this->manager = (manager_t*)context;
122
123 return &this->public.controller;
124 }
125