Linux Lite 8.0 RC1 has been released - Click here


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

cnss-gui
#1

Hm, this adds some dependancy but the folder selection dialog is limited.
Can we use this Jerry?

Code:
#!/bin/bash
#Samba share settings GUI for Linux Lite
#Licence GPLv2
#Run with: /usr/bin/su-to-root -X -c /path/to/cnss-gui
#Depends: python3-tk zenity
#

#Check if  python3-tk is installed
tk=`python3 <<END
'''Tries to import tkinter'''
try:
    import tkinter
    from tkinter import filedialog
    print ("Y")
except ImportError:
    print ("N")
END`

case $tk in
N) zenity --error --text="Package python3-tk is missing\nPlease install it.\napt-get install python3-tk"
     exit 1
;;
# Y) echo "python3-tk found" ;;
esac

TITLE="Lite Shares"

function first_time() {
        if ! zenity --question --title="$TITLE" --text="First time setup should be only runned once!\nDo you want to continue?"; then
            return
        fi

#----Displays users with id greater than 1000 and less than 60000
#----Adds FALSE and TRUE (TRUE only on the first) and
#----stores everything in an aray
LL_LIST_H+=($(awk -F':' '$3>=1000 && $3<=60000 {print "FALSE", $1}' /etc/passwd |sed '0,/FALSE/s/FALSE/TRUE/'))

#User selection dialog
user="$(zenity --title="Add a user to Samba" --list --radiolist --text="Select a user name you wish to add to Samba." --print-column=2  --column="Select" --column="Username" --width=325  --height=300  "${LL_LIST_H[@]}")"
       # Cancel
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                      return
    fi
#Exit variable for the return status from the smbpasswd that will be runing in the subshell
exit_status=$(mktemp /tmp/XXXXXX)

echo -e "$(zenity --forms --title="$TITLE" --text="Enter the password twice:" --separator="\n" --add-password="Enter Password" --add-password="Repeat Password" || exit )"  | (smbpasswd -a -s $USER  ||   echo 1 >> $exit_status)
#If exit satus is 1, passwords are mismatched or no password was typed
if [ "$(cat $exit_status )"  == "1" ]; then
    zenity --error --title="$TITLE" --text="Error:\nPassword mismatch or\nNo Password entered."
    rm -f $exit_status
    unset LL_LIST_H
    return
fi
#Changes default user to the username selected
echo '<'$user'> = "<'$user'>"' > /etc/samba/smbusers

unset LL_LIST_H
rm -f $exit_status

zenity --question --title "$TITLE" --text "Your current /etc/samba/smb.conf will be backed up,
\n \nDo you want to continue?"
  if [ $? = 1 ];
    then
    return
  fi
 
#Copies /etc/samba/smb.conf  to /etc/samba/smb.conf.backup
cp -f /etc/samba/smb.conf /etc/samba/smb.conf.backup

#Removes original
rm -f /etc/samba/smb.conf

#Creates new empty file
touch /etc/samba/smb.conf



#Workgroup selection dialog. On cancel selects the default value
group=$(zenity --title="$TITLE" --entry --entry-text=WORKGROUP --text="Enter the name of the workgroup:" --cancel-label="Use Default")
  if [ $? = 1 ]; then
    group="WORKGROUP"
  fi



#Hostname selection dialog. On cancel selects the default value
hostname=$(zenity --title="$TITLE" --entry-text=lite --entry --text="Enter the name of your computer (hostname):" --cancel-label="Use Default")
  if [ $? = 1 ]; then
    hostname="lite"
  fi

#Writing to smb.conf starts here
echo "#======================= Global Settings ====================================" >> /etc/samba/smb.conf
echo [global] >> /etc/samba/smb.conf

echo workgroup = "$group" >> /etc/samba/smb.conf
echo server string =  Linux Lite Shares >> /etc/samba/smb.conf

echo netbios name = "$hostname" >> /etc/samba/smb.conf
echo "security = user" >> /etc/samba/smb.conf
echo "encrypt passwords = true" >> /etc/samba/smb.conf
echo "username map = /etc/samba/smbusers" >> /etc/samba/smb.conf
echo "map to guest = bad user" >> /etc/samba/smb.conf
echo "guest account = nobody" >> /etc/samba/smb.conf
echo "dns proxy = no" >> /etc/samba/smb.conf
echo "#======================= Share Definitions ===================================" >> /etc/samba/smb.conf
        if zenity --question --title="$TITLE" --text="Do you want to add a share?"; then
            add_share
        else
            restart_network
        fi
        restart_network
}

function add_user() {
#----Displays users with id greater than 1000 and less than 60000
#----Adds FALSE and TRUE (TRUE only on the first) and
#----stores everything in an aray
LL_LIST_H+=($(awk -F':' '$3>=1000 && $3<=60000 {print "FALSE", $1}' /etc/passwd |sed '0,/FALSE/s/FALSE/TRUE/'))

#User selection dialog
user="$(zenity --title="Add a user to Samba" --list --radiolist --text="Select a user name you wish to add to Samba." --column="Select" --column="Username" --width=325  --height=300  "${LL_LIST_H[@]}")"
      # Cancel
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                      return
    fi

#Exit variable for the return status from the smbpasswd that will be runing in the subshell
exit_status=$(mktemp /tmp/XXXXXX)

echo -e "$(zenity --forms --title="Enter Password" --text="Enter the password twice:" --separator="\n" --add-password="Enter Password" --add-password="Repeat Password" || exit )"  | (smbpasswd -a -s $user  ||   echo 1 >> $exit_status)

if [ "$(cat $exit_status )"  == "1" ]; then
    zenity --error --title="$TITLE" --text="Error:\nPassword mismatch or\nNo Password entered."
    rm -f $exit_status
    unset LL_LIST_H
    return
fi
rm -f $exit_status

#Adds user to the list in /etc/samba/smbusers
echo '<'$user'> = "<'$user'>"' >> /etc/samba/smbusers

unset LL_LIST_H
return
}

#Function that adds share
function add_share() {
#Share description input
share=$(zenity --title="$TITLE" --entry --text="Enter a short name for the share with no blank spaces in the name. \n Examples: music or my-music ")
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                      return
    fi
   
    #If the user entered special chars, abort
    if [[ "$share" =~ [^0-9a-z-] ]]; then
        zenity --warning \
        --title="" --text="Share name can contain alphanumeric characters only\nShare can not be added!"
         return
    fi

cd /home

#Path selection directory for the share
path=`python3 <<END
import tkinter
from tkinter import filedialog

root = tkinter.Tk()
root.withdraw()

dirname = filedialog.askdirectory(parent=root,initialdir="/home",title='Pick a directory')
print (dirname)
END`

    #Check if the output is a path
        echo $path | grep "/"
    if [  $? -ne 0  ]; then
                      zenity --error --text="You must select a valid directory"
                      return
    fi
       #Check if the directory exists
       if [ ! -d $path ]; then
                       zenity --error --text="You must select a valid directory"
                       return
        fi
# Starts making changes after user input

# Marks the begining of share for easy handling
echo "#share_start $share" >> /etc/samba/smb.conf
echo ["$share"] >> /etc/samba/smb.conf

echo path = "$path" >> /etc/samba/smb.conf
echo "available = yes" >> /etc/samba/smb.conf
echo "valid users = %U %G" >> /etc/samba/smb.conf
echo "write list = %U" >> /etc/samba/smb.conf
echo browsable = yes >> /etc/samba/smb.conf
echo public = no >> /etc/samba/smb.conf
if zenity --question --title="$TITLE" --text="Do you want to allow recording and change on the files?"; then
    echo writable = yes >> /etc/samba/smb.conf
else
    echo writable = no >> /etc/samba/smb.conf
fi
echo "guest ok = no" >> /etc/samba/smb.conf
echo "read only = no" >> /etc/samba/smb.conf
echo "printable = no" >> /etc/samba/smb.conf
echo "locking = no" >> /etc/samba/smb.conf
echo "strict locking = no" >> /etc/samba/smb.conf

# Marks the end of share for easy handling
echo "#share_end $share" >> /etc/samba/smb.conf
return
}

