Skip to content

Commit 2cc8889

Browse files
authored
Merge pull request #23 from kristapsk/skip-mkpasswd
Allow to skip root password reset (mkpasswd) and ssh authorized key adding
2 parents 40ffa70 + 5750a47 commit 2cc8889

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

tools/finalize-nakamochi.sh

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,19 @@ patch_lnd_conf()
3737
run_main()
3838
{
3939
if [[ -z $2 ]] || [[ "$1" == "--help" ]]; then
40-
echo "Usage: $(basename "$0") [options] /dev/sda2 /dev/sdb1 [/mnt/usd [/mnt/ssd]]"
40+
echo "Usage: $(basename "$0") [options] /dev/sda2 /dev/sdb1 [/mnt/usd [/mnt/ssd [ssh_authorized_key]]]"
4141
echo "Where:"
42-
echo " /dev/sda2 - uSD card root partition (use '-' to not mount)"
43-
echo " /dev/sdb1 - SSD data partition (use '-' to not mount)"
44-
echo " /mnt/usd - uSD card mount point / directory"
45-
echo " /mnt/ssd - SSD mount point / directory"
42+
echo " /dev/sda2 - uSD card root partition (use '-' to not mount)"
43+
echo " /dev/sdb1 - SSD data partition (use '-' to not mount)"
44+
echo " /mnt/usd - uSD card mount point / directory"
45+
echo " /mnt/ssd - SSD mount point / directory"
46+
echo " ssh_authorized_key - optional SSH authorized pubkey (filename) to add to root user"
4647
echo "Options:"
47-
echo " --skip-mkp224o - skip generating onion keys (requires them to be already present on SSD)"
48-
echo " --test-image - configure image for testing (allows ssh root access with password)"
48+
echo " --skip-mkp224o - skip generating onion keys (requires them to be already present on SSD)"
49+
echo " --skip-passwd - skip setting root password (when mkpasswd is not available)"
50+
echo " --test-image - configure image for testing (allows ssh root access with password)"
4951
# FixMe: --update-update is temporary hack, remove after https://github.com/nakamochi/sysupdates/pull/8 is merged.
50-
echo " --update-update - update /sysupdates/update.sh on SSD from local copy"
52+
echo " --update-update - update /sysupdates/update.sh on SSD from local copy"
5153
echo "Example: $(basename "$0") /dev/sdc2 /dev/sdd1"
5254
exit 1
5355
fi
@@ -58,6 +60,7 @@ run_main()
5860
fi
5961

6062
skip_mkp224o=0
63+
skip_passwd=0
6164
test_image=0
6265
update_update=0
6366
while [[ "$1" == --* ]]; do
@@ -66,6 +69,10 @@ run_main()
6669
skip_mkp224o=1
6770
shift
6871
;;
72+
--skip-passwd)
73+
skip_passwd=1
74+
shift
75+
;;
6976
--test-image)
7077
test_image=1
7178
shift
@@ -88,9 +95,11 @@ run_main()
8895
fi
8996
fi
9097

91-
if ! check_exists mkpasswd; then
92-
echo "Error: mkpasswd not found, try 'apt install whois'."
93-
exit 1
98+
if [[ "$skip_passwd" -eq 0 ]]; then
99+
if ! check_exists mkpasswd; then
100+
echo "Error: mkpasswd is not installed, try 'apt install whois'."
101+
exit 1
102+
fi
94103
fi
95104

96105
if [[ ! -x "$base_dir/rpcauth.py" ]]; then
@@ -102,6 +111,7 @@ run_main()
102111
SSD_DEVICE="$2"
103112
USD_MOUNT_POINT="${3:-/mnt/usd}"
104113
SSD_MOUNT_POINT="${4:-/mnt/ssd}"
114+
SSH_AUTHORIZED_KEY="$5"
105115

106116
# "-" is a special case for tests without automounting.
107117

@@ -115,6 +125,11 @@ run_main()
115125
exit 1
116126
fi
117127

