r2904 Added changelog entry for ticket #234
[racktables] / install.php
1 <?php
2
3 // This script is intended for execution through a web-browser, e.g.:
4 // https://example.com/racktables/install.php
5 // See README file for more information.
6
7 $stepfunc[1] = 'not_already_installed';
8 $stepfunc[2] = 'platform_is_ok';
9 $stepfunc[3] = 'init_config';
10 $stepfunc[4] = 'init_database_static';
11 $stepfunc[5] = 'init_database_dynamic';
12 $stepfunc[6] = 'congrats';
13 $dbxlink = NULL;
14
15 if (isset ($_REQUEST['step']))
16 $step = $_REQUEST['step'];
17 else
18 $step = 1;
19
20 if ($step > count ($stepfunc))
21 {
22 require 'inc/init.php';
23 global $root;
24 header ("Location: " . $root);
25 exit;
26 }
27 $title = "RackTables installation: step ${step} of " . count ($stepfunc);
28 ?>
29 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
30 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
31 <head><title><?php echo $title; ?></title>
32 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
33 <link rel=stylesheet type='text/css' href=pi.css />
34 </head>
35 <body>
36 <center>
37 <?php
38 echo "<h1>${title}</h1><p>";
39
40 echo "</p><form method=post>\n";
41 $testres = $stepfunc[$step] ();
42 if ($testres)
43 {
44 $next_step = $step + 1;
45 echo "<input type=submit value='proceed'>";
46 }
47 else
48 {
49 $next_step = $step;
50 echo "<input type=submit value='retry'>";
51 }
52 echo "<input type=hidden name=step value='${next_step}'>\n";
53
54 ?>
55 </form>
56 </center>
57 </body>
58 </html>
59
60 <?php
61 // Check if the software is already installed.
62 function not_already_installed()
63 {
64 @include ('inc/secret.php');
65 if (isset ($pdo_dsn))
66 {
67 echo 'Your configuration file exists and seems to hold necessary data already.<br>';
68 return FALSE;
69 }
70 else
71 {
72 echo 'There seem to be no existing installation here, I am going to setup one now.<br>';
73 return TRUE;
74 }
75 }
76
77 // Check for PHP extensions.
78 function platform_is_ok ()
79 {
80 $nerrs = 0;
81 echo "<table border=1><tr><th>check item</th><th>result</th></tr>\n";
82
83 echo '<tr><td>PDO extension</td>';
84 if (class_exists ('PDO'))
85 echo '<td class=msg_success>Ok';
86 else
87 {
88 echo '<td class=msg_error>not found';
89 $nerrs++;
90 }
91 echo '</td></tr>';
92
93 echo '<tr><td>PDO-MySQL</td>';
94 if (defined ('PDO::MYSQL_ATTR_READ_DEFAULT_FILE'))
95 echo '<td class=msg_success>Ok';
96 else
97 {
98 echo '<td class=msg_error>not found';
99 $nerrs++;
100 }
101 echo '</td></tr>';
102
103 echo '<tr><td>hash function</td>';
104 if (function_exists ('sha1'))
105 echo '<td class=msg_success>Ok';
106 else
107 {
108 echo '<td class=msg_error>not found';
109 $nerrs++;
110 }
111 echo '</td></tr>';
112
113 echo '<tr><td>SNMP extension</td>';
114 if (defined ('SNMP_NULL'))
115 echo '<td class=msg_success>Ok';
116 else
117 echo '<td class=msg_warning>Not found. Live SNMP tab will not function properly until the extension is installed.';
118 echo '</td></tr>';
119
120 echo '<tr><td>GD functions</td>';
121 if (defined ('IMG_PNG'))
122 echo '<td class=msg_success>Ok';
123 else
124 {
125 echo '<td class=msg_error>not found';
126 $nerrs++;
127 }
128 echo '</td></tr>';
129
130 echo '<tr><td>HTTP scheme</td>';
131 if (!empty($_SERVER['HTTPS']) and $_SERVER['HTTPS'] != 'off')
132 echo '<td class=msg_success>HTTPs';
133 else
134 echo '<td class=msg_warning>HTTP (all your passwords will be transmitted in cleartext)';
135 echo '</td></tr>';
136
137 echo '<tr><td>Multibyte string extension</td>';
138 if (defined ('MB_CASE_LOWER'))
139 echo '<td class=msg_success>Ok';
140 else
141 {
142 echo '<td class=msg_error>not found';
143 $nerrs++;
144 }
145 echo '</td></tr>';
146
147 echo '<tr><td>LDAP extension</td>';
148 if (defined ('LDAP_OPT_DEREF'))
149 echo '<td class=msg_success>Ok';
150 else
151 {
152 echo '<td class=msg_warning>not found, LDAP authentication will not work';
153 }
154 echo '</td></tr>';
155
156 echo "</table>\n";
157 return !$nerrs;
158 }
159
160 // Check that we can write to configuration file.
161 // If so, ask for DB connection paramaters and test
162 // the connection. Neither save the parameters nor allow
163 // going further until we succeed with the given
164 // credentials.
165 function init_config ()
166 {
167 if (!is_writable ('inc/secret.php'))
168 {
169 echo "The inc/secret.php file is not writable by web-server. Make sure it is.";
170 echo "The following commands should suffice:<pre>touch inc/secret.php\nchmod 666 inc/secret.php</pre>";
171 echo 'Fedora Linux with SELinux may require this file to be owned by specific user (apache) and/or executing "setenforce 0" for the time of installation. ';
172 echo 'SELinux may be turned back on with "setenforce 1" command.';
173 return FALSE;
174 }
175 if
176 (
177 !isset ($_REQUEST['save_config']) or
178 empty ($_REQUEST['mysql_host']) or
179 empty ($_REQUEST['mysql_db']) or
180 empty ($_REQUEST['mysql_username']) or
181 empty ($_REQUEST['mysql_password'])
182 )
183 {
184 echo "<input type=hidden name=save_config value=1>\n";
185 echo '<table>';
186 echo "<tr><td><label for=mysql_host>MySQL host:</label></td>";
187 echo "<td><input type=text name=mysql_host id=mysql_host value=localhost></td></tr>\n";
188 echo "<tr><td><label for=mysql_host>database:</label></td>";
189 echo "<td><input type=text name=mysql_db id=mysql_db value=racktables></td></tr>\n";
190 echo "<tr><td><label for=mysql_username>username:</label></td>";
191 echo "<td><input type=text name=mysql_username></td></tr>\n";
192 echo "<tr><td><label for=mysql_password>password:</label></td>";
193 echo "<td><input type=password name=mysql_password></td></tr>\n";
194 echo '</table>';
195 return FALSE;
196 }
197 $pdo_dsn = 'mysql:host=' . $_REQUEST['mysql_host'] . ';dbname=' . $_REQUEST['mysql_db'];
198 try
199 {
200 $dbxlink = new PDO ($pdo_dsn, $_REQUEST['mysql_username'], $_REQUEST['mysql_password']);
201 }
202 catch (PDOException $e)
203 {
204 echo "<input type=hidden name=save_config value=1>\n";
205 echo '<table>';
206 echo "<tr><td><label for=mysql_host>MySQL host:</label></td>";
207 echo "<td><input type=text name=mysql_host id=mysql_host value='" . $_REQUEST['mysql_host'] . "'></td></tr>\n";
208 echo "<tr><td><label for=mysql_host>database:</label></td>";
209 echo "<td><input type=text name=mysql_db id=mysql_db value='" . $_REQUEST['mysql_db'] . "'></td></tr>\n";
210 echo "<tr><td><label for=mysql_username>username:</label></td>";
211 echo "<td><input type=text name=mysql_username value='" . $_REQUEST['mysql_username'] . "'></td></tr>\n";
212 echo "<tr><td><label for=mysql_password>password:</label></td>";
213 echo "<td><input type=password name=mysql_password value='" . $_REQUEST['mysql_password'] . "'></td></tr>\n";
214 echo "<tr><td colspan=2>The above parameters did not work. Check and try again.</td></tr>\n";
215 echo '</table>';
216 return FALSE;
217 }
218
219 // Make sure InnoDB is supported
220 require_once 'inc/database.php';
221 if (!isInnoDBSupported ($dbxlink))
222 {
223 echo 'Error: InnoDB support is disabled. See the README for details.';
224 return FALSE;
225 }
226
227 $conf = fopen ('inc/secret.php', 'w+');
228 if ($conf === FALSE)
229 {
230 echo "Error: failed to open inc/secret.php for writing";
231 return FALSE;
232 }
233 fwrite ($conf, "<?php\n/* This file has been generated automatically by RackTables installer.\n");
234 fwrite ($conf, " * you shouldn't normally edit it unless your database setup has changed.\n");
235 fwrite ($conf, " */\n");
236 fwrite ($conf, "\$pdo_dsn = '${pdo_dsn}';\n");
237 fwrite ($conf, "\$db_username = '" . $_REQUEST['mysql_username'] . "';\n");
238 fwrite ($conf, "\$db_password = '" . $_REQUEST['mysql_password'] . "';\n\n");
239 fwrite ($conf, <<<ENDOFTEXT
240 // Default setting is to authenticate users locally, but it is possible to
241 // employ existing LDAP or Apache userbase. Uncommenting below two lines MAY
242 // help in switching authentication to LDAP completely.
243 // More info: http://racktables.org/trac/wiki/RackTablesUserAuthentication
244 #\$user_auth_src = 'ldap';
245 #\$require_local_account = FALSE;
246
247 // This is only necessary for 'ldap' authentication source
248 \$LDAP_options = array
249 (
250 'server' => 'some.server',
251 'domain' => 'some.domain',
252 # 'search_dn' => 'ou=people,O=YourCompany',
253 'search_attr' => 'uid',
254 # 'displayname_attrs' => 'givenname familyname',
255
256 // LDAP cache, values in seconds. Refresh, retry and expiry values are
257 // treated exactly as those for DNS SOA record. Example values 300-15-600:
258 // unconditionally remeber successful auth for 5 minutes, after that still
259 // permit user access, but try to revalidate username and password on the
260 // server (not more often, than once in 15 seconds). After 10 minutes of
261 // unsuccessful retries give up and deny access, so someone goes to fix
262 // LDAP server.
263 'cache_refresh' => 300,
264 'cache_retry' => 15,
265 'cache_expiry' => 600,
266 );
267
268
269 ENDOFTEXT
270 );
271 fwrite ($conf, "?>\n");
272 fclose ($conf);
273 echo "The configuration file has been written successfully.<br>";
274 return TRUE;
275 }
276
277 function connect_to_db ()
278 {
279 require ('inc/secret.php');
280 global $dbxlink;
281 try
282 {
283 $dbxlink = new PDO ($pdo_dsn, $db_username, $db_password);
284 }
285 catch (PDOException $e)
286 {
287 die ('Error connecting to the database');
288 }
289 }
290
291 function init_database_static ()
292 {
293 connect_to_db ();
294 global $dbxlink;
295 $result = $dbxlink->query ('show tables');
296 $tables = $result->fetchAll (PDO::FETCH_NUM);
297 $result->closeCursor();
298 unset ($result);
299 if (count ($tables))
300 {
301 echo 'Your database is already holding ' . count ($tables);
302 echo ' tables, so I will stop here and let you check it yourself.<br>';
303 echo 'There is some important data there probably.<br>';
304 return FALSE;
305 }
306 echo 'Initializing the database...<br>';
307 echo '<table border=1>';
308 echo "<tr><th>file</th><th>queries</th><th>errors</th></tr>";
309 $errlist = array();
310 foreach (array ('structure', 'dictbase', 'dictvendors') as $part)
311 {
312 $filename = "install/init-${part}.sql";
313 echo "<tr><td>${filename}</td>";
314 $f = fopen ("install/init-${part}.sql", 'r');
315 if ($f === FALSE)
316 {
317 echo "Failed to open install/init-${part}.sql for reading";
318 return FALSE;
319 }
320 $longq = '';
321 while (!feof ($f))
322 {
323 $line = fgets ($f);
324 if (ereg ('^--', $line))
325 continue;
326 $longq .= $line;
327 }
328 fclose ($f);
329 $nq = $nerrs = 0;
330 foreach (preg_split ("/;\s*\n/", $longq) as $query)
331 {
332 $query = trim($query);
333 if (empty ($query))
334 continue;
335 $nq++;
336 if ($dbxlink->exec ($query) === FALSE)
337 {
338 $nerrs++;
339 $errlist[] = $query;
340 }
341 }
342 echo "<td>${nq}</td><td>${nerrs}</td></tr>\n";
343 }
344 echo '</table>';
345 if (count ($errlist))
346 {
347 echo '<pre>The following queries failed:\n';
348 foreach ($errlist as $q)
349 echo "${q}\n\n";
350 echo '</pre>';
351 return FALSE;
352 }
353 return TRUE;
354 }
355
356 function init_database_dynamic ()
357 {
358 connect_to_db();
359 global $dbxlink;
360 if (!isset ($_REQUEST['password']) or empty ($_REQUEST['password']))
361 {
362 $result = $dbxlink->query ('select count(user_id) from UserAccount where user_id = 1');
363 $row = $result->fetch (PDO::FETCH_NUM);
364 $nrecs = $row[0];
365 $result->closeCursor();
366 if (!$nrecs)
367 {
368 echo '<table border=1>';
369 echo '<caption>Administrator password not set</caption>';
370 echo '<tr><td><input type=password name=password></td></tr>';
371 echo '</table>';
372 }
373 return FALSE;
374 }
375 else
376 {
377 // Never send cleartext password over the wire.
378 $hash = sha1 ($_REQUEST['password']);
379 $query = "INSERT INTO `UserAccount` (`user_id`, `user_name`, `user_password_hash`, `user_realname`) " .
380 "VALUES (1,'admin','${hash}','RackTables Administrator')";
381 $result = $dbxlink->exec ($query);
382 echo "Administrator password has been set successfully.<br>";
383 return TRUE;
384 }
385 }
386
387 function congrats ()
388 {
389 echo 'Congratulations! RackTables installation is complete. After pressing Proceed you will ';
390 echo 'enter the system. Authenticate with <strong>admin</strong> username.<br>';
391 echo "RackTables web-site runs some <a href='http://racktables.org/trac/wiki'>wiki</a> pages ";
392 echo "and <a href='http://racktables.org/trac/report/1'>a bug tracker</a>.<br>We have also got ";
393 echo "a <a href='http://www.freelists.org/list/racktables-users'>mailing list</a> for users. Have fun.<br>";
394 return TRUE;
395 }
396
397 ?>