Project

General

Profile

Download (11.9 KB) Statistics
| Branch: | Tag: | Revision:
1 ee11cc6e Scott Ullrich
#!/usr/local/bin/php
2
<?php
3
/*
4
    pkg_mgr_install.php
5
    Copyright (C) 2004 Scott Ullrich
6
    All rights reserved.
7
8
    Redistribution and use in source and binary forms, with or without
9
    modification, are permitted provided that the following conditions are met:
10
11
    1. Redistributions of source code must retain the above copyright notice,
12
       this list of conditions and the following disclaimer.
13
14
    2. Redistributions in binary form must reproduce the above copyright
15
       notice, this list of conditions and the following disclaimer in the
16
       documentation and/or other materials provided with the distribution.
17
18
    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
    POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
require("guiconfig.inc");
31
require("xmlparse_pkg.inc");
32
33 55e50bd6 Scott Ullrich
/*
34
 *   open logging facility
35
 */
36 1a05a4ee Scott Ullrich
$fd_log = fopen("/tmp/pkg_mgr.log", "w");
37
fwrite($fd_log, "Begin of Package Manager installation session.\n");
38
39 55e50bd6 Scott Ullrich
/*
40
 *   update_output_window: update top textarea dynamically.
41
 */
42 ee11cc6e Scott Ullrich
function update_status($status) {
43
            echo "\n<script language=\"JavaScript\">document.forms[0].status.value=\"" . $status . "\";</script>";
44
}
45
46 55e50bd6 Scott Ullrich
/*
47
 *   update_output_window: update bottom textarea dynamically.
48
 */
49
function update_output_window($text) {
50
            $log = ereg_replace("\n", "\\n", $text);
51
            echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
52
}
53
54
/*
55
 *   get_dir: return an array of $dir
56
 */
57
function get_dir($dir) {
58
            $dir_array = array();
59
            $d = dir($dir);
60
            while (false !== ($entry = $d->read())) {
61
                        array_push($dir_array, $entry);
62
            }
63
            $d->close();
64
            return $dir_array;
65
}
66
67 c6c150e9 Scott Ullrich
/*
68
 *   exec_command_and_return_text_array: execute command and return output
69
 */
70
function exec_command_and_return_text_array($command) {
71
            $counter = 0;
72
            $fd = popen($command . " 2>&1 ", "r");
73
            while(!feof($fd)) {
74
                        $tmp .= fread($fd,49);
75
            }
76
            fclose($fd);
77
            $temp_array = split("\n", $tmp);
78
            return $tmp_array;
79
}
80
81 55e50bd6 Scott Ullrich
/*
82
 *   exec_command_and_return_text: execute command and return output
83
 */
84 1a05a4ee Scott Ullrich
function exec_command_and_return_text($command) {
85
            $counter = 0;
86
            $tmp = "";
87
            $fd = popen($command . " 2>&1 ", "r");
88
            while(!feof($fd)) {
89 55e50bd6 Scott Ullrich
                        $tmp .= fread($fd,49);
90 1a05a4ee Scott Ullrich
            }
91
            fclose($fd);
92
            return $tmp;
93
}
94
95 55e50bd6 Scott Ullrich
/*
96
 *   exec_command_and_return_text: execute command and update output window dynamically
97
 */
98 ee11cc6e Scott Ullrich
function execute_command_return_output($command) {
99 1a05a4ee Scott Ullrich
    global $fd_log;
100 ee11cc6e Scott Ullrich
    $fd = popen($command . " 2>&1 ", "r");
101
    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
102
    $counter = 0;
103
    $counter2 = 0;
104
    while(!feof($fd)) {
105 1a05a4ee Scott Ullrich
	$tmp = fread($fd, 50);
106
        fwrite($fd_log, $tmp);
107 ee11cc6e Scott Ullrich
	$tmp1 = ereg_replace("\n","\\n", $tmp);
108
	$text = ereg_replace("\"","'", $tmp1);
109
	if($lasttext == "..") {
110
	    $text = "";
111
	    $lasttext = "";
112
	    $counter=$counter-2;
113
	} else {
114
	    $lasttext .= $text;
115
	}
116
	if($counter > 51) {
117
	    $counter = 0;
118
	    $extrabreak = "\\n";
119
	} else {
120
	    $extrabreak = "";
121
	    $counter++;
122
	}
123
	if($counter2 > 600) {
124
	    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
125
	    $counter2 = 0;
126
	} else
127
	    $counter2++;
128
	echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = this.document.forms[0].output.value + \"" . $text . $extrabreak .  "\"; f('output'); </script>";
129
    }
130
    fclose($fd);
131
}
132
133
$a_out = &$pkg_config['packages'];
134
135
?>
136
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
137
<html>
138
<head>
139
<title><?=gentitle("System: Package Manager: Install Package");?></title>
140
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
141
<link href="gui.css" rel="stylesheet" type="text/css">
142
</head>
143
144
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
145
<?php include("fbegin.inc"); ?>
146
<p class="pgtitle">System: Package Manager: Install Package</p>
147
<form action="firewall_nat_out_load_balancing.php" method="post">
148
<?php if ($savemsg) print_info_box($savemsg); ?>
149
<?php if (file_exists($d_natconfdirty_path)): ?><p>
150
<?php print_info_box_np("The Package Manager configuration has been changed.<br>You must apply the changes in order for them to take effect.");?><br>
151
<input name="apply" type="submit" class="formbtn" id="apply" value="Apply changes"></p>
152
<?php endif; ?>
153
<?php
154
if(!file_exists("/tmp/pkg_config.xml")) {
155
            mwexec("cd {$g['tmp_path']} && /usr/bin/fetch \"http://www.pfsense.com/packages/pkg_config.xml\" >/dev/null 2>&1 ");
156
            if(!file_exists("{$g['tmp_path']}/pkg_config.xml")) {
157
                        print_info_box_np("Could not download pkg_config.xml from pfSense.com.  Check your DNS settings.");
158
                        die;
159
            }
160
}
161
162
$pkg_config = parse_xml_config("{$g['tmp_path']}/pkg_config.xml", "pfsensepkgs");
163
164
$id = $_GET['id'];
165
166
if(!$pkg_config['packages']) {
167
            print_info_box_np("Could not find any packages in pkg_config.xml");
168
}
169
?>
170
<table width="100%" border="0" cellpadding="0" cellspacing="0">  <tr><td>
171
  <ul id="tabnav">
