Project

General

Profile

Download (45.4 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-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9
 * Copyright (c) 2014-2022 Rubicon Communications, LLC (Netgate)
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25
require_once("globals.inc");
26
require_once("service-utils.inc");
27
require_once("/usr/local/www/includes/functions.inc.php");
28

    
29
if (file_exists("/cf/conf/use_xmlreader")) {
30
	require_once("xmlreader.inc");
31
} else {
32
	require_once("xmlparse.inc");
33
}
34

    
35
require_once("pfsense-utils.inc");
36

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

    
42
		if (!$debug) {
43
			return;
44
		}
45

    
46
		if (!$fd_log) {
47
			$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_debug.log",
48
			    "w");
49
		}
50

    
51
		if (!$fd_log) {
52
			update_status(gettext("Warning, could not open log " .
53
			    "for writing.") . "\n");
54
			return;
55
		}
56
		@fwrite($fd_log, $msg);
57
	}
58
}
59

    
60
/* Validate if pkg name is valid */
61
function pkg_valid_name($pkgname) {
62
	global $g;
63

    
64
	$pattern = "/^{$g['pkg_prefix']}[a-zA-Z0-9\.\-_]+$/";
65
	return preg_match($pattern, $pkgname);
66
}
67

    
68
/* Remove pkg_prefix from package name if it's present */
69
function pkg_remove_prefix(&$pkg_name) {
70
	global $g;
71

    
72
	if (substr($pkg_name, 0, strlen($g['pkg_prefix'])) ==
73
	    $g['pkg_prefix']) {
74
		$pkg_name = substr($pkg_name, strlen($g['pkg_prefix']));
75
	}
76
}
77

    
78
/* Execute pkg update when it's necessary */
79
function pkg_update($force = false) {
80
	global $g;
81

    
82
	return pkg_call("update" . ($force ? " -f" : ""));
83
}
84

    
85
/* return an array with necessary environment vars for pkg */
86
function pkg_env($extra_env = array()) {
87
	global $g;
88

    
89
	$user_agent = $g['product_label'] . '/' . $g['product_version'];
90
	if (!config_path_enabled('system','do_not_send_uniqueid')) {
91
		$user_agent .= ':' . system_get_uniqueid();
92
	}
93

    
94
	$pkg_env_vars = array(
95
		"LANG" => "C",
96
		"HTTP_USER_AGENT" => $user_agent,
97
		"ASSUME_ALWAYS_YES" => "true",
98
		"FETCH_TIMEOUT" => 5,
99
		"FETCH_RETRY" => 2
100
	);
101

    
102
	$http_proxy = config_get_path('system/proxyurl');
103
	$http_proxyport = config_get_path('system/proxyport');
104
	if (!empty($http_proxy)) {
105
		if (!empty($http_proxyport)) {
106
			$http_proxy .= ':' . $http_proxyport;
107
		}
108
		$pkg_env_vars['HTTP_PROXY'] = $http_proxy;
109

    
110
		$proxyuser = config_get_path('system/proxyuser');
111
		$proxypass = config_get_path('system/proxypass');
112
		if (!empty($proxyuser) && !empty($proxypass)) {
113
			$pkg_env_vars['HTTP_PROXY_AUTH'] = "basic:*:" .
114
			    $proxyuser . ":" . $proxypass;
115
		}
116
	}
117

    
118
#	if (config_path_enabled('system','use_mfs_tmpvar') &&
119
#	    !file_exists("/conf/ram_disks_failed")) {
120
#		$pkg_env_vars['PKG_DBDIR'] = '/root/var/db/pkg';
121
#		$pkg_env_vars['PKG_CACHEDIR'] = '/root/var/cache/pkg';
122
#	}
123

    
124
	foreach ($extra_env as $key => $value) {
125
		$pkg_env_vars[$key] = $value;
126
	}
127

    
128
	return $pkg_env_vars;
129
}
130

    
131
/* Execute a pkg call */
132
function pkg_call($params, $mute = false, $extra_env = array()) {
133
	if (empty($params)) {
134
		return false;
135
	}
136

    
137
	$descriptorspec = array(
138
		1 => array("pipe", "w"), /* stdout */
139
		2 => array("pipe", "w")	 /* stderr */
140
	);
141

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

    
146
	if (!is_resource($process)) {
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 tunable? */
154
	$timeout = 60; // seconds
155
	$error_log = '';
156

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

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

    
180
		$status = proc_get_status($process);
181
	} while ($status['running']);
182

    
183
	fclose($pipes[1]);
184
	fclose($pipes[2]);
185
	proc_close($process);
186

    
187

    
188
	$rc = $status['exitcode'];
189

    
190
	pkg_debug("pkg_call(): rc = {$rc}\n");
191
	if ($rc == 0) {
192
		return true;
193
	}
194

    
195
	pkg_debug("pkg_call(): error_log\n{$error_log}\n");
196
	if (!$mute) {
197
		update_status("\n\n" . sprintf(gettext("ERROR!!! An error " .
198
		    "occurred on pkg execution (rc = %d) with parameters " .
199
		    "'%s':"), $rc, $params) . "\n" . $error_log . "\n");
200
	}
201

    
202
	return false;
203
}
204

    
205
/* Execute pkg with $params, fill stdout and stderr and return pkg rc */
206
function pkg_exec($params, &$stdout, &$stderr, $extra_env = array()) {
207
	if (empty($params)) {
208
		return -1;
209
	}
210

    
211
	$descriptorspec = array(
212
		1 => array("pipe", "w"), /* stdout */
213
		2 => array("pipe", "w")	 /* stderr */
214
	);
215

    
216

    
217
	pkg_debug("pkg_exec(): {$params}\n");
218
	$process = proc_open("/usr/local/sbin/pkg-static {$params}",
219
	    $descriptorspec, $pipes, '/', pkg_env($extra_env));
220

    
221
	if (!is_resource($process)) {
222
		return -1;
223
	}
224

    
225
	$stdout = '';
226
	while (($l = fgets($pipes[1])) !== FALSE) {
227
		$stdout .= $l;
228
	}
229
	fclose($pipes[1]);
230

    
231
	$stderr = '';
232
	while (($l = fgets($pipes[2])) !== FALSE) {
233
		$stderr .= $l;
234
	}
235
	fclose($pipes[2]);
236

    
237

    
238
	return proc_close($process);
239
}
240

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

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

    
254
	if ($rc != 0) {
255
		return '?';
256
	}
257

    
258
	return str_replace("\n", "", $stdout);
