delaycontrol.php3 21.1 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2012 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
#
include("defs.php3");

#
# Only known and logged in users can do this.
#
29 30 31
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
32

33 34 35
#
# Verify page arguments.
#
36
$reqargs = RequiredPageArguments("experiment", PAGEARG_EXPERIMENT);
37 38
$optargs = OptionalPageArguments("submit",     PAGEARG_STRING,
				 "formfields", PAGEARG_ARRAY);
39

40
# Need these below.
41 42 43 44
$pid	  = $experiment->pid();
$eid	  = $experiment->eid();
$gid	  = $experiment->gid();
$state	  = $experiment->state();
45 46 47
$unix_ggid= $experiment->UnixGID();
$project  = $experiment->Project();
$unix_pgid= $project->unix_gid();
48

49 50 51 52 53
#
# Standard Testbed Header
#
PAGEHEADER("Delay Control");

54
#
55
# Look for transition and exit with error.
56
#
57 58 59
if ($state != $TB_EXPTSTATE_ACTIVE &&
    $state != $TB_EXPTSTATE_SWAPPED) {
    USERERROR("Experiment $eid is not ACTIVE or SWAPPED.<br>".
60 61 62 63 64 65 66 67 68
	      "You must wait until the experiment is no longer in transition.".
	      "<br>", 1);
}

#
# Must be active. The backend can deal with changing the base experiment
# when the experiment is swapped out, but we need to generate a form based
# on virt_lans instead of delays/linkdelays. Thats harder to do. 
#
69
if ($state != $TB_EXPTSTATE_ACTIVE) {
70 71 72 73 74 75 76
    USERERROR("Experiment $eid must be active to change its traffic ".
	      "shaping configuration!", 1);
}

#
# Verify permission.
#
77
if (! $experiment->AccessCheck($this_user, $TB_EXPT_MODIFY)) {
78
    USERERROR("You do not have permission to modify experiment $eid!", 1);
79 80
}

81
$result_delays =
82 83
    DBQueryFatal("select * from delays ".
		 "where eid='$eid' and pid='$pid' and noshaping=0 " .
84
		 "order by vname,vnode0,vnode1");
85
$result_linkdelays =
86 87
    DBQueryFatal("select * from linkdelays ".
		 "where eid='$eid' and pid='$pid' " .
88
		 "order by vlan,vnode");
89 90 91

if (mysql_num_rows($result_delays) == 0 &&
    mysql_num_rows($result_linkdelays) == 0) {
92 93 94
    USERERROR("No running delay nodes with eid='$eid' and pid='$pid'!", 1);
}

95 96 97 98 99 100
#
# Spit the form out using the array of data.
#
function SPITFORM($formfields, $errors)
{
    global $experiment, $pid, $eid, $result_delays, $result_linkdelays;
101
    global $EXPOSELINKTEST;
102 103 104 105 106 107 108 109 110 111 112 113 114

    if ($errors) {
	echo "<table class=nogrid
                     align=center border=0 cellpadding=6 cellspacing=0>
              <tr>
                 <th align=center colspan=2>
                   <font size=+1 color=red>
                      &nbsp;Oops, please fix the following errors!&nbsp;
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
115 116
            # XSS prevention.
	    $message = CleanString($message);
117 118 119 120 121 122 123 124 125
	    echo "<tr>
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$message</font></td>
                  </tr>\n";
	}
	echo "</table><br>\n";
    }
