output of encryption algs with zero key length
[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 /**
153 * Implementation of controller_t.get_name
154 */
155 static char* get_name(private_config_controller_t *this)
156 {
157 return "config";
158 }
159
160 /**
161 * Implementation of controller_t.handle
162 */
163 static void handle(private_config_controller_t *this,
164 request_t *request, char *action)
165 {
166 if (!this->manager->logged_in(this->manager))
167 {
168 return request->redirect(request, "auth/login");
169 }
170 if (this->manager->select_gateway(this->manager, 0) == NULL)
171 {
172 return request->redirect(request, "gateway/list");
173 }
174 if (action)
175 {
176 if (streq(action, "list"))
177 {
178 return list(this, request);
179 }
180 }
181 return request->redirect(request, "config/list");
182 }
183
184 /**
185 * Implementation of controller_t.destroy
186 */
187 static void destroy(private_config_controller_t *this)
188 {
189 free(this);
190 }
191
192 /*
193 * see header file
194 */
195 controller_t *config_controller_create(context_t *context, void *param)
196 {
197 private_config_controller_t *this = malloc_thing(private_config_controller_t);
198
199 this->public.controller.get_name = (char*(*)(controller_t*))get_name;
200 this->public.controller.handle = (void(*)(controller_t*,request_t*,char*,char*,char*,char*,char*))handle;
201 this->public.controller.destroy = (void(*)(controller_t*))destroy;
202
203 this->manager = (manager_t*)context;
204
205 return &this->public.controller;
206 }
207