From 90a594d7e2cd1ed85df26b952ce3af73305b41b9 Mon Sep 17 00:00:00 2001 From: doktornotor Date: Thu, 6 Aug 2015 08:38:48 +0200 Subject: tftp.inc - code improvements and cleanup - Fix copyright header - Remove unused functions - Use safe_mkdir() - Improve the backup restore code - Indentation and code style fixes --- config/tftp2/tftp.inc | 118 +++++++++++++++++--------------------------------- 1 file changed, 39 insertions(+), 79 deletions(-) (limited to 'config') diff --git a/config/tftp2/tftp.inc b/config/tftp2/tftp.inc index ea75e0d2..a2b7d1e3 100644 --- a/config/tftp2/tftp.inc +++ b/config/tftp2/tftp.inc @@ -1,23 +1,21 @@ 10^x|1024->2^x] - $s=array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB'); - $e=floor(log($bytes,$convention)); - return round($bytes/pow($convention,$e),2).' '.$s[$e]; + } + $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]; } function tftp_install_command() { global $config; - conf_mount_rw(); - - if (!is_dir('/tftpboot')) { - // Create the directory - exec("mkdir /tftpboot"); - - //Set the directory permissions - exec("chmod -R 777 /tftpboot"); - } - - if (!is_dir('/root/backup/')) { - // Create the backup directory - exec("mkdir /root/backup/"); - } - // if backup file exists restore it - $filename = 'tftp.bak.tgz'; + $tftpdir = "/tftpboot"; + $tftpbackup = "/root/backup/tftp.bak.tgz"; - //extract a specific directory to /usr/local/freeswitch - if (file_exists('/root/backup/'.$filename)) { - system('cd /; tar xvpfz /root/backup/tftp.bak.tgz'); - system('chmod -R 744 /tftpboot/*'); - unset($filename); + // Create the directories if required + conf_mount_rw(); + safe_mkdir("{$tftpdir}", 0777); + safe_mkdir("/root/backup/"); + + // Restore backup if it exists + if (file_exists($tftpbackup)) { + system("/usr/bin/tar xvpfz {$tftpbackup} -C /"); + system("/bin/chmod -R 0744 {$tftpdir}/*"); + unset($tftpbackup); } + conf_mount_ro(); } function tftp_deinstall_command() { - - //exec("rm -R /tftpboot"); + conf_mount_rw(); unlink_if_exists("/usr/local/etc/rc.d/tftp.sh"); unlink_if_exists("/tmp/pkg_mgr_tftp.log"); + conf_mount_ro(); } function tftp_generate_rules($type) { global $config, $FilterIflist; - if ($type != "nat") + + if ($type != "nat") { return; + } + // Open inetd.conf write handle - $inetd_fd = fopen("/var/etc/inetd.conf","a+"); - /* add tftp daemon */ + $inetd_fd = fopen("/var/etc/inetd.conf", "a+"); + // Add tftp daemon fwrite($inetd_fd, "tftp\t\tdgram\tudp\twait\t\troot\t/usr/libexec/tftpd\ttftpd /tftpboot\n"); - fclose($inetd_fd); // Close file handle + // Close file handle + fclose($inetd_fd); if (!empty($config['installedpackages']['tftpd']['config'][0]['tftpdinterface'])) { $tftpifs = explode(",", $config['installedpackages']['tftpd']['config'][0]['tftpdinterface']); - foreach($tftpifs as $tftpif) { + foreach ($tftpifs as $tftpif) { if ($FilterIflist[$tftpif]) { log_error("Adding TFTP nat rules"); $natrules .= "rdr pass on {$FilterIflist[$tftpif]['if']} proto udp from any to {$FilterIflist[$tftpif]['ip']} port 69 -> 127.0.0.1 port 69\n"; -- cgit v1.2.3