Project

General

Profile

Download (34.9 KB) Statistics
| Branch: | Tag: | Revision:
1 7597c8e8 Colin Smith
<?php
2 1262d7e7 Renato Botelho
/*
3
 * pkg-utils.inc
4 a2febf9a Stephen Beaver
 *
5 ac24dc24 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2005-2006 Colin Smith (ethethlay@gmail.com)
7 81299b5c Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
8 ac24dc24 Renato Botelho
 * All rights reserved.
9 a2febf9a Stephen Beaver
 *
10 b12ea3fb Renato Botelho
 * 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 a2febf9a Stephen Beaver
 *
14 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
15 a2febf9a Stephen Beaver
 *
16 b12ea3fb Renato Botelho
 * 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 8c6516d1 Colin Smith
 */
22 523855b0 Scott Ullrich
23 01a6e665 Ermal
require_once("globals.inc");
24 7eea4407 Ermal
require_once("service-utils.inc");
25 1e8644ca Renato Botelho
26 49aec489 Phil Davis
if (file_exists("/cf/conf/use_xmlreader")) {
27 093bcebc Scott Ullrich
	require_once("xmlreader.inc");
28 49aec489 Phil Davis
} else {
29 093bcebc Scott Ullrich
	require_once("xmlparse.inc");
30 49aec489 Phil Davis
}
31 33b7cc0d Colin Smith
32 1e8644ca Renato Botelho
require_once("pfsense-utils.inc");
33 b47833cc Scott Ullrich
34 eab543ed Ermal
if (!function_exists("pkg_debug")) {
35
	/* set up logging if needed */
36
	function pkg_debug($msg) {
37
		global $g, $debug, $fd_log;
38
39 49aec489 Phil Davis
		if (!$debug) {
40 eab543ed Ermal
			return;
41 49aec489 Phil Davis
		}
42 eab543ed Ermal
43
		if (!$fd_log) {
44 b27ac786 Renato Botelho
			if (!$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_debug.log", "w")) {
45 e791dee8 Renato Botelho
				update_status(gettext("Warning, could not open log for writing.") . "\n");
46 49aec489 Phil Davis
			}
47 eab543ed Ermal
		}
48
		@fwrite($fd_log, $msg);
49
	}
50
}
51
52 c078dd89 Renato Botelho
/* 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 c2eb2508 Renato Botelho
/* 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 e7405fbf Scott Ullrich
}
68 33b7cc0d Colin Smith
69 d3ff1e59 Renato Botelho
/* Execute pkg update when it's necessary */
70
function pkg_update($force = false) {
71
	global $g;
72
73 6cf87aec Renato Botelho
	return pkg_call("update" . ($force ? " -f" : ""));
74 d3ff1e59 Renato Botelho
}
75
76 75bb92c4 Renato Botelho
/* return an array with necessary environment vars for pkg */
77 a948633c Renato Botelho
function pkg_env($extra_env = array()) {
78 75bb92c4 Renato Botelho
	global $config, $g;
79
80
	$pkg_env_vars = array(
81 e21ac870 Renato Botelho
		"LANG" => "C",
82 75bb92c4 Renato Botelho
		"HTTP_USER_AGENT" => $user_agent,
83 a120c849 Renato Botelho
		"ASSUME_ALWAYS_YES" => "true",
84
		"FETCH_TIMEOUT" => 5,
85
		"FETCH_RETRY" => 2
86 75bb92c4 Renato Botelho
	);
87
88 a7fcbc1e jim-p
	if (!empty($config['system']['proxyurl'])) {
89
		$http_proxy = $config['system']['proxyurl'];
90
		if (!empty($config['system']['proxyport'])) {
91
			$http_proxy .= ':' . $config['system']['proxyport'];
92
		}
93
		$pkg_env_vars['HTTP_PROXY'] = $http_proxy;
94 1060378f jim-p
95
		if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
96
			$pkg_env_vars['HTTP_PROXY_AUTH'] = "basic:*:{$config['system']['proxyuser']}:{$config['system']['proxypass']}";
97
		}
98 a7fcbc1e jim-p
	}
99
100 dc61252a Renato Botelho
	if (isset($config['system']['use_mfs_tmpvar'])) {
101 75bb92c4 Renato Botelho
		$pkg_env_vars['PKG_DBDIR'] = '/root/var/db/pkg';
102
		$pkg_env_vars['PKG_CACHEDIR'] = '/root/var/cache/pkg';
103
	}
104
105 a948633c Renato Botelho
	foreach ($extra_env as $key => $value) {
106
		$pkg_env_vars[$key] = $value;
107
	}
108
109 75bb92c4 Renato Botelho
	return $pkg_env_vars;
110
}
111
112 c2eb2508 Renato Botelho
/* Execute a pkg call */
113 a948633c Renato Botelho
function pkg_call($params, $mute = false, $extra_env = array()) {
114 e791dee8 Renato Botelho
	global $g, $config;
115 6fd37d04 Renato Botelho
116 c2eb2508 Renato Botelho
	if (empty($params)) {
117
		return false;
118
	}
119
120 96bf5038 Renato Botelho
	$user_agent = $g['product_name'] . '/' . $g['product_version'];
121 3138b2e3 Renato Botelho
	if (!isset($config['system']['do_not_send_host_uuid'])) {
122 21dfcd61 Renato Botelho
		$user_agent .= ' : ' . get_single_sysctl('kern.hostuuid');
123 96bf5038 Renato Botelho
	}
124
125 6fd37d04 Renato Botelho
	$descriptorspec = array(
126
		1 => array("pipe", "w"), /* stdout */
127 a2febf9a Stephen Beaver
		2 => array("pipe", "w")	 /* stderr */
128 6fd37d04 Renato Botelho
	);
129
130 4b834523 Renato Botelho
131 6fd37d04 Renato Botelho
	pkg_debug("pkg_call(): {$params}\n");
132 75bb92c4 Renato Botelho
	$process = proc_open("/usr/sbin/pkg {$params}", $descriptorspec, $pipes,
133 a948633c Renato Botelho
	    '/', pkg_env($extra_env));
134 6fd37d04 Renato Botelho
135
	if (!is_resource($process)) {
136
		return false;
137
	}
138
139
	stream_set_blocking($pipes[1], 0);
140
	stream_set_blocking($pipes[2], 0);
141
142
	/* XXX: should be a tunnable? */
143
	$timeout = 300; // seconds
144
	$error_log = '';
145
146
	do {
147
		$write = array();
148
		$read = array($pipes[1], $pipes[2]);
149
		$except = array();
150
151
		$stream = stream_select($read, $write, $except, null, $timeout);
152
		if ($stream !== FALSE && $stream > 0) {
153
			foreach ($read as $pipe) {
154
				$content = stream_get_contents($pipe);
155
				if ($content == '') {
156
					continue;
157
				}
158
				if ($pipe === $pipes[1]) {
159
					if (!$mute) {
160 e791dee8 Renato Botelho
						update_status($content);
161 6fd37d04 Renato Botelho
					}
162
					flush();
163
				} else if ($pipe === $pipes[2]) {
164
					$error_log .= $content;
165
				}
166
			}
167 49aec489 Phil Davis
		}
168 a2febf9a Stephen Beaver
169 6fd37d04 Renato Botelho
		$status = proc_get_status($process);
170
	} while ($status['running']);
171 a2febf9a Stephen Beaver
172 6fd37d04 Renato Botelho
	fclose($pipes[1]);
173
	fclose($pipes[2]);
174
	proc_close($process);
175
176 4b834523 Renato Botelho
177 1c981897 Renato Botelho
	$rc = $status['exitcode'];
178 6fd37d04 Renato Botelho
179
	pkg_debug("pkg_call(): rc = {$rc}\n");
180
	if ($rc == 0) {
181
		return true;
182
	}
183
184
	pkg_debug("pkg_call(): error_log\n{$error_log}\n");
185
	if (!$mute) {
186 e791dee8 Renato Botelho
		update_status("\n\n" .  sprintf(gettext(
187
		    "ERROR!!! An error occurred on pkg execution (rc = %d) with parameters '%s':"),
188
		    $rc, $params) . "\n" . $error_log . "\n");
189 49aec489 Phil Davis
	}
190 a2febf9a Stephen Beaver
191 6fd37d04 Renato Botelho
	return false;
192 31e7e1bc Scott Ullrich
}
193
194 6fd37d04 Renato Botelho
/* Execute pkg with $params, fill stdout and stderr and return pkg rc */
195 a948633c Renato Botelho
function pkg_exec($params, &$stdout, &$stderr, $extra_env = array()) {
196 96bf5038 Renato Botelho
	global $g, $config;
197 6fd37d04 Renato Botelho
198
	if (empty($params)) {
199
		return -1;
200
	}
201
202 96bf5038 Renato Botelho
	$user_agent = $g['product_name'] . '/' . $g['product_version'];
203 3138b2e3 Renato Botelho
	if (!isset($config['system']['do_not_send_host_uuid'])) {
204 21dfcd61 Renato Botelho
		$user_agent .= ' : ' . get_single_sysctl('kern.hostuuid');
205 96bf5038 Renato Botelho
	}
206
207 6fd37d04 Renato Botelho
	$descriptorspec = array(
208
		1 => array("pipe", "w"), /* stdout */
209 a2febf9a Stephen Beaver
		2 => array("pipe", "w")	 /* stderr */
210 6fd37d04 Renato Botelho
	);
211
212 4b834523 Renato Botelho
213 6fd37d04 Renato Botelho
	pkg_debug("pkg_exec(): {$params}\n");
214 75bb92c4 Renato Botelho
	$process = proc_open("/usr/sbin/pkg {$params}", $descriptorspec, $pipes,
215 a948633c Renato Botelho
	    '/', pkg_env($extra_env));
216 6fd37d04 Renato Botelho
217
	if (!is_resource($process)) {
218
		return -1;
219
	}
220
221
	$stdout = '';
222
	while (($l = fgets($pipes[1])) !== FALSE) {
223
		$stdout .= $l;
224
	}
225
	fclose($pipes[1]);
226
227
	$stderr = '';
228
	while (($l = fgets($pipes[2])) !== FALSE) {
229
		$stderr .= $l;
230
	}
231
	fclose($pipes[2]);
232 c2eb2508 Renato Botelho
233 4b834523 Renato Botelho
234 6fd37d04 Renato Botelho
	return proc_close($process);
235 c2eb2508 Renato Botelho
}
236
237 8df1877b Renato Botelho
/* Compare 2 pkg versions and return:
238
 * '=' - versions are the same
239
 * '>' - $v1 > $v2
240
 * '<' - $v1 < $v2
241
 * '?' - Error
242
 */
