From b18919362a4334cc101fbbd65fbae1366446bef0 Mon Sep 17 00:00:00 2001 From: Lukas Macura Date: Sun, 24 Apr 2016 04:51:35 +0200 Subject: [PATCH] Repaired files and modes Repaired init functions --- install.sh | 107 +++++++++++++++++++++++------------------------ lib/cache.lib.sh | 31 +++++++++++--- lib/os.lib.sh | 3 ++ lib/preload.sh | 25 +++-------- lib/zaf.lib.sh | 23 ++++++++++ zaf | 27 ++---------- 6 files changed, 114 insertions(+), 102 deletions(-) diff --git a/install.sh b/install.sh index 8ac9725..74384c1 100755 --- a/install.sh +++ b/install.sh @@ -1,17 +1,6 @@ #!/bin/sh -[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1 -export ZAF_LOG_STDERR="-s" -if [ -z "$ZAF_URL" ]; then - # Runing as standalone install.sh. We have to download rest of files first. - ZAF_URL="https://github.com/limosek/zaf/" -fi - -[ -z "$ZAF_GITBRANCH" ] && ZAF_GITBRANCH=1.2 - -ZAF_TMP_DIR="/tmp/zaf-installer" -ZAF_DIR="$ZAF_TMP_DIR/zaf" -mkdir -p $ZAF_TMP_DIR +############### Functions # Lite version of zaf_fetch_url, full version will be loaded later zaf_fetch_url(){ @@ -33,40 +22,6 @@ zaf_download_files() { zaf_fetch_url $ZAF_URL/archive/$ZAF_GITBRANCH.tar.gz | tar -f - -C $ZAF_TMP_DIR -zx && mv $ZAF_TMP_DIR/zaf-$ZAF_GITBRANCH $ZAF_DIR || zaf_err "Cannot download and unpack zaf!" } -if ! [ -f README.md ]; then - # We are runing from stdin - if ! which curl >/dev/null; - then - zaf_err "Curl not found. Cannot continue. Please install it." - fi - echo "Installing from url $url..." - [ -z "$*" ] && auto=auto - zaf_download_files && cd $ZAF_DIR && exec ./install.sh $auto "$@" - echo "Error downloading and runing installer!" >&2 - exit 2 -fi - -if ! type zaf_version >/dev/null; then -. lib/zaf.lib.sh -. lib/plugin.lib.sh -. lib/os.lib.sh -. lib/ctrl.lib.sh -. lib/cache.lib.sh -. lib/zbxapi.lib.sh -fi - -# Read options as config for ZAF -for pair in "$@"; do - echo $pair | grep -q '^ZAF\_' || continue - option=$(echo $pair|cut -d '=' -f 1) - value=$(echo $pair|cut -d '=' -f 2-) - eval "C_${option}='$value'" - zaf_wrn "Overriding $option from cmdline." -done - -[ -z "$ZAF_CFG_FILE" ] && ZAF_CFG_FILE=$INSTALL_PREFIX/etc/zaf.conf -[ -n "$C_ZAF_DEBUG" ] && ZAF_DEBUG=$C_ZAF_DEBUG - # Read option. If it is already set in zaf.conf, it is skipped. If env variable is set, it is used instead of default # It sets global variable name on result. # $1 - option name @@ -269,6 +224,9 @@ zaf_configure(){ zaf_set_option ZAF_AGENT_CONFIG "$ZAF_AGENT_CONFIG" zaf_set_option ZAF_AGENT_CONFIGD "$ZAF_AGENT_CONFIGD" zaf_set_option ZAF_AGENT_BIN "$ZAF_AGENT_BIN" + zaf_set_option ZAF_FILES_UID "$ZAF_FILES_UID" + 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" zaf_set_option ZAF_SUDOERSD "$ZAF_SUDOERSD" zaf_set_option ZAF_CROND "$ZAF_CROND" @@ -321,18 +279,59 @@ zaf_postconfigure() { true } +############ First stage Init + +if ! [ -f README.md ]; then + # We are runing from stdin + if ! which curl >/dev/null; + then + zaf_err "Curl not found. Cannot continue. Please install it." + fi + echo "Installing from url $url..." + [ -z "$*" ] && auto=auto + zaf_download_files && cd $ZAF_DIR && exec ./install.sh $auto "$@" + echo "Error downloading and runing installer!" >&2 + exit 2 +fi + +# Try to load local downloaded libs +if ! type zaf_version >/dev/null; then +. lib/zaf.lib.sh +. lib/plugin.lib.sh +. lib/os.lib.sh +. lib/ctrl.lib.sh +. lib/cache.lib.sh +. lib/zbxapi.lib.sh +fi +# If something was wrong reading libs, then exit +if ! type zaf_version >/dev/null; then + echo "Problem loading libraries?" + exit 2 +fi + +########### Second stage init (all functions loaded) + +[ -z "$ZAF_CFG_FILE" ] && ZAF_CFG_FILE=$INSTALL_PREFIX/etc/zaf.conf if [ -f "${ZAF_CFG_FILE}" ]; then . "${ZAF_CFG_FILE}" fi -ZAF_TMP_DIR="/tmp/zaf-installer/" -! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}" -# If debug is on, do not remove tmp dir -if [ "$ZAF_DEBUG" -le 3 ]; then - trap "rm -rf ${ZAF_TMP_DIR} " EXIT -else - trap 'zaf_wrn "Leaving $ZAF_TMP_DIR" contents due to ZAF_DEBUG.' EXIT -fi +zaf_debug_init stderr + +ZAF_TMP_DIR="/tmp/zaf-installer" +ZAF_DIR="$ZAF_TMP_DIR/zaf" +zaf_tmp_init + +zaf_cache_init + +# Read options as config for ZAF +for pair in "$@"; do + echo $pair | grep -q '^ZAF\_' || continue + option=$(echo $pair|cut -d '=' -f 1) + value=$(echo $pair|cut -d '=' -f 2-) + eval "C_${option}='$value'" + zaf_wrn "Overriding $option from cmdline." +done case $1 in interactive) diff --git a/lib/cache.lib.sh b/lib/cache.lib.sh index 663138d..95a64b0 100644 --- a/lib/cache.lib.sh +++ b/lib/cache.lib.sh @@ -1,5 +1,30 @@ # Zaf cache related functions +zaf_cache_init(){ + [ -z "$ZAF_CACHE_DIR" ] && ZAF_CACHE_DIR=${ZAF_TMP_BASE}c + if [ -n "$ZAF_CACHE_DIR" ]; then + mkdir -p "$ZAF_CACHE_DIR" + if zaf_is_root; then + zaf_trc "Cache: Changing perms to $ZAF_CACHE_DIR (zabbix/$ZAF_ZABBIX_GID/0770)" + chown $ZAF_FILES_UID "$ZAF_CACHE_DIR" + chgrp $ZAF_FILES_GID "$ZAF_CACHE_DIR" + chmod $ZAF_FILES_UMASK "$ZAF_CACHE_DIR" + fi + if [ -w $ZAF_CACHE_DIR ]; then + zaf_trc "Cache: Removing stale entries" + (cd $ZAF_CACHE_DIR && find ./ -type f -name '*.info' -mmin +1 | \ + while read line ; do + echo rm -f $line $(basename $line .info) + done + ) + else + zaf_err "Cache dir is not accessible! Become root or member of $ZAF_FILES_GID group!" + fi + else + zaf_err "Cache dir not set." + fi +} + zaf_cache_clean(){ if [ -n "$ZAF_CACHE_DIR" ]; then zaf_wrn "Removing cache entries" @@ -7,11 +32,7 @@ zaf_cache_clean(){ else zaf_err "Cache dir not set." fi - mkdir -p "$ZAF_CACHE_DIR" - if zaf_is_root; then - chmod 770 "$ZAF_CACHE_DIR" - chgrp $(id -g zabbix) "$ZAF_CACHE_DIR" - fi + zaf_cache_init } # Get cache key from requested param diff --git a/lib/os.lib.sh b/lib/os.lib.sh index d10ed66..d3e320c 100644 --- a/lib/os.lib.sh +++ b/lib/os.lib.sh @@ -21,6 +21,9 @@ zaf_configure_os_freebsd() { } zaf_detect_system() { + ZAF_FILES_UID=zabbix + ZAF_FILES_GID=zabbix + ZAF_FILES_UMASK=0770 if which dpkg >/dev/null; then ZAF_PKG=dpkg ZAF_OS=$(lsb_release -is|zaf_tolower) diff --git a/lib/preload.sh b/lib/preload.sh index d638215..cbd9795 100644 --- a/lib/preload.sh +++ b/lib/preload.sh @@ -9,26 +9,13 @@ . ${ZAF_LIB_DIR}/zbxapi.lib.sh . ${ZAF_LIB_DIR}/cache.lib.sh -[ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf -[ -z "$ZAF_TMP_DIR" ] && ZAF_TMP_DIR="${ZAF_TMP_BASE}-$(zaf_random)" -[ -z "$ZAF_CACHE_DIR" ] && ZAF_CACHE_DIR=${ZAF_TMP_BASE}c - -rm -rf "${ZAF_TMP_DIR}" - -if ! [ -d "${ZAF_TMP_DIR}" ]; then - mkdir "${ZAF_TMP_DIR}" +if ! type zaf_version >/dev/null; then + echo "Problem loading libraries?" + exit 2 fi - -! [ -d "${ZAF_CACHE_DIR}" ] && mkdir "${ZAF_CACHE_DIR}" -[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1 - -if [ "$ZAF_DEBUG" -le 3 ]; then - trap "rm -rf ${ZAF_TMP_DIR}" EXIT -else - trap 'zaf_wrn "Leaving $ZAF_TMP_DIR" contents due to ZAF_DEBUG.' EXIT -fi - -#trap 'touch /tmp/aaaa' ALARM +zaf_debug_init +zaf_tmp_init +zaf_cache_init export ZAF_LIB_DIR export ZAF_TMP_DIR diff --git a/lib/zaf.lib.sh b/lib/zaf.lib.sh index 19566db..43f536d 100644 --- a/lib/zaf.lib.sh +++ b/lib/zaf.lib.sh @@ -38,6 +38,12 @@ zaf_hlp() { dl=$(expr $cols - $kl) printf %-${kl}s%-${dl}s%b "$1" "$2" "\n" } +# $1 if nonempty, log to stderr too +zaf_debug_init() { + [ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1 + export ZAF_DEBUG + [ -n "$1" ] && export ZAF_LOG_STDERR="-s" +} zaf_version(){ echo $ZAF_VERSION @@ -236,6 +242,23 @@ zaf_date_add() { date -d "$1 seconds" "+%Y-%m-%d %H:%M:%S" 2>/dev/null || date -d "$(expr $(date +%s) + $1)" -D %s "+%Y-%m-%d %H:%M:%S" } +zaf_tmp_init() { + [ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf + ZAF_TMP_DIR="${ZAF_TMP_BASE}-$(zaf_random)" + mkdir -p $ZAF_TMP_DIR + if zaf_is_root; then + chown $ZAF_FILES_UID "$ZAF_TMP_DIR" + chgrp $ZAF_FILES_GID "$ZAF_TMP_DIR" + chmod $ZAF_FILES_UMASK "$ZAF_TMP_DIR" + fi + # If debug is on, do not remove tmp dir + if [ "$ZAF_DEBUG" -le 3 ]; then + trap "rm -rf ${ZAF_TMP_DIR}" EXIT + else + trap 'zaf_wrn "Leaving $ZAF_TMP_DIR" contents due to ZAF_DEBUG.' EXIT + fi +} + # Create temp file and return its name # $1 prefix or empty zaf_tmpfile() { diff --git a/zaf b/zaf index a389fda..cf376ba 100755 --- a/zaf +++ b/zaf @@ -43,30 +43,9 @@ else [ -f ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh ] && . ${ZAF_LIB_DIR}/zaf.${ZAF_OS}.sh fi -[ -z "$ZAF_TMP_BASE" ] && ZAF_TMP_BASE=/tmp/zaf -ZAF_TMP_DIR="${ZAF_TMP_BASE}-$(zaf_random)" -[ -z "$ZAF_CACHE_DIR" ] && ZAF_CACHE_DIR=${ZAF_TMP_BASE}c - -[ -z "$ZAF_DEBUG" ] && ZAF_DEBUG=1 -export ZAF_DEBUG -export ZAF_LOG_STDERR="-s" - -# If debug is on, do not remove tmp dir -if [ "$ZAF_DEBUG" -le 3 ]; then - trap "rm -rf ${ZAF_TMP_DIR}" EXIT -else - trap 'zaf_wrn "Leaving $ZAF_TMP_DIR" contents due to ZAF_DEBUG.' EXIT -fi - -! [ -d "${ZAF_TMP_DIR}" ] && mkdir "${ZAF_TMP_DIR}" -if ! [ -d "${ZAF_CACHE_DIR}" ]; then - mkdir "${ZAF_CACHE_DIR}" -fi - -if zaf_is_root; then - chgrp $(id -g zabbix) "${ZAF_CACHE_DIR}" - chmod 770 "${ZAF_CACHE_DIR}" -fi +zaf_debug_init stderr +zaf_tmp_init +zaf_cache_init case $1 in