/
usr
/
local
/
lp
/
apps
/
malre
/
source_includes
/
File Upload :
llllll
Current File: //usr/local/lp/apps/malre/source_includes/basic.shf
### More basic functions that are safe to be sourced function fn_mktemp { local v_FILE if [[ -n "$d_WORKING" ]]; then if [[ ! -d "$d_WORKING"/tmp ]]; then mkdir "$d_WORKING"/tmp fi v_FILE="$d_WORKING/tmp/$( fn_make_ident 6 "$d_WORKING"/tmp )" touch "$v_FILE" else v_FILE="$( mktemp )" fi echo "$v_FILE" } function fn_make_ident { ### Given a number of characters, a directory, and a file name prefix, returns a file name that doesn't exist yet local v_NUM="$1" local v_DIR="$2" local v_PRE="$3" local v_IDENT="$v_PRE""$( /bin/cat /dev/urandom | tr -dc 'A-Za-z0-9' | fold -w $v_NUM | head -n 1 )" if [[ -n "$v_DIR" && -d "$v_DIR" && -e "$v_DIR"/"$v_IDENT" ]]; then v_IDENT="$( fn_make_ident "$v_NUM" "$v_DIR" "$v_PRE" )" fi echo "$v_IDENT" } function fn_sanitize { ### Given a variable that we're going to run through an egrep, escape all special characters v_OUTPUT="${1//\\/\\\\}" v_OUTPUT="${v_OUTPUT//\*/\\*}" v_OUTPUT="${v_OUTPUT//\./\\.}" v_OUTPUT="${v_OUTPUT//[/\\[}" v_OUTPUT="${v_OUTPUT//|/\\|}" v_OUTPUT="${v_OUTPUT//\?/\\?}" v_OUTPUT="${v_OUTPUT//\(/\\(}" v_OUTPUT="${v_OUTPUT//)/\\)}" v_OUTPUT="${v_OUTPUT//$/\\$}" v_OUTPUT="${v_OUTPUT//+/\\+}" v_OUTPUT="${v_OUTPUT//^/\\^}" v_OUTPUT="${v_OUTPUT//{/\\{}" echo -n "$v_OUTPUT" } function fn_file_path { ### Given a file (whether or not with full path), populate the variables "$s_FILE" and "$s_DIR" with the file and the directory ### The problem with realpath and readlink is that if the file is a symlink, then it will give us the path to the file that it links to. We don't want that. ### This gives us the real path to the directory, but keeps the filename local v_FILE="$1" ### If the file name ends in a slash, remove that slash if [[ "${v_FILE: -1}" == "/" ]]; then v_FILE="${v_FILE:0:${#v_FILE}-1}" fi ### Separate out the file name from the path s_FILE="${v_FILE##*/}" ### Find the length of the path local v_DIR_LEN=$(( ${#v_FILE} - ${#s_FILE} - 1 )) if [[ "$v_DIR_LEN" -le 0 ]]; then ### If no path was given, let's find it s_DIR="$( pwd )" else ### If a path was given, we'll find out what it really is s_DIR="${v_FILE:0:$v_DIR_LEN}" fi ### find the real path local s_DIR2="$( readlink -f "$s_DIR" 2> /dev/null )" if [[ -n "$s_DIR2" ]]; then s_DIR="$s_DIR2" fi } function fn_is_file_in_job { ### Given a file, output if it's within the purview of the job ### "$1" is the file ### Returns the full path to the file if the file is part of the job (or would be in a directory that is part fo the job); returns nothing if it is not ### If "$2" is "e", return "0" even if the file does not exist, regardless of whether it's part of the job ##### This does not appear to have been implimented local f_FILE="$1" fn_file_path "$f_FILE" if [[ ! -d "$s_DIR" ]]; then return fi f_FILE="$s_DIR""/""$s_FILE" unset s_DIR s_FILE local b_IS=false if [[ -n "$v_JIDENT" ]]; then local v_DIRS="$( cat "$d_JOB"/directories 2> /dev/null | wc -l )" local c for (( c=1; c<=$v_DIRS; c++ )); do local v_DIR="$( sed -n "${c}p" "$d_JOB"/directories | rev | cut -d ":" -f2- | rev )" if [[ -d "$v_DIR" ]]; then if [[ $( echo "$f_FILE" | egrep -c "^$( fn_sanitize "$v_DIR" )" ) -gt 0 ]]; then b_IS=true break fi fi done fi if [[ "$b_IS" == true ]]; then echo "$f_FILE" fi } function fn_rm { ### Test to ensure that a file is under the purview of the script or job before removing it local b_RM local b_DIREC=false while [[ -n "$1" ]]; do b_RM=false local f_FILE="$1" shift if [[ "$f_FILE" == "-d" ]]; then ### We're expecting to delete directories b_DIREC=true continue fi ### Make sure that we're referencing the file by its full path local d_FILE="$( cd -P "$( dirname "$f_FILE" )" && pwd )" if [[ -z "$d_FILE" || "${d_FILE:0:1}" != "/" ]]; then continue fi local v_FILE="$( basename "$f_FILE" )" if [[ -z "$v_FILE" ]]; then continue fi f_FILE="$d_FILE/$v_FILE" ### Make sure that the file is within the malre program directory, or within one of the directories for the job if [[ -n $d_PROGRAM && $( echo "$f_FILE" | egrep -c "^$( fn_sanitize "$d_PROGRAM" )" ) -gt 0 ]]; then b_RM=true elif [[ -n "$( fn_is_file_in_job "$f_FILE" )" ]]; then b_RM=true fi ### Remove the file if [[ "$b_RM" == true ]]; then if [[ -L "$f_FILE" ]]; then unlink -- "$f_FILE" elif [[ -f "$f_FILE" ]]; then rm -f --preserve-root -- "$f_FILE" elif [[ -d "$f_FILE" ]]; then if [[ "$b_DIREC" == true ]]; then rm -rf --preserve-root -- "$f_FILE" else echo "Asked to remove '$f_FILE', but it is a directory" fi fi elif [[ -e "$f_FILE" ]]; then echo "Will not remove '$f_FILE'" fi done } function fn_find_homedir { ### Given a directory, determine whose home directory it's in (if any) local v_PWD="$1" if [[ -z "$v_PWD" || "$v_PWD" == "/" ]]; then return fi if [[ $( cat /etc/passwd | egrep -c "^[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:$v_PWD/?:" ) -gt 0 ]]; then echo "$( cat /etc/passwd | egrep "^[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:$v_PWD/?:" | cut -d ":" -f1 | head -n1 )" return fi v_PWD="$( echo "$v_PWD" | rev | cut -d "/" -f2- | rev )" fn_find_homedir "$v_PWD" } function fn_account { ### Determine what account we're referencing ### Are we within someones home directory v_ACCOUNT="$( fn_find_homedir "$( pwd )" )" if [[ -n "$v_ACCOUNT" && "$v_ACCOUNT" != "root" ]]; then local v_YN read -ep "Account $v_ACCOUNT? (Y/n): " v_YN v_YN="${v_YN:0:1}" if [[ "$v_YN" == "n" || "$v_YN" == "N" ]]; then v_ACCOUNT= fi fi ### If we don't know the account name yet, ask for it if [[ -z "$v_ACCOUNT" || "$v_ACCOUNT" == "root" ]]; then read -ep "What is the username for the account: " v_ACCOUNT if [[ -z "$v_ACCOUNT" || "$v_ACCOUNT" == "root" ]]; then echo "Must provide an account name" exit 1 fi fi ### Make sure that the account given is present in /etc/passwd if [[ $( egrep -c "^$( fn_sanitize "$v_ACCOUNT" ):" /etc/passwd ) -lt 1 ]]; then echo "Account '$v_ACCOUNT' does not appear to be present in /etc/passwd" exit 1 fi v_HOMEDIR="$( egrep "^$( fn_sanitize "$v_ACCOUNT" ):" /etc/passwd | cut -d ":" -f6 )" }
Copyright ©2k19 -
Hexid
|
Tex7ure