function restore_prev() {
#If the file exists offers to restore  previous network share settings
if [ -f /etc/samba/smb.conf.backup ]; then
    if zenity --question --title="$TITLE" --text="This will restore your previous network share settings.\n\nIf you proceed your current settings will be removed.\n\nDo you want to continue?"; then
        rm -f /etc/samba/smb.conf
        mv -f /etc/samba/smb.conf.backup /etc/samba/smb.conf
        return
    else
        return
    fi
else
    zenity --info --title="$TITLE" --text="No previous settings found."
    return
fi
}

function restart_network() {
#Restarts network services
stdbuf -oL /bin/bash \-c '(sudo service smbd restart && sudo service nmbd restart && sleep 3 )' 2>&1 |
stdbuf -oL sed -n -e 's/^\(.\{128\}\).*/\1/' -e '/\[*$/ s/^/# /p' -e '/\*$/ s/^/# /p' |     #sed 's/^\(.\{128\}\).*/\1/'  Display only the first 128 characters
zenity --progress --title="Restarting Network Share Services..." --pulsate \
--width=600 --auto-close

                                if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                       
                                        zenity --error \
                                        --title="Error" --text="$CONFIGURE has failed."
                                        return
                                fi

PROCEED=$(zenity --info --title="$CONFIGURE" --window-icon=/usr/share/icons/zenity-llcc.png --width="280" --text="Your settings have been applied."; echo $?)
if [ ${PROCEED} -eq 1 ]; then
    zenity --info --title="Finished" --window-icon="${INSTALL_ICON}" --text="Your settings have been applied."
    return;
else
    return
fi
}

function rem_share() {
#Check if the share doesn't exists
if [[ -z "$(sed -n '/^\#share_start/{n;p;}' /etc/samba/smb.conf)" ]];then
        zenity --warning --title="$TITLE" --text="Nothing was found for removal\n#share_start not found!"
        return
fi
#Share names selection
sharename=$(sed -n '/^\#share_start/{n;p;}' /etc/samba/smb.conf | sed 's/\[/FALSE\n/g;s/\]//g' | zenity --list --radiolist --column=Select --column="Share Name" --text="Select the shares you whish to remove.")
if [ $? = 1 ]; then
    return
fi

if [ -z $sharename ]; then
    zenity --warning --title="$TITLE" --text="Nothing was selected for removal!"
    return
fi
#remove shares
for k in $sharename; do sudo sed -i '/^\#share_start '$k'/,/^\#share_end '$k'/d' /etc/samba/smb.conf; done
return
}


while (true); do
CHOICE=$(zenity --list --radiolist --title="$TITLE" --width=350 --height=500 --column=Select --column=Task \
FALSE "Run first time setup" \
TRUE "Show Shares" \
FALSE "Add a User" \
FALSE "Add a Share" \
FALSE "Remove a Share" \
FALSE "Edit the config file" \
FALSE "Restart Network Services" \
FALSE "Restore Previous Settings" \
FALSE "Exit" )

#If canceled exit
if [ $? = 1 ]; then
    exit 0
fi
case $CHOICE in
"Run first time setup") first_time
;;
"Show Shares") zenity --text-info --width=500  --height=600 --title="Show Samba Shares" </etc/samba/smb.conf
;;
"Add a User" )add_user
;;
"Add a Share") add_share
;;
"Remove a Share") rem_share
;;
"Edit the config file" ) exo-open /etc/samba/smb.conf
;;
"Restart Network Services") restart_network
;;
"Restore Previous Settings") restore_prev
;;
"Exit") exit 0
;;
esac
done

exit 0
Reply
#2

The new version, without python as we agreed.
I've taken the liberty to add a few extra stuff.
Option to change folder permissions and the owner. Since the folder created is owned by root.
Code:
#!/bin/bash
#Samba share settings GUI for Linux Lite
#Licence GPLv2
#Command: /usr/bin/su-to-root -X -c /path/to/cnss-gui
#Depends: zenity
#

TITLE="Lite Shares"

function first_time() {
        if ! zenity --question --title="$TITLE" --text="First time setup should be only runned once!\nDo you want to continue?"; then
            return
        fi

#----Displays users with id greater than 1000 and less than 60000
#----Adds FALSE and TRUE (TRUE only on the first) and
#----stores everything in an aray
LL_LIST_H+=($(awk -F':' '$3>=1000 && $3<=60000 {print "FALSE", $1}' /etc/passwd |sed '0,/FALSE/s/FALSE/TRUE/'))

#User selection dialog
user="$(zenity --title="Add a user to Samba" --list --radiolist --text="Select a user name you wish to add to Samba." --print-column=2  --column="Select" --column="Username" --width=325  --height=300  "${LL_LIST_H[@]}")"
       # Cancel
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                      return
    fi
#Exit variable for the return status from the smbpasswd that will be runing in the subshell
exit_status=$(mktemp /tmp/XXXXXX)

echo -e "$(zenity --forms --title="$TITLE" --text="Enter the password twice:" --separator="\n" --add-password="Enter Password" --add-password="Repeat Password" || exit )"  | (smbpasswd -a -s $USER  ||   echo 1 >> $exit_status)
#If exit satus is 1, passwords are mismatched or no password was typed
if [ "$(cat $exit_status )"  == "1" ]; then
    zenity --error --title="$TITLE" --text="Error:\nPassword mismatch or\nNo Password entered."
    rm -f $exit_status
    unset LL_LIST_H
    return
fi
#Changes default user to the username selected
echo '<'$user'> = "<'$user'>"' > /etc/samba/smbusers

unset LL_LIST_H
rm -f $exit_status

zenity --question --title="$TITLE" --text "Your current /etc/samba/smb.conf will be backed up,\n \nDo you want to continue?"
  if [ $? = 1 ];
    then
    return
  fi
 
#Copies /etc/samba/smb.conf  to /etc/samba/smb.conf.backup
cp -f /etc/samba/smb.conf /etc/samba/smb.conf.backup

#Removes original
rm -f /etc/samba/smb.conf

#Creates new empty file
touch /etc/samba/smb.conf



#Workgroup selection dialog. On cancel selects the default value
group=$(zenity --title="$TITLE" --entry --entry-text=WORKGROUP --text="Enter the name of the workgroup:" --cancel-label="Use Default")
  if [ $? = 1 ]; then
    group="WORKGROUP"
  fi



#Hostname selection dialog. On cancel selects the default value
hostname=$(zenity --title="$TITLE" --entry-text=lite --entry --text="Enter the name of your computer (hostname):" --cancel-label="Use Default")
  if [ $? = 1 ]; then
    hostname="lite"
  fi

#Writing to smb.conf starts here
echo "#======================= Global Settings ====================================" >> /etc/samba/smb.conf
echo [global] >> /etc/samba/smb.conf

echo workgroup = "$group" >> /etc/samba/smb.conf
echo server string =  Linux Lite Shares >> /etc/samba/smb.conf

echo netbios name = "$hostname" >> /etc/samba/smb.conf
echo "security = user" >> /etc/samba/smb.conf
echo "encrypt passwords = true" >> /etc/samba/smb.conf
echo "username map = /etc/samba/smbusers" >> /etc/samba/smb.conf
echo "map to guest = bad user" >> /etc/samba/smb.conf
echo "guest account = nobody" >> /etc/samba/smb.conf
echo "dns proxy = no" >> /etc/samba/smb.conf
echo "#======================= Share Definitions ===================================" >> /etc/samba/smb.conf
        if zenity --question --title="$TITLE" --text="Do you want to add a share?"; then
            add_share
        else
            restart_network
        fi
        restart_network
}

