Toon posts:

[BC3] Script voor nieuwe user

Pagina: 1
Acties:

Verwijderd

Topicstarter
Ben een beetje een newbie en heb daarom het volgende vraagje.

Ik wil een script maken om nieuwe gebruikers toe te voegen en aan de hand van vragen bepaalde dingen te laten uitvoeren, zoals of de nieuwe gebruiker een POP-box moet krijgen, een eigen website wil, een virtuele website wil, ftp-toegang wil, quota etc.
Iemand hier een voorbeeld van ? Mogen nog veel meer opties inzitten, maar heb geen flauw idee hoe zoiets aan te pakken.... :'(

Thanx alvast!

BTW, draai RH 7.1 op mijn bak....

  • leander
  • Registratie: Oktober 1999
  • Laatst online: 02-03-2025
Hier is alvast een beginnetje :)
je zult ''m wsch nog wel behoorlijk moeten tweaken...

Leander


#!/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=$#
LOGIN="$1"
EXIST=0

NOHOME="$2"

VERSION="adduser [version 1.11 PL1 (18-Jan-2001)]"
USERADMIN="useradmin@domain.nl"
LOGFILE="/root/useradmin/adduser.log"
TESTMODE="0"
# set TESTMODE to 1 if you don''t want to make the user...

PASSWD="/etc/passwd"
PBAK="/etc/passwd-"
PNEW="/etc/passwd.new"

SHADOW="/etc/shadow"
SBAK="/etc/shadow-"
SNEW="/etc/shadow.new"

GROUP="/etc/group"
GBAK="/etc/group-"
GNEW="/etc/group.new"

PLOCK="/etc/.pwd.lock"# Standard method of locking the password file.

##
# Ensure that root is running the script.
##
WHOAMI=`/usr/bin/whoami`
if [ $WHOAMI != "root" ]; then
echo "You must be root to add news users!"
exit 1
fi

echo $VERSION
##
# Ask for info about the new user...
##
echo ""
echo -n "Username: "
read USERNAME
echo -n "Full name: "
read FULLNAME
echo -n "User password: "
read USERPWD
echo -n "Email user: "
read USEREMAIL
echo "You have to choose a user type..."
echo " - guest user (1)"
echo " - mail user (2)"
echo " - both mail and guest user (3)"
echo -n "Usertype: "
read USERTYPE

LOGIN=$USERNAME
DHOME="/data/web"

# generate passwd, NB: the salt has to be random some day...
CRYPTPWD=`/usr/bin/perl -e ''$tmppwd = $ARGV[0]; $pwd = crypt ($tmppwd,''u2'');print $pwd'' $USERPWD`

DSHELL="/usr/bin/noshell"
# SKEL="/etc/skel" Guests don''t need that...
# SPOOL="/var/spool/mail" Or this...

if [ $USERTYPE = "1" ]; then
FIRST_UID=1000
NGID=400
GRPNAME="client"
PWHOME="/data/web/$LOGIN/./"
QUOTAMODEL="casper"
fi
if [ $USERTYPE = "2" ]; then
FIRST_UID=2000
NGID=505
GRPNAME="mailuser"
PWHOME="none"
SPOOL="/var/spool/mail"
QUOTAMODEL="pollux"
fi
if [ $USERTYPE = "3" ]; then
FIRST_UID=3000
NGID=400
GRPNAME="client"
PWHOME="/data/web/$LOGIN/./"
SPOOL="/var/spool/mail"
QUOTAMODEL="casper"
fi


# A few sanity checks...

if id "$LOGIN" >/dev/null 2>&1 ; then
echo "Guest $LOGIN already exists"
exit 1
fi

if [ $(echo "$LOGIN" | wc -c | tr -d '' '') -gt 9 ] ; then
# that 9 is intentional -- it counts the newline, too...
echo "$LOGIN is over eight characters"
exit 1
fi

if echo "$LOGIN" | grep -q : ; then
echo "$LOGIN contains a \`:'' character"
exit 1
fi

