summaryrefslogtreecommitdiffstats
path: root/etc/inc/vslb.inc
blob: 86616c787c13d8a4284230aae7c5c4df9063f8d0 (plain)
1
2
3
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
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
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
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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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
<?php
/* $Id$ */
/*
  vslb.inc
  Copyright (C) 2005-2008 Bill Marquette
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.

 */

/*
	pfSense_BUILDER_BINARIES:	/usr/local/sbin/relayd
	pfSense_MODULE:	routing
*/

/* DISABLE_PHP_LINT_CHECKING */

/* include all configuration functions */

class Monitor {
	private $conf = array();
	function __construct($config) {
		$this->conf = $config;
	}

	public function p() {
		return "check {$this->get('proto')}";
	}
	private function get($var) {
		return isset($this->$var) ? $this->$var : "";
	}
	protected function config($element) {
		return isset($this->conf[$element]) ? $this->conf[$element] : "";
	}
}

class TCPMonitor extends Monitor {
	protected $proto = 'tcp';
}

class SSLMonitor extends Monitor {
	protected $proto = 'ssl';
}

class ICMPMonitor extends Monitor {
	protected $proto = 'icmp';
}

class HTTPMonitor extends Monitor {
	protected $proto = 'http';
	function __construct($config) {
		parent::__construct($config);
	}
	public function p() {
		$method = ($this->code() != "") ? $this->code() : $this->digest();
		return "check {$this->proto} {$this->path()} {$this->host()} {$method}";
	}

	private function path() {
		return $this->config('path') != "" ? "'{$this->config('path')}'" : "";
	}

	private function host() {
		return $this->config('host') != "" ? "host {$this->config('host')}" : "";
	}

	private function code() {
		return $this->config('code') != "" ? "code {$this->config('code')}" : "";
	}

	private function digest() {
		return $this->config('digest') != "" ? "digest {$this->config('digest')}" : "";
	}
}

class HTTPSMonitor extends HTTPMonitor {
	protected $proto = 'https';
}

class SendMonitor extends Monitor {
	private $proto = 'send';
	function __construct($config) {
		parent::__construct($config);
	}
	public function p() {
		return "check {$this->proto} {$this->data()} expect {$this->pattern()} {$this->ssl()}";
	}


	private function data() {
		return $this->config('send') != "" ? "{$this->config('send')}" : "";
	}

	private function pattern() {
		return $this->config('expect') != "" ? "{$this->config('expect')}" : "";
	}

	private function ssl() {
		return $this->config('ssl') == true ? "ssl" : "";
	}
}

function echo_lbaction($action) {
  global $config;
  
  // Index actions by name
  $actions_a = array();
  for ($i=0; isset($config['load_balancer']['lbaction'][$i]); $i++)
		$actions_a[$config['load_balancer']['lbaction'][$i]['name']] = $config['load_balancer']['lbaction'][$i];

  $ret = "";
  $ret .=  "{$actions_a[$action]['direction']} {$actions_a[$action]['type']} {$actions_a[$action]['action']}";
  switch($actions_a[$action]['action']) {
    case 'append': {
      $ret .= " \"{$actions_a[$action]['options']['value']}\" to \"{$actions_a[$action]['options']['akey']}\"";
      break;
    }
    case 'change': {
      $ret .= " \"{$actions_a[$action]['options']['akey']}\" to \"{$actions_a[$action]['options']['value']}\"";
      break;
    }
    case 'expect': {
      $ret .= " \"{$actions_a[$action]['options']['value']}\" from \"{$actions_a[$action]['options']['akey']}\"";
      break;
    }
    case 'filter': {
      $ret .= " \"{$actions_a[$action]['options']['value']}\" from \"{$actions_a[$action]['options']['akey']}\"";
      break;
    }
    case 'hash': {
      $ret .= " \"{$actions_a[$action]['options']['akey']}\"";
      break;
    }
    case 'log': {
      $ret .= " \"{$actions_a[$action]['options']['akey']}\"";
      break;
    }
  }
  return $ret;
}

