Project

General

Profile

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

    
23
require_once("globals.inc");
24
require_once("service-utils.inc");
25

    
26
if (file_exists("/cf/conf/use_xmlreader")) {
27
	require_once("xmlreader.inc");
28
} else {
29
	require_once("xmlparse.inc");
30
}
31

    
32
require_once("pfsense-utils.inc");
33

    
34
if (!function_exists("pkg_debug")) {
35
	/* set up logging if needed */
36
	function pkg_debug($msg) {
37
		global $g, $debug, $fd_log;
38

    
39
		if (!$debug) {
40
			return;
41
		}
42

    
43
		if (!$fd_log) {
44
			if (!$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_debug.log", "w")) {
45
				update_status(gettext("Warning, could not open log for writing.") . "\n");
46
			}
47
		}
48
		@fwrite($fd_log, $msg);
49
	}
50
}
51

    
52
/* Validate if pkg name is valid */
53
function pkg_valid_name($pkgname) {
54
	global $g;
55

    
56
	$pattern = "/^{$g['pkg_prefix']}[a-zA-Z0-9\.\-_]+$/";
57
	return preg_match($pattern, $pkgname);
58
}
59

    
60
/* Remove pkg_prefix from package name if it's present */
61
function pkg_remove_prefix(&$pkg_name) {
62
	global $g;
63

    
64
	if (substr($pkg_name, 0, strlen($g['pkg_prefix'])) == $g['pkg_prefix']) {
65
		$pkg_name = substr($pkg_name, strlen($g['pkg_prefix']));
66
	}
67
}
68

    
69
/* Execute pkg update when it's necessary */
70
function pkg_update($force = false) {
71
	global $g;
72

    
73
	return pkg_call("update" . ($force ? " -f" : ""));
74
}
75

    
76
/* return an array with necessary environment vars for pkg */
77
function pkg_env($extra_env = array()) {
78
	global $config, $g;
79

    
80
	$pkg_env_vars = array(
81
		"LANG" => "C",
82
		"HTTP_USER_AGENT" => $user_agent,
83
		"ASSUME_ALWAYS_YES" => "true",
84
		"FETCH_TIMEOUT" => 5,
85
		"FETCH_RETRY" => 2
86
	);
87

    
88
	if (!empty($config['system']['proxyurl'])) {
89
		$http_proxy = $config['system']['proxyurl'];
90
		if (!empty($config['system']['proxyport'])) {
91
			$http_proxy .= ':' . $config['system']['proxyport'];
92
		}
93
		$pkg_env_vars['HTTP_PROXY'] = $http_proxy;
94

    
95
		if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
96
			$pkg_env_vars['HTTP_PROXY_AUTH'] = "basic:*:{$config['system']['proxyuser']}:{$config['system']['proxypass']}";
97
		}
98
	}
99

    
100
	if (isset($config['system']['use_mfs_tmpvar'])) {
101
		$pkg_env_vars['PKG_DBDIR'] = '/root/var/db/pkg';
102
		$pkg_env_vars['PKG_CACHEDIR'] = '/root/var/cache/pkg';
103
	}
104

    
105
	foreach ($extra_env as $key => $value) {
106
		$pkg_env_vars[$key] = $value;
107
	}
108

    
109
	return $pkg_env_vars;