function add_user() {
#----Displays users with id greater than 1000 and less than 60000
#----Adds FALSE and TRUE (TRUE only on the first) and
#----stores everything in an aray
LL_LIST_H+=($(awk -F':' '$3>=1000 && $3<=60000 {print "FALSE", $1}' /etc/passwd |sed '0,/FALSE/s/FALSE/TRUE/'))

#User selection dialog
user="$(zenity --title="Add a user to Samba" --list --radiolist --text="Select a user name you wish to add to Samba." --column="Select" --column="Username" --width=325  --height=300  "${LL_LIST_H[@]}")"
      # Cancel
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                      return
    fi

#Exit variable for the return status from the smbpasswd that will be runing in the subshell
exit_status=$(mktemp /tmp/XXXXXX)

echo -e "$(zenity --forms --title="Enter Password" --text="Enter the password twice:" --separator="\n" --add-password="Enter Password" --add-password="Repeat Password" || exit )"  | (smbpasswd -a -s $user  ||   echo 1 >> $exit_status)

if [ "$(cat $exit_status )"  == "1" ]; then
    zenity --error --title="$TITLE" --text="Error:\nPassword mismatch or\nNo Password entered."
    rm -f $exit_status
    unset LL_LIST_H
    return
fi
rm -f $exit_status

#Adds user to the list in /etc/samba/smbusers
echo '<'$user'> = "<'$user'>"' >> /etc/samba/smbusers

unset LL_LIST_H
return
}

#Function that adds share
function add_share() {
#Share description input
share=$(zenity --title="$TITLE" --entry --text="Enter a short name for the share with no blank spaces in the name. \n Examples: music or my-music ")
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                      return
    fi
   
    #If the user entered special chars, abort
    if [[ "$share" =~ [^0-9a-z-A-Z] ]]; then
        zenity --warning \
        --title="" --text="Share name can contain alphanumeric characters only\nShare can not be added!"
         return
    fi

cd /home

question_=$(zenity --list --radiolist --title="TITLE" --text="Would you like to select a folder or\ncreate a folder" --column="Select"  --column="What would you like to do?" --column="Description" TRUE "Select a folder" "Select a folder to share"  FALSE "Create a folder" "Select a folder and create a folder within")
   
        if [ $? != 0 ]; then
                      return
    fi

case $question_ in
"Select a folder")
#Path selection directory for the share
path="$(zenity --title="Select a folder to share" --file-selection --directory)"

    #Check if the output is a path
        echo "$path" | grep "/"
    if [  $? -ne 0  ]; then
                      zenity --error --text="You must select a valid directory"
                      return
    fi
       #Check if the directory exists
       if [ ! -d $path ]; then
                       zenity --error --text="You must select a valid directory"
                       return
        fi
;;
"Create a folder")
path=$(zenity --title="Select a folder" --file-selection --directory)

    #Check if the output is a path
        echo $path | grep "/"
    if [  $? -ne 0  ]; then
                      zenity --error --text="You must select a valid directory"
                      return
    fi
       
      #Check if the directory exists
       if [ ! -d $path ]; then
                       zenity --error --text="You must select a valid directory"
                       return
        fi

#cd to path
cd $path

f_name=$(zenity --title="$TITLE" --entry --entry-text="folder" --text="Enter the folder name:")

#Cancel
  if [ $? != 0 ]; then
    return
  fi
    #Check if folder name contains forbiden chars or empty
        echo "$f_name" | grep "/"
    if [  $? = 0  ]; then
                      zenity --error --text="Folder name can not contain these chars: /"
                      return
    fi
    if [  -z "$f_name"  ]; then
                      zenity --error --text="Folder name can not be empty"
                      return
    fi

mkdir "$f_name"
    if [  $? != 0  ]; then
                      zenity --error --text="Folder $f_name could not be created"
                      return
    fi
     
path="$path/$f_name"
#Check if the directory exists
       if [ ! -d "$path" ]; then
                       zenity --error --text="Folder $path could not be created"
                       return
       fi


OWNER=$(stat --printf=%U "$path")
ans=$(zenity --question --title="$TITLE" --text="Current owner of the folder $path is $OWNER\nWould you like to change the ownership?")
  if [ $? = 0 ]; then
       #----Displays users with id greater than 1000 and less than 60000
       #----Adds FALSE and TRUE (TRUE only on the first) and
       #----stores everything in an aray
       LL_LIST_H+=($(awk -F':' '$3>=1000 && $3<=60000 {print "FALSE", $1}' /etc/passwd |sed '0,/FALSE/s/FALSE/TRUE/'))

      #User selection dialog
      OWNER_Q=$(zenity --title="Add a user to Samba" --list --radiolist --text="Select a user to change the ownership or cancel." --print-column=2  --column="Select" --column="Username" --width=325  --height=300  "${LL_LIST_H[@]}")

        if [ $? = 0 ]; then
              chown $OWNER_Q:$OWNER_Q "$path"
    fi
  fi   

