Project

General

Profile

Regression #13025 » service-utils-new.inc

revised file - johnny stecchino, 04/03/2022 04:34 PM

 
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-2022 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
require_once("gwlb.inc");
34
require_once("system.inc");
35

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

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

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

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

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

    
79
	/* begin rcfile logic */
80
	$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";
81

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

    
86
	return;
87
}
88

    
89
function start_service($name, $after_sync = false) {
90
	global $config;
91

    
92
	if (empty($name)) {
93
		return;
94
	}
95

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

    
122
function stop_service($name) {
123
	global $config;
124

    
125
	if (empty($name)) {
126
		return;
127
	}
128

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

    
148
				break;
149
			}
150
		}
151
	}
152
}
153

    
154
function restart_service($name) {
155
	global $config;
156

    
157
	if (empty($name)) {
158
		return;
159
	}
160

    
161
	if (is_service_running($name)) {
162
		stop_service($name);
163
	}
164
	start_service($name);
165

    
166
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
167
		foreach ($config['installedpackages']['service'] as $service) {
168
			if (strtolower($service['name']) == strtolower($name)) {
169
				if ($service['restartcmd']) {
170
					eval($service['restartcmd']);
171
				}
172
				break;
173
			}
174
		}
175
	}
176
}
177

    
178
function is_pid_running($pidfile) {
179
	if (!file_exists($pidfile)) {
180
		return false;
181
	}
182

    
183
	return (isvalidpid($pidfile));
184
}
185

    
186
function is_dhcp_running($interface) {
187
	$status = find_dhclient_process($interface);
188
	if ($status != 0) {
189
		return true;
190
	}
191
	return false;
192
}
193

    
194
function restart_service_if_running($service) {
195
	global $config;
196
	if (is_service_running($service)) {
197
		restart_service($service);
198
	}
199
	return;
200
}
201

    
202
function is_service_enabled($service_name) {
203
	global $config;
204
	if ($service_name == "") {
205
		return false;
206
	}
207
	if (is_array($config['installedpackages'])) {
208
		if (isset($config['installedpackages'][$service_name]['config'][0]['enable']) &&
209
		    ((empty($config['installedpackages'][$service_name]['config'][0]['enable'])) ||
210
		    ($config['installedpackages'][$service_name]['config'][0]['enable'] === 'off'))) {
211
			return false;
212
		}
213
	}
214
	return true;
215
}
216

    
217
function is_service_running($service, $ps = "") {
218
	global $config;
219

    
220
	if (is_array($config['installedpackages']['service'])) {
221
		foreach ($config['installedpackages']['service'] as $aservice) {
222
			if (isset($aservice['name']) && (strtolower($service) == strtolower($aservice['name']))) {
223
				if ($aservice['custom_php_service_status_command'] <> "") {
224
					eval("\$rc={$aservice['custom_php_service_status_command']};");
225
					return $rc;
226
				}
227
				if (empty($aservice['executable'])) {
228
					return false;
229
				}
230
				if (is_process_running($aservice['executable'])) {
231
					return true;
232
				}
233

    
234
				return false;
235
			}
236
		}
237
	}
238

    
239
	if (is_process_running($service)) {
240
		return true;
241
	}
242

    
243
	return false;
244
}
245

    
246
function get_services() {
247
	global $config;
248
	if (is_array($config['installedpackages']['service'])) {
249
		$services = $config['installedpackages']['service'];
250
	} else {
251
		$services = array();
252
	}
253

    
254
	/*
255
	 * Add services that are in the base.
256
	 */
257
	if (is_radvd_enabled()) {
258
		$pconfig = array();
259
		$pconfig['name'] = "radvd";
260
		$pconfig['description'] = gettext("Router Advertisement Daemon");
261
		$pconfig['enabled'] = is_service_enabled("radvd");
262
		$pconfig['status'] = get_service_status($pconfig);
263
		$services[] = $pconfig;
264
	}
265

    
266
	if (isset($config['dnsmasq']['enable'])) {
267
		$pconfig = array();
268
		$pconfig['name'] = "dnsmasq";
269
		$pconfig['description'] = gettext("DNS Forwarder");
270
		$pconfig['enabled'] = is_service_enabled("dnsmasq");
271
		$pconfig['status'] = get_service_status($pconfig);
272
		$services[] = $pconfig;
273
	}
274

    
275
	if (isset($config['unbound']['enable'])) {
276
		$pconfig = array();
277
		$pconfig['name'] = "unbound";
278
		$pconfig['description'] = gettext("DNS Resolver");
279
		$pconfig['enabled'] = is_service_enabled("unbound");
280
		$pconfig['status'] = get_service_status($pconfig);
281
		$services[] = $pconfig;
282
	}
283

    
284
	if (watchdogd_enabled()) {
285
		$pconfig = array();
286
		$pconfig['name'] = "watchdogd";
287
		$pconfig['description'] = gettext("Watchdog daemon");
288
		$services[] = $pconfig;
289
	}
290

    
291
	if (isset($config['ipsec']['pkcs11support'])) {
292
		$pconfig = array();
293
		$pconfig['name'] = "pcscd";
294
		$pconfig['description'] = gettext("PC/SC Smart Card Daemon");
295
		$pconfig['enabled'] = is_service_enabled("pcscd");
296
		$pconfig['status'] = get_service_status($pconfig);
297
		$services[] = $pconfig;
298
	}
299

    
300
	init_config_arr(array('ntpd'));
301
	if ($config['ntpd']['enable'] != 'disabled') {
302
		$pconfig = array();
303
		$pconfig['name'] = "ntpd";
304
		$pconfig['description'] = gettext("NTP clock sync");
305
		$pconfig['enabled'] = is_service_enabled("ntpd");
306
		$pconfig['status'] = get_service_status($pconfig);
307
		$services[] = $pconfig;
308
	}
309

    
310
	$pconfig = array();
311
	$pconfig['name'] = "syslogd";
312
	$pconfig['description'] = gettext("System Logger Daemon");
313
	$pconfig['enabled'] = is_service_enabled("syslogd");
314
	$pconfig['status'] = get_service_status($pconfig);
315
	$services[] = $pconfig;
316

    
317
	if (is_array($config['captiveportal'])) {
318
		foreach ($config['captiveportal'] as $zone => $setting) {
319
			if (isset($setting['enable'])) {
320
				$pconfig = array();
321
				$pconfig['name'] = "captiveportal";
322
				$pconfig['zone'] = $zone;
323
				$pconfig['description'] = gettext("Captive Portal") . ": " . htmlspecialchars($setting['zone']);
324
				$services[] = $pconfig;
325
			}
326
		}
327
	}
328

    
329
	$iflist = array();
330
	$ifdescrs = get_configured_interface_list();
331
	foreach ($ifdescrs as $if) {
332
		$oc = $config['interfaces'][$if];
333
		if ($oc['if'] && (!link_interface_to_bridge($if))) {
334
			$iflist[$if] = $if;
335
		}
336
	}
337

    
338
	if (isset($config['dhcrelay']['enable'])) {
339
		$pconfig = array();
340
		$pconfig['name'] = "dhcrelay";
341
		$pconfig['description'] = gettext("DHCP Relay");
342
		$pconfig['enabled'] = is_service_enabled("dhcprelay");
343
		$pconfig['status'] = get_service_status($pconfig);
344
		$services[] = $pconfig;
345
	}
346

    
347
	if (isset($config['dhcrelay6']['enable'])) {
348
		$pconfig = array();
349
		$pconfig['name'] = "dhcrelay6";
350
		$pconfig['description'] = gettext("DHCPv6 Relay");
351
		$pconfig['enabled'] = is_service_enabled("dhcrelay6");
352
		$pconfig['status'] = get_service_status($pconfig);
353
		$services[] = $pconfig;
354
	}
355

    
356
	if (is_dhcp_server_enabled()) {
357
		$pconfig = array();
358
		$pconfig['name'] = "dhcpd";
359
		$pconfig['description'] = gettext("DHCP Service");
360
		$pconfig['enabled'] = is_service_enabled("dhcpd");
361
		$pconfig['status'] = get_service_status($pconfig);
362
		$services[] = $pconfig;
363
	}
364

    
365
	$gateways_arr = return_gateways_array();
366
	if (is_array($gateways_arr)) {
367
		$pconfig = array();
368
		$pconfig['name'] = "dpinger";
369
		$pconfig['description'] = gettext("Gateway Monitoring Daemon");
370
		$pconfig['enabled'] = is_service_enabled("dpinger");
371
		$pconfig['status'] = get_service_status($pconfig);
372
		$services[] = $pconfig;
373
	}
374

    
375
	if (isset($config['snmpd']['enable'])) {
376
		$pconfig = array();
377
		$pconfig['name'] = "bsnmpd";
378
		$pconfig['description'] = gettext("SNMP Service");
379
		$pconfig['enabled'] = is_service_enabled("bsnmpd");
380
		$pconfig['status'] = get_service_status($pconfig);
381
		$services[] = $pconfig;
382
	}
383

    
384
	if (is_array($config['igmpproxy']['igmpentry']) && (count($config['igmpproxy']['igmpentry']) > 0)) {
385
		$pconfig = array();
386
		$pconfig['name'] = "igmpproxy";
387
		$pconfig['description'] = gettext("IGMP proxy");
388
		$pconfig['enabled'] = is_service_enabled("igmpproxy");
389
		$pconfig['status'] = get_service_status($pconfig);
390
		$services[] = $pconfig;
391
	}
392

    
393
	if (isset($config['installedpackages']['miniupnpd']) && $config['installedpackages']['miniupnpd']['config'][0]['enable']) {
394
		$pconfig = array();
395
		$pconfig['name'] = "miniupnpd";
396
		$pconfig['description'] = gettext("UPnP Service");
397
		$pconfig['enabled'] = is_service_enabled("miniupnpd");
398
		$pconfig['status'] = get_service_status($pconfig);
399
		$services[] = $pconfig;
400
	}
401

    
402
	if (ipsec_enabled()) {
403
		$pconfig = array();
404
		$pconfig['name'] = "ipsec";
405
		$pconfig['description'] = gettext("IPsec VPN");
406
		$pconfig['enabled'] = is_service_enabled("ipsec");
407
		$pconfig['status'] = get_service_status($pconfig);
408
		$services[] = $pconfig;
409
	}
410

    
411
	if (isset($config['system']['ssh']['enable'])) {
412
		$pconfig = array();
413
		$pconfig['name'] = "sshd";
414
		$pconfig['description'] = gettext("Secure Shell Daemon");
415
		$pconfig['enabled'] = is_service_enabled("sshd");
416
		$pconfig['status'] = get_service_status($pconfig);
417
		$services[] = $pconfig;
418
	}
419

    
420
	foreach (array('server', 'client') as $mode) {
421
		init_config_arr(array('openvpn'));
422
		if (is_array($config['openvpn']["openvpn-{$mode}"])) {
423
			foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
424
				if (!isset($setting['disable'])) {
425
					$pconfig = array();
426
					$pconfig['name'] = "openvpn";
427
					$pconfig['mode'] = $mode;
428
					$pconfig['id'] = $id;
429
					$pconfig['vpnid'] = $setting['vpnid'];
430
					$pconfig['description'] = gettext("OpenVPN") . " " . $mode . ": " . htmlspecialchars($setting['description']);
431
					$pconfig['enabled'] = is_service_enabled("openvpn");
432
					$pconfig['status'] = get_service_status($pconfig);
433
					$services[] = $pconfig;
434
				}
435
			}
436
		}
437
	}
