Project

General

Profile

Download (20.9 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
/* /usr/local/pkg/ is where xml package files are stored. */
35
if(!file_exists("/usr/local/pkg/")) mwexec("mkdir -p /usr/local/pkg/");
36
/* /usr/local/pkg/pf is where custom php hook packages live to alter the rules when needed */
37
if(!file_exists("/usr/local/pkg/pf")) mwexec("mkdir -p /usr/local/pkg/pf");
38
/* /usr/local/www/ext is where package links live for the left hand pane */
39
if(!file_exists("/usr/local/www/ext/")) mwexec("mkdir -p /usr/local/www/ext");
40

    
41
$pb_percent = 1;
42

    
43
$a_out = &$pkg_config['packages'];
44

    
45
$packages_to_install = Array();
46

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

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

    
63
<?php if ($savemsg) print_info_box($savemsg); ?>
64
<?php
65

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

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

    
91
fetch_latest_pkg_config();
92

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

    
95
if(!$pkg_config['packages'])
96
    print_info_box_np("Could not find any packages in pkg_config.xml");
97

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

    
130
<?php
131

    
132

    
133

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

    
158
/*
159
 *  Loop through packages_to_install, installing needed packages
160
 */
161
foreach ($packages_to_install as $id) {
162

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

    
165
    update_progress_bar($pb_percent);
166
    $pb_percent += 10;
167

    
168
    /*
169
     * install the package
170
     */
171

    
172
    // Ensure directories are in place for pkg_add.
173
    mwexec("mkdir {$g['www_path']}/ext/Services >/dev/null 2>&1");
174
    mwexec("mkdir {$g['www_path']}/ext/System >/dev/null 2>&1");
175
    mwexec("mkdir {$g['www_path']}/ext/Interfaces >/dev/null 2>&1");
176
    mwexec("mkdir {$g['www_path']}/ext/Firewall >/dev/null 2>&1");
177
    mwexec("mkdir {$g['www_path']}/ext/VPN >/dev/null 2>&1");
178
    mwexec("mkdir {$g['www_path']}/ext/Status >/dev/null 2>&1");
179
    mwexec("mkdir {$g['www_path']}/ext/Diagnostics >/dev/null 2>&1");
180
    mwexec("mkdir /usr/local/pkg >/dev/null 2>&1");
181

    
182
    update_progress_bar($pb_percent);
183
    $pb_percent += 10;
184

    
185
    $a_out = &$pkg_config['packages']['package'];
186

    
187
    if($pkg_config['packages']['package'][$id]['verifyinstalledpkg'] <> "")
188
        $package_to_verify = $pkg_config['packages']['package'][$id]['verifyinstalledpkg'];
189
    else
190
        $package_to_verify = $pkg_config['packages']['package'][$id]['name'];
191

    
192
    $pkgent = array();
193
    $pkgent['name'] = $pkg_config['packages']['package'][$id]['name'];
194
    $pkgent['descr'] = $pkg_config['packages']['package'][$id]['descr'];
195
    $pkgent['category'] = $pkg_config['packages']['package'][$id]['category'];
196

    
197
    $pkgent['version'] = $pkg_config['packages']['package'][$id]['version'];
198

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

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

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

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

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

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

    
245
    update_progress_bar($pb_percent);
246
    $pb_percent += 10;
247

    
248
    /*
249
     * Open a /tmp/y file which will basically tell the
250
     * pkg_delete script to delete users and such if it asks.
251
     */
252
    $fd = fopen("{$g['tmp_path']}/y", "w");
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
    fclose($fd);
259

    
260
    if ($pkgent['pfsense_package_base_url'] <> "") {
261
        fwrite($fd_log, "Executing: cd {$g['tmp_path']}/ && /usr/sbin/pkg_add -r " . $pkgent['pfsense_package_base_url'] . "/" . $pkgent['pfsense_package'] . "\n" . $text);
262
        $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']);
263
        update_output_window($text);
264
    }
265

    
266
    update_progress_bar($pb_percent);
267
    $pb_percent += 10;
268

    
269
    if ($pkgent['depends_on_package_base_url'] <> "") {
270
                update_status("Downloading and installing " . $pkgent['name'] . " and its dependencies ... This could take a moment ...");
271
                $text = exec_command_and_return_text("cd {$g['tmp_path']}/ && /usr/sbin/pkg_add -r " . $pkgent['depends_on_package_base_url'] . "/" . $pkgent['depends_on_package']);
272
                update_output_window($text);
273
                fwrite($fd_log, "cd {$g['tmp_path']}/ && /usr/sbin/pkg_add -r " . $pkgent['depends_on_package_base_url'] . "/" . $pkgent['depends_on_package'] . "\n" . $text);;
274
    }
275

    
276
    if ($pkgent['depends_on_package_base_url'] <> "" or $pkgent['pfsense_package_base_url'] <> "") {
277
        $status = exec_command_and_return_text("ls {$g['vardb_path']}/pkg | grep " . $package_to_verify);
278
        fwrite($fd_log, "ls {$g['vardb_path']}/pkg | grep " . $package_to_verify . "\n" . $status);
279
        if($status <> "") {
280
                    update_status("Package installed.  Lets finish up.");
281
                    fwrite($fd_log, "Package installed.  Lets finish up.\n");
282
        } else {
283
                    fwrite($fd_log, "Package WAS NOT installed properly.\n");
284
                    fclose($fd_log);
285
                    $filecontents = exec_command_and_return_text("cat " . $file);
286
                    update_progress_bar(100);
287
                    echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
288
                    echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
289
                    update_status("Package WAS NOT installed properly...Something went wrong..\n" . $filecontents);
290
                    update_output_window("Error during package installation.");
291
                    sleep(1);
292
                    die;
293
        }
294
    }
295

    
296
    update_progress_bar($pb_percent);
297
    $pb_percent += 10;
298

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

    
301
    update_progress_bar($pb_percent);
302
    $pb_percent += 10;
303

    
304
    $config['installedpackages']['package'][] = $pkgent;
305

    
306
    if (isset($id) && $a_out[$id])
307
            $a_out[$id] = $pkgent;
308
    else
309
            $a_out[] = $pkgent;
310

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

    
314
    if(!$_GET['mode'] == "reinstallall") {
315
        update_output_window("Saving updated package information ...");
316
        fwrite($fd_log, "Saving updated package information ...\n");
317
        write_config();
318
    }
319

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

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

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

    
328
    /*
329
     * parse the config file for this package and install neededtext items.
330
     *
331
    */
332
    if(file_exists("/usr/local/pkg/" . $pkgent['name'] . ".xml")) {
333
                $package_conf = parse_xml_config_pkg("/usr/local/pkg/" . $pkgent['name'] . ".xml", "packagegui");
334
                if($package_conf['modify_system']['item'] <> "") {
335
                    foreach ($package_conf['modify_system']['item'] as $ms) {
336
                        update_progress_bar($pb_percent);
337
                        $pb_percent += 10;
338
                        if($ms['textneeded']) {
339
                            add_text_to_file($ms['modifyfilename'],$ms['textneeded']);
340
                        }
341
                    }
342
                }
343

    
344
                /*
345
                 * fetch additional files needed for package if defined
346
                 * and uncompress if needed.
347
                 */
348
                if ($package_conf['additional_files_needed'] <> "") {
349
                    foreach($package_conf['additional_files_needed'] as $afn) {
350
                        update_progress_bar($pb_percent);
351
                        $pb_percent += 10;
352
                        $filename = get_filename_from_url($afn['item'][0]);
353
                        fwrite($fd_log, "Downloading additional files needed for package " . $filename . " ...\n");
354
                        update_status("Downloading additional files needed for package " . $filename . " ...\n");
355
                        $prefix = "/usr/local/pkg/";
356
                        $pkg_chmod = "";
357
                        if($afn['chmod'] <> "")
358
                            $pkg_chmod = $afn['chmod'];
359
                        if($afn['prefix'] <> "")
360
                            $prefix = $afn['prefix'];
361
                        system("cd {$prefix} && /usr/bin/fetch " .  $afn['item'][0] . " 2>/dev/null");
362
                        if(stristr($filename, ".tgz") <> "") {
363
                            update_status("Extracting tgz archive to -C for " . $filename);
364
                            fwrite($fd_log, "Extracting tgz archive to -C for " . $filename . " ...\n");
365
                            system("/usr/bin/tar xzvf /usr/local/pkg/" . $filename . " -C / >/dev/null 2>&1");
366
                        }
367
                        if($pkg_chmod <> "") {
368
                            fwrite($fd_log, "Changing file mode for {$pkg_chmod} {$prefix}{$filename}\n");
369
                            system("/bin/chmod {$pkg_chmod} {$prefix}{$filename}");
370
                            chmod($prefix . $filename, $pkg_chmod);
371
                        }
372
                    }
373
                }
374

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

    
409
    update_progress_bar($pb_percent);
410
    $pb_percent += 10;
411

    
412
    // return dependency list to output later.
413
    $command = "TODELETE=`ls /var/db/pkg | grep " . $name . "` && /usr/sbin/pkg_info -r \$TODELETE | grep Dependency: | cut -d\" \" -f2";
414
    $dependencies = exec_command_and_return_text($command);
415
    if($dependencies == "")
416
        fwrite($fd_log, "Installed package " . $name);
417
    else
418
        fwrite($fd_log, "Installed package " . $name . " and the following dependencies:\n" . $dependencies);
419

    
420
    update_progress_bar($pb_percent);
421
    $pb_percent += 10;
422

    
423
    if($package_conf['custom_php_install_command']) {
424
        update_status("Executing post install commands...\n");
425
        fwrite($fd_log, "Executing post install commands...\n");
426
        $error_message = "";
427
        if($package_conf['custom_php_command_before_form'] <> "")
428
            if(php_check_syntax($package_conf['custom_php_command_before_form'], $error_message) == false)
429
                eval($package_conf['custom_php_command_before_form']);
430
        $pb_percent += 50;
431
        update_progress_bar(50);
432
        if(php_check_syntax($package_conf['custom_php_install_command'], $error_message) == false)
433
            eval($package_conf['custom_php_install_command']);
434
    }
435

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

    
439
    if ($pkgent['depends_on_package_base_url'] <> "" or $pkgent['pfsense_package_base_url'] <> "") {
440
        $status = exec_command_and_return_text("ls {$g['vardb_path']}/pkg | grep " . $package_to_verify);
441
        fwrite($fd_log, "ls {$g['vardb_path']}/pkg | grep " . $package_to_verify . "\n" . $status);
442
        if($status <> "") {
443
                    update_status("Package installation completed.");
444
                    fwrite($fd_log, "Package installation completed.\n");
445
                    log_error("Package " . $pkgent['name'] . " installation completed okay.");
446
                    update_progress_bar(100);
447
        } else {
448
                    update_status("Package WAS NOT installed properly.");
449
                    update_output_window("Package WAS NOT installed properly.");
450
                    fwrite($fd_log, "Package WAS NOT installed properly.\n");
451
                    log_error("Package " . $pkgent['name'] . " did not install correctly.");
452
                    update_progress_bar(100);
453
        }
454
    } else {
455
        update_status("Package installation completed.");
456
        fwrite($fd_log, "Package installation completed.\n");
457
    }
458

    
459
    update_progress_bar(100);
460

    
461
}
462

    
463
// close log
464
fclose($fd_log);
465

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

    
468
echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
469
echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
470

    
471

    
472

    
473
?>
(59-59/106)