Hand out shared secret of load tester for all identities
[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 rng_t *rng;
82
83 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
84 if (rng)
85 {
86 rng->get_bytes(rng, sizeof(buf), buf);
87 this->sid = chunk_to_hex(chunk_create(buf, sizeof(buf)), NULL, FALSE).ptr;
88 request->add_cookie(request, "SID", this->sid);
89 rng->destroy(rng);
90 }
91 }
92
93 /**
94 * run all registered filters
95 */
96 static bool run_filter(private_session_t *this, request_t *request, char *p0,
97 char *p1, char *p2, char *p3, char *p4, char *p5)
98 {
99 enumerator_t *enumerator;
100 filter_t *filter;
101
102 enumerator = this->filters->create_enumerator(this->filters);
103 while (enumerator->enumerate(enumerator, &filter))
104 {
105 if (!filter->run(filter, request, p0, p1, p2, p3, p4, p5))
106 {
107 enumerator->destroy(enumerator);
108 return FALSE;
109 }
110 }
111 enumerator->destroy(enumerator);
112 return TRUE;
113 }
114
115 /**
116 * Implementation of session_t.process.
117 */
118 static void process(private_session_t *this, request_t *request)
119 {
120 char *pos, *start, *param[6] = {NULL, NULL, NULL, NULL, NULL, NULL};
121 enumerator_t *enumerator;
122 bool handled = FALSE;
123 controller_t *current;
124 int i = 0;
125
126 if (this->sid == NULL)
127 {
128 create_sid(this, request);
129 }
130
131 start = request->get_path(request);
132 if (start)
133 {
134 if (*start == '/')
135 {
136 start++;
137 }
138 while ((pos = strchr(start, '/')) != NULL && i < 5)
139 {
140 param[i++] = strndupa(start, pos - start);
141 start = pos + 1;
142 }
143 param[i] = strdupa(start);
144
145 if (run_filter(this, request, param[0], param[1], param[2], param[3],
146 param[4], param[5]))
147 {
148 enumerator = this->controllers->create_enumerator(this->controllers);
149 while (enumerator->enumerate(enumerator, &current))
150 {
151 if (streq(current->get_name(current), param[0]))
152 {
153 current->handle(current, request, param[1], param[2],
154 param[3], param[4], param[5]);
155 handled = TRUE;
156 break;
157 }
158 }
159 enumerator->destroy(enumerator);
160 }
161 else
162 {
163 handled = TRUE;
164 }
165 }
166 if (!handled)
167 {
168 if (this->controllers->get_first(this->controllers,
169 (void**)&current) == SUCCESS)
170 {
171 request->redirect(request, current->get_name(current));
172 }
173 }
174 }
175
176 /**
177 * Implementation of session_t.get_sid.
178 */
179 static char* get_sid(private_session_t *this)
180 {
181 return this->sid;
182 }
183
184 /**
185 * Implementation of session_t.destroy
186 */
187 static void destroy(private_session_t *this)
188 {
189 this->controllers->destroy_offset(this->controllers, offsetof(controller_t, destroy));
190 this->filters->destroy_offset(this->filters, offsetof(filter_t, destroy));
191 DESTROY_IF(this->context);
192 free(this->sid);
193 free(this);
194 }
195
196 /*
197 * see header file
198 */
199 session_t *session_create(context_t *context)
200 {
201 private_session_t *this = malloc_thing(private_session_t);
202
203 this->public.add_controller = (void(*)(session_t*, controller_t*))add_controller;
204 this->public.add_filter = (void(*)(session_t*, filter_t*))add_filter;
205 this->public.process = (void(*)(session_t*,request_t*))process;
206 this->public.get_sid = (char*(*)(session_t*))get_sid;
207 this->public.destroy = (void(*)(session_t*))destroy;
208
209 this->sid = NULL;
210 this->controllers = linked_list_create();
211 this->filters = linked_list_create();
212 this->context = context;
213
214 return &this->public;
215 }
216