summaryrefslogtreecommitdiff
path: root/src/mod_evhost.c
blob: 2d777662974e7b5a16780a456f12afc9f31a09f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "first.h"

#include "base.h"
#include "plugin.h"
#include "log.h"
#include "stat_cache.h"

#include <stdlib.h>
#include <string.h>

/**
 *
 * #
 * # define a pattern for the host url finding
 * # %% => % sign
 * # %0 => domain name + tld
 * # %1 => tld
 * # %2 => domain name without tld
 * # %3 => subdomain 1 name
 * # %4 => subdomain 2 name
 * # %_ => fqdn (without port info)
 * #
 * evhost.path-pattern = "/home/ckruse/dev/www/%3/htdocs/"
 *
 */

typedef struct {
    /* pieces for path creation */
    const buffer *path_pieces;
} plugin_config;

typedef struct {
    PLUGIN_DATA;
    plugin_config defaults;
    plugin_config conf;

    array split_vals;
} plugin_data;

INIT_FUNC(mod_evhost_init) {
    return ck_calloc(1, sizeof(plugin_data));
}

static void mod_evhost_free_path_pieces(const buffer *path_pieces) {
    buffer *b;
    *(const buffer **)&b = path_pieces;
    for (; path_pieces->ptr; ++path_pieces) free(path_pieces->ptr);
    free(b);
}

FREE_FUNC(mod_evhost_free) {
    plugin_data * const p = p_d;
    array_free_data(&p->split_vals);
    if (NULL == p->cvlist) return;
    /* (init i to 0 if global context; to 1 to skip empty global context) */
    for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
        config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
        for (; -1 != cpv->k_id; ++cpv) {
            if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue;
            switch (cpv->k_id) {
              case 0: /* evhost.path-pattern */
                mod_evhost_free_path_pieces(cpv->v.v);
                break;
              default:
                break;
            }
        }
    }
}

__attribute_cold__
static buffer * mod_evhost_parse_pattern_err(buffer *bptr) {
    for (; bptr->ptr; ++bptr) free(bptr->ptr);
    return NULL;
}

static buffer * mod_evhost_parse_pattern(const char *ptr) {
	uint32_t used = 0;
	const uint32_t sz = 127;/* (sz+1 must match bptr[] num elts below) */
	const char *pos;
	buffer bptr[128]; /* (128 elements takes 2k on stack in 64-bit) */
	memset(bptr, 0, sizeof(bptr));

	for(pos=ptr;*ptr;ptr++) {
		if(*ptr == '%') {
			size_t len;
			if (used >= sz-1) /* (should not happen) */
				return mod_evhost_parse_pattern_err(bptr);

			/* "%%" "%_" "%x" "%{x.y}" where x and y are *single digit* 0 - 9 */
			if (ptr[1] == '%' || ptr[1] == '_' || light_isdigit(ptr[1])) {
				len = 2;
			} else if (ptr[1] == '{') {
				if (!light_isdigit(ptr[2]))
					return mod_evhost_parse_pattern_err(bptr);
				if (ptr[3] == '.') {
					if (!light_isdigit(ptr[4]))
						return mod_evhost_parse_pattern_err(bptr);
					if (ptr[5] != '}')
						return mod_evhost_parse_pattern_err(bptr);
					len = 6;
				} else if (ptr[3] == '}') {
					len = 4;
				} else {
					return mod_evhost_parse_pattern_err(bptr);
				}
			} else {
				return mod_evhost_parse_pattern_err(bptr);
			}

			buffer_copy_string_len(bptr+used, pos, ptr-pos);
			pos = ptr + len;

			buffer_copy_string_len(bptr+used+1, ptr, len);
			ptr += len - 1; /*(ptr++ in for() loop)*/

			used += 2;
		}
	}

	if(*pos != '\0') {
		if (used >= sz) /* (should not happen) */
			return mod_evhost_parse_pattern_err(bptr);
		buffer_copy_string_len(bptr+used, pos, ptr-pos);
		++used;
	}

	buffer * const path_pieces = ck_malloc((used+1) * sizeof(buffer));
	return memcpy(path_pieces, bptr, (used+1) * sizeof(buffer));
}

