~ubuntu-branches/ubuntu/vivid/globus-gss-assist/vivid

« back to all changes in this revision

Viewing changes to programs/grid-mapfile-add-entry.in

  • Committer: Bazaar Package Importer
  • Author(s): Mattias Ellert
  • Date: 2009-04-18 20:17:33 UTC
  • Revision ID: james.westby@ubuntu.com-20090418201733-xl4r26mgda1shx4q
Tags: upstream-4.0
ImportĀ upstreamĀ versionĀ 4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
 
 
3
 
4
# Copyright 1999-2006 University of Chicago
 
5
 
6
# Licensed under the Apache License, Version 2.0 (the "License");
 
7
# you may not use this file except in compliance with the License.
 
8
# You may obtain a copy of the License at
 
9
 
10
# http://www.apache.org/licenses/LICENSE-2.0
 
11
 
12
# Unless required by applicable law or agreed to in writing, software
 
13
# distributed under the License is distributed on an "AS IS" BASIS,
 
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
15
# See the License for the specific language governing permissions and
 
16
# limitations under the License.
 
17
 
18
 
 
19
#
 
20
# grid-mapfile-add-entry
 
21
#
 
22
 
 
23
if test -z "${GLOBUS_LOCATION}"; then
 
24
    echo ""
 
25
    echo "ERROR: Please set GLOBUS_LOCATION to the Globus installation directory before"
 
26
    echo "running this script"
 
27
    echo ""
 
28
    exit 1
 
29
fi
 
30
 
 
31
. ${GLOBUS_LOCATION}/libexec/globus-script-initializer
 
32
globus_source ${GLOBUS_LOCATION}/libexec/globus-sh-tools.sh
 
33
 
 
34
PROGRAM_NAME=`echo $0 | ${GLOBUS_SH_SED-sed} 's|.*/||g'`
 
35
 
 
36
PROGRAM_VERSION=`echo '$Revision: 1.7 $'| ${GLOBUS_SH_SED-sed} -e 's|\\$||g' -e 's|Revision: \(.*\)|\1|'`
 
37
 
 
38
VERSION="@VERSION@"
 
39
 
 
40
PACKAGE="@PACKAGE@"
 
41
 
 
42
DIRT_TIMESTAMP="@DIRT_TIMESTAMP@"
 
43
DIRT_BRANCH_ID="@DIRT_BRANCH_ID@"
 
44
 
 
45
 
 
46
short_usage="$PROGRAM_NAME -dn DN -ln LN 
 
47
[-help] [-d] [-f mapfile FILE]"
 
48
 
 
49
long_usage() {
 
50
    ${GLOBUS_SH_CAT-cat} >&2 <<EOF
 
51
 
 
52
${short_usage}
 
53
 
 
54
    $PROGRAM_NAME adds an entry to a Grid mapfile.
 
55
 
 
56
    Options:
 
57
      -help, -usage           Displays help
 
58
      -version                Displays version
 
59
      -dn DN                  Distinguished Name (DN) to add. Remember to 
 
60
                              quote the DN if it contains spaces.
 
61
      -ln LN1 [LN2...]        Local login name(s) to map DN to
 
62
      -dryrun, -d             Shows what would be done but will not add the entry
 
63
      -mapfile FILE, -f FILE  Path of Grid map file to be used
 
64
 
 
65
EOF
 
66
}
 
67
 
 
68
globus_source $libexecdir/globus-args-parser-header $@
 
69
 
 
70
##############################################
 
71
Cleanup()
 
72
{
 
73
 
 
74
if [ -f $CONSISTENCY_CHECK ]
 
75
then
 
76
        rm $CONSISTENCY_CHECK
 
77
fi
 
78
 
 
79
if [ -f $GRID_MAP_FILE_COPY ]
 
80
then
 
81
        rm $GRID_MAP_FILE_COPY
 
82
fi
 
83
 
 
84
if [ -f $EXISTING_DN_ENTRIES ]
 
85
then
 
86
        rm $EXISTING_DN_ENTRIES
 
87
fi
 
88
 
 
89
if [ -f $NEW_GRID_MAP_FILE ] ; then
 
90
        rm $NEW_GRID_MAP_FILE
 
91
fi
 
92
 
 
93
 
 
94
${GLOBUS_SH_CHMOD-chmod} 644 $GRID_MAP_FILE
 
95
if [ $? -ne 0 ]
 
96
then
 
97
        echo "ERROR: Could not change mode of $GRID_MAP_FILE back to 644" >&2
 
98
    exit 1
 
99
fi
 
100
 
 
101
 
 
102
}
 
