Project

General

Profile

Download (21.2 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) 2004 Scott Ullrich
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
/* /usr/local/pkg/ is where xml package files are stored. */
38
make_dirs("/usr/local/pkg");
39
/* /usr/local/pkg/pf is where custom php hook packages live to alter the rules when needed */
40
make_dirs("/usr/local/pkg/pf");
41
/* /usr/local/www/ext is where package links live for the left hand pane */
42
make_dirs("/usr/local/www/ext");
43

    
44
$pb_percent = 1;
45

    
46
$a_out = &$pkg_config['packages'];
47

    
48
$packages_to_install = Array();
49

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

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

    
66
<?php if ($savemsg) print_info_box($savemsg); ?>
67
<?php
68

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

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

    
94
fetch_latest_pkg_config();
95

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

    
98
if(!$pkg_config['packages'])
99
    print_info_box_np("Could not find any packages in pkg_config.xml");
100

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

    
133
<?php
134

    
135

    
136

    
137
if($_GET['mode'] == "reinstallall") {
138
    /*
139
     *  Loop through installed packages and if name matches
140
     *  push the package id onto array to reinstall
141
     */
142
    $counter = 0;
143
    foreach($pkg_config['packages']['package'] as $available_package) {
144
        foreach($config['installedpackages']['package'] as $package) {
145
            if($package['name'] == $available_package['name']) {
146
                array_push($packages_to_install, $counter);
147
                update_status("Adding package " . $package['name']);
148
                fwrite($fd_log, "Adding (" . $counter . ") " . $package['name'] . " to package installation array.\n" . $status);
149
            }
150
        }
151
        $counter++;
152
    }
153
} else {
154
    /*
155
     * Push the desired package id onto the install packages array
156
     */
157
    fwrite($fd_log, "Single package installation started.\n");
158
    array_push($packages_to_install, $_GET['id']);
159
}
160

    
161
/*
162
 *  Loop through packages_to_install, installing needed packages
163
 */