126 127 128 129
    # XSS prevention.
    while (list ($key, $val) = each ($formfields)) {
	$formfields[$key] = CleanString($val);
    }
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
    echo $experiment->PageHeader();
    echo "<br><br>\n";

    echo "Use this page to alter the traffic shaping parameters of your
	  <em>swapped in</em> experiment. You can change as many values as you
	  like at a time. The first line in each link or lan
	  (labeled <font color=red>All Nodes</font>) allows you to set
	  the parameters for the <em>entire</em> link or lan. If you want to
	  change the values for indvidual nodes, then enter new values on the
	  proper line instead. Anything you leave blank will be unaffected.<br> 
	  When you are ready, click on the Execute button at the bottom of the
	  form. If you want these changes to be saved across swapout, then
	  check the Save box.<br><br>\n";

    $url = CreateURL("delaycontrol", $experiment);

    print "<form action='$url' method=post>\n" .
	  "<table>\n" .
	  "<tr>" .
	  " <th rowspan=2>Link Name</th>".
	  " <th rowspan=2>Node</th>".
	  " <th rowspan=2>Delay (msec)</th>".
	  " <th rowspan=2>Bandwidth<br>(kb/s)</th>".
	  " <th rowspan=2>Loss (ratio)</th>".
	  " <th rowspan=2>Queue Size</th>".
	  " <td align=center colspan=4>
		RED/GRED<br>(only if link specified as RED)</td>".
	  "</tr>".
	  "<tr>".
	  " <th>q_weight</th>".
	  " <th>minthresh</th>".
	  " <th>maxthresh</th>".
	  " <th>linterm</th>".
	  "</tr>";

    # Get current state.
    $result_delays =
	DBQueryFatal("select * from delays ".
		     "where eid='$eid' and pid='$pid' and noshaping=0 " .
		     "order by vname,vnode0,vnode1");
    $result_linkdelays =
	DBQueryFatal("select * from linkdelays ".
		     "where eid='$eid' and pid='$pid' " .
		     "order by vlan,vnode");

    $num  = mysql_num_rows( $result_delays );
    $last = "";
    if ($num)
	mysql_data_seek($result_delays, 0);
    for ($i = 0; $i < $num; $i++) {
	$row = mysql_fetch_array($result_delays);

	$vlan   = $row["vname"];
	$vnode0 = $row["vnode0"];
	$vnode1 = $row["vnode1"];

	if (strcmp($last, $vlan)) {
	    $last = $vlan;
	    echo "<tr>\n";
	    echo "  <td><font color=blue>$vlan</font></td>\n";
	    echo "  <td><font color=red>All Nodes</font></td>\n";
192
	    echo "  <td> " .
193 194 195 196
		 "<input type=text
			 name=\"formfields[DC::delay::$vlan::allnodes]\"
			 size=6/>" .
		 "</td>\n";
197
	    echo "  <td> " .
198 199 200 201
		 "<input type=text
			 name=\"formfields[DC::bandwidth::$vlan::allnodes]\"
			 size=6/>" .
		 "</td>\n";
202
	    echo "  <td> " .
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
		 "<input type=text
			 name=\"formfields[DC::plr::$vlan::allnodes]\"
			 size=6/>" .
		 "</td>\n";
	    echo "  <td> " . 
		 "<input type=text
			 name=\"formfields[DC::limit::$vlan::allnodes]\"
			 size=6/>" .
		 "</td>\n";

	    if ($row["q0_red"]) {
		echo "  <td> " .
		     "<input type=text
			      name=\"formfields[DC::q_weight::$vlan::allnodes]\"
			      size=6/>".
		     "</td>\n";
		echo "  <td> " .
		     "<input type=text
221
			     name=\"formfields[DC::thresh::$vlan::allnodes]\"
222 223 224 225
			     size=6/>".
		      "</td>\n";
		echo "  <td> " .
		     "<input type=text
226
			     name=\"formfields[DC::maxthresh::$vlan::allnodes]\"
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
			     size=6/>" .
		     "</td>\n";
		echo "  <td> " .
		     "<input type=text
			     name=\"formfields[DC::linterm::$vlan::allnodes]\"
			     size=6/>" .
		     "</td>\n";
	    }
	    else {
		echo "<td>n/a</td>\n";
		echo "<td>n/a</td>\n";
		echo "<td>n/a</td>\n";
		echo "<td>n/a</td>\n";
	    }
	    echo "</tr>\n";
242
	}
243

244
	echo "<tr>\n";
245
	echo "  <td>&nbsp</td>\n";
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
	echo "  <td>$vnode0</td>\n";
	echo "  <td> " . $row["delay0"] . 
	     "<br><input type=text
			 name=\"formfields[DC::delay::$vlan::$vnode0]\"
			 size=6/>" .
	     "</td>\n";
	echo "  <td> " . $row["bandwidth0"] . 
	     "<br><input type=text
			 name=\"formfields[DC::bandwidth::$vlan::$vnode0]\"
			 size=6/>" .
	     "</td>\n";
	echo "  <td> " . $row["lossrate0"] . 
	     "<br><input type=text
			 name=\"formfields[DC::plr::$vlan::$vnode0]\"
			 size=6/>" .
	     "</td>\n";
	echo "  <td> " . $row["q0_limit"] .
	     ($row["q0_qinbytes"] ? "b" : "s") . 
	     "<br><input type=text
			 name=\"formfields[DC::limit::$vlan::$vnode0]\"
			 size=6/>" .
	     "</td>\n";
268
	if ($row["q0_red"]) {
269 270 271 272 273 274 275
	    echo "  <td> " . $row["q0_weight"] . 
		 "<br><input type=text
			 name=\"formfields[DC::q_weight::$vlan::$vnode0]\"
			 size=6/>".
		 "</td>\n";
	    echo "  <td> " . $row["q0_minthresh"] . 
		"<br><input type=text
276
			    name=\"formfields[DC::thresh::$vlan::$vnode0]\"
277
			    size=6/>".
278
		"</td>\n";
279 280
	    echo "  <td> " . $row["q0_maxthresh"] . 
		 "<br><input type=text
281
			     name=\"formfields[DC::maxthresh::$vlan::$vnode0]\"
282 283 284 285 286 287 288
			     size=6/>" .
		 "</td>\n";
	    echo "  <td> " . $row["q0_linterm"] . 
		 "<br><input type=text
			     name=\"formfields[DC::linterm::$vlan::$vnode0]\"
			     size=6/>" .
		 "</td>\n";
289 290 291 292 293 294 295
	}
	else {
	    echo "<td>n/a</td>\n";
	    echo "<td>n/a</td>\n";
	    echo "<td>n/a</td>\n";
	    echo "<td>n/a</td>\n";
	}
296 297 298 299 300 301 302 303 304 305
	echo "</tr>\n";

	#
	# If vnode0 and vnode1 are different, its a plain duplex link.
	# Otherwise its a lan node. At some point we will allow asymmetric
	# changes to lan nodes, but the backend script does not support that
	# so do not give them the option.
	#
	echo "<tr>\n";
	if (strcmp($row["vnode0"], $row["vnode1"])) {
306
	    echo "  <td>&nbsp</td>\n";
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
	    echo "  <td>$vnode1</td>\n";
	    echo "  <td> " . $row["delay1"] . 
		 "<br><input type=text
			     name=\"formfields[DC::delay::$vlan::$vnode1]\"
			     size=6/>" .
		 "</td>\n";
	    echo "  <td> " . $row["bandwidth1"] . 
		 "<br><input type=text
			     name=\"formfields[DC::bandwidth::$vlan::$vnode1]\"
			     size=6/>".
		 "</td>\n";
	    echo "  <td> " . $row["lossrate1"] . 
		 "<br><input type=text
			     name=\"formfields[DC::plr::$vlan::$vnode1]\"
			     size=6/>" .
		 "</td>\n";
	    echo "  <td> " . $row["q1_limit"] .
		($row["q1_qinbytes"] ? "b" : "s") . 
		 "<br><input type=text
			     name=\"formfields[DC::limit::$vlan::$vnode1]\"
			     size=6/>" .
		 "</td>\n";
	    if ($row["q0_red"]) {
		echo "  <td> " . $row["q1_weight"] . 
		     "<br><input type=text
				 name=\"formfields[DC::q_weight::$vlan::$vnode1]\"
				 size=6/>" .
		     "</td>\n";
		echo "  <td> " . $row["q1_minthresh"] . 
		     "<br><input type=text
337
				 name=\"formfields[DC::thresh::$vlan::$vnode1]\"
338 339 340 341
				 size=6/>".
		     "</td>\n";
		echo "  <td> " . $row["q1_maxthresh"] . 
		     "<br><input type=text
342
				 name=\"formfields[DC::maxthresh::$vlan::$vnode1]\"
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
				 size=6/>" .
		     "</td>\n";
		echo "  <td> " . $row["q1_linterm"] . 
		     "<br><input type=text
				 name=\"formfields[DC::linterm::$vlan::$vnode1]\"
				 size=6/>" .
		     "</td>\n";
	    }
	    else {
		echo "<td>n/a</td>\n";
		echo "<td>n/a</td>\n";
		echo "<td>n/a</td>\n";
		echo "<td>n/a</td>\n";
	    }
	}
	else {
	    if (0) {
		echo "  <td>&nbsp</td>\n";
		echo "  <td>&nbsp</td>\n";
		echo "  <td> " . $row["delay1"] . "</td>\n";
		echo "  <td> " . $row["bandwidth1"] . "</td>\n";
		echo "  <td> " . $row["lossrate1"] . "</td>\n";
		echo "  <td> " . $row["q1_limit"] . "</td>\n";
	    }
367
	}
368
	echo "</tr>\n";
369
    }
