Project

General

Profile

Download (25.6 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * service-utils.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2005-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2023 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2005-2006 Colin Smith (ethethlay@gmail.com)
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25
require_once("captiveportal.inc");
26
require_once("globals.inc");
27
require_once("gwlb.inc");
28
require_once("ipsec.inc");
29
require_once("openvpn.inc");
30
require_once("system.inc");
31
require_once("util.inc");
32
require_once("vpn.inc");
33

    
34
define("RCFILEPREFIX", "/usr/local/etc/rc.d/");
35
function write_rcfile($params) {
36
	global $g;
37

    
38
	safe_mkdir(RCFILEPREFIX);
39
	$rcfile_fullname = RCFILEPREFIX . $params['file'];
40
	if (!file_exists($rcfile_fullname) && !is_link($rcfile_fullname) && !touch($rcfile_fullname)) {
41
		return false;
42
	}
43

    
44
	if (!is_writable($rcfile_fullname) || empty($params['start'])) {
45
		return false;
46
	}
47

    
48
	$towrite = "#!/bin/sh\n";
49
	$towrite .= "# This file was automatically generated\n# by the {$g['product_label']} service handler.\n\n";
50

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

    
77
	/* begin rcfile logic */
78
	$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_restart\n\t\t;;\nesac\n\n";
79

    
80
	@file_put_contents($rcfile_fullname, $towrite);
81
	unset($towrite);
82
	@chmod("{$rcfile_fullname}", 0755);
83

    
84
	return;
85
}
86

    
87
function start_service($name, $after_sync = false) {
88
	if (empty($name)) {
89
		return;
90
	}
91

    
92
	foreach (config_get_path('installedpackages/service', []) as $service) {
93
		if (empty($service)) {
94
			continue;
95
		}
96
		if (isset($service['name']) && (strtolower($service['name']) == strtolower($name))) {
97
			/* Avoid starting twice if this is called just after a
98
			 * package sync which starts the service itself. */
99
			if ($after_sync && isset($service['starts_on_sync'])) {
100
				break;
101
			}
102
			if ($service['rcfile']) {
103
				$prefix = RCFILEPREFIX;
104
				if (!empty($service['prefix'])) {
105
					$prefix = &$service['prefix'];
106
				}
107
				if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
108
					mwexec_bg("{$prefix}{$service['rcfile']} start");
109
				}
110
			}
111
			if (!empty($service['startcmd'])) {
112
				eval($service['startcmd']);
113
			}
114
			break;
115
		}
116
	}
117
}
118

    
119
function stop_service($name) {
120
	if (empty($name)) {
121
		return;
122
	}
123

    
124
	foreach (config_get_path('installedpackages/service', []) as $service) {
125
		if (empty($service)) {
126
			continue;
127
		}
128
		if (strtolower($service['name']) == strtolower($name)) {
129
			if ($service['rcfile']) {
130
				$prefix = RCFILEPREFIX;
131
				if (!empty($service['prefix'])) {
132
					$prefix = &$service['prefix'];
133
				}
134
				if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
135
					mwexec("{$prefix}{$service['rcfile']} stop");
136
				}
137
				return;
138
			}
139
			if (!empty($service['stopcmd'])) {
140
				eval($service['stopcmd']);
141
			} elseif (!empty($service['executable'])) {
142
				mwexec("/usr/bin/killall " . escapeshellarg($service['executable']));
143
			}
144

    
145
			break;
146
		}
147
	}
