1
|
<?php
|
2
|
/*
|
3
|
* pkg-utils.inc
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2005-2006 Colin Smith (ethethlay@gmail.com)
|
7
|
* Copyright (c) 2004-2013 BSD Perimeter
|
8
|
* Copyright (c) 2013-2016 Electric Sheep Fencing
|
9
|
* Copyright (c) 2014-2019 Rubicon Communications, LLC (Netgate)
|
10
|
* All rights reserved.
|
11
|
*
|
12
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
13
|
* you may not use this file except in compliance with the License.
|
14
|
* You may obtain a copy of the License at
|
15
|
*
|
16
|
* http://www.apache.org/licenses/LICENSE-2.0
|
17
|
*
|
18
|
* Unless required by applicable law or agreed to in writing, software
|
19
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
20
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
21
|
* See the License for the specific language governing permissions and
|
22
|
* limitations under the License.
|
23
|
*/
|
24
|
|
25
|
require_once("globals.inc");
|
26
|
require_once("service-utils.inc");
|
27
|
|
28
|
if (file_exists("/cf/conf/use_xmlreader")) {
|
29
|
require_once("xmlreader.inc");
|
30
|
} else {
|
31
|
require_once("xmlparse.inc");
|
32
|
}
|
33
|
|
34
|
require_once("pfsense-utils.inc");
|
35
|
|
36
|
if (!function_exists("pkg_debug")) {
|
37
|
/* set up logging if needed */
|
38
|
function pkg_debug($msg) {
|
39
|
global $g, $debug, $fd_log;
|
40
|
|
41
|
if (!$debug) {
|
42
|
return;
|
43
|
}
|
44
|
|
45
|
if (!$fd_log) {
|
46
|
if (!$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_debug.log", "w")) {
|
47
|
update_status(gettext("Warning, could not open log for writing.") . "\n");
|
48
|
}
|
49
|
}
|
50
|
@fwrite($fd_log, $msg);
|
51
|
}
|
52
|
}
|
53
|
|
54
|
/* Validate if pkg name is valid */
|
55
|
function pkg_valid_name($pkgname) {
|
56
|
global $g;
|
57
|
|
58
|
$pattern = "/^{$g['pkg_prefix']}[a-zA-Z0-9\.\-_]+$/";
|
59
|
return preg_match($pattern, $pkgname);
|
60
|
}
|
61
|
|
62
|
/* Remove pkg_prefix from package name if it's present */
|
63
|
function pkg_remove_prefix(&$pkg_name) {
|
64
|
global $g;
|
65
|
|
66
|
if (substr($pkg_name, 0, strlen($g['pkg_prefix'])) == $g['pkg_prefix']) {
|
67
|
$pkg_name = substr($pkg_name, strlen($g['pkg_prefix']));
|
68
|
}
|
69
|
}
|
70
|
|
71
|
/* Execute pkg update when it's necessary */
|
72
|
function pkg_update($force = false) {
|
73
|
global $g;
|
74
|
|
75
|
return pkg_call("update" . ($force ? " -f" : ""));
|
76
|
}
|
77
|
|
78
|
/* return an array with necessary environment vars for pkg */
|
79
|
function pkg_env($extra_env = array()) {
|
80
|
global $config, $g;
|
81
|
|
82
|
$user_agent = $g['product_name'] . '/' . $g['product_version'];
|
83
|
if (!isset($config['system']['do_not_send_uniqueid'])) {
|
84
|
$user_agent .= ':' . system_get_uniqueid();
|
85
|
}
|
86
|
|
87
|
$pkg_env_vars = array(
|
88
|
"LANG" => "C",
|
89
|
"HTTP_USER_AGENT" => $user_agent,
|
90
|
"ASSUME_ALWAYS_YES" => "true",
|
91
|
"FETCH_TIMEOUT" => 5,
|
92
|
"FETCH_RETRY" => 2
|
93
|
);
|
94
|
|
95
|
if (!empty($config['system']['proxyurl'])) {
|
96
|
$http_proxy = $config['system']['proxyurl'];
|
97
|
if (!empty($config['system']['proxyport'])) {
|
98
|
$http_proxy .= ':' . $config['system']['proxyport'];
|
99
|
}
|
100
|
$pkg_env_vars['HTTP_PROXY'] = $http_proxy;
|
101
|
|
102
|
if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
|
103
|
$pkg_env_vars['HTTP_PROXY_AUTH'] = "basic:*:{$config['system']['proxyuser']}:{$config['system']['proxypass']}";
|
104
|
}
|
105
|
}
|
106
|
|
107
|
if (isset($config['system']['use_mfs_tmpvar'])) {
|
108
|
$pkg_env_vars['PKG_DBDIR'] = '/root/var/db/pkg';
|
109
|
$pkg_env_vars['PKG_CACHEDIR'] = '/root/var/cache/pkg';
|
110
|
}
|
111
|
|
112
|
foreach ($extra_env as $key => $value) {
|
113
|
$pkg_env_vars[$key] = $value;
|
114
|
}
|
115
|
|
116
|
return $pkg_env_vars;
|
117
|
}
|
118
|
|
119
|
/* Execute a pkg call */
|
120
|
function pkg_call($params, $mute = false, $extra_env = array()) {
|
121
|
global $g, $config;
|
122
|
|
123
|
if (empty($params)) {
|
124
|
return false;
|
125
|
}
|
126
|
|
127
|
$descriptorspec = array(
|
128
|
1 => array("pipe", "w"), /* stdout */
|
129
|
2 => array("pipe", "w") /* stderr */
|
130
|
);
|
131
|
|
132
|
pkg_debug("pkg_call(): {$params}\n");
|
133
|
$process = proc_open("/usr/local/sbin/pkg-static {$params}",
|
134
|
$descriptorspec, $pipes, '/', pkg_env($extra_env));
|
135
|
|
136
|
if (!is_resource($process)) {
|
137
|
return false;
|
138
|
}
|
139
|
|
140
|
stream_set_blocking($pipes[1], 0);
|
141
|
stream_set_blocking($pipes[2], 0);
|
142
|
|
143
|
/* XXX: should be a tunnable? */
|
144
|
$timeout = 60; // seconds
|
145
|
$error_log = '';
|
146
|
|
147
|
do {
|
148
|
$write = array();
|
149
|
$read = array($pipes[1], $pipes[2]);
|
150
|
$except = array();
|
151
|
|
152
|
$stream = @stream_select($read, $write, $except, $timeout);
|
153
|
if ($stream !== FALSE && $stream > 0) {
|
154
|
foreach ($read as $pipe) {
|
155
|
$content = stream_get_contents($pipe);
|
156
|
if ($content == '') {
|
157
|
continue;
|
158
|
}
|
159
|
if ($pipe === $pipes[1]) {
|
160
|
if (!$mute) {
|
161
|
update_status($content);
|
162
|
}
|
163
|
flush();
|
164
|
} else if ($pipe === $pipes[2]) {
|
165
|
$error_log .= $content;
|
166
|
}
|
167
|
}
|
168
|
}
|
169
|
|
170
|
$status = proc_get_status($process);
|
171
|
} while ($status['running']);
|
172
|
|
173
|
fclose($pipes[1]);
|
174
|
fclose($pipes[2]);
|
175
|
proc_close($process);
|
176
|
|
177
|
|
178
|
$rc = $status['exitcode'];
|
179
|
|
180
|
pkg_debug("pkg_call(): rc = {$rc}\n");
|
181
|
if ($rc == 0) {
|
182
|
return true;
|
183
|
}
|
184
|
|
185
|
pkg_debug("pkg_call(): error_log\n{$error_log}\n");
|
186
|
if (!$mute) {
|
187
|
update_status("\n\n" . sprintf(gettext(
|
188
|
"ERROR!!! An error occurred on pkg execution (rc = %d) with parameters '%s':"),
|
189
|
$rc, $params) . "\n" . $error_log . "\n");
|
190
|
}
|
191
|
|
192
|
return false;
|
193
|
}
|
194
|
|
195
|
/* Execute pkg with $params, fill stdout and stderr and return pkg rc */
|
196
|
function pkg_exec($params, &$stdout, &$stderr, $extra_env = array()) {
|
197
|
global $g, $config;
|
198
|
|
199
|
if (empty($params)) {
|
200
|
return -1;
|
201
|
}
|
202
|
|
203
|
$descriptorspec = array(
|
204
|
1 => array("pipe", "w"), /* stdout */
|
205
|
2 => array("pipe", "w") /* stderr */
|
206
|
);
|
207
|
|
208
|
|
209
|
pkg_debug("pkg_exec(): {$params}\n");
|
210
|
$process = proc_open("/usr/local/sbin/pkg-static {$params}",
|
211
|
$descriptorspec, $pipes, '/', pkg_env($extra_env));
|
212
|
|
213
|
if (!is_resource($process)) {
|
214
|
return -1;
|
215
|
}
|
216
|
|
217
|
$stdout = '';
|
218
|
while (($l = fgets($pipes[1])) !== FALSE) {
|
219
|
$stdout .= $l;
|
220
|
}
|
221
|
fclose($pipes[1]);
|
222
|
|
223
|
$stderr = '';
|
224
|
while (($l = fgets($pipes[2])) !== FALSE) {
|
225
|
$stderr .= $l;
|
226
|
}
|
227
|
fclose($pipes[2]);
|
228
|
|
229
|
|
230
|
return proc_close($process);
|
231
|
}
|
232
|
|
233
|
/* Compare 2 pkg versions and return:
|
234
|
* '=' - versions are the same
|
235
|
* '>' - $v1 > $v2
|
236
|
* '<' - $v1 < $v2
|
237
|
* '?' - Error
|
238
|
*/
|
239
|
function pkg_version_compare($v1, $v2) {
|
240
|
if (empty($v1) || empty($v2)) {
|
241
|
return '?';
|
242
|
}
|
243
|
|
244
|
$rc = pkg_exec("version -t '{$v1}' '{$v2}'", $stdout, $stderr);
|
245
|
|
246
|
if ($rc != 0) {
|
247
|
return '?';
|
248
|
}
|
249
|
|
250
|
return str_replace("\n", "", $stdout);
|
251
|
}
|
252
|
|
253
|
/* Check if package is installed */
|
254
|
function is_pkg_installed($pkg_name) {
|
255
|
global $g;
|
256
|
|
257
|
if (empty($pkg_name)) {
|
258
|
return false;
|
259
|
}
|
260
|
|
261
|
return pkg_call("info -e " . $pkg_name, true);
|
262
|
}
|
263
|
|
264
|
/* Install package, $pkg_name should not contain prefix */
|
265
|
function pkg_install($pkg_name, $force = false) {
|
266
|
global $g;
|
267
|
$result = false;
|
268
|
|
269
|
$shortname = $pkg_name;
|
270
|
pkg_remove_prefix($shortname);
|
271
|
|
272
|
$pkg_force = "";
|
273
|
if ($force) {
|
274
|
$pkg_force = "-f ";
|
275
|
}
|
276
|
|
277
|
pkg_debug("Installing package {$shortname}\n");
|
278
|
if ($force || !is_pkg_installed($pkg_name)) {
|
279
|
$result = pkg_call("install -y " . $pkg_force . $pkg_name);
|
280
|
/* Cleanup cacke to free disk space */
|
281
|
pkg_call("clean -y");
|
282
|
}
|
283
|
|
284
|
return $result;
|
285
|
}
|
286
|
|
287
|
/* Delete package from FreeBSD, $pkg_name should not contain prefix */
|
288
|
function pkg_delete($pkg_name) {
|
289
|
global $g;
|
290
|
|
291
|
$shortname = $pkg_name;
|
292
|
pkg_remove_prefix($shortname);
|
293
|
|
294
|
pkg_debug("Removing package {$shortname}\n");
|
295
|
if (is_pkg_installed($pkg_name)) {
|
296
|
pkg_call("delete -y " . $pkg_name);
|
297
|
/* Cleanup unecessary dependencies */
|
298
|
pkg_call("autoremove -y");
|
299
|
}
|
300
|
}
|
301
|
|
302
|
/* Check if package is present in config.xml */
|
303
|
function is_package_installed($package_name) {
|
304
|
return (get_package_id($package_name) != -1);
|
305
|
}
|
306
|
|
307
|
/* Find package array index */
|
308
|
function get_package_id($package_name) {
|
309
|
global $config;
|
310
|
|
311
|
if (!isset($config['installedpackages']['package']) ||
|
312
|
!is_array($config['installedpackages']['package'])) {
|
313
|
return -1;
|
314
|
}
|
315
|
|
316
|
foreach ($config['installedpackages']['package'] as $idx => $pkg) {
|
317
|
if ($pkg['name'] == $package_name ||
|
318
|
get_package_internal_name($pkg) == $package_name) {
|
319
|
return $idx;
|
320
|
}
|
321
|
}
|
322
|
|
323
|
return -1;
|
324
|
}
|
325
|
|
326
|
/* Return internal_name when it's defined, otherwise, returns name */
|
327
|
function get_package_internal_name($package_data) {
|
328
|
if (isset($package_data['internal_name']) && ($package_data['internal_name'] != "")) {
|
329
|
/* e.g. name is Ipguard-dev, internal name is ipguard */
|
330
|
return $package_data['internal_name'];
|
331
|
} else {
|
332
|
return $package_data['name'];
|
333
|
}
|
334
|
}
|
335
|
|
336
|
// Get information about packages.
|
337
|
function get_pkg_info($pkgs = 'all', $remote_repo_usage_disabled = false,
|
338
|
$installed_pkgs_only = false) {
|
339
|
global $g, $input_errors;
|
340
|
|
341
|
$out = $err = $extra_param = '';
|
342
|
$rc = 0;
|
343
|
|
344
|
unset($pkg_filter);
|
345
|
|
346
|
if (is_array($pkgs)) {
|
347
|
$pkg_filter = $pkgs;
|
348
|
$pkgs = $g['pkg_prefix'] . '*';
|
349
|
} elseif ($pkgs == 'all') {
|
350
|
$pkgs = $g['pkg_prefix'] . '*';
|
351
|
}
|
352
|
|
353
|
$base_packages = (substr($pkgs, 0, strlen($g['pkg_prefix'])) !=
|
354
|
$g['pkg_prefix']);
|
355
|
|
356
|
if ($installed_pkgs_only && !is_pkg_installed($pkgs)) {
|
357
|
/*
|
358
|
* Return early if the caller wants just installed packages
|
359
|
* and there are none. Saves doing any calls that might
|
360
|
* access a remote package repo.
|
361
|
*/
|
362
|
return array();
|
363
|
}
|
364
|
|
365
|
if (!function_exists('is_subsystem_dirty')) {
|
366
|
require_once("util.inc");
|
367
|
}
|
368
|
|
369
|
/* Do not run remote operations if pkg has a lock */
|
370
|
if (is_subsystem_dirty('pkg')) {
|
371
|
$remote_repo_usage_disabled = true;
|
372
|
$lock = false;
|
373
|
} else {
|
374
|
$lock = true;
|
375
|
}
|
376
|
|
377
|
if ($lock) {
|
378
|
mark_subsystem_dirty('pkg');
|
379
|
}
|
380
|
|
381
|
if ($remote_repo_usage_disabled) {
|
382
|
$extra_param = "-U ";
|
383
|
}
|
384
|
|
385
|
$did_search = false;
|
386
|
$search_rc = 0;
|
387
|
$info_rc = 0;
|
388
|
$search_items = array();
|
389
|
$info_items = array();
|
390
|
|
391
|
if ($base_packages) {
|
392
|
$repo_param = "";
|
393
|
} else {
|
394
|
$repo_param = "-r {$g['product_name']}";
|
395
|
}
|
396
|
|
397
|
/*
|
398
|
* If we want more than just the currently installed packages or
|
399
|
* we want up-to-date remote repo info then do a full pkg search
|
400
|
*/
|
401
|
if (!$installed_pkgs_only || !$remote_repo_usage_disabled) {
|
402
|
$did_search = true;
|
403
|
$search_rc = pkg_exec("search {$repo_param} " .
|
404
|
"{$extra_param}-R --raw-format json-compact " .
|
405
|
$pkgs, $search_out, $search_err);
|
406
|
if ($search_rc == 0) {
|
407
|
$search_items = explode("\n", chop($search_out));
|
408
|
array_walk($search_items, function(&$v, &$k) {
|
409
|
$v = json_decode($v, true);
|
410
|
});
|
411
|
}
|
412
|
}
|
413
|
|
414
|
/*
|
415
|
* We always should look for local items to detect packages that
|
416
|
* were removed from remote repo but are already installed locally
|
417
|
*
|
418
|
* Take pkg search return code into consideration to fallback to local
|
419
|
* information when remote repo is not accessible
|
420
|
*/
|
421
|
if (is_pkg_installed($pkgs) || $search_rc != 0) {
|
422
|
$info_rc = pkg_exec("info -R --raw-format json-compact " .
|
423
|
$pkgs, $info_out, $info_err);
|
424
|
if ($info_rc == 0) {
|
425
|
$info_items = explode("\n", chop($info_out));
|
426
|
array_walk($info_items, function(&$v, &$k) {
|
427
|
$v = json_decode($v, true);
|
428
|
});
|
429
|
}
|
430
|
}
|
431
|
|
432
|
if ($lock) {
|
433
|
clear_subsystem_dirty('pkg');
|
434
|
}
|
435
|
|
436
|
if ($search_rc != 0 && $info_rc != 0) {
|
437
|
update_status("\n" . gettext(
|
438
|
"ERROR: Error trying to get packages list. Aborting...")
|
439
|
. "\n");
|
440
|
update_status($search_err . "\n" . $info_err);
|
441
|
$input_errors[] = gettext(
|
442
|
"ERROR: Error trying to get packages list. Aborting...") .
|
443
|
"\n";
|
444
|
$input_errors[] = $search_err . "\n" . $info_err;
|
445
|
return array();
|
446
|
}
|
447
|
|
448
|
/* It was not possible to search, use local information only */
|
449
|
if ($search_rc != 0 || !$did_search) {
|
450
|
$search_items = $info_items;
|
451
|
} else {
|
452
|
foreach ($info_items as $pkg_info) {
|
453
|
if (empty($pkg_info['name'])) {
|
454
|
continue;
|
455
|
}
|
456
|
|
457
|
if (array_search($pkg_info['name'], array_column(
|
458
|
$search_items, 'name')) === FALSE) {
|
459
|
$pkg_info['obsolete'] = true;
|
460
|
$search_items[] = $pkg_info;
|
461
|
}
|
462
|
}
|
463
|
}
|
464
|
|
465
|
$result = array();
|
466
|
foreach ($search_items as $pkg_info) {
|
467
|
if (empty($pkg_info['name'])) {
|
468
|
continue;
|
469
|
}
|
470
|
|
471
|
if (isset($pkg_filter) && !in_array($pkg_info['name'],
|
472
|
$pkg_filter)) {
|
473
|
continue;
|
474
|
}
|
475
|
|
476
|
$pkg_info['shortname'] = $pkg_info['name'];
|
477
|
pkg_remove_prefix($pkg_info['shortname']);
|
478
|
|
479
|
/* XXX: Add it to globals.inc? */
|
480
|
$pkg_info['changeloglink'] =
|
481
|
"https://github.com/pfsense/FreeBSD-ports/commits/devel/" .
|
482
|
$pkg_info['categories'][0] . '/' . $pkg_info['name'];
|
483
|
|
484
|
$pkg_is_installed = false;
|
485
|
|
486
|
if (is_pkg_installed($pkg_info['name'])) {
|
487
|
$rc = pkg_exec("query %R {$pkg_info['name']}", $out,
|
488
|
$err);
|
489
|
if (!$base_packages &&
|
490
|
rtrim($out) != $g['product_name']) {
|
491
|
continue;
|
492
|
}
|
493
|
|
494
|
$pkg_info['installed'] = true;
|
495
|
$pkg_is_installed = true;
|
496
|
|
497
|
$rc = pkg_exec("query %v {$pkg_info['name']}", $out,
|
498
|
$err);
|
499
|
|
500
|
if ($rc != 0) {
|
501
|
update_status("\n" . gettext("ERROR: Error " .
|
502
|
"trying to get package version. " .
|
503
|
"Aborting...") . "\n");
|
504
|
update_status($err);
|
505
|
$input_errors[] = gettext("ERROR: Error " .
|
506
|
"trying to get package version. " .
|
507
|
"Aborting...") . "\n";
|
508
|
$input_errors[] = $err;
|
509
|
return array();
|
510
|
}
|
511
|
|
512
|
$pkg_info['installed_version'] = str_replace("\n", "",
|
513
|
$out);
|
514
|
|
515
|
/*
|
516
|
* We used pkg info to collect pkg data so remote
|
517
|
* version is not available. Lets try to collect it
|
518
|
* using rquery if possible
|
519
|
*/
|
520
|
if ($search_rc != 0 || !$did_search) {
|
521
|
$rc = pkg_exec(
|
522
|
"rquery -U %v {$pkg_info['name']}", $out,
|
523
|
$err);
|
524
|
|
525
|
if ($rc == 0) {
|
526
|
$pkg_info['version'] =
|
527
|
str_replace("\n", "", $out);
|
528
|
}
|
529
|
}
|
530
|
|
531
|
} else if (is_package_installed($pkg_info['shortname'])) {
|
532
|
$pkg_info['broken'] = true;
|
533
|
$pkg_is_installed = true;
|
534
|
}
|
535
|
|
536
|
$pkg_info['desc'] = preg_replace('/\n+WWW:.*$/', '',
|
537
|
$pkg_info['desc']);
|
538
|
|
539
|
if (!$installed_pkgs_only || $pkg_is_installed) {
|
540
|
$result[] = $pkg_info;
|
541
|
}
|
542
|
unset($pkg_info);
|
543
|
}
|
544
|
|
545
|
/* Sort result alphabetically */
|
546
|
usort($result, function($a, $b) {
|
547
|
return(strcasecmp ($a['name'], $b['name']));
|
548
|
});
|
549
|
|
550
|
return $result;
|
551
|
}
|
552
|
|
553
|
/*
|
554
|
* If binary pkg is installed but post-install tasks were not
|
555
|
* executed yet, do it now.
|
556
|
* This scenario can happen when a pkg is pre-installed during
|
557
|
* build phase, and at this point, cannot find a running system
|
558
|
* to register itself in config.xml and also execute custom
|
559
|
* install functions
|
560
|
*/
|
561
|
function register_all_installed_packages() {
|
562
|
global $g, $config, $pkg_interface;
|
563
|
|
564
|
$pkg_info = get_pkg_info('all', true, true);
|
565
|
|
566
|
foreach ($pkg_info as $pkg) {
|
567
|
pkg_remove_prefix($pkg['name']);
|
568
|
|
569
|
if (is_package_installed($pkg['name'])) {
|
570
|
continue;
|
571
|
}
|
572
|
|
573
|
update_status(sprintf(gettext(
|
574
|
"Running last steps of %s installation.") . "\n",
|
575
|
$pkg['name']));
|
576
|
install_package_xml($pkg['name']);
|
577
|
}
|
578
|
}
|
579
|
|
580
|
/*
|
581
|
* resync_all_package_configs() Force packages to setup their configuration and rc.d files.
|
582
|
* This function may also print output to the terminal indicating progress.
|
583
|
*/
|
584
|
function resync_all_package_configs($show_message = false) {
|
585
|
global $config, $pkg_interface, $g;
|
586
|
|
587
|
log_error(gettext("Resyncing configuration for all packages."));
|
588
|
|
589
|
if (!isset($config['installedpackages']['package']) ||
|
590
|
!is_array($config['installedpackages']['package'])) {
|
591
|
return;
|
592
|
}
|
593
|
|
594
|
if ($show_message == true) {
|
595
|
echo "Syncing packages:";
|
596
|
}
|
597
|
|
598
|
|
599
|
foreach ($config['installedpackages']['package'] as $idx => $package) {
|
600
|
if (empty($package['name'])) {
|
601
|
continue;
|
602
|
}
|
603
|
if ($show_message == true) {
|
604
|
echo " " . $package['name'];
|
605
|
}
|
606
|
if (platform_booting() != true) {
|
607
|
stop_service(get_package_internal_name($package));
|
608
|
}
|
609
|
sync_package($package['name']);
|
610
|
update_status(gettext("Syncing packages...") . "\n");
|
611
|
}
|
612
|
|
613
|
if ($show_message == true) {
|
614
|
echo " done.\n";
|
615
|
}
|
616
|
}
|
617
|
|
618
|
function uninstall_package($package_name) {
|
619
|
global $config;
|
620
|
|
621
|
$internal_name = $package_name;
|
622
|
$id = get_package_id($package_name);
|
623
|
if ($id >= 0) {
|
624
|
$internal_name = get_package_internal_name($config['installedpackages']['package'][$id]);
|
625
|
stop_service($internal_name);
|
626
|
}
|
627
|
$pkg_name = $g['pkg_prefix'] . $internal_name;
|
628
|
|
629
|
if (is_pkg_installed($pkg_name)) {
|
630
|
update_status(gettext("Removing package...") . "\n");
|
631
|
pkg_delete($pkg_name);
|
632
|
} else {
|
633
|
delete_package_xml($package_name);
|
634
|
}
|
635
|
|
636
|
update_status(gettext("done.") . "\n");
|
637
|
}
|
638
|
|
639
|
function reinstall_package($package_name) {
|
640
|
global $config, $g;
|
641
|
|
642
|
$internal_name = $package_name;
|
643
|
$id = get_package_id($package_name);
|
644
|
if ($id >= 0) {
|
645
|
$internal_name = get_package_internal_name($config['installedpackages']['package'][$id]);
|
646
|
}
|
647
|
$pkg_name = $g['pkg_prefix'] . $internal_name;
|
648
|
pkg_install($pkg_name);
|
649
|
}
|
650
|
|
651
|
/* Run <custom_php_resync_config_command> */
|
652
|
function sync_package($package_name) {
|
653
|
global $config, $builder_package_install;
|
654
|
|
655
|
// If this code is being called by pfspkg_installer
|
656
|
// which the builder system uses then return (ignore).
|
657
|
if ($builder_package_install) {
|
658
|
return;
|
659
|
}
|
660
|
|
661
|
if (empty($config['installedpackages']['package'])) {
|
662
|
return;
|
663
|
}
|
664
|
|
665
|
if (($pkg_id = get_package_id($package_name)) == -1) {
|
666
|
return; // This package doesn't really exist - exit the function.
|
667
|
}
|
668
|
|
669
|
if (!is_array($config['installedpackages']['package'][$pkg_id])) {
|
670
|
return; // No package belongs to the pkg_id passed to this function.
|
671
|
}
|
672
|
|
673
|
$package = &$config['installedpackages']['package'][$pkg_id];
|
674
|
if (!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
|
675
|
log_error(sprintf(gettext("The %s package is missing its configuration file and must be reinstalled."), $package['name']));
|
676
|
delete_package_xml($package['name']);
|
677
|
return;
|
678
|
}
|
679
|
|
680
|
$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
|
681
|
if (isset($pkg_config['nosync'])) {
|
682
|
return;
|
683
|
}
|
684
|
|
685
|
/* Bring in package include files */
|
686
|
if (!empty($pkg_config['include_file'])) {
|
687
|
$include_file = $pkg_config['include_file'];
|
688
|
if (file_exists($include_file)) {
|
689
|
require_once($include_file);
|
690
|
} else {
|
691
|
log_error(sprintf(gettext('Reinstalling package %1$s because its include file(%2$s) is missing!'), $package['name'], $include_file));
|
692
|
uninstall_package($package['name']);
|
693
|
if (reinstall_package($package['name']) != 0) {
|
694
|
log_error(sprintf(gettext("Reinstalling package %s failed. Take appropriate measures!!!"), $package['name']));
|
695
|
return;
|
696
|
}
|
697
|
if (file_exists($include_file)) {
|
698
|
require_once($include_file);
|
699
|
} else {
|
700
|
return;
|
701
|
}
|
702
|
}
|
703
|
}
|
704
|
|
705
|
if (!empty($pkg_config['custom_php_global_functions'])) {
|
706
|
eval($pkg_config['custom_php_global_functions']);
|
707
|
}
|
708
|
if (!empty($pkg_config['custom_php_resync_config_command'])) {
|
709
|
eval($pkg_config['custom_php_resync_config_command']);
|
710
|
}
|
711
|
}
|
712
|
|
713
|
/* Read info.xml installed by package and return an array */
|
714
|
function read_package_config($package_name) {
|
715
|
global $g;
|
716
|
|
717
|
$pkg_info_xml = '/usr/local/share/' . $g['pkg_prefix'] . $package_name . '/info.xml';
|
718
|
|
719
|
if (!file_exists($pkg_info_xml)) {
|
720
|
return false;
|
721
|
}
|
722
|
|
723
|
$pkg_info = parse_xml_config_pkg($pkg_info_xml, 'pfsensepkgs');
|
724
|
|
725
|
if (empty($pkg_info)) {
|
726
|
return false;
|
727
|
}
|
728
|
|
729
|
/* it always returns an array with 1 item */
|
730
|
return $pkg_info['package'][0];
|
731
|
}
|
732
|
|
733
|
/* Read package configurationfile and return an array */
|
734
|
function read_package_configurationfile($package_name) {
|
735
|
global $config, $g;
|
736
|
|
737
|
$pkg_config = array();
|
738
|
$id = get_package_id($package_name);
|
739
|
|
740
|
if ($id < 0 || !isset($config['installedpackages']['package'][$id]['configurationfile'])) {
|
741
|
return $pkg_config;
|
742
|
}
|
743
|
|
744
|
$pkg_configurationfile = $config['installedpackages']['package'][$id]['configurationfile'];
|
745
|
|
746
|
if (empty($pkg_configurationfile) || !file_exists('/usr/local/pkg/' . $pkg_configurationfile)) {
|
747
|
return $pkg_config;
|
748
|
}
|
749
|
|
750
|
$pkg_config = parse_xml_config_pkg('/usr/local/pkg/' . $pkg_configurationfile, "packagegui");
|
751
|
|
752
|
return $pkg_config;
|
753
|
}
|
754
|
|
755
|
function get_after_install_info($package_name) {
|
756
|
$pkg_config = read_package_config($package_name);
|
757
|
|
758
|
if (isset($pkg_config['after_install_info'])) {
|
759
|
return $pkg_config['after_install_info'];
|
760
|
}
|
761
|
|
762
|
return '';
|
763
|
}
|
764
|
|
765
|
function eval_once($toeval) {
|
766
|
global $evaled;
|
767
|
if (!$evaled) {
|
768
|
$evaled = array();
|
769
|
}
|
770
|
$evalmd5 = md5($toeval);
|
771
|
if (!in_array($evalmd5, $evaled)) {
|
772
|
@eval($toeval);
|
773
|
$evaled[] = $evalmd5;
|
774
|
}
|
775
|
return;
|
776
|
}
|
777
|
|
778
|
function install_package_xml($package_name) {
|
779
|
global $g, $config, $pkg_interface;
|
780
|
|
781
|
if (($pkg_info = read_package_config($package_name)) == false) {
|
782
|
return false;
|
783
|
}
|
784
|
|
785
|
/* safe side. Write config below will send to ro again. */
|
786
|
|
787
|
pkg_debug(gettext("Beginning package installation.") . "\n");
|
788
|
log_error(sprintf(gettext('Beginning package installation for %s .'), $pkg_info['name']));
|
789
|
|
790
|
/* add package information to config.xml */
|
791
|
$pkgid = get_package_id($pkg_info['name']);
|
792
|
update_status(gettext("Saving updated package information...") . "\n");
|
793
|
if ($pkgid == -1) {
|
794
|
init_config_arr(array('installedpackages', 'package'));
|
795
|
$config['installedpackages']['package'][] = $pkg_info;
|
796
|
$changedesc = sprintf(gettext("Installed %s package."), $pkg_info['name']);
|
797
|
$to_output = gettext("done.") . "\n";
|
798
|
} else {
|
799
|
init_config_arr(array('installedpackages', 'package', $pkgid));
|
800
|
$config['installedpackages']['package'][$pkgid] = $pkg_info;
|
801
|
$changedesc = sprintf(gettext("Overwrote previous installation of %s."), $pkg_info['name']);
|
802
|
$to_output = gettext("overwrite!") . "\n";
|
803
|
}
|
804
|
write_config(sprintf(gettext("Intermediate config write during package install for %s."), $pkg_info['name']));
|
805
|
update_status($to_output);
|
806
|
|
807
|
if (($pkgid = get_package_id($package_name)) == -1) {
|
808
|
update_status(sprintf(gettext('The %1$s package is not installed.%2$sInstallation aborted.'), $package_name, "\n\n"));
|
809
|
|
810
|
uninstall_package($package_name);
|
811
|
write_config($changedesc);
|
812
|
log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
|
813
|
update_status(gettext("Failed to install package.") . "\n");
|
814
|
return false;
|
815
|
}
|
816
|
|
817
|
if (file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
|
818
|
update_status(gettext("Loading package configuration... "));
|
819
|
$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $pkg_info['configurationfile'], "packagegui");
|
820
|
update_status(gettext("done.") . "\n");
|
821
|
update_status(gettext("Configuring package components...") . "\n");
|
822
|
if (!empty($pkg_config['filter_rules_needed'])) {
|
823
|
$config['installedpackages']['package'][$pkgid]['filter_rule_function'] = $pkg_config['filter_rules_needed'];
|
824
|
}
|
825
|
/* modify system files */
|
826
|
|
827
|
/* if a require exists, include it. this will
|
828
|
* show us where an error exists in a package
|
829
|
* instead of making us blindly guess
|
830
|
*/
|
831
|
$missing_include = false;
|
832
|
if ($pkg_config['include_file'] <> "") {
|
833
|
update_status(gettext("Loading package instructions...") . "\n");
|
834
|
if (file_exists($pkg_config['include_file'])) {
|
835
|
pkg_debug("require_once('{$pkg_config['include_file']}')\n");
|
836
|
require_once($pkg_config['include_file']);
|
837
|
} else {
|
838
|
pkg_debug("Missing include {$pkg_config['include_file']}\n");
|
839
|
$missing_include = true;
|
840
|
update_status(sprintf(gettext("Include %s is missing!"), basename($pkg_config['include_file'])) . "\n");
|
841
|
|
842
|
uninstall_package($package_name);
|
843
|
write_config($changedesc);
|
844
|
log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
|
845
|
update_status(gettext("Failed to install package.") . "\n");
|
846
|
return false;
|
847
|
}
|
848
|
}
|
849
|
|
850
|
/* custom commands */
|
851
|
update_status(gettext("Custom commands...") . "\n");
|
852
|
if ($missing_include == false) {
|
853
|
if ($pkg_config['custom_php_global_functions'] <> "") {
|
854
|
update_status(gettext("Executing custom_php_global_functions()..."));
|
855
|
eval_once($pkg_config['custom_php_global_functions']);
|
856
|
update_status(gettext("done.") . "\n");
|
857
|
}
|
858
|
if ($pkg_config['custom_php_install_command']) {
|
859
|
update_status(gettext("Executing custom_php_install_command()..."));
|
860
|
eval_once($pkg_config['custom_php_install_command']);
|
861
|
update_status(gettext("done.") . "\n");
|
862
|
}
|
863
|
if ($pkg_config['custom_php_resync_config_command'] <> "") {
|
864
|
update_status(gettext("Executing custom_php_resync_config_command()..."));
|
865
|
eval_once($pkg_config['custom_php_resync_config_command']);
|
866
|
update_status(gettext("done.") . "\n");
|
867
|
}
|
868
|
}
|
869
|
/* sidebar items */
|
870
|
if (is_array($pkg_config['menu'])) {
|
871
|
update_status(gettext("Menu items... "));
|
872
|
foreach ($pkg_config['menu'] as $menu) {
|
873
|
if (is_array($config['installedpackages']['menu'])) {
|
874
|
foreach ($config['installedpackages']['menu'] as $amenu) {
|
875
|
if ($amenu['name'] == $menu['name']) {
|
876
|
continue 2;
|
877
|
}
|
878
|
}
|
879
|
} else {
|
880
|
$config['installedpackages']['menu'] = array();
|
881
|
}
|
882
|
$config['installedpackages']['menu'][] = $menu;
|
883
|
}
|
884
|
update_status(gettext("done.") . "\n");
|
885
|
}
|
886
|
/* services */
|
887
|
if (is_array($pkg_config['service'])) {
|
888
|
update_status(gettext("Services... "));
|
889
|
foreach ($pkg_config['service'] as $service) {
|
890
|
if (is_array($config['installedpackages']['service'])) {
|
891
|
foreach ($config['installedpackages']['service'] as $aservice) {
|
892
|
if ($aservice['name'] == $service['name']) {
|
893
|
continue 2;
|
894
|
}
|
895
|
}
|
896
|
} else {
|
897
|
$config['installedpackages']['service'] = array();
|
898
|
}
|
899
|
$config['installedpackages']['service'][] = $service;
|
900
|
}
|
901
|
update_status(gettext("done.") . "\n");
|
902
|
}
|
903
|
if (is_array($pkg_config['tabs'])) {
|
904
|
$config['installedpackages']['package'][$pkgid]['tabs'] = $pkg_config['tabs'];
|
905
|
}
|
906
|
/* plugins */
|
907
|
if (isset($pkg_config['include_file'])) {
|
908
|
$config['installedpackages']['package'][$pkgid]['include_file'] = $pkg_config['include_file'];
|
909
|
}
|
910
|
if (is_array($pkg_config['plugins']['item'])) {
|
911
|
$config['installedpackages']['package'][$pkgid]['plugins']['item'] = $pkg_config['plugins']['item'];
|
912
|
}
|
913
|
} else {
|
914
|
pkg_debug("Unable to find config file\n");
|
915
|
update_status(gettext("Loading package configuration... failed!") . "\n\n" . gettext("Installation aborted."));
|
916
|
pkg_debug(gettext("Unable to load package configuration. Installation aborted.") ."\n");
|
917
|
|
918
|
uninstall_package($package_name);
|
919
|
write_config($changedesc);
|
920
|
log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
|
921
|
update_status(gettext("Failed to install package.") . "\n");
|
922
|
return false;
|
923
|
}
|
924
|
|
925
|
update_status(gettext("Writing configuration... "));
|
926
|
write_config($changedesc);
|
927
|
log_error(sprintf(gettext("Successfully installed package: %s."), $pkg_info['name']));
|
928
|
update_status(gettext("done.") . "\n");
|
929
|
if ($pkg_info['after_install_info']) {
|
930
|
update_status($pkg_info['after_install_info']);
|
931
|
}
|
932
|
|
933
|
/* set up package logging streams */
|
934
|
if ($pkg_info['logging']) {
|
935
|
system_syslogd_start(true);
|
936
|
}
|
937
|
|
938
|
return true;
|
939
|
}
|
940
|
|
941
|
function delete_package_xml($package_name, $when = "post-deinstall") {
|
942
|
global $g, $config, $pkg_interface;
|
943
|
|
944
|
|
945
|
$pkgid = get_package_id($package_name);
|
946
|
if ($pkgid == -1) {
|
947
|
update_status(sprintf(gettext('The %1$s package is not installed.%2$sDeletion aborted.'), $package_name, "\n\n"));
|
948
|
ob_flush();
|
949
|
sleep(1);
|
950
|
return;
|
951
|
}
|
952
|
pkg_debug(sprintf(gettext("Removing %s package... "), $package_name));
|
953
|
update_status(sprintf(gettext("Removing %s components..."), $package_name) . "\n");
|
954
|
/* parse package configuration */
|
955
|
init_config_arr(array('installedpackages', 'package', $pkgid));
|
956
|
$packages = &$config['installedpackages']['package'];
|
957
|
$pkg_info = &$packages[$pkgid];
|
958
|
init_config_arr(array('installedpackages', 'menu'));
|
959
|
$menus = &$config['installedpackages']['menu'];
|
960
|
init_config_arr(array('installedpackages', 'service'));
|
961
|
$services = &$config['installedpackages']['service'];
|
962
|
if (file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
|
963
|
$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $packages[$pkgid]['configurationfile'], "packagegui");
|
964
|
/* remove menu items */
|
965
|
if (is_array($pkg_config['menu'])) {
|
966
|
update_status(gettext("Menu items... "));
|
967
|
if (is_array($pkg_config['menu']) && is_array($menus)) {
|
968
|
foreach ($pkg_config['menu'] as $menu) {
|
969
|
foreach ($menus as $key => $instmenu) {
|
970
|
if ($instmenu['name'] == $menu['name']) {
|
971
|
unset($menus[$key]);
|
972
|
break;
|
973
|
}
|
974
|
}
|
975
|
}
|
976
|
}
|
977
|
update_status(gettext("done.") . "\n");
|
978
|
}
|
979
|
/* remove services */
|
980
|
if (is_array($pkg_config['service'])) {
|
981
|
update_status(gettext("Services... "));
|
982
|
if (is_array($pkg_config['service']) && is_array($services)) {
|
983
|
foreach ($pkg_config['service'] as $service) {
|
984
|
foreach ($services as $key => $instservice) {
|
985
|
if ($instservice['name'] == $service['name']) {
|
986
|
if (platform_booting() != true) {
|
987
|
stop_service($service['name']);
|
988
|
}
|
989
|
if ($service['rcfile']) {
|
990
|
$prefix = RCFILEPREFIX;
|
991
|
if (!empty($service['prefix'])) {
|
992
|
$prefix = $service['prefix'];
|
993
|
}
|
994
|
if (file_exists("{$prefix}{$service['rcfile']}")) {
|
995
|
@unlink("{$prefix}{$service['rcfile']}");
|
996
|
}
|
997
|
}
|
998
|
unset($services[$key]);
|
999
|
}
|
1000
|
}
|
1001
|
}
|
1002
|
}
|
1003
|
update_status(gettext("done.") . "\n");
|
1004
|
}
|
1005
|
/*
|
1006
|
* XXX: Otherwise inclusion of config.inc again invalidates actions taken.
|
1007
|
* Same is done during installation.
|
1008
|
*/
|
1009
|
write_config(sprintf(gettext("Intermediate config write during package removal for %s."), $package_name));
|
1010
|
|
1011
|
/*
|
1012
|
* If a require exists, include it. this will
|
1013
|
* show us where an error exists in a package
|
1014
|
* instead of making us blindly guess
|
1015
|
*/
|
1016
|
$missing_include = false;
|
1017
|
if ($pkg_config['include_file'] <> "") {
|
1018
|
update_status(gettext("Loading package instructions...") . "\n");
|
1019
|
if (file_exists($pkg_config['include_file'])) {
|
1020
|
pkg_debug("require_once(\"{$pkg_config['include_file']}\")\n");
|
1021
|
require_once($pkg_config['include_file']);
|
1022
|
} else {
|
1023
|
pkg_debug("Missing include {$pkg_config['include_file']}\n");
|
1024
|
$missing_include = true;
|
1025
|
update_status(sprintf(gettext("Include file %s could not be found for inclusion."), basename($pkg_config['include_file'])) . "\n");
|
1026
|
}
|
1027
|
}
|
1028
|
/* ermal
|
1029
|
* NOTE: It is not possible to handle parse errors on eval.
|
1030
|
* So we prevent it from being run at all to not interrupt all the other code.
|
1031
|
*/
|
1032
|
if ($when == "deinstall" && $missing_include == false) {
|
1033
|
/* evaluate this package's global functions and pre deinstall commands */
|
1034
|
if ($pkg_config['custom_php_global_functions'] <> "") {
|
1035
|
eval_once($pkg_config['custom_php_global_functions']);
|
1036
|
}
|
1037
|
if ($pkg_config['custom_php_pre_deinstall_command'] <> "") {
|
1038
|
eval_once($pkg_config['custom_php_pre_deinstall_command']);
|
1039
|
}
|
1040
|
}
|
1041
|
/* deinstall commands */
|
1042
|
if ($when == "deinstall" && $pkg_config['custom_php_deinstall_command'] <> "") {
|
1043
|
update_status(gettext("Deinstall commands... "));
|
1044
|
if ($missing_include == false) {
|
1045
|
eval_once($pkg_config['custom_php_deinstall_command']);
|
1046
|
update_status(gettext("done.") . "\n");
|
1047
|
} else {
|
1048
|
update_status("\n". gettext("Not executing custom deinstall hook because an include is missing.") . "\n");
|
1049
|
}
|
1050
|
}
|
1051
|
}
|
1052
|
/* syslog */
|
1053
|
$need_syslog_restart = false;
|
1054
|
if (is_array($pkg_info['logging']) && $pkg_info['logging']['logfilename'] <> "") {
|
1055
|
update_status(gettext("Syslog entries... "));
|
1056
|
@unlink_if_exists("{$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
|
1057
|
update_status("done.\n");
|
1058
|
$need_syslog_restart = true;
|
1059
|
}
|
1060
|
|
1061
|
if ($when == "post-deinstall") {
|
1062
|
/* remove config.xml entries */
|
1063
|
update_status(gettext("Configuration... "));
|
1064
|
unset($config['installedpackages']['package'][$pkgid]);
|
1065
|
update_status(gettext("done.") . "\n");
|
1066
|
write_config(sprintf(gettext("Removed %s package."), $package_name));
|
1067
|
/* remove package entry from /etc/syslog.conf if needed */
|
1068
|
/* this must be done after removing the entries from config.xml */
|
1069
|
if ($need_syslog_restart) {
|
1070
|
system_syslogd_start(true);
|
1071
|
}
|
1072
|
}
|
1073
|
}
|
1074
|
|
1075
|
/*
|
1076
|
* Used during upgrade process or retore backup process, verify all
|
1077
|
* packages installed in config.xml and install pkg accordingly
|
1078
|
*/
|
1079
|
function package_reinstall_all() {
|
1080
|
global $g, $config, $pkg_interface;
|
1081
|
|
1082
|
if (!isset($config['installedpackages']['package']) ||
|
1083
|
!is_array($config['installedpackages']['package'])) {
|
1084
|
return true;
|
1085
|
}
|
1086
|
|
1087
|
/*
|
1088
|
* Configure default pkg repo for current version instead of
|
1089
|
* using it from backup, that could be older
|
1090
|
*/
|
1091
|
$default_repo = pkg_get_default_repo();
|
1092
|
$current_repo_path = "";
|
1093
|
if (!empty($config['system']['pkg_repo_conf_path'])) {
|
1094
|
$current_repo_path = $config['system']['pkg_repo_conf_path'];
|
1095
|
}
|
1096
|
|
1097
|
if ($current_repo_path != $default_repo['path']) {
|
1098
|
$config['system']['pkg_repo_conf_path'] = $default_repo['path'];
|
1099
|
write_config( "Configured default pkg repo after restore");
|
1100
|
pkg_switch_repo($default_repo['path']);
|
1101
|
}
|
1102
|
|
1103
|
/* wait for internet connection */
|
1104
|
log_error(gettext("Waiting for Internet connection to update pkg " .
|
1105
|
"metadata and finish package reinstallation"));
|
1106
|
$ntries = 3;
|
1107
|
while ($ntries > 0) {
|
1108
|
if (pkg_update(true)) {
|
1109
|
break;
|
1110
|
}
|
1111
|
sleep(1);
|
1112
|
$ntries--;
|
1113
|
}
|
1114
|
|
1115
|
if ($ntries == 0) {
|
1116
|
return false;
|
1117
|
}
|
1118
|
|
1119
|
$package_list = array();
|
1120
|
foreach ($config['installedpackages']['package'] as $package) {
|
1121
|
$package_list[] = get_package_internal_name($package);
|
1122
|
}
|
1123
|
|
1124
|
if (!empty($package_list)) {
|
1125
|
$pkg_info = get_pkg_info();
|
1126
|
}
|
1127
|
|
1128
|
foreach ($package_list as $package) {
|
1129
|
$found = false;
|
1130
|
foreach ($pkg_info as $pkg) {
|
1131
|
pkg_remove_prefix($pkg['name']);
|
1132
|
if ($pkg['name'] == $package) {
|
1133
|
pkg_install($g['pkg_prefix'] . $package, true);
|
1134
|
$found = true;
|
1135
|
break;
|
1136
|
}
|
1137
|
}
|
1138
|
|
1139
|
if (!$found) {
|
1140
|
if (!function_exists("file_notice")) {
|
1141
|
require_once("notices.inc");
|
1142
|
}
|
1143
|
|
1144
|
file_notice(gettext("Package reinstall"),
|
1145
|
sprintf(gettext("Package %s does not exist in " .
|
1146
|
"current %s version and it has been removed."),
|
1147
|
$package, $g['product_name']));
|
1148
|
uninstall_package($package);
|
1149
|
}
|
1150
|
}
|
1151
|
|
1152
|
/*
|
1153
|
* Verify remaining binary packages not present in current config
|
1154
|
* during backup restore and remove them
|
1155
|
*/
|
1156
|
$installed_packages = get_pkg_info('all', true, true);
|
1157
|
foreach ($installed_packages as $package) {
|
1158
|
$shortname = $package['name'];
|
1159
|
pkg_remove_prefix($shortname);
|
1160
|
if (get_package_id($shortname) != -1) {
|
1161
|
continue;
|
1162
|
}
|
1163
|
pkg_delete($package['name']);
|
1164
|
}
|
1165
|
|
1166
|
return true;
|
1167
|
}
|
1168
|
|
1169
|
function stop_packages() {
|
1170
|
require_once("config.inc");
|
1171
|
require_once("functions.inc");
|
1172
|
require_once("filter.inc");
|
1173
|
require_once("shaper.inc");
|
1174
|
require_once("captiveportal.inc");
|
1175
|
require_once("pkg-utils.inc");
|
1176
|
require_once("pfsense-utils.inc");
|
1177
|
require_once("service-utils.inc");
|
1178
|
|
1179
|
global $config, $g;
|
1180
|
|
1181
|
log_error(gettext("Stopping all packages."));
|
1182
|
|
1183
|
$rcfiles = glob(RCFILEPREFIX . "*.sh");
|
1184
|
if (!$rcfiles) {
|
1185
|
$rcfiles = array();
|
1186
|
} else {
|
1187
|
$rcfiles = array_flip($rcfiles);
|
1188
|
if (!$rcfiles) {
|
1189
|
$rcfiles = array();
|
1190
|
}
|
1191
|
}
|
1192
|
|
1193
|
if (isset($config['installedpackages']['package']) &&
|
1194
|
is_array($config['installedpackages']['package'])) {
|
1195
|
foreach ($config['installedpackages']['package'] as $package) {
|
1196
|
echo " Stopping package {$package['name']}...";
|
1197
|
$internal_name = get_package_internal_name($package);
|
1198
|
stop_service($internal_name);
|
1199
|
unset($rcfiles[RCFILEPREFIX . strtolower($internal_name) . ".sh"]);
|
1200
|
echo "done.\n";
|
1201
|
}
|
1202
|
}
|
1203
|
|
1204
|
foreach ($rcfiles as $rcfile => $number) {
|
1205
|
$shell = @popen("/bin/sh", "w");
|
1206
|
if ($shell) {
|
1207
|
echo " Stopping {$rcfile}...";
|
1208
|
if (!@fwrite($shell, "{$rcfile} stop >>/tmp/bootup_messages 2>&1")) {
|
1209
|
if ($shell) {
|
1210
|
pclose($shell);
|
1211
|
}
|
1212
|
$shell = @popen("/bin/sh", "w");
|
1213
|
}
|
1214
|
echo "done.\n";
|
1215
|
pclose($shell);
|
1216
|
}
|
1217
|
}
|
1218
|
}
|
1219
|
|
1220
|
/* Identify which meta package is installed */
|
1221
|
function get_meta_pkg_name() {
|
1222
|
global $g;
|
1223
|
|
1224
|
/* XXX: Use pkg annotation */
|
1225
|
if (is_pkg_installed($g['product_name'])) {
|
1226
|
return $g['product_name'];
|
1227
|
}
|
1228
|
foreach ($g['alternativemetaports'] as $suffix) {
|
1229
|
if (is_pkg_installed($g['product_name'] . '-' . $suffix)) {
|
1230
|
return $g['product_name'] . '-' . $suffix;
|
1231
|
}
|
1232
|
}
|
1233
|
return false;
|
1234
|
}
|
1235
|
|
1236
|
/* Identify which base package is installed */
|
1237
|
function get_base_pkg_name() {
|
1238
|
global $g;
|
1239
|
|
1240
|
/* XXX: Use pkg annotation */
|
1241
|
if (is_pkg_installed($g['product_name'] . '-base-' . $g['platform'])) {
|
1242
|
return $g['product_name'] . '-base-' . $g['platform'];
|
1243
|
} else if (is_pkg_installed($g['product_name'] . '-base')) {
|
1244
|
return $g['product_name'] . '-base';
|
1245
|
}
|
1246
|
return false;
|
1247
|
}
|
1248
|
|
1249
|
/* Verify if system needs upgrade (meta package or base) */
|
1250
|
function get_system_pkg_version($baseonly = false, $use_cache = true) {
|
1251
|
global $g;
|
1252
|
|
1253
|
$cache_file = $g['version_cache_file'];
|
1254
|
$rc_file = $cache_file . '.rc';
|
1255
|
|
1256
|
$rc = "";
|
1257
|
if ($use_cache && file_exists($rc_file) &&
|
1258
|
(time()-filemtime($rc_file) < $g['version_cache_refresh'])) {
|
1259
|
$rc = chop(@file_get_contents($rc_file));
|
1260
|
}
|
1261
|
|
1262
|
if ($rc == "2") {
|
1263
|
$output = @file_get_contents($cache_file);
|
1264
|
} else if ($rc != "0") {
|
1265
|
$output = exec(
|
1266
|
"/usr/local/sbin/{$g['product_name']}-upgrade -c", $_gc,
|
1267
|
$rc);
|
1268
|
|
1269
|
/* Update cache if it succeeded */
|
1270
|
if ($rc == 0 || $rc == 2) {
|
1271
|
@file_put_contents($cache_file, $output);
|
1272
|
@file_put_contents($rc_file, $rc);
|
1273
|
}
|
1274
|
}
|
1275
|
|
1276
|
/* pfSense-upgrade returns 2 when there is a new version */
|
1277
|
if ($rc == "2") {
|
1278
|
$new_version = explode(' ', $output)[0];
|
1279
|
}
|
1280
|
|
1281
|
$base_pkg = get_base_pkg_name();
|
1282
|
$meta_pkg = get_meta_pkg_name();
|
1283
|
|
1284
|
if (!$base_pkg || !$meta_pkg) {
|
1285
|
return false;
|
1286
|
}
|
1287
|
|
1288
|
$info = get_pkg_info($base_pkg, true, true);
|
1289
|
|
1290
|
$pkg_info = array();
|
1291
|
foreach ($info as $item) {
|
1292
|
if ($item['name'] == $base_pkg) {
|
1293
|
$pkg_info = $item;
|
1294
|
break;
|
1295
|
}
|
1296
|
}
|
1297
|
|
1298
|
if (empty($pkg_info) || (!$baseonly && ($pkg_info['version'] ==
|
1299
|
$pkg_info['installed_version']))) {
|
1300
|
$info = get_pkg_info($meta_pkg, true, true);
|
1301
|
|
1302
|
foreach ($info as $item) {
|
1303
|
if ($item['name'] == $meta_pkg) {
|
1304
|
$pkg_info = $item;
|
1305
|
break;
|
1306
|
}
|
1307
|
}
|
1308
|
}
|
1309
|
|
1310
|
if (empty($pkg_info)) {
|
1311
|
return false;
|
1312
|
}
|
1313
|
|
1314
|
$result = array(
|
1315
|
'version' => $new_version ?: $pkg_info['version'],
|
1316
|
'installed_version' => $pkg_info['installed_version']
|
1317
|
);
|
1318
|
|
1319
|
$result['pkg_version_compare'] = pkg_version_compare(
|
1320
|
$result['installed_version'], $result['version']);
|
1321
|
|
1322
|
return $result;
|
1323
|
}
|
1324
|
|
1325
|
/* List available repos */
|
1326
|
function pkg_list_repos() {
|
1327
|
global $g;
|
1328
|
|
1329
|
$path = "/usr/local/share/{$g['product_name']}/pkg/repos";
|
1330
|
|
1331
|
$default_descr = @file_get_contents($path . "/{$g['product_name']}-repo.descr");
|
1332
|
|
1333
|
$default = array(
|
1334
|
'name' => 'Default',
|
1335
|
'path' => $path . "/{$g['product_name']}-repo.conf",
|
1336
|
'descr' => $default_descr
|
1337
|
);
|
1338
|
|
1339
|
$result = array($default);
|
1340
|
|
1341
|
$conf_files = glob("{$path}/{$g['product_name']}-repo-*.conf");
|
1342
|
foreach ($conf_files as $conf_file) {
|
1343
|
$descr_file = preg_replace('/.conf$/', '.descr', $conf_file);
|
1344
|
if (file_exists($descr_file)) {
|
1345
|
$descr_content = file($descr_file);
|
1346
|
$descr = chop($descr_content[0]);
|
1347
|
} else {
|
1348
|
$descr = 'Unknown';
|
1349
|
}
|
1350
|
if (!preg_match('/-repo-(.*).conf/', $conf_file, $matches)) {
|
1351
|
continue;
|
1352
|
}
|
1353
|
$entry = array(
|
1354
|
'name' => ucfirst(strtolower($matches[1])),
|
1355
|
'path' => $conf_file,
|
1356
|
'descr' => $descr
|
1357
|
);
|
1358
|
if (file_exists($conf_file . ".default")) {
|
1359
|
$entry['default'] = true;
|
1360
|
}
|
1361
|
$result[] = $entry;
|
1362
|
}
|
1363
|
|
1364
|
return $result;
|
1365
|
}
|
1366
|
|
1367
|
function pkg_get_default_repo() {
|
1368
|
$repos = pkg_list_repos();
|
1369
|
|
1370
|
foreach ($repos as $repo) {
|
1371
|
if (isset($repo['default'])) {
|
1372
|
return $repo;
|
1373
|
}
|
1374
|
}
|
1375
|
|
1376
|
/* No default found, return the first one */
|
1377
|
return ($repos[0]);
|
1378
|
}
|
1379
|
|
1380
|
/* List available repos on a format to be used by selectors */
|
1381
|
function pkg_build_repo_list() {
|
1382
|
$repos = pkg_list_repos();
|
1383
|
$list = array();
|
1384
|
|
1385
|
foreach ($repos as $repo) {
|
1386
|
$list[$repo['name']] = $repo['descr'];
|
1387
|
}
|
1388
|
|
1389
|
return($list);
|
1390
|
}
|
1391
|
|
1392
|
/* Find repo by path */
|
1393
|
function pkg_get_repo_name($path) {
|
1394
|
$repos = pkg_list_repos();
|
1395
|
|
1396
|
$default = $repos[0]['name'];
|
1397
|
foreach ($repos as $repo) {
|
1398
|
if ($repo['path'] == $path) {
|
1399
|
return $repo['name'];
|
1400
|
}
|
1401
|
if (isset($repo['default'])) {
|
1402
|
$default = $repo['name'];
|
1403
|
}
|
1404
|
}
|
1405
|
|
1406
|
/* Default */
|
1407
|
return $default;
|
1408
|
}
|
1409
|
|
1410
|
/* Setup pkg.conf according current repo */
|
1411
|
function pkg_conf_setup() {
|
1412
|
global $g;
|
1413
|
|
1414
|
$pkg_conf_path = "/usr/local/etc/pkg.conf";
|
1415
|
$conf = "/usr/local/etc/pkg/repos/{$g['product_name']}.conf";
|
1416
|
if (!file_exists($conf)) {
|
1417
|
return;
|
1418
|
}
|
1419
|
|
1420
|
$real_conf = readlink($conf);
|
1421
|
|
1422
|
if (!$real_conf) {
|
1423
|
return;
|
1424
|
}
|
1425
|
|
1426
|
$abi_file = str_replace('.conf', '.abi', $real_conf);
|
1427
|
$altabi_file = str_replace('.conf', '.altabi', $real_conf);
|
1428
|
|
1429
|
$pkg_conf = array();
|
1430
|
if (file_exists($abi_file) && file_exists($altabi_file)) {
|
1431
|
$abi = file_get_contents($abi_file);
|
1432
|
$altabi = file_get_contents($altabi_file);
|
1433
|
|
1434
|
$pkg_conf = array(
|
1435
|
"ABI={$abi}",
|
1436
|
"ALTABI={$altabi}"
|
1437
|
);
|
1438
|
}
|
1439
|
|
1440
|
file_put_contents($pkg_conf_path, $pkg_conf);
|
1441
|
}
|
1442
|
|
1443
|
/* Switch between stable and devel repos */
|
1444
|
function pkg_switch_repo($path) {
|
1445
|
global $g;
|
1446
|
|
1447
|
safe_mkdir("/usr/local/etc/pkg/repos");
|
1448
|
@unlink("/usr/local/etc/pkg/repos/{$g['product_name']}.conf");
|
1449
|
@symlink($path, "/usr/local/etc/pkg/repos/{$g['product_name']}.conf");
|
1450
|
|
1451
|
pkg_conf_setup();
|
1452
|
|
1453
|
/* Update pfSense_version cache */
|
1454
|
mwexec_bg("/etc/rc.update_pkg_metadata now");
|
1455
|
return;
|
1456
|
}
|
1457
|
|
1458
|
$FQDN = "https://ews.netgate.com/pfupdate";
|
1459
|
$refreshinterval = (24 * 3600); // 24 hours
|
1460
|
$idfile = "/var/db/uniqueid";
|
1461
|
$repopath = "/usr/local/share/{$g['product_name']}/pkg/repos";
|
1462
|
$configflename = "{$repopath}/{$g['product_name']}-repo-custom.conf";
|
1463
|
|
1464
|
// Update the list of available repositories from the server. This will allow migration to another
|
1465
|
// update repository should the existing one becomes unavailable
|
1466
|
function update_repos() {
|
1467
|
global $g, $config, $idfile, $FQDN, $repopath;
|
1468
|
|
1469
|
if (file_exists($idfile)) {
|
1470
|
// If the custom repository definition does not exist, or is more than 24 hours old
|
1471
|
// Fetch a copy from the server
|
1472
|
if ( (! file_exists($configflename)) || ( time()-filemtime($configflename) > $refreshinterval)) {
|
1473
|
if (function_exists('curl_version')) {
|
1474
|
// Gather some information about the system so the proper repo can be returned
|
1475
|
$nid = file_get_contents($idfile);
|
1476
|
$serial = system_get_serial();
|
1477
|
|
1478
|
$base_pkg = get_base_pkg_name();
|
1479
|
|
1480
|
$info = get_pkg_info($base_pkg, true, true);
|
1481
|
|
1482
|
$pkg_info = array();
|
1483
|
foreach ($info as $item) {
|
1484
|
if ($item['name'] == $base_pkg) {
|
1485
|
$pkg_info = $item;
|
1486
|
break;
|
1487
|
}
|
1488
|
}
|
1489
|
|
1490
|
// Compose a version string in JSON format
|
1491
|
$os = php_uname('s');
|
1492
|
$osver = php_uname('r');
|
1493
|
$v = strtolower(php_uname('v'));
|
1494
|
$ed = (strpos($v, 'factory') !== false) ? "Factory":"Community";
|
1495
|
$version = "{\"os\":\"" . $os . "\",\"osver\":\"" . $osver . "\",\"prod\":\"pfSense\"," . "\"ver\":\"" . $info[0]['version'] . "\",\"ed\":\"" . $ed . "\"}";
|
1496
|
|
1497
|
$arch = php_uname('m');
|
1498
|
$locale = $config['system']['language'];
|
1499
|
|
1500
|
$post = ['uid' => $nid,
|
1501
|
'language' => $locale,
|
1502
|
'serial' => $serial,
|
1503
|
'version' => $version,
|
1504
|
'arch' => $arch
|
1505
|
];
|
1506
|
|
1507
|
$ch = curl_init();
|
1508
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
1509
|
curl_setopt($ch, CURLOPT_VERBOSE, 0);
|
1510
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
1511
|
curl_setopt($ch, CURLOPT_USERAGENT, $g['product_name'] . '/' . $g['product_version']);
|
1512
|
curl_setopt($ch, CURLOPT_URL, $FQDN);
|
1513
|
curl_setopt($ch, CURLOPT_POST, true);
|
1514
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
|
1515
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,4);
|
1516
|
$response = curl_exec($ch);
|
1517
|
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
1518
|
curl_close($ch);
|
1519
|
|
1520
|
if ($status == 200) {
|
1521
|
save_repo($response);
|
1522
|
}
|
1523
|
}
|
1524
|
}
|
1525
|
}
|
1526
|
}
|
1527
|
|
1528
|
// Parse the received JSON data and save the custom repository information
|
1529
|
function save_repo($json) {
|
1530
|
global $repopath, $g;
|
1531
|
$repo = json_decode($json, true);
|
1532
|
|
1533
|
if (($repo != NULL) && isset($repo['abi']) && isset($repo['altabi']) && isset($repo['conf']) &&
|
1534
|
isset($repo['descr']) && isset($repo['name']) && (strlen($repo['conf']) > 10)) {
|
1535
|
$basename = "{$repopath}/{$g['product_name']}-repo-custom.";
|
1536
|
|
1537
|
file_put_contents($basename . "conf", base64_decode($repo['conf']));
|
1538
|
file_put_contents($basename . "descr", $repo['descr']);
|
1539
|
file_put_contents($basename . "abi", $repo['abi']);
|
1540
|
file_put_contents($basename . "altabi", $repo['altabi']);
|
1541
|
file_put_contents($basename . "name", $repo['name']);
|
1542
|
file_put_contents($basename . "help", $repo['help']);
|
1543
|
} else {
|
1544
|
// If there was anything wrong with the custom repository definition, remove the help text
|
1545
|
// to avoid possible confusion
|
1546
|
if (file_exists($basename . "help")) {
|
1547
|
unlink($basename . "help");
|
1548
|
}
|
1549
|
}
|
1550
|
}
|
1551
|
|
1552
|
?>
|