4
# Copyright 1999-2006 University of Chicago
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
10
# http://www.apache.org/licenses/LICENSE-2.0
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.
19
# grid-mapfile-check-consistency
31
sub check_gridmap_location ();
34
sub display_version ();
35
sub verify_mapfile_existence ();
36
sub load_gridmap_file ( $ );
37
sub check_valid_login_names ( $ );
39
# Gridmap file name, handle, and hash
40
my $GRID_MAP_FILE = "";
44
# Gridmap file check status codes
45
my $existence_check = 0;
47
my $valid_login_names_check = 0;
50
# DIRT_TIMESTAMP="@DIRT_TIMESTAMP@"
51
# DIRT_BRANCH_ID="@DIRT_BRANCH_ID@"
56
#######################################################################
59
# Main function of the program
61
# command line options
64
# 1 if gridmap file is not readable
65
# 2 if gridmap file is empty
66
# 3 if loading gridmap file failed (e.g. hardware errors)
67
# 4 if there are invalid login names in gridmap file
68
#######################################################################
71
check_gridmap_location();
75
print "Checking " . $GRID_MAP_FILE . " grid mapfile\n";
77
print "Verifying grid mapfile existence...";
78
$existence_check = verify_mapfile_existence();
79
if ($existence_check == 1)
81
print "ERROR: " . $GRID_MAP_FILE . " file is not readable\n";
84
elsif ($existence_check == 2)
86
print "\nGrid mapfile " . $GRID_MAP_FILE . " is empty\n";
92
print "Checking for duplicate entries...";
93
$load_check = load_gridmap_file(\%LINES);
96
print "ERROR: Cannot load " . $GRID_MAP_FILE . "\n";
99
elsif ($load_check == 0)
104
print "Checking for valid user names...";
105
$valid_login_names_check = check_valid_login_names(\%LINES);
106
if ($valid_login_names_check == 0)
110
elsif ($valid_login_names_check == 1)
118
#######################################################################
120
#######################################################################
121
# Function: check_gridmap_location
123
# Checks location of gridmap file conforming to rules
129
#######################################################################
130
sub check_gridmap_location ()
132
if (defined $ENV{"GRIDMAP"})
134
$GRID_MAP_FILE = $ENV{"GRIDMAP"};
138
if ($REAL_USER_ID == 0)
140
$GRID_MAP_FILE = "/etc/grid-security/grid-mapfile";
144
my $local_gridmap_file = $ENV{"HOME"} . "/.gridmap";
145
if (-e $local_gridmap_file && -r $local_gridmap_file)
147
$GRID_MAP_FILE = $local_gridmap_file;
151
$GRID_MAP_FILE = "/etc/grid-security/grid-mapfile";
157
#######################################################################
158
# Function: parse_options
160
# Parses script options. If gridmap file was specified,
161
# new value overrides previous one.
165
# 0 help of version options selected
166
#######################################################################
175
$parser = new Getopt::Long::Parser;
177
$parser->getoptions("usage|help|h" => \$help,
178
"version|v" => \$version,
179
"mapfile|file|f=s" => \$mapfile);
186
elsif ($version == 1)
191
elsif ($mapfile ne "")
193
$GRID_MAP_FILE = File::Spec->rel2abs($mapfile);
197
#######################################################################
198
# Function: display_help
200
# Displays help information
205
#######################################################################
208
my ($volume, $directory, $file) = File::Spec->splitpath($0);
210
print $file . " checks the consistency of the Grid mapfile\n";
212
print "--help, -help, --usage, -usage, --h, -h Displays help\n";
213
print "--version, -version, --v, -v Displays version\n";
214
print "--mapfile FILE, -mapfile FILE, --file FILE,
215
-file FILE, --f FILE, -f FILE Path of gridmap to be used\n";
218
#######################################################################
219
# Function: display_version
221
# Displays version information
226
#######################################################################
227
sub display_version ()
229
my ($volume, $directory, $file) = File::Spec->splitpath($0);
231
my $program_version = '$Revision: 1.13 $';
232
$program_version =~ /Revision: ([\d\.]+)/;
233
print $file . ": " . $1 . "\n";
236
######################################################################
237
# Function: verify_mapfile_existence
239
# If a gridmap file is a symlink, follows it.
240
# Checks if the gridmap file is readable.
241
# Checks if the gridmap file is writable.
242
# Checks if the gridmap file is empty.
243
# If the file is either non-writable or empty,
244
# appropriate warning is printed.
248
# 0 gridmap file is readable, writable and non-empty
249
# 1 gridmap file is non-readable (failed)
250
# 2 gridmap file is non-writable
251
#######################################################################
252
sub verify_mapfile_existence ()
254
if (-l $GRID_MAP_FILE)
256
my $link = readlink($GRID_MAP_FILE);
257
$GRID_MAP_FILE = $link;
260
if (! -r $GRID_MAP_FILE)
265
if (! -w $GRID_MAP_FILE)
267
print "\nWARNING: Grid mapfile " . $GRID_MAP_FILE . " is not writable\n";
270
if (-z $GRID_MAP_FILE)
279
#####################################################
280
# Function: load_gridmap_file
282
# Loads entire gridmap into a single hash.
283
# Single DN may be mapped to multiple users
284
# therefore hash contains lists not scalars.
285
# It speeds up later non-sequential access.
286
# It detects some errors:
287
# - missing double quotes
288
# - missing usernames
289
# - duplicate entries (logical names)
291
# Hash which is to hold entire gridmap as lines
293
# 0 gridmap file loaded successfully
294
# 1 gridmap file loading failed
295
# 2 if there are duplicate entries
296
#####################################################
297
sub load_gridmap_file ( $ )
299
$gridmap_handle = new FileHandle ("< $GRID_MAP_FILE");
301
if (!defined ($gridmap_handle))
309
my $duplicate_counter = 0;
312
while (! $gridmap_handle->eof() )
314
$buffer = $gridmap_handle->getline();
317
if ($buffer !~ /^\s*$/ && $buffer !~ /^\s*\#/)
320
$buffer =~ /^"(.*)"\s*(\S*)\s*$/;
324
print "\nERROR: Missing double quotes in the following entry: " . $buffer . "\n";
326
elsif (! defined ($2))
328
print "\nERROR: Missing user name(s) in the following entry: " . $buffer . "\n";
332
($key, $value) = ($1,$2);
334
if (exists ($$href{$key}) )
336
$duplicate_counter++;
337
print "\nERROR: Found duplicate entry: " . $buffer . "\n";
341
$$href{$key} = $value;
346
if ($duplicate_counter > 0)
348
print "ERROR: Found " . $duplicate_counter . " duplicate(s)\n";
354
#####################################################
355
# Function: check_valid_login_names
357
# Checks all gridmap entries if user names are
359
# If invalid user names are found, then each
360
# invalid entry is printed.
361
# Prints number of invalid entries if this
362
# number is non-zero.
364
# Reference to a hash holding gridmap entries
366
# 0 all entries are valid
367
# 1 if there are invalid login names
368
#####################################################
369
sub check_valid_login_names ( $ )
371
my ($href, $entry, $user, $user_id);
374
my %invalid_login_names = ();
378
foreach $entry (values(%$href))
382
@user_names = split (/,/, $entry);
386
$user_names[0] = $entry;
389
foreach $user (@user_names)
391
$user_id = getpwnam("$user");
392
if (! defined ($user_id) || $user_id < 0)
394
print "\nERROR: " . $user . " is not a valid local username\n";
395
if (! exists ($invalid_login_names{"$user"}) )
397
$invalid_login_names{"$user"} = 1;
402
my $counter = keys (%invalid_login_names);
405
print "ERROR: Found " . $counter . " invalid username(s)\n";