removed trailing spaces ([[:space:]]+$)
[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 /**
177 * Implementation of controller_t.get_name
178 */
179 static char* get_name(private_ikesa_controller_t *this)
180 {
181 return "ikesa";
182 }
183
184 /**
185 * Implementation of controller_t.handle
186 */
187 static void handle(private_ikesa_controller_t *this,
188 request_t *request, char *action)
189 {
190 if (!this->manager->logged_in(this->manager))
191 {
192 return request->redirect(request, "auth/login");
193 }
194 if (this->manager->select_gateway(this->manager, 0) == NULL)
195 {
196 return request->redirect(request, "gateway/list");
197 }
198 if (action)
199 {
200 if (streq(action, "list"))
201 {
202 return list(this, request);
203 }
204 }
205 return request->redirect(request, "ikesa/list");
206 }
207
208 /**
209 * Implementation of controller_t.destroy
210 */
211 static void destroy(private_ikesa_controller_t *this)
212 {
213 free(this);
214 }
215
216 /*
217 * see header file
218 */
219 controller_t *ikesa_controller_create(context_t *context, void *param)
220 {
221 private_ikesa_controller_t *this = malloc_thing(private_ikesa_controller_t);
222
223 this->public.controller.get_name = (char*(*)(controller_t*))get_name;
224 this->public.controller.handle = (void(*)(controller_t*,request_t*,char*,char*,char*,char*,char*))handle;
225 this->public.controller.destroy = (void(*)(controller_t*))destroy;
226
227 this->manager = (manager_t*)context;
228
229 return &this->public.controller;
230 }
231