print linenumber from all conf-emitted warnings

This commit is contained in:
rofl0r 2020-09-30 05:21:26 +01:00
parent f55c46eb39
commit 960972865c
3 changed files with 58 additions and 32 deletions

View File

@ -74,6 +74,9 @@
*/ */
#define RE_MAX_MATCHES 24 #define RE_MAX_MATCHES 24
#define CP_WARN(FMT, ...) \
log_message (LOG_WARNING, "line %lu: " FMT, lineno, __VA_ARGS__)
/* /*
* All configuration handling functions are REQUIRED to be defined * All configuration handling functions are REQUIRED to be defined
* with the same function template as below. * with the same function template as below.
@ -638,9 +641,7 @@ static HANDLE_FUNC (handle_anonymous)
return -1; return -1;
if(anonymous_insert (conf, arg) < 0) { if(anonymous_insert (conf, arg) < 0) {
log_message (LOG_WARNING, CP_WARN ("anonymous_insert() failed: '%s'", arg);
"anonymous_insert() failed: '%s'",
arg);
safefree(arg); safefree(arg);
return -1; return -1;
} }
@ -768,7 +769,7 @@ static HANDLE_FUNC (handle_group)
} }
static void warn_invalid_address(char *arg, unsigned long lineno) { static void warn_invalid_address(char *arg, unsigned long lineno) {
log_message (LOG_WARNING, "Invalid address %s on line %lu", arg, lineno); CP_WARN ("Invalid address %s", arg);
} }
static HANDLE_FUNC (handle_allow) static HANDLE_FUNC (handle_allow)
@ -813,8 +814,8 @@ static HANDLE_FUNC (handle_listen)
if (conf->listen_addrs == NULL) { if (conf->listen_addrs == NULL) {
conf->listen_addrs = sblist_new(sizeof(char*), 16); conf->listen_addrs = sblist_new(sizeof(char*), 16);
if (conf->listen_addrs == NULL) { if (conf->listen_addrs == NULL) {
log_message(LOG_WARNING, "Could not create a list " CP_WARN ("Could not create a list "
"of listen addresses."); "of listen addresses.", "");
safefree(arg); safefree(arg);
return -1; return -1;
} }
@ -840,9 +841,7 @@ static HANDLE_FUNC (handle_errorfile)
char *page = get_string_arg (line, &match[4]); char *page = get_string_arg (line, &match[4]);
if(add_new_errorpage (conf, page, err) < 0) { if(add_new_errorpage (conf, page, err) < 0) {
log_message (LOG_WARNING, CP_WARN ("add_new_errorpage() failed: '%s'", page);
"add_new_errorpage() failed: '%s'",
page);
safefree (page); safefree (page);
} }
return 0; return 0;
@ -1025,6 +1024,7 @@ static HANDLE_FUNC (handle_upstream)
int port, mi; int port, mi;
char *domain = 0, *user = 0, *pass = 0, *tmp; char *domain = 0, *user = 0, *pass = 0, *tmp;
enum proxy_type pt; enum proxy_type pt;
enum upstream_build_error ube;
if (match[3].rm_so != -1) { if (match[3].rm_so != -1) {
tmp = get_string_arg (line, &match[3]); tmp = get_string_arg (line, &match[3]);
@ -1034,9 +1034,9 @@ static HANDLE_FUNC (handle_upstream)
domain = get_string_arg (line, &match[4]); domain = get_string_arg (line, &match[4]);
if (!domain) if (!domain)
return -1; return -1;
upstream_add (NULL, 0, domain, 0, 0, PT_NONE, &conf->upstream_list); ube = upstream_add (NULL, 0, domain, 0, 0, PT_NONE, &conf->upstream_list);
safefree (domain); safefree (domain);
return 0; goto check_err;
} }
} }
@ -1066,13 +1066,16 @@ static HANDLE_FUNC (handle_upstream)
if (match[mi].rm_so != -1) if (match[mi].rm_so != -1)
domain = get_string_arg (line, &match[mi]); domain = get_string_arg (line, &match[mi]);
upstream_add (ip, port, domain, user, pass, pt, &conf->upstream_list); ube = upstream_add (ip, port, domain, user, pass, pt, &conf->upstream_list);
safefree (user); safefree (user);
safefree (pass); safefree (pass);
safefree (domain); safefree (domain);
safefree (ip); safefree (ip);
check_err:;
if(ube != UBE_SUCCESS)
CP_WARN("%s", upstream_build_error_string(ube));
return 0; return 0;
} }

View File

@ -43,20 +43,34 @@ proxy_type_name(proxy_type type)
} }
} }
const char* upstream_build_error_string(enum upstream_build_error ube) {
static const char *emap[] = {
[UBE_SUCCESS] = "",
[UBE_OOM] = "Unable to allocate memory in upstream_build()",
[UBE_USERLEN] = "User / pass in upstream config too long",
[UBE_EDOMAIN] = "Nonsense upstream none rule: empty domain",
[UBE_INVHOST] = "Nonsense upstream rule: invalid host or port",
[UBE_INVPARAMS] = "Nonsense upstream rule: invalid parameters",
[UBE_NETMASK] = "Nonsense upstream rule: failed to parse netmask",
};
return emap[ube];
}
/** /**
* Construct an upstream struct from input data. * Construct an upstream struct from input data.
*/ */
static struct upstream *upstream_build (const char *host, int port, const char *domain, static struct upstream *upstream_build (const char *host, int port, const char *domain,
const char *user, const char *pass, const char *user, const char *pass,
proxy_type type) proxy_type type, enum upstream_build_error *ube)
{ {
char *ptr; char *ptr;
struct upstream *up; struct upstream *up;
*ube = UBE_SUCCESS;
up = (struct upstream *) safemalloc (sizeof (struct upstream)); up = (struct upstream *) safemalloc (sizeof (struct upstream));
if (!up) { if (!up) {
log_message (LOG_ERR, *ube = UBE_OOM;
"Unable to allocate memory in upstream_build()");
return NULL; return NULL;
} }
@ -69,8 +83,7 @@ static struct upstream *upstream_build (const char *host, int port, const char *
ssize_t ret; ssize_t ret;
ret = basicauth_string(user, pass, b, sizeof b); ret = basicauth_string(user, pass, b, sizeof b);
if (ret == 0) { if (ret == 0) {
log_message (LOG_ERR, *ube = UBE_USERLEN;
"User / pass in upstream config too long");
return NULL; return NULL;
} }
up->ua.authstr = safestrdup (b); up->ua.authstr = safestrdup (b);
@ -83,13 +96,11 @@ static struct upstream *upstream_build (const char *host, int port, const char *
if (domain == NULL) { if (domain == NULL) {
if (type == PT_NONE) { if (type == PT_NONE) {
e_nonedomain:; e_nonedomain:;
log_message (LOG_WARNING, *ube = UBE_EDOMAIN;
"Nonsense upstream none rule: empty domain");
goto fail; goto fail;
} }
if (!host || !host[0] || port < 1) { if (!host || !host[0] || port < 1) {
log_message (LOG_WARNING, *ube = UBE_INVHOST;
"Nonsense upstream rule: invalid host or port");
goto fail; goto fail;
} }
@ -103,8 +114,7 @@ static struct upstream *upstream_build (const char *host, int port, const char *
if (!domain[0]) goto e_nonedomain; if (!domain[0]) goto e_nonedomain;
} else { } else {
if (!host || !host[0] || !domain[0]) { if (!host || !host[0] || !domain[0]) {
log_message (LOG_WARNING, *ube = UBE_INVPARAMS;
"Nonsense upstream rule: invalid parameters");
goto fail; goto fail;
} }
up->host = safestrdup (host); up->host = safestrdup (host);
@ -130,8 +140,7 @@ static struct upstream *upstream_build (const char *host, int port, const char *
} }
up->ip = up->ip & up->mask; up->ip = up->ip & up->mask;
} else { } else {
log_message (LOG_WARNING, *ube = UBE_NETMASK;
"Nonsense upstream rule: failed to parse netmask");
goto fail; goto fail;
} }
} else { } else {
@ -160,15 +169,17 @@ fail:
/* /*
* Add an entry to the upstream list * Add an entry to the upstream list
*/ */
void upstream_add (const char *host, int port, const char *domain, enum upstream_build_error upstream_add (
const char *host, int port, const char *domain,
const char *user, const char *pass, const char *user, const char *pass,
proxy_type type, struct upstream **upstream_list) proxy_type type, struct upstream **upstream_list)
{ {
struct upstream *up; struct upstream *up;
enum upstream_build_error ube;
up = upstream_build (host, port, domain, user, pass, type); up = upstream_build (host, port, domain, user, pass, type, &ube);
if (up == NULL) { if (up == NULL) {
return; return ube;
} }
if (!up->domain && !up->ip) { /* always add default to end */ if (!up->domain && !up->ip) { /* always add default to end */
@ -184,7 +195,7 @@ void upstream_add (const char *host, int port, const char *domain,
if (!tmp->next) { if (!tmp->next) {
up->next = NULL; up->next = NULL;
tmp->next = up; tmp->next = up;
return; return ube;
} }
tmp = tmp->next; tmp = tmp->next;
@ -194,14 +205,14 @@ void upstream_add (const char *host, int port, const char *domain,
up->next = *upstream_list; up->next = *upstream_list;
*upstream_list = up; *upstream_list = up;
return; return ube;
upstream_cleanup: upstream_cleanup:
safefree (up->host); safefree (up->host);
safefree (up->domain); safefree (up->domain);
safefree (up); safefree (up);
return; return ube;
} }
/* /*

View File

@ -27,6 +27,16 @@
#include "common.h" #include "common.h"
enum upstream_build_error {
UBE_SUCCESS = 0,
UBE_OOM,
UBE_USERLEN,
UBE_EDOMAIN,
UBE_INVHOST,
UBE_INVPARAMS,
UBE_NETMASK,
};
/* /*
* Even if upstream support is not compiled into tinyproxy, this * Even if upstream support is not compiled into tinyproxy, this
* structure still needs to be defined. * structure still needs to be defined.
@ -54,11 +64,13 @@ struct upstream {
#ifdef UPSTREAM_SUPPORT #ifdef UPSTREAM_SUPPORT
const char *proxy_type_name(proxy_type type); const char *proxy_type_name(proxy_type type);
extern void upstream_add (const char *host, int port, const char *domain, extern enum upstream_build_error upstream_add (
const char *host, int port, const char *domain,
const char *user, const char *pass, const char *user, const char *pass,
proxy_type type, struct upstream **upstream_list); proxy_type type, struct upstream **upstream_list);
extern struct upstream *upstream_get (char *host, struct upstream *up); extern struct upstream *upstream_get (char *host, struct upstream *up);
extern void free_upstream_list (struct upstream *up); extern void free_upstream_list (struct upstream *up);
extern const char* upstream_build_error_string(enum upstream_build_error);
#endif /* UPSTREAM_SUPPORT */ #endif /* UPSTREAM_SUPPORT */
#endif /* _TINYPROXY_UPSTREAM_H_ */ #endif /* _TINYPROXY_UPSTREAM_H_ */