93113deed0e3463c33cc5bca584548272874522b
[matthijs/servers/drsnuggles.git] / usr / local / bin / addsite
1 #!/bin/sh
2
3 if [ "$1" = "-h" -o "$1" = "--help" -o $# -ne 1 ]; then
4         echo "Usage $0 <dirname>"
5         echo "<dirname> is the full path to the site, such as /var/www/example.nl"
6         echo "which is created if it does not exist yet. If it exists, it's"
7         echo "permissions are reset".
8         exit 0
9 fi
10
11 HTTPD_USER=www-data
12 # The primary group of the created user
13 HTTPD_USERS_GID=1002
14 # The template to copy
15 TEMPLATE_DIR=/data/www/template
16 # The bases to create users under
17 USERBASE=ou=Users,dc=drsnuggles,dc=stderr,dc=nl
18 GROUPBASE=ou=Groups,dc=drsnuggles,dc=stderr,dc=nl
19 # PHP config to change the error_log setting in
20 PHP_CONFIG=conf/php.ini.override
21 # PHP error logfile to set error_log to
22 PHP_ERRORLOG=logs/php.log
23
24 # Get dir, but make it absolute
25 cd "$1"
26 DIR=`pwd`
27
28
29 if [ -e "$DIR" -a ! -d "$DIR" ]; then
30         echo "$DIR" must be a directory, or not exist yet.
31         exit 1;
32 fi
33
34 # Strip prefix
35 SITE=`basename $DIR`
36
37 # replace . with -
38 GROUP=`echo $SITE | sed s/\\\\./-/g`
39 SCRIPT_USER="httpd-$GROUP"
40
41 if getent passwd | grep $SCRIPT_USER &> /dev/null && getent group | grep $GROUP &> /dev/null; then
42         echo "$SCRIPT_USER and/or $GROUP already exists, skipping account creation"
43 else
44         # find a uid
45         ID=2000
46         while getent passwd | cut -f 3 -d: | grep "^$ID\$" &>/dev/null && getent group | cut -f 3 -d: | grep "^$ID\$" &> /dev/null; do
47                 ((ID++))
48         done;
49
50         echo Found uid/gid $ID for $SCRIPT_USER/$GROUP
51
52         # Create a user for scripts to run as, and a group to give write permissions to
53         # files.
54         ldapvi --profile bind --add --in --ldapvi <<EOF || exit
55 add cn=$GROUP,$GROUPBASE
56 cn: $GROUP
57 gidNumber: $ID
58 objectClass: posixGroup
59 objectClass: top
60
61 add cn=$SITE,$USERBASE
62 cn: $SITE
63 uidNumber: $ID
64 gidNumber: $HTTPD_USERS_GID
65 homeDirectory: $DIR
66 objectClass: posixAccount
67 objectClass: account
68 objectClass: top
69 uid: $SCRIPT_USER
70 EOF
71 fi
72
73 if getent passwd | grep $SCRIPT_USER &> /dev/null && getent group | grep $GROUP &> /dev/null; then
74         echo "$SCRIPT_USER and $GROUP created succesfully"
75 else
76         echo "User or group creation failed"
77         exit 1
78 fi
79
80 if [ -e "$DIR" ]; then
81         echo "Skipping creation of $DIR, it already exists";
82 else
83         # Create $DIR from $TEMPLATE_DIR, if it does not exist yet
84         echo "Creating $DIR from $TEMPLATE_DIR"
85         cp -R "$TEMPLATE_DIR" "$DIR"
86 fi
87
88 echo "Setting up permissions"
89 # Set up permissions
90 sudo chown -R 0:$GROUP "$DIR"
91
92 # By default, let the owner have write access, the group have read access
93 sudo setfacl -R --set d:u::rwX,d:g::rX,d:o::-,u::rwX,g::rX,o::- "$DIR"
94
95 # Give the group write access to htdocs, applications and conf
96 sudo setfacl -R -m g::rwX "$DIR/htdocs" "$DIR/applications" "$DIR/conf"
97
98 # Give lighttpd read access to the dir itself
99 sudo setfacl -m u:$HTTPD_USER:rx "$DIR"
100
101 # Allow lighttpd to read anything in htdocs, applications and conf
102 sudo setfacl -R -m d:u:$HTTPD_USER:rX,u:$HTTPD_USER:rX "$DIR/htdocs" "$DIR/applications" "$DIR/conf"
103
104 # Allow lighttpd to write new files in logs (but not touch existing or those created by lighttpd)
105 sudo setfacl -m u:$HTTPD_USER:rwX "$DIR/logs"
106
107 # Give scripts read access to the dir itself
108 sudo setfacl -m u:$SCRIPT_USER:rx "$DIR"
109
110 # Allow scripts to read anything in applications, htdocs and conf
111 sudo setfacl -R -m d:u:$SCRIPT_USER:rX,u:$SCRIPT_USER:rX "$DIR/applications" "$DIR/htdocs" "$DIR/conf"
112
113 # Allow scripts to create new files in logs and data (but not touch existing or those created by lighttpd)
114 sudo setfacl -m u:$SCRIPT_USER:rwX "$DIR/logs" "$DIR/data"
115
116 # Temp, chown existing log files
117 sudo sh -c "chown -R $SCRIPT_USER \"$DIR\"/logs/php.log* \"$DIR\"/logs/wipi.log*"
118 sudo sh -c "chown -R $HTTPD_USER \"$DIR\"/logs/access.log*"
119
120 # Now, set the error_log setting in php.ini
121
122 echo Updating `basename $PHP_CONFIG`
123
124 sudo sed -i "s#^error_log *=.*#error_log = $DIR/$PHP_ERRORLOG#" "$DIR/$PHP_CONFIG"
125
126
127 # Done!
128 echo "Done!"
129 echo "Now add human users to $GROUP."
130 echo "Also add this site to /usr/local/sbin/spawn-fcgi.sh and enable"
131 echo "fcgi in lighttpd if dynamic content is required."