summaryrefslogtreecommitdiffstats
path: root/helper
diff options
context:
space:
mode:
Diffstat (limited to 'helper')
-rw-r--r--helper/README.helper2
-rw-r--r--helper/downloader.inc42
-rw-r--r--helper/functions.common.sh44
-rw-r--r--helper/logging.inc57
-rw-r--r--helper/useradd.inc68
5 files changed, 138 insertions, 75 deletions
diff --git a/helper/README.helper b/helper/README.helper
index e69de29b..ebe242a0 100644
--- a/helper/README.helper
+++ b/helper/README.helper
@@ -0,0 +1,2 @@
+Put your helper units here
+Naming convention is <unitname>.inc
diff --git a/helper/downloader.inc b/helper/downloader.inc
new file mode 100644
index 00000000..7ded5b4f
--- /dev/null
+++ b/helper/downloader.inc
@@ -0,0 +1,42 @@
+# helper functions for downloading files or packages
+
+# download a file. usage:
+# download FROM [TO]
+# 1. download "http://example.com/something.tar.gz"
+# 2. download "http://example.com/something.tar.gz" "somename.tar.gz"
+download () {
+ [ $# -lt 1 -o $# -gt 2 ] && perror "download called with $# arguments, need 1 or 2"
+ if [ $# -eq 2 ]; then
+ pinfo "Downloading $2 from '$1'...."
+ wget -O "$2" "$1"
+ local RET=$?
+ else
+ pinfo "Downloading '$1'...."
+ wget "$1"
+ local RET=$?
+ fi
+ [ "x$RET" != "x0" ] && perror "downloading $1 failed, wget returned exit code $RET"
+}
+
+# download a file and untar it. usage:
+# download_untar FROM TO_DIR [TEMPFILE]
+# 1. download_untar "http://example.com/something.tar.gz" "src/"
+# 2. download_untar "http://example.com/something.tar.gz" "src/" "temporary_name.tar.gz"
+download_untar () {
+ [ $# -lt 2 -o $# -gt 3 ] && perror "download_untar called with $# arguments, need 2 or 3"
+ local URL=$1
+ local DEST=$2
+ if [ $# -eq 2 ]; then
+ local TMPFILE=dltmp.$(basename "$URL")
+ else
+ local TMPFILE=$3
+ fi
+ download "$URL" "$TMPFILE"
+ mkdir -p "$DEST"
+ pinfo "Unpacking to '$DEST'..."
+ tar xf "$TMPFILE" -C "${DEST}/"
+ local RET=$?
+ [ "x$RET" != "x0" ] && perror "could not untar $TMPFILE to $DEST (tar returned $RET)"
+ unlink "$TMPFILE"
+}
+
diff --git a/helper/functions.common.sh b/helper/functions.common.sh
deleted file mode 100644
index de33363c..00000000
--- a/helper/functions.common.sh
+++ /dev/null
@@ -1,44 +0,0 @@
-
-QUIET=0
-
-LOG_DIR=${ROOT_DIR}/logs
-
-mkdir -p $LOG_DIR
-
-set_quiet () {
- if [ "x$DEBUG" != "x1" -a "x$QUIET" != "x1" ]; then
- exec 6>&1 > $LOG_DIR/stdout.log
- exec 2> $LOG_DIR/stderr.log
- QUIET="1"
- fi
-}
-
-unset_quiet () {
- if [ "x$QUIET" = "x1" ]; then
- exec 1>&6 6>&-
- exec 2>&-
- QUIET="0"
- fi
-}
-
-
-pinfo () {
- unset_quiet
- echo -e "[info] $1"
- set_quiet
-}
-perror () {
- unset_quiet
- echo -e "[error] $1"
- set_quiet
-}
-pecho () {
- unset_quiet
- echo -e "[user] $1"
- set_quiet
-}
-pechon () {
- unset_quiet
- echo -e -n "[user] $1"
- set_quiet
-}
diff --git a/helper/logging.inc b/helper/logging.inc
new file mode 100644
index 00000000..2667e5e5
--- /dev/null
+++ b/helper/logging.inc
@@ -0,0 +1,57 @@
+
+MLTK_QUIET=0
+
+LOG_DIR=${ROOT_DIR}/logs
+
+mkdir -p $LOG_DIR
+
+set_quiet () {
+ if [ "x$DEBUG" != "x1" -a "x$MLTK_QUIET" != "x1" ]; then
+ exec 6>&1 > $LOG_DIR/stdout.log
+ exec 7>&2 2> $LOG_DIR/stderr.log
+ MLTK_QUIET="1"
+ fi
+}
+
+unset_quiet () {
+ if [ "x$MLTK_QUIET" = "x1" ]; then
+ exec 1>&6 6>&-
+ exec 2>&7 7>&-
+ MLTK_QUIET="0"
+ fi
+}
+
+
+pinfo () {
+ if [ "x$MLTK_QUIET" = "x1" ]; then
+ echo -e "\033[38;5;10m[info]\033[0m $TOOL_STR $@" >&6
+ else
+ echo -e "\033[38;5;10m[info]\033[0m $TOOL_STR $@"
+ fi
+}
+perror () {
+ if [ "x$MLTK_QUIET" = "x1" ]; then
+ echo -e "\033[38;5;9m[error]\033[0m $TOOL_STR $@" >&6
+ else
+ echo -e "\033[38;5;9m[error]\033[0m $TOOL_STR $@"
+ fi
+ qnd_exit
+}
+pwarning () {
+ if [ "x$MLTK_QUIET" = "x1" ]; then
+ echo -e "\033[38;5;11m[warning]\033[0m $TOOL_STR $@" >&6
+ else
+ echo -e "\033[38;5;11m[warning]\033[0m $TOOL_STR $@"
+ fi
+}
+
+pdebug () {
+ if [ "x$DEBUG" != "x1" ]; then
+ echo -e "[DEBUG] $TOOL_STR $@"
+ elif [ "x$MLTK_QUIET" = "x1" ]; then
+ echo -e "\033[38;5;6m[debug]\033[0m $TOOL_STR $@" >&6
+ else
+ echo -e "\033[38;5;6m[debug]\033[0m $TOOL_STR $@"
+ fi
+}
+
diff --git a/helper/useradd.inc b/helper/useradd.inc
index 7d1f5909..720a847d 100644
--- a/helper/useradd.inc
+++ b/helper/useradd.inc
@@ -9,15 +9,15 @@
# IDs will be generated in the range of 5-999 if not explicitly given
# TODO: Make it possible to pass a range of IDs if you don't want one <1000 but don't care about the exact ID
-. string.inc
+. "${ROOT_DIR}/helper/string.inc"
NAME_REGEX='^[a-z][-a-z0-9]*$'
# Generate a UID for a given USERNAME. Return existing UID if possible, generate new one otherwise
generate_uid()
{
- [ $# -ne 1 ] && echo "generate_uid fail. want 1 argument." && exit 1 >&2
- [ -z "${_PASSWD}" ] && echo "passwd file not set." && exit 1 >&2
+ [ $# -ne 1 ] && perror "generate_uid fail. want 1 argument."
+ [ -z "${_PASSWD}" ] && perror "passwd file not set."
local _UID=$(grep -E "^$1:[^:]*:[0-9]+:" "${_PASSWD}" | head -1 | awk -F ':' '{print $3}')
if [ "x${_UID}" = "x" ]
then
@@ -29,7 +29,7 @@ generate_uid()
local _TEST=$(grep -E "^[^:]+:[^:]*:${_UID}:" "${_PASSWD}")
[ "x${_TEST}" = "x" ] && break
done
- [ ${_TRIES} -ge 50 ] && echo "Generating a UID failed." && exit 1 >&2
+ [ ${_TRIES} -ge 50 ] && perror "Generating a UID failed."
fi
echo ${_UID}
}
@@ -37,8 +37,8 @@ generate_uid()
# Generate a UID for a given USERNAME. Return existing UID if possible, generate new one otherwise
generate_gid()
{
- [ $# -ne 2 ] && echo "generate_gid fail. want 2 arguments." && exit 1 >&2
- [ -z "${_GROUP}" ] && echo "group file not set." && exit 1 >&2
+ [ $# -ne 2 ] && perror "generate_gid fail. want 2 arguments."
+ [ -z "${_GROUP}" ] && perror "group file not set."
local _GID=$(grep -E "^$1:[^:]*:[0-9]+:" "${_GROUP}" | head -1 | awk -F ':' '{print $3}')
if [ "x${_GID}" = "x" ]
then
@@ -52,68 +52,73 @@ generate_gid()
[ "x${_TEST}" = "x" ] && break
_GID=$[ 5 + $RANDOM % 900 ] # using uid as gid not possible, generate new one
done
- [ ${_TRIES} -ge 50 ] && echo "Generating a GID failed." && exit 1 >&2
+ [ ${_TRIES} -ge 50 ] && perror "Generating a GID failed."
fi
echo ${_GID}
}
add_user() {
- [ -z "${INIT_DIR}" ] && echo "add_user: INIT_DIR not set" && exit 1
+ [ -z "${INIT_DIR}" ] && perror "add_user: INIT_DIR not set"
if [ -z $USER -a $# -eq 0 ]
then
- echo " ** add_user usage **"
- echo "add_user <username>"
- echo "OR"
- echo "USER=<username> [GROUP=<groupname>] [USERID=<userid>] [GROUPID=<groupid>] [USERHOME=<homedir>] [USERSHELL=<shell>] [PASSWORD=<pass>] add_user"
- exit 1
+ pwarning " ** add_user usage **"
+ pwarning "add_user <username>"
+ pwarning "OR"
+ pwarning "USER=<username> [GROUP=<groupname>] [USERID=<userid>] [GROUPID=<groupid>] [USERHOME=<homedir>] [USERSHELL=<shell>] [PASSWORD=<pass>] add_user"
+ perror "Aborting, please fix your script."
fi
local _PASSWD=${INIT_DIR}/etc/passwd
local _GROUP=${INIT_DIR}/etc/group
local _SHADOW=${INIT_DIR}/etc/shadow
- [ "x$USER" = "x" ] && local USER=$1
- USER=$(trim "$USER")
- if ! [[ $USER =~ $NAME_REGEX ]]
+ if [ "x$1" != "x" ]
then
- echo "Invalid username: $USER" >&2
- exit 1
+ local USER=$1
+ local GROUP=$1
+ local USERID=""
+ local GROUPID=""
+ local USERHOME=""
+ local USERSHELL=""
+ local PASSWORD=""
+ fi
+ USER=$(trim "$USER")
+ if ! [[ $USER =~ $NAME_REGEX ]]; then
+ perror "Invalid username: $USER"
fi
[ "x$GROUP" = "x" ] && local GROUP=$USER
GROUP=$(trim "$GROUP")
- if ! [[ $GROUP =~ $NAME_REGEX ]]
- then
- echo "Invalid group: $GROUP" >&2
- exit 1
+ if ! [[ $GROUP =~ $NAME_REGEX ]]; then
+ perror "Invalid group: $GROUP"
fi
[ "x$USERID" = "x" ] && local USERID=$(generate_uid ${USER})
USERID=$(trim "$USERID")
- [ "$USERID" -lt "1" -o "$USERID" -gt "65535" ] && echo "Invalid userid: $USERID" && exit 1 >&2
+ [ "$USERID" -lt "1" -o "$USERID" -gt "65535" ] && perror "Invalid userid: $USERID"
[ "x$GROUPID" = "x" ] && local GROUPID=$(generate_gid ${GROUP} ${USERID})
GROUPID=$(trim "$GROUPID")
- [ "$GROUPID" -lt "1" -o "$GROUPID" -gt "65535" ] && echo "Invalid groupid: $GROUPID" && exit 1 >&2
+ [ "$GROUPID" -lt "1" -o "$GROUPID" -gt "65535" ] && perror "Invalid groupid: $GROUPID"
# all required variables have been set
# does the desired username already exist? if so, check if UID matches, otherwise bail out
local _UID=$(grep -E "^${USER}:[^:]*:[0-9]+:" "${_PASSWD}" | head -1 | awk -F ':' '{print $3}')
- [ ! -z "${_UID}" ] && [ "x${_UID}" != "x${USERID}" ] && echo "User ${USER}(${USERID}) already exists with UID ${_UID}" && exit 1 >&2
+ [ ! -z "${_UID}" ] && [ "x${_UID}" != "x${USERID}" ] && perror "User ${USER}(${USERID}) already exists with UID ${_UID}"
# do the same for the group
local _GID=$(grep -E "^${GROUP}:[^:]*:[0-9]+:" "${_GROUP}" | head -1 | awk -F ':' '{print $3}')
- [ ! -z "${_GID}" ] && [ "x${_GID}" != "x${GROUPID}" ] && echo "Group ${GROUP}(${GROUPID}) already exists with GID ${_GID}" && exit 1 >&2
+ [ ! -z "${_GID}" ] && [ "x${_GID}" != "x${GROUPID}" ] && perror "Group ${GROUP}(${GROUPID}) already exists with GID ${_GID}"
# if user already exists, check if he is in another group than the one requested. if so, bail out
# (TODO: don't bail out and add user to the new group)
if [ ! -z "${_UID}" ]
then
- local _EXGID=$(grep -E "^${USER}:[^:]*:[0-9]+:" "${_PASSWD}" | head -1 | awk -F ':' '{print $3}')
- [ "x${GROUPID}" != "x${_EXGID}" ] && echo "Requested GID $GROUPID differs from existing GID $_EXGID" && exit 1 >&2
+ local _EXGID=$(grep -E "^${USER}:[^:]*:[0-9]+:" "${_PASSWD}" | head -1 | awk -F ':' '{print $4}')
+ [ "x${GROUPID}" != "x${_EXGID}" ] && perror "Requested GID $GROUPID differs from existing GID $_EXGID"
fi
# if user does not exist, try to add it
if [ -z "${_UID}" ]
then
local _TEST=$(grep -E "^[^:]+:[^:]*:${USERID}:" "${_PASSWD}")
- [ ! -z "${_TEST}" ] && echo "Cannot add $USER - desired UID $USERID already in use." && exit 1 >&2
+ [ ! -z "${_TEST}" ] && perror "Cannot add $USER - desired UID $USERID already in use."
fi
if [ -z "${_GID}" ]
then
local _TEST=$(grep -E "^[^:]+:[^:]*:${GROUPID}:" "${_GROUP}")
- [ ! -z "${_TEST}" ] && echo "Cannot add $GROUP - desired GID $GROUPID already in use." && exit 1 >&2
+ [ ! -z "${_TEST}" ] && perror "Cannot add $GROUP - desired GID $GROUPID already in use."
fi
[ -z "${USERHOME}" ] && local USERHOME=/nonexistent
[ -z "${USERSHELL}" ] && local USERSHELL=/bin/false
@@ -125,12 +130,13 @@ add_user() {
else
PASSWORD=$(sha1pass "${PASSWORD}")
[ -z "${PASSWORD}" ] && PASSWORD=$(openssl passwd -1 "${PASSWORD}")
- [ -z "${PASSWORD}" ] && "Error generating hashed password for $USER" && exit 1 >&2
+ [ -z "${PASSWORD}" ] && perror "Error generating hashed password for $USER"
fi
echo "${USER}:x:${USERID}:${GROUPID}:${USER}:${USERHOME}:${USERSHELL}" >> "${_PASSWD}"
echo "${USER}:${PASSWORD}:15555:0:99999:7:::" >> "${_SHADOW}"
fi
[ -z "${_GID}" ] && echo "${GROUP}:x:${GROUPID}:" >> "${_GROUP}"
echo "${USERID}"
+ exit 0
}