handle right=%any case in strongSwan manager
[strongswan.git] / src / manager / controller / control_controller.c
1 /**
2 * @file control_controller.c
3 *
4 * @brief Implementation of control_controller_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
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>.
16 *
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
20 * for more details.
21 */
22
23 #include "control_controller.h"
24 #include "../manager.h"
25 #include "../gateway.h"
26
27 #include <xml.h>
28
29 #include <library.h>
30
31
32 typedef struct private_control_controller_t private_control_controller_t;
33
34 /**
35 * private data of the task manager
36 */
37 struct private_control_controller_t {
38
39 /**
40 * public functions
41 */
42 control_controller_t public;
43
44 /**
45 * manager instance
46 */
47 manager_t *manager;
48 };
49
50 /**
51 * handle the result of a control operation
52 */
53 static void handle_result(private_control_controller_t *this, request_t *r,
54 enumerator_t *e)
55 {
56 enumerator_t *e1;
57 xml_t *xml;
58 char *name, *value;
59 int num = 0;
60
61 if (e)
62 {
63 while (e->enumerate(e, &xml, &name, &value))
64 {
65 if (streq(name, "status"))
66 {
67 if (value && atoi(value) == 0)
68 {
69 r->set(r, "result", "Operation executed successfully:");
70 }
71 else
72 {
73 r->set(r, "result", "Operation failed:");
74 }
75 }
76 else if (streq(name, "log"))
77 {
78 e1 = xml->children(xml);
79 while (e1->enumerate(e1, &xml, &name, &value))
80 {
81 if (streq(name, "item"))
82 {
83 r->setf(r, "log.%d=%s", ++num, value);
84 }
85 }
86 e1->destroy(e1);
87 }
88 }
89 e->destroy(e);
90 r->render(r, "templates/control/result.cs");
91 }
92 else
93 {
94 r->set(r, "title", "Error");
95 r->set(r, "error", "controlling the gateway failed");
96 r->render(r, "templates/error.cs");
97 }
98 }
99
100 /**
101 * initiate an IKE or CHILD SA
102 */
103 static void initiate(private_control_controller_t *this, request_t *r,
104 bool ike, char *config)
105 {
106 gateway_t *gateway;
107 enumerator_t *e;
108
109 r->setf(r, "title=Establishing %s SA %s", ike ? "IKE" : "CHILD", config);
110 gateway = this->manager->select_gateway(this->manager, 0);
111 e = gateway->initiate(gateway, ike, config);
112 handle_result(this, r, e);
113 }
114
115 /**
116 * terminate an IKE or CHILD SA
117 */
118 static void terminate(private_control_controller_t *this, request_t *r,
119 bool ike, u_int32_t id)
120 {
121 gateway_t *gateway;
122 enumerator_t *e;
123
124 r->setf(r, "title=Terminate %s SA %d", ike ? "IKE" : "CHILD", id);
125 gateway = this->manager->select_gateway(this->manager, 0);
126 e = gateway->terminate(gateway, ike, id);
127 handle_result(this, r, e);
128 }
129
130 /**
131 * Implementation of controller_t.get_name
132 */
133 static char* get_name(private_control_controller_t *this)
134 {
135 return "control";
136 }
137
138 /**
139 * Implementation of controller_t.handle
140 */
141 static void handle(private_control_controller_t *this,
142 request_t *request, char *action, char *str)
143 {
144 if (!this->manager->logged_in(this->manager))
145 {
146 return request->redirect(request, "auth/login");
147 }
148 if (this->manager->select_gateway(this->manager, 0) == NULL)
149 {
150 return request->redirect(request, "gateway/list");
151 }
152 if (action)
153 {
154 u_int32_t id;
155
156 if (streq(action, "terminateike"))
157 {
158 if (str && (id = atoi(str)))
159 {
160 return terminate(this, request, TRUE, id);
161 }
162 }
163 if (streq(action, "terminatechild"))
164 {
165 if (str && (id = atoi(str)))
166 {
167 return terminate(this, request, FALSE, id);
168 }
169 }
170 if (streq(action, "initiateike"))
171 {
172 if (str)
173 {
174 return initiate(this, request, TRUE, str);
175 }
176 }
177 if (streq(action, "initiatechild"))
178 {
179 if (str)
180 {
181 return initiate(this, request, FALSE, str);
182 }
183 }
184 }
185 return request->redirect(request, "ikesa/list");
186 }
187
188 /**
189 * Implementation of controller_t.destroy
190 */
191 static void destroy(private_control_controller_t *this)
192 {
193 free(this);
194 }
195
196 /*
197 * see header file
198 */
199 controller_t *control_controller_create(context_t *context, void *param)
200 {
201 private_control_controller_t *this = malloc_thing(private_control_controller_t);
202
203 this->public.controller.get_name = (char*(*)(controller_t*))get_name;
204 this->public.controller.handle = (void(*)(controller_t*,request_t*,char*,char*,char*,char*,char*))handle;
205 this->public.controller.destroy = (void(*)(controller_t*))destroy;
206
207 this->manager = (manager_t*)context;
208
209 return &this->public.controller;
210 }
211