PERM="$(stat --printf=%A "$path")"
ans=$(zenity --question --title="$TITLE" --text="Current permissions for folder $path are $PERM\nWould you like to change the permissions?")
  if [ $? = 0 ]; then
    USER_="${PERM:0:1}"

    if [[ $USER_ = 7 ]]; then TF1="TRUE"; TF2="TRUE"; TF3="TRUE"
      fi
    if [[ $USER_ = 6 ]]; then  TF1="TRUE"; TF2="TRUE"; TF3="FALSE"
      fi
    if [[ $USER_ = 5 ]]; then TF1="TRUE"; TF2="FALSE"; TF3="TRUE"
      fi
    if [[ $USER_ = 4 ]]; then TF1="TRUE"; TF2="FALSE"; TF3="FALSE"
      fi
    if [[ $USER_ = 3 ]]; then TF1="FALSE"; TF2="TRUE"; TF3="TRUE"
      fi
    if [[ $USER_ = 2 ]]; then TF1="FALSE"; TF2="TRUE"; TF3="FALSE"
      fi
    if [[ $USER_ = 1 ]]; then TF1="FALSE"; TF2="FALSE"; TF3="TRUE"
      fi
    if [[ $USER_ = 0 ]]; then TF1="FALSE"; TF2="FALSE"; TF3="FALSE"
      fi

    GRP_="${PERM:1:1}"
    if [[ $GRP_ = 7 ]]; then TF4="TRUE"; TF5="TRUE"; TF6="TRUE"
      fi
    if [[ $GRP_ = 6 ]]; then  TF4="TRUE"; TF5="TRUE"; TF6="FALSE"
      fi
    if [[ $GRP_ = 5 ]]; then TF4="TRUE"; TF5="FALSE"; TF6="TRUE"
      fi
    if [[ $GRP_ = 4 ]]; then TF4="TRUE"; TF5="FALSE"; TF6="FALSE"
      fi
    if [[ $GRP_ = 3 ]]; then TF4="FALSE"; TF5="TRUE"; TF6="TRUE"
      fi
    if [[ $GRP_ = 2 ]]; then TF4="FALSE"; TF5="TRUE"; TF6="FALSE"
      fi
    if [[ $GRP_ = 1 ]]; then TF4="FALSE"; TF5="FALSE"; TF6="TRUE"
      fi
    if [[ $GRP_ = 0 ]]; then TF4="FALSE"; TF5="FALSE"; TF6="FALSE"
      fi

    ALL_="${PERM:2:1}"
    if [[ $ALL_ = 7 ]]; then TF7="TRUE"; TF8="TRUE"; TF9="TRUE"
      fi
    if [[ $ALL_ = 6 ]]; then  TF7="TRUE"; TF8="TRUE"; TF9="FALSE"
      fi
    if [[ $ALL_ = 5 ]]; then TF7="TRUE"; TF8="FALSE"; TF9="TRUE"
      fi
    if [[ $ALL_ = 4 ]]; then TF7="TRUE"; TF8="FALSE"; TF9="FALSE"
      fi
    if [[ $ALL_ = 3 ]]; then TF7="FALSE"; TF8="TRUE"; TF9="TRUE"
      fi
    if [[ $ALL_ = 2 ]]; then TF7="FALSE"; TF8="TRUE"; TF9="FALSE"
      fi
    if [[ $ALL_ = 1 ]]; then TF7="FALSE"; TF8="FALSE"; TF9="TRUE"
      fi
    if [[ $ALL_ = 0 ]]; then TF7="FALSE"; TF8="FALSE"; TF9="FALSE"
      fi

    STAT_="${PERM:3:13}"
    ans=$(zenity  --height=450 --width=350 --list  --text "Folder:\n<b>$path</b>\nPermissions:\n<b>${STAT_}</b>\nDo you watn to change folder permissions?" --checklist  --column "pick" --column "options" \
    "$TF1" "user-read" "$TF2" "user-write" "$TF3" "user-exec" "$TF4" "group-read" "$TF5" "group-write" "$TF6" "group-exec" "$TF7" "all-read" "$TF8" "all-write" "$TF9" "all-exec" --separator=":")
    if [ "$ans" != "" ]; then
        searchuserread="user-read"
        searchuserwrite="user-write"
        searchuserexec="user-exec"
        user1="0"
        user2="0"
        user3="0"
        searchgroupread="group-read"
        searchgroupwrite="group-write"
        searchgroupexec="group-exec"
        group1="0"
        group2="0"
        group3="0"
        searchallread="all-read"
        searchallwrite="all-write"
        searchallexec="all-exec"
        all1="0"
        all2="0"
        all3="0"

        case $ans in  *"$searchuserread"*)
            user1="4" ;;
        esac

        case $ans in  *"$searchuserwrite"*)
            user2="2" ;;
        esac

        case $ans in  *"$searchuserexec"*)
            user3="1" ;;
        esac

        case $ans in  *"$searchgroupread"*)
            group1="4" ;;
        esac

        case $ans in  *"$searchgroupwrite"*)
            group2="2" ;;
        esac

        case $ans in  *"$searchgroupexec"*)
            group3="1" ;;
        esac

        case $ans in  *"$searchallread"*)
            all1="4" ;;
        esac

        case $ans in  *"$searchallwrite"*)
            all2="2" ;;
        esac

        case $ans in  *"$searchallexec"*)
            all3="1" ;;
        esac

        u=$(($user1 + $user2 + $user3))
        g=$(($group1 + $group2 + $group3))
        a=$(($all1 + $all2 + $all3))
        result="$u$g$a"
        chmod $result "$f_name" || { zenity --error --text="An error occurred!\ncheck if you are allowed\nto change permissions\nof the selected folder"; }
    fi
  fi
;;
esac
# Starts making changes after user input

# Marks the begining of share for easy handling
echo "#share_start $share" >> /etc/samba/smb.conf
echo ["$share"] >> /etc/samba/smb.conf

echo path = "$path" >> /etc/samba/smb.conf
echo "available = yes" >> /etc/samba/smb.conf
echo "valid users = %U %G" >> /etc/samba/smb.conf
echo "write list = %U" >> /etc/samba/smb.conf
echo browsable = yes >> /etc/samba/smb.conf
echo public = no >> /etc/samba/smb.conf
if zenity --question --title="$TITLE" --text="Do you want to allow recording and change on the files?"; then
    echo writable = yes >> /etc/samba/smb.conf
else
    echo writable = no >> /etc/samba/smb.conf
fi
echo "guest ok = no" >> /etc/samba/smb.conf
echo "read only = no" >> /etc/samba/smb.conf
echo "printable = no" >> /etc/samba/smb.conf
echo "locking = no" >> /etc/samba/smb.conf
echo "strict locking = no" >> /etc/samba/smb.conf

# Marks the end of share for easy handling
echo "#share_end $share" >> /etc/samba/smb.conf
return
}

function restore_prev() {
#If the file exists offers to restore  previous network share settings
if [ -f /etc/samba/smb.conf.backup ]; then
    if zenity --question --title="$TITLE" --text="This will restore your previous network share settings.\n\nIf you proceed your current settings will be removed.\n\nDo you want to continue?"; then
        rm -f /etc/samba/smb.conf
        mv -f /etc/samba/smb.conf.backup /etc/samba/smb.conf
        return
    else
        return
    fi
else
    zenity --info --title="$TITLE" --text="No previous settings found."
    return
fi
}

function restart_network() {
#Restarts network services
stdbuf -oL /bin/bash \-c '(sudo service smbd restart && sudo service nmbd restart && sleep 3 )' 2>&1 |
stdbuf -oL sed -n -e 's/^\(.\{128\}\).*/\1/' -e '/\[*$/ s/^/# /p' -e '/\*$/ s/^/# /p' |     #sed 's/^\(.\{128\}\).*/\1/'  Display only the first 128 characters
zenity --progress --title="Restarting Network Share Services..." --pulsate \
--width=600 --auto-close

                                if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                       
                                        zenity --error \
                                        --title="Error" --text="$CONFIGURE has failed."
                                        return
                                fi

PROCEED=$(zenity --info --title="$CONFIGURE" --window-icon=/usr/share/icons/zenity-llcc.png --width="280" --text="Your settings have been applied."; echo $?)
if [ ${PROCEED} -eq 1 ]; then
    zenity --info --title="Finished" --window-icon="${INSTALL_ICON}" --text="Your settings have been applied."
    return;
else
    return
fi
}

function rem_share() {
#Check if the share doesn't exists
if [[ -z "$(sed -n '/^\#share_start/{n;p;}' /etc/samba/smb.conf)" ]];then
        zenity --warning --title="$TITLE" --text="Nothing was found for removal\n#share_start not found!"
        return
fi
#Share names selection
sharename=$(sed -n '/^\#share_start/{n;p;}' /etc/samba/smb.conf | sed 's/\[/FALSE\n/g;s/\]//g' | zenity --list --radiolist --column=Select --column="Share Name" --text="Select the shares you whish to remove.")
if [ $? = 1 ]; then
    return
fi

if [ -z $sharename ]; then
    zenity --warning --title="$TITLE" --text="Nothing was selected for removal!"
    return
fi
#remove shares
for k in $sharename; do sudo sed -i '/^\#share_start '$k'/,/^\#share_end '$k'/d' /etc/samba/smb.conf; done
return
}


