1
|
#!/usr/local/bin/php
|
2
|
<?php
|
3
|
/*
|
4
|
pkg_mgr_install.php
|
5
|
Part of pfSense (www.pfSense.com)
|
6
|
Copyright (C) 2004 Scott Ullrich
|
7
|
All rights reserved.
|
8
|
|
9
|
Redistribution and use in source and binary forms, with or without
|
10
|
modification, are permitted provided that the following conditions are met:
|
11
|
|
12
|
1. Redistributions of source code must retain the above copyright notice,
|
13
|
this list of conditions and the following disclaimer.
|
14
|
|
15
|
2. Redistributions in binary form must reproduce the above copyright
|
16
|
notice, this list of conditions and the following disclaimer in the
|
17
|
documentation and/or other materials provided with the distribution.
|
18
|
|
19
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
20
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
21
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
22
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
23
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
24
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
25
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
26
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
27
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28
|
POSSIBILITY OF SUCH DAMAGE.
|
29
|
*/
|
30
|
|
31
|
require("guiconfig.inc");
|
32
|
require("xmlparse_pkg.inc");
|
33
|
|
34
|
if(!file_exists("/usr/local/pkg/")) mwexec("mkdir -p /usr/local/pkg/");
|
35
|
if(!file_exists("/usr/local/www/ext/")) mwexec("mkdir -p /usr/local/www/ext");
|
36
|
|
37
|
$pb_percent = 1;
|
38
|
|
39
|
/*
|
40
|
* open logging facility
|
41
|
*/
|
42
|
$fd_log = fopen("/tmp/pkg_mgr.log", "w");
|
43
|
fwrite($fd_log, "Begin of Package Manager installation session.\n");
|
44
|
|
45
|
/*
|
46
|
* update_output_window: update top textarea dynamically.
|
47
|
*/
|
48
|
function update_status($status) {
|
49
|
echo "\n<script language=\"JavaScript\">document.forms[0].status.value=\"" . $status . "\";</script>";
|
50
|
}
|
51
|
|
52
|
/*
|
53
|
* update_output_window: update bottom textarea dynamically.
|
54
|
*/
|
55
|
function update_output_window($text) {
|
56
|
$log = ereg_replace("\n", "\\n", $text);
|
57
|
echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
|
58
|
}
|
59
|
|
60
|
/*
|
61
|
* get_dir: return an array of $dir
|
62
|
*/
|
63
|
function get_dir($dir) {
|
64
|
$dir_array = array();
|
65
|
$d = dir($dir);
|
66
|
while (false !== ($entry = $d->read())) {
|
67
|
array_push($dir_array, $entry);
|
68
|
}
|
69
|
$d->close();
|
70
|
return $dir_array;
|
71
|
}
|
72
|
|
73
|
/*
|
74
|
* exec_command_and_return_text_array: execute command and return output
|
75
|
*/
|
76
|
function exec_command_and_return_text_array($command) {
|
77
|
$counter = 0;
|
78
|
$fd = popen($command . " 2>&1 ", "r");
|
79
|
while(!feof($fd)) {
|
80
|
$tmp .= fread($fd,49);
|
81
|
}
|
82
|
fclose($fd);
|
83
|
$temp_array = split("\n", $tmp);
|
84
|
return $tmp_array;
|
85
|
}
|
86
|
|
87
|
/*
|
88
|
* exec_command_and_return_text: execute command and return output
|
89
|
*/
|
90
|
function exec_command_and_return_text($command) {
|
91
|
$counter = 0;
|
92
|
$tmp = "";
|
93
|
$fd = popen($command . " 2>&1 ", "r");
|
94
|
while(!feof($fd)) {
|
95
|
$tmp .= fread($fd,49);
|
96
|
}
|
97
|
fclose($fd);
|
98
|
return $tmp;
|
99
|
}
|
100
|
|
101
|
/*
|
102
|
* exec_command_and_return_text: execute command and update output window dynamically
|
103
|
*/
|
104
|
function execute_command_return_output($command) {
|
105
|
global $fd_log, $pb_percent;
|
106
|
if($pb_percent > 100) $pb_percent = 1;
|
107
|
update_progress_bar($pb_percent);
|
108
|
$fd = popen($command . " 2>&1 ", "r");
|
109
|
echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
|
110
|
$counter = 0;
|
111
|
$counter2 = 0;
|
112
|
while(!feof($fd)) {
|
113
|
$tmp = fread($fd, 50);
|
114
|
fwrite($fd_log, $tmp);
|
115
|
$tmp1 = ereg_replace("\n","\\n", $tmp);
|
116
|
$text = ereg_replace("\"","'", $tmp1);
|
117
|
if($lasttext == "..") {
|
118
|
$text = "";
|
119
|
$lasttext = "";
|
120
|
$counter=$counter-2;
|
121
|
} else {
|
122
|
$lasttext .= $text;
|
123
|
}
|
124
|
if($counter > 51) {
|
125
|
$counter = 0;
|
126
|
$extrabreak = "\\n";
|
127
|
} else {
|
128
|
$extrabreak = "";
|
129
|
$counter++;
|
130
|
}
|
131
|
if($counter2 > 600) {
|
132
|
echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
|
133
|
$counter2 = 0;
|
134
|
} else
|
135
|
$counter2++;
|
136
|
echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = this.document.forms[0].output.value + \"" . $text . $extrabreak . "\"; f('output'); </script>";
|
137
|
}
|
138
|
$pb_percent++;
|
139
|
fclose($fd);
|
140
|
}
|
141
|
|
142
|
function update_progress_bar($percent) {
|
143
|
if($percent > 100) $percent = 1;
|
144
|
echo "\n<script type=\"text/javascript\" language=\"javascript\">";
|
145
|
echo "\ndocument.progressbar.style.width='" . $percent . "%';";
|
146
|
echo "\n</script>";
|
147
|
}
|
148
|
|
149
|
$a_out = &$pkg_config['packages'];
|
150
|
|
151
|
?>
|
152
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
153
|
<html>
|
154
|
<head>
|
155
|
<title><?=gentitle("System: Package Manager: Install Package");?></title>
|
156
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
157
|
<link href="gui.css" rel="stylesheet" type="text/css">
|
158
|
</head>
|
159
|
|
160
|
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
|
161
|
<?php
|
162
|
$config_tmp = $config;
|
163
|
$config = $pfSense_config;
|
164
|
include("fbegin.inc");
|
165
|
$config = $config_tmp;
|
166
|
?>
|
167
|
<p class="pgtitle">System: Package Manager: Install Package</p>
|
168
|
<form action="firewall_nat_out_load_balancing.php" method="post">
|
169
|
<?php if ($savemsg) print_info_box($savemsg); ?>
|
170
|
<?php
|
171
|
if(!file_exists("/tmp/pkg_config.xml")) {
|
172
|
mwexec("cd {$g['tmp_path']} && /usr/bin/fetch \"http://www.pfsense.com/packages/pkg_config.xml\" >/dev/null 2>&1 ");
|
173
|
if(!file_exists("{$g['tmp_path']}/pkg_config.xml")) {
|
174
|
print_info_box_np("Could not download pkg_config.xml from pfSense.com. Check your DNS settings.");
|
175
|
die;
|
176
|
}
|
177
|
}
|
178
|
|
179
|
$pkg_config = parse_xml_config_pkg("{$g['tmp_path']}/pkg_config.xml", "pfsensepkgs");
|
180
|
|
181
|
$id = $_GET['id'];
|
182
|
|
183
|
if(!$pkg_config['packages']) {
|
184
|
print_info_box_np("Could not find any packages in pkg_config.xml");
|
185
|
}
|
186
|
?>
|
187
|
<table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr><td>
|
188
|
<ul id="tabnav">
|
189
|
<li class="tabinact"><a href="pkg_mgr.php">Available Packages</a></li>
|
190
|
<li class="tabact">Installed Packages</a></li>
|
191
|
</ul>
|
192
|
</td></tr>
|
193
|
<tr>
|
194
|
<td class="tabcont">
|
195
|
<table width="100%" border="0" cellpadding="6" cellspacing="0">
|
196
|
<tr>
|
197
|
<td>
|
198
|
<!-- progress bar -->
|
199
|
<center>
|
200
|
<table id="progholder" name="progholder" height='20' border='1' bordercolor='black' width='420' bordercolordark='#000000' bordercolorlight='#000000' style='border-collapse: collapse' colspacing='2' cellpadding='2' cellspacing='2'><tr><td><img border='0' src='progress_bar.gif' width='280' height='23' name='progressbar' id='progressbar'></td></tr></table>
|
201
|
<br>
|
202
|
<!-- status box -->
|
203
|
<textarea cols="60" rows="1" name="status" id="status" wrap="hard">One moment please... This will take a while!</textarea>
|
204
|
<!-- command output box -->
|
205
|
<textarea cols="60" rows="25" name="output" id="output" wrap="hard"></textarea>
|
206
|
</center>
|
207
|
</td>
|
208
|
</tr>
|
209
|
</table>
|
210
|
</td>
|
211
|
</tr>
|
212
|
</table>
|
213
|
</form>
|
214
|
<?php include("fend.inc"); ?>
|
215
|
</body>
|
216
|
</html>
|
217
|
|
218
|
<?php
|
219
|
|
220
|
update_progress_bar($pb_percent);
|
221
|
$pb_percent += 10;
|
222
|
|
223
|
/*
|
224
|
* install the package
|
225
|
*/
|
226
|
|
227
|
// Ensure directories are in place for pkg_add.
|
228
|
mwexec("mkdir /usr/local/www/ext/Services >/dev/null 2>&1");
|
229
|
mwexec("mkdir /usr/local/www/ext/System >/dev/null 2>&1");
|
230
|
mwexec("mkdir /usr/local/www/ext/Interfaces >/dev/null 2>&1");
|
231
|
mwexec("mkdir /usr/local/www/ext/Firewall >/dev/null 2>&1");
|
232
|
mwexec("mkdir /usr/local/www/ext/VPN >/dev/null 2>&1");
|
233
|
mwexec("mkdir /usr/local/www/ext/Status >/dev/null 2>&1");
|
234
|
mwexec("mkdir /usr/local/www/ext/Diagnostics >/dev/null 2>&1");
|
235
|
mwexec("mkdir /usr/local/pkg >/dev/null 2>&1");
|
236
|
|
237
|
update_progress_bar($pb_percent);
|
238
|
$pb_percent += 10;
|
239
|
|
240
|
$a_out = &$pkg_config['packages']['package'];
|
241
|
$pkgent = array();
|
242
|
$pkgent['name'] = $pkg_config['packages']['package'][$id]['name'];
|
243
|
$pkgent['descr'] = $pkg_config['packages']['package'][$id]['descr'];
|
244
|
$pkgent['category'] = $pkg_config['packages']['package'][$id]['category'];
|
245
|
$pkgent['depends_on_package'] = $a_out[$id]['depends_on_package'];
|
246
|
$pkgent['depends_on_package_base_url'] = $a_out[$id]['depends_on_package_base_url'];
|
247
|
$pkgent['pfsense_package'] = $a_out[$id]['pfsense_package'];
|
248
|
$pkgent['pfsense_package_base_url'] = $a_out[$id]['pfsense_package_base_url'];
|
249
|
$pkgent['configurationfile'] = $a_out[$id]['configurationfile'];
|
250
|
if($pkg_config['packages']['package'][$id]['logging']) {
|
251
|
// logging facilities.
|
252
|
$pkgent['logging']['facility'] = $pkg_config['packages']['package'][$id]['logging']['facility'];
|
253
|
$pkgent['logging']['logfile_name'] = $pkg_config['packages']['package'][$id]['logging']['logfile_name'];
|
254
|
mwexec("/usr/sbin/clog -i -s 32768 /var/log/" . $pkgent['logging']['logfile_name']);
|
255
|
mwexec("chmod 0600 /var/log/" . $pkgent['logging']['logfile_name']);
|
256
|
add_text_to_file("/etc/syslog.conf", $pkgent['logging']['facilityname'] . "\t\t\t" . $pkgent['logging']['logfilename']);
|
257
|
mwexec("/usr/bin/killall -HUP syslogd");
|
258
|
}
|
259
|
$a_out = &$config['packages']['package']; // save item to installedpkgs
|
260
|
|
261
|
update_progress_bar($pb_percent);
|
262
|
$pb_percent += 10;
|
263
|
|
264
|
fwrite($fd_log, "ls /var/db/pkg | grep " . $pkgent['name'] . "\n" . $status);
|
265
|
if($status <> "") {
|
266
|
// package is already installed!?
|
267
|
print_info_box_np("NOTICE! " . $pkgent['name'] . " is already installed! Installation will be registered.");
|
268
|
}
|
269
|
|
270
|
if($pkg_config['packages']['package'][$id]['config_file'] <> "") {
|
271
|
update_status("Downloading configuration file.");
|
272
|
fwrite($fd_log, "Downloading configuration file " . $pkg_config['packages']['package'][$id]['config_file'] . " ... \n");
|
273
|
update_progress_bar($pb_percent);
|
274
|
$pb_percent += 10;
|
275
|
mwexec("cd /usr/local/pkg/ && fetch " . $pkg_config['packages']['package'][$id]['config_file']);
|
276
|
if(!file_exists("/usr/local/pkg/" . $pkgent['name'] . ".xml")) {
|
277
|
update_output_window("ERROR! Could not fetch " . $pkg_config['packages']['package'][$id]['config_file'] . "\n");
|
278
|
exit;
|
279
|
}
|
280
|
}
|
281
|
|
282
|
update_status("Downloading and installing " . $pkgent['name'] . " - " . $pkgent['pfsense_package'] . " and its dependencies ... This could take a moment ...");
|
283
|
fwrite($fd_log, "Downloading and installing " . $pkgent['name'] . " ... \n");
|
284
|
|
285
|
update_progress_bar($pb_percent);
|
286
|
$pb_percent += 10;
|
287
|
|
288
|
if ($pkgent['pfsense_package_base_url'] <> "") {
|
289
|
$text = exec_command_and_return_text("cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['pfsense_package_base_url'] . "/" . $pkgent['pfsense_package']);
|
290
|
update_output_window($text);
|
291
|
fwrite($fd_log, "Executing: cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['pfsense_package_base_url'] . "/" . $pkgent['pfsense_package'] . "\n" . $text);
|
292
|
}
|
293
|
|
294
|
update_progress_bar($pb_percent);
|
295
|
$pb_percent += 10;
|
296
|
|
297
|
if ($pkgent['depends_on_package_base_url'] <> "") {
|
298
|
update_status("Downloading and installing " . $pkgent['name'] . " and its dependencies ... This could take a moment ...");
|
299
|
$text = exec_command_and_return_text("cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['depends_on_package_base_url'] . "/" . $pkgent['depends_on_package']);
|
300
|
update_output_window($text);
|
301
|
fwrite($fd_log, "cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['depends_on_package_base_url'] . "/" . $pkgent['depends_on_package'] . "\n" . $text);;
|
302
|
}
|
303
|
|
304
|
update_progress_bar($pb_percent);
|
305
|
$pb_percent += 10;
|
306
|
|
307
|
$config = parse_xml_config("{$g['conf_path']}/config.xml", $g['xml_rootobj']);
|
308
|
|
309
|
update_progress_bar($pb_percent);
|
310
|
$pb_percent += 10;
|
311
|
|
312
|
$config['installedpackages']['package'][] = $pkgent;
|
313
|
|
314
|
if (isset($id) && $a_out[$id])
|
315
|
$a_out[$id] = $pkgent;
|
316
|
else
|
317
|
$a_out[] = $pkgent;
|
318
|
|
319
|
update_progress_bar($pb_percent);
|
320
|
$pb_percent += 10;
|
321
|
|
322
|
write_config();
|
323
|
|
324
|
update_progress_bar($pb_percent);
|
325
|
$pb_percent += 10;
|
326
|
|
327
|
$name = $pkgent['name'];
|
328
|
|
329
|
update_progress_bar($pb_percent);
|
330
|
$pb_percent++;
|
331
|
|
332
|
/*
|
333
|
* parse the config file for this package and install neededtext items.
|
334
|
*
|
335
|
*/
|
336
|
if(file_exists("/usr/local/pkg/" . $pkgent['name'] . ".xml")) {
|
337
|
$package_conf = parse_xml_config_pkg("/usr/local/pkg/" . $pkgent['name'] . ".xml", "packagegui");
|
338
|
if($package_conf['modify_system']['item'] <> "") {
|
339
|
foreach ($package_conf['modify_system']['item'] as $ms) {
|
340
|
update_progress_bar($pb_percent);
|
341
|
$pb_percent += 10;
|
342
|
if($ms['textneeded']) {
|
343
|
add_text_to_file($ms['modifyfilename'],$ms['textneeded']);
|
344
|
}
|
345
|
}
|
346
|
}
|
347
|
|
348
|
/*
|
349
|
* fetch additional files needed for package if defined
|
350
|
* and uncompress if needed.
|
351
|
*/
|
352
|
if ($package_conf['additional_files_needed'] <> "") {
|
353
|
foreach($package_conf['additional_files_needed']['item'] as $afn) {
|
354
|
update_progress_bar($pb_percent);
|
355
|
$pb_percent += 10;
|
356
|
$filename = get_filename_from_url($afn);
|
357
|
update_status("Downloading additional files needed for package " . $filename . " ...");
|
358
|
system("cd /usr/local/pkg && /usr/bin/fetch " . $afn . " 2>/dev/null");
|
359
|
if(stristr($filename, '.tgz') <> "")
|
360
|
system("cd /usr/local/pkg && tar xzvf " . $filename);
|
361
|
}
|
362
|
}
|
363
|
|
364
|
/*
|
365
|
* loop through menu installation items
|
366
|
* installing multiple items if need be.
|
367
|
*/
|
368
|
foreach ($package_conf['menu'] as $menu) {
|
369
|
// install menu item into the ext folder
|
370
|
fwrite($fd_log, "Adding menu option to " . $menu['section'] . "/" . $menu['name'] . "\n");
|
371
|
$fd = fopen("/usr/local/www/ext/" . $menu['section'] . "/" . $menu['name'] , "w");
|
372
|
if($menu['url'] <> "") {
|
373
|
// override $myurl for script.
|
374
|
$toeval = "\$myurl = \"" . getenv("HTTP_HOST") . "\"; \n";
|
375
|
eval($toeval);
|
376
|
// eval url so that above $myurl item can be processed if need be.
|
377
|
$urltmp = $menu['url'];
|
378
|
$toeval = "\$url = \"" . $urltmp . "\"; \n";
|
379
|
eval($toeval);
|
380
|
fwrite($fd, $url . "\n");
|
381
|
} else {
|
382
|
$xml = "";
|
383
|
if(stristr($menu['configfile'],".xml") == "") $xml = ".xml";
|
384
|
fwrite($fd, "/pkg.php?xml=" . $menu['configfile'] . $xml . "\n");
|
385
|
}
|
386
|
fclose($fd);
|
387
|
}
|
388
|
} else {
|
389
|
update_output_window("WARNING! /usr/local/pkg/" . $pkgent['name'] . ".xml" . " does not exist!\n");
|
390
|
fwrite($fd_log, "WARNING! /usr/local/pkg/" . $pkgent['name'] . ".xml" . " does not exist!\n");
|
391
|
}
|
392
|
fwrite($fd_log, "End of Package Manager installation session.\n");
|
393
|
|
394
|
update_progress_bar($pb_percent);
|
395
|
$pb_percent += 10;
|
396
|
|
397
|
// return dependency list to output later.
|
398
|
$command = "TODELETE=`ls /var/db/pkg | grep " . $name . "` && /usr/sbin/pkg_info -r \$TODELETE | grep Dependency: | cut -d\" \" -f2";
|
399
|
$dependencies = exec_command_and_return_text($command);
|
400
|
fwrite($fd_log, "Installed " . $name . " and the following dependencies:\n" . $dependencies);
|
401
|
|
402
|
update_progress_bar($pb_percent);
|
403
|
$pb_percent += 10;
|
404
|
|
405
|
$status = exec_command_and_return_text("ls /var/db/pkg | grep " . $pkgent['name']);
|
406
|
fwrite($fd_log, "ls /var/db/pkg | grep " . $pkgent['name'] . "\n" . $status);
|
407
|
if($status <> "") {
|
408
|
update_status("Package installation completed.");
|
409
|
fwrite($fd_log, "Package installation completed.\n");
|
410
|
} else {
|
411
|
update_status("Package WAS NOT installed properly.");
|
412
|
fwrite($fd_log, "Package WAS NOT installed properly.\n");
|
413
|
}
|
414
|
|
415
|
update_progress_bar($pb_percent);
|
416
|
$pb_percent += 10;
|
417
|
|
418
|
// close log
|
419
|
fclose($fd_log);
|
420
|
|
421
|
if($package_conf['custom_php_install_command']) {
|
422
|
eval($package_conf['custom_php_install_command']);
|
423
|
}
|
424
|
|
425
|
// reopen and read log in
|
426
|
$fd_log = fopen("/tmp/pkg_mgr.log", "r");
|
427
|
$tmp = "";
|
428
|
while(!feof($fd_log)) {
|
429
|
$tmp .= fread($fd_log,49);
|
430
|
}
|
431
|
fclose($fd_log);
|
432
|
$log = ereg_replace("\n", "\\n", $tmp);
|
433
|
//echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
|
434
|
|
435
|
update_progress_bar(100);
|
436
|
|
437
|
echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
|
438
|
echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
|
439
|
|
440
|
function add_text_to_file($file, $text) {
|
441
|
global $fd_log;
|
442
|
fwrite($fd_log, "Adding needed text items:\n");
|
443
|
$filecontents = exec_command_and_return_text("cat " . $file);
|
444
|
$filecontents = str_replace($text, "", $filecontents);
|
445
|
$text = $filecontents . $text;
|
446
|
fwrite($fd_log, $text . "\n");
|
447
|
$fd = fopen($file, "w");
|
448
|
fwrite($fd, $text . "\n");
|
449
|
fclose($fd);
|
450
|
}
|
451
|
|
452
|
function get_filename_from_url($url) {
|
453
|
$filenamesplit = split("/", $url);
|
454
|
foreach($filenamesplit as $fn) $filename = $fn;
|
455
|
return $filename;
|
456
|
}
|
457
|
|
458
|
?>
|
459
|
|
460
|
|
461
|
|
462
|
|
463
|
|
464
|
|
465
|
|
466
|
|
467
|
|