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