uhttpd: prefer px5g for certificate creation
[openwrt/staging/chunkeey.git] / package / network / services / uhttpd / files / uhttpd.init
index a3e09c44e7ee3ad35a9462b070348e46e0ae3b43..35c1985cd2b124d43b285a65a256cc9408be1a60 100755 (executable)
@@ -7,6 +7,7 @@ USE_PROCD=1
 
 UHTTPD_BIN="/usr/sbin/uhttpd"
 PX5G_BIN="/usr/sbin/px5g"
+OPENSSL_BIN="/usr/bin/openssl"
 
 append_arg() {
        local cfg="$1"
@@ -43,10 +44,14 @@ generate_keys() {
        config_get location   "$cfg" location
        config_get commonname "$cfg" commonname
 
-       [ -x "$PX5G_BIN" ] && {
-               $PX5G_BIN selfsigned -der \
-                       -days ${days:-730} -newkey rsa:${bits:-1024} -keyout "${UHTTPD_KEY}.new" -out "${UHTTPD_CERT}.new" \
-                       -subj /C="${country:-DE}"/ST="${state:-Saxony}"/L="${location:-Leipzig}"/CN="${commonname:-OpenWrt}"
+       # Prefer px5g for certificate generation (existence evaluated last)
+       local GENKEY_CMD=""
+       [ -x "$OPENSSL_BIN" ] && GENKEY_CMD="$OPENSSL_BIN req -x509 -outform der -nodes"
+       [ -x "$PX5G_BIN" ] && GENKEY_CMD="$PX5G_BIN selfsigned -der"
+       [ -n "$GENKEY_CMD" ] && {
+               $GENKEY_CMD \
+                       -days ${days:-730} -newkey rsa:${bits:-2048} -keyout "${UHTTPD_KEY}.new" -out "${UHTTPD_CERT}.new" \
+                       -subj /C="${country:-DE}"/ST="${state:-Saxony}"/L="${location:-Leipzig}"/CN="${commonname:-Lede}"
                sync
                mv "${UHTTPD_KEY}.new" "${UHTTPD_KEY}"
                mv "${UHTTPD_CERT}.new" "${UHTTPD_CERT}"
@@ -80,6 +85,7 @@ start_instance()
        [ -f /usr/lib/uhttpd_ubus.so ] && {
                append_arg "$cfg" ubus_prefix "-u"
                append_arg "$cfg" ubus_socket "-U"
+               append_bool "$cfg" ubus_cors "-X" 0
        }
        append_arg "$cfg" script_timeout "-t"
        append_arg "$cfg" network_timeout "-T"
@@ -94,6 +100,11 @@ start_instance()
        append_bool "$cfg" no_dirlists "-D" 0
        append_bool "$cfg" rfc1918_filter "-R" 0
 
+       config_get alias_list "$cfg" alias
+       for alias in $alias_list; do
+                procd_append_param command -y "$alias"
+       done
+
        config_get http "$cfg" listen_http
        for listen in $http; do
                 procd_append_param command -p "$listen"
@@ -130,6 +141,10 @@ start_instance()
                append_bool "$cfg" redirect_https "-q" 0
        }
 
+       for file in /etc/uhttpd/*.json; do
+               [ -s "$file" ] && procd_append_param command -H "$file"
+       done
+
        procd_close_instance
 }