Project

General

Profile

Download (31.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * pkg-utils.inc
4
 */
5
/* ====================================================================
6
 *	Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved.
7
 *	Copyright (c)  2005-2006 Colin Smith (ethethlay@gmail.com)
8
 *
9
 *	Redistribution and use in source and binary forms, with or without modification,
10
 *	are permitted provided that the following conditions are met:
11
 *
12
 *	1. Redistributions of source code must retain the above copyright notice,
13
 *		this list of conditions and the following disclaimer.
14
 *
15
 *	2. Redistributions in binary form must reproduce the above copyright
16
 *		notice, this list of conditions and the following disclaimer in
17
 *		the documentation and/or other materials provided with the
18
 *		distribution.
19
 *
20
 *	3. All advertising materials mentioning features or use of this software
21
 *		must display the following acknowledgment:
22
 *		"This product includes software developed by the pfSense Project
23
 *		 for use in the pfSense software distribution. (http://www.pfsense.org/).
24
 *
25
 *	4. The names "pfSense" and "pfSense Project" must not be used to
26
 *		 endorse or promote products derived from this software without
27
 *		 prior written permission. For written permission, please contact
28
 *		 coreteam@pfsense.org.
29
 *
30
 *	5. Products derived from this software may not be called "pfSense"
31
 *		nor may "pfSense" appear in their names without prior written
32
 *		permission of the Electric Sheep Fencing, LLC.
33
 *
34
 *	6. Redistributions of any form whatsoever must retain the following
35
 *		acknowledgment:
36
 *
37
 *	"This product includes software developed by the pfSense Project
38
 *	for use in the pfSense software distribution (http://www.pfsense.org/).
39
 *
40
 *	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
 *	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 *	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
 *	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
 *	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
 *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
 *	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
 *	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 *	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
 *	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
 *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
 *	OF THE POSSIBILITY OF SUCH DAMAGE.
52
 *
53
 *	====================================================================
54
 *
55
 */
56

    
57
require_once("globals.inc");
58
require_once("service-utils.inc");
59

    
60
if (file_exists("/cf/conf/use_xmlreader")) {
61
	require_once("xmlreader.inc");
62
} else {
63
	require_once("xmlparse.inc");
64
}
65

    
66
require_once("pfsense-utils.inc");
67

    
68
if (!function_exists("pkg_debug")) {
69
	/* set up logging if needed */
70
	function pkg_debug($msg) {
71
		global $g, $debug, $fd_log;
72

    
73
		if (!$debug) {
74
			return;
75
		}
76

    
77
		if (!$fd_log) {
78
			if (!$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_debug.log", "w")) {
79
				update_status(gettext("Warning, could not open log for writing.") . "\n");
80
			}
81
		}
82
		@fwrite($fd_log, $msg);
83
	}
84
}
85

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

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

    
95
/* Execute pkg update when it's necessary */
96
function pkg_update($force = false) {
97
	global $g;
98

    
99
	return pkg_call("update" . ($force ? " -f" : ""));
100
}
101

    
102
/* return an array with necessary environment vars for pkg */
103
function pkg_env() {
104
	global $config, $g;
105

    
106
	$pkg_env_vars = array(
107
		"LANG" => "C",
108
		"HTTP_USER_AGENT" => $user_agent,
109
		"ASSUME_ALWAYS_YES" => "true"
110
	);
111

    
112
	if ($g['platform'] == "nanobsd" ||
113
	    isset($config['system']['use_mfs_tmpvar'])) {
114
		$pkg_env_vars['PKG_DBDIR'] = '/root/var/db/pkg';
115
		$pkg_env_vars['PKG_CACHEDIR'] = '/root/var/cache/pkg';
116
	}
117

    
118
	return $pkg_env_vars;
119
}
120

    
121
/* Execute a pkg call */
122
function pkg_call($params, $mute = false) {
123
	global $g, $config;
124

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

    
129
	$user_agent = $g['product_name'] . '/' . $g['product_version'];
130
	if (!isset($config['system']['do_not_send_host_uuid'])) {
131
		$user_agent .= ' : ' . get_single_sysctl('kern.hostuuid');
132
	}
133

    
134
	$descriptorspec = array(
135
		1 => array("pipe", "w"), /* stdout */
136
		2 => array("pipe", "w")	 /* stderr */
137
	);
138

    
139
	conf_mount_rw();
140

    
141
	pkg_debug("pkg_call(): {$params}\n");
142
	$process = proc_open("/usr/sbin/pkg {$params}", $descriptorspec, $pipes,
143
	    '/', pkg_env());
144

    
145
	if (!is_resource($process)) {
146
		conf_mount_ro();
147
		return false;
148
	}
149

    
150
	stream_set_blocking($pipes[1], 0);
151
	stream_set_blocking($pipes[2], 0);
152

    
153
	/* XXX: should be a tunnable? */
154
	$timeout = 300; // seconds
155
	$error_log = '';
156
	$started = time();
157
	$maxwaittime = 10; // Number of seconds to wait for a response fromteh pacakge we are calling
158

    
159
	do {
160
		$write = array();
161
		$read = array($pipes[1], $pipes[2]);
162
		$except = array();
163

    
164
		$stream = stream_select($read, $write, $except, null, $timeout);
165
		if ($stream !== FALSE && $stream > 0) {
166
			foreach ($read as $pipe) {
167
				$content = stream_get_contents($pipe);
168
				if ($content == '') {
169
					continue;
170
				}
171
				if ($pipe === $pipes[1]) {
172
					if (!$mute) {
173
						update_status($content);
174
					}
175
					flush();
176
				} else if ($pipe === $pipes[2]) {
177
					$error_log .= $content;
178
				}
179
			}
180
		}
181

    
182
		$status = proc_get_status($process);
183

    
184
		$now = time();
185

    
186
		if (($now - $started) >= $maxwaittime) {
187
			$rc = -1;
188
			proc_terminate($process);
189
			break;
190
		}
191

    
192
	} while ($status['running']);
193

    
194
	fclose($pipes[1]);
195
	fclose($pipes[2]);
196
	proc_close($process);
197

    
198
	conf_mount_ro();
199

    
200
	if (!isset($rc)) {
201
		$rc = $status['exitcode'];
202
	}
203

    
204
	pkg_debug("pkg_call(): rc = {$rc}\n");
205
	if ($rc == 0) {
206
		return true;
207
	}
208

    
209
	pkg_debug("pkg_call(): error_log\n{$error_log}\n");
210
	if (!$mute) {
211
		update_status("\n\n" .  sprintf(gettext(
212
		    "ERROR!!! An error occurred on pkg execution (rc = %d) with parameters '%s':"),
213
		    $rc, $params) . "\n" . $error_log . "\n");
214
	}
215

    
216
	return false;
217
}
218

    
219
/* Execute pkg with $params, fill stdout and stderr and return pkg rc */
220
function pkg_exec($params, &$stdout, &$stderr) {
221
	global $g, $config;
222

    
223
	if (empty($params)) {
224
		return -1;
225
	}
226

    
227
	$user_agent = $g['product_name'] . '/' . $g['product_version'];
228
	if (!isset($config['system']['do_not_send_host_uuid'])) {
229
		$user_agent .= ' : ' . get_single_sysctl('kern.hostuuid');
230
	}
231

    
232
	$descriptorspec = array(
233
		1 => array("pipe", "w"), /* stdout */
234
		2 => array("pipe", "w")	 /* stderr */
235
	);
236

    
237
	conf_mount_rw();
238

    
239
	pkg_debug("pkg_exec(): {$params}\n");
240
	$process = proc_open("/usr/sbin/pkg {$params}", $descriptorspec, $pipes,
241
	    '/', pkg_env());
242

    
243
	if (!is_resource($process)) {
244
		conf_mount_ro();
245
		return -1;
246
	}
247

    
248
	$stdout = '';
249
	while (($l = fgets($pipes[1])) !== FALSE) {
250
		$stdout .= $l;
251
	}
252
	fclose($pipes[1]);
253

    
254
	$stderr = '';
255
	while (($l = fgets($pipes[2])) !== FALSE) {
256
		$stderr .= $l;
257
	}
258
	fclose($pipes[2]);
259

    
260
	conf_mount_ro();
261

    
262
	return proc_close($process);
263
}
264

    
265
/* Compare 2 pkg versions and return:
266
 * '=' - versions are the same
267
 * '>' - $v1 > $v2
268
 * '<' - $v1 < $v2
269
 * '?' - Error
270
 */
271
function pkg_version_compare($v1, $v2) {
272
	if (empty($v1) || empty($v2)) {
273
		return '?';
274
	}
275

    
276
	$rc = pkg_exec("version -t '{$v1}' '{$v2}'", $stdout, $stderr);
277

    
278
	if ($rc != 0) {
279
		return '?';
280
	}
281

    
282
	return str_replace("\n", "", $stdout);
283
}
284

    
285
/* Check if package is installed */
286
function is_pkg_installed($pkg_name) {
287
	global $g;
288

    
289
	if (empty($pkg_name)) {
290
		return false;
291
	}
292

    
293
	return pkg_call("info -e " . $pkg_name, true);
294
}
295

    
296
/* Install package, $pkg_name should not contain prefix */
297
function pkg_install($pkg_name, $force = false) {
298
	global $g;
299
	$result = false;
300

    
301
	$shortname = $pkg_name;
302
	pkg_remove_prefix($shortname);
303

    
304
	$pkg_force = "";
305
	if ($force) {
306
		$pkg_force = "-f ";
307
	}
308

    
309
	pkg_debug("Installing package {$shortname}\n");
310
	if ($force || !is_pkg_installed($pkg_name)) {
311
		$result = pkg_call("install -y " . $pkg_force . $pkg_name);
312
		/* Cleanup cacke to free disk space */
313
		pkg_call("clean -y");
314
	}
315

    
316
	return $result;
317
}
318

    
319
/* Delete package from FreeBSD, $pkg_name should not contain prefix */
320
function pkg_delete($pkg_name) {
321
	global $g;
322

    
323
	$shortname = $pkg_name;
324
	pkg_remove_prefix($shortname);
325

    
326
	pkg_debug("Removing package {$shortname}\n");
327
	if (is_pkg_installed($pkg_name)) {
328
		pkg_call("delete -y " . $pkg_name);
329
		/* Cleanup unecessary dependencies */
330
		pkg_call("autoremove -y");
331
	}
332
}
333

    
334
/* Check if package is present in config.xml */
335
function is_package_installed($package_name) {
336
	return (get_package_id($package_name) != -1);
337
}
338

    
339
/* Find package array index */
340
function get_package_id($package_name) {
341
	global $config;
342

    
343
	if (!is_array($config['installedpackages']['package'])) {
344
		return -1;
345
	}
346

    
347
	foreach ($config['installedpackages']['package'] as $idx => $pkg) {
348
		if ($pkg['name'] == $package_name ||
349
		    get_package_internal_name($pkg) == $package_name) {
350
			return $idx;
351
		}
352
	}
353

    
354
	return -1;
355
}
356

    
357
/* Keep backward compatibility since snort/suricata use this function */
358
function get_pkg_id($package_name) {
359
	return get_package_id($package_name);
360
}
361

    
362
/* Return internal_name when it's defined, otherwise, returns name */
363
function get_package_internal_name($package_data) {
364
	if (isset($package_data['internal_name']) && ($package_data['internal_name'] != "")) {
365
		/* e.g. name is Ipguard-dev, internal name is ipguard */
366
		return $package_data['internal_name'];
367
	} else {
368
		return $package_data['name'];
369
	}
370
}
371

    
372
// Get information about packages.
373
function get_pkg_info($pkgs = 'all', $info = 'all') {
374
	global $g, $input_errors;
375

    
376
	$out = '';
377
	$err = '';
378

    
379
	unset($pkg_filter);
380
	if (is_array($pkgs)) {
381
		$pkg_filter = $pkgs;
382
		$pkgs = 'all';
383
	}
384

    
385
	if ($pkgs == 'all') {
386
		$pkgs = $g['pkg_prefix'];
387
	}
388

    
389
	$rc = pkg_exec("search --raw-format json-compact " . $pkgs, $out, $err);
390

    
391
	if ($rc != 0) {
392
		update_status("\n" . gettext(
393
		    "ERROR: Error trying to get packages list. Aborting...")
394
		    . "\n");
395
		update_status($err);
396
		$input_errors[] =  gettext("ERROR: Error trying to get packages list. Aborting...") . "\n";
397
		$input_errors[] =  $err;
398
		return array();
399
	}
400

    
401
	$result = array();
402
	$pkgs_info = explode("\n", $out);
403
	foreach ($pkgs_info as $pkg_info_json) {
404
		$pkg_info = json_decode($pkg_info_json, true);
405
		if (!isset($pkg_info['name'])) {
406
			continue;
407
		}
408

    
409
		if (isset($pkg_filter) && !in_array($pkg_info['name'], $pkg_filter)) {
410
			continue;
411
		}
412

    
413
		$pkg_info['shortname'] = $pkg_info['name'];
414
		pkg_remove_prefix($pkg_info['shortname']);
415

    
416
		/* XXX: Add it to globals.inc? */
417
		$pkg_info['changeloglink'] =
418
		    "https://github.com/pfsense/FreeBSD-ports/commits/devel/" .
419
		    $pkg_info['categories'][0] . '/' . $pkg_info['name'];
420

    
421
		if (is_pkg_installed($pkg_info['name'])) {
422
			$pkg_info['installed'] = true;
423

    
424
			$rc = pkg_exec("query %v {$pkg_info['name']}", $out, $err);
425

    
426
			if ($rc != 0) {
427
				update_status("\n" . gettext(
428
				    "ERROR: Error trying to get package version. Aborting...")
429
				    . "\n");
430
				update_status($err);
431
				$input_errors[] =  gettext("ERROR: Error trying to get package version. Aborting...") . "\n";
432
				$input_errors[] =  $err;
433
				return array();
434
			}
435

    
436
			$pkg_info['installed_version'] = str_replace("\n", "", $out);
437
		} else if (is_package_installed($pkg_info['shortname'])) {
438
			$pkg_info['broken'] = true;
439
		}
440

    
441
		$pkg_info['desc'] = preg_replace('/\n+WWW:.*$/', '', $pkg_info['desc']);
442

    
443
		$result[] = $pkg_info;
444
		unset($pkg_info);
445
	}
446

    
447
	/* Sort result alphabetically */
448
	usort($result, function($a, $b) {
449
		return(strcasecmp ($a['name'], $b['name']));
450
	});
451

    
452
	return $result;
453
}
454

    
455
/*
456
 * resync_all_package_configs() Force packages to setup their configuration and rc.d files.
457
 * This function may also print output to the terminal indicating progress.
458
 */
459
function resync_all_package_configs($show_message = false) {
460
	global $config, $pkg_interface, $g;
461

    
462
	log_error(gettext("Resyncing configuration for all packages."));
463

    
464
	if (!is_array($config['installedpackages']['package'])) {
465
		return;
466
	}
467

    
468
	if ($show_message == true) {
469
		echo "Syncing packages:";
470
	}
471

    
472
	conf_mount_rw();
473

    
474
	foreach ($config['installedpackages']['package'] as $idx => $package) {
475
		if (empty($package['name'])) {
476
			continue;
477
		}
478
		if ($show_message == true) {
479
			echo " " . $package['name'];
480
		}
481
		if (platform_booting() != true) {
482
			stop_service(get_package_internal_name($package));
483
		}
484
		sync_package($package['name']);
485
		update_status(gettext("Syncing packages...") . "\n");
486
	}
487

    
488
	if ($show_message == true) {
489
		echo " done.\n";
490
	}
491

    
492
	@unlink("/conf/needs_package_sync");
493
	conf_mount_ro();
494
}
495

    
496
function uninstall_package($package_name) {
497
	global $config;
498

    
499
	$internal_name = $package_name;
500
	$id = get_package_id($package_name);
501
	if ($id >= 0) {
502
		$internal_name = get_package_internal_name($config['installedpackages']['package'][$id]);
503
		stop_service($internal_name);
504
	}
505
	$pkg_name = $g['pkg_prefix'] . $internal_name;
506

    
507
	if (is_pkg_installed($pkg_name)) {
508
		update_status(gettext("Removing package...") . "\n");
509
		pkg_delete($pkg_name);
510
	} else {
511
		delete_package_xml($package_name);
512
	}
513

    
514
	update_status(gettext("done.") . "\n");
515
}
516

    
517
/* Run <custom_php_resync_config_command> */
518
function sync_package($package_name) {
519
	global $config, $builder_package_install;
520

    
521
	// If this code is being called by pfspkg_installer
522
	// which the builder system uses then return (ignore).
523
	if ($builder_package_install) {
524
		return;
525
	}
526

    
527
	if (empty($config['installedpackages']['package'])) {
528
		return;
529
	}
530

    
531
	if (($pkg_id = get_package_id($package_name)) == -1) {
532
		return; // This package doesn't really exist - exit the function.
533
	}
534

    
535
	if (!is_array($config['installedpackages']['package'][$pkg_id])) {
536
		return;	 // No package belongs to the pkg_id passed to this function.
537
	}
538

    
539
	$package =& $config['installedpackages']['package'][$pkg_id];
540
	if (!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
541
		log_error(sprintf(gettext("The %s package is missing its configuration file and must be reinstalled."), $package['name']));
542
		delete_package_xml($package['name']);
543
		return;
544
	}
545

    
546
	$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
547
	if (isset($pkg_config['nosync'])) {
548
		return;
549
	}
550

    
551
	/* Bring in package include files */
552
	if (!empty($pkg_config['include_file'])) {
553
		$include_file = $pkg_config['include_file'];
554
		if (file_exists($include_file)) {
555
			require_once($include_file);
556
		} else {
557
			log_error(sprintf(gettext('Reinstalling package %1$s because its include file(%2$s) is missing!'), $package['name'], $include_file));
558
			uninstall_package($package['name']);
559
			if (install_package($package['name']) != 0) {
560
				log_error(sprintf(gettext("Reinstalling package %s failed. Take appropriate measures!!!"), $package['name']));
561
				return;
562
			}
563
			if (file_exists($include_file)) {
564
				require_once($include_file);
565
			} else {
566
				return;
567
			}
568
		}
569
	}
570

    
571
	if (!empty($pkg_config['custom_php_global_functions'])) {
572
		eval($pkg_config['custom_php_global_functions']);
573
	}
574
	if (!empty($pkg_config['custom_php_resync_config_command'])) {
575
		eval($pkg_config['custom_php_resync_config_command']);
576
	}
577
}
578

    
579
/* Read info.xml installed by package and return an array */
580
function read_package_config($package_name) {
581
	global $g;
582

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

    
585
	if (!file_exists($pkg_info_xml)) {
586
		return false;
587
	}
588

    
589
	$pkg_info = parse_xml_config_pkg($pkg_info_xml, 'pfsensepkgs');
590

    
591
	if (empty($pkg_info)) {
592
		return false;
593
	}
594

    
595
	/* it always returns an array with 1 item */
596
	return $pkg_info['package'][0];
597
}
598

    
599
/* Read package configurationfile and return an array */
600
function read_package_configurationfile($package_name) {
601
	global $config, $g;
602

    
603
	$pkg_config = array();
604
	$id = get_package_id($package_name);
605

    
606
	if ($id < 0 || !isset($config['installedpackages']['package'][$id]['configurationfile'])) {
607
		return $pkg_config;
608
	}
609

    
610
	$pkg_configurationfile = $config['installedpackages']['package'][$id]['configurationfile'];
611

    
612
	if (empty($pkg_configurationfile) || !file_exists('/usr/local/pkg/' . $pkg_configurationfile)) {
613
		return $pkg_config;
614
	}
615

    
616
	$pkg_config = parse_xml_config_pkg('/usr/local/pkg/' . $pkg_configurationfile, "packagegui");
617

    
618
	return $pkg_config;
619
}
620

    
621
function get_after_install_info($package_name) {
622
	$pkg_config = read_package_config($package_name);
623

    
624
	if (isset($pkg_config['after_install_info'])) {
625
		return $pkg_config['after_install_info'];
626
	}
627

    
628
	return '';
629
}
630

    
631
function eval_once($toeval) {
632
	global $evaled;
633
	if (!$evaled) {
634
		$evaled = array();
635
	}
636
	$evalmd5 = md5($toeval);
637
	if (!in_array($evalmd5, $evaled)) {
638
		@eval($toeval);
639
		$evaled[] = $evalmd5;
640
	}
641
	return;
642
}
643

    
644
function install_package_xml($package_name) {
645
	global $g, $config, $pkg_interface;
646

    
647
	if (($pkg_info = read_package_config($package_name)) == false) {
648
		return false;
649
	}
650

    
651
	/* safe side. Write config below will send to ro again. */
652
	conf_mount_rw();
653

    
654
	pkg_debug(gettext("Beginning package installation.") . "\n");
655
	log_error(sprintf(gettext('Beginning package installation for %s .'), $pkg_info['name']));
656

    
657
	/* add package information to config.xml */
658
	$pkgid = get_package_id($pkg_info['name']);
659
	update_status(gettext("Saving updated package information...") . "\n");
660
	if ($pkgid == -1) {
661
		$config['installedpackages']['package'][] = $pkg_info;
662
		$changedesc = sprintf(gettext("Installed %s package."), $pkg_info['name']);
663
		$to_output = gettext("done.") . "\n";
664
	} else {
665
		$config['installedpackages']['package'][$pkgid] = $pkg_info;
666
		$changedesc = sprintf(gettext("Overwrote previous installation of %s."), $pkg_info['name']);
667
		$to_output = gettext("overwrite!") . "\n";
668
	}
669
	unlink_if_exists('/conf/needs_package_sync');
670
	write_config(sprintf(gettext("Intermediate config write during package install for %s."), $pkg_info['name']));
671
	conf_mount_ro();
672
	update_status($to_output);
673

    
674
	if (($pkgid = get_package_id($package_name)) == -1) {
675
		update_status(sprintf(gettext("The %s package is not installed.%sInstallation aborted."), $package_name, "\n\n"));
676

    
677
		uninstall_package($package_name);
678
		write_config($changedesc);
679
		log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
680
		update_status(gettext("Failed to install package.") . "\n");
681
		return false;
682
	}
683

    
684
	if (file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
685
		update_status(gettext("Loading package configuration... "));
686
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $pkg_info['configurationfile'], "packagegui");
687
		update_status(gettext("done.") . "\n");
688
		update_status(gettext("Configuring package components...") . "\n");
689
		if (!empty($pkg_config['filter_rules_needed'])) {
690
			$config['installedpackages']['package'][$pkgid]['filter_rule_function'] = $pkg_config['filter_rules_needed'];
691
		}
692
		/* modify system files */
693

    
694
		/* if a require exists, include it.  this will
695
		 * show us where an error exists in a package
696
		 * instead of making us blindly guess
697
		 */
698
		$missing_include = false;
699
		if ($pkg_config['include_file'] <> "") {
700
			update_status(gettext("Loading package instructions...") . "\n");
701
			if (file_exists($pkg_config['include_file'])) {
702
				pkg_debug("require_once('{$pkg_config['include_file']}')\n");
703
				require_once($pkg_config['include_file']);
704
			} else {
705
				pkg_debug("Missing include {$pkg_config['include_file']}\n");
706
				$missing_include = true;
707
				update_status(sprintf(gettext("Include %s is missing!"), basename($pkg_config['include_file'])) . "\n");
708

    
709
				uninstall_package($package_name);
710
				write_config($changedesc);
711
				log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
712
				update_status(gettext("Failed to install package.") . "\n");
713
				return false;
714
			}
715
		}
716

    
717
		/* custom commands */
718
		update_status(gettext("Custom commands...") . "\n");
719
		if ($missing_include == false) {
720
			if ($pkg_config['custom_php_global_functions'] <> "") {
721
				update_status(gettext("Executing custom_php_global_functions()..."));
722
				eval_once($pkg_config['custom_php_global_functions']);
723
				update_status(gettext("done.") . "\n");
724
			}
725
			if ($pkg_config['custom_php_install_command']) {
726
				update_status(gettext("Executing custom_php_install_command()..."));
727
				eval_once($pkg_config['custom_php_install_command']);
728
				update_status(gettext("done.") . "\n");
729
			}
730
			if ($pkg_config['custom_php_resync_config_command'] <> "") {
731
				update_status(gettext("Executing custom_php_resync_config_command()..."));
732
				eval_once($pkg_config['custom_php_resync_config_command']);
733
				update_status(gettext("done.") . "\n");
734
			}
735
		}
736
		/* sidebar items */
737
		if (is_array($pkg_config['menu'])) {
738
			update_status(gettext("Menu items... "));
739
			foreach ($pkg_config['menu'] as $menu) {
740
				if (is_array($config['installedpackages']['menu'])) {
741
					foreach ($config['installedpackages']['menu'] as $amenu) {
742
						if ($amenu['name'] == $menu['name']) {
743
							continue 2;
744
						}
745
					}
746
				} else {
747
					$config['installedpackages']['menu'] = array();
748
				}
749
				$config['installedpackages']['menu'][] = $menu;
750
			}
751
			update_status(gettext("done.") . "\n");
752
		}
753
		/* services */
754
		if (is_array($pkg_config['service'])) {
755
			update_status(gettext("Services... "));
756
			foreach ($pkg_config['service'] as $service) {
757
				if (is_array($config['installedpackages']['service'])) {
758
					foreach ($config['installedpackages']['service'] as $aservice) {
759
						if ($aservice['name'] == $service['name']) {
760
							continue 2;
761
						}
762
					}
763
				} else {
764
					$config['installedpackages']['service'] = array();
765
				}
766
				$config['installedpackages']['service'][] = $service;
767
			}
768
			update_status(gettext("done.") . "\n");
769
		}
770
	} else {
771
		pkg_debug("Unable to find config file\n");
772
		update_status(gettext("Loading package configuration... failed!") . "\n\n" . gettext("Installation aborted."));
773
		pkg_debug(gettext("Unable to load package configuration. Installation aborted.") ."\n");
774

    
775
		uninstall_package($package_name);
776
		write_config($changedesc);
777
		log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
778
		update_status(gettext("Failed to install package.") . "\n");
779
		return false;
780
	}
781

    
782
	/* set up package logging streams */
783
	if ($pkg_info['logging']) {
784
		system_syslogd_start();
785
	}
786

    
787
	update_status(gettext("Writing configuration... "));
788
	write_config($changedesc);
789
	log_error(sprintf(gettext("Successfully installed package: %s."), $pkg_info['name']));
790
	update_status(gettext("done.") . "\n");
791
	if ($pkg_info['after_install_info']) {
792
		update_status($pkg_info['after_install_info']);
793
	}
794

    
795
	return true;
796
}
797

    
798
function delete_package_xml($package_name, $when = "post-deinstall") {
799
	global $g, $config, $pkg_interface;
800

    
801
	conf_mount_rw();
802

    
803
	$pkgid = get_package_id($package_name);
804
	if ($pkgid == -1) {
805
		update_status(sprintf(gettext("The %s package is not installed.%sDeletion aborted."), $package_name, "\n\n"));
806
		ob_flush();
807
		sleep(1);
808
		conf_mount_ro();
809
		return;
810
	}
811
	pkg_debug(sprintf(gettext("Removing %s package... "), $package_name));
812
	update_status(sprintf(gettext("Removing %s components..."), $package_name) . "\n");
813
	/* parse package configuration */
814
	$packages = &$config['installedpackages']['package'];
815
	$menus =& $config['installedpackages']['menu'];
816
	$services = &$config['installedpackages']['service'];
817
	$pkg_info =& $packages[$pkgid];
818
	if (file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
819
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $packages[$pkgid]['configurationfile'], "packagegui");
820
		/* remove menu items */
821
		if (is_array($pkg_config['menu'])) {
822
			update_status(gettext("Menu items... "));
823
			if (is_array($pkg_config['menu']) && is_array($menus)) {
824
				foreach ($pkg_config['menu'] as $menu) {
825
					foreach ($menus as $key => $instmenu) {
826
						if ($instmenu['name'] == $menu['name']) {
827
							unset($menus[$key]);
828
							break;
829
						}
830
					}
831
				}
832
			}
833
			update_status(gettext("done.") . "\n");
834
		}
835
		/* remove services */
836
		if (is_array($pkg_config['service'])) {
837
			update_status(gettext("Services... "));
838
			if (is_array($pkg_config['service']) && is_array($services)) {
839
				foreach ($pkg_config['service'] as $service) {
840
					foreach ($services as $key => $instservice) {
841
						if ($instservice['name'] == $service['name']) {
842
							if (platform_booting() != true) {
843
								stop_service($service['name']);
844
							}
845
							if ($service['rcfile']) {
846
								$prefix = RCFILEPREFIX;
847
								if (!empty($service['prefix'])) {
848
									$prefix = $service['prefix'];
849
								}
850
								if (file_exists("{$prefix}{$service['rcfile']}")) {
851
									@unlink("{$prefix}{$service['rcfile']}");
852
								}
853
							}
854
							unset($services[$key]);
855
						}
856
					}
857
				}
858
			}
859
			update_status(gettext("done.") . "\n");
860
		}
861
		/*
862
		 * XXX: Otherwise inclusion of config.inc again invalidates actions taken.
863
		 *	Same is done during installation.
864
		 */
865
		write_config(sprintf(gettext("Intermediate config write during package removal for %s."), $package_name));
866

    
867
		/*
868
		 * If a require exists, include it.	 this will
869
		 * show us where an error exists in a package
870
		 * instead of making us blindly guess
871
		 */
872
		$missing_include = false;
873
		if ($pkg_config['include_file'] <> "") {
874
			update_status(gettext("Loading package instructions...") . "\n");
875
			if (file_exists($pkg_config['include_file'])) {
876
				pkg_debug("require_once(\"{$pkg_config['include_file']}\")\n");
877
				require_once($pkg_config['include_file']);
878
			} else {
879
				pkg_debug("Missing include {$pkg_config['include_file']}\n");
880
				$missing_include = true;
881
				update_status(sprintf(gettext("Include file %s could not be found for inclusion."), basename($pkg_config['include_file'])) . "\n");
882
			}
883
		}
884
		/* ermal
885
		 * NOTE: It is not possible to handle parse errors on eval.
886
		 * So we prevent it from being run at all to not interrupt all the other code.
887
		 */
888
		if ($when == "deinstall" && $missing_include == false) {
889
			/* evaluate this package's global functions and pre deinstall commands */
890
			if ($pkg_config['custom_php_global_functions'] <> "") {
891
				eval_once($pkg_config['custom_php_global_functions']);
892
			}
893
			if ($pkg_config['custom_php_pre_deinstall_command'] <> "") {
894
				eval_once($pkg_config['custom_php_pre_deinstall_command']);
895
			}
896
		}
897
		/* deinstall commands */
898
		if ($when == "post-deinstall" && $pkg_config['custom_php_deinstall_command'] <> "") {
899
			update_status(gettext("Deinstall commands... "));
900
			if ($missing_include == false) {
901
				eval_once($pkg_config['custom_php_deinstall_command']);
902
				update_status(gettext("done.") . "\n");
903
			} else {
904
				update_status("\n". gettext("Not executing custom deinstall hook because an include is missing.") . "\n");
905
			}
906
		}
907
	}