103
##############################################
 
104
 
 
105
# Main Logic
 
106
 
 
107
secconfdir="/etc/grid-security"
 
108
GRID_MAP_FILE=${GRIDMAP-${secconfdir}/grid-mapfile}
 
109
ECHO_DRYRUN=:
 
110
 
 
111
# Parse command line arguments
 
112
 
 
113
if [ $# -lt 4 ] ; then
 
114
    globus_args_short_usage
 
115
    exit 1
 
116
fi
 
117
 
 
118
while [ -n "$1" ]; do
 
119
    case "$1" in
 
120
        -dn) 
 
121
            shift
 
122
            if [ $# -ge 1 ] ; then
 
123
                dn=$1
 
124
                shift
 
125
            else
 
126
                globus_args_option_error "-dn" "needs a DN argument"
 
127
            fi
 
128
        ;;
 
129
        -ln )
 
130
            shift
 
131
            if [ $# -ge 1 ] ; then
 
132
                ln=$1
 
133
                shift
 
134
                if [ $# -ge 1 ] ; then
 
135
                    while test "`${GLOBUS_SH_ECHO-echo} $1|${GLOBUS_SH_CUT-cut} -c 1`" != "-" ; do
 
136
                        ln=$ln" "$1
 
137
                        shift
 
138
                        if [ $# -eq 0 ] ; then
 
139
                            break
 
140
                        fi
 
141
                    done
 
142
                fi
 
143
            else
 
144
                globus_args_option_error "-ln" "needs a list of user login names"
 
145
                exit 1
 
146
            fi
 
147
            ;;
 
148
        -d | -dryrun )
 
149
            ECHO_DRYRUN=echo
 
150
            shift
 
151
            ;;
 
152
        -f | -mapfile )
 
153
            opt=$1
 
154
            shift
 
155
            GRID_MAP_FILE=$1
 
156
            shift
 
157
            ;;
 
158
        * )
 
159
            globus_args_unrecognized_option "$1"
 
160
            ;;
 
161
    esac
 
162
 
 
163
done
 
164
 
 
165
secure_tmpdir="`${GLOBUS_SH_DIRNAME-dirname} \"${GRID_MAP_FILE}\"`"
 
166
 
 
167
if test ! \( -r "${secure_tmpdir}" -a -w "${secure_tmpdir}" \) ; then
 
168
    echo "ERROR: This script requires read/write permissions in ${secure_tmpdir}" >&2
 
169
    exit 1
 
170
fi 
 
171
 
 
172
GRID_MAP_FILE_COPY=${secure_tmpdir}/.mapfile.copy.$$
 
173
NEW_GRID_MAP_FILE=${secure_tmpdir}/.new_mapfile.$$
 
174
CONSISTENCY_CHECK=${secure_tmpdir}/.consistency_check.$$
 
175
EXISTING_DN_ENTRIES=${secure_tmpdir}/.existing_dn_entries.$$
 
176
 
 
177
trap Cleanup 1 2 3 6 9 13 15
 
178
 
 
179
# Verify mapfile existance
 
180
 
 
181
echo "Modifying $GRID_MAP_FILE ..."
 
182
if [ ! -f $GRID_MAP_FILE ] ; then
 
183
    echo "$GRID_MAP_FILE does not exist... Attempting to create $GRID_MAP_FILE"
 
184
    ${GLOBUS_SH_TOUCH-touch} $GRID_MAP_FILE
 
185
    if [ $? -ne 0 ] ; then
 
186
        echo "ERROR: Could not create $GRID_MAP_FILE" >&2
 
187
        exit 1
 
188
    fi
 
189
 
 
190
    ${GLOBUS_SH_CHMOD-chmod} 644 $GRID_MAP_FILE
 
191
    if [ $? -ne 0 ] ; then
 
192
        echo "ERROR: Could not set proper access mode of $GRID_MAP_FILE" >&2
 
193
        exit 1
 
194
    fi
 
195
else
 
196
    if [ ! -r $GRID_MAP_FILE ] ; then
 
197
        globus_args_option_error "$opt" "\"${GRID_MAP_FILE}\" is not readable."
 
198
        exit 1
 
199
    fi
 
