removed colons from session cookie
[strongswan.git] / src / manager / lib / session.c
1 /**
2 * @file session.c
3 *
4 * @brief Implementation of session_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 #define _GNU_SOURCE
24
25 #include "session.h"
26
27 #include <string.h>
28 #include <fcgiapp.h>
29 #include <stdio.h>
30
31 #include <utils/linked_list.h>
32 #include <utils/randomizer.h>
33
34 typedef struct private_session_t private_session_t;
35
36 /**
37 * private data of the task manager
38 */
39 struct private_session_t {
40
41 /**
42 * public functions
43 */
44 session_t public;
45
46 /**
47 * session ID
48 */
49 char *sid;
50
51 /**
52 * list of controller instances controller_t
53 */
54 linked_list_t *controllers;
55
56 /**
57 * user defined session context
58 */
59 context_t *context;
60 };
61
62 /**
63 * Implementation of session_t.load_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 * Create a session ID and a cookie
72 */
73 static void create_sid(private_session_t *this, request_t *request)
74 {
75 char buf[16];
76 chunk_t chunk = chunk_from_buf(buf);
77 randomizer_t *randomizer = randomizer_create();
78
79 randomizer->get_pseudo_random_bytes(randomizer, sizeof(buf), buf);
80 this->sid = chunk_to_hex(chunk, FALSE);
81 request->add_cookie(request, "SID", this->sid);
82 randomizer->destroy(randomizer);
83 }
84
85 /**
86 * Implementation of session_t.process.
87 */
88 static void process(private_session_t *this, request_t *request)
89 {
90 char *pos, *path, *controller, *action;
91 iterator_t *iterator;
92 bool handled = FALSE;
93 controller_t *current;
94
95 if (this->sid == NULL)
96 {
97 create_sid(this, request);
98 }
99
100 path = request->get_path(request);
101 if (*path == '/') path++;
102 pos = strchr(path, '/');
103 if (pos == NULL)
104 {
105 controller = strdup(path);
106 action = strdup("");
107 }
108 else
109 {
110 controller = strndup(path, pos - path);
111 path = pos + 1;
112 pos = strchr(path, '/');
113 if (pos == NULL)
114 {
115 action = strdup(path);
116 }
117 else
118 {
119 action = strndup(path, pos - path);
120 }
121 }
122 iterator = this->controllers->create_iterator(this->controllers, TRUE);
123 while (iterator->iterate(iterator, (void**)&current))
124 {
125 if (streq(current->get_name(current), controller))
126 {
127 current->handle(current, request, action, NULL, NULL, NULL, NULL);
128 handled = TRUE;
129 break;
130 }
131 }
132 iterator->destroy(iterator);
133 free(controller);
134 free(action);
135 if (!handled)
136 {
137 if (this->controllers->get_first(this->controllers,
138 (void**)&current) == SUCCESS)
139 {
140 request->redirect(request, current->get_name(current));
141 }
142 }
143 }
144
145 /**
146 * Implementation of session_t.get_sid.
147 */
148 static char* get_sid(private_session_t *this)
149 {
150 return this->sid;
151 }
152
153 /**
154 * Implementation of session_t.destroy
155 */
156 static void destroy(private_session_t *this)
157 {
158 this->controllers->destroy_offset(this->controllers, offsetof(controller_t, destroy));
159 if (this->context) this->context->destroy(this->context);
160 free(this->sid);
161 free(this);
162 }
163
164 /*
165 * see header file
166 */
167 session_t *session_create(context_t *context)
168 {
169 private_session_t *this = malloc_thing(private_session_t);
170
171 this->public.add_controller = (void(*)(session_t*, controller_t*))add_controller;
172 this->public.process = (void(*)(session_t*,request_t*))process;
173 this->public.get_sid = (char*(*)(session_t*))get_sid;
174 this->public.destroy = (void(*)(session_t*))destroy;
175
176 this->sid = NULL;
177 this->controllers = linked_list_create();
178 this->context = context;
179
180 return &this->public;
181 }
182