One script and some questions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting One script and some questions
# 1  
Old 06-13-2013
One script and some questions

Hi, guys,
With some guys' help, I made a shellscript used to create new users(attached at the bottom).
I have some questions:
a). I need to prompt users to enter their password in my script, but when users type in their password, it's visible on screen. Is there any ways to hide the STDIN or make it invisible on screen?
b). I want the password contains at least one lowercase and one uppercase and one digit and one punctuation and the length must be between 8 and 16 characters long.
I validated it by using below code:
Code:
echo "${passwd}" | awk '/[a-z]/ && /[A-Z]/ && /[0-9]/ && /\W/ && (length($0) >=8 ) && (length($0) <= 16 ) { X=1 } END { exit(!X) }'

however, if the password is set to "3o1416!@#" and it doesn't apparently contain a uppercase, it can still pass the validation.
I don't know why and please give me a direction.
c). very important in my script adding a new user is designed as an interactive process, and I know it can be also designed as non-interactive.
Which is more commonly used?
What do you think of my script?
Thank you.

My script:
Code:
#!/bin/bash
# scriptname: create_new_user
# purpose: to create a new user

# functionname: loginname_validation
# purpose: to validate the loginname which is used to add a new user
function loginname_validation()
{
	local loginname="${1}"
	if grep -q "^${loginname}:" /etc/passwd && echo "The loginname already exists." 
	then
		return 1
	elif [ -z "${loginname}" ] && echo "You should enter a nonempty loginname." 
	then
		return 1
	elif echo "${loginname}" | grep -q '^[_a-Z][0-9a-Z_@.-]\{6,16\}[_a-Z0-9]$' && echo "You have entered a valid loginname." 
	then
		return 0 
	else
		echo "The loginname must start and end with a underscore or an alphabet. And its length must be between 8 and 16 characters long. Only alphabet or digit or 4 kinds of punctuation(@_-.) is allowable."
		return 1
	fi
}

# functionname: password_validation
# purpose: to validate the password
function password_validation()
{
	local passwd="${1}"
	if [ -z "${passwd}" ] && echo "The password must not be empty." 
	then
		return 1
	elif echo "${passwd}" | awk '/[a-z]/ && /[A-Z]/ && /[0-9]/ && /\W/ && (length($0) >=8 ) && (length($0) <= 16 ) { X=1 } END { exit(!X) }' && echo "You have entered a valid password."
	then
		return 0 
	else
		echo "The password must contain at least one lowercase and one uppercase and one digit and one punctuation, and its length must be between 8 and 16 characters long."
		return 1
	fi
}

# functionname: uid_validation
# purpose: to validate the uid
function uid_validation()
{
	local uid="${1}"
	if [ x"${uid}" = x`awk -F: '{print $3}' /etc/passwd | grep "^${uid}$"` ] && echo "The UID already exists."
	then
		return 1
	elif echo ${uid} | awk '/^[0-9][0-9]*[0-9]$/ && (($0) >= 500) && (($0) <= 32767) { X=1 } END { exit(!X) }' && echo " The UID is valid."
	then
		return 0
	else
		echo "You should enter a number between  500 and 32767 as UID."
		return 1
	fi
}

# functionname: grpname_validation
# purpose: to validate the groupname
function grpname_validation()
{
	local grpname="${1}"
	if grep -q "^${grpname}:" /etc/group && echo "The groupname already exists." 
	then
		return 1
	elif [ -z "${grpname}" ] && echo "You should enter a nonempty groupname." 
	then
		return 1
	elif echo "${grpname}" | grep -q '^[_a-Z][0-9a-Z_@.-]\{6,16\}[_a-Z]$' && echo "You have entered a valid groupname." 
	then
		return 0 
	else
		echo "The groupname must start and end with a underscore or an alphabet. And its length must be between 8 and 16 characters long. Only alphabet or digit or 4 kinds of punctuation(@_-.) is allowable."
		return 1
	fi
}

# functionname: fullname_validation
# purpose: to validate the fullname
function fullname_validation()
{
	local fullname="${1}"
	if [ -z "${fullname}" ] && echo "You should enter a nonempty fullname." 
	then
		return 1
	elif echo "${fullname}" | grep -q '^[a-Z][a-Z. -]\{4,16\}[a-Z]$' && echo "You have entered a valid groupname." 
	then
		return 0 
	else
		echo "The fullname must start and end with an alphabet. And its length must be between 8 and 16 characters long. Only alphabet or 2 kinds of punctuation(-.) is allowable."
		return 1
	fi
}

# functionname: homedir_validation
# purpose: to validate the homedir
function homedir_validation()
{
	local homedir="${1}"
	if [ -z "${homedir}" ] && echo "You should enter a nonempty home directory." 
	then
		return 1
	elif echo "${homedir}" | grep -E -q '^\/([0-9a-Z_.-]+\/?)+$' && echo "You have entered a valid home directory." 
	then
		return 0
	else
		echo "The absolute path of home directory shouldn't contain metacharacters."
		return 1
	fi
}

