Project

General

Profile

Download (21.4 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/* $Id$ */
4
/*
5
    pkg_mgr_install.php
6
    part of pfSense (http://www.pfSense.com)
7
    Copyright (C) 2005 Scott Ullrich and Colin Smith
8
    All rights reserved.
9

    
10
    Redistribution and use in source and binary forms, with or without
11
    modification, are permitted provided that the following conditions are met:
12

    
13
    1. Redistributions of source code must retain the above copyright notice,
14
       this list of conditions and the following disclaimer.
15

    
16
    2. Redistributions in binary form must reproduce the above copyright
17
       notice, this list of conditions and the following disclaimer in the
18
       documentation and/or other materials provided with the distribution.
19

    
20
    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
    POSSIBILITY OF SUCH DAMAGE.
30
*/
31

    
32
require("guiconfig.inc");
33
require("xmlparse_pkg.inc");
34

    
35
conf_mount_rw();
36

    
37
if(!file_exists("/var/db/pkg"))
38
            mkdir("/var/db/pkg");
39

    
40
/* /usr/local/pkg/ is where xml package files are stored. */
41
make_dirs("/usr/local/pkg");
42
/* /usr/local/pkg/pf is where custom php hook packages live to alter the rules when needed */
43
make_dirs("/usr/local/pkg/pf");
44
/* /usr/local/www/ext is where package links live for the left hand pane */
45
make_dirs("/usr/local/www/ext");
46

    
47
$a_out = &$pkg_config['packages'];
48
$packages_to_install = Array();
49
$static_output = "";
50
$static_status = "";
51
$sendto = "output";
52

    
53
?>
54
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
55
<html>
56
<head>
57
<title><?=gentitle("System: Package Manager: Install Package");?></title>
58
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
59
<link href="gui.css" rel="stylesheet" type="text/css">
60
</head>
61

    
62
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
63
<?php
64
include("fbegin.inc");
65
?>
66
<p class="pgtitle">System: Package Manager: Install Package</p>
67
<form action="pkg_mgr_install.php" method="post">
68

    
69
<?php if ($savemsg) print_info_box($savemsg); ?>
70
<?php
71

    
72
if($_GET['showlog'] <> "") {
73
            echo "<table>";
74
            echo "<tr><td>";
75
            echo "<pre>";
76
            // reopen and read log in
77
            $fd = fopen("{$g['tmp_path']}/pkg_mgr.log", "r");
78
            $tmp = "";
79
            while(!feof($fd)) {
80
                        $tmp .= fread($fd,49);
81
            }
82
            fclose($fd);
83
            echo $tmp;
84
            echo "</pre>";
85
            echo "</td></tr>";
86
            echo "</table>";
87
            exit;
88
}
89

    
90
/*
91
 *   open logging facility
92
 */
93
$fd_log = fopen("{$g['tmp_path']}/pkg_mgr.log", "w");
94
if(!$fd_log) log_error("Warning, could not open {$g['tmp_path']}/pkg_mgr.log for writing");
95
fwrite($fd_log, "Begin of Package Manager installation session.\n");
96

    
97
fetch_latest_pkg_config();
98

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

    
101
if(!$pkg_config['packages'])
102
    print_info_box_np("Could not find any packages in pkg_config.xml");
103

    
104
?>
105
<table width="100%" border="0" cellpadding="0" cellspacing="0">  <tr><td>
106
  <ul id="tabnav">
107
    <li class="tabinact"><a href="pkg_mgr.php">Available Packages</a></li>
108
    <li class="tabact">Installed Packages</a></li>
109
  </ul>
110
  </td></tr>
111
  <tr>
112
    <td class="tabcont">
113
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
114
               <tr>
115
                 <td>
116
                     <!-- progress bar -->
117
                     <center>
118
                     <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>
119
                     <br>
120
	             <!-- status box -->
121
                     <textarea cols="60" rows="1" name="status" id="status" wrap="hard">Beginning package installation.</textarea>
122
                     <!-- command output box -->
123
	             <textarea cols="60" rows="25" name="output" id="output" wrap="hard"></textarea>
124
                     </center>
125
                 </td>
126
               </tr>
127
        </table>
128
    </td>
129
  </tr>
130
</table>
131
</form>
132
<?php include("fend.inc"); ?>
133
</body>
134
</html>
135

    
136
<?php
137

    
138

    
139

    
140
if($_GET['mode'] == "reinstallall") {
141
    /*
142
     *  Loop through installed packages and if name matches
143
     *  push the package id onto array to reinstall
144
     */
145
    $instpkgs = array();
146
    $output_static = "";
147
    $counter = 0;
148
    if(is_array($pkg_config['packages']['package']))
149
		if(!is_string($instpkgs[0])) foreach($config['installedpackages']['package'] as $index => $instpkg) $instpkgs[] = $instpkg['name'];
150
                foreach($pkg_config['packages']['package'] as $index => $available_package) {
151
                        if(in_array($available_package['name'], $instpkgs)) {
152
				$packages_to_install[] = $index;
153
                                $output_static .= "Adding " . $package['name'] . " to installation array.\n";
154
                                update_output_window($output_static);
155
                                fwrite($fd_log, "Adding (" . $counter . ") " . $package['name'] . " to package installation array.\n" . $status);
156
                        }
157
                }
158
} else {
159
            /*
160
             * Push the desired package id onto the install packages array
161
             */
162
            fwrite($fd_log, "Single package installation started.\n");
163
            $packages_to_install[] =  $_GET['id'];
164
}
165

    
166
/*
167
 *  Loop through packages_to_install, installing needed packages
168
 */
169
foreach ($packages_to_install as $id) {
170

    
171
    fwrite($fd_log, "Begin package ${id}\n");
172

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

    
175
    /*
176
     * install the package
177
     */
178

    
179
    // Ensure directories are in place for pkg_add.
180
    safe_mkdir("{$g['www_path']}/ext/Services", 0755);
181
    safe_mkdir("{$g['www_path']}/ext/System", 0755);
182
    safe_mkdir("{$g['www_path']}/ext/Interfaces", 0755);
183
    safe_mkdir("{$g['www_path']}/ext/Firewall", 0755);
184
    safe_mkdir("{$g['www_path']}/ext/VPN", 0755);
185
    safe_mkdir("{$g['www_path']}/ext/Status", 0755);
186
    safe_mkdir("{$g['www_path']}/ext/Diagnostics", 0755);
187
    safe_mkdir("/usr/local/pkg", 0755);
188

    
189
    $a_out = &$pkg_config['packages']['package'];
190

    
191
    if($pkg_config['packages']['package'][$id]['verifyinstalledpkg'] <> "")
192
        $package_to_verify = $pkg_config['packages']['package'][$id]['verifyinstalledpkg'];
193
    else
194
        $package_to_verify = $pkg_config['packages']['package'][$id]['name'];
195

    
196
    $pkgent = array();
197
    $pkgent['name'] = $pkg_config['packages']['package'][$id]['name'];
198
    $pkgent['descr'] = $pkg_config['packages']['package'][$id]['descr'];
199
    $pkgent['category'] = $pkg_config['packages']['package'][$id]['category'];
200

    
201
    $pkgent['version'] = $pkg_config['packages']['package'][$id]['version'];
202

    
203
    $pkgent['depends_on_package'] = $a_out[$id]['depends_on_package'];
204
    $pkgent['depends_on_package_base_url'] = $a_out[$id]['depends_on_package_base_url'];
205
    $pkgent['pfsense_package'] = $a_out[$id]['pfsense_package'];
206
    $pkgent['pfsense_package_base_url'] = $a_out[$id]['pfsense_package_base_url'];
207
    $pkgent['configurationfile'] = $a_out[$id]['configurationfile'];
208
    if($pkg_config['packages']['package'][$id]['logging']) {
209
        // logging facilities.
210
        $pkgent['logging']['facility'] = $pkg_config['packages']['package'][$id]['logging']['facility'];
211
        $pkgent['logging']['logfile_name'] = $pkg_config['packages']['package'][$id]['logging']['logfile_name'];
212
        mwexec("/usr/sbin/clog -i -s 32768 {$g['varlog_path']}" . $pkgent['logging']['logfile_name']);
213
        chmod($g['varlog_path'] . $pkgent['logging']['logfile_name'], 0600);
214
        fwrite($fd_log, "Adding text to file /etc/syslog.conf\n");
215
        add_text_to_file("/etc/syslog.conf", $pkgent['logging']['facilityname'] . "\t\t\t" . $pkgent['logging']['logfilename']);
216
        mwexec("/usr/bin/killall -HUP syslogd");
217
    }
218
    $a_out = &$config['packages']['package']; // save item to installedpkgs
219
    fwrite($fd_log, "Begining (" . $id. ") " . $pkgent['name'] . " package installation.\n" . $status);
220

    
221
    log_error("Begining (" . $id. ") " . $pkgent['name'] . " package installation.");
222
    $static_status = "Beginning package installation for " . $pkgent['name'] . "...";
223
    update_status($static_status);
224

    
225
    if($pkg_config['packages']['package'][$id]['config_file'] <> "") {
226
        $static_output .= "Downloading package configuration file... ";
227
	download_file_with_progress_bar($pkg_config['packages']['package'][$id]['config_file'], "/usr/local/pkg/" . substr(strrchr($pkg_config['packages']['package'][$id]['config_file'], '/'), 1));
228
        fwrite($fd_log, "Downloading configuration file " . $pkg_config['packages']['package'][$id]['config_file'] . "...\n");
229
        if(!file_exists("/usr/local/pkg/" . substr(strrchr($pkg_config['packages']['package'][$id]['config_file'], '/'), 1))) {
230
            fwrite($fd_log, "ERROR!  Could not fetch " . $pkg_config['packages']['package'][$id]['config_file']);
231
            $static_output .= "failed!\n\nInstallation aborted.";
232
            update_output_window($static_output);
233
            echo "<br>Show <a href=\"pkg_mgr_install.php?showlog=true\">install log</a></center>";
234
            exit;
235
        }
236
	$static_output .= "done.\n";
237
	update_output_window($static_output);
238
    }
239

    
240
    update_status("Downloading and installing " . $pkgent['name'] . " and its dependencies.");
241
    fwrite($fd_log, "Downloading and installing " . $pkgent['name'] . " - " . $pkgent['pfsense_package'] . "...\n");
242

    
243
    /*
244
     * Open a /tmp/y file which will basically tell the
245
     * pkg_delete script to delete users and such if it asks.
246
     */
247
    $fd = fopen("{$g['tmp_path']}/y", "w");
248
    fwrite($fd, "y\n");
249
    fwrite($fd, "y\n");
250
    fwrite($fd, "y\n");
251
    fwrite($fd, "y\n");
252
    fwrite($fd, "y\n");
253
    fwrite($fd, "y\n");
254
    fwrite($fd, "y\n");
255
    fwrite($fd, "y\n");
256
    fwrite($fd, "y\n");
257
    fwrite($fd, "y\n");
258
    fwrite($fd, "y\n");
259
    fwrite($fd, "y\n");
260
    fwrite($fd, "y\n");
261
    fwrite($fd, "y\n");
262
    fwrite($fd, "y\n");
263
    fclose($fd);
264

    
265
    /* This directive is not yet used.
266
    if ($pkgent['pfsense_package_base_url'] <> "") {
267
        fwrite($fd_log, "Executing: cd {$g['tmp_path']}/ && /usr/sbin/pkg_add -r " . $pkgent['pfsense_package_base_url'] . "/" . $pkgent['pfsense_package'] . "\n" . $text);
268
        $text = exec_command_and_return_text("cd {$g['tmp_path']}/ && cat {$g['tmp_path']}/y | /usr/sbin/pkg_add -r " . $pkgent['pfsense_package_base_url'] . "/" . $pkgent['pfsense_package']);
269
        update_output_window($text);
270
    }
271
    */
272

    
273
    if ($pkgent['depends_on_package_base_url'] <> "") {
274
                update_status("Installing " . $pkgent['name'] . " and its dependencies.");
275
                $static_output .= "Downloading " . $pkgent['name'] . " and its dependencies... ";
276
		$static_orig = $static_output;
277
		$static_output .= "\n";
278
		update_output_window($static_output);
279
		exec("ls /var/db/pkg", $is_installed);
280
                $pkg_installed = false;
281
                foreach($is_installed as $is_inst) {
282
                	if($is_inst == $pkgent['name'] . '-' . $pkgent['version']) {
283
                		$pkg_installed = true;
284
                		break;
285
                	}
286
                }
287
		if($pkg_installed == false) pkg_fetch_recursive($pkgent['name'] . "-" . $pkgent['version'], $pkgent['depends_on_package'], 0, $pkgent['depends_on_package_base_url']);
288
		$static_output = $static_orig . "done.\n";
289
		update_output_window($static_output);
290
    }
291

    
292
    if ($pkgent['depends_on_package_base_url'] <> "" or $pkgent['pfsense_package_base_url'] <> "") {
293
        $static_output .= "Checking for successful package installation... ";
294
	update_output_window($static_output);
295
        if($pkg_installed == false) {
296
		exec("ls /var/db/pkg", $is_installed);
297
		foreach($is_installed as $is_inst) {
298
                         if($is_inst == $pkgent['name'] . '-' . $pkgent['version']) {
299
                                 $pkg_installed = true;
300
                                 break;
301
                         }
302
                 }
303
	}
304
        fwrite($fd_log, $is_installed[0] . "\n");
305
        if($pkg_installed == true) {
306
                    $static_output .= "done.\n";
307
		    update_output_window($static_output);
308
                    fwrite($fd_log, "pkg_add successfully completed.\n");
309
        } else {
310
		    $static_output .= "failed!\n\nInstallation aborted.";
311
		    update_output_window($static_output);
312
                    fwrite($fd_log, "Package WAS NOT installed properly.\n");
313
                    fclose($fd_log);
314
                    echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
315
                    echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
316
                    sleep(1);
317
                    die;
318
        }
319
    }
320

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

    
323
    $pkgid = get_pkg_id($pkgent['name']);
324

    
325
    if(!$_GET['mode'] == "reinstallall") {
326
	$static_output .= "Saving updated package information... ";
327
	update_output_window($static_output);
328
	if($pkgid == -1) {
329
		$config['installedpackages']['package'][] = $pkgent;
330
		$changedesc = "Installed {$pkgent['name']} package.";
331
		$to_output = "done.\n";
332
	} else {
333
		$config['installedpackages']['package'][$pkgid] = $pkgent;
334
		$changedesc = "Overwrote previous installation of {$pkgent['name']}.";
335
		$to_output = "overwrite!\n";
336
	}
337
        write_config($changedesc);
338
        conf_mount_rw();
339
	$static_output .= $to_output;
340
	update_output_window($static_output);
341
    }
342

    
343
    $name = $pkgent['name'];
344

    
345
    update_status("Finishing package installation...");
346

    
347
    /*
348
     * parse the config file for this package and install neededtext items.
349
     *
350
     */
351
    if(file_exists("/usr/local/pkg/" . $pkgent['name'] . ".xml")) {
352
                $package_conf = parse_xml_config_pkg("/usr/local/pkg/" . $pkgent['name'] . ".xml", "packagegui");
353
                if($package_conf['modify_system']['item'] <> "") {
354
		    $static_output .= "Modifying system files... ";
355
		    update_output_window($static_output);
356
                    foreach ($package_conf['modify_system']['item'] as $ms) {
357
                        if($ms['textneeded']) {
358
                            add_text_to_file($ms['modifyfilename'],$ms['textneeded']);
359
                        }
360
                    }
361
		    $static_output .= "done.\n";
362
		    update_output_window($static_output);
363
                }
364

    
365
                /*
366
                 * fetch additional files needed for package if defined
367
                 * and uncompress if needed.
368
                 */
369
                if ($package_conf['additional_files_needed'] <> "") {
370
		    $static_output .= "Downloading additional files needed for " . $pkgent['name'] . "... ";
371
                    update_output_window($static_output);
372
		    $i = 0;
373
		    $afn_count = count($package_conf['additional_files_needed']) -1;
374
                    foreach($package_conf['additional_files_needed'] as $afn) {
375
                        $filename = get_filename_from_url($afn['item'][0]);
376
                        fwrite($fd_log, "Downloading additional files needed for package " . $filename . "...\n");
377
			if ($i == $afn_count) {
378
				$static_orig = $static_output . $filename . ".\n";
379
			} else {
380
				$static_orig = $static_output . $filename . ", ";
381
			}
382
			$static_output .= $filename . " ";
383
			update_output_window($static_output);
384
                        $prefix = "/usr/local/pkg/";
385
                        $pkg_chmod = "";
386
                        if($afn['chmod'] <> "")
387
                            $pkg_chmod = $afn['chmod'];
388
                        if($afn['prefix'] <> "")
389
                            $prefix = $afn['prefix'];
390
                        download_file_with_progress_bar($afn['item'][0], $prefix . $filename);
391
			$static_output = $static_orig;
392
			update_output_window($static_output);
393
                        if(stristr($filename, ".tgz") <> "") {
394
                            update_status("Extracting tgz archive to -C for " . $filename);
395
                            fwrite($fd_log, "Extracting tgz archive to -C for " . $filename . " ...\n");
396
                            system("/usr/bin/tar xzvf " . $prefix . $filename . " -C / >/dev/null 2>&1");
397
                        }
398
                        if($pkg_chmod <> "") {
399
                            fwrite($fd_log, "Changing file mode for {$pkg_chmod} {$prefix}{$filename}\n");
400
                            chmod($prefix . $filename, $pkg_chmod);
401
                            system("/bin/chmod {$pkg_chmod} {$prefix}{$filename}");
402
                        }
403
		        $i++;
404
                    }
405
                }
406

    
407
                /*
408
                 * loop through menu installation items
409
                 * installing multiple items if need be.
410
                */
411
                if(is_array($package_conf['menu'])) {
412
		    $static_output .= "Installing menu items... ";
413
		    update_output_window($static_output);
414
                    foreach ($package_conf['menu'] as $menu) {
415
                        // install menu item into the ext folder
416
                        fwrite($fd_log, "Adding menu option to " . $menu['section'] . "/" . $menu['name'] . "\n");
417
                        $fd = fopen("{$g['www_path']}/ext/" . $menu['section'] . "/" . $menu['name'] , "w");
418
                        if($menu['url'] <> "") {
419
                                    // override $myurl for script.
420
                                    $toeval = "\$myurl = \"" . getenv("HTTP_HOST") . "\"; \n";
421
                                    $error_message = "";
422
                                    if(php_check_syntax($toeval, $error_message) == false)
423
                                        eval($toeval);
424
                                    // eval url so that above $myurl item can be processed if need be.
425
                                    $urltmp = $menu['url'];
426
                                    $toeval = "\$url = \"" . $urltmp . "\"; \n";
427
                                    if(php_check_syntax($toeval, $error_message) == false)
428
                                        eval($toeval);
429
                                    fwrite($fd, $url . "\n");
430
                        } else {
431
                                    $xml = "";
432
                                    if(stristr($menu['configfile'],".xml") == "") $xml = ".xml";
433
                                    fwrite($fd, "/pkg.php?xml=" . $menu['configfile'] . $xml . "\n");
434
                        }
435
                        fclose($fd);
436
                    }
437
		    $static_output .= "done.\n";
438
		    update_output_window($static_output);
439
		}
440
    } else {
441
                update_output_window("WARNING! /usr/local/pkg/" . $pkgent['name'] . ".xml" . " does not exist!\n");
442
                fwrite($fd_log, "WARNING! /usr/local/pkg/" . $pkgent['name'] . ".xml" . " does not exist!\n");
443
    }
444
    fwrite($fd_log, "End of Package Manager installation session.\n");
445

    
446
    /* return dependency list to output later.
447
    $command = "TODELETE=`ls /var/db/pkg | grep " . $name . "` && /usr/sbin/pkg_info -r \$TODELETE | grep Dependency: | cut -d\" \" -f2";
448
    $dependencies = exec_command_and_return_text($command);
449
    if($dependencies == "")
450
        fwrite($fd_log, "Installed package " . $name);
451
    else
452
        fwrite($fd_log, "Installed package " . $name . " and the following dependencies:\n" . $dependencies);
453
    */
454

    
455
    if($package_conf['custom_php_install_command'] <> "") {
456
	$static_output .= "Executing post install commands... ";
457
	update_output_window($static_output);
458
	if($package_conf['custom_php_global_functions'] <> "")
459
	    if(php_check_syntax($package_conf['custom_php_global_functions'], $error_message) == false)
460
		eval($package_conf['custom_php_global_functions']);
461
        fwrite($fd_log, "Executing post install commands...\n");
462
        $error_message = "";
463
        if($package_conf['custom_php_command_before_form'] <> "")
464
            if(php_check_syntax($package_conf['custom_php_command_before_form'], $error_message) == false)
465
                eval($package_conf['custom_php_command_before_form']);
466
        if(php_check_syntax($package_conf['custom_php_install_command'], $error_message) == false)
467
            eval($package_conf['custom_php_install_command']);
468
	$static_output .= "done.\n";
469
	update_output_window($static_output);
470
    }
471
    update_status("Package installation completed.");
472
    $static_output .= "\nPackage installation successful.";
473
    update_output_window($static_output);
474
    fwrite($fd_log, "Package installation completed.\n");
475
    log_error("Package " . $pkgent['name'] . " installation completed successfully.\n");
476
    fwrite($fd_log, "Status window output:\n" . $static_output);
477
}
478

    
479
// Delete all temporary package tarballs and staging areas.
480
unlink_if_exists("/tmp/apkg_*");
481
rmdir_recursive("/var/tmp/instmp*");
482

    
483

    
484
// close log
485
fclose($fd_log);
486

    
487
echo "<p><center>Installation completed.  Show <a href=\"pkg_mgr_install.php?showlog=true\">install log</a></center>";
488

    
489
echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
490

    
491
conf_mount_ro();
492

    
493
?>
(60-60/112)