Project

General

Profile

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