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