PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 1ms

/net-analyzer/midas-nms/files/install.php

http://github.com/funtoo/portage
PHP | 456 lines | 390 code | 26 blank | 40 comment | 49 complexity | e7a862adf3ca3c2e272a105a9d0f8508 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, LGPL-2.1, MIT, CC-BY-SA-3.0, LGPL-2.0, Unlicense, AGPL-3.0, CC0-1.0, BSD-3-Clause, GPL-2.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0
  1. <?
  2. /********************************************************************
  3. Copyright (c) 2003 Jason Sessler
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation files
  6. (the "Software"), to deal in the Software without restriction,
  7. including without limitation the rights to use, copy, modify, merge,
  8. publish, distribute, sublicense, and/or sell copies of the Software,
  9. and to permit persons to whom the Software is furnished to do so,
  10. subject to the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  17. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  18. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21. ********************************************************************/
  22. /********************************************************************
  23. Changelog:
  24. 01/11/2004 - Changed how the MIDAS version is loaded to variable.
  25. 12/15/2003 - Version was incorrect
  26. - Cleaned a few non-problematic typos
  27. - Added check for MySQL Support in PHP
  28. 12/02/2003 - Added set_time_limit()
  29. 11/27/2003 - Added on/off toggle for creating the
  30. inc/config/config.php file.
  31. ********************************************************************/
  32. // Needed for sql statements. When updating large MIDAS DBs, the
  33. // process takes a lot longer than the php default time_limit
  34. @set_time_limit(0);
  35. define(_DOC_ROOT, "../");
  36. define("_APPNAME", "MIDAS WebView Installer");
  37. include _DOC_ROOT . "inc/version.php";
  38. include _DOC_ROOT . "lib/mysql.inc.php";
  39. include _DOC_ROOT . "lib/html.inc.php";
  40. include _DOC_ROOT . "themes/plain_blue.php";
  41. include "install.inc.php";
  42. printf("<HTML>\n");
  43. printf("<HEAD>\n");
  44. printf("<TITLE>%s %s</TITLE>\n", _APPNAME, _VERSION);
  45. printf("</HEAD>\n");
  46. printf("<LINK rel=stylesheet href='%s' type=text/css>\n", $color['css']);
  47. printf("<BODY bgcolor=%s topmargin=10 leftmargin=10>\n", $color[3]);
  48. $DIR = _DOC_ROOT . "inc/config";
  49. $IMGDIR = _DOC_ROOT . "php-graph";
  50. $CONFIG = $DIR . "/config.php";
  51. $TEMPLATE = $DIR . "/config.template";
  52. if($_POST['LOC'] == "") {
  53. $info = null;
  54. $error = FALSE;
  55. if(@is_readable($CONFIG)) {
  56. $schk .= html_tag("TR",
  57. html_tag("TD", "Site Config Exists", $color[0], "class=blacksmall align=left") .
  58. html_tag("TD", "WARNING", $color[1], "class=blacksmall align=center"),
  59. "", "");
  60. } else {
  61. $schk .= html_tag("TR",
  62. html_tag("TD", "Site Config Exists", $color[0], "class=blacksmall align=left") .
  63. html_tag("TD", "OK", $color[12], "class=blacksmall align=center"),
  64. "", "");
  65. }
  66. if(@is_readable($TEMPLATE)) {
  67. $schk .= html_tag("TR",
  68. html_tag("TD", "Site Config Template Exists", $color[0], "class=blacksmall align=left") .
  69. html_tag("TD", "OK", $color[12], "class=blacksmall align=center"),
  70. "", "");
  71. } else {
  72. $schk .= html_tag("TR",
  73. html_tag("TD", "Site Config Template Exists", $color[0], "class=blacksmall align=left") .
  74. html_tag("TD", "ERROR", $color[2], "class=blacksmall align=center"),
  75. "", "");
  76. $error = TRUE;
  77. }
  78. if(@!is_writeable($DIR)) {
  79. $schk .= html_tag("TR",
  80. html_tag("TD", "/inc/config directory is Writable", $color[0], "class=blacksmall align=left") .
  81. html_tag("TD", "ERROR", $color[2], "class=blacksmall align=center"),
  82. "", "");
  83. $error = TRUE;
  84. } else {
  85. $schk .= html_tag("TR",
  86. html_tag("TD", "/inc/config directory is Writable", $color[0], "class=blacksmall align=left") .
  87. html_tag("TD", "OK", $color[12], "class=blacksmall align=center"),
  88. "", "");
  89. }
  90. if(@!is_writeable($IMGDIR)) {
  91. $schk .= html_tag("TR",
  92. html_tag("TD", "/php-graph directory is Writable", $color[0], "class=blacksmall align=left") .
  93. html_tag("TD", "ERROR", $color[2], "class=blacksmall align=center"),
  94. "", "");
  95. $error = TRUE;
  96. } else {
  97. $schk .= html_tag("TR",
  98. html_tag("TD", "/php-graph directory is Writable", $color[0], "class=blacksmall align=left") .
  99. html_tag("TD", "OK", $color[12], "class=blacksmall align=center"),
  100. "", "");
  101. }
  102. if(@!function_exists(mysql_connect)) {
  103. $schk .= html_tag("TR",
  104. html_tag("TD", "PHP with MySQL support", $color[0], "class=blacksmall align=left") .
  105. html_tag("TD", "ERROR", $color[2], "class=blacksmall align=center"),
  106. "", "");
  107. $error = TRUE;
  108. } else {
  109. $schk .= html_tag("TR",
  110. html_tag("TD", "PHP with MySQL support", $color[0], "class=blacksmall align=left") .
  111. html_tag("TD", "OK", $color[12], "class=blacksmall align=center"),
  112. "", "");
  113. }
  114. if(@!function_exists(imagepng)) {
  115. $schk .= html_tag("TR",
  116. html_tag("TD", "PHP with GD+PNG support", $color[0], "class=blacksmall align=left") .
  117. html_tag("TD", "WARNING", $color[1], "class=blacksmall align=center"),
  118. "", "");
  119. } else {
  120. $schk .= html_tag("TR",
  121. html_tag("TD", "PHP with GD+PNG support", $color[0], "class=blacksmall align=left") .
  122. html_tag("TD", "OK", $color[12], "class=blacksmall align=center"),
  123. "", "");
  124. }
  125. $conf = html_tag("TR",
  126. html_tag("TD", "Absolute directory\n", $color[0], "class=blacksmall align=left") .
  127. html_tag("TD", "<input type=textbox name=ABS_ROOT value='/var/www/localhost/htdocs/midas-nms' size=30>\n", $color[3], "class=blacksmall align=left"),
  128. "", "") .
  129. html_tag("TR",
  130. html_tag("TD", "URL Directory\n", $color[0], "class=blacksmall align=left") .
  131. html_tag("TD", "<input type=textbox name=DOC_ROOT value='/midas-nms' size=20>\n", $color[3], "class=blacksmall align=left"),
  132. "", "") .
  133. sprintf("<input type=hidden name=RRD_ROOT value='/php-graph'>\n") .
  134. html_tag("TR",
  135. html_tag("TD", "PHP Graphics URL Location\n", $color[0], "class=blacksmall align=left") .
  136. html_tag("TD", "/php-graph\n", $color[3], "class=blacksmall align=left"),
  137. "", "") .
  138. html_tag("TR",
  139. html_tag("TD", "MySQL Server\n", $color[0], "class=blacksmall align=left") .
  140. html_tag("TD", "<input type=textbox name=SERVER value='localhost' size=20>\n", $color[3], "class=blacksmall align=left"),
  141. "", "") .
  142. html_tag("TR",
  143. html_tag("TD", "MySQL user\n", $color[0], "class=blacksmall align=left") .
  144. html_tag("TD", "<input type=textbox name=USER value='root' size=15>\n", $color[3], "class=blacksmall align=left"),
  145. "", "") .
  146. html_tag("TR",
  147. html_tag("TD", "MySQL passwd\n", $color[0], "class=blacksmall align=left") .
  148. html_tag("TD", "<input type=password name=PASSWD value='' size=10>\n", $color[3], "class=blacksmall align=left"),
  149. "", "") .
  150. html_tag("TR",
  151. html_tag("TD", "MySQL dB\n", $color[0], "class=blacksmall align=left") .
  152. html_tag("TD", "<input type=textbox name=DB value='MIDAS2' size=15>\n", $color[3], "class=blacksmall align=left"),
  153. "", "") .
  154. html_tag("TR",
  155. html_tag("TD", "MIDAS MySQL allowed hosts\n", $color[0], "class=blacksmall align=left") .
  156. html_tag("TD", "<input type=textbox name='ALLOWED_HOSTS' value='localhost' size=25>\n", $color[3], "class=blacksmall align=left"),
  157. "", "") .
  158. html_tag("TR",
  159. html_tag("TD", "MIDAS MySQL user\n", $color[0], "class=blacksmall align=left") .
  160. html_tag("TD", "<input type=textbox name=MIDAS_USER value='midas' size=15>\n", $color[3], "class=blacksmall align=left"),
  161. "", "") .
  162. html_tag("TR",
  163. html_tag("TD", "MIDAS MySQL passwd\n", $color[0], "class=blacksmall align=left") .
  164. html_tag("TD", "<input type=textbox name=MIDAS_PASSWD value='password' size=10>\n", $color[3], "class=blacksmall align=left"),
  165. "", "") .
  166. html_tag("TR",
  167. html_tag("TD", "MIDAS dB SQL file\n", $color[0], "class=blacksmall align=left") .
  168. html_tag("TD", "<input type=textbox name=MIDAS_SQL value='/usr/share/midas-nms/sql/MIDAS.sql' size=55>\n", $color[3], "class=blacksmall align=left"),
  169. "", "") .
  170. html_tag("TR",
  171. html_tag("TD", "SNORT Default Rules SQL file\n", $color[0], "class=blacksmall align=left") .
  172. html_tag("TD", "<input type=textbox name=SNORT_SQL value='/usr/share/midas-nms/sql/DefaultSnortRules.sql' size=55>\n", $color[3], "class=blacksmall align=left"),
  173. "", "") .
  174. html_tag("TR",
  175. html_tag("TD", "Create inc/config/config.php file\n", $color[0], "class=blacksmall align=left") .
  176. html_tag("TD", "<input type=checkbox name=CREATE_CONFIG_FILE value='1' checked>\n", $color[3], "class=blacksmall align=center"),
  177. "", "") .
  178. html_tag("TR",
  179. html_tag("TD", "Create MIDAS dB user\n", $color[0], "class=blacksmall align=left") .
  180. html_tag("TD", "<input type=checkbox name=CREATE_MIDAS_USER value='1' checked>\n", $color[3], "class=blacksmall align=center"),
  181. "", "") .
  182. html_tag("TR",
  183. html_tag("TD", "Install MIDAS SQL\n", $color[0], "class=blacksmall align=left") .
  184. html_tag("TD", "<input type=checkbox name=INSTALL_MIDAS_SQL value='1' checked>\n", $color[3], "class=blacksmall align=center"),
  185. "", "") .
  186. html_tag("TR",
  187. html_tag("TD", "Install SNORT Default Rules SQL file\n", $color[0], "class=blacksmall align=left") .
  188. html_tag("TD", "<input type=checkbox name=INSTALL_SNORT_SQL value='1' checked>\n", $color[3], "class=blacksmall align=center"),
  189. "", "");
  190. $info = html_tag("TABLE",
  191. html_tag("TR",
  192. html_tag("TD", "<b>Site Configuration Sanity Checks</b>", $color[9], "colspan=2 class=blacksmall align=center"),
  193. "", "") .
  194. $schk,
  195. $color[10], "width=50% cellspacing=1 cellpadding=2") .
  196. "<br>" .
  197. ((!$error) ?
  198. html_tag("TABLE",
  199. html_tag("TR",
  200. sprintf("<FORM method=post action='install.php'>\n") .
  201. html_tag("TD", "<b>Site Configuration</b>", $color[9], "colspan=2 class=blacksmall align=center"),
  202. "", "") .
  203. $conf .
  204. html_tag("TR",
  205. html_tag("TD", "<input type=submit name=LOC value=INSTALL>\n", $color[3], "colspan=2 class=blacksmall align=center") .
  206. sprintf("</FORM>\n"),
  207. "", ""),
  208. $color[10], "width=50% cellspacing=1 cellpadding=2") :
  209. "The above error(s) must be fixed before continuing!\n");
  210. } else if($_POST['LOC'] == "INSTALL") {
  211. // Building /inc/config/config.php
  212. if($_POST['CREATE_CONFIG_FILE']) {
  213. if($tp = @fopen($TEMPLATE, "r")) {
  214. if($fp = @fopen($CONFIG, "w+")) {
  215. while(!@feof($tp)) {
  216. $line = @fgets($tp, 1024);
  217. $pattern = null;
  218. $replace = null;
  219. $x = 0;
  220. $sPOS = 0;
  221. $ePOS = 0;
  222. while(($sPOS = strpos($line, "$[", $ePOS)) !== FALSE) {
  223. if(($ePOS = strpos($line, "]$", $sPOS)) !== FALSE) {
  224. $pattern[$x] = substr($line, $sPOS, $ePOS-($sPOS-2));
  225. $replace[$x] = $_POST[str_replace(Array("$[", "]$"), Array("", ""), $pattern[$x])];
  226. $x++;
  227. } else {
  228. $ePOS = $sPOS;
  229. }
  230. }
  231. fputs($fp, str_replace($pattern, $replace, $line));
  232. }
  233. fclose($fp);
  234. } else {
  235. $inst = html_tag("TR",
  236. html_tag("TD", "config.php created", $color[0], "class=blacksmall align=left") .
  237. html_tag("TD", "ERROR", $color[2], "class=blacksmall align=center"),
  238. "", "");
  239. }
  240. fclose($tp);
  241. if(is_null($inst)) {
  242. $inst = html_tag("TR",
  243. html_tag("TD", "config.php created", $color[0], "class=blacksmall align=left") .
  244. html_tag("TD", "OK", $color[12], "class=blacksmall align=center"),
  245. "", "");
  246. }
  247. } else {
  248. $inst = html_tag("TR",
  249. html_tag("TD", "config.php created", $color[0], "class=blacksmall align=left") .
  250. html_tag("TD", "ERROR", $color[2], "class=blacksmall align=center"),
  251. "", "");
  252. }
  253. }
  254. // SQL install (If applicable)
  255. $credentials['mySQL']['server'] = $_POST['SERVER'];
  256. $credentials['mySQL']['user'] = $_POST['USER'];
  257. $credentials['mySQL']['passwd'] = $_POST['PASSWD'];
  258. $db = new conn_mySQL($credentials);
  259. $db->db_new_conn();
  260. if($_POST['CREATE_MIDAS_USER']) {
  261. $db->db = "mysql";
  262. $hostList = explode(",", $_POST['ALLOWED_HOSTS']);
  263. for($x=0;$x<sizeof($hostList);$x++) {
  264. $db->add_table("user");
  265. $db->add_data("Host", $hostList[$x]);
  266. $db->add_data("User", $_POST['MIDAS_USER']);
  267. $db->add_data("Password", sprintf("PASSWORD('%s')", $_POST['MIDAS_PASSWD']), FALSE);
  268. $ret = $db->compile_query("INSERT");
  269. if($ret['return']) {
  270. $db->run_query();
  271. $inst .= html_tag("TR",
  272. html_tag("TD", sprintf("MIDAS User created (%s)", $hostList[$x]), $color[0], "class=blacksmall align=left") .
  273. html_tag("TD", "OK", $color[12], "class=blacksmall align=center"),
  274. "", "");
  275. $db->clear_res();
  276. $db->add_table("db");
  277. $db->add_data("Host", $hostList[$x]);
  278. $db->add_data("Db", $_POST['DB']);
  279. $db->add_data("User", $_POST['MIDAS_USER']);
  280. $db->add_data("Select_priv", "Y");
  281. $db->add_data("Insert_priv", "Y");
  282. $db->add_data("Update_priv", "Y");
  283. $db->add_data("Delete_priv", "Y");
  284. $db->add_data("Create_priv", "Y");
  285. $db->add_data("Drop_priv", "Y");
  286. $db->add_data("Grant_priv", "Y");
  287. $db->add_data("References_priv", "Y");
  288. $db->add_data("Index_priv", "Y");
  289. $db->add_data("Alter_priv", "Y");
  290. $ret = $db->compile_query("INSERT");
  291. if($ret['return']) {
  292. $db->run_query();
  293. $inst .= html_tag("TR",
  294. html_tag("TD", sprintf("MIDAS User dB privileges (%s)", $hostList[$x]), $color[0], "class=blacksmall align=left") .
  295. html_tag("TD", "OK", $color[12], "class=blacksmall align=center"),
  296. "", "");
  297. } else {
  298. $inst .= html_tag("TR",
  299. html_tag("TD", sprintf("MIDAS User dB privileges (%s)", $hostList[$x]), $color[0], "class=blacksmall align=left") .
  300. html_tag("TD", "ERROR", $color[2], "class=blacksmall align=center"),
  301. "", "");
  302. }
  303. } else {
  304. $inst .= html_tag("TR",
  305. html_tag("TD", sprintf("MIDAS User created (%s)", $hostList[$x]), $color[0], "class=blacksmall align=left") .
  306. html_tag("TD", "ERROR", $color[2], "class=blacksmall align=center"),
  307. "", "");
  308. }
  309. $db->clear_res();
  310. }
  311. $db->db = null;
  312. $db->run_query("FLUSH PRIVILEGES");
  313. if($db->sqlResults) {
  314. $inst .= html_tag("TR",
  315. html_tag("TD", "Flushing privileges", $color[0], "class=blacksmall align=left") .
  316. html_tag("TD", "OK", $color[12], "class=blacksmall align=center"),
  317. "", "");
  318. } else {
  319. $inst .= html_tag("TR",
  320. html_tag("TD", "Flushing privileges", $color[0], "class=blacksmall align=left") .
  321. html_tag("TD", "ERROR", $color[2], "class=blacksmall align=center"),
  322. "", "");
  323. }
  324. }
  325. $db->clear_res();
  326. $db->db = null;
  327. if($_POST['INSTALL_MIDAS_SQL'] || $_POST['INSTALL_SNORT_SQL']) {
  328. $db->run_query(sprintf("CREATE DATABASE IF NOT EXISTS %s", $_POST['DB']));
  329. if($db->sqlResults) {
  330. $inst .= html_tag("TR",
  331. html_tag("TD", "MIDAS dB created", $color[0], "class=blacksmall align=left") .
  332. html_tag("TD", "OK", $color[12], "class=blacksmall align=center"),
  333. "", "");
  334. $db->clear_res();
  335. if($_POST['INSTALL_MIDAS_SQL']) {
  336. if($SQL = parseSQL($_POST['MIDAS_SQL'])) {
  337. $ERROR = FALSE;
  338. $db->db = $_POST['DB'];
  339. @reset($SQL);
  340. while(!is_null($key = @key($SQL))) {
  341. $db->run_query($SQL[$key]);
  342. if(!$db->sqlResults) {
  343. $ERROR = TRUE;
  344. }
  345. @next($SQL);
  346. }
  347. if($ERROR) {
  348. $inst .= html_tag("TR",
  349. html_tag("TD", "MIDAS SQL Import", $color[0], "class=blacksmall align=left") .
  350. html_tag("TD", "ERROR", $color[2], "class=blacksmall align=center"),
  351. "", "");
  352. } else {
  353. $inst .= html_tag("TR",
  354. html_tag("TD", "MIDAS SQL Import", $color[0], "class=blacksmall align=left") .
  355. html_tag("TD", "OK", $color[12], "class=blacksmall align=center"),
  356. "", "");
  357. }
  358. } else {
  359. $inst .= html_tag("TR",
  360. html_tag("TD", "MIDAS SQL Import", $color[0], "class=blacksmall align=left") .
  361. html_tag("TD", "ERROR", $color[2], "class=blacksmall align=center"),
  362. "", "");
  363. }
  364. }
  365. if($_POST['INSTALL_SNORT_SQL']) {
  366. if($SQL = parseSQL($_POST['SNORT_SQL'])) {
  367. $ERROR = FALSE;
  368. $db->db = $_POST['DB'];
  369. @reset($SQL);
  370. while(!is_null($key = @key($SQL))) {
  371. $db->run_query($SQL[$key]);
  372. if(!$db->sqlResults) {
  373. $ERROR = TRUE;
  374. }
  375. @next($SQL);
  376. }
  377. if($ERROR) {
  378. $inst .= html_tag("TR",
  379. html_tag("TD", "SNORT SQL Import", $color[0], "class=blacksmall align=left") .
  380. html_tag("TD", "ERROR", $color[2], "class=blacksmall align=center"),
  381. "", "");
  382. } else {
  383. $inst .= html_tag("TR",
  384. html_tag("TD", "SNORT SQL Import", $color[0], "class=blacksmall align=left") .
  385. html_tag("TD", "OK", $color[12], "class=blacksmall align=center"),
  386. "", "");
  387. }
  388. } else {
  389. $inst .= html_tag("TR",
  390. html_tag("TD", "SNORT SQL Import", $color[0], "class=blacksmall align=left") .
  391. html_tag("TD", "ERROR", $color[2], "class=blacksmall align=center"),
  392. "", "");
  393. }
  394. }
  395. } else {
  396. $inst .= html_tag("TR",
  397. html_tag("TD", "MIDAS dB created", $color[0], "class=blacksmall align=left") .
  398. html_tag("TD", "ERROR", $color[2], "class=blacksmall align=center"),
  399. "", "");
  400. }
  401. }
  402. $info = html_tag("TABLE",
  403. html_tag("TR",
  404. html_tag("TD", "<b>Site Installation Results</b>", $color[9], "colspan=2 class=blacksmall align=center"),
  405. "", "") .
  406. $inst .
  407. html_tag("TR",
  408. html_tag("TD", sprintf("Click <A HREF='../index.php'>here</A> to access the logon screen\n"),
  409. $color[12], "colspan=2 class=blacksmall align=center"),
  410. "", ""),
  411. $color[10], "width=50% cellspacing=1 cellpadding=1 align=center");
  412. }
  413. echo html_tag("TABLE",
  414. html_tag("TR",
  415. html_tag("TD",
  416. sprintf("<SPAN class='titlelarge'>%s %s</SPAN><br><br>\n", _APPNAME, _VERSION), $color[4], "height=50 align=right valign=center"),
  417. "", "") .
  418. html_tag("TR",
  419. html_tag("TD", $info, $color[12], "width=100% height=100% align=center valign=center"),
  420. "", ""),
  421. $color[10], "width=100% height=100% align=center valign=center");
  422. printf("</BODY>\n");
  423. printf("</HTML>\n");
  424. ?>