rungenilib.proxy.in 8.24 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/perl -w

#
# Copyright (c) 2000-2014 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
25
use strict;
26
use English;
27
use Getopt::Std;
28
29
30
31
32
33
34
35
36
use BSD::Resource;
use POSIX qw(:signal_h);
    
#
# Simply a wrapper for the geni-lib python environment
#
sub usage()
{
    print STDOUT
37
38
39
	"Usage: rungenilib.proxy [-J] -u user [args ...]\n".
        "Where options and arguments are those required by geni-lib,\n".
	"except for -J which says to run script in a jail.\n";
40
41
42
 
    exit(-1);
}
43
my $optlist = "u:vpb:WJ";
44
45
46
my $user;
my $getparams= 0;
my $paramsize;
47
my $warningsfatal = 0;
48
my $usejail = 0;
49
50
51
52
53
54
55
56

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
my $TESTMODE = 0;
my $GENILIB  = "$TB/lib/geni-lib/";
57
my $JAILPROG = "$TB/libexec/genilib-jail";
58
59
60
61
62
my $debug    = 0;

# Locals
my $tempdir = "/tmp/genilib-$$";
my $ifile   = "$$.py";
63
64
my $ofile   = "$$.out";
my $pfile   = "$$.json";
65

66
67
# Protos
sub fatal($);
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

#
# Turn off line buffering on output
#
$| = 1;

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/usr/bin:/sbin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
$ENV{"PYTHONPATH"} = $GENILIB;

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libtestbed;

#
88
# Parse command arguments. Once we return from getopts, all that should
89
90
# left are the required arguments.
#
91
92
my %options = ();
if (! getopts($optlist, \%options)) {
93
94
    usage();
}
95
96
97
98
99
100
101
102
103
104
105
106
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"p"})) {
    $getparams = 1;
}
if (defined($options{"b"})) {
    $paramsize = $options{"b"};
}
if (defined($options{"u"})) {
    $user = $options{"u"};
}
107
108
109
if (defined($options{"W"})) {
    $warningsfatal = 1;
}
110
111
112
if (defined($options{"J"})) {
    $usejail = 1;
}
113
114
115
116
117
118

#
# First option has to be the -u option, the user to run this script as.
# In testmode, we are not run as root, so run as the current user, and
# in the current directory (not invoked with ssh in testmode). 
#
119
my ($unix_uid,$unix_gid,$unix_glist);
120
121
122
123
124
125
if (! $TESTMODE) {
    if ($UID != 0) {
	die("*** $0:\n".
	    "    Must be root to run this script!");
    }

126
    (undef,undef,$unix_uid) = getpwnam($user) or
127
128
129
130
131
132
133
134
	die("*** $0:\n".
	    "    No such user $user\n");

    #
    # Need the entire group list for the user, cause of subgroups, and
    # cause thats the correct thing to do. Too bad perl does not have a
    # getgrouplist function like the C library.
    #
135
136
137
    $unix_glist = `id -G $user`;
    if ($unix_glist =~ /^([\d ]*)$/) {
	$unix_glist = $1;
138
139
140
    }
    else {
	die("*** $0:\n".
141
	    "    Unexpected results from 'id -G $user': $unix_glist\n");
142
143
144
    }

    # Need to split off the first group and create a proper list for $GUID.
145
146
147
    my @gglist   = split(" ", $unix_glist);
    $unix_gid    = $gglist[0];
    $unix_glist  = "$unix_gid $unix_glist";
148
149

    # Flip to user and never go back!
150
151
152
153
154
155
156
    if (!$usejail) {
	$GID            = $unix_gid;
	$EGID           = $unix_glist;
	$EUID = $UID    = $unix_uid;
	$ENV{'USER'}    = $user;
	$ENV{'LOGNAME'} = $user;
    }
157
158
159
160

    #
    # Create the tempdir and chmod it to keep people out.
    #
161
162
163
164
165
166
    # Note that in the $usejail case, the directory will be owned by root.
    # We leave it that way til the end so that a malicious user won't slip
    # in and insert symlinks for $ifile and $pfile. Since those files will
    # also be written as root, we need to make sure they aren't symlinks to
    # somewhere that the user could not normally access.
    #
167
168
169
170
171
172
173
174
175
176
    if (! mkdir($tempdir, 0750)) {
	die("Could not mkdir $tempdir: $!\n");
    }
    if (! chmod(0750, $tempdir)) {
	die("Could not chmod $tempdir to 0750: $!\n");
    }
    if (! chdir($tempdir)) {
	die("Could not chdir to $tempdir: $!\n");
    }
}
177
178
179
180
elsif ($usejail && $UID != 0) {
    die("*** $0:\n".
	"    Must be root to run this script with -J!");
}
181
182

