PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
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.

138 lines
3.7 KiB

  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 https://github.com/postfixadmin/postfixadmin
  10. *
  11. * @version $Id$
  12. * @license GNU GPL v2 or later.
  13. *
  14. * File: app-passwords.php
  15. * Used by users to view and change their app passwords.
  16. * Template File: app-passwords.tpl
  17. *
  18. *
  19. * Form POST \ GET Variables:
  20. *
  21. * fPassword_current
  22. * fAppDesc
  23. * fAppPass
  24. * fAppId
  25. *
  26. */
  27. require_once('../common.php');
  28. $smarty = PFASmarty::getInstance();
  29. $smarty->configureTheme($smarty->getRelPath());
  30. $username = authentication_get_username();
  31. $pPassword_text = "";
  32. $pUser_text = '';
  33. $pUser = '';
  34. if (authentication_has_role('global-admin')) {
  35. $login = new Login('admin');
  36. $admin = 2;
  37. $passwords = getAllAppPasswords();
  38. } elseif (authentication_has_role('admin')) {
  39. $login = new Login('admin');
  40. $admin = 1;
  41. $passwords = getAppPasswordsFor($username);
  42. } else {
  43. $login = new Login('mailbox');
  44. $admin = 0;
  45. $passwords = getAppPasswordsFor($username);
  46. }
  47. if ($_SERVER['REQUEST_METHOD'] == "POST") {
  48. if (safepost('token') != $_SESSION['PFA_token']) {
  49. die('Invalid token!');
  50. }
  51. if (isset($_POST['fCancel'])) {
  52. header("Location: main.php");
  53. exit(0);
  54. }
  55. if (isset($_POST['fAppPass'])) {
  56. $fPass = $_POST['fPassword_current'];
  57. $fAppDesc = $_POST['fAppDesc'];
  58. $fAppPass = $_POST['fAppPass'];
  59. try {
  60. if ($login->addAppPassword($username, $fPass, $fAppDesc, $fAppPass)) {
  61. flash_info($PALANG['pAppPassAdd_result_success']);
  62. header("Location: app-passwords.php");
  63. exit(0);
  64. } else {
  65. flash_error(Config::Lang_f('pAppPassAdd_result_error', $username));
  66. }
  67. } catch (\Exception $e) {
  68. flash_error($e->getMessage());
  69. }
  70. }
  71. if (isset($_POST['fAppId']) && is_numeric($_POST['fAppId'])) {
  72. $fAppId = (int)$_POST['fAppId'];
  73. // $username should be from $_SESSION and not modifiable by the end user
  74. // we don't want someone to be able to delete someone else's app password by guessing an id...
  75. $row = db_query_one('SELECT id FROM mailbox_app_password WHERE id = :id AND username = :username', ['username' => $username, 'id' => $fAppId]);
  76. if (!empty($row)) {
  77. $result = db_delete('mailbox_app_password', 'id', $row['id']);
  78. if ($result == 1) {
  79. flash_info($PALANG['pTotp_exceptions_revoked']);
  80. header("Location: app-passwords.php");
  81. exit(0);
  82. }
  83. }
  84. flash_error($PALANG['pPassword_result_error']);
  85. }
  86. }
  87. foreach ($passwords as $n => $pass) {
  88. if ($pass['username'] == $username) {
  89. $passwords[$n]['edit'] = 1;
  90. }
  91. if ($admin == 2) {
  92. $passwords[$n]['edit'] = 1;
  93. }
  94. }
  95. $smarty->assign('SESSID_USERNAME', $username);
  96. $smarty->assign('pPassword_text', $pPassword_text, false);
  97. $smarty->assign('pUser_text', $pUser_text, false);
  98. $smarty->assign('pUser', $pUser, false);
  99. $smarty->assign('pPasswords', $passwords, false);
  100. $smarty->assign('smarty_template', 'app-passwords');
  101. $smarty->display('index.tpl');
  102. /**
  103. * @return array
  104. */
  105. function getAllAppPasswords()
  106. {
  107. return db_query_all("SELECT * FROM mailbox_app_password");
  108. }
  109. /**
  110. * @param string $username
  111. * @return array
  112. * @todo if $username is a domain admin, we should return all app passwords for that domain.
  113. */
  114. function getAppPasswordsFor(string $username): array
  115. {
  116. return db_query_all("SELECT * FROM mailbox_app_password WHERE username = :username", ['username' => $username]);
  117. }
  118. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */