manager can query and list IKE_SA status (no layout yet)
[strongswan.git] / src / manager / controller / status_controller.c
1 /**
2 * @file status_controller.c
3 *
4 * @brief Implementation of status_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 "status_controller.h"
24 #include "../manager.h"
25 #include "../gateway.h"
26
27 #include <template.h>
28 #include <xml.h>
29
30 #include <library.h>
31
32
33 typedef struct private_status_controller_t private_status_controller_t;
34
35 /**
36 * private data of the task manager
37 */
38 struct private_status_controller_t {
39
40 /**
41 * public functions
42 */
43 status_controller_t public;
44
45 /**
46 * manager instance
47 */
48 manager_t *manager;
49 };
50
51 static void ikesalist(private_status_controller_t *this,
52 request_t *request, response_t *response)
53 {
54 char *str;
55 gateway_t *gateway;
56 xml_t *doc, *node;
57 enumerator_t *e1, *e2, *e3, *e4, *e5, *e6;
58 char *name, *value, *id, *section;
59
60 gateway = this->manager->select_gateway(this->manager, 0);
61 str = gateway->request(gateway, "<message type=\"request\" id=\"1\">"
62 "<query>"
63 "<ikesalist/>"
64 "</query>"
65 "</message>");
66 if (str == NULL)
67 {
68 response->printf(response, "gateway did not respond");
69 return;
70 }
71
72 doc = xml_create(str);
73 if (doc == NULL)
74 {
75 response->printf(response, "parsing XML failed");
76 return;
77 }
78
79 template_t *t = template_create("templates/status/ikesalist.cs");
80
81 e1 = doc->children(doc);
82 while (e1->enumerate(e1, &node, &name, &value))
83 {
84 if (streq(name, "message"))
85 {
86 e2 = node->children(node);
87 while (e2->enumerate(e2, &node, &name, &value))
88 {
89 if (streq(name, "query"))
90 {
91 e3 = node->children(node);
92 while (e3->enumerate(e3, &node, &name, &value))
93 {
94 if (streq(name, "ikesalist"))
95 {
96 e4 = node->children(node);
97 while (e4->enumerate(e4, &node, &name, &value))
98 {
99 if (streq(name, "ikesa"))
100 {
101 e5 = node->children(node);
102 while (e5->enumerate(e5, &node, &name, &value))
103 {
104 if (streq(name, "id"))
105 {
106 id = value;
107 }
108 else if(streq(name, "local") ||
109 streq(name, "remote"))
110 {
111 section = name;
112 e6 = node->children(node);
113 while (e6->enumerate(e6, &node, &name, &value))
114 {
115 t->setf(t, "ikesas.%s.%s.%s=%s", id, section, name, value);
116 }
117 e6->destroy(e6);
118 }
119 else
120 {
121 t->setf(t, "ikesas.%s.%s=%s", id, name, value);
122 }
123 }
124 e5->destroy(e5);
125 }
126 }
127 e4->destroy(e4);
128 }
129 }
130 e3->destroy(e3);
131 }
132 }
133 e2->destroy(e2);
134 }
135 }
136 e1->destroy(e1);
137
138 t->render(t, response);
139 t->destroy(t);
140 free(str);
141 }
142
143 /**
144 * redirect to authentication login
145 */
146 static void login(private_status_controller_t *this,
147 request_t *request, response_t *response)
148 {
149 response->redirect(response, "auth/login");
150 }
151
152 /**
153 * redirect to gateway selection
154 */
155 static void selection(private_status_controller_t *this,
156 request_t *request, response_t *response)
157 {
158 response->redirect(response, "gateway/list");
159 }
160
161 /**
162 * Implementation of controller_t.get_name
163 */
164 static char* get_name(private_status_controller_t *this)
165 {
166 return "status";
167 }
168
169 /**
170 * Implementation of controller_t.get_handler
171 */
172 static controller_handler_t get_handler(private_status_controller_t *this, char *name)
173 {
174 if (!this->manager->logged_in(this->manager)) return (controller_handler_t)login;
175 if (this->manager->select_gateway(this->manager, 0) == NULL) return (controller_handler_t)selection;
176 if (streq(name, "ikesalist")) return (controller_handler_t)ikesalist;
177 return NULL;
178 }
179
180 /**
181 * Implementation of controller_t.destroy
182 */
183 static void destroy(private_status_controller_t *this)
184 {
185 free(this);
186 }
187
188 /*
189 * see header file
190 */
191 controller_t *status_controller_create(context_t *context, void *param)
192 {
193 private_status_controller_t *this = malloc_thing(private_status_controller_t);
194
195 this->public.controller.get_name = (char*(*)(controller_t*))get_name;
196 this->public.controller.get_handler = (controller_handler_t(*)(controller_t*,char*))get_handler;
197 this->public.controller.destroy = (void(*)(controller_t*))destroy;
198
199 this->manager = (manager_t*)context;
200
201 return &this->public.controller;
202 }
203