Project

General

Profile

Download (34.3 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
/* Validate if pkg name is valid */
87
function pkg_valid_name($pkgname) {
88
	global $g;
89

    
90
	$pattern = "/^{$g['pkg_prefix']}[a-zA-Z0-9\.\-_]+$/";
91
	return preg_match($pattern, $pkgname);
92
}
93

    
94
/* Remove pkg_prefix from package name if it's present */
95
function pkg_remove_prefix(&$pkg_name) {
96
	global $g;
97

    
98
	if (substr($pkg_name, 0, strlen($g['pkg_prefix'])) == $g['pkg_prefix']) {
99
		$pkg_name = substr($pkg_name, strlen($g['pkg_prefix']));
100
	}
101
}
102

    
103
/* Execute pkg update when it's necessary */
104
function pkg_update($force = false) {
105
	global $g;
106

    
107
	return pkg_call("update" . ($force ? " -f" : ""));
108
}
109

    
110
/* return an array with necessary environment vars for pkg */
111
function pkg_env($extra_env = array()) {
112
	global $config, $g;
113

    
114
	$pkg_env_vars = array(
115
		"LANG" => "C",
116
		"HTTP_USER_AGENT" => $user_agent,
117
		"ASSUME_ALWAYS_YES" => "true",
118
		"FETCH_TIMEOUT" => 5,
119
		"FETCH_RETRY" => 2
120
	);
121

    
122
	if (!empty($config['system']['proxyurl'])) {
123
		$http_proxy = $config['system']['proxyurl'];
124
		if (!empty($config['system']['proxyport'])) {
125
			$http_proxy .= ':' . $config['system']['proxyport'];
126
		}
127
		$pkg_env_vars['HTTP_PROXY'] = $http_proxy;
128
	}
129

    
130
	if ($g['platform'] == "nanobsd" ||
131
	    isset($config['system']['use_mfs_tmpvar'])) {
132
		$pkg_env_vars['PKG_DBDIR'] = '/root/var/db/pkg';
133
		$pkg_env_vars['PKG_CACHEDIR'] = '/root/var/cache/pkg';
134
	}
135

    
136
	foreach ($extra_env as $key => $value) {
137
		$pkg_env_vars[$key] = $value;
138
	}
139

    
140
	return $pkg_env_vars;
141
}
142

    
143
/* Execute a pkg call */
144
function pkg_call($params, $mute = false, $extra_env = array()) {
145
	global $g, $config;
146

    
147
	if (empty($params)) {
148
		return false;
149
	}
150

    
151
	$user_agent = $g['product_name'] . '/' . $g['product_version'];
152
	if (!isset($config['system']['do_not_send_host_uuid'])) {
153
		$user_agent .= ' : ' . get_single_sysctl('kern.hostuuid');
154
	}
155

    
156
	$descriptorspec = array(
157
		1 => array("pipe", "w"), /* stdout */
158
		2 => array("pipe", "w")	 /* stderr */
159
	);
160

    
161
	conf_mount_rw();
162

    
163
	pkg_debug("pkg_call(): {$params}\n");
164
	$process = proc_open("/usr/sbin/pkg {$params}", $descriptorspec, $pipes,
165
	    '/', pkg_env($extra_env));
166

    
167
	if (!is_resource($process)) {
168
		conf_mount_ro();
169
		return false;
170
	}
171

    
172
	stream_set_blocking($pipes[1], 0);
173
	stream_set_blocking($pipes[2], 0);
174

    
175
	/* XXX: should be a tunnable? */
176
	$timeout = 300; // seconds
177
	$error_log = '';
178

    
179
	do {
180
		$write = array();
181
		$read = array($pipes[1], $pipes[2]);
182
		$except = array();
183

    
184
		$stream = stream_select($read, $write, $except, null, $timeout);
185
		if ($stream !== FALSE && $stream > 0) {
186
			foreach ($read as $pipe) {
187
				$content = stream_get_contents($pipe);
188
				if ($content == '') {
189
					continue;
190
				}
191
				if ($pipe === $pipes[1]) {
192
					if (!$mute) {
193
						update_status($content);
194
					}
195
					flush();
196
				} else if ($pipe === $pipes[2]) {
197
					$error_log .= $content;
198
				}
199
			}
200
		}
201

    
202
		$status = proc_get_status($process);
203
	} while ($status['running']);
204

    
205
	fclose($pipes[1]);
206
	fclose($pipes[2]);
207
	proc_close($process);
208

    
209
	conf_mount_ro();
210

    
211
	$rc = $status['exitcode'];
212

    
213
	pkg_debug("pkg_call(): rc = {$rc}\n");
214
	if ($rc == 0) {
215
		return true;
216
	}
217

    
218
	pkg_debug("pkg_call(): error_log\n{$error_log}\n");
219
	if (!$mute) {
220
		update_status("\n\n" .  sprintf(gettext(
221
		    "ERROR!!! An error occurred on pkg execution (rc = %d) with parameters '%s':"),
222
		    $rc, $params) . "\n" . $error_log . "\n");
223
	}
224

    
225
	return false;
226
}
227

    
228
/* Execute pkg with $params, fill stdout and stderr and return pkg rc */
229
function pkg_exec($params, &$stdout, &$stderr, $extra_env = array()) {
230
	global $g, $config;
231

    
232
	if (empty($params)) {
233
		return -1;
234
	}
235

    
236
	$user_agent = $g['product_name'] . '/' . $g['product_version'];
237
	if (!isset($config['system']['do_not_send_host_uuid'])) {
238
		$user_agent .= ' : ' . get_single_sysctl('kern.hostuuid');
239
	}
240

    
241
	$descriptorspec = array(
242
		1 => array("pipe", "w"), /* stdout */
243
		2 => array("pipe", "w")	 /* stderr */
244
	);
245

    
246
	conf_mount_rw();
247

    
248
	pkg_debug("pkg_exec(): {$params}\n");
249
	$process = proc_open("/usr/sbin/pkg {$params}", $descriptorspec, $pipes,
250
	    '/', pkg_env($extra_env));
251

    
252
	if (!is_resource($process)) {
253
		conf_mount_ro();
254
		return -1;
255
	}
256

    
257
	$stdout = '';
258
	while (($l = fgets($pipes[1])) !== FALSE) {
259
		$stdout .= $l;
260
	}
261
	fclose($pipes[1]);
262

    
263
	$stderr = '';
264
	while (($l = fgets($pipes[2])) !== FALSE) {
265
		$stderr .= $l;
266
	}
267
	fclose($pipes[2]);
268

    
269
	conf_mount_ro();
270

    
271
	return proc_close($process);
272
}
273

    
274
/* Compare 2 pkg versions and return:
275
 * '=' - versions are the same
276
 * '>' - $v1 > $v2
277
 * '<' - $v1 < $v2
278
 * '?' - Error
279
 */
280
function pkg_version_compare($v1, $v2) {
281
	if (empty($v1) || empty($v2)) {
282
		return '?';
283
	}
284

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

    
287
	if ($rc != 0) {
288
		return '?';
289
	}
290

    
291
	return str_replace("\n", "", $stdout);
292
}
293

    
294
/* Check if package is installed */
295
function is_pkg_installed($pkg_name) {
296
	global $g;
297

    
298
	if (empty($pkg_name)) {
299
		return false;
300
	}
301

    
302
	return pkg_call("info -e " . $pkg_name, true);
303
}
304

    
305
/* Install package, $pkg_name should not contain prefix */
306
function pkg_install($pkg_name, $force = false) {
307
	global $g;
308
	$result = false;
309

    
310
	$shortname = $pkg_name;
311
	pkg_remove_prefix($shortname);
312

    
313
	$pkg_force = "";
314
	if ($force) {
315
		$pkg_force = "-f ";
316
	}
317

    
318
	pkg_debug("Installing package {$shortname}\n");
319
	if ($force || !is_pkg_installed($pkg_name)) {
320
		$result = pkg_call("install -y " . $pkg_force . $pkg_name);
321
		/* Cleanup cacke to free disk space */
322
		pkg_call("clean -y");
323
	}
324

    
325
	return $result;
326
}
327

    
328
/* Delete package from FreeBSD, $pkg_name should not contain prefix */
329
function pkg_delete($pkg_name) {
330
	global $g;
331

    
332
	$shortname = $pkg_name;
333
	pkg_remove_prefix($shortname);
334

    
335
	pkg_debug("Removing package {$shortname}\n");
336
	if (is_pkg_installed($pkg_name)) {
337
		pkg_call("delete -y " . $pkg_name);
338
		/* Cleanup unecessary dependencies */
339
		pkg_call("autoremove -y");
340
	}
341
}
342

    
343
/* Check if package is present in config.xml */
344
function is_package_installed($package_name) {
345
	return (get_package_id($package_name) != -1);
346
}
347

    
348
/* Find package array index */
349
function get_package_id($package_name) {
350
	global $config;
351

    
352
	if (!is_array($config['installedpackages']['package'])) {
353
		return -1;
354
	}
355

    
356
	foreach ($config['installedpackages']['package'] as $idx => $pkg) {
357
		if ($pkg['name'] == $package_name ||
358
		    get_package_internal_name($pkg) == $package_name) {
359
			return $idx;
360
		}
361
	}
362

    
363
	return -1;
364
}
365

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

    
376
// Get information about packages.
377
function get_pkg_info($pkgs = 'all', $info = 'all', $only_local = false) {
378
	global $g, $input_errors;
379

    
380
	$out = '';
381
	$err = '';
382

    
383
	unset($pkg_filter);
384
	if (is_array($pkgs)) {
385
		$pkg_filter = $pkgs;
386
		$pkgs = 'all';
387
	}
388

    
389
	if ($pkgs == 'all') {
390
		$pkgs = $g['pkg_prefix'];
391
	}
392

    
393
	if (!function_exists('is_subsystem_dirty')) {
394
		require_once("util.inc");
395
	}
396

    
397
	/* Do not run remote operations if pkg has a lock */
398
	if (is_subsystem_dirty('pkg')) {
399
		$only_local = true;
400
		$lock = false;
401
	} else {
402
		$lock = true;
403
	}
404

    
405
	$extra_param = "";
406
	if ($only_local) {
407
		$extra_param = "-U ";
408
	}
409

    
410
	if ($lock) {
411
		mark_subsystem_dirty('pkg');
412
	}
413
	$rc = pkg_exec("search {$extra_param}--raw-format json-compact " . $pkgs, $out, $err);
414
	if ($lock) {
415
		clear_subsystem_dirty('pkg');
416
	}
417

    
418
	if ($rc != 0) {
419
		update_status("\n" . gettext(
420
		    "ERROR: Error trying to get packages list. Aborting...")
421
		    . "\n");
422
		update_status($err);
423
		$input_errors[] =  gettext("ERROR: Error trying to get packages list. Aborting...") . "\n";
424
		$input_errors[] =  $err;
425
		return array();
426
	}
427

    
428
	$result = array();
429
	$pkgs_info = explode("\n", $out);
430
	foreach ($pkgs_info as $pkg_info_json) {
431
		$pkg_info = json_decode($pkg_info_json, true);
432
		if (!isset($pkg_info['name'])) {
433
			continue;
434
		}
435

    
436
		if (isset($pkg_filter) && !in_array($pkg_info['name'], $pkg_filter)) {
437
			continue;
438
		}
439

    
440
		$pkg_info['shortname'] = $pkg_info['name'];
441
		pkg_remove_prefix($pkg_info['shortname']);
442

    
443
		/* XXX: Add it to globals.inc? */
444
		$pkg_info['changeloglink'] =
445
		    "https://github.com/pfsense/FreeBSD-ports/commits/devel/" .
446
		    $pkg_info['categories'][0] . '/' . $pkg_info['name'];
447

    
448
		if (is_pkg_installed($pkg_info['name'])) {
449
			$pkg_info['installed'] = true;
450

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

    
453
			if ($rc != 0) {
454
				update_status("\n" . gettext(
455
				    "ERROR: Error trying to get package version. Aborting...")
456
				    . "\n");
457
				update_status($err);
458
				$input_errors[] =  gettext("ERROR: Error trying to get package version. Aborting...") . "\n";
459
				$input_errors[] =  $err;
460
				return array();
461
			}
462

    
463
			$pkg_info['installed_version'] = str_replace("\n", "", $out);
464
		} else if (is_package_installed($pkg_info['shortname'])) {
465
			$pkg_info['broken'] = true;
466
		}
467

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

    
470
		$result[] = $pkg_info;
471
		unset($pkg_info);
472
	}
473

    
474
	/* Sort result alphabetically */
475
	usort($result, function($a, $b) {
476
		return(strcasecmp ($a['name'], $b['name']));
477
	});
478

    
479
	return $result;
480
}
481

    
482
/*
483
 * If binary pkg is installed but post-install tasks were not
484
 * executed yet, do it now.
485
 * This scenario can happen when a pkg is pre-installed during
486
 * build phase, and at this point, cannot find a running system
487
 * to register itself in config.xml and also execute custom
488
 * install functions
489
 */
490
function register_all_installed_packages() {
491
	global $g, $config, $pkg_interface;
492

    
493
	$pkg_info = get_pkg_info('all', 'all', true);
494

    
495
	foreach ($pkg_info as $pkg) {
496
		if (!isset($pkg['installed'])) {
497
			continue;
498
		}
499

    
500
		pkg_remove_prefix($pkg['name']);
501

    
502
		if (is_package_installed($pkg['name'])) {
503
			continue;
504
		}
505

    
506
		update_status(sprintf(gettext(
507
		    "Running last steps of %s installation.") . "\n",
508
		    $pkg['name']));
509
		install_package_xml($pkg['name']);
510
	}
511
}
512

    
513
/*
514
 * resync_all_package_configs() Force packages to setup their configuration and rc.d files.
515
 * This function may also print output to the terminal indicating progress.
516
 */
517
function resync_all_package_configs($show_message = false) {
518
	global $config, $pkg_interface, $g;
519

    
520
	log_error(gettext("Resyncing configuration for all packages."));
521

    
522
	if (!is_array($config['installedpackages']['package'])) {
523
		return;
524
	}
525

    
526
	if ($show_message == true) {
527
		echo "Syncing packages:";
528
	}
529

    
530
	conf_mount_rw();
531

    
532
	foreach ($config['installedpackages']['package'] as $idx => $package) {
533
		if (empty($package['name'])) {
534
			continue;
535
		}
536
		if ($show_message == true) {
537
			echo " " . $package['name'];
538
		}
539
		if (platform_booting() != true) {
540
			stop_service(get_package_internal_name($package));
541
		}
542
		sync_package($package['name']);
543
		update_status(gettext("Syncing packages...") . "\n");
544
	}
545

    
546
	if ($show_message == true) {
547
		echo " done.\n";
548
	}
549

    
550
	@unlink("/conf/needs_package_sync");
551
	conf_mount_ro();
552
}
553

    
554
function uninstall_package($package_name) {
555
	global $config;
556

    
557
	$internal_name = $package_name;
558
	$id = get_package_id($package_name);
559
	if ($id >= 0) {
560
		$internal_name = get_package_internal_name($config['installedpackages']['package'][$id]);
561
		stop_service($internal_name);
562
	}
563
	$pkg_name = $g['pkg_prefix'] . $internal_name;
564

    
565
	if (is_pkg_installed($pkg_name)) {
566
		update_status(gettext("Removing package...") . "\n");
567
		pkg_delete($pkg_name);
568
	} else {
569
		delete_package_xml($package_name);
570
	}
571

    
572
	update_status(gettext("done.") . "\n");
573
}
574

    
575
/* Run <custom_php_resync_config_command> */
576
function sync_package($package_name) {
577
	global $config, $builder_package_install;
578

    
579
	// If this code is being called by pfspkg_installer
580
	// which the builder system uses then return (ignore).
581
	if ($builder_package_install) {
582
		return;
583
	}
584

    
585
	if (empty($config['installedpackages']['package'])) {
586
		return;
587
	}
588

    
589
	if (($pkg_id = get_package_id($package_name)) == -1) {
590
		return; // This package doesn't really exist - exit the function.
591
	}
592

    
593
	if (!is_array($config['installedpackages']['package'][$pkg_id])) {
594
		return;	 // No package belongs to the pkg_id passed to this function.
595
	}
596

    
597
	$package =& $config['installedpackages']['package'][$pkg_id];
598
	if (!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
599
		log_error(sprintf(gettext("The %s package is missing its configuration file and must be reinstalled."), $package['name']));
600
		delete_package_xml($package['name']);
601
		return;
602
	}
603

    
604
	$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
605
	if (isset($pkg_config['nosync'])) {
606
		return;
607
	}
608

    
609
	/* Bring in package include files */
610
	if (!empty($pkg_config['include_file'])) {
611
		$include_file = $pkg_config['include_file'];
612
		if (file_exists($include_file)) {
613
			require_once($include_file);
614
		} else {
615
			log_error(sprintf(gettext('Reinstalling package %1$s because its include file(%2$s) is missing!'), $package['name'], $include_file));
616
			uninstall_package($package['name']);
617
			if (install_package($package['name']) != 0) {
618
				log_error(sprintf(gettext("Reinstalling package %s failed. Take appropriate measures!!!"), $package['name']));
619
				return;
620
			}
621
			if (file_exists($include_file)) {
622
				require_once($include_file);
623
			} else {
624
				return;
625
			}
626
		}
627
	}
628

    
629
	if (!empty($pkg_config['custom_php_global_functions'])) {
630
		eval($pkg_config['custom_php_global_functions']);
631
	}
632
	if (!empty($pkg_config['custom_php_resync_config_command'])) {
633
		eval($pkg_config['custom_php_resync_config_command']);
634
	}
635
}
636

    
637
/* Read info.xml installed by package and return an array */
638
function read_package_config($package_name) {
639
	global $g;
640

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

    
643
	if (!file_exists($pkg_info_xml)) {
644
		return false;
645
	}
646

    
647
	$pkg_info = parse_xml_config_pkg($pkg_info_xml, 'pfsensepkgs');
648

    
649
	if (empty($pkg_info)) {
650
		return false;
651
	}
652

    
653
	/* it always returns an array with 1 item */
654
	return $pkg_info['package'][0];
655
}
656

    
657
/* Read package configurationfile and return an array */
658
function read_package_configurationfile($package_name) {
659
	global $config, $g;
660

    
661
	$pkg_config = array();
662
	$id = get_package_id($package_name);
663

    
664
	if ($id < 0 || !isset($config['installedpackages']['package'][$id]['configurationfile'])) {
665
		return $pkg_config;
666
	}
667

    
668
	$pkg_configurationfile = $config['installedpackages']['package'][$id]['configurationfile'];
669

    
670
	if (empty($pkg_configurationfile) || !file_exists('/usr/local/pkg/' . $pkg_configurationfile)) {
671
		return $pkg_config;
672
	}
673

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

    
676
	return $pkg_config;
677
}
678

    
679
function get_after_install_info($package_name) {
680
	$pkg_config = read_package_config($package_name);
681

    
682
	if (isset($pkg_config['after_install_info'])) {
683
		return $pkg_config['after_install_info'];
684
	}
685

    
686
	return '';
687
}
688

    
689
function eval_once($toeval) {
690
	global $evaled;
691
	if (!$evaled) {
692
		$evaled = array();
693
	}
694
	$evalmd5 = md5($toeval);
695
	if (!in_array($evalmd5, $evaled)) {
696
		@eval($toeval);
697
		$evaled[] = $evalmd5;
698
	}
699
	return;
700
}
701

    
702
function install_package_xml($package_name) {
703
	global $g, $config, $pkg_interface;
704

    
705
	if (($pkg_info = read_package_config($package_name)) == false) {
706
		return false;
707
	}
708

    
709
	/* safe side. Write config below will send to ro again. */
710
	conf_mount_rw();
711

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

    
715
	/* add package information to config.xml */
716
	$pkgid = get_package_id($pkg_info['name']);
717
	update_status(gettext("Saving updated package information...") . "\n");
718
	if ($pkgid == -1) {
719
		$config['installedpackages']['package'][] = $pkg_info;
720
		$changedesc = sprintf(gettext("Installed %s package."), $pkg_info['name']);
721
		$to_output = gettext("done.") . "\n";
722
	} else {
723
		$config['installedpackages']['package'][$pkgid] = $pkg_info;
724
		$changedesc = sprintf(gettext("Overwrote previous installation of %s."), $pkg_info['name']);
725
		$to_output = gettext("overwrite!") . "\n";
726
	}
727
	unlink_if_exists('/conf/needs_package_sync');
728
	write_config(sprintf(gettext("Intermediate config write during package install for %s."), $pkg_info['name']));
729
	conf_mount_ro();
730
	update_status($to_output);
731

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

    
735
		uninstall_package($package_name);
736
		write_config($changedesc);
737
		log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
738
		update_status(gettext("Failed to install package.") . "\n");
739
		return false;
740
	}
741

    
742
	if (file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
743
		update_status(gettext("Loading package configuration... "));
744
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $pkg_info['configurationfile'], "packagegui");
745
		update_status(gettext("done.") . "\n");
746
		update_status(gettext("Configuring package components...") . "\n");
747
		if (!empty($pkg_config['filter_rules_needed'])) {
748
			$config['installedpackages']['package'][$pkgid]['filter_rule_function'] = $pkg_config['filter_rules_needed'];
749
		}
750
		/* modify system files */
751

    
752
		/* if a require exists, include it.  this will
753
		 * show us where an error exists in a package
754
		 * instead of making us blindly guess
755
		 */
756
		$missing_include = false;
757
		if ($pkg_config['include_file'] <> "") {
758
			update_status(gettext("Loading package instructions...") . "\n");
759
			if (file_exists($pkg_config['include_file'])) {
760
				pkg_debug("require_once('{$pkg_config['include_file']}')\n");
761
				require_once($pkg_config['include_file']);
762
			} else {
763
				pkg_debug("Missing include {$pkg_config['include_file']}\n");
764
				$missing_include = true;
765
				update_status(sprintf(gettext("Include %s is missing!"), basename($pkg_config['include_file'])) . "\n");
766

    
767
				uninstall_package($package_name);
768
				write_config($changedesc);
769
				log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
770
				update_status(gettext("Failed to install package.") . "\n");
771
				return false;
772
			}
773
		}
774

    
775
		/* custom commands */
776
		update_status(gettext("Custom commands...") . "\n");
777
		if ($missing_include == false) {
778
			if ($pkg_config['custom_php_global_functions'] <> "") {
779
				update_status(gettext("Executing custom_php_global_functions()..."));
780
				eval_once($pkg_config['custom_php_global_functions']);
781
				update_status(gettext("done.") . "\n");
782
			}
783
			if ($pkg_config['custom_php_install_command']) {
784
				update_status(gettext("Executing custom_php_install_command()..."));
785
				eval_once($pkg_config['custom_php_install_command']);
786
				update_status(gettext("done.") . "\n");
787
			}
788
			if ($pkg_config['custom_php_resync_config_command'] <> "") {
789
				update_status(gettext("Executing custom_php_resync_config_command()..."));
790
				eval_once($pkg_config['custom_php_resync_config_command']);
791
				update_status(gettext("done.") . "\n");
792
			}
793
		}
794
		/* sidebar items */
795
		if (is_array($pkg_config['menu'])) {
796
			update_status(gettext("Menu items... "));
797
			foreach ($pkg_config['menu'] as $menu) {
798
				if (is_array($config['installedpackages']['menu'])) {
799
					foreach ($config['installedpackages']['menu'] as $amenu) {
800
						if ($amenu['name'] == $menu['name']) {
801
							continue 2;
802
						}
803
					}
804
				} else {
805
					$config['installedpackages']['menu'] = array();
806
				}
807
				$config['installedpackages']['menu'][] = $menu;
808
			}
809
			update_status(gettext("done.") . "\n");
810
		}
811
		/* services */
812
		if (is_array($pkg_config['service'])) {
813
			update_status(gettext("Services... "));
814
			foreach ($pkg_config['service'] as $service) {
815
				if (is_array($config['installedpackages']['service'])) {
816
					foreach ($config['installedpackages']['service'] as $aservice) {
817
						if ($aservice['name'] == $service['name']) {
818
							continue 2;
819
						}
820
					}
821
				} else {
822
					$config['installedpackages']['service'] = array();
823
				}
824
				$config['installedpackages']['service'][] = $service;
825
			}
826
			update_status(gettext("done.") . "\n");
827
		}
828
	} else {
829
		pkg_debug("Unable to find config file\n");
830
		update_status(gettext("Loading package configuration... failed!") . "\n\n" . gettext("Installation aborted."));
831
		pkg_debug(gettext("Unable to load package configuration. Installation aborted.") ."\n");
832

    
833
		uninstall_package($package_name);
834
		write_config($changedesc);
835
		log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
836
		update_status(gettext("Failed to install package.") . "\n");
837
		return false;
838
	}