while (true); do
CHOICE=$(zenity --list --radiolist --title="$TITLE" --width=350 --height=500 --column=Select --column=Task \
FALSE "Run first time setup" \
TRUE "Show Shares" \
FALSE "Add a User" \
FALSE "Add a Share" \
FALSE "Remove a Share" \
FALSE "Edit the config file" \
FALSE "Restart Network Services" \
FALSE "Restore Previous Settings" \
FALSE "Exit" )

#If canceled exit
if [ $? = 1 ]; then
    exit 0
fi
case $CHOICE in
"Run first time setup") first_time
;;
"Show Shares") zenity --text-info --width=500  --height=600 --title="Show Samba Shares" </etc/samba/smb.conf
;;
"Add a User" )add_user
;;
"Add a Share") add_share
;;
"Remove a Share") rem_share
;;
"Edit the config file" ) exo-open /etc/samba/smb.conf
;;
"Restart Network Services") restart_network
;;
"Restore Previous Settings") restore_prev
;;
"Exit") exit 0
;;
esac
done

exit 0
This is the version without the ownership and permissions part
Code:
#!/bin/bash
#Samba share settings GUI for Linux Lite
#Licence GPLv2
#Command: /usr/bin/su-to-root -X -c /path/to/cnss-gui
#Depends: zenity
#

TITLE="Lite Shares"

function first_time() {
        if ! zenity --question --title="$TITLE" --text="First time setup should be only runned once!\nDo you want to continue?"; then
            return
        fi

#----Displays users with id greater than 1000 and less than 60000
#----Adds FALSE and TRUE (TRUE only on the first) and
#----stores everything in an aray
LL_LIST_H+=($(awk -F':' '$3>=1000 && $3<=60000 {print "FALSE", $1}' /etc/passwd |sed '0,/FALSE/s/FALSE/TRUE/'))

#User selection dialog
user="$(zenity --title="Add a user to Samba" --list --radiolist --text="Select a user name you wish to add to Samba." --print-column=2  --column="Select" --column="Username" --width=325  --height=300  "${LL_LIST_H[@]}")"
       # Cancel
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                      return
    fi
#Exit variable for the return status from the smbpasswd that will be runing in the subshell
exit_status=$(mktemp /tmp/XXXXXX)

echo -e "$(zenity --forms --title="$TITLE" --text="Enter the password twice:" --separator="\n" --add-password="Enter Password" --add-password="Repeat Password" || exit )"  | (smbpasswd -a -s $USER  ||   echo 1 >> $exit_status)
#If exit satus is 1, passwords are mismatched or no password was typed
if [ "$(cat $exit_status )"  == "1" ]; then
    zenity --error --title="$TITLE" --text="Error:\nPassword mismatch or\nNo Password entered."
    rm -f $exit_status
    unset LL_LIST_H
    return
fi
#Changes default user to the username selected
echo '<'$user'> = "<'$user'>"' > /etc/samba/smbusers

unset LL_LIST_H
rm -f $exit_status

zenity --question --title="$TITLE" --text "Your current /etc/samba/smb.conf will be backed up,\n \nDo you want to continue?"
  if [ $? = 1 ];
    then
    return
  fi
 
#Copies /etc/samba/smb.conf  to /etc/samba/smb.conf.backup
cp -f /etc/samba/smb.conf /etc/samba/smb.conf.backup

#Removes original
rm -f /etc/samba/smb.conf

#Creates new empty file
touch /etc/samba/smb.conf



#Workgroup selection dialog. On cancel selects the default value
group=$(zenity --title="$TITLE" --entry --entry-text=WORKGROUP --text="Enter the name of the workgroup:" --cancel-label="Use Default")
  if [ $? = 1 ]; then
    group="WORKGROUP"
  fi



#Hostname selection dialog. On cancel selects the default value
hostname=$(zenity --title="$TITLE" --entry-text=lite --entry --text="Enter the name of your computer (hostname):" --cancel-label="Use Default")
  if [ $? = 1 ]; then
    hostname="lite"
  fi

#Writing to smb.conf starts here
echo "#======================= Global Settings ====================================" >> /etc/samba/smb.conf
echo [global] >> /etc/samba/smb.conf

echo workgroup = "$group" >> /etc/samba/smb.conf
echo server string =  Linux Lite Shares >> /etc/samba/smb.conf

echo netbios name = "$hostname" >> /etc/samba/smb.conf
echo "security = user" >> /etc/samba/smb.conf
echo "encrypt passwords = true" >> /etc/samba/smb.conf
echo "username map = /etc/samba/smbusers" >> /etc/samba/smb.conf
echo "map to guest = bad user" >> /etc/samba/smb.conf
echo "guest account = nobody" >> /etc/samba/smb.conf
echo "dns proxy = no" >> /etc/samba/smb.conf
echo "#======================= Share Definitions ===================================" >> /etc/samba/smb.conf
        if zenity --question --title="$TITLE" --text="Do you want to add a share?"; then
            add_share
        else
            restart_network
        fi
        restart_network
}

function add_user() {
#----Displays users with id greater than 1000 and less than 60000
#----Adds FALSE and TRUE (TRUE only on the first) and
#----stores everything in an aray
LL_LIST_H+=($(awk -F':' '$3>=1000 && $3<=60000 {print "FALSE", $1}' /etc/passwd |sed '0,/FALSE/s/FALSE/TRUE/'))

#User selection dialog
user="$(zenity --title="Add a user to Samba" --list --radiolist --text="Select a user name you wish to add to Samba." --column="Select" --column="Username" --width=325  --height=300  "${LL_LIST_H[@]}")"
      # Cancel
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                      return
    fi

#Exit variable for the return status from the smbpasswd that will be runing in the subshell
exit_status=$(mktemp /tmp/XXXXXX)

echo -e "$(zenity --forms --title="Enter Password" --text="Enter the password twice:" --separator="\n" --add-password="Enter Password" --add-password="Repeat Password" || exit )"  | (smbpasswd -a -s $user  ||   echo 1 >> $exit_status)

if [ "$(cat $exit_status )"  == "1" ]; then
    zenity --error --title="$TITLE" --text="Error:\nPassword mismatch or\nNo Password entered."
    rm -f $exit_status
    unset LL_LIST_H
    return
fi
rm -f $exit_status

#Adds user to the list in /etc/samba/smbusers
echo '<'$user'> = "<'$user'>"' >> /etc/samba/smbusers

unset LL_LIST_H
return
}

#Function that adds share
function add_share() {
#Share description input
share=$(zenity --title="$TITLE" --entry --text="Enter a short name for the share with no blank spaces in the name. \n Examples: music or my-music ")
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                      return
    fi
   
    #If the user entered special chars, abort
    if [[ "$share" =~ [^0-9a-z-A-Z] ]]; then
        zenity --warning \
        --title="" --text="Share name can contain alphanumeric characters only\nShare can not be added!"
         return
    fi

cd /home

question_=$(zenity --list --radiolist --title="TITLE" --text="Would you like to select a folder or\ncreate a folder" --column="Select"  --column="What would you like to do?" --column="Description" TRUE "Select a folder" "Select a folder to share"  FALSE "Create a folder" "Select a folder and create a folder within")
   
        if [ $? != 0 ]; then
                      return
    fi

case $question_ in
"Select a folder")
#Path selection directory for the share
path="$(zenity --title="Select a folder to share" --file-selection --directory)"

    #Check if the output is a path
        echo "$path" | grep "/"
    if [  $? -ne 0  ]; then
                      zenity --error --text="You must select a valid directory"
                      return
    fi
       #Check if the directory exists
       if [ ! -d $path ]; then
                       zenity --error --text="You must select a valid directory"
                       return
        fi