243
function pkg_version_compare($v1, $v2) {
244
	if (empty($v1) || empty($v2)) {
245
		return '?';
246
	}
247
248 500dfdb8 Renato Botelho
	$rc = pkg_exec("version -t '{$v1}' '{$v2}'", $stdout, $stderr);
249 8df1877b Renato Botelho
250
	if ($rc != 0) {
251
		return '?';
252
	}
253
254
	return str_replace("\n", "", $stdout);
255
}
256
257 c2eb2508 Renato Botelho
/* Check if package is installed */
258
function is_pkg_installed($pkg_name) {
259
	global $g;
260
261 f5b1c660 Renato Botelho
	if (empty($pkg_name)) {
262
		return false;
263
	}
264 c2eb2508 Renato Botelho
265 500dfdb8 Renato Botelho
	return pkg_call("info -e " . $pkg_name, true);
266 c2eb2508 Renato Botelho
}
267
268 7379d80f Renato Botelho
/* Install package, $pkg_name should not contain prefix */
269 46903fb9 Renato Botelho
function pkg_install($pkg_name, $force = false) {
270 7379d80f Renato Botelho
	global $g;
271 e7553e1b Renato Botelho
	$result = false;
272 7379d80f Renato Botelho
273 f5b1c660 Renato Botelho
	$shortname = $pkg_name;
274
	pkg_remove_prefix($shortname);
275 7379d80f Renato Botelho
276 46903fb9 Renato Botelho
	$pkg_force = "";
277
	if ($force) {
278
		$pkg_force = "-f ";
279
	}
280
281 f5b1c660 Renato Botelho
	pkg_debug("Installing package {$shortname}\n");
282 46903fb9 Renato Botelho
	if ($force || !is_pkg_installed($pkg_name)) {
283 f5b1c660 Renato Botelho
		$result = pkg_call("install -y " . $pkg_force . $pkg_name);
284 e7553e1b Renato Botelho
		/* Cleanup cacke to free disk space */
285
		pkg_call("clean -y");
286 7379d80f Renato Botelho
	}
287
288 e7553e1b Renato Botelho
	return $result;
289 7379d80f Renato Botelho
}
290
291 c2eb2508 Renato Botelho
/* Delete package from FreeBSD, $pkg_name should not contain prefix */
292
function pkg_delete($pkg_name) {
293
	global $g;
294
295 f5b1c660 Renato Botelho
	$shortname = $pkg_name;
296
	pkg_remove_prefix($shortname);
297 c2eb2508 Renato Botelho
298 f5b1c660 Renato Botelho
	pkg_debug("Removing package {$shortname}\n");
299 c2eb2508 Renato Botelho
	if (is_pkg_installed($pkg_name)) {
300 f5b1c660 Renato Botelho
		pkg_call("delete -y " . $pkg_name);
301 c2eb2508 Renato Botelho
		/* Cleanup unecessary dependencies */
302 e1382589 Renato Botelho
		pkg_call("autoremove -y");
303 49aec489 Phil Davis
	}
304 8c6516d1 Colin Smith
}
305 43db85f8 Scott Ullrich
306 af5d93f6 Renato Botelho
/* Check if package is present in config.xml */
307
function is_package_installed($package_name) {
308
	return (get_package_id($package_name) != -1);
309 8c6516d1 Colin Smith
}
310 43db85f8 Scott Ullrich
311 af5d93f6 Renato Botelho
/* Find package array index */
312
function get_package_id($package_name) {
313 e65a287f Scott Ullrich
	global $config;
314
315 af5d93f6 Renato Botelho
	if (!is_array($config['installedpackages']['package'])) {
316
		return -1;
317
	}
318
319
	foreach ($config['installedpackages']['package'] as $idx => $pkg) {
320 a50534b6 Renato Botelho
		if ($pkg['name'] == $package_name ||
321 4e322e2c Phil Davis
		    get_package_internal_name($pkg) == $package_name) {
322 af5d93f6 Renato Botelho
			return $idx;
323 e65a287f Scott Ullrich
		}
324
	}
325 af5d93f6 Renato Botelho
326 e65a287f Scott Ullrich
	return -1;
327 8c6516d1 Colin Smith
}
328
329 af5d93f6 Renato Botelho
/* Return internal_name when it's defined, otherwise, returns name */
330
function get_package_internal_name($package_data) {
331
	if (isset($package_data['internal_name']) && ($package_data['internal_name'] != "")) {
332 75a01a7c Phil Davis
		/* e.g. name is Ipguard-dev, internal name is ipguard */
333 af5d93f6 Renato Botelho
		return $package_data['internal_name'];
334 75a01a7c Phil Davis
	} else {
335 af5d93f6 Renato Botelho
		return $package_data['name'];
336 75a01a7c Phil Davis
	}
337
}
338
339 a2febf9a Stephen Beaver
// Get information about packages.
340 04daf8b1 stilez
function get_pkg_info($pkgs = 'all', $remote_repo_usage_disabled = false, $installed_pkgs_only = false) {
341 587988f6 stilez
342 e791dee8 Renato Botelho
	global $g, $input_errors;
343 b2a66231 Ermal
344 04daf8b1 stilez
	$out = $err = $extra_param = '';
345 587988f6 stilez
	$rc = 0;
346 e65a287f Scott Ullrich
347 d65fc2ff Renato Botelho
	unset($pkg_filter);
348 04daf8b1 stilez
349 d65fc2ff Renato Botelho
	if (is_array($pkgs)) {
350
		$pkg_filter = $pkgs;
351 04daf8b1 stilez
		$pkgs = $g['pkg_prefix'] . '*';
352
	} elseif ($pkgs == 'all') {
353
		$pkgs = $g['pkg_prefix'] . '*';
354 65c94077 Renato Botelho
	}
355 b2a66231 Ermal
356 04daf8b1 stilez
	
357 9b7cfb3f Renato Botelho
	if (!function_exists('is_subsystem_dirty')) {
358
		require_once("util.inc");
359
	}
360
361
	/* Do not run remote operations if pkg has a lock */
362
	if (is_subsystem_dirty('pkg')) {
363 04daf8b1 stilez
		$remote_repo_usage_disabled = true;
364 9b7cfb3f Renato Botelho
		$lock = false;
365
	} else {
366
		$lock = true;
367
	}
368
369
	if ($lock) {
370
		mark_subsystem_dirty('pkg');
371
	}
372 587988f6 stilez
373 04daf8b1 stilez
	if ($remote_repo_usage_disabled) {
374
		$extra_param = "-U ";
375
	}
376
377
	if (!$installed_pkgs_only) {
378 587988f6 stilez
		$rc = pkg_exec("search {$extra_param}-R --raw-format json-compact " . $pkgs, $out, $err);
379
	}
380 04daf8b1 stilez
	if (($installed_pkgs_only || ($rc != 0 && $remote_repo_usage_disabled)) && is_package_installed($pkgs)) {
381
		/* Fall back on pkg info to return locally installed matching pkgs instead, if 
382 587988f6 stilez
		 *
383 04daf8b1 stilez
		 *   (1) only installed pkgs needed, or
384
		 *       we tried to check the local catalog copy (implying that we would have accepted incomplete/outdated pkg info)
385
		 *       but it didn't have any contents, or for other reasons returned an error. 
386
		 *   AND
387
		 *   (2) at least some pkgs matching <pattern> are installed
388 587988f6 stilez
		 *
389 04daf8b1 stilez
		 * Following an unsuccessful attempt to access a remote repo catalog, the local copy is wiped clear. Thereafter any
390
		 * "pkg search" will return an error until online+updated again. If the calling code would have accepted local copy info
391
		 * (which could be incomplete/out of date), then it makes sense to fall back on pkg info to at least return the known
392
		 * info about installed pkgs (pkg info should still work), instead of failing and returning no info at all. 
393
		 * For example, this at least enables offline view + management of installed pkgs in GUI/console.
394
		 *
395
		 * We skip this step if no matching pkgs are installed, because then pkg info would return a "no matching pkgs"
396
		 * RC code, even though this wouldn't be considered an "error" (and $out+$err would be correct empty strings if none match).
397
		 * Note that is_package_installed() is a wrapper for pkg info -e <pattern> which is what we need here.
398
		*/
399
		
400
		// ok, 1 or more packages match, so pkg info can be safely called to get the pkg list  
401
		$rc = pkg_exec("info -R --raw-format json-compact " . $pkgs, $out, $err);  
402 587988f6 stilez
	}
403 04daf8b1 stilez
404 9b7cfb3f Renato Botelho
	if ($lock) {
405
		clear_subsystem_dirty('pkg');
406
	}
407 b2a66231 Ermal
408 65c94077 Renato Botelho
	if ($rc != 0) {
409 e791dee8 Renato Botelho
		update_status("\n" . gettext(
410
		    "ERROR: Error trying to get packages list. Aborting...")
411
		    . "\n");
412
		update_status($err);
413 5c22f8ef Stephen Beaver
		$input_errors[] =  gettext("ERROR: Error trying to get packages list. Aborting...") . "\n";
414
		$input_errors[] =  $err;
415 65c94077 Renato Botelho
		return array();
416
	}
417
418
	$result = array();
419
	$pkgs_info = explode("\n", $out);
420
	foreach ($pkgs_info as $pkg_info_json) {
421
		$pkg_info = json_decode($pkg_info_json, true);
422
		if (!isset($pkg_info['name'])) {
423
			continue;
424
		}
425
426 d65fc2ff Renato Botelho
		if (isset($pkg_filter) && !in_array($pkg_info['name'], $pkg_filter)) {
427
			continue;
428
		}
429
430 b9e2048a Renato Botelho
		$pkg_info['shortname'] = $pkg_info['name'];
431
		pkg_remove_prefix($pkg_info['shortname']);
432
433
		/* XXX: Add it to globals.inc? */
434
		$pkg_info['changeloglink'] =
435
		    "https://github.com/pfsense/FreeBSD-ports/commits/devel/" .
436
		    $pkg_info['categories'][0] . '/' . $pkg_info['name'];
437
438 77340246 Renato Botelho
		if (is_pkg_installed($pkg_info['name'])) {
439
			$pkg_info['installed'] = true;
440 54f236f7 Renato Botelho
441 500dfdb8 Renato Botelho
			$rc = pkg_exec("query %v {$pkg_info['name']}", $out, $err);
442 54f236f7 Renato Botelho
443
			if ($rc != 0) {
444 e791dee8 Renato Botelho
				update_status("\n" . gettext(
445
				    "ERROR: Error trying to get package version. Aborting...")
446
				    . "\n");
447
				update_status($err);
448 54f236f7 Renato Botelho
				$input_errors[] =  gettext("ERROR: Error trying to get package version. Aborting...") . "\n";
449
				$input_errors[] =  $err;
450
				return array();
451
			}
452
453
			$pkg_info['installed_version'] = str_replace("\n", "", $out);
454 82692fe1 Renato Botelho
		} else if (is_package_installed($pkg_info['shortname'])) {
455
			$pkg_info['broken'] = true;
456 77340246 Renato Botelho
		}
457
458 c8cae8e5 Renato Botelho
		$pkg_info['desc'] = preg_replace('/\n+WWW:.*$/', '', $pkg_info['desc']);
459
460 65c94077 Renato Botelho
		$result[] = $pkg_info;
461
		unset($pkg_info);
462 34da63c3 Colin Smith
	}
463 b2a66231 Ermal
464 c5ecf722 Renato Botelho
	/* Sort result alphabetically */
465
	usort($result, function($a, $b) {
466
		return(strcasecmp ($a['name'], $b['name']));
467
	});
468
469 65c94077 Renato Botelho
	return $result;
470 8c6516d1 Colin Smith
}
471
472 bed6c19b Renato Botelho
/*
473
 * If binary pkg is installed but post-install tasks were not
474 0d0f419e Phil Davis
 * executed yet, do it now.
475 bed6c19b Renato Botelho
 * This scenario can happen when a pkg is pre-installed during
476
 * build phase, and at this point, cannot find a running system
477
 * to register itself in config.xml and also execute custom
478
 * install functions
479
 */
