forked from opensciencegrid/osg-make-vomsdir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosg-make-vomsdir
executable file
·392 lines (340 loc) · 10.8 KB
/
osg-make-vomsdir
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env perl
=head1 osg-make-vomsdir
osg-make-vomsdir - Make the set of LSC files needed to verify VOMS servers
=head2 Synopsis
make-voms-dir --vo <VO>
--host <host>
--vomsdir <DIR>
--vomses <FILE>
--help
=head2 Description
=over 4
=item B<--vo> <VO>
Only generate LSC files for the specified VO.
=item B<--host> <HOST>
Only generate LSC files for VOs hosted on the specified HOST.
=item B<--vomsdir <DIR>
Put LSC files in DIR. Defaults to 'vomsdir' in current directory.
=item B<--vomses <FILE>
Find list of files in FILE. Default to 'vomses' in the current directory.
=item B<--help>, B<--usage>
Print a usage message.
=back
This program creates the set of LSC files needed to verify VOMS servers. It works by contacting
a list of VOMS servers (read from the standard vomses file) and discerning what DN/CN each one
uses, then writes that into an LSC file.
If you run as root, this program will use your hostcert/key to authenticate to the VOMS servers.
If you run as non-root, you must have a valid proxy to authenticate to the VOMS servers.
=cut
use FileHandle;
use strict;
use warnings;
use Getopt::Long;
use Cwd 'abs_path';
use English;
# Ideas for future enhancement
# Add --check: Report if any LSC files are missing, or if there are extra LSC files,
# or if there appear to be any other problems.
#-----------------------------------------
#
# Global Variables
#
#-----------------------------------------
my $version = "1.0.0";
my $restrict_to_vo = "";
my $restrict_to_host = "";
my $voms_dir = "";
my $vomses_file = "";
my $cert_dir;
my $cert_file;
my $key_file;
my $debug = 0;
my @errors;
#-----------------------------------------
#
# Main loop
#
#-----------------------------------------
parse_command_line();
$cert_dir = find_cert_dir();
($cert_file, $key_file) = find_cert_and_key();
print_startup_info();
make_vomsdir();
print_errors();
exit(0);
#-----------------------------------------
#
# Function: parse_command_line
# Purpose: Parse all the command-line arguments.
# Make sure voms_dir and vomses_file are full paths
#
#-----------------------------------------
sub parse_command_line
{
my $parsed_ok;
$parsed_ok = GetOptions("vo=s" => \$restrict_to_vo,
"host=s" => \$restrict_to_host,
"vomsdir=s" => \$voms_dir,
"vomses=s" => \$vomses_file,
"debug" => \$debug,
"help|usage" => \&usage);
if (!$parsed_ok) {
usage();
}
if ($voms_dir eq "") {
$voms_dir = "vomsdir";
}
$voms_dir = abs_path($voms_dir);
if ($vomses_file eq "") {
$vomses_file = abs_path("vomses");
}
return;
}
#-----------------------------------------
#
# Function: usage
# Purpose: Print a very lovely usage statement.
#
#-----------------------------------------
sub usage
{
print "Usage: osg-make-vomsdir [-vo VO] [-host HOST] [--vomsdir DIR] [--vomses FILE] [--help]\n";
print "\n";
print "Purpose: Generate the LSC files for a set of VOs. The files will be placed into the\n";
print "standard 'vomsdir' hierarchy.\n";
print "\n";
print "Arguments:\n";
print " --vo <VO>: Only generate LSC files for the specified VO.\n";
print " --host <HOST>: Only generate LSC files for VOs hosted on the specified HOST.\n";
print " --vomsdir <DIR>: Put LSC files in DIR. Defaults to 'vomsdir' in current directory.\n";
print " --vomses <FILE>: Find list of files in FILE. Default to 'vomses' in the current directory.\n";
print " --help: Print this message.\n";
print "\n";
print "If you run as root, this program will use your hostcert/key to authenticate to the VOMS servers.\n";
print "If you run as non-root, you must have a valid proxy to authenticate to the VOMS servers.\n";
print "\n";
exit(1);
}
#-----------------------------------------
#
# Function: find_cert_dir
# Purpose: Return the absolute path of the directory where all the CA certificates are stored.
#
#-----------------------------------------
sub find_cert_dir
{
my $cert_dir = "";
if (defined($ENV{X509_CERT_DIR})) {
$cert_dir = $ENV{X509_CERT_DIR};
} elsif ( -d "/etc/grid-security/certificates") {
$cert_dir = "/etc/grid-security/certificates"
} else {
die "Can't find the CA certificate directory.\n";
}
$cert_dir = abs_path($cert_dir);
return $cert_dir;
}
#-----------------------------------------
#
# Function: find_cert_and_key
# Purpose: We need to authenticate to the VOMS servers in order to find out
# what DN they use. If we're root, we use the hostcert/key. If we're
# non-root, we use the proxy.
#
#-----------------------------------------
sub find_cert_and_key
{
my $cert;
my $key;
if ($UID == 0) {
$cert = "/etc/grid-security/hostcert.pem";
$key = "/etc/grid-security/hostkey.pem";
if (! -r $cert) {
die "Can't read $cert (needed to connect to VOMS servers).";
}
if (! -r $key) {
die "Can't read $key (needed to connect to VOMS servers).";
}
} else {
my $output;
my @outputs;
$output = `which grid-proxy-info`;
if ($? != 0) {
die "Can't find grid-proxy-info";
}
$output = `grid-proxy-info -exists`;
if ($? != 0) {
die "You don't have a proxy, please run grid-proxy-init";
}
$output = `grid-proxy-info -exists -hours 1`;
if ($? != 0) {
die "Your proxy expires in less than an hour, please renew it with grid-proxy-init";
}
@outputs = `grid-proxy-info -path`;
if ($? != 0) {
die "Can't find the path to your proxy.";
}
$cert = $outputs[0];
chomp($cert);
$key = "";
}
return ($cert, $key);
}
#-----------------------------------------
#
# Function: print_startup_info
# Purpose: Print some nice information before we start our work.
#
#-----------------------------------------
sub print_startup_info
{
print "osg-make-vomsdir version $version\n";
print "Will make $voms_dir:\n";
print " ...using CAs in $cert_dir\n";
print " ...finding VOMS servers from $vomses_file\n";
print " ...authenticating to VOMS server with $cert_file";
if ($key_file) {
print "/$key_file";
}
print "\n";
if ($restrict_to_vo) {
print " ...only for VO $restrict_to_vo\n";
}
if ($restrict_to_host) {
print " ...only fo VOs hosted on $restrict_to_host\n";
}
print "\n";
return;
}
#-----------------------------------------
#
# Function: make_vomsdir
# Purpose: The main work function that creates the vomsdir.
#
#-----------------------------------------
sub make_vomsdir
{
my $fh = new FileHandle $vomses_file;
if (not defined $fh) {
die "ERROR: Can't read $vomses_file\n";
}
while (my $line = <$fh>) {
if ($line =~ /^\"(\S*?)\"\s+\"(\S*?)\"\s+\"(\S*?)\"\s+\"(.*?)\"/) {
my ($vo, $server, $port, $dn) = ($1, $2, $3, $4);
if ($restrict_to_vo ne "" && $restrict_to_vo ne $vo) {
next;
}
if ($restrict_to_host ne "" && $restrict_to_host ne $server) {
next;
}
print "Making LSC file for $vo/$server... ";
# Note that we use the DN we get from openssl instead of the DN we
# could get out of the vomses file, to make sure it's right. (In one
# case, I found it was right from VOMS-Admin, but not VOMS.)
my ($success, $actual_dn, $ca) = find_dn_ca($server, $port, $vo);
if ($success) {
print "Done\n";
system("mkdir", "-p", "$voms_dir/$vo");
my $filename = "$voms_dir/$vo/$server.lsc";
my $lsc_fh = FileHandle->new($filename, "w+");
if (!defined($lsc_fh)) {
die "Can't write $filename\n";
}
print $lsc_fh "$actual_dn\n";
print $lsc_fh "$ca\n";
$lsc_fh->close();
} else {
print "FAILED!\n";
}
}
}
return;
}
#-----------------------------------------
#
# Function: find_dn_ca
# Purpose: For a single VOMS server, find the DN of the VOMS server and the DN of the
# CA that provided the VOMS's server DN.
#
#-----------------------------------------
sub find_dn_ca
{
my $server = $_[0];
my $port = $_[1];
my $vo = $_[2];
my @outputs;
my $dn = "";
my $ca = "";
my $cant_contact_voms = 0;
my $success = 0;
my $certarg = "";
my $keyarg = "";
if ($cert_file) {
$certarg = "-cert $cert_file";
}
if ($key_file) {
$keyarg = "-key $key_file";
}
eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm 10;
@outputs = `openssl s_client -connect $server:$port -prexit -CApath $cert_dir $certarg $keyarg < /dev/null 2>&1`;
if ($? != 0) {
$cant_contact_voms = 1;
}
alarm 0;
};
if ($@ || $cant_contact_voms) {
push (@errors, "ERROR: Can't contact $vo/$server. LSC file not created.\n");
$dn = "Undefined";
$ca = "Undefined";
} else {
for my $line (@outputs) {
# print "$line";
if ($line =~ /issuer=(.*)/) {
$ca = $1;
}
if ($line =~ /subject=(.*)/) {
$dn = $1;
}
}
if ($ca eq "") {
push (@errors, "ERROR: Can't find CA for $vo/$server. LSC file not created.\n");
if ($debug) {
print "******************* CA NOT FOUND\n";
for my $line (@outputs) {
print $line;
}
print "*******************\n";
}
} else {
$success = 1;
}
}
return ($success, $dn, $ca);
}
#-----------------------------------------
#
# Function: print_errors
# Purpose: Print all errors that occurred. We do it all at once,
# for easy-to-read output.
#
#-----------------------------------------
sub print_errors
{
my $num_errors = scalar(@errors);
print "\n";
if ($num_errors eq 0) {
print "Successful.\n";
} else {
if ($num_errors eq 1) {
print "1 Error:\n";
} else {
print "$num_errors Errors:\n";
}
foreach my $error (@errors) {
print " $error";
}
}
return;
}