merged the modularization branch (credentials) back to trunk
[strongswan.git] / src / manager / controller / ikesa_controller.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include "ikesa_controller.h"
19 #include "../manager.h"
20 #include "../gateway.h"
21
22 #include <xml.h>
23
24 #include <library.h>
25
26
27 typedef struct private_ikesa_controller_t private_ikesa_controller_t;
28
29 /**
30 * private data of the task manager
31 */
32 struct private_ikesa_controller_t {
33
34 /**
35 * public functions
36 */
37 ikesa_controller_t public;
38
39 /**
40 * manager instance
41 */
42 manager_t *manager;
43 };
44
45 /**
46 * read XML of a childsa element and fill template
47 */
48 static void process_childsa(private_ikesa_controller_t *this, char *id,
49 enumerator_t *e, request_t *r)
50 {
51 xml_t *xml;
52 enumerator_t *e1, *e2;
53 char *name, *value, *reqid = "", *section = "";
54 int num = 0;
55
56 while (e->enumerate(e, &xml, &name, &value))
57 {
58 if (streq(name, "reqid"))
59 {
60 reqid = value;
61 }
62 else if (streq(name, "local") || streq(name, "remote"))
63 {
64 section = name;
65 e1 = xml->children(xml);
66 while (e1->enumerate(e1, &xml, &name, &value))
67 {
68 if (streq(name, "networks"))
69 {
70 e2 = xml->children(xml);
71 while (e2->enumerate(e2, &xml, &name, &value))
72 {
73 if (streq(name, "network"))
74 {
75 r->setf(r, "ikesas.%s.childsas.%s.%s.networks.%d=%s",
76 id, reqid, section, ++num, value);
77 }
78 }
79 e2->destroy(e2);
80 }
81 else
82 {
83 r->setf(r, "ikesas.%s.childsas.%s.%s.%s=%s",
84 id, reqid, section, name, value);
85 }
86 }
87 e1->destroy(e1);
88 }
89 else
90 {
91 r->setf(r, "ikesas.%s.childsas.%s.%s=%s",
92 id, reqid, name, value);
93 }
94 }
95 }
96
97 /**
98 * read XML of a ikesa element and fill template
99 */
100 static void process_ikesa(private_ikesa_controller_t *this,
101 enumerator_t *e, request_t *r)
102 {
103 xml_t *xml;
104 enumerator_t *e1, *e2;
105 char *name, *value, *id = "", *section = "";
106
107 while (e->enumerate(e, &xml, &name, &value))
108 {
109 if (streq(name, "id"))
110 {
111 id = value;
112 }
113 else if (streq(name, "local") || streq(name, "remote"))
114 {
115 section = name;
116 e1 = xml->children(xml);
117 while (e1->enumerate(e1, &xml, &name, &value))
118 {
119 r->setf(r, "ikesas.%s.%s.%s=%s", id, section, name, value);
120 }
121 e1->destroy(e1);
122 }
123 else if (streq(name, "childsalist"))
124 {
125 e1 = xml->children(xml);
126 while (e1->enumerate(e1, &xml, &name, &value))
127 {
128 if (streq(name, "childsa"))
129 {
130 e2 = xml->children(xml);
131 process_childsa(this, id, e2, r);
132 e2->destroy(e2);
133 }
134 }
135 e1->destroy(e1);
136 }
137 else
138 {
139 r->setf(r, "ikesas.%s.%s=%s", id, name, value);
140 }
141 }
142 }
143
144 static void list(private_ikesa_controller_t *this, request_t *r)
145 {
146 gateway_t *gateway;
147 xml_t *xml;
148 enumerator_t *e1, *e2;
149 char *name, *value;
150
151 gateway = this->manager->select_gateway(this->manager, 0);
152 e1 = gateway->query_ikesalist(gateway);
153 if (e1 == NULL)
154 {
155 r->set(r, "title", "Error");
156 r->set(r, "error", "querying the gateway failed");
157 r->render(r, "templates/error.cs");
158 }
159 else
160 {
161 r->set(r, "title", "IKE SA overview");
162
163 while (e1->enumerate(e1, &xml, &name, &value))
164 {
165 if (streq(name, "ikesa"))
166 {
167 e2 = xml->children(xml);
168 process_ikesa(this, e2, r);
169 e2->destroy(e2);
170 }
171 }
172 e1->destroy(e1);
173
174 r->render(r, "templates/ikesa/list.cs");
175 }
176 }
177
178 /**
179 * Implementation of controller_t.get_name
180 */
181 static char* get_name(private_ikesa_controller_t *this)
182 {
183 return "ikesa";
184 }
185
186 /**
187 * Implementation of controller_t.handle
188 */
189 static void handle(private_ikesa_controller_t *this,
190 request_t *request, char *action)
191 {
192 if (!this->manager->logged_in(this->manager))
193 {
194 return request->redirect(request, "auth/login");
195 }
196 if (this->manager->select_gateway(this->manager, 0) == NULL)
197 {
198 return request->redirect(request, "gateway/list");
199 }
200 if (action)
201 {
202 if (streq(action, "list"))
203 {
204 return list(this, request);
205 }
206 }
207 return request->redirect(request, "ikesa/list");
208 }
209
210 /**
211 * Implementation of controller_t.destroy
212 */
213 static void destroy(private_ikesa_controller_t *this)
214 {
215 free(this);
216 }
217
218 /*
219 * see header file
220 */
221 controller_t *ikesa_controller_create(context_t *context, void *param)
222 {
223 private_ikesa_controller_t *this = malloc_thing(private_ikesa_controller_t);
224
225 this->public.controller.get_name = (char*(*)(controller_t*))get_name;
226 this->public.controller.handle = (void(*)(controller_t*,request_t*,char*,char*,char*,char*,char*))handle;
227 this->public.controller.destroy = (void(*)(controller_t*))destroy;
228
229 this->manager = (manager_t*)context;
230
231 return &this->public.controller;
232 }
233