osconfig_dump.php 8.63 KB
Newer Older
1 2 3
<?php
#
# Copyright (c) 2000-2008 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 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
#
require("defs.php3");
require("node_defs.php");

$reqargs = RequiredPageArguments("ip", PAGEARG_STRING);
$optargs = OptionalPageArguments("privkey", PAGEARG_STRING,
				 "check", PAGEARG_BOOLEAN,
				 "os", PAGEARG_STRING,
				 "os_version", PAGEARG_STRING,
				 "distro", PAGEARG_STRING,
				 "distro_version", PAGEARG_STRING,
				 "arch", PAGEARG_STRING,
				 "env", PAGEARG_STRING);

#
# The point of this page is to figure out what osconfig scripts
# the node ought to execute, and return them in a tarball.  We attempt to 
# cache the tarballs as much as possible and only rebuild when necessary.
#
# NOTE!  This page is only accessible over SSL, but it is only "secure" for 
# widearea nodes (which provide a privkey).  Local connections are not secure
# at all -- eventually this could move to tmcc.  And when you get down to it,
# the widearea privkeys are not well protected.  Consequently, NO PRIVATE DATA
# should go into these scripts.  There should not be a need for that, anyway.
# This is meant to be an operator mechanism, not a user mechanism, and it is 
# not meant for private data, just client-side config stuff.
#

#
# Constraints that we satisfy.  Note that different constraint types are ANDed;
# if there are multiple values to a constraint type, those are ORed.  Had to
# do something...
#
$constraints = array("node_id","class","type",
                     "os","os_version","distro","distro_version","arch");

#
# Available states in which we support an osconfig
#
$stages = array("premfs","postload");
$FILEROOT = "$TBDIR/etc/osconfig";

#
# A few helper functions.
#
function SPITERROR($msg) {
    header("Content-Type: text/plain");
David Johnson's avatar
David Johnson committed
70 71 72
    $fmsg = "TBERROR: $msg\n";
    header("Content-Length: " . strlen($fmsg) . "\n");
    echo "$fmsg";
73 74 75 76 77
    exit(1);
}

function SPITMSG($msg) {
    header("Content-Type: text/plain");
David Johnson's avatar
David Johnson committed
78 79 80
    $fmsg = "TBMSG: $msg\n";
    header("Content-Length: " . strlen($fmsg) . "\n");
    echo "$fmsg";
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 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
    exit(1);
}

function SPITNOTHING() {
    header("Content-Type: text/plain");
    header("Content-Length: 0");
    exit(0);
}

function IsControlNetIP($myIP) {
    global $CONTROL_NETWORK,$CONTROL_NETMASK;

    if (ip2long($CONTROL_NETWORK) == (ip2long($CONTROL_NETMASK) 
				      && ip2long($myIP)))
	return 1;

    return 0;
}

function match_constraint($constraint,$value,$data) {
    if (!isset($data[$constraint]))
	return FALSE;

    if (!strncmp($value,"/",1)) {
        # a pcre regex
	if (preg_match($value,$data[$constraint]))
	    return TRUE;
    }
    else if ($value == $data[$constraint]) {
	return TRUE;
    }

    return FALSE;
}

#
# do stuff!
#

# if we do not have a stage, assume one
if (!isset($env) || !in_array($env,$stages)) {
    $env = "postload";
}

# if check is not set, don't just check
if (!isset($check)) {
    $check = 0;
}

# Must use https
if (!isset($_SERVER["SSL_PROTOCOL"])) {
    SPITERROR("insecure");
}

# make sure args are sane
if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $ip) ||
    (isset($privkey) && !preg_match('/^[\w]+$/', $privkey))) {
    SPITERROR("invalid args");
}

# XXX for now, we assert if REMOTE_ADDR is not the same as $ip
# obviously, this needs to be changed for remote nodes with proxies...
if ($_SERVER["REMOTE_ADDR"] != $ip) {
    SPITERROR("no proxies allowed");
}

David Johnson's avatar
David Johnson committed
147
# need node_id below
148 149 150
#
# Make sure this is a valid request before we return anything.
#
David Johnson's avatar
David Johnson committed
151 152 153 154
# Note, we don't use Node::LookupByIP in the widearea case since there might
# be multiple node_ids using this control IP -- we explicitly look for the
# one in the widearea_nodeinfo table.
$node_id = "";
155
if (isset($privkey)) {
156
    $privkey = chop($privkey,"\n");
157
    $qres = DBQueryFatal("select node_id from widearea_nodeinfo" .
David Johnson's avatar
David Johnson committed
158
			 " where IP='" . addslashes($ip) . "' and" . 
159
			 "   privkey='" . addslashes($privkey) . "'");
David Johnson's avatar
David Johnson committed
160
    if (!$qres || mysql_num_rows($qres) < 1) {
161 162
	SPITERROR("noauth");
    }
David Johnson's avatar
David Johnson committed
163 164 165 166 167 168 169
    else if (mysql_num_rows($qres) > 1) {
	SPITERROR("ambiguous id");
    }

    $row = mysql_fetch_array($qres);
    $node_id = $row["node_id"];
    $node = Node::Lookup($node_id);
170 171 172 173 174
}
else {
    if (!IsControlNetIP($ip)) {
	SPITERROR("notlocal");
    }
175
    $node = Node::LookupByIP($ip);
David Johnson's avatar
David Johnson committed
176 177 178
    if ($node) {
	$node_id = $node->node_id();
    }
David Johnson's avatar
David Johnson committed
179 180 181
}
if (!$node) {
    SPITERROR("no such node");
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
}

