1 |
#!/usr/bin/perl -w |
2 |
# $Id: $ |
3 |
# |
4 |
# Helper script to restore the environment variables previously |
5 |
# wrapped into the environment variable GLEXEC_ENV using the |
6 |
# glexec_wrapenv.pl script. |
7 |
# Intended usage: |
8 |
# export GLEXEC_ENV=`glexec_wrapenv.pl` |
9 |
# /opt/glite/sbin/glexec glexec_unwrapenv.pl <YOUR-COMMAND> |
10 |
# By default the following environment variables are NOT unwrapped: |
11 |
# HOME LOGNAME USER X509_USER_PROXY _ (yes that's '_' !) |
12 |
# A user can add more env vars to be excluded using either |
13 |
# --exclude=A --exclude=B |
14 |
# or |
15 |
# --exclude=A,B,... |
16 |
# |
17 |
# Copyright (c) 2009 by |
18 |
# Jan Just Keijser (janjust@nikhef.nl) |
19 |
# Nikhef |
20 |
# Amsterdam |
21 |
# The Netherlands |
22 |
|
23 |
use strict; |
24 |
use warnings; |
25 |
|
26 |
use Compress::Zlib qw(inflateInit Z_STREAM_END Z_OK); |
27 |
use Getopt::Long qw(GetOptions); |
28 |
use MIME::Base64 qw(decode_base64); |
29 |
|
30 |
# These variables are excluded by default |
31 |
my @env_blacklist = ( "HOME", "LOGNAME", "USER", "X509_USER_PROXY", "_" ); |
32 |
my @exclude_env; |
33 |
|
34 |
GetOptions ("exclude=s" => \@exclude_env); |
35 |
@exclude_env = split( /,/, join( ',', @exclude_env, @env_blacklist) ); |
36 |
|
37 |
$ENV{GLEXEC_ENV} |
38 |
or die "GLEXEC_ENV not set. No environment to pass on"; |
39 |
|
40 |
# First, unwrap the Base64 encoded blob |
41 |
my $decoded_buf = decode_base64( $ENV{GLEXEC_ENV} ); |
42 |
|
43 |
# Then, decompress it into it's original space-separated set of Base64 blobs |
44 |
my $x = inflateInit() |
45 |
or die "Cannot create a inflation stream\n" ; |
46 |
|
47 |
my ($output, $status) = $x->inflate( \$decoded_buf ); |
48 |
|
49 |
die "inflation failed\n" |
50 |
unless $status == Z_STREAM_END or $status == Z_OK; |
51 |
|
52 |
# Split the space-separated set of Base64 blobs again into an array |
53 |
my @vars = split / /, $output; |
54 |
|
55 |
for (my $i = 0; $i <= $#vars; $i++) |
56 |
{ |
57 |
# Decode each Base64 encoded blob into a key=value pair |
58 |
my $keyvalue_pair = decode_base64( $vars[$i] ); |
59 |
my $pos = -1; |
60 |
|
61 |
# Look for the first '=' sign |
62 |
if (($pos = index( $keyvalue_pair, '=' )) > -1 ) |
63 |
{ |
64 |
# NOTE: using tricks like (\w+) (\w+) will NOT work |
65 |
# when environment variables span multiple lines |
66 |
|
67 |
# the "key" is everything before the first '=' sign |
68 |
my $key = substr( $keyvalue_pair, 0, $pos ); |
69 |
# the "value" is everything after the first '=' sign |
70 |
my $value = substr( $keyvalue_pair, $pos+1 ); |
71 |
|
72 |
# if the variable is not on our exclusion list, set it |
73 |
if ( ! grep { /$key/ } @exclude_env ) |
74 |
{ |
75 |
$ENV{$key} = $value; |
76 |
} |
77 |
} |
78 |
else |
79 |
{ |
80 |
# this should never happen, really |
81 |
printf STDERR "no = sign found in [$keyvalue_pair]!\n"; |
82 |
} |
83 |
} |
84 |
|
85 |
# Finally, execute the user payload command |
86 |
exec ( @ARGV ); |
87 |
|