Project

General

Profile

Download (42.5 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-2018 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
	$user_agent = $g['product_name'] . '/' . $g['product_version'];
81
	if (!isset($config['system']['do_not_send_uniqueid'])) {
82
		$user_agent .= ':' . system_get_uniqueid();
83
	}
84

    
85
	$pkg_env_vars = array(
86
		"LANG" => "C",
87
		"HTTP_USER_AGENT" => $user_agent,
88
		"ASSUME_ALWAYS_YES" => "true",
89
		"FETCH_TIMEOUT" => 5,
90
		"FETCH_RETRY" => 2
91
	);
92

    
93
	if (!empty($config['system']['proxyurl'])) {
94
		$http_proxy = $config['system']['proxyurl'];
95
		if (!empty($config['system']['proxyport'])) {
96
			$http_proxy .= ':' . $config['system']['proxyport'];
97
		}
98
		$pkg_env_vars['HTTP_PROXY'] = $http_proxy;
99

    
100
		if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
101
			$pkg_env_vars['HTTP_PROXY_AUTH'] = "basic:*:{$config['system']['proxyuser']}:{$config['system']['proxypass']}";
102
		}
103
	}
104

    
105
	if (isset($config['system']['use_mfs_tmpvar'])) {
106
		$pkg_env_vars['PKG_DBDIR'] = '/root/var/db/pkg';
107
		$pkg_env_vars['PKG_CACHEDIR'] = '/root/var/cache/pkg';
108
	}
109

    
110
	foreach ($extra_env as $key => $value) {
111
		$pkg_env_vars[$key] = $value;
112
	}
113

    
114
	return $pkg_env_vars;
115
}
116

    
117
/* Execute a pkg call */
118
function pkg_call($params, $mute = false, $extra_env = array()) {
119
	global $g, $config;
120

    
121
	if (empty($params)) {
122
		return false;
123
	}
124

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

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

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

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

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

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

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

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

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

    
175

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

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

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

    
190
	return false;
191
}
192

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

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

    
201
	$descriptorspec = array(
202
		1 => array("pipe", "w"), /* stdout */
203
		2 => array("pipe", "w")	 /* stderr */
204
	);
205

    
206

    
207
	pkg_debug("pkg_exec(): {$params}\n");
208
	$process = proc_open("/usr/local/sbin/pkg-static {$params}",
209
	    $descriptorspec, $pipes, '/', pkg_env($extra_env));
210

    
211
	if (!is_resource($process)) {
212
		return -1;
213
	}
214

    
215
	$stdout = '';
216
	while (($l = fgets($pipes[1])) !== FALSE) {
217
		$stdout .= $l;
218
	}
219
	fclose($pipes[1]);
220

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

    
227

    
228
	return proc_close($process);
229
}
230

    
231
/* Compare 2 pkg versions and return:
232
 * '=' - versions are the same
233
 * '>' - $v1 > $v2
234
 * '<' - $v1 < $v2
235
 * '?' - Error
236
 */