164
foreach ($packages_to_install as $id) {
165

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

    
168
    update_progress_bar($pb_percent);
169
    $pb_percent += 10;
170

    
171
    /*
172
     * install the package
173
     */
174

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

    
185
    update_progress_bar($pb_percent);
186
    $pb_percent += 10;
187

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

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

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

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

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

    
220
    log_error("Begining (" . $id. ") " . $pkgent['name'] . " package installation.");
221

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

    
225
    fwrite($fd_log, "ls {$g['vardb_path']}/pkg | grep " . $package_to_verify . "\n" . $status);
226
    if($status <> "") {
227
                // package is already installed!?
228
                if(!$_GET['mode'] == "reinstallall")
229
                    print_info_box_np("NOTICE! " . $pkgent['name'] . " is already installed!  Installation will be registered.");
230
    }
231

    
232
    if($pkg_config['packages']['package'][$id]['config_file'] <> "") {
233
        update_status("Downloading configuration file.");
234
        fwrite($fd_log, "Downloading configuration file " . $pkg_config['packages']['package'][$id]['config_file'] . " ... \n");
235
        update_progress_bar($pb_percent);
236
        $pb_percent += 10;
237
        mwexec("cd /usr/local/pkg/ && fetch " . $pkg_config['packages']['package'][$id]['config_file']);
238
        if(!file_exists("/usr/local/pkg/" . $pkgent['name'] . ".xml")) {
239
            fwrite($fd_log, "ERROR!  Could not fetch " . $pkg_config['packages']['package'][$id]['config_file']);
240
            update_output_window("ERROR!  Could not fetch " . $pkg_config['packages']['package'][$id]['config_file'] . "\n");
241
            exit;
242
        }
243
    }
244

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

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

    
251
    /*
252
     * Open a /tmp/y file which will basically tell the
253
     * pkg_delete script to delete users and such if it asks.
254
     */
255
    $fd = fopen("{$g['tmp_path']}/y", "w");
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
    fwrite($fd, "y\n");
264
    fwrite($fd, "y\n");
265
    fwrite($fd, "y\n");
266
    fwrite($fd, "y\n");
267
    fwrite($fd, "y\n");
268
    fwrite($fd, "y\n");
269
    fwrite($fd, "y\n");
270
    fwrite($fd, "y\n");
271
    fclose($fd);
272

    
273
    if ($pkgent['pfsense_package_base_url'] <> "") {
274
        fwrite($fd_log, "Executing: cd {$g['tmp_path']}/ && /usr/sbin/pkg_add -r " . $pkgent['pfsense_package_base_url'] . "/" . $pkgent['pfsense_package'] . "\n" . $text);
275
        $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']);
276
        update_output_window($text);
277
    }
278

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

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

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

    
309
    update_progress_bar($pb_percent);
310
    $pb_percent += 10;
311

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

    
314
    update_progress_bar($pb_percent);
315
    $pb_percent += 10;
316

    
317
    $config['installedpackages']['package'][] = $pkgent;
318

    
319
    if (isset($id) && $a_out[$id])
320
            $a_out[$id] = $pkgent;
321
    else
322
            $a_out[] = $pkgent;
323

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

    
327
    if(!$_GET['mode'] == "reinstallall") {
328
        update_output_window("Saving updated package information ...");
329
        fwrite($fd_log, "Saving updated package information ...\n");
330
        write_config("Installed package {$pkgent['name']}");
331
        // remount rw after write_config() since it will mount ro.
332
        conf_mount_rw();
333
    }
334

    
335
    update_progress_bar($pb_percent);
336
    $pb_percent += 10;
337

    
338
    $name = $pkgent['name'];
339

    
340
    update_progress_bar($pb_percent);
341
    $pb_percent++;
342

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

    
359
                /*
360
                 * fetch additional files needed for package if defined
361
                 * and uncompress if needed.
362
                 */
363
                if ($package_conf['additional_files_needed'] <> "") {
364
                    foreach($package_conf['additional_files_needed'] as $afn) {
365
                        update_progress_bar($pb_percent);
366
                        $pb_percent += 10;
367
                        $filename = get_filename_from_url($afn['item'][0]);
368
                        fwrite($fd_log, "Downloading additional files needed for package " . $filename . " ...\n");
369
                        update_status("Downloading additional files needed for package " . $filename . " ...");
370
                        $prefix = "/usr/local/pkg/";
371
                        $pkg_chmod = "";
372
                        if($afn['chmod'] <> "")
373
                            $pkg_chmod = $afn['chmod'];
374
                        if($afn['prefix'] <> "")
375
                            $prefix = $afn['prefix'];
376
                        system("cd {$prefix} && /usr/bin/fetch " .  $afn['item'][0] . " 2>/dev/null");
377
                        if(stristr($filename, ".tgz") <> "") {
378
                            update_status("Extracting tgz archive to -C for " . $filename);
379
                            fwrite($fd_log, "Extracting tgz archive to -C for " . $filename . " ...\n");
380
                            system("/usr/bin/tar xzvf " . $prefix . $filename . " -C / >/dev/null 2>&1");
381
                        }
382
                        if($pkg_chmod <> "") {
383
                            fwrite($fd_log, "Changing file mode for {$pkg_chmod} {$prefix}{$filename}\n");
384
                            chmod($prefix . $filename, $pkg_chmod);
385
                            system("/bin/chmod {$pkg_chmod} {$prefix}{$filename}");
386
                        }
387
                    }
388
                }
389

    
390
                /*
391
                 * loop through menu installation items
392
                 * installing multiple items if need be.
393
                */
394
                if(is_array($package_conf['menu']))
395
                    foreach ($package_conf['menu'] as $menu) {
396
                        // install menu item into the ext folder
397
                        fwrite($fd_log, "Adding menu option to " . $menu['section'] . "/" . $menu['name'] . "\n");
398
                        $fd = fopen("{$g['www_path']}/ext/" . $menu['section'] . "/" . $menu['name'] , "w");
399
                        if($menu['url'] <> "") {
400
                                    // override $myurl for script.
401
                                    $toeval = "\$myurl = \"" . getenv("HTTP_HOST") . "\"; \n";
402
                                    $error_message = "";
403
                                    if(php_check_syntax($toeval, $error_message) == false)
404
                                        eval($toeval);
405
                                    // eval url so that above $myurl item can be processed if need be.
406
                                    $urltmp = $menu['url'];
407
                                    $toeval = "\$url = \"" . $urltmp . "\"; \n";
408
                                    if(php_check_syntax($toeval, $error_message) == false)
409
                                        eval($toeval);
410
                                    fwrite($fd, $url . "\n");
411
                        } else {
412
                                    $xml = "";
413
                                    if(stristr($menu['configfile'],".xml") == "") $xml = ".xml";
414
                                    fwrite($fd, "/pkg.php?xml=" . $menu['configfile'] . $xml . "\n");
415
                        }
416
                        fclose($fd);
417
                    }
418
    } else {
419
                update_output_window("WARNING! /usr/local/pkg/" . $pkgent['name'] . ".xml" . " does not exist!\n");
420
                fwrite($fd_log, "WARNING! /usr/local/pkg/" . $pkgent['name'] . ".xml" . " does not exist!\n");
421
    }
