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