# functionname: add_newuser
# purpose: to add a new user
function add_new_user()
{
# enter a login name
read -p "Please enter a new loginname: " ln
until loginname_validation $ln
do
	read -p "Please enter a new loginname: " ln
done

# enter a password
while true
do
	read -p "Please enter your password: " pw
	until password_validation $pw
	do
		read -p "Please enter your password: " pw
	done
	read -p "Please confirm your password: " confirmpw
	if [ $confirmpw == $pw ]
	then
		break
	else
		echo "The passwords do not match.Please reenter."
		continue 1
	fi
done
encrypted_pw=`openssl passwd -crypt $confirmpw`

# enter a user id
read -p "Specify a UID Automatically? (Y/N)" isautomatic
case $isautomatic in
Y|y)
#	uidopt=""
;;
*)
	read -p "Please enter a UID: " uid
	until uid_validation $uid
	do
		read -p "Please enter a UID: " uid
	done
	uidopt="-u $uid"
;;
esac

# choose a group

read -p "Create a Group Automatically? (Y/N)" isautomatic
case $isautomatic in
Y|y)
	grpname="$ln"
#	grpopt=""
;;
*)
	read -p "Use an existing group? (Y/N)" issetgroup
	case $issetgroup in
	Y|y)
		read -p "Enter the groupname: " grpname
		until grep -q "^${grpname}:" /etc/group
			do
				echo "Specified group not found."
				read -p "Enter the groupname: " grpname
			done
	;;
	*)
		read -p "Enter the new groupname: " grpname
		until grpname_validation "$grpname"
		do
			read -p "Enter the new groupname: " grpname
		done
		groupadd $grpname
	;;
	esac
	grpopt="-g $grpname"
;;
esac

# enter a fullname

read -p "Use the loginname as the full name? (Y/N)" isautomatic
case $isautomatic in
Y|y)
	fullname="$ln"
;;
*)
	read -p "Please enter the FullName: " fullname
	until fullname_validation "$fullname"
	do
		read -p "Please enter the FullName: " fullname
	done
;;
esac
# enter the home directory

read -p "Create Home Directory Automatically? (Y/N)" isautomatic
case $isautomatic in
Y|y)
	homedir="/home/$ln"
	homediropt="-m"
;;
*)
	read -p "Use an existing directory as the Home Directory? (Y/N)" issethomedir
	case $issethomedir in
	Y|y)
		read -p "Enter the absolute path of the specified directory: " homedir
		until [ -d "$homedir" ]
			do
				echo "Specified directory not found."
				read -p "Enter the absolute path of the specified directory: " homedir
			done
	;;
	*)
		read -p "Enter the absolute path of the Home Directory you want: " homedir
		until homedir_validation "$homedir"
		do
			read -p "Enter the absolute path of the Home Directory you want: " homedir
		done
	;;
	esac
	homediropt="-m -d $homedir"
;;
esac
# enter the login shell

read -p "Choose the /bin/bash as login shell? (Y/N)" isautomatic
case $isautomatic in
Y|y)
	loginshell="/bin/bash"
#	lsopt=""
;;
*)
	read -p "Please enter the login shell: " loginshell
	until grep -q "$loginshell" /etc/shells
	do
		echo "Not invalid loginshell."
		read -p "Please enter the login shell: " loginshell
	done
	lsopt="-s $loginshell"
;;
esac

# add the new user
echo "The info of new user is shown below:"
printf "%-16s %-16s\n" "Login Name:" "$ln"
printf "%-16s %-16s\n" "Password:" "$confirmpw"
printf "%-16s %-16s\n" "Group Name:" "$grpname"
printf "%-16s %-16s\n" "Full Name:" "$fullname"
printf "%-16s %-16s\n" "Home Directory:" "$homedir"
printf "%-16s %-16s\n" "Login Shell:" "$loginshell"
read -p "Ready to create? (Y/N)" isreallycreate
case $isreallycreate in
Y|y)
	useradd -p "${encrypted_pw}" $uidopt $grpopt -c "$fullname" $homediropt $lsopt "$ln"
;;
*)
	echo "Canceled."
esac
}

add_new_user
:<<COMMENT
# change the permission, ownership and group of home directory
echo "Start to change the permission of $homedir. You have three options: "
echo "1) Give all permissions to the owner, give read and execute permissions to everyone else."
echo "2) Give read and write permissions to the owner and group, give read permissions to others."
echo "3) Ohter."
read -p "Enter the number you choose: " option
case $option in
1)
	chmod -R 755 $homedir
;;
2)
	chmod -R 644 $homedir
;;
*)
	read -p "Enter 3 permission bits you want: " coption
	until echo "$coption" | grep -q '^[0-7][0-7][0-7]$'
	do
		echo "You should enter 3 permission bits each of which is 0-7."
		read -p "Enter 3 permission bits you want: " coption
	done
	chmod -R "$coption" $homedir
;;
esac
chown -R "$loginname:$grpname" $homedir
COMMENT

