the option has been changed to --outform
[strongswan.git] / src / libfast / session.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #define _GNU_SOURCE
17
18 #include "session.h"
19
20 #include <string.h>
21 #include <fcgiapp.h>
22 #include <stdio.h>
23
24 #include <utils/linked_list.h>
25
26 typedef struct private_session_t private_session_t;
27
28 /**
29 * private data of the task manager
30 */
31 struct private_session_t {
32
33 /**
34 * public functions
35 */
36 session_t public;
37
38 /**
39 * session ID
40 */
41 char *sid;
42
43 /**
44 * list of controller instances controller_t
45 */
46 linked_list_t *controllers;
47
48 /**
49 * list of filter instances filter_t
50 */
51 linked_list_t *filters;
52
53 /**
54 * user defined session context
55 */
56 context_t *context;
57 };
58
59 /**
60 * Implementation of session_t.add_controller.
61 */
62 static void add_controller(private_session_t *this, controller_t *controller)
63 {
64 this->controllers->insert_last(this->controllers, controller);
65 }
66
67 /**
68 * Implementation of session_t.add_filter.
69 */
70 static void add_filter(private_session_t *this, filter_t *filter)
71 {
72 this->filters->insert_last(this->filters, filter);
73 }
74
75 /**
76 * Create a session ID and a cookie
77 */
78 static void create_sid(private_session_t *this, request_t *request)
79 {
80 char buf[16];
81 chunk_t chunk = chunk_from_buf(buf);
82 rng_t *rng;
83
84 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
85 if (rng)
86 {
87 rng->get_bytes(rng, sizeof(buf), buf);
88 this->sid = chunk_to_hex(chunk, NULL, FALSE).ptr;
89 request->add_cookie(request, "SID", this->sid);
90 rng->destroy(rng);
91 }
92 }
93
94 /**
95 * run all registered filters
96 */
97 static bool run_filter(private_session_t *this, request_t *request, char *p0,
98 char *p1, char *p2, char *p3, char *p4, char *p5)
99 {
100 enumerator_t *enumerator;
101 filter_t *filter;
102
103 enumerator = this->filters->create_enumerator(this->filters);
104 while (enumerator->enumerate(enumerator, &filter))
105 {
106 if (!filter->run(filter, request, p0, p1, p2, p3, p4, p5))
107 {
108 enumerator->destroy(enumerator);
109 return FALSE;
110 }
111 }
112 enumerator->destroy(enumerator);
113 return TRUE;
114 }
115
116 /**
117 * Implementation of session_t.process.
118 */
119 static void process(private_session_t *this, request_t *request)
120 {
121 char *pos, *start, *param[6] = {NULL, NULL, NULL, NULL, NULL, NULL};
122 enumerator_t *enumerator;
123 bool handled = FALSE;
124 controller_t *current;
125 int i = 0;
126
127 if (this->sid == NULL)
128 {
129 create_sid(this, request);
130 }
131
132 start = request->get_path(request);
133 if (start)
134 {
135 if (*start == '/')
136 {
137 start++;
138 }
139 while ((pos = strchr(start, '/')) != NULL && i < 5)
140 {
141 param[i++] = strndupa(start, pos - start);
142 start = pos + 1;
143 }
144 param[i] = strdupa(start);
145
146 if (run_filter(this, request, param[0], param[1], param[2], param[3],
147 param[4], param[5]))
148 {
149 enumerator = this->controllers->create_enumerator(this->controllers);
150 while (enumerator->enumerate(enumerator, &current))
151 {
152 if (streq(current->get_name(current), param[0]))
153 {
154 current->handle(current, request, param[1], param[2],
155 param[3], param[4], param[5]);
156 handled = TRUE;
157 break;
158 }
159 }
160 enumerator->destroy(enumerator);
161 }
162 else
163 {
164 handled = TRUE;
165 }
166 }
167 if (!handled)
168 {
169 if (this->controllers->get_first(this->controllers,
170 (void**)&current) == SUCCESS)
171 {
172 request->redirect(request, current->get_name(current));
173 }
174 }
175 }
176
177 /**
178 * Implementation of session_t.get_sid.
179 */
180 static char* get_sid(private_session_t *this)
181 {
182 return this->sid;
183 }
184
185 /**
186 * Implementation of session_t.destroy
187 */
188 static void destroy(private_session_t *this)
189 {
190 this->controllers->destroy_offset(this->controllers, offsetof(controller_t, destroy));
191 this->filters->destroy_offset(this->filters, offsetof(filter_t, destroy));
192 DESTROY_IF(this->context);
193 free(this->sid);
194 free(this);
195 }
196
197 /*
198 * see header file
199 */
200 session_t *session_create(context_t *context)
201 {
202 private_session_t *this = malloc_thing(private_session_t);
203
204 this->public.add_controller = (void(*)(session_t*, controller_t*))add_controller;
205 this->public.add_filter = (void(*)(session_t*, filter_t*))add_filter;
206 this->public.process = (void(*)(session_t*,request_t*))process;
207 this->public.get_sid = (char*(*)(session_t*))get_sid;
208 this->public.destroy = (void(*)(session_t*))destroy;
209
210 this->sid = NULL;
211 this->controllers = linked_list_create();
212 this->filters = linked_list_create();
213 this->context = context;
214
215 return &this->public;
216 }
217