259
}
260

    
261
/* Check if package is installed */
262
function is_pkg_installed($pkg_name) {
263
	global $g;
264

    
265
	if (empty($pkg_name)) {
266
		return false;
267
	}
268

    
269
	return pkg_call("info -e " . $pkg_name, true);
270
}
271

    
272
/* Install package, $pkg_name should not contain prefix */
273
function pkg_install($pkg_name, $force = false) {
274
	global $g;
275
	$result = false;
276

    
277
	$shortname = $pkg_name;
278
	pkg_remove_prefix($shortname);
279

    
280
	$pkg_force = "";
281
	if ($force) {
282
		$pkg_force = "-f ";
283
	}
284

    
285
	pkg_debug("Installing package {$shortname}\n");
286
	if ($force || !is_pkg_installed($pkg_name)) {
287
		$result = pkg_call("install -y " . $pkg_force . $pkg_name);
288
		/* Cleanup cache to free disk space */
289
		pkg_call("clean -y");
290
	}
291

    
292
	return $result;
293
}
294

    
295
/* Delete package from FreeBSD, $pkg_name should not contain prefix */
296
function pkg_delete($pkg_name) {
297
	global $g;
298

    
299
	$shortname = $pkg_name;
300
	pkg_remove_prefix($shortname);
301

    
302
	pkg_debug("Removing package {$shortname}\n");
303
	if (is_pkg_installed($pkg_name)) {
304
		pkg_call("delete -y " . $pkg_name);
305
		/* Cleanup unnecessary dependencies */
306
		pkg_call("autoremove -y");
307
	}
308
}
309

    
310
/* Check if package is present in config.xml */
311
function is_package_installed($package_name) {
312
	return (get_package_id($package_name) != -1);
313
}
314

    
315
/* Find package array index */
316
function get_package_id($package_name) {
317
	foreach (config_get_path('installedpackages/package', []) as $idx => $pkg) {
318
		if ($pkg['name'] == $package_name ||
319
		    get_package_internal_name($pkg) == $package_name) {
320
			return $idx;
321
		}
322
	}
323

    
324
	return -1;
325
}
326

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

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

    
343
	$out = $err = $extra_param = '';
344
	$rc = 0;
345

    
346
	unset($pkg_filter);
347

    
348
	if (is_array($pkgs)) {
349
		$pkg_filter = $pkgs;
350
		$pkgs = $g['pkg_prefix'] . '*';
351
	} elseif ($pkgs == 'all') {
352
		$pkgs = $g['pkg_prefix'] . '*';
353
	}
354

    
355
	$base_packages = (substr($pkgs, 0, strlen($g['pkg_prefix'])) !=
356
	    $g['pkg_prefix']);
357

    
358
	if ($installed_pkgs_only && !is_pkg_installed($pkgs)) {
359
		/*
360
		 * Return early if the caller wants just installed packages
361
		 * and there are none.  Saves doing any calls that might
362
		 * access a remote package repo.
363
		 */
364
		return array();
365
	}
366

    
367
	if (!function_exists('is_subsystem_dirty')) {
368
		require_once("util.inc");
369
	}
370

    
371
	/* Do not run remote operations if pkg has a lock */
372
	if (is_subsystem_dirty('pkg')) {
373
		$remote_repo_usage_disabled = true;
374
		$lock = false;
375
	} else {
376
		$lock = true;
377
	}
378

    
379
	if ($lock) {
380
		mark_subsystem_dirty('pkg');
381
	}
382

    
383
	if ($remote_repo_usage_disabled) {
384
		$extra_param = "-U ";
385
	}
386

    
387
	$did_search = false;
388
	$search_rc = 0;
389
	$info_rc = 0;
390
	$search_items = array();
391
	$info_items = array();
392

    
393
	if ($base_packages) {
394
		$repo_param = "";
395
	} else {
396
		$repo_param = "-r {$g['product_name']}";
397
	}
398

    
399
	/*
400
	 * If we want more than just the currently installed packages or
401
	 * we want up-to-date remote repo info then do a full pkg search
402
	 */
403
	if (!$installed_pkgs_only || !$remote_repo_usage_disabled) {
404
		$did_search = true;
405
		$search_rc = pkg_exec("search {$repo_param} " .
406
		    "{$extra_param}-R --raw-format json-compact " .
407
		    $pkgs, $search_out, $search_err);
408
		if ($search_rc == 0) {
409
			$search_items = explode("\n", chop($search_out));
410
			array_walk($search_items, function(&$v, &$k) {
411
				$v = json_decode($v, true);
412
			});
413
		}
414
	}
415

    
416
	/*
417
	 * We always should look for local items to detect packages that
418
	 * were removed from remote repo but are already installed locally
419
	 *
420
	 * Take pkg search return code into consideration to fallback to local
421
	 * information when remote repo is not accessible
422
	 */
423
	if (is_pkg_installed($pkgs) || $search_rc != 0) {
424
		$info_rc = pkg_exec("info -R --raw-format json-compact " .
425
		    $pkgs, $info_out, $info_err);
426
		if ($info_rc == 0) {
427
			$info_items = explode("\n", chop($info_out));
428
			array_walk($info_items, function(&$v, &$k) {
429
				$v = json_decode($v, true);
430
			});
431
		}
432
	}
433

    
434
	if ($lock) {
435
		clear_subsystem_dirty('pkg');
436
	}
437

    
438
	if ($search_rc != 0 && $info_rc != 0) {
439
		update_status("\n" . gettext(
440
		    "ERROR: Error trying to get packages list. Aborting...")
441
		    . "\n");
442
		update_status($search_err . "\n" . $info_err);
443
		$input_errors[] = gettext(
444
		    "ERROR: Error trying to get packages list. Aborting...") .
445
		    "\n";
446
		$input_errors[] = $search_err . "\n" . $info_err;
447
		return array();
448
	}
449

    
450
	/* It was not possible to search, use local information only */
451
	if ($search_rc != 0 || !$did_search) {
452
		$search_items = $info_items;
453
	} else {
454
		foreach ($info_items as $pkg_info) {
455
			if (empty($pkg_info['name'])) {
456
				continue;
457
			}
458

    
459
			if (array_search($pkg_info['name'], array_column(
460
			    $search_items, 'name')) === FALSE) {
461
				$pkg_info['obsolete'] = true;
462
				$search_items[] = $pkg_info;
463
			}
464
		}
465
	}
466

    
467
	$result = array();
468
	foreach ($search_items as $pkg_info) {
469
		if (empty($pkg_info['name'])) {
470
			continue;
471
		}
472

    
473
		if (isset($pkg_filter) && !in_array($pkg_info['name'],
474
		    $pkg_filter)) {
475
			continue;
476
		}
477

    
478
		$pkg_info['shortname'] = $pkg_info['name'];
479
		pkg_remove_prefix($pkg_info['shortname']);
480

    
481
		/* XXX: Add it to globals.inc? */
482
		$pkg_info['changeloglink'] =
483
		    "https://github.com/pfsense/FreeBSD-ports/commits/devel/" .
484
		    $pkg_info['categories'][0] . '/' . $pkg_info['name'];
485

    
486
		$pkg_is_installed = false;
487

    
488
		if (is_pkg_installed($pkg_info['name'])) {
489
			$rc = pkg_exec("query %R {$pkg_info['name']}", $out,
490
			    $err);
491
			if (!$base_packages &&
492
			    rtrim($out) != $g['product_name']) {
493
				continue;
494
			}
495

    
496
			$pkg_info['installed'] = true;
497
			$pkg_is_installed = true;
498

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

    
502
			if ($rc != 0) {
503
				update_status("\n" . gettext("ERROR: Error " .
504
				    "trying to get package version. " .
505
				    "Aborting...") . "\n");
506
				update_status($err);
507
				$input_errors[] = gettext("ERROR: Error " .
508
				    "trying to get package version. " .
509
				    "Aborting...") . "\n";
510
				$input_errors[] = $err;
511
				return array();
512
			}
513

    
514
			$pkg_info['installed_version'] = str_replace("\n", "",
515
			    $out);
516

    
517
			/*
518
			 * We used pkg info to collect pkg data so remote
519
			 * version is not available. Lets try to collect it
520
			 * using rquery if possible
521
			 */
522
			if ($search_rc != 0 || !$did_search) {
523
				$rc = pkg_exec(
524
				    "rquery -U %v {$pkg_info['name']}", $out,
525
				    $err);
526

    
527
				if ($rc == 0) {
528
					$pkg_info['version'] =
529
					    str_replace("\n", "", $out);
530
				}
531
			}
532

    
533
		} else if (is_package_installed($pkg_info['shortname'])) {
534
			$pkg_info['broken'] = true;
535
			$pkg_is_installed = true;
536
		}
537

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

    
541
		if (!$installed_pkgs_only || $pkg_is_installed) {
542
			$result[] = $pkg_info;
543
		}
544
		unset($pkg_info);
545
	}
