newosid.in 7.65 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use strict;
use Getopt::Std;
use XML::Simple;
use Data::Dumper;

#
# Create a new osid from a XML description. 
#
sub usage()
{
    print("Usage: newosid [-v] <xmlfile>\n");
    exit(-1);
}
my $optlist = "dv";
my $debug   = 0;
my $verify  = 0;	# Check data and return status only. 

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TBAUDIT	= "@TBAUDITEMAIL@";

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

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

#
# Load the Testbed support stuff.
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use User;
use Project;
use OSinfo;

# Protos
sub fatal($);
sub UserError(;$);

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"v"})) {
    $verify = 1;
}
if (@ARGV != 1) {
    usage();
}
my $xmlfile  = shift(@ARGV);

#
# Map invoking user to object. 
# If invoked as "nobody" we are coming from the web interface and the
# current user context is "implied" (see tbauth.php3).
#
my $this_user;

if (getpwuid($UID) ne "nobody") {
    $this_user = User->ThisUser();

    if (! defined($this_user)) {
	fatal("You ($UID) do not exist!");
    }
    fatal("You must have admin privledges to create new osids")
	if (!$this_user->IsAdmin());
}
else {
    #
    # Check the filename when invoked from the web interface; must be a
    # file in /tmp.
    #
    if ($xmlfile =~ /^([-\w\.\/]+)$/) {
	$xmlfile = $1;
    }
    else {
	fatal("Bad data in pathname: $xmlfile");
    }

    # Use realpath to resolve any symlinks.
    my $translated = `realpath $xmlfile`;
    if ($translated =~ /^(\/tmp\/[-\w\.\/]+)$/) {
	$xmlfile = $1;
    }
    else {
	fatal("Bad data in translated pathname: $xmlfile");
    }

Russ Fish's avatar
Russ Fish committed
113
    # The web interface (and in the future the xmlrpc interface) sets this.
114
115
116
    $this_user = User->ImpliedUser();

    if (! defined($this_user)) {
Russ Fish's avatar
Russ Fish committed
117
	fatal("Cannot determine implied user!");
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
    }
}

#
# These are the fields that we allow to come in from the XMLfile.
#
my $SLOT_OPTIONAL	= 0x1;	# The field is not required.
my $SLOT_REQUIRED	= 0x2;  # The field is required and must be non-null.
my $SLOT_ADMINONLY	= 0x4;  # Only admins can set this field.
#
# XXX We should encode all of this in the DB so that we can generate the
# forms on the fly, as well as this checking code.
#
my %xmlfields =
    # XML Field Name        DB slot name         Flags             Default
    ("description"	=> ["description",	$SLOT_REQUIRED],
     "osname"		=> ["osname"	,	$SLOT_REQUIRED],
     "project"		=> ["pid",		$SLOT_REQUIRED],
     "OS"		=> ["OS",		$SLOT_REQUIRED],
     "version"		=> ["version",		$SLOT_OPTIONAL,	   ""],
     "path"		=> ["path",		$SLOT_OPTIONAL,	   "NULL"],
     "magic",		=> ["magic",		$SLOT_OPTIONAL,	   ""],
     "op_mode",		=> ["op_mode",		$SLOT_REQUIRED],
     "features",	=> ["osfeatures",	$SLOT_OPTIONAL,	   ""],
     "shared",		=> ["shared",		$SLOT_ADMINONLY,   0],
     "mustclean",	=> ["mustclean",	$SLOT_ADMINONLY,   1],
     "nextosid",	=> ["nextosid",		$SLOT_ADMINONLY],
     "reboot_waittime",	=> ["reboot_waittime",	$SLOT_ADMINONLY]);

#
# Must wrap the parser in eval since it exits on error.
#
my $xmlparse = eval { XMLin($xmlfile,
			    VarAttr => 'name',
			    ContentKey => '-content',
			    SuppressEmpty => undef); };
fatal($@)
    if ($@);

#
# Process and dump the errors (formatted for the web interface).
# We should probably XML format the errors instead but not sure I want
# to go there yet.
#
my %errors = ();

#
# Make sure all the required arguments were provided.
#
foreach my $key (keys(%xmlfields)) {
    my (undef, $required, undef) = @{$xmlfields{$key}};

    $errors{$key} = "Required value not provided"
	if ($required & $SLOT_REQUIRED  &&
	    ! exists($xmlparse->{'attribute'}->{"$key"}));
}
UserError()
    if (keys(%errors));

