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

« back to all changes in this revision

Viewing changes to programs/grid-mapfile-check-consistency.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
#! @PERL@
 
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
# grid-mapfile-check-consistency
 
20
 
 
21
use strict;
 
22
use warnings;
 
23
 
 
24
use English;
 
25
use FileHandle;
 
26
use File::Spec;
 
27
use Getopt::Long;
 
28
 
 
29
# Prototypes
 
30
sub main ( @ );
 
31
sub check_gridmap_location ();
 
32
sub parse_options ();
 
33
sub display_help ();
 
34
sub display_version ();
 
35
sub verify_mapfile_existence ();
 
36
sub load_gridmap_file ( $ );
 
37
sub check_valid_login_names ( $ );
 
38
 
 
39
# Gridmap file name, handle, and hash
 
40
my $GRID_MAP_FILE = "";
 
41
my $gridmap_handle;
 
42
my %LINES;
 
43
 
 
44
# Gridmap file check status codes
 
45
my $existence_check = 0;
 
46
my $load_check = 0;
 
47
my $valid_login_names_check = 0;
 
48
 
 
49
# DiRT support
 
50
# DIRT_TIMESTAMP="@DIRT_TIMESTAMP@"
 
51
# DIRT_BRANCH_ID="@DIRT_BRANCH_ID@"
 
52
 
 
53
# Invoke "main"
 
54
main ( @ARGV );
 
55
 
 
56
#######################################################################
 
57
# Function: main
 
58
# Description:
 
59
#       Main function of the program
 
60
# Parameters:
 
61
#       command line options
 
62
# Returns:
 
63
#       0 if checks passed
 
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
#######################################################################
 
69
sub main ( @ ) 
 
70
{
 
71
        check_gridmap_location();
 
72
 
 
73
        parse_options();
 
74
 
 
75
        print "Checking " . $GRID_MAP_FILE . " grid mapfile\n";
 
76
 
 
77
        print "Verifying grid mapfile existence...";
 
78
        $existence_check = verify_mapfile_existence();
 
79
        if ($existence_check == 1) 
 
80
        {
 
81
                print "ERROR: " . $GRID_MAP_FILE . " file is not readable\n";
 
82
                exit 1;
 
83
        } 
 
84
        elsif ($existence_check == 2)
 
85
        {
 
86
            print "\nGrid mapfile " . $GRID_MAP_FILE . " is empty\n";
 
87
            exit 2;     
 
88
        }
 
89
        
 
90
        print "OK\n";
 
91
 
 
92
        print "Checking for duplicate entries...";
 
93
        $load_check = load_gridmap_file(\%LINES);
 
94
        if ($load_check == 1) 
 
95
        {       
 
96
                print "ERROR: Cannot load " . $GRID_MAP_FILE . "\n";
 
97
                exit 3;
 
98
        } 
 
99
        elsif ($load_check == 0) 
 
100
        {
 
101
                print "OK\n";
 
102
        }
 
103
 
 
104
        print "Checking for valid user names...";
 
105
        $valid_login_names_check = check_valid_login_names(\%LINES);
 
106
        if ($valid_login_names_check == 0) 
 
107
        {
 
108
                print "OK\n";
 
109
        } 
 
110
        elsif ($valid_login_names_check == 1) 
 
111
        {
 
112
                exit 4;
 
113
        }
 
114
        exit 0;
 
115
}
 
116
 
 
117
 
 
118
#######################################################################
 
119
# Helper functions
 
120
#######################################################################
 
121
# Function: check_gridmap_location
 
122
# Description:
 
123
#       Checks location of gridmap file conforming to rules 
 
124
#       defined by GSI
 
125
# Parameters:
 
126
#       None
 
127
# Returns:
 
128
#       None
 
129
#######################################################################
 
130
sub check_gridmap_location ()
 