546

    
547
	/* Sort result alphabetically */
548
	usort($result, function($a, $b) {
549
		return(strcasecmp ($a['name'], $b['name']));
550
	});
551

    
552
	return $result;
553
}
554

    
555
/*
556
 * If binary pkg is installed but post-install tasks were not
557
 * executed yet, do it now.
558
 * This scenario can happen when a pkg is pre-installed during
559
 * build phase, and at this point, cannot find a running system
560
 * to register itself in config.xml and also execute custom
561
 * install functions
562
 */
563
function register_all_installed_packages() {
564
	$pkg_info = get_pkg_info('all', true, true);
565

    
566
	foreach ($pkg_info as $pkg) {
567
		pkg_remove_prefix($pkg['name']);
568

    
569
		if (is_package_installed($pkg['name'])) {
570
			continue;
571
		}
572

    
573
		update_status(sprintf(gettext(
574
		    "Running last steps of %s installation.") . "\n",
575
		    $pkg['name']));
576
		install_package_xml($pkg['name']);
577
	}
578
}
579

    
580
/*
581
 * resync_all_package_configs() Force packages to setup their configuration
582
 * and rc.d files.  This function may also print output to the terminal
583
 * indicating progress.
584
 */
585
function resync_all_package_configs($show_message = false) {
586
	log_error(gettext("Resyncing configuration for all packages."));
587

    
588
	if ($show_message == true) {
589
		echo "Syncing packages:";
590
	}
591

    
592
	foreach (config_get_path('installedpackages/package', []) as $idx => $package) {
593
		if (empty($package['name'])) {
594
			continue;
595
		}
596
		if ($show_message == true) {
597
			echo " " . $package['name'];
598
		}
599
		if (platform_booting() != true) {
600
			stop_service(get_package_internal_name($package));
601
		}
602
		sync_package($package['name']);
603
		update_status(gettext("Syncing packages...") . "\n");
604
	}
605

    
606
	if ($show_message == true) {
607
		echo " done.\n";
608
	}
609
}
610

    
611
function uninstall_package($package_name) {
612
	global $g;
613

    
614
	$internal_name = $package_name;
615
	$id = get_package_id($package_name);
616
	if ($id >= 0) {
617
		$internal_name = get_package_internal_name(
618
		    config_get_path("installedpackages/package/{$id}"));
619
		stop_service($internal_name);
620
	}
621
	$pkg_name = $g['pkg_prefix'] . $internal_name;
622

    
623
	if (is_pkg_installed($pkg_name)) {
624
		update_status(gettext("Removing package...") . "\n");
625
		pkg_delete($pkg_name);
626
	} else {
627
		delete_package_xml($package_name);
628
	}
629

    
630
	update_status(gettext("done.") . "\n");
631
}
632

    
633
function reinstall_package($package_name) {
634
	global $g;
635

    
636
	$internal_name = $package_name;
637
	$id = get_package_id($package_name);
638
	if ($id >= 0) {
639
		$internal_name = get_package_internal_name(
640
			config_get_path("installedpackages/package/{$id}"));
641
	}
642
	$pkg_name = $g['pkg_prefix'] . $internal_name;
643
	pkg_install($pkg_name);
644
}
645

    
646
/* Run <custom_php_resync_config_command> */
647
function sync_package($package_name) {
648
	global $builder_package_install;
649

    
650
	// If this code is being called by pfspkg_installer
651
	// which the builder system uses then return (ignore).
652
	if ($builder_package_install) {
653
		return;
654
	}
655

    
656
	if (empty(config_get_path('installedpackages/package', []))) {
657
		return;
658
	}
659

    
660
	if (($pkg_id = get_package_id($package_name)) == -1) {
661
		// This package doesn't really exist - exit the function.
662
		return;
663
	}
664

    
665
	$package = config_get_path("installedpackages/package/{$pkg_id}");
666
	if (empty($package)) {
667
		// No package belongs to the pkg_id passed to this function.
668
		return;
669
	}
670

    
671
	if (!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
672
		log_error(sprintf(gettext("The %s package is missing its " .
673
		    "configuration file and must be reinstalled."),
674
		    $package['name']));
675
		delete_package_xml($package['name']);
676
		return;
677
	}
678

    
679
	$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" .
680
	    $package['configurationfile'], "packagegui");
681
	if (isset($pkg_config['nosync'])) {
682
		return;
683
	}
684

    
685
	/* Bring in package include files */
