Project

General

Profile

Download (24.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/****h* pfSense/pkg-utils
3
	NAME
4
		pkg-utils.inc - Package subsystem
5
	DESCRIPTION
6
		This file contains various functions used by the pfSense package system.
7
	HISTORY
8
		$Id$
9

    
10
	Copyright (C) 2010 Ermal Luçi
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
	ARISING 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:	/usr/bin/cd	/usr/bin/tar	/usr/sbin/fifolog_create	/bin/chmod
38
	pfSense_BUILDER_BINARIES:	/usr/sbin/pkg_add	/usr/sbin/pkg_info	/usr/sbin/pkg_delete	/bin/rm
39
	pfSense_MODULE:	pkg
40
*/
41

    
42
require_once("globals.inc");
43
require_once("service-utils.inc");
44
if (file_exists("/cf/conf/use_xmlreader")) {
45
	require_once("xmlreader.inc");
46
} else {
47
	require_once("xmlparse.inc");
48
}
49
require_once("pfsense-utils.inc");
50

    
51
if (!function_exists("update_status")) {
52
	function update_status($status) {
53
		echo $status . "\n";
54
	}
55
}
56
if (!function_exists("update_output_window")) {
57
	function update_output_window($status) {
58
		echo $status . "\n";
59
	}
60
}
61

    
62
if (!function_exists("pkg_debug")) {
63
	/* set up logging if needed */
64
	function pkg_debug($msg) {
65
		global $g, $debug, $fd_log;
66

    
67
		if (!$debug) {
68
			return;
69
		}
70

    
71
		if (!$fd_log) {
72
			if (!$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_{$package}.log", "w")) {
73
				update_output_window("Warning, could not open log for writing.");
74
			}
75
		}
76
		@fwrite($fd_log, $msg);
77
	}
78
}
79

    
80
global $g;
81
if (!isset($g['platform'])) {
82
	$g['platform'] = trim(file_get_contents("/etc/platform"));
83
}
84

    
85
/* Remove pkg_prefix from package name if it's present */
86
function pkg_remove_prefix(&$pkg_name) {
87
	global $g;
88

    
89
	if (substr($pkg_name, 0, strlen($g['pkg_prefix'])) == $g['pkg_prefix']) {
90
		$pkg_name = substr($pkg_name, strlen($g['pkg_prefix']));
91
	}
92
}
93

    
94
/* Execute a pkg call */
95
function pkg_call($params) {
96
	if (empty($params)) {
97
		return false;
98
	}
99

    
100
	// XXX: Use proper call with fifo to collect statistics
101
	$_gc = exec(escapeshellcmd("env ASSUME_ALWAYS_YES=true /usr/sbin/pkg {$params}"), $output, $rc);
102

    
103
	return ($rc == 0);
104
}
105

    
106
/* Check if package is installed */
107
function is_pkg_installed($pkg_name) {
108
	global $g;
109

    
110
	pkg_remove_prefix($pkg_name);
111

    
112
	return pkg_call("info -e " . $g['pkg_prefix'] . $pkg_name);
113
}
114

    
115
/* Install package, $pkg_name should not contain prefix */
116
function pkg_install($pkg_name) {
117
	global $g;
118

    
119
	pkg_remove_prefix($pkg_name);
120

    
121
	if (!is_pkg_installed($pkg_name)) {
122
		return pkg_call("install -q -y " . $g['pkg_prefix'] . $pkg_name);
123
	}
124

    
125
	return false;
126
}
127

    
128
/* Delete package from FreeBSD, $pkg_name should not contain prefix */
129
function pkg_delete($pkg_name) {
130
	global $g;
131

    
132
	pkg_remove_prefix($pkg_name);
133

    
134
	if (is_pkg_installed($pkg_name)) {
135
		pkg_call("delete -q -y " . $g['pkg_prefix'] . $pkg_name);
136
		/* Cleanup unecessary dependencies */
137
		pkg_call("autoremove -y -q");
138
	}
139
}
140

    
141
/* Check if package is present in config.xml */
142
function is_package_installed($package_name) {
143
	return (get_package_id($package_name) != -1);
144
}
145

    
146
/* Find package array index */
147
function get_package_id($package_name) {
148
	global $config;
149

    
150
	if (!is_array($config['installedpackages']['package'])) {
151
		return -1;
152
	}
153

    
154
	foreach ($config['installedpackages']['package'] as $idx => $pkg) {
155
		if ($pkg['name'] == $package_name) {
156
			return $idx;
157
		}
158
	}
159

    
160
	return -1;
161
}
162

    
163
/* Keep backward compatibility since snort/suricata use this function */
164
function get_pkg_id($package_name) {
165
	return get_package_id($package_name);
166
}
167

    
168
/* Return internal_name when it's defined, otherwise, returns name */
169
function get_package_internal_name($package_data) {
170
	if (isset($package_data['internal_name']) && ($package_data['internal_name'] != "")) {
171
		/* e.g. name is Ipguard-dev, internal name is ipguard */
172
		return $package_data['internal_name'];
173
	} else {
174
		return $package_data['name'];
175
	}
176
}
177

    
178
/****f* pkg-utils/get_pkg_info
179
 * NAME
180
 *   get_pkg_info - Retrieve package information from package server.
181
 * INPUTS
182
 *   $pkgs - 'all' to retrieve all packages, an array containing package names otherwise
183
 *   $info - 'all' to retrieve all information, an array containing keys otherwise
184
 * RESULT
185
 *   $raw_versions - Array containing retrieved information, indexed by package name.
186
 ******/
187
function get_pkg_info($pkgs = 'all', $info = 'all') {
188
	global $g;
189

    
190
	// XXX: implement
191
	return array();
192
}
193

    
194
/*
195
 * resync_all_package_configs() Force packages to setup their configuration and rc.d files.
196
 * This function may also print output to the terminal indicating progress.
197
 */
198
function resync_all_package_configs($show_message = false) {
199
	global $config, $pkg_interface, $g;
200

    
201
	log_error(gettext("Resyncing configuration for all packages."));
202

    
203
	if (!is_array($config['installedpackages']['package'])) {
204
		return;
205
	}
206

    
207
	if ($show_message == true) {
208
		echo "Syncing packages:";
209
	}
210

    
211
	conf_mount_rw();
212

    
213
	foreach ($config['installedpackages']['package'] as $idx => $package) {
214
		if (empty($package['name'])) {
215
			continue;
216
		}
217
		if ($show_message == true) {
218
			echo " " . $package['name'];
219
		}
220
		if (platform_booting() != true) {
221
			stop_service(get_package_internal_name($package));
222
		}
223
		sync_package($package['name']);
224
		if ($pkg_interface == "console") {
225
			echo "\n" . gettext("Syncing packages:");
226
		}
227
	}
228

    
229
	if ($show_message == true) {
230
		echo " done.\n";
231
	}
232

    
233
	@unlink("/conf/needs_package_sync");
234
	conf_mount_ro();
235
}
236

    
237
function uninstall_package($package_name) {
238
	global $config, $static_output;
239

    
240
	$id = get_package_id($package_name);
241
	if ($id >= 0) {
242
		$internal_name = get_package_internal_name($config['installedpackages']['package'][$id]);
243
		stop_service($internal_name);
244
		$static_output .= "Removing package...\n";
245
		update_output_window($static_output);
246
		pkg_delete($internal_name);
247
	}
248
	delete_package_xml($package_name);
249

    
250
	$static_output .= gettext("done.") . "\n";
251
	update_output_window($static_output);
252
}
253

    
254
/* Run <custom_php_resync_config_command> */
255
function sync_package($package_name) {
256
	global $config, $builder_package_install;
257

    
258
	// If this code is being called by pfspkg_installer
259
	// which the builder system uses then return (ignore).
260
	if ($builder_package_install) {
261
		return;
262
	}
263

    
264
	if (empty($config['installedpackages']['package'])) {
265
		return;
266
	}
267

    
268
	if (($pkg_id = get_package_id($package_name)) == -1) {
269
		return; // This package doesn't really exist - exit the function.
270
	}
271

    
272
	if (!is_array($config['installedpackages']['package'][$pkg_id])) {
273
		return;  // No package belongs to the pkg_id passed to this function.
274
	}
275

    
276
	$package =& $config['installedpackages']['package'][$pkg_id];
277
	if (!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
278
		log_error(sprintf(gettext("The %s package is missing its configuration file and must be reinstalled."), $package['name']));
279
		delete_package_xml($package['name']);
280
		return;
281
	}
282

    
283
	$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
284
	if (isset($pkg_config['nosync'])) {
285
		return;
286
	}
287

    
288
	/* Bring in package include files */
289
	if (!empty($pkg_config['include_file'])) {
290
		$include_file = $pkg_config['include_file'];
291
		if (file_exists($include_file)) {
292
			require_once($include_file);
293
		} else {
294
			log_error("Reinstalling package {$package['name']} because its include file({$include_file}) is missing!");
295
			uninstall_package($package['name']);
296
			if (install_package($package['name']) < 0) {
297
				log_error("Reinstalling package {$package['name']} failed. Take appropriate measures!!!");
298
				return;
299
			}
300
			if (file_exists($include_file)) {
301
				require_once($include_file);
302
			} else {
303
				return;
304
			}
305
		}
306
	}
307

    
308
	if (!empty($pkg_config['custom_php_global_functions'])) {
309
		eval($pkg_config['custom_php_global_functions']);
310
	}
311
	if (!empty($pkg_config['custom_php_resync_config_command'])) {
312
		eval($pkg_config['custom_php_resync_config_command']);
313
	}
314
}
315

    
316
/* Read info.xml installed by package and return an array */
317
function read_package_config($package_name) {
318
	global $g;
319

    
320
	$pkg_info_xml = '/usr/local/share/' . $g['pkg_prefix'] . $package_name . '/info.xml';
321

    
322
	if (!file_exists($pkg_info_xml)) {
323
		return false;
324
	}
325

    
326
	$pkg_info = parse_xml_config_pkg($pkg_info_xml, 'pfsensepkgs');
327

    
328
	if (empty($pkg_info)) {
329
		return false;
330
	}
331

    
332
	/* it always returns an array with 1 item */
333
	return $pkg_info['package'][0];
334
}
335

    
336
function get_after_install_info($package_name) {
337
	$pkg_config = read_pkg_config($package_name);
338

    
339
	if (isset($pkg_config['after_install_info'])) {
340
		return $pkg_config['after_install_info'];
341
	}
342

    
343
	return '';
344
}
345

    
346
function eval_once($toeval) {
347
	global $evaled;
348
	if (!$evaled) {
349
		$evaled = array();
350
	}
351
	$evalmd5 = md5($toeval);
352
	if (!in_array($evalmd5, $evaled)) {
353
		@eval($toeval);
354
		$evaled[] = $evalmd5;
355
	}
356
	return;
357
}
358

    
359
function install_package($package_name) {
360
	global $g, $config, $static_output, $pkg_interface;
361

    
362
	if ($pkg_interface == "console") {
363
		echo "\n";
364
	}
365

    
366
	return pkg_install($package_name);
367
}
368

    
369
function install_package_xml($package_name) {
370
	global $g, $config, $static_output, $pkg_interface;
371

    
372
	if ($pkg_interface == "console") {
373
		echo "\n";
374
	}
375

    
376
	if (($pkg_info = read_package_config($package_name)) == false) {
377
		return false;
378
	}
379

    
380
	/* safe side. Write config below will send to ro again. */
381
	conf_mount_rw();
382

    
383
	pkg_debug(gettext("Beginning package installation.") . "\n");
384
	log_error(sprintf(gettext('Beginning package installation for %s .'), $pkg_info['name']));
385
	$static_output .= sprintf(gettext("Beginning package installation for %s .\n"), $pkg_info['name']);
386
	update_status($static_output);
387

    
388
	/* add package information to config.xml */
389
	$pkgid = get_package_id($pkg_info['name']);
390
	$static_output .= gettext("Saving updated package information...") . " ";
391
	update_output_window($static_output);
392
	if ($pkgid == -1) {
393
		$config['installedpackages']['package'][] = $pkg_info;
394
		$changedesc = sprintf(gettext("Installed %s package."),$pkg_info['name']);
395
		$to_output = gettext("done.") . "\n";
396
	} else {
397
		$config['installedpackages']['package'][$pkgid] = $pkg_info;
398
		$changedesc = sprintf(gettext("Overwrote previous installation of %s."), $pkg_info['name']);
399
		$to_output = gettext("overwrite!") . "\n";
400
	}
401
	unlink_if_exists('/conf/needs_package_sync');
402
	conf_mount_ro();
403
	write_config("Intermediate config write during package install for {$pkg_info['name']}.");
404
	$static_output .= $to_output;
405
	update_output_window($static_output);
406

    
407
	if (($pkgid = get_package_id($package_name)) == -1) {
408
		$static_output .= sprintf(gettext("The %s package is not installed.%sInstallation aborted."), $package_name, "\n\n");
409
		update_output_window($static_output);
410
		if ($pkg_interface <> "console") {
411
			echo "\n<script type=\"text/javascript\">document.progressbar.style.visibility='hidden';</script>";
412
			echo "\n<script type=\"text/javascript\">document.progholder.style.visibility='hidden';</script>";
413
		}
414

    
415
		uninstall_package($package_name);
416
		write_config($changedesc);
417
		log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
418
		$static_output .= gettext("Failed to install package.") . "\n";
419
		update_output_window($static_output);
420
		return false;
421
	}
422

    
423
	$configfile = substr(strrchr($pkg_info['config_file'], '/'), 1);
424
	if (file_exists("/usr/local/pkg/" . $configfile)) {
425
		$static_output .= gettext("Loading package configuration... ");
426
		update_output_window($static_output);
427
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $configfile, "packagegui");
428
		$static_output .= gettext("done.") . "\n";
429
		update_output_window($static_output);
430
		$static_output .= gettext("Configuring package components...\n");
431
		if (!empty($pkg_config['filter_rules_needed'])) {
432
			$config['installedpackages']['package'][$pkgid]['filter_rule_function'] = $pkg_config['filter_rules_needed'];
433
		}
434
		update_output_window($static_output);
435
		/* modify system files */
436

    
437
		/*   if a require exists, include it.  this will
438
		 *   show us where an error exists in a package
439
		 *   instead of making us blindly guess
440
		 */
441
		$missing_include = false;
442
		if ($pkg_config['include_file'] <> "") {
443
			$static_output .= gettext("Loading package instructions...") . "\n";
444
			update_output_window($static_output);
445
			pkg_debug("require_once('{$pkg_config['include_file']}')\n");
446
			if (file_exists($pkg_config['include_file'])) {
447
				require_once($pkg_config['include_file']);
448
			} else {
449
				$missing_include = true;
450
				$static_output .= "Include " . basename($pkg_config['include_file']) . " is missing!\n";
451
				update_output_window($static_output);
452

    
453
				uninstall_package($package_name);
454
				write_config($changedesc);
455
				log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
456
				$static_output .= gettext("Failed to install package.") . "\n";
457
				update_output_window($static_output);
458
				return false;
459
			}
460
		}
461

    
462
		/* custom commands */
463
		$static_output .= gettext("Custom commands...") . "\n";
464
		update_output_window($static_output);
465
		if ($missing_include == false) {
466
			if ($pkg_config['custom_php_global_functions'] <> "") {
467
				$static_output .= gettext("Executing custom_php_global_functions()...");
468
				update_output_window($static_output);
469
				eval_once($pkg_config['custom_php_global_functions']);
470
				$static_output .= gettext("done.") . "\n";
471
				update_output_window($static_output);
472
			}
473
			if ($pkg_config['custom_php_install_command']) {
474
				$static_output .= gettext("Executing custom_php_install_command()...");
475
				update_output_window($static_output);
476
				eval_once($pkg_config['custom_php_install_command']);
477
				$static_output .= gettext("done.") . "\n";
478
				update_output_window($static_output);
479
			}
480
			if ($pkg_config['custom_php_resync_config_command'] <> "") {
481
				$static_output .= gettext("Executing custom_php_resync_config_command()...");
482
				update_output_window($static_output);
483
				eval_once($pkg_config['custom_php_resync_config_command']);
484
				$static_output .= gettext("done.") . "\n";
485
				update_output_window($static_output);
486
			}
487
		}
488
		/* sidebar items */
489
		if (is_array($pkg_config['menu'])) {
490
			$static_output .= gettext("Menu items... ");
491
			update_output_window($static_output);
492
			foreach ($pkg_config['menu'] as $menu) {
493
				if (is_array($config['installedpackages']['menu'])) {
494
					foreach ($config['installedpackages']['menu'] as $amenu) {
495
						if ($amenu['name'] == $menu['name']) {
496
							continue 2;
497
						}
498
					}
499
				} else {
500
					$config['installedpackages']['menu'] = array();
501
				}
502
				$config['installedpackages']['menu'][] = $menu;
503
			}
504
			$static_output .= gettext("done.") . "\n";
505
			update_output_window($static_output);
506
		}
507
		/* services */
508
		if (is_array($pkg_config['service'])) {
509
			$static_output .= gettext("Services... ");
510
			update_output_window($static_output);
511
			foreach ($pkg_config['service'] as $service) {
512
				if (is_array($config['installedpackages']['service'])) {
513
					foreach ($config['installedpackages']['service'] as $aservice) {
514
						if ($aservice['name'] == $service['name']) {
515
							continue 2;
516
						}
517
					}
518
				} else {
519
					$config['installedpackages']['service'] = array();
520
				}
521
				$config['installedpackages']['service'][] = $service;
522
			}
523
			$static_output .= gettext("done.") . "\n";
524
			update_output_window($static_output);
525
		}
526
	} else {
527
		$static_output .= gettext("Loading package configuration... failed!") . "\n\n" . gettext("Installation aborted.");
528
		update_output_window($static_output);
529
		pkg_debug(gettext("Unable to load package configuration. Installation aborted.") ."\n");
530
		if ($pkg_interface <> "console") {
531
			echo "\n<script type=\"text/javascript\">document.progressbar.style.visibility='hidden';</script>";
532
			echo "\n<script type=\"text/javascript\">document.progholder.style.visibility='hidden';</script>";
533
		}
534

    
535
		uninstall_package($package_name);
536
		write_config($changedesc);
537
		log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
538
		$static_output .= gettext("Failed to install package.") . "\n";
539
		update_output_window($static_output);
540
		return false;
541
	}
542

    
543
	/* set up package logging streams */
544
	if ($pkg_info['logging']) {
545
		system_syslogd_start();
546
	}
547

    
548
	$static_output .= gettext("Writing configuration... ");
549
	update_output_window($static_output);
550
	write_config($changedesc);
551
	log_error(sprintf(gettext("Successfully installed package: %s."), $pkg_info['name']));
552
	$static_output .= gettext("done.") . "\n";
553
	update_output_window($static_output);
554
	if ($pkg_info['after_install_info']) {
555
		update_output_window($pkg_info['after_install_info']);
556
	}
557

    
558
	return true;
559
}
560

    
561
function delete_package($package_name) {
562
	global $config, $g, $static_output;
563

    
564
	if (!is_package_installed($package_name)) {
565
		return;
566
	}
567

    
568
	$static_output .= sprintf(gettext("Starting package deletion for %s..."),$package_name);
569
	update_output_window($static_output);
570

    
571
	pkg_delete($package_name);
572
	$static_output .= "done.\n";
573
	update_output_window($static_output);
574

    
575
	return;
576
}
577

    
578
function delete_package_xml($package_name, $when = "post-deinstall") {
579
	global $g, $config, $static_output, $pkg_interface;
580

    
581
	conf_mount_rw();
582

    
583
	$pkgid = get_package_id($package_name);
584
	if ($pkgid == -1) {
585
		$static_output .= sprintf(gettext("The %s package is not installed.%sDeletion aborted."), $package_name, "\n\n");
586
		update_output_window($static_output);
587
		if ($pkg_interface <> "console") {
588
			echo "\n<script type=\"text/javascript\">document.progressbar.style.visibility='hidden';</script>";
589
			echo "\n<script type=\"text/javascript\">document.progholder.style.visibility='hidden';</script>";
590
		}
591
		ob_flush();
592
		sleep(1);
593
		conf_mount_ro();
594
		return;
595
	}
596
	pkg_debug(sprintf(gettext("Removing %s package... "),$package_name));
597
	$static_output .= sprintf(gettext("Removing %s components..."),$package_name) . "\n";
598
	update_output_window($static_output);
599
	/* parse package configuration */
600
	$packages = &$config['installedpackages']['package'];
601
	$menus =& $config['installedpackages']['menu'];
602
	$services = &$config['installedpackages']['service'];
603
	$pkg_info =& $packages[$pkgid];
604
	if (file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
605
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $packages[$pkgid]['configurationfile'], "packagegui");
606
		/* remove menu items */
607
		if (is_array($pkg_config['menu'])) {
608
			$static_output .= gettext("Menu items... ");
609
			update_output_window($static_output);
610
			if (is_array($pkg_config['menu']) && is_array($menus)) {
611
				foreach ($pkg_config['menu'] as $menu) {
612
					foreach ($menus as $key => $instmenu) {
613
						if ($instmenu['name'] == $menu['name']) {
614
							unset($menus[$key]);
615
							break;
616
						}
617
					}
618
				}
619
			}
620
			$static_output .= gettext("done.") . "\n";
621
			update_output_window($static_output);
622
		}
623
		/* remove services */
624
		if (is_array($pkg_config['service'])) {
625
			$static_output .= gettext("Services... ");
626
			update_output_window($static_output);
627
			if (is_array($pkg_config['service']) && is_array($services)) {
628
				foreach ($pkg_config['service'] as $service) {
629
					foreach ($services as $key => $instservice) {
630
						if ($instservice['name'] == $service['name']) {
631
							if (platform_booting() != true) {
632
								stop_service($service['name']);
633
							}
634
							if ($service['rcfile']) {
635
								$prefix = RCFILEPREFIX;
636
								if (!empty($service['prefix'])) {
637
									$prefix = $service['prefix'];
638
								}
639
								if (file_exists("{$prefix}{$service['rcfile']}")) {
640
									@unlink("{$prefix}{$service['rcfile']}");
641
								}
642
							}
643
							unset($services[$key]);
644
						}
645
					}
646
				}
647
			}
648
			$static_output .= gettext("done.") . "\n";
649
			update_output_window($static_output);
650
		}
651
		/*
652
		 * XXX: Otherwise inclusion of config.inc again invalidates actions taken.
653
		 * 	Same is done during installation.
654
		 */
655
		write_config("Intermediate config write during package removal for {$package_name}.");
656

    
657
		/*
658
		 * If a require exists, include it.  this will
659
		 * show us where an error exists in a package
660
		 * instead of making us blindly guess
661
		 */
662
		$missing_include = false;
663
		if ($pkg_config['include_file'] <> "") {
664
			$static_output .= gettext("Loading package instructions...") . "\n";
665
			update_output_window($static_output);
666
			pkg_debug("require_once(\"{$pkg_config['include_file']}\")\n");
667
			if (file_exists($pkg_config['include_file'])) {
668
				require_once($pkg_config['include_file']);
669
			} else {
670
				$missing_include = true;
671
				update_output_window($static_output);
672
				$static_output .= "Include file " . basename($pkg_config['include_file']) . " could not be found for inclusion.\n";
673
			}
674
		}
675
		/* ermal
676
		 * NOTE: It is not possible to handle parse errors on eval.
677
		 * So we prevent it from being run at all to not interrupt all the other code.
678
		 */
679
		if ($when == "deinstall" && $missing_include == false) {
680
			/* evaluate this package's global functions and pre deinstall commands */
681
			if ($pkg_config['custom_php_global_functions'] <> "") {
682
				eval_once($pkg_config['custom_php_global_functions']);
683
			}
684
			if ($pkg_config['custom_php_pre_deinstall_command'] <> "") {
685
				eval_once($pkg_config['custom_php_pre_deinstall_command']);
686
			}
687
		}
688
		/* deinstall commands */
689
		if ($when == "post-deinstall" && $pkg_config['custom_php_deinstall_command'] <> "") {
690
			$static_output .= gettext("Deinstall commands... ");
691
			update_output_window($static_output);
692
			if ($missing_include == false) {
693
				eval_once($pkg_config['custom_php_deinstall_command']);
694
				$static_output .= gettext("done.") . "\n";
695
			} else {
696
				$static_output .= "\nNot executing custom deinstall hook because an include is missing.\n";
697
			}
698
			update_output_window($static_output);
699
		}
700
	}
