Development of an internal social media platform with personalised dashboards for students
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.

modifyPassword.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """
  2. """
  3. # Created on 2015.11.27
  4. #
  5. # Author: Giovanni Cannata
  6. #
  7. # Copyright 2015 - 2018 Giovanni Cannata
  8. #
  9. # This file is part of ldap3.
  10. #
  11. # ldap3 is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU Lesser General Public License as published
  13. # by the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # ldap3 is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU Lesser General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU Lesser General Public License
  22. # along with ldap3 in the COPYING and COPYING.LESSER files.
  23. # If not, see <http://www.gnu.org/licenses/>.
  24. from ... import MODIFY_REPLACE, MODIFY_DELETE, MODIFY_ADD
  25. from ...utils.log import log, log_enabled, PROTOCOL
  26. from ...core.results import RESULT_SUCCESS
  27. from ...utils.dn import safe_dn
  28. from ...utils.conv import to_unicode
  29. def ad_modify_password(connection, user_dn, new_password, old_password, controls=None):
  30. # old password must be None to reset password with sufficient privileges
  31. if connection.check_names:
  32. user_dn = safe_dn(user_dn)
  33. if str is bytes: # python2, converts to unicode
  34. new_password = to_unicode(new_password)
  35. if old_password:
  36. old_password = to_unicode(old_password)
  37. encoded_new_password = ('"%s"' % new_password).encode('utf-16-le')
  38. if old_password: # normal users must specify old and new password
  39. encoded_old_password = ('"%s"' % old_password).encode('utf-16-le')
  40. result = connection.modify(user_dn,
  41. {'unicodePwd': [(MODIFY_DELETE, [encoded_old_password]),
  42. (MODIFY_ADD, [encoded_new_password])]},
  43. controls)
  44. else: # admin users can reset password without sending the old one
  45. result = connection.modify(user_dn,
  46. {'unicodePwd': [(MODIFY_REPLACE, [encoded_new_password])]},
  47. controls)
  48. if not connection.strategy.sync:
  49. _, result = connection.get_response(result)
  50. else:
  51. result = connection.result
  52. # change successful, returns True
  53. if result['result'] == RESULT_SUCCESS:
  54. return True
  55. # change was not successful, raises exception if raise_exception = True in connection or returns the operation result, error code is in result['result']
  56. if connection.raise_exceptions:
  57. from ...core.exceptions import LDAPOperationResult
  58. if log_enabled(PROTOCOL):
  59. log(PROTOCOL, 'operation result <%s> for <%s>', result, connection)
  60. raise LDAPOperationResult(result=result['result'], description=result['description'], dn=result['dn'], message=result['message'], response_type=result['type'])
  61. return False