686
	if (!empty($pkg_config['include_file'])) {
687
		$include_file = $pkg_config['include_file'];
688
		if (file_exists($include_file)) {
689
			require_once($include_file);
690
		} else {
691
			log_error(sprintf(gettext('Reinstalling package %1$s " .
692
			    "because its include file(%2$s) is missing!'),
693
			    $package['name'], $include_file));
694
			uninstall_package($package['name']);
695
			if (reinstall_package($package['name']) != 0) {
696
				log_error(sprintf(gettext("Reinstalling " .
697
				    "package %s failed. Take appropriate " .
698
				    "measures!!!"), $package['name']));
699
				return;
700
			}
701
			if (file_exists($include_file)) {
702
				require_once($include_file);
703
			} else {
704
				return;
705
			}
706
		}
707
	}
708

    
709
	if (!empty($pkg_config['custom_php_global_functions'])) {
710
		eval($pkg_config['custom_php_global_functions']);
711
	}
712
	if (!empty($pkg_config['custom_php_resync_config_command'])) {
713
		eval($pkg_config['custom_php_resync_config_command']);
714
	}
715
}
716

    
717
/* Read info.xml installed by package and return an array */
718
function read_package_config($package_name) {
719
	global $g;
720

    
721
	$pkg_info_xml = '/usr/local/share/' . $g['pkg_prefix'] . $package_name .
722
	    '/info.xml';
723

    
724
	if (!file_exists($pkg_info_xml)) {
725
		return false;
726
	}
727

    
728
	$pkg_info = parse_xml_config_pkg($pkg_info_xml, 'pfsensepkgs');
729

    
730
	if (empty($pkg_info)) {
731
		return false;
732
	}
733

    
734
	/* it always returns an array with 1 item */
735
	return $pkg_info['package'][0];
736
}
737

    
738
/* Read package configurationfile and return an array */
739
function read_package_configurationfile($package_name) {
740
	$pkg_config = array();
741
	$id = get_package_id($package_name);
742
	$pkg_data = config_get_path("installedpackages/package/{$id}", []);
743
   
744
	if ($id < 0 || empty($pkg_data)) {
745
		return $pkg_config;
746
	}
747

    
748
	if (empty($pkg_data['configurationfile'])) {
749
		return $pkg_config;
750
	}
751

    
752
	if (!file_exists('/usr/local/pkg/' . $pkg_data['configurationfile'])) {
753
		return $pkg_config;
754
	}
755

    
756
	$pkg_config = parse_xml_config_pkg('/usr/local/pkg/' .
757
	    $pkg_data['configurationfile'], "packagegui");
758

    
759
	return $pkg_config;
760
}
761

    
762
function get_after_install_info($package_name) {
763
	$pkg_config = read_package_config($package_name);
764

    
765
	if (isset($pkg_config['after_install_info'])) {
766
		return $pkg_config['after_install_info'];
767
	}
768

    
769
	return '';
770
}
771

    
772
function eval_once($toeval) {
773
	global $evaled;
774
	if (!$evaled) {
775
		$evaled = array();
776
	}
777
	$evalmd5 = md5($toeval);
778
	if (!in_array($evalmd5, $evaled)) {
779
		@eval($toeval);
780
		$evaled[] = $evalmd5;
781
	}
782
	return;
783
}
784

    
785
function install_package_xml($package_name) {
786
	if (($pkg_info = read_package_config($package_name)) == false) {
787
		return false;
788
	}
789

    
790
	pkg_debug(gettext("Beginning package installation.") . "\n");
791
	log_error(sprintf(gettext('Beginning package installation for %s .'),
792
	    $pkg_info['name']));
793

    
794
	/* add package information to config.xml */
795
	$pkgid = get_package_id($pkg_info['name']);
796
	update_status(gettext("Saving updated package information...") . "\n");
797
	init_config_arr(array('installedpackages', 'package'));
798
	$pkgs = config_get_path('installedpackages/package', []);
799
	if ($pkgid == -1) {
800
		$pkgs[] = $pkg_info;
801
		$changedesc = sprintf(gettext("Installed %s package."),
802
		    $pkg_info['name']);
803
		$to_output = gettext("done.") . "\n";
804
	} else {
805
		$pkgs[$pkgid] = $pkg_info;
806
		$changedesc = sprintf(gettext("Overwrote previous " .
807
		    "installation of %s."), $pkg_info['name']);
808
		$to_output = gettext("overwrite!") . "\n";
809
	}
810
	config_set_path('installedpackages/package', $pkgs);
811
	write_config(sprintf(gettext("Intermediate config write during " .
812
	    "package install for %s."), $pkg_info['name']));
813
	update_status($to_output);
814

    
815
	if (($pkgid = get_package_id($package_name)) == -1) {
816
		update_status(sprintf(gettext('The %1$s package is not ' .
817
		    'installed.%2$sInstallation aborted.'), $package_name,
818
		    "\n\n"));
819

    
820
		uninstall_package($package_name);
821
		write_config($changedesc);
822
		log_error(sprintf(gettext("Failed to install package: %s."),
823
		    $pkg_info['name']));
824
		update_status(gettext("Failed to install package.") . "\n");
825
		return false;
826
	}
827

    
828
	if (!file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
829
		pkg_debug("Unable to find config file\n");
830
		update_status(gettext("Loading package configuration... " .
831
		    "failed!") .  "\n\n" . gettext("Installation aborted."));
832
		pkg_debug(gettext("Unable to load package configuration. " .
833
		    "Installation aborted.") ."\n");
834

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

    
843
	update_status(gettext("Loading package configuration... "));
844
	$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" .
845
	    $pkg_info['configurationfile'], "packagegui");
846
	update_status(gettext("done.") . "\n");
847
	update_status(gettext("Configuring package components...") .
848
	    "\n");
849
	if (!empty($pkg_config['filter_rules_needed'])) {
850
		config_set_path("installedpackages/package/{$pkgid}/filter_rule_function",
851
						$pkg_config['filter_rules_needed']);
852
	}
853
	/* modify system files */
854

    
855
	/* if a require exists, include it.  this will
856
	 * show us where an error exists in a package
857
	 * instead of making us blindly guess
858
	 */
859
	$missing_include = false;
860
	if ($pkg_config['include_file'] <> "") {
861
		update_status(gettext("Loading package instructions...") .
862
		    "\n");
863
		if (file_exists($pkg_config['include_file'])) {
864
			pkg_debug("require_once('" .
865
			    $pkg_config['include_file'] . "')\n");
866
			require_once($pkg_config['include_file']);
867
		} else {
868
			pkg_debug("Missing include " .
869
			    "{$pkg_config['include_file']}\n");
870
			$missing_include = true;
871
			update_status(sprintf(gettext("Include %s is missing!"),
872
			    basename($pkg_config['include_file'])) . "\n");
873

    
874
			uninstall_package($package_name);
875
			write_config($changedesc);
876
			log_error(sprintf(gettext(
877
			    "Failed to install package: %s."),
878
			    $pkg_info['name']));
879
			update_status(gettext("Failed to install package.") .
880
			    "\n");
881
			return false;
882
		}
883
	}
884

    
885
	/* custom commands */
886
	update_status(gettext("Custom commands...") . "\n");
887
	if ($missing_include == false) {
888
		if ($pkg_config['custom_php_global_functions'] <> "") {
889
			update_status(gettext(
890
			    "Executing custom_php_global_functions()..."));
891
			eval_once($pkg_config['custom_php_global_functions']);
892
			update_status(gettext("done.") . "\n");
893
		}
894
		if ($pkg_config['custom_php_install_command']) {
895
			update_status(gettext(
896
			    "Executing custom_php_install_command()..."));
897
			eval_once($pkg_config['custom_php_install_command']);
898
			update_status(gettext("done.") . "\n");
899
		}
900
		if ($pkg_config['custom_php_resync_config_command'] <> "") {
901
			update_status(gettext(
902
			    "Executing custom_php_resync_config_command()..."));
903
			eval_once(
904
			    $pkg_config['custom_php_resync_config_command']);
905
			update_status(gettext("done.") . "\n");
906
		}
907
	}
908
	/* sidebar items */
909
	if (is_array($pkg_config['menu'])) {
910
		init_config_arr(array('installedpackages', 'menu'));
911
		$menus = config_get_path('installedpackages/menu', []);
912
		update_status(gettext("Menu items... "));
913
		foreach ($pkg_config['menu'] as $menu) {
914
			foreach ($menus as $amenu) {
915
				if ($amenu['name'] == $menu['name']) {
916
					continue 2;
917
				}
918
			}
919
			$menus[] = $menu;
920
		}
921
		config_set_path('installedpackages/menu', $menus);
922
		update_status(gettext("done.") . "\n");
923
	}
924
	/* services */
925
	init_config_arr(array('installedpackages', 'service'));
926
	if (is_array($pkg_config['service'])) {
927
		update_status(gettext("Services... "));
928
		$services = config_get_path('installedpackages/service', []);
929
		foreach ($pkg_config['service'] as $service) {
930
			if (empty($service)) {
931
				continue;
932
			}
933
			foreach ($services as $aservice) {
934
				if (empty($aservice)) {
935
					continue;
936
				}
937
				if ($aservice['name'] == $service['name']) {
938
					continue 2;
939
				}
940
			}
941
			$services[] = $service;
942
		}
943
		config_set_path('installedpackages/service', $services);
944
		update_status(gettext("done.") . "\n");
945
	}
946
	if (is_array($pkg_config['tabs'])) {
947
		init_config_arr(array('installedpackages', 'package', $pkgid, 'tabs'));
948
		config_set_path("installedpackages/package/{$pkgid}/tabs",$pkg_config['tabs']);
949
	}
950
	/* plugins */
951
	if (isset($pkg_config['include_file'])) {
952
		config_set_path("installedpackages/package/{$pkgid}/include_file", $pkg_config['include_file']);
953
	}
954
	if (is_array($pkg_config['plugins'])) {
955
		init_config_arr(array('installedpackages', 'package', $pkgid, 'plugins'));
956
		config_set_path("installedpackages/package/{$pkgid}/plugins", $pkg_config['plugins']);
957
	}
958

    
959
	update_status(gettext("Writing configuration... "));
960
	write_config($changedesc);
961
	log_error(sprintf(gettext("Successfully installed package: %s."),
962
	    $pkg_info['name']));
963
	update_status(gettext("done.") . "\n");
964
	if ($pkg_info['after_install_info']) {
965
		update_status($pkg_info['after_install_info']);
966
	}
967

    
968
	/* set up package logging streams */
969
	if ($pkg_info['logging']) {
970
		system_syslogd_start(true);
971
	}
972

    
973
	return true;
974
}
975

    
976
function delete_package_xml($package_name, $when = "post-deinstall") {
977
	global $g;
978

    
979

    
980
	$pkgid = get_package_id($package_name);
981
	if ($pkgid == -1) {
982
		update_status(sprintf(gettext('The %1$s package is not ' .
983
		    'installed.%2$sDeletion aborted.'), $package_name, "\n\n"));
984
		ob_flush();
985
		sleep(1);
986
		return;
987
	}
988
	pkg_debug(sprintf(gettext("Removing %s package... "), $package_name));
989
	update_status(sprintf(gettext("Removing %s components..."),
990
	    $package_name) . "\n");
991
	/* parse package configuration */
992
	init_config_arr(array('installedpackages', 'package', $pkgid));
993
	$pkg_info = config_get_path("installedpackages/package/{$pkgid}",[]);
994
	init_config_arr(array('installedpackages', 'menu'));
995
	$menus = config_get_path('installedpackages/menu', []);
996
	init_config_arr(array('installedpackages', 'service'));
997
	$services = config_get_path('installedpackages/service', []);
998
	if (file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
999
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" .
1000
		    $pkg_info['configurationfile'], "packagegui");
1001
		/* remove menu items */
1002
		if (is_array($pkg_config['menu']) && is_array($menus)) {
1003
			update_status(gettext("Menu items... "));
1004
			foreach ($pkg_config['menu'] as $menu) {
1005
				foreach ($menus as $key => $instmenu) {
1006
					if (empty($instmenu) || ($instmenu['name'] ==
1007
					    $menu['name'])) {
1008
						config_del_path("installedpackages/menu/{$key}");
1009
						break;
1010
					}
1011
				}
1012
			}
1013
			update_status(gettext("done.") . "\n");
1014
		}
1015
		/* remove services */
1016
		if (is_array($pkg_config['service']) && is_array($services)) {
1017
			update_status(gettext("Services... "));
1018
			foreach ($pkg_config['service'] as $service) {
1019
				if (empty($service)) {
1020
					continue;
1021
				}
1022
				foreach ($services as $key => $instservice) {
1023
					if (empty($instservice) ||
1024
					    ($instservice['name'] !=
1025
					    $service['name'])) {
1026
						continue;
1027
					}
1028
					if (platform_booting() != true) {
1029
						stop_service($service['name']);
1030
					}
1031
					if ($service['rcfile']) {
1032
						if (empty($service['prefix'])) {
1033
							$prefix = RCFILEPREFIX;
1034
						} else {
1035
							$prefix =
1036
							    $service['prefix'];
1037
						}
1038
						unlink_if_exists($prefix .
1039
						    $service['rcfile']);
1040
					}
1041
					config_del_path("installedpackages/service/{$key}");
1042
				}
1043
			}
1044
			update_status(gettext("done.") . "\n");
1045
		}
1046
		/*
1047
		 * XXX: Otherwise inclusion of config.inc again invalidates
1048
		 *      actions taken.
1049
		 *	Same is done during installation.
1050
		 */
1051
		write_config(sprintf(gettext("Intermediate config write " .
1052
		    "during package removal for %s."), $package_name));
1053

    
1054
		/*
1055
		 * If a require exists, include it. this will
1056
		 * show us where an error exists in a package
1057
		 * instead of making us blindly guess
1058
		 */
1059
		$missing_include = false;
1060
		if ($pkg_config['include_file'] <> "") {
1061
			update_status(gettext("Loading package instructions...")
1062
			    . "\n");
1063
			if (file_exists($pkg_config['include_file'])) {
1064
				pkg_debug("require_once(\"" .
1065
				    "{$pkg_config['include_file']}\")\n");
1066
				require_once($pkg_config['include_file']);
1067
			} else {
1068
				pkg_debug("Missing include " .
1069
				    $pkg_config['include_file'] . "\n");
1070
				$missing_include = true;
1071
				update_status(sprintf(gettext("Include file " .
1072
				    "%s could not be found for inclusion."),
1073
				    basename($pkg_config['include_file'])) .
1074
				    "\n");
1075
			}
1076
		}
1077
		/*
1078
		 * NOTE: It is not possible to handle parse errors on eval.
1079
		 * So we prevent it from being run at all to not interrupt all
1080
		 * the other code.
1081
		 */
1082
		if ($when == "deinstall" && $missing_include == false) {
1083
			/*
1084
			 * evaluate this package's global functions and pre
1085
			 * deinstall commands
1086
			 */
1087
			if ($pkg_config['custom_php_global_functions'] <> "") {
1088
				eval_once($pkg_config['custom_php_global_functions']);
1089
			}
1090
			if ($pkg_config['custom_php_pre_deinstall_command'] <> "") {
1091
				eval_once($pkg_config['custom_php_pre_deinstall_command']);
1092
			}
1093
		}
1094
		/* deinstall commands */
1095
		if ($when == "deinstall" &&
1096
		    $pkg_config['custom_php_deinstall_command'] <> "") {
1097
			update_status(gettext("Deinstall commands... "));
1098
			if ($missing_include == false) {
1099
				eval_once($pkg_config['custom_php_deinstall_command']);
1100
				update_status(gettext("done.") . "\n");
1101
			} else {
1102
				update_status("\n". gettext("Not executing " .
1103
				    "custom deinstall hook because an " .
1104
				    "include is missing.") . "\n");
1105
			}
1106
		}
1107
	}
