forked from tbaldur/cyberpanel-mods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixperms.sh
455 lines (381 loc) · 12.3 KB
/
fixperms.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/usr/bin/env bash
## Author: Michael Ramsey
## Objective Fix permissions issues on CyberPanel/cPanel/Plesk for a linux user or users
# https://gitlab.com/wizardassistantscripts/fixperms
#
# Forked from https://github.com/PeachFlame/cPanel-fixperms
#
# Plesk portion credits too
# https://www.orware.com/blog/tips-and-how-tos/plesk/correct-httpdocs-permissions
# https://support.plesk.com/hc/en-us/articles/115001969889--BUG-plesk-repair-fs-doesn-t-set-correct-owner-inside-httpdocs
## How to use.
# wget https://gitlab.com/wizardassistantscripts/fixperms/-/raw/master/fixperms.sh ; bash fixperms.sh username
#
# wget https://gitlab.com/wizardassistantscripts/fixperms/-/raw/master/fixperms.sh ; bash fixperms.sh exampleuserbob
#
# Or once of
## bash <(curl -s https://gitlab.com/wizardassistantscripts/fixperms/-/raw/master/fixperms.sh || wget -qO - https://gitlab.com/wizardassistantscripts/fixperms/-/raw/master/fixperms.sh) exampleuserbob;
#
# Permanent Install for reuse via the below
# wget -O /usr/bin/fixperms https://gitlab.com/wizardassistantscripts/fixperms/-/raw/master/fixperms.sh; chmod +x /usr/bin/fixperms;
#
# Then
# fixperms -v -a Username
# fixperms -v -all
# Username=$1
#Detect Control panel
if [ -f /usr/local/cpanel/cpanel ]; then
# Cpanel check for /usr/local/cpanel/cpanel -V
ControlPanel="cpanel"
#user_homedir="/home/${Username}"
elif [ -f /usr/bin/cyberpanel ]; then
# CyberPanel check /usr/bin/cyberpanel
ControlPanel="cyberpanel"
#Get users homedir path
#user_homedir=$(grep -E "^${Username}:" /etc/passwd | cut -d: -f6)
elif [ -f /usr/local/psa/core.version ]; then
# Plesk check /usr/local/psa/core.version
ControlPanel="plesk"
#Get users homedir path
#user_homedir=$(grep -E "^${Username}:" /etc/passwd | cut -d: -f6)
else
echo "Not able to detect Control panel. Unsupported Control Panel exiting now"
exit 1;
fi
echo "=============================================================";
echo "$ControlPanel Control Panel Detected"
echo "=============================================================";
echo "";
# Set verbose to null
verbose=""
#Print the help text
helptext () {
tput bold
tput setaf 2
echo "Fix perms script help:"
echo "Sets file/directory permissions to match suPHP and FastCGI schemes"
echo "USAGE: fixperms [options] -a account_name"
echo "-------"
echo "Options:"
echo "-h or --help: print this screen and exit"
echo "-v: verbose output"
echo "-all: run on all Cyberpanel accounts"
echo "--account or -a: specify a Cyberpanel/cPanel/Plesk account"
# echo "--domain or -d: specify a Cyberpanel domain"
tput sgr0
exit 0
}
#Detect OS
if [ -f /etc/os-release ]; then
# freedesktop.org and systemd
. /etc/os-release
OS=$NAME
VER=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
# linuxbase.org
OS=$(lsb_release -si)
VER=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
# For some versions of Debian/Ubuntu without lsb_release command
. /etc/lsb-release
OS=$DISTRIB_ID
VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
# Older Debian/Ubuntu/etc.
OS=Debian
VER=$(cat /etc/debian_version)
elif [ -f /etc/SuSe-release ]; then
# Older SuSE/etc.
...
elif [ -f /etc/redhat-release ]; then
# Older Red Hat, CentOS, etc.
...
else
# Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
OS=$(uname -s)
VER=$(uname -r)
fi
#### Cyberpanel Section
# fix mailperms
fixmailperms_cyberpanel () {
tput bold
tput setaf 4
echo "Fixing mailperms...."
tput sgr0
#Fix perms of /home/vmail
chown -R vmail:vmail /home/vmail
chmod 755 /home/vmail
find /home/vmail -type d -exec chmod 0755 {} \;
find /home/vmail -type f -exec chmod 0640 {} \;
echo "Finished fixing mailperms...."
}
# Main workhorse, fix perms per account passed to it
fixperms_cyberpanel () {
#Get account from what is passed to the function
account=$1
#Make sure account isn't blank
if [ -z "$account" ]
then
tput bold
tput setaf 1
echo "Need an account name!"
tput sgr0
helptext
#Else, start doing work
else
# Get linux user from Domain
domain_username=$(grep -E "/${1}:" /etc/passwd | cut -d: -f1)
if id "$1" >/dev/null 2>&1; then
echo "$1 exists"
elif id "$domain_username" >/dev/null 2>&1; then
echo "Found user: $domain_username from domain: $1"
echo "$domain_username exists"
account=$domain_username
else
echo "user does not exist"
fi
#Get the account's homedir
HOMEDIR=$(grep -E "^${account}:" /etc/passwd | cut -d: -f6)
echo "User Homedirectory: ${HOMEDIR}"
tput bold
tput setaf 4
echo "Fixing perms for $account:"
tput setaf 3
if [ -d "$HOMEDIR/.cagefs" ]; then
chmod 775 "$HOMEDIR"/.cagefs
chmod 700 "$HOMEDIR"/.cagefs/tmp
chmod 700 "$HOMEDIR"/.cagefs/var
chmod 777 "$HOMEDIR"/.cagefs/cache
chmod 777 "$HOMEDIR"/.cagefs/run
fi
echo "------------------------"
tput setaf 4
echo "Fixing website files...."
tput sgr0
#Fix individual files in public_html
find "$HOMEDIR"/public_html -type d -exec chmod "$verbose" 755 {} \;
find "$HOMEDIR"/public_html -type f -print0 | xargs -d$'\n' -r chmod "$verbose" 644
find "$HOMEDIR"/public_html -name '*.cgi' -print0 -o -name '*.pl' | xargs -0 -r chmod "$verbose" 755
chown $verbose -R "$account":"$account" "$HOMEDIR"/public_html/*
# Hidden files test support: https://serverfault.com/a/156481
chown "$verbose" -R "$account":"$account" "$HOMEDIR"/public_html/.[^.]*
find "$HOMEDIR"/* -name .htaccess -exec chown "$verbose" "$account"."$account" {} \;
tput bold
tput setaf 4
echo "Fixing public_html...."
tput sgr0
#Fix perms of public_html itself
chown "$verbose" "$account":nobody "$HOMEDIR"/public_html
chmod "$verbose" 755 "$HOMEDIR"/public_html
tput bold
tput setaf 4
echo "Fixing logs...."
tput sgr0
#Fix perms of $HOMEDIR/logs
chown "$verbose" nobody:"$account" "$HOMEDIR"/logs
chmod "$verbose" 750 "$HOMEDIR"/logs
find "$HOMEDIR"/logs/* -name '*.access_log' -exec chown "$verbose" nobody."$account" {} \;
#Fix subdomains that lie outside of public_html
#tput setaf 3
#tput bold
#echo "------------------------"
#tput setaf 4
#echo "Fixing any domains with a document root outside of public_html...."
#for SUBDOMAIN in $(grep -i documentroot /var/cpanel/userdata/$account/* | grep -v '.cache\|_SSL' | awk '{print $2}' | grep -v public_html)
#do
#tput bold
#tput setaf 4
#echo "Fixing sub/addon domain document root $SUBDOMAIN...."
#tput sgr0
#find $SUBDOMAIN -type d -exec chmod $verbose 755 {} \;
#find $SUBDOMAIN -type f -print0 | xargs -d$'\n' -r chmod $verbose 644
#find $SUBDOMAIN -name '*.cgi' -o -name '*.pl' | xargs -r chmod $verbose 755
#chown $verbose -R $account:$account $SUBDOMAIN
#find $SUBDOMAIN -name .htaccess -exec chown $verbose $account.$account {} \;
#done
#Finished
tput bold
tput setaf 3
echo "Finished!"
echo "------------------------"
printf "\n\n"
tput sgr0
fi
return 0
}
#########cPanel
# Main workhorse, fix perms per account passed to it
fixperms_cpanel () {
#Get account from what is passed to the function
account=$1
#Check account against cPanel users file
if ! grep "$account" /var/cpanel/users/*
then
tput bold
tput setaf 1
echo "Invalid cPanel account"
tput sgr0
exit 0
fi
#Make sure account isn't blank
if [ -z "$account" ]
then
tput bold
tput setaf 1
echo "Need an account name!"
tput sgr0
helptext
#Else, start doing work
else
#Get the account's homedir
HOMEDIR=$(grep -E "^${account}:" /etc/passwd | cut -d: -f6)
echo "User Homedirectory: ${HOMEDIR}"
tput bold
tput setaf 4
echo "Fixing perms for $account:"
tput setaf 3
if [ -d "$HOMEDIR/.cagefs" ]; then
chmod 775 "$HOMEDIR"/.cagefs
chmod 700 "$HOMEDIR"/.cagefs/tmp
chmod 700 "$HOMEDIR"/.cagefs/var
chmod 777 "$HOMEDIR"/.cagefs/cache
chmod 777 "$HOMEDIR"/.cagefs/run
fi
echo "------------------------"
tput setaf 4
echo "Fixing website files...."
tput sgr0
#Fix individual files in public_html
find "$HOMEDIR"/public_html -type d -exec chmod "$verbose" 755 {} \;
find "$HOMEDIR"/public_html -type f -print0 | xargs -0 -d$'\n' -r chmod "$verbose" 644
find "$HOMEDIR"/public_html -name '*.cgi' -print0 -o -name '*.pl' | xargs -0 -r chmod "$verbose" 755
chown $verbose -R "$account":"$account" "$HOMEDIR"/public_html/*
# fix hidden files and folders like .well-known/ with root or other user perms
chown "$verbose" -R "$account":"$account" "$HOMEDIR"/public_html/.[^.]*
find "$HOMEDIR"/* -name .htaccess -exec chown "$verbose" "$account"."$account" {} \;
tput bold
tput setaf 4
echo "Fixing public_html...."
tput sgr0
#Fix perms of public_html itself
chown "$verbose" "$account":nobody "$HOMEDIR"/public_html
chmod "$verbose" 750 "$HOMEDIR"/public_html
#Fix subdomains that lie outside of public_html
tput setaf 3
tput bold
echo "------------------------"
tput setaf 4
echo "Fixing any domains with a document root outside of public_html...."
for SUBDOMAIN in $(grep -i documentroot /var/cpanel/userdata/"$account"/* | grep -v '.cache\|_SSL' | awk '{print $2}' | grep -v public_html)
do
tput bold
tput setaf 4
echo "Fixing sub/addon domain document root $SUBDOMAIN...."
tput sgr0
find "$SUBDOMAIN" -type d -exec chmod "$verbose" 755 {} \;
find "$SUBDOMAIN" -type f -print0 | xargs -0 -d$'\n' -r chmod "$verbose" 644
find "$SUBDOMAIN" -name '*.cgi' -print0 -o -name '*.pl' | xargs -0 -r chmod "$verbose" 755
chown "$verbose" -R "$account":"$account" "$SUBDOMAIN"
chmod "$verbose" 755 "$SUBDOMAIN"
find "$SUBDOMAIN" -name .htaccess -exec chown "$verbose" "$account"."$account" {} \;
done
#Finished
tput bold
tput setaf 3
echo "Finished!"
echo "------------------------"
printf "\n\n"
tput sgr0
fi
return 0
}
###################################
##################################
fixperms () {
Username=$1
if [ "${ControlPanel}" == "cpanel" ] ; then
fixperms_cpanel "${Username}"
# Fix users mailperms
tput bold
tput setaf 4
echo "Fixing Mailperms...."
tput sgr0
/scripts/mailperm --verbose "${Username}"
#Finished
tput bold
tput setaf 3
echo "Finished!"
echo "------------------------"
printf "\n\n"
tput sgr0
elif [ "${ControlPanel}" == "cyberpanel" ] ; then
fixperms_cyberpanel "${Username}"
fixmailperms_cyberpanel
elif [ "${ControlPanel}" == "plesk" ] ; then
#Get users homedir path
user_homedir=$(grep -E "^${Username}:" /etc/passwd | cut -d: -f6)
echo "User Homedirectory: ${user_homedir}"
echo "Resetting perms/ownership for ${user_homedir}/httpdocs"
sudo chown -R "${Username}":psacln "${user_homedir}"/httpdocs
sudo chown "${Username}":psaserv "${user_homedir}"/httpdocs
fi
}
all () {
if [ "${ControlPanel}" == "cpanel" ] ; then
for user in $(cut -d: -f1 /etc/domainusers)
do
fixperms_cpanel "$user"
done
# Fix all users mailperms
/scripts/mailperm --verbose
elif [ "${ControlPanel}" == "cyberpanel" ] ; then
if [[ $OS = 'CentOS Linux' ]] ; then
for user in $(getent passwd | awk -F: '5001<$3 && $3<6000 {print $1}' |grep -v spamd)
do
fixperms_cyberpanel "$user"
done
fixmailperms_cyberpanel
fi
if [[ $OS = 'Ubuntu' ]] ; then
for user in $(getent passwd | awk -F: '1001<$3 && $3<2000 {print $1}')
do
fixperms_cyberpanel "$user"
done
fixmailperms_cyberpanel
fi
fi
}
#Main function, switches options passed to it
case "$1" in
-h) helptext
;;
--help) helptext
;;
-v) verbose="-v"
case "$2" in
-all) all
;;
--account) fixperms "$3"
;;
-a) fixperms "$3"
;;
*) tput bold
tput setaf 1
echo "Invalid Option!"
helptext
;;
esac
;;
-all) all
;;
--account) fixperms "$2"
;;
-a) fixperms "$2"
;;
*)
tput bold
tput setaf 1
echo "Invalid Option!"
helptext
;;
esac