📄 add_user
字号:
#!/bin/sh5## @(#)adduser.sh 2.9 (ULTRIX) 3/12/90## Copyright (c) 1984, 1989, 1990 by # Digital Equipment Corporation, Maynard, MA # All rights reserved. # # This software is furnished under a license and may be used and # copied only in accordance with the terms of such license and # with the inclusion of the above copyright notice. This # software or any other copies thereof may not be provided or # otherwise made available to any other person. No title to and # ownership of the software is hereby transferred. # # The information in this software is subject to change without # notice and should not be construed as a commitment by Digital # Equipment Corporation. # # Digital assumes no responsibility for the use or reliability # of its software on equipment which is not supplied by Digital. ## Purpose: To add new users to the group and passwd file# Usage: adduser# # Remarks:# Interactive adduser script to ease the process of adding accounts.########################################################################### History## 30-Apr-1990 Jon Wallace# added command line argument code for fis### 001 James C. Overman 05-Aug-1990# Moved references to /tmp to /usr/tmp # ########################################################################LL=""UIDFLG=0USERFLG=0NAMEFLG=0SHELLFLG=0LOGGRPFLG=0OTHGRPFLG=0PARENTFLG=0# The maximum uid should really be read from /usr/include/limits.h UID_MAX=32000umask 022if test ! -w /etc/passwdthen echo "Please su to root first." exit 1fi# Trap signalstrap '/etc/unlockpw ; exit 1' 1 2 3 18# Lock the passwd and group files/etc/lockpwexstat="$?"if test $exstat -ne 0then exit $exstatelse trap '/etc/unlockpw ; exit 0' 0fi## See if this system is a Yellow Pages client.#tail -1 /etc/passwd | grep "^[+-]:" > /dev/nullyp_used="$?"tail -1 /etc/group | grep "^[+-]:" > /dev/nullif [ $? -eq 0 ] || [ $yp_used -eq 0 ]then /etc/unlockpw echo "This system makes use of the Yellow Pages service foruser account administration. Please add the new userby following the steps given in the Overview of YellowPages chapter of the Network Management Guide." exit 5fi################################################################ Get any command line arguments and set the variables#while [ $# -gt 0 ]do case $1 in -l ) # Login Name USER=$2 USERFLG=1 shift;shift ;; -u ) # UID number UIDFLG=1 case $2 in "" | -* ) shift ;; * ) UID=$2 shift;shift ;; esac ;; -r ) # Users Real Name NAME=$2 NAMEFLG=1 shift;shift ;; -g ) # Group Name LOGGRPFLG=1 case $2 in "" | -* ) shift ;; * ) LOGGROUP=$2 shift;shift ;; esac ;; -p ) # Parent Directory PARENTFLG=1 case $2 in "" | -* ) shift ;; * ) PARENT=$2 shift;shift ;; esac ;; -s ) # Shell Choice SHELLFLG=1 case $2 in "" | -* ) shift ;; * ) LSHELL=$2 shift;shift ;; esac ;; -o ) OTHGRPFLG=1 case $2 in "" | -* ) shift ;; * ) OTHGRP="$*" shift;shift ;; esac ;; * ) echo "adduser: Bad option. Usage: adduser -[lurgpso]" exit 1 ;; esacdone######################################################################### Get new user's login name#while truedo case $USERFLG in 0 ) echo "Enter login name for new user (initials, first or last name): \c" read USER ;; esac case "${USER}" in '') USERFLG=0 ;; *:*) echo 'Error, login name cannot contain colons.' USERFLG=0 ;; ?????????*) echo 'Error, login name cannot be longer than 8 characters.' USERFLG=0 ;; *) # See if user already exists in passwd file, if so exit. if grep -s "^${USER}:" /etc/passwd then /etc/unlockpw echo "User ${USER} already in /etc/passwd file." exit 5 fi break ;; esacdone# Get the user ID for the new user. Sort the passwd file on uid,# get the largest uid, validity check it as a valid number, then# add one to it to get the new uid.DEF_UID=`sort -nt: +2 -3 /etc/passwd | tail -1 | cut -d: -f3 | sed -n '/[0-9][0-9]*/p'`if test ! "${DEF_UID}" # Check for valid ${UID}then echo '' echo "The password file (/etc/passwd) may be corrupt!" echo "Exiting ${0} script. New entry not created!" /etc/unlockpw exit 1fiif test "${DEF_UID}" -gt ${UID_MAX}then echo '' echo "A uid greater than the maximum allowed," ${UID_MAX} "was found." echo "Exiting ${0} script. New entry not created!" /etc/unlockpw exit 1fiDEF_UID=`expr "${DEF_UID}" + 1`# Verfify UIDwhile truedo case $UIDFLG in 0 ) echo "${LL}Enter uid for new user [${DEF_UID}]: " \\c read UID ;; esac case "$UID" in '') UID=$DEF_UID break ;; *) TEST=`expr "$UID" : '\([0-9][0-9]*\)'` case $TEST in $UID ) if [ $UID -gt $UID_MAX ] then echo "UID must be less than ${UID_MAX}" UIDFLG=0 else if grep -s "^[^:]*:[^:]*:${UID}:" /etc/passwd then echo "There is another account with uid ${UID}, is this ok [yes]? " \\c read Y case "${Y}" in [yY]*|'') ;; *) UIDFLG=0 continue ;; esac fi fi break ;; * ) echo "Bad value for UID, must be an integer" UIDFLG=0 ;; esac ;; esacdone############################################################################ Get new user's real name#while truedo case $NAMEFLG in 0 ) echo "${LL}Enter full name for new user: \c" read NAME ;; esac case "${NAME}" in *:*) echo 'Error, name may not contain colons.' NAMEFLG=0 ;; '') NAMEFLG=0 ;; *) break ;; esacdone############################################################################ Get the login group for the new user.#while truedo case $LOGGRPFLG in 0 ) echo "${LL}What login group should $USER go into? [users]: \c" read LOGGROUP ;; esac case "${LOGGROUP}" in *:*) echo 'Error, group name may not contain colons.' LOGGRPFLG=0 continue ;; '') LOGGROUP=users ;; *) ;; esac LOGGID=`grep "^${LOGGROUP}:" /etc/group | cut -d: -f3` if test ! "${LOGGID}" then echo echo "Unknown group: ${LOGGROUP}. Known groups are:" echo cut -d: -f1 < /etc/group | pr -t -l1 -4 while true do echo "Do you want to add group ${LOGGROUP} to the /etc/group file? [yes]: " \\c read ADDGROUP case "${ADDGROUP}" in [yY]*|'') echo "Adding new group to /etc/group file..." addgroup "${LOGGROUP}" LOGGID=${?} break 2 ;; [nN]*) LOGGRPFLG=0 break ;; * ) echo 'Invalid response, you must answer yes or no.' ;; esac done else break fidone########################################################################## Get other groups if this user is to be part of any others#while truedo case $OTHGRPFLG in 0 ) echo "Enter any other group(s) that '${USER}' should be a member of(<RETURN> only if none): " \\c read OTHGRP OTHGRP=`echo $OTHGRP` ;; esac case $OTHGRP in "" ) break ;; * ) for K in $OTHGRP do case $K in *:*) echo 'Error: group name may not contain colons.' echo "Cannot add '${USER}' to group '${K}'." OTHGRPFLG=0 ;; *) OTH_GROUP="$OTH_GROUP $K" ;; esac done ;; esac case $OTHGRPFLG in 1 ) break ;; esacdone########################################################################## Get the group ID for the new user#for K in $OTH_GROUPdo GID=`grep "^${K}:" /etc/group | cut -d: -f3` if test ! "${GID}" then echo echo "Unknown group: ${K}. Known groups are:" echo cut -d: -f1 < /etc/group | pr -t -l1 -4 while true do echo "Do you want to add group ${K} to the /etc/group file [yes]? " \\c read ADDGROUP case "${ADDGROUP}" in [yY]*|'') echo '' echo 'Adding new group to /etc/group file...' addgroup "${K}" break ;; [nN]*) continue 2 ;; *) echo 'Error, invalid response, you must answer yes or no.' ;; esac done fi # Add the user to each group as it is specified grep "^${K}:" /etc/group | cut -d: -f4 | grep -s "${USER}" case $? in 0 ) echo "" echo "User ${USER} is already a member of group ${K}." ;; * ) rm -f /usr/tmp/group > /dev/null cp /etc/group /usr/tmp/group ed /usr/tmp/group > /dev/null << EOF /^${K}:/s/\$/,${USER}/ g/:,/s//:/ w qEOF mv /usr/tmp/group /etc/group ;; esacdone######################################################################### Get the parent directory for the user#while truedo case $PARENTFLG in 0 ) echo "Enter parent directory for ${USER} [/usr/users]: " \\c read PARENT ;; esac case "${PARENT}" in '') PARENT=/usr/users ;; esac if [ -d "${PARENT}" ] then break else while true do echo "${PARENT} not found, do you want to create it? [yes]: " \\c read ADDDIR case "${ADDDIR}" in [yY]*|'') if mkdir "${PARENT}" then chmod 755 "${PARENT}" chgrp "${LOGGROUP}" "${PARENT}" break 2 else echo "Unable to create ${PARENT}." PARENTFLG=0 break fi ;; nN*) PARENTFLG=0 break ;; *) echo 'Error, you must answer either yes or no.' ;; esac done fidone######################################################################### Get the users login shell.#while truedo case $SHELLFLG in 0 ) echo "${LL}The shells are:" echo pr -4 -t /etc/shells echo echo 'Enter the users login shell name [/bin/csh]: ' \\c read LSHELL ;; esac case "${LSHELL}" in '' ) LSHELL=/bin/csh break ;; */* ) ;; * ) X=`grep "/${LSHELL}$" /etc/shells` if [ -n "${X}" ] then LSHELL="${X}" break else SHELLFLG=0 fi ;; esacdoneecho "${LL}Adding new user..."# Make sure parent directories for everything exist.if test \! -d /usr/spoolthen mkdir /usr/spoolfiif test \! -d /usr/spool/mailthen mkdir /usr/spool/mailfi# Add the user to the password file.echo "${USER}:Nologin:${UID}:${LOGGID}:${NAME}:${PARENT}/${USER}:${LSHELL}" >> /etc/passwdif [ -f /usr/etc/mkpasswd -a -f /etc/passwd.pag ] then echo 'Rebuilding the passwd data base...' ( cd /etc ; /usr/etc/mkpasswd -u passwd )fi# Add the user to the auth fileif test -f /etc/auth.pag -a -f /usr/etc/sec/setauth thenDEFPASSMAXLIFE=60DEFPASSMINLIFE=0 echo "Enter maximum password lifetime in days [${DEFPASSMAXLIFE}]: " \\c read MAXPASSLIFE if test ! "${MAXPASSLIFE}" then MAXPASSLIFE=${DEFPASSMAXLIFE} fi MAXPASSLIFE=`expr "${MAXPASSLIFE}" \* 24 \* 60 \* 60`# echo "Enter minimum password lifetime in days [${DEFPASSMINLIFE}]: " \\c read MINPASSLIFE if test ! "${MINPASSLIFE}" then MINPASSLIFE=${DEFPASSMINLIFE} fi MINPASSLIFE=`expr "${MINPASSLIFE}" \* 24 \* 60 \* 60`# echo 'Will machine generated passwords be required [no]? ' \\c if read X then case "${X}" in [Yy]*) AUTHMASK=03 ;; *) AUTHMASK=07 ;; esac fi echo "${UID}:Nologin:1:${MINPASSLIFE}:${MAXPASSLIFE}:${AUTHMASK}:0:${UID}:00:00:00" \ | /usr/etc/sec/setauth echo 'Do you wish to edit the auth file entry for this user [no]? ' \\c if read X then case "${X}" in [yY]*) /usr/etc/sec/edauth "${USER}" ;; esac fifi# Create home and bin directories, and set-up filesecho 'Creating home directory...'cd "${PARENT}"if [ "${?}" -ne 0 ] then echo "Unable to cd to parent directory ${PARENT}" exit 1fifor I in "${USER}" "${USER}/bin" do if [ -d "${I}" ] then echo "${PARENT}/${I} already exists." else mkdir "${I}" chmod 0751 "${I}" /etc/chown "${USER}" "${I}" chgrp "${LOGGROUP}" "${I}" fidonefor I in .profile .login .cshrc do if [ -s "${USER}/${I}" ] then echo "${PARENT}/${USER}/${I} already exists." else cp /usr/skel/${I} "${USER}/${I}" chmod 0751 "${USER}/${I}" /etc/chown "${USER}" "${USER}/${I}" chgrp "${LOGGROUP}" "${USER}/${I}" fidone# Unlock the password and group files/etc/unlockpw# Set a passwordecho "You must set a password for the newly created user account."while true do if passwd "${USER}" then break fidoneexit 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -