Project

General

Profile

Download (21.2 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 d71fc5d3 jim-p
	if (isset($config['installedpackages']['routed']) && $config['installedpackages']['routed']['config'][0]['enable']) {
348
		$pconfig = array();
349
		$pconfig['name'] = "routed";
350
		$pconfig['description'] = gettext("RIP Daemon");
351
		$services[] = $pconfig;
352
	}
353
354 e48cdc01 jim-p
	if (isset($config['ipsec']['enable'])) {
355
		$pconfig = array();
356 8b4abd59 Ermal
		$pconfig['name'] = "ipsec";
357 e48cdc01 jim-p
		$pconfig['description'] = gettext("IPsec VPN");
358
		$services[] = $pconfig;
359
	}
360
361 aaa78416 jim-p
	if (isset($config['system']['enablesshd'])) {
362
		$pconfig = array();
363
		$pconfig['name'] = "sshd";
364
		$pconfig['description'] = gettext("Secure Shell Daemon");
365
		$services[] = $pconfig;
366
	}
367
368 e48cdc01 jim-p
	foreach (array('server', 'client') as $mode) {
369
		if (is_array($config['openvpn']["openvpn-{$mode}"])) {
370
			foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
371
				if (!isset($setting['disable'])) {
372
					$pconfig = array();
373
					$pconfig['name'] = "openvpn";
374
					$pconfig['mode'] = $mode;
375
					$pconfig['id'] = $id;
376
					$pconfig['vpnid'] = $setting['vpnid'];
377
					$pconfig['description'] = gettext("OpenVPN") . " ".$mode.": ".htmlspecialchars($setting['description']);
378
					$services[] = $pconfig;
379
				}
380
			}
381
		}
382
	}
383
384
	if (count($config['load_balancer']['virtual_server']) && count($config['load_balancer']['lbpool'])) {
385
		$pconfig = array();
386
		$pconfig['name'] = "relayd";
387
		$pconfig['description'] = gettext("Server load balancing daemon");
388
		$services[] = $pconfig;
389
	}
390
	return $services;
