Project

General

Profile

Download (15.3 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/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
if(!file_exists("/usr/local/pkg/")) mwexec("mkdir -p /usr/local/pkg/");
34
if(!file_exists("/usr/local/www/ext/")) mwexec("mkdir -p /usr/local/www/ext");
35

    
36
$pb_percent = 1;
37

    
38
/*
39
 *   open logging facility
40
 */
41
$fd_log = fopen("/tmp/pkg_mgr.log", "w");
42
fwrite($fd_log, "Begin of Package Manager installation session.\n");
43

    
44
/*
45
 *   update_output_window: update top textarea dynamically.
46
 */
47
function update_status($status) {
48
            echo "\n<script language=\"JavaScript\">document.forms[0].status.value=\"" . $status . "\";</script>";
49
}
50

    
51
/*
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
/*
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
/*
87
 *   exec_command_and_return_text: execute command and return output
88
 */
89
function exec_command_and_return_text($command) {
90
            $counter = 0;
91
            $tmp = "";
92
            $fd = popen($command . " 2>&1 ", "r");
93
            while(!feof($fd)) {
94
                        $tmp .= fread($fd,49);
95
            }
96
            fclose($fd);
97
            return $tmp;
98
}
99

    
100
/*
101
 *   exec_command_and_return_text: execute command and update output window dynamically
102
 */
103
function execute_command_return_output($command) {
104
    global $fd_log, $pb_percent;
105
    if($pb_percent > 100) $pb_percent = 1;
106
    update_progress_bar($pb_percent);
107
    $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
	$tmp = fread($fd, 50);
113
        fwrite($fd_log, $tmp);
114
	$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
    $pb_percent++;
138
    fclose($fd);
139
}
140

    
141
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
$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
<?php
161
$config_tmp = $config;
162
$config = $pfSense_config;
163
include("fbegin.inc");
164
$config = $config_tmp;
165
?>
166
<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
$pkg_config = parse_xml_config_pkg("{$g['tmp_path']}/pkg_config.xml", "pfsensepkgs");
179

    
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
    <li class="tabinact"><a href="pkg_mgr.php">Available Packages</a></li>
189
    <li class="tabact">Installed Packages</a></li>
190
  </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
                     <!-- 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
                 </td>
207
               </tr>
208
        </table>
209
    </td>
210
  </tr>
211
</table>
212
</form>
213
<?php include("fend.inc"); ?>
214
</body>
215
</html>
216

    
217
<?php
218

    
219
update_progress_bar($pb_percent);
220
$pb_percent += 10;
221

    
222
/* install the package */
223

    
224
// 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
mwexec("mkdir /usr/local/www/ext/Diagnostics >/dev/null 2>&1");
232

    
233
update_progress_bar($pb_percent);
234
$pb_percent += 10;
235

    
236
$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
$pkgent['depends_on_package_base_url'] = $a_out[$id]['depends_on_package_base_url'];
243
$pkgent['pfsense_package'] = $a_out[$id]['pfsense_package'];
244
$pkgent['pfsense_package_base_url'] = $a_out[$id]['pfsense_package_base_url'];
245
$pkgent['configurationfile'] = $a_out[$id]['configurationfile'];
246
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
    mwexec("/usr/sbin/clog -i -s 32768 /var/log/" . $pkgent['logging']['logfile_name']);
251
    mwexec("chmod 0600 /var/log/" . $pkgent['logging']['logfile_name']);
252
    add_text_to_file("/etc/syslog.conf", $pkgent['logging']['facilityname'] . "\t\t\t" . $pkgent['logging']['logfilename']);
253
    mwexec("/usr/bin/killall -HUP syslogd");
254
}
255
$a_out = &$config['packages']['package']; // save item to installedpkgs
256

    
257
update_progress_bar($pb_percent);
258
$pb_percent += 10;
259

    
260
fwrite($fd_log, "ls /var/db/pkg | grep " . $pkgent['name'] . "\n" . $status);
261
if($status <> "") {
262
            // package is already installed!?
263
            print_info_box_np("NOTICE! " . $pkgent['name'] . " is already installed!  Installation will be registered.");
264
}
265

    
266
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
update_status("Downloading and installing " . $pkgent['name'] . " - " . $pkgent['pfsense_package'] . " and its dependencies ... This could take a moment ...");
278
fwrite($fd_log, "Downloading and installing " . $pkgent['name'] . " ... \n");
279

    
280
update_progress_bar($pb_percent);
281
$pb_percent += 10;
282

    
283
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

    
289
update_progress_bar($pb_percent);
290
$pb_percent += 10;
291

    
292
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
            update_output_window($text);