480
function register_all_installed_packages() {
481
	global $g, $config, $pkg_interface;
482
483 587988f6 stilez
	$pkg_info = get_pkg_info('all', true, true);
484
485 bed6c19b Renato Botelho
486
	foreach ($pkg_info as $pkg) {
487
		if (!isset($pkg['installed'])) {
488
			continue;
489
		}
490
491
		pkg_remove_prefix($pkg['name']);
492
493
		if (is_package_installed($pkg['name'])) {
494
			continue;
495
		}
496
497
		update_status(sprintf(gettext(
498
		    "Running last steps of %s installation.") . "\n",
499
		    $pkg['name']));
500
		install_package_xml($pkg['name']);
501
	}
502
}
503
504 8c6516d1 Colin Smith
/*
505
 * resync_all_package_configs() Force packages to setup their configuration and rc.d files.
506
 * This function may also print output to the terminal indicating progress.
507
 */
508
function resync_all_package_configs($show_message = false) {
509 b1224cdc jim-p
	global $config, $pkg_interface, $g;
510 b2a66231 Ermal
511 6acdf659 Carlos Eduardo Ramos
	log_error(gettext("Resyncing configuration for all packages."));
512 06e57df8 Scott Ullrich
513 49aec489 Phil Davis
	if (!is_array($config['installedpackages']['package'])) {
514 3a9eb3c9 Ermal
		return;
515 49aec489 Phil Davis
	}
516 06e57df8 Scott Ullrich
517 49aec489 Phil Davis
	if ($show_message == true) {
518 3a9eb3c9 Ermal
		echo "Syncing packages:";
519 49aec489 Phil Davis
	}
520 b2a66231 Ermal
521 06e57df8 Scott Ullrich
522 49aec489 Phil Davis
	foreach ($config['installedpackages']['package'] as $idx => $package) {
523
		if (empty($package['name'])) {
524 2addd5b2 Ermal
			continue;
525 49aec489 Phil Davis
		}
526
		if ($show_message == true) {
527 2addd5b2 Ermal
			echo " " . $package['name'];
528 49aec489 Phil Davis
		}
529
		if (platform_booting() != true) {
530 af5d93f6 Renato Botelho
			stop_service(get_package_internal_name($package));
531 49aec489 Phil Davis
		}
532 a0d4336f Renato Botelho
		sync_package($package['name']);
533 e791dee8 Renato Botelho
		update_status(gettext("Syncing packages...") . "\n");
534 e65a287f Scott Ullrich
	}
535 06e57df8 Scott Ullrich
536 49aec489 Phil Davis
	if ($show_message == true) {
537 08452bff Warren Baker
		echo " done.\n";
538 49aec489 Phil Davis
	}
539 06e57df8 Scott Ullrich
540 3a9eb3c9 Ermal
	@unlink("/conf/needs_package_sync");
541 8c6516d1 Colin Smith
}
542
543 d1a8f050 Renato Botelho
function uninstall_package($package_name) {
544 e791dee8 Renato Botelho
	global $config;
545 b2a66231 Ermal
546 0d579b59 Renato Botelho
	$internal_name = $package_name;
547 d1a8f050 Renato Botelho
	$id = get_package_id($package_name);
548 df5da531 Ermal
	if ($id >= 0) {
549 d1a8f050 Renato Botelho
		$internal_name = get_package_internal_name($config['installedpackages']['package'][$id]);
550
		stop_service($internal_name);
551 0d579b59 Renato Botelho
	}
552 f5b1c660 Renato Botelho
	$pkg_name = $g['pkg_prefix'] . $internal_name;
553 0d579b59 Renato Botelho
554 f5b1c660 Renato Botelho
	if (is_pkg_installed($pkg_name)) {
555 e791dee8 Renato Botelho
		update_status(gettext("Removing package...") . "\n");
556 f5b1c660 Renato Botelho
		pkg_delete($pkg_name);
557 0d579b59 Renato Botelho
	} else {
558
		delete_package_xml($package_name);
559 1570d27a Ermal Lu?i
	}
560 4c6a49d7 Scott Ullrich
561 e791dee8 Renato Botelho
	update_status(gettext("done.") . "\n");
562 f898cf33 Scott Ullrich
}
563
564 a0d4336f Renato Botelho
/* Run <custom_php_resync_config_command> */
565
function sync_package($package_name) {
566
	global $config, $builder_package_install;
567 2d26ee5e Sjon Hortensius
568
	// If this code is being called by pfspkg_installer
569 09e11b69 Scott Ullrich
	// which the builder system uses then return (ignore).
570 49aec489 Phil Davis
	if ($builder_package_install) {
571 f0695975 Scott Ullrich
		return;
572 49aec489 Phil Davis
	}
573 2d26ee5e Sjon Hortensius
574 49aec489 Phil Davis
	if (empty($config['installedpackages']['package'])) {
575 b2a66231 Ermal
		return;
576 49aec489 Phil Davis
	}
577 a0d4336f Renato Botelho
578
	if (($pkg_id = get_package_id($package_name)) == -1) {
579
		return; // This package doesn't really exist - exit the function.
580 49aec489 Phil Davis
	}
581 3e643dba Ermal LUÇI
582 49aec489 Phil Davis
	if (!is_array($config['installedpackages']['package'][$pkg_id])) {
583 a2febf9a Stephen Beaver
		return;	 // No package belongs to the pkg_id passed to this function.
584 49aec489 Phil Davis
	}
585 3e643dba Ermal LUÇI
586
	$package =& $config['installedpackages']['package'][$pkg_id];
587 49aec489 Phil Davis
	if (!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
588 6acdf659 Carlos Eduardo Ramos
		log_error(sprintf(gettext("The %s package is missing its configuration file and must be reinstalled."), $package['name']));
589 106574d1 Renato Botelho
		delete_package_xml($package['name']);
590 a0d4336f Renato Botelho
		return;
591 2c794549 Ermal
	}
592 a0d4336f Renato Botelho
593 2c794549 Ermal
	$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
594 49aec489 Phil Davis
	if (isset($pkg_config['nosync'])) {
595 2addd5b2 Ermal
		return;
596 49aec489 Phil Davis
	}
597 a0d4336f Renato Botelho
598 2c794549 Ermal
	/* Bring in package include files */
599
	if (!empty($pkg_config['include_file'])) {
600
		$include_file = $pkg_config['include_file'];
601 49aec489 Phil Davis
		if (file_exists($include_file)) {
602 2c794549 Ermal
			require_once($include_file);
603 49aec489 Phil Davis
		} else {
604 e8c516a0 Phil Davis
			log_error(sprintf(gettext('Reinstalling package %1$s because its include file(%2$s) is missing!'), $package['name'], $include_file));
605 2c794549 Ermal
			uninstall_package($package['name']);
606 fad3ad59 Renato Botelho
			if (install_package($package['name']) != 0) {
607 e8c516a0 Phil Davis
				log_error(sprintf(gettext("Reinstalling package %s failed. Take appropriate measures!!!"), $package['name']));
608 a0d4336f Renato Botelho
				return;
609
			}
610
			if (file_exists($include_file)) {
611
				require_once($include_file);
612
			} else {
613
				return;
614 83cfae8d Ermal Lu?i
			}
615 30e4c34a Scott Ullrich
		}
616 2c794549 Ermal
	}
617 30e4c34a Scott Ullrich
618 49aec489 Phil Davis
	if (!empty($pkg_config['custom_php_global_functions'])) {
619 2c794549 Ermal
		eval($pkg_config['custom_php_global_functions']);
620 49aec489 Phil Davis
	}
621
	if (!empty($pkg_config['custom_php_resync_config_command'])) {
622 2c794549 Ermal
		eval($pkg_config['custom_php_resync_config_command']);
623 49aec489 Phil Davis
	}
624 8c6516d1 Colin Smith
}
625
626 801fcf24 Renato Botelho
/* Read info.xml installed by package and return an array */
627
function read_package_config($package_name) {
628
	global $g;
629 e5b5e29c Renato Botelho
630 801fcf24 Renato Botelho
	$pkg_info_xml = '/usr/local/share/' . $g['pkg_prefix'] . $package_name . '/info.xml';
631 7860191a Renato Botelho
632 801fcf24 Renato Botelho
	if (!file_exists($pkg_info_xml)) {
633
		return false;
634
	}
635 7860191a Renato Botelho
636 801fcf24 Renato Botelho
	$pkg_info = parse_xml_config_pkg($pkg_info_xml, 'pfsensepkgs');
637 7860191a Renato Botelho
638 801fcf24 Renato Botelho
	if (empty($pkg_info)) {
639
		return false;
640 43dad535 Vinicius Coque
	}
641 801fcf24 Renato Botelho
642
	/* it always returns an array with 1 item */
643
	return $pkg_info['package'][0];
644 8c6516d1 Colin Smith
}
645
646 a2b0d909 Renato Botelho
/* Read package configurationfile and return an array */
647
function read_package_configurationfile($package_name) {
648
	global $config, $g;
649
650
	$pkg_config = array();
651
	$id = get_package_id($package_name);
652
653
	if ($id < 0 || !isset($config['installedpackages']['package'][$id]['configurationfile'])) {
654
		return $pkg_config;
655
	}
656
657
	$pkg_configurationfile = $config['installedpackages']['package'][$id]['configurationfile'];
658
659
	if (empty($pkg_configurationfile) || !file_exists('/usr/local/pkg/' . $pkg_configurationfile)) {
660
		return $pkg_config;
661
	}
662
663
	$pkg_config = parse_xml_config_pkg('/usr/local/pkg/' . $pkg_configurationfile, "packagegui");
664
665
	return $pkg_config;
666
}
667
668 801fcf24 Renato Botelho
function get_after_install_info($package_name) {
669 666c49ce Renato Botelho
	$pkg_config = read_package_config($package_name);
670 384e2647 Renato Botelho
671 801fcf24 Renato Botelho
	if (isset($pkg_config['after_install_info'])) {
672
		return $pkg_config['after_install_info'];
673 49aec489 Phil Davis
	}
674 384e2647 Renato Botelho
675 801fcf24 Renato Botelho
	return '';
676
}
677 384e2647 Renato Botelho
678 801fcf24 Renato Botelho
function eval_once($toeval) {
679
	global $evaled;
680
	if (!$evaled) {
681
		$evaled = array();
682 49aec489 Phil Davis
	}
683 801fcf24 Renato Botelho
	$evalmd5 = md5($toeval);
684
	if (!in_array($evalmd5, $evaled)) {
685
		@eval($toeval);
686
		$evaled[] = $evalmd5;
687 384e2647 Renato Botelho
	}
688 801fcf24 Renato Botelho
	return;
689 384e2647 Renato Botelho
}
690
691 801fcf24 Renato Botelho
function install_package_xml($package_name) {
692 e791dee8 Renato Botelho
	global $g, $config, $pkg_interface;
693 bfe9c9e7 jim-p
694 801fcf24 Renato Botelho
	if (($pkg_info = read_package_config($package_name)) == false) {
695
		return false;
696 49aec489 Phil Davis
	}
697 cfde64b8 Scott Ullrich
698 801fcf24 Renato Botelho
	/* safe side. Write config below will send to ro again. */
699
700 c92ccac7 Vinicius Coque
	pkg_debug(gettext("Beginning package installation.") . "\n");
701
	log_error(sprintf(gettext('Beginning package installation for %s .'), $pkg_info['name']));
702 2a0e6517 Colin Smith
703 7597c8e8 Colin Smith
	/* add package information to config.xml */
704 af5d93f6 Renato Botelho
	$pkgid = get_package_id($pkg_info['name']);
705 e791dee8 Renato Botelho
	update_status(gettext("Saving updated package information...") . "\n");
706 49aec489 Phil Davis
	if ($pkgid == -1) {
707 7597c8e8 Colin Smith
		$config['installedpackages']['package'][] = $pkg_info;
708 086cf944 Phil Davis
		$changedesc = sprintf(gettext("Installed %s package."), $pkg_info['name']);
709 6acdf659 Carlos Eduardo Ramos
		$to_output = gettext("done.") . "\n";
710 7597c8e8 Colin Smith
	} else {
711
		$config['installedpackages']['package'][$pkgid] = $pkg_info;
712 6acdf659 Carlos Eduardo Ramos
		$changedesc = sprintf(gettext("Overwrote previous installation of %s."), $pkg_info['name']);
713
		$to_output = gettext("overwrite!") . "\n";
714 7597c8e8 Colin Smith
	}
715 801fcf24 Renato Botelho
	unlink_if_exists('/conf/needs_package_sync');
716 e8c516a0 Phil Davis
	write_config(sprintf(gettext("Intermediate config write during package install for %s."), $pkg_info['name']));
717 e791dee8 Renato Botelho
	update_status($to_output);
718 b2a66231 Ermal
719 801fcf24 Renato Botelho
	if (($pkgid = get_package_id($package_name)) == -1) {
720 1579e70f Phil Davis
		update_status(sprintf(gettext('The %1$s package is not installed.%2$sInstallation aborted.'), $package_name, "\n\n"));
721 b2a66231 Ermal
722 801fcf24 Renato Botelho
		uninstall_package($package_name);
723
		write_config($changedesc);
724
		log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
725 e791dee8 Renato Botelho
		update_status(gettext("Failed to install package.") . "\n");
726 2c794549 Ermal
		return false;
727 e65a287f Scott Ullrich
	}
728 bfe9c9e7 jim-p
729 5a5cbf01 jim-p
	if (file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
730 e791dee8 Renato Botelho
		update_status(gettext("Loading package configuration... "));
731 5a5cbf01 jim-p
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $pkg_info['configurationfile'], "packagegui");
732 e791dee8 Renato Botelho
		update_status(gettext("done.") . "\n");
733
		update_status(gettext("Configuring package components...") . "\n");
734 49aec489 Phil Davis
		if (!empty($pkg_config['filter_rules_needed'])) {
735 bc771948 Ermal Lu?i
			$config['installedpackages']['package'][$pkgid]['filter_rule_function'] = $pkg_config['filter_rules_needed'];
736 49aec489 Phil Davis
		}
737 7597c8e8 Colin Smith
		/* modify system files */
738 b2a66231 Ermal
739 a2febf9a Stephen Beaver
		/* if a require exists, include it.  this will
740
		 * show us where an error exists in a package
741
		 * instead of making us blindly guess
742 2df5cb99 Scott Ullrich
		 */
743 fcf92dae Ermal
		$missing_include = false;
744 49aec489 Phil Davis
		if ($pkg_config['include_file'] <> "") {
745 e791dee8 Renato Botelho
			update_status(gettext("Loading package instructions...") . "\n");
746 49aec489 Phil Davis
			if (file_exists($pkg_config['include_file'])) {
747 9b1aa8d9 Renato Botelho
				pkg_debug("require_once('{$pkg_config['include_file']}')\n");
748 43ad432c Ermal Lu?i
				require_once($pkg_config['include_file']);
749 49aec489 Phil Davis
			} else {
750 9b1aa8d9 Renato Botelho
				pkg_debug("Missing include {$pkg_config['include_file']}\n");
751 fcf92dae Ermal
				$missing_include = true;
752 e8c516a0 Phil Davis
				update_status(sprintf(gettext("Include %s is missing!"), basename($pkg_config['include_file'])) . "\n");
753 801fcf24 Renato Botelho
754
				uninstall_package($package_name);
755
				write_config($changedesc);
756
				log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
757 e791dee8 Renato Botelho
				update_status(gettext("Failed to install package.") . "\n");
758 fcf92dae Ermal
				return false;
759
			}
760 43db85f8 Scott Ullrich
		}
761 57811192 Ermal
762
		/* custom commands */
763 e791dee8 Renato Botelho
		update_status(gettext("Custom commands...") . "\n");
764 57811192 Ermal
		if ($missing_include == false) {
765 49aec489 Phil Davis
			if ($pkg_config['custom_php_global_functions'] <> "") {
766 e791dee8 Renato Botelho
				update_status(gettext("Executing custom_php_global_functions()..."));
767 57811192 Ermal
				eval_once($pkg_config['custom_php_global_functions']);
768 e791dee8 Renato Botelho
				update_status(gettext("done.") . "\n");
769 57811192 Ermal
			}
770 49aec489 Phil Davis
			if ($pkg_config['custom_php_install_command']) {
771 e791dee8 Renato Botelho
				update_status(gettext("Executing custom_php_install_command()..."));
772 57811192 Ermal
				eval_once($pkg_config['custom_php_install_command']);
773 e791dee8 Renato Botelho
				update_status(gettext("done.") . "\n");
774 57811192 Ermal
			}
775 49aec489 Phil Davis
			if ($pkg_config['custom_php_resync_config_command'] <> "") {
776 e791dee8 Renato Botelho
				update_status(gettext("Executing custom_php_resync_config_command()..."));
777 57811192 Ermal
				eval_once($pkg_config['custom_php_resync_config_command']);
778 e791dee8 Renato Botelho
				update_status(gettext("done.") . "\n");
779 57811192 Ermal
			}
780
		}
781 7597c8e8 Colin Smith
		/* sidebar items */
782 49aec489 Phil Davis
		if (is_array($pkg_config['menu'])) {
783 e791dee8 Renato Botelho
			update_status(gettext("Menu items... "));
784 49aec489 Phil Davis
			foreach ($pkg_config['menu'] as $menu) {
785
				if (is_array($config['installedpackages']['menu'])) {
786
					foreach ($config['installedpackages']['menu'] as $amenu) {
787
						if ($amenu['name'] == $menu['name']) {
788 1570d27a Ermal Lu?i
							continue 2;
789 49aec489 Phil Davis
						}
790
					}
791
				} else {
792 27018d3c Ermal
					$config['installedpackages']['menu'] = array();
793 49aec489 Phil Davis
				}
794 1570d27a Ermal Lu?i
				$config['installedpackages']['menu'][] = $menu;
795 7597c8e8 Colin Smith
			}
796 e791dee8 Renato Botelho
			update_status(gettext("done.") . "\n");
797 7597c8e8 Colin Smith
		}
798 2dc264a4 Colin Smith
		/* services */
799 49aec489 Phil Davis
		if (is_array($pkg_config['service'])) {
800 e791dee8 Renato Botelho
			update_status(gettext("Services... "));
801 49aec489 Phil Davis
			foreach ($pkg_config['service'] as $service) {
802
				if (is_array($config['installedpackages']['service'])) {
803
					foreach ($config['installedpackages']['service'] as $aservice) {
804
						if ($aservice['name'] == $service['name']) {
805 d282095a Renato Botelho
							continue 2;
806 49aec489 Phil Davis
						}
807
					}
808
				} else {
809 27018d3c Ermal
					$config['installedpackages']['service'] = array();
810 49aec489 Phil Davis
				}
811 2dc264a4 Colin Smith
				$config['installedpackages']['service'][] = $service;
812
			}
813 e791dee8 Renato Botelho
			update_status(gettext("done.") . "\n");
814 2dc264a4 Colin Smith
		}
815 7597c8e8 Colin Smith
	} else {
816 9b1aa8d9 Renato Botelho
		pkg_debug("Unable to find config file\n");
817 e791dee8 Renato Botelho
		update_status(gettext("Loading package configuration... failed!") . "\n\n" . gettext("Installation aborted."));
818 c92ccac7 Vinicius Coque
		pkg_debug(gettext("Unable to load package configuration. Installation aborted.") ."\n");
819 801fcf24 Renato Botelho
820
		uninstall_package($package_name);
821
		write_config($changedesc);
822
		log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
823 e791dee8 Renato Botelho
		update_status(gettext("Failed to install package.") . "\n");
824 2c794549 Ermal
		return false;
825 7597c8e8 Colin Smith
	}
