siddou.tk

dialog bash scripts to manage SambaLdap Users/Groups

Tutorial source: http://bash.cyberciti.biz/guide/A_menu_box

nano /usr/sbin/smbtools

#!/bin/bash

#pre-required:
#apt-get install dialog ldap-utils

# Store menu options selected by the user
INPUT=/tmp/menu.sh.$$

# Storage file for displaying list command output
OUTPUT=/tmp/output.sh.$$

# trap and delete temp files
trap “rm $OUTPUT; rm $INPUT; exit” SIGHUP SIGINT SIGTERM

#
# Purpose – display output using msgbox
# $1 -> set msgbox height
# $2 -> set msgbox width
# $3 -> set msgbox title
#
function display_output () {
local h=${1-10} # box height default 10
local w=${2-41} # box width default 41
local t=${3-Output} # box title
dialog –backtitle “Manage Samba Users” –title “${t}” –clear –msgbox “$(<$OUTPUT)" ${h} ${w} } # function display_addusergroup () { local h=${1-10} # box height default 10 local w=${2-41} # box width default 41 local t=${3-Output} # box title dialog --backtitle "Manage User Groups" --title "${t}" --clear --msgbox "$USER added to the folowing groups: \n $(<$OUTPUT)" ${h} ${w} } # function display_delusergroup () { local h=${1-10} # box height default 10 local w=${2-41} # box width default 41 local t=${3-Output} # box title dialog --backtitle "Manage User Groups" --title "${t}" --clear --msgbox "$USER deleted from the folowing groups: \n $(<$OUTPUT)" ${h} ${w} } # function display_smbuserpwd () { local h=${1-10} # box height default 10 local w=${2-41} # box width default 41 local t=${3-Output} # box title dialog --backtitle "Change password" --title "${t}" --clear --msgbox "$USER password changed" ${h} ${w} } # # Purpose - display all samba groups # function show_smbgroup(){ > /tmp/output.sh.$$
ldapsearch -x -H ldaps://ldaps_server_name -b “ou=Groups,dc=siddou,dc=com” \
-D “uid=ro_user,ou=Users,dc=siddou,dc=com” -w ro_user_pwd memberUid \
| grep -B 1 “memberUid: ” >$OUTPUT
display_output 60 70 “smbgroup”
}
#
# Purpose – display user samba groups
#
function show_smbusergroup(){
> /tmp/output.sh.$$
uid=””
exec 3>&1
USER=$(dialog \
–backtitle “List user’s groups” \
–title “” \
–form “Select username (ex. plasc)” \
15 50 0 \
“Username:” 1 1 “$uid” 1 10 10 0 \
2>&1 1>&3)
exec 3>&-

ldapsearch -x -H ldaps://ldaps_server_name -b “ou=Groups,dc=siddou,dc=com” \
-D “uid=ro_user,ou=Users,dc=siddou,dc=com” -w ro_user_pwd memberUid=$USER \
| grep “displayName: ” >$OUTPUT
display_output 60 70 “smbusergroup”
}
#
# Purpose – display samba users
#
function show_smbuser(){
> /tmp/output.sh.$$
ldapsearch -x -H ldaps://ldaps_server_name -b “ou=Users,dc=siddou,dc=com” \
-D “uid=ro_user,ou=Users,dc=siddou,dc=com” -w ro_user_pwd uid gecos \
| grep -A 1 “uid: ” >$OUTPUT
display_output 50 50 “smbuser”
}
#
# Purpose – change samba user password
#
function show_smbuserpwd(){
> /tmp/output.sh.$$
uid=””
exec 3>&1
USER=$(dialog \
–backtitle “Change user password” \
–title “” \
–form “Select username (ex. plasc)” \
10 30 0 \
“Username:” 1 1 “$uid” 1 10 5 0 \
2>&1 1>&3)
exec 3>&-
if [ “$USER” = “root” ]; then
echo “cannot change root password”
elif [ “$USER” = “” ]; then
echo “cannot change root password”
else
/usr/local/sbin/smbldap-passwd -a $USER
fi
display_smbuserpwd 10 30 “smbuserpwd”
}
#
#
# Purpose – add samba user to a group
#
function add_usergroup(){
> /tmp/output.sh.$$
uid=””
exec 3>&1
USER=$(dialog \
–backtitle “Add user to a group” \
–title “” \
–form “Select username (ex. plasc)” \
15 50 0 \
“Username:” 1 1 “$uid” 1 10 10 0 \
2>&1 1>&3)
exec 3>&-
dialog –separate-output –title “” –checklist \
“Choose $USER’s groups” 20 78 20 \
“admin” “Sysadmin group” OFF \
“commercial” “Commercial group” OFF \
“production” “Production group” OFF \
“administratives” “Administratives group” OFF \
“marketing” “Marketing group” OFF 2>$OUTPUT
IN=$(<$OUTPUT) arr2=$IN for x in $arr2 do /usr/local/sbin/smbldap-groupmod -m $USER $x done display_addusergroup 40 50 "adduser_group" } # # # Purpose - Delete samba user from a group # function del_usergroup(){ > /tmp/output.sh.$$
uid=””
exec 3>&1
USER=$(dialog \
–backtitle “Delete user from a group” \
–title “” \
–form “Select username (ex. plasc)” \
15 50 0 \
“Username:” 1 1 “$uid” 1 10 10 0 \
2>&1 1>&3)
exec 3>&-
dialog –separate-output –title “” –checklist \
“Choose groups to remove from $USER” 20 78 20 \
“admin” “Sysadmin group” OFF \
“commercial” “Commercial group” OFF \
“production” “Production group” OFF \
“administratives” “Administratives group” OFF \
“marketing” “Marketing group” OFF 2>$OUTPUT
IN=$(<$OUTPUT) arr2=$IN for x in $arr2 do /usr/local/sbin/smbldap-groupmod -x $USER $x done display_delusergroup 40 50 "deluser_group" } # # # Purpose - add new samba # function add_user(){ > /tmp/output.sh.$$
uid=””
gecos=””
mail=””
exec 3>&1
USER=$(dialog \
–backtitle “Create user” \
–title “” \
–form “Select username (ex. plasc)” \
15 50 0 \
“Username:” 1 1 “$uid” 1 10 5 0 \
2>&1 1>&3)
exec 3>&-
exec 3>&1
GECOS=$(dialog \
–backtitle “Create user” \
–title “” \
–form “Enter display name (ex. Paul Lascar)” \
15 50 0 \
“Display Name:” 1 1 “$gecos” 1 15 40 0 \
2>&1 1>&3)
exec 3>&-
exec 3>&1
MAIL=$(dialog \
–backtitle “Create user” \
–title “” \
–form “Enter mail (ex. Paul.Lascar@siddou.com)” \
15 50 0 \
“Email:” 1 1 “$mail” 1 7 40 0 \
2>&1 1>&3)
exec 3>&-
/usr/local/sbin/smbldap-useradd -a -c “\”$GECOS\”” -P $USER -M $MAIL &&
mkdir /mnt/SIDDOU/USERS/$USER &&
chown $USER:513 /mnt/SIDDOU/USERS/$USER &&
chmod 700 /mnt/SIDDOU/USERS/$USER &&
chattr +i /mnt/SIDDOU/USERS/$USER &&
mkdir /mnt/SIDDOU/SHARE/$USER &&
chown $USER:513 /mnt/SIDDOU/SHARE/$USER &&
chmod 750 /mnt/SIDDOU/SHARE/$USER &&
chattr +i /mnt/SIDDOU/SHARE/$USER