422
    fwrite($fd_log, "End of Package Manager installation session.\n");
423

    
424
    update_progress_bar($pb_percent);
425
    $pb_percent += 10;
426

    
427
    // return dependency list to output later.
428
    $command = "TODELETE=`ls /var/db/pkg | grep " . $name . "` && /usr/sbin/pkg_info -r \$TODELETE | grep Dependency: | cut -d\" \" -f2";
429
    $dependencies = exec_command_and_return_text($command);
430
    if($dependencies == "")
431
        fwrite($fd_log, "Installed package " . $name);
432
    else
433
        fwrite($fd_log, "Installed package " . $name . " and the following dependencies:\n" . $dependencies);
434

    
435
    update_progress_bar($pb_percent);
436
    $pb_percent += 10;
437

    
438
    if($package_conf['custom_php_install_command'] <> "") {
439
	if($package_conf['custom_php_global_functions'] <> "")
440
	    if(php_check_syntax($package_conf['custom_php_global_functions'], $error_message) == false)
441
		eval($package_conf['custom_php_global_functions']);
442
        update_status("Executing post install commands...");
443
        fwrite($fd_log, "Executing post install commands...\n");
444
        $error_message = "";
445
        if($package_conf['custom_php_command_before_form'] <> "")
446
            if(php_check_syntax($package_conf['custom_php_command_before_form'], $error_message) == false)
447
                eval($package_conf['custom_php_command_before_form']);
448
        $pb_percent += 50;
449
        update_progress_bar(50);
450
        if(php_check_syntax($package_conf['custom_php_install_command'], $error_message) == false)
451
            eval($package_conf['custom_php_install_command']);
452
    }
453

    
454
    $pb_percent += 10;
455
    update_progress_bar($pb_percent);
456

    
457
    if ($pkgent['depends_on_package_base_url'] <> "" or $pkgent['pfsense_package_base_url'] <> "") {
458
        $status = exec_command_and_return_text("ls {$g['vardb_path']}/pkg | grep " . $package_to_verify);
459
        fwrite($fd_log, "ls {$g['vardb_path']}/pkg | grep " . $package_to_verify . "\n" . $status);
460
        if($status <> "") {
461
                    update_status("Package installation completed.");
462
                    fwrite($fd_log, "Package installation completed.\n");
463
                    log_error("Package " . $pkgent['name'] . " installation completed okay.");
464
                    update_progress_bar(100);
465
        } else {
466
                    update_status("Package WAS NOT installed properly.");
467
                    update_output_window("Package WAS NOT installed properly.");
468
                    fwrite($fd_log, "Package WAS NOT installed properly.\n");
469
                    log_error("Package " . $pkgent['name'] . " did not install correctly.");
470
                    update_progress_bar(100);
471
        }
472
    } else {
473
        update_status("Package installation completed.");
474
        fwrite($fd_log, "Package installation completed.\n");
475
    }
476

    
477
    update_progress_bar(100);
478

    
479
}
480

    
481
// close log
482
fclose($fd_log);
483

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

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

    
488
conf_mount_ro();
489

    
490
?>
(60-60/111)