Project

General

Profile

Download (20.6 KB) Statistics
| Branch: | Tag: | Revision:
1 c83068f6 Colin Smith
<?php
2
/****h* pfSense/service-utils
3 61e047a5 Phil Davis
	NAME
4
		service-utils.inc - Service facility
5
	DESCRIPTION
6
		This file contains various functions used by the pfSense service facility.
7
	HISTORY
8
	$Id$
9
10
	Copyright (C) 2005-2006 Colin Smith (ethethlay@gmail.com)
11
	All rights reserved.
12
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14
15
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17
18
	2. Redistributions in binary form must reproduce the above copyright
19
	   notice, this list of conditions and the following disclaimer in the
20
	   documentation and/or other materials provided with the distribution.
21
22
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
33 c83068f6 Colin Smith
 */
34
35 523855b0 Scott Ullrich
/*
36 05c4bfa0 Ermal
	pfSense_BUILDER_BINARIES:	/bin/pgrep /bin/sh /usr/bin/killall
37 523855b0 Scott Ullrich
	pfSense_MODULE:	utils
38
*/
39 ab1cf455 jim-p
require_once("globals.inc");
40 ee6e6011 jim-p
require_once("captiveportal.inc");
41
require_once("openvpn.inc");
42
require_once("ipsec.inc");
43
require_once("vpn.inc");
44
require_once("vslb.inc");
45 b05a8f35 jim-p
require_once("gwlb.inc");
46 523855b0 Scott Ullrich
47 e9d66ed4 phildd
define("RCFILEPREFIX", "/usr/local/etc/rc.d/");
48 c83068f6 Colin Smith
function write_rcfile($params) {
49 c2151973 Ermal
	global $g;
50 af799b48 Renato Botelho
51 6f931ad2 Phil Davis
	safe_mkdir(RCFILEPREFIX);
52 e9d66ed4 phildd
	$rcfile_fullname = RCFILEPREFIX . $params['file'];
53 61e047a5 Phil Davis
	if (!file_exists($rcfile_fullname) && !is_link($rcfile_fullname) && !touch($rcfile_fullname)) {
54 af799b48 Renato Botelho
		return false;
55 61e047a5 Phil Davis
	}
56 c2151973 Ermal
57 61e047a5 Phil Davis
	if (!is_writable($rcfile_fullname) || empty($params['start'])) {
58 ba8495f0 Ermal
		return false;
59 61e047a5 Phil Davis
	}
60 af799b48 Renato Botelho
61 ba8495f0 Ermal
	$towrite = "#!/bin/sh\n";
62 4dddf8c3 Warren Baker
	$towrite .= "# This file was automatically generated\n# by the {$g['product_name']} service handler.\n\n";
63 ba8495f0 Ermal
64 c83068f6 Colin Smith
	/* write our rc functions */
65 ba8495f0 Ermal
	$towrite .= "rc_start() {\n";
66
	$towrite .= "\t{$params['start']}\n";
67
	$towrite .= "}\n\n";
68 61e047a5 Phil Davis
	if (!empty($params['stop'])) {
69 c83068f6 Colin Smith
		$tokill =& $params['stop'];
70 61e047a5 Phil Davis
	} else if (!empty($params['executable'])) {
71 a8dbad89 Colin Smith
		/* just nuke the executable */
72 873c1701 Renato Botelho
		$tokill = "/usr/bin/killall " . escapeshellarg($params['executable']);
73 c83068f6 Colin Smith
	} else {
74 a8dbad89 Colin Smith
		/* make an educated guess (bad) */
75 c83068f6 Colin Smith
		$tokill = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
76
	}
77 ba8495f0 Ermal
	$towrite .= "rc_stop() {\n";
78
	$towrite .= "\t{$tokill}\n";
79
	$towrite .= "}\n\n";
80 4685e464 Scott Ullrich
81 c83068f6 Colin Smith
	/* begin rcfile logic */
82 ba8495f0 Ermal
	$towrite .= "case \$1 in\n\tstart)\n\t\trc_start\n\t\t;;\n\tstop)\n\t\trc_stop\n\t\t;;\n\trestart)\n\t\trc_stop\n\t\trc_start\n\t\t;;\nesac\n\n";
83
84 44b19298 Ermal
	@file_put_contents($rcfile_fullname, $towrite);
85
	unset($towrite);
86 e9d66ed4 phildd
	@chmod("{$rcfile_fullname}", 0755);
87 ba8495f0 Ermal
88 c83068f6 Colin Smith
	return;
89
}
90 31f346a8 Colin Smith
91 58826c73 Colin Smith
function start_service($name) {
92 669e1adb Bill Marquette
	global $config;
93 ba8495f0 Ermal
94 61e047a5 Phil Davis
	if (empty($name)) {
95 b27ade8e Ermal
		return;
96 61e047a5 Phil Davis
	}
97 b27ade8e Ermal
98 44b19298 Ermal
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
99 61e047a5 Phil Davis
		foreach ($config['installedpackages']['service'] as $service) {
100
			if (strtolower($service['name']) == strtolower($name)) {
101
				if ($service['rcfile']) {
102 e9d66ed4 phildd
					$prefix = RCFILEPREFIX;
103 ba8495f0 Ermal
					if (!empty($service['prefix'])) {
104 31f346a8 Colin Smith
						$prefix =& $service['prefix'];
105
					}
106 61e047a5 Phil Davis
					if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
107 ba8495f0 Ermal
						mwexec_bg("{$prefix}{$service['rcfile']} start");
108 a4ea3340 Scott Ullrich
					}
109 31f346a8 Colin Smith
				}
110 61e047a5 Phil Davis
				if (!empty($service['startcmd'])) {
111 31f346a8 Colin Smith
					eval($service['startcmd']);
112 61e047a5 Phil Davis
				}
113 31f346a8 Colin Smith
				break;
114
			}
115
		}