# 2  
Old 06-13-2013
Quote:
Originally Posted by franksunnn
a). I need to prompt users to enter their password in my script, but when users type in their password, it's visible on screen. Is there any ways to hide the STDIN or make it invisible on screen?
b). I want the password contains at least one lowercase and one uppercase and one digit and one punctuation and the length must be between 8 and 16 characters long.
Don't do it this way. You are trying to re-invent the wheel here. Instead of letting user enter a password you should set a random password (use /dev/random to do so), send it per mail to the user (or some similar way) and let the user change it when they first log on. You can set the characteristics for valid passwords, depending on the flavour of your OS, and let system utilities (like passwd) do all this validation work and the hiding what has been typed, etc.. No need to program this in shell for a second time.

I hope this helps.

bakunin
# 3  
Old 06-13-2013
Quote:
Originally Posted by bakunin
Don't do it this way. You are trying to re-invent the wheel here. Instead of letting user enter a password you should set a random password (use /dev/random to do so), send it per mail to the user (or some similar way) and let the user change it when they first log on. You can set the characteristics for valid passwords, depending on the flavour of your OS, and let system utilities (like passwd) do all this validation work and the hiding what has been typed, etc.. No need to program this in shell for a second time.

I hope this helps.

bakunin
I think you are right.
What about my third question?~~
# 4  
Old 06-14-2013
to make the password invisable you can use this:

Code:
stty -echo
print -n Enter password: 
stty echo

# 5  
Old 06-14-2013
The automated nature of the random password method means the admin never has a user's password. The password should be force-expired by the same automated process, so it must change during the first login. While on UNIX root can log in as any user, and probably cover any tracks, on other systems like RDBMS that is not true. If anything goes seriously sideways, the admin does not want to have any knowledge of user passwords.

Sprint does this really badly, showing associates your password and making you say it over the phone. It makes me sick every time they do!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Print questions from a questions folder in a sequential order

1.) I am to write scripts that will be phasetest folder in the home directory. 2.) The folder should have a set-up,phase and display files I have written a small script which i used to check for the existing users and their password. What I need help with: I have a set of questions in a... (19 Replies)
Discussion started by: moraks007
19 Replies

2. Shell Programming and Scripting

Shell script questions

Hello. I am a beginner and I need to create a script that goes: There are several library where there are compressed files. I have to decompress all files that start with 'XA%' and retrieve all rows that contain 'CSSF' and copy this to a file. Can anyone help me? Thank you in advance :) (5 Replies)
Discussion started by: papou5480
5 Replies

3. Shell Programming and Scripting

Bash script questions for gurus

guys, I need a steer in the right direction for this issue. it would be great if anyone of you can help me out. i have a textfile where i want to swap the lines based on the user input. The textfile is looks like the #file 1 name TB #file 1 ID 1000 # #file for ID1 system1... (3 Replies)
Discussion started by: mayi
3 Replies

4. Shell Programming and Scripting

how to write a script contain list of questions

hi i want to write a script that contain questions upto 50..when i run the script it has to ask the questions one by one with options like a,b,c,d and user has to answer the each question and finally result (contain how many user selected correctly) please help me...i need hints to do....... (8 Replies)
Discussion started by: srinivas2828
8 Replies

5. UNIX for Dummies Questions & Answers

Questions about cron and php script

I'm trying to set up a php script that runs via crontab on a server. I've never done anything like this before. My questions: 1) I want the crontab to run the script everyday from 3:58 PM to 4:13 PM EST. How is that written in crontab? 2) The command I want crontab to run is: php -q... (3 Replies)
Discussion started by: colin72
3 Replies

6. UNIX for Dummies Questions & Answers

script questions

Does anyone know how to use shell scripts to write the following if input is a b c d I wnat output as d c b a Thanks (2 Replies)
Discussion started by: james94538
2 Replies

7. Shell Programming and Scripting

A few questions from a newbie(shell script)

Q1>How do i read and write to file in shell script. Here is what i want let's assume the filename as "file1" Read file1 Check the content of file1 which can be either "0" or "1" if(content == 0) { execute a command } flush file1(remove all contents in it) write "1" in to... (5 Replies)
Discussion started by: perk_bud
5 Replies

8. Shell Programming and Scripting

Shell script fundamental Questions

Hi Can u please guide me to understand following script : $JAVA_HOME/bin/java -cp $CLASSPATH -Djava.naming.factory.initial=weblogic.jndi.T3InitialContextFactory -Djava.naming.provider .url=$INITIAL_CONTEXT_PROVIDER -Djava.naming.security.principal=system... (2 Replies)
Discussion started by: pankajkrmishra
2 Replies

9. Shell Programming and Scripting

2 Questions about my process control script

Hi all, My tiny mind is struggling with the following script that shuts down Tomcat on Sol. 8 and kills any defunct processes Tomcat might leave behind (as it so often does). I realize that fixing Tomcat would be best, but this is a band-aid so I won't have to do as much off-hours support of... (3 Replies)
Discussion started by: dkaplowitz
3 Replies

10. UNIX for Dummies Questions & Answers

Few script questions

Hi all, Just two quick questions about writting some scripts. The script I am writting has to be able to add users. Well I can work out the commands I need to put in for the user to be added. But how would I need to do to set the password for that user. Keeping in mind the script will be run... (1 Reply)
Discussion started by: merlin
1 Replies
Login or Register to Ask a Question