template_metadata.in 6.8 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2006, 2007 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{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/>.
# 
# }}}
23 24 25 26
#
use English;
use strict;
use Getopt::Std;
27
use Cwd qw(realpath);
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

#
# Add metadata to a template.
#
# Exit codes are important; they tell the web page what has happened so
# it can say something useful to the user. Fatal errors are mostly done
# with die(), but expected errors use this routine. At some point we will
# use the DB to communicate the actual error.
#
# $status < 0 - Fatal error. Something went wrong we did not expect.
# $status = 0 - Everything okay.
# $status > 0 - Expected error. User not allowed for some reason. 
# 
sub usage()
{
    print(STDERR
44
	  "Usage: template_metadata -a <action> -f <file> [-t type] ".
45 46
	  "<name> <guid/vers>\n".
	  "switches and arguments:\n".
47 48
	  "-a <action>    - add, delete or modify\n".
	  "-t <type>      - type if not generic user metadata (add only)\n".
49 50 51 52 53
	  "-f <file>      - file of metadata value\n".
	  "<name>         - Metadata name\n".
	  "<guid/vers>    - GUID and version of template\n");
    exit(-1);
}
54
my $optlist	 = "a:f:t:";
55 56 57 58 59 60
my %options      = ();
my $action;
my $template_guid;
my $template_vers;
my $metadata_name;
my $metadata_value;
61
my $metadata_type;
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
my $inputfile;

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

# Locals
my $template;
my $instance;
# For the END block below.
my $cleaning    = 0;
my $justexit    = 1;

# Protos
sub ParseArgs();

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

# Be careful not to exit on transient error
$libdb::DBQUERY_MAXTRIES = 0;

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

#
# Untaint the path
#
# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Verify user and get his DB uid.
#
107 108 109
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    tbdie("You ($UID) do not exist!");
110
}
111 112
my $user_dbid  = $this_user->dbid();
my $user_uid   = $this_user->uid();
113 114 115 116 117 118 119 120 121 122 123 124

# Now parse arguments.
ParseArgs();

#
# Grab template and do access check.
#
$template = Template->Lookup($template_guid, $template_vers);

if (!defined($template)) {
    tbdie("Experiment template $template_guid/$template_vers does not exist!");
}
125
if (! $template->AccessCheck($this_user, TB_EXPT_MODIFY)) {
126 127 128 129 130
    tberror("You do not have permission to add metadata to template ".
	    "$template_guid/$template_vers");
    exit(1);
}

131 132
if ($action ne "delete") {
    $metadata_value = `cat $inputfile`;
133

134 135 136 137 138
    if (! TBcheck_dbslot($metadata_value,
			 "experiment_template_metadata", "value",
			 TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	tbdie("Improper characters in metadata value!");
    }
139 140 141 142 143 144
}

my $already_exists = $template->LookupMetadata($metadata_name);
tbdie("Could not lookup metadata name '$metadata_name'")
    if ($already_exists < 0);

145 146 147 148
if ($action eq "delete") {
    tbdie("Metadata name '$metadata_name' does not exist!")
	if (!$already_exists);
    
149
    $template->DeleteMetadata($metadata_name) == 0
150 151 152
	or tbdie("Could not delete metadata item '$metadata_name'");
}
elsif ($action eq "add") {
153 154 155
    tbdie("Metadata name '$metadata_name' already in use!")
	if ($already_exists);
    
156
    $template->NewMetadata($metadata_name, $metadata_value,
157
			   $this_user, $metadata_type) == 0
158 159 160 161 162 163
	or tbdie("Could not add new metadata item '$metadata_name'");
}
else {
    tbdie("Metadata name '$metadata_name' does not exist!")
	if (!$already_exists);
    
164
    $template->ModifyMetadata($metadata_name, $metadata_value, $this_user) == 0
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
	or tbdie("Could not modify metadata item '$metadata_name'");
}
exit(0);

#
# Parse command arguments. Once we return from getopts, all that are
# left are the required arguments.
#
sub ParseArgs()
{
    my %options;
    
    if (! getopts($optlist, \%options)) {
	usage();
    }

    if (@ARGV != 2) {
	usage();
    }

    #
    # Pick up metadata name and untaint.
    #
    $metadata_name = shift(@ARGV);

    if (! TBcheck_dbslot($metadata_name,
			 "experiment_template_metadata", "name",
			 TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	tbdie("Improper metadata name!");
    }
    
    #
    # Pick up guid/version and untaint.
    #
    my $tmp = shift(@ARGV);

    if ($tmp =~ /^([\w]*)\/([\d]*)$/) {
	$template_guid = $1;
	$template_vers = $2;
    }
    else {
	usage();
    }

    if (!defined($options{"a"})) {
	tbdie("Must supply -a option");
    }
    $action = $options{"a"};
    usage()
214 215 216 217 218 219 220 221 222 223 224
	if ($action ne "add" && $action ne "modify" && $action ne "delete");

    if (defined($options{"t"})) {
	$metadata_type = $options{"t"};

	if (! TBcheck_dbslot($metadata_type,
			     "experiment_template_metadata", "metadata_type",
			     TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	    tbdie("Improper metadata type!");
	}
    }
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

    if (defined($options{"f"})) {
	$inputfile = $options{"f"};

	# Note different taint check (allow /).
	if ($inputfile =~ /^([-\w\.\/]+)$/) {
	    $inputfile = $1;
	}
	else {
	    tbdie("Bad data in metadata file: $inputfile");
	}

	#
	# Called from ops interactively. Make sure NS file in /proj or /users.
	#
	# Use realpath to resolve any symlinks.
	#
242
	my $translated = realpath($inputfile);
243 244 245 246 247 248 249 250 251 252 253
	if ($translated =~ /^([-\w\.\/]+)$/) {
	    $inputfile = $1;
	}
	else {
	    tbdie("Bad data returned by realpath: $translated");
	}

	tbdie("$inputfile does not exist")
	    if (! -f $inputfile);

	#
254 255 256 257 258 259
	# The file must reside in an acceptible location. Since this script
	# runs as the caller, regular file permission checks ensure it is a
	# file the user is allowed to use.  So we don't have to be too tight
	# with the RE matching /tmp and /var/tmp files.  Note that the .txt
	# files are allowed since this script is invoked directly from web
	# interface which generates a name that should not be guessable.
260 261 262 263
	#
	if (! ($inputfile =~ /^\/tmp\/[-\w]+-\d+\.txt/) &&
	    ! ($inputfile =~ /^\/tmp\/\d+\.txt/) &&
	    ! ($inputfile =~ /^\/var\/tmp\/php\w+/) &&
264
	    ! TBValidUserDir($inputfile, 0)) {
265 266 267 268 269
	    tberror("$inputfile does not resolve to an allowed directory!");
	    # Note positive status; so error goes to user not tbops.
	    exit(1);
	}
    }
270
    elsif ($action ne "delete") {
271 272 273 274
	usage();
    }
}