148
}
149

    
150
function restart_service($name) {
151
	if (empty($name)) {
152
		return;
153
	}
154

    
155
	if (is_service_running($name)) {
156
		stop_service($name);
157
	}
158
	start_service($name);
159

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

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

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

    
181
function is_dhcp_running($interface) {
182
	$status = find_dhclient_process($interface);
183
	if ($status != 0) {
184
		return true;
185
	}
186
	return false;
187
}
188

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

    
196
function is_service_enabled($service_name) {
197
	switch ($service_name) {
198
		case 'bsnmpd':
199
			return config_path_enabled('snmpd');
200
			break;
201
		case 'dhcrelay':
202
			return config_path_enabled('dhcrelay');
203
			break;
204
		case 'dhcrelay6':
205
			return config_path_enabled('dhcrelay6');
206
			break;
207
		case 'dhcpd':
208
			return is_dhcp_server_enabled();
209
			break;
210
		case 'dnsmasq':
211
			return config_path_enabled('dnsmasq');
212
			break;
213
		case 'dpinger':
214
			/* TODO: Should loop through gateways and check to make
215
			 *       sure they don't all have monitoring disabled. */
216
			$gateways_arr = return_gateways_array();
217
			return is_array($gateways_arr) && !empty($gateways_arr);
218
			break;
219
		case 'igmpproxy':
220
			return (config_path_enabled('igmpproxy') && !empty(config_get_path('igmpproxy/igmpentry', [])));
221
			break;
222
		case 'ipsec':
223
			return ipsec_enabled();
224
			break;
225
		case 'miniupnpd':
226
			return (config_get_path('installedpackages/miniupnpd/config/0/enable') == 'on');
227
			break;
228
		case 'ntpd':
229
			return (config_get_path('ntpd/enable') != 'disabled');
230
			break;
231
		case 'pcscd':
232
			return config_path_enabled('ipsec', 'pkcs11support');
233
			break;
234
		case 'radvd':
235
			return is_radvd_enabled();
236
			break;
237
		case 'sshd':
238
			return config_path_enabled('system/ssh');
239
			break;
240
		case 'syslogd':
241
			$local_enabled = !config_path_enabled('syslog', 'disablelocallogging');
242
			$remote_enabled = !empty(system_syslogd_get_remote_servers(config_get_path('syslog', [])));
243
			return ($local_enabled || $remote_enabled);
244
			break;
245
		case 'unbound':
246
			return config_path_enabled('unbound');
247
			break;
248
		default:
249
			/* Do nothing since we can't determine for certain. */
250
	}
251
	/* TODO: The service name isn't likely to match the config tag,
252
	 *       needs better logic or pkg plugin to probe */
253
	$pkg_enabled = config_get_path("installedpackages/{$service_name}/config/0/enable", null);
254
	if (($pkg_enabled !== null) &&
255
	    (empty($pkg_enabled) || ($pkg_enabled === 'off'))) {
256
		/* Only return false for cases where the config area exists and
257
		 * appears to be disabled. */
258
		return false;
259
	}
260

    
261
	/* Unknown service, for compatibility reasons, return true. */
262
	return true;
263
}
264

    
265
function is_service_running($service, $ps = "") {
266
	foreach (config_get_path('installedpackages/service', []) as $aservice) {
267
		if (empty($aservice)) {
268
			continue;
269
		}
270
		if (isset($aservice['name']) && (strtolower($service) == strtolower($aservice['name']))) {
271
			if ($aservice['custom_php_service_status_command'] <> "") {
272
				eval("\$rc={$aservice['custom_php_service_status_command']};");
273
				return $rc;
274
			}
275
			if (empty($aservice['executable'])) {
276
				return false;
277
			}
278
			if (is_process_running($aservice['executable'])) {
279
				return true;
280
			}
281
			return false;
282
		}
283
	}
284
	if (is_process_running($service)) {
285
		return true;
286
	}
287
	return false;
288
}
289

    
290
function get_services() {
291
	$services = config_get_path('installedpackages/service', []);
292

    
293
	/* Clean up any empty services */
294
	foreach ($services as $k => &$s) {
295
		if (empty($s)) {
296
			config_del_path('installedpackages/services/' . $k);
297
			unset($s);
298
		}
299
	}
300

    
301
	/*
302
	 * Add services that are in the base.
303
	 */
304
	if (is_radvd_enabled()) {
305
		$pconfig = array();
306
		$pconfig['name'] = "radvd";
307
		$pconfig['description'] = gettext("Router Advertisement Daemon");
308
		$pconfig['enabled'] = true;
309
		$pconfig['status'] = get_service_status($pconfig);
310
		$services[] = $pconfig;
311
	}
312

    
313
	if (config_path_enabled('dnsmasq')) {
314
		$pconfig = array();
315
		$pconfig['name'] = "dnsmasq";
316
		$pconfig['description'] = gettext("DNS Forwarder");
317
		$pconfig['enabled'] = true;
318
		$pconfig['status'] = get_service_status($pconfig);
319
		$services[] = $pconfig;
320
	}
321

    
322
	if (config_path_enabled('unbound')) {
323
		$pconfig = array();
324
		$pconfig['name'] = "unbound";
325
		$pconfig['description'] = gettext("DNS Resolver");
326
		$pconfig['enabled'] = true;
327
		$pconfig['status'] = get_service_status($pconfig);
328
		$services[] = $pconfig;
329
	}
330

    
331
	if (config_path_enabled('ipsec', 'pkcs11support')) {
332
		$pconfig = array();
333
		$pconfig['name'] = "pcscd";
334
		$pconfig['description'] = gettext("PC/SC Smart Card Daemon");
335
		$pconfig['enabled'] = true;
336
		$pconfig['status'] = get_service_status($pconfig);
337
		$services[] = $pconfig;
338
	}
339

    
340
	if (config_get_path('ntpd/enable') != 'disabled') {
341
		$pconfig = array();
342
		$pconfig['name'] = "ntpd";
343
		$pconfig['description'] = gettext("NTP clock sync");
344
		$pconfig['enabled'] = true;
345
		$pconfig['status'] = get_service_status($pconfig);
346
		$services[] = $pconfig;
347
	}
348

    
349
	$pconfig = array();
350
	$pconfig['name'] = "syslogd";
351
	$pconfig['description'] = gettext("System Logger Daemon");
352
	$pconfig['enabled'] = is_service_enabled("syslogd");
353
	$pconfig['status'] = get_service_status($pconfig);
354
	$services[] = $pconfig;
355

    
356
	foreach (config_get_path('captiveportal', []) as $zone => $setting) {
357
		if (empty($setting)) {
358
			continue;
359
		}
360
		if (isset($setting['enable'])) {
361
			$pconfig = array();
362
			$pconfig['name'] = "captiveportal";
363
			$pconfig['zone'] = $zone;
364
			$pconfig['enabled'] = true;
365
			$pconfig['description'] = gettext("Captive Portal") . ": " . htmlspecialchars($setting['zone']);
366
			$services[] = $pconfig;
367
		}
368
	}
369

    
370
	$iflist = array();
371
	$ifdescrs = get_configured_interface_list();
372
	foreach ($ifdescrs as $if) {
373
		if (config_get_path("interfaces/{$if}/if") &&
374
		    !link_interface_to_bridge($if)) {
375
			$iflist[$if] = $if;
376
		}
377
	}
378

    
379
	if (config_path_enabled('dhcrelay')) {
380
		$pconfig = array();
381
		$pconfig['name'] = "dhcrelay";
382
		$pconfig['description'] = gettext('ISC DHCP Relay Agent');
383
		$pconfig['enabled'] = true;
384
		$pconfig['status'] = get_service_status($pconfig);
385
		$services[] = $pconfig;
386
	}
387

    
388
	if (config_path_enabled('dhcrelay6')) {
389
		$pconfig = array();
390
		$pconfig['name'] = "dhcrelay6";
391
		$pconfig['description'] = gettext('ISC DHCPv6 Relay Agent');
392
		$pconfig['enabled'] = true;
393
		$pconfig['status'] = get_service_status($pconfig);
394
		$services[] = $pconfig;
395
	}
396

    
397
	if (is_dhcp_server_enabled()) {
398
		$pconfig = array();
399
		$pconfig['name'] = "dhcpd";
400
		$pconfig['description'] = gettext('ISC DHCP Server');
401
		$pconfig['enabled'] = true;
402
		$pconfig['status'] = get_service_status($pconfig);
403
		$services[] = $pconfig;
404
	}
405

    
406
	$gateways_arr = return_gateways_array();
407
	if (is_array($gateways_arr) && !empty($gateways_arr)) {
408
		$pconfig = array();
409
		$pconfig['name'] = "dpinger";
410
		$pconfig['description'] = gettext("Gateway Monitoring Daemon");
411
		$pconfig['enabled'] = is_service_enabled("dpinger");
412
		$pconfig['status'] = get_service_status($pconfig);
413
		$services[] = $pconfig;
414
	}
415

    
416
	if (config_path_enabled('snmpd')) {
417
		$pconfig = array();
418
		$pconfig['name'] = "bsnmpd";
419
		$pconfig['description'] = gettext("SNMP Service");
420
		$pconfig['enabled'] = true;
421
		$pconfig['status'] = get_service_status($pconfig);
422
		$services[] = $pconfig;
423
	}
424

    
425
	if (!empty(config_get_path('igmpproxy/igmpentry', []))) {
426
		$pconfig = array();
427
		$pconfig['name'] = "igmpproxy";
428
		$pconfig['description'] = gettext("IGMP proxy");
429
		$pconfig['enabled'] = is_service_enabled("igmpproxy");
430
		$pconfig['status'] = get_service_status($pconfig);
431
		$services[] = $pconfig;
432
	}
433

    
434
	if (config_get_path('installedpackages/miniupnpd/config/0/enable') == 'on') {
435
		$pconfig = array();
436
		$pconfig['name'] = "miniupnpd";
437
		$pconfig['description'] = gettext("UPnP Service");
438
		$pconfig['enabled'] = true;
439
		$pconfig['status'] = get_service_status($pconfig);
440
		$services[] = $pconfig;
441
	}
442

    
443
	if (ipsec_enabled()) {
444
		$pconfig = array();
445
		$pconfig['name'] = "ipsec";
446
		$pconfig['description'] = gettext("IPsec VPN");
447
		$pconfig['enabled'] = true;
448
		$pconfig['status'] = get_service_status($pconfig);
449
		$services[] = $pconfig;
450
	}
451

    
452
	if (config_path_enabled('system/ssh')) {
453
		$pconfig = array();
454
		$pconfig['name'] = "sshd";
455
		$pconfig['description'] = gettext("Secure Shell Daemon");
456
		$pconfig['enabled'] = true;
457
		$pconfig['status'] = get_service_status($pconfig);
458
		$services[] = $pconfig;
459
	}
460

    
461
	foreach (array('server', 'client') as $mode) {
462
		foreach (config_get_path("openvpn/openvpn-{$mode}", []) as $id => $setting) {
463
			if (!isset($setting['disable'])) {
464
				$pconfig = array();
465
				$pconfig['name'] = "openvpn";
466
				$pconfig['mode'] = $mode;
467
				$pconfig['id'] = $id;
468
				$pconfig['vpnid'] = $setting['vpnid'];
469
				$pconfig['description'] = gettext("OpenVPN") . " " . $mode . ": " . htmlspecialchars($setting['description']);
470
				$pconfig['enabled'] = true;
471
				$pconfig['status'] = get_service_status($pconfig);
472
				$services[] = $pconfig;
473
			}
474
		}
475
	}
476

    
477
	return $services;
478
}
479

    
480
function find_service_by_name($name) {
481
	$services = get_services();
482

    
483
	foreach ($services as $service) {
484
		if (isset($service["name"]) && ($service["name"] == $name)) {
485
			return $service;
486
		}
487
	}
488
	return array();
489
}
490

    
491
function find_service_by_openvpn_vpnid($vpnid) {
492
	$services = get_services();
493
	foreach ($services as $service) {
494
		if (isset($service["name"]) && ($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) {
495
			return $service;
496
		}
497
	}
498
	return array();
499
}
500

    
501
function find_service_by_cp_zone($zone) {
502
	$services = get_services();
503
	foreach ($services as $service) {
504
		if (isset($service["name"]) && ($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) {
505
			return $service;
506
		}
507
	}
508
	return array();
509
}
510

    
511
function service_description_compare($a, $b) {
512
	if (strtolower($a['description']) == strtolower($b['description'])) {
513
		return 0;
514
	}
515
	return (strtolower($a['description']) < strtolower($b['description'])) ? -1 : 1;
516
}
517

    
518
function service_name_compare($a, $b) {
519
	if (!isset($a['name']) || !isset($b['name'])) {
520
		return -1;
521
	}
522
	/* If the names are equal, fall back to sorting by description */
523
	if (strtolower($a['name']) == strtolower($b['name'])) {
524
		return service_description_compare($a, $b);
525
	}
526
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
527
}
528

    
529
function service_dispname_compare($a, $b) {
530
	/* If two items have an instance suffix, perform an integer comparison to avoid awkward sorting */
531
	if ((strpos($a['dispname'], '_') > 0) && (strpos($b['dispname'], '_') > 0)) {
532
		list($adn1, $adn2) = explode('_', $a['dispname'], 2);
533
		list($bdn1, $bdn2) = explode('_', $b['dispname'], 2);
534
		if (($adn1 == $bdn1) && is_numeric($adn2) && is_numeric($bdn2)) {
535
			if ($adn2 == $bdn2) {
536
				return 0;
537
			}
538
			return ((int)$adn2 < (int)$bdn2) ? -1 : 1;
539
		}
540
	}
541
	/* If the display names are equal, compare the internal name */
542
	if (strtolower($a['dispname']) == strtolower($b['dispname'])) {
543
		return service_name_compare($a, $b);
544
	}
545
	return (strtolower($a['dispname']) < strtolower($b['dispname'])) ? -1 : 1;
546
}
547

    
548
function get_pkg_descr($package_name) {
549
	foreach (config_get_path('installedpackages/package', []) as $pkg) {
550
		if ($pkg['name'] == $package_name) {
551
			return $pkg['descr'];
552
		}
553
	}
554
	return gettext("Not available.");
555
}
556

    
557
function get_service_status($service) {
558
	global $g;
559
	switch ($service['name']) {
560
		case "openvpn":
561
			$running = is_pid_running("{$g['varrun_path']}/openvpn_{$service['mode']}{$service['vpnid']}.pid");
562
			break;
563
		case "captiveportal":
564
			$running = is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal.pid");
565
			if (config_path_enabled("captiveportal/{$service['zone']}", 'httpslogin')) {
566
				$running = $running && is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal-SSL.pid");
567
			}
568
			break;
569
		case "vhosts-http":
570
			$running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid");
571
			break;
572
		case "dhcrelay6":
573
			$running = is_pid_running("{$g['varrun_path']}/dhcrelay6.pid");
574
			break;
575
		case 'ipsec':
576
			$running = (is_pid_running("{$g['varrun_path']}/charon.pid") || is_process_running('charon'));
577
			break;
578
		default:
579
			$running = is_service_running($service['name']);
580
	}
581
	return $running;
582
}
583

    
584
function get_service_status_icon($service, $withtext = true, $smallicon = false, $withthumbs = false, $title = "service_state") {
585
	$output = "";
586

    
587
	if (get_service_status($service)) {
588
		$statustext = gettext("Running");
589
		$text_class = "text-success";
590
		$fa_class = "fa fa-check-circle";
591
		$fa_class_thumbs = "fa fa-thumbs-o-up";
592
		$Thumbs_UpDown = "Thumbs up";
593
	} else {
594
		if (is_service_enabled($service['name'])) {
595
			$statustext = gettext("Stopped");
596
			$text_class = "text-danger";
597
			$fa_class = "fa fa-times-circle";
598
		} else {
599
			$statustext = gettext("Disabled");
600
			$text_class = "text-warning";
601
			$fa_class = "fa fa-ban";
602
		}
603
		$fa_class_thumbs = "fa fa-thumbs-o-down";
604
		$Thumbs_UpDown = "Thumbs down";
605
	}
606
	$fa_size = ($smallicon) ? "fa-1x" : "fa-lg";
607

    
608
	if ($title == "state") {
609
		$title = $statustext;
610
	} elseif ($title == "service_state") {
611
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["name"], $statustext);
612
	} elseif ($title == "description_state") {
613
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["description"], $statustext);
614
	} elseif ($title == "description_service_state") {
615
		$title = sprintf(gettext('%1$s, %2$s Service is %3$s'), $service["description"], $service["name"], $statustext);
616
	}
617

    
618
	$spacer = ($withthumbs || $withtext) ? " " : "";
619

    
620
	$output = "<i class=\"{$text_class} {$fa_class} {$fa_size}\" title=\"{$title}\"><span style=\"display: none\">{$statustext}</span></i>{$spacer}";
621

    
622
	$spacer = ($withtext) ? " " : "";
623
	if ($withthumbs) {
624
		$output .= "<i class=\"{$text_class} {$fa_class_thumbs} {$fa_size}\" title=\"{$Thumbs_UpDown}\"></i>{$spacer}";
625
	}
626

    
627
	if ($withtext) {
628
		$output .= "<span class=\"" . $text_class . "\">" . $statustext . "</span>";
629
	}
630

    
631
	return $output;
632
}
633

    
634
function get_service_control_links($service, $addname = false) {
635
	global $g;
636
	$output = "";
637
	$stitle = ($addname) ? $service['name'] . " " : "";
638

    
639
	switch ($service['name']) {
640
		case "openvpn":
641
			$link = '<a title="%s" href="#" id="openvpn-%s-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
642
		break;
643
		case "captiveportal":
644
			$link = '<a title="%s" href="#" id="captiveportal-%s-' . $service['zone'] . '">';
645
		break;
646
		default:
647
			$link = '<a title="%s" href="#" id="%s-' . $service['name'] . '">';
648
	}
649

    
650
	if (get_service_status($service)) {
651
		switch ($service['name']) {
652
			case "openvpn":
653
				$output .= '<a href="#" id="openvpn-restartservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
654
				break;
655
			case "captiveportal":
656
				$output .= '<a href="#" id="captiveportal-restartservice-' . $service['zone'] . '">';
657
				break;
658
			default:
659
				$output .= '<a href="#" id="restartservice-' . $service['name'] . '" >';
660
		}
661

    
662
		$output .= "<i class=\"fa fa-repeat\" title=\"" . sprintf(gettext("Restart %sService"), $stitle) . "\"></i></a>\n";
663

    
664
		switch ($service['name']) {
665
			case "openvpn":
666
				$output .= '<a href="#" id="openvpn-stopservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
667
				break;
668
			case "captiveportal":
669
				$output .= '<a href="#" id="captiveportal-stopservice-' . $service['zone'] . '">';
670
				break;
671
			default:
672
				$output .= '<a href="#" id="stopservice-' . $service['name'] . '">';
673
		}
674

    
675
		$output .= "<i class=\"fa fa-stop-circle-o\" title=\"" . sprintf(gettext("Stop %sService"), $stitle) . "\"></i></a>";
676

    
677
	} else {
678
		$service_enabled = is_service_enabled($service['name']);
679

    
680
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
681
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
682
			$output .= '<i class="fa fa-play-circle"></i></a> ';
683
		}
684
	}
685

    
686
	return $output;
687
}
688

    
689
function service_control_start($name, $extras) {
690
	global $g;
691
	switch ($name) {
692
		case 'radvd':
693
			services_radvd_configure();
694
			break;
695
		case 'captiveportal':
696
			$zone = htmlspecialchars($extras['zone']);
697
			captiveportal_init_webgui_zonename($zone);
698
			break;
699
		case 'ntpd':
700
			system_ntp_configure();
701
			break;
702
		case 'dpinger':
703
			setup_gateways_monitor();
704
			break;
705
		case 'bsnmpd':
706
			services_snmpd_configure();
707
			break;
708
		case 'dhcrelay':
709
			services_dhcrelay_configure();
710
			break;
711
		case 'dhcrelay6':
712
			services_dhcrelay6_configure();
713
			break;
714
		case 'dnsmasq':
715
			services_dnsmasq_configure();
716
			break;
717
		case 'unbound':
718
			services_unbound_configure();
719
			break;
720
		case 'dhcpd':
721
			services_dhcpd_configure();
722
			break;
723
		case 'igmpproxy':
724
			services_igmpproxy_configure();
725
			break;
726
		case 'miniupnpd':
727
			upnp_action('start');
728
			break;
729
		case 'ipsec':
730
			ipsec_force_reload();
731
			break;
732
		case 'sshd':
733
			send_event("service restart sshd");
734
			break;
735
		case 'pcscd':
736
			ipsec_force_reload();
737
			break;
738
		case 'openvpn':
739
			$vpnmode = isset($extras['vpnmode']) ? htmlspecialchars($extras['vpnmode']) : htmlspecialchars($extras['mode']);
740
			if (($vpnmode == "server") || ($vpnmode == "client")) {
741
				$id = isset($extras['vpnid']) ? htmlspecialchars($extras['vpnid']) : htmlspecialchars($extras['id']);
742
				$configfile = "{$g['openvpn_base']}/{$vpnmode}{$id}/config.ovpn";
743
				if (file_exists($configfile)) {
744
					openvpn_restart_by_vpnid($vpnmode, $id);
745
				}
746
			}
747
			break;
748
		case 'syslogd':
749
			system_syslogd_start();
750
			break;
751
		default:
752
			start_service($name);
753
			break;
754
	}
755
	return sprintf(gettext("%s has been started."), htmlspecialchars($name));
756
}
757
function service_control_stop($name, $extras) {
758
	global $g;
759
	switch ($name) {
760
		case 'radvd':
761
			killbypid("{$g['varrun_path']}/radvd.pid");
762
			break;
763
		case 'captiveportal':
764
			$zone = htmlspecialchars($extras['zone']);
765
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
766
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
767
			break;
768
		case 'ntpd':
769
			killbyname("ntpd");
770
			break;
771
		case 'openntpd':
772
			killbyname("openntpd");
773
			break;
774
		case 'dpinger':
775
			stop_dpinger();
776
			break;
777
		case 'bsnmpd':
778
			killbypid("{$g['varrun_path']}/snmpd.pid");
779
			break;
780
		case 'choparp':
781
			killbyname("choparp");
782
			break;
783
		case 'dhcpd':
784
			killbyname("dhcpd");
785
			break;
786
		case 'dhcrelay':
787
			killbypid("{$g['varrun_path']}/dhcrelay.pid");
788
			break;
789
		case 'dhcrelay6':
790
			killbypid("{$g['varrun_path']}/dhcrelay6.pid");
791
			break;
792
		case 'dnsmasq':
793
			killbypid("{$g['varrun_path']}/dnsmasq.pid");
794
			break;
795
		case 'unbound':
796
			killbypid("{$g['varrun_path']}/unbound.pid");
797
			break;
798
		case 'igmpproxy':
799
			killbyname("igmpproxy");
800
			break;
801
		case 'miniupnpd':
802
			upnp_action('stop');
803
			break;
804
		case 'sshd':
805
			killbyname("sshd");
806
			break;
807
		case 'pcscd':
808
		case 'ipsec':
809
			exec("/usr/local/sbin/strongswanrc stop");
810
			if (isvalidproc("pcscd")) {
811
				killbyname("pcscd");
812
			}
813
			break;
814
		case 'openvpn':
815
			$vpnmode = htmlspecialchars($extras['vpnmode']);
816
			if (($vpnmode == "server") or ($vpnmode == "client")) {
817
				$id = htmlspecialchars($extras['id']);
818
				$pidfile = "{$g['varrun_path']}/openvpn_{$vpnmode}{$id}.pid";
819
				killbypid($pidfile);
820
				openvpn_delete_tmp($vpnmode, $id);
821
			}
822
			break;
823
		case 'syslogd':
824
			if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
825
				sigkillbypid("{$g['varrun_path']}/syslog.pid", "TERM");
826
				usleep(100000);
827
			}
828
			if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
829
				sigkillbypid("{$g['varrun_path']}/syslog.pid", "KILL");
830
				usleep(100000);
831
			}