701
	/* syslog */
702
	if (is_array($pkg_info['logging']) && $pkg_info['logging']['logfile_name'] <> "") {
703
		$static_output .= "Syslog entries... ";
704
		update_output_window($static_output);
705
		remove_text_from_file("/etc/syslog.conf", $pkg_info['logging']['facilityname'] . "\t\t\t\t" . $pkg_info['logging']['logfilename']);
706
		system_syslogd_start();
707
		@unlink("{$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
708
		$static_output .= "done.\n";
709
		update_output_window($static_output);
710
	}
711

    
712
	conf_mount_ro();
713
	/* remove config.xml entries */
714
	$static_output .= gettext("Configuration... ");
715
	update_output_window($static_output);
716
	unset($config['installedpackages']['package'][$pkgid]);
717
	$static_output .= gettext("done.") . "\n";
718
	update_output_window($static_output);
719
	write_config("Removed {$package_name} package.\n");
720
}
721

    
722
function pkg_reinstall_all() {
723
	global $g, $config;
724

    
725
	// XXX: implement
726
	return;
727
}
728

    
729
function stop_packages() {
730
	require_once("config.inc");
731
	require_once("functions.inc");
732
	require_once("filter.inc");
733
	require_once("shaper.inc");
734
	require_once("captiveportal.inc");
735
	require_once("pkg-utils.inc");
736
	require_once("pfsense-utils.inc");
737
	require_once("service-utils.inc");
738

    
739
	global $config, $g;
740

    
741
	log_error("Stopping all packages.");
742

    
743
	$rcfiles = glob(RCFILEPREFIX . "*.sh");
744
	if (!$rcfiles) {
745
		$rcfiles = array();
746
	} else {
747
		$rcfiles = array_flip($rcfiles);
748
		if (!$rcfiles) {
749
			$rcfiles = array();
750
		}
751
	}
752

    
753
	if (is_array($config['installedpackages']['package'])) {
754
		foreach ($config['installedpackages']['package'] as $package) {
755
			echo " Stopping package {$package['name']}...";
756
			$internal_name = get_package_internal_name($package);
757
			stop_service($internal_name);
758
			unset($rcfiles[RCFILEPREFIX . strtolower($internal_name) . ".sh"]);
759
			echo "done.\n";
760
		}
761
	}
762

    
763
	foreach ($rcfiles as $rcfile => $number) {
764
		$shell = @popen("/bin/sh", "w");
765
		if ($shell) {
766
			echo " Stopping {$rcfile}...";
767
			if (!@fwrite($shell, "{$rcfile} stop >>/tmp/bootup_messages 2>&1")) {
768
				if ($shell) {
769
					pclose($shell);
770
				}
771
				$shell = @popen("/bin/sh", "w");
772
			}
773
			echo "done.\n";
774
			pclose($shell);
775
		}
776
	}
777
}
778

    
779
function verify_all_package_servers() {
780
	// XXX: Remove it after GUI is ready
781
	return true;
782
}
783

    
784
function check_package_server_ssl() {
785
	// XXX: Remove it after GUI is ready
786
	return true;
787
}
788

    
789
?>
(40-40/67)