activateusers.sh

2 posts / 0 new
Last post
itefix
Offline
Last seen: 1 hour 2 min ago
Joined: 01.05.2008 - 21:33
activateusers.sh

## Small change in order to create multiple users read from a file
# without interaction
#
# The filename is passed as the argument to the script
# Each line contains a user, as folows
# L: for local users
# D: for domain users
#
#

#!/bin/bash
# Script for activation of a user.
# v1.0.2 - Tevfik Karagulle (tevfik@itefix.no)
# v1.0.2.1 - Paracelsus

echo
echo -e "\f###############################\n"
echo -e "Activate some users for copssh\n"
echo -e "###############################\n"

adminuser=$USERNAME

LUSERS=$(grep -E "^L:.*" $1|sed s/^L://)
DUSERS=$(grep -E "^D:.*" $1|sed s/^D://)

echo "Local users to create:"
echo $LUSERS
echo "Domain users to create:"
echo $DUSERS

# Function to terminate after getting input from user
terminate ()
{
read -t 10 -n 1 -p "Press a key to continue ..."
exit $1
}

#function to create a user
just_do_it()
{
# Check if the user exists (for local users only)
if ; then
if ! net user $user >&/dev/null; then
echo "User account $user does not seem to exist on this machine."
echo "You have to create it first. Exit!"
terminate 1
fi
fi

userhome=/home/$user
# Check if home directory exists
if ; then
read -n 1 -p "Directory $userhome does already exist. Remove it ? (y/n) " prompt
echo
if ; then
rm -rf $userhome
fi
fi

# remove existing user entries from /etc/passwd
cat /etc/passwd | sed "/^$user/d" > /tmp/passwd
cat /tmp/passwd > /etc/passwd
rm -f /tmp/passwd

# add user entry
if ; then
mkpasswd -l -u $user >> /etc/passwd
mkgroup -l -u > /etc/group
fi
if ; then
mkpasswd -d $domain -u $user -p /home >> /etc/passwd
mkgroup -l -u > /etc/group
mkgroup -d -g "Domain Users" >> /etc/group
fi

# make directory stuff
mkdir -p $userhome/.ssh
cd /home

listacc | sed "s/SET//" > /tmp/_tmp1
source /tmp/_tmp1
rm -f /tmp/_tmp1

# Generating public key pair
cd $userhome/.ssh
echo "Generate PKA keypair with null passphrase!"
ssh-keygen -q -b 1024 -t rsa -f $user -N ''
cat $user.pub > authorized_keys
cat $user.pub > authorized_keys2
echo "Private key is $userhome/.ssh/$user"

# Make a shortcut to user's real home directory
if ; then
cd $userhome
ln -s "`cygpath -H`/$user" "myhome"
echo "A shortcut/symbolic link to your windows home directory is created (myhome)."
fi

# Set windows permissions on home directory
xcacls $user /T /G $LISTACC_ADMGROUP:F /Y
if ; then
xcacls $user /T /E /G $user:F /Y
fi
if ; then
xcacls $user /T /E /G $domain\\$user:F /Y
fi

chmod -R 770 $userhome
chown -R $user:$LISTACC_ADMGROUP $userhome
chown $user:$LISTACC_ADMGROUP $userhome/.ssh

# making private key really private
chmod 600 "$userhome/.ssh/$user"

if ; then
echo -e "\nActivation process for $user is completed."
fi
if ; then
echo -e "\nActivation process for $domain \\ $user is completed."
fi

echo -e "You may establish an ssh connection to this machine now.\n"
}

for user in $LUSERS
do
usertype="l"
just_do_it
done

for user in $DUSERS
do
usertype="d"
just_do_it
done

terminate 0

Click Text Here

itefix
Offline
Last seen: 1 hour 2 min ago
Joined: 01.05.2008 - 21:33
Re: activateusers.sh

Wow!! Thanks a lot for your contribution.

Rgrds Tev

Topic locked

Release news