Skip to content

Commit ca30929

Browse files
Update fixperms.sh
Added smart fall back detection to find the linux user from domain if domain is provided vs username for Cyberpanel user. Also ran it through shellcheck andupdated and optimized the code and added multi control panel detection code.
1 parent a3846aa commit ca30929

File tree

1 file changed

+262
-41
lines changed

1 file changed

+262
-41
lines changed

CPScripts/fixperms.sh

Lines changed: 262 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,62 @@
1-
#! /bin/bash
2-
# Cyberpanel Fix Perms
3-
# https://gitlab.com/cyberpaneltoolsnscripts/cyberpanel-fixperms
1+
#!/usr/bin/env bash
2+
## Author: Michael Ramsey
3+
## Objective Fix permissions issues on CyberPanel/cPanel/Plesk for a linux user or users
4+
# https://gitlab.com/wizardassistantscripts/fixperms
45
#
56
# Forked from https://github.com/PeachFlame/cPanel-fixperms
67
#
8+
# Plesk portion credits too
9+
# https://www.orware.com/blog/tips-and-how-tos/plesk/correct-httpdocs-permissions
10+
# https://support.plesk.com/hc/en-us/articles/115001969889--BUG-plesk-repair-fs-doesn-t-set-correct-owner-inside-httpdocs
11+
12+
## How to use.
13+
# wget https://gitlab.com/wizardassistantscripts/fixperms/-/raw/master/fixperms.sh ; bash fixperms.sh username
14+
#
15+
# wget https://gitlab.com/wizardassistantscripts/fixperms/-/raw/master/fixperms.sh ; bash fixperms.sh exampleuserbob
16+
#
17+
# Or once of
18+
## bash <(curl -s https://gitlab.com/wizardassistantscripts/fixperms/-/raw/master/fixperms.sh || wget -qO - https://gitlab.com/wizardassistantscripts/fixperms/-/raw/master/fixperms.sh) exampleuserbob;
19+
#
20+
# Permanent Install for reuse via the below
21+
# wget -O /usr/bin/fixperms https://gitlab.com/wizardassistantscripts/fixperms/-/raw/master/fixperms.sh; chmod +x /usr/bin/fixperms;
22+
#
23+
# Then
24+
# fixperms -v -a Username
25+
# fixperms -v -all
26+
# Username=$1
27+
28+
29+
#Detect Control panel
30+
if [ -f /usr/local/cpanel/cpanel ]; then
31+
# Cpanel check for /usr/local/cpanel/cpanel -V
32+
ControlPanel="cpanel"
33+
#user_homedir="/home/${Username}"
34+
35+
36+
elif [ -f /usr/bin/cyberpanel ]; then
37+
# CyberPanel check /usr/bin/cyberpanel
38+
ControlPanel="cyberpanel"
39+
#Get users homedir path
40+
#user_homedir=$(grep -E "^${Username}:" /etc/passwd | cut -d: -f6)
41+
42+
elif [ -f /usr/local/psa/core.version ]; then
43+
# Plesk check /usr/local/psa/core.version
44+
ControlPanel="plesk"
45+
46+
#Get users homedir path
47+
#user_homedir=$(grep -E "^${Username}:" /etc/passwd | cut -d: -f6)
48+
49+
50+
else
51+
echo "Not able to detect Control panel. Unsupported Control Panel exiting now"
52+
exit 1;
53+
fi
54+
echo "=============================================================";
55+
echo "$ControlPanel Control Panel Detected"
56+
echo "=============================================================";
57+
echo "";
58+
59+
760