200
 
 
201
    if [ ! -w $GRID_MAP_FILE ] ; then
 
202
        globus_args_option_error "$opt" "\"${GRID_MAP_FILE}\" is not writeable."
 
203
        exit 1
 
204
    fi
 
205
fi
 
206
 
 
207
if [ -z "$ln" -o -z "$dn" ] ; then
 
208
    echo "Both the -dn and the -ln arguments must be provided"
 
209
    globus_args_short_usage
 
210
    exit 1
 
211
fi
 
212
 
 
213
# Make a copy of production map file for comparison to original later
 
214
 
 
215
${GLOBUS_SH_CP-cp} $GRID_MAP_FILE $GRID_MAP_FILE_COPY
 
216
if [ $? -ne 0 ] ; then
 
217
    echo "ERROR: Could not make a copy of $GRID_MAP_FILE" >&2
 
218
    Cleanup
 
219
    exit 1
 
220
fi
 
221
 
 
222
# Change mode of existing map file to read only (logical UNIX lock)
 
223
 
 
224
${GLOBUS_SH_CHMOD-chmod} 400 $GRID_MAP_FILE
 
225
if [ $? -ne 0 ] ; then
 
226
    echo "ERROR: Could not change mode of $GRID_MAP_FILE" >&2
 
227
    Cleanup
 
228
    exit 1
 
229
fi
 
230
 
 
231
 
 
232
$ECHO_DRYRUN "Verifying that Local Name(s)=($ln) are legitimate local accounts."
 
233
 
 
234
for name in $ln ; do
 
235
    $ECHO_DRYRUN "Checking ln(s)=$name"
 
236
    ${libexecdir}/globus-is-local-user $name
 
237
    if  [ "$?" -eq 0 ] ; then
 
238
        $ECHO_DRYRUN "Local Name=$name does exist"
 
239
    else
 
240
        echo "entry not added because the LN(s) is/are not legitimate"
 
241
        $ECHO_DRYRUN "Local Name=$name does *NOT* exist"
 
242
        $ECHO_DRYRUN "Entry *NOT* added"
 
243
        Cleanup
 
244
        exit 1
 
245
    fi
 
246
done
 
247
 
 
248
$ECHO_DRYRUN "Local Name(s)=($ln) is/are valid. Requested entry will be added."
 
249
 
 
250
${GLOBUS_SH_TOUCH-touch} $NEW_GRID_MAP_FILE            
 
251
${GLOBUS_SH_CHMOD-chmod} 644 $NEW_GRID_MAP_FILE
 
252
if [ $? -ne 0 ] ; then
 
253
    echo "ERROR: Could not set proper access mode of $NEW_GRID_MAP_FILE" >&2
 
254
    Cleanup
 
255
    exit 1
 
256
fi
 
257
 
 
258
updated_existing_dn="false"
 
259
 
 
260
while read line || test ! -z "${line}" ; do
 
261
    # Check for double quote delimitor
 
262
    delim=`echo $line | cut -c1`
 
263
    if [ "X$delim" = "X\"" ]; then
 
264
        # DN is double quote delimited
 
265
        # Check for terminating double quote
 
266
        term_check=`echo $line | cut -c2- | ${GLOBUS_SH_GREP-grep} \"`
 
267
        if [ -z "$term_check" ]; then
 
268
            echo "The following entry is missing a closing double quote"
 
269
            echo "$line"
 
270
            Cleanup
 
271
            exit 1
 
272
        fi
 
273
        existing_dn=`echo $line | cut -f2 -d\"`
 
274
    else
 
275
        # No double quote delimitor on DN
 
276
        existing_dn=`echo $line | ${GLOBUS_SH_SED-sed} -e 's/\([^    ]*\)[   ]*.*/\1/'`
 
277
    fi
 
278
 
 
279
    if test ! "$dn" = "$existing_dn" ; then
 
280
        echo $line >> $NEW_GRID_MAP_FILE
 
281
    else
 
282
        for name in $ln ; do
 
283
            if test -z "`echo \"$line\" | ${GLOBUS_SH_GREP-grep} \"\<$name\>\"`"; then
 
284
                line="$line,$name"
 
285
                added_map="$added_map $name"
 
286
            else
 
287
                omitted_map="$omitted_map $name"
 
288
            fi
 
289
        done
 
290
        echo $line >> $NEW_GRID_MAP_FILE
 
