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