[mod_auth] require digest uri= match original URI
[lighttpd.git] / src / mod_alias.c
blobbab1b6705be059c339b888440b8cd0a9a5d5c4e7
1 #include "first.h"
3 #include "base.h"
4 #include "log.h"
5 #include "buffer.h"
7 #include "plugin.h"
9 #include <stdlib.h>
10 #include <string.h>
12 /* plugin config for all request/connections */
13 typedef struct {
14 array *alias;
15 } plugin_config;
17 typedef struct {
18 PLUGIN_DATA;
20 plugin_config **config_storage;
22 plugin_config conf;
23 } plugin_data;
25 /* init the plugin data */
26 INIT_FUNC(mod_alias_init) {
27 plugin_data *p;
29 p = calloc(1, sizeof(*p));
33 return p;
36 /* detroy the plugin data */
37 FREE_FUNC(mod_alias_free) {
38 plugin_data *p = p_d;
40 if (!p) return HANDLER_GO_ON;
42 if (p->config_storage) {
43 size_t i;
45 for (i = 0; i < srv->config_context->used; i++) {
46 plugin_config *s = p->config_storage[i];
48 if (NULL == s) continue;
50 array_free(s->alias);
52 free(s);
54 free(p->config_storage);
57 free(p);
59 return HANDLER_GO_ON;
62 /* handle plugin config and check values */
64 SETDEFAULTS_FUNC(mod_alias_set_defaults) {
65 plugin_data *p = p_d;
66 size_t i = 0;
68 config_values_t cv[] = {
69 { "alias.url", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
70 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
73 if (!p) return HANDLER_ERROR;
75 p->config_storage = calloc(srv->config_context->used, sizeof(plugin_config *));
77 for (i = 0; i < srv->config_context->used; i++) {
78 data_config const* config = (data_config const*)srv->config_context->data[i];
79 plugin_config *s;
81 s = calloc(1, sizeof(plugin_config));
82 s->alias = array_init();
83 cv[0].destination = s->alias;
85 p->config_storage[i] = s;
87 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
88 return HANDLER_ERROR;
91 if (!array_is_kvstring(s->alias)) {
92 log_error_write(srv, __FILE__, __LINE__, "s",
93 "unexpected value for alias.url; expected list of \"urlpath\" => \"filepath\"");
94 return HANDLER_ERROR;
97 if (s->alias->used >= 2) {
98 const array *a = s->alias;
99 size_t j, k;
101 for (j = 0; j < a->used; j ++) {
102 const buffer *prefix = a->data[a->sorted[j]]->key;
103 for (k = j + 1; k < a->used; k ++) {
104 const buffer *key = a->data[a->sorted[k]]->key;
106 if (buffer_string_length(key) < buffer_string_length(prefix)) {
107 break;
109 if (memcmp(key->ptr, prefix->ptr, buffer_string_length(prefix)) != 0) {
110 break;
112 /* ok, they have same prefix. check position */
113 if (a->sorted[j] < a->sorted[k]) {
114 log_error_write(srv, __FILE__, __LINE__, "SBSBS",
115 "url.alias: `", key, "' will never match as `", prefix, "' matched first");
116 return HANDLER_ERROR;
123 return HANDLER_GO_ON;
126 #define PATCH(x) \
127 p->conf.x = s->x;
128 static int mod_alias_patch_connection(server *srv, connection *con, plugin_data *p) {
129 size_t i, j;
130 plugin_config *s = p->config_storage[0];
132 PATCH(alias);
134 /* skip the first, the global context */
135 for (i = 1; i < srv->config_context->used; i++) {
136 data_config *dc = (data_config *)srv->config_context->data[i];
137 s = p->config_storage[i];
139 /* condition didn't match */
140 if (!config_check_cond(srv, con, dc)) continue;
142 /* merge config */
143 for (j = 0; j < dc->value->used; j++) {
144 data_unset *du = dc->value->data[j];
146 if (buffer_is_equal_string(du->key, CONST_STR_LEN("alias.url"))) {
147 PATCH(alias);
152 return 0;
154 #undef PATCH
156 PHYSICALPATH_FUNC(mod_alias_physical_handler) {
157 plugin_data *p = p_d;
158 char *uri_ptr;
159 size_t uri_len = buffer_string_length(con->physical.path);
160 size_t basedir_len, alias_len;
161 data_string *ds;
163 if (0 == uri_len) return HANDLER_GO_ON;
165 mod_alias_patch_connection(srv, con, p);
167 /* do not include trailing slash on basedir */
168 basedir_len = buffer_string_length(con->physical.basedir);
169 if ('/' == con->physical.basedir->ptr[basedir_len-1]) --basedir_len;
170 uri_len -= basedir_len;
171 uri_ptr = con->physical.path->ptr + basedir_len;
173 ds = (!con->conf.force_lowercase_filenames)
174 ? (data_string *)array_match_key_prefix_klen(p->conf.alias, uri_ptr, uri_len)
175 : (data_string *)array_match_key_prefix_nc_klen(p->conf.alias, uri_ptr, uri_len);
176 if (NULL == ds) { return HANDLER_GO_ON; }
178 /* matched */
180 /* check for path traversal in url-path following alias if key
181 * does not end in slash, but replacement value ends in slash */
182 alias_len = buffer_string_length(ds->key);
183 if (uri_ptr[alias_len] == '.') {
184 char *s = uri_ptr + alias_len + 1;
185 if (*s == '.') ++s;
186 if (*s == '/' || *s == '\0') {
187 size_t vlen = buffer_string_length(ds->value);
188 if (0 != alias_len && ds->key->ptr[alias_len-1] != '/'
189 && 0 != vlen && ds->value->ptr[vlen-1] == '/') {
190 con->http_status = 403;
191 return HANDLER_FINISHED;
196 buffer_copy_buffer(con->physical.basedir, ds->value);
197 buffer_copy_buffer(srv->tmp_buf, ds->value);
198 buffer_append_string(srv->tmp_buf, uri_ptr + alias_len);
199 buffer_copy_buffer(con->physical.path, srv->tmp_buf);
201 return HANDLER_GO_ON;
204 /* this function is called at dlopen() time and inits the callbacks */
206 int mod_alias_plugin_init(plugin *p);
207 int mod_alias_plugin_init(plugin *p) {
208 p->version = LIGHTTPD_VERSION_ID;
209 p->name = buffer_init_string("alias");
211 p->init = mod_alias_init;
212 p->handle_physical= mod_alias_physical_handler;
213 p->set_defaults = mod_alias_set_defaults;
214 p->cleanup = mod_alias_free;
216 p->data = NULL;
218 return 0;