131
{
 
132
        if (defined $ENV{"GRIDMAP"}) 
 
133
        {
 
134
                $GRID_MAP_FILE = $ENV{"GRIDMAP"};
 
135
        }
 
136
        else
 
137
        {
 
138
                if ($REAL_USER_ID == 0) 
 
139
                {
 
140
                        $GRID_MAP_FILE = "/etc/grid-security/grid-mapfile";
 
141
                } 
 
142
                else 
 
143
                {
 
144
                        my $local_gridmap_file = $ENV{"HOME"} . "/.gridmap";    
 
145
                        if (-e $local_gridmap_file && -r $local_gridmap_file) 
 
146
                        {                               
 
147
                                $GRID_MAP_FILE = $local_gridmap_file;
 
148
                        }
 
149
                        else 
 
150
                        {
 
151
                                $GRID_MAP_FILE = "/etc/grid-security/grid-mapfile";
 
152
                        }
 
153
                }
 
154
        }       
 
155
}
 
156
 
 
157
#######################################################################
 
158
# Function: parse_options
 
159
# Description:
 
160
#       Parses script options. If gridmap file was specified, 
 
161
#       new value overrides previous one.
 
162
# Parameters:
 
163
#       None
 
164
# Returns:
 
165
#       0 help of version options selected
 
166
#######################################################################
 
167
sub parse_options ()
 
168
{
 
169
        my $parser;
 
170
        
 
171
        my $help = 0;
 
172
        my $version = 0;
 
173
        my $mapfile = "";
 
174
        
 
175
        $parser = new Getopt::Long::Parser;
 
176
                      
 
177
        $parser->getoptions("usage|help|h" => \$help, 
 
178
                                            "version|v" => \$version,
 
179
                                            "mapfile|file|f=s" => \$mapfile);
 
180
 
 
181
        if ($help == 1) 
 
182
        {       
 
183
                display_help();
 
184
                exit 0;
 
185
        } 
 
186
        elsif ($version == 1) 
 
187
        {
 
188
                display_version();
 
189
                exit 0;
 
190
        } 
 
191
        elsif ($mapfile ne "") 
 
192
        {
 
193
            $GRID_MAP_FILE = File::Spec->rel2abs($mapfile);     
 
194
        }       
 
195
}
 
196
 
 
197
#######################################################################
 
198
# Function: display_help
 
199
# Description:
 
200
#       Displays help information
 
201
# Parameters:
 
202
#       None
 
203
# Returns:
 
204
#       None
 
205
#######################################################################
 
206
sub display_help ()
 
207
{
 
208
    my ($volume, $directory, $file) = File::Spec->splitpath($0);        
 
209
        
 
210
        print $file . " checks the consistency of the Grid mapfile\n";
 
211
        print "Options:\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";
 
216
}
 
217
 
 
218
#######################################################################
 
219
# Function: display_version
 
220
# Description:
 
221
#       Displays version information
 
222
# Parameters:
 
223
#       None
 
224
# Returns:
 
225
#       None
 
226
#######################################################################
 
227
sub display_version () 
 
228
{    
 
229
    my ($volume, $directory, $file) = File::Spec->splitpath($0);
 
230
   
 
231
    my $program_version = '$Revision: 1.13 $';    
 
232
    $program_version =~ /Revision: ([\d\.]+)/;
 
233
    print $file . ": " . $1 . "\n";
 
234
}
 
235
 
 
236
######################################################################
 
237
# Function: verify_mapfile_existence
 
238
# Description:
 
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.
 
245
# Parameters:
 
246
#       None
 
247
# Returns:
 
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 ()
 
253
{       
 
254
        if (-l $GRID_MAP_FILE) 
 
255
        {
 
256
                my $link = readlink($GRID_MAP_FILE);
 
257
                $GRID_MAP_FILE = $link;
 
258
        }
 
259
        
 
260
        if (! -r $GRID_MAP_FILE) 
 
261
        {
 
262
                return 1;
 
263
        }
 
264
        
 
265
        if (! -w $GRID_MAP_FILE) 
 
266
        {               
 
267
                print "\nWARNING: Grid mapfile " . $GRID_MAP_FILE . " is not writable\n";
 
268
        }
 
269
        
 
270
        if (-z $GRID_MAP_FILE) 
 
271
        {               
 
272
                return 2;
 
273
        }
 
274
        
 
275
        return 0;
 
276
}
 
277
 
 
278
 
 
279
#####################################################
 
