authorizations.inc.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. <?php
  2. // -------------------------------------------------------------------------------
  3. // | net2ftp: a web based FTP client |
  4. // | Copyright (c) 2003-2013 by David Gartner |
  5. // | |
  6. // | This program is free software; you can redistribute it and/or |
  7. // | modify it under the terms of the GNU General Public License |
  8. // | as published by the Free Software Foundation; either version 2 |
  9. // | of the License, or (at your option) any later version. |
  10. // | |
  11. // -------------------------------------------------------------------------------
  12. // **************************************************************************************
  13. // **************************************************************************************
  14. // ** **
  15. // ** **
  16. function encryptPassword($password) {
  17. // --------------
  18. // This function encrypts the FTP password
  19. // --------------
  20. // -------------------------------------------------------------------------
  21. // Global variables and settings
  22. // -------------------------------------------------------------------------
  23. global $net2ftp_settings;
  24. // -------------------------------------------------------------------------
  25. // If mcrypt libraries are available, encrypt the password with the Stone PHP SafeCrypt library
  26. // http://blog.sc.tri-bit.com/archives/101
  27. // -------------------------------------------------------------------------
  28. // if (function_exists("mcrypt_module_open") == true) {
  29. // $packed = PackCrypt($password, DEFAULT_MD5_SALT);
  30. // if ($packed["success"] == true) { return $packed["output"]; }
  31. // else {
  32. // setErrorVars(false, "An error occured when trying to encrypt the password: " . $packed["reason"], debug_backtrace(), __FILE__, __LINE__);
  33. // }
  34. // }
  35. // -------------------------------------------------------------------------
  36. // Else, XOR it with a random string
  37. // -------------------------------------------------------------------------
  38. // else {
  39. $password_encrypted = "";
  40. $encryption_string = sha1($net2ftp_settings["encryption_string"]);
  41. if (strlen($encryption_string) % 2 == 1) { // we need even number of characters
  42. $encryption_string .= $encryption_string[0];
  43. }
  44. for ($i=0; $i < strlen($password); $i++) { // encrypts one character - two bytes at once
  45. $password_encrypted .= sprintf("%02X", hexdec(substr($encryption_string, 2*$i % strlen($encryption_string), 2)) ^ ord($password[$i]));
  46. }
  47. return $password_encrypted;
  48. // }
  49. } // End function encryptPassword
  50. // ** **
  51. // ** **
  52. // **************************************************************************************
  53. // **************************************************************************************
  54. // **************************************************************************************
  55. // **************************************************************************************
  56. // ** **
  57. // ** **
  58. function decryptPassword($password_encrypted) {
  59. // --------------
  60. // This function decrypts the FTP password
  61. // --------------
  62. // -------------------------------------------------------------------------
  63. // Global variables and settings
  64. // -------------------------------------------------------------------------
  65. global $net2ftp_settings;
  66. // -------------------------------------------------------------------------
  67. // If mcrypt libraries are available, encrypt the password with the Stone PHP SafeCrypt library
  68. // http://blog.sc.tri-bit.com/archives/101
  69. // -------------------------------------------------------------------------
  70. // if (function_exists("mcrypt_module_open") == true) {
  71. // $unpacked = UnpackCrypt($password_encrypted, DEFAULT_MD5_SALT);
  72. // if ($unpacked["success"] == true) { return $unpacked["output"]; }
  73. // else {
  74. // setErrorVars(false, "An error occured when trying to decrypt the password: " . $unpacked["reason"], debug_backtrace(), __FILE__, __LINE__);
  75. // }
  76. // }
  77. // -------------------------------------------------------------------------
  78. // Else, XOR it with a random string
  79. // -------------------------------------------------------------------------
  80. // else {
  81. $password = "";
  82. $encryption_string = sha1($net2ftp_settings["encryption_string"]);
  83. if (strlen($encryption_string) % 2 == 1) { // we need even number of characters
  84. $encryption_string .= $encryption_string[0];
  85. }
  86. for ($i=0; $i < strlen($password_encrypted); $i += 2) { // decrypts two bytes - one character at once
  87. $password .= chr(hexdec(substr($encryption_string, $i % strlen($encryption_string), 2)) ^ hexdec(substr($password_encrypted, $i, 2)));
  88. }
  89. return $password;
  90. // }
  91. } // End function decryptPassword
  92. // ** **
  93. // ** **
  94. // **************************************************************************************
  95. // **************************************************************************************
  96. // **************************************************************************************
  97. // **************************************************************************************
  98. // ** **
  99. // ** **
  100. function checkIPinNetwork($ip, $network) {
  101. // ----------
  102. // This function checks if an IP address is part of a network
  103. // If yes, it returns true; if no, it returns false
  104. //
  105. // The network's IP address range must be one of these notations:
  106. // - Single IP (example: 192.168.1.1)
  107. // - IP from-to (example: 192.168.1.1-192.168.1.10
  108. // - CIDR notation (example: 192.168.1.0/30 or 192.168.1/30)
  109. // ----------
  110. $ip = trim($ip);
  111. $network = trim($network);
  112. $d = strpos($network,"-");
  113. if ($d===false) {
  114. $ip_arr = explode("/", $network);
  115. if (!preg_match("@\d*\.\d*\.\d*\.\d*@", $ip_arr[0], $matches)){
  116. $ip_arr[0] .= ".0"; // To handle networks like 192.168.1/30 (instead of 192.168.1.0/30)
  117. }
  118. $network_long = ip2long($ip_arr[0]);
  119. $x = ip2long($ip_arr[1]);
  120. $mask = long2ip($x) == $ip_arr[1] ? $x : (0xffffffff << (32 - $ip_arr[1]));
  121. $ip_long = ip2long($ip);
  122. return ($ip_long & $mask) == ($network_long & $mask);
  123. }
  124. else {
  125. $from = ip2long(trim(substr($network,0,$d)));
  126. $to = ip2long(trim(substr($network,$d+1)));
  127. $ip = ip2long($ip);
  128. return ($ip>=$from and $ip<=$to);
  129. }
  130. } // End function checkIPinNetwork
  131. // ** **
  132. // ** **
  133. // **************************************************************************************
  134. // **************************************************************************************
  135. // **************************************************************************************
  136. // **************************************************************************************
  137. // ** **
  138. // ** **
  139. function printLoginInfo() {
  140. // --------------
  141. // This function prints the ftpserver, username and login information
  142. // --------------
  143. global $net2ftp_globals;
  144. echo "<input type=\"hidden\" name=\"ftpserver\" value=\"" . htmlEncode2($net2ftp_globals["ftpserver"]) . "\" />\n";
  145. echo "<input type=\"hidden\" name=\"ftpserverport\" value=\"" . htmlEncode2($net2ftp_globals["ftpserverport"]) . "\" />\n";
  146. echo "<input type=\"hidden\" name=\"username\" value=\"" . htmlEncode2($net2ftp_globals["username"]) . "\" />\n";
  147. // echo "<input type=\"hidden\" name=\"password_encrypted\" value=\"" . htmlEncode2($net2ftp_globals["password_encrypted"]) . "\" />\n";
  148. echo "<input type=\"hidden\" name=\"language\" value=\"" . htmlEncode2($net2ftp_globals["language"]) . "\" />\n";
  149. echo "<input type=\"hidden\" name=\"skin\" value=\"" . htmlEncode2($net2ftp_globals["skin"]) . "\" />\n";
  150. echo "<input type=\"hidden\" name=\"ftpmode\" value=\"" . htmlEncode2($net2ftp_globals["ftpmode"]) . "\" />\n";
  151. echo "<input type=\"hidden\" name=\"passivemode\" value=\"" . htmlEncode2($net2ftp_globals["passivemode"]) . "\" />\n";
  152. echo "<input type=\"hidden\" name=\"protocol\" value=\"" . htmlEncode2($net2ftp_globals["protocol"]) . "\" />\n";
  153. echo "<input type=\"hidden\" name=\"viewmode\" value=\"" . htmlEncode2($net2ftp_globals["viewmode"]) . "\" />\n";
  154. echo "<input type=\"hidden\" name=\"sort\" value=\"" . htmlEncode2($net2ftp_globals["sort"]) . "\" />\n";
  155. echo "<input type=\"hidden\" name=\"sortorder\" value=\"" . htmlEncode2($net2ftp_globals["sortorder"]) . "\" />\n";
  156. } // End function printLoginInfo
  157. // ** **
  158. // ** **
  159. // **************************************************************************************
  160. // **************************************************************************************
  161. // **************************************************************************************
  162. // **************************************************************************************
  163. // ** **
  164. // ** **
  165. function printLoginInfo_javascript() {
  166. // --------------
  167. // This function prints the ftpserver, username and login information -- for javascript input
  168. // --------------
  169. global $net2ftp_globals;
  170. echo " d.writeln('<input type=\"hidden\" name=\"ftpserver\" value=\"" . javascriptEncode2($net2ftp_globals["ftpserver"]) . "\" />');\n";
  171. echo " d.writeln('<input type=\"hidden\" name=\"ftpserverport\" value=\"" . javascriptEncode2($net2ftp_globals["ftpserverport"]) . "\" />');\n";
  172. echo " d.writeln('<input type=\"hidden\" name=\"username\" value=\"" . javascriptEncode2($net2ftp_globals["username"]) . "\" />');\n";
  173. // echo " d.writeln('<input type=\"hidden\" name=\"password_encrypted\" value=\"" . javascriptEncode2($net2ftp_globals["password_encrypted"]) . "\" />');\n";
  174. echo " d.writeln('<input type=\"hidden\" name=\"language\" value=\"" . javascriptEncode2($net2ftp_globals["language"]) . "\" />');\n";
  175. echo " d.writeln('<input type=\"hidden\" name=\"skin\" value=\"" . javascriptEncode2($net2ftp_globals["skin"]) . "\" />');\n";
  176. echo " d.writeln('<input type=\"hidden\" name=\"ftpmode\" value=\"" . javascriptEncode2($net2ftp_globals["ftpmode"]) . "\" />');\n";
  177. echo " d.writeln('<input type=\"hidden\" name=\"passivemode\" value=\"" . javascriptEncode2($net2ftp_globals["passivemode"]) . "\" />');\n";
  178. echo " d.writeln('<input type=\"hidden\" name=\"protocol\" value=\"" . javascriptEncode2($net2ftp_globals["protocol"]) . "\" />');\n";
  179. echo " d.writeln('<input type=\"hidden\" name=\"viewmode\" value=\"" . javascriptEncode2($net2ftp_globals["viewmode"]) . "\" />');\n";
  180. echo " d.writeln('<input type=\"hidden\" name=\"sort\" value=\"" . javascriptEncode2($net2ftp_globals["sort"]) . "\" />');\n";
  181. echo " d.writeln('<input type=\"hidden\" name=\"sortorder\" value=\"" . javascriptEncode2($net2ftp_globals["sortorder"]) . "\" />');\n";
  182. } // End function printLoginInfo_javascript
  183. // ** **
  184. // ** **
  185. // **************************************************************************************
  186. // **************************************************************************************
  187. // **************************************************************************************
  188. // **************************************************************************************
  189. // ** **
  190. // ** **
  191. function printPHP_SELF($case) {
  192. // --------------
  193. // This function prints $PHP_SELF, the name of the script itself
  194. // --------------
  195. // -------------------------------------------------------------------------
  196. // Global variables and settings
  197. // -------------------------------------------------------------------------
  198. global $net2ftp_globals, $net2ftp_settings;
  199. $ftpserver = urlEncode2($net2ftp_globals["ftpserver"]);
  200. $ftpserverport = urlEncode2($net2ftp_globals["ftpserverport"]);
  201. $username = urlEncode2($net2ftp_globals["username"]);
  202. $language = urlEncode2($net2ftp_globals["language"]);
  203. $skin = urlEncode2($net2ftp_globals["skin"]);
  204. $ftpmode = urlEncode2($net2ftp_globals["ftpmode"]);
  205. $passivemode = urlEncode2($net2ftp_globals["passivemode"]);
  206. $protocol = urlEncode2($net2ftp_globals["protocol"]);
  207. $viewmode = urlEncode2($net2ftp_globals["viewmode"]);
  208. $sort = urlEncode2($net2ftp_globals["sort"]);
  209. $sortorder = urlEncode2($net2ftp_globals["sortorder"]);
  210. $state_html = urlEncode2($net2ftp_globals["state"]);
  211. $state2_html = urlEncode2($net2ftp_globals["state2"]);
  212. $directory_html = urlEncode2($net2ftp_globals["directory"]);
  213. $entry_html = urlEncode2($net2ftp_globals["entry"]);
  214. if (isset($_SESSION["net2ftp_password_encrypted_" . $net2ftp_globals["ftpserver"] . $net2ftp_globals["username"]]) == true) {
  215. $password_encrypted = urlEncode2($_SESSION["net2ftp_password_encrypted_" . $net2ftp_globals["ftpserver"] . $net2ftp_globals["username"]]);
  216. }
  217. elseif (isset($net2ftp_globals["password_encrypted"]) == true) {
  218. $password_encrypted = urlEncode2($net2ftp_globals["password_encrypted"]);
  219. }
  220. else {
  221. $password_encrypted = "";
  222. }
  223. // From /includes/registerglobals.inc.php
  224. $URL = $net2ftp_globals["action_url"];
  225. // If the URL already contains parameters (?param1=value1&amp;param2=value2...), append &amp;
  226. // If not, append a ?
  227. if (strpos($URL, "?") !== false) { $URL .= "&amp;"; }
  228. else { $URL .= "?"; }
  229. // Append further parameters
  230. if ($case == "actions") {
  231. $URL .= "ftpserver=$ftpserver&amp;ftpserverport=$ftpserverport&amp;username=$username&amp;language=$language&amp;skin=$skin&amp;ftpmode=$ftpmode&amp;passivemode=$passivemode&amp;protocol=$protocol&amp;viewmode=$viewmode&amp;sort=$sort&amp;sortorder=$sortorder";
  232. }
  233. // Bookmark with password: go straight to the bookmarked state
  234. elseif ($case == "bookmark_withpw") {
  235. $URL .= "ftpserver=$ftpserver&amp;amp;ftpserverport=$ftpserverport&amp;amp;username=$username&amp;amp;password_encrypted=$password_encrypted&amp;amp;language=$language&amp;amp;skin=$skin&amp;amp;ftpmode=$ftpmode&amp;amp;passivemode=$passivemode&amp;amp;protocol=$protocol&amp;amp;viewmode=$viewmode&amp;amp;sort=$sort&amp;amp;sortorder=$sortorder&amp;amp;state=$state_html&amp;amp;state2=$state2_html&amp;amp;directory=$directory_html&amp;amp;entry=$entry_html";
  236. }
  237. // Bookmark without password: go first to the login_small state to enter the password
  238. elseif ($case == "bookmark_withoutpw") {
  239. $URL .= "ftpserver=$ftpserver&amp;amp;ftpserverport=$ftpserverport&amp;amp;username=$username&amp;amp;language=$language&amp;amp;skin=$skin&amp;amp;ftpmode=$ftpmode&amp;amp;passivemode=$passivemode&amp;amp;protocol=$protocol&amp;amp;viewmode=$viewmode&amp;amp;sort=$sort&amp;amp;sortorder=$sortorder&amp;amp;state=login_small&amp;amp;state2=bookmark&amp;amp;go_to_state=$state_html&amp;amp;go_to_state2=$state2_html&amp;amp;directory=$directory_html&amp;amp;entry=$entry_html";
  240. }
  241. // Jupload java applet: the cookie information is added to the page using javascript (/skins/blue/jupload1.template.php)
  242. elseif ($case == "jupload") {
  243. $URL .= "ftpserver=$ftpserver&amp;ftpserverport=$ftpserverport&amp;username=$username&amp;language=$language&amp;skin=$skin&amp;ftpmode=$ftpmode&amp;passivemode=$passivemode&amp;protocol=$protocol&amp;directory=$directory_html&amp;state=jupload&amp;screen=2";
  244. }
  245. elseif ($case == "view") {
  246. $URL .= "ftpserver=$ftpserver&amp;ftpserverport=$ftpserverport&amp;username=$username&amp;language=$language&amp;skin=$skin&amp;ftpmode=$ftpmode&amp;passivemode=$passivemode&amp;protocol=$protocol&amp;viewmode=$viewmode&amp;sort=$sort&amp;sortorder=$sortorder&amp;state=$state_html&amp;state2=image&amp;directory=$directory_html&amp;entry=$entry_html";
  247. }
  248. elseif ($case == "createDirectoryTreeWindow") {
  249. $URL = $net2ftp_globals["application_rootdir_url"] . "/index.php";
  250. }
  251. // Change skin
  252. elseif ($case == "defaultskin") {
  253. $URL .= "ftpserver=$ftpserver&amp;ftpserverport=$ftpserverport&amp;username=$username&amp;language=$language&amp;skin=" . $net2ftp_settings["default_skin"] . "&amp;ftpmode=$ftpmode&amp;passivemode=$passivemode&amp;protocol=$protocol&amp;viewmode=$viewmode&amp;sort=$sort&amp;sortorder=$sortorder&amp;state=$state_html&amp;state2=$state2_html&amp;directory=$directory_html&amp;entry=$entry_html";
  254. }
  255. return $URL;
  256. } // End function printPHP_SELF
  257. // ** **
  258. // ** **
  259. // **************************************************************************************
  260. // **************************************************************************************
  261. // **************************************************************************************
  262. // **************************************************************************************
  263. // ** **
  264. // ** **
  265. function checkAuthorization($ftpserver, $ftpserverport, $directory, $username) {
  266. // --------------
  267. // This function
  268. // checks if the FTP server is in the list of those that may be accessed
  269. // checks if the FTP server is in the list of those that may NOT be accessed
  270. // checks if the IP address is in the list of banned IP addresses
  271. // checks if the FTP server port is in the allowed range
  272. // If all is OK, then the user may continue...
  273. // --------------
  274. // -------------------------------------------------------------------------
  275. // Global variables
  276. // -------------------------------------------------------------------------
  277. global $net2ftp_globals, $net2ftp_settings, $net2ftp_result;
  278. // -------------------------------------------------------------------------
  279. // Check if the FTP server is in the list of those that may be accessed
  280. // -------------------------------------------------------------------------
  281. if ($net2ftp_settings["allowed_ftpservers"][1] != "ALL") {
  282. $result1 = array_search($ftpserver, $net2ftp_settings["allowed_ftpservers"]);
  283. if ($result1 == false) {
  284. $errormessage = __("The FTP server <b>%1\$s</b> is not in the list of allowed FTP servers.", $ftpserver);
  285. setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
  286. return false;
  287. }
  288. }
  289. // -------------------------------------------------------------------------
  290. // Check if the FTP server is in the list of those that may NOT be accessed
  291. // -------------------------------------------------------------------------
  292. if (isset($net2ftp_settings["banned_ftpservers"][1]) == true && $net2ftp_settings["banned_ftpservers"][1] != "NONE") {
  293. $result2 = array_search($ftpserver, $net2ftp_settings["banned_ftpservers"]);
  294. if ($result2 != false) {
  295. $errormessage = __("The FTP server <b>%1\$s</b> is in the list of banned FTP servers.", $ftpserver);
  296. setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
  297. return false;
  298. }
  299. }
  300. // -------------------------------------------------------------------------
  301. // Check if the FTP server port is OK
  302. // -------------------------------------------------------------------------
  303. // Do not perform this check if ALL ports are allowed
  304. if ($net2ftp_settings["allowed_ftpserverport"] != "ALL" ) {
  305. // Report the error if another port nr has been entered than the one which is allowed
  306. if ($ftpserverport != $net2ftp_settings["allowed_ftpserverport"]) {
  307. $errormessage = __("The FTP server port %1\$s may not be used.", $ftpserverport);
  308. setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
  309. return false;
  310. }
  311. }
  312. // -------------------------------------------------------------------------
  313. // Check if the IP address is in the list of those that may be used
  314. // -------------------------------------------------------------------------
  315. if ($net2ftp_settings["allowed_addresses"][1] != "ALL") {
  316. $result3 = false;
  317. for ($i=1; $i<=sizeof($net2ftp_settings["allowed_addresses"]); $i++) {
  318. if (checkIPinNetwork($net2ftp_globals["REMOTE_ADDR"], $net2ftp_settings["allowed_addresses"][$i]) == true) { $result3 = true; }
  319. }
  320. if ($result3 == false) {
  321. $errormessage = __("Your IP address (%1\$s) is not in the list of allowed IP addresses.", $net2ftp_globals["REMOTE_ADDR"]);
  322. setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
  323. return false;
  324. }
  325. }
  326. // -------------------------------------------------------------------------
  327. // Check if the IP address is in the list of those that may NOT be used
  328. // -------------------------------------------------------------------------
  329. if (isset($net2ftp_settings["banned_addresses"][1]) == true && $net2ftp_settings["banned_addresses"][1] != "NONE") {
  330. $result4 = false;
  331. for ($i=1; $i<=sizeof($net2ftp_settings["banned_addresses"]); $i++) {
  332. if (checkIPinNetwork($net2ftp_globals["REMOTE_ADDR"], $net2ftp_settings["banned_addresses"][$i]) == true) { $result4 = true; }
  333. }
  334. if ($result4 != false) {
  335. $errormessage = __("Your IP address (%1\$s) is in the list of banned IP addresses.", $net2ftp_globals["REMOTE_ADDR"]);
  336. setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
  337. return false;
  338. }
  339. }
  340. // -------------------------------------------------------------------------
  341. // Check if the directory is authorised:
  342. // 1 - Whether the current $directory name contains a banned keyword.
  343. // 2 - If the current $directory is a subdirectory of the homedirectory.
  344. // The rootdirectory is first checked for the current user; if this is not set,
  345. // the default rootdirectory is checked.
  346. // -------------------------------------------------------------------------
  347. $result4 = checkAuthorizedDirectory($directory);
  348. if ($net2ftp_result["success"] == false) { return false; }
  349. if ($result4 == false) {
  350. $net2ftp_globals["directory"] = $net2ftp_globals["homedirectory"];
  351. $net2ftp_globals["directory_html"] = htmlEncode2($net2ftp_globals["directory"]);
  352. $net2ftp_globals["directory_js"] = javascriptEncode2($net2ftp_globals["directory"]);
  353. if (strlen($net2ftp_globals["directory"]) > 0) { $net2ftp_globals["printdirectory"] = $net2ftp_globals["directory"]; }
  354. else { $net2ftp_globals["printdirectory"] = "/"; }
  355. }
  356. // -------------------------------------------------------------------------
  357. // If everything is OK, return true
  358. // -------------------------------------------------------------------------
  359. return true;
  360. } // end checkAuthorization
  361. // ** **
  362. // ** **
  363. // **************************************************************************************
  364. // **************************************************************************************
  365. // **************************************************************************************
  366. // **************************************************************************************
  367. // ** **
  368. // ** **
  369. function checkAuthorizedDirectory($directory) {
  370. // --------------
  371. // 1 - This function checks whether the current $directory name contains a banned
  372. // keyword.
  373. // 2 - It also checks if the current $directory is a subdirectory of the
  374. // homedirectory. The rootdirectory is first checked for the current user;
  375. // if this is not set, the default rootdirectory is checked.
  376. // --------------
  377. // -------------------------------------------------------------------------
  378. // Global variables
  379. // -------------------------------------------------------------------------
  380. global $net2ftp_globals, $net2ftp_settings, $net2ftp_result;
  381. // -------------------------------------------------------------------------
  382. // 1 - Check if the directory name contains a banned keyword
  383. // -------------------------------------------------------------------------
  384. if (checkAuthorizedName($directory) == false) { return false; }
  385. // -------------------------------------------------------------------------
  386. // 2 - Check if the directory is a subdirectory of the homedirectory (set in the DB)
  387. // -------------------------------------------------------------------------
  388. // ----------------------------------------------
  389. // Initial checks
  390. // ----------------------------------------------
  391. if ($net2ftp_settings["use_database"] != "yes" || $net2ftp_settings["check_homedirectory"] != "yes") { return true; }
  392. // ----------------------------------------------
  393. // Get the homedirectory from the database, then store it in a global
  394. // variable, and from then on, don't access the database any more
  395. // ----------------------------------------------
  396. $net2ftp_globals["homedirectory"] = getRootdirectory();
  397. // ----------------------------------------------
  398. // Check if the current directory is a subdirectory of the homedirectory
  399. // ----------------------------------------------
  400. if (isSubdirectory($net2ftp_globals["homedirectory"], $directory) == false) { return false; }
  401. else { return true; }
  402. } // end checkAuthorizedDirectory
  403. // ** **
  404. // ** **
  405. // **************************************************************************************
  406. // **************************************************************************************
  407. // **************************************************************************************
  408. // **************************************************************************************
  409. // ** **
  410. // ** **
  411. function checkAuthorizedName($dirfilename) {
  412. // --------------
  413. // This function checks if the directory/file/symlink name contains a forbidden keyword
  414. // --------------
  415. // -------------------------------------------------------------------------
  416. // Global variables
  417. // -------------------------------------------------------------------------
  418. global $net2ftp_settings;
  419. // -------------------------------------------------------------------------
  420. // Check
  421. // -------------------------------------------------------------------------
  422. if (isset($net2ftp_settings["banned_keywords"][1]) == true && $net2ftp_settings["banned_keywords"][1] != "NONE") {
  423. for ($i=1; $i<=sizeof($net2ftp_settings["banned_keywords"]); $i++) {
  424. if (strpos($dirfilename, $net2ftp_settings["banned_keywords"][$i]) !== false) { return false; }
  425. }
  426. }
  427. return true;
  428. } // end checkAuthorizedName
  429. // ** **
  430. // ** **
  431. // **************************************************************************************
  432. // **************************************************************************************
  433. // **************************************************************************************
  434. // **************************************************************************************
  435. // ** **
  436. // ** **
  437. function getRootdirectory() {
  438. // --------------
  439. // This function gets the user's root directory from the database and
  440. // stores it in $net2ftp_globals["homedirectory"].
  441. //
  442. // If $net2ftp_globals["homedirectory"] is already filled in (cache), no connection
  443. // is made to the DB and this value is returned.
  444. // --------------
  445. // -------------------------------------------------------------------------
  446. // Global variables
  447. // -------------------------------------------------------------------------
  448. global $net2ftp_globals, $net2ftp_settings, $net2ftp_result;
  449. // -------------------------------------------------------------------------
  450. // Initial checks
  451. // -------------------------------------------------------------------------
  452. if ($net2ftp_settings["use_database"] != "yes" || $net2ftp_settings["check_homedirectory"] != "yes") {
  453. $net2ftp_globals["homedirectory"] = "/";
  454. }
  455. // -------------------------------------------------------------------------
  456. // Get the homedirectory from the database, then store it in a global
  457. // variable, and from then on, don't access the database any more
  458. // -------------------------------------------------------------------------
  459. if (isset($net2ftp_globals["homedirectory"]) == false) {
  460. // -------------------------------------------------------------------------
  461. // Add slashes to variables which are used in a SQL query, and which are
  462. // potentially unsafe (supplied by the user)
  463. // -------------------------------------------------------------------------
  464. $net2ftp_ftpserver_safe = addslashes($net2ftp_globals["ftpserver"]);
  465. $net2ftp_username_safe = addslashes($net2ftp_globals["username"]);
  466. // -------------------------------------------------------------------------
  467. // Connect
  468. // -------------------------------------------------------------------------
  469. $mydb = connect2db();
  470. if ($net2ftp_result["success"] == false) { return false; }
  471. // -------------------------------------------------------------------------
  472. // Get user's home directory
  473. // -------------------------------------------------------------------------
  474. $sqlquery1 = "SELECT homedirectory FROM net2ftp_users WHERE ftpserver = '$net2ftp_ftpserver_safe' AND username = '$net2ftp_username_safe';";
  475. $result1 = mysql_query("$sqlquery1") or die("Unable to execute SQL SELECT query (isAuthorizedDirectory > sqlquery1) <br /> $sqlquery1");
  476. $nrofrows1 = mysql_num_rows($result1);
  477. if ($nrofrows1 == 0) {
  478. $net2ftp_globals["homedirectory"] = "/";
  479. }
  480. elseif ($nrofrows1 == 1) {
  481. $resultRow1 = mysql_fetch_row($result1);
  482. $net2ftp_globals["homedirectory"] = $resultRow1[0];
  483. }
  484. else {
  485. setErrorVars(false, __("Table net2ftp_users contains duplicate rows."), debug_backtrace(), __FILE__, __LINE__);
  486. return false;
  487. }
  488. }
  489. return $net2ftp_globals["homedirectory"];
  490. } // end getRootdirectory
  491. // ** **
  492. // ** **
  493. // **************************************************************************************
  494. // **************************************************************************************
  495. // **************************************************************************************
  496. // **************************************************************************************
  497. // ** **
  498. // ** **
  499. function isSubdirectory($parentdir, $childdir) {
  500. // --------------
  501. // Returns true if the childdir is a subdirectory of the parentdir
  502. // --------------
  503. // If the parentdir is empty or the root directory, then the childdir is
  504. // a the same as or a subdirectory of the parentdir
  505. if ($parentdir == "" || $parentdir == "/" || $parentdir == "\\") { return true; }
  506. // Strip the directories of leading and trailing slashes
  507. $parentdir = stripDirectory($parentdir);
  508. $childdir = stripDirectory($childdir);
  509. $parentdir_length = strlen($parentdir);
  510. // Check if the first characters of the childdir are different from the
  511. // parentdir. Example:
  512. // parentdir: /home/abc
  513. // childdir: /home/blabla ==> false
  514. // childdir: /home/abcd ==> continue further checks
  515. // childdir: /home/abc/xyz ==> continue further checks
  516. $childdir_firstchars = substr($childdir, 0, $parentdir_length);
  517. if ($childdir_firstchars != $parentdir) { return false; }
  518. // If the first characters of the childdir are identical to the parentdir,
  519. // check if the first next character of the childdir name is different.
  520. // Example:
  521. // parentdir: /home/abc
  522. // childdir: /home/abcd ==> false
  523. // childdir: /home/abc/xyz ==> true
  524. $childdir_nextchar = substr($childdir, $parentdir_length, 1);
  525. if ($childdir_nextchar != "/" && $childdir_nextchar != "\\") { return false; }
  526. return true;
  527. } // end isSubdirectory
  528. // ** **
  529. // ** **
  530. // **************************************************************************************
  531. // **************************************************************************************
  532. // **************************************************************************************
  533. // **************************************************************************************
  534. // ** **
  535. // ** **
  536. function checkAdminUsernamePassword() {
  537. // --------------
  538. // This function checks the Administrator username and password.
  539. // If one of the two is not filled in or incorrect, a header() is sent
  540. // to redirect the user to the login_small page.
  541. // --------------
  542. // -------------------------------------------------------------------------
  543. // Global variables
  544. // -------------------------------------------------------------------------
  545. global $net2ftp_globals, $net2ftp_settings, $net2ftp_result;
  546. $input_admin_username = $_POST["input_admin_username"];
  547. $input_admin_password = $_POST["input_admin_password"];
  548. // -------------------------------------------------------------------------
  549. // Check Admin username and password
  550. // -------------------------------------------------------------------------
  551. // Set the error message depending on the case
  552. // Redirect the user to the login_small page
  553. // No username or password filled in
  554. if ($input_admin_username == "" || $input_admin_password == "") {
  555. $errormessage = htmlEncode2(__("You did not enter your Administrator username or password."));
  556. header("Location: " . $net2ftp_globals["action_url"] . "?state=login_small&state2=admin&go_to_state=" . $net2ftp_globals["state"] . "&go_to_state2=" . $net2ftp_globals["state2"] . "&errormessage=" . $errormessage);
  557. $net2ftp_result["exit"] = true;
  558. return false;
  559. }
  560. // Wrong username or password
  561. elseif ($input_admin_username != $net2ftp_settings["admin_username"] ||
  562. $input_admin_password != $net2ftp_settings["admin_password"]) {
  563. $errormessage = htmlEncode2(__("Wrong username or password. Please try again."));
  564. header("Location: " . $net2ftp_globals["action_url"] . "?state=login_small&state2=admin&go_to_state=" . $net2ftp_globals["state"] . "&go_to_state2=" . $net2ftp_globals["state2"] . "&errormessage=" . $errormessage);
  565. $net2ftp_result["exit"] = true;
  566. return false;
  567. }
  568. return true;
  569. } // end checkAdminUsernamePassword()
  570. // ** **
  571. // ** **
  572. // **************************************************************************************
  573. // **************************************************************************************
  574. ?>