Merge branch 'stroke-timeout'
[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
16 #include "ikesa_controller.h"
17 #include "../manager.h"
18 #include "../gateway.h"
19
20 #include <xml.h>
21
22 #include <library.h>
23
24
25 typedef struct private_ikesa_controller_t private_ikesa_controller_t;
26
27 /**
28 * private data of the task manager
29 */
30 struct private_ikesa_controller_t {
31
32 /**
33 * public functions
34 */
35 ikesa_controller_t public;
36
37 /**
38 * manager instance
39 */
40 manager_t *manager;
41 };
42
43 /**
44 * read XML of a childsa element and fill template
45 */
46 static void process_childsa(private_ikesa_controller_t *this, char *id,
47 enumerator_t *e, request_t *r)
48 {
49 xml_t *xml;
50 enumerator_t *e1, *e2;
51 char *name, *value, *reqid = "", *section = "";
52 int num = 0;
53
54 while (e->enumerate(e, &xml, &name, &value))
55 {
56 if (streq(name, "reqid"))
57 {
58 reqid = value;
59 }
60 else if (streq(name, "local") || streq(name, "remote"))
61 {
62 section = name;
63 e1 = xml->children(xml);
64 while (e1->enumerate(e1, &xml, &name, &value))
65 {
66 if (streq(name, "networks"))
67 {
68 e2 = xml->children(xml);
69 while (e2->enumerate(e2, &xml, &name, &value))
70 {
71 if (streq(name, "network"))
72 {
73 r->setf(r, "ikesas.%s.childsas.%s.%s.networks.%d=%s",
74 id, reqid, section, ++num, value);
75 }
76 }
77 e2->destroy(e2);
78 }
79 else
80 {
81 r->setf(r, "ikesas.%s.childsas.%s.%s.%s=%s",
82 id, reqid, section, name, value);
83 }
84 }
85 e1->destroy(e1);
86 }
87 else
88 {
89 r->setf(r, "ikesas.%s.childsas.%s.%s=%s",
90 id, reqid, name, value);
91 }
92 }
93 }
94
95 /**
96 * read XML of a ikesa element and fill template
97 */
98 static void process_ikesa(private_ikesa_controller_t *this,
99 enumerator_t *e, request_t *r)
100 {
101 xml_t *xml;
102 enumerator_t *e1, *e2;
103 char *name, *value, *id = "", *section = "";
104
105 while (e->enumerate(e, &xml, &name, &value))
106 {
107 if (streq(name, "id"))
108 {
109 id = value;
110 }
111 else if (streq(name, "local") || streq(name, "remote"))
112 {
113 section = name;
114 e1 = xml->children(xml);
115 while (e1->enumerate(e1, &xml, &name, &value))
116 {
117 r->setf(r, "ikesas.%s.%s.%s=%s", id, section, name, value);
118 }
119 e1->destroy(e1);
120 }
121 else if (streq(name, "childsalist"))
122 {
123 e1 = xml->children(xml);
124 while (e1->enumerate(e1, &xml, &name, &value))
125 {
126 if (streq(name, "childsa"))
127 {
128 e2 = xml->children(xml);
129 process_childsa(this, id, e2, r);
130 e2->destroy(e2);
131 }
132 }
133 e1->destroy(e1);
134 }
135 else
136 {
137 r->setf(r, "ikesas.%s.%s=%s", id, name, value);
138 }
139 }
140 }
141
142 static void list(private_ikesa_controller_t *this, request_t *r)
143 {
144 gateway_t *gateway;
145 xml_t *xml;
146 enumerator_t *e1, *e2;
147 char *name, *value;
148
149 gateway = this->manager->select_gateway(this->manager, 0);
150 e1 = gateway->query_ikesalist(gateway);
151 if (e1 == NULL)
152 {
153 r->set(r, "title", "Error");
154 r->set(r, "error", "querying the gateway failed");
155 r->render(r, "templates/error.cs");
156 }
157 else
158 {
159 r->set(r, "title", "IKE SA overview");
160
161 while (e1->enumerate(e1, &xml, &name, &value))
162 {
163 if (streq(name, "ikesa"))
164 {
165 e2 = xml->children(xml);
166 process_ikesa(this, e2, r);
167 e2->destroy(e2);
168 }
169 }
170 e1->destroy(e1);
171
172 r->render(r, "templates/ikesa/list.cs");
173 }
174 }
175
176 METHOD(controller_t, get_name, char*,
177 private_ikesa_controller_t *this)
178 {
179 return "ikesa";
180 }
181
182 METHOD(controller_t, handle, void,
183 private_ikesa_controller_t *this, request_t *request, char *action,
184 char *p2, char *p3, char *p4, char *p5)
185 {
186 if (!this->manager->logged_in(this->manager))
187 {
188 return request->redirect(request, "auth/login");
189 }
190 if (this->manager->select_gateway(this->manager, 0) == NULL)
191 {
192 return request->redirect(request, "gateway/list");
193 }
194 if (action)
195 {
196 if (streq(action, "list"))
197 {
198 return list(this, request);
199 }
200 }
201 return request->redirect(request, "ikesa/list");
202 }
203
204 METHOD(controller_t, destroy, void,
205 private_ikesa_controller_t *this)
206 {
207 free(this);
208 }
209
210 /*
211 * see header file
212 */
213 controller_t *ikesa_controller_create(context_t *context, void *param)
214 {
215 private_ikesa_controller_t *this;
216
217 INIT(this,
218 .public = {
219 .controller = {
220 .get_name = _get_name,
221 .handle = _handle,
222 .destroy = _destroy,
223 },
224 },
225 .manager = (manager_t*)context,
226 );
227
228 return &this->public.controller;
229 }
230