370

371 372 373 374 375
    $num = mysql_num_rows( $result_linkdelays );
    if ($num)
	mysql_data_seek($result_linkdelays, 0);
    for ($i = 0; $i < $num; $i++) {
	$row = mysql_fetch_array( $result_linkdelays );
376

377 378
	$vlan  = $row["vlan"];
	$vnode = $row["vnode"];
379

380 381 382 383 384
	if (strcmp($last, $vlan)) {
	    $last = $vlan;
	    echo "<tr>\n";
	    echo "  <td><font color=blue>$vlan</font></td>\n";
	    echo "  <td><font color=red>All Nodes</font></td>\n";
385
	    echo "  <td> " .
386 387 388 389
		 "<input type=text
			 name=\"formfields[DC::delay::$vlan::allnodes]\"
			 size=6/>" .
		 "</td>\n";
390
	    echo "  <td> " .
391 392 393 394
		 "<input type=text
			 name=\"formfields[DC::bandwidth::$vlan::allnodes]\"
			 size=6/>" .
		 "</td>\n";
395
	    echo "  <td> " .
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
		 "<input type=text
			 name=\"formfields[DC::plr::$vlan::allnodes]\"
			 size=6/>" .
		 "</td>\n";
	    echo "  <td> " . 
		 "<input type=text
			 name=\"formfields[DC::limit::$vlan::allnodes]\"
			 size=6/>" .
		 "</td>\n";
	    if ($row["q_red"]) {
		echo "  <td> " .
		     "<input type=text
			     name=\"formfields[DC::q_weight::$vlan::allnodes]\"
			     size=6/>".
		     "</td>\n";
		echo "  <td> " .
		     "<input type=text
413
			     name=\"formfields[DC::thresh::$vlan::allnodes]\"
414 415 416 417
			     size=6/>".
		     "</td>\n";
		echo "  <td> " .
		     "<input type=text
418
			     name=\"formfields[DC::maxthresh::$vlan::allnodes]\"
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
			     size=6/>" .
		     "</td>\n";
		echo "  <td> " .
		     "<input type=text
			     name=\"formfields[DC::linterm::$vlan::allnodes]\"
			     size=6/>" .
		     "</td>\n";
	    }
	    else {
		echo "<td>n/a</td>\n";
		echo "<td>n/a</td>\n";
		echo "<td>n/a</td>\n";
		echo "<td>n/a</td>\n";
	    }
	    echo "</tr>\n";
	}
	echo "<tr>\n";
	echo "  <td>&nbsp</td>\n";
	echo "  <td>$vnode</td>\n";
	echo "  <td> " . $row["delay"] . 
	     "<br><input type=text
			 name=\"formfields[DC::delay::$vlan::$vnode]\"
			 size=6/>" .
	     "</td>\n";
	echo "  <td> " . $row["bandwidth"] . 
	     "<br><input type=text
			 name=\"formfields[DC::bandwidth::$vlan::$vnode]\"
			 size=6/>" .
	     "</td>\n";
	echo "  <td> " . $row["lossrate"] . 
	     "<br><input type=text
			 name=\"formfields[DC::plr::$vlan::$vnode]\"
			 size=6/>" .
	     "</td>\n";
	echo "  <td> " . $row["q_limit"] .
	    ($row["q_qinbytes"] ? "b" : "s") . 
	     "<br><input type=text
			 name=\"formfields[DC::limit::$vlan::$vnode]\"
			 size=6/>" .
	     "</td>\n";
	if ($row["q_red"]) {
	    echo "  <td> " . $row["q_weight"] . 
		 "<br><input type=text
			     name=\"formfields[DC::q_weight::$vlan::$vnode]\"
			     size=6/>" .
		 "</td>\n";
	    echo "  <td> " . $row["q_minthresh"] . 
		 "<br><input type=text
467
			     name=\"formfields[DC::thresh::$vlan::$vnode]\"
468 469 470 471
			     size=6/>" .
		 "</td>\n";
	    echo "  <td> " . $row["q_maxthresh"] . 
		 "<br><input type=text
472
			     name=\"formfields[DC::maxthresh::$vlan::$vnode]\"
473 474 475 476 477 478 479
			     size=6/>" .
		 "</td>\n";
	    echo "  <td> " . $row["q_linterm"] . 
		 "<br><input type=text
			     name=\"formfields[DC::linterm::$vlan::$vnode]\"
			     size=6/>" .
		 "</td>\n";
480 481
	}
	else {
482 483 484 485
		echo "<td>n/a</td>\n";
		echo "<td>n/a</td>\n";
		echo "<td>n/a</td>\n";
		echo "<td>n/a</td>\n";
486 487
	}
	echo "</tr>\n";
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562

       #
       # If duplex, its a lan node. Print reverse params.
       # Note, we do not allow them to change lan nodes asymmetrically yet
       # since the backend script cannot handle that.
       #
       if (0 && !strcmp($row{"type"}, "duplex")) {
	   echo "<tr>\n";
	   echo "  <td>&nbsp</td>\n";
	   echo "  <td>&nbsp</td>\n";
	   echo "  <td> " . $row["rdelay"] . "</td>\n";
	   echo "  <td> " . $row["rbandwidth"] . "</td>\n";
	   echo "  <td> " . $row["rlossrate"] . "</td>\n";
	   echo "  <td> " . $row["q_limit"] . "</td>\n";
	   echo "</tr>\n";
       }

    }

    echo "</table>
	   <input type=checkbox
	          name=\"formfields[modbase]\"
		  value=Yep";
    if (isset($formfields["modbase"]) &&
	strcmp($formfields["modbase"], "Yep") == 0)
	echo "    checked";
    echo " >\n";
    echo " &nbsp; <b>Save?</b> (Check this box if you want these settings to
			       be used next time the experiment is swapped in)
	  <br><br>
	  <input type=submit name=submit value=Execute>
	  </form>\n";

    if (STUDLY() || $EXPOSELINKTEST) {
    echo "<br>
	  After you change the settings, you can run
	  <a href=linktest.php3?pid=$pid&eid=$eid>Linktest</a> to make sure the
	  links are configured properly,<br>
	  but <b><em>only</em></b> if you clicked the 'Save' box above!\n";

    echo "<br><br> 
	  <b>We strongly recommend that you always use
	  <a href=linktest.php3?pid=$pid&eid=$eid>Linktest</a> or some
	  other testing mechanism to ensure that your links and lans are
	  behaving as you expect them to.</b>\n";
    } 
}

