xref: /lighttpd1.4/src/mod_rewrite.c (revision 5e14db43)
1 #include "first.h"
2 
3 #include "base.h"
4 #include "keyvalue.h"
5 #include "log.h"
6 #include "buffer.h"
7 #include "burl.h"
8 
9 #include "plugin.h"
10 #include "stat_cache.h"
11 
12 #include <stdlib.h>
13 #include <string.h>
14 
15 typedef struct {
16     pcre_keyvalue_buffer *rewrite;
17     pcre_keyvalue_buffer *rewrite_NF;
18 } plugin_config;
19 
20 enum { REWRITE_STATE_REWRITTEN = 1024, REWRITE_STATE_FINISHED = 2048}; /*flags*/
21 
22 typedef struct {
23     PLUGIN_DATA;
24     plugin_config defaults;
25     plugin_config conf;
26 } plugin_data;
27 
INIT_FUNC(mod_rewrite_init)28 INIT_FUNC(mod_rewrite_init) {
29     return ck_calloc(1, sizeof(plugin_data));
30 }
31 
FREE_FUNC(mod_rewrite_free)32 FREE_FUNC(mod_rewrite_free) {
33     plugin_data * const p = p_d;
34     if (NULL == p->cvlist) return;
35     /* (init i to 0 if global context; to 1 to skip empty global context) */
36     for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
37         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
38         /* kvb value might be copied in multiple directives; free only once */
39         pcre_keyvalue_buffer *kvb = NULL, *kvb_NF = NULL;
40         for (; -1 != cpv->k_id; ++cpv) {
41             switch (cpv->k_id) {
42               case 0: /* url.rewrite-once */
43               case 1: /* url.rewrite-final */
44               case 2: /* url.rewrite */
45               case 3: /* url.rewrite-repeat */
46                 if (cpv->vtype == T_CONFIG_LOCAL)
47                     kvb = cpv->v.v;
48                 break;
49               case 4: /* url.rewrite-if-not-file */
50               case 5: /* url.rewrite-repeat-if-not-file */
51                 if (cpv->vtype == T_CONFIG_LOCAL)
52                     kvb_NF = cpv->v.v;
53               default:
54                 break;
55             }
56         }
57         if (kvb)    pcre_keyvalue_buffer_free(kvb);
58         if (kvb_NF) pcre_keyvalue_buffer_free(kvb_NF);
59     }
60 }
61 
mod_rewrite_merge_config_cpv(plugin_config * const pconf,const config_plugin_value_t * const cpv)62 static void mod_rewrite_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
63     switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
64       case 0: /* url.rewrite-once */
65       case 1: /* url.rewrite-final */
66       case 2: /* url.rewrite */
67       case 3: /* url.rewrite-repeat */
68         /*if (cpv->vtype == T_CONFIG_LOCAL)*//*always true here in mod_rewrite*/
69             pconf->rewrite = cpv->v.v;
70         break;
71       case 4: /* url.rewrite-if-not-file */
72       case 5: /* url.rewrite-repeat-if-not-file */
73         /*if (cpv->vtype == T_CONFIG_LOCAL)*//*always true here in mod_rewrite*/
74             pconf->rewrite_NF = cpv->v.v;
75         break;
76       default:/* should not happen */
77         return;
78     }
79 }
80 
mod_rewrite_merge_config(plugin_config * const pconf,const config_plugin_value_t * cpv)81 static void mod_rewrite_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
82     do {
83         mod_rewrite_merge_config_cpv(pconf, cpv);
84     } while ((++cpv)->k_id != -1);
85 }
86 
mod_rewrite_patch_config(request_st * const r,plugin_data * const p)87 static void mod_rewrite_patch_config(request_st * const r, plugin_data * const p) {
88     p->conf = p->defaults; /* copy small struct instead of memcpy() */
89     /*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
90     for (int i = 1, used = p->nconfig; i < used; ++i) {
91         if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
92             mod_rewrite_merge_config(&p->conf, p->cvlist+p->cvlist[i].v.u2[0]);
93     }
94 }
95 
mod_rewrite_parse_list(server * srv,const array * a,pcre_keyvalue_buffer * kvb,const int condidx)96 static pcre_keyvalue_buffer * mod_rewrite_parse_list(server *srv, const array *a, pcre_keyvalue_buffer *kvb, const int condidx) {
97     const int pcre_jit = config_feature_bool(srv, "server.pcre_jit", 1);
98     int allocated = 0;
99     if (NULL == kvb) {
100         allocated = 1;
101         kvb = pcre_keyvalue_buffer_init();
102         kvb->cfgidx = condidx;
103     }
104 
105     buffer * const tb = srv->tmp_buf;
106     int percent = 0;
107     for (uint32_t j = 0; j < a->used; ++j) {
108         data_string *ds = (data_string *)a->data[j];
109         if (srv->srvconf.http_url_normalize) {
110             pcre_keyvalue_burl_normalize_key(&ds->key, tb);
111             pcre_keyvalue_burl_normalize_value(&ds->value, tb);
112         }
113         for (const char *s = ds->value.ptr; (s = strchr(s, '%')); ++s) {
114             if (s[1] == '%')
115                 ++s;
116             else if (light_isdigit(s[1]) || s[1] == '{') {
117                 percent |= 1;
118                 break;
119             }
120         }
121         if (!pcre_keyvalue_buffer_append(srv->errh, kvb, &ds->key, &ds->value,
122                                          pcre_jit)) {
123             log_error(srv->errh, __FILE__, __LINE__,
124               "pcre-compile failed for %s", ds->key.ptr);
125             if (allocated) pcre_keyvalue_buffer_free(kvb);
126             return NULL;
127         }
128     }
129     if (percent && 0 == kvb->x0)
130         kvb->x0 = config_capture(srv, condidx);
131 
132     return kvb;
133 }
134 
SETDEFAULTS_FUNC(mod_rewrite_set_defaults)135 SETDEFAULTS_FUNC(mod_rewrite_set_defaults) {
136     static const config_plugin_keys_t cpk[] = {
137       { CONST_STR_LEN("url.rewrite-once"),
138         T_CONFIG_ARRAY_KVSTRING,
139         T_CONFIG_SCOPE_CONNECTION }
140      ,{ CONST_STR_LEN("url.rewrite-final"),  /* old name => url.rewrite-once */
141         T_CONFIG_ARRAY_KVSTRING,
142         T_CONFIG_SCOPE_CONNECTION }
143      ,{ CONST_STR_LEN("url.rewrite"),        /* old name => url.rewrite-once */
144         T_CONFIG_ARRAY_KVSTRING,
145         T_CONFIG_SCOPE_CONNECTION }
146      ,{ CONST_STR_LEN("url.rewrite-repeat"),
147         T_CONFIG_ARRAY_KVSTRING,
148         T_CONFIG_SCOPE_CONNECTION }
149      ,{ CONST_STR_LEN("url.rewrite-if-not-file"), /* rewrite-once if ENOENT */
150         T_CONFIG_ARRAY_KVSTRING,
151         T_CONFIG_SCOPE_CONNECTION }
152      ,{ CONST_STR_LEN("url.rewrite-repeat-if-not-file"), /* repeat if ENOENT */
153         T_CONFIG_ARRAY_KVSTRING,
154         T_CONFIG_SCOPE_CONNECTION }
155      ,{ NULL, 0,
156         T_CONFIG_UNSET,
157         T_CONFIG_SCOPE_UNSET }
158     };
159 
160     plugin_data * const p = p_d;
161     if (!config_plugin_values_init(srv, p, cpk, "mod_rewrite"))
162         return HANDLER_ERROR;
163 
164     /* process and validate config directives
165      * (init i to 0 if global context; to 1 to skip empty global context) */
166     for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
167         config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
168         /* parse directives in specific order to encode repeat_idx in kvb->x1 */
169         config_plugin_value_t *rewrite_once = NULL, *rewrite_repeat = NULL,
170                               *rewrite_NF = NULL,   *rewrite_repeat_NF = NULL,
171                               *rewrite = NULL,      *rewrite_final = NULL;
172         for (; -1 != cpv->k_id; ++cpv) {
173             switch (cpv->k_id) {
174               case 0: /* url.rewrite-once */
175                 rewrite_once = cpv;
176                 break;
177               case 1: /* url.rewrite-final */
178                 rewrite_final = cpv;
179                 break;
180               case 2: /* url.rewrite */
181                 rewrite = cpv;
182                 break;
183               case 3: /* url.rewrite-repeat */
184                 rewrite_repeat = cpv;
185                 break;
186               case 4: /* url.rewrite-if-not-file */
187                 rewrite_NF = cpv;
188                 break;
189               case 5: /* url.rewrite-repeat-if-not-file */
190                 rewrite_repeat_NF = cpv;
191                 break;
192               default:/* should not happen */
193                 break;
194             }
195         }
196 
197         const int condidx = p->cvlist[i].k_id;
198         pcre_keyvalue_buffer *kvb = NULL, *kvb_NF = NULL;
199 
200         if ((cpv = rewrite_once)) {
201             cpv->v.v = mod_rewrite_parse_list(srv, cpv->v.a, kvb, condidx);
202             if (NULL == cpv->v.v) return HANDLER_ERROR;
203             cpv->vtype = T_CONFIG_LOCAL;
204             kvb = cpv->v.v;
205         }
206 
207         if ((cpv = rewrite_final)) {
208             cpv->v.v = mod_rewrite_parse_list(srv, cpv->v.a, kvb, condidx);
209             if (NULL == cpv->v.v) return HANDLER_ERROR;
210             cpv->vtype = T_CONFIG_LOCAL;
211             kvb = cpv->v.v;
212         }
213 
214         if ((cpv = rewrite)) {
215             cpv->v.v = mod_rewrite_parse_list(srv, cpv->v.a, kvb, condidx);
216             if (NULL == cpv->v.v) return HANDLER_ERROR;
217             cpv->vtype = T_CONFIG_LOCAL;
218             kvb = cpv->v.v;
219         }
220 
221         if (kvb) kvb->x1 = (int)kvb->used; /* repeat_idx */
222 
223         if ((cpv = rewrite_repeat)) {
224             cpv->v.v = mod_rewrite_parse_list(srv, cpv->v.a, kvb, condidx);
225             if (NULL == cpv->v.v) return HANDLER_ERROR;
226             cpv->vtype = T_CONFIG_LOCAL;
227             /*kvb = cpv->v.v;*/
228         }
229 
230         if ((cpv = rewrite_NF)) {
231             cpv->v.v = mod_rewrite_parse_list(srv, cpv->v.a, kvb_NF, condidx);
232             if (NULL == cpv->v.v) return HANDLER_ERROR;
233             cpv->vtype = T_CONFIG_LOCAL;
234             kvb_NF = cpv->v.v;
235         }
236 
237         if (kvb_NF) kvb_NF->x1 = (int)kvb_NF->used; /* repeat_idx */
238 
239         if ((cpv = rewrite_repeat_NF)) {
240             cpv->v.v = mod_rewrite_parse_list(srv, cpv->v.a, kvb_NF, condidx);
241             if (NULL == cpv->v.v) return HANDLER_ERROR;
242             cpv->vtype = T_CONFIG_LOCAL;
243             /*kvb_NF = cpv->v.v;*/
244         }
245     }
246 
247     /* initialize p->defaults from global config context */
248     if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
249         const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
250         if (-1 != cpv->k_id)
251             mod_rewrite_merge_config(&p->defaults, cpv);
252     }
253 
254     return HANDLER_GO_ON;
255 }
256 
257 __attribute_cold__
process_rewrite_rules_loop_error(request_st * const r,const int cfgidx)258 static handler_t process_rewrite_rules_loop_error(request_st * const r, const int cfgidx) {
259     if (0 != cfgidx) {
260         config_cond_info cfginfo;
261         config_get_config_cond_info(&cfginfo, (uint32_t)cfgidx);
262         log_error(r->conf.errh, __FILE__, __LINE__,
263           "ENDLESS LOOP IN rewrite-rule DETECTED ... aborting request, "
264           "perhaps you want to use url.rewrite-once instead of "
265           "url.rewrite-repeat (%s)", cfginfo.comp_key);
266     }
267     else
268         log_error(r->conf.errh, __FILE__, __LINE__,
269           "ENDLESS LOOP IN rewrite-rule DETECTED ... aborting request");
270 
271     return HANDLER_ERROR;
272 }
273 
URIHANDLER_FUNC(mod_rewrite_con_reset)274 URIHANDLER_FUNC(mod_rewrite_con_reset) {
275     r->plugin_ctx[((plugin_data *)p_d)->id] = NULL;
276     return HANDLER_GO_ON;
277 }
278 
process_rewrite_rules(request_st * const r,plugin_data * p,const pcre_keyvalue_buffer * kvb)279 static handler_t process_rewrite_rules(request_st * const r, plugin_data *p, const pcre_keyvalue_buffer *kvb) {
280 	struct burl_parts_t burl;
281 	pcre_keyvalue_ctx ctx;
282 	handler_t rc;
283 
284 	if (r->plugin_ctx[p->id]) {
285 		uintptr_t * const hctx = (uintptr_t *)(r->plugin_ctx + p->id);
286 		if (((++*hctx) & 0x1FF) > 100) {
287 			return process_rewrite_rules_loop_error(r, kvb->cfgidx);
288 		}
289 		if (*hctx & REWRITE_STATE_FINISHED) return HANDLER_GO_ON;
290 	}
291 
292 	ctx.cache = NULL;
293 	if (kvb->x0) { /*(kvb->x0 is capture_idx)*/
294 		ctx.cache = r->cond_match[kvb->x0 - 1];
295         }
296 	ctx.burl = &burl;
297 	burl.scheme    = &r->uri.scheme;
298 	burl.authority = &r->uri.authority;
299 	burl.port      = sock_addr_get_port(&r->con->srv_socket->addr);
300 	burl.path      = &r->target; /*(uri-encoded and includes query-part)*/
301 	burl.query     = &r->uri.query;
302 	if (buffer_is_blank(burl.authority))
303 		burl.authority = r->server_name;
304 
305 	buffer * const tb = r->tmp_buf;
306 	rc = pcre_keyvalue_buffer_process(kvb, &ctx, &r->target, tb);
307 	if (HANDLER_FINISHED == rc && !buffer_is_blank(tb) && tb->ptr[0] == '/') {
308 		buffer_copy_buffer(&r->target, tb);
309 		uintptr_t * const hctx = (uintptr_t *)(r->plugin_ctx + p->id);
310 		*hctx |= REWRITE_STATE_REWRITTEN;
311 		/*(kvb->x1 is repeat_idx)*/
312 		if (ctx.m < kvb->x1) *hctx |= REWRITE_STATE_FINISHED;
313 		buffer_reset(&r->physical.path);
314 		rc = HANDLER_COMEBACK;
315 	}
316 	else if (HANDLER_FINISHED == rc) {
317 		rc = HANDLER_ERROR;
318 		log_error(r->conf.errh, __FILE__, __LINE__,
319 		  "mod_rewrite invalid result (not beginning with '/') "
320 		  "while processing uri: %s", r->target.ptr);
321 	}
322 	else if (HANDLER_ERROR == rc) {
323 		log_error(r->conf.errh, __FILE__, __LINE__,
324 		  "pcre_exec() error "
325 		  "while processing uri: %s", r->target.ptr);
326 	}
327 	return rc;
328 }
329 
URIHANDLER_FUNC(mod_rewrite_physical)330 URIHANDLER_FUNC(mod_rewrite_physical) {
331     plugin_data * const p = p_d;
332 
333     if (NULL != r->handler_module) return HANDLER_GO_ON;
334 
335     mod_rewrite_patch_config(r, p);
336     if (!p->conf.rewrite_NF || !p->conf.rewrite_NF->used) return HANDLER_GO_ON;
337 
338     /* skip if physical.path is a regular file */
339     const stat_cache_st * const st = stat_cache_path_stat(&r->physical.path);
340     if (st && S_ISREG(st->st_mode)) return HANDLER_GO_ON;
341 
342     return process_rewrite_rules(r, p, p->conf.rewrite_NF);
343 }
344 
URIHANDLER_FUNC(mod_rewrite_uri_handler)345 URIHANDLER_FUNC(mod_rewrite_uri_handler) {
346     plugin_data *p = p_d;
347 
348     mod_rewrite_patch_config(r, p);
349     if (!p->conf.rewrite || !p->conf.rewrite->used) return HANDLER_GO_ON;
350 
351     return process_rewrite_rules(r, p, p->conf.rewrite);
352 }
353 
354 
355 __attribute_cold__
356 int mod_rewrite_plugin_init(plugin *p);
mod_rewrite_plugin_init(plugin * p)357 int mod_rewrite_plugin_init(plugin *p) {
358 	p->version     = LIGHTTPD_VERSION_ID;
359 	p->name        = "rewrite";
360 
361 	p->init        = mod_rewrite_init;
362 	/* it has to stay _raw as we are matching on uri + querystring
363 	 */
364 
365 	p->handle_uri_raw = mod_rewrite_uri_handler;
366 	p->handle_physical = mod_rewrite_physical;
367 	p->cleanup     = mod_rewrite_free;
368 	p->handle_request_reset = mod_rewrite_con_reset;
369 	p->set_defaults = mod_rewrite_set_defaults;
370 
371 	return 0;
372 }
373