From a67d54e9dc1052a2d71a50c140c752c133f74e7a Mon Sep 17 00:00:00 2001 From: David Kebler Date: Wed, 21 Feb 2024 11:33:37 -0800 Subject: [PATCH] Major refactor upgrades for remote scripting and access via ssh ssh-copy now uses options and has help and uses new helpers to prepare the scp command remote module totally redone. now uses sshcp (scp) to copy the file makes use of the much improved bundle function for bundling script now that remote_script is refactored can use for ssh-pub-key and for the uci remote install function cleaned up ssh function a bit add parsing functions to net-utils for parsing host --- modules/install-shell-base.lib | 39 --- modules/install-shell-base.sh | 30 ++ modules/net-utils.mod | 28 +- modules/nomachine.lib | 13 + modules/remote.mod | 264 ++++++++++++------ modules/ssh-config.mod | 4 +- modules/ssh-copy.func | 160 ++++++++--- modules/ssh-pubkey.mod | 225 ++++++++------- modules/ssh.func | 29 +- modules/sshfs.mod | 2 +- ssh/etc/sshd_config | 1 + ssh/etc/sshd_config.d/01-lockdown.conf | 7 + .../sshd_config.d/02-enable-X11-forward.conf | 3 + ssh/etc/sshd_config.d/02-enable-sftp.conf | 5 + ssh/etc/sshd_config.d/10-deny.conf | 2 + ssh/etc/sshd_config.d/31-SomeFromAddress.conf | 5 + ssh/etc/sshd_config.sample | 116 ++++++++ ssh/session/interactive.off | 5 - ssh/session/readme.md | 2 - ssh/ssh.inst | 3 + 20 files changed, 653 insertions(+), 290 deletions(-) delete mode 100644 modules/install-shell-base.lib create mode 100644 modules/install-shell-base.sh create mode 100644 modules/nomachine.lib create mode 100644 ssh/etc/sshd_config create mode 100644 ssh/etc/sshd_config.d/01-lockdown.conf create mode 100644 ssh/etc/sshd_config.d/02-enable-X11-forward.conf create mode 100644 ssh/etc/sshd_config.d/02-enable-sftp.conf create mode 100644 ssh/etc/sshd_config.d/10-deny.conf create mode 100644 ssh/etc/sshd_config.d/31-SomeFromAddress.conf create mode 100644 ssh/etc/sshd_config.sample delete mode 100644 ssh/session/interactive.off delete mode 100644 ssh/session/readme.md create mode 100644 ssh/ssh.inst diff --git a/modules/install-shell-base.lib b/modules/install-shell-base.lib deleted file mode 100644 index 3bf631f..0000000 --- a/modules/install-shell-base.lib +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash -# assumes that bash is installed -#!/bin/bash - -# must! be run as sudo -install_shell_base () { - # TODO have a cross distro package install - [[ ! $(which git) ]] && echo git must be installed first && return 1 - [[ ! $EUID -eq 0 ]] && { echo ERROR script must be run as root; return 2; } - mkdir -p /shell/base - git clone https://git.kebler.net/bash/shell-base.git /shell/base - chown -R ${1:-$1000}:${1:-1000} /shell - chmod -R +r /shell - /bin/bash /shell/base/install/install.sh ${1:-$1000} -} - -# remote_install_shell_base -r -# module_load install-shell-base && remote_install_shell_base -r newboxr -remote_install_shell_base () { - module_load remote - local supass;local user - if [[ $1 == "-r" ]]; then - shift 1; - user=$2; - else - [[ ! $2 ]] && { echo sudo password for remote user required; return 2; } - supass="-p $2" - user=$3 - fi - remote_script $supass install-shell-base $user -- $1 -} - -# # if script was executed then call the function -(return 0 2>/dev/null) || install_shell_base $@ - -# example -# $ module_load install-shell-base -# $ remote_install_shell_base -r portabler david - diff --git a/modules/install-shell-base.sh b/modules/install-shell-base.sh new file mode 100644 index 0000000..400e440 --- /dev/null +++ b/modules/install-shell-base.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# assumes that bash is installed +#!/bin/bash +# must! be run as sudo + +install_shell_base () { + # TODO have a cross distro package install + module_load distro + set_distro + + [[ ! $(command -v git) ]] && echo git must be installed first && $INSTALL_PKGS git + # TODO, avoid which in all scripts. or put which in environment if not on machine + [[ ! $(command -v which) ]] && echo 'which' must be installed first && $INSTALL_PKGS which + echo $USER, $EUID + [[ ! $EUID -eq 0 ]] && { echo ERROR script must be run as root; return 2; } + echo I am ROOT, now running script + echo UCI user: $1 + # TODO make the repo and clone values dynamic or bundle current base on sending machine a archive + mkdir -p /shell/base + git clone https://git.kebler.net/bash/shell-base.git /shell/base + chown -R ${1:-$1000}:${1:-1000} /shell + chmod -R +r /shell + + /bin/bash /shell/base/install/install.sh ${1:-1000} +} + +# # if script was executed then call the function + ( return 0 2>/dev/null ) || install_shell_base $@ + + diff --git a/modules/net-utils.mod b/modules/net-utils.mod index 8718556..4077a97 100644 --- a/modules/net-utils.mod +++ b/modules/net-utils.mod @@ -37,26 +37,36 @@ lookup_host () { # usage: lookup_host hostname < configfile > local config; local host; local lhost config=$([[ $2 ]] && echo $2 || echo ${SSH_CONFIG:-$HOME/.ssh/config}) -host=$(parse_host $1) +host=$(get_hostname_host $1)return lhost=$(ssh -F $config -G $host | grep -w hostname | cut -d' ' -f2) [[ $lhost ]] && echo $lhost || echo $host } -parse_host () { - # usage: parse_host +get_hostname_host () { + # usage: get_hostname_host # returns extracted host if passed user@host # otherwise return value passed - [[ $1 =~ "@" ]] && { echo $(sed 's/.*@\(.*\)/\1/' <<< "$1");return 0; } - echo $1 - return 1 + # [[ $1 =~ "@" ]] && { echo $(sed 's/.*@\(.*\)/\1/' <<< "$1");return 0; } + local hostname + hostname=$(cut -s -d@ -f2 <<< "$1") + if [[ ! $hostname ]] && type ssh_config_get &>/dev/null ; then + hostname=$(ssh_config_get -h $1) + fi + [[ $hostname ]] && echo $hostname || return 1 } -parse_user () { - # usage: parse_user +get_user_host () { + # [[ $1 =~ "@" ]] && { echo $(sed 's/\(.*\)@.*/\1/' <<< "$1"); return 0; } || return 1 + # usage: get_user_host # returns extracted user if passed user@host # otherwise returns nothing # return 1 no user in string - [[ $1 =~ "@" ]] && { echo $(sed 's/\(.*\)@.*/\1/' <<< "$1"); return 0; } || return 1 + local user + user=$(cut -s -d@ -f1 <<< "$1") + if [[ ! $user ]] && type ssh_config_get &>/dev/null ; then + user=$(ssh_config_get -u $1) + fi + [[ $user ]] && echo $user || return 1 } diff --git a/modules/nomachine.lib b/modules/nomachine.lib new file mode 100644 index 0000000..fe13e2e --- /dev/null +++ b/modules/nomachine.lib @@ -0,0 +1,13 @@ +/usr/NX/etc/server.cfg + +sed + +#AcceptedAuthenticationMethods all +to +AcceptedAuthenticationMethods NX-private-key + +restrat the server + +sdr nxserver + +copy a public key to ~/.nx/config/authorized.crt \ No newline at end of file diff --git a/modules/remote.mod b/modules/remote.mod index 146ac22..0f42587 100644 --- a/modules/remote.mod +++ b/modules/remote.mod @@ -1,97 +1,172 @@ #!/bin/bash module_load ssh +module_load bundle +module_load helpers +module_load ssh-copy + + +remote_function () { + [[ $2 ]] && module_load $2 + if declare -f $1; then + local file + file=$(mkrfilename function) + $(declare -f $1) > $file + echo $file + else + >&2 echo fatal: unable to source funtion $1, aborting + return 1 + fi +} + + remote_script () { - # usage: remote_script script -- host - # see ssh function - # -local sshargs;local user;local supass;local fn;local args;local file -[[ "$*" =~ "--" ]] && sshargs=$(sed 's/.*--\(.*\)/\1/' <<< "$@") -[[ ! $sshargs ]] && { echo missing remote machine, must provide at least a hostname, -- hostname; return 3; } -# reset arguments to just those before -- -set -- $(sed 's/\(.*\)--.*/\1/' <<< "$@") + local sshargs;local user;local supass;local cfn; local rfn; local args; + local script; local host; local dr; local supass + local hostname; local bscript; local ruser; local usesudo -local OPTION - local OPTARG - local OPTIND - while getopts 'u:s:f:p:' OPTION; do - # echo OPTION $OPTION ARG $OPTARG INDX $OPTIND - case "$OPTION" in - s) - # script filename to run - file=$OPTARG - ;; + help() { + + cat < host script