You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

setup.php 17KB

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