src/server/daemon/config.c

branch
webdav
changeset 259
0b8692959d37
parent 256
19259b6c5cf7
child 260
4779a6fb4fbe
equal deleted inserted replaced
252:5653a9626cc0 259:0b8692959d37
53 #include "../util/pblock.h" 53 #include "../util/pblock.h"
54 #include "../util/util.h" 54 #include "../util/util.h"
55 #include "../util/atomic.h" 55 #include "../util/atomic.h"
56 #include "ucx/buffer.h" 56 #include "ucx/buffer.h"
57 57
58 pool_handle_t *cfg_pool; 58 pool_handle_t *init_pool;
59 59
60 // TODO: Funktion für ConfigDirective -> directive
61 // TODO: Funktion für UcxList parameter list -> pblock
62 60
63 int load_init_conf(char *file) { 61 int load_init_conf(char *file) {
64 log_ereport(LOG_VERBOSE, "load_init_conf"); 62 log_ereport(LOG_VERBOSE, "load_init_conf");
65 63
66 InitConfig *cfg = load_init_config(file); 64 InitConfig *cfg = load_init_config(file);
68 log_ereport(LOG_FAILURE, "Cannot load init.conf"); 66 log_ereport(LOG_FAILURE, "Cannot load init.conf");
69 return 1; 67 return 1;
70 } 68 }
71 UcxAllocator *mp = cfg->parser.mp; 69 UcxAllocator *mp = cfg->parser.mp;
72 70
73 cfg_pool = pool_create(); // one pool for one Configuration 71 init_pool = pool_create(); // one pool for one Configuration
74 UcxList *dirs = cfg->directives; 72 UcxList *dirs = cfg->directives;
75 while(dirs != NULL) { 73 while(dirs != NULL) {
76 ConfigDirective *dir = dirs->data; 74 ConfigDirective *dir = dirs->data;
77 75
78 /* create NSAPI directive */ 76 /* create NSAPI directive */
79 directive *d = malloc(sizeof(directive)); 77 directive *d = malloc(sizeof(directive));
80 d->param = pblock_create_pool(cfg_pool, 8); 78 d->param = pblock_create_pool(init_pool, 8);
81 UcxList *param = cfg_param_list(dir->value, mp); 79 UcxList *param = cfg_param_list(dir->value, mp);
82 while(param != NULL) { 80 while(param != NULL) {
83 ConfigParam *p = param->data; 81 ConfigParam *p = param->data;
84 pblock_nvlinsert( 82 pblock_nvlinsert(
85 p->name.ptr, 83 p->name.ptr,
125 free_init_config(cfg); 123 free_init_config(cfg);
126 124
127 return 0; 125 return 0;
128 } 126 }
129 127
130 ServerConfiguration* load_server_conf(ServerConfiguration *old, char *file) { 128 ServerConfiguration* load_server_conf(char *file) {
131 log_ereport(LOG_VERBOSE, "load_server_conf"); 129 log_ereport(LOG_VERBOSE, "load_server_conf");
132 130
133 ServerConfig *serverconf = load_server_config(file); 131 ServerConfig *serverconf = serverconfig_load(file);
134 if(serverconf == NULL) { 132 if(!serverconf) {
135 log_ereport(LOG_FAILURE, "Cannot load server.conf"); 133 log_ereport(LOG_FAILURE, "Cannot load server.conf");
136 } 134 return NULL;
137 ServerConfiguration *serverconfig = calloc(1, sizeof(ServerConfiguration)); 135 }
136
137 pool_handle_t *pool = pool_create();
138
139
140 ServerConfiguration *serverconfig = pool_calloc(pool, 1, sizeof(ServerConfiguration));
138 serverconfig->ref = 1; 141 serverconfig->ref = 1;
139 serverconfig->pool = pool_create(); 142 serverconfig->pool = pool;
140 serverconfig->listeners = NULL; 143 serverconfig->listeners = NULL;
141 serverconfig->host_vs = ucx_map_new(16); 144 serverconfig->host_vs = ucx_map_new(16);
142 serverconfig->authdbs = ucx_map_new(16); 145 serverconfig->authdbs = ucx_map_new(16);
146
147 UcxAllocator allocator = util_pool_allocator(serverconfig->pool);
148 serverconfig->a = pool_malloc(pool, sizeof(UcxAllocator));
149 *serverconfig->a = allocator;
150
143 // TODO: init serverconfig stuff 151 // TODO: init serverconfig stuff
144 152
145 153
146 /* 154 /*
147 * convert ServerConfig to ServerConfiguration 155 * convert ServerConfig to ServerConfiguration
159 /* 167 /*
160 * free stuff on error 168 * free stuff on error
161 */ 169 */
162 170
163 // init logfile first 171 // init logfile first
164 UcxList *lfl = ucx_map_sstr_get(serverconf->objects, sstrn("LogFile", 7)); 172 UcxList *list = NULL;
165 if(lfl != NULL) { 173
166 ServerConfigObject *logobj = lfl->data; 174 list = serverconfig_get_node_list(serverconf->root, CONFIG_NODE_OBJECT, SC("LogFile"));
167 if(logobj == NULL) { 175 if(list) {
176 ConfigNode *logobj = list->data;
177 if(!logobj) {
168 // error 178 // error
169 return NULL; 179 return NULL; // TODO: fix memory leak
170 } 180 }
171 181
172 int ret = cfg_handle_logfile(serverconfig, logobj); 182 int ret = cfg_handle_logfile(serverconfig, logobj);
173 if(ret != 0) { 183 if(ret != 0) {
174 // cannot initialize log file 184 // cannot initialize log file
175 return NULL; 185 return NULL; // TODO: fix memory leak
176 } 186 }
177 } else { 187 } else {
178 // horrible error 188 // horrible error
179 return NULL; 189 return NULL;
180 } 190 }
191 ucx_list_free(list);
181 192
182 UcxList *list = ucx_map_sstr_get(serverconf->objects, sstrn("Runtime", 7)); 193 list = serverconfig_get_node_list(serverconf->root, CONFIG_NODE_OBJECT, SC("Runtime"));
183 UCX_FOREACH(elm, list) { 194 UCX_FOREACH(elm, list) {
184 ServerConfigObject *scfgobj = elm->data; 195 ConfigNode *runtimeobj = elm->data;
185 if(cfg_handle_runtime(serverconfig, scfgobj)) { 196 if(cfg_handle_runtime(serverconfig, runtimeobj)) {
186 // error 197 // error
187 return NULL; 198 return NULL;
188 } 199 }
189 } 200 }
190 201 ucx_list_free(list);
191 list = ucx_map_sstr_get(serverconf->objects, sstrn("Threadpool", 10)); 202
203 list = serverconfig_get_node_list(serverconf->root, CONFIG_NODE_OBJECT, SC("Threadpool"));
192 UCX_FOREACH(elm, list) { 204 UCX_FOREACH(elm, list) {
193 if(cfg_handle_threadpool(serverconfig, elm->data)) { 205 if(cfg_handle_threadpool(serverconfig, elm->data)) {
194 return NULL; 206 return NULL;
195 } 207 }
196 } 208 }
209 ucx_list_free(list);
197 // check thread pool config 210 // check thread pool config
198 if(check_thread_pool_cfg() != 0) { 211 if(check_thread_pool_cfg() != 0) {
199 /* critical error */ 212 /* critical error */
200 return NULL; 213 return NULL;
201 } 214 }
202 215
203 list = ucx_map_sstr_get(serverconf->objects, sstrn("EventHandler", 12)); 216 list = serverconfig_get_node_list(serverconf->root, CONFIG_NODE_OBJECT, SC("EventHandler"));
204 UCX_FOREACH(elm, list) { 217 UCX_FOREACH(elm, list) {
205 if(cfg_handle_eventhandler( 218 if(cfg_handle_eventhandler(
206 serverconfig, (ServerConfigObject*)elm->data)) { 219 serverconfig, elm->data)) {
207 // error 220 // error
208 return NULL; 221 return NULL;
209 } 222 }
210 } 223 }
211 // check event handler config 224 // check event handler config
212 if(check_event_handler_cfg() != 0) { 225 if(check_event_handler_cfg() != 0) {
213 /* critical error */ 226 /* critical error */
214 return NULL; 227 return NULL;
215 } 228 }
216 229 ucx_list_free(list);
217 list = ucx_map_sstr_get(serverconf->objects, sstrn("AccessLog", 9)); 230
231 list = serverconfig_get_node_list(serverconf->root, CONFIG_NODE_OBJECT, SC("AccessLog"));
218 UCX_FOREACH(elm, list) { 232 UCX_FOREACH(elm, list) {
219 ServerConfigObject *scfgobj = elm->data; 233 ConfigNode *scfgobj = elm->data;
220 if(cfg_handle_accesslog(serverconfig, scfgobj)) { 234 if(cfg_handle_accesslog(serverconfig, scfgobj)) {
221 return NULL; 235 return NULL;
222 } 236 }
223 } 237 }
224 238 ucx_list_free(list);
225 list = ucx_map_sstr_get(serverconf->objects, sstrn("AuthDB", 6)); 239
240 list = serverconfig_get_node_list(serverconf->root, CONFIG_NODE_OBJECT, SC("AuthDB"));
226 UCX_FOREACH(elm, list) { 241 UCX_FOREACH(elm, list) {
227 ServerConfigObject *scfgobj = elm->data; 242 ConfigNode *scfgobj = elm->data;
228 if(cfg_handle_authdb(serverconfig, scfgobj)) { 243 if(cfg_handle_authdb(serverconfig, scfgobj)) {
229 return NULL; 244 return NULL;
230 } 245 }
231 } 246 }
232 247 ucx_list_free(list);
233 list = ucx_map_sstr_get(serverconf->objects, sstrn("Listener", 8)); 248
249 list = serverconfig_get_node_list(serverconf->root, CONFIG_NODE_OBJECT, SC("Listener"));
234 UCX_FOREACH(elm, list) { 250 UCX_FOREACH(elm, list) {
235 ServerConfigObject *scfgobj = elm->data; 251 ConfigNode *scfgobj = elm->data;
236 if(cfg_handle_listener(serverconfig, scfgobj)) { 252 if(cfg_handle_listener(serverconfig, scfgobj)) {
237 return NULL; 253 return NULL;
238 } 254 }
239 } 255 }
240 256 ucx_list_free(list);
241 list = ucx_map_sstr_get(serverconf->objects, sstrn("VirtualServer", 13)); 257
258 list = serverconfig_get_node_list(serverconf->root, CONFIG_NODE_OBJECT, SC("VirtualServer"));
242 UCX_FOREACH(elm, list) { 259 UCX_FOREACH(elm, list) {
243 ServerConfigObject *scfgobj = elm->data; 260 ConfigNode *scfgobj = elm->data;
244 if(cfg_handle_vs(serverconfig, scfgobj)) { 261 if(cfg_handle_vs(serverconfig, scfgobj)) {
245 return NULL; 262 return NULL;
246 } 263 }
247 } 264 }
265 ucx_list_free(list);
248 266
249 267
250 // set VirtualServer for all listeners 268 // set VirtualServer for all listeners
251 UcxList *ls = serverconfig->listeners; 269 UcxList *ls = serverconfig->listeners;
252 while(ls) { 270 while(ls) {
266 } 284 }
267 285
268 ls = ls->next; 286 ls = ls->next;
269 } 287 }
270 288
271 free_server_config(serverconf); 289 serverconfig_free(serverconf);
290
272 return serverconfig; 291 return serverconfig;
273 } 292 }
274 293
275 void cfg_ref(ServerConfiguration *cfg) { 294 void cfg_ref(ServerConfiguration *cfg) {
276 ws_atomic_inc32(&cfg->ref); 295 ws_atomic_inc32(&cfg->ref);
277 } 296 }
278 297
279 void cfg_unref(ServerConfiguration *cfg) { 298 void cfg_unref(ServerConfiguration *cfg) {
280 uint32_t ref = ws_atomic_dec32(&cfg->ref); 299 uint32_t ref = ws_atomic_dec32(&cfg->ref);
281 if(ref == 0) { 300 if(ref == 0) {
282 // TODO: free configuration 301 pool_destroy(cfg->pool);
283 printf("free ServerConfiguration %"PRIxPTR"\n", (intptr_t)cfg);
284 } 302 }
285 } 303 }
286 304
287 305
288 void init_server_config_parser() { 306 void init_server_config_parser() {
289 307
290 } 308 }
291 309
292 int cfg_handle_runtime(ServerConfiguration *cfg, ServerConfigObject *obj) { 310 int cfg_handle_runtime(ServerConfiguration *cfg, ConfigNode *obj) {
293 sstr_t user = cfg_directivelist_get_str(obj->directives, sstr("User")); 311 scstr_t user = serverconfig_directive_value(obj, SC("User"));
294 if(user.ptr) { 312 if(user.ptr) {
295 cfg->user = sstrdup_pool(cfg->pool, user); 313 cfg->user = sstrdup_a(cfg->a, user);
296 } 314 }
297 sstr_t tmp = cfg_directivelist_get_str(obj->directives, sstr("Temp")); 315 scstr_t tmp = serverconfig_directive_value(obj, SC("Temp"));
298 if(tmp.ptr) { 316 if(tmp.ptr) {
299 cfg->tmp = sstrdup_pool(cfg->pool, tmp); 317 cfg->tmp = sstrdup_a(cfg->a, tmp);
300 } else { 318 } else {
319 // TODO: do this check after all config loading is done
301 log_ereport(LOG_MISCONFIG, "no temporary directory specified"); 320 log_ereport(LOG_MISCONFIG, "no temporary directory specified");
302 return -1; 321 return -1;
303 } 322 }
304 323
305 // mime file 324 // mime file
306 sstr_t mf = cfg_directivelist_get_str(obj->directives, sstr("MimeFile")); 325 scstr_t mf = serverconfig_directive_value(obj, SC("MimeFile"));
307 sstr_t base = sstr("config/"); 326 scstr_t base = SC("config/");
308 sstr_t file = sstrcat(2, base, mf); 327 sstr_t file = sstrcat(2, base, mf);
309 328
310 ConfigFile *f = cfgmgr_get_file(file); 329 if(mime_conf_load(cfg, file)) {
311 if(f == NULL) { 330 return -1;
312 f = malloc(sizeof(ConfigFile)); 331 }
313 f->data = NULL;
314 f->file = sstrdup(file);
315 f->reload = mime_conf_reload;
316 f->last_modified = 0;
317
318 // load the file content
319 //f->reload(f, cfg);
320 if(cfgmgr_reload_file(f, cfg, NULL)) {
321 free(f->file.ptr);
322 free(f);
323
324 free(file.ptr);
325 return -1;
326 }
327 cfgmgr_attach_file(f);
328 }
329
330 cfg->mimetypes = f->data;
331 332
332 free(file.ptr); 333 free(file.ptr);
333 return 0; 334 return 0;
334 } 335 }
335 336
336 int cfg_handle_logfile(ServerConfiguration *cfg, ServerConfigObject *obj) { 337 int cfg_handle_logfile(ServerConfiguration *cfg, ConfigNode *obj) {
337 sstr_t file = cfg_directivelist_get_str(obj->directives, sstr("File")); 338 scstr_t file = serverconfig_directive_value(obj, SC("File"));
338 sstr_t lvl = cfg_directivelist_get_str(obj->directives, sstr("Level")); 339 scstr_t lvl = serverconfig_directive_value(obj, SC("Level"));
339 340
340 if(file.ptr == NULL || lvl.ptr == NULL) { 341 if(file.ptr == NULL || lvl.ptr == NULL) {
341 /* missing log file parameters */ 342 /* missing log file parameters */
342 return -1; 343 return -1;
343 } 344 }
344 345
345 LogConfig logcfg; 346 LogConfig logcfg;
346 logcfg.file = sstrdup(file).ptr; 347 logcfg.file = file.ptr;
347 logcfg.level = sstrdup(lvl).ptr; 348 logcfg.level = lvl.ptr;
348 logcfg.log_stdout = 0; 349 logcfg.log_stdout = 0;
349 logcfg.log_stderr = 0; 350 logcfg.log_stderr = 0;
350 /* TODO: stdout, stderr config */ 351 /* TODO: stdout, stderr config */
351 352
352 int ret = init_log_file(&logcfg); 353 int ret = init_log_file(&logcfg);
353
354 free(logcfg.file);
355 free(logcfg.level);
356 354
357 return ret; 355 return ret;
358 } 356 }
359 357
360 int cfg_handle_threadpool(ServerConfiguration *cfg, ServerConfigObject *obj) { 358 int cfg_handle_threadpool(ServerConfiguration *cfg, ConfigNode *obj) {
361 ThreadPoolConfig poolcfg; 359 ThreadPoolConfig poolcfg;
362 poolcfg.min_threads = 4; 360 poolcfg.min_threads = 4;
363 poolcfg.min_threads = 4; 361 poolcfg.min_threads = 4;
364 poolcfg.max_threads = 8; 362 poolcfg.max_threads = 8;
365 poolcfg.queue_size = 64; 363 poolcfg.queue_size = 64;
366 poolcfg.stack_size = 262144; 364 poolcfg.stack_size = 262144;
367 365
368 sstr_t name = cfg_directivelist_get_str( 366 scstr_t name = serverconfig_directive_value(obj, SC("Name"));
369 obj->directives, 367 scstr_t min = serverconfig_directive_value(obj, SC("MinThreads"));
370 sstr("Name")); 368 scstr_t max = serverconfig_directive_value(obj, SC("MaxThreads"));
371 sstr_t min = cfg_directivelist_get_str( 369 scstr_t stack = serverconfig_directive_value(obj, SC("StackSize"));
372 obj->directives, 370 scstr_t queue = serverconfig_directive_value(obj, SC("QueueSize"));
373 sstr("MinThreads"));
374 sstr_t max = cfg_directivelist_get_str(
375 obj->directives,
376 sstr("MaxThreads"));
377 sstr_t stack = cfg_directivelist_get_str(
378 obj->directives,
379 sstr("StackSize"));
380 sstr_t queue = cfg_directivelist_get_str(
381 obj->directives,
382 sstr("QueueSize"));
383 // TODO: Type 371 // TODO: Type
384 372
385 if(name.length == 0) { 373 if(name.length == 0) {
386 // TODO: log error 374 // TODO: log error
387 return 1; 375 return 1;
388 } 376 }
389 377
390 if(min.length != 0) { 378 if(min.length != 0) {
391 min = sstrdup(min);
392 poolcfg.min_threads = atoi(min.ptr); 379 poolcfg.min_threads = atoi(min.ptr);
393 free(min.ptr);
394 } 380 }
395 381
396 if(max.length != 0) { 382 if(max.length != 0) {
397 max = sstrdup(max);
398 poolcfg.max_threads = atoi(max.ptr); 383 poolcfg.max_threads = atoi(max.ptr);
399 free(max.ptr);
400 } 384 }
401 385
402 if(stack.length != 0) { 386 if(stack.length != 0) {
403 stack = sstrdup(stack);
404 poolcfg.stack_size = atoi(stack.ptr); 387 poolcfg.stack_size = atoi(stack.ptr);
405 free(stack.ptr);
406 } 388 }
407 389
408 if(queue.length != 0) { 390 if(queue.length != 0) {
409 queue = sstrdup(queue);
410 poolcfg.queue_size = atoi(queue.ptr); 391 poolcfg.queue_size = atoi(queue.ptr);
411 free(queue.ptr);
412 } 392 }
413 393
414 create_threadpool(name, &poolcfg); 394 create_threadpool(name, &poolcfg);
415 395
416 return 0; 396 return 0;
417 } 397 }
418 398
419 int cfg_handle_eventhandler(ServerConfiguration *c, ServerConfigObject *obj) { 399 int cfg_handle_eventhandler(ServerConfiguration *c, ConfigNode *obj) {
420 EventHandlerConfig evcfg; 400 EventHandlerConfig evcfg;
421 401
422 sstr_t name = cfg_directivelist_get_str(obj->directives, sstr("Name")); 402 scstr_t name = serverconfig_directive_value(obj, SC("Name"));
423 sstr_t threads = cfg_directivelist_get_str( 403 scstr_t threads = serverconfig_directive_value(obj, SC("Threads"));
424 obj->directives, 404 scstr_t isdefault = serverconfig_directive_value(obj, SC("Default"));
425 sstr("Threads"));
426 sstr_t isdefault = cfg_directivelist_get_str(
427 obj->directives,
428 sstr("Default"));
429 405
430 evcfg.name = name; 406 evcfg.name = name;
431 407
432 sstr_t s = sstrdup(threads); 408 evcfg.nthreads = atoi(threads.ptr);
433 evcfg.nthreads = atoi(s.ptr);
434 free(s.ptr);
435 409
436 evcfg.isdefault = util_getboolean(isdefault.ptr, 0); 410 evcfg.isdefault = util_getboolean(isdefault.ptr, 0);
437 411
438 return create_event_handler(&evcfg); 412 return create_event_handler(&evcfg);
439 } 413 }
440 414
441 int cfg_handle_accesslog(ServerConfiguration *cfg, ServerConfigObject *obj) { 415 int cfg_handle_accesslog(ServerConfiguration *cfg, ConfigNode *obj) {
442 // TODO: use a name to identify the log file 416 // TODO: use a name to identify the log file
443 417
444 sstr_t file = cfg_directivelist_get_str(obj->directives, sstr("File")); 418 scstr_t file = serverconfig_directive_value(obj, SC("File"));
445 if(file.ptr == NULL) { 419 if(file.ptr == NULL) {
446 return 0; 420 return 0;
447 } 421 }
448 sstr_t format; 422 sstr_t format;
449 format.ptr = NULL; 423 format.ptr = NULL;
454 if(!log_file) { 428 if(!log_file) {
455 // TODO: error/warning 429 // TODO: error/warning
456 return 0; 430 return 0;
457 } 431 }
458 AccessLog *log = pool_malloc(cfg->pool, sizeof(AccessLog)); 432 AccessLog *log = pool_malloc(cfg->pool, sizeof(AccessLog));
459 log->file = sstrdup_pool(cfg->pool, file); 433 log->file = sstrdup_a(cfg->a, file);
460 log->format = format; 434 log->format = format;
461 log->log = log_file; 435 log->log = log_file;
462 cfg->logfiles = ucx_list_append(cfg->logfiles, log); 436 cfg->logfiles = ucx_list_append_a(cfg->a, cfg->logfiles, log);
463 437
464 if(!cfg->default_log) { 438 if(!cfg->default_log) {
465 cfg->default_log = log; 439 cfg->default_log = log;
466 } 440 }
467 441
468 return 0; 442 return 0;
469 } 443 }
470 444
471 int cfg_handle_authdb(ServerConfiguration *cfg, ServerConfigObject *obj) { 445 int cfg_handle_authdb(ServerConfiguration *cfg, ConfigNode *obj) {
472 sstr_t name = cfg_directivelist_get_str(obj->directives, sstr("Name")); 446 scstr_t name = serverconfig_directive_value(obj, SC("Name"));
473 sstr_t type = cfg_directivelist_get_str(obj->directives, sstr("Type")); 447 scstr_t type = serverconfig_directive_value(obj, SC("Type"));
448
449 AuthDB *authdb = NULL;
474 450
475 if(!sstrcmp(type, sstr("ldap"))) { 451 if(!sstrcmp(type, sstr("ldap"))) {
476 LDAPConfig conf; 452 LDAPConfig conf;
477 453
478 sstr_t host = cfg_directivelist_get_str( 454 scstr_t host = serverconfig_directive_value(obj, SC("Host"));
479 obj->directives, 455 scstr_t port = serverconfig_directive_value( obj, SC("Port"));
480 sstr("Host")); 456 scstr_t basedn = serverconfig_directive_value(obj, SC("BaseDN"));
481 sstr_t port = cfg_directivelist_get_str( 457 scstr_t binddn = serverconfig_directive_value(obj, SC("BindDN"));
482 obj->directives, 458 scstr_t basepw = serverconfig_directive_value(obj, SC("BindPW"));
483 sstr("Port")); 459
484 sstr_t basedn = cfg_directivelist_get_str( 460 conf.hostname = sstrdup_a(cfg->a, host).ptr;
485 obj->directives,
486 sstr("BaseDN"));
487 sstr_t binddn = cfg_directivelist_get_str(
488 obj->directives,
489 sstr("BindDN"));
490 sstr_t basepw = cfg_directivelist_get_str(
491 obj->directives,
492 sstr("BindPW"));
493
494 host = sstrdup(host);
495 port = sstrdup(port);
496 basedn = sstrdup(basedn);
497 binddn = sstrdup(binddn);
498 basepw = sstrdup(basepw);
499
500 conf.hostname = host.ptr;
501 conf.port = atoi(port.ptr); 461 conf.port = atoi(port.ptr);
502 conf.basedn = basedn.ptr; 462 conf.basedn = sstrdup_a(cfg->a, basedn).ptr;
503 conf.binddn = binddn.ptr; 463 conf.binddn = sstrdup_a(cfg->a, binddn).ptr;
504 conf.bindpw = basepw.ptr; 464 conf.bindpw = sstrdup_a(cfg->a, basepw).ptr;
505 465
506 name = sstrdup(name); 466 authdb = create_ldap_authdb(cfg, name.ptr, &conf);
507
508 AuthDB *authdb = create_ldap_authdb(name.ptr, &conf);
509 ucx_map_sstr_put(cfg->authdbs, name, authdb);
510
511 // TODO: create_ldap_authdb should copy the strings
512 /*
513 free(host.ptr);
514 free(port.ptr);
515 free(basedn.ptr);
516 free(binddn.ptr);
517 free(basepw.ptr);
518 free(name.ptr);
519 */
520
521 } else if(!sstrcmp(type, sstr("keyfile"))) { 467 } else if(!sstrcmp(type, sstr("keyfile"))) {
522 // we only need the file parameter 468 // we only need the file parameter
523 sstr_t file = cfg_directivelist_get_str( 469 scstr_t file = serverconfig_directive_value(obj, SC("File"));
524 obj->directives,
525 sstr("File"));
526 if(file.length == 0) { 470 if(file.length == 0) {
527 log_ereport( 471 log_ereport(
528 LOG_MISCONFIG, 472 LOG_MISCONFIG,
529 "missing File parameter for keyfile authdb"); 473 "missing File parameter for keyfile authdb");
530 return 1; 474 return 1;
531 } 475 }
532 476
533 // load keyfile 477 // load keyfile
534 ConfigFile *f = cfgmgr_get_file(file); 478 authdb = keyfile_load(cfg, file);
535 if(f == NULL) { 479 }
536 f = malloc(sizeof(ConfigFile)); 480
537 f->data = NULL; 481 if(authdb) {
538 f->file = sstrdup(file); 482 if(ucx_map_sstr_put(cfg->authdbs, name, authdb)) {
539 f->reload = keyfile_reload; 483 return -1;
540 f->last_modified = 0; 484 }
541 //f->reload(f, cfg); 485 }
542 if(cfgmgr_reload_file(f, cfg, NULL)) { 486
543 free(f->file.ptr);
544 free(f);
545 return -1;
546 }
547 cfgmgr_attach_file(f);
548 }
549
550 // add keyfile authdb
551 Keyfile *keyfile = f->data;
552 keyfile->authdb.name = sstrdup(name).ptr;
553 ucx_map_sstr_put(cfg->authdbs, name, keyfile);
554 }
555
556 return 0; 487 return 0;
557 } 488 }
558 489
559 int cfg_handle_listener(ServerConfiguration *cfg, ServerConfigObject *obj) { 490 int cfg_handle_listener(ServerConfiguration *cfg, ConfigNode *obj) {
560 ListenerConfig lc; 491 ListenerConfig lc;
561 ZERO(&lc, sizeof(ListenerConfig)); 492 ZERO(&lc, sizeof(ListenerConfig));
562 lc.cfg = cfg; 493 lc.cfg = cfg;
563 lc.port = 8080; 494 lc.port = 8080;
564 lc.nacceptors = 1; 495 lc.nacceptors = 1;
565 496
497 scstr_t name = serverconfig_directive_value(obj, SC("Name"));
498 scstr_t port = serverconfig_directive_value(obj, SC("Port"));
499 scstr_t vs = serverconfig_directive_value(obj, SC("DefaultVS"));
500 scstr_t thrp = serverconfig_directive_value(obj, SC("Threadpool"));
501 scstr_t blck = serverconfig_directive_value(obj, SC("BlockingIO"));
502
566 // TODO: use sstrdup_pool? 503 // TODO: use sstrdup_pool?
567 lc.name = sstrdup(cfg_directivelist_get_str( 504 lc.name = sstrdup(name);
568 obj->directives, 505 lc.port = atoi(port.ptr);
569 sstr("Name"))); 506 lc.vs = sstrdup(vs);
570 lc.port = atoi(cfg_directivelist_get_str( 507 lc.threadpool = sstrdup(thrp);
571 obj->directives, 508
572 sstr("Port")).ptr); 509 lc.blockingio = util_getboolean_s(blck, WS_FALSE);
573 lc.vs = sstrdup(cfg_directivelist_get_str( 510
574 obj->directives, 511 scstr_t ssl = serverconfig_directive_value(obj, SC("SSL"));
575 sstr("DefaultVS")));
576 lc.threadpool = sstrdup(cfg_directivelist_get_str(
577 obj->directives,
578 sstr("Threadpool")));
579
580 sstr_t blockingio = cfg_directivelist_get_str(
581 obj->directives,
582 sstr("BlockingIO"));
583 if(blockingio.ptr) {
584 lc.blockingio = util_getboolean_s(blockingio, WS_FALSE);
585 }
586
587 sstr_t ssl = cfg_directivelist_get_str(obj->directives, S("SSL"));
588 if(util_getboolean_s(ssl, WS_FALSE)) { 512 if(util_getboolean_s(ssl, WS_FALSE)) {
589 sstr_t cert = cfg_directivelist_get_str(obj->directives, S("Cert")); 513 scstr_t cert = serverconfig_directive_value(obj, SC("Cert"));
590 sstr_t privkey = cfg_directivelist_get_str(obj->directives, S("Key")); 514 scstr_t privkey = serverconfig_directive_value(obj, SC("Key"));
591 sstr_t chain = cfg_directivelist_get_str(obj->directives, S("CertChain")); 515 scstr_t chain = serverconfig_directive_value(obj, SC("CertChain"));
592 sstr_t disableprot = cfg_directivelist_get_str( 516 scstr_t disableprot = serverconfig_directive_value(obj, SC("SSLDisableProtocol"));
593 obj->directives,
594 S("SSLDisableProtocol"));
595 517
596 WSBool config_ok = WS_TRUE; 518 WSBool config_ok = WS_TRUE;
597 // TODO: log error 519 // TODO: log error
598 if(!cert.ptr && !chain.ptr) { 520 if(!cert.ptr && !chain.ptr) {
599 log_ereport( 521 log_ereport(
626 HttpListener *listener = http_listener_create(&lc); 548 HttpListener *listener = http_listener_create(&lc);
627 if(!listener) { 549 if(!listener) {
628 return 1; 550 return 1;
629 } 551 }
630 552
631 listener->default_vs.vs_name = lc.vs.ptr; 553 listener->default_vs.vs_name = sstrdup_a(cfg->a, lc.vs).ptr;
632 cfg->listeners = ucx_list_append(cfg->listeners, listener); 554 cfg->listeners = ucx_list_append_a(cfg->a, cfg->listeners, listener);
633 555
634 return 0; 556 return 0;
635 } 557 }
636 558
637 int cfg_handle_vs(ServerConfiguration *cfg, ServerConfigObject *obj) { 559 int cfg_handle_vs(ServerConfiguration *cfg, ConfigNode *obj) {
638 VirtualServer *vs = vs_new(); 560 VirtualServer *vs = vs_new();
639 561
640 vs->name = sstrdup(cfg_directivelist_get_str( 562 vs->name = sstrdup_a(cfg->a, serverconfig_directive_value(obj, SC("Name")));
641 obj->directives, 563 vs->host = sstrdup_a(cfg->a, serverconfig_directive_value(obj, SC("Host")));
642 sstr("Name"))); 564 vs->document_root = sstrdup_a(cfg->a, serverconfig_directive_value(obj, SC("DocRoot")));
643 vs->host = sstrdup(cfg_directivelist_get_str( 565
644 obj->directives, 566 scstr_t objfile = serverconfig_directive_value(obj, SC("ObjectFile"));
645 sstr("Host"))); 567 scstr_t aclfile = serverconfig_directive_value(obj, SC("ACLFile"));
646 vs->document_root = sstrdup(cfg_directivelist_get_str(
647 obj->directives,
648 sstr("DocRoot")));
649 sstr_t objfile = cfg_directivelist_get_str(
650 obj->directives,
651 sstr("ObjectFile"));
652 sstr_t aclfile = cfg_directivelist_get_str(
653 obj->directives,
654 sstr("ACLFile"));
655 568
656 // load the object config file 569 // load the object config file
657 sstr_t base = sstr("config/"); 570 sstr_t base = sstr("config/");
658 sstr_t file = sstrcat(2, base, objfile); 571 sstr_t file = sstrcat(2, base, objfile);
659 file = sstrcat(2, base, objfile); 572 // sstrcat with allocator because we want to keep the string
660 573 file = sstrcat_a(cfg->a, 2, base, objfile);
661 // the file is managed by the configuration manager 574
662 ConfigFile *f = cfgmgr_get_file(file); 575 HTTPObjectConfig *httpobj = objconf_load(cfg, file);
663 if(f == NULL) { 576 if(!httpobj) {
664 f = malloc(sizeof(ConfigFile)); 577 return -1;
665 f->data = NULL; 578 }
666 f->file = sstrdup(file); 579 vs->objectfile = file;
667 f->reload = object_conf_reload; 580 vs->objects = httpobj;
668 f->last_modified = 0;
669 //f->reload(f, cfg);
670 if(cfgmgr_reload_file(f, cfg, NULL)) {
671 free(f->file.ptr);
672 free(f);
673
674 free(file.ptr);
675 return -1;
676 }
677 cfgmgr_attach_file(f);
678 }
679 vs->objectfile = sstrdup(file);
680 vs->objects = (HTTPObjectConfig*)f->data;
681 free(file.ptr);
682 581
683 582
684 // load acl config file 583 // load acl config file
685 file = sstrcat(2, base, aclfile); 584 file = sstrcat(2, base, aclfile);
686 585
687 ConfigFile *aclf = cfgmgr_get_file(file); 586 ACLData *acldata = acl_conf_load(cfg, file);
688 if(aclf == NULL) { 587 if(!acldata) {
689 aclf = malloc(sizeof(ConfigFile)); 588 return -1;
690 aclf->data = NULL; 589 }
691 aclf->file = sstrdup(file); 590 vs->acls = acldata;
692 aclf->reload = acl_conf_reload; 591
693 aclf->last_modified = 0;
694 //aclf->reload(aclf, cfg);
695 if(cfgmgr_reload_file(aclf, cfg, NULL)) {
696 free(aclf->file.ptr);
697 free(aclf);
698
699 free(file.ptr);
700 return -1;
701 }
702 cfgmgr_attach_file(aclf);
703 }
704 vs->acls = aclf->data;
705 free(file.ptr); 592 free(file.ptr);
706 593
594
707 // set the access log for the virtual server 595 // set the access log for the virtual server
708 // TODO: don't use always the default 596 // TODO: don't always use the default
709 vs->log = cfg->default_log; 597 vs->log = cfg->default_log;
710 598
711 ucx_map_sstr_put(cfg->host_vs, vs->host, vs); 599 ucx_map_sstr_put(cfg->host_vs, vs->host, vs);
712 600
713 return 0; 601 return 0;
714 } 602 }
715 603
716 604
717 int object_conf_reload(ConfigFile *file, ServerConfiguration *cfg) { 605 static int convert_objconf(ServerConfiguration *scfg, ObjectConfig *cfg, HTTPObjectConfig *conf, sstr_t file) {
718 HTTPObjectConfig *old_conf = file->data; 606 pool_handle_t *pool = conf->pool;
719 file->data = load_obj_conf(file->file.ptr);
720 if(old_conf) {
721 object_conf_unref(old_conf);
722 }
723 if(file->data) {
724 return 0;
725 } else {
726 return 1;
727 }
728 }
729
730 void object_conf_ref(HTTPObjectConfig *conf) {
731 if(conf) {
732 ws_atomic_inc32(&conf->ref);
733 }
734 }
735
736 void object_conf_unref(HTTPObjectConfig *conf) {
737 uint32_t ref = ws_atomic_dec32(&conf->ref);
738 if(ref == 0) {
739 printf("free HTTPObjectConfig %"PRIxPTR"\n", (intptr_t)conf);
740 pool_destroy(conf->pool);
741 }
742 }
743
744 HTTPObjectConfig* load_obj_conf(char *file) {
745 log_ereport(LOG_VERBOSE, "load_obj_conf");
746
747 // new conf function test
748 ObjectConfig *cfg = load_object_config(file);
749 UcxAllocator *mp = cfg->parser.mp;
750 if(cfg == NULL) {
751 return NULL;
752 }
753
754 // create object config
755 pool_handle_t *pool = pool_create();
756 HTTPObjectConfig *conf = pool_calloc(pool, sizeof(HTTPObjectConfig), 1);
757 conf->pool = pool;
758
759 // convert ObjectConfig to HTTPObjectConfig
760
761 // add objects
762 conf->nobj = ucx_list_size(cfg->objects);
763 conf->objects = pool_calloc(pool, conf->nobj, sizeof(httpd_object*));
764 607
765 UcxList *objlist = cfg->objects; 608 UcxList *objlist = cfg->objects;
766 int i = 0; 609 int i = 0;
767 while(objlist != NULL) { 610 while(objlist != NULL) {
768 ConfigObject *cob = objlist->data; 611 ConfigObject *cob = objlist->data;
770 // get name and ppath 613 // get name and ppath
771 char *name = NULL; 614 char *name = NULL;
772 char *ppath = NULL; 615 char *ppath = NULL;
773 if(cob->name.length > 0) { 616 if(cob->name.length > 0) {
774 name = sstrdup_pool(pool, cob->name).ptr; 617 name = sstrdup_pool(pool, cob->name).ptr;
618 if(!name) return -1;
775 } 619 }
776 if(cob->ppath.length > 0) { 620 if(cob->ppath.length > 0) {
777 ppath = sstrdup_pool(pool, cob->ppath).ptr; 621 ppath = sstrdup_pool(pool, cob->ppath).ptr;
622 if(!ppath) return -1;
778 } 623 }
779 624
780 // create and add object 625 // create and add object
781 httpd_object *obj = object_new(pool, name); 626 httpd_object *obj = object_new(pool, name);
627 if(!obj) return -1;
782 obj->path = NULL; 628 obj->path = NULL;
783 629
784 conf->objects[i] = obj; 630 conf->objects[i] = obj;
785 631
786 // add directives 632 // add directives
787 for(int j=0;j<NUM_NSAPI_TYPES-1;j++) { 633 for(int j=0;j<NUM_NSAPI_TYPES-1;j++) {
788 UcxList *dirs = cob->directives[j]; 634 UcxList *dirs = cob->directives[j];
789 while(dirs != NULL) { 635 while(dirs != NULL) {
790 ConfigDirective *cfgdir = dirs->data; 636 ConfigDirective *cfgdir = dirs->data;
791 637
792 directive *d = pool_malloc(pool, sizeof(directive)); 638 directive *d = pool_malloc(pool, sizeof(directive));
639 if(!d) return -1;
793 if(cfgdir->condition) { 640 if(cfgdir->condition) {
794 sstr_t expr = cfgdir->condition->param_str; 641 sstr_t expr = cfgdir->condition->param_str;
795 d->cond = condition_from_str(pool, expr.ptr, expr.length); 642 d->cond = condition_from_str(pool, expr.ptr, expr.length);
796 } else { 643 } else {
797 d->cond = NULL; 644 d->cond = NULL;
798 } 645 }
799 d->param = pblock_create_pool(pool, 8); 646 d->param = pblock_create_pool(pool, 8);
800 647
801 // add params 648 // add params
802 UcxList *param = cfg_param_list(cfgdir->value, mp); 649 UcxList *param = cfg_param_list(cfgdir->value, scfg->a);
803 while(param != NULL) { 650 while(param != NULL) {
804 ConfigParam *p = param->data; 651 ConfigParam *p = param->data;
805 pblock_nvlinsert( 652 pblock_nvlinsert(
806 p->name.ptr, 653 p->name.ptr,
807 p->name.length, 654 p->name.length,
812 } 659 }
813 660
814 // get function 661 // get function
815 char *func_name = pblock_findval("fn", d->param); 662 char *func_name = pblock_findval("fn", d->param);
816 if(!func_name) { 663 if(!func_name) {
817 log_ereport(LOG_MISCONFIG, "%s: Missing fn parameter", file); 664 log_ereport(LOG_MISCONFIG, "%s: Missing fn parameter", file.ptr);
818 return NULL; 665 return -1;
819 } 666 }
820 d->func = get_function(func_name); 667 d->func = get_function(func_name);
821 if(!d->func) { 668 if(!d->func) {
822 log_ereport(LOG_MISCONFIG, "func %s not found", func_name); 669 log_ereport(LOG_MISCONFIG, "func %s not found", func_name);
823 return NULL; 670 return -1;
824 } 671 }
825 672
826 dirs = dirs->next; 673 dirs = dirs->next;
827 674
828 // add function to dtable 675 // add function to dtable
832 679
833 // next 680 // next
834 i++; 681 i++;
835 objlist = objlist->next; 682 objlist = objlist->next;
836 } 683 }
684
685 return 0;
686 }
687
688 HTTPObjectConfig* objconf_load(ServerConfiguration *scfg, sstr_t file) {
689 log_ereport(LOG_VERBOSE, "load_obj_conf");
690
691 int ret = 0;
692
693 // create object config
694 pool_handle_t *pool = scfg->pool;
695 HTTPObjectConfig *conf = pool_calloc(pool, sizeof(HTTPObjectConfig), 1);
696 if(!conf) {
697 return NULL;
698 }
699 conf->pool = pool;
700
701 // load obj config file
702 ObjectConfig *cfg = load_object_config(file.ptr);
703 if(!cfg) {
704 return NULL;
705 }
706
707 // convert ObjectConfig to HTTPObjectConfig
708
709 // add objects
710 conf->nobj = ucx_list_size(cfg->objects);
711 conf->objects = pool_calloc(pool, conf->nobj, sizeof(httpd_object*));
712 if(conf->objects) {
713 ret = convert_objconf(scfg, cfg, conf, file);
714 } else {
715 ret = -1;
716 }
837 717
838 free_object_config(cfg); 718 free_object_config(cfg);
839 719
840 return conf; 720 return !ret ? conf : NULL;
841 } 721 }
842 722
843 int mime_conf_reload(ConfigFile *file, ServerConfiguration *cfg) { 723 int mime_conf_load(ServerConfiguration *cfg, sstr_t file) {
844 MimeConfig *mimecfg = load_mime_config(file->file.ptr); 724 MimeConfig *mimecfg = load_mime_config(file.ptr);
845 MimeMap *old_conf = file->data; 725 if(!mimecfg) {
846 726 return -1;
847 MimeMap *mimemap = malloc(sizeof(MimeMap)); 727 }
848 mimemap->ref = 1; 728
849 UcxMap *map = ucx_map_new((mimecfg->ntypes * 3) / 2); 729 int ret = 0;
850 mimemap->map = map; 730
851 731 // cleanup in case of errors is done by the allocator
852 // add ext type pairs 732 MimeMap *mimemap = almalloc(cfg->a, sizeof(MimeMap));
853 UCX_FOREACH(md, mimecfg->directives) { 733 UcxMap *map = ucx_map_new_a(cfg->a, (mimecfg->ntypes * 3) / 2);
854 MimeDirective *d = md->data; 734
855 // add the type for each extension to the map 735 if(mimemap && map) {
856 UCX_FOREACH(xl, d->exts) { 736 mimemap->map = map;
857 sstr_t ext = sstr(xl->data); 737
858 sstr_t value = sstrdup(d->type); 738 // add ext type pairs
859 ucx_map_sstr_put(map, ext, value.ptr); 739 UCX_FOREACH(md, mimecfg->directives) {
860 } 740 MimeDirective *d = md->data;
861 } 741 // add the type for each extension to the map
862 742 UCX_FOREACH(xl, d->exts) {
863 file->data = mimemap; 743 sstr_t ext = sstr(xl->data);
864 744 sstr_t value = sstrdup(d->type);
865 if(old_conf) { 745 if(ucx_map_sstr_put(map, ext, value.ptr)) {
866 mime_conf_unref(old_conf); 746 ret = -1;
747 break;
748 }
749 }
750 if(ret) {
751 break;
752 }
753 }
754
755 cfg->mimetypes = mimemap;
756 } else {
757 ret = -1;
867 } 758 }
868 759
869 free_mime_config(mimecfg); 760 free_mime_config(mimecfg);
870 return 0; 761 return ret;
871 } 762 }
872 763
873 void mime_conf_ref(MimeMap *conf) { 764
874 if(conf) { 765
875 ws_atomic_inc32(&conf->ref); 766 ACLData* acl_conf_load(ServerConfiguration *cfg, sstr_t file) {
876 } 767 ACLFile *aclfile = load_acl_file(file.ptr);
877 } 768
878 769 // TODO: malloc return checks
879 void mime_conf_unref(MimeMap *conf) { 770
880 uint32_t ref = ws_atomic_dec32(&conf->ref); 771 ACLData *acldata = acl_data_new(cfg->a);
881 if(ref == 0) {
882 printf("free MimeConfig %"PRIxPTR"\n", (intptr_t)conf);
883 UcxMapIterator i = ucx_map_iterator(conf->map);
884 char *str;
885 UCX_MAP_FOREACH(key, str, i) {
886 free(str);
887 }
888 ucx_map_free(conf->map);
889 free(conf);
890 }
891 }
892
893 int acl_conf_reload(ConfigFile *file, ServerConfiguration *cfg) {
894 ACLFile *aclfile = load_acl_file(file->file.ptr);
895
896 ACLData *acldata = acl_data_new();
897 UCX_FOREACH(elm, aclfile->namedACLs) { 772 UCX_FOREACH(elm, aclfile->namedACLs) {
898 ACLConfig *ac = elm->data; 773 ACLConfig *ac = elm->data;
899 ACLList *acl = acl_config_convert(cfg, ac); 774 ACLList *acl = acl_config_convert(cfg, ac);
900 log_ereport(LOG_VERBOSE, "add acl: %.*s", (int)ac->id.length, ac->id.ptr); 775 log_ereport(LOG_VERBOSE, "add acl: %.*s", (int)ac->id.length, ac->id.ptr);
901 ucx_map_sstr_put(acldata->namedACLs, ac->id, acl); 776 ucx_map_sstr_put(acldata->namedACLs, ac->id, acl);
902 } 777 }
903 free_acl_file(aclfile); 778 free_acl_file(aclfile);
904 779
905 ACLData *old_data = file->data; 780 return acldata;
906 file->data = acldata;
907 if(old_data) {
908 acl_data_unref(old_data);
909 }
910
911 return 0;
912 } 781 }
913 782
914 ACLList* acl_config_convert(ServerConfiguration *cfg, ACLConfig *acl) { 783 ACLList* acl_config_convert(ServerConfiguration *cfg, ACLConfig *acl) {
915 WSAcl *acllist = malloc(sizeof(WSAcl)); 784 UcxAllocator *a = cfg->a;
785
786 WSAcl *acllist = almalloc(cfg->a, sizeof(WSAcl));
916 acllist->acl.check = (acl_check_f)wsacl_check; 787 acllist->acl.check = (acl_check_f)wsacl_check;
917 acllist->acl.authdb = NULL; 788 acllist->acl.authdb = NULL;
918 acllist->acl.authprompt = NULL; 789 acllist->acl.authprompt = NULL;
919 acllist->acl.isextern = 0; 790 acllist->acl.isextern = 0;
920 acllist->ace = NULL; 791 acllist->ace = NULL;
923 if(acl->type.ptr && !sstrcmp(acl->type, sstr("fs"))) { 794 if(acl->type.ptr && !sstrcmp(acl->type, sstr("fs"))) {
924 acllist->acl.isextern = 1; 795 acllist->acl.isextern = 1;
925 } 796 }
926 797
927 size_t s = ucx_list_size(acl->entries); 798 size_t s = ucx_list_size(acl->entries);
928 WSAce **aces = calloc(s, sizeof(WSAce*)); 799 WSAce **tmp_aces = calloc(s, sizeof(WSAce*));
929 WSAce **eces = calloc(s, sizeof(WSAce*)); 800 WSAce **tmp_eces = calloc(s, sizeof(WSAce*));
930 int ai = 0; 801 int ai = 0;
931 int ei = 0; 802 int ei = 0;
932 803
933 // convert entries 804 // convert entries
934 UCX_FOREACH(elm, acl->entries) { 805 UCX_FOREACH(elm, acl->entries) {
935 ACEConfig *acecfg = elm->data; 806 ACEConfig *acecfg = elm->data;
936 807
937 // copy data 808 // copy data
938 WSAce *ace = malloc(sizeof(WSAce)); 809 WSAce *ace = almalloc(a, sizeof(WSAce));
939 ace->access_mask = acecfg->access_mask; 810 ace->access_mask = acecfg->access_mask;
940 ace->flags = acecfg->flags; 811 ace->flags = acecfg->flags;
941 ace->type = acecfg->type; 812 ace->type = acecfg->type;
942 ace->who = sstrdup(acecfg->who).ptr; 813 ace->who = sstrdup_a(a, acecfg->who).ptr;
943 814
944 // add the entry to the correct array 815 // add the entry to the correct array
945 if(ace->type >= ACL_TYPE_AUDIT) { 816 if(ace->type >= ACL_TYPE_AUDIT) {
946 eces[ei] = ace; 817 tmp_eces[ei] = ace;
947 ei++; 818 ei++;
948 } else { 819 } else {
949 aces[ai] = ace; 820 tmp_aces[ai] = ace;
950 ai++; 821 ai++;
951 } 822 }
952 } 823 }
953 824
954 // create new entrie arrays with perfect fitting size 825 // create new entrie arrays with perfect fitting size
955 if(ai > 0) { 826 if(ai > 0) {
956 acllist->ace = calloc(ai, sizeof(WSAce*)); 827 acllist->ace = alcalloc(a, ai, sizeof(WSAce*));
957 } 828 }
958 if(ei > 0) { 829 if(ei > 0) {
959 acllist->ece = calloc(ei, sizeof(WSAce*)); 830 acllist->ece = alcalloc(a, ei, sizeof(WSAce*));
960 } 831 }
961 memcpy(acllist->ace, aces, ai*sizeof(WSAce*)); 832 memcpy(acllist->ace, tmp_aces, ai*sizeof(WSAce*));
962 memcpy(acllist->ece, eces, ei*sizeof(WSAce*)); 833 memcpy(acllist->ece, tmp_eces, ei*sizeof(WSAce*));
963 acllist->acenum = ai; 834 acllist->acenum = ai;
964 acllist->ecenum = ei; 835 acllist->ecenum = ei;
965 836
966 free(aces); 837 free(tmp_aces);
967 free(eces); 838 free(tmp_eces);
968 839
969 // get authentication information 840 // get authentication information
970 if(acl->authparam) { 841 if(acl->authparam) {
971 sstr_t authdb_str = cfg_param_get(acl->authparam, sstr("authdb")); 842 sstr_t authdb_str = cfg_param_get(acl->authparam, sstr("authdb"));
972 sstr_t prompt_str = cfg_param_get(acl->authparam, sstr("prompt")); 843 sstr_t prompt_str = cfg_param_get(acl->authparam, sstr("prompt"));
973 844
974 if(authdb_str.ptr) { 845 if(authdb_str.ptr) {
975 AuthDB *authdb = ucx_map_sstr_get(cfg->authdbs, authdb_str); 846 AuthDB *authdb = ucx_map_sstr_get(cfg->authdbs, authdb_str);
976 acllist->acl.authdb = authdb; 847 acllist->acl.authdb = authdb;
977 if(authdb && prompt_str.ptr) { 848 if(authdb && prompt_str.ptr) {
978 acllist->acl.authprompt = sstrdup(prompt_str).ptr; 849 acllist->acl.authprompt = sstrdup_a(a, prompt_str).ptr;
979 } 850 }
980 } 851 }
981 } 852 }
982 853
983 return &acllist->acl; 854 return &acllist->acl;
984 } 855 }
985 856
986 int keyfile_reload(ConfigFile *file, ServerConfiguration *cfg) { 857 AuthDB* keyfile_load(ServerConfiguration *cfg, scstr_t file) {
987 KeyfileConfig *conf = load_keyfile_config(file->file.ptr); 858 Keyfile *keyfile = keyfile_new(cfg->a);
859 if(!keyfile) {
860 return NULL;
861 }
862
863 KeyfileConfig *conf = load_keyfile_config(file.ptr);
988 if(!conf) { 864 if(!conf) {
989 return 1; 865 return NULL;
990 } 866 }
991 867
992 Keyfile *keyfile = keyfile_new(); 868 AuthDB *ret = &keyfile->authdb;
993 869
994 UCX_FOREACH(elm, conf->users) { 870 UCX_FOREACH(elm, conf->users) {
995 KeyfileEntry *user = elm->data; 871 KeyfileEntry *user = elm->data;
996 keyfile_add_user( 872 if(keyfile_add_user(
997 keyfile, 873 keyfile,
998 user->name, 874 user->name,
999 user->hashtype, 875 user->hashtype,
1000 user->hashdata, 876 user->hashdata,
1001 user->groups, 877 user->groups,
1002 user->numgroups); 878 user->numgroups))
879 {
880 ret = NULL;
881 break;
882 }
1003 } 883 }
1004 884
1005 free_keyfile_config(conf); 885 free_keyfile_config(conf);
1006 886
1007 Keyfile *old_data = file->data; 887 return ret;
1008 file->data = keyfile; 888 }
1009 if(old_data) {
1010 keyfile_unref(old_data);
1011 }
1012
1013 return 0;
1014 }
1015
1016
1017 sstr_t cfg_load_file(sstr_t file) {
1018 sstr_t r;
1019 r.ptr = NULL;
1020 r.length = 0;
1021
1022 if(!file.ptr) {
1023 return r;
1024 }
1025
1026 sstr_t f = sstrdup(file);
1027 FILE *in = fopen(f.ptr, "r");
1028 if(!in) {
1029 return r;
1030 }
1031
1032 UcxBuffer *buf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
1033 if(!buf) {
1034 fclose(in);
1035 return r;
1036 }
1037
1038 if(ucx_stream_copy(in, buf, (read_func)fread, (write_func)ucx_buffer_write) == 0) {
1039 fclose(in);
1040 ucx_buffer_free(buf);
1041 return r;
1042 }
1043
1044 r.ptr = buf->space;
1045 r.length = buf->pos;
1046
1047 free(buf);
1048 fclose(in);
1049
1050 return r;
1051 }

mercurial