home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / armlinux / alpha / PARTITIONS / USR_GZ / usr / sbin / adduser < prev    next >
Encoding:
Text File  |  1995-06-09  |  6.0 KB  |  247 lines

  1. #! /bin/sh
  2. #
  3. # adduser 1.92: a utility to add users to the system
  4. #
  5. # Copyright (C) 1994 Debian Association, Inc.
  6. #
  7. #    adduser is free software; you can redistribute it and/or modify it
  8. #    under the terms of the GNU General Public License as published by
  9. #    the Free Software Foundation; either version 2 of the License, or
  10. #    (at your option) any later version.
  11. #
  12. #    adduser is distributed in the hope that it will be useful, but
  13. #    WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. #    General Public License for more details.
  16. #
  17. #    You should have received a copy of the GNU General Public License
  18. #    along with adduser; if not, write to the Free Software Foundation,
  19. #    Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. set -e
  22.  
  23. # Everything happens too fast, so don't let the user interrupt.
  24. # We certainly don't want a half-done job.
  25. trap "" 1 2 3 15
  26.  
  27. exist=0
  28. numarg=$#
  29.  
  30. username="$1"
  31.  
  32. defaults="/etc/adduser.conf"
  33. passwd="/etc/passwd"
  34. group="/etc/group"
  35. pbak="/etc/passwd~"
  36. gbak="/etc/group~"
  37.  
  38. # This is the standard method of locking the password file.
  39. # Is there a ``standard'' method of locking the group file?
  40. plock="/etc/ptmp"
  41.  
  42. # We use this to lock the group file in Debian GNU/Linux.
  43. glock="/etc/gtmp"
  44.  
  45. # If USERGROUPS is "no" and there are no users in the default group,
  46. # then add the user to the group in $group without a leading comma.
  47. # If there are, then add the user to the group in $group preceded by
  48. # a comma.  If USERGROUPS is "yes", then simply create the new group
  49. # and add the user to it.
  50. add_to_group()
  51. {
  52.   if [ $USERGROUPS = "no" ]
  53.   then
  54.     # First of all, determine the name of the group.
  55.     group_number=$1
  56.     group_name=`grep "::$group_number:" $group | cut -f 1 -d ":"`
  57.     echo -n "($group_name)... "
  58.     if grep -x "^$group_name::$group_number:$" $group >/dev/null 2>&1
  59.     then
  60.       sed "/^$group_name/s/\$/$username/" $group > $glock
  61.     else
  62.       sed "/^$group_name/s/\$/,$username/" $group > $glock
  63.     fi
  64.     if [ -f $glock ]
  65.     then
  66.       cp $group $gbak
  67.       mv $glock $group
  68.     fi
  69.   else
  70.     # Note that arguments are completely ignored in this case.
  71.     echo -n "($username)... "
  72.     echo "$username::$nuid:$username" >> $glock
  73.     cp $group $gbak
  74.     mv $glock $group
  75.   fi
  76. }
  77.  
  78. # What do we do if $defaults doesn't exist? 
  79. no_defaults ()
  80. {
  81.   DSHELL="/bin/bash"
  82.   DHOME="/home"
  83.   SKEL="/etc/skel"
  84.   USERGROUPS="yes"
  85.   FIRST_UID=1000
  86.   USERS_GID=1000
  87. }
  88.  
  89. # Make sure that we can add $username to the system.
  90. if [ `whoami` != "root" ]
  91. then
  92.   echo "$0: only root may add users to the system."
  93.   exit 1
  94. fi
  95. if [ $numarg = 0 ]
  96. then
  97.   echo "$0: you need to specify the username to add;"
  98.   echo "for example, \`$0 imurdock'."
  99.   echo "The default variables are defined in the file"
  100.   echo "$defaults."
  101.   exit 1
  102. fi
  103. if grep ^$username /etc/passwd >/dev/null
  104. then
  105.   echo "$0: the user $username already exists." ; exit 1
  106. fi
  107. if [ -f $plock ]
  108. then
  109.   echo "$0: $passwd is locked.  Try again later." ; exit 1
  110. fi
  111. if [ -f $glock ]
  112. then
  113.   echo "$0: $group is locked.  Try again later." ; exit 1
  114. fi
  115.  
  116. # Okay, we can.
  117. cp $passwd $plock
  118. cp $group  $glock
  119. # Damn! We don't have link(1) (or is it link(8)?),
  120. # so we'll have to live with the race condition - iwj
  121.  
  122. # And now the programs begins.
  123.  
  124. # Check that we have a valid username.
  125.  
  126. if [ -f $defaults ]
  127. then
  128.   echo -n "$0: reading in $defaults... " ; source $defaults ; echo "done."
  129. else
  130.   echo "$0: no defaults file found.  Using built-in defaults." ; no_defaults
  131. fi
  132.  
  133. echo "" ; echo -n "Looking for first available UID... "
  134. nuid=`cat /etc/passwd | sed /^nobody:/d | cut -f 3 -d ":" | sort -n | \
  135.       tail -1` ; nuid=`expr $nuid + 1`
  136. if [ $nuid -lt $FIRST_UID ]
  137. then
  138.   nuid=$FIRST_UID
  139. fi
  140. if [ $USERGROUPS = "no" ]
  141. then
  142.   ngid=$USERS_GID
  143. elif [ $USERGROUPS = "yes" ]
  144. then
  145.   ngid=$nuid
  146. else
  147.   echo -e "\n$0: USERGROUPS should be \`yes' or \`no'."
  148.   rm -f $plock $glock
  149.   exit 1
  150. fi
  151. echo "done.  Using UID $nuid and GID $ngid."
  152.  
  153. if [ $nuid = 0 -o $nuid -gt 65534 ]        # _Definitely_ don't want that!
  154. then
  155.   echo ""
  156.   echo "$0: Ack!  Something went wrong!  Aborting!"
  157.   echo ""
  158.   rm -f $plock $glock
  159.   exit 1                    # _Big_ time...
  160. fi
  161.  
  162. echo -n "Adding user $username... "
  163. echo "$username:*:$nuid:$ngid::$DHOME/$username:$DSHELL" >> $plock
  164. cp $passwd $pbak
  165. mv $plock $passwd
  166. echo "done."
  167.  
  168. echo -n "Adding $username to group $ngid "
  169. add_to_group $ngid
  170. echo "done."
  171.  
  172. echo -n "Creating home directory: $DHOME/$username... "
  173. if [ -d $DHOME/$username ]
  174. then
  175.   echo -e "\n*** $DHOME/$username already exists!  Not copying files from $SKEL. ***"
  176. else
  177.   mkdir $DHOME/$username
  178.   if [ "$USERGROUPS" = "yes" ]
  179.   then
  180.     chown $nuid.$ngid $DHOME/$username
  181.     chmod 2775 $DHOME/$username
  182.   fi
  183.   cp -i $SKEL/.[a-z]* $SKEL/* $DHOME/$username >/dev/null 2>&1 || true
  184.   if [ "$USERGROUPS" = "yes" ]
  185.   then
  186.     for dotfile in .bash_profile .profile .login
  187.     do
  188.       if [ -f $DHOME/$username/$dotfile ]
  189.       then
  190.         sed 's/umask 0\([267]\)\1/umask 00\1/' $DHOME/$username/$dotfile \
  191.          > $DHOME/$username/$dotfile.new
  192.         mv $DHOME/$username/$dotfile.new \
  193.          $DHOME/$username/$dotfile
  194.       fi
  195.     done
  196.     chmod g+w $DHOME/$username/.[a-z]*
  197.   fi
  198.   # Probably will never happen, but just in case... we don't want all
  199.   # files on the system to be `chown'ed to $nuid.$ngid!
  200.   if [ "$DHOME/$username" != "/" ]
  201.   then
  202.     chown -R $nuid.$ngid $DHOME/$username
  203.   fi
  204.   echo "done."
  205. fi
  206.  
  207. set -f
  208.  
  209. passwd $username
  210. pass=`cat $passwd | fgrep $username | cut -d : -f 2`
  211. while [ "$pass" = "*" ]
  212. do
  213. {
  214.   echo -e "\nPlease try again."
  215.   passwd $username
  216.   pass=`cat $passwd | fgrep $username | cut -d : -f 2`
  217. }
  218. done
  219.  
  220. set +f
  221. chfn $username
  222. if [ -x /usr/bin/finger ]
  223. then
  224.   finger $username
  225.   echo -ne "\n\nIs this okay (y/n)? "
  226. else
  227.   echo -ne "\n\nIs this finger information correct (y/n)? "
  228. fi
  229. read input
  230.  
  231. until [ $input = y -o $input = Y ]
  232. do
  233. {
  234.   chfn $username
  235.   if [ -x /usr/bin/finger ]
  236.   then
  237.     finger $username
  238.     echo -ne "\n\nIs this okay [y/n]? "
  239.   else
  240.     echo -ne "\n\nIs this finger information correct [y/n]? "
  241.   fi
  242.   read input
  243. }
  244. done
  245.  
  246. # EOF
  247.