1108
	/* syslog */
1109
	$need_syslog_restart = false;
1110
	if (is_array($pkg_info['logging']) && !empty($pkg_info['logging']['logfilename'])) {
1111
		update_status(gettext("Syslog entries... "));
1112
		/* remove package-related syslog configuration but retain log data,
1113
		 * see https://redmine.pfsense.org/issues/11846 */
1114
		@unlink_if_exists("{$g['varetc_path']}/syslog.d/" . basename($pkg_info['logging']['logfilename']) . ".conf");
1115
		update_status("done.\n");
1116
		$need_syslog_restart = true;
1117
	}
1118

    
1119
	if ($when == "post-deinstall") {
1120
		/* remove config.xml entries */
1121
		update_status(gettext("Configuration... "));
1122
		config_del_path("installedpackages/package/{$pkgid}");
1123
		update_status(gettext("done.") . "\n");
1124
		write_config(sprintf(gettext("Removed %s package."),
1125
		    $package_name));
1126
		/*
1127
		 * remove package entry from /etc/syslog.conf if needed
1128
		 * this must be done after removing the entries from config.xml
1129
		 */
1130
		if ($need_syslog_restart) {
1131
			system_syslogd_start(true);
1132
		}
1133
	}
1134
}
1135

    
1136
/*
1137
 * Used during upgrade process or restore backup process, verify all
1138
 * packages installed in config.xml and install pkg accordingly
1139
 */