861
# Set verbose to null
962
verbose=""
@@ -21,7 +74,7 @@ helptext () {
2174
echo "-h or --help: print this screen and exit"
2275
echo "-v: verbose output"
2376
echo "-all: run on all Cyberpanel accounts"
24-
echo "--account or -a: specify a Cyberpanel account"
77+
echo "--account or -a: specify a Cyberpanel/cPanel/Plesk account"
2578
# echo "--domain or -d: specify a Cyberpanel domain"
2679
tput sgr0
2780
exit 0
@@ -59,8 +112,11 @@ else
59112
fi
60113

61114

115+
116+
#### Cyberpanel Section
117+
62118
# fix mailperms
63-
fixmailperms () {
119+
fixmailperms_cyberpanel () {
64120
tput bold
65121
tput setaf 4
66122
echo "Fixing mailperms...."
@@ -75,7 +131,7 @@ fixmailperms () {
75131
}
76132

77133
# Main workhorse, fix perms per account passed to it
78-
fixperms () {
134+
fixperms_cyberpanel () {
79135

80136
#Get account from what is passed to the function
81137
account=$1
@@ -91,43 +147,64 @@ fixperms () {
91147
#Else, start doing work
92148
else
93149

94-
#Get the account's homedir
95-
HOMEDIR=$(egrep "^${account}:" /etc/passwd | cut -d: -f6)
150+
# Get linux user from Domain
151+
domain_username=$(grep -E "/${1}:" /etc/passwd | cut -d: -f1)
152+
if id "$1" >/dev/null 2>&1; then
153+
echo "$1 exists"
154+
elif id "$domain_username" >/dev/null 2>&1; then
155+
echo "Found user: $domain_username from domain: $1"
156+
echo "$domain_username exists"
157+
account=$domain_username
158+
else
159+
echo "user does not exist"
160+
fi
161+
96162

163+
#Get the account's homedir
164+
HOMEDIR=$(grep -E "^${account}:" /etc/passwd | cut -d: -f6)
165+
echo "User Homedirectory: ${HOMEDIR}"
97166
tput bold
98167
tput setaf 4
99168
echo "Fixing perms for $account:"
100169
tput setaf 3
170+
if [ -d "$HOMEDIR/.cagefs" ]; then
171+
chmod 775 "$HOMEDIR"/.cagefs
172+
chmod 700 "$HOMEDIR"/.cagefs/tmp
173+
chmod 700 "$HOMEDIR"/.cagefs/var
174+
chmod 777 "$HOMEDIR"/.cagefs/cache
175+
chmod 777 "$HOMEDIR"/.cagefs/run
176+
fi
101177
echo "------------------------"
102178
tput setaf 4
103179
echo "Fixing website files...."
104180
tput sgr0
105-
181+
182+
106183
#Fix individual files in public_html
107-
find "$HOMEDIR"/public_html -type d -exec chmod $verbose 755 {} \;
108-
find "$HOMEDIR"/public_html -type f | xargs -d$'\n' -r chmod $verbose 644
109-
find "$HOMEDIR"/public_html -name '*.cgi' -o -name '*.pl' | xargs -r chmod $verbose 755
184+
find "$HOMEDIR"/public_html -type d -exec chmod "$verbose" 755 {} \;
185+
find "$HOMEDIR"/public_html -type f -print0 | xargs -d$'\n' -r chmod "$verbose" 644
186+
find "$HOMEDIR"/public_html -name '*.cgi' -print0 -o -name '*.pl' | xargs -0 -r chmod "$verbose" 755
110187
#chown $verbose -R "$account":"$account" "$HOMEDIR"/public_html/*
111188
# Hidden files test support: https://serverfault.com/a/156481
112-
chown $verbose -R "$account":"$account" "$HOMEDIR"/public_html/.[^.]*
113-
find "$HOMEDIR"/* -name .htaccess -exec chown $verbose "$account"."$account" {} \;
189+
chown "$verbose" -R "$account":"$account" "$HOMEDIR"/public_html/.[^.]*
190+
find "$HOMEDIR"/* -name .htaccess -exec chown "$verbose" "$account"."$account" {} \;
114191

115192
tput bold
116193
tput setaf 4
117194
echo "Fixing public_html...."
118195
tput sgr0
119196
#Fix perms of public_html itself
120-
chown $verbose "$account":"$account" "$HOMEDIR"/public_html
121-
chmod $verbose 755 "$HOMEDIR"/public_html
197+
chown "$verbose" "$account":"$account" "$HOMEDIR"/public_html
198+
chmod "$verbose" 755 "$HOMEDIR"/public_html
122199

123200
tput bold
124201
tput setaf 4
125202
echo "Fixing logs...."
126203
tput sgr0
127204
#Fix perms of $HOMEDIR/logs
128-
chown $verbose nobody:"$account" "$HOMEDIR"/logs
129-
chmod $verbose 750 "$HOMEDIR"/logs
130-
find "$HOMEDIR"/logs/* -name '*.access_log' -exec chown $verbose nobody."$account" {} \;
205+
chown "$verbose" nobody:"$account" "$HOMEDIR"/logs
206+
chmod "$verbose" 750 "$HOMEDIR"/logs
207+
find "$HOMEDIR"/logs/* -name '*.access_log' -exec chown "$verbose" nobody."$account" {} \;
131208

132209

133210

@@ -144,7 +221,7 @@ fixperms () {
144221
#echo "Fixing sub/addon domain document root $SUBDOMAIN...."
145222
#tput sgr0
146223
#find $SUBDOMAIN -type d -exec chmod $verbose 755 {} \;
147-
#find $SUBDOMAIN -type f | xargs -d$'\n' -r chmod $verbose 644
224+
#find $SUBDOMAIN -type f -print0 | xargs -d$'\n' -r chmod $verbose 644
148225
#find $SUBDOMAIN -name '*.cgi' -o -name '*.pl' | xargs -r chmod $verbose 755
149226
#chown $verbose -R $account:$account $SUBDOMAIN
150227
#find $SUBDOMAIN -name .htaccess -exec chown $verbose $account.$account {} \;
@@ -162,33 +239,177 @@ fixperms () {
162239
return 0
163240
}
164241

165-
#Parses all users through Cyberpanel's users file
166-
#all () {
167-
# for user in $(cut -d: -f1 /etc/domainusers)
168-
# do
169-
# fixperms "$user"
170-
# done
171-
#}
172242

243+
#########cPanel
244+
# Main workhorse, fix perms per account passed to it
245+
fixperms_cpanel () {
173246

174-
all () {
247+
#Get account from what is passed to the function
248+
account=$1
175249

176-
if [[ $OS = 'CentOS Linux' ]] ; then
177-
for user in $(getent passwd | awk -F: '5001<$3 && $3<6000 {print $1}' |grep -v spamd)
178-
do
179-
fixperms "$user"
180-
done
181-
fixmailperms
182-
fi
250+
#Check account against cPanel users file
251+
if ! grep "$account" /var/cpanel/users/*
252+
then
253+
tput bold
254+
tput setaf 1
255+
echo "Invalid cPanel account"
256+
tput sgr0
257+
exit 0
258+
fi
183259

184-
if [[ $OS = 'Ubuntu' ]] ; then
185-
for user in $(getent passwd | awk -F: '1001<$3 && $3<2000 {print $1}')
260+
#Make sure account isn't blank
261+
if [ -z "$account" ]
262+
then
263+
tput bold
264+
tput setaf 1
265+
echo "Need an account name!"
266+
tput sgr0
267+
helptext
268+
#Else, start doing work
269+
else
270+
271+
#Get the account's homedir
272+
HOMEDIR=$(grep -E "^${account}:" /etc/passwd | cut -d: -f6)
273+
echo "User Homedirectory: ${HOMEDIR}"
274+
tput bold
275+
tput setaf 4
276+
echo "Fixing perms for $account:"
277+
tput setaf 3
278+
if [ -d "$HOMEDIR/.cagefs" ]; then
279+
chmod 775 "$HOMEDIR"/.cagefs
280+
chmod 700 "$HOMEDIR"/.cagefs/tmp
281+
chmod 700 "$HOMEDIR"/.cagefs/var
282+
chmod 777 "$HOMEDIR"/.cagefs/cache
283+
chmod 777 "$HOMEDIR"/.cagefs/run
284+
fi
285+
echo "------------------------"
286+
tput setaf 4
287+
echo "Fixing website files...."
288+
tput sgr0
289+
290+
291+
292+
#Fix individual files in public_html
293+
find "$HOMEDIR"/public_html -type d -exec chmod "$verbose" 755 {} \;
294+
find "$HOMEDIR"/public_html -type f -print0 | xargs -0 -d$'\n' -r chmod "$verbose" 644
295+
find "$HOMEDIR"/public_html -name '*.cgi' -print0 -o -name '*.pl' | xargs -0 -r chmod "$verbose" 755
296+
# fix hidden files and folders like .well-known/ with root or other user perms
297+
chown "$verbose" -R "$account":"$account" "$HOMEDIR"/public_html/.[^.]*
298+
find "$HOMEDIR"/* -name .htaccess -exec chown "$verbose" "$account"."$account" {} \;
299+
300+
tput bold
301+
tput setaf 4
302+
echo "Fixing public_html...."
303+
tput sgr0
304+
#Fix perms of public_html itself
305+
chown "$verbose" "$account":nobody "$HOMEDIR"/public_html
306+
chmod "$verbose" 750 "$HOMEDIR"/public_html
307+
308+
#Fix subdomains that lie outside of public_html
309+
tput setaf 3
310+
tput bold
311+
echo "------------------------"
312+
tput setaf 4
313+
echo "Fixing any domains with a document root outside of public_html...."
314+
for SUBDOMAIN in $(grep -i documentroot /var/cpanel/userdata/"$account"/* | grep -v '.cache\|_SSL' | awk '{print $2}' | grep -v public_html)
186315
do
187-
fixperms "$user"
316+
tput bold
317+
tput setaf 4
318+
echo "Fixing sub/addon domain document root $SUBDOMAIN...."
319+
tput sgr0
320+
find "$SUBDOMAIN" -type d -exec chmod "$verbose" 755 {} \;
321+
find "$SUBDOMAIN" -type f -print0 | xargs -0 -d$'\n' -r chmod "$verbose" 644
322+
find "$SUBDOMAIN" -name '*.cgi' -print0 -o -name '*.pl' | xargs -0 -r chmod "$verbose" 755
323+
chown "$verbose" -R "$account":"$account" "$SUBDOMAIN"
324+
chmod "$verbose" 755 "$SUBDOMAIN"
325+
find "$SUBDOMAIN" -name .htaccess -exec chown "$verbose" "$account"."$account" {} \;
188326
done
189-
fixmailperms
190-
fi
191327

328+
#Finished
329+
tput bold
330+
tput setaf 3
331+
echo "Finished!"
332+
echo "------------------------"
333+
printf "\n\n"
334+
tput sgr0
335+
fi
336+
337+
return 0
338+
}
339+
340+
###################################
341+
342+
343+
344+
345+
346+
##################################
347+
348+
fixperms () {
349+
Username=$1
350+
if [ "${ControlPanel}" == "cpanel" ] ; then
351+
352+
fixperms_cpanel "${Username}"
353+
# Fix users mailperms
354+
tput bold
355+
tput setaf 4
356+
echo "Fixing Mailperms...."
357+
tput sgr0
358+
/scripts/mailperm --verbose "${Username}"
359+
#Finished
360+
tput bold
361+
tput setaf 3
362+
echo "Finished!"
363+
echo "------------------------"
364+
printf "\n\n"
365+
tput sgr0
366+
367+
elif [ "${ControlPanel}" == "cyberpanel" ] ; then
368+
369+
fixperms_cyberpanel "${Username}"
370+
fixmailperms_cyberpanel
371+
372+
373+
elif [ "${ControlPanel}" == "plesk" ] ; then
374+
#Get users homedir path
375+
user_homedir=$(grep -E "^${Username}:" /etc/passwd | cut -d: -f6)
376+
echo "User Homedirectory: ${user_homedir}"
377+
echo "Resetting perms/ownership for ${user_homedir}/httpdocs"
378+
sudo chown -R "${Username}":psacln "${user_homedir}"/httpdocs
379+
sudo chown "${Username}":psaserv "${user_homedir}"/httpdocs
380+
381+
fi
382+
}
383+
384+
all () {
385+
386+
if [ "${ControlPanel}" == "cpanel" ] ; then
387+
388+
for user in $(cut -d: -f1 /etc/domainusers)
389+
do
390+
fixperms_cpanel "$user"
391+
done
392+
# Fix all users mailperms
393+
/scripts/mailperm --verbose
394+
395+
elif [ "${ControlPanel}" == "cyberpanel" ] ; then
396+
397+
if [[ $OS = 'CentOS Linux' ]] ; then
398+
for user in $(getent passwd | awk -F: '5001<$3 && $3<6000 {print $1}' |grep -v spamd)
399+
do
400+
fixperms_cyberpanel "$user"
401+
done
402+
fixmailperms_cyberpanel
403+
fi
404+
405+
if [[ $OS = 'Ubuntu' ]] ; then
406+
for user in $(getent passwd | awk -F: '1001<$3 && $3<2000 {print $1}')
407+
do
408+
fixperms_cyberpanel "$user"
409+
done
410+
fixmailperms_cyberpanel
411+
fi
412+
fi
192413
}
193414

194415

@@ -230,4 +451,4 @@ case "$1" in
230451
echo "Invalid Option!"
231452
helptext
232453
;;
233-
esac
454+
esac

0 commit comments

Comments
 (0)