home *** CD-ROM | disk | FTP | other *** search
/ Dream 46 / Amiga_Dream_46.iso / Linux68k / adduser next >
Text File  |  1997-11-13  |  4KB  |  147 lines

  1. #! /bin/sh
  2. #
  3. # adduser 1.0: a utility to add users to the system
  4. #
  5. # Copyright (C) 1994 Ian A. Murdock <imurdock@shell.portal.com>
  6. #
  7. #    This program is free software; you can redistribute it and/or modify
  8. #    it 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. #    This program is distributed in the hope that it will be useful,
  13. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #    GNU General Public License for more details.
  16. #
  17. #    You should have received a copy of the GNU General Public License
  18. #    along with this program; if not, write to the Free Software
  19. #    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. #
  21. # Written for the Debian Linux distribution (01/21/94).  Feel free to use
  22. # it in YOUR distribution, too. :)  Please note that this is just a simple
  23. # script that _somewhat_ automates the really boring and repetitive task
  24. # of creating new user accounts.  It makes no attempt to be sophisticated.
  25. # Let me know if you improve it in any way.
  26. #
  27. # I need to write a man page for this.
  28. #
  29. # Modified by Marc Ewing <marc@redhat.com> for RHS Linux
  30.  
  31. # Everything happens too fast, so don't let the user interrupt.
  32. trap "" 1 2 3 15
  33.  
  34. # Set a few important variables before getting started.
  35. NUMARG=$#
  36. LOGIN="$1"
  37. EXIST=0
  38.  
  39. NOHOME="$2"
  40.  
  41. PASSWD="/etc/passwd"
  42. PBAK="/etc/passwd-"        # Some programs use /etc/passwd-, others use
  43.                 # /etc/passwd.OLD.  Take your pick.
  44.  
  45. GROUP="/etc/group"
  46. GBAK="/etc/group-"
  47.  
  48. PLOCK="/etc/ptmp"        # Standard method of locking the password file.
  49.  
  50. DSHELL="/bin/bash"
  51. DHOME="/home"
  52. SKEL="/etc/skel"
  53. SPOOL="/var/spool/mail"
  54. FIRST_UID=500
  55. FIRST_GID=500
  56.  
  57. # A few sanity checks...
  58. if [ `id -u` != 0 ]; then
  59.     echo "Only root may add users to the system." ; exit 1
  60. fi
  61.  
  62. if [ $NUMARG = 0 ]; then
  63.     echo "You need to specify the login to add; for example, \`adduser" \
  64.         "imurdock'." ; exit 1
  65. fi
  66.  
  67. id $LOGIN >/dev/null 2>/dev/null && EXIST=1
  68.  
  69. if [ $EXIST = 1 ]; then
  70.     echo "The login $LOGIN already exists."
  71.     exit 1
  72. fi
  73.  
  74. if [ -f $PLOCK ]; then
  75.     echo "$PASSWD is locked.  Try again later." ; exit 1
  76. fi
  77.  
  78. # And now the program begins: 
  79. echo "" ; echo -n "Looking for first available UID..."
  80. NUID=`cut -f 3 -d ":" $PASSWD | sort -n | awk -v uid=$FIRST_UID '
  81.         { if ($1 == uid) uid = uid + 1; }
  82. END        { print uid; }
  83. '`
  84.  
  85. if [ $NUID -ge 65536 ]; then
  86.     echo "Sorry, ran out of uids."
  87.     exit 1
  88. fi
  89. echo " $NUID"
  90.  
  91.  
  92. echo -n "Looking for first available GID..."
  93. NGID=`cut -f 3 -d ":" $GROUP | sort -n | awk -v gid=$FIRST_GID '
  94.         { if ($1 == gid) gid = gid + 1; }
  95. END        { print gid; }
  96. '`
  97.  
  98. if [ $NGID -lt $FIRST_GID ]; then
  99.     NGID=$FIRST_GID
  100. fi
  101. echo " $NGID"
  102.  
  103. echo "" ; echo -n "Adding login: $LOGIN..."
  104. touch $PLOCK ;
  105.  
  106. cp $PASSWD $PBAK
  107. echo "$LOGIN:*:$NUID:$NGID:RHS Linux User:$DHOME/$LOGIN:$DSHELL" >> $PASSWD
  108.  
  109. # Add user to users group
  110. cp $GROUP $GBAK
  111. sed "s/^\(users.*[^:]\)\$/\1,$LOGIN/" < $GBAK |
  112. sed "s/^\(users.*:\)\$/\1$LOGIN/" > $GROUP
  113.  
  114. #sed "s/^\(users.*[^:]\)$/\1,$LOGIN/" < $GBAK |
  115. #sed "s/^\(users.*:\)$/\1,$LOGIN/" > $GROUP
  116.  
  117. echo "$LOGIN::$NGID:$LOGIN" >> $GROUP
  118.  
  119. rm -f $PLOCK
  120. echo "done."
  121.  
  122. if [ "x$NOHOME" = "x" ]; then
  123.     echo -n "Creating home directory: $DHOME/$LOGIN..."
  124.     mkdir $DHOME/$LOGIN
  125.     chmod 2775 $DHOME/$LOGIN
  126.     cp -a $SKEL/.??* $SKEL/* $DHOME/$LOGIN >/dev/null 2>/dev/null
  127.     chown -R $NUID.$NGID $DHOME/$LOGIN
  128.     echo "done."
  129. fi
  130.  
  131. echo -n "Creating mailbox: $SPOOL/$LOGIN..."
  132. touch $SPOOL/$LOGIN ; chmod 660 $SPOOL/$LOGIN ; chown $NUID.mail $SPOOL/$LOGIN
  133. echo "done."
  134.  
  135. echo ""
  136. echo "Don't forget to set the password."
  137. if [ "x$NOHOME" != "x" ]; then
  138.     echo ""
  139.     echo "The home directory for $LOGIN was set to $DHOME/$LOGIN but the directory"
  140.     echo "was not created.  Be sure that you set it up properly."
  141. fi
  142.  
  143. #passwd $LOGIN
  144. #chfn $LOGIN
  145.  
  146. # EOF
  147.