391
}
392
393 76692ad2 jim-p
function find_service_by_name($name) {
394
	$services = get_services();
395 61e047a5 Phil Davis
	foreach ($services as $service) {
396
		if ($service["name"] == $name) {
397 76692ad2 jim-p
			return $service;
398 61e047a5 Phil Davis
		}
399
	}
400 76692ad2 jim-p
	return array();
401
}
402
403 6d9b1074 jim-p
function find_service_by_openvpn_vpnid($vpnid) {
404
	$services = get_services();
405 61e047a5 Phil Davis
	foreach ($services as $service) {
406
		if (($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) {
407 6d9b1074 jim-p
			return $service;
408 61e047a5 Phil Davis
		}
409
	}
410 6d9b1074 jim-p
	return array();
411
}
412
413
function find_service_by_cp_zone($zone) {
414
	$services = get_services();
415 61e047a5 Phil Davis
	foreach ($services as $service) {
416
		if (($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) {
417 6d9b1074 jim-p
			return $service;
418 61e047a5 Phil Davis
		}
419
	}
420 6d9b1074 jim-p
	return array();
421
}
422
423 e48cdc01 jim-p
function service_name_compare($a, $b) {
424 61e047a5 Phil Davis
	if (strtolower($a['name']) == strtolower($b['name'])) {
425 e48cdc01 jim-p
		return 0;
426 61e047a5 Phil Davis
	}
427 e48cdc01 jim-p
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
428
}
429
430
function get_pkg_descr($package_name) {
431
	global $config;
432
	if (is_array($config['installedpackages']['package'])) {
433 61e047a5 Phil Davis
		foreach ($config['installedpackages']['package'] as $pkg) {
434
			if ($pkg['name'] == $package_name) {
435 e48cdc01 jim-p
				return $pkg['descr'];
436 61e047a5 Phil Davis
			}
437 e48cdc01 jim-p
		}
438
	}
439
	return gettext("Not available.");
440
}
441
442
function get_service_status($service) {
443
	global $g;
444
	switch ($service['name']) {
445
		case "openvpn":
446
			$running = is_pid_running("{$g['varrun_path']}/openvpn_{$service['mode']}{$service['vpnid']}.pid");
447
			break;
448
		case "captiveportal":
449
			$running = is_pid_running("{$g['varrun_path']}/lighty-{$service['zone']}-CaptivePortal.pid");
450 61e047a5 Phil Davis
			if (isset($config['captiveportal'][$service['zone']]['httpslogin'])) {
451 e48cdc01 jim-p
				$running = $running && is_pid_running("{$g['varrun_path']}/lighty-{$service['zone']}-CaptivePortal-SSL.pid");
452 61e047a5 Phil Davis
			}
453 e48cdc01 jim-p
			break;
454 18725085 Ermal
		case "vhosts-http":
455
			$running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid");
456
			break;
457 9590e0de Phil Davis
		case "dhcrelay6":
458
			$running = is_pid_running("{$g['varrun_path']}/dhcrelay6.pid");
459
			break;
460 e381cc01 Ermal
		case 'ipsec':
461
			$running = is_pid_running("{$g['varrun_path']}/charon.pid");
462
			break;
463 e48cdc01 jim-p
		default:
464
			$running = is_service_running($service['name']);
465
	}
466
	return $running;
467
}
468
469 36d1c798 jim-p
function get_service_status_icon($service, $withtext = true, $smallicon = false) {
470 e48cdc01 jim-p
	global $g;
471
	$output = "";
472 61e047a5 Phil Davis
	if (get_service_status($service)) {
473 92421710 jim-p
		$statustext = gettext("Running");
474 36d1c798 jim-p
		$output .= "<img style=\"vertical-align:middle\" title=\"" . sprintf(gettext("%s Service is"),$service["name"]) . " {$statustext}\" src=\"/themes/" . $g["theme"] . "/images/icons/";
475
		$output .= ($smallicon) ? "icon_pass.gif" : "icon_service_running.gif";
476 17b8c60a Colin Fleming
		$output .= "\" alt=\"status\" />&nbsp;";
477 61e047a5 Phil Davis
		if ($withtext) {
478 17b8c60a Colin Fleming
			$output .= "&nbsp;" . $statustext;
479 61e047a5 Phil Davis
		}
480 e48cdc01 jim-p
	} else {
481 ccfc0269 phildd
		$service_enabled = is_service_enabled($service['name']);
482
		$statustext = ($service_enabled) ? gettext("Stopped") : gettext("Disabled");
483 36d1c798 jim-p
		$output .= "<img style=\"vertical-align:middle\" title=\"" . sprintf(gettext("%s Service is"),$service["name"]) . " {$statustext}\" src=\"/themes/" . $g["theme"] . "/images/icons/";
484
		$output .= ($smallicon) ? "icon_block.gif" : "icon_service_stopped.gif";
485 17b8c60a Colin Fleming
		$output .= "\" alt=\"status\" />&nbsp;";
486 61e047a5 Phil Davis
		if ($withtext) {
487 17b8c60a Colin Fleming
			$output .= "&nbsp;<font color=\"white\">{$statustext}</font>";
488 61e047a5 Phil Davis
		}
489 e48cdc01 jim-p
	}
490
	return $output;
491
}
492
493 f4daf025 jim-p
function get_service_control_links($service, $addname = false) {
494 e48cdc01 jim-p
	global $g;
495
	$output = "";
496 f4daf025 jim-p
	$stitle = ($addname) ? $service['name'] . " " : "";
497 61e047a5 Phil Davis
	if (get_service_status($service)) {
498 e48cdc01 jim-p
		switch ($service['name']) {
499
			case "openvpn":
500 bb877f44 Colin Fleming
				$output .= "<a href='status_services.php?mode=restartservice&amp;service={$service['name']}&amp;vpnmode={$service['mode']}&amp;id={$service['vpnid']}'>";
501 e48cdc01 jim-p
				break;
502
			case "captiveportal":
503 bb877f44 Colin Fleming
				$output .= "<a href='status_services.php?mode=restartservice&amp;service={$service['name']}&amp;zone={$service['zone']}'>";
504 e48cdc01 jim-p
				break;
505
			default:
506 bb877f44 Colin Fleming
				$output .= "<a href='status_services.php?mode=restartservice&amp;service={$service['name']}'>";
507 e48cdc01 jim-p
		}
508 92c27873 dneuhaeuser
		$output .= "<img style=\"vertical-align:middle\" title='" . sprintf(gettext("Restart %sService"),$stitle) . "' border='0' src='/themes/".$g['theme']."/images/icons/icon_service_restart.gif' alt='restart' /></a>\n";
509 e48cdc01 jim-p
		switch ($service['name']) {
510
			case "openvpn":
511 bb877f44 Colin Fleming
				$output .= "<a href='status_services.php?mode=stopservice&amp;service={$service['name']}&amp;vpnmode={$service['mode']}&amp;id={$service['vpnid']}'>";
512 e48cdc01 jim-p
				break;
513
			case "captiveportal":
514 bb877f44 Colin Fleming
				$output .= "<a href='status_services.php?mode=stopservice&amp;service={$service['name']}&amp;zone={$service['zone']}'>";
515 e48cdc01 jim-p
				break;
516
			default:
517 bb877f44 Colin Fleming
				$output .= "<a href='status_services.php?mode=stopservice&amp;service={$service['name']}'>";
518 e48cdc01 jim-p
		}
519 92c27873 dneuhaeuser
		$output .= "<img style=\"vertical-align:middle\" title='" . sprintf(gettext("Stop %sService"),$stitle) . "' border='0' src='/themes/".$g['theme']."/images/icons/icon_service_stop.gif' alt='stop' />";
520 e48cdc01 jim-p
		$output .= "</a>";
521
	} else {
522 ccfc0269 phildd
		$service_enabled = is_service_enabled($service['name']);
523 e48cdc01 jim-p
		switch ($service['name']) {
524
			case "openvpn":
525 bb877f44 Colin Fleming
				$output .= "<a href='status_services.php?mode=startservice&amp;service={$service['name']}&amp;vpnmode={$service['mode']}&amp;id={$service['vpnid']}'>";
526 e48cdc01 jim-p
				break;
527
			case "captiveportal":
528 bb877f44 Colin Fleming
				$output .= "<a href='status_services.php?mode=startservice&amp;service={$service['name']}&amp;zone={$service['zone']}'>";
529 e48cdc01 jim-p
				break;
530
			default:
531 61e047a5 Phil Davis
				if ($service_enabled) {
532 ccfc0269 phildd
					$output .= "<a href='status_services.php?mode=startservice&amp;service={$service['name']}'>";
533 61e047a5 Phil Davis
				}
534 e48cdc01 jim-p
		}
535 61e047a5 Phil Davis
		if ($service_enabled) {
536 92c27873 dneuhaeuser
			$output .= "<img style=\"vertical-align:middle\" title='" . sprintf(gettext("Start %sService"),$stitle) . "' border='0' src='/themes/".$g['theme']."/images/icons/icon_service_start.gif' alt='start' /></a>\n";
537 61e047a5 Phil Davis
		}
538 e48cdc01 jim-p
	}
539
	return $output;
540
}
541 ee6e6011 jim-p
542
function service_control_start($name, $extras) {
543
	global $g;
544 61e047a5 Phil Davis
	switch ($name) {
545 ee6e6011 jim-p
		case 'radvd':
546
			services_radvd_configure();
547
			break;
548
		case 'captiveportal':
549 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
550 ee6e6011 jim-p
			captiveportal_init_webgui_zonename($zone);
551
			break;
552
		case 'ntpd':
553
		case 'openntpd':
554
			system_ntp_configure();
555
			break;
556 b05a8f35 jim-p
		case 'apinger':
557
			setup_gateways_monitor();
558
			break;
559 ee6e6011 jim-p
		case 'bsnmpd':
560
			services_snmpd_configure();
561
			break;
562 9590e0de Phil Davis
		case 'dhcrelay':
563
			services_dhcrelay_configure();
564
			break;
565
		case 'dhcrelay6':
566
			services_dhcrelay6_configure();
567
			break;
568 ee6e6011 jim-p
		case 'dnsmasq':
569
			services_dnsmasq_configure();
570
			break;
571 a8604dc6 Phil Davis
		case 'unbound':
572
			services_unbound_configure();
573
			break;
574 ee6e6011 jim-p
		case 'dhcpd':
575
			services_dhcpd_configure();
576
			break;
577
		case 'igmpproxy':
578
			services_igmpproxy_configure();
579
			break;
580
		case 'miniupnpd':
581
			upnp_action('start');
582
			break;
583 8b4abd59 Ermal
		case 'ipsec':
584 ee6e6011 jim-p
			vpn_ipsec_force_reload();
585
			break;
586 aaa78416 jim-p
		case 'sshd':
587
			send_event("service restart sshd");
588
			break;
589 ee6e6011 jim-p
		case 'openvpn':
590 2f9951fe Renato Botelho
			$vpnmode = isset($extras['vpnmode']) ? htmlspecialchars($extras['vpnmode']) : htmlspecialchars($extras['mode']);
591 ee6e6011 jim-p
			if (($vpnmode == "server") || ($vpnmode == "client")) {
592 2f9951fe Renato Botelho
				$id = isset($extras['vpnid']) ? htmlspecialchars($extras['vpnid']) : htmlspecialchars($extras['id']);
593 ee6e6011 jim-p
				$configfile = "{$g['varetc_path']}/openvpn/{$vpnmode}{$id}.conf";
594 61e047a5 Phil Davis
				if (file_exists($configfile)) {
595 ee6e6011 jim-p
					openvpn_restart_by_vpnid($vpnmode, $id);
596 61e047a5 Phil Davis
				}
597 ee6e6011 jim-p
			}
598
			break;
599
		case 'relayd':
600
			relayd_configure();
601
			break;
602
		default:
603
			start_service($name);
604
			break;
605
	}
606
	return sprintf(gettext("%s has been started."),htmlspecialchars($name));
607
}
608
function service_control_stop($name, $extras) {
609
	global $g;
610 61e047a5 Phil Davis
	switch ($name) {
611 ee6e6011 jim-p
		case 'radvd':
612
			killbypid("{$g['varrun_path']}/radvd.pid");
613
			break;
614
		case 'captiveportal':
615 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
616 ee6e6011 jim-p
			killbypid("{$g['varrun_path']}/lighty-{$zone}-CaptivePortal.pid");
617
			killbypid("{$g['varrun_path']}/lighty-{$zone}-CaptivePortal-SSL.pid");
618
			break;
619
		case 'ntpd':
620
			killbyname("ntpd");
621
			break;
622
		case 'openntpd':
623
			killbyname("openntpd");
624
			break;
625 b05a8f35 jim-p
		case 'apinger':
626
			killbypid("{$g['varrun_path']}/apinger.pid");
627
			break;
628 ee6e6011 jim-p
		case 'bsnmpd':
629
			killbypid("{$g['varrun_path']}/snmpd.pid");
630
			break;
631
		case 'choparp':
632
			killbyname("choparp");
633
			break;
634
		case 'dhcpd':
635
			killbyname("dhcpd");
636
			break;
637
		case 'dhcrelay':
638
			killbypid("{$g['varrun_path']}/dhcrelay.pid");
639
			break;
640 9590e0de Phil Davis
		case 'dhcrelay6':
641
			killbypid("{$g['varrun_path']}/dhcrelay6.pid");
642
			break;
643 ee6e6011 jim-p
		case 'dnsmasq':
644
			killbypid("{$g['varrun_path']}/dnsmasq.pid");
645
			break;
646 33232486 Warren Baker
		case 'unbound':
647
			killbypid("{$g['varrun_path']}/unbound.pid");
648
			break;
649 ee6e6011 jim-p
		case 'igmpproxy':
650
			killbyname("igmpproxy");
651
			break;
652
		case 'miniupnpd':
653
			upnp_action('stop');
654
			break;
655
		case 'sshd':
656
			killbyname("sshd");
657
			break;
658 8b4abd59 Ermal
		case 'ipsec':
659
			exec("/usr/local/sbin/ipsec stop");
660 ee6e6011 jim-p
			break;
661
		case 'openvpn':
662 2f9951fe Renato Botelho
			$vpnmode = htmlspecialchars($extras['vpnmode']);
663 ee6e6011 jim-p
			if (($vpnmode == "server") or ($vpnmode == "client")) {
664 2f9951fe Renato Botelho
				$id = htmlspecialchars($extras['id']);
665 ee6e6011 jim-p
				$pidfile = "{$g['varrun_path']}/openvpn_{$vpnmode}{$id}.pid";
666
				killbypid($pidfile);
667
			}
668
			break;
669
		case 'relayd':
670
			mwexec('pkill relayd');
671
			break;
672
		default:
673
			stop_service($name);
674
			break;
675
	}
676
	return sprintf(gettext("%s has been stopped."), htmlspecialchars($name));
677
}
678 33232486 Warren Baker
679 ee6e6011 jim-p
function service_control_restart($name, $extras) {
680
	global $g;
681 61e047a5 Phil Davis
	switch ($name) {
682 ee6e6011 jim-p
		case 'radvd':
683
			services_radvd_configure();
684
			break;
685
		case 'captiveportal':
686 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
687 ee6e6011 jim-p
			killbypid("{$g['varrun_path']}/lighty-{$zone}-CaptivePortal.pid");
688
			killbypid("{$g['varrun_path']}/lighty-{$zone}-CaptivePortal-SSL.pid");
689
			captiveportal_init_webgui_zonename($zone);
690
			break;
691
		case 'ntpd':
692
		case 'openntpd':
693
			system_ntp_configure();
694
			break;
695 b05a8f35 jim-p
		case 'apinger':
696
			killbypid("{$g['varrun_path']}/apinger.pid");
697
			setup_gateways_monitor();
698
			break;
699 ee6e6011 jim-p
		case 'bsnmpd':
700
			services_snmpd_configure();
701
			break;
702 9590e0de Phil Davis
		case 'dhcrelay':
703
			services_dhcrelay_configure();
704
			break;
705
		case 'dhcrelay6':
706
			services_dhcrelay6_configure();
707
			break;
708 ee6e6011 jim-p
		case 'dnsmasq':
709
			services_dnsmasq_configure();
710
			break;
711 33232486 Warren Baker
		case 'unbound':
712
			services_unbound_configure();
713
			break;
714 ee6e6011 jim-p
		case 'dhcpd':
715
			services_dhcpd_configure();
716
			break;
717
		case 'igmpproxy':
718
			services_igmpproxy_configure();
719
			break;
720
		case 'miniupnpd':
721
			upnp_action('restart');
722
			break;
723 8b4abd59 Ermal
		case 'ipsec':
724 ee6e6011 jim-p
			vpn_ipsec_force_reload();
725
			break;
726 aaa78416 jim-p
		case 'sshd':
727
			send_event("service restart sshd");
728
			break;
729 ee6e6011 jim-p
		case 'openvpn':
730 2f9951fe Renato Botelho
			$vpnmode = htmlspecialchars($extras['vpnmode']);
731 ee6e6011 jim-p
			if ($vpnmode == "server" || $vpnmode == "client") {
732 2f9951fe Renato Botelho
				$id = htmlspecialchars($extras['id']);
733 ee6e6011 jim-p
				$configfile = "{$g['varetc_path']}/openvpn/{$vpnmode}{$id}.conf";
734 61e047a5 Phil Davis
				if (file_exists($configfile)) {
735 ee6e6011 jim-p
					openvpn_restart_by_vpnid($vpnmode, $id);
736 61e047a5 Phil Davis
				}
737 ee6e6011 jim-p
			}
738
			break;
739
		case 'relayd':
740
			relayd_configure(true);
741
			break;
742
		default:
743
			restart_service($name);
744
			break;
745
	}
746
	return sprintf(gettext("%s has been restarted."),htmlspecialchars($name));
747
}
748
749 17098641 Ermal
?>