438

    
439
	return $services;
440
}
441

    
442
function find_service_by_name($name) {
443
	$services = get_services();
444
	
445
	foreach ($services as $service) {
446
		if (isset($service["name"]) && ($service["name"] == $name)) {
447
			return $service;
448
		}
449
	}
450
	return array();
451
}
452

    
453
function find_service_by_openvpn_vpnid($vpnid) {
454
	$services = get_services();
455
	foreach ($services as $service) {
456
		if (isset($service["name"]) && ($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) {
457
			return $service;
458
		}
459
	}
460
	return array();
461
}
462

    
463
function find_service_by_cp_zone($zone) {
464
	$services = get_services();
465
	foreach ($services as $service) {
466
		if (isset($service["name"]) && ($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) {
467
			return $service;
468
		}
469
	}
470
	return array();
471
}
472

    
473
function service_description_compare($a, $b) {
474
	if (strtolower($a['description']) == strtolower($b['description'])) {
475
		return 0;
476
	}
477
	return (strtolower($a['description']) < strtolower($b['description'])) ? -1 : 1;
478
}
479

    
480
function service_name_compare($a, $b) {
481
	if (!isset($a['name']) || !isset($b['name'])) {
482
		return -1;
483
	}
484
	/* If the names are equal, fall back to sorting by description */
485
	if (strtolower($a['name']) == strtolower($b['name'])) {
486
		return service_description_compare($a, $b);
487
	}
488
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
489
}
490

    
491
function service_dispname_compare($a, $b) {
492
	/* If two items have an instance suffix, perform an integer comparison to avoid awkward sorting */
493
	if ((strpos($a['dispname'], '_') > 0) && (strpos($b['dispname'], '_') > 0)) {
494
		list($adn1, $adn2) = explode('_', $a['dispname'], 2);
495
		list($bdn1, $bdn2) = explode('_', $b['dispname'], 2);
496
		if (($adn1 == $bdn1) && is_numeric($adn2) && is_numeric($bdn2)) {
497
			if ($adn2 == $bdn2) {
498
				return 0;
499
			}
500
			return ((int)$adn2 < (int)$bdn2) ? -1 : 1;
501
		}
502
	}
503
	/* If the display names are equal, compare the internal name */
504
	if (strtolower($a['dispname']) == strtolower($b['dispname'])) {
505
		return service_name_compare($a, $b);
506
	}
507
	return (strtolower($a['dispname']) < strtolower($b['dispname'])) ? -1 : 1;
508
}
509

    
510
function get_pkg_descr($package_name) {
511
	global $config;
512
	if (is_array($config['installedpackages']['package'])) {
513
		foreach ($config['installedpackages']['package'] as $pkg) {
514
			if ($pkg['name'] == $package_name) {
515
				return $pkg['descr'];
516
			}
517
		}
518
	}
519
	return gettext("Not available.");
520
}
521

    
522
function get_service_status($service) {
523
	global $g;
524
	switch ($service['name']) {
525
		case "openvpn":
526
			$running = is_pid_running("{$g['varrun_path']}/openvpn_{$service['mode']}{$service['vpnid']}.pid");
527
			break;
528
		case "captiveportal":
529
			$running = is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal.pid");
530
			if (isset($config['captiveportal'][$service['zone']]['httpslogin'])) {
531
				$running = $running && is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal-SSL.pid");
532
			}
533
			break;
534
		case "vhosts-http":
535
			$running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid");
536
			break;
537
		case "dhcrelay6":
538
			$running = is_pid_running("{$g['varrun_path']}/dhcrelay6.pid");
539
			break;
540
		case 'ipsec':
541
			$running = (is_pid_running("{$g['varrun_path']}/charon.pid") || is_process_running('charon'));
542
			break;
543
		case 'watchdogd':
544
			$running = is_pid_running("{$g['varrun_path']}/watchdogd.pid");
545
			break;
546
		default:
547
			$running = is_service_running($service['name']);
548
	}
549
	return $running;
550
}
551

    
552
function get_service_status_icon($service, $withtext = true, $smallicon = false, $withthumbs = false, $title = "service_state") {
553
	$output = "";
554

    
555
	if (get_service_status($service)) {
556
		$statustext = gettext("Running");
557
		$text_class = "text-success";
558
		$fa_class = "fa fa-check-circle";
559
		$fa_class_thumbs = "fa fa-thumbs-o-up";
560
		$Thumbs_UpDown = "Thumbs up";
561
	} else {
562
		if (is_service_enabled($service['name'])) {
563
			$statustext = gettext("Stopped");
564
			$text_class = "text-danger";
565
			$fa_class = "fa fa-times-circle";
566
		} else {
567
			$statustext = gettext("Disabled");
568
			$text_class = "text-warning";
569
			$fa_class = "fa fa-ban";
570
		}
571
		$fa_class_thumbs = "fa fa-thumbs-o-down";
572
		$Thumbs_UpDown = "Thumbs down";
573
	}
574
	$fa_size = ($smallicon) ? "fa-1x" : "fa-lg";
575

    
576
	if ($title == "state") {
577
		$title = $statustext;
578
	} elseif ($title == "service_state") {
579
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["name"], $statustext);
580
	} elseif ($title == "description_state") {
581
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["description"], $statustext);
582
	} elseif ($title == "description_service_state") {
583
		$title = sprintf(gettext('%1$s, %2$s Service is %3$s'), $service["description"], $service["name"], $statustext);
584
	}
585

    
586
	$spacer = ($withthumbs || $withtext) ? " " : "";
587

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

    
590
	$spacer = ($withtext) ? " " : "";
591
	if ($withthumbs) {
592
		$output .= "<i class=\"{$text_class} {$fa_class_thumbs} {$fa_size}\" title=\"{$Thumbs_UpDown}\"></i>{$spacer}";
593
	}
594

    
595
	if ($withtext) {
596
		$output .= "<span class=\"" . $text_class . "\">" . $statustext . "</span>";
597
	}
598

    
599
	return $output;
600
}
601

    
602
function get_service_control_links($service, $addname = false) {
603
	global $g;
604
	$output = "";
605
	$stitle = ($addname) ? $service['name'] . " " : "";
606

    
607
	switch ($service['name']) {
608
		case "openvpn":
609
			$link = '<a title="%s" href="#" id="openvpn-%s-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
610
		break;
611
		case "captiveportal":
612
			$link = '<a title="%s" href="#" id="captiveportal-%s-' . $service['zone'] . '">';
613
		break;
614
		default:
615
			$link = '<a title="%s" href="#" id="%s-' . $service['name'] . '">';
616
	}
617

    
618
	if (get_service_status($service)) {
619
		switch ($service['name']) {
620
			case "openvpn":
621
				$output .= '<a href="#" id="openvpn-restartservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
622
				break;
623
			case "captiveportal":
624
				$output .= '<a href="#" id="captiveportal-restartservice-' . $service['zone'] . '">';
625
				break;
626
			default:
627
				$output .= '<a href="#" id="restartservice-' . $service['name'] . '" >';
628
		}
629

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

    
632
		switch ($service['name']) {
633
			case "openvpn":
634
				$output .= '<a href="#" id="openvpn-stopservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
635
				break;
636
			case "captiveportal":
637
				$output .= '<a href="#" id="captiveportal-stopservice-' . $service['zone'] . '">';
638
				break;
639
			default:
640
				$output .= '<a href="#" id="stopservice-' . $service['name'] . '">';
641
		}
642

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

    
645
	} else {
646
		$service_enabled = is_service_enabled($service['name']);
647

    
648
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
649
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
650
			$output .= '<i class="fa fa-play-circle"></i></a> ';
651
		}
652
	}
