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.

158 lines
5.4 KiB

  1. #!/usr/bin/python2
  2. # coding=utf-8
  3. import ldb
  4. from samba.samdb import SamDB
  5. from samba.auth import system_session
  6. # from samba.ndr import ndr_pack, ndr_unpack
  7. # from samba.dcerpc import security
  8. import samba.param
  9. # import base64
  10. # import binascii
  11. import time
  12. base = "CN=Users,DC=techgrow,DC=local" # можно не заморачиваться с ОУ и тогда base = "CN=Users,DC=myDom,DC=lan"
  13. domainName = 'techgrow.local'
  14. class UserAd:
  15. def __init__(self, sam, base, domainName, usrName, usrPass=''):
  16. self.sam = sam
  17. self.base = base
  18. self.domainName = domainName
  19. self.usrName = usrName
  20. self.usrPass = usrPass
  21. self.exists = self.chek_if_exists()
  22. self.memberOf = self.check_memberof()
  23. def chek_if_exists(self):
  24. expression = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=%s))" % self.usrName
  25. if len(self.sam.search(base=base, expression=expression, attrs=['sAMAccountName'])) > 0:
  26. return True
  27. else:
  28. return False
  29. def check_memberof(self):
  30. expression = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=%s))" % self.usrName
  31. res = self.sam.search(base=base, expression=expression, attrs=['memberOf'])
  32. groups = []
  33. if len(res) == 0 or len(res[0]) == 0:
  34. return groups
  35. for k in res[0]['memberOf']:
  36. groups.append(str(k).split(',')[0].split('=')[1])
  37. return groups
  38. def add(self):
  39. ld = {'dn': 'CN=%s,%s' % (self.usrName, base),
  40. "sAMAccountName": self.usrName,
  41. "userPrincipalName": "%s@%s" % (self.usrName, self.domainName),
  42. "objectClass": "user",
  43. "displayName": self.usrName,
  44. "description": self.usrName,
  45. "homeDirectory": r"\\%s\users\%s" % ("dc01", self.usrName),
  46. 'scriptPath': "loginScr.cmd",
  47. }
  48. self.sam.transaction_start()
  49. try:
  50. self.sam.add(ld)
  51. self.sam.setpassword("(samAccountName=%s)" % ldb.binary_encode(self.usrName), self.usrPass, False)
  52. except Exception as exc:
  53. self.sam.transaction_cancel()
  54. print(exc)
  55. return exc
  56. else:
  57. self.sam.transaction_commit()
  58. self.exists = self.chek_if_exists()
  59. return False
  60. def set_passwd(self):
  61. self.sam.transaction_start()
  62. try:
  63. self.sam.setpassword("(&(objectClass=user)(samAccountName=%s))" % ldb.binary_encode(self.usrName),
  64. self.usrPass, False)
  65. except Exception as exc:
  66. self.sam.transaction_cancel()
  67. return exc
  68. else:
  69. self.sam.transaction_commit()
  70. return False
  71. def add_in_group(self, grpName):
  72. self.sam.transaction_start()
  73. try:
  74. self.sam.add_remove_group_members(grpName, [self.usrName], add_members_operation=True)
  75. except Exception as exc:
  76. self.sam.transaction_cancel()
  77. return exc
  78. else:
  79. self.sam.transaction_commit()
  80. self.memberOf = self.check_memberof()
  81. return False
  82. def remove_from_group(self, grpName):
  83. self.sam.transaction_start()
  84. try:
  85. self.sam.add_remove_group_members(grpName, [self.usrName], add_members_operation=True)
  86. except Exception as exc:
  87. self.sam.transaction_cancel()
  88. return exc
  89. else:
  90. self.sam.transaction_commit()
  91. return False
  92. def remove(self):
  93. smb_filter = ("(&(sAMAccountName=%s)(sAMAccountType=805306368))" %
  94. ldb.binary_encode(self.usrName))
  95. try:
  96. res = self.sam.search(base=self.sam.domain_dn(),
  97. scope=ldb.SCOPE_SUBTREE,
  98. expression=smb_filter,
  99. attrs=["dn"])
  100. user_dn = res[0].dn
  101. except IndexError:
  102. return 'Unable to find user "%s"' % self.usrName
  103. try:
  104. self.sam.delete(user_dn)
  105. except Exception as e:
  106. return 'Failed to remove user "%s", %s' % (self.usrName, e)
  107. return False
  108. def disable(self):
  109. self.sam.transaction_start()
  110. try:
  111. self.sam.disable_account("(&(objectClass=user)(samAccountName=%s))" % ldb.binary_encode(self.usrName))
  112. except Exception as exc:
  113. self.sam.transaction_cancel()
  114. return exc
  115. else:
  116. self.sam.transaction_commit()
  117. return False
  118. def enable(self):
  119. self.sam.transaction_start()
  120. try:
  121. self.sam.enable_account("(&(objectClass=user)(samAccountName=%s))" % ldb.binary_encode(self.usrName))
  122. except Exception as exc:
  123. self.sam.transaction_cancel()
  124. return exc
  125. else:
  126. self.sam.transaction_commit()
  127. return False
  128. def main():
  129. lp = samba.param.LoadParm()
  130. lp.load(samba.param.default_path()) # или lp.load("/etc/samba/smb.conf")
  131. sam = SamDB(lp=lp, session_info=system_session())
  132. test_usr = UserAd(sam, base, domainName, 'tst', "secret34daD")
  133. print(test_usr.memberOf)
  134. print("Set disable error: ", test_usr.disable())
  135. time.sleep(1)
  136. print("Set enable error: ", test_usr.enable())
  137. time.sleep(1)
  138. print("Set remove error: ", test_usr.remove())
  139. if __name__ == '__main__':
  140. main()

Powered by TurnKey Linux.