Project

General

Profile

Download (14.6 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
	$running = shell_exec("/bin/pgrep -F {$pidfile} 2>/dev/null");
177

    
178
	return (!empty($running));
179
}
180

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

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

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

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

    
210
				return false;
211
			}
212
		}
213
	}
214

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

    
218
	return false;
219
}
220

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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