1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
|
<?php
/****h* pfSense/pkg-utils
NAME
pkg-utils.inc - Package subsystem
DESCRIPTION
This file contains various functions used by the pfSense package system.
HISTORY
$Id$
*/
/* ====================================================================
* Copyright (c) 2004-2015 Electric Sheep Fencing, LLC. All rights reserved.
* Copyright (c) 2004, 2005 Scott Ullrich
* Copyright (c) 2005-2006 Colin Smith (ethethlay@gmail.com)
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgment:
* "This product includes software developed by the pfSense Project
* for use in the pfSense software distribution. (http://www.pfsense.org/).
*
* 4. The names "pfSense" and "pfSense Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* coreteam@pfsense.org.
*
* 5. Products derived from this software may not be called "pfSense"
* nor may "pfSense" appear in their names without prior written
* permission of the Electric Sheep Fencing, LLC.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
*
* "This product includes software developed by the pfSense Project
* for use in the pfSense software distribution (http://www.pfsense.org/).
*
* THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ====================================================================
*
*/
/*
pfSense_BUILDER_BINARIES: /usr/bin/cd /usr/bin/tar /usr/sbin/fifolog_create /bin/chmod
pfSense_BUILDER_BINARIES: /usr/sbin/pkg_add /usr/sbin/pkg_info /usr/sbin/pkg_delete /bin/rm
pfSense_MODULE: pkg
*/
require_once("globals.inc");
require_once("service-utils.inc");
if (file_exists("/cf/conf/use_xmlreader")) {
require_once("xmlreader.inc");
} else {
require_once("xmlparse.inc");
}
require_once("pfsense-utils.inc");
if (!function_exists("pkg_debug")) {
/* set up logging if needed */
function pkg_debug($msg) {
global $g, $debug, $fd_log;
if (!$debug) {
return;
}
if (!$fd_log) {
if (!$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_debug.log", "w")) {
update_output_window("Warning, could not open log for writing.");
}
}
@fwrite($fd_log, $msg);
}
}
global $g;
if (!isset($g['platform'])) {
$g['platform'] = trim(file_get_contents("/etc/platform"));
}
/* Remove pkg_prefix from package name if it's present */
function pkg_remove_prefix(&$pkg_name) {
global $g;
if (substr($pkg_name, 0, strlen($g['pkg_prefix'])) == $g['pkg_prefix']) {
$pkg_name = substr($pkg_name, strlen($g['pkg_prefix']));
}
}
/* Execute pkg update when it's necessary */
function pkg_update($force = false) {
global $g;
$now = strftime('%s');
if (!$force) {
$last_update_file="{$g['varrun_path']}/{$g['product_name']}-upgrade-last-update";
if (file_exists($last_update_file)) {
$last_update = rtrim(file_get_contents($last_update_file), "\n");
if (!is_numericint($last_update)) {
$last_update = 0;
}
}
if ($last_update > 0) {
if ($now > $last_update && ($now - $last_update) <= (60 * 60)) {
return true;
}
}
}
$rc = pkg_call("update");
if ($rc) {
file_put_contents($last_update_file, $now . "\n");
}
return $rc;
}
/* Execute a pkg call */
function pkg_call($params, $mute = false) {
global $static_output, $g, $config;
if (empty($params)) {
return false;
}
$user_agent = $g['product_name'] . '/' . $g['product_version'];
if (!isset($config['system']['host_uuid'])) {
$user_agent .= ' : ' . get_single_sysctl('kern.hostuuid');
}
$env = array(
"HTTP_USER_AGENT" => $user_agent,
"ASSUME_ALWAYS_YES" => "true",
"REPO_AUTOUPDATE" => "false"
);
$descriptorspec = array(
1 => array("pipe", "w"), /* stdout */
2 => array("pipe", "w") /* stderr */
);
pkg_debug("pkg_call(): {$params}\n");
$process = proc_open("/usr/sbin/pkg {$params}", $descriptorspec, $pipes, '/', $env);
if (!is_resource($process)) {
return false;
}
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
/* XXX: should be a tunnable? */
$timeout = 300; // seconds
$error_log = '';
$started = time();
$maxwaittime = 10; // Number of seconds to wait for a response fromteh pacakge we are calling
do {
$write = array();
$read = array($pipes[1], $pipes[2]);
$except = array();
$stream = stream_select($read, $write, $except, null, $timeout);
if ($stream !== FALSE && $stream > 0) {
foreach ($read as $pipe) {
$content = stream_get_contents($pipe);
if ($content == '') {
continue;
}
if ($pipe === $pipes[1]) {
if (!$mute) {
$static_output .= $content;
update_output_window($static_output);
}
flush();
} else if ($pipe === $pipes[2]) {
$error_log .= $content;
}
}
}
$status = proc_get_status($process);
$now = time();
if(($now - $started) >= $maxwaittime) {
$rc = -1;
proc_terminate($process);
break;
}
} while ($status['running']);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
if(!isset($rc)) {
$rc = $status['exitcode'];
}
pkg_debug("pkg_call(): rc = {$rc}\n");
if ($rc == 0) {
return true;
}
pkg_debug("pkg_call(): error_log\n{$error_log}\n");
if (!$mute) {
$static_output .= "\n\n" . sprintf(gettext("ERROR!!! An error occurred on pkg execution (rc = %d) with parameters '%s':"), $rc, $params) . "\n" . $error_log;
update_output_window($static_output);
}
return false;
}
/* Execute pkg with $params, fill stdout and stderr and return pkg rc */
function pkg_exec($params, &$stdout, &$stderr) {
global $g, $config;
if (empty($params)) {
return -1;
}
$user_agent = $g['product_name'] . '/' . $g['product_version'];
if (!isset($config['system']['host_uuid'])) {
$user_agent .= ' : ' . get_single_sysctl('kern.hostuuid');
}
$env = array(
"HTTP_USER_AGENT" => $user_agent,
"ASSUME_ALWAYS_YES" => "true",
"REPO_AUTOUPDATE" => "false"
);
$descriptorspec = array(
1 => array("pipe", "w"), /* stdout */
2 => array("pipe", "w") /* stderr */
);
pkg_debug("pkg_exec(): {$params}\n");
$process = proc_open("/usr/sbin/pkg {$params}", $descriptorspec, $pipes, '/', $env);
if (!is_resource($process)) {
return -1;
}
$stdout = '';
while (($l = fgets($pipes[1])) !== FALSE) {
$stdout .= $l;
}
fclose($pipes[1]);
$stderr = '';
while (($l = fgets($pipes[2])) !== FALSE) {
$stderr .= $l;
}
fclose($pipes[2]);
return proc_close($process);
}
/* Compare 2 pkg versions and return:
* '=' - versions are the same
* '>' - $v1 > $v2
* '<' - $v1 < $v2
* '?' - Error
*/
function pkg_version_compare($v1, $v2) {
if (empty($v1) || empty($v2)) {
return '?';
}
$rc = pkg_exec("version -t '{$v1}' '{$v2}'", $stdout, $stderr);
if ($rc != 0) {
return '?';
}
return str_replace("\n", "", $stdout);
}
/* Check if package is installed */
function is_pkg_installed($pkg_name) {
global $g;
pkg_remove_prefix($pkg_name);
return pkg_call("info -e " . $g['pkg_prefix'] . $pkg_name, true);
}
/* Install package, $pkg_name should not contain prefix */
function pkg_install($pkg_name) {
global $g;
$result = false;
pkg_remove_prefix($pkg_name);
pkg_debug("Installing package {$pkg_name}\n");
if (!is_pkg_installed($pkg_name)) {
$result = pkg_call("install -y " . $g['pkg_prefix'] . $pkg_name);
/* Cleanup cacke to free disk space */
pkg_call("clean -y");
}
return $result;
}
/* Delete package from FreeBSD, $pkg_name should not contain prefix */
function pkg_delete($pkg_name) {
global $g;
pkg_remove_prefix($pkg_name);
pkg_debug("Removing package {$pkg_name}\n");
if (is_pkg_installed($pkg_name)) {
pkg_call("delete -y " . $g['pkg_prefix'] . $pkg_name);
/* Cleanup unecessary dependencies */
pkg_call("autoremove -y");
}
}
/* Check if package is present in config.xml */
function is_package_installed($package_name) {
return (get_package_id($package_name) != -1);
}
/* Find package array index */
function get_package_id($package_name) {
global $config;
if (!is_array($config['installedpackages']['package'])) {
return -1;
}
foreach ($config['installedpackages']['package'] as $idx => $pkg) {
if ($pkg['name'] == $package_name ||
get_package_internal_name($pkg) == $package_name) {
return $idx;
}
}
return -1;
}
/* Keep backward compatibility since snort/suricata use this function */
function get_pkg_id($package_name) {
return get_package_id($package_name);
}
/* Return internal_name when it's defined, otherwise, returns name */
function get_package_internal_name($package_data) {
if (isset($package_data['internal_name']) && ($package_data['internal_name'] != "")) {
/* e.g. name is Ipguard-dev, internal name is ipguard */
return $package_data['internal_name'];
} else {
return $package_data['name'];
}
}
// Get information about packages.
function get_pkg_info($pkgs = 'all', $info = 'all') {
global $g, $static_output, $input_errors;
$out = '';
$err = '';
unset($pkg_filter);
if (is_array($pkgs)) {
$pkg_filter = $pkgs;
$pkgs = 'all';
}
if ($pkgs == 'all') {
$pkgs = $g['pkg_prefix'];
}
/* Make sure repo metadata is up2date */
$static_output .= "\n" . gettext("Updating package repository metadada...") . "\n";
update_status($static_output);
if (!pkg_update()) {
$input_errors[] = gettext("ERROR: An error occurred when updating packages repository. Aborting...") . "\n";
$static_output .= "\n" . gettext("ERROR: An error occurred when updating packages repository. Aborting...") . "\n";
update_status($static_output);
return array();
}
$rc = pkg_exec("search -U --raw-format json-compact " . $pkgs, $out, $err);
if ($rc != 0) {
$static_output .= "\n" . gettext("ERROR: Error trying to get packages list. Aborting...") . "\n";
$static_output .= $err;
$input_errors[] = gettext("ERROR: Error trying to get packages list. Aborting...") . "\n";
$input_errors[] = $err;
update_status($static_output);
return array();
}
$result = array();
$pkgs_info = explode("\n", $out);
foreach ($pkgs_info as $pkg_info_json) {
$pkg_info = json_decode($pkg_info_json, true);
if (!isset($pkg_info['name'])) {
continue;
}
if (isset($pkg_filter) && !in_array($pkg_info['name'], $pkg_filter)) {
continue;
}
if (is_pkg_installed($pkg_info['name'])) {
$pkg_info['installed'] = true;
}
$pkg_info['desc'] = preg_replace('/\n+WWW:.*$/', '', $pkg_info['desc']);
$result[] = $pkg_info;
unset($pkg_info);
}
return $result;
}
/*
* resync_all_package_configs() Force packages to setup their configuration and rc.d files.
* This function may also print output to the terminal indicating progress.
*/
function resync_all_package_configs($show_message = false) {
global $config, $pkg_interface, $g;
log_error(gettext("Resyncing configuration for all packages."));
if (!is_array($config['installedpackages']['package'])) {
return;
}
if ($show_message == true) {
echo "Syncing packages:";
}
conf_mount_rw();
foreach ($config['installedpackages']['package'] as $idx => $package) {
if (empty($package['name'])) {
continue;
}
if ($show_message == true) {
echo " " . $package['name'];
}
if (platform_booting() != true) {
stop_service(get_package_internal_name($package));
}
sync_package($package['name']);
if ($pkg_interface == "console") {
echo "\n" . gettext("Syncing packages:");
}
}
if ($show_message == true) {
echo " done.\n";
}
@unlink("/conf/needs_package_sync");
conf_mount_ro();
}
function uninstall_package($package_name) {
global $config, $static_output;
$internal_name = $package_name;
$id = get_package_id($package_name);
if ($id >= 0) {
$internal_name = get_package_internal_name($config['installedpackages']['package'][$id]);
stop_service($internal_name);
}
if (is_pkg_installed($internal_name)) {
$static_output .= "Removing package...\n";
update_output_window($static_output);
pkg_delete($internal_name);
} else {
delete_package_xml($package_name);
}
$static_output .= gettext("done.") . "\n";
update_output_window($static_output);
}
/* Run <custom_php_resync_config_command> */
function sync_package($package_name) {
global $config, $builder_package_install;
// If this code is being called by pfspkg_installer
// which the builder system uses then return (ignore).
if ($builder_package_install) {
return;
}
if (empty($config['installedpackages']['package'])) {
return;
}
if (($pkg_id = get_package_id($package_name)) == -1) {
return; // This package doesn't really exist - exit the function.
}
if (!is_array($config['installedpackages']['package'][$pkg_id])) {
return; // No package belongs to the pkg_id passed to this function.
}
$package =& $config['installedpackages']['package'][$pkg_id];
if (!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
log_error(sprintf(gettext("The %s package is missing its configuration file and must be reinstalled."), $package['name']));
delete_package_xml($package['name']);
return;
}
$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
if (isset($pkg_config['nosync'])) {
return;
}
/* Bring in package include files */
if (!empty($pkg_config['include_file'])) {
$include_file = $pkg_config['include_file'];
if (file_exists($include_file)) {
require_once($include_file);
} else {
log_error("Reinstalling package {$package['name']} because its include file({$include_file}) is missing!");
uninstall_package($package['name']);
if (install_package($package['name']) != 0) {
log_error("Reinstalling package {$package['name']} failed. Take appropriate measures!!!");
return;
}
if (file_exists($include_file)) {
require_once($include_file);
} else {
return;
}
}
}
if (!empty($pkg_config['custom_php_global_functions'])) {
eval($pkg_config['custom_php_global_functions']);
}
if (!empty($pkg_config['custom_php_resync_config_command'])) {
eval($pkg_config['custom_php_resync_config_command']);
}
}
/* Read info.xml installed by package and return an array */
function read_package_config($package_name) {
global $g;
$pkg_info_xml = '/usr/local/share/' . $g['pkg_prefix'] . $package_name . '/info.xml';
if (!file_exists($pkg_info_xml)) {
return false;
}
$pkg_info = parse_xml_config_pkg($pkg_info_xml, 'pfsensepkgs');
if (empty($pkg_info)) {
return false;
}
/* it always returns an array with 1 item */
return $pkg_info['package'][0];
}
function get_after_install_info($package_name) {
$pkg_config = read_package_config($package_name);
if (isset($pkg_config['after_install_info'])) {
return $pkg_config['after_install_info'];
}
return '';
}
function eval_once($toeval) {
global $evaled;
if (!$evaled) {
$evaled = array();
}
$evalmd5 = md5($toeval);
if (!in_array($evalmd5, $evaled)) {
@eval($toeval);
$evaled[] = $evalmd5;
}
return;
}
function install_package($package_name) {
global $g, $config, $static_output, $pkg_interface;
if ($pkg_interface == "console") {
echo "\n";
}
return pkg_install($package_name);
}
function install_package_xml($package_name) {
global $g, $config, $static_output, $pkg_interface;
if (($pkg_info = read_package_config($package_name)) == false) {
return false;
}
/* safe side. Write config below will send to ro again. */
conf_mount_rw();
pkg_debug(gettext("Beginning package installation.") . "\n");
log_error(sprintf(gettext('Beginning package installation for %s .'), $pkg_info['name']));
/* add package information to config.xml */
$pkgid = get_package_id($pkg_info['name']);
$static_output .= gettext("Saving updated package information...") . " ";
update_output_window($static_output);
if ($pkgid == -1) {
$config['installedpackages']['package'][] = $pkg_info;
$changedesc = sprintf(gettext("Installed %s package."), $pkg_info['name']);
$to_output = gettext("done.") . "\n";
} else {
$config['installedpackages']['package'][$pkgid] = $pkg_info;
$changedesc = sprintf(gettext("Overwrote previous installation of %s."), $pkg_info['name']);
$to_output = gettext("overwrite!") . "\n";
}
unlink_if_exists('/conf/needs_package_sync');
write_config("Intermediate config write during package install for {$pkg_info['name']}.");
conf_mount_ro();
$static_output .= $to_output;
update_output_window($static_output);
if (($pkgid = get_package_id($package_name)) == -1) {
$static_output .= sprintf(gettext("The %s package is not installed.%sInstallation aborted."), $package_name, "\n\n");
update_output_window($static_output);
if ($pkg_interface <> "console") {
echo "\n<script>document.getElementById('progressbar').style.visibility='hidden';</script>";
echo "\n<script>document.getElementById('progholder').style.visibility='hidden';</script>";
}
uninstall_package($package_name);
write_config($changedesc);
log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
$static_output .= gettext("Failed to install package.") . "\n";
update_output_window($static_output);
return false;
}
$configfile = substr(strrchr($pkg_info['config_file'], '/'), 1);
if (file_exists("/usr/local/pkg/" . $configfile)) {
$static_output .= gettext("Loading package configuration... ");
update_output_window($static_output);
$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $configfile, "packagegui");
$static_output .= gettext("done.") . "\n";
update_output_window($static_output);
$static_output .= gettext("Configuring package components...\n");
if (!empty($pkg_config['filter_rules_needed'])) {
$config['installedpackages']['package'][$pkgid]['filter_rule_function'] = $pkg_config['filter_rules_needed'];
}
update_output_window($static_output);
/* modify system files */
/* if a require exists, include it. this will
* show us where an error exists in a package
* instead of making us blindly guess
*/
$missing_include = false;
if ($pkg_config['include_file'] <> "") {
$static_output .= gettext("Loading package instructions...") . "\n";
update_output_window($static_output);
if (file_exists($pkg_config['include_file'])) {
pkg_debug("require_once('{$pkg_config['include_file']}')\n");
require_once($pkg_config['include_file']);
} else {
pkg_debug("Missing include {$pkg_config['include_file']}\n");
$missing_include = true;
$static_output .= "Include " . basename($pkg_config['include_file']) . " is missing!\n";
update_output_window($static_output);
uninstall_package($package_name);
write_config($changedesc);
log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
$static_output .= gettext("Failed to install package.") . "\n";
update_output_window($static_output);
return false;
}
}
/* custom commands */
$static_output .= gettext("Custom commands...") . "\n";
update_output_window($static_output);
if ($missing_include == false) {
if ($pkg_config['custom_php_global_functions'] <> "") {
$static_output .= gettext("Executing custom_php_global_functions()...");
update_output_window($static_output);
eval_once($pkg_config['custom_php_global_functions']);
$static_output .= gettext("done.") . "\n";
update_output_window($static_output);
}
if ($pkg_config['custom_php_install_command']) {
$static_output .= gettext("Executing custom_php_install_command()...");
update_output_window($static_output);
eval_once($pkg_config['custom_php_install_command']);
$static_output .= gettext("done.") . "\n";
update_output_window($static_output);
}
if ($pkg_config['custom_php_resync_config_command'] <> "") {
$static_output .= gettext("Executing custom_php_resync_config_command()...");
update_output_window($static_output);
eval_once($pkg_config['custom_php_resync_config_command']);
$static_output .= gettext("done.") . "\n";
update_output_window($static_output);
}
}
/* sidebar items */
if (is_array($pkg_config['menu'])) {
$static_output .= gettext("Menu items... ");
update_output_window($static_output);
foreach ($pkg_config['menu'] as $menu) {
if (is_array($config['installedpackages']['menu'])) {
foreach ($config['installedpackages']['menu'] as $amenu) {
if ($amenu['name'] == $menu['name']) {
continue 2;
}
}
} else {
$config['installedpackages']['menu'] = array();
}
$config['installedpackages']['menu'][] = $menu;
}
$static_output .= gettext("done.") . "\n";
update_output_window($static_output);
}
/* services */
if (is_array($pkg_config['service'])) {
$static_output .= gettext("Services... ");
update_output_window($static_output);
foreach ($pkg_config['service'] as $service) {
if (is_array($config['installedpackages']['service'])) {
foreach ($config['installedpackages']['service'] as $aservice) {
if ($aservice['name'] == $service['name']) {
continue 2;
}
}
} else {
$config['installedpackages']['service'] = array();
}
$config['installedpackages']['service'][] = $service;
}
$static_output .= gettext("done.") . "\n";
update_output_window($static_output);
}
} else {
pkg_debug("Unable to find config file\n");
$static_output .= gettext("Loading package configuration... failed!") . "\n\n" . gettext("Installation aborted.");
update_output_window($static_output);
pkg_debug(gettext("Unable to load package configuration. Installation aborted.") ."\n");
if ($pkg_interface <> "console") {
echo "\n<script>document.getElementById('progressbar').style.visibility='hidden';</script>";
echo "\n<script>document.getElementById('progholder').style.visibility='hidden';</script>";
}
uninstall_package($package_name);
write_config($changedesc);
log_error(sprintf(gettext("Failed to install package: %s."), $pkg_info['name']));
$static_output .= gettext("Failed to install package.") . "\n";
update_output_window($static_output);
return false;
}
/* set up package logging streams */
if ($pkg_info['logging']) {
system_syslogd_start();
}
$static_output .= gettext("Writing configuration... ");
update_output_window($static_output);
write_config($changedesc);
log_error(sprintf(gettext("Successfully installed package: %s."), $pkg_info['name']));
$static_output .= gettext("done.") . "\n";
update_output_window($static_output);
if ($pkg_info['after_install_info']) {
update_output_window($pkg_info['after_install_info']);
}
return true;
}
function delete_package($package_name) {
global $config, $g, $static_output;
if (!is_package_installed($package_name)) {
return;
}
$static_output .= sprintf(gettext("Starting package deletion for %s..."), $package_name);
update_output_window($static_output);
pkg_delete($package_name);
$static_output .= "done.\n";
update_output_window($static_output);
return;
}
function delete_package_xml($package_name, $when = "post-deinstall") {
global $g, $config, $static_output, $pkg_interface;
conf_mount_rw();
$pkgid = get_package_id($package_name);
if ($pkgid == -1) {
$static_output .= sprintf(gettext("The %s package is not installed.%sDeletion aborted."), $package_name, "\n\n");
update_output_window($static_output);
if ($pkg_interface <> "console") {
echo "\n<script>document.getElementById('progressbar').style.visibility='hidden';</script>";
echo "\n<script>document.getElementById('progholder').style.visibility='hidden';</script>";
}
ob_flush();
sleep(1);
conf_mount_ro();
return;
}
pkg_debug(sprintf(gettext("Removing %s package... "), $package_name));
$static_output .= sprintf(gettext("Removing %s components..."), $package_name) . "\n";
update_output_window($static_output);
/* parse package configuration */
$packages = &$config['installedpackages']['package'];
$menus =& $config['installedpackages']['menu'];
$services = &$config['installedpackages']['service'];
$pkg_info =& $packages[$pkgid];
if (file_exists("/usr/local/pkg/" . $pkg_info['configurationfile'])) {
$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $packages[$pkgid]['configurationfile'], "packagegui");
/* remove menu items */
if (is_array($pkg_config['menu'])) {
$static_output .= gettext("Menu items... ");
update_output_window($static_output);
if (is_array($pkg_config['menu']) && is_array($menus)) {
foreach ($pkg_config['menu'] as $menu) {
foreach ($menus as $key => $instmenu) {
if ($instmenu['name'] == $menu['name']) {
unset($menus[$key]);
break;
}
}
}
}
$static_output .= gettext("done.") . "\n";
update_output_window($static_output);
}
/* remove services */
if (is_array($pkg_config['service'])) {
$static_output .= gettext("Services... ");
update_output_window($static_output);
if (is_array($pkg_config['service']) && is_array($services)) {
foreach ($pkg_config['service'] as $service) {
foreach ($services as $key => $instservice) {
if ($instservice['name'] == $service['name']) {
if (platform_booting() != true) {
stop_service($service['name']);
}
if ($service['rcfile']) {
$prefix = RCFILEPREFIX;
if (!empty($service['prefix'])) {
$prefix = $service['prefix'];
}
if (file_exists("{$prefix}{$service['rcfile']}")) {
@unlink("{$prefix}{$service['rcfile']}");
}
}
unset($services[$key]);
}
}
}
}
$static_output .= gettext("done.") . "\n";
update_output_window($static_output);
}
/*
* XXX: Otherwise inclusion of config.inc again invalidates actions taken.
* Same is done during installation.
*/
write_config("Intermediate config write during package removal for {$package_name}.");
/*
* If a require exists, include it. this will
* show us where an error exists in a package
* instead of making us blindly guess
*/
$missing_include = false;
if ($pkg_config['include_file'] <> "") {
$static_output .= gettext("Loading package instructions...") . "\n";
update_output_window($static_output);
if (file_exists($pkg_config['include_file'])) {
pkg_debug("require_once(\"{$pkg_config['include_file']}\")\n");
require_once($pkg_config['include_file']);
} else {
pkg_debug("Missing include {$pkg_config['include_file']}\n");
$missing_include = true;
update_output_window($static_output);
$static_output .= "Include file " . basename($pkg_config['include_file']) . " could not be found for inclusion.\n";
}
}
/* ermal
* NOTE: It is not possible to handle parse errors on eval.
* So we prevent it from being run at all to not interrupt all the other code.
*/
if ($when == "deinstall" && $missing_include == false) {
/* evaluate this package's global functions and pre deinstall commands */
if ($pkg_config['custom_php_global_functions'] <> "") {
eval_once($pkg_config['custom_php_global_functions']);
}
if ($pkg_config['custom_php_pre_deinstall_command'] <> "") {
eval_once($pkg_config['custom_php_pre_deinstall_command']);
}
}
/* deinstall commands */
if ($when == "post-deinstall" && $pkg_config['custom_php_deinstall_command'] <> "") {
$static_output .= gettext("Deinstall commands... ");
update_output_window($static_output);
if ($missing_include == false) {
eval_once($pkg_config['custom_php_deinstall_command']);
$static_output .= gettext("done.") . "\n";
} else {
$static_output .= "\nNot executing custom deinstall hook because an include is missing.\n";
}
update_output_window($static_output);
}
}
/* syslog */
$need_syslog_restart = false;
if (is_array($pkg_info['logging']) && $pkg_info['logging']['logfilename'] <> "") {
$static_output .= "Syslog entries... ";
update_output_window($static_output);
@unlink("{$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
$static_output .= "done.\n";
update_output_window($static_output);
$need_syslog_restart = true;
}
/* remove config.xml entries */
$static_output .= gettext("Configuration... ");
update_output_window($static_output);
unset($config['installedpackages']['package'][$pkgid]);
$static_output .= gettext("done.") . "\n";
update_output_window($static_output);
write_config("Removed {$package_name} package.\n");
/* remove package entry from /etc/syslog.conf if needed */
/* this must be done after removing the entries from config.xml */
if ($need_syslog_restart) {
system_syslogd_start();
}
conf_mount_ro();
}
function pkg_reinstall_all() {
global $g, $config;
// XXX: implement
return;
}
function stop_packages() {
require_once("config.inc");
require_once("functions.inc");
require_once("filter.inc");
require_once("shaper.inc");
require_once("captiveportal.inc");
require_once("pkg-utils.inc");
require_once("pfsense-utils.inc");
require_once("service-utils.inc");
global $config, $g;
log_error("Stopping all packages.");
$rcfiles = glob(RCFILEPREFIX . "*.sh");
if (!$rcfiles) {
$rcfiles = array();
} else {
$rcfiles = array_flip($rcfiles);
if (!$rcfiles) {
$rcfiles = array();
}
}
if (is_array($config['installedpackages']['package'])) {
foreach ($config['installedpackages']['package'] as $package) {
echo " Stopping package {$package['name']}...";
$internal_name = get_package_internal_name($package);
stop_service($internal_name);
unset($rcfiles[RCFILEPREFIX . strtolower($internal_name) . ".sh"]);
echo "done.\n";
}
}
foreach ($rcfiles as $rcfile => $number) {
$shell = @popen("/bin/sh", "w");
if ($shell) {
echo " Stopping {$rcfile}...";
if (!@fwrite($shell, "{$rcfile} stop >>/tmp/bootup_messages 2>&1")) {
if ($shell) {
pclose($shell);
}
$shell = @popen("/bin/sh", "w");
}
echo "done.\n";
pclose($shell);
}
}
}
?>
|