2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
16 #include "config_controller.h"
17 #include "../manager.h"
18 #include "../gateway.h"
25 typedef struct private_config_controller_t private_config_controller_t
;
28 * private data of the task manager
30 struct private_config_controller_t
{
35 config_controller_t
public;
44 * read XML of a peerconfig element and fill template
46 static void process_peerconfig(private_config_controller_t
*this,
47 enumerator_t
*e
, request_t
*r
)
50 enumerator_t
*e1
, *e2
, *e3
;
51 char *name
, *value
, *config
= "", *child
= "", *section
= "";
53 while (e
->enumerate(e
, &xml
, &name
, &value
))
55 if (streq(name
, "name"))
59 else if (streq(name
, "ikeconfig"))
61 e1
= xml
->children(xml
);
62 while (e1
->enumerate(e1
, &xml
, &name
, &value
))
64 if (streq(name
, "local") || streq(name
, "remote"))
66 if (streq(value
, "0.0.0.0") || streq(value
, "::"))
70 r
->setf(r
, "peercfgs.%s.ikecfg.%s=%s", config
, name
, value
);
75 else if (streq(name
, "childconfiglist"))
77 e1
= xml
->children(xml
);
78 while (e1
->enumerate(e1
, &xml
, &name
, &value
))
80 if (streq(name
, "childconfig"))
84 e2
= xml
->children(xml
);
85 while (e2
->enumerate(e2
, &xml
, &name
, &value
))
87 if (streq(name
, "name"))
91 else if (streq(name
, "local") || streq(name
, "remote"))
94 e3
= xml
->children(xml
);
95 while (e3
->enumerate(e3
, &xml
, &name
, &value
))
97 if (streq(name
, "network"))
99 r
->setf(r
, "peercfgs.%s.childcfgs.%s.%s.networks.%d=%s",
100 config
, child
, section
, ++num
, value
);
113 r
->setf(r
, "peercfgs.%s.%s=%s", config
, name
, value
);
118 static void list(private_config_controller_t
*this, request_t
*r
)
122 enumerator_t
*e1
, *e2
;
125 gateway
= this->manager
->select_gateway(this->manager
, 0);
126 e1
= gateway
->query_configlist(gateway
);
129 r
->set(r
, "title", "Error");
130 r
->set(r
, "error", "querying the gateway failed");
131 r
->render(r
, "templates/error.cs");
135 r
->set(r
, "title", "Configuration overview");
137 while (e1
->enumerate(e1
, &xml
, &name
, &value
))
139 if (streq(name
, "peerconfig"))
141 e2
= xml
->children(xml
);
142 process_peerconfig(this, e2
, r
);
148 r
->render(r
, "templates/config/list.cs");
153 * Implementation of controller_t.get_name
155 static char* get_name(private_config_controller_t
*this)
161 * Implementation of controller_t.handle
163 static void handle(private_config_controller_t
*this,
164 request_t
*request
, char *action
)
166 if (!this->manager
->logged_in(this->manager
))
168 return request
->redirect(request
, "auth/login");
170 if (this->manager
->select_gateway(this->manager
, 0) == NULL
)
172 return request
->redirect(request
, "gateway/list");
176 if (streq(action
, "list"))
178 return list(this, request
);
181 return request
->redirect(request
, "config/list");
185 * Implementation of controller_t.destroy
187 static void destroy(private_config_controller_t
*this)
195 controller_t
*config_controller_create(context_t
*context
, void *param
)
197 private_config_controller_t
*this = malloc_thing(private_config_controller_t
);
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
;
203 this->manager
= (manager_t
*)context
;
205 return &this->public.controller
;