Project

General

Profile

« Previous | Next » 

Revision 108e868d

Added by Jim Pingle almost 12 years ago

When renaming or deleting a virtual server, clean up the old relayd anchor name. Otherwise the rules are still there and valid, and will cause problems as they will override the new VS settings. Also clear out the anchors when stopping relayd or starting fresh that way no old settings could conflict.

View differences:

etc/inc/vslb.inc
365 365
		if (! empty($vs_a)) {
366 366
			if ($kill_first) {
367 367
				mwexec('pkill relayd');
368
				/* Remove all active relayd anchors now that relayd is no longer running. */
369
				cleanup_lb_anchor("*");
368 370
				mwexec("/usr/local/sbin/relayd -f {$g['varetc_path']}/relayd.conf");
369 371
			} else {
370 372
				// it's running and there is a config, just reload
......
379 381
			 *  returns "command failed"
380 382
			 */
381 383
			mwexec('pkill relayd');
384
			/* Remove all active relayd anchors now that relayd is no longer running. */
385
			cleanup_lb_anchor("*");
382 386
		}
383 387
	} else {
384 388
		if (! empty($vs_a)) {
385 389
			// not running and there is a config, start it
390
			/* Remove all active relayd anchors so it can start fresh. */
391
			cleanup_lb_anchor("*");
386 392
			mwexec("/usr/local/sbin/relayd -f {$g['varetc_path']}/relayd.conf");
387 393
		}
388 394
	}
......
482 488
	return $relay_hosts;
483 489
}
484 490

  
491
/* Get a list of all relayd virtual server anchors */
492
function get_lb_anchors() {
493
	/* NOTE: These names come back prepended with "relayd/" e.g. "relayd/MyVSName" */
494
	return explode("\n", trim(`/sbin/pfctl -sA -a relayd | /usr/bin/awk '{print $1;}'`));
495
}
496

  
497
/* Remove NAT rules from a relayd anchor that is no longer in use.
498
	$anchorname can either be * to clear all anchors or a specific anchor name.*/
499
function cleanup_lb_anchor($anchorname = "*") {
500
	$lbanchors = get_lb_anchors();
501
	foreach ($lbanchors as $lba) {
502
		if (($anchorname == "*") || ($lba == "relayd/{$anchorname}")) {
503
			/* Flush both the NAT and the Table for the anchor, so it will be completely removed by pf. */
504
			mwexec("/sbin/pfctl -a " . escapeshellarg($lba) . " -F nat");
505
			mwexec("/sbin/pfctl -a " . escapeshellarg($lba) . " -F Tables");
506
		}
507
	}
508
}
509

  
510
/* Mark an anchor for later cleanup. This will allow us to remove an old VS name */
511
function cleanup_lb_mark_anchor($name) {
512
	global $g;
513
	/* Nothing to do! */
514
	if (empty($name))
515
		return;
516
	$filename = "{$g['tmp_path']}/relayd_anchors_remove";
517
	$cleanup_anchors = array();
518
	/* Read in any currently unapplied name changes */
519
	if (file_exists($filename))
520
		$cleanup_anchors = explode("\n", file_get_contents($filename));
521
	/* Only add the anchor to the list if it's not already there. */
522
	if (!in_array($name, $cleanup_anchors))
523
		$cleanup_anchors[] = $name;
524
	file_put_contents($filename, implode("\n", $cleanup_anchors));
525
}
526

  
527
/* Cleanup relayd anchors that have been marked for cleanup. */
528
function cleanup_lb_marked() {
529
	global $g, $config;
530
	$filename = "{$g['tmp_path']}/relayd_anchors_remove";
531
	$cleanup_anchors = array();
532
	/* Nothing to do! */
533
	if (!file_exists($filename)) {
534
		return;
535
	} else {
536
		$cleanup_anchors = explode("\n", file_get_contents($filename));
537
		/* Nothing to do! */
538
		if (empty($cleanup_anchors))
539
			return;
540
	}
541

  
542
	/* Load current names so we can make sure we don't remove an anchor that is still in use. */
543
	$vs_a = $config['load_balancer']['virtual_server'];
544
	$active_vsnames = array();
545
	if(is_array($vs_a)) {
546
		foreach ($vs_a as $vs) {
547
			$active_vsnames[] = $vs['name'];
548
		}
549
	}
550

  
551
	foreach ($cleanup_anchors as $anchor) {
552
		/* Only cleanup an anchor if it is not still active. */
553
		if (!in_array($anchor, $active_vsnames)) {
554
			cleanup_lb_anchor($anchor);
555
		}
556
	}
557
	unlink_if_exists($filename);
558
}
559

  
485 560
?>
usr/local/www/load_balancer_virtual_server.php
58 58
		$retval |= filter_configure();
59 59
		$retval |= relayd_configure();
60 60
		$savemsg = get_std_save_message($retval);
61
		/* Wipe out old relayd anchors no longer in use. */
62
		cleanup_lb_marked();
61 63
		clear_subsystem_dirty('loadbalancer');
62 64
	}
63 65
}
......
66 68
	if (array_key_exists($_GET['id'], $a_vs)) {
67 69

  
68 70
		if (!$input_errors) {
71
			cleanup_lb_mark_anchor($a_vs[$_GET['id']]['name']);
69 72
			unset($a_vs[$_GET['id']]);
70 73
			write_config();
71 74
			mark_subsystem_dirty('loadbalancer');
usr/local/www/load_balancer_virtual_server_edit.php
117 117
		if($_POST['sitedown'] == "")
118 118
			unset($vsent['sitedown']);
119 119

  
120
		if (isset($id) && $a_vs[$id])
120
		if (isset($id) && $a_vs[$id]) {
121
			if ($a_vs[$id]['name'] != $_POST['name']) {
122
				/* Because the VS name changed, mark the old name for cleanup. */
123
				cleanup_lb_mark_anchor($a_vs[$id]['name']);
124
			}
121 125
			$a_vs[$id] = $vsent;
122
		else
126
		} else
123 127
			$a_vs[] = $vsent;
124 128

  
125 129
		if ($changecount > 0) {

Also available in: Unified diff