sitecheckin.in 7.07 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2000-2013 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 23 24 25 26 27 28 29 30
# 
# {{{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/>.
# 
# }}}
#
use English;
use strict;
use Getopt::Std;
use XML::Simple;
use Data::Dumper;
use Date::Parse;
use Time::Local;
31
use Cwd qw(realpath);
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 113 114 115 116 117 118 119 120

#
# Add (or update) an emulab site record. 
#
sub usage()
{
    print("Usage: sitecheckin <xmlfile>\n");
    exit(-1);
}
my $optlist = "dn";
my $debug   = 0;
my $impotent= 0;

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

#
# Do not run this script as root please.
#
if ($UID == 0) {
    die("*** $0:\n".
	"    Please do not run this as root!\n");
}

#
# 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 GeniCertificate;

# Protos
sub fatal($);

#
# 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{"n"})) {
    $impotent = 1;
}
usage()
    if (@ARGV != 1);

my $xmlfile  = shift(@ARGV);

#
# Map invoking user to object. 
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
}

#
# 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.
121
my $translated = realpath($xmlfile);
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
if ($translated =~ /^(\/tmp\/[-\w\.\/]+)$/) {
    $xmlfile = $1;
}
else {
    fatal("Bad data in translated pathname: $xmlfile");
}

#
# These are the fields that we allow to come in from the XMLfile.
# A couple of others are derived from the certificate.
#
my %required = ("certificate"   => "certificate",
		"url"		=> "url",
		"buildinfo"	=> "buildinfo",
		"commithash"	=> "commithash",
		"dbrev"		=> "dbrev",
		"install"	=> "install",
		"os_version"	=> "os_version",
		"perl_version"	=> "perl_version",
		"tbops"		=> "tbops");
		
#
# Must wrap the parser in eval since it exits on error.
#
my $xmlparse = eval { XMLin($xmlfile,
			    VarAttr => 'name',
			    ContentKey => '-content',
			    SuppressEmpty => undef); };
fatal($@)
    if ($@);

print STDERR Dumper($xmlparse)
    if ($debug);

#
# Make sure all the required arguments were provided.
#
foreach my $key (keys(%required)) {
    fatal("Missing required attribute '$key'")
	if (! exists($xmlparse->{'attribute'}->{"$key"}));
}

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

foreach my $key (keys(%{ $xmlparse->{'attribute'} })) {
    my $value = $xmlparse->{'attribute'}->{"$key"}->{'value'};
    if (!defined($value)) {	# Empty string comes from XML as an undef value.
	$xmlparse->{'attribute'}->{"$key"}->{'value'} = $value = "";
    }

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

    my $dbslot;

    # Must be in the allowed lists above, with exceptions handled below
    if (exists($required{$key})) {
	$dbslot = $required{$key};
	next
	    if (!defined($dbslot));
	fatal("Null value for required field $key")
	    if (!defined($value));
    }
    else {
	fatal("Invalid attribute in XML: '$key' -> '$value'\n");
    }

    # Now check that the value is legal.
    if (! TBcheck_dbslot($value, "emulab_sites", $dbslot,
			 TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	fatal("Illegal data: $key - $value");
    }

    #
    # Do a taint check to avoid warnings, since the values are abviously okay.
    #
    if ($value =~ /^(.*)$/) {
	$value = $1;
    }
    $checkin_args{$dbslot} = $value;
}

#
# Need to load the certificate. We use the ProtGeni code for this.
#
my $certificate = GeniCertificate->LoadFromString($checkin_args{"certificate"});
if (!defined($certificate)) {
    fatal("Could not parse CA certificate");
}
delete($checkin_args{"certificate"});

#
# Now do special checks.
#
221
my $urn = "";
222 223
my $commonname = $certificate->CommonName();

224 225 226 227 228
if (defined($certificate->urn())) {
    if (! TBcheck_dbslot($certificate->urn(), "emulab_sites", "urn",
			 TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	fatal("Illegal URN:" . $certificate->urn());
    }
229
    $urn = $certificate->urn();    
230 231 232 233
}
if (!defined($commonname) ||
    ! TBcheck_dbslot($commonname, "emulab_sites", "commonname",
		     TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
234
    fatal("Illegal CN: $commonname");
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
}
$checkin_args{"commonname"} = $commonname;

# Convert the buildinfo.
my $buildinfo = str2time($checkin_args{"buildinfo"});
if (!defined($buildinfo)) {
    fatal("Could not parse buildinfo");
}
delete($checkin_args{"buildinfo"});

#
# See if this urn already exists. The commonname is not supposed to
# change, so if it does send mail and bail.
#
my $safe_urn = DBQuoteSpecial($urn);
my $safe_cn  = DBQuoteSpecial($commonname);

my $query_result =
    DBQueryFatal("select urn,commonname from emulab_sites ".
254 255
		 "where commonname=$safe_cn or ".
		 "      (urn!='' and urn=$safe_urn)"); 
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
if ($query_result->numrows > 1) {
    fatal("Inconsistent site data for $urn,$commonname");
}
my $exists = $query_result->numrows;
my @sets   = ();

foreach my $key (keys(%checkin_args)) {
    my $val = $checkin_args{$key};

    # Treat NULL special.
    push (@sets, "${key}=" . ($val eq "NULL" ?
			      "NULL" : DBQuoteSpecial($val)));
}
my $query = "set " . join(",", @sets) . ", updated=now()";
if ($exists) {
271 272 273 274 275
    $query = "update emulab_sites $query,buildinfo=FROM_UNIXTIME($buildinfo) ";
    if ($urn ne "") {
	$query .= ",urn=$safe_urn ";
    }
    $query .= "where commonname=$safe_cn";
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
}
else {
    $query = "insert into emulab_sites $query, ".
	"buildinfo=FROM_UNIXTIME($buildinfo), created=now(), urn=$safe_urn";
}
if ($debug) {
    print STDERR "$query\n";
}
if (!$impotent) {
    DBQueryFatal($query);
}
if (!$exists || $debug) {
    my $vals = "";

    foreach my $key (keys(%checkin_args)) {
	$vals .= sprintf("%-15s %s\n", "${key}:", $checkin_args{$key});
    }
    SENDMAIL($TBOPS, "Emulab site checkin",
	     "Emulab site has checked in.\n\n".
	     sprintf("%-15s %s\n", "URN:", $urn).	     
	     $vals, $TBOPS);
}
exit(0);

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

    die("*** $0:\n".
	"    $mesg\n");
}