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.

102 lines
3.6 KiB

6 years ago
6 years ago
4 years ago
4 years ago
6 years ago
4 years ago
4 years ago
6 years ago
6 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
6 years ago
  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. from UserAd_class import UserAd
  12. import zayavka_reader as z_reader
  13. base = "CN=Users,DC=techgrow,DC=local" # можно не заморачиваться с ОУ и тогда base = "CN=Users,DC=myDom,DC=lan"
  14. domainName = 'techgrow.local'
  15. dbconn = {'host': '10.3.11.177',
  16. 'user': 'readonlyuser',
  17. 'pass': 'Readonly@2006',
  18. 'base': 'techgrow'}
  19. sql = 'select * from LDAP'
  20. sql_update_changes = "update LDAP set Changes = 'N' where ID = '%s'"
  21. sql_delete = "delete from LDAP where Delete = 'Y' and ID = '%s'"
  22. ad_groups = ['vpn-users', 'gogs']
  23. ad_groups_users = ['vpn-users', 'gogs']
  24. ad_groups_bitrix = ['vpn-web-vpn', 'gogs']
  25. ad_groups_1c = ['vpn-1c-vpn', 'Domain Users']
  26. ad_group_xserv = 'ics'
  27. ad_group_rdp = 'Пользователи удаленного рабочего стола'
  28. def users_make_ad(sam, base):
  29. expression = "(&(objectCategory=person)(objectClass=user))"
  30. users = {}
  31. res = sam.search(base=base, expression=expression, attrs=['*', 'userAccountControl'])
  32. for i in res:
  33. enabled = 0
  34. if int(str(i['userAccountControl'])) & 2 == 0:
  35. enabled = 1
  36. memberOf = []
  37. for k in i['memberOf']:
  38. memberOf.append(str(k).split(',')[0].split('=')[1])
  39. users[str(i['samAccountName'])] = {'samAccountName': str(i['samAccountName']),
  40. 'memberOf': memberOf,
  41. 'userAccountControl': int(str(i['userAccountControl'])),
  42. 'enabled': enabled}
  43. return users
  44. def main():
  45. lp = samba.param.LoadParm()
  46. lp.load(samba.param.default_path()) # или lp.load("/etc/samba/smb.conf")
  47. sam = SamDB(lp=lp, session_info=system_session())
  48. expression = "(&(objectCategory=person)(objectClass=user))"
  49. res = sam.search(base=base, expression=expression, attrs=['*', 'userAccountControl'])
  50. for i in res:
  51. print(str(i['samAccountName']))
  52. res, err = z_reader.mysql_reader(dbconn, sql)
  53. if err:
  54. print(err)
  55. exit(0)
  56. z_users = z_reader.users_make_zayavka(res)
  57. i = 0
  58. for z_user in z_users:
  59. print(z_user, z_users[z_user])
  60. ad_user = UserAd(sam, base, domainName, z_users[z_user]['samAccountName'], z_users[z_user]['usrPass'])
  61. if z_users[z_user]['toRemove']: # удаляем, если такой есть
  62. print("removing user")
  63. if ad_user.exists:
  64. ad_user.remove()
  65. dat, err = z_reader.mysql_reader(dbconn, sql_delete % z_users[z_user]['id'])
  66. if err:
  67. print(err)
  68. continue
  69. if not ad_user.exists:
  70. print("make new user")
  71. ad_res = ad_user.add()
  72. if ad_res:
  73. print(ad_res)
  74. if z_users[z_user]['pwChange']: # обновляем пароль
  75. ad_res = ad_user.set_passwd()
  76. if ad_res:
  77. print(ad_res)
  78. dat, err = z_reader.mysql_reader(dbconn, sql_update_changes % z_users[z_user]['id'])
  79. if err:
  80. print(err)
  81. if z_users[z_user]['enabled']: # инаблим или дисаблим
  82. ad_user.enable()
  83. else:
  84. ad_user.disable()
  85. for group in z_users[z_user]['memberOf']:
  86. ad_user.add_in_group(group)
  87. if __name__ == '__main__':
  88. main()

Powered by TurnKey Linux.