-
Notifications
You must be signed in to change notification settings - Fork 2
/
nginx-common.sh
65 lines (56 loc) · 2.08 KB
/
nginx-common.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
#!/bin/bash
#
# Common functions for nginx administration, shared by Ruby, Php and possibly more scripts
function prepare_user {
local user_name=$1
# Does user exist?
HOMEDIR=/srv/www/$user_name
id $user_name > /dev/null 2>&1
if [ $? -ne 0 ]; then
# Add new user
mkdir -p $HOMEDIR
useradd --home "$HOMEDIR" $user_name
chown root:$user_name $HOMEDIR
chmod 0751 $HOMEDIR
# Set up for logging
mkdir $HOMEDIR/dev
chown root:root $HOMEDIR/dev
chmod 0751 $HOMEDIR/dev
fi
# Does sftp group exist?
cat /etc/group | grep sftp > /dev/null
if [ $? -ne 0 ]; then
addgroup sftp > /dev/null
fi
usermod -a -G sftp $user_name
# Does ssh configuration has a sftp section?
grep -q 'Match Group sftp' /etc/ssh/sshd_config
if [ $? -ne 0 ]; then
cp /etc/ssh/sshd_config tmp/sshd_config.backup
echo '' >> /etc/ssh/sshd_config
echo 'Match Group sftp' >> /etc/ssh/sshd_config
echo ' ChrootDirectory %h' >> /etc/ssh/sshd_config
echo ' ForceCommand internal-sftp -f AUTH -l INFO' >> /etc/ssh/sshd_config
echo ' AllowTcpForwarding no' >> /etc/ssh/sshd_config
sshd -t
if [ $? -ne 0 ]; then
cp tmp/sshd_config.backup /etc/ssh/sshd_config
echo 'SSH reported error in configuration file after change. Previous config is restored.'
echo 'Fix configuration file /etc/ssh/sshd_config manually. The script is continuing.'
else
rm tmp/sshd_config.backup
restart ssh
fi
fi
# Set up for logging sftp operations for this user
if [ ! -f /etc/rsyslog.d/sftp.conf ]; then
echo ':programname, isequal, "internal-sftp" /var/log/sftp.log' >> /etc/rsyslog.d/sftp.conf
echo ':programname, isequal, "internal-sftp" ~' >> /etc/rsyslog.d/sftp.conf
fi
sed -i "/sftp.log/i \$AddUnixListenSocket $HOMEDIR/dev/log" /etc/rsyslog.d/sftp.conf
restart rsyslog
if [ $? -ne 0 ]; then
echo 'Failed to restart rsyslog'
exit $?
fi
}