#! /bin/sh # # adduser 2.0: a utility to add users to the system # # Copyright (C) 1998 Todd Robinson # Copyright (C) 1994 Ian A. Murdock # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # # Originally for the Debian Linux distribution (01/21/94). Feel free to use # it in YOUR distribution, too. :) Please note that this is just a simple # script that _somewhat_ automates the really boring and repetitive task # of creating new user accounts. It makes no attempt to be sophisticated. # Let me know if you improve it in any way. # # I need to write a man page for this. # # Modified by Marc Ewing for RHS Linux # 12/05/98 Modified by Todd Robinson to add command line options: # -g adds the new user to the indicated group # default: users # -d makes the indicated directory the users home # default: /home/'user name' # -h c|f|m disable c)reation of home directory # disable f)illing of home directory from /etc/skel # disable m)ail box setup (sendmail) # -n users real name or relevant comment # default: 'RH Linux User' # -s makes the indicated program the users shell # default: /bin/bash # Everything happens too fast, so don't let the user interrupt. trap "" 1 2 3 15 # Set a few important variables before getting started. NUMARG=$# EXIST=0 PASSWD="/etc/passwd" PBAK="/etc/passwd-" # Some programs use /etc/passwd-, others use # /etc/passwd.OLD. Take your pick. GROUP_FILE="/etc/group" GROUP_FILE_BAK="/etc/group-" GROUP="users" PLOCK="/etc/ptmp" # Standard method of locking the password file. NAME="RH Linux User" NEW_SHELL="/bin/bash" HOME_BASE="/home" SKEL="/etc/skel" SPOOL="/var/spool/mail" FIRST_UID=500 CREATE_HOME=1 FILL_HOME=1 MAKE_MAIL=1 ARG_ERR=0 # A few sanity checks... if [ `id -u` != 0 ]; then echo "Only root may add users to the system." ; exit 1 fi if [ $NUMARG = 0 ] then ARG_ERR=1 fi # Get command line options if any while getopts d:g:h:n:s option do case $option in g) GROUP=$OPTARG;; d) HOME_DIR=$OPTARG;; n) NAME=$OPTARG;; h) case $OPTARG in c) CREATE_HOME=0;; f) FILL_HOME=0;; m) MAKE_MAIL=0;; *) ARG_ERR=3;; esac;; s) NEW_SHELL=$OPTARG;; *) ARG_ERR=2;; esac done shift `expr $OPTIND - 1` if [ $# -ne 1 ] then echo "$0: no user name specified, or invalid argument count" ARG_ERR=4 fi if [ $ARG_ERR -gt 0 ] then echo "Usage: adduser [ -g ][ -d ][ -h c|f|m ]" echo " [ -n ][ -s ] " echo $ARG_ERR exit 1 fi NEW_USER="$1" if [ -z $HOME_DIR ] then HOME_DIR=$HOME_BASE/$NEW_USER fi id $NEW_USER >/dev/null 2>/dev/null && EXIST=1 if [ $EXIST = 1 ]; then echo "The login $NEW_USER already exists." exit 1 fi if [ -f $PLOCK ]; then echo "$PASSWD is locked. Try again later." exit 1 fi # And now the program begins: echo "" ; echo -n "Looking for first available UID..." NUID=`cut -f 3 -d ":" $PASSWD | sort -n | awk -v uid=$FIRST_UID ' { if ($1 == uid) uid = uid + 1; } END { print uid; } '` if [ $NUID -ge 65536 ] then echo "" echo "*** Sorry, ran out of uids." exit 1 fi echo " $NUID" echo -n "Looking for Group $GROUP GID..." NGID=`grep $GROUP $GROUP_FILE |cut -f 3 -d ":"` if [ -z $NGID ] then echo "" echo "*** Sorry, can't find Group: $GROUP" exit 1 fi echo " $NGID" echo "" ; echo -n "Adding login: $NEW_USER..." touch $PLOCK ; cp $PASSWD $PBAK echo "$NEW_USER:*:$NUID:$NGID:$NAME:$HOME_DIR:$NEW_SHELL" >> $PASSWD # Add new user to selected group cp $GROUP_FILE $GROUP_FILE_BAK sed "s/^\($GROUP.*[^:]\)\$/\1,$NEW_USER/" < $GROUP_FILE_BAK | sed "s/^\($GROUP.*:\)\$/\1$NEW_USER/" > $GROUP_FILE #sed "s/^\(users.*[^:]\)$/\1,$LOGIN/" < $GBAK | #sed "s/^\(users.*:\)$/\1,$LOGIN/" > $GROUP #echo "$LOGIN::$NGID:$LOGIN" >> $GROUP rm -f $PLOCK echo "done." if [ $CREATE_HOME -eq 1 ] then echo -n "Creating home directory: $HOME_DIR..." mkdir $HOME_DIR chmod 2775 $HOME_DIR if [ $FILL_HOME -eq 1 ] then cp -a $SKEL/.??* $SKEL/* $HOME_DIR >/dev/null 2>/dev/null chown -R $NUID.$NGID $HOME_DIR fi echo "done." fi if [ $MAKE_MAIL -eq 1 ] then echo -n "Creating mailbox: $SPOOL/$NEW_USER..." touch $SPOOL/$NEW_USER chmod 660 $SPOOL/$NEW_USER chown $NUID.mail $SPOOL/$NEW_USER echo "done." fi echo "" echo "Don't forget to set the password." exit 0