Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFE: Root password #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions nixos-infect
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

set -e -o pipefail

ROOT_PASSWORD=

makeConf() {
# Skip everything if main config already present
[[ -e /etc/nixos/configuration.nix ]] && return 0
# NB <<"EOF" quotes / $ ` in heredocs, <<EOF does not
mkdir -p /etc/nixos
# generate some default password, to make login possible if network is down
set +o pipefail
ROOT_PASSWORD="$(cat /dev/urandom | tr -dc _A-Z-a-z-0-9 | head -c12)"
set -o pipefail
# Prevent grep for sending error code 1 (and halting execution) when no lines are selected : https://www.unix.com/man-page/posix/1P/grep
local IFS=$'\n'; keys=($(grep -vE '^[[:space:]]*(#|$)' /root/.ssh/authorized_keys || [[ $? == 1 ]]))
local network_import=""
Expand All @@ -29,6 +35,9 @@ makeConf() {
users.users.root.openssh.authorizedKeys.keys = [$(for key in "${keys[@]}"; do echo -n "
\"$key\""; done)
];
$([ -n "$ROOT_PASSWORD" ] && echo "
users.users.root.password = ''$ROOT_PASSWORD'';
")
}
EOF
# If you rerun this later, be sure to prune the filesSystems attr
Expand Down Expand Up @@ -105,6 +114,8 @@ EOF
ipv6.addresses = [$(for a in "${eth0_ip6s[@]}"; do echo -n "
$a"; done)
];
ipv4.routes = [ { address = "${gateway}"; prefixLength = 32; } ];
ipv6.routes = [ { address = "${gateway6}"; prefixLength = 32; } ];
};
$interfaces1
};
Expand Down Expand Up @@ -260,6 +271,14 @@ makeConf
infect
removeSwap


if [ -n "$ROOT_PASSWORD" ]; then
echo "-----"
echo "Root password set: $ROOT_PASSWORD"
echo "You can use it if instance is not reachable through SSH. Don't forget to change it after infection complete."
echo "-----"
fi

if [[ -z "$NO_REBOOT" ]]; then
reboot
fi