From 73289c75b5858fa89e152957abdf641870f25aa8 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 22 Feb 2013 15:20:10 +0100 Subject: 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 --- helper/fileutil.inc | 17 ++++++++++++ helper/useradd.inc | 66 ++++++++++++++++++++++++++++++++------------ mltk | 1 + remote/tools/base/base.build | 63 +++++++++++++++++++++++------------------- 4 files changed, 102 insertions(+), 45 deletions(-) create mode 100644 helper/fileutil.inc 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" } diff --git a/mltk b/mltk index 85675e93..6f775cd5 100755 --- a/mltk +++ b/mltk @@ -29,6 +29,7 @@ qnd_exit() { . "${ROOT_DIR}/helper/logging.inc" . "${ROOT_DIR}/helper/useradd.inc" . "${ROOT_DIR}/helper/downloader.inc" +. "${ROOT_DIR}/helper/fileutil.inc" banner () { echo -e "\033[38;5;202m\t __ __ __ " diff --git a/remote/tools/base/base.build b/remote/tools/base/base.build index e580e359..fa58585d 100644 --- a/remote/tools/base/base.build +++ b/remote/tools/base/base.build @@ -9,7 +9,7 @@ fetch_source() { build() { BUILDDIR=${TOOL_DIR}/${TOOL}/build - [ ! -d ${BUILDDIR} ] && mkdir -p ${BUILDDIR} + mkdir -p ${BUILDDIR} FILELIST="list_binaries_and_files" [ -e ${FILELIST} ] && rm ${FILELIST} @@ -19,42 +19,47 @@ build() { BIN_LOCATION=$(which ${BIN}) if [ ! -z ${BIN_LOCATION} -a -e ${BIN_LOCATION} ]; then - echo "[$TOOL] Processing $BIN at $BIN_LOCATION ..." - echo ${BIN_LOCATION} >> ${FILELIST} - [ -L ${BIN_LOCATION} ] \ - && echo "[$TOOL] ${BIN_LOCATION} is a symbolic link, copying $(readlink -f ${BIN_LOCATION})" \ - && echo $(readlink -f ${BIN_LOCATION}) >> ${FILELIST} + pdebug "Processing $BIN at $BIN_LOCATION ..." + echo ${BIN_LOCATION} >> "${FILELIST}" + [ -L "${BIN_LOCATION}" ] \ + && pdebug "${BIN_LOCATION} is a symbolic link, copying $(readlink -f "${BIN_LOCATION}")" \ + && echo $(readlink -f "${BIN_LOCATION}") >> "${FILELIST}" else - echo "[$TOOL] ${BIN} not found on the system! Please install it." - exit 1 + perror "${BIN} not found on the system! Please install it." fi done for LIB in ${REQUIRED_LIBRARIES} do - for LIB_LOCATION in $(locate ${LIB}|grep ^/lib/) + for LIB_LOCATION in $(locate ${LIB} | grep ^/lib/) do - echo ${LIB_LOCATION} >> ${FILELIST} + echo ${LIB_LOCATION} >> "${FILELIST}" done done for FILE in ${REQUIRED_DIRECTORIES} do - [ ! -f ${FILE} ] && echo ${FILE} >> ${FILELIST} + [ ! -d ${FILE} ] && perror "Missing required directory $FILE" + echo ${FILE} >> "${FILELIST}" done for FILE in ${REQUIRED_FILES} do - [ ! -d ${FILE} ] && echo ${FILE} >> ${FILELIST} + [ ! -f ${FILE} ] && perror "Missing required file $FILE" + echo ${FILE} >> "${FILELIST}" done - echo "[$TOOL] File list generated as ${BUILDDIR}/${FILELIST}." - echo "--------------------------------------------------------------------" - (tar -cpv $(cat ${FILELIST}) | tar -xpv -C ${BUILDDIR}) &>/dev/null + local NUMFILES=$(cat "${FILELIST}" | wc -l) + if [ "x$NUMFILES" != "x" -a "x$NUMFILES" != "x0" ]; then + pinfo "File list generated at ${BUILDDIR}/${FILELIST} ($NUMFILES entries)" + tar -cp $(cat "${FILELIST}") | tar -xp -C "${BUILDDIR}" + local RET=$? + [ $RET -ne 0 ] && perror "tar-copy from '$FILELIST' to '$BUILDDIR' failed." + fi } clean_users_and_groups() { # Pass the root directory of the "system" you want to clean, eg '/' for your real machine (probably a bad idea) - [ $# -ne 1 ] && echo "Missing param to clean_users_and_groups" && exit 1 + [ $# -ne 1 ] && perror "Missing param to clean_users_and_groups" local SYSBASE=$1 local PASSWD=${SYSBASE}/etc/passwd local GROUP=${SYSBASE}/etc/group @@ -81,40 +86,41 @@ clean_users_and_groups() { post_copy() { # make basic directory structure - mkdir -p ${INIT_DIR}/{bin,dev,proc,lib,etc,mnt,sys,var/run,var/lock,var/log,run/lock,run/shm,openslx/mnt} + mkdir -p "${INIT_DIR}/{bin,dev,proc,lib,etc,mnt,sys,var/run,var/lock,var/log,run/lock,run/shm,openslx/mnt}" # copy devices from running system - cp -a /dev/{console,kmsg,mem,null,shm,tty,tty0,tty1,tty9,fb0,urandom,zero} \ - ${INIT_DIR}/dev + cp -a "/dev/{console,kmsg,mem,null,shm,tty,tty0,tty1,tty9,fb0,urandom,zero}" \ + "${INIT_DIR}/dev" # set /etc/environment to include /openslx/bin and /openslx/sbin echo "PATH=\"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/openslx/sbin:/openslx/bin\"" \ - > ${INIT_DIR}/etc/environment + > "${INIT_DIR}/etc/environment" # copy static files - cp -r ${TOOL_DIR}/${TOOL}/data/* ${INIT_DIR} + cp -r "${TOOL_DIR}/${TOOL}/data/*" "${INIT_DIR}" # better: dirname $(ldd $SHELL|grep libc | awk -F " " '{print $3}') # copy pam modules, TODO: better way to find arch-dependant interfix... - (tar -cpv $(dirname $(locate pam_unix.so|grep ^/lib/)) | tar -xpv -C ${INIT_DIR}) &>/dev/null + tarcopy "$(dirname $(locate pam_unix.so | grep ^/lib/))" "${INIT_DIR}" # quick fix for /etc/fstab - echo "# no configuration" >> ${INIT_DIR}/etc/fstab + echo "# no configuration" >> "${INIT_DIR}/etc/fstab" # link /etc/mtab, needed for systemd [ ! -e ${INIT_DIR}/etc/mtab ] && ln -s /proc/self/mounts ${INIT_DIR}/etc/mtab # quick fix for missing group in /etc/group - echo "lock:x:128:" >> ${INIT_DIR}/etc/group + add_group "lock" # clean passwd/group files (do BEFORE setting root pw) clean_users_and_groups "${INIT_DIR}" - # setup root account - sed -i "s/^root:.*/root:\$6\$QGcBjw6e\$sJvGXBfKuNd4Cx5\.aVxJT7PCq\/AWrP\/JLgvDHBGN164Rp2HRIENPQzieO\/2ctlQS7yRgWwaWTqJkZr7Uq37af1:15657:0:99999:7:::/g" ${INIT_DIR}/etc/shadow + # setup root accoun + USER=root PASSWORD="!r00t" add_user + [ ! -d ${INIT_DIR}/root ] && mkdir ${INIT_DIR}/root - echo "minibox" > ${INIT_DIR}/etc/hostname + echo "minilinux-$(hostname)" > "${INIT_DIR}/etc/hostname" # copy kernel modules #[ ! -d ${INIT_DIR}/lib/modules/$(uname -r) ] && mkdir -p ${INIT_DIR}/lib/modules/$(uname -r) @@ -122,5 +128,6 @@ post_copy() { #quick fix xterm symlink [ -e /usr/lib/libXaw7.so.7.0.0 ] && \ - ln -s /usr/lib/libXaw7.so.7.0.0 ${INIT_DIR}/usr/lib/libXaw7.so.7 + ln -s "/usr/lib/libXaw7.so.7.0.0" "${INIT_DIR}/usr/lib/libXaw7.so.7" } + -- cgit v1.2.3-55-g7522