237
function pkg_version_compare($v1, $v2) {
238
	if (empty($v1) || empty($v2)) {
239
		return '?';
240
	}
241

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

    
244
	if ($rc != 0) {
245
		return '?';
246
	}
247

    
248
	return str_replace("\n", "", $stdout);
249
}
250

    
251
/* Check if package is installed */
252
function is_pkg_installed($pkg_name) {
253
	global $g;
254

    
255
	if (empty($pkg_name)) {
256
		return false;
257
	}
258

    
259
	return pkg_call("info -e " . $pkg_name, true);
260
}
261

    
262
/* Install package, $pkg_name should not contain prefix */
263
function pkg_install($pkg_name, $force = false) {
264
	global $g;
265
	$result = false;
266

    
267
	$shortname = $pkg_name;
268
	pkg_remove_prefix($shortname);
269

    
270
	$pkg_force = "";
271
	if ($force) {
272
		$pkg_force = "-f ";
273
	}
274

    
275
	pkg_debug("Installing package {$shortname}\n");
276
	if ($force || !is_pkg_installed($pkg_name)) {
277
		$result = pkg_call("install -y " . $pkg_force . $pkg_name);
278
		/* Cleanup cacke to free disk space */
279
		pkg_call("clean -y");
280
	}
281

    
282
	return $result;
283
}
284

    
285
/* Delete package from FreeBSD, $pkg_name should not contain prefix */
286
function pkg_delete($pkg_name) {
287
	global $g;
288

    
289
	$shortname = $pkg_name;
290
	pkg_remove_prefix($shortname);
291

    
292
	pkg_debug("Removing package {$shortname}\n");
293
	if (is_pkg_installed($pkg_name)) {
294
		pkg_call("delete -y " . $pkg_name);
295
		/* Cleanup unecessary dependencies */
296
		pkg_call("autoremove -y");
297
	}
298
}
299

    
300
/* Check if package is present in config.xml */
301
function is_package_installed($package_name) {
302
	return (get_package_id($package_name) != -1);
303
}
304

    
305
/* Find package array index */
306
function get_package_id($package_name) {
307
	global $config;
308

    
309
	if (!is_array($config['installedpackages']['package'])) {
310
		return -1;
311
	}
312

    
313
	foreach ($config['installedpackages']['package'] as $idx => $pkg) {
314
		if ($pkg['name'] == $package_name ||
315
		    get_package_internal_name($pkg) == $package_name) {
316
			return $idx;
317
		}
318
	}
319

    
320
	return -1;
321
}
322

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

    
333
// Get information about packages.
334
function get_pkg_info($pkgs = 'all', $remote_repo_usage_disabled = false,
335
    $installed_pkgs_only = false) {
336
	global $g, $input_errors;
337

    
338
	$out = $err = $extra_param = '';
339
	$rc = 0;
340

    
341
	unset($pkg_filter);
342

    
343
	if (is_array($pkgs)) {
344
		$pkg_filter = $pkgs;
345
		$pkgs = $g['pkg_prefix'] . '*';
346
	} elseif ($pkgs == 'all') {
347
		$pkgs = $g['pkg_prefix'] . '*';
348
	}
349

    
350

    
351
	if ($installed_pkgs_only && !is_pkg_installed($pkgs)) {
352
		/*
353
		 * Return early if the caller wants just installed packages
354
		 * and there are none.  Saves doing any calls that might
355
		 * access a remote package repo.
356
		 */
357
		return array();
358
	}
359

    
360
	if (!function_exists('is_subsystem_dirty')) {
361
		require_once("util.inc");
362
	}
363

    
364
	/* Do not run remote operations if pkg has a lock */
365
	if (is_subsystem_dirty('pkg')) {
366
		$remote_repo_usage_disabled = true;
367
		$lock = false;
368
	} else {
369
		$lock = true;
370
	}
371

    
372
	if ($lock) {
373
		mark_subsystem_dirty('pkg');
374
	}
375

    
376
	if ($remote_repo_usage_disabled) {
377
		$extra_param = "-U ";
378
	}
379

    
380
	$did_search = false;
381
	$search_rc = 0;
382
	$info_rc = 0;
383
	$search_items = array();
384
	$info_items = array();
385

    
386
	/*
387
	 * If we want more than just the currently installed packages or
388
	 * we want up-to-date remote repo info then do a full pkg search
389
	 */
390
	if (!$installed_pkgs_only || !$remote_repo_usage_disabled) {
391
		$did_search = true;
392
		$search_rc = pkg_exec("search -r {$g['product_name']} " .
393
		    "{$extra_param}-R --raw-format json-compact " .
394
		    $pkgs, $search_out, $search_err);
395
		if ($search_rc == 0) {
396
			$search_items = explode("\n", chop($search_out));
397
			array_walk($search_items, function(&$v, &$k) {
398
				$v = json_decode($v, true);
399
			});
400
		}
401
	}
402

    
403
	/*
404
	 * We always should look for local items to detect packages that
405
	 * were removed from remote repo but are already installed locally
406
	 *
407
	 * Take pkg search return code into consideration to fallback to local
408
	 * information when remote repo is not accessible
409
	 */
410
	if (is_pkg_installed($pkgs) || $search_rc != 0) {
411
		$info_rc = pkg_exec("info -R --raw-format json-compact " .
412
		    $pkgs, $info_out, $info_err);
413
		if ($info_rc == 0) {
414
			$info_items = explode("\n", chop($info_out));
415
			array_walk($info_items, function(&$v, &$k) {
416
				$v = json_decode($v, true);
417
			});
418
		}
419
	}
420

    
421
	if ($lock) {
422
		clear_subsystem_dirty('pkg');
423
	}
424

    
425
	if ($search_rc != 0 && $info_rc != 0) {
426
		update_status("\n" . gettext(
427
		    "ERROR: Error trying to get packages list. Aborting...")
428
		    . "\n");
429
		update_status($search_err . "\n" . $info_err);
430
		$input_errors[] = gettext(
431
		    "ERROR: Error trying to get packages list. Aborting...") .
432
		    "\n";
433
		$input_errors[] = $search_err . "\n" . $info_err;
434
		return array();
435
	}
436

    
437
	/* It was not possible to search, use local information only */
438
	if ($search_rc != 0 || !$did_search) {
439
		$search_items = $info_items;
440
	} else {
441
		foreach ($info_items as $pkg_info) {
442
			if (empty($pkg_info['name'])) {
443
				continue;
444
			}
445

    
446
			if (array_search($pkg_info['name'], array_column(
447
			    $search_items, 'name')) === FALSE) {
448
				$pkg_info['obsolete'] = true;
449
				$search_items[] = $pkg_info;
450
			}
451
		}
452
	}
453

    
454
	$result = array();
455
	foreach ($search_items as $pkg_info) {
456
		if (empty($pkg_info['name'])) {
457
			continue;
458
		}
459

    
460
		if (isset($pkg_filter) && !in_array($pkg_info['name'],
461
		    $pkg_filter)) {
462
			continue;
463
		}
464

    
465
		$pkg_info['shortname'] = $pkg_info['name'];
466
		pkg_remove_prefix($pkg_info['shortname']);
467

    
468
		/* XXX: Add it to globals.inc? */
469
		$pkg_info['changeloglink'] =
470
		    "https://github.com/pfsense/FreeBSD-ports/commits/devel/" .
471
		    $pkg_info['categories'][0] . '/' . $pkg_info['name'];
472

    
473
		$pkg_is_installed = false;
474

    
475
		if (is_pkg_installed($pkg_info['name'])) {
476
			$rc = pkg_exec("query %R {$pkg_info['name']}", $out,
477
			    $err);
478
			if (rtrim($out) != $g['product_name']) {
479
				continue;
480
			}
481

    
482
			$pkg_info['installed'] = true;
483
			$pkg_is_installed = true;
484

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

    
488
			if ($rc != 0) {
489
				update_status("\n" . gettext("ERROR: Error " .
490
				    "trying to get package version. " .
491
				    "Aborting...") . "\n");
492
				update_status($err);
493
				$input_errors[] = gettext("ERROR: Error " .
494
				    "trying to get package version. " .
495
				    "Aborting...") . "\n";
496
				$input_errors[] = $err;
497
				return array();
498
			}
499

    
500
			$pkg_info['installed_version'] = str_replace("\n", "",
501
			    $out);
502

    
503
			/*
504
			 * We used pkg info to collect pkg data so remote
505
			 * version is not available. Lets try to collect it
506
			 * using rquery if possible
507
			 */
508
			if ($search_rc != 0 || !$did_search) {
509
				$rc = pkg_exec(
510
				    "rquery -U %v {$pkg_info['name']}", $out,
511
				    $err);
512

    
513
				if ($rc == 0) {
514
					$pkg_info['version'] =
515
					    str_replace("\n", "", $out);
516
				}
517
			}
518

    
519
		} else if (is_package_installed($pkg_info['shortname'])) {
520
			$pkg_info['broken'] = true;
521
			$pkg_is_installed = true;
522
		}
523

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

    
527
		if (!$installed_pkgs_only || $pkg_is_installed) {
528
			$result[] = $pkg_info;
529
		}
530
		unset($pkg_info);
531
	}
532

    
533
	/* Sort result alphabetically */
534
	usort($result, function($a, $b) {
535
		return(strcasecmp ($a['name'], $b['name']));
536
	});
537

    
538
	return $result;
539
}
540

    
541
/*
542
 * If binary pkg is installed but post-install tasks were not
543
 * executed yet, do it now.
544
 * This scenario can happen when a pkg is pre-installed during
545
 * build phase, and at this point, cannot find a running system
546
 * to register itself in config.xml and also execute custom
547
 * install functions
548
 */
