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 if (streq(value
, "0.0.0.0") || streq(value
, "::"))
77 r
->setf(r
, "peercfgs.%s.ikecfg.%s=%s", config
, name
, value
);
82 else if (streq(name
, "childconfiglist"))
84 e1
= xml
->children(xml
);
85 while (e1
->enumerate(e1
, &xml
, &name
, &value
))
87 if (streq(name
, "childconfig"))
91 e2
= xml
->children(xml
);
92 while (e2
->enumerate(e2
, &xml
, &name
, &value
))
94 if (streq(name
, "name"))
98 else if (streq(name
, "local") || streq(name
, "remote"))
101 e3
= xml
->children(xml
);
102 while (e3
->enumerate(e3
, &xml
, &name
, &value
))
104 if (streq(name
, "network"))
106 r
->setf(r
, "peercfgs.%s.childcfgs.%s.%s.networks.%d=%s",
107 config
, child
, section
, ++num
, value
);
120 r
->setf(r
, "peercfgs.%s.%s=%s", config
, name
, value
);
125 static void list(private_config_controller_t
*this, request_t
*r
)
129 enumerator_t
*e1
, *e2
;
132 gateway
= this->manager
->select_gateway(this->manager
, 0);
133 e1
= gateway
->query_configlist(gateway
);
136 r
->set(r
, "title", "Error");
137 r
->set(r
, "error", "querying the gateway failed");
138 r
->render(r
, "templates/error.cs");
142 r
->set(r
, "title", "Configuration overview");
144 while (e1
->enumerate(e1
, &xml
, &name
, &value
))
146 if (streq(name
, "peerconfig"))
148 e2
= xml
->children(xml
);
149 process_peerconfig(this, e2
, r
);
155 r
->render(r
, "templates/config/list.cs");
160 * Implementation of controller_t.get_name
162 static char* get_name(private_config_controller_t
*this)
168 * Implementation of controller_t.handle
170 static void handle(private_config_controller_t
*this,
171 request_t
*request
, char *action
)
173 if (!this->manager
->logged_in(this->manager
))
175 return request
->redirect(request
, "auth/login");
177 if (this->manager
->select_gateway(this->manager
, 0) == NULL
)
179 return request
->redirect(request
, "gateway/list");
183 if (streq(action
, "list"))
185 return list(this, request
);
188 return request
->redirect(request
, "config/list");
192 * Implementation of controller_t.destroy
194 static void destroy(private_config_controller_t
*this)
202 controller_t
*config_controller_create(context_t
*context
, void *param
)
204 private_config_controller_t
*this = malloc_thing(private_config_controller_t
);
206 this->public.controller
.get_name
= (char*(*)(controller_t
*))get_name
;
207 this->public.controller
.handle
= (void(*)(controller_t
*,request_t
*,char*,char*,char*,char*,char*))handle
;
208 this->public.controller
.destroy
= (void(*)(controller_t
*))destroy
;
210 this->manager
= (manager_t
*)context
;
212 return &this->public.controller
;