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