base-files: set root password if present inside board.json
authorJohn Crispin <john@phrozen.org>
Sat, 21 Sep 2024 15:53:24 +0000 (17:53 +0200)
committerJohn Crispin <john@phrozen.org>
Wed, 2 Oct 2024 13:41:33 +0000 (15:41 +0200)
Add code to set plain password or put the hash into /etc/shadow.

Signed-off-by: John Crispin <john@phrozen.org>
package/base-files/files/etc/uci-defaults/50-root-passwd [new file with mode: 0644]

diff --git a/package/base-files/files/etc/uci-defaults/50-root-passwd b/package/base-files/files/etc/uci-defaults/50-root-passwd
new file mode 100644 (file)
index 0000000..9eddf15
--- /dev/null
@@ -0,0 +1,12 @@
+. /usr/share/libubox/jshn.sh
+
+json_init
+json_load "$(cat /etc/board.json)"
+
+json_select credentials
+       json_get_vars root_password_hash root_password_hash
+       [ -z "$root_password_hash" ] || sed -i "s|^root:[^:]*|root:$root_password_hash|g" /etc/shadow
+
+       json_get_vars root_password_plain root_password_plain
+       [ -z "$root_password_plain" ] || { (echo "$root_password_plain"; sleep 1; echo "$root_password_plain") | passwd root }
+json_select ..