2 * @file config_controller.c
4 * @brief Implementation of config_controller_t.
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
23 #include "config_controller.h"
24 #include "../manager.h"
25 #include "../gateway.h"
32 typedef struct private_config_controller_t private_config_controller_t
;
35 * private data of the task manager
37 struct private_config_controller_t
{
42 config_controller_t
public;
51 * read XML of a peerconfig element and fill template
53 static void process_peerconfig(private_config_controller_t
*this,
54 enumerator_t
*e
, request_t
*r
)
57 enumerator_t
*e1
, *e2
, *e3
;
58 char *name
, *value
, *config
= "", *child
= "", *section
= "";
60 while (e
->enumerate(e
, &xml
, &name
, &value
))
62 if (streq(name
, "name"))
66 else if (streq(name
, "ikeconfig"))
68 e1
= xml
->children(xml
);
69 while (e1
->enumerate(e1
, &xml
, &name
, &value
))
71 if (streq(name
, "local") || streq(name
, "remote"))
73 r
->setf(r
, "peercfgs.%s.ikecfg.%s=%s", config
, name
, value
);
78 else if (streq(name
, "childconfiglist"))
80 e1
= xml
->children(xml
);
81 while (e1
->enumerate(e1
, &xml
, &name
, &value
))
83 if (streq(name
, "childconfig"))
87 e2
= xml
->children(xml
);
88 while (e2
->enumerate(e2
, &xml
, &name
, &value
))
90 if (streq(name
, "name"))
94 else if (streq(name
, "local") || streq(name
, "remote"))
97 e3
= xml
->children(xml
);
98 while (e3
->enumerate(e3
, &xml
, &name
, &value
))
100 if (streq(name
, "network"))
102 r
->setf(r
, "peercfgs.%s.childcfgs.%s.%s.networks.%d=%s",
103 config
, child
, section
, ++num
, value
);
116 r
->setf(r
, "peercfgs.%s.%s=%s", config
, name
, value
);
121 static void list(private_config_controller_t
*this, request_t
*r
)
125 enumerator_t
*e1
, *e2
;
128 gateway
= this->manager
->select_gateway(this->manager
, 0);
129 e1
= gateway
->query_configlist(gateway
);
132 r
->set(r
, "title", "Error");
133 r
->set(r
, "error", "querying the gateway failed");
134 r
->render(r
, "templates/error.cs");
138 r
->set(r
, "title", "Configuration overview");
140 while (e1
->enumerate(e1
, &xml
, &name
, &value
))
142 if (streq(name
, "peerconfig"))
144 e2
= xml
->children(xml
);
145 process_peerconfig(this, e2
, r
);
151 r
->render(r
, "templates/config/list.cs");
156 * Implementation of controller_t.get_name
158 static char* get_name(private_config_controller_t
*this)
164 * Implementation of controller_t.handle
166 static void handle(private_config_controller_t
*this,
167 request_t
*request
, char *action
)
169 if (!this->manager
->logged_in(this->manager
))
171 return request
->redirect(request
, "auth/login");
173 if (this->manager
->select_gateway(this->manager
, 0) == NULL
)
175 return request
->redirect(request
, "gateway/list");
179 if (streq(action
, "list"))
181 return list(this, request
);
184 return request
->redirect(request
, "config/list");
188 * Implementation of controller_t.destroy
190 static void destroy(private_config_controller_t
*this)
198 controller_t
*config_controller_create(context_t
*context
, void *param
)
200 private_config_controller_t
*this = malloc_thing(private_config_controller_t
);
202 this->public.controller
.get_name
= (char*(*)(controller_t
*))get_name
;
203 this->public.controller
.handle
= (void(*)(controller_t
*,request_t
*,char*,char*,char*,char*,char*))handle
;
204 this->public.controller
.destroy
= (void(*)(controller_t
*))destroy
;
206 this->manager
= (manager_t
*)context
;
208 return &this->public.controller
;