296
            fwrite($fd_log, "cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['depends_on_package_base_url'] . "/" . $pkgent['depends_on_package'] . "\n" . $text);;
297
}
298

    
299
update_progress_bar($pb_percent);
300
$pb_percent += 10;
301

    
302
$config = parse_xml_config("{$g['conf_path']}/config.xml", $g['xml_rootobj']);
303

    
304
update_progress_bar($pb_percent);
305
$pb_percent += 10;
306

    
307
$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
update_progress_bar($pb_percent);
315
$pb_percent += 10;
316

    
317
write_config();
318

    
319
update_progress_bar($pb_percent);
320
$pb_percent += 10;
321

    
322
$name = $pkgent['name'];
323

    
324
update_progress_bar($pb_percent);
325
$pb_percent++;
326

    
327
// parse the config file for this package and install neededtext items.
328
if(file_exists("/usr/local/pkg/" . $pkgent['name'] . ".xml")) {
329
            $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
            }
339
            // install menu item into the ext folder
340
            fwrite($fd_log, "Adding menu option to " . $package_conf['menu']['section'] . "/" . $package_conf['menu']['name'] . "\n");
341
            $fd = fopen("/usr/local/www/ext/" . $package_conf['menu']['section'] . "/" . $package_conf['menu']['name'] , "w");
342
            fwrite($fd, "/pkg.php?xml=" . $package_conf['name'] . ".xml\n");
343
            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
update_progress_bar($pb_percent);
351
$pb_percent += 10;
352

    
353
// 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
fwrite($fd_log, "Installed " . $name . " and the following dependencies:\n" . $dependencies);
357

    
358
update_progress_bar($pb_percent);
359
$pb_percent += 10;
360

    
361
$status = exec_command_and_return_text("ls /var/db/pkg | grep " . $pkgent['name']);
362
fwrite($fd_log, "ls /var/db/pkg | grep " . $pkgent['name'] . "\n" . $status);
363
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
update_progress_bar($pb_percent);
372
$pb_percent += 10;
373

    
374
// close log
375
fclose($fd_log);
376

    
377
if($package_conf['custom_php_install_command']) {
378
    eval($package_conf['custom_php_install_command']);
379
}
380

    
381
// reopen and read log in
382
$fd_log = fopen("/tmp/pkg_mgr.log", "r");
383
$tmp = "";
384
while(!feof($fd_log)) {
385
            $tmp .= fread($fd_log,49);
386
}
387
fclose($fd_log);
388
$log = ereg_replace("\n", "\\n", $tmp);
389
//echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
390

    
391
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
function add_text_to_file($file, $text) {
397
    global $fd_log;
398
    fwrite($fd_log, "Adding needed text items:\n");
399
    $filecontents = exec_command_and_return_text("cat " . $file);
400
    $filecontents = str_replace($text, "", $filecontents);
401
    $text = $filecontents . $text;
402
    fwrite($fd_log, $text . "\n");
403
    $fd = fopen($file, "w");
404
    fwrite($fd, $text . "\n");
405
    fclose($fd);
406
}
407

    
408
?>
409

    
410

    
411

    
412

    
413

    
414

    
415

    
416

    
417

    
(54-54/98)