Project

General

Profile

Download (31.6 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 c2eb2508 Renato Botelho
/* Remove pkg_prefix from package name if it's present */
87
function pkg_remove_prefix(&$pkg_name) {
88
	global $g;
89
90
	if (substr($pkg_name, 0, strlen($g['pkg_prefix'])) == $g['pkg_prefix']) {
91
		$pkg_name = substr($pkg_name, strlen($g['pkg_prefix']));
92
	}
93 e7405fbf Scott Ullrich
}
94 33b7cc0d Colin Smith
95 d3ff1e59 Renato Botelho
/* Execute pkg update when it's necessary */
96
function pkg_update($force = false) {
97
	global $g;
98
99 6cf87aec Renato Botelho
	return pkg_call("update" . ($force ? " -f" : ""));
100 d3ff1e59 Renato Botelho
}
101
102 75bb92c4 Renato Botelho
/* return an array with necessary environment vars for pkg */
103
function pkg_env() {
104
	global $config, $g;
105
106
	$pkg_env_vars = array(
107 e21ac870 Renato Botelho
		"LANG" => "C",
108 75bb92c4 Renato Botelho
		"HTTP_USER_AGENT" => $user_agent,
109 6cf87aec Renato Botelho
		"ASSUME_ALWAYS_YES" => "true"
110 75bb92c4 Renato Botelho
	);
111
112
	if ($g['platform'] == "nanobsd" ||
113
	    isset($config['system']['use_mfs_tmpvar'])) {
114
		$pkg_env_vars['PKG_DBDIR'] = '/root/var/db/pkg';
115
		$pkg_env_vars['PKG_CACHEDIR'] = '/root/var/cache/pkg';
116
	}
117
118
	return $pkg_env_vars;
119
}
120
121 c2eb2508 Renato Botelho
/* Execute a pkg call */
122 500dfdb8 Renato Botelho
function pkg_call($params, $mute = false) {
123 e791dee8 Renato Botelho
	global $g, $config;
124 6fd37d04 Renato Botelho
125 c2eb2508 Renato Botelho
	if (empty($params)) {
126
		return false;
127
	}
128
129 96bf5038 Renato Botelho
	$user_agent = $g['product_name'] . '/' . $g['product_version'];
130 3138b2e3 Renato Botelho
	if (!isset($config['system']['do_not_send_host_uuid'])) {
131 21dfcd61 Renato Botelho
		$user_agent .= ' : ' . get_single_sysctl('kern.hostuuid');
132 96bf5038 Renato Botelho
	}
133
134 6fd37d04 Renato Botelho
	$descriptorspec = array(
135
		1 => array("pipe", "w"), /* stdout */
136 a2febf9a Stephen Beaver
		2 => array("pipe", "w")	 /* stderr */
137 6fd37d04 Renato Botelho
	);
138
139 500dfdb8 Renato Botelho
	conf_mount_rw();
140 4b834523 Renato Botelho
141 6fd37d04 Renato Botelho
	pkg_debug("pkg_call(): {$params}\n");
142 75bb92c4 Renato Botelho
	$process = proc_open("/usr/sbin/pkg {$params}", $descriptorspec, $pipes,
143
	    '/', pkg_env());
144 6fd37d04 Renato Botelho
145
	if (!is_resource($process)) {
146 500dfdb8 Renato Botelho
		conf_mount_ro();
147 6fd37d04 Renato Botelho
		return false;
148
	}
149
150
	stream_set_blocking($pipes[1], 0);
151
	stream_set_blocking($pipes[2], 0);
152
153
	/* XXX: should be a tunnable? */
154
	$timeout = 300; // seconds
155
	$error_log = '';
156 a2febf9a Stephen Beaver
	$started = time();
157
	$maxwaittime = 10; // Number of seconds to wait for a response fromteh pacakge we are calling
158 6fd37d04 Renato Botelho
159
	do {
160
		$write = array();
161
		$read = array($pipes[1], $pipes[2]);
162
		$except = array();
163
164
		$stream = stream_select($read, $write, $except, null, $timeout);
165
		if ($stream !== FALSE && $stream > 0) {
166
			foreach ($read as $pipe) {
167
				$content = stream_get_contents($pipe);
168
				if ($content == '') {
169
					continue;
170
				}
171
				if ($pipe === $pipes[1]) {
172
					if (!$mute) {
173 e791dee8 Renato Botelho
						update_status($content);
174 6fd37d04 Renato Botelho
					}
175
					flush();
176
				} else if ($pipe === $pipes[2]) {
177
					$error_log .= $content;
178
				}
179
			}
180 49aec489 Phil Davis
		}
181 a2febf9a Stephen Beaver
182 6fd37d04 Renato Botelho
		$status = proc_get_status($process);
183 a2febf9a Stephen Beaver
184
		$now = time();
185
186 4e322e2c Phil Davis
		if (($now - $started) >= $maxwaittime) {
187 a2febf9a Stephen Beaver
			$rc = -1;
188
			proc_terminate($process);
189
			break;
190
		}
191
192 6fd37d04 Renato Botelho
	} while ($status['running']);
193 a2febf9a Stephen Beaver
194 6fd37d04 Renato Botelho
	fclose($pipes[1]);
195
	fclose($pipes[2]);
196
	proc_close($process);
197
198 500dfdb8 Renato Botelho
	conf_mount_ro();
199 4b834523 Renato Botelho
200 4e322e2c Phil Davis
	if (!isset($rc)) {
201 a2febf9a Stephen Beaver
		$rc = $status['exitcode'];
202
	}
203 6fd37d04 Renato Botelho
204
	pkg_debug("pkg_call(): rc = {$rc}\n");
205
	if ($rc == 0) {
206
		return true;
207
	}
208
209
	pkg_debug("pkg_call(): error_log\n{$error_log}\n");
210
	if (!$mute) {
211 e791dee8 Renato Botelho
		update_status("\n\n" .  sprintf(gettext(
212
		    "ERROR!!! An error occurred on pkg execution (rc = %d) with parameters '%s':"),
213
		    $rc, $params) . "\n" . $error_log . "\n");
214 49aec489 Phil Davis
	}
215 a2febf9a Stephen Beaver
216 6fd37d04 Renato Botelho
	return false;
217 31e7e1bc Scott Ullrich
}
218
219 6fd37d04 Renato Botelho
/* Execute pkg with $params, fill stdout and stderr and return pkg rc */
220 500dfdb8 Renato Botelho
function pkg_exec($params, &$stdout, &$stderr) {
221 96bf5038 Renato Botelho
	global $g, $config;
222 6fd37d04 Renato Botelho
223
	if (empty($params)) {
224
		return -1;
225
	}
226
227 96bf5038 Renato Botelho
	$user_agent = $g['product_name'] . '/' . $g['product_version'];
228 3138b2e3 Renato Botelho
	if (!isset($config['system']['do_not_send_host_uuid'])) {
229 21dfcd61 Renato Botelho
		$user_agent .= ' : ' . get_single_sysctl('kern.hostuuid');
230 96bf5038 Renato Botelho
	}
231
232 6fd37d04 Renato Botelho
	$descriptorspec = array(
233
		1 => array("pipe", "w"), /* stdout */
234 a2febf9a Stephen Beaver
		2 => array("pipe", "w")	 /* stderr */
235 6fd37d04 Renato Botelho
	);
236
237 500dfdb8 Renato Botelho
	conf_mount_rw();
238 4b834523 Renato Botelho
239 6fd37d04 Renato Botelho
	pkg_debug("pkg_exec(): {$params}\n");
240 75bb92c4 Renato Botelho
	$process = proc_open("/usr/sbin/pkg {$params}", $descriptorspec, $pipes,
241
	    '/', pkg_env());
242 6fd37d04 Renato Botelho
243
	if (!is_resource($process)) {
244 500dfdb8 Renato Botelho
		conf_mount_ro();
245 6fd37d04 Renato Botelho
		return -1;
246
	}
247
248
	$stdout = '';
249
	while (($l = fgets($pipes[1])) !== FALSE) {
250
		$stdout .= $l;
251
	}
252
	fclose($pipes[1]);
253
254
	$stderr = '';
255
	while (($l = fgets($pipes[2])) !== FALSE) {
256
		$stderr .= $l;
257
	}
258
	fclose($pipes[2]);
259 c2eb2508 Renato Botelho
260 500dfdb8 Renato Botelho
	conf_mount_ro();
261 4b834523 Renato Botelho
262 6fd37d04 Renato Botelho
	return proc_close($process);
263 c2eb2508 Renato Botelho
}
264
265 8df1877b Renato Botelho
/* Compare 2 pkg versions and return:
266
 * '=' - versions are the same
267
 * '>' - $v1 > $v2
268
 * '<' - $v1 < $v2
269
 * '?' - Error
270
 */
271
function pkg_version_compare($v1, $v2) {
272
	if (empty($v1) || empty($v2)) {
273
		return '?';
274
	}
275
276 500dfdb8 Renato Botelho
	$rc = pkg_exec("version -t '{$v1}' '{$v2}'", $stdout, $stderr);
277 8df1877b Renato Botelho
278
	if ($rc != 0) {
279
		return '?';
280
	}
281
282
	return str_replace("\n", "", $stdout);
283
}
284
285 c2eb2508 Renato Botelho
/* Check if package is installed */
286
function is_pkg_installed($pkg_name) {
287
	global $g;
288
289 f5b1c660 Renato Botelho
	if (empty($pkg_name)) {
290
		return false;
291
	}
292 c2eb2508 Renato Botelho
293 500dfdb8 Renato Botelho
	return pkg_call("info -e " . $pkg_name, true);
294 c2eb2508 Renato Botelho
}
295
296 7379d80f Renato Botelho
/* Install package, $pkg_name should not contain prefix */
297 46903fb9 Renato Botelho
function pkg_install($pkg_name, $force = false) {
298 7379d80f Renato Botelho
	global $g;
299 e7553e1b Renato Botelho
	$result = false;
300 7379d80f Renato Botelho
301 f5b1c660 Renato Botelho
	$shortname = $pkg_name;
302
	pkg_remove_prefix($shortname);
303 7379d80f Renato Botelho
304 46903fb9 Renato Botelho
	$pkg_force = "";
305
	if ($force) {
306
		$pkg_force = "-f ";
307
	}
308
309 f5b1c660 Renato Botelho
	pkg_debug("Installing package {$shortname}\n");
310 46903fb9 Renato Botelho
	if ($force || !is_pkg_installed($pkg_name)) {
311 f5b1c660 Renato Botelho
		$result = pkg_call("install -y " . $pkg_force . $pkg_name);
312 e7553e1b Renato Botelho
		/* Cleanup cacke to free disk space */
313
		pkg_call("clean -y");
314 7379d80f Renato Botelho
	}
315
316 e7553e1b Renato Botelho
	return $result;
317 7379d80f Renato Botelho
}
318
319 c2eb2508 Renato Botelho
/* Delete package from FreeBSD, $pkg_name should not contain prefix */
320
function pkg_delete($pkg_name) {
321
	global $g;
322
323 f5b1c660 Renato Botelho
	$shortname = $pkg_name;
324
	pkg_remove_prefix($shortname);
325 c2eb2508 Renato Botelho
326 f5b1c660 Renato Botelho
	pkg_debug("Removing package {$shortname}\n");
327 c2eb2508 Renato Botelho
	if (is_pkg_installed($pkg_name)) {
328 f5b1c660 Renato Botelho
		pkg_call("delete -y " . $pkg_name);
329 c2eb2508 Renato Botelho
		/* Cleanup unecessary dependencies */
330 e1382589 Renato Botelho
		pkg_call("autoremove -y");
331 49aec489 Phil Davis
	}
332 8c6516d1 Colin Smith
}
333 43db85f8 Scott Ullrich
334 af5d93f6 Renato Botelho
/* Check if package is present in config.xml */
335
function is_package_installed($package_name) {
336
	return (get_package_id($package_name) != -1);
337 8c6516d1 Colin Smith
}
338 43db85f8 Scott Ullrich
339 af5d93f6 Renato Botelho
/* Find package array index */
340
function get_package_id($package_name) {
341 e65a287f Scott Ullrich
	global $config;
342
343 af5d93f6 Renato Botelho
	if (!is_array($config['installedpackages']['package'])) {
344
		return -1;
345
	}
346
347
	foreach ($config['installedpackages']['package'] as $idx => $pkg) {
348 a50534b6 Renato Botelho
		if ($pkg['name'] == $package_name ||
349 4e322e2c Phil Davis
		    get_package_internal_name($pkg) == $package_name) {
350 af5d93f6 Renato Botelho
			return $idx;
351 e65a287f Scott Ullrich
		}
352
	}
353 af5d93f6 Renato Botelho
354 e65a287f Scott Ullrich
	return -1;
355 8c6516d1 Colin Smith
}
356
357 e0d24d88 Renato Botelho
/* Keep backward compatibility since snort/suricata use this function */
358
function get_pkg_id($package_name) {
359
	return get_package_id($package_name);
360
}
361
362 af5d93f6 Renato Botelho
/* Return internal_name when it's defined, otherwise, returns name */
363
function get_package_internal_name($package_data) {
364
	if (isset($package_data['internal_name']) && ($package_data['internal_name'] != "")) {
365 75a01a7c Phil Davis
		/* e.g. name is Ipguard-dev, internal name is ipguard */
366 af5d93f6 Renato Botelho
		return $package_data['internal_name'];
367 75a01a7c Phil Davis
	} else {
368 af5d93f6 Renato Botelho
		return $package_data['name'];
369 75a01a7c Phil Davis
	}
370
}
371
372 a2febf9a Stephen Beaver
// Get information about packages.
373 33b7cc0d Colin Smith
function get_pkg_info($pkgs = 'all', $info = 'all') {
374 e791dee8 Renato Botelho
	global $g, $input_errors;
375 b2a66231 Ermal
376 65c94077 Renato Botelho
	$out = '';
377
	$err = '';
378 e65a287f Scott Ullrich
379 d65fc2ff Renato Botelho
	unset($pkg_filter);
380
	if (is_array($pkgs)) {
381
		$pkg_filter = $pkgs;
382
		$pkgs = 'all';
383
	}
384
385 65c94077 Renato Botelho
	if ($pkgs == 'all') {
386
		$pkgs = $g['pkg_prefix'];
387
	}
388 b2a66231 Ermal
389 6cf87aec Renato Botelho
	$rc = pkg_exec("search --raw-format json-compact " . $pkgs, $out, $err);
390 b2a66231 Ermal
391 65c94077 Renato Botelho
	if ($rc != 0) {
392 e791dee8 Renato Botelho
		update_status("\n" . gettext(
393
		    "ERROR: Error trying to get packages list. Aborting...")
394
		    . "\n");
395
		update_status($err);
396 5c22f8ef Stephen Beaver
		$input_errors[] =  gettext("ERROR: Error trying to get packages list. Aborting...") . "\n";
397
		$input_errors[] =  $err;
398 65c94077 Renato Botelho
		return array();
399
	}
400
401
	$result = array();
402
	$pkgs_info = explode("\n", $out);
403
	foreach ($pkgs_info as $pkg_info_json) {
404
		$pkg_info = json_decode($pkg_info_json, true);
405
		if (!isset($pkg_info['name'])) {
406
			continue;
407
		}
408
409 d65fc2ff Renato Botelho
		if (isset($pkg_filter) && !in_array($pkg_info['name'], $pkg_filter)) {
410
			continue;
411
		}
412
413 b9e2048a Renato Botelho
		$pkg_info['shortname'] = $pkg_info['name'];
414
		pkg_remove_prefix($pkg_info['shortname']);
415
416
		/* XXX: Add it to globals.inc? */
417
		$pkg_info['changeloglink'] =
418
		    "https://github.com/pfsense/FreeBSD-ports/commits/devel/" .
419
		    $pkg_info['categories'][0] . '/' . $pkg_info['name'];
420
421 77340246 Renato Botelho
		if (is_pkg_installed($pkg_info['name'])) {
422
			$pkg_info['installed'] = true;
423 54f236f7 Renato Botelho
424 500dfdb8 Renato Botelho
			$rc = pkg_exec("query %v {$pkg_info['name']}", $out, $err);
425 54f236f7 Renato Botelho
426
			if ($rc != 0) {
427 e791dee8 Renato Botelho
				update_status("\n" . gettext(
428
				    "ERROR: Error trying to get package version. Aborting...")
429
				    . "\n");
430
				update_status($err);
431 54f236f7 Renato Botelho
				$input_errors[] =  gettext("ERROR: Error trying to get package version. Aborting...") . "\n";
432
				$input_errors[] =  $err;
433
				return array();
434
			}
435
436
			$pkg_info['installed_version'] = str_replace("\n", "", $out);
437 82692fe1 Renato Botelho
		} else if (is_package_installed($pkg_info['shortname'])) {
438
			$pkg_info['broken'] = true;
439 77340246 Renato Botelho
		}
440
441 c8cae8e5 Renato Botelho
		$pkg_info['desc'] = preg_replace('/\n+WWW:.*$/', '', $pkg_info['desc']);
442
443 65c94077 Renato Botelho
		$result[] = $pkg_info;
444
		unset($pkg_info);
445 34da63c3 Colin Smith
	}
446 b2a66231 Ermal
447 c5ecf722 Renato Botelho
	/* Sort result alphabetically */
448
	usort($result, function($a, $b) {
449
		return(strcasecmp ($a['name'], $b['name']));
450
	});
451
452 65c94077 Renato Botelho
	return $result;
453 8c6516d1 Colin Smith
}
454
455
/*
456
 * resync_all_package_configs() Force packages to setup their configuration and rc.d files.
457
 * This function may also print output to the terminal indicating progress.
458
 */
459
function resync_all_package_configs($show_message = false) {
460 b1224cdc jim-p
	global $config, $pkg_interface, $g;
461 b2a66231 Ermal
462 6acdf659 Carlos Eduardo Ramos
	log_error(gettext("Resyncing configuration for all packages."));
463 06e57df8 Scott Ullrich
464 49aec489 Phil Davis
	if (!is_array($config['installedpackages']['package'])) {
465 3a9eb3c9 Ermal
		return;
466 49aec489 Phil Davis
	}
467 06e57df8 Scott Ullrich
468 49aec489 Phil Davis
	if ($show_message == true) {
469 3a9eb3c9 Ermal
		echo "Syncing packages:";
470 49aec489 Phil Davis
	}
471 b2a66231 Ermal
472 78b94214 Ermal
	conf_mount_rw();
473 06e57df8 Scott Ullrich
474 49aec489 Phil Davis
	foreach ($config['installedpackages']['package'] as $idx => $package) {
475
		if (empty($package['name'])) {
476 2addd5b2 Ermal
			continue;
477 49aec489 Phil Davis
		}
478
		if ($show_message == true) {
479 2addd5b2 Ermal
			echo " " . $package['name'];
480 49aec489 Phil Davis
		}
481
		if (platform_booting() != true) {
482 af5d93f6 Renato Botelho
			stop_service(get_package_internal_name($package));
483 49aec489 Phil Davis
		}
484 a0d4336f Renato Botelho
		sync_package($package['name']);
485 e791dee8 Renato Botelho
		update_status(gettext("Syncing packages...") . "\n");
486 e65a287f Scott Ullrich
	}
487 06e57df8 Scott Ullrich
488 49aec489 Phil Davis
	if ($show_message == true) {
489 08452bff Warren Baker
		echo " done.\n";
490 49aec489 Phil Davis
	}
491 06e57df8 Scott Ullrich
492 3a9eb3c9 Ermal
	@unlink("/conf/needs_package_sync");
493 78b94214 Ermal
	conf_mount_ro();
494 8c6516d1 Colin Smith
}
495
496 d1a8f050 Renato Botelho
function uninstall_package($package_name) {
497 e791dee8 Renato Botelho
	global $config;
498 b2a66231 Ermal
499 0d579b59 Renato Botelho
	$internal_name = $package_name;
500 d1a8f050 Renato Botelho
	$id = get_package_id($package_name);
501 df5da531 Ermal
	if ($id >= 0) {
502 d1a8f050 Renato Botelho
		$internal_name = get_package_internal_name($config['installedpackages']['package'][$id]);
503
		stop_service($internal_name);
504 0d579b59 Renato Botelho
	}
505 f5b1c660 Renato Botelho
	$pkg_name = $g['pkg_prefix'] . $internal_name;
506 0d579b59 Renato Botelho
507 f5b1c660 Renato Botelho
	if (is_pkg_installed($pkg_name)) {
508 e791dee8 Renato Botelho
		update_status(gettext("Removing package...") . "\n");
509 f5b1c660 Renato Botelho
		pkg_delete($pkg_name);
510 0d579b59 Renato Botelho
	} else {
511
		delete_package_xml($package_name);
512 1570d27a Ermal Lu?i
	}
513 4c6a49d7 Scott Ullrich
514 e791dee8 Renato Botelho
	update_status(gettext("done.") . "\n");
515 f898cf33 Scott Ullrich
}
516
517 a0d4336f Renato Botelho
/* Run <custom_php_resync_config_command> */
518
function sync_package($package_name) {
519
	global $config, $builder_package_install;
520 2d26ee5e Sjon Hortensius
521
	// If this code is being called by pfspkg_installer
522 09e11b69 Scott Ullrich
	// which the builder system uses then return (ignore).
523 49aec489 Phil Davis
	if ($builder_package_install) {
524 f0695975 Scott Ullrich
		return;
525 49aec489 Phil Davis
	}
526 2d26ee5e Sjon Hortensius
527 49aec489 Phil Davis
	if (empty($config['installedpackages']['package'])) {
528 b2a66231 Ermal
		return;
529 49aec489 Phil Davis
	}
530 a0d4336f Renato Botelho
531
	if (($pkg_id = get_package_id($package_name)) == -1) {
532
		return; // This package doesn't really exist - exit the function.
533 49aec489 Phil Davis
	}
534 3e643dba Ermal LUÇI
535 49aec489 Phil Davis
	if (!is_array($config['installedpackages']['package'][$pkg_id])) {
536 a2febf9a Stephen Beaver
		return;	 // No package belongs to the pkg_id passed to this function.
537 49aec489 Phil Davis
	}
538 3e643dba Ermal LUÇI
539
	$package =& $config['installedpackages']['package'][$pkg_id];
540 49aec489 Phil Davis
	if (!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
541 6acdf659 Carlos Eduardo Ramos
		log_error(sprintf(gettext("The %s package is missing its configuration file and must be reinstalled."), $package['name']));
542 106574d1 Renato Botelho
		delete_package_xml($package['name']);
543 a0d4336f Renato Botelho
		return;
544 2c794549 Ermal
	}
545 a0d4336f Renato Botelho
546 2c794549 Ermal
	$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
547 49aec489 Phil Davis
	if (isset($pkg_config['nosync'])) {
548 2addd5b2 Ermal
		return;
549 49aec489 Phil Davis
	}
550 a0d4336f Renato Botelho
551 2c794549 Ermal
	/* Bring in package include files */
552
	if (!empty($pkg_config['include_file'])) {
553
		$include_file = $pkg_config['include_file'];
554 49aec489 Phil Davis
		if (file_exists($include_file)) {
555 2c794549 Ermal
			require_once($include_file);
556 49aec489 Phil Davis
		} else {
557 2c794549 Ermal
			log_error("Reinstalling package {$package['name']} because its include file({$include_file}) is missing!");
558
			uninstall_package($package['name']);
559 fad3ad59 Renato Botelho
			if (install_package($package['name']) != 0) {
560 2c794549 Ermal
				log_error("Reinstalling package {$package['name']} failed. Take appropriate measures!!!");
561 a0d4336f Renato Botelho
				return;
562
			}
563
			if (file_exists($include_file)) {
564
				require_once($include_file);
565
			} else {
566
				return;
567 83cfae8d Ermal Lu?i
			}
568 30e4c34a Scott Ullrich
		}
569 2c794549 Ermal
	}
570 30e4c34a Scott Ullrich
571 49aec489 Phil Davis
	if (!empty($pkg_config['custom_php_global_functions'])) {
572 2c794549 Ermal
		eval($pkg_config['custom_php_global_functions']);
573 49aec489 Phil Davis
	}
574
	if (!empty($pkg_config['custom_php_resync_config_command'])) {
575 2c794549 Ermal
		eval($pkg_config['custom_php_resync_config_command']);
576 49aec489 Phil Davis
	}
577 8c6516d1 Colin Smith
}
578
579 801fcf24 Renato Botelho
/* Read info.xml installed by package and return an array */
580
function read_package_config($package_name) {
581
	global $g;
582 e5b5e29c Renato Botelho
583 801fcf24 Renato Botelho
	$pkg_info_xml = '/usr/local/share/' . $g['pkg_prefix'] . $package_name . '/info.xml';
584 7860191a Renato Botelho
585 801fcf24 Renato Botelho
	if (!file_exists($pkg_info_xml)) {
586
		return false;
587
	}
588 7860191a Renato Botelho
589 801fcf24 Renato Botelho
	$pkg_info = parse_xml_config_pkg($pkg_info_xml, 'pfsensepkgs');
590 7860191a Renato Botelho
591 801fcf24 Renato Botelho
	if (empty($pkg_info)) {
592
		return false;
593 43dad535 Vinicius Coque
	}
594 801fcf24 Renato Botelho
595
	/* it always returns an array with 1 item */
596
	return $pkg_info['package'][0];
597 8c6516d1 Colin Smith
}
598
599 a2b0d909 Renato Botelho
/* Read package configurationfile and return an array */
600
function read_package_configurationfile($package_name) {
601
	global $config, $g;
602
603
	$pkg_config = array();
604
	$id = get_package_id($package_name);
605
606
	if ($id < 0 || !isset($config['installedpackages']['package'][$id]['configurationfile'])) {
607
		return $pkg_config;
608
	}
609
610
	$pkg_configurationfile = $config['installedpackages']['package'][$id]['configurationfile'];
611
612
	if (empty($pkg_configurationfile) || !file_exists('/usr/local/pkg/' . $pkg_configurationfile)) {
613
		return $pkg_config;
614
	}
615
616
	$pkg_config = parse_xml_config_pkg('/usr/local/pkg/' . $pkg_configurationfile, "packagegui");
617
618
	return $pkg_config;
619
}
620
621 801fcf24 Renato Botelho
function get_after_install_info($package_name) {
622 666c49ce Renato Botelho
	$pkg_config = read_package_config($package_name);
623 384e2647 Renato Botelho
624 801fcf24 Renato Botelho
	if (isset($pkg_config['after_install_info'])) {
625
		return $pkg_config['after_install_info'];
626 49aec489 Phil Davis
	}
627 384e2647 Renato Botelho
628 801fcf24 Renato Botelho
	return '';
629
}
630 384e2647 Renato Botelho
631 801fcf24 Renato Botelho
function eval_once($toeval) {
632
	global $evaled;
633
	if (!$evaled) {
634
		$evaled = array();
635 49aec489 Phil Davis
	}
636 801fcf24 Renato Botelho
	$evalmd5 = md5($toeval);
637
	if (!in_array($evalmd5, $evaled)) {
638
		@eval($toeval);
639
		$evaled[] = $evalmd5;
640 384e2647 Renato Botelho
	}
641 801fcf24 Renato Botelho
	return;
642 384e2647 Renato Botelho
}
643
644 801fcf24 Renato Botelho
function install_package_xml($package_name) {
645 e791dee8 Renato Botelho
	global $g, $config, $pkg_interface;
646 bfe9c9e7 jim-p
647 801fcf24 Renato Botelho
	if (($pkg_info = read_package_config($package_name)) == false) {
648
		return false;
649 49aec489 Phil Davis
	}
650 cfde64b8 Scott Ullrich
651 801fcf24 Renato Botelho
	/* safe side. Write config below will send to ro again. */
652
	conf_mount_rw();
653
654 c92ccac7 Vinicius Coque
	pkg_debug(gettext("Beginning package installation.") . "\n");
655
	log_error(sprintf(gettext('Beginning package installation for %s .'), $pkg_info['name']));
656 2a0e6517 Colin Smith
657 7597c8e8 Colin Smith
	/* add package information to config.xml */
658 af5d93f6 Renato Botelho
	$pkgid = get_package_id($pkg_info['name']);
659 e791dee8 Renato Botelho
	update_status(gettext("Saving updated package information...") . "\n");
660 49aec489 Phil Davis
	if ($pkgid == -1) {
661 7597c8e8 Colin Smith
		$config['installedpackages']['package'][] = $pkg_info;
662 086cf944 Phil Davis
		$changedesc = sprintf(gettext("Installed %s package."), $pkg_info['name']);
663 6acdf659 Carlos Eduardo Ramos
		$to_output = gettext("done.") . "\n";
664 7597c8e8 Colin Smith
	} else {
665
		$config['installedpackages']['package'][$pkgid] = $pkg_info;
666 6acdf659 Carlos Eduardo Ramos
		$changedesc = sprintf(gettext("Overwrote previous installation of %s."), $pkg_info['name']);
667
		$to_output = gettext("overwrite!") . "\n";
668 7597c8e8 Colin Smith
	}
669 801fcf24 Renato Botelho
	unlink_if_exists('/conf/needs_package_sync');
670 f9c8e64c jim-p
	write_config("Intermediate config write during package install for {$pkg_info['name']}.");
671 e8a3a81b Renato Botelho
	conf_mount_ro();
672 e791dee8 Renato Botelho
	update_status($to_output);
673 b2a66231 Ermal
674 801fcf24 Renato Botelho
	if (($pkgid = get_package_id($package_name)) == -1) {
675 e791dee8 Renato Botelho
		update_status(sprintf(gettext("The %s package is not installed.%sInstallation aborted."), $package_name, "\n\n"));
676 b2a66231 Ermal
677 801fcf24 Renato Botelho
		uninstall_package($package_name);
678
		write_config($changedesc);
679
		log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
680 e791dee8 Renato Botelho
		update_status(gettext("Failed to install package.") . "\n");
681 2c794549 Ermal
		return false;
682 e65a287f Scott Ullrich
	}
683 bfe9c9e7 jim-p
684 5a5cbf01 jim-p
	if (file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
685 e791dee8 Renato Botelho
		update_status(gettext("Loading package configuration... "));
686 5a5cbf01 jim-p
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $pkg_info['configurationfile'], "packagegui");
687 e791dee8 Renato Botelho
		update_status(gettext("done.") . "\n");
688
		update_status(gettext("Configuring package components...") . "\n");
689 49aec489 Phil Davis
		if (!empty($pkg_config['filter_rules_needed'])) {
690 bc771948 Ermal Lu?i
			$config['installedpackages']['package'][$pkgid]['filter_rule_function'] = $pkg_config['filter_rules_needed'];
691 49aec489 Phil Davis
		}
692 7597c8e8 Colin Smith
		/* modify system files */
693 b2a66231 Ermal
694 a2febf9a Stephen Beaver
		/* if a require exists, include it.  this will
695
		 * show us where an error exists in a package
696
		 * instead of making us blindly guess
697 2df5cb99 Scott Ullrich
		 */
698 fcf92dae Ermal
		$missing_include = false;
699 49aec489 Phil Davis
		if ($pkg_config['include_file'] <> "") {
700 e791dee8 Renato Botelho
			update_status(gettext("Loading package instructions...") . "\n");
701 49aec489 Phil Davis
			if (file_exists($pkg_config['include_file'])) {
702 9b1aa8d9 Renato Botelho
				pkg_debug("require_once('{$pkg_config['include_file']}')\n");
703 43ad432c Ermal Lu?i
				require_once($pkg_config['include_file']);
704 49aec489 Phil Davis
			} else {
705 9b1aa8d9 Renato Botelho
				pkg_debug("Missing include {$pkg_config['include_file']}\n");
706 fcf92dae Ermal
				$missing_include = true;
707 e791dee8 Renato Botelho
				update_status("Include " . basename($pkg_config['include_file']) . " is missing!\n");
708 801fcf24 Renato Botelho
709
				uninstall_package($package_name);
710
				write_config($changedesc);
711
				log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
712 e791dee8 Renato Botelho
				update_status(gettext("Failed to install package.") . "\n");
713 fcf92dae Ermal
				return false;
714
			}
715 43db85f8 Scott Ullrich
		}
716 57811192 Ermal
717
		/* custom commands */
718 e791dee8 Renato Botelho
		update_status(gettext("Custom commands...") . "\n");
719 57811192 Ermal
		if ($missing_include == false) {
720 49aec489 Phil Davis
			if ($pkg_config['custom_php_global_functions'] <> "") {
721 e791dee8 Renato Botelho
				update_status(gettext("Executing custom_php_global_functions()..."));
722 57811192 Ermal
				eval_once($pkg_config['custom_php_global_functions']);
723 e791dee8 Renato Botelho
				update_status(gettext("done.") . "\n");
724 57811192 Ermal
			}
725 49aec489 Phil Davis
			if ($pkg_config['custom_php_install_command']) {
726 e791dee8 Renato Botelho
				update_status(gettext("Executing custom_php_install_command()..."));
727 57811192 Ermal
				eval_once($pkg_config['custom_php_install_command']);
728 e791dee8 Renato Botelho
				update_status(gettext("done.") . "\n");
729 57811192 Ermal
			}
730 49aec489 Phil Davis
			if ($pkg_config['custom_php_resync_config_command'] <> "") {
731 e791dee8 Renato Botelho
				update_status(gettext("Executing custom_php_resync_config_command()..."));
732 57811192 Ermal
				eval_once($pkg_config['custom_php_resync_config_command']);
733 e791dee8 Renato Botelho
				update_status(gettext("done.") . "\n");
734 57811192 Ermal
			}
735
		}
736 7597c8e8 Colin Smith
		/* sidebar items */
737 49aec489 Phil Davis
		if (is_array($pkg_config['menu'])) {
738 e791dee8 Renato Botelho
			update_status(gettext("Menu items... "));
739 49aec489 Phil Davis
			foreach ($pkg_config['menu'] as $menu) {
740
				if (is_array($config['installedpackages']['menu'])) {
741
					foreach ($config['installedpackages']['menu'] as $amenu) {
742
						if ($amenu['name'] == $menu['name']) {
743 1570d27a Ermal Lu?i
							continue 2;
744 49aec489 Phil Davis
						}
745
					}
746
				} else {
747 27018d3c Ermal
					$config['installedpackages']['menu'] = array();
748 49aec489 Phil Davis
				}
749 1570d27a Ermal Lu?i
				$config['installedpackages']['menu'][] = $menu;
750 7597c8e8 Colin Smith
			}
751 e791dee8 Renato Botelho
			update_status(gettext("done.") . "\n");
752 7597c8e8 Colin Smith
		}
753 2dc264a4 Colin Smith
		/* services */
754 49aec489 Phil Davis
		if (is_array($pkg_config['service'])) {
755 e791dee8 Renato Botelho
			update_status(gettext("Services... "));
756 49aec489 Phil Davis
			foreach ($pkg_config['service'] as $service) {
757
				if (is_array($config['installedpackages']['service'])) {
758
					foreach ($config['installedpackages']['service'] as $aservice) {
759
						if ($aservice['name'] == $service['name']) {
760 d282095a Renato Botelho
							continue 2;
761 49aec489 Phil Davis
						}
762
					}
763
				} else {
764 27018d3c Ermal
					$config['installedpackages']['service'] = array();
765 49aec489 Phil Davis
				}
766 2dc264a4 Colin Smith
				$config['installedpackages']['service'][] = $service;
767
			}
768 e791dee8 Renato Botelho
			update_status(gettext("done.") . "\n");
769 2dc264a4 Colin Smith
		}
770 7597c8e8 Colin Smith
	} else {
771 9b1aa8d9 Renato Botelho
		pkg_debug("Unable to find config file\n");
772 e791dee8 Renato Botelho
		update_status(gettext("Loading package configuration... failed!") . "\n\n" . gettext("Installation aborted."));
773 c92ccac7 Vinicius Coque
		pkg_debug(gettext("Unable to load package configuration. Installation aborted.") ."\n");
774 801fcf24 Renato Botelho
775
		uninstall_package($package_name);
776
		write_config($changedesc);
777
		log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
778 e791dee8 Renato Botelho
		update_status(gettext("Failed to install package.") . "\n");
779 2c794549 Ermal
		return false;
780 7597c8e8 Colin Smith
	}
781 2c794549 Ermal
782
	/* set up package logging streams */
783 49aec489 Phil Davis
	if ($pkg_info['logging']) {
784 2c794549 Ermal
		system_syslogd_start();
785
	}
786
787 e791dee8 Renato Botelho
	update_status(gettext("Writing configuration... "));
788 801fcf24 Renato Botelho
	write_config($changedesc);
789
	log_error(sprintf(gettext("Successfully installed package: %s."), $pkg_info['name']));
790 e791dee8 Renato Botelho
	update_status(gettext("done.") . "\n");
791 801fcf24 Renato Botelho
	if ($pkg_info['after_install_info']) {
792 e791dee8 Renato Botelho
		update_status($pkg_info['after_install_info']);
793 2d26ee5e Sjon Hortensius
	}
794 801fcf24 Renato Botelho
795 2c794549 Ermal
	return true;
796 64974db7 Scott Ullrich
}
797
798 3bb7e3e8 Renato Botelho
function delete_package_xml($package_name, $when = "post-deinstall") {
799 e791dee8 Renato Botelho
	global $g, $config, $pkg_interface;
800 b2a66231 Ermal
801 232b01db jim-p
	conf_mount_rw();
802 6955830f Ermal Lu?i
803 3bb7e3e8 Renato Botelho
	$pkgid = get_package_id($package_name);
804 b2a66231 Ermal
	if ($pkgid == -1) {
805 e791dee8 Renato Botelho
		update_status(sprintf(gettext("The %s package is not installed.%sDeletion aborted."), $package_name, "\n\n"));
806 e65a287f Scott Ullrich
		ob_flush();
807
		sleep(1);
808 3339fac0 Ermal Lu?i
		conf_mount_ro();
809 e65a287f Scott Ullrich
		return;
810
	}
811 086cf944 Phil Davis
	pkg_debug(sprintf(gettext("Removing %s package... "), $package_name));
812 e791dee8 Renato Botelho
	update_status(sprintf(gettext("Removing %s components..."), $package_name) . "\n");
813 407bf67a Colin Smith
	/* parse package configuration */
814
	$packages = &$config['installedpackages']['package'];
815 b63f2e8b Matthew Grooms
	$menus =& $config['installedpackages']['menu'];
816 3c41c4ab Colin Smith
	$services = &$config['installedpackages']['service'];
817 2addd5b2 Ermal
	$pkg_info =& $packages[$pkgid];
818 49aec489 Phil Davis
	if (file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
819 19a11678 Colin Smith
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $packages[$pkgid]['configurationfile'], "packagegui");
820 3f01fe47 Colin Smith
		/* remove menu items */
821 49aec489 Phil Davis
		if (is_array($pkg_config['menu'])) {
822 e791dee8 Renato Botelho
			update_status(gettext("Menu items... "));
823 8604523b Ermal Lu?i
			if (is_array($pkg_config['menu']) && is_array($menus)) {
824 49aec489 Phil Davis
				foreach ($pkg_config['menu'] as $menu) {
825
					foreach ($menus as $key => $instmenu) {
826
						if ($instmenu['name'] == $menu['name']) {
827 8604523b Ermal Lu?i
							unset($menus[$key]);
828 b2a66231 Ermal
							break;
829
						}
830
					}
831 8604523b Ermal Lu?i
				}
832
			}
833 e791dee8 Renato Botelho
			update_status(gettext("done.") . "\n");
834 407bf67a Colin Smith
		}
835 3c41c4ab Colin Smith
		/* remove services */
836 49aec489 Phil Davis
		if (is_array($pkg_config['service'])) {
837 e791dee8 Renato Botelho
			update_status(gettext("Services... "));
838 8604523b Ermal Lu?i
			if (is_array($pkg_config['service']) && is_array($services)) {
839 49aec489 Phil Davis
				foreach ($pkg_config['service'] as $service) {
840
					foreach ($services as $key => $instservice) {
841
						if ($instservice['name'] == $service['name']) {
842
							if (platform_booting() != true) {
843 06e57df8 Scott Ullrich
								stop_service($service['name']);
844 49aec489 Phil Davis
							}
845
							if ($service['rcfile']) {
846 c5966711 phildd
								$prefix = RCFILEPREFIX;
847 49aec489 Phil Davis
								if (!empty($service['prefix'])) {
848 941baf1e Ermal
									$prefix = $service['prefix'];
849 49aec489 Phil Davis
								}
850
								if (file_exists("{$prefix}{$service['rcfile']}")) {
851 941baf1e Ermal
									@unlink("{$prefix}{$service['rcfile']}");
852 49aec489 Phil Davis
								}
853 941baf1e Ermal
							}
854 8604523b Ermal Lu?i
							unset($services[$key]);
855
						}
856 0cab7cad Colin Smith
					}
857 3c41c4ab Colin Smith
				}
858
			}
859 e791dee8 Renato Botelho
			update_status(gettext("done.") . "\n");
860 3c41c4ab Colin Smith
		}
861 b2a66231 Ermal
		/*
862
		 * XXX: Otherwise inclusion of config.inc again invalidates actions taken.
863 a2febf9a Stephen Beaver
		 *	Same is done during installation.
864 b2a66231 Ermal
		 */
865 3bb7e3e8 Renato Botelho
		write_config("Intermediate config write during package removal for {$package_name}.");
866 b2a66231 Ermal
867
		/*
868 a2febf9a Stephen Beaver
		 * If a require exists, include it.	 this will
869 b2a66231 Ermal
		 * show us where an error exists in a package
870
		 * instead of making us blindly guess
871 892aef15 Scott Ullrich
		 */
872 fcf92dae Ermal
		$missing_include = false;
873 49aec489 Phil Davis
		if ($pkg_config['include_file'] <> "") {
874 e791dee8 Renato Botelho
			update_status(gettext("Loading package instructions...") . "\n");
875 49aec489 Phil Davis
			if (file_exists($pkg_config['include_file'])) {
876 9b1aa8d9 Renato Botelho
				pkg_debug("require_once(\"{$pkg_config['include_file']}\")\n");
877 1570d27a Ermal Lu?i
				require_once($pkg_config['include_file']);
878 49aec489 Phil Davis
			} else {
879 9b1aa8d9 Renato Botelho
				pkg_debug("Missing include {$pkg_config['include_file']}\n");
880 fcf92dae Ermal
				$missing_include = true;
881 e791dee8 Renato Botelho
				update_status("Include file " . basename($pkg_config['include_file']) . " could not be found for inclusion.\n");
882 fcf92dae Ermal
			}
883
		}
884
		/* ermal
885
		 * NOTE: It is not possible to handle parse errors on eval.
886
		 * So we prevent it from being run at all to not interrupt all the other code.
887
		 */
888 3bb7e3e8 Renato Botelho
		if ($when == "deinstall" && $missing_include == false) {
889 49aec489 Phil Davis
			/* evaluate this package's global functions and pre deinstall commands */
890
			if ($pkg_config['custom_php_global_functions'] <> "") {
891 fcf92dae Ermal
				eval_once($pkg_config['custom_php_global_functions']);
892 49aec489 Phil Davis
			}
893
			if ($pkg_config['custom_php_pre_deinstall_command'] <> "") {
894 fcf92dae Ermal
				eval_once($pkg_config['custom_php_pre_deinstall_command']);
895 49aec489 Phil Davis
			}
896 43db85f8 Scott Ullrich
		}
897 644d2d59 Colin Smith
		/* deinstall commands */
898 3bb7e3e8 Renato Botelho
		if ($when == "post-deinstall" && $pkg_config['custom_php_deinstall_command'] <> "") {
899 e791dee8 Renato Botelho
			update_status(gettext("Deinstall commands... "));
900 fcf92dae Ermal
			if ($missing_include == false) {
901
				eval_once($pkg_config['custom_php_deinstall_command']);
902 e791dee8 Renato Botelho
				update_status(gettext("done.") . "\n");
903 49aec489 Phil Davis
			} else {
904 e791dee8 Renato Botelho
				update_status("\nNot executing custom deinstall hook because an include is missing.\n");
905 49aec489 Phil Davis
			}
906 644d2d59 Colin Smith
		}
907 407bf67a Colin Smith
	}
