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 |
# Wrapper script to wrap the current environment into a single |
26 |
# environment variable GLEXEC_ENV. This variable is passed |
27 |
# onto the glexec child process, where it can be unpacked to |
28 |
# restore the environment variables that were lost when the |
29 |
# set-uid glexec was invoked. |
30 |
# |
31 |
# Intended usage: |
32 |
# export GLEXEC_ENV=`glexec_wrapenv.pl` |
33 |
# /opt/glite/sbin/glexec glexec_unwrapenv.pl -- <YOUR-COMMAND> |
34 |
# |
35 |
# By default the following environment variables are NOT wrapped: |
36 |
# HOME LOGNAME USER X509_USER_PROXY _ (yes that's '_' !) |
37 |
# A user can add more env vars to be excluded using either |
38 |
# --exclude=A --exclude=B |
39 |
# or |
40 |
# --exclude=A,B,... |
41 |
# |
42 |
|
43 |
use strict; |
44 |
use warnings; |
45 |
|
46 |
use Compress::Zlib qw(deflateInit Z_OK); |
47 |
use Getopt::Long qw(GetOptions); |
48 |
use MIME::Base64 qw(encode_base64); |
49 |
|
50 |
# These variables are excluded by default |
51 |
my @env_blacklist = ( "HOME", "LOGNAME", "USER", "X509_USER_PROXY", "_" ); |
52 |
|
53 |
my @exclude_env; |
54 |
my $key; |
55 |
my $buf; |
56 |
my $encoded_buf = ''; |
57 |
my $output = ''; |
58 |
|
59 |
GetOptions ("exclude=s" => \@exclude_env); |
60 |
@exclude_env = split( /,/, join( ',', @exclude_env, @env_blacklist) ); |
61 |
|
62 |
# go through all environment variables and encode them as separate |
63 |
# key-value pair entities. This will enable us to later unpack them. |
64 |
foreach $key (keys(%ENV)) |
65 |
{ |
66 |
if ( ! grep { /$key/ } @exclude_env ) |
67 |
{ |
68 |
$buf = $key . "=" . $ENV{$key}; |
69 |
$encoded_buf .= encode_base64($buf, '') . " "; |
70 |
} |
71 |
else |
72 |
{ |
73 |
printf STDERR "Skipping $key\n"; |
74 |
} |
75 |
} |
76 |
|
77 |
# Compress the encoded env vars to save some memory |
78 |
my $x = deflateInit() |
79 |
or die "Cannot create a deflation stream\n" ; |
80 |
|
81 |
my ($deflated_buf, $status) = $x->deflate( $encoded_buf ); |
82 |
$status == Z_OK or die "deflation failed\n"; |
83 |
$output = $deflated_buf; |
84 |
|
85 |
($deflated_buf, $status) = $x->flush(); |
86 |
$status == Z_OK or die "deflation failed\n"; |
87 |
$output .= $deflated_buf; |
88 |
|
89 |
# Finally, encode the compressed stream again and print it out |
90 |
print encode_base64( $output, '' ); |
91 |
|