diff mbox

[OpenWrt-Devel,RESEND] network/services/uhttpd: Add Basic Auth config

Message ID 1455607726-84165-1-git-send-email-openwrt@daniel.thecshore.com
State Changes Requested
Delegated to: Felix Fietkau
Headers show

Commit Message

Daniel Dickinson Feb. 16, 2016, 7:28 a.m. UTC
From: Daniel Dickinson <openwrt@daniel.thecshore.com>

No comment on previous send of this patch, hence resent.

We add an 'httpauth' section type that contains the options:

instance: Which uhttpd config section the auth is associated with
prefix: What virtual or real URL is being protected
username: The username for the Basic Auth dialogue
password: Hashed (crypt()) or plaintext password for the Basic Auth dialogue

If instance is not specified auth section applies to
all instances of uhttpd; if instance is specified
the auth section only applies to the uhttpd config
section with the same name as specified with the
instance option.

Signed-off-by: Daniel Dickinson <openwrt@daniel.thecshore.com>
---
 package/network/services/uhttpd/files/uhttpd.init | 36 ++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)
diff mbox

Patch

diff --git a/package/network/services/uhttpd/files/uhttpd.init b/package/network/services/uhttpd/files/uhttpd.init
index 4ca6c8d..910828e 100755
--- a/package/network/services/uhttpd/files/uhttpd.init
+++ b/package/network/services/uhttpd/files/uhttpd.init
@@ -53,6 +53,28 @@  generate_keys() {
 	}
 }
 
+create_httpauth() {
+	local cfg="$1"
+	local basecfg="$2"
+	local prefix username password
+
+	local instance
+	config_get instance "$cfg" instance
+	if [ -n "$instance" ] && [ "$instance" != "$basecfg" ]; then
+		return
+	fi
+
+	config_get prefix "$cfg" prefix
+	config_get username "$cfg" username
+	config_get password "$cfg" password
+
+	if [ -z "$prefix" ] || [ -z "$username" ] || [ -z "$password" ]; then
+		return
+	fi
+	echo "${prefix}:${username}:${password}" >>$httpdconf
+	haveauth=1
+}
+
 start_instance()
 {
 	UHTTPD_CERT=""
@@ -60,17 +82,29 @@  start_instance()
 
 	local cfg="$1"
 	local realm="$(uci_get system.@system[0].hostname)"
-	local listen http https interpreter indexes path handler
+	local listen http https interpreter indexes path handler httpdconf haveauth
 
 	procd_open_instance
 	procd_set_param respawn
 	procd_set_param stderr 1
 	procd_set_param command "$UHTTPD_BIN" -f
 
+	config_get config "$cfg" config
+	if [ -z "$config" ]; then
+		mkdir -p /var/etc/uhttpd
+		httpdconf="/var/etc/uhttpd/httpd.${cfg}.conf"
+		rm -f ${httpdconf}
+		config_foreach create_httpauth httpauth "$cfg"
+		if [ "$haveauth" = "1" ]; then
+			procd_append_param command -c ${httpdconf}
+		fi
+	fi
+
 	append_arg "$cfg" home "-h"
 	append_arg "$cfg" realm "-r" "${realm:-OpenWrt}"
 	append_arg "$cfg" config "-c"
 	append_arg "$cfg" cgi_prefix "-x"
+
 	[ -f /usr/lib/uhttpd_lua.so ] && {
 		config_get handler "$cfg" lua_handler
 		[ -f "$handler" ] && append_arg "$cfg" lua_prefix "-l" && {