#!/bin/bash 
#
#  This script is for the login 'new'.  Create this login and set
#  Their shell to this file.  This file MUST run with SUID root.  However
#  the bad news is that you cannot run shell scripts like this.  You'll
#  have to make user 'new' a superuser.  This isn't a security risk
#  though, since it is IMPOSSIBLE!! to get to a command shell like this,
#  especially by just hitting <CTRL>-C like most stupid idiots think.
#  Let me say it again..  YOU CAN NOT!  GRAB A ROOT SHELL 	This file 
#  *IS* the shell, the *ONLY* shell to user 'new' !!  Also I tried to use 
#  the sudo package, but it would not echo to a file as root and was more 
#  trouble than it was really worth, it wouldn't have made any difference 
#  from a security standpoint anyway.  Remember this is not C, you cannot 
#  have buffer overruns with scripts.
#
#  It will ask the user for his information and create the new account.
#  I've purposely set this up with functions to operate with this SINGLE
#  file, eliminating the need to call external scripts.  
#
#  The username is converted to lower case because EMAIL is not case
#  sensitive and will cause problems with that.  

BBSNAME="Solarflow BBS"

ETC_PATH=/usr/local/etc
INFO_PATH=/usr/local/info
DOC_PATH=/usr/local/doc
LIB_PATH=/usr/local/lib
DONE=nope
WHERE=$(w | grep new | tail -1 | awk '{printf("(%s) (%s)", $2, $3)}')
NOW=$(date '+%-I:%M %p  %a %b %-d')

echo -e "\033(U"	# This one line of code will tell the linux
			# console to use IBM graphics.

subscribe()
{
	cat $LIB_PATH/14.default
	echo
	echo "************************************************************"
	echo "* Please take the time to answer the following 7 questions.*"
	echo "* Once the questionnaire has been completed, your account  *"
	echo "* will be created.  You'll have the option to create an    *"
        echo "* alias for yourself if you want, as well as the choice to *"
	echo "* keep your real name hidden from other users.  These      *"
	echo "* questions are only intended for statistical reasons.     *"
	echo "************************************************************"
	echo
	cat $LIB_PATH/10.default
	echo "What is your FIRST and LAST name? (optional)"
	read NAME
	echo
	echo "What is your City and State/Province  (or COUNTRY if outside North America) "
	read CITY
	echo 
	echo "What type of Processor does your computer have? (345)86, etc)"
	read PROCESSOR
	echo
	echo "What Operating System are you using? (Win95, DOS, UNIX, etc)"
	read OS
	echo
	echo "What TERM ot Telnet program are you using? (Procomm, QVTterm, xterm, etc)"
	read PROG
	echo
	echo "Is there anything you like to see the most BBSes? (Files, Chat, web space, etc)"
	read INTERESTS
	echo
	echo -n "Where did you hear of this BBS? "
	read HEAR
	cat $LIB_PATH/14.default
	echo 
	echo "--------------------------------------------------------------------"
	echo "Now you must provide a username to log into the system with."
	echo "This can be your alias, a combination of your first and last name or"
        echo "whatever you like, but a few system restrictions do apply to ensure"
        echo "your login name can work properly."
	cat $LIB_PATH/9.default
	echo " -Punctuation characters like @!#$%^&* should be avoided"
	echo " -Login names can only be ONE word"
	echo " -Login names are converted to LOWER CASE"
 	echo " -Passwords ARE case sensative"
	cat $LIB_PATH/14.default
	echo "--------------------------------------------------------------------"
	echo 
	cat $LIB_PATH/10.default
	echo -n "What do you want your login name to be? "
	read USERNAME
	USERNAME=$(echo $USERNAME | tr [:upper:] [:lower:])
	cat $LIB_PATH/14.default
	echo
	echo "------------------------------------------------------------"
	echo "Now you can enter a comment for yourself.  This will be"
	echo "available to all other users and can be as long as you like."
	echo "If you don't want to enter a comment, then just press ENTER."
	echo "------------------------------------------------------------"
	echo 
	cat $LIB_PATH/10.default
	echo -n "Comment: "
	read PLAN
	clear
	cat $LIB_PATH/13.default
	echo 
	echo
	echo "-- Questionaire Completed,  Creating Your Account... "
	cat $LIB_PATH/14.default
	/usr/sbin/useradd -s /usr/local/bin/BBS -c "$NAME" $USERNAME 2>>$ETC_PATH/sysop.log
	if [ $? = 0 ]
		then
		chmod 711 /home/$USERNAME	#Read permision for Web pages 
		cat $LIB_PATH/11.default
		echo "Account Created!"
		echo $PLAN > /home/$USERNAME/.plan
		chown $USERNAME /home/$USERNAME/.plan
		chgrp $USERNAME /home/$USERNAME/.plan
		echo "$USERNAME				$CITY" >> $DOC_PATH/bbsusers.ans
		echo "$USERNAME; $NAME; $CITY; $PROCESSOR; $OS; $PROG; $INTERESTS; $HEAR; $NOW" >> $INFO_PATH/user.list
		echo "-----------------------------------------------------------------------" >> $INFO_PATH/user.list
		echo "********************************************" >> $ETC_PATH/sysop.log
		echo "-- NEW USER CREATED: '$USERNAME' from $WHERE at $NOW" >> $ETC_PATH/sysop.log
		cat $LIB_PATH/10.default
		echo
		echo -n "Do you want your real name HIDDEN to other users? (Y/N) "
		read RESPONSE
		if [ $RESPONSE = y -o $RESPONSE = Y ]
		then
			echo
			chfn -f $USERNAME $USERNAME
		fi
		cat $LIB_PATH/14.default
		echo
		echo "Now you must choose a Password for yourself."
		echo "** NOTE: passwords ARE case sensative **"
		echo
		cat $LIB_PATH/10.default
		passwd $USERNAME
		while ! [ $? = 0 ]
		do
			echo
			passwd $USERNAME
			done
		DONE=yup
		cat $LIB_PATH/14.default
		echo "All done!  Press ENTER when you have written down your password ..." ; read NULL
		su $USERNAME
		echo "$USERNAME logging OFF from $WHERE at $NOW" >> /home/$USERNAME/user.log
	else
		cat $LIB_PATH/11.default
		echo "Account creation FAILED.  Error has been logged..."  
		echo 
		echo "The system was unable to create your account!"
 		echo "This is probably because you ignored the restrictions on creating a username." 
		echo "You might want to try again.."
		echo
		echo "Account creation FAILED for: $USERNAME; $NAME; $CITY; $PROCESSOR; $OS; $PROG; $INTERESTS; $HEAR; $PLAN AT $NOW from: $WHERE" >> $ETC_PATH/sysop.log
		sleep 5
		DONE=yup
		exit
	fi
}
cat $LIB_PATH/14.default
echo
echo "Welcome New User!"  
echo "There are just a few quick questions to answer."
echo
echo "** Please set your client software to support ANSI colour and IBM graphics **"

while [ $DONE = nope ] 
do
	cat $LIB_PATH/13.default
	echo -n "Would you like to create an account? (Y/N) "
	read RESPONSE
	case $RESPONSE in
	Y | y )
		subscribe    # More functions can be put here if necessary
	;;

	N | n )
		cat $LIB_PATH/11.default
		echo "Leaving $BBSNAME.  Disconnecting ..."
		DONE=yup
		exit
	;;

	esac
done
 
exit