826 2c794549 Ermal
827
	/* set up package logging streams */
828 49aec489 Phil Davis
	if ($pkg_info['logging']) {
829 2c794549 Ermal
		system_syslogd_start();
830
	}
831
832 e791dee8 Renato Botelho
	update_status(gettext("Writing configuration... "));
833 801fcf24 Renato Botelho
	write_config($changedesc);
834
	log_error(sprintf(gettext("Successfully installed package: %s."), $pkg_info['name']));
835 e791dee8 Renato Botelho
	update_status(gettext("done.") . "\n");
836 801fcf24 Renato Botelho
	if ($pkg_info['after_install_info']) {
837 e791dee8 Renato Botelho
		update_status($pkg_info['after_install_info']);
838 2d26ee5e Sjon Hortensius
	}
839 801fcf24 Renato Botelho
840 2c794549 Ermal
	return true;
841 64974db7 Scott Ullrich
}
842
843 3bb7e3e8 Renato Botelho
function delete_package_xml($package_name, $when = "post-deinstall") {
844 e791dee8 Renato Botelho
	global $g, $config, $pkg_interface;
845 b2a66231 Ermal
846 6955830f Ermal Lu?i
847 3bb7e3e8 Renato Botelho
	$pkgid = get_package_id($package_name);
848 b2a66231 Ermal
	if ($pkgid == -1) {
849 1579e70f Phil Davis
		update_status(sprintf(gettext('The %1$s package is not installed.%2$sDeletion aborted.'), $package_name, "\n\n"));
850 e65a287f Scott Ullrich
		ob_flush();
851
		sleep(1);
852
		return;
853
	}
854 086cf944 Phil Davis
	pkg_debug(sprintf(gettext("Removing %s package... "), $package_name));
855 e791dee8 Renato Botelho
	update_status(sprintf(gettext("Removing %s components..."), $package_name) . "\n");
856 407bf67a Colin Smith
	/* parse package configuration */
857
	$packages = &$config['installedpackages']['package'];
858 b63f2e8b Matthew Grooms
	$menus =& $config['installedpackages']['menu'];
859 3c41c4ab Colin Smith
	$services = &$config['installedpackages']['service'];
860 2addd5b2 Ermal
	$pkg_info =& $packages[$pkgid];
861 49aec489 Phil Davis
	if (file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
862 19a11678 Colin Smith
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $packages[$pkgid]['configurationfile'], "packagegui");
863 3f01fe47 Colin Smith
		/* remove menu items */
864 49aec489 Phil Davis
		if (is_array($pkg_config['menu'])) {
865 e791dee8 Renato Botelho
			update_status(gettext("Menu items... "));
866 8604523b Ermal Lu?i
			if (is_array($pkg_config['menu']) && is_array($menus)) {
867 49aec489 Phil Davis
				foreach ($pkg_config['menu'] as $menu) {
868
					foreach ($menus as $key => $instmenu) {
869
						if ($instmenu['name'] == $menu['name']) {
870 8604523b Ermal Lu?i
							unset($menus[$key]);
871 b2a66231 Ermal
							break;
872
						}
873
					}
874 8604523b Ermal Lu?i
				}
875
			}
876 e791dee8 Renato Botelho
			update_status(gettext("done.") . "\n");
877 407bf67a Colin Smith
		}
878 3c41c4ab Colin Smith
		/* remove services */
879 49aec489 Phil Davis
		if (is_array($pkg_config['service'])) {
880 e791dee8 Renato Botelho
			update_status(gettext("Services... "));
881 8604523b Ermal Lu?i
			if (is_array($pkg_config['service']) && is_array($services)) {
882 49aec489 Phil Davis
				foreach ($pkg_config['service'] as $service) {
883
					foreach ($services as $key => $instservice) {
884
						if ($instservice['name'] == $service['name']) {
885
							if (platform_booting() != true) {
886 06e57df8 Scott Ullrich
								stop_service($service['name']);
887 49aec489 Phil Davis
							}
888
							if ($service['rcfile']) {
889 c5966711 phildd
								$prefix = RCFILEPREFIX;
890 49aec489 Phil Davis
								if (!empty($service['prefix'])) {
891 941baf1e Ermal
									$prefix = $service['prefix'];
892 49aec489 Phil Davis
								}
893
								if (file_exists("{$prefix}{$service['rcfile']}")) {
894 941baf1e Ermal
									@unlink("{$prefix}{$service['rcfile']}");
895 49aec489 Phil Davis
								}
896 941baf1e Ermal
							}
897 8604523b Ermal Lu?i
							unset($services[$key]);
898
						}
899 0cab7cad Colin Smith
					}
900 3c41c4ab Colin Smith
				}
901
			}
902 e791dee8 Renato Botelho
			update_status(gettext("done.") . "\n");
903 3c41c4ab Colin Smith
		}
904 b2a66231 Ermal
		/*
905
		 * XXX: Otherwise inclusion of config.inc again invalidates actions taken.
906 a2febf9a Stephen Beaver
		 *	Same is done during installation.
907 b2a66231 Ermal
		 */
908 e8c516a0 Phil Davis
		write_config(sprintf(gettext("Intermediate config write during package removal for %s."), $package_name));
909 b2a66231 Ermal
910
		/*
911 a2febf9a Stephen Beaver
		 * If a require exists, include it.	 this will
912 b2a66231 Ermal
		 * show us where an error exists in a package
913
		 * instead of making us blindly guess
914 892aef15 Scott Ullrich
		 */
915 fcf92dae Ermal
		$missing_include = false;
916 49aec489 Phil Davis
		if ($pkg_config['include_file'] <> "") {
917 e791dee8 Renato Botelho
			update_status(gettext("Loading package instructions...") . "\n");
918 49aec489 Phil Davis
			if (file_exists($pkg_config['include_file'])) {
919 9b1aa8d9 Renato Botelho
				pkg_debug("require_once(\"{$pkg_config['include_file']}\")\n");
920 1570d27a Ermal Lu?i
				require_once($pkg_config['include_file']);
921 49aec489 Phil Davis
			} else {
922 9b1aa8d9 Renato Botelho
				pkg_debug("Missing include {$pkg_config['include_file']}\n");
923 fcf92dae Ermal
				$missing_include = true;
924 e8c516a0 Phil Davis
				update_status(sprintf(gettext("Include file %s could not be found for inclusion."), basename($pkg_config['include_file'])) . "\n");
925 fcf92dae Ermal
			}
926
		}
927
		/* ermal
928
		 * NOTE: It is not possible to handle parse errors on eval.
929
		 * So we prevent it from being run at all to not interrupt all the other code.
930
		 */
931 3bb7e3e8 Renato Botelho
		if ($when == "deinstall" && $missing_include == false) {
932 49aec489 Phil Davis
			/* evaluate this package's global functions and pre deinstall commands */
933
			if ($pkg_config['custom_php_global_functions'] <> "") {
934 fcf92dae Ermal
				eval_once($pkg_config['custom_php_global_functions']);
935 49aec489 Phil Davis
			}
936
			if ($pkg_config['custom_php_pre_deinstall_command'] <> "") {
937 fcf92dae Ermal
				eval_once($pkg_config['custom_php_pre_deinstall_command']);
938 49aec489 Phil Davis
			}
939 43db85f8 Scott Ullrich
		}
940 644d2d59 Colin Smith
		/* deinstall commands */
941 3bb7e3e8 Renato Botelho
		if ($when == "post-deinstall" && $pkg_config['custom_php_deinstall_command'] <> "") {
942 e791dee8 Renato Botelho
			update_status(gettext("Deinstall commands... "));
943 fcf92dae Ermal
			if ($missing_include == false) {
944
				eval_once($pkg_config['custom_php_deinstall_command']);
945 e791dee8 Renato Botelho
				update_status(gettext("done.") . "\n");
946 49aec489 Phil Davis
			} else {
947 e8c516a0 Phil Davis
				update_status("\n". gettext("Not executing custom deinstall hook because an include is missing.") . "\n");
948 49aec489 Phil Davis
			}
949 644d2d59 Colin Smith
		}
950 407bf67a Colin Smith
	}