;;
"Create a folder")
path=$(zenity --title="Select a folder" --file-selection --directory)

    #Check if the output is a path
        echo $path | grep "/"
    if [  $? -ne 0  ]; then
                      zenity --error --text="You must select a valid directory"
                      return
    fi
       
      #Check if the directory exists
       if [ ! -d $path ]; then
                       zenity --error --text="You must select a valid directory"
                       return
        fi

#cd to path
cd $path

f_name=$(zenity --title="$TITLE" --entry --entry-text="folder" --text="Enter the folder name:")

#Cancel
  if [ $? != 0 ]; then
    return
  fi
    #Check if folder name contains forbiden chars or empty
        echo "$f_name" | grep "/"
    if [  $? = 0  ]; then
                      zenity --error --text="Folder name can not contain these chars: /"
                      return
    fi
    if [  -z "$f_name"  ]; then
                      zenity --error --text="Folder name can not be empty"
                      return
    fi

mkdir "$f_name"
    if [  $? != 0  ]; then
                      zenity --error --text="Folder $f_name could not be created"
                      return
    fi
     
path="$path/$f_name"
#Check if the directory exists
       if [ ! -d "$path" ]; then
                       zenity --error --text="Folder $path could not be created"
                       return
       fi
;;
esac
# Starts making changes after user input

# Marks the begining of share for easy handling
echo "#share_start $share" >> /etc/samba/smb.conf
echo ["$share"] >> /etc/samba/smb.conf

echo path = "$path" >> /etc/samba/smb.conf
echo "available = yes" >> /etc/samba/smb.conf
echo "valid users = %U %G" >> /etc/samba/smb.conf
echo "write list = %U" >> /etc/samba/smb.conf
echo browsable = yes >> /etc/samba/smb.conf
echo public = no >> /etc/samba/smb.conf
if zenity --question --title="$TITLE" --text="Do you want to allow recording and change on the files?"; then
    echo writable = yes >> /etc/samba/smb.conf
else
    echo writable = no >> /etc/samba/smb.conf
fi
echo "guest ok = no" >> /etc/samba/smb.conf
echo "read only = no" >> /etc/samba/smb.conf
echo "printable = no" >> /etc/samba/smb.conf
echo "locking = no" >> /etc/samba/smb.conf
echo "strict locking = no" >> /etc/samba/smb.conf

# Marks the end of share for easy handling
echo "#share_end $share" >> /etc/samba/smb.conf
return
}

function restore_prev() {
#If the file exists offers to restore  previous network share settings
if [ -f /etc/samba/smb.conf.backup ]; then
    if zenity --question --title="$TITLE" --text="This will restore your previous network share settings.\n\nIf you proceed your current settings will be removed.\n\nDo you want to continue?"; then
        rm -f /etc/samba/smb.conf
        mv -f /etc/samba/smb.conf.backup /etc/samba/smb.conf
        return
    else
        return
    fi
else
    zenity --info --title="$TITLE" --text="No previous settings found."
    return
fi
}

function restart_network() {
#Restarts network services
stdbuf -oL /bin/bash \-c '(sudo service smbd restart && sudo service nmbd restart && sleep 3 )' 2>&1 |
stdbuf -oL sed -n -e 's/^\(.\{128\}\).*/\1/' -e '/\[*$/ s/^/# /p' -e '/\*$/ s/^/# /p' |     #sed 's/^\(.\{128\}\).*/\1/'  Display only the first 128 characters
zenity --progress --title="Restarting Network Share Services..." --pulsate \
--width=600 --auto-close

                                if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                       
                                        zenity --error \
                                        --title="Error" --text="$CONFIGURE has failed."
                                        return
                                fi

PROCEED=$(zenity --info --title="$CONFIGURE" --window-icon=/usr/share/icons/zenity-llcc.png --width="280" --text="Your settings have been applied."; echo $?)
if [ ${PROCEED} -eq 1 ]; then
    zenity --info --title="Finished" --window-icon="${INSTALL_ICON}" --text="Your settings have been applied."
    return;
else
    return
fi
}

function rem_share() {
#Check if the share doesn't exists
if [[ -z "$(sed -n '/^\#share_start/{n;p;}' /etc/samba/smb.conf)" ]];then
        zenity --warning --title="$TITLE" --text="Nothing was found for removal\n#share_start not found!"
        return
fi
#Share names selection
sharename=$(sed -n '/^\#share_start/{n;p;}' /etc/samba/smb.conf | sed 's/\[/FALSE\n/g;s/\]//g' | zenity --list --radiolist --column=Select --column="Share Name" --text="Select the shares you whish to remove.")
if [ $? = 1 ]; then
    return
fi

if [ -z $sharename ]; then
    zenity --warning --title="$TITLE" --text="Nothing was selected for removal!"
    return
fi
#remove shares
for k in $sharename; do sudo sed -i '/^\#share_start '$k'/,/^\#share_end '$k'/d' /etc/samba/smb.conf; done
return
}


while (true); do
CHOICE=$(zenity --list --radiolist --title="$TITLE" --width=350 --height=500 --column=Select --column=Task \
FALSE "Run first time setup" \
TRUE "Show Shares" \
FALSE "Add a User" \
FALSE "Add a Share" \
FALSE "Remove a Share" \
FALSE "Edit the config file" \
FALSE "Restart Network Services" \
FALSE "Restore Previous Settings" \
FALSE "Exit" )

#If canceled exit
if [ $? = 1 ]; then
    exit 0
fi
case $CHOICE in
"Run first time setup") first_time
;;
"Show Shares") zenity --text-info --width=500  --height=600 --title="Show Samba Shares" </etc/samba/smb.conf
;;
"Add a User" )add_user
;;
"Add a Share") add_share
;;
"Remove a Share") rem_share
;;
"Edit the config file" ) exo-open /etc/samba/smb.conf
;;
"Restart Network Services") restart_network
;;
"Restore Previous Settings") restore_prev
;;
"Exit") exit 0
;;
esac
done

exit 0
Reply
#3

Newbies won't understand the permission system. I like the 2nd script for this reason.

We need to eliminate this dialogue:

[Image: 5ZWpRu4.png]

and this:

[Image: 4vIbaoT.png]

This share is created after following all the steps:

[Image: x5ymB9A.png]




Download your free copy of Linux Lite today.

Jerry Bezencon
Linux Lite Creator

"Do not correct a fool, or he will hate you; correct a wise man and he will appreciate you."

[Image: X5qGkCg.png]

[Image: 0op1GNe.png] [Image: LgJ2mtP.png] [Image: vLZcFUE.png] [Image: lrUHro3.jpg]
Reply
#4

In simple terms, this is what the process should be for Add a Share:

1) Radiolist appears as below:

[Image: z08yIah.png]

2) If Create a folder is selected, the file selection dialogue opens, allowing user to create a folder, bash script reads this choice.

2a) If Select a folder is selected, user selects a pre-existing folder, bash script reads this choice.

3) Remove the Do you want to allow recording and change on the file? dialogue question. Just give read/write by owner by default.

4) Return to main Lite Shares dialogue.

Download your free copy of Linux Lite today.

Jerry Bezencon
Linux Lite Creator

"Do not correct a fool, or he will hate you; correct a wise man and he will appreciate you."

[Image: X5qGkCg.png]

[Image: 0op1GNe.png] [Image: LgJ2mtP.png] [Image: vLZcFUE.png] [Image: lrUHro3.jpg]
Reply
#5

