-
Notifications
You must be signed in to change notification settings - Fork 589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pfSense-pkg-tftpd package improvements #262
Changes from 14 commits
12330f3
e9c72f4
9859ff4
bc48698
57a8304
589160c
8ca4fd3
97b210f
d88217a
74ae3e1
92edca3
8b98d75
3c950c1
af4dac7
397fb5a
fe2af9c
a35a2b8
ea93c1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
* tftpd.inc | ||
* | ||
* part of pfSense (https://www.pfsense.org) | ||
* Copyright (c) 2015-2017 Rubicon Communications, LLC (Netgate) | ||
* Copyright (c) 2016 Stefan Seidel | ||
* All rights reserved. | ||
* | ||
|
@@ -19,25 +20,60 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
if (!function_exists("filter_configure")) { | ||
require_once("filter.inc"); | ||
} | ||
require_once("globals.inc"); | ||
require_once("interfaces.inc"); | ||
require_once("pfsense-utils.inc"); | ||
require_once("service-utils.inc"); | ||
require_once("util.inc"); | ||
|
||
/* Helper function for files listing */ | ||
function tftp_byte_convert($bytes) { | ||
if ($bytes <= 0) { | ||
return '0 Byte'; | ||
} | ||
$convention = 1000; | ||
$s = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB'); | ||
$e = floor(log($bytes, $convention)); | ||
return round($bytes/pow($convention, $e), 2) . ' ' . $s[$e]; | ||
} | ||
|
||
/* Create backup of the TFTP server directory */ | ||
function tftp_create_backup($trigger_download = false) { | ||
global $backup_dir, $backup_path, $files_dir; | ||
|
||
conf_mount_rw(); | ||
safe_mkdir("{$backup_dir}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to see a test that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. backup_dir is hardcoded to |
||
if (mwexec("/usr/bin/tar -czC / -f {$backup_path} {$files_dir}") || !file_exists("{$backup_path}")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable insertions here need to be changed out so they use |
||
header("Location: tftp_files.php?savemsg=Backup+failed.&result=alert-warning"); | ||
} elseif ($trigger_download == false) { | ||
header("Location: tftp_files.php?savemsg=Backup+has+been+created"); | ||
} | ||
conf_mount_ro(); | ||
} | ||
|
||
function install_package_tftpd() { | ||
safe_mkdir("/tftpboot"); | ||
if (is_array($config['installedpackages']['tftpd'])) { | ||
$tftpd_conf = &$config['installedpackages']['tftpd']['config'][0]; | ||
} else { | ||
$tftpd_conf = array(); | ||
} | ||
$datadir = ($tftpd_conf['datadir'] ?: '/tftpboot'); | ||
safe_mkdir("{$datadir}"); | ||
unlink_if_exists("/usr/local/etc/rc.d/tftpd"); | ||
} | ||
|
||
function deinstall_package_tftpd() { | ||
@rmdir("/tftpboot"); | ||
if (is_array($config['installedpackages']['tftpd'])) { | ||
$tftpd_conf = &$config['installedpackages']['tftpd']['config'][0]; | ||
} else { | ||
$tftpd_conf = array(); | ||
} | ||
$datadir = ($tftpd_conf['datadir'] ?: '/tftpboot'); | ||
// Will only get removed when empty | ||
@rmdir("{$datadir}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This definitely needs a sanity check to make sure the user didn't do something dumb like set it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As noted there, the only thing it removed are empty dirs. http://php.net/manual/en/function.rmdir.php There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True but if it's empty, why bother? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well to not leave lingering empty dir there (some people like to not leave cruft behind). Can remove it altogether of course, I have no preference here. |
||
} | ||
|
||
function sync_package_tftpd() { | ||
global $config; | ||
global $g, $config; | ||
|
||
conf_mount_rw(); | ||
|
||
|
@@ -56,17 +92,35 @@ function sync_package_tftpd() { | |
unlink_if_exists('/usr/local/etc/rc.d/tftpd.sh'); | ||
return; | ||
} | ||
|
||
|
||
// Root directory | ||
$datadir = $tftpd_conf['datadir']; | ||
|
||
if (!is_dir($datadir)) { | ||
log_error("TFTP files directory {$datadir} does not exist."); | ||
return; | ||
} | ||
|
||
// TFTP Server Bind IP | ||
if (!empty($tftpd_conf['tftpd_ip'])) { | ||
$address = $tftpd_conf['tftpd_ip']; | ||
if (is_ipaddrv6($address)) { | ||
$address = "-a [{$address}]"; | ||
} else { | ||
$address = "-a {$address}"; | ||
} | ||
} | ||
|
||
$pidfile = "{$g['varrun_path']}/tftpd-hpa.pid"; | ||
|
||
// IPv4 Only? | ||
if ($tftpd_conf['tftpd_ipv4only'] == "on") { | ||
$options = "-4"; | ||
} | ||
|
||
// Max Block Size | ||
if (!empty($tftpd_conf['tftpd_blocksize'])) { | ||
$options .= " -B {$tftpd_conf['tftpd_blocksize']}"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above, re: |
||
} | ||
|
||
write_rcfile(array( | ||
"file" => "tftpd.sh", | ||
"start" => "/usr/local/libexec/in.tftpd -l -s {$datadir}", | ||
"start" => "/usr/local/libexec/in.tftpd -l -s {$datadir} {$address} -P {$pidfile} {$options}", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above, re: |
||
"stop" => "/usr/bin/killall in.tftpd" | ||
) | ||
); | ||
|
@@ -82,14 +136,33 @@ function sync_package_tftpd() { | |
} | ||
|
||
conf_mount_ro(); | ||
|
||
filter_configure(); | ||
} | ||
|
||
function validate_form_tftpd($post, &$input_errors) { | ||
if ($post['datadir'] && !is_dir($post['datadir'])) { | ||
$input_errors[] = 'Directory for files does not exist!'; | ||
} | ||
|
||
if ($post['datadir'] == '/') { | ||
$input_errors[] = 'Setting "/" as directory for files is not allowed!'; | ||
} | ||
|
||
if ($post['tftpd_ip']) { | ||
if ($post['tftpd_ipv4only'] && !is_ipaddrv4($post['tftpd_ip'])) { | ||
$input_errors[] = 'TFTP Server Bind IP must be a valid IPv4 address!'; | ||
} elseif (!is_ipaddr($post['tftpd_ip'])) { | ||
$input_errors[] = 'TFTP Server Bind IP must be a valid IP address!'; | ||
} | ||
if (!is_ipaddr_configured($post['tftpd_ip'])) { | ||
$input_errors[] = "{$post['tftpd_ip']} TFTP Server Bind IP must be a valid, locally configured IP address!"; | ||
} | ||
} | ||
|
||
if ($post['tftpd_blocksize']) { | ||
if (!is_numericint($post['tftpd_blocksize']) || ($post['tftpd_blocksize'] < 512) || ($post['tftpd_blocksize'] > 65464)) { | ||
$input_errors[] = 'Max Block Size must be an integer with a permitted range from 512 to 65464!'; | ||
} | ||
} | ||
} | ||
|
||
?> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have format_bytes() in util.inc, is this different in some way that's required by TFTP?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea, no clue that it existed.