110
}
111

    
112
/* Execute a pkg call */
113
function pkg_call($params, $mute = false, $extra_env = array()) {
114
	global $g, $config;
115

    
116
	if (empty($params)) {
117
		return false;
118
	}
119

    
120
	$user_agent = $g['product_name'] . '/' . $g['product_version'];
121
	if (!isset($config['system']['do_not_send_host_uuid'])) {
122
		$user_agent .= ' : ' . get_single_sysctl('kern.hostuuid');
123
	}
124

    
125
	$descriptorspec = array(
126
		1 => array("pipe", "w"), /* stdout */
127
		2 => array("pipe", "w")	 /* stderr */
128
	);
129

    
130

    
131
	pkg_debug("pkg_call(): {$params}\n");
132
	$process = proc_open("/usr/sbin/pkg {$params}", $descriptorspec, $pipes,
133
	    '/', pkg_env($extra_env));
134

    
135
	if (!is_resource($process)) {
136
		return false;
137
	}
138

    
139
	stream_set_blocking($pipes[1], 0);
140
	stream_set_blocking($pipes[2], 0);
141

    
142
	/* XXX: should be a tunnable? */
143
	$timeout = 300; // seconds
144
	$error_log = '';
145

    
146
	do {
147
		$write = array();
148
		$read = array($pipes[1], $pipes[2]);
149
		$except = array();
150

    
151
		$stream = stream_select($read, $write, $except, null, $timeout);
152
		if ($stream !== FALSE && $stream > 0) {
153
			foreach ($read as $pipe) {
154
				$content = stream_get_contents($pipe);
155
				if ($content == '') {
156
					continue;
157
				}
158
				if ($pipe === $pipes[1]) {
159
					if (!$mute) {
160
						update_status($content);
161
					}
162
					flush();
163
				} else if ($pipe === $pipes[2]) {
164
					$error_log .= $content;
165
				}
166
			}
167
		}
168

    
169
		$status = proc_get_status($process);
170
	} while ($status['running']);
171

    
172
	fclose($pipes[1]);
173
	fclose($pipes[2]);
174
	proc_close($process);
175

    
176

    
177
	$rc = $status['exitcode'];
178

    
179
	pkg_debug("pkg_call(): rc = {$rc}\n");
180
	if ($rc == 0) {
181
		return true;
182
	}
183

    
184
	pkg_debug("pkg_call(): error_log\n{$error_log}\n");
185
	if (!$mute) {
186
		update_status("\n\n" .  sprintf(gettext(
187
		    "ERROR!!! An error occurred on pkg execution (rc = %d) with parameters '%s':"),
188
		    $rc, $params) . "\n" . $error_log . "\n");
189
	}
190

    
191
	return false;
192
}
193

    
194
/* Execute pkg with $params, fill stdout and stderr and return pkg rc */
195
function pkg_exec($params, &$stdout, &$stderr, $extra_env = array()) {
196
	global $g, $config;
197

    
198
	if (empty($params)) {
199
		return -1;
200
	}
201

    
202
	$user_agent = $g['product_name'] . '/' . $g['product_version'];
203
	if (!isset($config['system']['do_not_send_host_uuid'])) {
204
		$user_agent .= ' : ' . get_single_sysctl('kern.hostuuid');
205
	}
206

    
207
	$descriptorspec = array(
208
		1 => array("pipe", "w"), /* stdout */
209
		2 => array("pipe", "w")	 /* stderr */
210
	);
211

    
212

    
213
	pkg_debug("pkg_exec(): {$params}\n");
214
	$process = proc_open("/usr/sbin/pkg {$params}", $descriptorspec, $pipes,
215
	    '/', pkg_env($extra_env));
216

    
217
	if (!is_resource($process)) {
218
		return -1;
219
	}
220

    
221
	$stdout = '';
222
	while (($l = fgets($pipes[1])) !== FALSE) {
223
		$stdout .= $l;
224
	}
225
	fclose($pipes[1]);
226

    
227
	$stderr = '';
228
	while (($l = fgets($pipes[2])) !== FALSE) {
229
		$stderr .= $l;
230
	}
231
	fclose($pipes[2]);
232

    
233

    
234
	return proc_close($process);
235
}
236

    
237
/* Compare 2 pkg versions and return:
238
 * '=' - versions are the same
239
 * '>' - $v1 > $v2
240
 * '<' - $v1 < $v2
241
 * '?' - Error
242
 */
