Samba ADC user manager script


This is a script that you can use to view/manage users on a Linux/Samba Based Domain controller from the command line with an easy to follow text menu based Q/A structure.

DISCLAIMER: THIS SCRIPT HAS THE ABILITY TO DELETE USERS AND THEIR FILES, PLEASE USE THIS SCRIPT RESPONSIBLY, I CANNOT BE HELD RESPONSIBLE FOR LOSS OF DATA DUE TO YOUR NEGLIGENCE 

To use this script, Copy the code below in to a new file and name it usrmgr.sh (or whatever you like with a .sh extension)

Code:

#! /bin/bash
#Samba Domain controller user managment script by Ron laws 
#Script Version 2.1 (Refined)
#Email ron@coolmediatech.com
#See http://rons-forum.co.uk/viewtopic.php?f=10&t=28&p=33#p33 for Updates


#First run variable,
STAT=0


#Main Menu
begin(){
   clear
   STAT="1"
   echo "Samba Domain User Manager"
   echo "Please Choose:"
   echo "0. List Users and Domain Computers (joined)"
   echo "1. Create user"
   echo "2. Delete user"
   echo "3. Change someones password"
   echo "4. Quit"
   read -e DO
   # Handle Choice
   if [ $DO -eq 0 ]; then
      clear;
      list
   elif [ $DO -eq 1 ]; then
      clear;
      create
   elif [ $DO -eq 2 ]; then
      clear;
      delete
   elif [ $DO -eq 3 ]; then
      clear;
      edit
   elif [ $DO -eq 4 ]; then
      echo "Quitting...";
      exit
   fi
}


#List Users
list(){
   echo "$ at end of name denotes computers joined to the domain server"
   echo "-------------------------Begin List---------------------------"
   pdbedit -w -L | awk -F: '{print $1}'
   echo "--------------------------End List----------------------------"
   echo "Press enter to continue"
   read 
   begin
}


#Create a User
create(){
   echo "Please enter a name for the new user:"
   read -e USER
   echo "enter group (users)"
   read -e GROUP
   echo "Creating user "$USER
   useradd $USER -m -G $GROUP
   echo "Please give the user a password:"
   smbpasswd -a $USER
   echo "Done!, Created User" $USER "in group" $GROUP
   echo "Hit enter to Continue..."
   read
   begin
}


#Delete A User
delete(){
   echo "Please enter a name for the user you wish to delete:"
   read -e USER
   if [ "$USER" = "root" ]; then #Dissallow deletion of root :)
      echo "Deleting of root is disallowed for the sanity of yourself and the safety of this machine."
      echo "The root account is a vital system account which is required by the system to operate. Deleting this account will cause the operating system to stop working permanently."
      echo "Press enter to go to the beginning"
      read
      begin
   fi


   echo "WARNING!!! YOU ARE ABOUT TO DELETE USER" $USER "ARE YOU SURE??? (1=yes/0=no)"
   read -e SURE
      if [ $SURE -eq 1 ]; then 
         echo "Deleting User" $USER
         deluser $USER
         smbpasswd -x $USER
      else
         begin
      fi
   
   echo "Would you like to Delete "$USER"'s Files? (/home/"$USER"/) (1=yes/0=no)"
   read -e DEL
      if [ $DEL -eq 1 ]; then
         echo "Deleting /home/"$USER "Please Wait..."
         rm -rf /home/$USER
rm -rf /home/samba/profiles/$USER
         rm -rf /home/samba/profiles/$USER.V2
         echo "Done, Hit enter to continue"
         read
         begin
      elif [ $DEL -eq 0 ]; then
         echo "Keeping Files"
         echo "Press Enter to continue..."
         read
         begin
      fi
}


#Change a User password
change()   {
   echo "Please enter username to change the password of:"
   read -e USER
   smbpasswd -a $USER
   echo $USER"'s password changed!"
   echo "Hit enter to Continue..."
   read
   begin
}


#Workaround for First run, Get trapped in the loop.
if [ $STAT -eq 0 ]; then
   if [[ $(/usr/bin/id -u) -ne 0 ]]; then
         echo "Not running as root, to use this script you must execute it under the root user or with sudo at the beginning."
          exit
   fi
begin
fi


Make the script executable
Code: Select all
 chmod a+x usrmgr.sh

then run it by typing ./usrmgr.sh in the terminal

Enjoy

No comments:

Post a Comment