#
# We build up an array of arguments to pass to OSinfo->Create() as we check
# the attributes.
#
my %newosid_args = ();

foreach my $key (keys(%{ $xmlparse->{'attribute'} })) {
    my $value = $xmlparse->{'attribute'}->{"$key"}->{'value'};

    if ($debug) {
	print STDERR "User attribute: '$key' -> '$value'\n";
    }

    $errors{$key} = "Unknown attribute"
	if (!exists($xmlfields{$key}));

    my ($dbslot, $required, $default) = @{$xmlfields{$key}};

    if ($required & $SLOT_REQUIRED) {
	# A slot that must be provided, so do not allow a null value.
	if (!defined($value)) {
	    $errors{$key} = "Must provide a non-null value";
	    next;
	}
    }
    if ($required & $SLOT_OPTIONAL) {
	# Optional slot. If value is null skip it. Might not be the correct
	# thing to do all the time?
	if (!defined($value)) {
	    next
		if (!defined($default));
	    $value = $default;
	}
    }
    if ($required & $SLOT_ADMINONLY) {
	# Admin implies optional, but thats probably not correct approach.
	$errors{$key} = "Administrators only"
	    if (! $this_user->IsAdmin());
    }
	
    # Now check that the value is legal.
    if (! TBcheck_dbslot($value, "os_info", $dbslot, TBDB_CHECKDBSLOT_ERROR)) {
	$errors{$key} = TBFieldErrorString();
	next;
    }

    $newosid_args{$dbslot} = $value;
}
UserError()
    if (keys(%errors));

#
# Now do special checks.
#
my $project = Project->Lookup($newosid_args{"pid"});
if (!defined($project)) {
    UserError("Project: No such project");
}
if (!$project->AccessCheck($this_user, TB_PROJECT_MAKEOSID())) {
    UserError("Project: Not enough permission");
}

# OS must be in the allowed list.
if (! OSinfo->ValidOS($newosid_args{"OS"})) {
    UserError("OS: Invalid");
}
# Ditto the opmode.
if (! OSinfo->ValidOpMode($newosid_args{"op_mode"})) {
    UserError("OpMode: Invalid");
}

# Nextosid check. Must exist. admin check done above.
if (exists($newosid_args{"nextosid"})) {
    my $nextos = OSinfo->Lookup($newosid_args{"nextosid"});

    if (!defined($nextos)) {
	UserError("Nextosid: Does not exist");
    }
}

# Mere users have to supply a version, but admin people do not.
if (! $this_user->IsAdmin() &&
    (!exists($newosid_args{"version"}) || $newosid_args{"version"} eq "")) {
    UserError("Version: Required value not provided");
}

# reboot waittime default value is not set by an admin user.
if (! exists($newosid_args{"reboot_waittime"})) {
    $newosid_args{"reboot_waittime"} =
	OSinfo->RebootWaitTime($newosid_args{"OS"});
}
exit(0)
    if ($verify);

#
# Now safe to create the OSID.
#
# We pass the osname along as an argument to Create(), so remove it from
# the argument array.
#
my $osname = $newosid_args{"osname"};
delete($newosid_args{"osname"});
# Ditto the pid.
delete($newosid_args{"pid"});

Russ Fish's avatar
Russ Fish committed
282
283
284
285
286
287
288
289
my $usrerr;
my $new_osinfo = OSinfo->Create($project, $this_user, $osname,
				\%newosid_args, \$usrerr);
UserError($usrerr)
    if (defined($usrerr));
fatal("Could not create new OSID!")
    if (!defined($new_osinfo));

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
my $osid = $new_osinfo->osid();
	    
# The web interface requires this line to be printed.
print "OSID $osname/$osid has been created\n";
exit(0);

sub fatal($)
{
    my ($mesg) = @_;

    print STDERR "*** $0:\n".
	         "    $mesg\n";
    # Exit with negative status so web interface treats it as system error.
    exit(-1);
}

sub UserError(;$)
{
    my ($mesg) = @_;

    if (keys(%errors)) {
	foreach my $key (keys(%errors)) {
	    my $val = $errors{$key};
	    print "${key}: $val\n";
	}
    }
    print "$mesg\n"
	if (defined($mesg));

    # Exit with positive status so web interface treats it as user error.
    exit(1);
}