243
function pkg_version_compare($v1, $v2) {
244
	if (empty($v1) || empty($v2)) {
245
		return '?';
246
	}
247

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

    
250
	if ($rc != 0) {
251
		return '?';
252
	}
253

    
254
	return str_replace("\n", "", $stdout);
255
}
256

    
257
/* Check if package is installed */
258
function is_pkg_installed($pkg_name) {
259
	global $g;
260

    
261
	if (empty($pkg_name)) {
262
		return false;
263
	}
264

    
265
	return pkg_call("info -e " . $pkg_name, true);
266
}
267

    
268
/* Install package, $pkg_name should not contain prefix */
269
function pkg_install($pkg_name, $force = false) {
270
	global $g;
271
	$result = false;
272

    
273
	$shortname = $pkg_name;
274
	pkg_remove_prefix($shortname);
275

    
276
	$pkg_force = "";
277
	if ($force) {
278
		$pkg_force = "-f ";
279
	}
280

    
281
	pkg_debug("Installing package {$shortname}\n");
282
	if ($force || !is_pkg_installed($pkg_name)) {
283
		$result = pkg_call("install -y " . $pkg_force . $pkg_name);
284
		/* Cleanup cacke to free disk space */
285
		pkg_call("clean -y");
286
	}
287

    
288
	return $result;
289
}
290

    
291
/* Delete package from FreeBSD, $pkg_name should not contain prefix */
292
function pkg_delete($pkg_name) {
293
	global $g;
294

    
295
	$shortname = $pkg_name;
296
	pkg_remove_prefix($shortname);
297

    
298
	pkg_debug("Removing package {$shortname}\n");
299
	if (is_pkg_installed($pkg_name)) {
300
		pkg_call("delete -y " . $pkg_name);
301
		/* Cleanup unecessary dependencies */
302
		pkg_call("autoremove -y");
303
	}
304
}
305

    
306
/* Check if package is present in config.xml */
307
function is_package_installed($package_name) {
308
	return (get_package_id($package_name) != -1);
309
}
310

    
311
/* Find package array index */
312
function get_package_id($package_name) {
313
	global $config;
314

    
315
	if (!is_array($config['installedpackages']['package'])) {
316
		return -1;
317
	}
318

    
319
	foreach ($config['installedpackages']['package'] as $idx => $pkg) {
320
		if ($pkg['name'] == $package_name ||
321
		    get_package_internal_name($pkg) == $package_name) {
322
			return $idx;
323
		}
324
	}
325

    
326
	return -1;
327
}
328

    
329
/* Return internal_name when it's defined, otherwise, returns name */
330
function get_package_internal_name($package_data) {
331
	if (isset($package_data['internal_name']) && ($package_data['internal_name'] != "")) {
332
		/* e.g. name is Ipguard-dev, internal name is ipguard */
333
		return $package_data['internal_name'];
334
	} else {
335
		return $package_data['name'];
336
	}
337
}
338

    
339
// Get information about packages.
340
function get_pkg_info($pkgs = 'all', $only_local = false) {
341
	global $g, $input_errors;
342

    
343
	$out = '';
344
	$err = '';
345

    
346
	unset($pkg_filter);
347
	if (is_array($pkgs)) {
348
		$pkg_filter = $pkgs;
349
		$pkgs = 'all';
350
	}
351

    
352
	if ($pkgs == 'all') {
353
		$pkgs = $g['pkg_prefix'];
354
	}
355

    
356
	if (!function_exists('is_subsystem_dirty')) {
357
		require_once("util.inc");
358
	}
359

    
360
	/* Do not run remote operations if pkg has a lock */
361
	if (is_subsystem_dirty('pkg')) {
362
		$only_local = true;
363
		$lock = false;
364
	} else {
365
		$lock = true;
366
	}
367

    
368
	$extra_param = "";
369
	if ($only_local) {
370
		$extra_param = "-U ";
371
	}
372

    
373
	if ($lock) {
374
		mark_subsystem_dirty('pkg');
375
	}
376
	$rc = pkg_exec("search {$extra_param}-R --raw-format json-compact " . $pkgs, $out, $err);
377
	if ($lock) {
378
		clear_subsystem_dirty('pkg');
379
	}
380

    
381
	if ($rc != 0) {
382
		update_status("\n" . gettext(
383
		    "ERROR: Error trying to get packages list. Aborting...")
384
		    . "\n");
385
		update_status($err);
386
		$input_errors[] =  gettext("ERROR: Error trying to get packages list. Aborting...") . "\n";
387
		$input_errors[] =  $err;
388
		return array();
389
	}
390

    
391
	$result = array();
392
	$pkgs_info = explode("\n", $out);
393
	foreach ($pkgs_info as $pkg_info_json) {
394
		$pkg_info = json_decode($pkg_info_json, true);
395
		if (!isset($pkg_info['name'])) {
396
			continue;
397
		}
398

    
399
		if (isset($pkg_filter) && !in_array($pkg_info['name'], $pkg_filter)) {
400
			continue;
401
		}
402

    
403
		$pkg_info['shortname'] = $pkg_info['name'];
404
		pkg_remove_prefix($pkg_info['shortname']);
405

    
406
		/* XXX: Add it to globals.inc? */
407
		$pkg_info['changeloglink'] =
408
		    "https://github.com/pfsense/FreeBSD-ports/commits/devel/" .
409
		    $pkg_info['categories'][0] . '/' . $pkg_info['name'];
410

    
411
		if (is_pkg_installed($pkg_info['name'])) {
412
			$pkg_info['installed'] = true;
413

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

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

    
426
			$pkg_info['installed_version'] = str_replace("\n", "", $out);
427
		} else if (is_package_installed($pkg_info['shortname'])) {
428
			$pkg_info['broken'] = true;
429
		}
430

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

    
433
		$result[] = $pkg_info;
434
		unset($pkg_info);
435
	}
436

    
437
	/* Sort result alphabetically */
438
	usort($result, function($a, $b) {
439
		return(strcasecmp ($a['name'], $b['name']));
440
	});
441

    
442
	return $result;
443
}
444

    
445
/*
446
 * If binary pkg is installed but post-install tasks were not
447
 * executed yet, do it now.
448
 * This scenario can happen when a pkg is pre-installed during
449
 * build phase, and at this point, cannot find a running system
450
 * to register itself in config.xml and also execute custom
451
 * install functions
452
 */