I'll see what I can do. It's almost 1am I'll be off to bed now. :)
Reply
#6

Just had a thought, need to add Set Workgroup name to main dialogue.

Download your free copy of Linux Lite today.

Jerry Bezencon
Linux Lite Creator

"Do not correct a fool, or he will hate you; correct a wise man and he will appreciate you."

[Image: X5qGkCg.png]

[Image: 0op1GNe.png] [Image: LgJ2mtP.png] [Image: vLZcFUE.png] [Image: lrUHro3.jpg]
Reply
#7

Made some progress
cnss-gui
Code:
#!/bin/bash
#Samba share settings GUI for Linux Lite
#Licence GPLv2
#Command: /usr/bin/su-to-root -X -c /path/to/cnss-gui
#Depends: zenity
#

TITLE="Lite Shares"

function first_time() {
        if ! zenity --question --title="$TITLE" --text="First time setup should be only runned once!\nYour current /etc/samba/smb.conf will be backed up,\nDo you want to continue?"; then
            return
        fi

#----Displays users with id greater than 1000 and less than 60000
#----Adds FALSE and TRUE (TRUE only on the first) and
#----stores everything in an aray
LL_LIST_H+=($(awk -F':' '$3>=1000 && $3<=60000 {print "FALSE", $1}' /etc/passwd |sed '0,/FALSE/s/FALSE/TRUE/'))

#User selection dialog
user="$(zenity --title="Add a user to Samba" --list --radiolist --text="Select a user name you wish to add to Samba." --column="Select" --column="Username" --width=325  --height=300  "${LL_LIST_H[@]}")"
      # Cancel
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                      unset LL_LIST_H
                      return
    fi

unset LL_LIST_H

#Workgroup selection dialog. On cancel selects the default value
group=$(zenity --title="$TITLE" --entry --entry-text=WORKGROUP --text="Enter the name of the workgroup:" --cancel-label="Use Default")
  if [ $? = 1 ]; then
    group="WORKGROUP"
  fi



#Hostname selection dialog. On cancel selects the default value
hostname=$(zenity --title="$TITLE" --entry-text=lite --entry --text="Enter the name of your computer (hostname):" --cancel-label="Use Default")
  if [ $? = 1 ]; then
    hostname="lite"
  fi

if zenity --question --text="You selected:\nWorkgroup: <b>$group</b>\nHostname: <b>$hostname</b>\nUsername:<b>$user</b>\n Do you want to save?"; then
    #Ask for password, pass variables and run the script
    gksudo -m "You need to provide your password in order to add users." --  env user="$user" group="$group" hostname="$hostname" /usr/scripts/samba/cnss-first-time
fi

}

function add_user() {
#----Displays users with id greater than 1000 and less than 60000
#----Adds FALSE and TRUE (TRUE only on the first) and
#----stores everything in an aray
LL_LIST_H+=($(awk -F':' '$3>=1000 && $3<=60000 {print "FALSE", $1}' /etc/passwd |sed '0,/FALSE/s/FALSE/TRUE/'))

#User selection dialog
user="$(zenity --title="Add a user to Samba" --list --radiolist --text="Select a user name you wish to add to Samba." --column="Select" --column="Username" --width=325  --height=300  "${LL_LIST_H[@]}")"
      # Cancel
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                      unset LL_LIST_H
                      return
    fi


#Ask for password, pass variables and run the script
gksudo -m "You need to provide your password in order to add users." --  env user="$user" /usr/scripts/samba/cnss-add-user

unset LL_LIST_H
return
}

#Function that adds share
function add_share() {
#Share description input
share=$(zenity --title="$TITLE" --entry --text="Enter a short name for the share with no blank spaces in the name. \n Examples: music or my-music ")
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                      return
    fi
   
    #If the user entered special chars, abort
    if [[ "$share" =~ [^0-9a-z-A-Z] ]]; then
        zenity --warning \
        --title="" --text="Share name can contain alphanumeric characters only\nShare can not be added!"
         return
    fi

conf=$(cat /etc/samba/smb.conf | grep "^\[$share\]")
if [[ ! -z "$conf" ]]; then
        zenity --warning \
        --title="" --text="Share name allready exists\nShare can not be added!"
         return
fi

#Path selection directory for the share
path="$(zenity --title="Select a folder to share" --file-selection --directory)"

    #Check if the output is a path
        echo "$path" | grep "/"
    if [  $? -ne 0  ]; then
                      zenity --error --text="You must select a valid directory"
                      return
    fi
       #Check if the directory exists
       if [ ! -d $path ]; then
                       zenity --error --text="You must select a valid directory"
                       return
        fi



gksudo -m "You need to provide your password in order to add shares to SAMBA." --  env path="$path" share="$share" /bin/bash -c '
    # Marks the begining of share for easy handling
    echo "#share_start $share" >> /etc/samba/smb.conf
    echo ["$share"] >> /etc/samba/smb.conf

    echo path = "$path" >> /etc/samba/smb.conf
    echo "available = yes" >> /etc/samba/smb.conf
    echo "valid users = %U %G" >> /etc/samba/smb.conf
    echo "write list = %U" >> /etc/samba/smb.conf
    echo browsable = yes >> /etc/samba/smb.conf
    echo public = no >> /etc/samba/smb.conf
    echo writable = yes >> /etc/samba/smb.conf

    echo "guest ok = no" >> /etc/samba/smb.conf
    echo "read only = no" >> /etc/samba/smb.conf
    echo "printable = no" >> /etc/samba/smb.conf
    echo "locking = no" >> /etc/samba/smb.conf
    echo "strict locking = no" >> /etc/samba/smb.conf

    # Marks the end of share for easy handling
    echo "#share_end $share" >> /etc/samba/smb.conf'

return
}

function restore_prev() {
#If the file exists offers to restore  previous network share settings
if [ -f /etc/samba/smb.conf.backup ]; then
    gksudo -m "You need to provide your password for restoring previous netwotk share settings." -- /bin/bash -c '
    if zenity --question --title="$TITLE" --text="This will restore your previous network share settings.\n\nIf you proceed your current settings will be backed up.\n\nDo you want to continue?"; then
        mv -f /etc/samba/smb.conf /etc/samba/smb.conf.backup_
        mv -f /etc/samba/smb.conf.backup /etc/samba/smb.conf
            mv -f /etc/samba/smb.conf.backup_  /etc/samba/smb.conf.backup
        exit
    else
        exit
    fi'
else
    zenity --info --title="$TITLE" --text="No previous settings found."
    return
fi
}

function restart_network() {
#Restarts network services
gksudo -m "You need to provide your password in order to restart network services." --   /usr/scripts/samba/restart_network

return
}

function rem_share() {
#Check if the share doesn't exists
if [[ -z "$(sed -n '/^\#share_start/{n;p;}' /etc/samba/smb.conf)" ]];then
        zenity --warning --title="$TITLE" --text="Nothing was found for removal\n#share_start not found!"
        return
fi
#Share names selection
sharename=$(sed -n '/^\#share_start/{n;p;}' /etc/samba/smb.conf | sed 's/\[/FALSE\n/g;s/\]//g' | zenity --list --radiolist --column=Select --column="Share Name" --text="Select the shares you whish to remove.")
if [ $? = 1 ]; then
    return
fi

if [ -z $sharename ]; then
    zenity --warning --title="$TITLE" --text="Nothing was selected for removal!"
    return
fi

#remove shares
gksudo -m "You need to provide your password in order to remove network shares." --  env sharename="$sharename" /bin/bash -c '
               for k in $sharename; do sudo sed -i '/^\#share_start '$k'/,/^\#share_end '$k'/d' /etc/samba/smb.conf; done
'
return
}


