Apache HTTPD
mod_session_cookie.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "mod_session.h"
18#include "apr_lib.h"
19#include "apr_strings.h"
20#include "http_log.h"
21#include "util_cookies.h"
22
23#define MOD_SESSION_COOKIE "mod_session_cookie"
24
25module AP_MODULE_DECLARE_DATA session_cookie_module;
26
30typedef struct {
31 const char *name;
33 const char *name_attrs;
34 const char *name2;
36 const char *name2_attrs;
37 int remove;
40
58{
59
61 &session_cookie_module);
62
63 /* create RFC2109 compliant cookie */
64 if (conf->name_set) {
65 if (z->encoded && z->encoded[0]) {
66 ap_cookie_write(r, conf->name, z->encoded, conf->name_attrs,
67 z->maxage, r->err_headers_out,
68 NULL);
69 }
70 else {
73 }
74 }
75
76 /* create RFC2965 compliant cookie */
77 if (conf->name2_set) {
78 if (z->encoded && z->encoded[0]) {
79 ap_cookie_write2(r, conf->name2, z->encoded, conf->name2_attrs,
80 z->maxage, r->err_headers_out,
81 NULL);
82 }
83 else {
86 }
87 }
88
89 if (conf->name_set || conf->name2_set) {
90 return OK;
91 }
92 return DECLINED;
93
94}
95
110{
111
113 &session_cookie_module);
114
116 const char *val = NULL;
117 const char *note = NULL;
118 const char *name = NULL;
119 request_rec *m = r;
120
121 /* find the first redirect */
122 while (m->prev) {
123 m = m->prev;
124 }
125 /* find the main request */
126 while (m->main) {
127 m = m->main;
128 }
129
130 /* is our session in a cookie? */
131 if (conf->name2_set) {
132 name = conf->name2;
133 }
134 else if (conf->name_set) {
135 name = conf->name;
136 }
137 else {
138 return DECLINED;
139 }
140
141 /* first look in the notes */
143 zz = (session_rec *)apr_table_get(m->notes, note);
144 if (zz) {
145 *z = zz;
146 return OK;
147 }
148
149 /* otherwise, try parse the cookie */
150 ap_cookie_read(r, name, &val, conf->remove);
151
152 /* create a new session and return it */
153 zz = (session_rec *) apr_pcalloc(m->pool, sizeof(session_rec));
154 zz->pool = m->pool;
155 zz->entries = apr_table_make(m->pool, 10);
156 zz->encoded = val;
157 *z = zz;
158
159 /* put the session in the notes so we don't have to parse it again */
160 apr_table_setn(m->notes, note, (char *)zz);
161
162 /* don't cache auth protected pages */
163 apr_table_addn(r->headers_out, "Cache-Control", "no-cache, private");
164
165 return OK;
166
167}
168
169
170
172{
175
176 return (void *) new;
177}
178
180 void *addv)
181{
186
187 new->name = (add->name_set == 0) ? base->name : add->name;
188 new->name_attrs = (add->name_set == 0) ? base->name_attrs : add->name_attrs;
189 new->name_set = add->name_set || base->name_set;
190 new->name2 = (add->name2_set == 0) ? base->name2 : add->name2;
191 new->name2_attrs = (add->name2_set == 0) ? base->name2_attrs : add->name2_attrs;
192 new->name2_set = add->name2_set || base->name2_set;
193 new->remove = (add->remove_set == 0) ? base->remove : add->remove;
194 new->remove_set = add->remove_set || base->remove_set;
195
196 return new;
197}
198
203static const char *check_string(cmd_parms * cmd, const char *string)
204{
205 if (!string || !*string || ap_strchr_c(string, '=') || ap_strchr_c(string, '&')) {
206 return apr_pstrcat(cmd->pool, cmd->directive->directive,
207 " cannot be empty, or contain '=' or '&'.",
208 NULL);
209 }
210 return NULL;
211}
212
213static const char *set_cookie_name(cmd_parms * cmd, void *config,
214 const char *args)
215{
216 char *last;
217 char *line = apr_pstrdup(cmd->pool, args);
219 char *cookie = apr_strtok(line, " \t", &last);
220 conf->name = cookie;
221 conf->name_set = 1;
222 while (apr_isspace(*last)) {
223 last++;
224 }
225 conf->name_attrs = last;
226 return check_string(cmd, cookie);
227}
228
229static const char *set_cookie_name2(cmd_parms * cmd, void *config,
230 const char *args)
231{
232 char *last;
233 char *line = apr_pstrdup(cmd->pool, args);
235 char *cookie = apr_strtok(line, " \t", &last);
236 conf->name2 = cookie;
237 conf->name2_set = 1;
238 while (apr_isspace(*last)) {
239 last++;
240 }
241 conf->name2_attrs = last;
242 return check_string(cmd, cookie);
243}
244
245static const char *
246 set_remove(cmd_parms * parms, void *dconf, int flag)
247{
248 session_cookie_dir_conf *conf = dconf;
249
250 conf->remove = flag;
251 conf->remove_set = 1;
252
253 return NULL;
254}
255
257{
259 "The name of the RFC2109 cookie carrying the session"),
261 "The name of the RFC2965 cookie carrying the session"),
262 AP_INIT_FLAG("SessionCookieRemove", set_remove, NULL, RSRC_CONF|OR_AUTHCFG,
263 "Set to 'On' to remove the session cookie from the headers "
264 "and hide the cookie from a backend server or process"),
265 {NULL}
266};
267
273
275{
277 create_session_cookie_dir_config, /* dir config creater */
278 merge_session_cookie_dir_config, /* dir merger --- default is to
279 * override */
280 NULL, /* server config */
281 NULL, /* merge server config */
282 session_cookie_cmds, /* command apr_table_t */
283 register_hooks /* register hooks */
284};
APR general purpose library routines.
APR Strings library.
#define ap_get_module_config(v, m)
#define AP_DECLARE_MODULE(foo)
#define AP_INIT_FLAG(directive, func, mconfig, where, help)
ap_conf_vector_t * base
#define AP_INIT_RAW_ARGS(directive, func, mconfig, where, help)
request_rec * r
#define DECLINED
Definition httpd.h:457
#define OK
Definition httpd.h:456
void * dummy
Definition http_vhost.h:62
#define APR_HOOK_MIDDLE
Definition apr_hooks.h:303
#define RSRC_CONF
#define OR_AUTHCFG
void ap_hook_session_save(ap_HOOK_session_save_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition mod_session.c:37
void ap_hook_session_load(ap_HOOK_session_load_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition mod_session.c:35
#define STANDARD20_MODULE_STUFF
#define ap_strchr_c(s, c)
Definition httpd.h:2353
apr_size_t size
apr_uint32_t val
Definition apr_atomic.h:66
#define apr_isspace(c)
Definition apr_lib.h:225
int apr_status_t
Definition apr_errno.h:44
const char apr_int32_t flag
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
const char char ** last
const void * m
apr_cmdtype_e cmd
const char const char *const * args
Apache Logging library.
apr_pool_t * p
Definition md_event.c:32
Session Module for Apache.
return NULL
Definition mod_so.c:359
char * name
A structure that represents the current request.
Definition httpd.h:845
request_rec * prev
Definition httpd.h:856
apr_table_t * err_headers_out
Definition httpd.h:981
request_rec * main
Definition httpd.h:860
struct ap_conf_vector_t * per_dir_config
Definition httpd.h:1047
apr_table_t * headers_out
Definition httpd.h:978
Apache cookie library.