summaryrefslogtreecommitdiffstats
path: root/helper
diff options
context:
space:
mode:
authorSimon Rettberg2013-02-22 15:20:10 +0100
committerSimon Rettberg2013-02-22 15:20:10 +0100
commit73289c75b5858fa89e152957abdf641870f25aa8 (patch)
treebd8bb88a717622847a3932ec56b4ff7e456aa8a5 /helper
parent 1. New logging/output system - use functions provided by helper/logging.inc (diff)
downloadtm-scripts-73289c75b5858fa89e152957abdf641870f25aa8.tar.gz
tm-scripts-73289c75b5858fa89e152957abdf641870f25aa8.tar.xz
tm-scripts-73289c75b5858fa89e152957abdf641870f25aa8.zip
Added helper/fileutil.inc that provides tarcopy, an error-checking version of "tar cp xx | tar xp -C xx"
Added add_group function Changed add_user to update an existing user's password if called with a password Updated base.build to use new logging system
Diffstat (limited to 'helper')
-rw-r--r--helper/fileutil.inc17
-rw-r--r--helper/useradd.inc66
2 files changed, 66 insertions, 17 deletions
diff --git a/helper/fileutil.inc b/helper/fileutil.inc
new file mode 100644
index 00000000..40bc42d5
--- /dev/null
+++ b/helper/fileutil.inc
@@ -0,0 +1,17 @@
+# copy list of files using tar
+tarcopy () {
+ [ $# -ne 2 ] && perror "Sanity check failed: tarcopy needs exactly two params, but $# were given."
+ if [ -z $1 ]; then
+ pwarning "tarcopy called with empty input list (dest was '$2')"
+ return
+ fi
+ local SHORT=$1
+ [ ${#SHORT} -gt 18 ] && SHORT=$(echo "$SHORT" | cut -c-13)...$(echo "$SHORT" | cut -c${#SHORT}-)
+ [ -z $2 ] && perror "tarcopy called with empty destination."
+ tar -cp "$1" | tar -xp -C "$2"
+ local PS1=$PIPESTATUS[0]
+ local PS2=$PIPESTATUS[1]
+ [ $PS1 -ne 0 ] && perror "packing-part of tar-copy from '$SHORT' to '$2' failed."
+ [ $PS2 -ne 0 ] && perror "unpacking-part of tar-copy from '$SHORT' to '$2' failed."
+}
+
diff --git a/helper/useradd.inc b/helper/useradd.inc
index 720a847d..f8675dd5 100644
--- a/helper/useradd.inc
+++ b/helper/useradd.inc
@@ -34,7 +34,7 @@ generate_uid()
echo ${_UID}
}
-# Generate a UID for a given USERNAME. Return existing UID if possible, generate new one otherwise
+# Generate a GID for a given GROUPNAME. Return existing GID if possible, generate new one otherwise
generate_gid()
{
[ $# -ne 2 ] && perror "generate_gid fail. want 2 arguments."
@@ -89,12 +89,12 @@ add_user() {
if ! [[ $GROUP =~ $NAME_REGEX ]]; then
perror "Invalid group: $GROUP"
fi
- [ "x$USERID" = "x" ] && local USERID=$(generate_uid ${USER})
+ [ "x$USERID" = "x" ] && local USERID=$(generate_uid "${USER}")
USERID=$(trim "$USERID")
- [ "$USERID" -lt "1" -o "$USERID" -gt "65535" ] && perror "Invalid userid: $USERID"
- [ "x$GROUPID" = "x" ] && local GROUPID=$(generate_gid ${GROUP} ${USERID})
+ [ "$USERID" -lt "0" -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" ] && perror "Invalid groupid: $GROUPID"
+ [ "$GROUPID" -lt "0" -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}')
@@ -122,21 +122,53 @@ add_user() {
fi
[ -z "${USERHOME}" ] && local USERHOME=/nonexistent
[ -z "${USERSHELL}" ] && local USERSHELL=/bin/false
- if [ -z "${_UID}" ]
+ # create password
+ if [ -z "${PASSWORD}" ]
then
- if [ -z "${PASSWORD}" ]
- then
- local PASSWORD='*'
- else
- PASSWORD=$(sha1pass "${PASSWORD}")
- [ -z "${PASSWORD}" ] && PASSWORD=$(openssl passwd -1 "${PASSWORD}")
- [ -z "${PASSWORD}" ] && perror "Error generating hashed password for $USER"
- fi
+ local PASSWORD='*'
+ else
+ PASSWORD=$(sha1pass "${PASSWORD}")
+ [ -z "${PASSWORD}" ] && PASSWORD=$(openssl passwd -1 "${PASSWORD}")
+ [ -z "${PASSWORD}" ] && perror "Error generating hashed password for $USER"
+ fi
+ # add user, or replace password
+ if [ -z "${_UID}" ]; then
+ # create user
echo "${USER}:x:${USERID}:${GROUPID}:${USER}:${USERHOME}:${USERSHELL}" >> "${_PASSWD}"
echo "${USER}:${PASSWORD}:15555:0:99999:7:::" >> "${_SHADOW}"
+ pinfo "Created user $USER"
+ elif [ "$PASSWORD" != "*" ]; then
+ # update user's password
+ sed -i -r "s/^${USER}:[^:]*:(.*)\$/${USER}:${PASSWORD}:\1/g" "${_SHADOW}"
+ pinfo "Updated password of $USER"
+ fi
+ [ -z "${_GID}" ] && pinfo "Created group $GROUP" && echo "${GROUP}:x:${GROUPID}:" >> "${_GROUP}"
+}
+
+add_group () {
+ [ $# -lt 1 ] && perror "add_group called without argument."
+ [ -z "${INIT_DIR}" ] && perror "add_group: INIT_DIR not set"
+ local _PASSWD=${INIT_DIR}/etc/passwd
+ local _GROUP=${INIT_DIR}/etc/group
+ local _SHADOW=${INIT_DIR}/etc/shadow
+ local GROUP=$1
+ local GROUPID="-"
+ if ! [[ $GROUP =~ $NAME_REGEX ]]; then
+ perror "Invalid group: $GROUP"
+ fi
+ [ $# -ge 2 ] && [ ! -z "$2" ] && GROUPID=$2
+ local _GID=$(grep -E "^${GROUP}:[^:]*:[0-9]+:" "${_GROUP}" | head -1 | awk -F ':' '{print $3}')
+ [ "x${_GID}" != "x" ] && [ "x$GROUPID" = "x-" -o "x$GROUPID" = "x${_GID}" ] && return # nothing to do, already exists
+ [ "x${_GID}" != "x" ] && perror "Group $GROUP already exists with GID ${_GID}, but creation was requested with GID $GROUPID"
+ if [ "x$GROUPID" = "x-" ]; then
+ local _UID=$(grep -E "^${GROUP}:[^:]*:[0-9]+:" "${_PASSWD}" | head -1 | awk -F ':' '{print $3}')
+ [ -z "${_UID}" ] && _UID=100
+ GROUPID=$(generate_gid "$GROUP" "${_UID}")
+ fi
+ if ! [[ $GROUPID =~ [0-9]+ ]]; then
+ perror "add_group: GROUPID not numeric (is '$GROUPID')"
fi
- [ -z "${_GID}" ] && echo "${GROUP}:x:${GROUPID}:" >> "${_GROUP}"
- echo "${USERID}"
- exit 0
+ echo "${GROUP}:x:${GROUPID}:" >> "${_GROUP}"
+ pinfo "Created group $GROUP"
}