116
	}
117
}
118
119 58826c73 Colin Smith
function stop_service($name) {
120 cd72ded3 Timo Boettcher
	global $config;
121 246a887a Ermal
122 61e047a5 Phil Davis
	if (empty($name)) {
123 8bf2e9e5 Ermal
		return;
124 61e047a5 Phil Davis
	}
125 8bf2e9e5 Ermal
126 b4d772dc Ermal Luçi
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
127 61e047a5 Phil Davis
		foreach ($config['installedpackages']['service'] as $service) {
128
			if (strtolower($service['name']) == strtolower($name)) {
129
				if ($service['rcfile']) {
130 e9d66ed4 phildd
					$prefix = RCFILEPREFIX;
131 61e047a5 Phil Davis
					if (!empty($service['prefix'])) {
132 cd72ded3 Timo Boettcher
						$prefix =& $service['prefix'];
133
					}
134 61e047a5 Phil Davis
					if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
135 6ae78f08 Marcello Coutinho
						mwexec("{$prefix}{$service['rcfile']} stop");
136 098820e2 Ermal
					}
137
					return;
138 cd72ded3 Timo Boettcher
				}
139 61e047a5 Phil Davis
				if (!empty($service['stopcmd'])) {
140 cd72ded3 Timo Boettcher
					eval($service['stopcmd']);
141 61e047a5 Phil Davis
				}
142 ba8495f0 Ermal
143 cd72ded3 Timo Boettcher
				break;
144
			}
145
		}
146
	}
147 31f346a8 Colin Smith
}
148
149 58826c73 Colin Smith
function restart_service($name) {
150 cd72ded3 Timo Boettcher
	global $config;
151 ba8495f0 Ermal
152 61e047a5 Phil Davis
	if (empty($name)) {
153 8bf2e9e5 Ermal
		return;
154 61e047a5 Phil Davis
	}
155 8bf2e9e5 Ermal
156 636ab238 Colin Smith
	stop_service($name);
157
	start_service($name);
158 ba8495f0 Ermal
159 b4d772dc Ermal Luçi
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
160 61e047a5 Phil Davis
		foreach ($config['installedpackages']['service'] as $service) {
161
			if (strtolower($service['name']) == strtolower($name)) {
162
				if ($service['restartcmd']) {
163 cd72ded3 Timo Boettcher
					eval($service['restartcmd']);
164
				}
165
				break;
166
			}
167
		}
168
	}