1140
function package_reinstall_all() {
1141
	global $g;
1142

    
1143
	$pkgs = config_get_path('installedpackages/package');
1144
	if ($pkgs === null) {
1145
		return true;
1146
	}
1147

    
1148
	/*
1149
	 * Configure default pkg repo for current version instead of
1150
	 * using it from backup, that could be older
1151
	 */
1152
	$default_repo = pkg_get_default_repo();
1153
	$current_repo_path = "";
1154
	if (!empty(config_get_path('system/pkg_repo_conf_path'))) {
1155
		$current_repo_path = config_get_path('system/pkg_repo_conf_path');
1156
	}
1157

    
1158
	if ($current_repo_path != $default_repo['path']) {
1159
		config_set_path('system/pkg_repo_conf_path', $default_repo['path']);
1160
		write_config( "Configured default pkg repo after restore");
1161
		pkg_switch_repo($default_repo['path']);
1162
	}
1163

    
1164
	/* wait for internet connection */
1165
	log_error(gettext("Waiting for Internet connection to update pkg " .
1166
	    "metadata and finish package reinstallation"));
1167
	$ntries = 3;
1168
	while ($ntries > 0) {
1169
		if (pkg_update(true)) {
1170
			break;
1171
		}
1172
		sleep(1);
1173
		$ntries--;
1174
	}
1175

    
1176
	if ($ntries == 0) {
1177
		return false;
1178
	}
1179

    
1180
	if (!empty($pkgs)) { 
1181
		$pkg_info = get_pkg_info();
1182
	}
1183

    
1184
	foreach ($pkgs as $package) {
1185
		$found = false;
1186
		foreach ($pkg_info as $pkg) {
1187
			pkg_remove_prefix($pkg['name']);
1188
			if ($pkg['name'] == $package['internal_name']) {
1189
				pkg_install($g['pkg_prefix'] . $package['internal_name'], true);
1190
				$found = true;
1191
			} elseif ($pkg['name'] == $package['name']) {
1192
				/* some packages use 'name' as the package name,
1193
				 * see https://redmine.pfsense.org/issues/12766 */
1194
				pkg_install($g['pkg_prefix'] . $package['name'], true);
1195
				$found = true;
1196
			}
1197
			if ($found) {
1198
				break;
1199
			}
1200
		}
1201

    
1202
		if (!$found) {
1203
			if (!function_exists("file_notice")) {
1204
				require_once("notices.inc");
1205
			}
1206

    
1207
			/* Name of the package that cannot be found. If it has
1208
			 * an internal name, include that as well. */
1209
			$pkgname = $package['name'];
1210
			if (!empty($package['internal_name'])) {
1211
				$pkgname .= " ({$package['internal_name']})";
1212
			}
1213
			file_notice(gettext("Package reinstall"),
1214
			    sprintf(gettext("Package %s does not exist in " .
1215
			    "current %s version and it has been removed."),
1216
			    $pkgname, $g['product_label']));
1217
			uninstall_package($package);
1218
		}
1219
	}
1220

    
1221
	/*
1222
	 * Verify remaining binary packages not present in current config
1223
	 * during backup restore and remove them
1224
	 */
1225
	$installed_packages = get_pkg_info('all', true, true);
1226
	foreach ($installed_packages as $package) {
1227
		$shortname = $package['name'];
1228
		pkg_remove_prefix($shortname);
1229
		if (get_package_id($shortname) != -1) {
1230
			continue;
1231
		}
1232
		pkg_delete($package['name']);
1233
	}
1234

    
1235
	return true;
1236
}
1237

    
1238
function stop_packages() {
1239
	require_once("config.inc");
1240
	require_once("functions.inc");
1241
	require_once("filter.inc");
1242
	require_once("shaper.inc");
1243
	require_once("captiveportal.inc");
1244
	require_once("pkg-utils.inc");
1245
	require_once("pfsense-utils.inc");
1246
	require_once("service-utils.inc");
1247

    
1248
	log_error(gettext("Stopping all packages."));
1249

    
1250
	$rcfiles = glob(RCFILEPREFIX . "*.sh");
1251
	if (!$rcfiles) {
1252
		$rcfiles = array();
1253
	} else {
1254
		$rcfiles = array_flip($rcfiles);
1255
		if (!$rcfiles) {
1256
			$rcfiles = array();
1257
		}
1258
	}
1259

    
1260
	foreach (config_get_path('installedpackages/package', []) as $package) {
1261
		$internal_name = get_package_internal_name($package);
1262
		if (is_service_running($internal_name)) {
1263
			echo " Stopping package {$package['name']}...";
1264
			stop_service($internal_name);
1265
			echo "done.\n";
1266
			unset($rcfiles[RCFILEPREFIX . strtolower($internal_name) . ".sh"]);
1267
		}
1268
	}
1269

    
1270
	foreach ($rcfiles as $rcfile => $number) {
1271
		$shell = @popen("/bin/sh", "w");
1272
		if ($shell) {
1273
			echo " Stopping {$rcfile}...";
1274
			if (!@fwrite($shell,
1275
			    "{$rcfile} stop >>/tmp/bootup_messages 2>&1")) {
1276
				if ($shell) {
1277
					pclose($shell);
1278
				}
1279
				$shell = @popen("/bin/sh", "w");
1280
			}
1281
			echo "done.\n";
1282
			pclose($shell);
1283
		}
1284
	}
1285
}
1286

    
1287
/* Identify which meta package is installed */
1288
function get_meta_pkg_name() {
1289
	global $g;
1290

    
1291
	/* XXX: Use pkg annotation */
1292
	if (is_pkg_installed($g['product_name'])) {
1293
		return $g['product_name'];
1294
	}
1295
	foreach ($g['alternativemetaports'] as $suffix) {
1296
		if (is_pkg_installed($g['product_name'] . '-' . $suffix)) {
1297
			return $g['product_name'] . '-' . $suffix;
1298
		}
1299
	}
1300
	return false;
1301
}
1302

    
1303
/* Identify which base package is installed */
1304
function get_base_pkg_name() {
1305
	global $g;
1306

    
1307
	/* XXX: Use pkg annotation */
1308
	if (is_pkg_installed($g['product_name'] . '-base-' . $g['product_name'])) {
1309
		return $g['product_name'] . '-base-' . $g['product_name'];
1310
	} else if (is_pkg_installed($g['product_name'] . '-base')) {
1311
		return $g['product_name'] . '-base';
1312
	}
1313
	return false;
1314
}
1315

    
1316
/* Verify if system needs upgrade (meta package or base) */
1317
function get_system_pkg_version($baseonly = false, $use_cache = true) {
1318
	global $g;
1319

    
1320
	if (!check_dnsavailable('any')) {
1321
		return false;
1322
	}
1323

    
1324
	$cache_file = $g['version_cache_file'];
1325
	$rc_file = $cache_file . '.rc';
1326

    
1327
	$rc = "";
1328
	if ($use_cache && file_exists($rc_file) &&
1329
	    (time()-filemtime($rc_file) < $g['version_cache_refresh'])) {
1330
		$rc = chop(@file_get_contents($rc_file));
1331
	}
1332

    
1333
	if ($rc == "2") {
1334
		$output = @file_get_contents($cache_file);
1335
	} else if ($rc != "0") {
1336
		$output = exec(
1337
		    "/usr/local/sbin/{$g['product_name']}-upgrade -c", $_gc,
1338
		    $rc);
1339

    
1340
		/* Update cache if it succeeded */
1341
		if ($rc == 0 || $rc == 2) {
1342
			@file_put_contents($cache_file, $output);
1343
			@file_put_contents($rc_file, $rc);
1344
		} else {
1345
			return false;
1346
		}
1347
	}
1348

    
1349
	/* pfSense-upgrade returns 2 when there is a new version */
1350
	if ($rc == "2") {
1351
		$new_version = explode(' ', $output)[0];
1352
	}
1353

    
1354
	$base_pkg = get_base_pkg_name();
1355
	$meta_pkg = get_meta_pkg_name();
1356

    
1357
	if (!$base_pkg || !$meta_pkg) {
1358
		return false;
1359
	}
1360

    
1361
	$info = get_pkg_info($base_pkg, true, true);
1362

    
1363
	$pkg_info = array();
1364
	foreach ($info as $item) {
1365
		if ($item['name'] == $base_pkg) {
1366
			$pkg_info = $item;
1367
			break;
1368
		}
1369
	}
1370

    
1371
	if (empty($pkg_info) || (!$baseonly && ($pkg_info['version'] ==
1372
	    $pkg_info['installed_version']))) {
1373
		$info = get_pkg_info($meta_pkg, true, true);
1374

    
1375
		foreach ($info as $item) {
1376
			if ($item['name'] == $meta_pkg) {
1377
				$pkg_info = $item;
1378
				break;
1379
			}
1380
		}
1381
	}
1382

    
1383
	if (empty($pkg_info)) {
1384
		return false;
1385
	}
1386

    
1387
	$result = array(
1388
	    'version'           => $new_version ?: $pkg_info['version'],
1389
	    'installed_version' => $pkg_info['installed_version']
1390
	);
1391

    
1392
	$result['pkg_version_compare'] = pkg_version_compare(
1393
	    $result['installed_version'], $result['version']);
1394

    
1395
	return $result;
1396
}
1397

    
1398
/* List available repos */
1399
function pkg_list_repos() {
1400
	global $g;
1401

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

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

    
1407
	$default = array(
1408
	    'name' => 'Default',
1409
	    'path' => $path . "/{$g['product_name']}-repo.conf",
1410
	    'descr' => $default_descr
1411
	);
1412

    
1413
	$result = array($default);
1414

    
1415
	$conf_files = glob("{$path}/{$g['product_name']}-repo-*.conf");
1416
	foreach ($conf_files as $conf_file) {
1417
		$descr_file = preg_replace('/.conf$/', '.descr', $conf_file);
1418
		if (file_exists($descr_file)) {
1419
			$descr_content = file($descr_file);
1420
			$descr = chop($descr_content[0]);
1421
		} else {
1422
			$descr = 'Unknown';
1423
		}
1424
		if (!preg_match('/-repo-(.*).conf/', $conf_file, $matches)) {
1425
			continue;
1426
		}
1427
		$entry = array(
1428
		    'name' => ucfirst(strtolower($matches[1])),
1429
		    'path' => $conf_file,
1430
		    'descr' => $descr
1431
		);
1432
		if (file_exists($conf_file . ".default")) {
1433
			$entry['default'] = true;
1434
		}
1435
		$result[] = $entry;
1436
	}
1437

    
1438
	return $result;
1439
}
1440

    
1441
function pkg_get_default_repo() {
1442
	$repos = pkg_list_repos();
1443

    
1444
	foreach ($repos as $repo) {
1445
		if (isset($repo['default'])) {
1446
			return $repo;
1447
		}
1448
	}
1449

    
1450
	/* No default found, return the first one */
1451
	return ($repos[0]);
1452
}
1453

    
1454
/* List available repos on a format to be used by selectors */
1455
function pkg_build_repo_list() {
1456
	$repos = pkg_list_repos();
1457
	$list = array();
1458

    
1459
	foreach ($repos as $repo) {
1460
		$list[$repo['name']] = $repo['descr'];
1461
	}
1462

    
1463
	return($list);
1464
}
1465

    
1466
/* Find repo by path */
1467
function pkg_get_repo_name($path) {
1468
	$repos = pkg_list_repos();
1469

    
1470
	$default = $repos[0]['name'];
1471
	foreach ($repos as $repo) {
1472
		if ($repo['path'] == $path) {
1473
			return $repo['name'];
1474
		}
1475
		if (isset($repo['default'])) {
1476
			$default = $repo['name'];
1477
		}
1478
	}
1479

    
1480
	/* Default */
1481
	return $default;
1482
}
1483

    
1484
/* Setup pkg.conf according current repo */
1485
function pkg_conf_setup($repo_path) {
1486
	global $g;
1487

    
1488
	$pkg_conf_path = "/usr/local/etc/pkg.conf";
1489
	$conf = "/usr/local/etc/pkg/repos/{$g['product_name']}.conf";
1490
	if (!file_exists($conf)) {
1491
		return;
1492
	}
1493

    
1494
	$real_conf = readlink($conf);
1495

    
1496
	if (!$real_conf) {
1497
		return;
1498
	}
1499

    
1500
	$abi_file = str_replace('.conf', '.abi', $real_conf);
1501
	$altabi_file = str_replace('.conf', '.altabi', $real_conf);
1502

    
1503
	$pkg_conf = array();
1504
	if (file_exists($abi_file) && file_exists($altabi_file)) {
1505
		$abi = file_get_contents($abi_file);
1506
		$altabi = file_get_contents($altabi_file);
1507

    
1508
		$pkg_conf = array(
1509
			"ABI={$abi}\n",
1510
			"ALTABI={$altabi}\n"
1511
		);
1512
	}
1513

    
1514
	$auth_ca = "/etc/ssl/netgate-ca.pem";
1515
	$auth_cert = "/etc/ssl/pfSense-repo-custom.cert";
1516
	$auth_key = "/etc/ssl/pfSense-repo-custom.key";
1517
	if (strpos($repo_path, "{$g['product_name']}-repo-custom") &&
1518
	    file_exists($auth_ca) && file_exists($auth_cert) &&
1519
	    file_exists($auth_key)) {
1520
		$pkg_conf[] = "PKG_ENV {\n";
1521
		$pkg_conf[] = "\tSSL_CA_CERT_FILE=$auth_ca\n";
1522
		$pkg_conf[] = "\tSSL_CLIENT_CERT_FILE=$auth_cert\n";
1523
		$pkg_conf[] = "\tSSL_CLIENT_KEY_FILE=$auth_key\n";
1524
		$pkg_conf[] = "}\n";
1525
	}
1526

    
1527
	file_put_contents($pkg_conf_path, $pkg_conf);
1528
}
1529

    
1530
/* Switch between stable and devel repos */
1531
function pkg_switch_repo($path) {
1532
	global $g;
1533

    
1534
	safe_mkdir("/usr/local/etc/pkg/repos");
1535
	@unlink("/usr/local/etc/pkg/repos/{$g['product_name']}.conf");
1536
	@symlink($path, "/usr/local/etc/pkg/repos/{$g['product_name']}.conf");
1537

    
1538
	pkg_conf_setup($path);
1539

    
1540
	/* Update pfSense_version cache */
1541
	mwexec_bg("/etc/rc.update_pkg_metadata now");
1542
	return;
1543
}
1544

    
1545
$FQDN = "https://ews.netgate.com/pfupdate";
1546
$refreshinterval = (24 * 3600);	// 24 hours
1547
$idfile = "/var/db/uniqueid";
1548
$repopath = "/usr/local/share/{$g['product_name']}/pkg/repos";
1549
$configflename = "{$repopath}/{$g['product_name']}-repo-custom.conf";
1550
$cafilesrc = "/usr/local/share/{$g['product_name']}/ssl/netgate-ca.pem";
1551
$cafile = "/etc/ssl/netgate-ca.pem";
1552

    
1553
/*
1554
 * Update the list of available repositories from the server. This will allow
1555
 * migration to another update repository should the existing one become
1556
 * unavailable
1557
 */
