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