handle right=%any case in strongSwan manager
[strongswan.git] / src / manager / controller / config_controller.c
1 /**
2 * @file config_controller.c
3 *
4 * @brief Implementation of config_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 "config_controller.h"
24 #include "../manager.h"
25 #include "../gateway.h"
26
27 #include <xml.h>
28
29 #include <library.h>
30
31
32 typedef struct private_config_controller_t private_config_controller_t;
33
34 /**
35 * private data of the task manager
36 */
37 struct private_config_controller_t {
38
39 /**
40 * public functions
41 */
42 config_controller_t public;
43
44 /**
45 * manager instance
46 */
47 manager_t *manager;
48 };
49
50 /**
51 * read XML of a peerconfig element and fill template
52 */
53 static void process_peerconfig(private_config_controller_t *this,
54 enumerator_t *e, request_t *r)
55 {
56 xml_t *xml;
57 enumerator_t *e1, *e2, *e3;
58 char *name, *value, *config = "", *child = "", *section = "";
59
60 while (e->enumerate(e, &xml, &name, &value))
61 {
62 if (streq(name, "name"))
63 {
64 config = value;
65 }
66 else if (streq(name, "ikeconfig"))
67 {
68 e1 = xml->children(xml);
69 while (e1->enumerate(e1, &xml, &name, &value))
70 {
71 if (streq(name, "local") || streq(name, "remote"))
72 {
73 if (streq(value, "0.0.0.0") || streq(value, "::"))
74 {
75 value = "%any";
76 }
77 r->setf(r, "peercfgs.%s.ikecfg.%s=%s", config, name, value);
78 }
79 }
80 e1->destroy(e1);
81 }
82 else if (streq(name, "childconfiglist"))
83 {
84 e1 = xml->children(xml);
85 while (e1->enumerate(e1, &xml, &name, &value))
86 {
87 if (streq(name, "childconfig"))
88 {
89 int num = 0;
90
91 e2 = xml->children(xml);
92 while (e2->enumerate(e2, &xml, &name, &value))
93 {
94 if (streq(name, "name"))
95 {
96 child = value;
97 }
98 else if (streq(name, "local") || streq(name, "remote"))
99 {
100 section = name;
101 e3 = xml->children(xml);
102 while (e3->enumerate(e3, &xml, &name, &value))
103 {
104 if (streq(name, "network"))
105 {
106 r->setf(r, "peercfgs.%s.childcfgs.%s.%s.networks.%d=%s",
107 config, child, section, ++num, value);
108 }
109 }
110 e3->destroy(e3);
111 }
112 }
113 e2->destroy(e2);
114 }
115 }
116 e1->destroy(e1);
117 }
118 else
119 {
120 r->setf(r, "peercfgs.%s.%s=%s", config, name, value);
121 }
122 }
123 }
124
125 static void list(private_config_controller_t *this, request_t *r)
126 {
127 gateway_t *gateway;
128 xml_t *xml;
129 enumerator_t *e1, *e2;
130 char *name, *value;
131
132 gateway = this->manager->select_gateway(this->manager, 0);
133 e1 = gateway->query_configlist(gateway);
134 if (e1 == NULL)
135 {
136 r->set(r, "title", "Error");
137 r->set(r, "error", "querying the gateway failed");
138 r->render(r, "templates/error.cs");
139 }
140 else
141 {
142 r->set(r, "title", "Configuration overview");
143
144 while (e1->enumerate(e1, &xml, &name, &value))
145 {
146 if (streq(name, "peerconfig"))
147 {
148 e2 = xml->children(xml);
149 process_peerconfig(this, e2, r);
150 e2->destroy(e2);
151 }
152 }
153 e1->destroy(e1);
154
155 r->render(r, "templates/config/list.cs");
156 }
157 }
158
159 /**
160 * Implementation of controller_t.get_name
161 */
162 static char* get_name(private_config_controller_t *this)
163 {
164 return "config";
165 }
166
167 /**
168 * Implementation of controller_t.handle
169 */
170 static void handle(private_config_controller_t *this,
171 request_t *request, char *action)
172 {
173 if (!this->manager->logged_in(this->manager))
174 {
175 return request->redirect(request, "auth/login");
176 }
177 if (this->manager->select_gateway(this->manager, 0) == NULL)
178 {
179 return request->redirect(request, "gateway/list");
180 }
181 if (action)
182 {
183 if (streq(action, "list"))
184 {
185 return list(this, request);
186 }
187 }
188 return request->redirect(request, "config/list");
189 }
190
191 /**
192 * Implementation of controller_t.destroy
193 */
194 static void destroy(private_config_controller_t *this)
195 {
196 free(this);
197 }
198
199 /*
200 * see header file
201 */
202 controller_t *config_controller_create(context_t *context, void *param)
203 {
204 private_config_controller_t *this = malloc_thing(private_config_controller_t);
205
206 this->public.controller.get_name = (char*(*)(controller_t*))get_name;
207 this->public.controller.handle = (void(*)(controller_t*,request_t*,char*,char*,char*,char*,char*))handle;
208 this->public.controller.destroy = (void(*)(controller_t*))destroy;
209
210 this->manager = (manager_t*)context;
211
212 return &this->public.controller;
213 }
214