453
function register_all_installed_packages() {
454
	global $g, $config, $pkg_interface;
455

    
456
	$pkg_info = get_pkg_info('all', true);
457

    
458
	foreach ($pkg_info as $pkg) {
459
		if (!isset($pkg['installed'])) {
460
			continue;
461
		}
462

    
463
		pkg_remove_prefix($pkg['name']);
464

    
465
		if (is_package_installed($pkg['name'])) {
466
			continue;
467
		}
468

    
469
		update_status(sprintf(gettext(
470
		    "Running last steps of %s installation.") . "\n",
471
		    $pkg['name']));
472
		install_package_xml($pkg['name']);
473
	}
474
}
475

    
476
/*
477
 * resync_all_package_configs() Force packages to setup their configuration and rc.d files.
478
 * This function may also print output to the terminal indicating progress.
479
 */
480
function resync_all_package_configs($show_message = false) {
481
	global $config, $pkg_interface, $g;
482

    
483
	log_error(gettext("Resyncing configuration for all packages."));
484

    
485
	if (!is_array($config['installedpackages']['package'])) {
486
		return;
487
	}
488

    
489
	if ($show_message == true) {
490
		echo "Syncing packages:";
491
	}
492

    
493

    
494
	foreach ($config['installedpackages']['package'] as $idx => $package) {
495
		if (empty($package['name'])) {
496
			continue;
497
		}
498
		if ($show_message == true) {
499
			echo " " . $package['name'];
500
		}
501
		if (platform_booting() != true) {
502
			stop_service(get_package_internal_name($package));
503
		}
504
		sync_package($package['name']);
505
		update_status(gettext("Syncing packages...") . "\n");
506
	}
507

    
508
	if ($show_message == true) {
509
		echo " done.\n";
510
	}
511

    
512
	@unlink("/conf/needs_package_sync");
513
}
514

    
515
function uninstall_package($package_name) {
516
	global $config;
517

    
518
	$internal_name = $package_name;
519
	$id = get_package_id($package_name);
520
	if ($id >= 0) {
521
		$internal_name = get_package_internal_name($config['installedpackages']['package'][$id]);
522
		stop_service($internal_name);
523
	}
524
	$pkg_name = $g['pkg_prefix'] . $internal_name;
525

    
526
	if (is_pkg_installed($pkg_name)) {
527
		update_status(gettext("Removing package...") . "\n");
528
		pkg_delete($pkg_name);
529
	} else {
530
		delete_package_xml($package_name);
531
	}
532

    
533
	update_status(gettext("done.") . "\n");
534
}
535

    
536
/* Run <custom_php_resync_config_command> */
537
function sync_package($package_name) {
538
	global $config, $builder_package_install;
539

    
540
	// If this code is being called by pfspkg_installer
541
	// which the builder system uses then return (ignore).
542
	if ($builder_package_install) {
543
		return;
544
	}
545

    
546
	if (empty($config['installedpackages']['package'])) {
547
		return;
548
	}
549

    
550
	if (($pkg_id = get_package_id($package_name)) == -1) {
551
		return; // This package doesn't really exist - exit the function.
552
	}
553

    
554
	if (!is_array($config['installedpackages']['package'][$pkg_id])) {
555
		return;	 // No package belongs to the pkg_id passed to this function.
556
	}
557

    
558
	$package =& $config['installedpackages']['package'][$pkg_id];
559
	if (!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
560
		log_error(sprintf(gettext("The %s package is missing its configuration file and must be reinstalled."), $package['name']));
561
		delete_package_xml($package['name']);
562
		return;
563
	}
564

    
565
	$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
566
	if (isset($pkg_config['nosync'])) {
567
		return;
568
	}
569

    
570
	/* Bring in package include files */
571
	if (!empty($pkg_config['include_file'])) {
572
		$include_file = $pkg_config['include_file'];
573
		if (file_exists($include_file)) {
574
			require_once($include_file);
575
		} else {
576
			log_error(sprintf(gettext('Reinstalling package %1$s because its include file(%2$s) is missing!'), $package['name'], $include_file));
577
			uninstall_package($package['name']);
578
			if (install_package($package['name']) != 0) {
579
				log_error(sprintf(gettext("Reinstalling package %s failed. Take appropriate measures!!!"), $package['name']));
580
				return;
581
			}
582
			if (file_exists($include_file)) {
583
				require_once($include_file);
584
			} else {
585
				return;
586
			}
587
		}
588
	}
589

    
590
	if (!empty($pkg_config['custom_php_global_functions'])) {
591
		eval($pkg_config['custom_php_global_functions']);
592
	}
593
	if (!empty($pkg_config['custom_php_resync_config_command'])) {
594
		eval($pkg_config['custom_php_resync_config_command']);
595
	}
596
}
597

    
598
/* Read info.xml installed by package and return an array */
599
function read_package_config($package_name) {
600
	global $g;
601

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

    
604
	if (!file_exists($pkg_info_xml)) {
605
		return false;
606
	}
607

    
608
	$pkg_info = parse_xml_config_pkg($pkg_info_xml, 'pfsensepkgs');
609

    
610
	if (empty($pkg_info)) {
611
		return false;
612
	}
613

    
614
	/* it always returns an array with 1 item */
615
	return $pkg_info['package'][0];
616
}
617

    
618
/* Read package configurationfile and return an array */
619
function read_package_configurationfile($package_name) {
620
	global $config, $g;
621

    
622
	$pkg_config = array();
623
	$id = get_package_id($package_name);
624

    
625
	if ($id < 0 || !isset($config['installedpackages']['package'][$id]['configurationfile'])) {
626
		return $pkg_config;
627
	}
628

    
629
	$pkg_configurationfile = $config['installedpackages']['package'][$id]['configurationfile'];
630

    
631
	if (empty($pkg_configurationfile) || !file_exists('/usr/local/pkg/' . $pkg_configurationfile)) {
632
		return $pkg_config;
633
	}
634

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

    
637
	return $pkg_config;
638
}
639

    
640
function get_after_install_info($package_name) {
641
	$pkg_config = read_package_config($package_name);
642

    
643
	if (isset($pkg_config['after_install_info'])) {
644
		return $pkg_config['after_install_info'];
645
	}
646

    
647
	return '';
648
}
649

    
650
function eval_once($toeval) {
651
	global $evaled;
652
	if (!$evaled) {
653
		$evaled = array();
654
	}
655
	$evalmd5 = md5($toeval);
656
	if (!in_array($evalmd5, $evaled)) {
657
		@eval($toeval);
658
		$evaled[] = $evalmd5;
659
	}
660
	return;
661
}
662

    
663
function install_package_xml($package_name) {
664
	global $g, $config, $pkg_interface;
665

    
666
	if (($pkg_info = read_package_config($package_name)) == false) {
667
		return false;
668
	}
669

    
670
	/* safe side. Write config below will send to ro again. */
671

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

    
675
	/* add package information to config.xml */
676
	$pkgid = get_package_id($pkg_info['name']);
677
	update_status(gettext("Saving updated package information...") . "\n");
678
	if ($pkgid == -1) {
679
		$config['installedpackages']['package'][] = $pkg_info;
680
		$changedesc = sprintf(gettext("Installed %s package."), $pkg_info['name']);
681
		$to_output = gettext("done.") . "\n";
682
	} else {
683
		$config['installedpackages']['package'][$pkgid] = $pkg_info;
684
		$changedesc = sprintf(gettext("Overwrote previous installation of %s."), $pkg_info['name']);
685
		$to_output = gettext("overwrite!") . "\n";
686
	}
687
	unlink_if_exists('/conf/needs_package_sync');
688
	write_config(sprintf(gettext("Intermediate config write during package install for %s."), $pkg_info['name']));
689
	update_status($to_output);
690

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

    
694
		uninstall_package($package_name);
695
		write_config($changedesc);
696
		log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
697
		update_status(gettext("Failed to install package.") . "\n");
698
		return false;
699
	}