839

    
840
	/* set up package logging streams */
841
	if ($pkg_info['logging']) {
842
		system_syslogd_start();
843
	}
844

    
845
	update_status(gettext("Writing configuration... "));
846
	write_config($changedesc);
847
	log_error(sprintf(gettext("Successfully installed package: %s."), $pkg_info['name']));
848
	update_status(gettext("done.") . "\n");
849
	if ($pkg_info['after_install_info']) {
850
		update_status($pkg_info['after_install_info']);
851
	}
852

    
853
	return true;
854
}
855

    
856
function delete_package_xml($package_name, $when = "post-deinstall") {
857
	global $g, $config, $pkg_interface;
858

    
859
	conf_mount_rw();
860

    
861
	$pkgid = get_package_id($package_name);
862
	if ($pkgid == -1) {
863
		update_status(sprintf(gettext("The %s package is not installed.%sDeletion aborted."), $package_name, "\n\n"));
864
		ob_flush();
865
		sleep(1);
866
		conf_mount_ro();
867
		return;
868
	}
869
	pkg_debug(sprintf(gettext("Removing %s package... "), $package_name));
870
	update_status(sprintf(gettext("Removing %s components..."), $package_name) . "\n");
871
	/* parse package configuration */
872
	$packages = &$config['installedpackages']['package'];
873
	$menus =& $config['installedpackages']['menu'];
874
	$services = &$config['installedpackages']['service'];
875
	$pkg_info =& $packages[$pkgid];
876
	if (file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
877
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $packages[$pkgid]['configurationfile'], "packagegui");
878
		/* remove menu items */
879
		if (is_array($pkg_config['menu'])) {
880
			update_status(gettext("Menu items... "));
881
			if (is_array($pkg_config['menu']) && is_array($menus)) {
882
				foreach ($pkg_config['menu'] as $menu) {
883
					foreach ($menus as $key => $instmenu) {
884
						if ($instmenu['name'] == $menu['name']) {
885
							unset($menus[$key]);
886
							break;
887
						}
888
					}
889
				}
890
			}
891
			update_status(gettext("done.") . "\n");
892
		}
893
		/* remove services */
894
		if (is_array($pkg_config['service'])) {
895
			update_status(gettext("Services... "));
896
			if (is_array($pkg_config['service']) && is_array($services)) {
897
				foreach ($pkg_config['service'] as $service) {
898
					foreach ($services as $key => $instservice) {
899
						if ($instservice['name'] == $service['name']) {
900
							if (platform_booting() != true) {
901
								stop_service($service['name']);
902
							}
903
							if ($service['rcfile']) {
904
								$prefix = RCFILEPREFIX;
905
								if (!empty($service['prefix'])) {
906
									$prefix = $service['prefix'];
907
								}
908
								if (file_exists("{$prefix}{$service['rcfile']}")) {
909
									@unlink("{$prefix}{$service['rcfile']}");
910
								}
911
							}
912
							unset($services[$key]);
913
						}
914
					}
915
				}
916
			}
917
			update_status(gettext("done.") . "\n");
918
		}
919
		/*
920
		 * XXX: Otherwise inclusion of config.inc again invalidates actions taken.
921
		 *	Same is done during installation.
922
		 */
923
		write_config(sprintf(gettext("Intermediate config write during package removal for %s."), $package_name));
924

    
925
		/*
926
		 * If a require exists, include it.	 this will
927
		 * show us where an error exists in a package
928
		 * instead of making us blindly guess
929
		 */
930
		$missing_include = false;
931
		if ($pkg_config['include_file'] <> "") {
932
			update_status(gettext("Loading package instructions...") . "\n");
933
			if (file_exists($pkg_config['include_file'])) {
934
				pkg_debug("require_once(\"{$pkg_config['include_file']}\")\n");
935
				require_once($pkg_config['include_file']);
936
			} else {
937
				pkg_debug("Missing include {$pkg_config['include_file']}\n");
938
				$missing_include = true;
939
				update_status(sprintf(gettext("Include file %s could not be found for inclusion."), basename($pkg_config['include_file'])) . "\n");
940
			}
941
		}
942
		/* ermal
943
		 * NOTE: It is not possible to handle parse errors on eval.
944
		 * So we prevent it from being run at all to not interrupt all the other code.
945
		 */
946
		if ($when == "deinstall" && $missing_include == false) {
947
			/* evaluate this package's global functions and pre deinstall commands */
948
			if ($pkg_config['custom_php_global_functions'] <> "") {
949
				eval_once($pkg_config['custom_php_global_functions']);
950
			}
951
			if ($pkg_config['custom_php_pre_deinstall_command'] <> "") {
952
				eval_once($pkg_config['custom_php_pre_deinstall_command']);
953
			}
954
		}
955
		/* deinstall commands */
956
		if ($when == "post-deinstall" && $pkg_config['custom_php_deinstall_command'] <> "") {
957
			update_status(gettext("Deinstall commands... "));
958
			if ($missing_include == false) {
959
				eval_once($pkg_config['custom_php_deinstall_command']);
960
				update_status(gettext("done.") . "\n");
961
			} else {
962
				update_status("\n". gettext("Not executing custom deinstall hook because an include is missing.") . "\n");
963
			}
964
		}
965
	}