908 2addd5b2 Ermal
	/* syslog */
909 ce7edfff doktornotor
	$need_syslog_restart = false;
910 8a82c222 doktornotor
	if (is_array($pkg_info['logging']) && $pkg_info['logging']['logfilename'] <> "") {
911 e791dee8 Renato Botelho
		update_status("Syslog entries... ");
912 2addd5b2 Ermal
		@unlink("{$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
913 e791dee8 Renato Botelho
		update_status("done.\n");
914 ce7edfff doktornotor
		$need_syslog_restart = true;
915 2addd5b2 Ermal
	}
916 2d26ee5e Sjon Hortensius
917 6b861ecd Renato Botelho
	if ($when == "post-deinstall") {
918
		/* remove config.xml entries */
919
		update_status(gettext("Configuration... "));
920
		unset($config['installedpackages']['package'][$pkgid]);
921
		update_status(gettext("done.") . "\n");
922
		write_config("Removed {$package_name} package.\n");
923
	}
924 ce7edfff doktornotor
925
	/* remove package entry from /etc/syslog.conf if needed */
926 f164ace4 doktornotor
	/* this must be done after removing the entries from config.xml */
927 ce7edfff doktornotor
	if ($need_syslog_restart) {
928
		system_syslogd_start();
929
	}
930
931 e8a3a81b Renato Botelho
	conf_mount_ro();
932 f0a550fd Colin Smith
}
933 5025a56c Scott Ullrich
934 46903fb9 Renato Botelho
/*
935
 * Used during upgrade process or retore backup process, verify all
936
 * packages installed in config.xml and install pkg accordingly
937
 */