700

    
701
	if (file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
702
		update_status(gettext("Loading package configuration... "));
703
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $pkg_info['configurationfile'], "packagegui");
704
		update_status(gettext("done.") . "\n");
705
		update_status(gettext("Configuring package components...") . "\n");
706
		if (!empty($pkg_config['filter_rules_needed'])) {
707
			$config['installedpackages']['package'][$pkgid]['filter_rule_function'] = $pkg_config['filter_rules_needed'];
708
		}
709
		/* modify system files */
710

    
711
		/* if a require exists, include it.  this will
712
		 * show us where an error exists in a package
713
		 * instead of making us blindly guess
714
		 */
715
		$missing_include = false;
716
		if ($pkg_config['include_file'] <> "") {
717
			update_status(gettext("Loading package instructions...") . "\n");
718
			if (file_exists($pkg_config['include_file'])) {
719
				pkg_debug("require_once('{$pkg_config['include_file']}')\n");
720
				require_once($pkg_config['include_file']);
721
			} else {
722
				pkg_debug("Missing include {$pkg_config['include_file']}\n");
723
				$missing_include = true;
724
				update_status(sprintf(gettext("Include %s is missing!"), basename($pkg_config['include_file'])) . "\n");
725

    
726
				uninstall_package($package_name);
727
				write_config($changedesc);
728
				log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
729
				update_status(gettext("Failed to install package.") . "\n");
730
				return false;
731
			}
732
		}