172 55e50bd6 Scott Ullrich
    <li class="tabinact"><a href="pkg_mgr.php">Available Packages</a></li>
173
    <li class="tabact">Installed Packages</a></li>
174 ee11cc6e Scott Ullrich
  </ul>
175
  </td></tr>
176
  <tr>
177
    <td class="tabcont">
178
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
179
               <tr>
180
                 <td>
181 1a05a4ee Scott Ullrich
	             <textarea cols="100%" rows="1" name="status" id="status" wrap="hard">One moment please... This will take a while!</textarea>
182
	             <textarea cols="100%" rows="25" name="output" id="output" wrap="hard"></textarea>
183 ee11cc6e Scott Ullrich
                 </td>
184
               </tr>
185
        </table>
186
    </td>
187
  </tr>
188
</table>
189
</form>
190
<?php include("fend.inc"); ?>
191
</body>
192
</html>
193
194
<?
195
196
/* install the package */
197
198 c6c150e9 Scott Ullrich
// Ensure directories are in place for pkg_add.
199
mwexec("mkdir /usr/local/www/ext/Services >/dev/null 2>&1");
200
mwexec("mkdir /usr/local/www/ext/System >/dev/null 2>&1");
201
mwexec("mkdir /usr/local/www/ext/Interfaces >/dev/null 2>&1");
202
mwexec("mkdir /usr/local/www/ext/Firewall >/dev/null 2>&1");
203
mwexec("mkdir /usr/local/www/ext/VPN >/dev/null 2>&1");
204
mwexec("mkdir /usr/local/www/ext/Status >/dev/null 2>&1");
205
206 ee11cc6e Scott Ullrich
$a_out = &$pkg_config['packages']['package'];
207
$pkgent = array();
208
$pkgent['name'] = $pkg_config['packages']['package'][$id]['name'];
209
$pkgent['descr'] = $pkg_config['packages']['package'][$id]['descr'];
210
$pkgent['category'] = $pkg_config['packages']['package'][$id]['category'];
211
$pkgent['depends_on_package'] = $a_out[$id]['depends_on_package'];
212
$pkgent['depends_on_package_base'] = $a_out[$id]['depends_on_package_base'];
213
$pkgent['pfsense_package'] = $a_out[$id]['pfsense_package'];
214
$pkgent['pfsense_package_base'] = $a_out[$id]['pfsense_package_base'];
215 1a05a4ee Scott Ullrich
$pkgent['configurationfile'] = $a_out[$id]['configurationfile'];
216 ee11cc6e Scott Ullrich
$a_out = &$config['packages']['package'];
217
218 55e50bd6 Scott Ullrich
fwrite($fd_log, "ls /var/db/pkg | grep " . $pkgent['name'] . "\n" . $status);
219 1a05a4ee Scott Ullrich
if($status <> "") {
220
            // package is already installed!?
221
            print_info_box_np("NOTICE! " . $pkgent['name'] . " is already installed!  Installation will be registered.");
222
}
223 ee11cc6e Scott Ullrich
224 55e50bd6 Scott Ullrich
update_status("Downloading and installing " . $pkgent['name'] . " - " . $pkgent['pfsense_package'] . " and its dependencies ... This could take a moment ...");
225 1a05a4ee Scott Ullrich
fwrite($fd_log, "Downloading and installing " . $pkgent['name'] . " ... \n");
226 ee11cc6e Scott Ullrich
227 1a05a4ee Scott Ullrich
$text = exec_command_and_return_text("cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['pfsense_package_base'] . "/" . $pkgent['pfsense_package']);
228
update_output_window($text);
229 55e50bd6 Scott Ullrich
fwrite($fd_log, "Executing: cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['pfsense_package_base'] . "/" . $pkgent['pfsense_package'] . "\n" . $text);
230 ee11cc6e Scott Ullrich
231 1a05a4ee Scott Ullrich
if ($pkgent['pfsense_package_base']) {
232 55e50bd6 Scott Ullrich
            update_status("Downloading and installing " . $pkgent['name'] . " - " . $pkgent['depends_on_package_base'] . " and its dependencies ... This could take a moment ...");
233 1a05a4ee Scott Ullrich
            $text = exec_command_and_return_text("cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['depends_on_package_base'] . "/" . $pkgent['depends_on_package']);
234
            update_output_window($text);
235 55e50bd6 Scott Ullrich
            fwrite($fd_log, "cd /tmp/ && /usr/sbin/pkg_add -r " . $pkgent['depends_on_package_base'] . "/" . $pkgent['depends_on_package'] . "\n" . $text);;
236 1a05a4ee Scott Ullrich
}
237 ee11cc6e Scott Ullrich
238
$config = parse_xml_config("{$g['conf_path']}/config.xml", $g['xml_rootobj']);
239
240
$config['installedpackages']['package'][] = $pkgent;
241
242
if (isset($id) && $a_out[$id])
243
        $a_out[$id] = $pkgent;
244
else
245
        $a_out[] = $pkgent;
246
247
write_config();
248
249 55e50bd6 Scott Ullrich
$name = $pkgent['name'];
250
251
// parse the config file for this package and install neededtext items.
252
if(file_exists("/usr/local/pkg/" . $pkgent['name'] . ".xml")) {
253
            $config = parse_xml_config("/usr/local/pkg/" . $pkgent['name'] . ".xml", "packagegui");
254
            foreach ($config['modify_system']['item'] as $ms) {
255
                        if($ms['textneeded']) {
256
                                  fwrite($fd_log, "Adding needed text items:\n");
257
                                  $filecontents = exec_command_and_return_text("cat " . $ms['modifyfilename']);
258
                                  $text = ereg_replace($ms['textneeded'], "", $filecontents);
259
                                  $text .= $ms['textneeded'];
260
                                  fwrite($fd_log, $ms['textneeded'] . "\n");
261
                                  $fd = fopen($ms['modifyfilename'], "w");
262
                                  fwrite($fd, $text . "\n");
263
                                  fclose($fd);
264
                        }
265
            }
266
            // install menu item into the ext folder
267
            fwrite($fd_log, "Adding menu option to " . $config['menu']['section'] . "/" . $config['name'] . ":\n");
268
            $fd = fopen("/usr/local/www/ext/" . $config['menu']['section'] . "/" . $config['name'] , "w");
269
            fwrite($fd, "/usr/local/www/pkg.php?xml=" . $config['name'] . "\n");
270
            fclose($fd);
271
} else {
272
            update_output_window("WARNING! /usr/local/pkg/" . $pkgent['name'] . ".xml" . " does not exist!\n");
273
            fwrite($fd_log, "WARNING! /usr/local/pkg/" . $pkgent['name'] . ".xml" . " does not exist!\n");
274
}
275
fwrite($fd_log, "End of Package Manager installation session.\n");
276
277
// return dependency list to output later.
278
$command = "TODELETE=`ls /var/db/pkg | grep " . $name . "` && /usr/sbin/pkg_info -r \$TODELETE | grep Dependency: | cut -d\" \" -f2";
279
$dependencies = exec_command_and_return_text($command);
280
fwrite($fd_log, "Installed " . $name . " and the following depdencies:\n" . $dependencies);
281
282 1a05a4ee Scott Ullrich
$status = exec_command_and_return_text("ls /var/db/pkg | grep " . $pkgent['name']);
283 55e50bd6 Scott Ullrich
fwrite($fd_log, "ls /var/db/pkg | grep " . $pkgent['name'] . "\n" . $status);
284 1a05a4ee Scott Ullrich
if($status <> "") {
285
            update_status("Package installation completed.");
286
            fwrite($fd_log, "Package installation completed.\n");
287
} else {
288
            update_status("Package WAS NOT installed properly.");
289
            fwrite($fd_log, "Package WAS NOT installed properly.\n");
290
}
291
292 55e50bd6 Scott Ullrich
// close log
293 1a05a4ee Scott Ullrich
fclose($fd_log);
294
295 55e50bd6 Scott Ullrich
// reopen and read log in
296 1a05a4ee Scott Ullrich
$fd_log = fopen("/tmp/pkg_mgr.log", "r");
297
$tmp = "";
298
while(!feof($fd_log)) {
299
            $tmp .= fread($fd_log,49);
300
}
301 55e50bd6 Scott Ullrich
fclose($fd_log);
302 1a05a4ee Scott Ullrich
$log = ereg_replace("\n", "\\n", $tmp);
303
echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
304
305 ee11cc6e Scott Ullrich
?>
306
307
308
309
310
311
312
313