966
	/* syslog */
967
	$need_syslog_restart = false;
968
	if (is_array($pkg_info['logging']) && $pkg_info['logging']['logfilename'] <> "") {
969
		update_status(gettext("Syslog entries... "));
970
		@unlink("{$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
971
		update_status("done.\n");
972
		$need_syslog_restart = true;
973
	}
974

    
975
	if ($when == "post-deinstall") {
976
		/* remove config.xml entries */
977
		update_status(gettext("Configuration... "));
978
		unset($config['installedpackages']['package'][$pkgid]);
979
		update_status(gettext("done.") . "\n");
980
		write_config(sprintf(gettext("Removed %s package."), $package_name));
981
	}
982

    
983
	/* remove package entry from /etc/syslog.conf if needed */
984
	/* this must be done after removing the entries from config.xml */
985
	if ($need_syslog_restart) {
986
		system_syslogd_start();
987
	}
988

    
989
	conf_mount_ro();
990
}
991

    
992
/*
993
 * Used during upgrade process or retore backup process, verify all
994
 * packages installed in config.xml and install pkg accordingly
995
 */
996
function package_reinstall_all() {
997
	global $g, $config, $pkg_interface;
998

    
999
	$upgrade = (file_exists('/conf/needs_package_sync') && platform_booting());
1000

    
1001
	if ((!isset($config['installedpackages']['package']) ||
1002
	    !is_array($config['installedpackages']['package'])) && !$upgrade) {
1003
		return true;
1004
	}
1005

    
1006
	/* During boot after upgrade, wait for internet connection */
1007
	if ($upgrade) {
1008
		update_status(gettext("Waiting for Internet connection to update pkg metadata and finish package reinstallation"));
1009
		$ntries = 3;
1010
		while ($ntries > 0) {
1011
			if (pkg_update(true)) {
1012
				break;
1013
			}
1014
			update_status('.');
1015
			sleep(1);
1016
			$ntries--;
1017
		}
1018
		update_status("\n");
1019

    
1020
		if ($ntries == 0) {
1021
			file_notice(gettext("Package reinstall"),
1022
			    gettext("Package reinstall process was ABORTED due to lack of internet connectivity"));
1023
			return false;
1024
		}
1025
	}
1026

    
1027
	$pkg_info = get_pkg_info();
1028

    
1029
	if ($upgrade &&
1030
	    file_exists("{$g['cf_conf_path']}/packages_to_reinstall_after_upgrade.txt")) {
1031
		$package_list = file("{$g['cf_conf_path']}/packages_to_reinstall_after_upgrade.txt",
1032
		    FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
1033
		unlink_if_exists("{$g['cf_conf_path']}/packages_to_reinstall_after_upgrade.txt");
1034
	} else {
1035
		$package_list = array();
1036
		foreach ($config['installedpackages']['package'] as $package) {
1037
			$package_list[] = get_package_internal_name($package);
1038
		}
1039
	}
1040

    
1041
	foreach ($package_list as $package) {
1042
		$found = false;
1043
		foreach ($pkg_info as $pkg) {
1044
			pkg_remove_prefix($pkg['name']);
1045
			if ($pkg['name'] == $package) {
1046
				pkg_install($g['pkg_prefix'] . $package, true);
1047
				$found = true;
1048
				break;
1049
			}
1050
		}
1051

    
1052
		if (!$found) {
1053
			if (!function_exists("file_notice")) {
1054
				require_once("notices.inc");
1055
			}
1056

    
1057
			file_notice(gettext("Package reinstall"),
1058
			    sprintf(gettext("Package %s does not exist in current %s version and it has been removed."),
1059
			    $package, $g['product_name']));
1060
			uninstall_package($package);
1061
		}
1062
	}
1063

    
1064
	return true;
1065
}
1066

    
1067
function stop_packages() {
1068
	require_once("config.inc");
1069
	require_once("functions.inc");
1070
	require_once("filter.inc");
1071
	require_once("shaper.inc");
1072
	require_once("captiveportal.inc");
1073
	require_once("pkg-utils.inc");
1074
	require_once("pfsense-utils.inc");
1075
	require_once("service-utils.inc");
1076

    
1077
	global $config, $g;
1078

    
1079
	log_error(gettext("Stopping all packages."));
1080

    
1081
	$rcfiles = glob(RCFILEPREFIX . "*.sh");
1082
	if (!$rcfiles) {
1083
		$rcfiles = array();
1084
	} else {
1085
		$rcfiles = array_flip($rcfiles);
1086
		if (!$rcfiles) {
1087
			$rcfiles = array();
1088
		}
1089
	}
1090

    
1091
	if (is_array($config['installedpackages']['package'])) {
1092
		foreach ($config['installedpackages']['package'] as $package) {
1093
			echo " Stopping package {$package['name']}...";
1094
			$internal_name = get_package_internal_name($package);
1095
			stop_service($internal_name);
1096
			unset($rcfiles[RCFILEPREFIX . strtolower($internal_name) . ".sh"]);
1097
			echo "done.\n";
1098
		}
1099
	}
1100

    
1101
	foreach ($rcfiles as $rcfile => $number) {
1102
		$shell = @popen("/bin/sh", "w");
1103
		if ($shell) {
1104
			echo " Stopping {$rcfile}...";
1105
			if (!@fwrite($shell, "{$rcfile} stop >>/tmp/bootup_messages 2>&1")) {
1106
				if ($shell) {
1107
					pclose($shell);
1108
				}
1109
				$shell = @popen("/bin/sh", "w");
1110
			}
1111
			echo "done.\n";
1112
			pclose($shell);
1113
		}
1114
	}
1115
}
1116

    
1117
/* Identify which meta package is installed */
1118
function get_meta_pkg_name() {
1119
	global $g;
1120

    
1121
	/* XXX: Use pkg annotation */
1122
	if (is_pkg_installed($g['product_name'])) {
1123
		return $g['product_name'];
1124
	} else if (is_pkg_installed($g['product_name'] . '-vmware')) {
1125
		return $g['product_name'] . '-vmware';
1126
	}
1127
	return false;
1128
}
1129

    
1130
/* Identify which base package is installed */
1131
function get_base_pkg_name() {
1132
	global $g;
1133

    
1134
	/* XXX: Use pkg annotation */
1135
	if (is_pkg_installed($g['product_name'] . '-base-' . $g['platform'])) {
1136
		return $g['product_name'] . '-base-' . $g['platform'];
1137
	} else if (is_pkg_installed($g['product_name'] . '-base')) {
1138
		return $g['product_name'] . '-base';
1139
	}
1140
	return false;
1141
}
1142

    
1143
/* Verify if system needs upgrade (meta package or base) */
1144
function get_system_pkg_version($baseonly = false) {
1145
	global $g;
1146

    
1147
	$base_pkg = get_base_pkg_name();
1148
	$meta_pkg = get_meta_pkg_name();
1149

    
1150
	if (!$base_pkg || !$meta_pkg) {
1151
		return false;
1152
	}
1153

    
1154
	$info = get_pkg_info($base_pkg);
1155
	$pkg_name = $base_pkg;
1156

    
1157
	$pkg_info = array();
1158
	foreach ($info as $item) {
1159
		if ($item['name'] == $base_pkg) {
1160
			$pkg_info = $item;
1161
		}
1162
	}
1163

    
1164
	if (empty($pkg_info) || (!$baseonly && ($pkg_info['version'] == $pkg_info['installed_version']))) {
1165
		$info = get_pkg_info($meta_pkg);
1166
		$pkg_name = $meta_pkg;
1167

    
1168
		foreach ($info as $item) {
1169
			if ($item['name'] == $meta_pkg) {
1170
				$pkg_info = $item;
1171
			}
1172
		}
1173
	}
1174

    
1175
	if (empty($pkg_info)) {
1176
		return false;
1177
	}
1178

    
1179
	return array(
1180
	    'pkg_name'          => $pkg_name,
1181
	    'version'           => $pkg_info['version'],
1182
	    'installed_version' => $pkg_info['installed_version']
1183
	);
1184
}
1185

    
1186
/* List available repos */
1187
function pkg_list_repos() {
1188
	global $g;
1189

    
1190
	$path = "/usr/local/share/{$g['product_name']}/pkg/repos";
1191

    
1192
	$default_descr = @file_get_contents($path . "/{$g['product_name']}-repo.descr");
1193

    
1194
	$default = array(
1195
	    'name' => 'Default',
1196
	    'path' => $path . "/{$g['product_name']}-repo.conf",
1197
	    'descr' => $default_descr
1198
	);
1199

    
1200
	$result = array($default);
1201

    
1202
	$conf_files = glob("{$path}/{$g['product_name']}-repo-*.conf");
1203
	foreach ($conf_files as $conf_file) {
1204
		$descr_file = preg_replace('/.conf$/', '.descr', $conf_file);
1205
		if (file_exists($descr_file)) {
1206
			$descr_content = file($descr_file);
1207
			$descr = chop($descr_content[0]);
1208
		} else {
1209
			$descr = 'Unknown';
1210
		}
1211
		if (!preg_match('/-repo-(.*).conf/', $conf_file, $matches)) {
1212
			continue;
1213
		}
1214
		$entry = array(
1215
		    'name' => ucfirst(strtolower($matches[1])),
1216
		    'path' => $conf_file,
1217
		    'descr' => $descr
1218
		);
1219
		$result[] = $entry;
1220
	}
1221

    
1222
	return $result;
1223
}
1224

    
1225
/* Switch between stable and devel repos */
1226
function pkg_switch_repo($path) {
1227
	global $g;
1228

    
1229
	safe_mkdir("/usr/local/etc/pkg/repos");
1230
	@unlink("/usr/local/etc/pkg/repos/{$g['product_name']}.conf");
1231
	@symlink($path, "/usr/local/etc/pkg/repos/{$g['product_name']}.conf");
1232

    
1233
	return pkg_update(true);
1234
}
1235

    
1236
?>
(40-40/65)