#! /usr/bin/sh # # Filename: test-cid # # Created by: David Stillman # # Modified by: Br. David Carlson Dec 1996, Jan 1997 # # Script to prompt system administrator for lastnames of people for whom # new accounts are to be made. This script produces a file called # acs.input that contains the ids and fullnames of these people. # Whenever possible, the user id is chosen to be the lastname of the person. # The script takes the fullnames from the student-data file of registered # students whenever possible. If no such student is in this file, but # the system admin wants to create an account anyway, then the fullname # is entered from the keyboard. This script checks to see that there # are no duplicate ids (either duplicates with the existing password file # of duplicates with what has already been put in the acs.input file). # Ids are also limited to a length of 8 characters. Duplicates are # avoided by adding extra characters if there is room within the limit # of length 8, or by overwriting the last few characters with new ones # if need be. It is recommended that the added characters be the first # one, two, or three characters of the person's first name. # # Input file: student-data # Note that the format of the file is one student per line, # with each line containing |lastname |firstname |fullname # possibly followed by further information. # # Output file: acs-input lower_id() # puts string tmp_id in lower case { tmp_id=`echo $tmp_id | tr [:upper:] [:lower:]` } trim_id() # cuts string tmp_id down to length at most 8 { id_len=`expr length $tmp_id` if [ $id_len -gt 8 ] then tmp_id=`echo $tmp_id | cut -c1-8` fi } grep_stu() # Creates file acs-tmp1 containing a line for each registered student whose # name matches or contains the lastname under consideration as a new user. # The file has the following format, starting with a | symbol: # |last |first |fullname etc... { stu_grep=`grep -i $lname student-data` echo "$stu_grep" > acs-tmp1 } see_dup_stu() # Displays data on registered students with same lastname. { echo "\n\t\t<< Grep student-data for [$lname] >> " while read line do num=`echo $line | cut -d "," -f1` name=`echo $line | cut -d "," -f3` echo "$num $name" done < acs-tmp2 } auto_fullname() # Gets fullname of registered student with given lastname from file acs-tmp2 { while read line2 do line_num=`echo $line2 | cut -d "," -f1` fname=`echo $line2 | cut -d "," -f3` if [ "$line_num" = "$stu_num" ] then fullname="$fname" fi done < acs-tmp2 } creat_dup_stu() # Creates file acs-tmp2 containing data on registered students with the # given lastname. This data is in the form: # number,lastname,fullname # The lines are numbered 1, 2, 3, etc. { num=0 while read line do # Get rid of spaces between fields in student-data lname2=`echo $line | cut -d "|" -f2 | tr -d " "` lname2=`echo $lname2 | tr [:upper:] [:lower:]` # Get the fullname: cr_fname=`echo $line | cut -d "|" -f4` if [ "$lname2" = "$lname" ] then num=`expr $num + 1` echo "$num,$lname2,$cr_fname" >> acs-tmp2 fi done < "acs-tmp1" } get_letrs() # Get 1 to 3 letters to append to or overwrite the end of the lastname # to try to get a unique id. { num_chars="" until [ "$num_chars" = "ok" ] do echo "Enter up to [3] characters to make the id unique: \c" read letrs len_letrs=`expr length $letrs` if [ $len_letrs -le 3 -a $len_letrs -ge 1 ] then num_chars="ok" fi done } grep_pw() # Look for new_id in the passwd file. Set pw_grep to indicate if a duplicate # was found or not. { # note the use of -x to get an exact match -- added by Br. David pw_grep=`cut -d ":" -f1 /etc/passwd | grep -x $new_id` if [ "$pw_grep" != "" ] then pw_grep="dup" else pw_grep="" fi } grep_acs() # Look for new_id in the existing acs.input file. Set acs_grep to # indicate if a duplicate was found or not. { acs_grep="" if [ -s acs.input ] then # -x option added by Br. David acs_grep=`cut -d "," -f1 acs.input | grep -x $new_id` if [ "$acs_grep" != "" ] then acs_grep="dup" fi fi } validate_id() # Tries to validate tmp_id as the new id, placing the result in new_id. # If need be characters are added at the end to produce a unique id. { lower_id trim_id letrs="" # Find out if duplicates exist in /etc/passwd and/or the acs.input file new_id="$tmp_id" hold_id="$new_id" valid_id="false" echo "try [$tmp_id] as the new id >>\n" until [ "$valid_id" = "true" ] do grep_pw grep_acs if [ "$acs_grep" = "dup" -o "$pw_grep" = "dup" ] then new_id="$hold_id" echo "\nFor reference, the following matches exist in acs.input\n" grep -i $new_id acs.input echo "\nFor reference, the following matches exist in /etc/passwd\n" cut -d ":" -f1,5 /etc/passwd | grep -i $new_id id_len=`expr length $new_id` case $id_len in [1-7] ) get_letrs tmp_id="$new_id$letrs" trim_id lower_id new_id=$tmp_id;; 8 ) echo "\n[$new_id] is 8 characters long; adding characters will" echo "replace characters starting from the right.\n" get_letrs idremlen=`expr 8 - $len_letrs` new_id=`echo $new_id | cut -c1-$idremlen` tmp_id="$new_id$letrs" lower_id new_id=$tmp_id echo "The new id is $new_id";; esac else # no duplicate was found valid_id=true fi done } get_fullname() # Get a fullname from the keyboard for the person to be added. { fname_ok="" until [ "$fname_ok" = "ok" ] do echo "Enter fullname (or s to skip): \c" read fullname if [ "$fullname" = "s" ] then flag="skip" fname_ok="ok" else resp="" until [ "$resp" = "ok" ] do echo "\n$fullname okay to use (y/n/s)? \c" read resp case "$resp" in y | Y ) fname_ok="ok" resp="ok";; n | N ) resp="ok";; s | S ) resp="ok" flag="skip" fname_ok="ok";; esac done fi done } get_lname() # Get a lastname from the keyboard for the person to be added. { echo "Enter a lastname (q to quit): \c" read lname } ######################################################### # MAIN PROGRAM # ######################################################### # zero the acs.input file: > acs.input continue="yes" clear echo echo "Note: Use lowercase for all responses including lastnames." echo "Exception: Type the fullname in mixed case, exactly as you want it" echo "to be shown by the finger command. Put the first name, middle" echo "initial, and lastname in the fullname so as to help to distinguish" echo "this person from other users with similar names. Do not use commas" echo "in the fullname, but periods are OK. A title such as Dr. is OK." echo until [ "$continue" = "no" ] do tmp_id="" stu_grep="" get_lname if [ "$lname" = "q" ] then continue="no" else # zero the acs-tmp1 and acs_tmp2 files: > acs-tmp1 > acs-tmp2 # cut out the id and fullname fields and grep for lname: echo "\n\t\t<< Grep /etc/passwd for [$lname] >>" cut -d ":" -f1,5 /etc/passwd | grep -i $lname grep_stu creat_dup_stu tmp_id=$lname if [ "$stu_grep" != "" ] then # One of more registered students already has this lastname. see_dup_stu response="" until [ "$response" = "ok" ] do echo "\nEnter number(for fullname above), s(to skip), \c" echo "or nf(for new fullname): \c" read ansr2 case "$ansr2" in [1-9]|10|11|12|13|14|15) flag="number" stu_num="$ansr2" if [ "$stu_num" -gt "$num" ] then echo "Number out of range!" else auto_fullname echo "\n<< Use No. $ansr2, $fullname, and \c" validate_id response="ok" fi;; s ) flag="skip" response="ok";; nf ) echo "The response was nf for new fullname" flag="fullname" get_fullname if [ "$flag" != "skip" ] then validate_id fi response="ok";; esac done else # No registered student was found with this lastname. echo "\n<< Last name [$lname] cannot be found in the student \c" echo "data file. >>\n" flag="fullname" get_fullname if [ "$flag" != "skip" ] then validate_id fi fi if [ "$flag" != "skip" ] then valid_resp="false" until [ "$valid_resp" = "true" ] do echo "\nCan we add $fullname as $tmp_id? (y/n): \c" read ansr case $ansr in y | Y ) echo "[$fullname] will be added as [$tmp_id]\n" echo "$tmp_id,$fullname" >> acs.input valid_resp="true";; n | N ) echo "[$fullname] as [$tmp_id] will be skipped\n" valid_resp="true";; esac done else echo "\n\t\t<< Skip account creation for [$lname] >>\n" fi fi done