549
function register_all_installed_packages() {
550
	global $g, $config, $pkg_interface;
551

    
552
	$pkg_info = get_pkg_info('all', true, true);
553

    
554
	foreach ($pkg_info as $pkg) {
555
		pkg_remove_prefix($pkg['name']);
556

    
557
		if (is_package_installed($pkg['name'])) {
558
			continue;
559
		}
560

    
561
		update_status(sprintf(gettext(
562
		    "Running last steps of %s installation.") . "\n",
563
		    $pkg['name']));
564
		install_package_xml($pkg['name']);
565
	}
566
}
567

    
568
/*
569
 * resync_all_package_configs() Force packages to setup their configuration and rc.d files.
570
 * This function may also print output to the terminal indicating progress.
571
 */
572
function resync_all_package_configs($show_message = false) {
573
	global $config, $pkg_interface, $g;
574

    
575
	log_error(gettext("Resyncing configuration for all packages."));
576

    
577
	if (!is_array($config['installedpackages']['package'])) {
578
		return;
579
	}
580

    
581
	if ($show_message == true) {
582
		echo "Syncing packages:";
583
	}
584

    
585

    
586
	foreach ($config['installedpackages']['package'] as $idx => $package) {
587
		if (empty($package['name'])) {
588
			continue;
589
		}
590
		if ($show_message == true) {
591
			echo " " . $package['name'];
592
		}
593
		if (platform_booting() != true) {
594
			stop_service(get_package_internal_name($package));
595
		}
596
		sync_package($package['name']);
597
		update_status(gettext("Syncing packages...") . "\n");
598
	}
599

    
600
	if ($show_message == true) {
601
		echo " done.\n";
602
	}
603

    
604
	@unlink("/conf/needs_package_sync");
605
}
606

    
607
function uninstall_package($package_name) {
608
	global $config;
609

    
610
	$internal_name = $package_name;
611
	$id = get_package_id($package_name);
612
	if ($id >= 0) {
613
		$internal_name = get_package_internal_name($config['installedpackages']['package'][$id]);
614
		stop_service($internal_name);
615
	}
616
	$pkg_name = $g['pkg_prefix'] . $internal_name;
617

    
618
	if (is_pkg_installed($pkg_name)) {
619
		update_status(gettext("Removing package...") . "\n");
620
		pkg_delete($pkg_name);
621
	} else {
622
		delete_package_xml($package_name);
623
	}
624

    
625
	update_status(gettext("done.") . "\n");
626
}
627

    
628
function reinstall_package($package_name) {
629
	global $config, $g;
630

    
631
	$internal_name = $package_name;
632
	$id = get_package_id($package_name);
633
	if ($id >= 0) {
634
		$internal_name = get_package_internal_name($config['installedpackages']['package'][$id]);
635
	}
636
	$pkg_name = $g['pkg_prefix'] . $internal_name;
637
	pkg_install($pkg_name);
638
}
639

    
640
/* Run <custom_php_resync_config_command> */
641
function sync_package($package_name) {
642
	global $config, $builder_package_install;
643

    
644
	// If this code is being called by pfspkg_installer
645
	// which the builder system uses then return (ignore).
646
	if ($builder_package_install) {
647
		return;
648
	}
649

    
650
	if (empty($config['installedpackages']['package'])) {
651
		return;
652
	}
653

    
654
	if (($pkg_id = get_package_id($package_name)) == -1) {
655
		return; // This package doesn't really exist - exit the function.
656
	}
657

    
658
	if (!is_array($config['installedpackages']['package'][$pkg_id])) {
659
		return;	 // No package belongs to the pkg_id passed to this function.
660
	}
661

    
662
	$package =& $config['installedpackages']['package'][$pkg_id];
663
	if (!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
664
		log_error(sprintf(gettext("The %s package is missing its configuration file and must be reinstalled."), $package['name']));
665
		delete_package_xml($package['name']);
666
		return;
667
	}
668

    
669
	$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
670
	if (isset($pkg_config['nosync'])) {
671
		return;
672
	}
673

    
674
	/* Bring in package include files */
675
	if (!empty($pkg_config['include_file'])) {
676
		$include_file = $pkg_config['include_file'];
677
		if (file_exists($include_file)) {
678
			require_once($include_file);
679
		} else {
680
			log_error(sprintf(gettext('Reinstalling package %1$s because its include file(%2$s) is missing!'), $package['name'], $include_file));
681
			uninstall_package($package['name']);
682
			if (reinstall_package($package['name']) != 0) {
683
				log_error(sprintf(gettext("Reinstalling package %s failed. Take appropriate measures!!!"), $package['name']));
684
				return;
685
			}
686
			if (file_exists($include_file)) {
687
				require_once($include_file);
688
			} else {
689
				return;
690
			}
691
		}
692
	}
693

    
694
	if (!empty($pkg_config['custom_php_global_functions'])) {
695
		eval($pkg_config['custom_php_global_functions']);
696
	}
697
	if (!empty($pkg_config['custom_php_resync_config_command'])) {
698
		eval($pkg_config['custom_php_resync_config_command']);
699
	}
700
}
701

    
702
/* Read info.xml installed by package and return an array */
703
function read_package_config($package_name) {
704
	global $g;
705

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

    
708
	if (!file_exists($pkg_info_xml)) {
709
		return false;
710
	}
711

    
712
	$pkg_info = parse_xml_config_pkg($pkg_info_xml, 'pfsensepkgs');
713

    
714
	if (empty($pkg_info)) {
715
		return false;
716
	}
717

    
718
	/* it always returns an array with 1 item */
719
	return $pkg_info['package'][0];
720
}
721

    
722
/* Read package configurationfile and return an array */
723
function read_package_configurationfile($package_name) {
724
	global $config, $g;
725

    
726
	$pkg_config = array();
727
	$id = get_package_id($package_name);
728

    
729
	if ($id < 0 || !isset($config['installedpackages']['package'][$id]['configurationfile'])) {
730
		return $pkg_config;
731
	}
732

    
733
	$pkg_configurationfile = $config['installedpackages']['package'][$id]['configurationfile'];
734

    
735
	if (empty($pkg_configurationfile) || !file_exists('/usr/local/pkg/' . $pkg_configurationfile)) {
736
		return $pkg_config;
737
	}
738

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

    
741
	return $pkg_config;
742
}
743

    
744
function get_after_install_info($package_name) {
745
	$pkg_config = read_package_config($package_name);
746

    
747
	if (isset($pkg_config['after_install_info'])) {
748
		return $pkg_config['after_install_info'];
749
	}
750

    
751
	return '';
752
}
753

    
754
function eval_once($toeval) {
755
	global $evaled;
756
	if (!$evaled) {
757
		$evaled = array();
758
	}
759
	$evalmd5 = md5($toeval);
760
	if (!in_array($evalmd5, $evaled)) {
761
		@eval($toeval);
762
		$evaled[] = $evalmd5;
763
	}
764
	return;
765
}
766

    
767
function install_package_xml($package_name) {
768
	global $g, $config, $pkg_interface;
769

    
770
	if (($pkg_info = read_package_config($package_name)) == false) {
771
		return false;
772
	}
773

    
774
	/* safe side. Write config below will send to ro again. */
775

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

    
779
	/* add package information to config.xml */
780
	$pkgid = get_package_id($pkg_info['name']);
781
	update_status(gettext("Saving updated package information...") . "\n");
782
	if ($pkgid == -1) {
783
		$config['installedpackages']['package'][] = $pkg_info;
784
		$changedesc = sprintf(gettext("Installed %s package."), $pkg_info['name']);
785
		$to_output = gettext("done.") . "\n";
786
	} else {
787
		$config['installedpackages']['package'][$pkgid] = $pkg_info;
788
		$changedesc = sprintf(gettext("Overwrote previous installation of %s."), $pkg_info['name']);
789
		$to_output = gettext("overwrite!") . "\n";
790
	}
791
	unlink_if_exists('/conf/needs_package_sync');
792
	write_config(sprintf(gettext("Intermediate config write during package install for %s."), $pkg_info['name']));
793
	update_status($to_output);
794

    
795
	if (($pkgid = get_package_id($package_name)) == -1) {
796
		update_status(sprintf(gettext('The %1$s package is not installed.%2$sInstallation aborted.'), $package_name, "\n\n"));
797

    
798
		uninstall_package($package_name);
799
		write_config($changedesc);
800
		log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
801
		update_status(gettext("Failed to install package.") . "\n");
802
		return false;
803
	}
804

    
805
	if (file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
806
		update_status(gettext("Loading package configuration... "));
807
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $pkg_info['configurationfile'], "packagegui");
808
		update_status(gettext("done.") . "\n");
809
		update_status(gettext("Configuring package components...") . "\n");
810
		if (!empty($pkg_config['filter_rules_needed'])) {
811
			$config['installedpackages']['package'][$pkgid]['filter_rule_function'] = $pkg_config['filter_rules_needed'];
812
		}
813
		/* modify system files */
814

    
815
		/* if a require exists, include it.  this will
816
		 * show us where an error exists in a package
817
		 * instead of making us blindly guess
818
		 */
819
		$missing_include = false;
820
		if ($pkg_config['include_file'] <> "") {
821
			update_status(gettext("Loading package instructions...") . "\n");
822
			if (file_exists($pkg_config['include_file'])) {
823
				pkg_debug("require_once('{$pkg_config['include_file']}')\n");
824
				require_once($pkg_config['include_file']);
825
			} else {
826
				pkg_debug("Missing include {$pkg_config['include_file']}\n");
827
				$missing_include = true;
828
				update_status(sprintf(gettext("Include %s is missing!"), basename($pkg_config['include_file'])) . "\n");
829

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

    
838
		/* custom commands */
839
		update_status(gettext("Custom commands...") . "\n");
840
		if ($missing_include == false) {
841
			if ($pkg_config['custom_php_global_functions'] <> "") {
842
				update_status(gettext("Executing custom_php_global_functions()..."));
843
				eval_once($pkg_config['custom_php_global_functions']);
844
				update_status(gettext("done.") . "\n");
845
			}
846
			if ($pkg_config['custom_php_install_command']) {
847
				update_status(gettext("Executing custom_php_install_command()..."));
848
				eval_once($pkg_config['custom_php_install_command']);
849
				update_status(gettext("done.") . "\n");
850
			}
851
			if ($pkg_config['custom_php_resync_config_command'] <> "") {
852
				update_status(gettext("Executing custom_php_resync_config_command()..."));
853
				eval_once($pkg_config['custom_php_resync_config_command']);
854
				update_status(gettext("done.") . "\n");
855
			}
856
		}
857
		/* sidebar items */
858
		if (is_array($pkg_config['menu'])) {
859
			update_status(gettext("Menu items... "));
860
			foreach ($pkg_config['menu'] as $menu) {
861
				if (is_array($config['installedpackages']['menu'])) {
862
					foreach ($config['installedpackages']['menu'] as $amenu) {
863
						if ($amenu['name'] == $menu['name']) {
864
							continue 2;
865
						}
866
					}
867
				} else {
868
					$config['installedpackages']['menu'] = array();
869
				}
870
				$config['installedpackages']['menu'][] = $menu;
871
			}
872
			update_status(gettext("done.") . "\n");
873
		}
874
		/* services */
875
		if (is_array($pkg_config['service'])) {
876
			update_status(gettext("Services... "));
877
			foreach ($pkg_config['service'] as $service) {
878
				if (is_array($config['installedpackages']['service'])) {
879
					foreach ($config['installedpackages']['service'] as $aservice) {
880
						if ($aservice['name'] == $service['name']) {
881
							continue 2;
882
						}
883
					}
884
				} else {
885
					$config['installedpackages']['service'] = array();
886
				}
887
				$config['installedpackages']['service'][] = $service;
888
			}
889
			update_status(gettext("done.") . "\n");
890
		}
891
 		if (is_array($pkg_config['tabs'])) {
892
 			$config['installedpackages']['package'][$pkgid]['tabs'] = $pkg_config['tabs'];
893
 		}
894
		/* plugins */
895
		if (isset($pkg_config['include_file'])) {
896
			$config['installedpackages']['package'][$pkgid]['include_file'] = $pkg_config['include_file'];
897
		}
898
		if (is_array($pkg_config['plugins']['item'])) {
899
			$config['installedpackages']['package'][$pkgid]['plugins']['item'] = $pkg_config['plugins']['item'];
900
		}
901
	} else {
902
		pkg_debug("Unable to find config file\n");
903
		update_status(gettext("Loading package configuration... failed!") . "\n\n" . gettext("Installation aborted."));
904
		pkg_debug(gettext("Unable to load package configuration. Installation aborted.") ."\n");
905

    
906
		uninstall_package($package_name);
907
		write_config($changedesc);
908
		log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
909
		update_status(gettext("Failed to install package.") . "\n");
910
		return false;
911
	}
912

    
913
	update_status(gettext("Writing configuration... "));
914
	write_config($changedesc);
915
	log_error(sprintf(gettext("Successfully installed package: %s."), $pkg_info['name']));
916
	update_status(gettext("done.") . "\n");
917
	if ($pkg_info['after_install_info']) {
918
		update_status($pkg_info['after_install_info']);
919
	}
920

    
921
	/* set up package logging streams */
922
	if ($pkg_info['logging']) {
923
		system_syslogd_start(true);
924
	}
925

    
926
	return true;
927
}
928

    
929
function delete_package_xml($package_name, $when = "post-deinstall") {
930
	global $g, $config, $pkg_interface;
931

    
932

    
933
	$pkgid = get_package_id($package_name);
934
	if ($pkgid == -1) {
935
		update_status(sprintf(gettext('The %1$s package is not installed.%2$sDeletion aborted.'), $package_name, "\n\n"));
936
		ob_flush();
937
		sleep(1);
938
		return;
939
	}
940
	pkg_debug(sprintf(gettext("Removing %s package... "), $package_name));
941
	update_status(sprintf(gettext("Removing %s components..."), $package_name) . "\n");
942
	/* parse package configuration */
943
	$packages = &$config['installedpackages']['package'];
944
	$menus =& $config['installedpackages']['menu'];
945
	$services = &$config['installedpackages']['service'];
946
	$pkg_info =& $packages[$pkgid];
947
	if (file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
948
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $packages[$pkgid]['configurationfile'], "packagegui");
949
		/* remove menu items */
950
		if (is_array($pkg_config['menu'])) {
951
			update_status(gettext("Menu items... "));
952
			if (is_array($pkg_config['menu']) && is_array($menus)) {
953
				foreach ($pkg_config['menu'] as $menu) {
954
					foreach ($menus as $key => $instmenu) {
955
						if ($instmenu['name'] == $menu['name']) {
956
							unset($menus[$key]);
957
							break;
958
						}
959
					}
960
				}
961
			}
962
			update_status(gettext("done.") . "\n");
963
		}
964
		/* remove services */
965
		if (is_array($pkg_config['service'])) {
966
			update_status(gettext("Services... "));
967
			if (is_array($pkg_config['service']) && is_array($services)) {
968
				foreach ($pkg_config['service'] as $service) {
969
					foreach ($services as $key => $instservice) {
970
						if ($instservice['name'] == $service['name']) {
971
							if (platform_booting() != true) {
972
								stop_service($service['name']);
973
							}
974
							if ($service['rcfile']) {
975
								$prefix = RCFILEPREFIX;
976
								if (!empty($service['prefix'])) {
977
									$prefix = $service['prefix'];
978
								}
979
								if (file_exists("{$prefix}{$service['rcfile']}")) {
980
									@unlink("{$prefix}{$service['rcfile']}");
981
								}
982
							}
983
							unset($services[$key]);
984
						}
985
					}
986
				}
987
			}
988
			update_status(gettext("done.") . "\n");
989
		}
990
		/*
991
		 * XXX: Otherwise inclusion of config.inc again invalidates actions taken.
992
		 *	Same is done during installation.
993
		 */
994
		write_config(sprintf(gettext("Intermediate config write during package removal for %s."), $package_name));
995

    
996
		/*
997
		 * If a require exists, include it.	 this will
998
		 * show us where an error exists in a package
999
		 * instead of making us blindly guess
1000
		 */
1001
		$missing_include = false;
1002
		if ($pkg_config['include_file'] <> "") {
1003
			update_status(gettext("Loading package instructions...") . "\n");
1004
			if (file_exists($pkg_config['include_file'])) {
1005
				pkg_debug("require_once(\"{$pkg_config['include_file']}\")\n");
1006
				require_once($pkg_config['include_file']);
1007
			} else {
1008
				pkg_debug("Missing include {$pkg_config['include_file']}\n");
1009
				$missing_include = true;
1010
				update_status(sprintf(gettext("Include file %s could not be found for inclusion."), basename($pkg_config['include_file'])) . "\n");
1011
			}
1012
		}
1013
		/* ermal
1014
		 * NOTE: It is not possible to handle parse errors on eval.
1015
		 * So we prevent it from being run at all to not interrupt all the other code.
1016
		 */
1017
		if ($when == "deinstall" && $missing_include == false) {
1018
			/* evaluate this package's global functions and pre deinstall commands */
1019
			if ($pkg_config['custom_php_global_functions'] <> "") {
1020
				eval_once($pkg_config['custom_php_global_functions']);
1021
			}
1022
			if ($pkg_config['custom_php_pre_deinstall_command'] <> "") {
1023
				eval_once($pkg_config['custom_php_pre_deinstall_command']);
1024
			}
1025
		}
1026
		/* deinstall commands */
1027
		if ($when == "deinstall" && $pkg_config['custom_php_deinstall_command'] <> "") {
1028
			update_status(gettext("Deinstall commands... "));
1029
			if ($missing_include == false) {
1030
				eval_once($pkg_config['custom_php_deinstall_command']);
1031
				update_status(gettext("done.") . "\n");
1032
			} else {
1033
				update_status("\n". gettext("Not executing custom deinstall hook because an include is missing.") . "\n");
1034
			}
1035
		}
1036
	}
1037
	/* syslog */
1038
	$need_syslog_restart = false;
1039
	if (is_array($pkg_info['logging']) && $pkg_info['logging']['logfilename'] <> "") {
1040
		update_status(gettext("Syslog entries... "));
1041
		@unlink_if_exists("{$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
1042
		update_status("done.\n");
1043
		$need_syslog_restart = true;
1044
	}
1045

    
1046
	if ($when == "post-deinstall") {
1047
		/* remove config.xml entries */
1048
		update_status(gettext("Configuration... "));
1049
		unset($config['installedpackages']['package'][$pkgid]);
1050
		update_status(gettext("done.") . "\n");
1051
		write_config(sprintf(gettext("Removed %s package."), $package_name));
1052
		/* remove package entry from /etc/syslog.conf if needed */
1053
		/* this must be done after removing the entries from config.xml */
1054
		if ($need_syslog_restart) {
1055
			system_syslogd_start(true);
1056
		}
1057
	}
1058
}
1059

    
1060
/*
1061
 * Used during upgrade process or retore backup process, verify all
1062
 * packages installed in config.xml and install pkg accordingly
1063
 */
1064
function package_reinstall_all() {
1065
	global $g, $config, $pkg_interface;
1066

    
1067
	if (platform_booting()) {
1068
		$upgrade = file_exists('/conf/needs_package_sync');
1069
		$restore = file_exists('/conf/needs_package_sync_after_reboot');
1070
	} else {
1071
		$upgrade = false;
1072
		$restore = false;
1073
	}
1074

    
1075
	if ((!isset($config['installedpackages']['package']) ||
1076
	    !is_array($config['installedpackages']['package'])) && !$upgrade) {
1077
		return true;
1078
	}
1079

    
1080
	/* When restoring a backup, use default pkg repo */
1081
	if ($restore) {
1082
		/*
1083
		 * Configure default pkg repo for current version instead of
1084
		 * using it from backup, that could be older
1085
		 */
1086
		$default_repo = pkg_get_default_repo();
1087
		$current_repo_path = "";
1088
		if (!empty($config['system']['pkg_repo_conf_path'])) {
1089
			$current_repo_path =
1090
			    $config['system']['pkg_repo_conf_path'];
1091
		}
1092

    
1093
		if ($current_repo_path != $default_repo['path']) {
1094
			$config['system']['pkg_repo_conf_path'] =
1095
			    $default_repo['path'];
1096
			write_config(
1097
			    "Configured default pkg repo after restore");
1098
			pkg_switch_repo($default_repo['path']);
1099
		}
1100
	}
1101

    
1102
	/* During boot after upgrade, wait for internet connection */
1103
	if ($upgrade) {
1104
		update_status(gettext("Waiting for Internet connection to update pkg metadata and finish package reinstallation"));
1105
		$ntries = 3;
1106
		while ($ntries > 0) {
1107
			if (pkg_update(true)) {
1108
				break;
1109
			}
1110
			update_status('.');
1111
			sleep(1);
1112
			$ntries--;
1113
		}
1114
		update_status("\n");
1115

    
1116
		if ($ntries == 0) {
1117
			file_notice(gettext("Package reinstall"),
1118
			    gettext("Package reinstall process was ABORTED due to lack of internet connectivity"));
1119
			return false;
1120
		}
1121
	}
1122

    
1123
	if ($upgrade &&
1124
	    file_exists("{$g['cf_conf_path']}/packages_to_reinstall_after_upgrade.txt")) {
1125
		$package_list = file("{$g['cf_conf_path']}/packages_to_reinstall_after_upgrade.txt",
1126
		    FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
1127
		unlink_if_exists("{$g['cf_conf_path']}/packages_to_reinstall_after_upgrade.txt");
1128
	} else {
1129
		if (!isset($config['installedpackages']['package']) || !is_array($config['installedpackages']['package'])) {
1130
			return true;
1131
		}
1132
		$package_list = array();
1133
		foreach ($config['installedpackages']['package'] as $package) {
1134
			$package_list[] = get_package_internal_name($package);
1135
		}
1136
	}
1137

    
1138
	if (!empty($package_list)) {
1139
		$pkg_info = get_pkg_info();
1140
	}
1141
	foreach ($package_list as $package) {
1142
		$found = false;
1143
		foreach ($pkg_info as $pkg) {
1144
			pkg_remove_prefix($pkg['name']);
1145
			if ($pkg['name'] == $package) {
1146
				pkg_install($g['pkg_prefix'] . $package, true);
1147
				$found = true;
1148
				break;
1149
			}
1150
		}
1151

    
1152
		if (!$found) {
1153
			if (!function_exists("file_notice")) {
1154
				require_once("notices.inc");
1155
			}
1156

    
1157
			file_notice(gettext("Package reinstall"),
1158
			    sprintf(gettext("Package %s does not exist in current %s version and it has been removed."),
1159
			    $package, $g['product_name']));
1160
			uninstall_package($package);
1161
		}
1162
	}
1163

    
1164
	/*
1165
	 * Verify remaining binary packages not present in current config
1166
	 * during backup restore and remove them
1167
	 */
1168
	if ($restore) {
1169
		$installed_packages = get_pkg_info('all', true, true);
1170
		foreach ($installed_packages as $package) {
1171
			$shortname = $package['name'];
1172
			pkg_remove_prefix($shortname);
1173
			if (get_package_id($shortname) != -1) {
1174
				continue;
1175
			}
1176
			pkg_delete($package['name']);
1177
		}
1178
	}
1179

    
1180
	return true;
1181
}
1182

    
1183
function stop_packages() {
1184
	require_once("config.inc");
1185
	require_once("functions.inc");
1186
	require_once("filter.inc");
1187
	require_once("shaper.inc");
1188
	require_once("captiveportal.inc");
1189
	require_once("pkg-utils.inc");
1190
	require_once("pfsense-utils.inc");
1191
	require_once("service-utils.inc");
1192

    
1193
	global $config, $g;
1194

    
1195
	log_error(gettext("Stopping all packages."));
1196

    
1197
	$rcfiles = glob(RCFILEPREFIX . "*.sh");
1198
	if (!$rcfiles) {
1199
		$rcfiles = array();
1200
	} else {
1201
		$rcfiles = array_flip($rcfiles);
1202
		if (!$rcfiles) {
1203
			$rcfiles = array();
1204
		}
1205
	}
1206

    
1207
	if (is_array($config['installedpackages']['package'])) {
1208
		foreach ($config['installedpackages']['package'] as $package) {
1209
			echo " Stopping package {$package['name']}...";
1210
			$internal_name = get_package_internal_name($package);
1211
			stop_service($internal_name);
1212
			unset($rcfiles[RCFILEPREFIX . strtolower($internal_name) . ".sh"]);
1213
			echo "done.\n";
1214
		}
1215
	}
1216

    
1217
	foreach ($rcfiles as $rcfile => $number) {
1218
		$shell = @popen("/bin/sh", "w");
1219
		if ($shell) {
1220
			echo " Stopping {$rcfile}...";
1221
			if (!@fwrite($shell, "{$rcfile} stop >>/tmp/bootup_messages 2>&1")) {
1222
				if ($shell) {
1223
					pclose($shell);
1224
				}
1225
				$shell = @popen("/bin/sh", "w");
1226
			}
1227
			echo "done.\n";
1228
			pclose($shell);
1229
		}
1230
	}
1231
}
1232

    
1233
/* Identify which meta package is installed */
1234
function get_meta_pkg_name() {
1235
	global $g;
1236

    
1237
	/* XXX: Use pkg annotation */
1238
	if (is_pkg_installed($g['product_name'])) {
1239
		return $g['product_name'];
1240
	}
1241
	foreach ($g['alternativemetaports'] as $suffix) {
1242
		if (is_pkg_installed($g['product_name'] . '-' . $suffix)) {
1243
			return $g['product_name'] . '-' . $suffix;
1244
		}
1245
	}
1246
	return false;
1247
}
1248

    
1249
/* Identify which base package is installed */
1250
function get_base_pkg_name() {
1251
	global $g;
1252

    
1253
	/* XXX: Use pkg annotation */
1254
	if (is_pkg_installed($g['product_name'] . '-base-' . $g['platform'])) {
1255
		return $g['product_name'] . '-base-' . $g['platform'];
1256
	} else if (is_pkg_installed($g['product_name'] . '-base')) {
1257
		return $g['product_name'] . '-base';
1258
	}
1259
	return false;
1260
}
1261

    
1262
/* Verify if system needs upgrade (meta package or base) */
1263
function get_system_pkg_version($baseonly = false, $use_cache = true) {
1264
	global $g;
1265

    
1266
	$cache_file = $g['version_cache_file'];
1267
	$rc_file = $cache_file . '.rc';
1268

    
1269
	$rc = "";
1270
	if ($use_cache && file_exists($rc_file) &&
1271
	    (time()-filemtime($rc_file) < $g['version_cache_refresh'])) {
1272
		$rc = chop(@file_get_contents($rc_file));
1273
	}
1274

    
1275
	if ($rc == "2") {
1276
		$output = @file_get_contents($cache_file);
1277
	} else if ($rc != "0") {
1278
		$output = exec(
1279
		    "/usr/local/sbin/{$g['product_name']}-upgrade -c", $_gc,
1280
		    $rc);
1281

    
1282
		/* Update cache if it succeeded */
1283
		if ($rc == 0 || $rc == 2) {
1284
			@file_put_contents($cache_file, $output);
1285
			@file_put_contents($rc_file, $rc);
1286
		}
1287
	}
1288

    
1289
	/* pfSense-upgrade returns 2 when there is a new version */
1290
	if ($rc == "2") {
1291
		$new_version = explode(' ', $output)[0];
1292
	}
1293

    
1294
	$base_pkg = get_base_pkg_name();
1295
	$meta_pkg = get_meta_pkg_name();
1296

    
1297
	if (!$base_pkg || !$meta_pkg) {
1298
		return false;
1299
	}
1300

    
1301
	$info = get_pkg_info($base_pkg, true, true);
1302

    
1303
	$pkg_info = array();
1304
	foreach ($info as $item) {
1305
		if ($item['name'] == $base_pkg) {
1306
			$pkg_info = $item;
1307
			break;
1308
		}
1309
	}
1310

    
1311
	if (empty($pkg_info) || (!$baseonly && ($pkg_info['version'] ==
1312
	    $pkg_info['installed_version']))) {
1313
		$info = get_pkg_info($meta_pkg, true, true);
1314

    
1315
		foreach ($info as $item) {
1316
			if ($item['name'] == $meta_pkg) {
1317
				$pkg_info = $item;
1318
				break;
1319
			}
1320
		}
1321
	}
1322

    
1323
	if (empty($pkg_info)) {
1324
		return false;
1325
	}
1326

    
1327
	$result = array(
1328
	    'version'           => $new_version ?: $pkg_info['version'],
1329
	    'installed_version' => $pkg_info['installed_version']
1330
	);
1331

    
1332
	$result['pkg_version_compare'] = pkg_version_compare(
1333
	    $result['installed_version'], $result['version']);
1334

    
1335
	return $result;
1336
}
1337

    
1338
/* List available repos */
1339
function pkg_list_repos() {
1340
	global $g;
1341

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

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

    
1346
	$default = array(
1347
	    'name' => 'Default',
1348
	    'path' => $path . "/{$g['product_name']}-repo.conf",
1349
	    'descr' => $default_descr
1350
	);
1351

    
1352
	$result = array($default);
1353

    
1354
	$conf_files = glob("{$path}/{$g['product_name']}-repo-*.conf");
1355
	foreach ($conf_files as $conf_file) {
1356
		$descr_file = preg_replace('/.conf$/', '.descr', $conf_file);
1357
		if (file_exists($descr_file)) {
1358
			$descr_content = file($descr_file);
1359
			$descr = chop($descr_content[0]);
1360
		} else {
1361
			$descr = 'Unknown';
1362
		}
1363
		if (!preg_match('/-repo-(.*).conf/', $conf_file, $matches)) {
1364
			continue;
1365
		}
1366
		$entry = array(
1367
		    'name' => ucfirst(strtolower($matches[1])),
1368
		    'path' => $conf_file,
1369
		    'descr' => $descr
1370
		);
1371
		if (file_exists($conf_file . ".default")) {
1372
			$entry['default'] = true;
1373
		}
1374
		$result[] = $entry;
1375
	}
1376

    
1377
	return $result;
1378
}
1379

    
1380
function pkg_get_default_repo() {
1381
	$repos = pkg_list_repos();
1382

    
1383
	foreach ($repos as $repo) {
1384
		if (isset($repo['default'])) {
1385
			return $repo;
1386
		}
1387
	}
1388

    
1389
	/* No default found, return the first one */
1390
	return ($repos[0]);
1391
}
1392

    
1393
/* List available repos on a format to be used by selectors */
1394
function pkg_build_repo_list() {
1395
	$repos = pkg_list_repos();
1396
	$list = array();
1397

    
1398
	foreach ($repos as $repo) {
1399
		$list[$repo['name']] = $repo['descr'];
1400
	}
1401

    
1402
	return($list);
1403
}
1404

    
1405
/* Find repo by path */
1406
function pkg_get_repo_name($path) {
1407
	$repos = pkg_list_repos();
1408

    
1409
	$default = $repos[0]['name'];
1410
	foreach ($repos as $repo) {
1411
		if ($repo['path'] == $path) {
1412
			return $repo['name'];
1413
		}
1414
		if (isset($repo['default'])) {
1415
			$default = $repo['name'];
1416
		}
1417
	}
1418

    
1419
	/* Default */
1420
	return $default;
1421
}
1422

    
1423
/* Setup pkg.conf according current repo */
1424
function pkg_conf_setup() {
1425
	global $g;
1426

    
1427
	$pkg_conf_path = "/usr/local/etc/pkg.conf";
1428
	$conf = "/usr/local/etc/pkg/repos/{$g['product_name']}.conf";
1429
	if (!file_exists($conf)) {
1430
		return;
1431
	}
1432

    
1433
	$real_conf = readlink($conf);
1434

    
1435
	if (!$real_conf) {
1436
		return;
1437
	}
1438

    
1439
	$abi_file = str_replace('.conf', '.abi', $real_conf);
1440
	$altabi_file = str_replace('.conf', '.altabi', $real_conf);
1441

    
1442
	$pkg_conf = array();
1443
	if (file_exists($abi_file) && file_exists($altabi_file)) {
1444
		$abi = file_get_contents($abi_file);
1445
		$altabi = file_get_contents($altabi_file);
1446

    
1447
		$pkg_conf = array(
1448
			"ABI={$abi}",
1449
			"ALTABI={$altabi}"
1450
		);
1451
	}
1452

    
1453
	file_put_contents($pkg_conf_path, $pkg_conf);
1454
}
1455

    
1456
/* Switch between stable and devel repos */
1457
function pkg_switch_repo($path) {
1458
	global $g;
1459

    
1460
	safe_mkdir("/usr/local/etc/pkg/repos");
1461
	@unlink("/usr/local/etc/pkg/repos/{$g['product_name']}.conf");
1462
	@symlink($path, "/usr/local/etc/pkg/repos/{$g['product_name']}.conf");
1463

    
1464
	pkg_conf_setup();
1465

    
1466
	/* Update pfSense_version cache */
1467
	mwexec_bg("/etc/rc.update_pkg_metadata now");
1468
	return;
1469
}
1470

    
1471
$FQDN = "https://ews.netgate.com/pfupdate";
1472
$refreshinterval = (24 * 3600);	// 24 hours
1473
$idfile = "/var/db/uniqueid";
1474
$repopath = "/usr/local/share/{$g['product_name']}/pkg/repos";
1475
$configflename = "{$repopath}/{$g['product_name']}-repo-custom.conf";
1476

    
1477
// Update the list of available repositories from the server. This will allow migration to another
1478
// update repository should the existing one becomes unavailable
1479
function update_repos() {
1480
	global $g, $config, $idfile, $FQDN, $repopath;
1481

    
1482
	if (file_exists($idfile)) {
1483
		// If the custom repository definition does not exist, or is more than 24 hours old
1484
		// Fetch a copy from the server
1485
		if ( (! file_exists($configflename)) || ( time()-filemtime($configflename) > $refreshinterval)) {
1486
			if (function_exists('curl_version')) {
1487
				// Gather some information about the system so the proper repo can be returned
1488
				$nid = file_get_contents($idfile);
1489
				$serial = system_get_serial();
1490
				$arch = php_uname('p');
1491

    
1492
				$base_pkg = get_base_pkg_name();
1493

    
1494
				$info = get_pkg_info($base_pkg, true, true);
1495

    
1496
				$pkg_info = array();
1497
					foreach ($info as $item) {
1498
						if ($item['name'] == $base_pkg) {
1499
							$pkg_info = $item;
1500
							break;
1501
						}
1502
					}
1503

    
1504
				$version = $pkg_info['installed_version'];
1505
				$locale = $config['system']['language'];
1506
				// Architecture comes from the last word in the uname output
1507
				$arch = array_pop(explode(' ', php_uname('p')));
1508

    
1509
				$post = ['uid' => $nid,
1510
						 'language' => $locale,
1511
						 'serial' => $serial,
1512
						 'version' => $version,
1513
						 'arch' => $arch
1514
						];
1515

    
1516
				$ch = curl_init();
1517
				curl_setopt($ch, CURLOPT_HEADER, 0);
1518
				curl_setopt($ch, CURLOPT_VERBOSE, 0);
1519
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
1520
				curl_setopt($ch, CURLOPT_USERAGENT, $g['product_name'] . '/' . $g['product_version']);
1521
				curl_setopt($ch, CURLOPT_URL, $FQDN);
1522
				curl_setopt($ch, CURLOPT_POST, true);
1523
				curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
1524
				curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,4);
1525
				$response = curl_exec($ch);
1526
				$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1527
				curl_close($ch);
1528

    
1529
				if ($status == 200) {
1530
					save_repo($response);
1531
				}
1532
			}
1533
		}
1534
	}
1535
}
1536

    
1537
// Parse the received JSON data and save the custom repository information
1538
function save_repo($json) {
1539
	global $repopath, $g;
1540
	$repo = json_decode($json, true);
1541

    
1542
	if (($repo != NULL) && isset($repo['abi']) && isset($repo['altabi']) && isset($repo['conf']) &&
1543
	    isset($repo['descr']) && isset($repo['name']) && (strlen($repo['conf']) > 10)) {
1544
		$basename = "{$repopath}/{$g['product_name']}-repo-custom.";
1545

    
1546
		file_put_contents($basename . "conf", base64_decode($repo['conf']));
1547
		file_put_contents($basename . "descr", $repo['descr']);
1548
		file_put_contents($basename . "abi", $repo['abi']);
1549
		file_put_contents($basename . "altabi", $repo['altabi']);
1550
		file_put_contents($basename . "name", $repo['name']);
1551
		file_put_contents($basename . "help", $repo['help']);
1552
	} else {
1553
		// If there was anything wrong with the custom repository definition, remove the help text
1554
		// to avoid possible confusion
1555
		if (file_exists($basename . "help")) {
1556
			unlink($basename . "help");
1557
		}
1558
	}
1559
}
1560

    
1561
?>
(40-40/60)