setup.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. /**
  3. * Postfix Admin
  4. *
  5. * LICENSE
  6. * This source file is subject to the GPL license that is bundled with
  7. * this package in the file LICENSE.TXT.
  8. *
  9. * Further details on the project are available at http://postfixadmin.sf.net
  10. *
  11. * @version $Id: setup.php 1788 2015-05-18 20:18:09Z christian_boltz $
  12. * @license GNU GPL v2 or later.
  13. *
  14. * File: setup.php
  15. * Used to help ensure a server is setup appropriately during installation/setup.
  16. * After setup, it should be renamed or removed.
  17. *
  18. * Template File: -none-
  19. *
  20. * Template Variables: -none-
  21. *
  22. * Form POST \ GET Variables: -none-
  23. */
  24. define('POSTFIXADMIN', 1); # by defining it here, common.php will not start a session.
  25. require_once(dirname(__FILE__).'/common.php'); # make sure correct common.php is used.
  26. $CONF['show_header_text'] = 'NO';
  27. $CONF['theme_logo'] = 'images/logo-default.png';
  28. $CONF['theme_css'] = 'css/default.css';
  29. require($incpath.'/templates/header.php');
  30. ?>
  31. <div class='setup'>
  32. <h2>Postfix Admin Setup Checker</h2>
  33. <p>Running software:
  34. <ul>
  35. <?php
  36. //
  37. // Check for availablilty functions
  38. //
  39. $f_phpversion = function_exists ("phpversion");
  40. $f_apache_get_version = function_exists ("apache_get_version");
  41. $f_get_magic_quotes_gpc = function_exists ("get_magic_quotes_gpc");
  42. $f_mysql_connect = function_exists ("mysql_connect");
  43. $f_mysqli_connect = function_exists ("mysqli_connect");
  44. $f_pg_connect = function_exists ("pg_connect");
  45. $f_session_start = function_exists ("session_start");
  46. $f_preg_match = function_exists ("preg_match");
  47. $f_mb_encode_mimeheader = function_exists ("mb_encode_mimeheader");
  48. $f_imap_open = function_exists ("imap_open");
  49. $file_config = file_exists (realpath ("./config.inc.php"));
  50. $error = 0;
  51. //
  52. // Check for PHP version
  53. //
  54. if ($f_phpversion == 1)
  55. {
  56. if (phpversion() < 5) {
  57. print "<li><b>Error: Depends on: PHP v5</b><br /></li>\n";
  58. $error += 1;
  59. } elseif (version_compare(phpversion(), '5.2.3') < 0) {
  60. # smarty uses htmlentities() with 4 parameters, the 4th parameter was introduced in PHP 5.2.3
  61. # older PHP versions will cause warnings
  62. $phpversion = 5;
  63. print "<li><b>Recommended PHP version: >= 5.2.3, you have " . phpversion () . "</b></li>\n";
  64. } else {
  65. $phpversion = 5;
  66. print "<li>PHP version " . phpversion () . "</li>\n";
  67. }
  68. # TODO: check for PHP >= 5.2.3 - smarty uses htmlentities with 4 parameters. The forth parameter was added in PHP 5.2.3, older versions will give a warning
  69. }
  70. else
  71. {
  72. print "<li><b>Unable to check for PHP version. (missing function: phpversion())</b></li>\n";
  73. }
  74. //
  75. // Check for Apache version
  76. //
  77. if ($f_apache_get_version == 1)
  78. {
  79. print "<li>" . apache_get_version() . "</li>\n";
  80. }
  81. else
  82. {
  83. # not running on Apache.
  84. # However postfixadmin _is_ running, so obviously we are on a supported webserver ;-))
  85. # No need to confuse the user with a warning.
  86. }
  87. print "</ul>";
  88. print "<p>Checking for dependencies:\n";
  89. print "<ul>\n";
  90. //
  91. // Check for Magic Quotes
  92. //
  93. if ($f_get_magic_quotes_gpc == 1)
  94. {
  95. if (get_magic_quotes_gpc () == 0)
  96. {
  97. print "<li>Magic Quotes: Disabled - OK</li>\n";
  98. }
  99. else
  100. {
  101. print "<li><b>Warning: Magic Quotes: ON (internal workaround used)</b></li>\n";
  102. }
  103. }
  104. else
  105. {
  106. print "<li><b>Unable to check for Magic Quotes. (missing function: get_magic_quotes_gpc())</b></li>\n";
  107. }
  108. //
  109. // Check for config.inc.php
  110. //
  111. $config_loaded = 0;
  112. if ($file_config == 1)
  113. {
  114. print "<li>Depends on: presence config.inc.php - OK</li>\n";
  115. require_once($incpath.'/config.inc.php');
  116. $config_loaded = 1;
  117. if(isset($CONF['configured'])) {
  118. if($CONF['configured'] === TRUE) {
  119. print "<li>Checking \$CONF['configured'] - OK\n";
  120. } else {
  121. print "<li><b>Warning: \$CONF['configured'] is 'false'.<br>\n";
  122. print "You must edit your config.inc.php and change this to true (this indicates you've created the database and user)</b>\n";
  123. }
  124. }
  125. }
  126. else
  127. {
  128. print "<li><b>Error: Depends on: presence config.inc.php - NOT FOUND</b><br /></li>\n";
  129. print "Create the file, and edit as appropriate (e.g. select database type etc)<br />";
  130. print "For example:<br />\n";
  131. print "<code><pre>cp config.inc.php.sample config.inc.php</pre></code>\n";
  132. $error =+ 1;
  133. }
  134. //
  135. // Check if templates directory is writable
  136. //
  137. if (!is_writeable($incpath.'/templates_c'))
  138. {
  139. print "<li><b>Error: Smarty template compile directory templates_c is not writable.</b><br />\n";
  140. print "<b>Please make it writable.</b><br />\n";
  141. print "<b>If you are using SELinux or AppArmor, you might need to adjust their setup to allow write access.</b><br />\n";
  142. $error =+ 1;
  143. } else {
  144. print "<li>Smarty template compile directory is writable - OK<br />\n";
  145. }
  146. //
  147. // Check if there is support for at least 1 database
  148. //
  149. if (($f_mysql_connect == 0) and ($f_mysqli_connect == 0) and ($f_pg_connect == 0))
  150. {
  151. print "<li><b>Error: There is no database support in your PHP setup</b><br />\n";
  152. print "To install MySQL 3.23 or 4.0 support on FreeBSD:<br />\n";
  153. print "<pre>% cd /usr/ports/databases/php$phpversion-mysql/\n";
  154. print "% make clean install\n";
  155. print " - or with portupgrade -\n";
  156. print "% portinstall php$phpversion-mysql</pre>\n";
  157. if ($phpversion >= 5)
  158. {
  159. print "To install MySQL 4.1 support on FreeBSD:<br />\n";
  160. print "<pre>% cd /usr/ports/databases/php5-mysqli/\n";
  161. print "% make clean install\n";
  162. print " - or with portupgrade -\n";
  163. print "% portinstall php5-mysqli</pre>\n";
  164. }
  165. print "To install PostgreSQL support on FreeBSD:<br />\n";
  166. print "<pre>% cd /usr/ports/databases/php$phpversion-pgsql/\n";
  167. print "% make clean install\n";
  168. print " - or with portupgrade -\n";
  169. print "% portinstall php$phpversion-pgsql</pre></li>\n";
  170. $error =+ 1;
  171. }
  172. //
  173. // MySQL 3.23, 4.0 functions
  174. //
  175. if ($f_mysql_connect == 1)
  176. {
  177. print "<li>Depends on: MySQL 3.23, 4.0 - OK</li>\n";
  178. }
  179. //
  180. // MySQL 4.1 functions
  181. //
  182. if ($phpversion >= 5)
  183. {
  184. if ($f_mysqli_connect == 1)
  185. {
  186. print "<li>Depends on: MySQL 4.1 - OK\n";
  187. if ( !($config_loaded && $CONF['database_type'] == 'mysqli') ) {
  188. print "<br>(change the database_type to 'mysqli' in config.inc.php if you want to use MySQL)\n";
  189. }
  190. print "</li>";
  191. }
  192. }
  193. //
  194. // PostgreSQL functions
  195. //
  196. if ($f_pg_connect == 1)
  197. {
  198. print "<li>Depends on: PostgreSQL - OK \n";
  199. if ( !($config_loaded && $CONF['database_type'] == 'pgsql') ) {
  200. print "<br>(change the database_type to 'pgsql' in config.inc.php if you want to use PostgreSQL)\n";
  201. }
  202. print "</li>";
  203. }
  204. //
  205. // Database connection
  206. //
  207. if ($config_loaded) {
  208. list ($link, $error_text) = db_connect(TRUE);
  209. if ($error_text == "") {
  210. print "<li>Testing database connection - OK - {$CONF['database_type']}://{$CONF['database_user']}:xxxxx@{$CONF['database_host']}/{$CONF['database_name']}</li>";
  211. } else {
  212. print "<li><b>Error: Can't connect to database</b><br />\n";
  213. print "Please edit the \$CONF['database_*'] parameters in config.inc.php.\n";
  214. print "$error_text</li>\n";
  215. $error ++;
  216. }
  217. }
  218. //
  219. // Session functions
  220. //
  221. if ($f_session_start == 1)
  222. {
  223. print "<li>Depends on: session - OK</li>\n";
  224. }
  225. else
  226. {
  227. print "<li><b>Error: Depends on: session - NOT FOUND</b><br />\n";
  228. print "To install session support on FreeBSD:<br />\n";
  229. print "<pre>% cd /usr/ports/www/php$phpversion-session/\n";
  230. print "% make clean install\n";
  231. print " - or with portupgrade -\n";
  232. print "% portinstall php$phpversion-session</pre></li>\n";
  233. $error =+ 1;
  234. }
  235. //
  236. // PCRE functions
  237. //
  238. if ($f_preg_match == 1)
  239. {
  240. print "<li>Depends on: pcre - OK</li>\n";
  241. }
  242. else
  243. {
  244. print "<li><b>Error: Depends on: pcre - NOT FOUND</b><br />\n";
  245. print "To install pcre support on FreeBSD:<br />\n";
  246. print "<pre>% cd /usr/ports/devel/php$phpversion-pcre/\n";
  247. print "% make clean install\n";
  248. print " - or with portupgrade -\n";
  249. print "% portinstall php$phpversion-pcre</pre></li>\n";
  250. $error =+ 1;
  251. }
  252. //
  253. // Multibyte functions
  254. //
  255. if ( $f_mb_encode_mimeheader == 1 )
  256. {
  257. print "<li>Depends on: multibyte string - OK</li>\n";
  258. }
  259. else
  260. {
  261. print "<li><b>Error: Depends on: multibyte string - NOT FOUND</b><br />\n";
  262. print "To install multibyte string support, install php$phpversion-mbstring</li>\n";
  263. $error =+ 1;
  264. }
  265. //
  266. // Imap functions
  267. //
  268. if ( $f_imap_open == 1)
  269. {
  270. print "<li>Depends on: IMAP functions - OK</li>\n";
  271. }
  272. else
  273. {
  274. print "<li><b>Warning: Depends on: IMAP functions - NOT FOUND</b><br />\n";
  275. print "To install IMAP support, install php$phpversion-imap<br />\n";
  276. print "Without IMAP support, you won't be able to create subfolders when creating mailboxes.</li>\n";
  277. # $error =+ 1;
  278. }
  279. print "</ul>";
  280. if ($error != 0)
  281. {
  282. print "<p><b>Please fix the errors listed above.</b></p>";
  283. }
  284. else
  285. {
  286. print "<p>Everything seems fine... attempting to create/update database structure</p>\n";
  287. require_once($incpath.'/upgrade.php');
  288. $tUsername = '';
  289. $setupMessage = '';
  290. $lostpw_error = 0;
  291. $setuppw = "";
  292. if (isset($CONF['setup_password'])) $setuppw = $CONF['setup_password'];
  293. if (safepost("form") == "setuppw") {
  294. # "setup password" form submitted
  295. if (safepost('setup_password') != safepost('setup_password2')) {
  296. $setupMessage = "The two passwords differ!";
  297. $lostpw_error = 1;
  298. } else {
  299. list ($lostpw_error, $lostpw_result) = check_setup_password(safepost('setup_password'), 1);
  300. $setupMessage = $lostpw_result;
  301. $setuppw = "changed";
  302. }
  303. } elseif (safepost("form") == "createadmin") {
  304. # "create admin" form submitted
  305. list ($pw_check_error, $pw_check_result) = check_setup_password(safepost('setup_password'));
  306. if ($pw_check_result != 'pass_OK') {
  307. $error += 1;
  308. $setupMessage = $pw_check_result;
  309. }
  310. if($error == 0 && $pw_check_result == 'pass_OK') {
  311. // XXX need to ensure domains table includes an 'ALL' entry.
  312. $table_domain = table_by_key('domain');
  313. $r = db_query("SELECT * FROM $table_domain WHERE domain = 'ALL'");
  314. if($r['rows'] == 0) {
  315. db_insert('domain', array('domain' => 'ALL', 'description' => '', 'transport' => '') ); // all other fields should default through the schema.
  316. }
  317. $values = array(
  318. 'username' => safepost('username'),
  319. 'password' => safepost('password'),
  320. 'password2' => safepost('password2'),
  321. 'superadmin' => 1,
  322. 'domains' => array(),
  323. 'active' => 1,
  324. );
  325. list ($error, $setupMessage, $errormsg) = create_admin($values);
  326. if ($error != 0) {
  327. $tUsername = htmlentities($values['username']);
  328. } else {
  329. $setupMessage .= "<p>You are done with your basic setup. ";
  330. $setupMessage .= "<p><b>You can now <a href='login.php'>login to PostfixAdmin</a> using the account you just created.</b>";
  331. }
  332. }
  333. }
  334. if ( ($setuppw == "" || $setuppw == "changeme" || safeget("lostpw") == 1 || $lostpw_error != 0) /* && $_SERVER['REQUEST_METHOD'] != "POST" */ ) {
  335. # show "create setup password" form
  336. ?>
  337. <div class="standout"><?php print $setupMessage; ?></div>
  338. <div id="edit_form">
  339. <form name="setuppw" method="post" action="setup.php">
  340. <input type="hidden" name="form" value="setuppw" />
  341. <table>
  342. <td colspan="3"><h3>Change setup password</h3></td>
  343. </tr>
  344. <tr>
  345. <td>Setup password</td>
  346. <td><input class="flat" type="password" name="setup_password" value="" /></td>
  347. <td></td>
  348. </tr>
  349. <tr>
  350. <td>Setup password (again)</td>
  351. <td><input class="flat" type="password" name="setup_password2" value="" /></td>
  352. <td></td>
  353. </tr>
  354. <tr>
  355. <td colspan="3" class="hlp_center"><input class="button" type="submit" name="submit" value="Generate password hash" /></td>
  356. </tr>
  357. </table>
  358. </form>
  359. </div>
  360. <?php
  361. } elseif ($_SERVER['REQUEST_METHOD'] == "GET" || $error != 0 || $lostpw_error == 0) {
  362. ?>
  363. <div class="standout"><?php print $setupMessage; ?></div>
  364. <div id="edit_form">
  365. <form name="create_admin" method="post">
  366. <input type="hidden" name="form" value="createadmin" />
  367. <table>
  368. <td colspan="3"><h3>Create superadmin account</h3></td>
  369. </tr>
  370. <tr>
  371. <td>Setup password</td>
  372. <td><input class="flat" type="password" name="setup_password" value="" /></td>
  373. <td><a href="setup.php?lostpw=1">Lost password?</a></td>
  374. </tr>
  375. <tr>
  376. <td><?php print $PALANG['admin'] . ":"; ?></td>
  377. <td><input class="flat" type="text" name="username" value="<?php print $tUsername; ?>" /></td>
  378. <td><?php if (isset($errormsg['username'])) print $errormsg['username']; ?><?php print $PALANG['email_address'] ?></td>
  379. </tr>
  380. <tr>
  381. <td><?php print $PALANG['password'] . ":"; ?></td>
  382. <td><input class="flat" type="password" name="password" /></td>
  383. <td><?php if (isset($errormsg['password'])) print $errormsg['password']; ?></td>
  384. </tr>
  385. <tr>
  386. <td><?php print $PALANG['password_again'] . ":"; ?></td>
  387. <td><input class="flat" type="password" name="password2" /></td>
  388. <td><?php if (isset($errormsg['password2'])) print $errormsg['password2']; ?></td>
  389. </tr>
  390. <tr>
  391. <td colspan="3" class="hlp_center"><input class="button" type="submit" name="submit" value="<?php print $PALANG['pAdminCreate_admin_button']; ?>" /></td>
  392. </tr>
  393. </table>
  394. </form>
  395. </div>
  396. <?php
  397. }
  398. print "<b>Since version 2.3 there is no requirement to delete setup.php!</b><br />\n";
  399. print "<b>Check the config.inc.php file for any other settings that you might need to change!<br />\n";
  400. }
  401. ?>
  402. </div>
  403. </body>
  404. </html>
  405. <?php
  406. function generate_setup_password_salt() {
  407. $salt = time() . '*' . $_SERVER['REMOTE_ADDR'] . '*' . mt_rand(0,60000);
  408. $salt = md5($salt);
  409. return $salt;
  410. }
  411. function encrypt_setup_password($password, $salt) {
  412. return $salt . ':' . sha1($salt . ':' . $password);
  413. }
  414. /*
  415. returns: array(
  416. 'error' => 0 (or 1),
  417. 'message => text
  418. )
  419. */
  420. function check_setup_password($password, $lostpw_mode = 0) {
  421. global $CONF;
  422. $error = 1; # be pessimistic
  423. $setuppw = "";
  424. if (isset($CONF['setup_password'])) $setuppw = $CONF['setup_password'];
  425. list($confsalt, $confpass, $trash) = explode(':', $setuppw . '::');
  426. $pass = encrypt_setup_password($password, $confsalt);
  427. $validpass = validate_password($password);
  428. if ($password == "" ) { # no password specified?
  429. $result = "Setup password must be specified<br />If you didn't set up a setup password yet, enter the password you want to use.";
  430. } elseif (count($validpass) > 0) {
  431. $result = $validpass[0]; # TODO: honor all error messages, not only the first one
  432. } elseif ($pass == $setuppw && $lostpw_mode == 0) { # correct passsword (and not asking for a new password)
  433. $result = "pass_OK";
  434. $error = 0;
  435. } else {
  436. $pass = encrypt_setup_password($password, generate_setup_password_salt());
  437. $result = "";
  438. if ($lostpw_mode == 1) {
  439. $error = 0; # non-matching password is expected when the user asks for a new password
  440. } else {
  441. $result = '<p><b>Setup password not specified correctly</b></p>';
  442. }
  443. $result .= '<p>If you want to use the password you entered as setup password, edit config.inc.php or config.local.php and set</p>';
  444. $result .= "<pre>\$CONF['setup_password'] = '$pass';</pre>";
  445. }
  446. return array ($error, $result);
  447. }
  448. function create_admin($values) {
  449. DEFINE('POSTFIXADMIN_SETUP', 1); # avoids instant redirect to login.php after creating the admin
  450. $handler = new AdminHandler(1, 'setup.php');
  451. $formconf = $handler->webformConfig();
  452. if (!$handler->init($values['username'])) {
  453. return array(1, "", $handler->errormsg);
  454. }
  455. if (!$handler->set($values)) {
  456. return array(1, "", $handler->errormsg);
  457. }
  458. if (!$handler->store()) {
  459. return array(1, "", $handler->errormsg);
  460. }
  461. return array(
  462. 0,
  463. $handler->infomsg['success'],
  464. array(),
  465. );
  466. }
  467. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
  468. ?>