Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

NetValidatePasswordPolicy.py 3.4KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 optparse
  18. import sys
  19. from pprint import pprint
  20. import win32api
  21. import win32net
  22. import win32netcon
  23. def main():
  24. parser = optparse.OptionParser(
  25. "%prog [options] auth|change ...",
  26. description="A win32net.NetValidatePasswordPolicy demo.",
  27. )
  28. parser.add_option(
  29. "-u",
  30. "--username",
  31. action="store",
  32. help="The username to pass to the function (only for the " "change command",
  33. )
  34. parser.add_option(
  35. "-p",
  36. "--password",
  37. action="store",
  38. help="The clear-text password to pass to the function "
  39. "(only for the 'change' command)",
  40. )
  41. parser.add_option(
  42. "-m",
  43. "--password-matched",
  44. action="store_false",
  45. default=True,
  46. help="Used to specify the password does NOT match (ie, "
  47. "uses False for the PasswordMatch/PasswordMatched "
  48. "arg, both 'auth' and 'change' commands)",
  49. )
  50. parser.add_option(
  51. "-s",
  52. "--server",
  53. action="store",
  54. help="The name of the server to execute the command on",
  55. )
  56. parser.add_option(
  57. "-f",
  58. "--show_fields",
  59. action="store_true",
  60. default=False,
  61. help="Print the NET_VALIDATE_PERSISTED_FIELDS returned",
  62. )
  63. options, args = parser.parse_args()
  64. if not args:
  65. args = ["auth"]
  66. for arg in args:
  67. if arg == "auth":
  68. input = {
  69. "PasswordMatched": options.password_matched,
  70. }
  71. val_type = win32netcon.NetValidateAuthentication
  72. elif arg == "change":
  73. input = {
  74. "ClearPassword": options.password,
  75. "PasswordMatch": options.password_matched,
  76. "UserAccountName": options.username,
  77. }
  78. val_type = win32netcon.NetValidatePasswordChange
  79. else:
  80. parser.error("Invalid arg - must be 'auth' or 'change'")
  81. try:
  82. fields, status = win32net.NetValidatePasswordPolicy(
  83. options.server, None, val_type, input
  84. )
  85. except NotImplementedError:
  86. print("NetValidatePasswordPolicy not implemented on this platform.")
  87. return 1
  88. except win32net.error as exc:
  89. print("NetValidatePasswordPolicy failed: ", exc)
  90. return 1
  91. if options.show_fields:
  92. print("NET_VALIDATE_PERSISTED_FIELDS fields:")
  93. pprint(fields)
  94. print(
  95. "Result of %r validation is %d: %s"
  96. % (arg, status, win32api.FormatMessage(status).strip())
  97. )
  98. return 0
  99. if __name__ == "__main__":
  100. sys.exit(main())