From 3888123dcd40872c4750708bee21f1c123ff7d41 Mon Sep 17 00:00:00 2001 From: David Coppit Date: Fri, 26 Jun 2015 00:19:25 -0400 Subject: [PATCH] Generate the user and group names Also check if the the user already exists. --- monitor.sh | 7 ++----- runas.sh | 41 ++++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/monitor.sh b/monitor.sh index 48cffa5..82fa08f 100755 --- a/monitor.sh +++ b/monitor.sh @@ -219,11 +219,8 @@ do # Wait until it's okay to run the command again, monstering up events as we do so wait_for_minimum_period $last_run_time - # Generate a user from the MD5 of the config file - USER=$(md5sum $CONFIG_FILE | awk '{print $1}') - - echo "$(ts) Running command" - /files/runas.sh $USER:$USER_ID:$GROUP_ID $COMMAND & + echo "$(ts) Running command with user ID $USER_ID and group ID $GROUP_ID" + /files/runas.sh $USER_ID $GROUP_ID $COMMAND & PID=$! last_run_time=$(date +"%s") diff --git a/runas.sh b/runas.sh index 70a1bae..d7056f5 100755 --- a/runas.sh +++ b/runas.sh @@ -1,7 +1,5 @@ #!/bin/bash -GROUP=docker - #----------------------------------------------------------------------------------------------------------------------- function ts { @@ -11,28 +9,37 @@ function ts { #----------------------------------------------------------------------------------------------------------------------- function process_args { - local USER_UID_GID=$1 + # These are intended to be global + USER_ID=$1 + GROUP_ID=$2 - if [[ ! "$USER_UID_GID" =~ ^[A-Za-z0-9._][-A-Za-z0-9._]*:[0-9]{1,}:[0-9]{1,}$ ]] + if [[ ! "$USER_ID" =~ ^[0-9]{1,}$ ]] then - echo "USER_UID_GID value $USER_UID_GID is not valid. It should be of the form ::" + echo "User ID value $USER_ID is not valid. It must be a whole number" exit 1 fi - # These are meant to be global. - USER=${USER_UID_GID%:*:*} - USER_ID=${USER_UID_GID#*:} - USER_ID=${USER_ID%:*} - GROUP_ID=${USER_UID_GID#*:*:} + if [[ ! "$GROUP_ID" =~ ^[0-9]{1,}$ ]] + then + echo "Group ID value $GROUP_ID is not valid. It must be a whole number" + exit 1 + fi } #----------------------------------------------------------------------------------------------------------------------- function create_user { - local USER=$1 - local USER_ID=$2 - local GROUP=$3 - local GROUP_ID=$4 + local USER_ID=$1 + local GROUP_ID=$2 + + USER="user_${USER_ID}_$GROUP_ID" + GROUP="group_${USER_ID}_$GROUP_ID" + + if id -u $USER >/dev/null 2>&1 + then + echo "$(ts) User \"$USER\" already exists. Skipping creation of user and group..." + return + fi echo "$(ts) Creating user \"$USER\" (ID $USER_ID) and group \"$GROUP\" (ID $GROUP_ID) to run the command..." @@ -48,10 +55,10 @@ function create_user { process_args "$@" -# Shift off the arg so that we can exec $@ below -shift +# Shift off the args so that we can exec $@ below +shift; shift -create_user $USER $USER_ID $GROUP $GROUP_ID +create_user $USER_ID $GROUP_ID echo "$(ts) Running command as user \"$USER\"..." exec /sbin/setuser $USER "$@"