832
			/* Make sure sshguard stops as well */
833
			sigkillbyname("sshguard", "TERM");
834
			break;
835
		default:
836
			stop_service($name);
837
			break;
838
	}
839
	return sprintf(gettext("%s has been stopped."), htmlspecialchars($name));
840
}
841

    
842
function service_control_restart($name, $extras) {
843
	global $g;
844
	switch ($name) {
845
		case 'radvd':
846
			services_radvd_configure();
847
			break;
848
		case 'captiveportal':
849
			$zone = htmlspecialchars($extras['zone']);
850
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
851
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
852
			/* see https://redmine.pfsense.org/issues/12651 */
853
			sleep(1);
854
			captiveportal_init_webgui_zonename($zone);
855
			break;
856
		case 'ntpd':
857
		case 'openntpd':
858
			system_ntp_configure();
859
			break;
860
		case 'dpinger':
861
			setup_gateways_monitor();
862
			break;
863
		case 'bsnmpd':
864
			services_snmpd_configure();
865
			break;
866
		case 'dhcrelay':
867
			services_dhcrelay_configure();
868
			break;
869
		case 'dhcrelay6':
870
			services_dhcrelay6_configure();
871
			break;
872
		case 'dnsmasq':
873
			services_dnsmasq_configure();
874
			break;
875
		case 'unbound':
876
			services_unbound_configure();
877
			break;
878
		case 'dhcpd':
879
			services_dhcpd_configure();
880
			break;
881
		case 'igmpproxy':
882
			services_igmpproxy_configure();
883
			break;
884
		case 'miniupnpd':
885
			upnp_action('restart');
886
			break;
887
		case 'ipsec':
888
			ipsec_force_reload();
889
			break;
890
		case 'sshd':
891
			send_event("service restart sshd");
892
			break;
893
		case 'pcscd':
894
			exec("/usr/local/sbin/strongswanrc stop");
895
			if (isvalidproc("pcscd")) {
896
				killbyname("pcscd");
897
			}
898
			ipsec_force_reload();
899
			break;
900
		case 'openvpn':
901
			$vpnmode = htmlspecialchars($extras['vpnmode']);
902
			if (($vpnmode == "server") || ($vpnmode == "client")) {
903
				$id = htmlspecialchars($extras['id']);
904
				$configfile = "{$g['openvpn_base']}/{$vpnmode}{$id}/config.ovpn";
905
				if (file_exists($configfile)) {
906
					openvpn_restart_by_vpnid($vpnmode, $id);
907
				}
908
			}
909
			break;
910
		case 'syslogd':
911
			system_syslogd_start();
912
			break;
913
		default:
914
			restart_service($name);
915
			break;
916
	}
917
	return sprintf(gettext("%s has been restarted."), htmlspecialchars($name));
918
}
919

    
920
?>
(46-46/61)