Project

General

Profile

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