From f72488708bb98ab928db9aec902b06412da89db9 Mon Sep 17 00:00:00 2001 From: Lukas Macura Date: Mon, 21 Nov 2016 16:59:59 +0100 Subject: [PATCH] Repaired 'which' detection and user environment checking --- README.md | 6 +++++- install.sh | 18 ++++++++++++------ lib/ctrl.lib.sh | 8 ++++---- lib/os.lib.sh | 20 +++++++++++++++----- lib/zafcache | 2 +- 5 files changed, 37 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index cff3eb4..e9cdeee 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,8 @@ git clone https://github.com/limosek/zaf.git; cd zaf; git checkout master ``` So you can pass ANY configuration of your zabbix agent directly to installer prefixing it with *Z_*. Please note that options are *Case Sensitive*! -Next to this, you can pass ANY zaf config options by *ZAF_* prefix. Interresting ZAF options: +Next to this, you can pass ANY zaf config options by *ZAF_* prefix. You can see all interesting ZAF options if you use install interactive command. +Interresting ZAF options: ``` # If we want to use GIT and local GIT repository ZAF_GIT='1'# Default @@ -63,6 +64,9 @@ Next to this, you can pass ANY zaf config options by *ZAF_* prefix. Interresting # Plugins can be downloaded from http[s] too ZAF_REPO_URL='https://raw.githubusercontent.com/limosek/zaf-plugins/master/' + # Force installer to act as root even if it is not detected + ZAF_ISROOT=1 + ``` Installer will try to autoguess suitable config options for your system. diff --git a/install.sh b/install.sh index df49095..86f5943 100755 --- a/install.sh +++ b/install.sh @@ -39,6 +39,7 @@ zaf_download_files() { zaf_get_option(){ local opt + ZAF_HELP_OPTS="$ZAF_HELP_OPTS\n$1 $2 [$3]" eval opt=\$C_$1 if [ -n "$opt" ]; then eval "$1='$opt'" @@ -231,7 +232,7 @@ zaf_configure(){ fi fi fi - if which git >/dev/null; then + if zaf_which git >/dev/null; then ZAF_GIT=1 else ZAF_GIT=0 @@ -295,12 +296,17 @@ zaf_configure(){ zaf_set_option ZAF_FILES_GID "$ZAF_FILES_GID" zaf_set_option ZAF_FILES_UMASK "$ZAF_FILES_UMASK" zaf_set_option ZAF_AGENT_RESTART "$ZAF_AGENT_RESTART" - if [ -f $ZABBIX_SERVER_BIN ]; then + if [ -x "$ZAF_SERVER_BIN" ]; then zaf_set_option ZAF_SERVER_CONFIG "$ZAF_SERVER_CONFIG" zaf_set_option ZAF_SERVER_CONFIGD "$ZAF_SERVER_CONFIGD" zaf_set_option ZAF_SERVER_EXTSCRIPTS "$(zaf_get_zabbix_option $ZAF_SERVER_CONFIG ExternalScripts)" + zaf_set_option ZAF_SERVER_BIN "$ZAF_SERVER_BIN" + else + zaf_set_option ZAF_SERVER_CONFIG "" + zaf_set_option ZAF_SERVER_BIN "" + zaf_set_option ZAF_SERVER_CONFIGD "" + zaf_set_option ZAF_SERVER_EXTSCRIPTS "" fi - zaf_set_option ZAF_SERVER_BIN "$ZAF_SERVER_BIN" zaf_set_option ZAF_SUDOERSD "$ZAF_SUDOERSD" zaf_set_option ZAF_CROND "$ZAF_CROND" zaf_set_option ZAF_ZBXAPI_URL "$ZAF_ZBXAPI_URL" @@ -378,7 +384,7 @@ if ! [ -f README.md ]; then export ZAF_TMP_DIR="/tmp/zaf-installer" export ZAF_DIR="$ZAF_TMP_DIR/zaf" mkdir -p $ZAF_TMP_DIR - if ! which curl >/dev/null; + if ! zaf_which curl >/dev/null; then zaf_err "Curl not found. Cannot continue. Please install it." fi @@ -390,7 +396,7 @@ if ! [ -f README.md ]; then fi # Try to load local downloaded libs -if ! type zaf_version >/dev/null; then +if ! type zaf_version >/dev/null 2>/dev/null; then . lib/zaf.lib.sh . lib/plugin.lib.sh . lib/os.lib.sh @@ -500,7 +506,7 @@ install) echo 'Example 2 (preconfigure agent options): install.sh auto A_Server=zabbix.server A_ServerActive=zabbix.server A_Hostname=$(hostname)' echo "Example 3 (preconfigure zaf packaging system to use): install.sh auto ZAF_PKG=opkg" echo "Example 4 (interactive): install.sh interactive" - echo + echo exit 1 esac diff --git a/lib/ctrl.lib.sh b/lib/ctrl.lib.sh index 55cf0e4..27cf682 100644 --- a/lib/ctrl.lib.sh +++ b/lib/ctrl.lib.sh @@ -137,7 +137,7 @@ zaf_ctrl_check_deps() { fi deps=$(zaf_ctrl_get_global_block <$1 | zaf_block_get_option "Depends-bin" ) for cmd in $deps; do - if ! which $cmd >/dev/null; then + if ! zaf_which $cmd >/dev/null; then zaf_err "Missing binary dependency $cmd. Please install it first." fi done @@ -160,13 +160,13 @@ zaf_ctrl_sudo() { zaf_dbg "Installing sudoers entry $ZAF_SUDOERSD/zaf_$plugin" sudo=$(zaf_ctrl_get_global_option $2 "Sudo" | zaf_far '{PLUGINDIR}' "${plugindir}") [ -z "$sudo" ] && return # Nothing to install - if ! which sudo >/dev/null; then + if ! zaf_which sudo >/dev/null; then zaf_wrn "Sudo needed bud not installed?" fi cmd=$(echo $sudo | cut -d ' ' -f 1) parms=$(echo $sudo | cut -d ' ' -f 2-) - if which $cmd >/dev/null ; then - (echo "zabbix ALL=NOPASSWD:SETENV: $(which $cmd) $(echo $parms | tr '%' '*')";echo) >$ZAF_SUDOERSD/zaf_$plugin || zaf_err "Error during zaf_ctrl_sudo" + if zaf_which $cmd >/dev/null ; then + (echo "zabbix ALL=NOPASSWD:SETENV: $(zaf_which $cmd) $(echo $parms | tr '%' '*')";echo) >$ZAF_SUDOERSD/zaf_$plugin || zaf_err "Error during zaf_ctrl_sudo" chmod 0440 $ZAF_SUDOERSD/zaf_$plugin else zaf_err "Cannot find binary '$cmd' to put into sudoers." diff --git a/lib/os.lib.sh b/lib/os.lib.sh index 2b9f857..cd3c807 100644 --- a/lib/os.lib.sh +++ b/lib/os.lib.sh @@ -20,31 +20,41 @@ zaf_configure_os_freebsd() { ZAF_SUDOERSD="/usr/local/etc/sudoers.d" } +zaf_which() { + if which >/dev/null 2>/dev/null; then + which "$1" + else + [ -x /bin/$1 ] && { echo /bin/$1; return; } + [ -x /usr/bin/$1 ] && { echo /usr/bin/$1; return; } + return 1 + fi +} + zaf_detect_system() { ZAF_FILES_UID=zabbix ZAF_FILES_GID=zabbix ZAF_FILES_UMASK=0770 - if which dpkg >/dev/null; then + if zaf_which dpkg >/dev/null; then ZAF_PKG=dpkg ZAF_OS=$(lsb_release -is|zaf_tolower) ZAF_OS_CODENAME=$(lsb_release -cs|zaf_tolower) ZAF_CURL_INSECURE=0 ZAF_AGENT_PKG="zabbix-agent" return - else if which rpm >/dev/null; then + else if zaf_which rpm >/dev/null; then ZAF_PKG="rpm" ZAF_OS=$(lsb_release -is|zaf_tolower) ZAF_OS_CODENAME=$(lsb_release -cs|zaf_tolower) ZAF_CURL_INSECURE=0 ZAF_AGENT_PKG="zabbix-agent" return - else if which opkg >/dev/null; then + else if zaf_which opkg >/dev/null; then ZAF_PKG="opkg" . /etc/openwrt_release ZAF_OS="$(echo $DISTRIB_ID|zaf_tolower)" ZAF_OS_CODENAME="$(echo $DISTRIB_CODENAME|zaf_tolower)" return - else if which pkg >/dev/null; then + else if zaf_which pkg >/dev/null; then ZAF_PKG="pkg" ZAF_OS="freebsd" ZAF_OS_CODENAME="$(freebsd-version|cut -d '-' -f 1)" @@ -81,7 +91,7 @@ zaf_os_specific(){ } zaf_is_root(){ - [ "$USER" = "root" ] + [ "$USER" = "root" ] || [ "$EUID" = "0" ] || [ -n "$ZAF_ISROOT" ] } # Install file, bin or directory and respect install prefix diff --git a/lib/zafcache b/lib/zafcache index 3fc3c24..7cca7fb 100644 --- a/lib/zafcache +++ b/lib/zafcache @@ -21,7 +21,7 @@ fi if ! zaf_fromcache "$key"; then zaf_fromcache "$key" >/dev/null 2>/dev/null [ $? -eq 3 ] && { zaf_err "Operation $key already in progress."; } - if which at >/dev/null 2>/dev/null; then + if zaf_which at >/dev/null 2>/dev/null; then at -M now >/dev/null 2>/dev/null <"\$tmpf" | zaf_tocache_stdin "$key" "$seconds" ; [ -s \$tmpf ] && zaf_wrn <\$tmpf EOF