169 31f346a8 Colin Smith
}
170 ec4e071a Colin Smith
171 cd72ded3 Timo Boettcher
function is_pid_running($pidfile) {
172 61e047a5 Phil Davis
	if (!file_exists($pidfile)) {
173 ba8495f0 Ermal
		return false;
174 61e047a5 Phil Davis
	}
175 ba8495f0 Ermal
176 17098641 Ermal
	return (isvalidpid($pidfile));
177 cd72ded3 Timo Boettcher
}
178
179 8ff8520f Scott Ullrich
function is_dhcp_running($interface) {
180 fda8dc28 Seth Mos
	$status = find_dhclient_process($interface);
181 61e047a5 Phil Davis
	if ($status != 0) {
182 ca88e48f Scott Ullrich
		return true;
183 61e047a5 Phil Davis
	}
184 ca88e48f Scott Ullrich
	return false;
185 8ff8520f Scott Ullrich
}
186
187 0661a033 Colin Smith
function restart_service_if_running($service) {
188
	global $config;
189 61e047a5 Phil Davis
	if (is_service_running($service)) {
190 0661a033 Colin Smith
		restart_service($service);
191 61e047a5 Phil Davis
	}
192 0661a033 Colin Smith
	return;
193
}
194
195 ccfc0269 phildd
function is_service_enabled($service_name) {
196
	global $config;
197 61e047a5 Phil Davis
	if ($service_name == "") {
198 ccfc0269 phildd
		return false;
199 61e047a5 Phil Davis
	}
200 762e8cf9 jim-p
	if (is_array($config['installedpackages'])) {
201
		if (isset($config['installedpackages'][$service_name]['config'][0]['enable']) &&
202 61e047a5 Phil Davis
		    ((empty($config['installedpackages'][$service_name]['config'][0]['enable'])) ||
203
		    ($config['installedpackages'][$service_name]['config'][0]['enable'] === 'off'))) {
204 762e8cf9 jim-p
			return false;
205
		}
206
	}
207 ccfc0269 phildd
	return true;
208
}
209
210 fbeb6d02 Colin Smith
function is_service_running($service, $ps = "") {
211
	global $config;
212 ba8495f0 Ermal
213 61e047a5 Phil Davis
	if (is_array($config['installedpackages']['service'])) {
214
		foreach ($config['installedpackages']['service'] as $aservice) {
215
			if (strtolower($service) == strtolower($aservice['name'])) {
216 ba8495f0 Ermal
				if ($aservice['custom_php_service_status_command'] <> "") {
217
					eval("\$rc={$aservice['custom_php_service_status_command']};");
218 c2d49311 Eirik Oeverby
					return $rc;
219
				}
220 61e047a5 Phil Davis
				if (empty($aservice['executable'])) {
221 ec4e071a Colin Smith
					return false;
222 61e047a5 Phil Davis
				}
223
				if (is_process_running($aservice['executable'])) {
224 ba8495f0 Ermal
					return true;
225 61e047a5 Phil Davis
				}
226 ba8495f0 Ermal
227
				return false;
228 cd72ded3 Timo Boettcher
			}
229
		}
230
	}
231 ba8495f0 Ermal
232 61e047a5 Phil Davis
	if (is_process_running($service)) {
233 ba8495f0 Ermal
		return true;
234 61e047a5 Phil Davis
	}
235 ba8495f0 Ermal
236
	return false;
237 ec4e071a Colin Smith
}
238 ff074bf9 Scott Ullrich
239 e48cdc01 jim-p
function get_services() {
240
	global $config;
241 61e047a5 Phil Davis
	if (is_array($config['installedpackages']['service'])) {
242 e48cdc01 jim-p
		$services = $config['installedpackages']['service'];
243 61e047a5 Phil Davis
	} else {
244 e48cdc01 jim-p
		$services = array();
245 61e047a5 Phil Davis
	}
246 e48cdc01 jim-p
247 61e047a5 Phil Davis
	/*
248
	 * Add services that are in the base.
249 e48cdc01 jim-p
	 */
250 33232486 Warren Baker
	if (is_radvd_enabled()) {
251 e48cdc01 jim-p
		$pconfig = array();
252
		$pconfig['name'] = "radvd";
253
		$pconfig['description'] = gettext("Router Advertisement Daemon");
254
		$services[] = $pconfig;
255
	}
256
257 33232486 Warren Baker
	if (isset($config['dnsmasq']['enable'])) {
258 e48cdc01 jim-p
		$pconfig = array();
259
		$pconfig['name'] = "dnsmasq";
260
		$pconfig['description'] = gettext("DNS Forwarder");
261
		$services[] = $pconfig;
262
	}
263
264 33232486 Warren Baker
	if (isset($config['unbound']['enable'])) {
265
		$pconfig = array();
266
		$pconfig['name'] = "unbound";
267 a3fb1412 Phil Davis
		$pconfig['description'] = gettext("DNS Resolver");
268 33232486 Warren Baker
		$services[] = $pconfig;
269
	}
270
271 e48cdc01 jim-p
	$pconfig = array();
272
	$pconfig['name'] = "ntpd";
273
	$pconfig['description'] = gettext("NTP clock sync");
274
	$services[] = $pconfig;
275
276
	if (is_array($config['captiveportal'])) {
277
		foreach ($config['captiveportal'] as $zone => $setting) {
278
			if (isset($setting['enable'])) {
279
				$pconfig = array();
280
				$pconfig['name'] = "captiveportal";
281
				$pconfig['zone'] = $zone;
282
				$pconfig['description'] = gettext("Captive Portal") . ": ".htmlspecialchars($setting['zone']);
283
				$services[] = $pconfig;
284
			}
285
		}
286
	}
287
288
	$iflist = array();
289
	$ifdescrs = get_configured_interface_list();
290
	foreach ($ifdescrs as $if) {
291
		$oc = $config['interfaces'][$if];
292 61e047a5 Phil Davis
		if ($oc['if'] && (!link_interface_to_bridge($if))) {
293 e48cdc01 jim-p
			$iflist[$if] = $if;
294 61e047a5 Phil Davis
		}
295 e48cdc01 jim-p
	}
296
297 33232486 Warren Baker
	if (isset($config['dhcrelay']['enable'])) {
298 e48cdc01 jim-p
		$pconfig = array();
299
		$pconfig['name'] = "dhcrelay";
300
		$pconfig['description'] = gettext("DHCP Relay");
301
		$services[] = $pconfig;
302
	}
303
304 33232486 Warren Baker
	if (isset($config['dhcrelay6']['enable'])) {
305 9590e0de Phil Davis
		$pconfig = array();
306
		$pconfig['name'] = "dhcrelay6";
307
		$pconfig['description'] = gettext("DHCPv6 Relay");
308
		$services[] = $pconfig;
309
	}
310
311 33232486 Warren Baker
	if (is_dhcp_server_enabled()) {
312 e48cdc01 jim-p
		$pconfig = array();
313
		$pconfig['name'] = "dhcpd";
314
		$pconfig['description'] = gettext("DHCP Service");
315
		$services[] = $pconfig;
316
	}
317
318 b05a8f35 jim-p
	$gateways_arr = return_gateways_array();
319
	if (is_array($gateways_arr)) {
320
		$pconfig = array();
321
		$pconfig['name'] = "apinger";
322
		$pconfig['description'] = gettext("Gateway Monitoring Daemon");
323
		$services[] = $pconfig;
324
	}
325
326 33232486 Warren Baker
	if (isset($config['snmpd']['enable'])) {
327 e48cdc01 jim-p
		$pconfig = array();
328
		$pconfig['name'] = "bsnmpd";
329
		$pconfig['description'] = gettext("SNMP Service");
330
		$services[] = $pconfig;
331
	}
332
333
	if (is_array($config['igmpproxy']['igmpentry']) && (count($config['igmpproxy']['igmpentry']) > 0)) {
334
		$pconfig = array();
335
		$pconfig['name'] = "igmpproxy";
336
		$pconfig['description'] = gettext("IGMP proxy");
337
		$services[] = $pconfig;
338
	}
339
340
	if (isset($config['installedpackages']['miniupnpd']) && $config['installedpackages']['miniupnpd']['config'][0]['enable']) {
341
		$pconfig = array();
342
		$pconfig['name'] = "miniupnpd";
343
		$pconfig['description'] = gettext("UPnP Service");
344
		$services[] = $pconfig;
345
	}
346
347
	if (isset($config['ipsec']['enable'])) {
348
		$pconfig = array();
349 8b4abd59 Ermal
		$pconfig['name'] = "ipsec";
350 e48cdc01 jim-p
		$pconfig['description'] = gettext("IPsec VPN");
351
		$services[] = $pconfig;
352
	}
353
354 aaa78416 jim-p
	if (isset($config['system']['enablesshd'])) {
355
		$pconfig = array();
356
		$pconfig['name'] = "sshd";
357
		$pconfig['description'] = gettext("Secure Shell Daemon");
358
		$services[] = $pconfig;
359
	}
360
361 e48cdc01 jim-p
	foreach (array('server', 'client') as $mode) {
362
		if (is_array($config['openvpn']["openvpn-{$mode}"])) {
363
			foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
364
				if (!isset($setting['disable'])) {
365
					$pconfig = array();
366
					$pconfig['name'] = "openvpn";
367
					$pconfig['mode'] = $mode;
368
					$pconfig['id'] = $id;
369
					$pconfig['vpnid'] = $setting['vpnid'];
370
					$pconfig['description'] = gettext("OpenVPN") . " ".$mode.": ".htmlspecialchars($setting['description']);
371
					$services[] = $pconfig;
372
				}
373
			}
374
		}
375
	}
