-
Notifications
You must be signed in to change notification settings - Fork 1
/
cnss-gui
274 lines (229 loc) · 9.9 KB
/
cnss-gui
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/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
#Authors: Misko_2083, Jerry Bezencon
TITLE="Lite Shares"
function first_time() {
if ! zenity --question --title="First Time Network Shares Setup" --text="First time setup should only be run once.\nYour current settings will be backed up.\n\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 the Setup" --list --radiolist --text="Select a Username you wish to add to the Setup." --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="Enter Workgroup Name" --entry --entry-text=WORKGROUP --text="Enter the name of the Workgroup.\n\nHINT: Make sure the Workgroup name you enter is\nthe same as the other networked computers." --cancel-label="Use Default")
if [ $? = 1 ]; then
group="WORKGROUP"
fi
#Hostname selection dialog. On cancel selects the default value - needs fixing, 'Use Default' does not read the system hostname
hostname=$(zenity --title="Enter your Hostname" --entry-text=myhostnamehere --entry --text="Enter the name of your computer (hostname).\n\nHINT: Your hostname appears in the Terminal\nafter the @ symbol." --cancel-label="Use Default")
if [ $? = 1 ]; then
hostname="myhostnamehere"
fi
if zenity --question --title="$TITLE" --text="Do you want to add a Share?"; then
#Share description input
share=$(zenity --title="Network Share Name" --entry --text="Enter a short name for the share. Be sure to use:\n- No blank spaces in the name.\n- Use lower case only.\n\nThis is the name of the share that will appear on the Network.\n\nExamples: share, my-share ")
if [ "${PIPESTATUS[0]}" -ne "0" ]; then
unset share
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 cannot be added!"
unset share
fi
if [ ! -z "$share" ]; then
cd ~
#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."
unset path
unset share
fi
#Check if the directory exists
if [ ! -d $path ]; then
zenity --error --text="You must select a valid directory.\nShare will not be added."
unset path
unset share
fi
fi
fi
if zenity --question --title="Confirm Settings" --text="You selected:\n\nWorkgroup: <b>$group</b>\nHostname: <b>$hostname</b>\nUsername: <b>$user</b>\nShare name: <b>$share</b>\nShare: <b>$path</b>\n\nDo you want to continue?"; 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" share="$share" path="$path" /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 Username 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
if grep '^<'$user'> = "<'$user'>"$' /etc/samba/smbusers; then
zenity --warning \
--title="$TITLE" --text="Username is already added.\nUser can not be added!\nUse Change User Password instead."
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 ch_password() {
#get list of added smb users from /etc/samba/smbusers
LL_LIST_H+=($(awk '{print "FALSE", $1}' /etc/samba/smbusers | sed 's/[<>]//g' |sed '0,/FALSE/s/FALSE/TRUE/'))
if [ -z ${LL_LIST_H[@]} ]; then
zenity --warning \
--title="$TITLE" --text="No User found.\nAdd a User first."
return
fi
#User selection dialog
user="$(zenity --title="Change Samba Password" --list --radiolist --text="Select a Username to change the password on." --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 change the Users Samba password." -- env user="$user" /usr/scripts/samba/cnss-chpasswd
unset LL_LIST_H
return
}
#Function that adds share
function add_share() {
#Share description input
share=$(zenity --title="Network Share Name" --entry --text="Enter a short name for the share. Be sure to use:\n- No blank spaces in the name.\n- Use lower case only.\n\nThis is the name of the share that will appear on the Network.\n\nExamples: share, my-share ")
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 already 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 '
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
echo -e "\n" >> /etc/samba/smb.conf'
zenity --info --title="Share Added" --text="Share Added."
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 Network 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
zenity --info --title="Restore Previous Settings" --width="260" --text="Previous Settings restored."
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() {
#remove shares
gksudo -m "You need to provide your password in order to remove Network Shares." /usr/scripts/samba/cnss-remove
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 your Shares" \
FALSE "Add a User" \
FALSE "Change User Password" \
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 your Shares") zenity --text-info --width=600 --height=450 --title="Your Network Shares" </etc/samba/smb.conf
;;
"Add a User" )add_user
;;
"Change User Password") ch_password
;;
"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