#
# On first load, display initial values.
#
if (!isset($submit)) {
    $defaults = array();
    $defaults["modbase"] = 0;

    SPITFORM($defaults, 0);
    PAGEFOOTER();
    return;
}

#
# Otherwise, must validate and redisplay if errors
#
$errors = array();

#
# Array of changes, indexed by [lan:node]
# 
$changes = array();
while (list ($header, $value) = each ($formfields)) {
    $changestring = strstr($header, "DC::");
    if (! $changestring) {
	continue;
    }
    if (!isset($value) || !strcmp($value, "") ||
Mike Hibler's avatar
Mike Hibler committed
563
	!preg_match('/^[0-9\.bs]*$/', "$value")) {
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
	continue;
    }

    # Too bad sscanf is broken ...
    $temp      = substr($header, 4);
    $param     = substr($temp, 0, strpos($temp, "::"));
    $temp      = substr($temp, strpos($temp, "::") + 2);
    $lan       = substr($temp, 0, strpos($temp, "::"));
    $vnode     = substr($temp, strpos($temp, "::") + 2);

    ##echo "$param $lan $vnode $value<br>\n";

    #
    # Must taint check! This stuff is going to a shell program. 
    # 
    if (!preg_match('/^[-\w]+$/', $param)) {
	$errors["Param $param"] = "Invalid characters";
	continue;
    }
    if (!preg_match('/^[-\w]+$/', $lan)) {
	$errors["Lan $param"] = "Invalid characters";
	continue;
    }
    if (!preg_match('/^[-\w]+$/', $vnode)) {
	$errors["Vnode $param"] = "Invalid characters";
	continue;
    }

    #
    # Queue limit is special. Need to parse for "b" or "s" extension.
    #
    $qlimitarg = "";
    if (! strcmp($param, "limit")) {
	$lastchr = $value{strlen($value)-1};

	if (ctype_alpha($lastchr)) {
	    if ($lastchr == "s") {
		$qlimitarg = "queue-in-bytes=0";
	    }
	    elseif ($lastchr == "b") {
		$qlimitarg = "queue-in-bytes=1";
	    }
	    $value = substr($value, 0, strlen($value) - 1);
	}
    }

    if (! array_key_exists($lan, $changes)) {
	$changes["$lan"] = array();
612
    }
613 614 615 616 617 618

    if (!isset($vnode) || $vnode == "") {
	if (! array_key_exists("allnodes", $changes["$lan"])) {
	    $changes["$lan"]["allnodes"] = "";
	}
	$changes["$lan"]["allnodes"] .= "${param}=${value} $qlimitarg ";
619 620
    }
    else {
621 622 623 624
	if (! array_key_exists("$vnode", $changes["$lan"])) {
	    $changes["$lan"]["$vnode"] = "";
	}
	$changes["$lan"]["$vnode"] .= "${param}=${value} $qlimitarg ";
625
    }
626 627
}

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
#
# If any errors, respit the form with the current values and the
# error messages displayed. Iterate until happy.
# 
if (count($errors)) {
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    return;
}