733

    
734
		/* custom commands */
735
		update_status(gettext("Custom commands...") . "\n");
736
		if ($missing_include == false) {
737
			if ($pkg_config['custom_php_global_functions'] <> "") {
738
				update_status(gettext("Executing custom_php_global_functions()..."));
739
				eval_once($pkg_config['custom_php_global_functions']);
740
				update_status(gettext("done.") . "\n");
741
			}
742
			if ($pkg_config['custom_php_install_command']) {
743
				update_status(gettext("Executing custom_php_install_command()..."));
744
				eval_once($pkg_config['custom_php_install_command']);
745
				update_status(gettext("done.") . "\n");
746
			}
747
			if ($pkg_config['custom_php_resync_config_command'] <> "") {
748
				update_status(gettext("Executing custom_php_resync_config_command()..."));
749
				eval_once($pkg_config['custom_php_resync_config_command']);
750
				update_status(gettext("done.") . "\n");
751
			}
752
		}
753
		/* sidebar items */
754
		if (is_array($pkg_config['menu'])) {
755
			update_status(gettext("Menu items... "));
756
			foreach ($pkg_config['menu'] as $menu) {
757
				if (is_array($config['installedpackages']['menu'])) {
758
					foreach ($config['installedpackages']['menu'] as $amenu) {
759
						if ($amenu['name'] == $menu['name']) {
760
							continue 2;
761
						}
762
					}
763
				} else {
764
					$config['installedpackages']['menu'] = array();
765
				}
766
				$config['installedpackages']['menu'][] = $menu;
767
			}
768
			update_status(gettext("done.") . "\n");
769
		}
770
		/* services */
771
		if (is_array($pkg_config['service'])) {
772
			update_status(gettext("Services... "));
773
			foreach ($pkg_config['service'] as $service) {
774
				if (is_array($config['installedpackages']['service'])) {
775
					foreach ($config['installedpackages']['service'] as $aservice) {
776
						if ($aservice['name'] == $service['name']) {
777
							continue 2;
778
						}
779
					}
780
				} else {
781
					$config['installedpackages']['service'] = array();
782
				}
783
				$config['installedpackages']['service'][] = $service;
784
			}
785
			update_status(gettext("done.") . "\n");
786
		}
787
	} else {
788
		pkg_debug("Unable to find config file\n");
789
		update_status(gettext("Loading package configuration... failed!") . "\n\n" . gettext("Installation aborted."));
790
		pkg_debug(gettext("Unable to load package configuration. Installation aborted.") ."\n");
791

    
792
		uninstall_package($package_name);
793
		write_config($changedesc);
794
		log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
795
		update_status(gettext("Failed to install package.") . "\n");
796
		return false;
797
	}
798

    
799
	/* set up package logging streams */
800
	if ($pkg_info['logging']) {
801
		system_syslogd_start();
802
	}
803

    
804
	update_status(gettext("Writing configuration... "));
805
	write_config($changedesc);
806
	log_error(sprintf(gettext("Successfully installed package: %s."), $pkg_info['name']));
807
	update_status(gettext("done.") . "\n");
808
	if ($pkg_info['after_install_info']) {
809
		update_status($pkg_info['after_install_info']);
810
	}
811

    
812
	return true;
813
}
814

    
815
function delete_package_xml($package_name, $when = "post-deinstall") {
816
	global $g, $config, $pkg_interface;
817

    
818

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

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

    
932
	if ($when == "post-deinstall") {
933
		/* remove config.xml entries */
934
		update_status(gettext("Configuration... "));
935
		unset($config['installedpackages']['package'][$pkgid]);
936
		update_status(gettext("done.") . "\n");
937
		write_config(sprintf(gettext("Removed %s package."), $package_name));
938
	}
939

    
940
	/* remove package entry from /etc/syslog.conf if needed */
941
	/* this must be done after removing the entries from config.xml */
942
	if ($need_syslog_restart) {
943
		system_syslogd_start();
944
	}
945

    
946
}
947

    
948
/*
949
 * Used during upgrade process or retore backup process, verify all
950
 * packages installed in config.xml and install pkg accordingly
951
 */