653

    
654
	return $output;
655
}
656

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

    
816
function service_control_restart($name, $extras) {
817
	global $g;
818
	switch ($name) {
819
		case 'radvd':
820
			services_radvd_configure();
821
			break;
822
		case 'captiveportal':
823
			$zone = htmlspecialchars($extras['zone']);
824
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
825
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
826
			captiveportal_init_webgui_zonename($zone);
827
			break;
828
		case 'ntpd':
829
		case 'openntpd':
830
			system_ntp_configure();
831
			break;
832
		case 'dpinger':
833
			setup_gateways_monitor();
834
			break;
835
		case 'bsnmpd':
836
			services_snmpd_configure();
837
			break;
838
		case 'dhcrelay':
839
			services_dhcrelay_configure();
840
			break;
841
		case 'dhcrelay6':
842
			services_dhcrelay6_configure();
843
			break;
844
		case 'dnsmasq':
845
			services_dnsmasq_configure();
846
			break;
847
		case 'unbound':
848
			services_unbound_configure();
849
			break;
850
		case 'dhcpd':
851
			services_dhcpd_configure();
852
			break;
853
		case 'igmpproxy':
854
			services_igmpproxy_configure();
855
			break;
856
		case 'miniupnpd':
857
			upnp_action('restart');
858
			break;
859
		case 'ipsec':
860
			ipsec_force_reload();
861
			break;
862
		case 'sshd':
863
			send_event("service restart sshd");
864
			break;
865
		case 'pcscd':
866
			exec("/usr/local/sbin/strongswanrc stop");
867
			if (isvalidproc("pcscd")) {
868
				killbyname("pcscd");
869
			}
870
			ipsec_force_reload();
871
			break;
872
		case 'openvpn':
873
			$vpnmode = htmlspecialchars($extras['vpnmode']);
874
			if ($vpnmode == "server" || $vpnmode == "client") {
875
				$id = htmlspecialchars($extras['id']);
876
				$configfile = "{$g['openvpn_base']}/{$vpnmode}{$id}/config.ovpn";
877
				if (file_exists($configfile)) {
878
					openvpn_restart_by_vpnid($vpnmode, $id);
879
				}
880
			}
881
			break;
882
		case 'watchdogd':
883
			watchdogd_configure();
884
			break;
885
		case 'syslogd':
886
			system_syslogd_start();
887
			break;
888
		default:
889
			restart_service($name);
890
			break;
891
	}
892
	return sprintf(gettext("%s has been restarted."), htmlspecialchars($name));
893
}
894

    
895
?>
(1-1/2)