1 |
#!/usr/bin/perl -w |
2 |
|
3 |
# Copyright (c) Members of the EGEE Collaboration. 2009. |
4 |
# See http://www.eu-egee.org/partners/ for details on the copyright |
5 |
# holders. |
6 |
# |
7 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
8 |
# you may not use this file except in compliance with the License. |
9 |
# You may obtain a copy of the License at |
10 |
# |
11 |
# http://www.apache.org/licenses/LICENSE-2.0 |
12 |
# |
13 |
# Unless required by applicable law or agreed to in writing, software |
14 |
# distributed under the License is distributed on an "AS IS" BASIS, |
15 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 |
# See the License for the specific language governing permissions and |
17 |
# limitations under the License. |
18 |
# |
19 |
# Authors: Jan Just Keijser |
20 |
# NIKHEF Amsterdam, the Netherlands |
21 |
# janjust@nikhef.nl |
22 |
|
23 |
# $Id: $ |
24 |
# |
25 |
# Helper script to restore the environment variables previously |
26 |
# wrapped into the environment variable GLEXEC_ENV using the |
27 |
# glexec_wrapenv.pl script. |
28 |
# |
29 |
# Intended usage: |
30 |
# export GLEXEC_ENV=`glexec_wrapenv.pl` |
31 |
# /opt/glite/sbin/glexec glexec_unwrapenv.pl -- <YOUR-COMMAND> |
32 |
# |
33 |
# By default the following environment variables are NOT unwrapped: |
34 |
# HOME LOGNAME USER X509_USER_PROXY _ (yes that's '_' !) |
35 |
# A user can add more env vars to be excluded using either |
36 |
# --exclude=A --exclude=B |
37 |
# or |
38 |
# --exclude=A,B,... |
39 |
# |
40 |
|
41 |
use strict; |
42 |
use warnings; |
43 |
|
44 |
use Compress::Zlib qw(inflateInit Z_STREAM_END Z_OK); |
45 |
use Getopt::Long qw(GetOptions); |
46 |
use MIME::Base64 qw(decode_base64); |
47 |
|
48 |
# These variables are excluded by default |
49 |
my @env_blacklist = ( "HOME", "LOGNAME", "USER", "X509_USER_PROXY", "_" ); |
50 |
my @exclude_env; |
51 |
|
52 |
GetOptions ("exclude=s" => \@exclude_env); |
53 |
@exclude_env = split( /,/, join( ',', @exclude_env, @env_blacklist) ); |
54 |
|
55 |
$ENV{GLEXEC_ENV} |
56 |
or die "GLEXEC_ENV not set. No environment to pass on"; |
57 |
|
58 |
# First, unwrap the Base64 encoded blob |
59 |
my $decoded_buf = decode_base64( $ENV{GLEXEC_ENV} ); |
60 |
|
61 |
# Then, decompress it into it's original space-separated set of Base64 blobs |
62 |
my $x = inflateInit() |
63 |
or die "Cannot create a inflation stream\n" ; |
64 |
|
65 |
my ($output, $status) = $x->inflate( \$decoded_buf ); |
66 |
|
67 |
die "inflation failed\n" |
68 |
unless $status == Z_STREAM_END or $status == Z_OK; |
69 |
|
70 |
# Split the space-separated set of Base64 blobs again into an array |
71 |
my @vars = split / /, $output; |
72 |
|
73 |
for (my $i = 0; $i <= $#vars; $i++) |
74 |
{ |
75 |
# Decode each Base64 encoded blob into a key=value pair |
76 |
my $keyvalue_pair = decode_base64( $vars[$i] ); |
77 |
my $pos = -1; |
78 |
|
79 |
# Look for the first '=' sign |
80 |
if (($pos = index( $keyvalue_pair, '=' )) > -1 ) |
81 |
{ |
82 |
# NOTE: using tricks like (\w+) (\w+) will NOT work |
83 |
# when environment variables span multiple lines |
84 |
|
85 |
# the "key" is everything before the first '=' sign |
86 |
my $key = substr( $keyvalue_pair, 0, $pos ); |
87 |
# the "value" is everything after the first '=' sign |
88 |
my $value = substr( $keyvalue_pair, $pos+1 ); |
89 |
|
90 |
# if the variable is not on our exclusion list, set it |
91 |
if ( ! grep { /$key/ } @exclude_env ) |
92 |
{ |
93 |
$ENV{$key} = $value; |
94 |
} |
95 |
} |
96 |
else |
97 |
{ |
98 |
# this should never happen, really |
99 |
printf STDERR "no = sign found in [$keyvalue_pair]!\n"; |
100 |
} |
101 |
} |
102 |
|
103 |
# Finally, execute the user payload command |
104 |
exec ( @ARGV ); |
105 |
|