Project

General

Profile

Download (19.3 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/*
4
    pkg_mgr_install.php
5
    part of pfSense (http://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
            echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = '';</script>\n";
80
            while(!feof($fd)) {
81
                        $tmp .= fread($fd,49);
82
                        echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = this.document.forms[0].output.value + \"" . $tmp . $extrabreak .  "\"; </script>";
83
            }
84
            fclose($fd);
85
            $temp_array = split("\n", $tmp);
86
            return $tmp_array;
87
}
88

    
89
/*
90
 *   exec_command_and_return_text: execute command and return output
91
 */
92
function exec_command_and_return_text($command) {
93
            $counter = 0;
94
            $tmp = "";
95
            $fd = popen($command . " 2>&1 ", "r");
96
            echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = '';</script>\n";
97
            while(!feof($fd)) {
98
                        $tmp .= fread($fd,49);
99
                        echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = this.document.forms[0].output.value + \"" . $tmp . $extrabreak .  "\"; </script>";
100
            }
101
            fclose($fd);
102
            return $tmp;
103
}
104

    
105
/*
106
 *   exec_command_and_return_text: execute command and update output window dynamically
107
 */
108
function execute_command_return_output($command) {
109
    global $fd_log, $pb_percent;
110
    if($pb_percent > 100) $pb_percent = 1;
111
    update_progress_bar($pb_percent);
112
    $fd = popen($command . " 2>&1 ", "r");
113
    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
114
    $counter = 0;
115
    $counter2 = 0;
116
    while(!feof($fd)) {
117
	$tmp = fread($fd, 50);
118
        fwrite($fd_log, $tmp);
119
	$tmp1 = ereg_replace("\n","\\n", $tmp);
120
	$text = ereg_replace("\"","'", $tmp1);
121
	echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = this.document.forms[0].output.value + \"" . $tmp . $extrabreak .  "\"; f('output'); </script>";
122
    }
123
    $pb_percent++;
124
    fclose($fd);
125
}
126

    
127
function update_progress_bar($percent) {
128
            if($percent > 100) $percent = 1;
129
            echo "\n<script type=\"text/javascript\" language=\"javascript\">";
130
            echo "\ndocument.progressbar.style.width='" . $percent . "%';";
131
            echo "\n</script>";
132
}
133

    
134
$a_out = &$pkg_config['packages'];
135

    
136
?>
137
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
138
<html>
139
<head>
140
<title><?=gentitle("System: Package Manager: Install Package");?></title>
141
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
142
<link href="gui.css" rel="stylesheet" type="text/css">
143
</head>
144

    
145
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
146
<?php
147
$config_tmp = $config;
148
$config = $pfSense_config;
149
include("fbegin.inc");
150
$config = $config_tmp;
151
?>
152
<p class="pgtitle">System: Package Manager: Install Package</p>
153
<form action="firewall_nat_out_load_balancing.php" method="post">
154
<?php if ($savemsg) print_info_box($savemsg); ?>
155
<?php
156
if(!file_exists("/tmp/pkg_config.xml")) {
157
            mwexec("cd {$g['tmp_path']} && /usr/bin/fetch \"http://www.pfsense.com/packages/pkg_config.xml\" >/dev/null 2>&1 ");
158
            if(!file_exists("{$g['tmp_path']}/pkg_config.xml")) {
159
                        print_info_box_np("Could not download pkg_config.xml from pfSense.com.  Check your DNS settings.");
160
                        die;
161
            }
162
}
163

    
164
$pkg_config = parse_xml_config_pkg("{$g['tmp_path']}/pkg_config.xml", "pfsensepkgs");
165

    
166
$id = $_GET['id'];
167

    
168
if(!$pkg_config['packages']) {
169
            print_info_box_np("Could not find any packages in pkg_config.xml");
170
}
171
?>
172
<table width="100%" border="0" cellpadding="0" cellspacing="0">  <tr><td>
173
  <ul id="tabnav">
174
    <li class="tabinact"><a href="pkg_mgr.php">Available Packages</a></li>
175
    <li class="tabact">Installed Packages</a></li>
176
  </ul>
177
  </td></tr>
178
  <tr>
179
    <td class="tabcont">
180
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
181
               <tr>
182
                 <td>
183
                     <!-- progress bar -->
184
                     <center>
185
                     <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>
186
                     <br>
187
	             <!-- status box -->
188
                     <textarea cols="60" rows="1" name="status" id="status" wrap="hard">One moment please... This will take a while!</textarea>
189
                     <!-- command output box -->
190
	             <textarea cols="60" rows="25" name="output" id="output" wrap="hard"></textarea>
191
                     </center>
192
                 </td>
193
               </tr>
194
        </table>
195
    </td>
196
  </tr>
197
</table>
198
</form>
199
<?php include("fend.inc"); ?>
200
</body>
201
</html>
202

    
203
<?php
204

    
205
update_progress_bar($pb_percent);
206
$pb_percent += 10;
207

    
208
/*
209
 * install the package
210
*/
211

    
212
// Ensure directories are in place for pkg_add.
213
mwexec("mkdir /usr/local/www/ext/Services >/dev/null 2>&1");
214
mwexec("mkdir /usr/local/www/ext/System >/dev/null 2>&1");
215
mwexec("mkdir /usr/local/www/ext/Interfaces >/dev/null 2>&1");
216
mwexec("mkdir /usr/local/www/ext/Firewall >/dev/null 2>&1");
217
mwexec("mkdir /usr/local/www/ext/VPN >/dev/null 2>&1");
218
mwexec("mkdir /usr/local/www/ext/Status >/dev/null 2>&1");
219
mwexec("mkdir /usr/local/www/ext/Diagnostics >/dev/null 2>&1");
220
mwexec("mkdir /usr/local/pkg >/dev/null 2>&1");
221

    
222
update_progress_bar($pb_percent);
223
$pb_percent += 10;
224

    
225
$a_out = &$pkg_config['packages']['package'];
226
$pkgent = array();
227
$pkgent['name'] = $pkg_config['packages']['package'][$id]['name'];
228
$pkgent['descr'] = $pkg_config['packages']['package'][$id]['descr'];
229
$pkgent['category'] = $pkg_config['packages']['package'][$id]['category'];
230

    
231
$pkgent['version'] = $pkg_config['packages']['package'][$id]['version'];
232

    
233
$pkgent['depends_on_package'] = $a_out[$id]['depends_on_package'];
234
$pkgent['depends_on_package_base_url'] = $a_out[$id]['depends_on_package_base_url'];
235
$pkgent['pfsense_package'] = $a_out[$id]['pfsense_package'];
236
$pkgent['pfsense_package_base_url'] = $a_out[$id]['pfsense_package_base_url'];
237
$pkgent['configurationfile'] = $a_out[$id]['configurationfile'];
238
if($pkg_config['packages']['package'][$id]['logging']) {
239
    // logging facilities.
240
    $pkgent['logging']['facility'] = $pkg_config['packages']['package'][$id]['logging']['facility'];
241
    $pkgent['logging']['logfile_name'] = $pkg_config['packages']['package'][$id]['logging']['logfile_name'];
242
    mwexec("/usr/sbin/clog -i -s 32768 /var/log/" . $pkgent['logging']['logfile_name']);
243
    mwexec("chmod 0600 /var/log/" . $pkgent['logging']['logfile_name']);
244
    add_text_to_file("/etc/syslog.conf", $pkgent['logging']['facilityname'] . "\t\t\t" . $pkgent['logging']['logfilename']);
245
    mwexec("/usr/bin/killall -HUP syslogd");
246
}
247
$a_out = &$config['packages']['package']; // save item to installedpkgs
248

    
249
update_progress_bar($pb_percent);
250
$pb_percent += 10;
251

    
252
fwrite($fd_log, "ls /var/db/pkg | grep " . $pkgent['name'] . "\n" . $status);
253
if($status <> "") {
254
            // package is already installed!?
255
            print_info_box_np("NOTICE! " . $pkgent['name'] . " is already installed!  Installation will be registered.");
256
}
257

    
258
if($pkg_config['packages']['package'][$id]['config_file'] <> "") {
259
    update_status("Downloading configuration file.");
260
    fwrite($fd_log, "Downloading configuration file " . $pkg_config['packages']['package'][$id]['config_file'] . " ... \n");
261
    update_progress_bar($pb_percent);
262
    $pb_percent += 10;
263
    mwexec("cd /usr/local/pkg/ && fetch " . $pkg_config['packages']['package'][$id]['config_file']);
264
    if(!file_exists("/usr/local/pkg/" . $pkgent['name'] . ".xml")) {
265
        update_output_window("ERROR!  Could not fetch " . $pkg_config['packages']['package'][$id]['config_file'] . "\n");
266
        exit;
267
    }
268
}
269

    
270
update_status("Downloading and installing " . $pkgent['name'] . " - " . $pkgent['pfsense_package'] . " and its dependencies ... This could take a moment ...");
271
fwrite($fd_log, "Downloading and installing " . $pkgent['name'] . " ... \n");
272

    
273
update_progress_bar($pb_percent);
274
$pb_percent += 10;
275

    
276
if ($pkgent['pfsense_package_base_url'] <> "") {
277
    $text = exec_command_and_return_text("cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['pfsense_package_base_url'] . "/" . $pkgent['pfsense_package']);
278
    update_output_window($text);
279
    fwrite($fd_log, "Executing: cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['pfsense_package_base_url'] . "/" . $pkgent['pfsense_package'] . "\n" . $text);
280
}
281

    
282
update_progress_bar($pb_percent);
283
$pb_percent += 10;
284

    
285
if ($pkgent['depends_on_package_base_url'] <> "") {
286
            update_status("Downloading and installing " . $pkgent['name'] . " and its dependencies ... This could take a moment ...");
287
            $text = exec_command_and_return_text("cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['depends_on_package_base_url'] . "/" . $pkgent['depends_on_package']);
288
            update_output_window($text);
289
            fwrite($fd_log, "cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['depends_on_package_base_url'] . "/" . $pkgent['depends_on_package'] . "\n" . $text);;
290
}
291

    
292
if ($pkgent['depends_on_package_base_url'] <> "" or $pkgent['pfsense_package_base_url'] <> "") {
293
    $status = exec_command_and_return_text("ls /var/db/pkg | grep " . $pkgent['name']);
294
    fwrite($fd_log, "ls /var/db/pkg | grep " . $pkgent['name'] . "\n" . $status);
295
    if($status <> "") {
296
                update_status("Package installed.  Lets finish up.");
297
                fwrite($fd_log, "Package installed.  Lets finish up.\n");
298
    } else {
299
                fwrite($fd_log, "Package WAS NOT installed properly.\n");
300
                fclose($fd_log);
301
                $filecontents = exec_command_and_return_text("cat " . $file);
302
                update_progress_bar(100);
303
                echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
304
                echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
305
                update_status("Package WAS NOT installed properly...Something went wrong..\n" . $filecontents);
306
                update_output_window("Error during package installation.");
307
                sleep(1);
308
                die;
309
    }
310
}
311

    
312
update_progress_bar($pb_percent);
313
$pb_percent += 10;
314

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

    
317
update_progress_bar($pb_percent);
318
$pb_percent += 10;
319

    
320
$config['installedpackages']['package'][] = $pkgent;
321

    
322
if (isset($id) && $a_out[$id])
323
        $a_out[$id] = $pkgent;
324
else
325
        $a_out[] = $pkgent;
326

    
327
update_progress_bar($pb_percent);
328
$pb_percent += 10;
329

    
330
write_config();
331

    
332
update_progress_bar($pb_percent);
333
$pb_percent += 10;
334

    
335
$name = $pkgent['name'];
336

    
337
update_progress_bar($pb_percent);
338
$pb_percent++;
339

    
340
/*
341
 * parse the config file for this package and install neededtext items.
342
 *
343
*/
344
if(file_exists("/usr/local/pkg/" . $pkgent['name'] . ".xml")) {
345
            $package_conf = parse_xml_config_pkg("/usr/local/pkg/" . $pkgent['name'] . ".xml", "packagegui");
346
            if($package_conf['modify_system']['item'] <> "") {
347
                foreach ($package_conf['modify_system']['item'] as $ms) {
348
                    update_progress_bar($pb_percent);
349
                    $pb_percent += 10;
350
                    if($ms['textneeded']) {
351
                        add_text_to_file($ms['modifyfilename'],$ms['textneeded']);
352
                    }
353
                }
354
            }
355

    
356
            /*
357
             * fetch additional files needed for package if defined
358
             * and uncompress if needed.
359
             */
360
            if ($package_conf['additional_files_needed'] <> "") {
361
                        foreach($package_conf['additional_files_needed']['item'] as $afn) {
362
                                    update_progress_bar($pb_percent);
363
                                    $pb_percent += 10;
364
                                    $filename = get_filename_from_url($afn);
365
                                    update_status("Downloading additional files needed for package " . $filename . " ...");
366
                                    system("cd /usr/local/pkg && /usr/bin/fetch " .  $afn . " 2>/dev/null");
367
                                    if(stristr($filename, '.tgz') <> "")
368
                                                system("cd /usr/local/pkg && tar xzvf " . $filename . " -C / >/dev/null 2>&1");
369
                        }
370
            }
371

    
372
            /*
373
             * loop through menu installation items
374
             * installing multiple items if need be.
375
            */
376
            if(is_array($package_conf['menu']))
377
                foreach ($package_conf['menu'] as $menu) {
378
                            // install menu item into the ext folder
379
                            fwrite($fd_log, "Adding menu option to " . $menu['section'] . "/" . $menu['name'] . "\n");
380
                            $fd = fopen("/usr/local/www/ext/" . $menu['section'] . "/" . $menu['name'] , "w");
381
                            if($menu['url'] <> "") {
382
                                        // override $myurl for script.
383
                                        $toeval = "\$myurl = \"" . getenv("HTTP_HOST") . "\"; \n";
384
                                        eval($toeval);
385
                                        // eval url so that above $myurl item can be processed if need be.
386
                                        $urltmp = $menu['url'];
387
                                        $toeval = "\$url = \"" . $urltmp . "\"; \n";
388
                                        eval($toeval);
389
                                        fwrite($fd, $url . "\n");
390
                            } else {
391
                                        $xml = "";
392
                                        if(stristr($menu['configfile'],".xml") == "") $xml = ".xml";
393
                                        fwrite($fd, "/pkg.php?xml=" . $menu['configfile'] . $xml . "\n");
394
                            }
395
                            fclose($fd);
396
            }
397
} else {
398
            update_output_window("WARNING! /usr/local/pkg/" . $pkgent['name'] . ".xml" . " does not exist!\n");
399
            fwrite($fd_log, "WARNING! /usr/local/pkg/" . $pkgent['name'] . ".xml" . " does not exist!\n");
400
}
401
fwrite($fd_log, "End of Package Manager installation session.\n");
402

    
403
update_progress_bar($pb_percent);
404
$pb_percent += 10;
405

    
406
// return dependency list to output later.
407
$command = "TODELETE=`ls /var/db/pkg | grep " . $name . "` && /usr/sbin/pkg_info -r \$TODELETE | grep Dependency: | cut -d\" \" -f2";
408
$dependencies = exec_command_and_return_text($command);
409
fwrite($fd_log, "Installed " . $name . " and the following dependencies:\n" . $dependencies);
410

    
411
update_progress_bar($pb_percent);
412
$pb_percent += 10;
413

    
414
if ($pkgent['depends_on_package_base_url'] <> "" or $pkgent['pfsense_package_base_url'] <> "") {
415
    $status = exec_command_and_return_text("ls /var/db/pkg | grep " . $pkgent['name']);
416
    fwrite($fd_log, "ls /var/db/pkg | grep " . $pkgent['name'] . "\n" . $status);
417
    if($status <> "") {
418
                update_status("Package installation completed.");
419
                fwrite($fd_log, "Package installation completed.\n");
420
    } else {
421
                update_status("Package WAS NOT installed properly.");
422
                fwrite($fd_log, "Package WAS NOT installed properly.\n");
423
    }
424
} else {
425
    update_status("Package installation completed.");
426
    fwrite($fd_log, "Package installation completed.\n");
427
}
428

    
429
update_progress_bar($pb_percent);
430
$pb_percent += 10;
431

    
432
// close log
433
fclose($fd_log);
434

    
435
if($package_conf['custom_php_install_command']) {
436
    eval($package_conf['custom_php_install_command']);
437
}
438

    
439
// reopen and read log in
440
$fd_log = fopen("/tmp/pkg_mgr.log", "r");
441
$tmp = "";
442
while(!feof($fd_log)) {
443
            $tmp .= fread($fd_log,49);
444
}
445
fclose($fd_log);
446
$log = ereg_replace("\n", "\\n", $tmp);
447
//echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
448

    
449
update_progress_bar(100);
450

    
451
echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
452
echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
453

    
454
function add_text_to_file($file, $text) {
455
    global $fd_log;
456
    fwrite($fd_log, "Adding needed text items:\n");
457
    $filecontents = exec_command_and_return_text("cat " . $file);
458
    $filecontents = str_replace($text, "", $filecontents);
459
    $text = $filecontents . $text;
460
    fwrite($fd_log, $text . "\n");
461
    $fd = fopen($file, "w");
462
    fwrite($fd, $text . "\n");
463
    fclose($fd);
464
}
465

    
466
function get_filename_from_url($url) {
467
            $filenamesplit = split("/", $url);
468
            foreach($filenamesplit as $fn) $filename = $fn;
469
            return $filename;
470
}
471

    
472
?>
473

    
474

    
475

    
476

    
477

    
478

    
479

    
480

    
481

    
(55-55/99)