source: trunk/src/basedir_prot.sh @ 912

Revision 912, 3.9 KB checked in by benjamin, 7 years ago (diff)

ne plus dépendre de AlternC-admintools dans les commentaires

  • Property svn:executable set to *
Line 
1#!/bin/sh
2
3set -e
4
5# Ceci créé un hack php pour chacun des domaines hébergés par alternc
6# ce hack consiste à restreindre chaque usager à son propre répertoire
7# dans alternc/html/u/user avec open_base_dir
8
9# ce script a les dépendances suivantes:
10# (mysql, /etc/alternc/local.sh) OR /usr/bin/get_account_by_domain dans
11# l'ancien package alternc-admintools désormais dans alternc natif.
12# cut, awk, sort
13
14override_d=/var/alternc/apacheconf
15override_f=${override_d}/override_php.conf
16extra_paths="/var/alternc/dns/redir:/usr/share/php/:/var/alternc/tmp/:/tmp/"
17
18. /etc/alternc/local.sh
19if [ -z "$MYSQL_HOST" ]
20then
21    MYSQL_HOST="localhost"
22fi
23
24# imprime le nom d'usager associé au domaine
25get_account_by_domain() {
26        # les admintools ne sont peut-être pas là
27        if [ -x "/usr/bin/get_account_by_domain" ]
28        then
29                # only first field, only first line
30                /usr/bin/get_account_by_domain "$1" | cut -d\  -f1 | cut -d'
31' -f 1
32        else
33                # implantons localement ce que nous avons besoin, puisque admintools
34                # n'est pas là
35                mysql -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASS -D$MYSQL_DATABASE -B -N -e \
36                'SELECT a.login FROM membres a, sub_domaines b WHERE a.uid = b.compte AND \
37                CONCAT(IF(sub="", "", CONCAT(sub, ".")), domaine) = "'"$1"'" LIMIT 1;'
38        fi
39}
40
41# add the standard input to a given file, only if not already present
42append_no_dupe() {
43        realfile="$1"
44        tmpfile=`mktemp`
45        trap "rm -f $tmpfile; exit 1" 1 2 15
46        cat > $tmpfile
47        if [ -r "$realfile" ] &&
48                (diff -q "$tmpfile" "$realfile" > /dev/null || \
49                        diff -u "$tmpfile" "$realfile"  | grep '^ ' | sed 's/^ //' | diff -q - "$tmpfile" > /dev/null)
50        then
51                ret=0
52        else
53                ret=1
54                cat "$tmpfile" >> "$realfile"
55        fi
56        rm -f "$tmpfile"
57        return "$ret"
58}
59
60add_dom_entry() {
61        # protect ourselves from interrupts
62        trap "rm -f ${override_f}.new; exit 1" 1 2 15
63        # ajouter une entrée, seulement s'il n'y en pas déjà, pour ce domaine
64        (echo "$1"; [ -r $override_f ] && cat $override_f) | \
65        sort -u > ${override_f}.new && \
66        cp ${override_f}.new ${override_f} && \
67        rm ${override_f}.new
68}
69
70# la première lettre de l'avant-dernière partie du domaine (e.g.
71# www.alternc.org -> a)
72#
73# argument: le domaine
74# imprime: la lettre
75init_dom_letter() {
76    echo "$1" | awk '{z=split($NF, a, ".") ; print substr(a[z-1], 1, 1)}'
77}
78
79echo -n "adding open_base_dir protection for:"
80# boucle sur tous les domaines hébergés, ou sur les arguments de la
81# ligne de commande
82if [ $# -gt 0 ]; then
83        for i in "$*"
84        do
85                if echo "$i" | grep -q '^\*\.'
86                then
87                    echo skipping wildcard "$i" >&2
88                    continue
89                fi
90                if echo "$i" | grep -q /var/alternc/dns > /dev/null; then
91                        dom="$i"
92                else
93                    initial_domain=`init_dom_letter "$i"`
94                    dom="/var/alternc/dns/$initial_domain/$i"
95                fi
96                if [ -e "$dom" ]; then
97                        doms="$doms $dom"
98                else
99                        echo skipping non-existent domain "$dom" >&2
100                fi
101        done
102else
103        doms=`find /var/alternc/dns -type l`
104fi
105
106for i in $doms
107do
108        # don't "protect" squirrelmail, it legitimatly needs to consult
109        # files out of its own directory
110        if readlink "$i" | grep -q '^/var/alternc/bureau/admin/webmail/*$' || \
111           readlink "$i" | grep -q '^/var/alternc/bureau/*$'
112        then
113                continue
114        fi
115        domain=`basename "$i"`
116        account=`get_account_by_domain $domain`
117        if [ -z "$account" ]; then
118                continue
119        fi
120        # la première lettre de l'avant-dernière partie du domaine (e.g.
121        # www.alternc.org -> a)
122        initial_domain=`init_dom_letter "$domain"`
123        # la première lettre du username
124        initial_account=`echo "$account" | cut -c1`
125        path1="/var/alternc/dns/$initial_domain/$domain"
126        path2="/var/alternc/html/$initial_account/$account"
127
128        mkdir -p "$override_d/$initial_domain"
129        if append_no_dupe "$override_d/$initial_domain/$domain" <<EOF
130<Directory ${path1}>
131  php_admin_value open_basedir ${path2}/:${extra_paths}
132</Directory>
133EOF
134        then
135                true
136        else
137                echo -n " $domain"
138                add_dom_entry "Include $override_d/$initial_domain/$domain"
139        fi
140done
141
142echo .
Note: See TracBrowser for help on using the repository browser.