]> granicus.if.org Git - apache/blob - server/util_cookies.c
mod_session_cookie: Add a session implementation capable of storing
[apache] / server / util_cookies.c
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 #define CORE_PRIVATE
18
19 #include "util_cookies.h"
20 #include "apr_lib.h"
21 #include "apr_strings.h"
22 #include "http_log.h"
23
24 #define LOG_PREFIX "ap_cookie: "
25
26 /**
27  * Write an RFC2109 compliant cookie.
28  *
29  * @param r The request
30  * @param name The name of the cookie.
31  * @param val The value to place in the cookie.
32  * @param attrs The string containing additional cookie attributes. If NULL, the
33  *              DEFAULT_ATTRS will be used.
34  * @param maxage If non zero, a Max-Age header will be added to the cookie.
35  */
36 AP_DECLARE(apr_status_t) ap_cookie_write(request_rec * r, const char *name, const char *val,
37                                          const char *attrs, long maxage)
38 {
39
40     char *buffer;
41
42     /* handle expiry */
43     buffer = "";
44     if (maxage) {
45         buffer = apr_pstrcat(r->pool, "Max-Age=", apr_ltoa(r->pool, maxage), ";", NULL);
46     }
47
48     /* create RFC2109 compliant cookie */
49     char *rfc2109 = apr_pstrcat(r->pool, name, "=", val, ";",
50                                 buffer,
51                                 attrs && strlen(attrs) > 0 ?
52                                 attrs : DEFAULT_ATTRS, NULL);
53     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOG_PREFIX
54                   "user '%s' set cookie: '%s'", r->user, rfc2109);
55     apr_table_addn(r->headers_out, SET_COOKIE, rfc2109);
56
57     return APR_SUCCESS;
58
59 }
60
61 /**
62  * Write an RFC2965 compliant cookie.
63  *
64  * @param r The request
65  * @param name2 The name of the cookie.
66  * @param val The value to place in the cookie.
67  * @param attrs2 The string containing additional cookie attributes. If NULL, the
68  *               DEFAULT_ATTRS will be used.
69  * @param maxage If non zero, a Max-Age header will be added to the cookie.
70  */
71 AP_DECLARE(apr_status_t) ap_cookie_write2(request_rec * r, const char *name2, const char *val,
72                                           const char *attrs2, long maxage)
73 {
74
75     char *buffer;
76
77     /* handle expiry */
78     buffer = "";
79     if (maxage) {
80         buffer = apr_pstrcat(r->pool, "Max-Age=", apr_ltoa(r->pool, maxage), ";");
81     }
82
83     /* create RFC2965 compliant cookie */
84     char *rfc2965 = apr_pstrcat(r->pool, name2, "=", val, ";",
85                                 buffer,
86                                 attrs2 && strlen(attrs2) > 0 ?
87                                 attrs2 : DEFAULT_ATTRS, NULL);
88     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOG_PREFIX
89                   "user '%s' set cookie2: '%s'", r->user, rfc2965);
90     apr_table_addn(r->headers_out, SET_COOKIE2, rfc2965);
91
92     return APR_SUCCESS;
93
94 }
95
96 /**
97  * Remove an RFC2109 compliant cookie.
98  *
99  * @param r The request
100  * @param name The name of the cookie.
101  */
102 AP_DECLARE(apr_status_t) ap_cookie_remove(request_rec * r, const char *name)
103 {
104
105     /* create RFC2109 compliant cookie */
106     char *rfc2109 = apr_pstrcat(r->pool, name, "=;",
107                                 CLEAR_ATTRS, NULL);
108     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOG_PREFIX
109                   "user '%s' removed cookie: '%s'", r->user, rfc2109);
110     apr_table_addn(r->headers_out, SET_COOKIE, rfc2109);
111
112     return APR_SUCCESS;
113
114 }
115
116 /**
117  * Remove an RFC2965 compliant cookie.
118  *
119  * @param r The request
120  * @param name2 The name of the cookie.
121  */
122 AP_DECLARE(apr_status_t) ap_cookie_remove2(request_rec * r, const char *name2)
123 {
124
125     /* create RFC2965 compliant cookie */
126     char *rfc2965 = apr_pstrcat(r->pool, name2, "=;",
127                                 CLEAR_ATTRS, NULL);
128     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, LOG_PREFIX
129                   "user '%s' removed cookie2: '%s'", r->user, rfc2965);
130     apr_table_addn(r->headers_out, SET_COOKIE2, rfc2965);
131
132     return APR_SUCCESS;
133
134 }
135
136 /* Iterate through the cookies, isolate our cookie and then remove it.
137  *
138  * If our cookie appears two or more times, but with different values,
139  * remove it twice and set the duplicated flag to true. Remove any
140  * $path or other attributes following our cookie if present. If we end
141  * up with an empty cookie, remove the whole header.
142  */
143 static int extract_cookie_line(ap_cookie_do * v, const char *key, const char *val)
144 {
145     char *last1, *last2;
146     char *cookie = apr_pstrdup(v->r->pool, val);
147     const char *name = apr_pstrcat(v->r->pool, v->name ? v->name : "", "=", NULL);
148     size_t len = strlen(name);
149     char *new_cookie = "";
150     const char *comma = ",";
151     char *next1;
152     const char *semi = ";";
153     char *next2;
154     const char *sep = "";
155     int cookies = 0;
156
157     /* find the cookie called name */
158     int eat = 0;
159     next1 = apr_strtok(cookie, comma, &last1);
160     while (next1) {
161         next2 = apr_strtok(next1, semi, &last2);
162         while (next2) {
163             char *trim = next2;
164             while (apr_isspace(*trim)) {
165                 trim++;
166             }
167             if (!strncmp(trim, name, len)) {
168                 if (v->encoded) {
169                     if (strcmp(v->encoded, trim + len)) {
170                         v->duplicated = 1;
171                     }
172                 }
173                 v->encoded = apr_pstrdup(v->r->pool, trim + len);
174                 eat = 1;
175             }
176             else {
177                 if (*trim != '$') {
178                     cookies++;
179                     eat = 0;
180                 }
181                 if (!eat) {
182                     new_cookie = apr_pstrcat(v->r->pool, new_cookie, sep, next2, NULL);
183                 }
184             }
185             next2 = apr_strtok(NULL, semi, &last2);
186             sep = semi;
187         }
188
189         next1 = apr_strtok(NULL, comma, &last1);
190         sep = comma;
191     }
192
193     /* any cookies left over? */
194     if (cookies) {
195         apr_table_addn(v->new_cookies, key, new_cookie);
196     }
197
198     return 1;
199 }
200
201 /**
202  * Read a cookie called name, placing its value in val.
203  *
204  * Both the Cookie and Cookie2 headers are scanned for the cookie.
205  *
206  * If the cookie is duplicated, this function returns APR_EGENERAL. If found,
207  * and if remove is non zero, the cookie will be removed from the headers, and
208  * thus kept private from the backend.
209  */
210 AP_DECLARE(apr_status_t) ap_cookie_read(request_rec * r, const char *name, const char **val,
211                                         int remove)
212 {
213
214     ap_cookie_do v;
215     v.r = r;
216     v.encoded = NULL;
217     v.new_cookies = apr_table_make(r->pool, 10);
218     v.duplicated = 0;
219     v.name = name;
220
221     apr_table_do((int (*) (void *, const char *, const char *))
222                  extract_cookie_line, (void *) &v, r->headers_in,
223                  "Cookie", "Cookie2", NULL);
224     if (v.duplicated) {
225         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, LOG_PREFIX
226          "client submitted cookie '%s' more than once: %s", v.name, r->uri);
227         return APR_EGENERAL;
228     }
229
230     /* remove our cookie(s), and replace them */
231     if (remove) {
232         apr_table_unset(r->headers_in, "Cookie");
233         apr_table_unset(r->headers_in, "Cookie2");
234         r->headers_in = apr_table_overlay(r->pool, r->headers_in, v.new_cookies);
235     }
236
237     *val = v.encoded;
238
239     return APR_SUCCESS;
240
241 }
242
243 /**
244  * Sanity check a given string that it exists, is not empty,
245  * and does not contain the special characters '=', ';' and '&'.
246  *
247  * It is used to sanity check the cookie names.
248  */
249 AP_DECLARE(apr_status_t) ap_cookie_check_string(const char *string)
250 {
251     if (!string || !*string || ap_strchr_c(string, '=') || ap_strchr_c(string, '&') ||
252         ap_strchr_c(string, ';')) {
253         return APR_EGENERAL;
254     }
255     return APR_SUCCESS;
256 }