952
function package_reinstall_all() {
953
	global $g, $config, $pkg_interface;
954

    
955
	$upgrade = (file_exists('/conf/needs_package_sync') && platform_booting());
956

    
957
	if ((!isset($config['installedpackages']['package']) ||
958
	    !is_array($config['installedpackages']['package'])) && !$upgrade) {
959
		return true;
960
	}
961

    
962
	/* During boot after upgrade, wait for internet connection */
963
	if ($upgrade) {
964
		update_status(gettext("Waiting for Internet connection to update pkg metadata and finish package reinstallation"));
965
		$ntries = 3;
966
		while ($ntries > 0) {
967
			if (pkg_update(true)) {
968
				break;
969
			}
970
			update_status('.');
971
			sleep(1);
972
			$ntries--;
973
		}
974
		update_status("\n");
975

    
976
		if ($ntries == 0) {
977
			file_notice(gettext("Package reinstall"),
978
			    gettext("Package reinstall process was ABORTED due to lack of internet connectivity"));
979
			return false;
980
		}
981
	}
982

    
983
	$pkg_info = get_pkg_info();
984

    
985
	if ($upgrade &&
986
	    file_exists("{$g['cf_conf_path']}/packages_to_reinstall_after_upgrade.txt")) {
987
		$package_list = file("{$g['cf_conf_path']}/packages_to_reinstall_after_upgrade.txt",
988
		    FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
989
		unlink_if_exists("{$g['cf_conf_path']}/packages_to_reinstall_after_upgrade.txt");
990
	} else {
991
		if (!isset($config['installedpackages']['package']) || !is_array($config['installedpackages']['package'])) {
992
			return true;
993
		}
994
		$package_list = array();
995
		foreach ($config['installedpackages']['package'] as $package) {
996
			$package_list[] = get_package_internal_name($package);
997
		}
998
	}
999

    
1000
	foreach ($package_list as $package) {
1001
		$found = false;
1002
		foreach ($pkg_info as $pkg) {
1003
			pkg_remove_prefix($pkg['name']);
1004
			if ($pkg['name'] == $package) {
1005
				pkg_install($g['pkg_prefix'] . $package, true);
1006
				$found = true;
1007
				break;
1008
			}
1009
		}
1010

    
1011
		if (!$found) {
1012
			if (!function_exists("file_notice")) {
1013
				require_once("notices.inc");
1014
			}
1015

    
1016
			file_notice(gettext("Package reinstall"),
1017
			    sprintf(gettext("Package %s does not exist in current %s version and it has been removed."),
1018
			    $package, $g['product_name']));
1019
			uninstall_package($package);
1020
		}
1021
	}
1022

    
1023
	return true;
1024
}
1025

    
1026
function stop_packages() {
1027
	require_once("config.inc");
1028
	require_once("functions.inc");
1029
	require_once("filter.inc");
1030
	require_once("shaper.inc");
1031
	require_once("captiveportal.inc");
1032
	require_once("pkg-utils.inc");
1033
	require_once("pfsense-utils.inc");
1034
	require_once("service-utils.inc");
1035

    
1036
	global $config, $g;
1037

    
1038
	log_error(gettext("Stopping all packages."));
1039

    
1040
	$rcfiles = glob(RCFILEPREFIX . "*.sh");
1041
	if (!$rcfiles) {
1042
		$rcfiles = array();
1043
	} else {
1044
		$rcfiles = array_flip($rcfiles);
1045
		if (!$rcfiles) {
1046
			$rcfiles = array();
1047
		}
1048
	}
1049

    
1050
	if (is_array($config['installedpackages']['package'])) {
1051
		foreach ($config['installedpackages']['package'] as $package) {
1052
			echo " Stopping package {$package['name']}...";
1053
			$internal_name = get_package_internal_name($package);
1054
			stop_service($internal_name);
1055
			unset($rcfiles[RCFILEPREFIX . strtolower($internal_name) . ".sh"]);
1056
			echo "done.\n";
1057
		}
1058
	}
1059

    
1060
	foreach ($rcfiles as $rcfile => $number) {
1061
		$shell = @popen("/bin/sh", "w");
1062
		if ($shell) {
1063
			echo " Stopping {$rcfile}...";
1064
			if (!@fwrite($shell, "{$rcfile} stop >>/tmp/bootup_messages 2>&1")) {
1065
				if ($shell) {
1066
					pclose($shell);
1067
				}
1068
				$shell = @popen("/bin/sh", "w");
1069
			}
1070
			echo "done.\n";
1071
			pclose($shell);
1072
		}
1073
	}
1074
}
1075

    
1076
/* Identify which meta package is installed */
1077
function get_meta_pkg_name() {
1078
	global $g;
1079

    
1080
	/* XXX: Use pkg annotation */
1081
	if (is_pkg_installed($g['product_name'])) {
1082
		return $g['product_name'];
1083
	} else if (is_pkg_installed($g['product_name'] . '-vmware')) {
1084
		return $g['product_name'] . '-vmware';
1085
	}
1086
	return false;
1087
}
1088

    
1089
/* Identify which base package is installed */
1090
function get_base_pkg_name() {
1091
	global $g;
1092

    
1093
	/* XXX: Use pkg annotation */
1094
	if (is_pkg_installed($g['product_name'] . '-base-' . $g['platform'])) {
1095
		return $g['product_name'] . '-base-' . $g['platform'];
1096
	} else if (is_pkg_installed($g['product_name'] . '-base')) {
1097
		return $g['product_name'] . '-base';
1098
	}
1099
	return false;
1100
}
1101

    
1102
/* Verify if system needs upgrade (meta package or base) */
1103
function get_system_pkg_version($baseonly = false) {
1104
	global $g;
1105

    
1106
	$base_pkg = get_base_pkg_name();
1107
	$meta_pkg = get_meta_pkg_name();
1108

    
1109
	if (!$base_pkg || !$meta_pkg) {
1110
		return false;
1111
	}
1112

    
1113
	$info = get_pkg_info($base_pkg);
1114
	$pkg_name = $base_pkg;
1115

    
1116
	$pkg_info = array();
1117
	foreach ($info as $item) {
1118
		if ($item['name'] == $base_pkg) {
1119
			$pkg_info = $item;
1120
		}
1121
	}
1122

    
1123
	if (empty($pkg_info) || (!$baseonly && ($pkg_info['version'] == $pkg_info['installed_version']))) {
1124
		$info = get_pkg_info($meta_pkg);
1125
		$pkg_name = $meta_pkg;
1126

    
1127
		foreach ($info as $item) {
1128
			if ($item['name'] == $meta_pkg) {
1129
				$pkg_info = $item;
1130
			}
1131
		}
1132
	}
1133

    
1134
	if (empty($pkg_info)) {
1135
		return false;
1136
	}
1137

    
1138
	return array(
1139
	    'pkg_name'          => $pkg_name,
1140
	    'version'           => $pkg_info['version'],
1141
	    'installed_version' => $pkg_info['installed_version']
1142
	);
1143
}
1144

    
1145
/* List available repos */
1146
function pkg_list_repos() {
1147
	global $g;
1148

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

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

    
1153
	$default = array(
1154
	    'name' => 'Default',
1155
	    'path' => $path . "/{$g['product_name']}-repo.conf",
1156
	    'descr' => $default_descr
1157
	);
1158

    
1159
	$result = array($default);
1160

    
1161
	$conf_files = glob("{$path}/{$g['product_name']}-repo-*.conf");
1162
	foreach ($conf_files as $conf_file) {
1163
		$descr_file = preg_replace('/.conf$/', '.descr', $conf_file);
1164
		if (file_exists($descr_file)) {
1165
			$descr_content = file($descr_file);
1166
			$descr = chop($descr_content[0]);
1167
		} else {
1168
			$descr = 'Unknown';
1169
		}
1170
		if (!preg_match('/-repo-(.*).conf/', $conf_file, $matches)) {
1171
			continue;
1172
		}
1173
		$entry = array(
1174
		    'name' => ucfirst(strtolower($matches[1])),
1175
		    'path' => $conf_file,
1176
		    'descr' => $descr
1177
		);
1178
		$result[] = $entry;
1179
	}
1180

    
1181
	return $result;
1182
}
1183

    
1184
/* Switch between stable and devel repos */
1185
function pkg_switch_repo($path) {
1186
	global $g;
1187

    
1188
	safe_mkdir("/usr/local/etc/pkg/repos");
1189
	@unlink("/usr/local/etc/pkg/repos/{$g['product_name']}.conf");
1190
	@symlink($path, "/usr/local/etc/pkg/repos/{$g['product_name']}.conf");
1191

    
1192
	$abi_file = str_replace('.conf', '.abi', $path);
1193
	$altabi_file = str_replace('.conf', '.altabi', $path);
1194

    
1195
	if (file_exists($abi_file) && file_exists($altabi_file)) {
1196
		$abi = file_get_contents($abi_file);
1197
		$altabi = file_get_contents($altabi_file);
1198

    
1199
		$pkg_conf = array(
1200
			"ABI={$abi}",
1201
			"ALTABI={$altabi}"
1202
		);
1203

    
1204
		file_put_contents("/usr/local/etc/pkg.conf", $pkg_conf);
1205
	}
1206

    
1207
	return pkg_update(true);
1208
}
1209

    
1210
?>
(31-31/51)