128+
if [[ -n "$SSH_AUTHORIZED_KEY" ]] && [[ ! -f "$SSH_AUTHORIZED_KEY" ]]; then
129+
echo "Error: SSH authorized pubkey file $SSH_AUTHORIZED_KEY does not exist."
130+
exit 1
131+
fi
132+
118133
if [[ ! -d "$USD_MOUNT_POINT" ]]; then
119134
if [[ "$USD_DEVICE" != "-" ]]; then
120135
echo -n "Creating mount point: $USD_MOUNT_POINT ... "
@@ -272,17 +287,23 @@ run_main()
272287
echo -n "Finalizing image for testing ... "
273288
sed -i "s/^#?PermitRootLogin.*/PermitRootLogin yes/" "$USD_MOUNT_POINT"/etc/ssh/sshd_config
274289
sed -i "s/^#?PasswordAuthentication.*/PasswordAuthentication yes/" "$USD_MOUNT_POINT"/etc/ssh/sshd_config
275-
root_pass="nakamochi"
276-
crypted_root_pass="$(mkpasswd "$root_pass" | sed 's/\$/\\\$/g')"
277-
sed -i "s|^root:[^:]*:|root:$crypted_root_pass:|" "$USD_MOUNT_POINT"/etc/shadow
290+
if [[ "$skip_passwd" -eq 0 ]]; then
291+
root_pass="nakamochi"
292+
crypted_root_pass="$(mkpasswd "$root_pass" | sed 's/\$/\\\$/g')"
293+
sed -i "s|^root:[^:]*:|root:$crypted_root_pass:|" "$USD_MOUNT_POINT"/etc/shadow
294+
fi
278295
echo "done."
279-
echo "Test image root password is $root_pass, ssh root login allowed."
296+
if [[ "$skip_passwd" -eq 0 ]]; then
297+
echo "Test image root password is $root_pass, ssh root login allowed."
298+
fi
280299
else
281300
echo -n "Finalizing image for production ... "
282301
sed -i "s/^#?PermitRootLogin.*/PermitRootLogin no/" "$USD_MOUNT_POINT"/etc/ssh/sshd_config
283302
sed -i "s/^#?PasswordAuthentication.*/PasswordAuthentication no/" "$USD_MOUNT_POINT"/etc/ssh/sshd_config
284-
crypted_root_pass="$(mkpasswd "$(tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 13; echo)" | sed 's/\$/\\\$/g')"
285-
sed -i "s|^root:[^:]*:|root:$crypted_root_pass:|" "$USD_MOUNT_POINT"/etc/shadow
303+
if [[ "$skip_passwd" -eq 0 ]]; then
304+
crypted_root_pass="$(mkpasswd "$(tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 13; echo)" | sed 's/\$/\\\$/g')"
305+
sed -i "s|^root:[^:]*:|root:$crypted_root_pass:|" "$USD_MOUNT_POINT"/etc/shadow
306+
fi
286307
echo "done."
287308
fi
288309

@@ -304,6 +325,15 @@ run_main()
304325
cp "$base_dir"/../rootfiles/etc/wpa_supplicant/wpa_supplicant.conf "$USD_MOUNT_POINT"/etc/wpa_supplicant/wpa_supplicant.conf
305326
echo "done."
306327

328+
if [[ -n "$SSH_AUTHORIZED_KEY" ]] && [[ -f "$SSH_AUTHORIZED_KEY" ]]; then
329+
# add SSH authorized key to root user
330+
echo -n "Adding SSH authorized key from $SSH_AUTHORIZED_KEY ... "
331+
mkdir -p "$USD_MOUNT_POINT"/root/.ssh
332+
cat "$SSH_AUTHORIZED_KEY" >> "$USD_MOUNT_POINT"/root/.ssh/authorized_keys
333+
chmod 600 "$USD_MOUNT_POINT"/root/.ssh/authorized_keys
334+
echo "done."
335+
fi
336+
307337
sync
308338
echo "All DONE, Nakamochi uSD and SSD should be ready!"
309339
}

0 commit comments

Comments
 (0)