Script: softice-osc-adduser

From SOFTICE

Jump to: navigation, search


#!/bin/bash
# copyleft Alessio Gaspar, SOFTICE project, 2006/07/17
# modified by Matt Rideout, SOFTICE project, 2007/07/31,  2007/08/30, 2007/09/13
# modified by Alessio Gaspar, SOFTICE project, 2008/01/09, 2008/04/29
# file:         $SOFTICE_ROOT/bin/softice-user-add
# chmod:        only executable by root
# credits:      used parts of add_user script available at http://www.hccfl.edu/pollock/AUnix2/addUsers.htm
#               Written 2002 by Wayne Pollock, Tampa FL USA.

# Create and then populate user account with all the data necessary for the student
# to work on the softice osc labs

# configuration of our softice installation
SOFTICE_ROOT="/softice"
what="softice-user-add"


#TODOs;
#       - re-tar package so that it extracts both the os stuff and the bin w/ the user add / del tools
#       - modify this script to test whether we are using warewulf or not (same for samba)

#_____________________________________________________________________________
function usage {
#_____________________________________________________________________________

    echo "softice-user-add Coure StudentName Password"
    echo "   examples:"
    echo "      softice-user-add course StudentName Password"
    echo "      softice-user-add course FileName"
    echo "   filename needs to be an existing filename containing"
    echo "        students accounts information"
    echo "   Courses cop4610 and eel4782 also set up the environment for students"
    echo "        by calling respectively softice-osc-setup or softice-net-setup"
    exit -1
}



#_____________________________________________________________________________
function add_from_file {
#_____________________________________________________________________________
    # parameters
    # $1 groupname
    # $2 filename
    groupname="students"
    course=$1
    filename=$2

    if [[ ! -e $filename ]]
    then
        echo "$what Error: File $filename doesn't exist"
        exit -1
    fi

    exec 3< $filename

    until [ $done ]
    do
      # this one stores the line in a variable
      read <&3 myline
      # this one parses it according to $1 $2 $3...
      set -- $myline
      #  The "--" prevents nasty surprises if $planet is null or
      #+ begins with a dash.
      #  May need to save original positional parameters,
      #+ since they get overwritten.
      #  One way of doing this is to use an array,
      #         original_params=("$@")
      # cf. adv bash scripting guide

      if [ $? != 0 ]
      then
          done=1
          continue
      fi
      if [[ $# != 5 ]]
      then
          echo "$what ERROR: Data file $filename not properly formatted"
          echo "             Last Name <tab> First Name <tab> Login Name <tab> Email Address <tab> Password"
          echo "             read $# words: $*"
          exit -1
      fi

      StudentName="$1 $2"
      StudentLogin=$3
      StudentEmail=$4
      StudentPassword=$5
      echo "$what   ADDING $StudentName to group $groupname with login $StudentLogin"
      # the inlining below skips question about where to store the default ssh key file
      # then skips twice the passphrase
      # ok the ssh-keygen call below has been removed for now
      add_one_student $course $StudentLogin $StudentPassword

      mail -s "USF Linux account for $groupname" $StudentEmail <<EOF
The following account has been opened for you to use exclusively for work related to one of your USF course.
Please refer to the syllabus for information about legal and proper usage of the University computing resources.
Hostname: penguin.lakeland.usf.edu
Login:    $StudentLogin
Password: $StudentPassword
Course:   $course
EOF
    done

exit 0;
}





#_____________________________________________________________________________
function add_one_student {
#_____________________________________________________________________________

    course=$1
    student=$2
    password=$3

    echo "$what Building environment for user $student course $course password $password"
    #echo " softice root = $SOFTICE_ROOT"
    #echo " TAG = $TAG"

    for TARGET in /home/students /home/students/$course
    do
        if [[ !  -d $TARGET ]]
        then
                mkdir $TARGET
                chown root:students $TARGET
                chmod u=rwx,og=rx $TARGET
        fi
    done


    #useradd -m -g students -d /home/students/$course/$student -c "$course" -e $closing -p '' $student -s /bin/bash
    useradd -m -g students -d /home/students/$course/$student -p '' $student -s /bin/bash
    echo $student:$password | chpasswd

    # set the student as the owner of all files in their home directory, and deny access to all other users
    chown -R $student:students /home/students/$course/$student
    chmod -R og-rwx /home/students/$course/$student/

    dpkg -l | grep ii | grep samba > /dev/null
    if [[ $? -eq 0 ]]
    then
        # create a Samba account
        sambaPassword=`dd if=/dev/random count=10 bs=1 | hexdump  | cut -d \  -f 2-| head -n 1 | tr -d " "`
        $SOFTICE_ROOT/bin/smb-user-add $student $sambaPassword

        # create .credentials file for SMB mounts
        echo "username=$student" > /home/students/$course/$student/.credentials
        echo "password=$sambaPassword" >> /home/students/$course/$student/.credentials
        chown $student:students /home/students/$course/$student/.credentials
        chmod 600 /home/students/$course/$student/.credentials
    fi


    # delete local user from nodes
    if [[ -d /vnfs ]]
    then
        # add local users to nodes
        cat /etc/shadow | grep ^$student: >> /vnfs/default/etc/shadow
        wwnodes --sync
    fi
    # TODO:   verify pam settings for group students
    # TODO:   what about disk quotas


    if [[ $TAG = "osc" ]]
    then
        su - $student -c $SOFTICE_ROOT/$TAG/bin/softice-osc-setup
    fi

    if [[ $TAG = "net" ]]
    then
        su - $student -c $SOFTICE_ROOT/$TAG/bin/softice-net-setup
    fi

    cd $WHEREWECOMEFROM

    return
}


#_____________________________________________________________________________
# script starts here
#_____________________________________________________________________________



if [[ $# != 2 && $# != 3 ]]
then
    usage
fi


WHEREWECOMEFROM="`pwd`"

course=$1
#let's first figure out if we're running for osc or elsa labs
if [[ $course = "cop4610" ]]
then
    TAG="osc"
fi
if [[ $course = "eel4782" ]]
then
    TAG="net"
fi


if [[ $# == 2 ]]
then
    add_from_file $*
fi

if [[ $# == 3 ]]
then
    add_one_student $*
fi

exit 0