Project

General

Profile

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