# Call delay_config to make changes on each lan:node pair.
foreach ($changes as $lan => $foo) {
    foreach ($foo as $vnode => $string) {

	#
	# Build up argument array to pass along.
	#
	$args = array();

	# Required.
	$args["pid"] = $pid;
	$args["eid"] = $eid;
	$args["link"] = $lan;

	# Optional.
	if (isset($formfields["modbase"])) {
	    $args["modify"] = strcmp($formfields["modbase"], "Yep") ? 0 : 1;
	}
	# This could be "allnodes" or a source vnode name.  Skip if "allnodes".
	if (strcmp($vnode, "allnodes")) {
	    $args["vnode"] = $vnode;
	}

	# Separate the params out of the string gathered for this lan:node pair.
	foreach (explode(' ', trim($string)) as $param_value) {
	    if ($param_value != "") {
		list($param, $value) = explode('=', $param_value);
		$args[$param] = $value;
	    }
	}

669
	if (! ($result = ChangeDelayConfig($uid, $unix_pgid, $unix_ggid,
670 671 672 673 674 675 676 677 678
					   $args, $errors))) {
	    # Always respit the form so that the form fields are not lost.
	    # I just hate it when that happens so lets not be guilty of it ourselves.
	    SPITFORM($formfields, $errors);
	    PAGEFOOTER();
	    return;
	}
    }
}
679

