PageRenderTime 49ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/password/drivers/virtualmin.php

https://github.com/trimbakgopalghare/roundcubemail
PHP | 79 lines | 58 code | 8 blank | 13 comment | 5 complexity | a0e19b0302addd77b3b6194b935c9f81 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Virtualmin Password Driver
  4. *
  5. * Driver that adds functionality to change the users Virtualmin password.
  6. * The code is derrived from the Squirrelmail "Change Cyrus/SASL Password" Plugin
  7. * by Thomas Bruederli.
  8. *
  9. * It only works with virtualmin on the same host where Roundcube runs
  10. * and requires shell access and gcc in order to compile the binary.
  11. *
  12. * @version 3.0
  13. * @author Martijn de Munnik
  14. */
  15. class rcube_virtualmin_password
  16. {
  17. function save($currpass, $newpass)
  18. {
  19. $rcmail = rcmail::get_instance();
  20. $format = $rcmail->config->get('password_virtualmin_format', 0);
  21. $username = $_SESSION['username'];
  22. switch ($format) {
  23. case 1: // username%domain
  24. $domain = substr(strrchr($username, "%"), 1);
  25. break;
  26. case 2: // username.domain (could be bogus)
  27. $pieces = explode(".", $username);
  28. $domain = $pieces[count($pieces)-2]. "." . end($pieces);
  29. break;
  30. case 3: // domain.username (could be bogus)
  31. $pieces = explode(".", $username);
  32. $domain = $pieces[0]. "." . $pieces[1];
  33. break;
  34. case 4: // username-domain
  35. $domain = substr(strrchr($username, "-"), 1);
  36. break;
  37. case 5: // domain-username
  38. $domain = str_replace(strrchr($username, "-"), "", $username);
  39. break;
  40. case 6: // username_domain
  41. $domain = substr(strrchr($username, "_"), 1);
  42. break;
  43. case 7: // domain_username
  44. $pieces = explode("_", $username);
  45. $domain = $pieces[0];
  46. break;
  47. default: // username@domain
  48. $domain = substr(strrchr($username, "@"), 1);
  49. }
  50. if (!$domain) {
  51. $domain = $rcmail->user->get_username('domain');
  52. }
  53. $username = escapeshellcmd($username);
  54. $domain = escapeshellcmd($domain);
  55. $newpass = escapeshellcmd($newpass);
  56. $curdir = RCUBE_PLUGINS_DIR . 'password/helpers';
  57. exec("$curdir/chgvirtualminpasswd modify-user --domain $domain --user $username --pass $newpass", $output, $returnvalue);
  58. if ($returnvalue == 0) {
  59. return PASSWORD_SUCCESS;
  60. }
  61. else {
  62. rcube::raise_error(array(
  63. 'code' => 600,
  64. 'type' => 'php',
  65. 'file' => __FILE__, 'line' => __LINE__,
  66. 'message' => "Password plugin: Unable to execute $curdir/chgvirtualminpasswd"
  67. ), true, false);
  68. }
  69. return PASSWORD_ERROR;
  70. }
  71. }