id $LOGIN >/dev/null 2>/dev/null && EXIST=1

if [ $EXIST = 1 ]; then
echo "The login $LOGIN already exists."
exit 1
fi

#if ln $PASSWD $PLOCK; then : ; else
#echo "$PASSWD is locked. Try again later." ; exit 1
#fi

die ()
{
rm -f $PLOCK
exit 1
}

# 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 65535 ]; then
echo "Sorry, ran out of uids."
die
fi
echo " $NUID"


# echo -n "Looking for first available GID..."
# NGID=`cut -f 3 -d ":" $GROUP | sort -n | awk -v gid=$FIRST_GID ''
# { if ($1 == gid) gid = gid + 1; }
# END{ print gid; }
# ''`
#
# if [ $NGID -lt $FIRST_GID ]; then
#NGID=$FIRST_GID
# fi
#
# if [ $NGID -ge 65535 ]; then
# echo "Sorry, ran out of gids."
#die
# fi
# echo " $NGID"

if [ $TESTMODE = "0" ]; then

echo "" ; echo -n "Adding login: $LOGIN..."

cp $PASSWD $PBAK || die
cp $PASSWD $PNEW || die
if [ -f "$SHADOW" ] ; then
echo "$LOGIN:x:$NUID:$NGID:$FULLNAME:$PWHOME:$DSHELL" | \
cat >> $PNEW || die
# need cat because echo won''t return a return code
cp $SHADOW $SBAK || die
cp $SHADOW $SNEW || die
# The date invocation is non-standard, but works with GNU date.
# It fills in the "last changed" field with the number of DAYS
# since epoch (86400 seconds per day; %s is second since epoch).
echo "$LOGIN:$CRYPTPWD:$(($(date ''+%s'')/86400))::::::" | \
cat >> $SNEW || die
else
# note: the /./ sequence determines where the chroot() is done to, group=400.
echo "$LOGIN:$CRYPTPWD:$NUID:$NGID:$FULLNAME:$PWHOME:$DSHELL" | \
cat >> $PNEW || die
fi

# Add user to users group
cp $GROUP $GBAK || die
cp $GROUP $GNEW || die
sed "s/^\($GRPNAME:.*[^:]\)\$/\1,$LOGIN/"''
''"s/^\($GRPNAME:.*:\)\$/\1$LOGIN/" < $GBAK > $GNEW || die

# echo "client::400:$LOGIN" | cat >> $GNEW || die

mv $GNEW $GROUP
if [ -f "$SHADOW" ] ; then
mv $SNEW $SHADOW
fi
mv $PNEW $PASSWD

rm -f $PLOCK
echo "done."

if [ $USERTYPE != "2" ]; then