680 681 682
# Always respit the form so that the form fields are not lost.
# I just hate it when that happens so lets not be guilty of it ourselves.
SPITFORM($formfields, $errors);
683
PAGEFOOTER();
684 685 686 687 688 689 690 691 692 693 694 695 696 697
return;

#
# When there's a DelayConfig class, this will be a Class function to change them...
#
function ChangeDelayConfig($uid, $pid, $unix_gid, $args, &$errors) {
    global $suexec_output, $suexec_output_array, $TBADMINGROUP;

    #
    # Generate a temporary file and write in the XML goo.
    #
    $xmlname = tempnam("/tmp", "delay_config");
    if (! $xmlname) {
	TBERROR("Could not create temporary filename", 0);
698
	$errors[] = "Transient error(1); please try again later.";
699 700 701 702
	return null;
    }
    if (! ($fp = fopen($xmlname, "w"))) {
	TBERROR("Could not open temp file $xmlname", 0);
703
	$errors[] = "Transient error(2); please try again later.";
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
	return null;
    }

    fwrite($fp, "<PubKey>\n");
    foreach ($args as $name => $value) {
	fwrite($fp, "<attribute name=\"$name\">");
	fwrite($fp, "  <value>" . htmlspecialchars($value) . "</value>");
	fwrite($fp, "</attribute>\n");
    }
    fwrite($fp, "</PubKey>\n");
    fclose($fp);
    chmod($xmlname, 0666);

    #
    # Need proper auditing or logging. For now, run as normal and
    # if something breaks we get the mail from the web interface.
    # This might change depending on how often we get email!
    #
    $retval = SUEXEC($uid, "$pid,$unix_gid", "webdelay_config -X $xmlname",
		     SUEXEC_ACTION_IGNORE);
    if ($retval) {
	# Ug, I know this hardwired return value is bad! 
	if ($retval == 2) {
	    # A usage error, reported to user. At some point the
	    # approach is to do all the argument checking first.
	    if (count($suexec_output_array)) {
		for ($i = 0; $i < count($suexec_output_array); $i++) {
		    $line = $suexec_output_array[$i];
		    if (preg_match("/^([-\w]+):\s*(.*)$/",
				   $line, $matches)) {
			$errors[$matches[1]] = $matches[2];
		    }
		    else
			$errors[] = $line;
		}
	    }
	    else
741
		$errors[] = "Transient error(3, $retval); please try again later.";
742 743 744
	}
	else {
	    # All other errors reported to tbops since they are bad!
745
	    $errors[] = "Transient error(4, $retval); please try again later.";
746 747 748 749 750 751 752 753 754 755 756 757 758 759
	    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	}
	return null;
    }

    # There are no return value(s) to parse at the end of the output.

    # Unlink this here, so that the file is left behind in case of error.
    # We can then edit the pubkeys by hand from the xmlfile, if desired.
    unlink($xmlname);

    return true; 
}

760 761
?>