908
	/* syslog */
909
	$need_syslog_restart = false;
910
	if (is_array($pkg_info['logging']) && $pkg_info['logging']['logfilename'] <> "") {
911
		update_status(gettext("Syslog entries... "));
912
		@unlink("{$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
913
		update_status("done.\n");
914
		$need_syslog_restart = true;
915
	}
916

    
917
	if ($when == "post-deinstall") {
918
		/* remove config.xml entries */
919
		update_status(gettext("Configuration... "));
920
		unset($config['installedpackages']['package'][$pkgid]);
921
		update_status(gettext("done.") . "\n");
922
		write_config(sprintf(gettext("Removed %s package."), $package_name));
923
	}
924

    
925
	/* remove package entry from /etc/syslog.conf if needed */
926
	/* this must be done after removing the entries from config.xml */
927
	if ($need_syslog_restart) {
928
		system_syslogd_start();
929
	}
930

    
931
	conf_mount_ro();
932
}
933

    
934
/*
935
 * Used during upgrade process or retore backup process, verify all
936
 * packages installed in config.xml and install pkg accordingly
937
 */
938
function package_reinstall_all() {
939
	global $g, $config, $pkg_interface;
940

    
941
	if (!isset($config['installedpackages']['package']) ||
942
	    !is_array($config['installedpackages']['package'])) {
943
		return true;
944
	}
945

    
946
	$upgrade = (file_exists('/conf/needs_package_sync') && platform_booting());
947

    
948
	/* During boot after upgrade, wait for internet connection */
949
	if ($upgrade) {
950
		update_status(gettext("Waiting for internet connection to update pkg metadata and finish package reinstallation"));
951
		while (true) {
952
			if (pkg_update(true)) {
953
				break;
954
			}
955
			update_status('.');
956
			sleep(1);
957
		}
958
		update_status("\n");
959
	}
960

    
961
	$pkg_info = get_pkg_info();
962

    
963
	foreach ($config['installedpackages']['package'] as $package) {
964
		$found = false;
965
		$internal_name = get_package_internal_name($package);
966
		foreach ($pkg_info as $pkg) {
967
			pkg_remove_prefix($pkg['name']);
968
			if ($pkg['name'] == $internal_name) {
969
				$found = true;
970
				break;
971
			}
972
		}
973

    
974
		if (!$found) {
975
			if (!function_exists("file_notice")) {
976
				require_once("notices.inc");
977
			}
978

    
979
			file_notice(gettext("Package reinstall"),
980
			    sprintf(gettext("Package %s does not exist in current %s version and it has been removed."), $package['name'], $g['product_name']));
981
			uninstall_package($package['name']);
982
		}
983
	}
984

    
985
	/* Obsoleted packages were removed, lets reinstall all remaining */
986
	foreach ($config['installedpackages']['package'] as $package) {
987
		$internal_name = get_package_internal_name($package);
988
		pkg_install($g['pkg_prefix'] . $internal_name, true);
989
	}
990

    
991
	return true;
992
}
993

    
994
function stop_packages() {
995
	require_once("config.inc");
996
	require_once("functions.inc");
997
	require_once("filter.inc");
998
	require_once("shaper.inc");
999
	require_once("captiveportal.inc");
1000
	require_once("pkg-utils.inc");
1001
	require_once("pfsense-utils.inc");
1002
	require_once("service-utils.inc");
1003

    
1004
	global $config, $g;
1005

    
1006
	log_error(gettext("Stopping all packages."));
1007

    
1008
	$rcfiles = glob(RCFILEPREFIX . "*.sh");
1009
	if (!$rcfiles) {
1010
		$rcfiles = array();
1011
	} else {
1012
		$rcfiles = array_flip($rcfiles);
1013
		if (!$rcfiles) {
1014
			$rcfiles = array();
1015
		}
1016
	}
1017

    
1018
	if (is_array($config['installedpackages']['package'])) {
1019
		foreach ($config['installedpackages']['package'] as $package) {
1020
			echo " Stopping package {$package['name']}...";
1021
			$internal_name = get_package_internal_name($package);
1022
			stop_service($internal_name);
1023
			unset($rcfiles[RCFILEPREFIX . strtolower($internal_name) . ".sh"]);
1024
			echo "done.\n";
1025
		}
1026
	}
1027

    
1028
	foreach ($rcfiles as $rcfile => $number) {
1029
		$shell = @popen("/bin/sh", "w");
1030
		if ($shell) {
1031
			echo " Stopping {$rcfile}...";
1032
			if (!@fwrite($shell, "{$rcfile} stop >>/tmp/bootup_messages 2>&1")) {
1033
				if ($shell) {
1034
					pclose($shell);
1035
				}
1036
				$shell = @popen("/bin/sh", "w");
1037
			}
1038
			echo "done.\n";
1039
			pclose($shell);
1040
		}
1041
	}
1042
}
1043

    
1044
/* Identify which meta package is installed */
1045
function get_meta_pkg_name() {
1046
	global $g;
1047

    
1048
	/* XXX: Use pkg annotation */
1049
	if (is_pkg_installed($g['product_name'])) {
1050
		return $g['product_name'];
1051
	} else if (is_pkg_installed($g['product_name'] . '-vmware')) {
1052
		return $g['product_name'] . '-vmware';
1053
	}
1054
	return false;
1055
}
1056

    
1057
/* Identify which base package is installed */
1058
function get_base_pkg_name() {
1059
	global $g;
1060

    
1061
	/* XXX: Use pkg annotation */
1062
	if (is_pkg_installed($g['product_name'] . '-base-' . $g['platform'])) {
1063
		return $g['product_name'];
1064
		return $g['product_name'] . '-base-' . $g['platform'];
1065
	} else if (is_pkg_installed($g['product_name'] . '-base')) {
1066
		return $g['product_name'] . '-base';
1067
	}
1068
	return false;
1069
}
1070

    
1071
/* Verify if system needs upgrade (meta package or base) */
1072
function get_system_pkg_version() {
1073
	global $g;
1074

    
1075
	$base_pkg = get_base_pkg_name();
1076
	$meta_pkg = get_meta_pkg_name();
1077

    
1078
	if (!$base_pkg || !$meta_pkg) {
1079
		return false;
1080
	}
1081

    
1082
	$info = get_pkg_info($base_pkg);
1083
	$pkg_name = $base_pkg;
1084

    
1085
	$pkg_info = array();
1086
	foreach ($info as $item) {
1087
		if ($item['name'] == $base_pkg) {
1088
			$pkg_info = $item;
1089
		}
1090
	}
1091

    
1092
	if (empty($pkg_info) ||
1093
	    $pkg_info['version'] == $pkg_info['installed_version']) {
1094
		$info = get_pkg_info($meta_pkg);
1095
		$pkg_name = $meta_pkg;
1096

    
1097
		foreach ($info as $item) {
1098
			if ($item['name'] == $meta_pkg) {
1099
				$pkg_info = $item;
1100
			}
1101
		}
1102
	}
1103

    
1104
	if (empty($pkg_info)) {
1105
		return false;
1106
	}
1107

    
1108
	return array(
1109
	    'pkg_name'          => $pkg_name,
1110
	    'version'           => $pkg_info['version'],
1111
	    'installed_version' => $pkg_info['installed_version']
1112
	);
1113
}
1114

    
1115
/* Switch between stable and devel repos */
1116
function pkg_switch_repo($devel = false) {
1117
	global $g;
1118

    
1119
	$repo_stable = $g['product_name'] . '-repo';
1120
	$repo_devel = $g['product_name'] . '-repo-devel';
1121

    
1122
	if ($devel) {
1123
		$repo_target = $repo_devel;
1124
	} else {
1125
		$repo_target = $repo_stable;
1126
	}
1127

    
1128
	if (is_pkg_installed($repo_target)) {
1129
		/* It's already installed */
1130
		return true;
1131
	}
1132

    
1133
	/*
1134
	 * Since both install files in the same place, just
1135
	 * call pkg_install for target and current one will
1136
	 * be replaced
1137
	 */
1138
	return pkg_install($repo_target, true);
1139
}
1140

    
1141
?>
(40-40/65)