#
183
184
# Take our input and write it to the temp file. If we are also getting
# a parameter file in the stream, write that to another file.
185
#
186
187
188
189
190
191
192
193
194
195
196
197
198
199
if (defined($paramsize) && $paramsize) {
    my $paramstr;
    
    open(PTMP, ">$pfile") ||
	fatal("Couldn't open $pfile\n");

    my $cc = read(STDIN, $paramstr, $paramsize);
    if (!defined($cc) || $cc != $paramsize) {
	fatal("Could not read paramter block from stdin");
    }
    print PTMP $paramstr;
    close(PTMP);
    chmod(0644, $pfile);
}
200
201
202
203
204
205
206
207
open(TMP, ">$ifile") ||
    fatal("Couldn't open $ifile\n");
while (<STDIN>) {
    print TMP $_;
}
close(TMP);
chmod(0644, $ifile);

208
209
210
211
212
213
214
215
216
217
218
#
# Need to add things to the environment for the portal module.
#
$ENV{'GENILIB_PORTAL_MODE'} = "Yep";
$ENV{'GENILIB_PORTAL_REQUEST_PATH'} = $ofile;
if ($getparams) {
    $ENV{'GENILIB_PORTAL_DUMPPARAMS_PATH'} = $ofile;
}
elsif (defined($paramsize) && $paramsize) {
    $ENV{'GENILIB_PORTAL_PARAMS_PATH'} = $pfile;
}
219
220
221
if ($warningsfatal) {
    $ENV{'GENILIB_PORTAL_WARNINGS_ARE_FATAL'} = "1";
}
222

Mike Hibler's avatar
Mike Hibler committed
223
my $exit_status;
224
225
226
227
228
229
230
231
232
233
234
235
236
if ($usejail) {
    #
    # We are executing the command in a jail, fire off the jail script.
    # Note that the directory with all the files is owned by root and
    # not writable by the user. We have to do this since the jail script
    # runs as root and trusts us to validate all the files. The only way
    # we can do that is to prevent the user from messing with them til
    # the jail is done.
    #
    # Note that the jail script will copy the files again into the jail
    # name space. Those copies will be owned by the user so they can be
    # read/written.
    #
237

238
    $exit_status = system("$JAILPROG -u $user $ifile");
239

240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    #
    # Now that we are done with the files, chown them to the user and
    # drop our permissions for any remaining activities (e.g., reading
    # the results file).
    #
    if ($unix_uid) {
	chown($unix_uid, $unix_gid, $tempdir, $ifile, $pfile, $ofile);
	$GID  = $unix_gid;
	$EGID = $unix_glist;
	$EUID = $UID = $unix_uid;
    }
} else {
    #
    # Fork a child process to run the parser in.
    #
    my $pid = fork();

    if (!defined($pid)) {
	die("*** $0:\n".
	    "    Could not fork a new process!");
    }
261
262

    #
263
    # Child runs the parser, niced down, and then exits.
264
    #
265
266
267
268
269
270
    if (! $pid) {
	# Set the CPU limit for us.
	setrlimit(RLIMIT_CPU, 600, 600);
	
	# Give parent a chance to react.
	sleep(1);
271

272
273
274
275
276
277
	#
	# Dup stdout to stderr; all output is considered error output.
	# which allows us to use stdout to send the rspec back to boss.
	#
	close(STDOUT);
	POSIX::dup2(fileno(STDERR), 1);
278

279
280
281
282
283
284
285
286
	exec("nice -15 /usr/local/bin/python $ifile");
	die("Could not exec the parser!\n");
    }

    #
    # Parent waits.
    #
    waitpid($pid, 0);
Mike Hibler's avatar
Mike Hibler committed
287
    $exit_status = $?;
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

#
# If the child was KILLed, then it overran its time limit.
# Send email. Otherwise, exit with result of child.
#
if (($exit_status & 0xff) == SIGKILL) {
    print STDERR "geni-lib Exceeded CPU Limit\n";
    $exit_status = 15;
}
elsif ($exit_status & 0xff) {
    # Get the mapping from signal num. to name
    use Config;
    my (%sig_num, @sig_name);
    my @names = split ' ', $Config{sig_name};
    @sig_num{@names} = split ' ', $Config{sig_num};
    foreach (@names) {$sig_name[$sig_num{$_}] ||= $_}
    
    my $signal = $exit_status & 0x7f;
    my $signame = $sig_name[$signal];
    
    print STDERR "geni-lib Died with SIG$signame.\n";
    $exit_status = 128 + $signal;
}
elsif ($exit_status) {
    $exit_status = $exit_status >> 8;
}
elsif (! -s $ofile)  {
    print STDERR "Cannot find the rspec, did you call printRspec(rspec)?\n";
    $exit_status = 1;
}
else {
    # Send the rspec output to stdout (back to boss).
    system("cat $ofile");
}
unless($debug) {
    if (-d $tempdir) {
	system("/bin/rm -r $tempdir");
    }
}
exit($exit_status);

330
331
sub fatal($) {
    my ($mesg) = $_[0];
332

333
334
335
336
    print STDERR "*** $0:\n".
	         "    $mesg\n";
    exit(-1);
}