2013-11-09 16:21:52 +01:00
|
|
|
/*
|
2021-01-11 23:46:08 +01:00
|
|
|
* Copyright (c) 2013-2021 Joris Vink <joris@coders.se>
|
2013-11-09 16:21:52 +01:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-03-06 09:29:46 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2013-11-09 16:21:52 +01:00
|
|
|
#include "kore.h"
|
|
|
|
|
|
|
|
TAILQ_HEAD(, kore_validator) validators;
|
|
|
|
|
|
|
|
void
|
|
|
|
kore_validator_init(void)
|
|
|
|
{
|
|
|
|
TAILQ_INIT(&validators);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-08-04 12:40:21 +02:00
|
|
|
kore_validator_add(const char *name, u_int8_t type, const char *arg)
|
2013-11-09 16:21:52 +01:00
|
|
|
{
|
2018-08-10 08:06:09 +02:00
|
|
|
int ret;
|
2013-11-09 16:21:52 +01:00
|
|
|
struct kore_validator *val;
|
|
|
|
|
|
|
|
val = kore_malloc(sizeof(*val));
|
|
|
|
val->type = type;
|
|
|
|
|
|
|
|
switch (val->type) {
|
|
|
|
case KORE_VALIDATOR_TYPE_REGEX:
|
2018-08-10 08:06:09 +02:00
|
|
|
ret = regcomp(&(val->rctx), arg, REG_EXTENDED | REG_NOSUB);
|
|
|
|
if (ret) {
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_free(val);
|
2013-11-09 16:21:52 +01:00
|
|
|
kore_log(LOG_NOTICE,
|
2018-08-10 08:06:09 +02:00
|
|
|
"validator %s has bad regex %s (%d)",
|
|
|
|
name, arg, ret);
|
2013-11-09 16:21:52 +01:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case KORE_VALIDATOR_TYPE_FUNCTION:
|
2017-01-12 23:38:51 +01:00
|
|
|
val->rcall = kore_runtime_getcall(arg);
|
|
|
|
if (val->rcall == NULL) {
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_free(val);
|
2013-11-09 16:21:52 +01:00
|
|
|
kore_log(LOG_NOTICE,
|
|
|
|
"validator %s has undefined callback %s",
|
|
|
|
name, arg);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_free(val);
|
2013-11-09 16:21:52 +01:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
val->arg = kore_strdup(arg);
|
|
|
|
val->name = kore_strdup(name);
|
|
|
|
TAILQ_INSERT_TAIL(&validators, val, list);
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-08-04 12:40:21 +02:00
|
|
|
kore_validator_run(struct http_request *req, const char *name, char *data)
|
2013-11-09 16:21:52 +01:00
|
|
|
{
|
|
|
|
struct kore_validator *val;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(val, &validators, list) {
|
|
|
|
if (strcmp(val->name, name))
|
|
|
|
continue;
|
|
|
|
|
2014-02-01 17:47:58 +01:00
|
|
|
return (kore_validator_check(req, val, data));
|
2013-11-09 16:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2013-11-10 15:17:15 +01:00
|
|
|
int
|
2014-02-01 17:47:58 +01:00
|
|
|
kore_validator_check(struct http_request *req, struct kore_validator *val,
|
2018-02-14 13:48:49 +01:00
|
|
|
const void *data)
|
2013-11-10 15:17:15 +01:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
switch (val->type) {
|
|
|
|
case KORE_VALIDATOR_TYPE_REGEX:
|
|
|
|
if (!regexec(&(val->rctx), data, 0, NULL, 0))
|
|
|
|
r = KORE_RESULT_OK;
|
|
|
|
else
|
|
|
|
r = KORE_RESULT_ERROR;
|
|
|
|
break;
|
|
|
|
case KORE_VALIDATOR_TYPE_FUNCTION:
|
2017-01-12 23:38:51 +01:00
|
|
|
r = kore_runtime_validator(val->rcall, req, data);
|
2013-11-10 15:17:15 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
r = KORE_RESULT_ERROR;
|
|
|
|
kore_log(LOG_NOTICE, "invalid type %d for validator %s",
|
|
|
|
val->type, val->name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (r);
|
|
|
|
}
|
|
|
|
|
2017-08-31 17:11:24 +02:00
|
|
|
void
|
|
|
|
kore_validator_reload(void)
|
|
|
|
{
|
|
|
|
struct kore_validator *val;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(val, &validators, list) {
|
|
|
|
if (val->type != KORE_VALIDATOR_TYPE_FUNCTION)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
kore_free(val->rcall);
|
|
|
|
val->rcall = kore_runtime_getcall(val->arg);
|
|
|
|
if (val->rcall == NULL)
|
|
|
|
fatal("no function for validator %s found", val->arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-10 15:17:15 +01:00
|
|
|
struct kore_validator *
|
2014-08-04 12:40:21 +02:00
|
|
|
kore_validator_lookup(const char *name)
|
2013-11-10 15:17:15 +01:00
|
|
|
{
|
|
|
|
struct kore_validator *val;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(val, &validators, list) {
|
|
|
|
if (!strcmp(val->name, name))
|
|
|
|
return (val);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|