280
# Function: load_gridmap_file
 
281
# Description:
 
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)
 
290
# Parameters:
 
291
#       Hash which is to hold entire gridmap as lines
 
292
# Returns:
 
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 ( $ )
 
298
{       
 
299
        $gridmap_handle = new FileHandle ("< $GRID_MAP_FILE");
 
300
        
 
301
        if (!defined ($gridmap_handle)) 
 
302
        {
 
303
                return 1;
 
304
        }       
 
305
 
 
306
        my ($key, $value);
 
307
        my $href = shift;
 
308
        
 
309
        my $duplicate_counter = 0;
 
310
                
 
311
        my $buffer;
 
312
        while (! $gridmap_handle->eof() ) 
 
313
        {               
 
314
                $buffer = $gridmap_handle->getline();
 
315
                chomp ($buffer);
 
316
                
 
317
                if ($buffer !~ /^\s*$/ && $buffer !~ /^\s*\#/) 
 
318
                {
 
319
                  $buffer =~ s/^\s*//;
 
320
                  $buffer =~ /^"(.*)"\s*(\S*)\s*$/;
 
321
 
 
322
          if (! defined ($1)) 
 
323
          {
 
324
              print "\nERROR: Missing double quotes in the following entry: " . $buffer . "\n";                       
 
325
          }
 
326
          elsif (! defined ($2)) 
 
327
          {
 
328
              print "\nERROR: Missing user name(s) in the following entry: " . $buffer . "\n";
 
329
          }
 
330
          else 
 
331
          {
 
332
              ($key, $value) = ($1,$2);
 
333
          
 
334
                      if (exists ($$href{$key}) ) 
 
335
                      {
 
336
                             $duplicate_counter++;
 
337
                             print "\nERROR: Found duplicate entry: " . $buffer . "\n";
 
338
                      } 
 
339
                      else 
 
340
                      {
 
341
                            $$href{$key} = $value;
 
342
                      }
 
343
          }
 
344
                }
 
345
        }
 
346
        if ($duplicate_counter > 0) 
 
347
        {
 
348
                print "ERROR: Found " . $duplicate_counter . " duplicate(s)\n";         
 
349
                return 2;
 
350
        }       
 
351
        return 0;
 
352
}
 
353
 
 
354
#####################################################
 
355
# Function: check_valid_login_names
 
356
# Description:
 
357
#       Checks all gridmap entries if user names are
 
358
#       valid login names.
 
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.
 
363
# Parameters:
 
364
#       Reference to a hash holding gridmap entries
 
365
# Returns:
 
366
#       0 all entries are valid
 
367
#       1 if there are invalid login names
 
368
#####################################################
 
369
sub check_valid_login_names ( $ ) 
 
370
{
 
371
        my ($href, $entry, $user, $user_id);
 
372
        my @user_names;
 
373
        
 
374
        my %invalid_login_names = ();
 
375
                
 
376
        $href = shift;
 
377
 
 
378
        foreach $entry (values(%$href)) 
 
379
        {
 
380
            if ($entry =~ /,/) 
 
381
            {
 
382
              @user_names = split (/,/, $entry);
 
383
            } 
 
384
            else 
 
385
            {
 
386
              $user_names[0] = $entry;
 
387
            }       
 
388
            
 
389
            foreach $user (@user_names) 
 
390
            {
 
391
                    $user_id = getpwnam("$user");
 
392
                    if (! defined ($user_id) || $user_id < 0)  
 
393
                    {
 
394
                            print "\nERROR: " . $user . " is not a valid local username\n";
 
395
                            if (! exists ($invalid_login_names{"$user"}) )
 
396
                            {
 
397
                                $invalid_login_names{"$user"} = 1;
 
398
                            }  
 
399
                    }
 
400
            }
 
401
        }
 
402
        my $counter = keys (%invalid_login_names);
 
403
        if ($counter > 0) 
 
404
        {
 
405
                print "ERROR: Found " . $counter . " invalid username(s)\n";
 
406
                return 1;
 
407
        } 
 
408
        else
 
409
        {
 
410
                return 0;
 
411
        }
 
412
}