From 9fac3ef792d7923e1fabd90b7b46acbf227d8578 Mon Sep 17 00:00:00 2001 From: Valen Blanco Date: Mon, 3 Oct 2016 17:33:10 +0200 Subject: [PATCH] Added new field to upstream struct; added new function for registering an upstream proxy with basic auth --- src/upstream.c | 36 +++++++++++++++++++++++++++++------- src/upstream.h | 4 ++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/upstream.c b/src/upstream.c index 6b25f9b..eda28f2 100644 --- a/src/upstream.c +++ b/src/upstream.c @@ -30,9 +30,12 @@ #ifdef UPSTREAM_SUPPORT /** - * Construct an upstream struct from input data. + * Construct an upstream struct from input data, with basic auth credentials. + * 'basic_auth' can be NULL. */ -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 *basic_auth, + const char *domain) { char *ptr; struct upstream *up; @@ -44,7 +47,7 @@ static struct upstream *upstream_build (const char *host, int port, const char * return NULL; } - up->host = up->domain = NULL; + up->host = up->domain = up->basic_auth = NULL; up->ip = up->mask = 0; if (domain == NULL) { @@ -57,6 +60,9 @@ static struct upstream *upstream_build (const char *host, int port, const char * up->host = safestrdup (host); up->port = port; + if (basic_auth != NULL) + up->basic_auth = safestrdup (basic_auth); + log_message (LOG_INFO, "Added upstream %s:%d for [default]", host, port); } else if (host == NULL) { @@ -101,6 +107,9 @@ static struct upstream *upstream_build (const char *host, int port, const char * up->port = port; up->domain = safestrdup (domain); + if (basic_auth != NULL) + up->basic_auth = safestrdup (basic_auth); + log_message (LOG_INFO, "Added upstream %s:%d for %s", host, port, domain); } @@ -109,6 +118,7 @@ static struct upstream *upstream_build (const char *host, int port, const char * fail: safefree (up->host); + safefree (up->basic_auth); safefree (up->domain); safefree (up); @@ -116,14 +126,15 @@ fail: } /* - * Add an entry to the upstream list + * Add an entry to the upstream list, with basic auth credentials. + * 'basic_auth' can be NULL. */ -void upstream_add (const char *host, int port, const char *domain, - struct upstream **upstream_list) +void upstream_bauth_add (const char *host, int port, const char *basic_auth, + const char *domain, struct upstream **upstream_list) { struct upstream *up; - up = upstream_build (host, port, domain); + up = upstream_build (host, port, basic_auth, domain); if (up == NULL) { return; } @@ -155,12 +166,22 @@ void upstream_add (const char *host, int port, const char *domain, upstream_cleanup: safefree (up->host); + safefree (up->basic_auth); safefree (up->domain); safefree (up); return; } +/* + * Add an entry to the upstream list + */ +void upstream_add (const char *host, int port, const char *domain, + struct upstream **upstream_list) +{ + upstream_bauth_add (host, port, NULL, domain, upstream_list); +} + /* * Check if a host is in the upstream list */ @@ -216,6 +237,7 @@ void free_upstream_list (struct upstream *up) struct upstream *tmp = up; up = up->next; safefree (tmp->domain); + safefree (tmp->basic_auth); safefree (tmp->host); safefree (tmp); } diff --git a/src/upstream.h b/src/upstream.h index 34dad68..1e965e1 100644 --- a/src/upstream.h +++ b/src/upstream.h @@ -36,12 +36,16 @@ struct upstream { char *domain; /* optional */ char *host; int port; + char *basic_auth; /* optional, Base64 basic auth */ in_addr_t ip, mask; }; #ifdef UPSTREAM_SUPPORT extern void upstream_add (const char *host, int port, const char *domain, struct upstream **upstream_list); +extern void upstream_bauth_add (const char *host, int port, + const char *basic_auth, const char *domain, + struct upstream **upstream_list); extern struct upstream *upstream_get (char *host, struct upstream *up); extern void free_upstream_list (struct upstream *up); #endif /* UPSTREAM_SUPPORT */