#
# Load up and match constraints.
#
$qres = DBQueryFatal("select target_file_idx,constraint_name,constraint_value" .
		     "  from osconfig_targets" .
		     " where target_apply='" . addslashes($env) . "'" . 
		     " order by constraint_idx");
if (!mysql_num_rows($qres))
    SPITNOTHING();

$match_targets = array();
$conproc = array();

$data = array("node_id" => $node_id,"class" => $node->TypeClass(),
	      "type" => $node->type());
if (isset($os)) 
    $data["os"] = $os;
if (isset($os_version)) 
    $data["os_version"] = $os_version;
if (isset($distro)) 
    $data["distro"] = $distro;
if (isset($distro_version)) 
    $data["distro_version"] = $distro_version;
if (isset($arch)) 
    $data["arch"] = $arch;

while ($row = mysql_fetch_array($qres)) {
    $c = $row["constraint_name"];
    if (!in_array($c,$constraints))
	continue;

David Johnson's avatar
David Johnson committed
215
#echo "matching $c / " . $row["constraint_value"] . " to " . $data["$c"] . "\n";
216
    $val = match_constraint($c,$row["constraint_value"],$data);
David Johnson's avatar
David Johnson committed
217
#var_dump($val);
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    
    if (!isset($conproc[$row["target_file_idx"]]))
	$conproc[$row["target_file_idx"]] = array();

    if (!isset($conproc[$row["target_file_idx"]][$c]))
	$conproc[$row["target_file_idx"]][$c] = $val;
    else 
	$conproc[$row["target_file_idx"]][$c] = 
	    ($conproc[$row["target_file_idx"]][$c] || $val);
}

#
# Look through the results; for each target_idx, if all constraints
# have been met, then we can call this target matched.
#
foreach ($conproc as $target_idx => $target) {
David Johnson's avatar
David Johnson committed
234
    $cmatch = TRUE;
235
    foreach ($target as $constraint => $match) 
David Johnson's avatar
David Johnson committed
236
	$cmatch = ($cmatch && $match);
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

    if ($cmatch)
	array_push($match_targets,$target_idx);
}

#
# If we have no matches and the client is asking for a tarball, spit nothing;
# if the client is just checking if we have a tarball for them, spit an
# "update=(yes|no)" msg.
#
if (!count($match_targets)) {
    if ($check) {
	SPITMSG("update=no");
    }
    else {
	SPITNOTHING();
    }
}
else if ($check) {
    SPITMSG("update=yes");
}

#
# Now, for each target matched, grab the file-to-send info, and pack them up.
# We just grab everything cause it's cheap and there's no better way.
#
$qres = DBQueryFatal("select of.type as type,of.path as path," . 
		     "    of.dest as dest,of.file_idx as file_idx" .
		     "  from osconfig_targets as ot" . 
		     " left join osconfig_files as of" . 
		     "   on ot.target_file_idx=of.file_idx" . 
		     " where ot.target_apply='" . addslashes($env) . "'" . 
		     " group by of.file_idx" . 
		     " order by of.prio desc");
$done = array();
$files = array();
$manifest_str = "";
$i = 0;
while ($row = mysql_fetch_array($qres)) {
    if (in_array($row['file_idx'],$match_targets)
David Johnson's avatar
David Johnson committed
277
	&& !in_array($row['file_idx'],$done)) {
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
	$files[$i++] = $row["path"];
	$manifest_str .= $row["path"] . "\t" . $row["type"] . "\t" . 
	    $row["dest"] . "\n";
	array_push($done,$row['file_idx']);
    }
}

# make a random dir for tar use.
while (TRUE) {
    $randdir = "/tmp/osconfig." . rand(100,10000);
    if (mkdir($randdir)) {
	break;
    }
}
$archive = "$randdir/archive";
$manifest = "$randdir/MANIFEST";

$fd = fopen($manifest,"w");
fwrite($fd,$manifest_str);
fclose($fd);

$origwd = getcwd();
chdir($FILEROOT);

$retval = system("tar -cf $archive " . implode(" ",$files));
if ($retval)
David Johnson's avatar
David Johnson committed
304
    SPITERROR("cdata1");
305 306 307 308

chdir("$randdir");
$retval = system("tar -rf $archive MANIFEST");
if ($retval)
David Johnson's avatar
David Johnson committed
309
    SPITERROR("cdata2");
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

system("gzip $archive");
$archive .= ".gz";

#
# Phew!  Blow chunk and finish.
#
$fstat = stat($archive);
header("Content-Type: application/x-gzip");
header("Content-Length: " . $fstat["size"]);
readfile($archive);

# cleanup
chdir($origwd);
unlink($archive);
unlink($manifest);
rmdir($randdir);

return;
329 330

?>