static void mod_evhost_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
    switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
      case 0: /* evhost.path-pattern */
        if (cpv->vtype == T_CONFIG_LOCAL)
            pconf->path_pieces = cpv->v.v;
        break;
      default:/* should not happen */
        return;
    }
}

static void mod_evhost_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
    do {
        mod_evhost_merge_config_cpv(pconf, cpv);
    } while ((++cpv)->k_id != -1);
}

static void mod_evhost_patch_config(request_st * const r, plugin_data * const p) {
    p->conf = p->defaults; /* copy small struct instead of memcpy() */
    /*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
    for (int i = 1, used = p->nconfig; i < used; ++i) {
        if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
            mod_evhost_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
    }
}

SETDEFAULTS_FUNC(mod_evhost_set_defaults) {
    static const config_plugin_keys_t cpk[] = {
      { CONST_STR_LEN("evhost.path-pattern"),
        T_CONFIG_STRING,
        T_CONFIG_SCOPE_CONNECTION }
     ,{ NULL, 0,
        T_CONFIG_UNSET,
        T_CONFIG_SCOPE_UNSET }
    };

    plugin_data * const p = p_d;
    if (!config_plugin_values_init(srv, p, cpk, "mod_evhost"))
        return HANDLER_ERROR;

    /* process and validate config directives
     * (init i to 0 if global context; to 1 to skip empty global context) */
    for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
        config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
        for (; -1 != cpv->k_id; ++cpv) {
            switch (cpv->k_id) {
              case 0: /* evhost.path-pattern */
                if (!buffer_is_blank(cpv->v.b)) {
                    const char * const ptr = cpv->v.b->ptr;
                    cpv->v.v = mod_evhost_parse_pattern(ptr);
                    if (NULL == cpv->v.v) {
                        log_error(srv->errh, __FILE__, __LINE__,
                          "invalid evhost.path-pattern: %s", ptr);
                        return HANDLER_ERROR;
                    }
                    cpv->vtype = T_CONFIG_LOCAL;
                }
                break;
              default:/* should not happen */
                break;
            }
        }
    }

    /* initialize p->defaults from global config context */
    if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
        const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
        if (-1 != cpv->k_id)
            mod_evhost_merge_config(&p->defaults, cpv);
    }

    return HANDLER_GO_ON;
}

/**
 * assign the different parts of the domain to array-indezes (sub2.sub1.domain.tld)
 * - %0 - domain.tld
 * - %1 - tld
 * - %2 - domain
 * - %3 - sub1
 * - ...
 */

static void mod_evhost_parse_host(buffer *key, array *host, buffer *authority) {
	char *ptr = authority->ptr + buffer_clen(authority);
	char *colon = ptr; /* needed to filter out the colon (if exists) */
	int first = 1;
	int i;

	/*if (ptr == authority->ptr) return;*//*(no authority checked earlier)*/

	if (*authority->ptr == '[') { /* authority is IPv6 literal address */
                colon = ptr;
                if (ptr[-1] != ']') {
			do { --ptr; } while (ptr > authority->ptr && ptr[-1] != ']');
			if (*ptr != ':') return; /*(should not happen for valid authority)*/
			colon = ptr;
		}
		ptr = authority->ptr;
		array_set_key_value(host,CONST_STR_LEN("%0"),ptr,colon-ptr);
		return;
	}

	/* first, find the domain + tld */
	for(; ptr > authority->ptr; --ptr) {
		if(*ptr == '.') {
			if(first) first = 0;
			else      break;
		} else if(*ptr == ':') {
			colon = ptr;
			first = 1;
		}
	}

	/* if we stopped at a dot, skip the dot */
	if (*ptr == '.') ptr++;
	array_set_key_value(host, CONST_STR_LEN("%0"), ptr, colon-ptr);

	/* if the : is not the start of the authority, go on parsing the hostname */

	if (colon != authority->ptr) {
		for(ptr = colon - 1, i = 1; ptr > authority->ptr; --ptr) {
			if(*ptr == '.') {
				if (ptr != colon - 1) {
					/* is something between the dots */
					buffer_clear(key);
					buffer_append_char(key, '%');
					buffer_append_int(key, i++);
					array_set_key_value(host, BUF_PTR_LEN(key), ptr+1, colon-ptr-1);
				}
				colon = ptr;
			}
		}

		/* if the . is not the first character of the hostname */
		if (colon != ptr) {
			buffer_clear(key);
			buffer_append_char(key, '%');
			buffer_append_int(key, i /* ++ */);
			array_set_key_value(host, BUF_PTR_LEN(key), ptr, colon-ptr);
		}
	}
}