if [ "x$NOHOME" = "x" ]; then
echo "Creating home directory: $DHOME/$LOGIN..."
mkdir $DHOME/$LOGIN
mkdir $DHOME/$LOGIN/public_html
chmod 755 $DHOME/$LOGIN $DHOME/$LOGIN/public_html
#cp -a $SKEL/.??* $SKEL/* $DHOME/$LOGIN >/dev/null 2>/dev/null
chown $LOGIN.client $DHOME/$LOGIN $DHOME/$LOGIN/public_html
echo "Building directory structure: $DHOME/$LOGIN/bin and $DHOME/$LOGIN/lib..."
mkdir $DHOME/$LOGIN/bin $DHOME/$LOGIN/lib
echo "Setting permissions..."
chown root.daemon $DHOME/$LOGIN/lib $DHOME/$LOGIN/bin
chmod 111 $DHOME/$LOGIN/bin $DHOME/$LOGIN/lib
echo "Building contents of $DHOME/$LOGIN/bin and $DHOME/$LOGIN/lib..."
cp -rpf /var/ftp/bin/* $DHOME/$LOGIN/bin
cp -rpf /var/ftp/lib/* $DHOME/$LOGIN/lib
echo "done."
fi
fi

if [ $USERTYPE != "1" ]; then
echo "Creating mailbox: $SPOOL/$LOGIN..."
touch $SPOOL/$LOGIN ; chmod 640 $SPOOL/$LOGIN ; chown $NUID.mail $SPOOL/$LOGIN
echo "done."
fi

echo ""
echo "Setting up quota for new account..."

/usr/sbin/edquota -p $QUOTAMODEL $LOGIN

echo ""
if [ "x$NOHOME" != "x" ]; then
echo ""
echo "The home directory for $LOGIN was set to $DHOME/$LOGIN but the directory"
echo "was not created. Be sure that you set it up properly."
fi

fi
# End of ''if TESTMODE = 0'' block.

############
# Mail the new user about this wonderful account
##

if [ $USERTYPE = "1" -o $USERTYPE = "3" ]; then
(
echo "Subject: guest-account created"
echo '' ''
echo "Beste $FULLNAME,"
echo " "
echo "Zoals gevraagd is er een homepage-account voor je aangemaakt op de"
echo "server:"
echo " "
echo -n "loginnaam: "
echo $USERNAME
echo -n "password: "
echo $USERPWD
echo " "
echo "Gebruik: "
echo "Inloggen op de sever, met:"
echo "1) TELNET: om je password te veranderen"
echo "2) FTP: om (html)bestanden te uploaden"
echo " "
echo "Alleen de bestanden in de directory public_html zijn zichtbaar voor de"
echo "buitenwereld. De file ~/public_html/home.html is de code voor de"
echo "voorpagina van je homepage. De URL hiervan is:"
echo "http://www.domain.nl/~$USERNAME"
echo "Andere bestanden, bijv. ~/public_html/page2.html, krijgen de URL"
echo "http://www.domain.nl/~$USERNAME/page2.html"
echo " "
echo "Graag toekomstige veranderingen van je e-mail adres mailen naar"
echo "useradmin@domain.nl."
echo " "
echo "Succes!"
echo " "
)|sendmail -f ''useradmin'' $USEREMAIL
#,$USERADMIN
fi

if [ $USERTYPE = "2" -o $USERTYPE = "3" ]; then
(
echo "Subject: mail-account created"
echo '' ''
echo "Beste $FULLNAME,"
echo " "
echo "Er is een e-mail account voor je aangemaakt op de server:"
echo " "
echo -n "loginnaam: "
echo $USERNAME
echo -n "password: "
echo $USERPWD
echo " "
echo "Maak de volgende instellingen in je mail programma:"
echo "Pop account: username@domain.nl"
echo "Return address: username@domain.nl (dit is dus je e-mail adres)"
echo "SMTP-server: domain.nl"
echo " "
echo "Om het password te veranderen kun je met telnet inloggen op"
echo "domain.nl"
echo "Geef je username, het huidige password, en (2x) het nieuwe password."
echo "Succes!"
echo " "
)|sendmail -f ''useradmin'' $USEREMAIL
# $USERADMIN
fi

############
# Write some stuff in the logfile
##

(
echo $VERSION
date
echo "Added: $FULLNAME"
echo "login: $USERNAME"
echo "email: $USEREMAIL"
echo "usertype: $USERTYPE"
echo "initial password: $USERPWD"
echo "crypted password: $CRYPTPWD"
)|cat >> $LOGFILE
chmod 600 $LOGFILE
# That''s to be sure in case of a loss :-)

############
# Notify the almighty useradmin
##

echo "Well, all is done. The useradmin will be notified..."
(
echo "O well, yet another user has been created on the server: $LOGIN"
echo '' ''
echo "Some facts:"
echo $VERSION
date
echo "Added: $FULLNAME"
echo "login: $USERNAME"
echo "email: $USEREMAIL"
echo "usertype: $USERTYPE"
echo "initial password: $USERPWD"
echo "crypted password: $CRYPTPWD"
echo " "
echo ''The very entry in our passwordfile:''
echo '' ''
tail -1 /etc/passwd
echo '' ''
echo ''And who were around at that very moment...''
echo '' ''
who
)|mail -s ''adduser output'' $USERADMIN

############
# There''s nothing left to say...
#