dcae1079667bd8661963d0aa051fcba71d47b091
[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=Httpd Users,ou=Users,dc=drsnuggles,dc=stderr,dc=nl"
18 GROUPBASE="ou=Domain Groups,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
25 DIR="$1"
26
27 if [ -e "$DIR" ]; then
28         if [ ! -d "$DIR" ]; then
29                 echo "$DIR" must be a directory, or not exist yet.
30                 exit 1;
31         fi
32         echo "Skipping creation of $DIR, it already exists";
33 else
34         # Create $DIR from $TEMPLATE_DIR, if it does not exist yet
35         echo "Creating $DIR from $TEMPLATE_DIR"
36         cp -R "$TEMPLATE_DIR" "$DIR"
37 fi
38
39 # Make $DIR absolute
40 cd "$DIR"
41 DIR=`pwd`
42
43 # Strip prefix
44 SITE=`basename $DIR`
45
46 # replace . with -
47 GROUP=`echo $SITE | sed s/\\\\./-/g`
48 SCRIPT_USER="httpd-$GROUP"
49
50 if getent passwd | grep $SCRIPT_USER &> /dev/null && getent group | grep $GROUP &> /dev/null; then
51         echo "$SCRIPT_USER and/or $GROUP already exists, skipping account creation"
52 else
53         # find a uid
54         ID=2000
55         while getent passwd | cut -f 3 -d: | grep "^$ID\$" &>/dev/null && getent group | cut -f 3 -d: | grep "^$ID\$" &> /dev/null; do
56                 ((ID++))
57         done;
58
59         echo Found uid/gid $ID for $SCRIPT_USER/$GROUP
60
61         # Create a user for scripts to run as, and a group to give write permissions to
62         # files.
63         ldapvi --profile bind --add --in --ldapvi <<EOF || exit
64 add cn=$GROUP,$GROUPBASE
65 cn: $GROUP
66 gidNumber: $ID
67 objectClass: posixGroup
68 objectClass: top
69
70 add cn=$SITE,$USERBASE
71 cn: $SITE
72 uidNumber: $ID
73 gidNumber: $HTTPD_USERS_GID
74 homeDirectory: $DIR
75 objectClass: posixAccount
76 objectClass: account
77 objectClass: top
78 uid: $SCRIPT_USER
79 EOF
80 fi
81
82 if getent passwd | grep $SCRIPT_USER &> /dev/null && getent group | grep $GROUP &> /dev/null; then
83         echo "$SCRIPT_USER and $GROUP created succesfully"
84 else
85         echo "User or group creation failed"
86         exit 1
87 fi
88
89 echo "Setting up permissions"
90 # Set up permissions
91 sudo chown -R 0:$GROUP "$DIR"
92
93 # By default, let the owner have write access, the group have read access
94 sudo setfacl -R --set d:u::rwX,d:g::rX,d:o::-,u::rwX,g::rX,o::- "$DIR"
95
96 # Give the group write access to htdocs, applications, conf and data
97 sudo setfacl -R -m g::rwX,d:g::rwX "$DIR/htdocs" "$DIR/applications" "$DIR/conf" "$DIR/data"
98
99 # Give lighttpd read access to the dir itself
100 sudo setfacl -m u:$HTTPD_USER:rx "$DIR"
101
102 # Allow lighttpd to read anything in htdocs, applications, conf and data
103 sudo setfacl -R -m d:u:$HTTPD_USER:rX,u:$HTTPD_USER:rX "$DIR/htdocs" "$DIR/applications" "$DIR/conf" "$DIR/data"
104
105 # Allow lighttpd to write new files in logs (but not touch existing or those created by lighttpd)
106 sudo setfacl -m u:$HTTPD_USER:rwX "$DIR/logs"
107
108 # Give scripts read access to the dir itself
109 sudo setfacl -m u:$SCRIPT_USER:rx "$DIR"
110
111 # Allow scripts to read anything in applications, htdocs and conf
112 sudo setfacl -R -m d:u:$SCRIPT_USER:rX,u:$SCRIPT_USER:rX "$DIR/applications" "$DIR/htdocs" "$DIR/conf"
113
114 # Allow scripts to create new files in logs and data (but not touch existing or those created by lighttpd)
115 sudo setfacl -m u:$SCRIPT_USER:rwX "$DIR/logs" "$DIR/data"
116
117 # Temp, chown existing log files
118 sudo sh -c "chown -R $SCRIPT_USER \"$DIR\"/logs/php.log* \"$DIR\"/logs/wipi.log*"
119 sudo sh -c "chown -R $HTTPD_USER \"$DIR\"/logs/access.log*"
120
121 # Now, set the error_log setting in php.ini. This ensures each domein will have
122 # a separate logfile for errors, since lighttpd only supports a single error
123 # log (When error_log is not set, error messages will go to lighttpd's log
124 # automatically).
125
126 echo Updating `basename $PHP_CONFIG`
127 sudo sed -i "s#^error_log *=.*#error_log = $DIR/$PHP_ERRORLOG#" "$DIR/$PHP_CONFIG"
128 sudo update-php.ini
129
130
131 # Done!
132 echo "Done!"
133 echo "Now add human users to $GROUP."
134 echo "Also add this site to /usr/local/sbin/spawn-fcgi.sh and enable"
135 echo "fcgi in lighttpd if dynamic content is required."