938
function package_reinstall_all() {
939 e791dee8 Renato Botelho
	global $g, $config, $pkg_interface;
940 c53eb903 Ermal
941 46903fb9 Renato Botelho
	if (!isset($config['installedpackages']['package']) ||
942
	    !is_array($config['installedpackages']['package'])) {
943
		return true;
944
	}
945
946
	$upgrade = (file_exists('/conf/needs_package_sync') && platform_booting());
947
948
	/* During boot after upgrade, wait for internet connection */
949
	if ($upgrade) {
950 e791dee8 Renato Botelho
		update_status(gettext("Waiting for internet connection to update pkg metadata and fini package reinstallation"));
951 46903fb9 Renato Botelho
		while (true) {
952
			if (pkg_update(true)) {
953
				break;
954
			}
955 e791dee8 Renato Botelho
			update_status('.');
956 46903fb9 Renato Botelho
			sleep(1);
957
		}
958 e791dee8 Renato Botelho
		update_status("\n");
959 46903fb9 Renato Botelho
	}
960
961
	$pkg_info = get_pkg_info();
962
963
	foreach ($config['installedpackages']['package'] as $package) {
964
		$found = false;
965
		$internal_name = get_package_internal_name($package);
966
		foreach ($pkg_info as $pkg) {
967
			pkg_remove_prefix($pkg['name']);
968
			if ($pkg['name'] == $internal_name) {
969
				$found = true;
970
				break;
971
			}
972
		}
973
974
		if (!$found) {
975
			if (!function_exists("file_notice")) {
976
				require_once("notices.inc");
977
			}
978
979
			file_notice(gettext("Package reinstall"),
980
			    sprintf(gettext("Package %s does not exist in current %s version and it has been removed."), $package['name'], $g['product_name']));
981
			uninstall_package($package['name']);
982
		}
983
	}
984
985
	/* Obsoleted packages were removed, lets reinstall all remaining */
986
	foreach ($config['installedpackages']['package'] as $package) {
987
		$internal_name = get_package_internal_name($package);
988 f5b1c660 Renato Botelho
		pkg_install($g['pkg_prefix'] . $internal_name, true);
989 46903fb9 Renato Botelho
	}
990
991
	return true;
992 9b193619 Scott Ullrich
}
993
994 60dd7649 jim-p
function stop_packages() {
995
	require_once("config.inc");
996
	require_once("functions.inc");
997
	require_once("filter.inc");
998
	require_once("shaper.inc");
999
	require_once("captiveportal.inc");
1000
	require_once("pkg-utils.inc");
1001
	require_once("pfsense-utils.inc");
1002
	require_once("service-utils.inc");
1003
1004 c5966711 phildd
	global $config, $g;
1005 60dd7649 jim-p
1006
	log_error("Stopping all packages.");
1007
1008 c5966711 phildd
	$rcfiles = glob(RCFILEPREFIX . "*.sh");
1009 49aec489 Phil Davis
	if (!$rcfiles) {
1010 60dd7649 jim-p
		$rcfiles = array();
1011 49aec489 Phil Davis
	} else {
1012 60dd7649 jim-p
		$rcfiles = array_flip($rcfiles);
1013 49aec489 Phil Davis
		if (!$rcfiles) {
1014 60dd7649 jim-p
			$rcfiles = array();
1015 49aec489 Phil Davis
		}
1016 60dd7649 jim-p
	}
1017
1018
	if (is_array($config['installedpackages']['package'])) {
1019 49aec489 Phil Davis
		foreach ($config['installedpackages']['package'] as $package) {
1020 60dd7649 jim-p
			echo " Stopping package {$package['name']}...";
1021 af5d93f6 Renato Botelho
			$internal_name = get_package_internal_name($package);
1022 75a01a7c Phil Davis
			stop_service($internal_name);
1023
			unset($rcfiles[RCFILEPREFIX . strtolower($internal_name) . ".sh"]);
1024 60dd7649 jim-p
			echo "done.\n";
1025
		}
1026
	}
1027
1028 6186cdc4 jim-p
	foreach ($rcfiles as $rcfile => $number) {
1029
		$shell = @popen("/bin/sh", "w");
1030
		if ($shell) {
1031 60dd7649 jim-p
			echo " Stopping {$rcfile}...";
1032 61ef1385 Ermal
			if (!@fwrite($shell, "{$rcfile} stop >>/tmp/bootup_messages 2>&1")) {
1033 49aec489 Phil Davis
				if ($shell) {
1034 61ef1385 Ermal
					pclose($shell);
1035 49aec489 Phil Davis
				}
1036 61ef1385 Ermal
				$shell = @popen("/bin/sh", "w");
1037
			}
1038 60dd7649 jim-p
			echo "done.\n";
1039 6186cdc4 jim-p
			pclose($shell);
1040 60dd7649 jim-p
		}
1041
	}
1042
}
1043
1044 d5f0597b Renato Botelho
/* Identify which meta package is installed */
1045
function get_meta_pkg_name() {
1046
	global $g;
1047
1048
	/* XXX: Use pkg annotation */
1049
	if (is_pkg_installed($g['product_name'])) {
1050
		return $g['product_name'];
1051
	} else if (is_pkg_installed($g['product_name'] . '-vmware')) {
1052
		return $g['product_name'] . '-vmware';
1053
	}
1054
	return false;
1055
}
1056
1057
/* Identify which base package is installed */
1058
function get_base_pkg_name() {
1059
	global $g;
1060
1061
	/* XXX: Use pkg annotation */
1062
	if (is_pkg_installed($g['product_name'] . '-base-' . $g['platform'])) {
1063
		return $g['product_name'];
1064
		return $g['product_name'] . '-base-' . $g['platform'];
1065
	} else if (is_pkg_installed($g['product_name'] . '-base')) {
1066
		return $g['product_name'] . '-base';
1067
	}
1068
	return false;
1069
}
1070
1071
/* Verify if system needs upgrade (meta package or base) */
1072
function get_system_pkg_version() {
1073
	global $g;
1074
1075
	$base_pkg = get_base_pkg_name();
1076
	$meta_pkg = get_meta_pkg_name();
1077
1078
	if (!$base_pkg || !$meta_pkg) {
1079
		return false;
1080
	}
1081
1082
	$info = get_pkg_info($base_pkg);
1083
	$pkg_name = $base_pkg;
1084
1085
	$pkg_info = array();
1086
	foreach ($info as $item) {
1087
		if ($item['name'] == $base_pkg) {
1088
			$pkg_info = $item;
1089
		}
1090
	}
1091
1092
	if (empty($pkg_info) ||
1093
	    $pkg_info['version'] == $pkg_info['installed_version']) {
1094
		$info = get_pkg_info($meta_pkg);
1095
		$pkg_name = $meta_pkg;
1096
1097
		foreach ($info as $item) {
1098 59d256a1 Renato Botelho
			if ($item['name'] == $meta_pkg) {
1099 d5f0597b Renato Botelho
				$pkg_info = $item;
1100
			}
1101
		}
1102
	}
1103
1104
	if (empty($pkg_info)) {
1105
		return false;
1106
	}
1107
1108
	return array(
1109
	    'pkg_name'          => $pkg_name,
1110
	    'version'           => $pkg_info['version'],
1111
	    'installed_version' => $pkg_info['installed_version']
1112
	);
1113
}
1114
1115 ad3c9763 Renato Botelho
/* Switch between stable and devel repos */
1116
function pkg_switch_repo($devel = false) {
1117
	global $g;
1118
1119
	$repo_stable = $g['product_name'] . '-repo';
1120
	$repo_devel = $g['product_name'] . '-repo-devel';
1121
1122
	if ($devel) {
1123
		$repo_target = $repo_devel;
1124
	} else {
1125
		$repo_target = $repo_stable;
1126
	}
1127
1128
	if (is_pkg_installed($repo_target)) {
1129
		/* It's already installed */
1130
		return true;
1131
	}
1132
1133
	/*
1134
	 * Since both install files in the same place, just
1135
	 * call pkg_install for target and current one will
1136
	 * be replaced
1137
	 */
1138
	return pkg_install($repo_target, true);
1139
}
1140
1141 61ef1385 Ermal
?>