-
Notifications
You must be signed in to change notification settings - Fork 9
/
functions.sh
267 lines (232 loc) · 7.24 KB
/
functions.sh
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
function fastinstall() {
passato=${passato:-yes}
username=${username:-root}
usernameforfastinstall=$username
useradd
username=$usernameforfastinstall
sharename=${sharename:-root}
sharecomment=${sharecomment:-root directory}
sharepath=${sharepath:-/share/root}
shareadd
unset username
unset passato
}
function useradd() {
username=${username:-$1}
password=${password:-$2}
unset params[1]
unset params[2]
#Checking the username is true ?
while [[ ! $username =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9]+$|^[a-zA-Z0-9_.+-]+$ ]];
do
#if the username setted by the envoriment bypass to read username
if [ -z "$username" ]
then
read -p 'username » ' -e -i root username
fi
#Checking the username pattern
if [[ ! $username =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9]+$|^[a-zA-Z0-9_.+-]+$ ]];
then
echo 'Your username is has a unexpected char. You can write a email adress or username'
username=''
fi
done
passrandom=`< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c8`
if [ ! -z "$password" ]
then
password1="$password"
password2="$password"
else
if [ "$passato" == "yes" ]
then
password="$passrandom"
echo "Your password is $password"
password1="$password"
password2="$password"
fi
fi
passpattern="^[0-9A-z#$%^&*()\-_+={}[\]|\:;\"'<>,.?]{8,}$"
while [[ ! "$password1" == "$password2" ]] || [ -z "$password1" ] || [[ ! $password1 =~ $passpattern ]];
do
#Checking the pattern of password
while [[ ! $password1 =~ $passpattern ]];
do
#if the setted by envoriment bypass to asking password
if [ -z "$password1" ]
then
read -p "Please enter password (at least 8 character) random password $passrandom »" -s password1
echo
fi
#Checking pattern of password
if [[ ! $password1 =~ $passpattern ]]
then
echo 'Your password is contains unexpected char or does not have requirements'
password1=''
password2=''
fi
done
if [ -z "$password2" ]
then
read -p 'Please retype password » ' -s password2
echo
fi
if [[ ! $password1 == $password2 ]]
then
echo 'Your passwords do not match each other'
password1=''
password2=''
fi
done
#Adding new samba user
adduser --disabled-password $username > /dev/null 2>&1
echo "$password1
$password2" | smbpasswd -a $username
usernameforshare=$username
unset password1
unset password2
unset username
unset password
[ ! -f "/etc/samba/firstrun" ] && reload-samba
}
function shareadd() {
if [ ! -z $1 ]
then
sharename=${sharename:-$1}
sharepath=${sharepath:-$2}
username=${username:-$3}
sharecomment=${sharecomment:-$4}
fi
#To getting Share Name From User
while [[ ! $sharename =~ ^[a-zA-Z0-9._+-]+$ ]];
do
#if the username setted by the envoriment bypass to read username
if [ -z "$sharename" ]
then
read -p 'Share Name » ' -e -i root sharename
fi
#Checking the username pattern
if [[ $sharename =~ ^[a-zA-Z0-9._+-]+$ ]];
then
if [ -f "/etc/samba/conf.d/$sharename.conf" ];
then
echo "Your server configration allready located at /etc/samba/conf.d/$sharefile.conf"
echo 'Please remove this config or use a different-name'
sharename=''
fi
else
echo 'Your Share Name has a unexpected char. You can use A-z 0-9 and _- characters'
sharename=''
fi
done
#To getting Share Comment From User
while [[ ! $sharecomment =~ (^[ a-zA-Z0-9\.\-_]+)$ ]];
do
#if the username setted by the envoriment bypass to read username
if [ -z "$sharecomment" ]
then
read -p 'Share Comment » ' -e -i "$sharename Directory" sharecomment
fi
#Checking the username pattern
if [[ ! $sharecomment =~ (^[ a-zA-Z0-9\.\-_]+)$ ]];
then
echo 'Your Share comment has a unexpected char. You can use A-z 0-9 and _- characters'
sharecomment=''
fi
done
#To getting Share Path From User
while [[ ! $sharepath =~ (^[/a-zA-Z0-9._+-]+)$ ]];
do
#if the username setted by the envoriment bypass to read username
if [ -z "$sharepath" ]
then
read -p 'Share path » ' -e -i "/share/$sharename" sharepath
fi
#Checking the username pattern
if [[ ! $sharepath =~ (^[/a-zA-Z0-9._+-]+)$ ]];
then
echo 'Your Share path has a unexpected char. You can use A-z 0-9 and _- characters'
sharepath=''
fi
done
#To getting UserName From User for allowed users
while [[ ! $username =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9]+$|^[a-zA-Z0-9_.+-]+$ ]];
do
#if the username setted by the envoriment bypass to read username
if [ -z "$usernameforshare" ]
then
read -p "Valid users for $sharename » " username
else
if [ -z "$username" ]
then
read -p "Valid users for $sharename » " -e -i $usernameforshare username
fi
fi
#Checking the username pattern
if [[ ! $username =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9]+$|^[a-zA-Z0-9_.+-]+$ ]];
then
echo 'Your username is has a unexpected char. You can write a email adresss or usernames'
username=''
fi
done
#Writing share configration
if [[ $sharename =~ ^[a-zA-Z0-9._+-]+$ ]] && [ ! -z "$sharename" ] && [ ! -z "$username" ] && [ ! -z "$sharecomment" ];
then
mkdir -p $sharepath
echo "Share name » $sharename
share dir » $sharepath
share comment » $sharecomment
share valid users » $username"
echo "[$sharename]
comment = $sharecomment
path = $sharepath
read only = No
writeable = yes
browsable = yes
valid users = $username
public = no
create mask = 0640
directory mask = 0750
guest ok = no" > /etc/samba/conf.d/$sharename.conf
update-samba
else
echo "Error. Your configration file cannot saved"
fi
unset sharename
unset sharecomment
unset sharepath
unset username
unset usernameforshare
}
function sharedel() {
sharename=${sharename:-$1}
#Checking file is exist end does not have a dangerous character like a "*"
if [ -f "/etc/samba/conf.d/$sharename.conf" ] && [[ $sharename =~ ^[a-zA-Z0-9._+-]+$ ]] ;
then
echo "Deleting file /etc/samba/conf.d/$sharename.conf"
rm /etc/samba/conf.d/$sharename.conf
update-samba
else
echo "$sharename is not exist in config location"
fi
}
function sharelist() {
ls -1 /etc/samba/conf.d/ | sed -e 's/\.conf$//'
}
function shareshow() {
if [ -f "/etc/samba/conf.d/$1.conf" ] && [[ $1 =~ ^[a-zA-Z0-9._+-]+$ ]] ;
then
cat /etc/samba/conf.d/$1.conf
else
echo "This config not exist"
fi
}
function update-samba() {
echo "Re generating config file"
cat /etc/samba/smb-base.conf > /etc/samba/smb.conf
cat /etc/samba/conf.d/*.conf >> /etc/samba/smb.conf
reload-samba
}
function reload-samba() {
echo "Reloading samba service"
smbcontrol all reload-config
}