1558
function update_repos() {
1559
	global $g, $cafile, $cafilesrc, $idfile, $FQDN;
1560

    
1561
	if (!file_exists($idfile) || !function_exists('curl_version')) {
1562
		return;
1563
	}
1564
	/* Copy the CA file to the proper place before start. */
1565
	if (!file_exists($cafile) && file_exists($cafilesrc)) {
1566
		copy($cafilesrc, $cafile);
1567
	}
1568
	/*
1569
	 * If the custom repository definition does not exist, or is more
1570
	 * than 24 hours old fetch a copy from the server
1571
	 */
1572
	if (!file_exists($configflename) ||
1573
	    (time()-filemtime($configflename) > $refreshinterval)) {
1574
		/*
1575
		 * Gather some information about the system so the proper
1576
		 * repo can be returned
1577
		 */
1578
		$nid = file_get_contents($idfile);
1579
		$serial = system_get_serial();
1580

    
1581
		// Compose a version string in JSON format
1582
		$os = php_uname('s');
1583
		$osver = php_uname('r');
1584
		$ed =  (strpos($g['product_label'], 'Plus') !== false)
1585
		    ? "Plus" : "Community";
1586
		$pkglist = get_pkg_info("all", false, true);
1587
		$platform = system_identify_specific_platform();
1588
		$platformname = gettext('Unknown system');
1589

    
1590
		if (isset($platform['descr'])) {
1591
			$platformname = $platform['descr'];
1592
		}
1593

    
1594
		$arch =  php_uname('m');
1595
		$locale = config_get_path('system/language');
1596
		
1597
		// Find root file system type
1598
		$filesystems = get_mounted_filesystems();
1599
		$fstype = "";
1600

    
1601
		foreach ($filesystems as $fs)  {
1602
		   if ($fs['mountpoint'] == "/") {
1603
		      $fstype = strtoupper($fs['type']);
1604
		      break;
1605
		   }
1606
		}
1607

    
1608
		$va = array();
1609
		$va['platform'] = $platformname;
1610
		$va['os'] = $os;
1611
		$va['osver'] = $platformname;
1612
		$va['fstype'] = $fstype;
1613
		$va['prod'] = $g['product_label'];
1614
		$va['ver'] = $g['product_version_string'];
1615
		$va['ed'] = $ed;
1616
		$va['pkgs'] = array();
1617

    
1618
		foreach($pkglist as $pkg) {
1619
			$va['pkgs'][] = array('name' => $pkg['shortname'], 'ver' => $pkg['version']);
1620
		}
1621

    
1622
		$post = [
1623
		    'uid' => $nid,
1624
		    'language' => $locale,
1625
		    'serial' => $serial,
1626
		    'version' => json_encode($va),
1627
		    'arch' => $arch
1628
		];
1629

    
1630
		$ch = curl_init();
1631
		curl_setopt($ch, CURLOPT_HEADER, 0);
1632
		curl_setopt($ch, CURLOPT_VERBOSE, 0);
1633
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
1634
		curl_setopt($ch, CURLOPT_USERAGENT, $g['product_label'] . '/' . $g['product_version']);
1635
		curl_setopt($ch, CURLOPT_URL, $FQDN);
1636
		curl_setopt($ch, CURLOPT_POST, true);
1637
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
1638
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,4);
1639
		set_curlproxy($ch);
1640

    
1641
		$response = curl_exec($ch);
1642
		$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1643

    
1644
		curl_close($ch);
1645

    
1646
		if ($status == 200) {
1647
			save_repo($response);
1648
		}
1649
	}