291
        updated_existing_dn="$line"
 
292
    fi
 
293
done < $GRID_MAP_FILE_COPY
 
294
 
 
295
 
 
296
# Verify that no changes to original map file
 
297
# during the execution of this program
 
298
 
 
299
${GLOBUS_SH_DIFF-diff} $GRID_MAP_FILE_COPY $GRID_MAP_FILE > $CONSISTENCY_CHECK
 
300
if  [ -s $CONSISTENCY_CHECK ] ; then
 
301
    echo "ERROR: $GRID_MAP_FILE has changed since this program started" >&2
 
302
    echo "No changes will be made." >&2
 
303
    Cleanup
 
304
    exit 1
 
305
else
 
306
    # Restore proper permissions to original grid map file
 
307
    ${GLOBUS_SH_CHMOD-chmod} 644 $GRID_MAP_FILE
 
308
    if [ $? -ne 0 ] ; then
 
309
        echo "ERROR: Could not change mode of $GRID_MAP_FILE" >&2
 
310
        Cleanup
 
311
        exit 1
 
312
    fi
 
313
 
 
314
    ${GLOBUS_SH_CP-cp} $GRID_MAP_FILE_COPY $GRID_MAP_FILE.old
 
315
    if [ $? -ne 0 ] ; then
 
316
        echo "ERROR: Could not create a copy of $GRID_MAP_FILE" >&2
 
317
        Cleanup
 
318
        exit 1
 
319
    fi
 
320
fi
 
321
 
 
322
if [ "$updated_existing_dn" = "false" ]; then
 
323
    # format new entry of dn and ln 
 
324
    new_ln_entry=`echo $ln | ${GLOBUS_SH_SED-sed} -e 's/ /,/g'`
 
325
    new_mapfile_entry="\"$dn\" $new_ln_entry"
 
326
    # Append new entry to original grid map file
 
327
    $ECHO_DRYRUN "Appending new entry $new_mapfile_entry"
 
328
    if [ "$ECHO_DRYRUN" = "echo" ] ; then
 
329
        echo "Since ( dryrun, -d ) option was used no actions were carried out"
 
330
        Cleanup
 
331
        exit 0
 
332
    fi
 
333
 
 
334
    echo $new_mapfile_entry >> $GRID_MAP_FILE
 
335
    if [ $? -ne 0 ] ; then
 
336
        echo "ERROR: Could not add new entry to $GRID_MAP_FILE" >&2
 
337
        Cleanup
 
338
        exit 1
 
339
    else
 
340
        echo "New entry:"
 
341
        echo "$new_mapfile_entry"
 
342
        echo "(1) entry added"
 
343
    fi
 
344
else
 
345
    echo "DN $dn already exists."
 
346
 
 
347
    $ECHO_DRYRUN "Updating entry to $updated_existing_dn"
 
348
    if [ "$ECHO_DRYRUN" = "echo" ] ; then
 
349
        echo "Since ( dryrun, -d ) option was used no actions were carried out"
 
350
        Cleanup
 
351
        exit 0
 
352
    fi
 
353
 
 
354
    ${GLOBUS_SH_MV-mv} $NEW_GRID_MAP_FILE $GRID_MAP_FILE
 
355
 
 
356
    if [ $? -ne 0 ] ; then
 
357
        echo "ERROR: Could not create a new $GRID_MAP_FILE" >&2
 
358
        Cleanup
 
359
    else
 
360
        if test -n "$added_map" ; then
 
361
            if test -n "$omitted_map" ; then
 
362
                omitted_map=", already present and ignored:$omitted_map"
 
363
            fi
 
364
            echo "(added mappings:$added_map$omitted_map)"
 
365
            echo "Updated entry:"
 
366
            echo "$updated_existing_dn"
 
367
            echo "(1) entry modified"
 
368
        else
 
369
            echo "No changes were made - already present and ignored:$omitted_map"
 
370
        fi
 
371
    fi
 
372
fi
 
373
 
 
374
${GLOBUS_SH_CP-cp} $GRID_MAP_FILE_COPY $GRID_MAP_FILE.old
 
375
if [ $? -ne 0 ] ; then
 
376
    echo "ERROR: Could not create a copy of $GRID_MAP_FILE" >&2
 
377
    Cleanup
 
378
    exit 1
 
379
fi
 
380
 
 
381
Cleanup
 
382
 
 
383
exit 0
 
384