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