static void mod_evhost_build_doc_root_path(buffer *b, array *parsed_host, buffer *authority, const buffer *path_pieces) {
	array_reset_data_strings(parsed_host);
	mod_evhost_parse_host(b, parsed_host, authority);
	buffer_clear(b);

	for (const char *ptr; (ptr = path_pieces->ptr); ++path_pieces) {
		if (*ptr == '%') {
			const data_string *ds;

			if (*(ptr+1) == '%') {
				/* %% */
				buffer_append_char(b, '%');
			} else if (*(ptr+1) == '_' ) {
				/* %_ == full hostname */
				char *colon = strchr(authority->ptr, ':');

				if(colon == NULL) {
					buffer_append_string_buffer(b, authority); /* adds fqdn */
				} else {
					/* strip the port out of the authority-part of the URI scheme */
					buffer_append_string_len(b, authority->ptr, colon - authority->ptr); /* adds fqdn */
				}
			} else if (ptr[1] == '{' ) {
				char s[3] = "% ";
				s[1] = ptr[2]; /*(assumes single digit before '.', and, optionally, '.' and single digit after '.')*/
				if (NULL != (ds = (data_string *)array_get_element_klen(parsed_host, s, 2))) {
					if (ptr[3] != '.' || ptr[4] == '0') {
						buffer_append_string_buffer(b, &ds->value);
					} else {
						if ((size_t)(ptr[4]-'0') <= buffer_clen(&ds->value)) {
							buffer_append_char(b, ds->value.ptr[(ptr[4]-'0')-1]);
						}
					}
				} else {
					/* unhandled %-sequence */
				}
			} else if (NULL != (ds = (data_string *)array_get_element_klen(parsed_host, BUF_PTR_LEN(path_pieces)))) {
				buffer_append_string_buffer(b, &ds->value);
			} else {
				/* unhandled %-sequence */
			}
		} else {
			buffer_append_string_buffer(b, path_pieces);
		}
	}

	buffer_append_slash(b);
}

static handler_t mod_evhost_uri_handler(request_st * const r, void *p_d) {
	plugin_data *p = p_d;

	/* not authority set */
	if (buffer_is_blank(&r->uri.authority)) return HANDLER_GO_ON;

	mod_evhost_patch_config(r, p);

	/* missing even default(global) conf */
	if (NULL == p->conf.path_pieces) return HANDLER_GO_ON;

	buffer * const b = r->tmp_buf;/*(tmp_buf cleared before use in call below)*/
	mod_evhost_build_doc_root_path(b, &p->split_vals, &r->uri.authority, p->conf.path_pieces);

	if (!stat_cache_path_isdir(b)) {
		log_perror(r->conf.errh, __FILE__, __LINE__, "%s", b->ptr);
	} else {
		buffer_copy_buffer(&r->physical.doc_root, b);
	}

	return HANDLER_GO_ON;
}


__attribute_cold__
__declspec_dllexport__
int mod_evhost_plugin_init(plugin *p);
int mod_evhost_plugin_init(plugin *p) {
	p->version     = LIGHTTPD_VERSION_ID;
	p->name        = "evhost";
	p->init                    = mod_evhost_init;
	p->set_defaults            = mod_evhost_set_defaults;
	p->handle_docroot          = mod_evhost_uri_handler;
	p->cleanup                 = mod_evhost_free;

	return 0;
}