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