merged the modularization branch (credentials) back to trunk
[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 * $Id$
16 */
17
18 #define _GNU_SOURCE
19
20 #include "session.h"
21
22 #include <string.h>
23 #include <fcgiapp.h>
24 #include <stdio.h>
25
26 #include <utils/linked_list.h>
27 #include <utils/randomizer.h>
28
29 typedef struct private_session_t private_session_t;
30
31 /**
32 * private data of the task manager
33 */
34 struct private_session_t {
35
36 /**
37 * public functions
38 */
39 session_t public;
40
41 /**
42 * session ID
43 */
44 char *sid;
45
46 /**
47 * list of controller instances controller_t
48 */
49 linked_list_t *controllers;
50
51 /**
52 * list of filter instances filter_t
53 */
54 linked_list_t *filters;
55
56 /**
57 * user defined session context
58 */
59 context_t *context;
60 };
61
62 /**
63 * Implementation of session_t.add_controller.
64 */
65 static void add_controller(private_session_t *this, controller_t *controller)
66 {
67 this->controllers->insert_last(this->controllers, controller);
68 }
69
70 /**
71 * Implementation of session_t.add_filter.
72 */
73 static void add_filter(private_session_t *this, filter_t *filter)
74 {
75 this->filters->insert_last(this->filters, filter);
76 }
77
78 /**
79 * Create a session ID and a cookie
80 */
81 static void create_sid(private_session_t *this, request_t *request)
82 {
83 char buf[16];
84 chunk_t chunk = chunk_from_buf(buf);
85 randomizer_t *randomizer = randomizer_create();
86
87 randomizer->get_pseudo_random_bytes(randomizer, sizeof(buf), buf);
88 this->sid = chunk_to_hex(chunk, FALSE);
89 request->add_cookie(request, "SID", this->sid);
90 randomizer->destroy(randomizer);
91 }
92
93 /**
94 * run all registered filters
95 */
96 static bool run_filter(private_session_t *this, request_t *request,
97 controller_t *controller)
98 {
99 iterator_t *iterator;
100 filter_t *filter;
101
102 iterator = this->filters->create_iterator(this->filters, TRUE);
103 while (iterator->iterate(iterator, (void**)&filter))
104 {
105 if (!filter->run(filter, request, controller))
106 {
107 iterator->destroy(iterator);
108 return FALSE;
109 }
110 }
111 iterator->destroy(iterator);
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 iterator_t *iterator;
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 == '/') start++;
135 while ((pos = strchr(start, '/')) != NULL && i < 5)
136 {
137 param[i++] = strndup(start, pos - start);
138 start = pos + 1;
139 }
140 param[i] = strdup(start);
141 iterator = this->controllers->create_iterator(this->controllers, TRUE);
142 while (iterator->iterate(iterator, (void**)&current))
143 {
144 if (streq(current->get_name(current), param[0]))
145 {
146 if (run_filter(this, request, current))
147 {
148 current->handle(current, request, param[1], param[2],
149 param[3], param[4], param[5]);
150 handled = TRUE;
151 }
152 break;
153 }
154 }
155 iterator->destroy(iterator);
156 for (i = 0; i < 6; i++)
157 {
158 free(param[i]);
159 }
160 }
161 if (!handled)
162 {
163 if (this->controllers->get_first(this->controllers,
164 (void**)&current) == SUCCESS)
165 {
166 request->redirect(request, current->get_name(current));
167 }
168 }
169 }
170
171 /**
172 * Implementation of session_t.get_sid.
173 */
174 static char* get_sid(private_session_t *this)
175 {
176 return this->sid;
177 }
178
179 /**
180 * Implementation of session_t.destroy
181 */
182 static void destroy(private_session_t *this)
183 {
184 this->controllers->destroy_offset(this->controllers, offsetof(controller_t, destroy));
185 this->filters->destroy_offset(this->filters, offsetof(filter_t, destroy));
186 if (this->context) this->context->destroy(this->context);
187 free(this->sid);
188 free(this);
189 }
190
191 /*
192 * see header file
193 */
194 session_t *session_create(context_t *context)
195 {
196 private_session_t *this = malloc_thing(private_session_t);
197
198 this->public.add_controller = (void(*)(session_t*, controller_t*))add_controller;
199 this->public.add_filter = (void(*)(session_t*, filter_t*))add_filter;
200 this->public.process = (void(*)(session_t*,request_t*))process;
201 this->public.get_sid = (char*(*)(session_t*))get_sid;
202 this->public.destroy = (void(*)(session_t*))destroy;
203
204 this->sid = NULL;
205 this->controllers = linked_list_create();
206 this->filters = linked_list_create();
207 this->context = context;
208
209 return &this->public;
210 }
211