while (true); do
CHOICE=$(zenity --list --radiolist --title="$TITLE" --width=350 --height=500 --column=Select --column=Task \
FALSE "Run first time setup" \
TRUE "Show Shares" \
FALSE "Add a User" \
FALSE "Add a Share" \
FALSE "Remove a Share" \
FALSE "Edit the config file" \
FALSE "Restart Network Services" \
FALSE "Restore Previous Settings" \
FALSE "Exit" )

#If canceled exit
if [ $? = 1 ]; then
    exit 0
fi
case $CHOICE in
"Run first time setup") first_time
;;
"Show Shares") zenity --text-info --width=500  --height=600 --title="Show Samba Shares" </etc/samba/smb.conf
;;
"Add a User" )add_user
;;
"Add a Share") add_share
;;
"Remove a Share") rem_share
;;
"Edit the config file" ) gksudo -m "You need to provide your password in order to edit network shares." --  env sharename="$sharename" /bin/bash -c 'exo-open /etc/samba/smb.conf'
;;
"Restart Network Services") restart_network
;;
"Restore Previous Settings") restore_prev
;;
"Exit") exit 0
;;
esac
done

exit 0
/usr/scripts/samba/cnss-add-user
Code:
#!/bin/bash
    #Exit variable for the return status from the smbpasswd that will be runing in the subshell
    exit_status=$(mktemp /tmp/XXXXXX)

    echo -e "$(zenity --forms --title="Enter Password" --text="Enter the password for SAMBA twice.\nNote that this is not your" --separator="\n" --add-password="Enter Password" --add-password="Repeat Password" || exit )" | (smbpasswd -a -s $user  ||   echo 1 >> $exit_status)

    if [ "$(cat $exit_status )"  == "1" ]; then
        zenity --error --title="$TITLE" --text="Error:\nPassword mismatch or\nNo Password entered."
        rm -f $exit_status
        exit
    fi
    rm -f $exit_status

    #Adds user to the list in /etc/samba/smbusers
    echo '<'$user'> = "<'$user'>"' >> /etc/samba/smbusers
/usr/scripts/samba/cnss-first-time
Code:
#!/bin/bash
TITLE="Lite Shares"
function adduser () {
    #Exit variable for the return status from the smbpasswd that will be runing in the subshell
    exit_status=$(mktemp /tmp/XXXXXX)

    echo -e "$(zenity --forms --title="Enter Password" --text="Enter the password for SAMBA twice.\nNote that this is not your administrator password" --separator="\n" --add-password="Enter Password" --add-password="Repeat Password" || exit )" | (smbpasswd -a -s $user  ||   echo 1 >> $exit_status)

    if [ "$(cat $exit_status )"  == "1" ]; then
        zenity --error --title="$TITLE" --text="Error:\nPassword mismatch or\nNo Password entered."
        rm -f $exit_status
        exit
    fi
    rm -f $exit_status

    #Adds user to the list in /etc/samba/smbusers
    echo '<'$user'> = "<'$user'>"' > /etc/samba/smbusers
}

function write_conf () {
#Writing to smb.conf starts here
echo "#======================= Global Settings ====================================" >> /etc/samba/smb.conf
echo [global] >> /etc/samba/smb.conf

echo workgroup = "$group" >> /etc/samba/smb.conf
echo server string =  Linux Lite Shares >> /etc/samba/smb.conf

echo netbios name = "$hostname" >> /etc/samba/smb.conf
echo "security = user" >> /etc/samba/smb.conf
echo "encrypt passwords = true" >> /etc/samba/smb.conf
echo "username map = /etc/samba/smbusers" >> /etc/samba/smb.conf
echo "map to guest = bad user" >> /etc/samba/smb.conf
echo "guest account = nobody" >> /etc/samba/smb.conf
echo "dns proxy = no" >> /etc/samba/smb.conf
echo "#======================= Share Definitions ===================================" >> /etc/samba/smb.conf
}

function add_share() {
#Share description input
share=$(zenity --title="$TITLE" --entry --text="Enter a short name for the share with no blank spaces in the name. \n Examples: music or my-music ")
    if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                      return
    fi
    
    #If the user entered special chars, abort
    if [[ "$share" =~ [^0-9a-z-A-Z] ]]; then
        zenity --warning \
        --title="" --text="Share name can contain alphanumeric characters only\nShare can not be added!"
         return
    fi


#Path selection directory for the share
path="$(zenity --title="Select a folder to share" --file-selection --directory)"

    #Check if the output is a path
        echo "$path" | grep "/"
    if [  $? -ne 0  ]; then
                      zenity --error --text="You must select a valid directory\nShare will not be added"
                      return
    fi
       #Check if the directory exists
       if [ ! -d $path ]; then
                       zenity --error --text="You must select a valid directory\nShare will not be added"
                       return
        fi

    # Marks the begining of share for easy handling
    echo "#share_start $share" >> /etc/samba/smb.conf
    echo ["$share"] >> /etc/samba/smb.conf

    echo path = "$path" >> /etc/samba/smb.conf
    echo "available = yes" >> /etc/samba/smb.conf
    echo "valid users = %U %G" >> /etc/samba/smb.conf
    echo "write list = %U" >> /etc/samba/smb.conf
    echo browsable = yes >> /etc/samba/smb.conf
    echo public = no >> /etc/samba/smb.conf
    echo writable = yes >> /etc/samba/smb.conf

    echo "guest ok = no" >> /etc/samba/smb.conf
    echo "read only = no" >> /etc/samba/smb.conf
    echo "printable = no" >> /etc/samba/smb.conf
    echo "locking = no" >> /etc/samba/smb.conf
    echo "strict locking = no" >> /etc/samba/smb.conf

    # Marks the end of share for easy handling
    echo "#share_end $share" >> /etc/samba/smb.conf

return
}

adduser

#Copies /etc/samba/smb.conf  to /etc/samba/smb.conf.backup
cp -f /etc/samba/smb.conf /etc/samba/smb.conf.backup

#Removes original
rm -f /etc/samba/smb.conf

#Creates new empty file
touch /etc/samba/smb.conf

write_conf

if zenity --question --title="$TITLE" --text="Do you want to add a share?"; then
       add_share
fi
    /usr/scripts/samba/restart_network
/usr/scripts/samba/restart_network
Code:
#!/bin/bash
         TITLE="Lite Shares"
         stdbuf -oL /bin/bash \-c '(sudo service smbd restart && sudo service nmbd restart && sleep 3 )' 2>&1 |
         stdbuf -oL sed -n -e  's/^\(.\{128\}\).*/\1/' -e '/\[*$/ s/^/# /p' -e '/\*$/ s/^/# /p' |
         zenity --progress --title="Restarting Network Share Services..." --pulsate --width=600 --auto-close
                                if [ "${PIPESTATUS[0]}" -ne "0" ]; then
                       
                                        zenity --error  --title="Error" --text="Restarting network services has failed."
                                        exit
                                fi
                                        zenity --info --title="$TITLE" --window-icon=/usr/share/icons/zenity-llcc.png --width="280" --text="Network services restarted succsessfully."
Reply
#8

#start and #end tags still remain
I need a command which will remove a section in a file
from this
Code:
[global]
a
b
c
[share]
d
e
f
[n-th]
x
y
z
to this
Code:
[global]
a
b
c
[n-th]
x
y
z
or this
Code:
[global]
a
b
c
[share]
d
e
f
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)