-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmk_vo.sh
executable file
·211 lines (184 loc) · 5.73 KB
/
mk_vo.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/bin/bash
set -u
# mk_vo.sh <path>
#
# Sample script for generating lists of VO members. To be used with
# the Apache module mod_gacl.
#
# Copyright 2008 (C) Frederik Orellana, Niels Bohr Institute,
# University of Copenhagen. All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
# Configuration
#
# The DocumentRoot of the web server. When recursing upwards looking
# for a .gacl file, this is where we stop. You may want to change this.
DOCUMENT_ROOT="/var/www/html/grid/data"
GACL_FILE=".gacl"
GACL_VO_FILE=".gacl_vo"
TMP_GACL_VO_FILE=".gacl_vo.$(basename $0).$$.tmp"
LOCK_FILE=".gacl_lock"
DN_LIST_TAG="dn-list"
DN_LIST_URL_TAG="url"
MAX_RECURSE=12
TMP_FILE="/tmp/$(basename $0).$$.tmp"
#
# First find the directory containing the .gacl file to check.
#
# The argument supplied to this script must be a path - either of a
# directory or of a file. Here we find the directory (and the file name).
path="$@"
if [ -z $path ]; then
echo "no path given"
exit -1
fi
if [ -d $path ]; then
dir=$path
file=""
else
dir=`echo $path | sed -r 's|(.*)/[^/]*|\1|'`
file=`echo $path | sed -r 's|.*/([^/]+)|\1|'`
fi
# Recurse upwards until a .gacl file is found.
i=0
while [ "$dir" != "$DOCUMENT_ROOT" -a "$dir" != "/" ]; do
if [ -e $dir/$GACL_FILE ]; then
echo "Found $GACL_FILE file in $dir"
cd "$dir"
break
fi
# Go one level up.
dir=`echo $dir | sed -r 's|(.*)/$|\1|' | sed -r 's|(.*)/[^/]+|\1|'`
if [ -z "$dir" ]; then
dir="/"
fi
i=`expr $i + 1`
echo $i
if [ $i -gt $MAX_RECURSE ]; then
break
fi
done
if [ ! -e $GACL_FILE ]; then
echo "no $GACL_FILE file found"
rm -f $GACL_VO_FILE
exit -2
fi
#
# Check if the .gacl file contains elements of the form
# <dn-list><url>https://some.url/vo.txt</url></dn-list>.
# If it does, construct list of URLs. If it does not,
# delete any existing .gacl_vo file.
#
check=`grep "<$DN_LIST_TAG>" $GACL_FILE`
if [ -z "$check" ]; then
echo "no $DN_LIST_URL_TAG tag found"
if ( set -o noclobber; echo "$$" > "$LOCK_FILE") 2> /dev/null;
then
echo "Deleting file $PWD/$GACL_VO_FILE"
trap 'rm -f "$GACL_VO_FILE" "$LOCK_FILE"; exit 0' INT TERM EXIT
rm -f "$GACL_VO_FILE" "$LOCK_FILE"
trap - INT TERM EXIT
else
echo "Failed to acquire lock file: $PWD/$LOCK_FILE." 1>&2
echo "Held by $(cat $LOCK_FILE)" 1>&2
echo "Leaving $PWD/$GACL_VO_FILE" 1>&2
fi
exit 0
fi
# Get the list of URLs
gacl_line=`sed -n '1h;2,$H;${g;s/\n//g;p}' .gacl | sed -r 's/>\s+</></g'`
old_line=""
url_list=$gacl_line
while [ "$url_list" != "$old_line" ]; do
old_line=$url_list
url_list=`echo "$old_line" | sed -r "s|(.*)<$DN_LIST_TAG><$DN_LIST_URL_TAG>(.*)</$DN_LIST_URL_TAG></$DN_LIST_TAG>(.*)|\2\\t\1\3|"`
done
url_list=`echo "$url_list" | sed -r 's|(.*http[s]*://\S+)\t[^\t]*|\1|'`
if [ -z "$url_list" -o "$url_list" == "$gacl_line" ]; then
echo "no $DN_LIST_URL_TAG tag found"
exit 0
fi
#
# Construct list of entries in the form
# URL1<allow>...</allow><deny>...</deny>URL2<allow>...</allow><deny>...</deny>...
#
gacl_line=`sed -n '1h;2,$H;${g;s/\n//g;p}' .gacl | sed -r 's/>\s+</></g'`
old_line=""
entries_list=`echo "$gacl_line" | sed 's|<entry>|<entry\t>|gi' | sed 's|</entry>|</entry\t>|gi'`
while [ "$entries_list" != "$old_line" ]; do
old_line=$entries_list
entries_list=`echo "$old_line" | sed -r "s|(.*)<entry\t>([^\t]*)<$DN_LIST_TAG><$DN_LIST_URL_TAG>([^\t]*)</$DN_LIST_URL_TAG></$DN_LIST_TAG>([^\t]*)</entry\t>(.*)|\3\2\4\1\5|"`
done
entries_list=`echo "$entries_list" | sed -r 's|(.+)<gacl>.*|\1|i'`https://
#echo "$url_list" | tr '\t' ' '
#echo "$entries_list"
#
# Write the .gacl_vo file.
#
# Write a lock.
# From http://www.davidpashley.com/articles/writing-robust-shell-scripts.html
if ( set -o noclobber; echo "$$" > "$LOCK_FILE") 2> /dev/null;
then
echo "Writing file $PWD/$GACL_VO_FILE"
else
echo "Failed to acquire lock file: $PWD/$LOCK_FILE." 1>&2
echo "Held by $(cat $LOCK_FILE)" 1>&2
echo "NOT writing new .gacl_vo now." 1>&2
exit 0
fi
trap 'rm -f "$GACL_VO_FILE" "$LOCK_FILE"; exit 0' INT TERM EXIT
# ------------------------------------------------
echo "<gacl>" > $TMP_GACL_VO_FILE
for url in $url_list; do
url1=`echo $url | sed 's|http[s]*://|\t|g'`
perms=`echo $entries_list | sed 's|http[s]*://|\t|g'`
perms=`echo "$perms" | sed -r "s|.*$url1([^\t]+)\t.*|\1|"`
echo URL: "$url"
echo PERMS: "$perms"
curl --insecure -L $url 2>/dev/null > $TMP_FILE
## Make sure we have a newline at the end. Otherwise read will ignore the last line
echo >> $TMP_FILE
cat $TMP_FILE | sed 's/\"//g' | while read name; do
if [ -z "$name" ]; then
continue
fi
## Ignore comments
test=`echo $name | sed -r 's|^\s*#.*$||'`
if [ -z "$test" ]; then
continue
fi
cat >> $TMP_GACL_VO_FILE <<EOF
<entry>
<person>
<dn>$name</dn>
</person>
$perms
</entry>
EOF
done
done
rm -f $TMP_FILE
echo "</gacl>" >> $TMP_GACL_VO_FILE
echo >> $TMP_GACL_VO_FILE
mv -f $TMP_GACL_VO_FILE $GACL_VO_FILE
ok=`grep -r -v '^</*gacl>$' $GACL_VO_FILE`
if [ -z "$ok" ]; then
echo "VO file empty, deleting $PWD/$GACL_VO_FILE."
rm -f $GACL_VO_FILE
fi
# ------------------------------------------------
# Clear lock
rm -f "$LOCK_FILE"
trap - INT TERM EXIT