Skip to content

Commit

Permalink
Refactor new --sshcopyid behavior into separate cmdline option --sshc…
Browse files Browse the repository at this point in the history
…opyauth

Related to commit 07e835e and the discussion within
#153

If execution of --sshcopyid fails, then user might want to be aware of
it. So instead of implementing the copying of .ssh/authorized_keys as
fallback of --sshcopyid, let's provide it via cmdline option
--sshcopyauth.

Reviewed-by: Chris Hofstaedtler
Reviewed-by: Darshaka Pathirana
Closes: #153
  • Loading branch information
mika committed Jun 3, 2020
1 parent 64250ae commit 3865f3e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
36 changes: 34 additions & 2 deletions grml-debootstrap
Expand Up @@ -155,6 +155,7 @@ Configuration options:
--hostname <name> Hostname of Debian system.
--nopassword Do not prompt for the root password.
--password <pwd> Use specified password as password for user root.
--sshcopyauth Use ${HOME}/.ssh/authorized_keys to authorise root login on the target system.
--sshcopyid Use locally available public keys to authorise root login on the target system.
--bootappend <line> Add specified appendline to kernel whilst booting.
--chroot-scripts <d> Execute chroot scripts from specified directory.
Expand Down Expand Up @@ -343,7 +344,7 @@ fi
# }}}

# cmdline handling {{{
CMDLINE_OPTS=mirror:,iso:,release:,target:,mntpoint:,debopt:,defaultinterfaces,interactive,nodebootstrap,nointerfaces,nokernel,nopackages,filesystem:,config:,confdir:,packages:,chroot-scripts:,scripts:,post-scripts:,pre-scripts:,debconf:,vm,vmfile,vmsize:,keep_src_list,hostname:,password:,nopassword,grmlrepos,backportrepos,bootappend:,grub:,efi:,arch:,insecure,verbose,help,version,force,debug,contrib,non-free,remove-configs,sshcopyid
CMDLINE_OPTS=mirror:,iso:,release:,target:,mntpoint:,debopt:,defaultinterfaces,interactive,nodebootstrap,nointerfaces,nokernel,nopackages,filesystem:,config:,confdir:,packages:,chroot-scripts:,scripts:,post-scripts:,pre-scripts:,debconf:,vm,vmfile,vmsize:,keep_src_list,hostname:,password:,nopassword,grmlrepos,backportrepos,bootappend:,grub:,efi:,arch:,insecure,verbose,help,version,force,debug,contrib,non-free,remove-configs,sshcopyid,sshcopyauth

_opt_temp=$(getopt --name grml-debootstrap -o +m:i:r:t:p:c:d:vhV --long \
$CMDLINE_OPTS -- "$@")
Expand Down Expand Up @@ -460,6 +461,9 @@ while :; do
--sshcopyid) # Use locally available public keys to authorise root login on the target system
_opt_sshcopyid=T
;;
--sshcopyauth) # Use .ssh/authorized_keys to authorise root login on the target system
_opt_sshcopyauth=T
;;
--grmlrepos) # Enable Grml repository
_opt_grmlrepos=T
;;
Expand Down Expand Up @@ -561,6 +565,7 @@ done
[ "$_opt_nointerfaces" ] && NOINTERFACES="true"
[ "$_opt_nokernel" ] && NOKERNEL="true"
[ "$_opt_sshcopyid" ] && SSHCOPYID="true"
[ "$_opt_sshcopyauth" ] && SSHCOPYAUTH="true"
[ "$_opt_bootappend" ] && BOOT_APPEND=$_opt_bootappend
[ "$_opt_grub" ] && GRUB=$_opt_grub
[ "$_opt_efi" ] && EFI=$_opt_efi
Expand Down Expand Up @@ -589,6 +594,12 @@ if [ "$_opt_grub" ] && [ "$_opt_vmfile" ] ; then
bailout 1
fi

if [ "${_opt_sshcopyid}" ] && [ "${_opt_sshcopyauth}" ] ; then
eerror "The --sshcopyid option is incompatible with --sshcopyauth, please drop either of them from your command line."
eend 1
bailout 1
fi

if [ -n "$ISO" ] && [[ "$DEBOOTSTRAP" =~ mmdebstrap$ ]] ; then
eerror "The ISO option is incompatible with usage of mmdebstrap for bootstrapping."
eerror "Either drop the --iso ... option or use plain debootstrap instead."
Expand Down Expand Up @@ -1810,7 +1821,28 @@ iface ${interface} inet dhcp
bailout 1
fi
else
eerror "Could not open a connection to your authentication agent or the agent has no identites."
eerror "Error: Could not open a connection to your authentication agent or the agent has no identities."
eend 1
bailout 1
fi
fi

if [ -n "${SSHCOPYAUTH}" ] ; then
AUTHORIZED_KEYS_SOURCE=${AUTHORIZED_KEYS_SOURCE:-${HOME}/.ssh/authorized_keys}

if ! [ -f "${AUTHORIZED_KEYS_SOURCE}" ]; then
eerror "Error: could not read '${AUTHORIZED_KEYS_SOURCE}' for setting up SSH key login."
eend 1
bailout 1
fi

AUTHORIZED_KEYS_TARGET="${MNTPOINT}/root/.ssh/"
einfo "Copying '${AUTHORIZED_KEYS_SOURCE}' to '${AUTHORIZED_KEYS_TARGET}' as requested via --sshcopyauth option."
mkdir -m 0700 -p "${AUTHORIZED_KEYS_TARGET}"
if cp "${AUTHORIZED_KEYS_SOURCE}" "${AUTHORIZED_KEYS_TARGET}" ; then
eend 0
else
eerror "Error: copying '${AUTHORIZED_KEYS_SOURCE}' to '${AUTHORIZED_KEYS_TARGET}' failed."
eend 1
bailout 1
fi
Expand Down
7 changes: 7 additions & 0 deletions grml-debootstrap.8.txt
Expand Up @@ -226,6 +226,13 @@ Options and environment variables
Delete grml-debootstrap configuration files (/etc/debootstrap/*) from installed
system. Useful for reproducible builds or if you don't want to leak information.

*--sshcopyauth*::

Use locally available _$HOME/.ssh/authorized_keys_ to authorise root login on the target system.
This installs _$HOME/.ssh/authorized_keys_ as _/root/.ssh/authorized_keys_ on
the target system. If a different file than _$HOME/.ssh/authorized_keys_ should
be used, the environment variable _AUTHORIZED_KEYS_SOURCE_ can be used.

*--sshcopyid*::

Use locally available public keys to authorise root login on the target system.
Expand Down

0 comments on commit 3865f3e

Please sign in to comment.