template_metadata.in 6.09 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2006, 2007 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# All rights reserved.
#
use English;
use strict;
use Getopt::Std;

#
# 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
26
	  "Usage: template_metadata -a <action> -f <file> [-t type] ".
27 28
	  "<name> <guid/vers>\n".
	  "switches and arguments:\n".
29 30
	  "-a <action>    - add, delete or modify\n".
	  "-t <type>      - type if not generic user metadata (add only)\n".
31 32 33 34 35
	  "-f <file>      - file of metadata value\n".
	  "<name>         - Metadata name\n".
	  "<guid/vers>    - GUID and version of template\n");
    exit(-1);
}
36
my $optlist	 = "a:f:t:";
37 38 39 40 41 42
my %options      = ();
my $action;
my $template_guid;
my $template_vers;
my $metadata_name;
my $metadata_value;
43
my $metadata_type;
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
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.
#
89 90 91
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    tbdie("You ($UID) do not exist!");
92
}
93 94
my $user_dbid  = $this_user->dbid();
my $user_uid   = $this_user->uid();
95 96 97 98 99 100 101 102 103 104 105 106

# 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!");
}
107
if (! $template->AccessCheck($this_user, TB_EXPT_MODIFY)) {
108 109 110 111 112
    tberror("You do not have permission to add metadata to template ".
	    "$template_guid/$template_vers");
    exit(1);
}

113 114
if ($action ne "delete") {
    $metadata_value = `cat $inputfile`;
115

116 117 118 119 120
    if (! TBcheck_dbslot($metadata_value,
			 "experiment_template_metadata", "value",
			 TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	tbdie("Improper characters in metadata value!");
    }
121 122 123 124 125 126
}

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

127 128 129 130
if ($action eq "delete") {
    tbdie("Metadata name '$metadata_name' does not exist!")
	if (!$already_exists);
    
131
    $template->DeleteMetadata($metadata_name) == 0
132 133 134
	or tbdie("Could not delete metadata item '$metadata_name'");
}
elsif ($action eq "add") {
135 136 137
    tbdie("Metadata name '$metadata_name' already in use!")
	if ($already_exists);
    
138
    $template->NewMetadata($metadata_name, $metadata_value,
139
			   $this_user, $metadata_type) == 0
140 141 142 143 144 145
	or tbdie("Could not add new metadata item '$metadata_name'");
}
else {
    tbdie("Metadata name '$metadata_name' does not exist!")
	if (!$already_exists);
    
146
    $template->ModifyMetadata($metadata_name, $metadata_value, $this_user) == 0
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
	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()
196 197 198 199 200 201 202 203 204 205 206
	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!");
	}
    }
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

    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.
	#
	my $translated = `realpath $inputfile`;
	if ($translated =~ /^([-\w\.\/]+)$/) {
	    $inputfile = $1;
	}
	else {
	    tbdie("Bad data returned by realpath: $translated");
	}

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

	#
236 237 238 239 240 241
	# 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.
242 243 244 245
	#
	if (! ($inputfile =~ /^\/tmp\/[-\w]+-\d+\.txt/) &&
	    ! ($inputfile =~ /^\/tmp\/\d+\.txt/) &&
	    ! ($inputfile =~ /^\/var\/tmp\/php\w+/) &&
246
	    ! TBValidUserDir($inputfile, 0)) {
247 248 249 250 251
	    tberror("$inputfile does not resolve to an allowed directory!");
	    # Note positive status; so error goes to user not tbops.
	    exit(1);
	}
    }
252
    elsif ($action ne "delete") {
253 254 255 256
	usage();
    }
}