Project

General

Profile

Download (14.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/****h* pfSense/service-utils
3
 * 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
 *
11
 * Copyright (C) 2005-2006 Colin Smith (ethethlay@gmail.com)
12
 * All rights reserved.
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *
16
 * 1. Redistributions of source code must retain the above copyright notice,
17
 * this list of conditions and the following disclaimer.
18
 *
19
 * 2. Redistributions in binary form must reproduce the above copyright
20
 * notice, this list of conditions and the following disclaimer in the
21
 * documentation and/or other materials provided with the distribution.
22
 *
23
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
 * RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
 * POSSIBILITY OF SUCH DAMAGE.
33
 *
34
 */
35

    
36
/*
37
	pfSense_BUILDER_BINARIES:	/bin/pgrep /bin/sh /usr/bin/killall
38
	pfSense_MODULE:	utils
39
*/
40

    
41
define("RCFILEPREFIX", "/usr/local/etc/rc.d/");
42
function write_rcfile($params) {
43
	global $g;
44

    
45
	$rcfile_fullname = RCFILEPREFIX . $params['file'];
46
	if (!file_exists($rcfile_fullname) && !touch($rcfile_fullname))
47
		return false;
48

    
49
	if (!is_writable($rcfile_fullname) || empty($params['start']))
50
		return false;
51

    
52
	$towrite = "#!/bin/sh\n";
53
	$towrite .= "# This file was automatically generated\n# by the {$g['product_name']} service handler.\n\n";
54

    
55
	/* write our rc functions */
56
	$towrite .= "rc_start() {\n";
57
	$towrite .= "\t{$params['start']}\n";
58
	$towrite .= "}\n\n";
59
	if(!empty($params['stop'])) {
60
		$tokill =& $params['stop'];
61
	} else if(!empty($params['executable'])) {
62
		/* just nuke the executable */
63
		$tokill = "/usr/bin/killall {$params['executable']}";
64
	} else {
65
		/* make an educated guess (bad) */
66
		$tokill = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
67
	}
68
	$towrite .= "rc_stop() {\n";
69
	$towrite .= "\t{$tokill}\n";
70
	$towrite .= "}\n\n";
71

    
72
	/* begin rcfile logic */
73
	$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";
74

    
75
	file_put_contents($rcfile_fullname, $towrite);
76
	@chmod("{$rcfile_fullname}", 0755);
77

    
78
	return;
79
}
80

    
81
function start_service($name) {
82
	global $config;
83

    
84
	if (empty($name))
85
		return;
86

    
87
	/* make sure service is stopped before starting */
88
	stop_service($name);
89
	sleep(2);
90

    
91
	$rcfile_fullname = RCFILEPREFIX . $name . '.sh';
92
	if(file_exists($rcfile_fullname)) {
93
		mwexec_bg("/bin/sh {$rcfile_fullname} start");
94
		return;
95
	}
96
	if($config['installedpackages']['service']) {
97
		foreach($config['installedpackages']['service'] as $service) {
98
			if(strtolower($service['name']) == strtolower($name)) {
99
				if($service['rcfile']) {
100
					$prefix = RCFILEPREFIX;
101
					if (!empty($service['prefix'])) {
102
						$prefix =& $service['prefix'];
103
					}
104
					if(file_exists("{$prefix}{$service['rcfile']}")) {
105
						mwexec_bg("{$prefix}{$service['rcfile']} start");
106
					}
107
				}
108
				if (!empty($service['startcmd']))
109
					eval($service['startcmd']);
110
				break;
111
			}
112
		}
113
	}
114
}
115

    
116
function stop_service($name) {
117
	global $config;
118

    
119
	if (empty($name))
120
		return;
121

    
122
	if ($config['installedpackages']['service']) {
123
		foreach($config['installedpackages']['service'] as $service) {
124
			if(strtolower($service['name']) == strtolower($name)) {
125
				if($service['rcfile']) {
126
					$prefix = RCFILEPREFIX;
127
					if(!empty($service['prefix'])) {
128
						$prefix =& $service['prefix'];
129
					}
130
					if(file_exists("{$prefix}{$service['rcfile']}")) {
131
						mwexec("{$prefix}{$service['rcfile']} stop");
132
					}
133
					return;
134
				}
135
				if (!empty($service['stopcmd']))
136
					eval($service['stopcmd']);
137

    
138
				if(!($service['rcfile'] or $service['stopcmd'])) {
139
					if(is_process_running("{$service['executable']}"))
140
						mwexec("/usr/bin/killall {$service['executable']}");
141
					return;
142
				}
143
				break;
144
			}
145
		}
146
	}
147
	/* finally if we get here lets simply kill the service name */
148
	if(is_process_running("{$name}"))
149
		mwexec("/usr/bin/killall {$name}");
150
}
151

    
152
function restart_service($name) {
153
	global $config;
154

    
155
	if (empty($name))
156
		return;
157

    
158
	stop_service($name);
159
	start_service($name);
160

    
161
	if($config['installedpackages']['service']) {
162
		foreach($config['installedpackages']['service'] as $service) {
163
			if(strtolower($service['name']) == strtolower($name)) {
164
				if($service['restartcmd']) {
165
					eval($service['restartcmd']);
166
				}
167
				break;
168
			}
169
		}
170
	}
171
}
172

    
173
function is_pid_running($pidfile) {
174
	if (!file_exists($pidfile))
175
		return false;
176

    
177
	return (isvalidpid($pidfile));
178
}
179

    
180
function is_dhcp_running($interface) {
181
	$status = find_dhclient_process($interface);
182
	if($status <> "")
183
		return true;
184
	return false;
185
}
186

    
187
function restart_service_if_running($service) {
188
	global $config;
189
	if(is_service_running($service))
190
		restart_service($service);
191
	return;
192
}
193

    
194
function is_service_running($service, $ps = "") {
195
	global $config;
196

    
197
	if(is_array($config['installedpackages']['service'])) {
198
		foreach($config['installedpackages']['service'] as $aservice) {
199
			if(strtolower($service) == strtolower($aservice['name'])) {
200
				if ($aservice['custom_php_service_status_command'] <> "") {
201
					eval("\$rc={$aservice['custom_php_service_status_command']};");
202
					return $rc;
203
				}
204
				if(empty($aservice['executable']))
205
					return false;
206
				if (is_process_running($aservice['executable']))
207
					return true;
208

    
209
				return false;
210
			}
211
		}
212
	}
213

    
214
	if (is_process_running($service))
215
		return true;
216

    
217
	return false;
218
}
219

    
220
function get_services() {
221
	global $config;
222
	if (is_array($config['installedpackages']['service']))
223
		$services = $config['installedpackages']['service'];
224
	else
225
		$services = array();
226

    
227
	/*    Add services that are in the base.
228
	 *
229
	 */
230
	if(is_radvd_enabled()) {
231
		$pconfig = array();
232
		$pconfig['name'] = "radvd";
233
		$pconfig['description'] = gettext("Router Advertisement Daemon");
234
		$services[] = $pconfig;
235
	}
236

    
237
	if(isset($config['dnsmasq']['enable'])) {
238
		$pconfig = array();
239
		$pconfig['name'] = "dnsmasq";
240
		$pconfig['description'] = gettext("DNS Forwarder");
241
		$services[] = $pconfig;
242
	}
243

    
244
	$pconfig = array();
245
	$pconfig['name'] = "ntpd";
246
	$pconfig['description'] = gettext("NTP clock sync");
247
	$services[] = $pconfig;
248

    
249
	if (is_array($config['captiveportal'])) {
250
		foreach ($config['captiveportal'] as $zone => $setting) {
251
			if (isset($setting['enable'])) {
252
				$pconfig = array();
253
				$pconfig['name'] = "captiveportal";
254
				$pconfig['zone'] = $zone;
255
				$pconfig['description'] = gettext("Captive Portal") . ": ".htmlspecialchars($setting['zone']);
256
				$services[] = $pconfig;
257
			}
258
		}
259
	}
260

    
261
	$iflist = array();
262
	$ifdescrs = get_configured_interface_list();
263
	foreach ($ifdescrs as $if) {
264
		$oc = $config['interfaces'][$if];
265
		if ($oc['if'] && (!link_interface_to_bridge($if)))
266
			$iflist[$if] = $if;
267
	}
268
	$show_dhcprelay = false;
269
	foreach($iflist as $if) {
270
		if(isset($config['dhcrelay'][$if]['enable']))
271
			$show_dhcprelay = true;
272
	}
273

    
274
	if($show_dhcprelay == true) {
275
		$pconfig = array();
276
		$pconfig['name'] = "dhcrelay";
277
		$pconfig['description'] = gettext("DHCP Relay");
278
		$services[] = $pconfig;
279
	}
280

    
281
	if(is_dhcp_server_enabled()) {
282
		$pconfig = array();
283
		$pconfig['name'] = "dhcpd";
284
		$pconfig['description'] = gettext("DHCP Service");
285
		$services[] = $pconfig;
286
	}
287

    
288
	if(isset($config['snmpd']['enable'])) {
289
		$pconfig = array();
290
		$pconfig['name'] = "bsnmpd";
291
		$pconfig['description'] = gettext("SNMP Service");
292
		$services[] = $pconfig;
293
	}
294

    
295
	if (is_array($config['igmpproxy']['igmpentry']) && (count($config['igmpproxy']['igmpentry']) > 0)) {
296
		$pconfig = array();
297
		$pconfig['name'] = "igmpproxy";
298
		$pconfig['description'] = gettext("IGMP proxy");
299
		$services[] = $pconfig;
300
	}
301

    
302
	if (isset($config['installedpackages']['miniupnpd']) && $config['installedpackages']['miniupnpd']['config'][0]['enable']) {
303
		$pconfig = array();
304
		$pconfig['name'] = "miniupnpd";
305
		$pconfig['description'] = gettext("UPnP Service");
306
		$services[] = $pconfig;
307
	}
308

    
309
	if (isset($config['installedpackages']['routed']) && $config['installedpackages']['routed']['config'][0]['enable']) {
310
		$pconfig = array();
311
		$pconfig['name'] = "routed";
312
		$pconfig['description'] = gettext("RIP Daemon");
313
		$services[] = $pconfig;
314
	}
315

    
316
	if (isset($config['ipsec']['enable'])) {
317
		$pconfig = array();
318
		$pconfig['name'] = "racoon";
319
		$pconfig['description'] = gettext("IPsec VPN");
320
		$services[] = $pconfig;
321
	}
322

    
323
	foreach (array('server', 'client') as $mode) {
324
		if (is_array($config['openvpn']["openvpn-{$mode}"])) {
325
			foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
326
				if (!isset($setting['disable'])) {
327
					$pconfig = array();
328
					$pconfig['name'] = "openvpn";
329
					$pconfig['mode'] = $mode;
330
					$pconfig['id'] = $id;
331
					$pconfig['vpnid'] = $setting['vpnid'];
332
					$pconfig['description'] = gettext("OpenVPN") . " ".$mode.": ".htmlspecialchars($setting['description']);
333
					$services[] = $pconfig;
334
				}
335
			}
336
		}
337
	}
338

    
339
	if (count($config['load_balancer']['virtual_server']) && count($config['load_balancer']['lbpool'])) {
340
		$pconfig = array();
341
		$pconfig['name'] = "relayd";
342
		$pconfig['description'] = gettext("Server load balancing daemon");
343
		$services[] = $pconfig;
344
	}
345
	return $services;
346
}
347

    
348
function find_service_by_name($name) {
349
	$services = get_services();
350
	foreach ($services as $service)
351
		if ($service["name"] == $name)
352
			return $service;
353
	return array();
354
}
355

    
356
function find_service_by_openvpn_vpnid($vpnid) {
357
	$services = get_services();
358
	foreach ($services as $service)
359
		if (($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid))
360
			return $service;
361
	return array();
362
}
363

    
364
function find_service_by_cp_zone($zone) {
365
	$services = get_services();
366
	foreach ($services as $service)
367
		if (($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone))
368
			return $service;
369
	return array();
370
}
371

    
372
function service_name_compare($a, $b) {
373
	if (strtolower($a['name']) == strtolower($b['name']))
374
		return 0;
375
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
376
}
377

    
378
function get_pkg_descr($package_name) {
379
	global $config;
380
	if (is_array($config['installedpackages']['package'])) {
381
		foreach($config['installedpackages']['package'] as $pkg) {
382
			if($pkg['name'] == $package_name)
383
				return $pkg['descr'];
384
		}
385
	}
386
	return gettext("Not available.");
387
}
388

    
389
function get_service_status($service) {
390
	global $g;
391
	switch ($service['name']) {
392
		case "openvpn":
393
			$running = is_pid_running("{$g['varrun_path']}/openvpn_{$service['mode']}{$service['vpnid']}.pid");
394
			break;
395
		case "captiveportal":
396
			$running = is_pid_running("{$g['varrun_path']}/lighty-{$service['zone']}-CaptivePortal.pid");
397
			if (isset($config['captiveportal'][$service['zone']]['httpslogin']))
398
				$running = $running && is_pid_running("{$g['varrun_path']}/lighty-{$service['zone']}-CaptivePortal-SSL.pid");
399
			break;
400
		default:
401
			$running = is_service_running($service['name']);
402
	}
403
	return $running;
404
}
405

    
406
function get_service_status_icon($service, $withtext = true, $smallicon = false) {
407
	global $g;
408
	$output = "";
409
	if(get_service_status($service)) {
410
		$statustext = gettext("Running");
411
		$output .= '<td class="listr"><center>' . "\n";
412
		$output .= "<img style=\"vertical-align:middle\" title=\"" . sprintf(gettext("%s Service is"),$service["name"]) . " {$statustext}\" src=\"/themes/" . $g["theme"] . "/images/icons/";
413
		$output .= ($smallicon) ? "icon_pass.gif" : "icon_service_running.gif";
414
		$output .= "\">";
415
		if ($withtext)
416
			$output .= "&nbsp;&nbsp;" . $statustext;
417
		$output .= "</td>\n";
418
	} else {
419
		$statustext = gettext("Stopped");
420
		$output .= '<td class="listbg"><center>' . "\n";
421
		$output .= "<img style=\"vertical-align:middle\" title=\"" . sprintf(gettext("%s Service is"),$service["name"]) . " {$statustext}\" src=\"/themes/" . $g["theme"] . "/images/icons/";
422
		$output .= ($smallicon) ? "icon_block.gif" : "icon_service_stopped.gif";
423
		$output .= "\">";
424
		if ($withtext)
425
			$output .= "&nbsp;&nbsp;" . "<font color=\"white\">{$statustext}</font>";
426
		$output .= "</td>\n";
427
	}
428
	return $output;
429
}
430

    
431
function get_service_control_links($service, $addname = false) {
432
	global $g;
433
	$output = "";
434
	$stitle = ($addname) ? $service['name'] . " " : "";
435
	if(get_service_status($service)) {
436
		switch ($service['name']) {
437
			case "openvpn":
438
				$output .= "<a href='status_services.php?mode=restartservice&service={$service['name']}&vpnmode={$service['mode']}&id={$service['vpnid']}'>";
439
				break;
440
			case "captiveportal":
441
				$output .= "<a href='status_services.php?mode=restartservice&service={$service['name']}&zone={$service['zone']}'>";
442
				break;
443
			default:
444
				$output .= "<a href='status_services.php?mode=restartservice&service={$service['name']}'>";
445
		}
446
		$output .= "<img style=\"vertical-align:middle\" title='" . sprintf(gettext("Restart %sService"),$stitle) . "' border='0' src='./themes/".$g['theme']."/images/icons/icon_service_restart.gif'></a>\n";
447
		switch ($service['name']) {
448
			case "openvpn":
449
				$output .= "<a href='status_services.php?mode=stopservice&service={$service['name']}&vpnmode={$service['mode']}&id={$service['vpnid']}'>";
450
				break;
451
			case "captiveportal":
452
				$output .= "<a href='status_services.php?mode=stopservice&service={$service['name']}&zone={$service['zone']}'>";
453
				break;
454
			default:
455
				$output .= "<a href='status_services.php?mode=stopservice&service={$service['name']}'>";
456
		}
457
		$output .= "<img style=\"vertical-align:middle\" title='" . sprintf(gettext("Stop %sService"),$stitle) . "' border='0' src='./themes/".$g['theme']."/images/icons/icon_service_stop.gif'>";
458
		$output .= "</a>";
459
	} else {
460
		switch ($service['name']) {
461
			case "openvpn":
462
				$output .= "<a href='status_services.php?mode=startservice&service={$service['name']}&vpnmode={$service['mode']}&id={$service['vpnid']}'>";
463
				break;
464
			case "captiveportal":
465
				$output .= "<a href='status_services.php?mode=startservice&service={$service['name']}&zone={$service['zone']}'>";
466
				break;
467
			default:
468
				$output .= "<a href='status_services.php?mode=startservice&service={$service['name']}'>";
469
		}
470
		$output .= "<img style=\"vertical-align:middle\" title='" . sprintf(gettext("Start %sService"),$stitle) . "' border='0' src='./themes/".$g['theme']."/images/icons/icon_service_start.gif'></a>\n";
471
	}
472
	return $output;
473
}
474
?>
(48-48/67)