1650
}
1651

    
1652
// Parse the received JSON data and save the custom repository information
1653
function save_repo($json) {
1654
	global $repopath, $g;
1655

    
1656
	$repo = json_decode($json, true);
1657
	$basename = "{$repopath}/{$g['product_name']}-repo-custom.";
1658

    
1659
	if (($repo != NULL) && isset($repo['abi']) && isset($repo['altabi']) &&
1660
	    isset($repo['conf']) && isset($repo['descr']) &&
1661
	    isset($repo['name']) && (strlen($repo['conf']) > 10)) {
1662

    
1663
		if (!empty($repo['conf']))		{file_put_contents($basename . "conf", base64_decode($repo['conf']));}
1664
		if (!empty($repo['descr']))		{file_put_contents($basename . "descr", $repo['descr']);}
1665
		if (!empty($repo['abi']))		{file_put_contents($basename . "abi", $repo['abi']);}
1666
		if (!empty($repo['altabi']))	{file_put_contents($basename . "altabi", $repo['altabi']);}
1667
		if (!empty($repo['name']))		{file_put_contents($basename . "name", $repo['name']);}
1668
		if (!empty($repo['help']))		{file_put_contents($basename . "help", $repo['help']);}
1669

    
1670

    
1671
		// Save fingerprint file
1672
		if (!empty($repo['fingerprint'])) {
1673
			if (!empty($repo['fpname'])) {
1674
				$fppath = "/usr/local/share/pfSense/keys/pkg/trusted/" . $repo['fpname'];
1675
			} else {
1676
				$fppath = "/usr/local/share/pfSense/keys/pkg/trusted/custom.pfsense.org";
1677
			}
1678

    
1679
			file_put_contents($fppath, $repo['fingerprint']);
1680
		}
1681

    
1682
		// Save the client Cert & key
1683
		if (!empty($repo['cert']) && !empty($repo['key'])) {
1684
			$certbasename = "/etc/ssl/{$g['product_name']}-repo-custom.";
1685
			file_put_contents($certbasename . "cert", $repo['cert']);
1686
			file_put_contents($certbasename . "key", $repo['key']);
1687
			chmod($certbasename . "key", 0600);
1688
		}
1689
	} else {
1690
		/*
1691
		 * If there was anything wrong with the custom repository
1692
		 * definition, remove the custom files to avoid possible confusion
1693
		 */
1694
		foreach (glob($basename . "*") as $f) {
1695
			unlink($f);
1696
		}
1697

    
1698
		// If config.xml still contains a reference to the custom repo, replace it with the "stable" repository definition
1699
		if (strpos(config_get_path('system/pkg_repo_conf_path'), "custom") !== false) {
1700
			config_set_path('system/pkg_repo_conf_path',
1701
							"/usr/local/share/pfSense/pkg/repos/pfSense-repo.conf");
1702
			write_config("-NoReMoTeBaCkUp");
1703
			pkg_switch_repo("/usr/local/share/pfSense/pkg/repos/pfSense-repo.conf");
1704
		}
1705
	}
1706
}
1707

    
1708
?>
(41-41/62)