1
|
/****h* pfSense/pkg-utils
|
2
|
* NAME
|
3
|
* pkg-utils.inc - Package subsystem
|
4
|
* DESCRIPTION
|
5
|
* This file contains various functions used by the pfSense package system.
|
6
|
* HISTORY
|
7
|
* $Id$
|
8
|
******
|
9
|
*
|
10
|
* Copyright (C) 2005 Colin Smith (ethethlay@gmail.com)
|
11
|
* All rights reserved.
|
12
|
* Redistribution and use in source and binary forms, with or without
|
13
|
* modification, are permitted provided that the following conditions are met:
|
14
|
*
|
15
|
* 1. Redistributions of source code must retain the above copyright notice,
|
16
|
* this list of conditions and the following disclaimer.
|
17
|
*
|
18
|
* 2. Redistributions in binary form must reproduce the above copyright
|
19
|
* notice, this list of conditions and the following disclaimer in the
|
20
|
* documentation and/or other materials provided with the distribution.
|
21
|
*
|
22
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
23
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
24
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
25
|
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
26
|
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
27
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
28
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
29
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
30
|
* RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
31
|
* POSSIBILITY OF SUCH DAMAGE.
|
32
|
*
|
33
|
*/
|
34
|
|
35
|
require_once("xmlrpc.inc");
|
36
|
|
37
|
/* Initialize the package subsystem and set interface */
|
38
|
$valid_pkg_interfaces = array(
|
39
|
'web',
|
40
|
'console'
|
41
|
);
|
42
|
|
43
|
if((!isset($pkg_interface)) or (!in_array($pkg_interface, $valid_pkg_interfaces)))
|
44
|
$pkg_interface = "web";
|
45
|
|
46
|
/****f* pkg-utils/is_package_installed
|
47
|
* NAME
|
48
|
* is_package_installed - Check whether a package is installed.
|
49
|
* INPUTS
|
50
|
* $packagename - name of the package to check
|
51
|
* RESULT
|
52
|
* boolean - true if the package is installed, false otherwise
|
53
|
* NOTES
|
54
|
* This function is deprecated - get_pkg_id() can already check for installation.
|
55
|
******/
|
56
|
function is_package_installed($packagename) {
|
57
|
$pkg = get_pkg_id($packagename);
|
58
|
if($pkg == -1) return false;
|
59
|
return true;
|
60
|
}
|
61
|
|
62
|
/****f* pkg-utils/get_pkg_id
|
63
|
* NAME
|
64
|
* get_pkg_id - Find a package's numeric ID.
|
65
|
* INPUTS
|
66
|
* $pkg_name - name of the package to check
|
67
|
* RESULT
|
68
|
* integer - -1 if package is not found, >-1 otherwise
|
69
|
******/
|
70
|
function get_pkg_id($pkg_name) {
|
71
|
global $config;
|
72
|
global $pkg_config;
|
73
|
if(is_array($config['installedpackages']['package'])) {
|
74
|
$i = 0;
|
75
|
foreach($config['installedpackages']['package'] as $pkg) {
|
76
|
if($pkg['name'] == $pkg_name) return $i;
|
77
|
$i++;
|
78
|
}
|
79
|
}
|
80
|
return -1;
|
81
|
}
|
82
|
|
83
|
/****f* pkg-utils/get_pkg_info
|
84
|
* NAME
|
85
|
* get_pkg_info - Retrive package information from pfsense.com.
|
86
|
* INPUTS
|
87
|
* $pkgs - 'all' to retrive all packages, an array containing package names otherwise
|
88
|
* $info - 'all' to retrive all information, an array containing keys otherwise
|
89
|
* RESULT
|
90
|
* $raw_versions - Array containing retrieved information, indexed by package name.
|
91
|
******/
|
92
|
function get_pkg_info($pkgs = 'all', $info = 'all') {
|
93
|
$params = array("pkg" => $pkgs, "info" => $info);
|
94
|
$msg = new XML_RPC_Message('pfsense.get_pkgs', array(php_value_to_xmlrpc($params)));
|
95
|
$cli = new XML_RPC_Client($versioncheck_path, $versioncheck_base_url);
|
96
|
$resp = $cli->send($msg);
|
97
|
$raw_versions = $resp->value();
|
98
|
return xmlrpc_value_to_php($raw_versions);
|
99
|
}
|
100
|
|
101
|
/*
|
102
|
* resync_all_package_configs() Force packages to setup their configuration and rc.d files.
|
103
|
* This function may also print output to the terminal indicating progress.
|
104
|
*/
|
105
|
function resync_all_package_configs($show_message = false) {
|
106
|
global $config;
|
107
|
$i = 0;
|
108
|
log_error("Resyncing configuration for all packages.");
|
109
|
if(!$config['installedpackages']['package']) return;
|
110
|
if($show_message == true) print "Syncing packages:";
|
111
|
foreach($config['installedpackages']['package'] as $package) {
|
112
|
if($show_message == true) print " " . $package['name'];
|
113
|
sync_package($i, true, true);
|
114
|
$i++;
|
115
|
}
|
116
|
if($show_message == true) print ".\n";
|
117
|
}
|
118
|
|
119
|
/*
|
120
|
* get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", return_nosync = 1): Return a package's dependencies.
|
121
|
*
|
122
|
* $filetype = "all" || ".xml", ".tgz", etc.
|
123
|
* $format = "files" (full filenames) || "names" (stripped / parsed depend names)
|
124
|
* $return_nosync = 1 (return depends that have nosync set) | 0 (ignore packages with nosync)
|
125
|
*
|
126
|
*/
|
127
|
function get_pkg_depends($pkg_name, $filetype = ".xml", $format = "files", $return_nosync = 1) {
|
128
|
global $config;
|
129
|
if(!is_numeric($pkg_name)) {
|
130
|
$pkg_name = get_pkg_id($pkg_name);
|
131
|
if($pkg_id == -1) return -1; // This package doesn't really exist - exit the function.
|
132
|
} else {
|
133
|
if(!isset($config['installedpackages']['package'][$pkg_id])) return; // No package belongs to the pkg_id passed to this function.
|
134
|
}
|
135
|
$package = $config['installedpackages']['package'][$pkg_id];
|
136
|
print '$package done.';
|
137
|
if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) { // If the package's config file doesn't exist, log an error and fetch it.
|
138
|
log_error("Fetching missing configuration XML for " . $package['name']);
|
139
|
mwexec("/usr/bin/fetch -o /usr/local/pkg/" . $package['configurationfile'] . " http://www.pfsense.com/packages/config/" . $package['configurationfile']);
|
140
|
}
|
141
|
$pkg_xml = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
|
142
|
if($pkg_xml['additional_files_needed'] != "") {
|
143
|
foreach($pkg_xml['additional_files_needed'] as $item) {
|
144
|
if (($return_nosync == 0) && (isset($item['nosync']))) continue; // Do not return depends with nosync set if not required.
|
145
|
$depend_file = substr(strrchr($item['item']['0'], '/'),1); // Strip URLs down to filenames.
|
146
|
$depend_name = substr(substr($depend_file,0,strpos($depend_file,".")+1),0,-1); // Strip filename down to dependency name.
|
147
|
if (($filetype != "all") && (!preg_match("/${filetype}/i", $depend_file))) continue;
|
148
|
if ($item['prefix'] != "") {
|
149
|
$prefix = $item['prefix'];
|
150
|
} else {
|
151
|
$prefix = "/usr/local/pkg/";
|
152
|
}
|
153
|
if(!file_exists($prefix . $pkg_name)) {
|
154
|
log_error("Fetching missing dependency (" . $depend_name . ") for " . $pkg_name);
|
155
|
mwexec("/usr/local/bin/fetch -o " . $prefix . $depend_file . " " . $item['name']['0']);
|
156
|
if($item['chmod'] != "")
|
157
|
chmod($prefix . $depend_file, $item['chmod']); // Handle chmods.
|
158
|
}
|
159
|
switch ($format) {
|
160
|
case "files":
|
161
|
$depends[] = $depend_file;
|
162
|
break;
|
163
|
case "names":
|
164
|
switch ($filetype) {
|
165
|
case "all":
|
166
|
if(preg_match("/\.xml/i", $depend_file)) {
|
167
|
$depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
|
168
|
$depends[] = $depend_xml['name'];
|
169
|
break;
|
170
|
} else {
|
171
|
$depends[] = $depend_name; // If this dependency isn't package XML, use the stripped filename.
|
172
|
break;
|
173
|
}
|
174
|
case ".xml":
|
175
|
$depend_xml = parse_xml_config_pkg("/usr/local/pkg/" . $depend_file, "packagegui");
|
176
|
$depends[] = $depend_xml['name'];
|
177
|
break;
|
178
|
default:
|
179
|
$depends[] = $depend_name; // If we aren't looking for XML, use the stripped filename (it's all we have).
|
180
|
break;
|
181
|
}
|
182
|
}
|
183
|
}
|
184
|
return $depends;
|
185
|
}
|
186
|
}
|
187
|
|
188
|
/*
|
189
|
* sync_package($pkg_name, $sync_depends = true, $show_message = false) Force a package to setup its configuration and rc.d files.
|
190
|
*/
|
191
|
function sync_package($pkg_name, $sync_depends = true, $show_message = false) {
|
192
|
global $config;
|
193
|
|
194
|
if(!file_exists("/usr/local/pkg")) mwexec("/bin/mkdir -p /usr/local/pkg/pf");
|
195
|
if(!$config['installedpackages']['package']) return;
|
196
|
if(!is_numeric($pkg_name)) {
|
197
|
$pkg_id = get_pkg_id($pkg_name);
|
198
|
if($pkg_id == -1) return -1; // This package doesn't really exist - exit the function.
|
199
|
} else {
|
200
|
$pkg_id = $pkg_name;
|
201
|
if(!isset($config['installedpackages']['package'][$pkg_id]))
|
202
|
return; // No package belongs to the pkg_id passed to this function.
|
203
|
}
|
204
|
$package = $config['installedpackages']['package'][$pkg_id];
|
205
|
if(!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
|
206
|
//if($show_message == true) print "(f)"; Don't mess with this until the package system has settled.
|
207
|
log_error("Fetching missing configuration XML for " . $package['name']);
|
208
|
mwexec("/usr/bin/fetch -o /usr/local/pkg/" . $package['configurationfile'] . " http://www.pfsense.com/packages/config/" . $package['configurationfile']);
|
209
|
}
|
210
|
$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], "packagegui");
|
211
|
if(isset($pkg_config['nosync'])) continue;
|
212
|
//if($show_message == true) print "Syncing " . $pkg_name;
|
213
|
if($pkg['custom_php_global_functions'] <> "")
|
214
|
eval($pkg['custom_php_global_functions']);
|
215
|
if($pkg_config['custom_php_command_before_form'] <> "")
|
216
|
eval($pkg_config['custom_php_command_before_form']);
|
217
|
if($pkg_config['custom_php_resync_config_command'] <> "")
|
218
|
eval($pkg_config['custom_php_resync_config_command']);
|
219
|
if($sync_depends == true) {
|
220
|
$depends = get_pkg_depends($pkg_name, ".xml", "files", 1); // Call dependency handler and do a little more error checking.
|
221
|
if(is_array($depends)) {
|
222
|
foreach($depends as $item) {
|
223
|
$item_config = parse_xml_config_pkg("/usr/local/pkg/" . $item, "packagegui");
|
224
|
if(isset($item_config['nosync'])) continue;
|
225
|
if($item_config['custom_php_command_before_form'] <> "") {
|
226
|
eval($item_config['custom_php_command_before_form']);
|
227
|
print "Evaled dependency.";
|
228
|
}
|
229
|
if($item_config['custom_php_resync_config_command'] <> "") {
|
230
|
eval($item_config['custom_php_resync_config_command']);
|
231
|
print "Evaled dependency.";
|
232
|
}
|
233
|
if($show_message == true) print " " . $item_config['name'];
|
234
|
}
|
235
|
}
|
236
|
}
|
237
|
// if($show_message == true) print ".";
|
238
|
}
|
239
|
|
240
|
function pkg_fetch_recursive($pkgname, $filename, $dependlevel = 0, $base_url = 'http://ftp2.freebsd.org/pub/FreeBSD/ports/i386/packages-5.4-release/Latest') {
|
241
|
global $pkgent, $static_status, $static_output, $g, $pkg_interface, $fd_log;
|
242
|
$pkg_extension = strrchr($filename, '.');
|
243
|
$static_output .= "\n" . str_repeat(" ", $dependlevel * 2) . $pkgname . " ";
|
244
|
$fetchto = "/tmp/apkg_" . $pkgname . $pkg_extension;
|
245
|
switch($pkg_interface) {
|
246
|
default:
|
247
|
case "web":
|
248
|
download_file_with_progress_bar($base_url . "/" . $filename, $fetchto);
|
249
|
break;
|
250
|
case "console":
|
251
|
exec("/usr/bin/fetch -o {$fetchto} {$baseurl}/{$filename}");
|
252
|
break;
|
253
|
}
|
254
|
// update_output_window($static_output . "\n\n" . $pkg_progress);
|
255
|
exec("/usr/bin/tar -O -f {$fetchto} -x +CONTENTS", $slaveout);
|
256
|
$workingdir = preg_grep("/instmp/", $slaveout);
|
257
|
$workingdir = $workingdir[0];
|
258
|
$raw_depends_list = array_values(preg_grep("/\@pkgdep/", $slaveout));
|
259
|
if($raw_depends_list != "") {
|
260
|
if($pkgent['exclude_dependency'] != "")
|
261
|
$raw_depends_list = array_values(preg_grep($pkent['exclude_dependency'], PREG_GREP_INVERT));
|
262
|
foreach($raw_depends_list as $adepend) {
|
263
|
$working_depend = explode(" ", $adepend);
|
264
|
//$working_depend = explode("-", $working_depend[1]);
|
265
|
$depend_filename = $working_depend[1] . $pkg_extension;
|
266
|
exec("ls /var/db/pkg", $is_installed);
|
267
|
$pkg_installed = false;
|
268
|
foreach($is_installed as $is_inst) {
|
269
|
if($is_inst == $working_depend[1]) {
|
270
|
$pkg_installed = true;
|
271
|
break;
|
272
|
}
|
273
|
}
|
274
|
// $is_installed = array_values(preg_grep("/\b{$working_depend[0]}\b/i", $is_installed));
|
275
|
if($pkg_installed === false) {
|
276
|
pkg_fetch_recursive($working_depend[1], $depend_filename, $dependlevel + 1, $base_url);
|
277
|
} else {
|
278
|
$dependlevel++;
|
279
|
$static_output .= "\n" . str_repeat(" ", $dependlevel * 2) . $working_depend[1] . " ";
|
280
|
@fwrite($fd_log, $working_depend[1] . "\n");
|
281
|
}
|
282
|
}
|
283
|
}
|
284
|
exec("cat {$g['tmp_path']}/y | /usr/sbin/pkg_add -fv {$fetchto} 2>&1", $pkgaddout);
|
285
|
@fwrite($fd_log, $pkgname . " " . print_r($pkgaddout, true) . "\n");
|
286
|
return true;
|
287
|
}
|
288
|
|
289
|
function download_file_with_progress_bar($url_file, $destination_file) {
|
290
|
global $ch, $fout, $file_size, $downloaded, $counter, $pkg_interface;
|
291
|
$file_size = 1;
|
292
|
$downloaded = 1;
|
293
|
/* open destination file */
|
294
|
$fout = fopen($destination_file, "wb");
|
295
|
|
296
|
/*
|
297
|
Originally by Author: Keyvan Minoukadeh
|
298
|
Modified by Scott Ullrich to return Content-Length size
|
299
|
*/
|
300
|
|
301
|
$ch = curl_init();
|
302
|
curl_setopt($ch, CURLOPT_URL, $url_file);
|
303
|
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
|
304
|
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'read_body');
|
305
|
curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
|
306
|
|
307
|
curl_exec($ch);
|
308
|
fclose($fout);
|
309
|
curl_close($ch);
|
310
|
|
311
|
return 1;
|
312
|
}
|
313
|
|
314
|
function read_header($ch, $string) {
|
315
|
global $file_size, $ch, $fout;
|
316
|
$length = strlen($string);
|
317
|
ereg("(Content-Length:) (.*)", $string, $regs);
|
318
|
if($regs[2] <> "") {
|
319
|
$file_size = intval($regs[2]);
|
320
|
}
|
321
|
return $length;
|
322
|
}
|
323
|
|
324
|
function pkg_update_output($string) {
|
325
|
global $pkg_interface;
|
326
|
switch($pkg_interface) {
|
327
|
default:
|
328
|
case "web":
|
329
|
$toput = ereg_replace("\n", "\\n", $string);
|
330
|
echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $toput . "\";</script>";
|
331
|
break;
|
332
|
case "console":
|
333
|
print $string;
|
334
|
break;
|
335
|
}
|
336
|
return;
|
337
|
}
|
338
|
|
339
|
function pkg_update_status($string) {
|
340
|
global $pkg_interface;
|
341
|
switch($pkg_interface) {
|
342
|
default:
|
343
|
case "web":
|
344
|
echo "\n<script language=\"JavaScript\">document.forms[0].status.value=\"" . $string . "\";</script>";
|
345
|
break;
|
346
|
case "console":
|
347
|
print $string;
|
348
|
break;
|
349
|
}
|
350
|
return;
|
351
|
}
|
352
|
|
353
|
function read_body($ch, $string) {
|
354
|
global $fout, $file_size, $downloaded, $counter, $sendto, $static_output, $lastseen, $pkg_interface;
|
355
|
$length = strlen($string);
|
356
|
$downloaded += intval($length);
|
357
|
$downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
|
358
|
$downloadProgress = 100 - $downloadProgress;
|
359
|
if($lastseen <> $downloadProgress and $downloadProgress < 101) {
|
360
|
if($sendto == "status") {
|
361
|
$tostatus = $static_status . $downloadProgress . "%";
|
362
|
update_status($tostatus);
|
363
|
} else {
|
364
|
$tooutput = $static_output . $downloadProgress . "%";
|
365
|
update_output_window($tooutput);
|
366
|
}
|
367
|
update_progress_bar($downloadProgress);
|
368
|
$lastseen = $downloadProgress;
|
369
|
}
|
370
|
fwrite($fout, $string);
|
371
|
return $length;
|
372
|
}
|