951 2addd5b2 Ermal
	/* syslog */
952 ce7edfff doktornotor
	$need_syslog_restart = false;
953 8a82c222 doktornotor
	if (is_array($pkg_info['logging']) && $pkg_info['logging']['logfilename'] <> "") {
954 e8c516a0 Phil Davis
		update_status(gettext("Syslog entries... "));
955 2addd5b2 Ermal
		@unlink("{$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
956 e791dee8 Renato Botelho
		update_status("done.\n");
957 ce7edfff doktornotor
		$need_syslog_restart = true;
958 2addd5b2 Ermal
	}
959 2d26ee5e Sjon Hortensius
960 6b861ecd Renato Botelho
	if ($when == "post-deinstall") {
961
		/* remove config.xml entries */
962
		update_status(gettext("Configuration... "));
963
		unset($config['installedpackages']['package'][$pkgid]);
964
		update_status(gettext("done.") . "\n");
965 e8c516a0 Phil Davis
		write_config(sprintf(gettext("Removed %s package."), $package_name));
966 6b861ecd Renato Botelho
	}
967 ce7edfff doktornotor
968
	/* remove package entry from /etc/syslog.conf if needed */
969 f164ace4 doktornotor
	/* this must be done after removing the entries from config.xml */
970 ce7edfff doktornotor
	if ($need_syslog_restart) {
971
		system_syslogd_start();
972
	}
973
974 f0a550fd Colin Smith
}
975 5025a56c Scott Ullrich
976 46903fb9 Renato Botelho
/*
977
 * Used during upgrade process or retore backup process, verify all
978
 * packages installed in config.xml and install pkg accordingly
979
 */
