![]() |
Listing 6
#! /bin/bash - # FILENAME: chtpwd # DESCRIPTION: Creates httpd password for relevant usernames, optionally # updating actual htpasswd file. Data are stored in a file with the following # format: # # username:clear_password:crypted_password:mailaddr # # USAGE: chtpwd [-H] [-f file] [-h file] [-l lenght ] [-u] [-v] # OPTIONS: # -H - Prints help message # -f file - File usernames are stored in # -h file - Name of htpasswd file to change # -l lenght - Desired password lenght # -u - Updates actual htpasswd file # -v - Produces verbose output # # LABEN S.p.A. - 09-jan-1998 # # HISTORY: # 0.0 Luca Salvadori <[email protected]> 09-jan-1998 # - Functions and behaviour # # ########################## S U B R O U T I N E S ########################### # Initialization of global variables # Program Information AUTHOR="Luca Salvadori <[email protected]>" VERSION="0.0" DATE="09-jan-1998" # End of program information MKPWD=~/work/utils/mkpwd.pl # Default utility to create passwords DEF_FILE=~/work/utils/htpasswd # Default data file DEF_HTPASSWD=~httpd/etc/htpasswd # Default httpd authorization file DEF_PWDLEN=8 # Default httpd password lenght UPDATE=0 # Don't update htpasswd file UMASK=077 # Default mask to create files with MASK="600" # Default protection mask for data file # WARNING! It contains clear passwords! TEMP=/tmp # Temporary directory TMPFILE=$TEMP/`basename $0`.$$ # Scratch file VERBOSE=0 # Brief output is default # Options string OPTS=":-H -f: -h: -l: -u -v" # Default options DEFOPT="-f $DEF_FILE -h $DEF_HTPASSWD -l $DEF_PWDLEN -u" # End of global variables initialization ##################### E N D O F S U B R O U T I N E S #################### function helpmsg() { echo "" 1>&2 echo " `basename $0` Version $VERSION - $DATE" 1>&2 echo " Author: $AUTHOR" 1>&2 echo "" 1>&2 echo " USAGE: chtpwd [-H] [-f file] [-h file] [-l lenght] [-u] [-v]" 1>&2 echo " OPTIONS:" 1>&2 echo " -H - Prints help message" 1>&2 echo " -f file - File usernames are stored in" 1>&2 echo " -h file - Name of htpasswd file to change" 1>&2 echo " -l lenght - Desired password lenght" 1>&2 echo " -u - Updates actual htpasswd file" 1>&2 echo " -v - Produces verbose output" 1>&2 echo "" 1>&2 } ########################## M A I N P R O G R A M ########################## # Parsing input parameters and assigning default if needed options=`echo $*` [ "$options" = "" ] && { $0 $DEFOPT ; exit } # Parsing options and setting defaults if needed while [ $OPTIND -le $# ] do getopts "$OPTS" option case $option in H) helpmsg exit 0 ;; f) FILE=$OPTARG ;; h) HTPASSWD=$OPTARG ;; l) PWDLEN=$OPTARG ;; u) UPDATE=1 ;; v) VERBOSE=1 ;; "?") echo "`basename $0` - ERROR: Option -$OPTARG requires an argument or is unknown." 1>&2 echo "Run `basename $0` -H for help." 1>&2 exit 3 ;; *) echo "`basename $0` - ERROR: Unknown option -$OPTARG." 1>&2 echo "Run `basename $0` -H for help." 1>&2 exit 2 ;; esac case $OPTARG in -*) echo "`basename $0` - ERROR: Option -$option requires an argument." 1>&2 echo "Invalid argument $OPTARG." 1>&2 echo "Run `basename $0` -H for help." 1>&2 exit 3 ;; esac done # Setting defaults for unselected options HTPASSWD=${HTPASSWD:-$DEF_HTPASSWD} FILE=${FILE:-$DEF_FILE} PWDLEN=${PWDLEN:-$DEF_PWDLEN} # Perform sanity checks, just in case [ ! -f $FILE ] && { echo "`basename $0` - ERROR: Data file $FILE does not exist." 1>&2 ; echo "Run `basename $0` -H for help." 1>&2 ; exit 6 } [ ! -f $MKPWD ] && { echo "`basename $0` - ERROR: Program file $MKPWD does not exist." 1>&2 ; echo "Run `basename $0` -H for help." 1>&2 ; exit 7 } # Here begins the real stuff # Setting protection to data file, just in case... [ -f $FILE ] && chmod $MASK $FILE # Creating scratch file with proper protections touch $TMPFILE chmod $MASK $TMPFILE # Looping for all usernames # Displaying startup message in verbose mode if [ $VERBOSE -eq 1 ] then echo "`basename $0` - Verbose output -------------------------------------------------------------------------- Invoked with following defaults: - Desired password lenght: $PWDLEN - Data file: $FILE - HTTPD access file: $HTPASSWD" if [ $UPDATE -eq 1 ] then echo "- Update HTTPD access file $HTPASSWD" else echo "- Don't update HTTPD access file $HTPASSWD" fi echo "-------------------------------------------------------------" fi for line in `cat $FILE` do # Getting parameters user=`echo $line | cut -d":" -f1` mailaddr=`echo $line | cut -d":" -f4` pwdstring=`$MKPWD $PWDLEN` # Verbose output [ $VERBOSE -eq 1 ] && echo -n "Processing $user ($mailaddr)..." # Appending new line to scratch file echo $user:$pwdstring:$mailaddr >> $TMPFILE # Verbose output [ $VERBOSE -eq 1 ] && echo "Done." done # Move scratch file to final destination [ -f $TMPFILE ] && mv $TMPFILE $FILE # Setting protection to data file, just in case... [ -f $FILE ] && chmod $MASK $FILE # If update option is selected, do i if [ $UPDATE -eq 1 ] then [ $VERBOSE -eq 1 ] && echo -n "Updating $HTPASSWD file..." for line in `cat $FILE | cut -d":" -f1,3` do user=`echo $line | cut -d":" -f1` ed -s $HTPASSWD << END 1,\$g;^$user:;s;^.*;$line; w q END [ $VERBOSE -eq 1 ] && echo -n "." done [ $VERBOSE -eq 1 ] && echo "Done." fi # Cleanup and exit [ -f $TMPFILE ] && rm $TMPFILE exit ################### E N D O F M A I N P R O G R A M ################### | ||||||||||
© 2003 CMP Media Inc. All Rights Reserved. |