NetValidatePasswordPolicy.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """A demo of using win32net.NetValidatePasswordPolicy.
  2. Example usage:
  3. % NetValidatePasswordPolicy.py --password=foo change
  4. which might return:
  5. > Result of 'change' validation is 0: The operation completed successfully.
  6. or depending on the policy:
  7. > Result of 'change' validation is 2245: The password does not meet the
  8. > password policy requirements. Check the minimum password length,
  9. > password complexity and password history requirements.
  10. Adding --user doesn't seem to change the output (even the PasswordLastSet seen
  11. when '-f' is used doesn't depend on the username), but theoretically it will
  12. also check the password history for the specified user.
  13. % NetValidatePasswordPolicy.py auth
  14. which always (with and without '-m') seems to return:
  15. > Result of 'auth' validation is 2701: Password must change at next logon
  16. """
  17. import sys
  18. import win32api
  19. import win32net, win32netcon
  20. import optparse
  21. from pprint import pprint
  22. def main():
  23. parser = optparse.OptionParser("%prog [options] auth|change ...",
  24. description="A win32net.NetValidatePasswordPolicy demo.")
  25. parser.add_option("-u", "--username",
  26. action="store",
  27. help="The username to pass to the function (only for the "
  28. "change command")
  29. parser.add_option("-p", "--password",
  30. action="store",
  31. help="The clear-text password to pass to the function "
  32. "(only for the 'change' command)")
  33. parser.add_option("-m", "--password-matched",
  34. action="store_false", default=True,
  35. help="Used to specify the password does NOT match (ie, "
  36. "uses False for the PasswordMatch/PasswordMatched "
  37. "arg, both 'auth' and 'change' commands)")
  38. parser.add_option("-s", "--server",
  39. action="store",
  40. help="The name of the server to execute the command on")
  41. parser.add_option("-f", "--show_fields",
  42. action="store_true", default=False,
  43. help="Print the NET_VALIDATE_PERSISTED_FIELDS returned")
  44. options, args = parser.parse_args()
  45. if not args:
  46. args = ["auth"]
  47. for arg in args:
  48. if arg == "auth":
  49. input = {"PasswordMatched": options.password_matched,
  50. }
  51. val_type = win32netcon.NetValidateAuthentication
  52. elif arg == "change":
  53. input = {"ClearPassword": options.password,
  54. "PasswordMatch": options.password_matched,
  55. "UserAccountName": options.username,
  56. }
  57. val_type = win32netcon.NetValidatePasswordChange
  58. else:
  59. parser.error("Invalid arg - must be 'auth' or 'change'")
  60. try:
  61. fields, status = win32net.NetValidatePasswordPolicy(options.server,
  62. None, val_type, input)
  63. except NotImplementedError:
  64. print("NetValidatePasswordPolicy not implemented on this platform.")
  65. return 1
  66. except win32net.error as exc:
  67. print("NetValidatePasswordPolicy failed: ", exc)
  68. return 1
  69. if options.show_fields:
  70. print("NET_VALIDATE_PERSISTED_FIELDS fields:")
  71. pprint(fields)
  72. print("Result of %r validation is %d: %s" % \
  73. (arg, status, win32api.FormatMessage(status).strip()))
  74. return 0
  75. if __name__=='__main__':
  76. sys.exit(main())