376
377
	if (count($config['load_balancer']['virtual_server']) && count($config['load_balancer']['lbpool'])) {
378
		$pconfig = array();
379
		$pconfig['name'] = "relayd";
380
		$pconfig['description'] = gettext("Server load balancing daemon");
381
		$services[] = $pconfig;
382
	}
383
	return $services;
384
}
385
386 76692ad2 jim-p
function find_service_by_name($name) {
387
	$services = get_services();
388 61e047a5 Phil Davis
	foreach ($services as $service) {
389
		if ($service["name"] == $name) {
390 76692ad2 jim-p
			return $service;
391 61e047a5 Phil Davis
		}
392
	}
393 76692ad2 jim-p
	return array();
394
}
395
396 6d9b1074 jim-p
function find_service_by_openvpn_vpnid($vpnid) {
397
	$services = get_services();
398 61e047a5 Phil Davis
	foreach ($services as $service) {
399
		if (($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) {
400 6d9b1074 jim-p
			return $service;
401 61e047a5 Phil Davis
		}
402
	}
403 6d9b1074 jim-p
	return array();
404
}
405
406
function find_service_by_cp_zone($zone) {
407
	$services = get_services();
408 61e047a5 Phil Davis
	foreach ($services as $service) {
409
		if (($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) {
410 6d9b1074 jim-p
			return $service;
411 61e047a5 Phil Davis
		}
412
	}
413 6d9b1074 jim-p
	return array();
414
}
415
416 e48cdc01 jim-p
function service_name_compare($a, $b) {
417 61e047a5 Phil Davis
	if (strtolower($a['name']) == strtolower($b['name'])) {
418 e48cdc01 jim-p
		return 0;
419 61e047a5 Phil Davis
	}
420 e48cdc01 jim-p
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
421
}
422
423
function get_pkg_descr($package_name) {
424
	global $config;
425
	if (is_array($config['installedpackages']['package'])) {
426 61e047a5 Phil Davis
		foreach ($config['installedpackages']['package'] as $pkg) {
427
			if ($pkg['name'] == $package_name) {
428 e48cdc01 jim-p
				return $pkg['descr'];
429 61e047a5 Phil Davis
			}
430 e48cdc01 jim-p
		}
431
	}
432
	return gettext("Not available.");
433
}
434
435
function get_service_status($service) {
436
	global $g;
437
	switch ($service['name']) {
438
		case "openvpn":
439
			$running = is_pid_running("{$g['varrun_path']}/openvpn_{$service['mode']}{$service['vpnid']}.pid");
440
			break;
441
		case "captiveportal":
442
			$running = is_pid_running("{$g['varrun_path']}/lighty-{$service['zone']}-CaptivePortal.pid");
443 61e047a5 Phil Davis
			if (isset($config['captiveportal'][$service['zone']]['httpslogin'])) {
444 e48cdc01 jim-p
				$running = $running && is_pid_running("{$g['varrun_path']}/lighty-{$service['zone']}-CaptivePortal-SSL.pid");
445 61e047a5 Phil Davis
			}
446 e48cdc01 jim-p
			break;
447 18725085 Ermal
		case "vhosts-http":
448
			$running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid");
449
			break;
450 9590e0de Phil Davis
		case "dhcrelay6":
451
			$running = is_pid_running("{$g['varrun_path']}/dhcrelay6.pid");
452
			break;
453 e381cc01 Ermal
		case 'ipsec':
454
			$running = is_pid_running("{$g['varrun_path']}/charon.pid");
455
			break;
456 e48cdc01 jim-p
		default:
457
			$running = is_service_running($service['name']);
458
	}
459
	return $running;
460
}
461
462 36d1c798 jim-p
function get_service_status_icon($service, $withtext = true, $smallicon = false) {
463 e48cdc01 jim-p
	global $g;
464
	$output = "";
465 61e047a5 Phil Davis
	if (get_service_status($service)) {
466 92421710 jim-p
		$statustext = gettext("Running");
467 9f605c1c Hari
		$output .= "<a title=\"" . sprintf(gettext("%s Service is"), $service["name"]) . " {$statustext}\" ><i class=\"";
468
		$output .= ($smallicon) ? "icon icon-play" : "icon icon-large icon-play";
469
		$output .= "\" ></i></a>";
470 61e047a5 Phil Davis
		if ($withtext) {
471 17b8c60a Colin Fleming
			$output .= "&nbsp;" . $statustext;
472 61e047a5 Phil Davis
		}
473 e48cdc01 jim-p
	} else {
474 ccfc0269 phildd
		$service_enabled = is_service_enabled($service['name']);
475
		$statustext = ($service_enabled) ? gettext("Stopped") : gettext("Disabled");
476 9f605c1c Hari
		$output .= "<a title=\"" . sprintf(gettext("%s Service is"), $service["name"]) . " {$statustext}\" ><i class=\"";
477
		$output .= ($smallicon) ? "icon icon-remove" : "icon icon-large icon-remove";
478
		$output .= "\" ></i></a>";
479 61e047a5 Phil Davis
		if ($withtext) {
480 9f605c1c Hari
			$output .= "&nbsp;" . $statustext;
481 61e047a5 Phil Davis
		}
482 e48cdc01 jim-p
	}
483
	return $output;
484
}
485
486 f4daf025 jim-p
function get_service_control_links($service, $addname = false) {
487 49840447 PiBa-NL
	global $g;
488 e48cdc01 jim-p
	$output = "";
489 f4daf025 jim-p
	$stitle = ($addname) ? $service['name'] . " " : "";
490 1180e4f0 Sjon Hortensius
491
	switch ($service['name']) {
492
		case "openvpn":
493 0b1c2965 Stephen Beaver
			$link = '<a title="%s" href="status_services.php?mode=%s&amp;service='.$service['name'] . '&amp;vpnmode=' . $service['mode'] . '&amp;id=' . $service['vpnid'] . '">';
494 1180e4f0 Sjon Hortensius
		break;
495
		case "captiveportal":
496 0b1c2965 Stephen Beaver
			$link = '<a title="%s" href="status_services.php?mode=%s&amp;service=' . $service['name'] . '&amp;zone=' . $service['zone'] . '">';
497 1180e4f0 Sjon Hortensius
		break;
498
		default:
499 0b1c2965 Stephen Beaver
			$link = '<a title="%s" href="status_services.php?mode=%s&amp;service=' . $service['name'] . '">';
500 1180e4f0 Sjon Hortensius
	}
501
502 61e047a5 Phil Davis
	if (get_service_status($service)) {
503 e48cdc01 jim-p
		switch ($service['name']) {
504
			case "openvpn":
505 bb877f44 Colin Fleming
				$output .= "<a href='status_services.php?mode=restartservice&amp;service={$service['name']}&amp;vpnmode={$service['mode']}&amp;id={$service['vpnid']}'>";
506 e48cdc01 jim-p
				break;
507
			case "captiveportal":
508 bb877f44 Colin Fleming
				$output .= "<a href='status_services.php?mode=restartservice&amp;service={$service['name']}&amp;zone={$service['zone']}'>";
509 e48cdc01 jim-p
				break;
510
			default:
511 bb877f44 Colin Fleming
				$output .= "<a href='status_services.php?mode=restartservice&amp;service={$service['name']}'>";
512 e48cdc01 jim-p
		}
513 eafd375a Jared Dillard
		$output .= "<i class=\"icon-large icon-repeat\" title='" . sprintf(gettext("Restart %sService"), $stitle) . "' alt='restart'></i></a>\n";
514 e48cdc01 jim-p
		switch ($service['name']) {
515
			case "openvpn":
516 bb877f44 Colin Fleming
				$output .= "<a href='status_services.php?mode=stopservice&amp;service={$service['name']}&amp;vpnmode={$service['mode']}&amp;id={$service['vpnid']}'>";
517 e48cdc01 jim-p
				break;
518
			case "captiveportal":
519 bb877f44 Colin Fleming
				$output .= "<a href='status_services.php?mode=stopservice&amp;service={$service['name']}&amp;zone={$service['zone']}'>";
520 e48cdc01 jim-p
				break;
521
			default:
522 bb877f44 Colin Fleming
				$output .= "<a href='status_services.php?mode=stopservice&amp;service={$service['name']}'>";
523 e48cdc01 jim-p
		}
524 eafd375a Jared Dillard
		$output .= "<i class=\"icon-large icon-stop\" title='" . sprintf(gettext("Stop %sService"), $stitle) . "' alt='stop'></i></a>";
525 e48cdc01 jim-p
	} else {
526 ccfc0269 phildd
		$service_enabled = is_service_enabled($service['name']);
527 1180e4f0 Sjon Hortensius
528 46bb8a0b Sjon Hortensius
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
529 1180e4f0 Sjon Hortensius
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
530
			$output .= '<i class="icon icon-play-circle"></i></a> ';
531 e48cdc01 jim-p
		}
532
	}
533 1180e4f0 Sjon Hortensius
534 e48cdc01 jim-p
	return $output;
535
}
536 ee6e6011 jim-p
537
function service_control_start($name, $extras) {
538
	global $g;
539 61e047a5 Phil Davis
	switch ($name) {
540 ee6e6011 jim-p
		case 'radvd':
541
			services_radvd_configure();
542
			break;
543
		case 'captiveportal':
544 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
545 ee6e6011 jim-p
			captiveportal_init_webgui_zonename($zone);
546
			break;
547
		case 'ntpd':
548
		case 'openntpd':
549
			system_ntp_configure();
550
			break;
551 b05a8f35 jim-p
		case 'apinger':
552
			setup_gateways_monitor();
553
			break;
554 ee6e6011 jim-p
		case 'bsnmpd':
555
			services_snmpd_configure();
556
			break;
557 9590e0de Phil Davis
		case 'dhcrelay':
558
			services_dhcrelay_configure();
559
			break;
560
		case 'dhcrelay6':
561
			services_dhcrelay6_configure();
562
			break;
563 ee6e6011 jim-p
		case 'dnsmasq':
564
			services_dnsmasq_configure();
565
			break;
566 a8604dc6 Phil Davis
		case 'unbound':
567
			services_unbound_configure();
568
			break;
569 ee6e6011 jim-p
		case 'dhcpd':
570
			services_dhcpd_configure();
571
			break;
572
		case 'igmpproxy':
573
			services_igmpproxy_configure();
574
			break;
575
		case 'miniupnpd':
576
			upnp_action('start');
577
			break;
578 8b4abd59 Ermal
		case 'ipsec':
579 ee6e6011 jim-p
			vpn_ipsec_force_reload();
580
			break;
581 aaa78416 jim-p
		case 'sshd':
582
			send_event("service restart sshd");
583
			break;
584 ee6e6011 jim-p
		case 'openvpn':
585 2f9951fe Renato Botelho
			$vpnmode = isset($extras['vpnmode']) ? htmlspecialchars($extras['vpnmode']) : htmlspecialchars($extras['mode']);
586 ee6e6011 jim-p
			if (($vpnmode == "server") || ($vpnmode == "client")) {
587 2f9951fe Renato Botelho
				$id = isset($extras['vpnid']) ? htmlspecialchars($extras['vpnid']) : htmlspecialchars($extras['id']);
588 ee6e6011 jim-p
				$configfile = "{$g['varetc_path']}/openvpn/{$vpnmode}{$id}.conf";
589 61e047a5 Phil Davis
				if (file_exists($configfile)) {
590 ee6e6011 jim-p
					openvpn_restart_by_vpnid($vpnmode, $id);
591 61e047a5 Phil Davis
				}
592 ee6e6011 jim-p
			}
593
			break;
594
		case 'relayd':
595
			relayd_configure();
596
			break;
597
		default:
598
			start_service($name);
599
			break;
600
	}
601 086cf944 Phil Davis
	return sprintf(gettext("%s has been started."), htmlspecialchars($name));
602 ee6e6011 jim-p
}
603
function service_control_stop($name, $extras) {
604
	global $g;
605 61e047a5 Phil Davis
	switch ($name) {
606 ee6e6011 jim-p
		case 'radvd':
607
			killbypid("{$g['varrun_path']}/radvd.pid");
608
			break;
609
		case 'captiveportal':
610 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
611 ee6e6011 jim-p
			killbypid("{$g['varrun_path']}/lighty-{$zone}-CaptivePortal.pid");
612
			killbypid("{$g['varrun_path']}/lighty-{$zone}-CaptivePortal-SSL.pid");
613
			break;
614
		case 'ntpd':
615
			killbyname("ntpd");
616
			break;
617
		case 'openntpd':
618
			killbyname("openntpd");
619
			break;
620 b05a8f35 jim-p
		case 'apinger':
621
			killbypid("{$g['varrun_path']}/apinger.pid");
622
			break;
623 ee6e6011 jim-p
		case 'bsnmpd':
624
			killbypid("{$g['varrun_path']}/snmpd.pid");
625
			break;
626
		case 'choparp':
627
			killbyname("choparp");
628
			break;
629
		case 'dhcpd':
630
			killbyname("dhcpd");
631
			break;
632
		case 'dhcrelay':
633
			killbypid("{$g['varrun_path']}/dhcrelay.pid");
634
			break;
635 9590e0de Phil Davis
		case 'dhcrelay6':
636
			killbypid("{$g['varrun_path']}/dhcrelay6.pid");
637
			break;
638 ee6e6011 jim-p
		case 'dnsmasq':
639
			killbypid("{$g['varrun_path']}/dnsmasq.pid");
640
			break;
641 33232486 Warren Baker
		case 'unbound':
642
			killbypid("{$g['varrun_path']}/unbound.pid");
643
			break;
644 ee6e6011 jim-p
		case 'igmpproxy':
645
			killbyname("igmpproxy");
646
			break;
647
		case 'miniupnpd':
648
			upnp_action('stop');
649
			break;
650
		case 'sshd':
651
			killbyname("sshd");
652
			break;
653 8b4abd59 Ermal
		case 'ipsec':
654
			exec("/usr/local/sbin/ipsec stop");
655 ee6e6011 jim-p
			break;
656
		case 'openvpn':
657 2f9951fe Renato Botelho
			$vpnmode = htmlspecialchars($extras['vpnmode']);
658 ee6e6011 jim-p
			if (($vpnmode == "server") or ($vpnmode == "client")) {
659 2f9951fe Renato Botelho
				$id = htmlspecialchars($extras['id']);
660 ee6e6011 jim-p
				$pidfile = "{$g['varrun_path']}/openvpn_{$vpnmode}{$id}.pid";
661
				killbypid($pidfile);
662
			}
663
			break;
664
		case 'relayd':
665
			mwexec('pkill relayd');
666
			break;
667
		default:
668
			stop_service($name);
669
			break;
670
	}
671
	return sprintf(gettext("%s has been stopped."), htmlspecialchars($name));
672
}
673 33232486 Warren Baker
674 ee6e6011 jim-p
function service_control_restart($name, $extras) {
675
	global $g;
676 61e047a5 Phil Davis
	switch ($name) {
677 ee6e6011 jim-p
		case 'radvd':
678
			services_radvd_configure();
679
			break;
680
		case 'captiveportal':
681 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
682 ee6e6011 jim-p
			killbypid("{$g['varrun_path']}/lighty-{$zone}-CaptivePortal.pid");
683
			killbypid("{$g['varrun_path']}/lighty-{$zone}-CaptivePortal-SSL.pid");
684
			captiveportal_init_webgui_zonename($zone);
685
			break;
686
		case 'ntpd':
687
		case 'openntpd':
688
			system_ntp_configure();
689
			break;
690 b05a8f35 jim-p
		case 'apinger':
691
			killbypid("{$g['varrun_path']}/apinger.pid");
692
			setup_gateways_monitor();
693
			break;
694 ee6e6011 jim-p
		case 'bsnmpd':
695
			services_snmpd_configure();
696
			break;
697 9590e0de Phil Davis
		case 'dhcrelay':
698
			services_dhcrelay_configure();
699
			break;
700
		case 'dhcrelay6':
701
			services_dhcrelay6_configure();
702
			break;
703 ee6e6011 jim-p
		case 'dnsmasq':
704
			services_dnsmasq_configure();
705
			break;
706 33232486 Warren Baker
		case 'unbound':
707
			services_unbound_configure();
708
			break;
709 ee6e6011 jim-p
		case 'dhcpd':
710
			services_dhcpd_configure();
711
			break;
712
		case 'igmpproxy':
713
			services_igmpproxy_configure();
714
			break;
715
		case 'miniupnpd':
716
			upnp_action('restart');
717
			break;
718 8b4abd59 Ermal
		case 'ipsec':
719 ee6e6011 jim-p
			vpn_ipsec_force_reload();
720
			break;
721 aaa78416 jim-p
		case 'sshd':
722
			send_event("service restart sshd");
723
			break;
724 ee6e6011 jim-p
		case 'openvpn':
725 2f9951fe Renato Botelho
			$vpnmode = htmlspecialchars($extras['vpnmode']);
726 ee6e6011 jim-p
			if ($vpnmode == "server" || $vpnmode == "client") {
727 2f9951fe Renato Botelho
				$id = htmlspecialchars($extras['id']);
728 ee6e6011 jim-p
				$configfile = "{$g['varetc_path']}/openvpn/{$vpnmode}{$id}.conf";
729 61e047a5 Phil Davis
				if (file_exists($configfile)) {
730 ee6e6011 jim-p
					openvpn_restart_by_vpnid($vpnmode, $id);
731 61e047a5 Phil Davis
				}
732 ee6e6011 jim-p
			}
733
			break;
734
		case 'relayd':
735
			relayd_configure(true);
736
			break;
737
		default:
738
			restart_service($name);
739
			break;
740
	}
741 086cf944 Phil Davis
	return sprintf(gettext("%s has been restarted."), htmlspecialchars($name));
742 ee6e6011 jim-p
}
743
744 17098641 Ermal
?>