dialog –separate-output –title “” –checklist \
“Choose $USER’s groups” 20 78 20 \
“admin” “Sysadmin group” OFF \
“commercial” “Commercial group” OFF \
“production” “Production group” OFF \
“administratives” “Administratives group” OFF \
“marketing” “Marketing group” OFF 2>$OUTPUT
IN=$(<$OUTPUT) arr2=$IN for x in $arr2 do /usr/local/sbin/smbldap-groupmod -m $USER $x done display_addusergroup 40 50 "add_user" } # # # Purpose - Delete samba user # function del_user(){ > /tmp/output.sh.$$
uid=””
exec 3>&1
USER=$(dialog \
–backtitle “Delete user” \
–title “” \
–form “Select username to delete (ex. plasc)” \
15 50 0 \
“Username:” 1 1 “$uid” 1 10 10 0 \
2>&1 1>&3)
exec 3>&-

dialog –title “Confirmation” \
–backtitle “” \
–yesno “Are you sure you want to permanently delete user $USER?” 7 60

# Get exit status
# 0 means user hit [yes] button.
# 1 means user hit [no] button.
# 255 means user hit [Esc] key.
response=$?
case $response in
0) /usr/local/sbin/smbldap-userdel $USER ;;
1) echo “User $USER not deleted.”;;
255) echo “[ESC] key pressed.”;;
esac
display_output 40 50 “del_user”
}
#
# set infinite loop
#
while true
do

### display main menu ###
dialog –clear –backtitle “SAMBA01 Tools” \
–title “[Menu]” \
–menu “Choose the TASK” 20 70 10 \
smbgroup “Display all samba groups” \
smbusergroup “display user samba groups” \
smbuser “Display samba users” \
smbuserpwd “Change user password” \
addusergroup “Add user to a group” \
adduser “Create new user” \
delusergroup “Delete user from a group” \
deluser “Delete user (but keep home folder)” \
Exit “Exit to the shell” 2>”${INPUT}”

menuitem=$(<"${INPUT}") # make decsion case $menuitem in smbgroup) show_smbgroup;; smbusergroup) show_smbusergroup;; smbuser) show_smbuser;; smbuserpwd) show_smbuserpwd;; addusergroup) add_usergroup;; adduser) add_user;; delusergroup) del_usergroup;; deluser) del_user;; Exit) break;; esac done # if temp files found, delete em [ -f $OUTPUT ] && rm $OUTPUT [ -f $INPUT ] && rm $INPUT clear [/sourcecode]

Leave a Reply

Your email address will not be published. Required fields are marked *