1 #include "first.h" 2 3 #include "base.h" 4 #include "plugin.h" 5 #include "log.h" 6 #include "stat_cache.h" 7 8 #include <stdlib.h> 9 #include <string.h> 10 #include <errno.h> 11 12 typedef struct { 13 /* unparsed pieces */ 14 buffer *path_pieces_raw; 15 16 /* pieces for path creation */ 17 size_t len; 18 buffer **path_pieces; 19 } plugin_config; 20 21 typedef struct { 22 PLUGIN_DATA; 23 plugin_config **config_storage; 24 plugin_config conf; 25 buffer *tmp_buf; 26 array *split_vals; 27 } plugin_data; 28 29 INIT_FUNC(mod_evhost_init) { 30 plugin_data *p; 31 32 p = calloc(1, sizeof(*p)); 33 34 p->tmp_buf = buffer_init(); 35 p->split_vals = array_init(); 36 37 return p; 38 } 39 40 FREE_FUNC(mod_evhost_free) { 41 plugin_data *p = p_d; 42 43 UNUSED(srv); 44 45 if (!p) return HANDLER_GO_ON; 46 47 if (p->config_storage) { 48 size_t i; 49 for (i = 0; i < srv->config_context->used; i++) { 50 plugin_config *s = p->config_storage[i]; 51 52 if (NULL == s) continue; 53 54 if(s->path_pieces) { 55 size_t j; 56 for (j = 0; j < s->len; j++) { 57 buffer_free(s->path_pieces[j]); 58 } 59 60 free(s->path_pieces); 61 } 62 63 buffer_free(s->path_pieces_raw); 64 65 free(s); 66 } 67 free(p->config_storage); 68 } 69 70 buffer_free(p->tmp_buf); 71 array_free(p->split_vals); 72 73 free(p); 74 75 return HANDLER_GO_ON; 76 } 77 78 static int mod_evhost_parse_pattern(plugin_config *s) { 79 char *ptr = s->path_pieces_raw->ptr,*pos; 80 81 s->path_pieces = NULL; 82 83 for(pos=ptr;*ptr;ptr++) { 84 if(*ptr == '%') { 85 size_t len; 86 s->path_pieces = realloc(s->path_pieces,(s->len+2) * sizeof(*s->path_pieces)); 87 s->path_pieces[s->len] = buffer_init(); 88 s->path_pieces[s->len+1] = buffer_init(); 89 90 /* "%%" "%_" "%x" "%{x.y}" where x and y are *single digit* 0 - 9 */ 91 if (ptr[1] == '%' || ptr[1] == '_' || light_isdigit(ptr[1])) { 92 len = 2; 93 } else if (ptr[1] == '{') { 94 if (!light_isdigit(ptr[2])) return -1; 95 if (ptr[3] == '.') { 96 if (!light_isdigit(ptr[4])) return -1; 97 if (ptr[5] != '}') return -1; 98 len = 6; 99 } else if (ptr[3] == '}') { 100 len = 4; 101 } else { 102 return -1; 103 } 104 } else { 105 return -1; 106 } 107 108 buffer_copy_string_len(s->path_pieces[s->len],pos,ptr-pos); 109 pos = ptr + len; 110 111 buffer_copy_string_len(s->path_pieces[s->len+1],ptr,len); 112 ptr += len - 1; /*(ptr++ in for() loop)*/ 113 114 s->len += 2; 115 } 116 } 117 118 if(*pos != '\0') { 119 s->path_pieces = realloc(s->path_pieces,(s->len+1) * sizeof(*s->path_pieces)); 120 s->path_pieces[s->len] = buffer_init(); 121 122 buffer_copy_string_len(s->path_pieces[s->len],pos,ptr-pos); 123 124 s->len += 1; 125 } 126 127 return 0; 128 } 129 130 SETDEFAULTS_FUNC(mod_evhost_set_defaults) { 131 plugin_data *p = p_d; 132 size_t i; 133 134 /** 135 * 136 * # 137 * # define a pattern for the host url finding 138 * # %% => % sign 139 * # %0 => domain name + tld 140 * # %1 => tld 141 * # %2 => domain name without tld 142 * # %3 => subdomain 1 name 143 * # %4 => subdomain 2 name 144 * # %_ => fqdn (without port info) 145 * # 146 * evhost.path-pattern = "/home/ckruse/dev/www/%3/htdocs/" 147 * 148 */ 149 150 config_values_t cv[] = { 151 { "evhost.path-pattern", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, 152 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET } 153 }; 154 155 if (!p) return HANDLER_ERROR; 156 157 p->config_storage = calloc(srv->config_context->used, sizeof(plugin_config *)); 158 159 for (i = 0; i < srv->config_context->used; i++) { 160 data_config const* config = (data_config const*)srv->config_context->data[i]; 161 plugin_config *s; 162 163 s = calloc(1, sizeof(plugin_config)); 164 s->path_pieces_raw = buffer_init(); 165 s->path_pieces = NULL; 166 s->len = 0; 167 168 cv[0].destination = s->path_pieces_raw; 169 170 p->config_storage[i] = s; 171 172 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) { 173 return HANDLER_ERROR; 174 } 175 176 if (!buffer_string_is_empty(s->path_pieces_raw)) { 177 if (0 != mod_evhost_parse_pattern(s)) { 178 log_error_write(srv, __FILE__, __LINE__, "sb", "invalid evhost.path-pattern:", s->path_pieces_raw); 179 return HANDLER_ERROR; 180 } 181 } 182 } 183 184 return HANDLER_GO_ON; 185 } 186 187 /** 188 * assign the different parts of the domain to array-indezes (sub2.sub1.domain.tld) 189 * - %0 - domain.tld 190 * - %1 - tld 191 * - %2 - domain 192 * - %3 - sub1 193 * - ... 194 */ 195 196 static void mod_evhost_parse_host(buffer *key, array *host, buffer *authority) { 197 char *ptr = authority->ptr + buffer_string_length(authority); 198 char *colon = ptr; /* needed to filter out the colon (if exists) */ 199 int first = 1; 200 int i; 201 202 /*if (ptr == authority->ptr) return;*//*(no authority checked earlier)*/ 203 204 if (*authority->ptr == '[') { /* authority is IPv6 literal address */ 205 colon = ptr; 206 if (ptr[-1] != ']') { 207 do { --ptr; } while (ptr > authority->ptr && ptr[-1] != ']'); 208 if (*ptr != ':') return; /*(should not happen for valid authority)*/ 209 colon = ptr; 210 } 211 ptr = authority->ptr; 212 array_set_key_value(host,CONST_STR_LEN("%0"),ptr,colon-ptr); 213 return; 214 } 215 216 /* first, find the domain + tld */ 217 for(; ptr > authority->ptr; --ptr) { 218 if(*ptr == '.') { 219 if(first) first = 0; 220 else break; 221 } else if(*ptr == ':') { 222 colon = ptr; 223 first = 1; 224 } 225 } 226 227 /* if we stopped at a dot, skip the dot */ 228 if (*ptr == '.') ptr++; 229 array_set_key_value(host, CONST_STR_LEN("%0"), ptr, colon-ptr); 230 231 /* if the : is not the start of the authority, go on parsing the hostname */ 232 233 if (colon != authority->ptr) { 234 for(ptr = colon - 1, i = 1; ptr > authority->ptr; --ptr) { 235 if(*ptr == '.') { 236 if (ptr != colon - 1) { 237 /* is something between the dots */ 238 buffer_copy_string_len(key, CONST_STR_LEN("%")); 239 buffer_append_int(key, i++); 240 array_set_key_value(host, CONST_BUF_LEN(key), ptr+1, colon-ptr-1); 241 } 242 colon = ptr; 243 } 244 } 245 246 /* if the . is not the first charactor of the hostname */ 247 if (colon != ptr) { 248 buffer_copy_string_len(key, CONST_STR_LEN("%")); 249 buffer_append_int(key, i /* ++ */); 250 array_set_key_value(host, CONST_BUF_LEN(key), ptr, colon-ptr); 251 } 252 } 253 } 254 255 #define PATCH(x) \ 256 p->conf.x = s->x; 257 static int mod_evhost_patch_connection(server *srv, connection *con, plugin_data *p) { 258 size_t i, j; 259 plugin_config *s = p->config_storage[0]; 260 261 PATCH(path_pieces); 262 PATCH(len); 263 264 /* skip the first, the global context */ 265 for (i = 1; i < srv->config_context->used; i++) { 266 if (!config_check_cond(con, i)) continue; /* condition not matched */ 267 268 data_config *dc = (data_config *)srv->config_context->data[i]; 269 s = p->config_storage[i]; 270 271 /* merge config */ 272 for (j = 0; j < dc->value->used; j++) { 273 data_unset *du = dc->value->data[j]; 274 275 if (buffer_is_equal_string(&du->key, CONST_STR_LEN("evhost.path-pattern"))) { 276 PATCH(path_pieces); 277 PATCH(len); 278 } 279 } 280 } 281 282 return 0; 283 } 284 #undef PATCH 285 286 287 static void mod_evhost_build_doc_root_path(buffer *b, array *parsed_host, buffer *authority, buffer **path_pieces, const size_t npieces) { 288 array_reset_data_strings(parsed_host); 289 mod_evhost_parse_host(b, parsed_host, authority); 290 buffer_clear(b); 291 292 for (size_t i = 0; i < npieces; ++i) { 293 const char *ptr = path_pieces[i]->ptr; 294 if (*ptr == '%') { 295 const data_string *ds; 296 297 if (*(ptr+1) == '%') { 298 /* %% */ 299 buffer_append_string_len(b, CONST_STR_LEN("%")); 300 } else if (*(ptr+1) == '_' ) { 301 /* %_ == full hostname */ 302 char *colon = strchr(authority->ptr, ':'); 303 304 if(colon == NULL) { 305 buffer_append_string_buffer(b, authority); /* adds fqdn */ 306 } else { 307 /* strip the port out of the authority-part of the URI scheme */ 308 buffer_append_string_len(b, authority->ptr, colon - authority->ptr); /* adds fqdn */ 309 } 310 } else if (ptr[1] == '{' ) { 311 char s[3] = "% "; 312 s[1] = ptr[2]; /*(assumes single digit before '.', and, optionally, '.' and single digit after '.')*/ 313 if (NULL != (ds = (data_string *)array_get_element_klen(parsed_host, s, 2))) { 314 if (ptr[3] != '.' || ptr[4] == '0') { 315 buffer_append_string_buffer(b, &ds->value); 316 } else { 317 if ((size_t)(ptr[4]-'0') <= buffer_string_length(&ds->value)) { 318 buffer_append_string_len(b, ds->value.ptr+(ptr[4]-'0')-1, 1); 319 } 320 } 321 } else { 322 /* unhandled %-sequence */ 323 } 324 } else if (NULL != (ds = (data_string *)array_get_element_klen(parsed_host, CONST_BUF_LEN(path_pieces[i])))) { 325 buffer_append_string_buffer(b, &ds->value); 326 } else { 327 /* unhandled %-sequence */ 328 } 329 } else { 330 buffer_append_string_buffer(b, path_pieces[i]); 331 } 332 } 333 334 buffer_append_slash(b); 335 } 336 337 static handler_t mod_evhost_uri_handler(server *srv, connection *con, void *p_d) { 338 plugin_data *p = p_d; 339 stat_cache_entry *sce = NULL; 340 341 /* not authority set */ 342 if (buffer_string_is_empty(con->uri.authority)) return HANDLER_GO_ON; 343 344 mod_evhost_patch_connection(srv, con, p); 345 346 /* missing even default(global) conf */ 347 if (0 == p->conf.len) { 348 return HANDLER_GO_ON; 349 } 350 351 mod_evhost_build_doc_root_path(p->tmp_buf, p->split_vals, con->uri.authority, p->conf.path_pieces, p->conf.len); 352 353 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, p->tmp_buf, &sce)) { 354 log_error_write(srv, __FILE__, __LINE__, "sb", strerror(errno), p->tmp_buf); 355 } else if(!S_ISDIR(sce->st.st_mode)) { 356 log_error_write(srv, __FILE__, __LINE__, "sb", "not a directory:", p->tmp_buf); 357 } else { 358 buffer_copy_buffer(con->physical.doc_root, p->tmp_buf); 359 } 360 361 return HANDLER_GO_ON; 362 } 363 364 int mod_evhost_plugin_init(plugin *p); 365 int mod_evhost_plugin_init(plugin *p) { 366 p->version = LIGHTTPD_VERSION_ID; 367 p->name = "evhost"; 368 p->init = mod_evhost_init; 369 p->set_defaults = mod_evhost_set_defaults; 370 p->handle_docroot = mod_evhost_uri_handler; 371 p->cleanup = mod_evhost_free; 372 373 return 0; 374 } 375 376 /* eof */ 377