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