980
function package_reinstall_all() {
981 e791dee8 Renato Botelho
	global $g, $config, $pkg_interface;
982 c53eb903 Ermal
983 7b31a030 Chris Buechler
	$upgrade = (file_exists('/conf/needs_package_sync') && platform_booting());
984
985
	if ((!isset($config['installedpackages']['package']) ||
986
	    !is_array($config['installedpackages']['package'])) && !$upgrade) {
987 46903fb9 Renato Botelho
		return true;
988
	}
989
990
	/* During boot after upgrade, wait for internet connection */
991
	if ($upgrade) {
992 7b31a030 Chris Buechler
		update_status(gettext("Waiting for Internet connection to update pkg metadata and finish package reinstallation"));
993 3610a08e Renato Botelho
		$ntries = 3;
994
		while ($ntries > 0) {
995 46903fb9 Renato Botelho
			if (pkg_update(true)) {
996
				break;
997
			}
998 e791dee8 Renato Botelho
			update_status('.');
999 46903fb9 Renato Botelho
			sleep(1);
1000 3610a08e Renato Botelho
			$ntries--;
1001 46903fb9 Renato Botelho
		}
1002 e791dee8 Renato Botelho
		update_status("\n");
1003 3610a08e Renato Botelho
1004
		if ($ntries == 0) {
1005
			file_notice(gettext("Package reinstall"),
1006
			    gettext("Package reinstall process was ABORTED due to lack of internet connectivity"));
1007
			return false;
1008
		}
1009 46903fb9 Renato Botelho
	}
1010
1011
	$pkg_info = get_pkg_info();
1012
1013 c0cb3c73 Renato Botelho
	if ($upgrade &&
1014
	    file_exists("{$g['cf_conf_path']}/packages_to_reinstall_after_upgrade.txt")) {
1015
		$package_list = file("{$g['cf_conf_path']}/packages_to_reinstall_after_upgrade.txt",
1016
		    FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
1017
		unlink_if_exists("{$g['cf_conf_path']}/packages_to_reinstall_after_upgrade.txt");
1018
	} else {
1019 5eda5816 NewEraCracker
		if (!isset($config['installedpackages']['package']) || !is_array($config['installedpackages']['package'])) {
1020
			return true;
1021
		}
1022 c0cb3c73 Renato Botelho
		$package_list = array();
1023
		foreach ($config['installedpackages']['package'] as $package) {
1024
			$package_list[] = get_package_internal_name($package);
1025
		}
1026
	}
1027
1028
	foreach ($package_list as $package) {
1029 46903fb9 Renato Botelho
		$found = false;
1030
		foreach ($pkg_info as $pkg) {
1031
			pkg_remove_prefix($pkg['name']);
1032 c0cb3c73 Renato Botelho
			if ($pkg['name'] == $package) {
1033
				pkg_install($g['pkg_prefix'] . $package, true);
1034 46903fb9 Renato Botelho
				$found = true;
1035
				break;
1036
			}
1037
		}
1038
1039
		if (!$found) {
1040
			if (!function_exists("file_notice")) {
1041
				require_once("notices.inc");
1042
			}
1043
1044
			file_notice(gettext("Package reinstall"),
1045 c0cb3c73 Renato Botelho
			    sprintf(gettext("Package %s does not exist in current %s version and it has been removed."),
1046
			    $package, $g['product_name']));
1047
			uninstall_package($package);
1048 46903fb9 Renato Botelho
		}
1049
	}
1050
1051
	return true;
1052 9b193619 Scott Ullrich
}
1053
1054 60dd7649 jim-p
function stop_packages() {
1055
	require_once("config.inc");
1056
	require_once("functions.inc");
1057
	require_once("filter.inc");
1058
	require_once("shaper.inc");
1059
	require_once("captiveportal.inc");
1060
	require_once("pkg-utils.inc");
1061
	require_once("pfsense-utils.inc");
1062
	require_once("service-utils.inc");
1063
1064 c5966711 phildd
	global $config, $g;
1065 60dd7649 jim-p
1066 e8c516a0 Phil Davis
	log_error(gettext("Stopping all packages."));
1067 60dd7649 jim-p
1068 c5966711 phildd
	$rcfiles = glob(RCFILEPREFIX . "*.sh");
1069 49aec489 Phil Davis
	if (!$rcfiles) {
1070 60dd7649 jim-p
		$rcfiles = array();
1071 49aec489 Phil Davis
	} else {
1072 60dd7649 jim-p
		$rcfiles = array_flip($rcfiles);
1073 49aec489 Phil Davis
		if (!$rcfiles) {
1074 60dd7649 jim-p
			$rcfiles = array();
1075 49aec489 Phil Davis
		}
1076 60dd7649 jim-p
	}
1077
1078
	if (is_array($config['installedpackages']['package'])) {
1079 49aec489 Phil Davis
		foreach ($config['installedpackages']['package'] as $package) {
1080 60dd7649 jim-p
			echo " Stopping package {$package['name']}...";
1081 af5d93f6 Renato Botelho
			$internal_name = get_package_internal_name($package);
1082 75a01a7c Phil Davis
			stop_service($internal_name);
1083
			unset($rcfiles[RCFILEPREFIX . strtolower($internal_name) . ".sh"]);
1084 60dd7649 jim-p
			echo "done.\n";
1085
		}
1086
	}
1087
1088 6186cdc4 jim-p
	foreach ($rcfiles as $rcfile => $number) {
1089
		$shell = @popen("/bin/sh", "w");
1090
		if ($shell) {
1091 60dd7649 jim-p
			echo " Stopping {$rcfile}...";
1092 61ef1385 Ermal
			if (!@fwrite($shell, "{$rcfile} stop >>/tmp/bootup_messages 2>&1")) {
1093 49aec489 Phil Davis
				if ($shell) {
1094 61ef1385 Ermal
					pclose($shell);
1095 49aec489 Phil Davis
				}
1096 61ef1385 Ermal
				$shell = @popen("/bin/sh", "w");
1097
			}
1098 60dd7649 jim-p
			echo "done.\n";
1099 6186cdc4 jim-p
			pclose($shell);
1100 60dd7649 jim-p
		}
1101
	}
1102
}
1103
1104 d5f0597b Renato Botelho
/* Identify which meta package is installed */
1105
function get_meta_pkg_name() {
1106
	global $g;
1107
1108
	/* XXX: Use pkg annotation */
1109
	if (is_pkg_installed($g['product_name'])) {
1110
		return $g['product_name'];
1111
	} else if (is_pkg_installed($g['product_name'] . '-vmware')) {
1112
		return $g['product_name'] . '-vmware';
1113
	}
1114
	return false;
1115
}
1116
1117
/* Identify which base package is installed */
1118
function get_base_pkg_name() {
1119
	global $g;
1120
1121
	/* XXX: Use pkg annotation */
1122
	if (is_pkg_installed($g['product_name'] . '-base-' . $g['platform'])) {
1123
		return $g['product_name'] . '-base-' . $g['platform'];
1124
	} else if (is_pkg_installed($g['product_name'] . '-base')) {
1125
		return $g['product_name'] . '-base';
1126
	}
1127
	return false;
1128
}
1129
1130
/* Verify if system needs upgrade (meta package or base) */
1131 d1c57eb8 Stephen Beaver
function get_system_pkg_version($baseonly = false) {
1132 d5f0597b Renato Botelho
	global $g;
1133
1134 ee836314 Renato Botelho
	$output = exec("/usr/local/sbin/{$g['product_name']}-upgrade -c", $_gc,
1135
	    $rc);
1136
1137
	/* pfSense-upgrade returns 2 when there is a new version */
1138
	if ($rc == 2) {
1139
		$new_version = explode(' ', $output)[0];
1140
	}
1141
1142 d5f0597b Renato Botelho
	$base_pkg = get_base_pkg_name();
1143
	$meta_pkg = get_meta_pkg_name();
1144
1145
	if (!$base_pkg || !$meta_pkg) {
1146
		return false;
1147
	}
1148
1149 ee836314 Renato Botelho
	$info = get_pkg_info($base_pkg, true);
1150 d5f0597b Renato Botelho
1151
	$pkg_info = array();
1152
	foreach ($info as $item) {
1153
		if ($item['name'] == $base_pkg) {
1154
			$pkg_info = $item;
1155 ee836314 Renato Botelho
			break;
1156 d5f0597b Renato Botelho
		}
1157
	}
1158
1159 ee836314 Renato Botelho
	if (empty($pkg_info) || (!$baseonly && ($pkg_info['version'] ==
1160
	    $pkg_info['installed_version']))) {
1161
		$info = get_pkg_info($meta_pkg, true);
1162 d5f0597b Renato Botelho
1163
		foreach ($info as $item) {
1164 59d256a1 Renato Botelho
			if ($item['name'] == $meta_pkg) {
1165 d5f0597b Renato Botelho
				$pkg_info = $item;
1166 ee836314 Renato Botelho
				break;
1167 d5f0597b Renato Botelho
			}
1168
		}
1169
	}
1170
1171
	if (empty($pkg_info)) {
1172
		return false;
1173
	}
1174
1175
	return array(
1176 ee836314 Renato Botelho
	    'version'           => $new_version ?: $pkg_info['version'],
1177 d5f0597b Renato Botelho
	    'installed_version' => $pkg_info['installed_version']
1178
	);
1179
}
1180
1181 db8621d8 Renato Botelho
/* List available repos */
1182
function pkg_list_repos() {
1183 ad3c9763 Renato Botelho
	global $g;
1184
1185 db8621d8 Renato Botelho
	$path = "/usr/local/share/{$g['product_name']}/pkg/repos";
1186 ad3c9763 Renato Botelho
1187 db8621d8 Renato Botelho
	$default_descr = @file_get_contents($path . "/{$g['product_name']}-repo.descr");
1188 ad3c9763 Renato Botelho
1189 db8621d8 Renato Botelho
	$default = array(
1190
	    'name' => 'Default',
1191
	    'path' => $path . "/{$g['product_name']}-repo.conf",
1192
	    'descr' => $default_descr
1193
	);
1194
1195
	$result = array($default);
1196
1197
	$conf_files = glob("{$path}/{$g['product_name']}-repo-*.conf");
1198
	foreach ($conf_files as $conf_file) {
1199
		$descr_file = preg_replace('/.conf$/', '.descr', $conf_file);
1200
		if (file_exists($descr_file)) {
1201
			$descr_content = file($descr_file);
1202
			$descr = chop($descr_content[0]);
1203
		} else {
1204
			$descr = 'Unknown';
1205
		}
1206
		if (!preg_match('/-repo-(.*).conf/', $conf_file, $matches)) {
1207
			continue;
1208
		}
1209
		$entry = array(
1210
		    'name' => ucfirst(strtolower($matches[1])),
1211
		    'path' => $conf_file,
1212
		    'descr' => $descr
1213
		);
1214
		$result[] = $entry;
1215 ad3c9763 Renato Botelho
	}
1216
1217 db8621d8 Renato Botelho
	return $result;
1218
}
1219
1220
/* Switch between stable and devel repos */
1221
function pkg_switch_repo($path) {
1222
	global $g;
1223
1224
	safe_mkdir("/usr/local/etc/pkg/repos");
1225
	@unlink("/usr/local/etc/pkg/repos/{$g['product_name']}.conf");
1226
	@symlink($path, "/usr/local/etc/pkg/repos/{$g['product_name']}.conf");
1227
1228 ce6e6519 Renato Botelho
	$abi_file = str_replace('.conf', '.abi', $path);
1229
	$altabi_file = str_replace('.conf', '.altabi', $path);
1230
1231
	if (file_exists($abi_file) && file_exists($altabi_file)) {
1232
		$abi = file_get_contents($abi_file);
1233
		$altabi = file_get_contents($altabi_file);
1234
1235
		$pkg_conf = array(
1236
			"ABI={$abi}",
1237
			"ALTABI={$altabi}"
1238
		);
1239
1240
		file_put_contents("/usr/local/etc/pkg.conf", $pkg_conf);
1241
	}
1242
1243 db8621d8 Renato Botelho
	return pkg_update(true);
1244 ad3c9763 Renato Botelho
}
1245
1246 61ef1385 Ermal
?>