function relayd_configure() {
  global $config, $g;

  $vs_a = $config['load_balancer']['virtual_server'];
  $pool_a = $config['load_balancer']['lbpool'];
  $protocol_a = $config['load_balancer']['lbprotocol'];

  $check_a = array();

  foreach ((array)$config['load_balancer']['monitor_type'] as $type) {
    switch($type['type']) {
      case 'icmp': {
        $mon = new ICMPMonitor($type['options']);
	break;
      }
      case 'tcp': {
        $mon = new TCPMonitor($type['options']);
	break;
      }
      case 'http': {
        $mon = new HTTPMonitor($type['options']);
	break;
      }
      case 'https': {
        $mon = new HTTPSMonitor($type['options']);
	break;
      }
      case 'send': {
        $mon = new SendMonitor($type['options']);
	break;
      }
    }
    $check_a[$type['name']] = $mon->p();
  }


  $fd = fopen("{$g['varetc_path']}/relayd.conf", "w");

  /* reindex pools by name as we loop through the pools array */
  $pools = array();
  /* Virtual server pools */
  if(is_array($pool_a)) {
    for ($i = 0; isset($pool_a[$i]); $i++) {
      if(is_array($pool_a[$i]['servers'])) {
        $srvtxt = implode(", ", $pool_a[$i]['servers']);
        $conf .= "table <{$pool_a[$i]['name']}> { $srvtxt }\n";
        /* Index by name for easier fetching when we loop through the virtual servers */
        $pools[$pool_a[$i]['name']] = $pool_a[$i];
      }
    }
  }
  if(is_array($protocol_a)) {
    for ($i = 0; isset($protocol_a[$i]); $i++) {
      $proto = "{$protocol_a[$i]['type']} protocol \"{$protocol_a[$i]['name']}\" {\n";
      if(is_array($protocol_a[$i]['lbaction'])) {
        if($protocol_a[$i]['lbaction'][0] == "") {
          continue;
        }
        for ($a = 0; isset($protocol_a[$i]['lbaction'][$a]); $a++) {
          $proto .= "  " . echo_lbaction($protocol_a[$i]['lbaction'][$a]) . "\n";
        }
      }
      $proto .= "}\n";
      $conf .= $proto;
    }
  }
  if(is_array($vs_a)) {
    for ($i = 0; isset($vs_a[$i]); $i++) {
      switch($vs_a[$i]['mode']) {
        case 'redirect': {
          $conf .= "redirect \"{$vs_a[$i]['name']}\" {\n";
          $conf .= "  listen on {$vs_a[$i]['ipaddr']} port {$vs_a[$i]['port']}\n";
          $conf .= "  forward to <{$vs_a[$i]['pool']}> port {$pools[$vs_a[$i]['pool']]['port']} {$check_a[$pools[$vs_a[$i]['pool']]['monitor']]} timeout 1000\n";

          # sitedown MUST use the same port as the primary pool - sucks, but it's a relayd thing
          if (isset($vs_a[$i]['sitedown']) && strlen($vs_a[$i]['sitedown']) > 0)
            $conf .= "  forward to <{$vs_a[$i]['sitedown']}> port {$pools[$vs_a[$i]['pool']]['port']} {$check_a[$pools[$vs_a[$i]['pool']]['monitor']]} timeout 1000\n";

          $conf .= "}\n";
          break;
        }
        case 'relay': {
          $conf .= "relay \"{$vs_a[$i]['name']}\" {\n";
          $conf .= "  listen on {$vs_a[$i]['ipaddr']} port {$vs_a[$i]['port']}\n";
          $conf .= "  protocol \"{$vs_a[$i]['relay_protocol']}\"\n";
          $conf .= "  forward to <{$vs_a[$i]['pool']}> port {$pools[$vs_a[$i]['pool']]['port']} {$check_a[$pools[$vs_a[$i]['pool']]['monitor']]} timeout 1000\n";

          if (isset($vs_a[$i]['sitedown']) &&  strlen($vs_a[$i]['sitedown']) > 0)
            $conf .= "  forward to <{$vs_a[$i]['sitedown']}> port {$pools[$vs_a[$i]['pool']]['port']} {$check_a[$pools[$vs_a[$i]['pool']]['monitor']]} timeout 1000\n";
          $conf .= "}\n";
          break;
        }
      }
    }
  }
  fwrite($fd, $conf);
  fclose($fd);

  if (is_process_running('relayd: parent')) {
    /*
     * XXX: Something breaks our control connection with relayd and makes relayctl stop working
     * rule reloads are the current suspect
     * mwexec('/usr/local/bin/relayctl stop');
     */
     mwexec('pkill relayd');
  }
  if (! empty($vs_a)) {
    mwexec("/usr/local/sbin/relayd -f {$g['varetc_path']}/relayd.conf");
  }
}

?>
OpenPOWER on IntegriCloud