Setting ‘User Cannot Change Password’ using Python
Adding users manually to [[Active_Directory|Active Directory]] can be a chore, especially if you have a lot of users to add and/or you need to remember to set several options. Fortunately AD is readily scriptable. I generally use Python for this purpose, and there are numerous examples for how to do things like add a new user or add an account to a group.
One thing I couldn’t find was a simple account of how to set the option which the ADUC ‘New User’ wizard labels ‘User Cannot Change Password’. This option is not as simple as setting a flag when creating the user object; you have to add an ACE denying the ‘Change Password’ permission. Microsoft helpfully document the process, so it’s just a matter of translating the instructions into Python.
# See http://msdn.microsoft.com/en-us/library/aa746398.aspx # Standard definitions ChangePasswordGuid = '{ab721a53-1e2f-11d0-9819-00aa0040529b}' ADS_ACETYPE_ACCESS_DENIED_OBJECT = 0x6 SID_SELF = "S-1-5-10" SID_EVERYONE = "S-1-1-0" # Get the full localised names for 'Everyone' and 'Self' # For English language Domain Controllers this is equivalent to: # selfName = u'NT AUTHORITY\\SELF' # everyoneName = u'Everyone' selfAccount = win32security.LookupAccountSid(None, win32security.GetBinarySid(SID_SELF)) everyoneAccount = win32security.LookupAccountSid(None, win32security.GetBinarySid(SID_EVERYONE)) selfName = ("%s\\%s" % (selfAccount[1], selfAccount[0])).strip('\\') everyoneName = ("%s\\%s" % (everyoneAccount[1], everyoneAccount[0])).strip('\\') user = active_directory.find_user(userName) sd = user.ntSecurityDescriptor dacl = sd.DiscretionaryAcl for ace in dacl: if ace.ObjectType.lower() == ChangePasswordGuid.lower(): if ace.Trustee == selfName or ace.Trustee == everyoneName: ace.AceType = ADS_ACETYPE_ACCESS_DENIED_OBJECT sd.DiscretionaryAcl = dacl user.Put('ntSecurityDescriptor', sd) user.SetInfo() |
Two things to note:
First, I’ve assumed that the name of the user to modify is stored in ‘userName
‘, and used the active_directory module to do the lookup. Alternatively the object can be retrieved using win32com
directly, by doing something like the following:
location = "cn=Users,dc=example,dc=org" |
or
location = "ou=Roaming Users,ou=My User OU,dc=example,dc=org" |
followed by
user = win32com.client.GetObject("LDAP://cn=%s,%s" % (userName, location)) |
In the active_directory
case userName
could be either the logon name (eg. ‘jsmith’) or the canonical name (eg. ‘John Smith’). In the win32com
case it has to be the latter.
Second, in my case the user objects already had the relevant ACEs so it was just a case of setting them to ‘deny’ when necessary. Microsoft’s documentation describes how to add the entries if they’re absent, so presumably there must be circumstances in which that would happen. Translating that description into Python is a simple enough process that it’s left as an exercise for the reader.