daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest May 31st, 2017 560 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/env python3
  2.  
  3. import os
  4. import yaml
  5. import munch
  6. import ldap3
  7. import logging
  8. import passlib.hash
  9. import passlib.utils
  10. import envelopes
  11. import datetime
  12.  
  13.  
  14. LDAP_USER = 'cn=automaticum,ou=services,dc=maglab,dc=space'
  15. LDAP_PASS = ''
  16.  
  17. MEMBERS_PATH = '/home/jali/jali/members'
  18.  
  19. USER_BASE_DN = 'ou=users,dc=maglab,dc=space'
  20. GROUP_BASE_DN = 'ou=groups,dc=maglab,dc=space'
  21.  
  22.  
  23. USER_DN = 'cn={},' + USER_BASE_DN
  24. GROUP_DN = 'cn=members,' + GROUP_BASE_DN
  25.  
  26. MAIL_HOST = 'mx2.open-mail.net'
  27. MAIL_PORT = 587
  28. MAIL_USER = 'vorstand@maglab.space'
  29. MAIL_PASS = ''
  30. MAIL_FROM = 'Vorstand <vorstand@maglab.space>'
  31. MAIL_COPY = 'Vorstand <vorstand@maglab.space>'
  32. MAIL_SUBJ = 'Dein mag.lab Account wurde angelegt'
  33. MAIL_TEXT = u'''\
  34. Hallo {nick},
  35.  
  36. dein mag.lab Account wurde angelegt. Du kannst den Account benutzen um dich an
  37. den Diensten des Hackerpsace anzumelden. Weitere Informationen über den
  38. Benutzer-Account kannst du im Wiki[1] einsehen.
  39.  
  40. Benutzer: {nick}
  41. Password: {password}
  42.  
  43. Das Passwort wurde generiert und muss vor der ersten Verwendung geändert
  44. werden. Hierfür verwende bitte folgenden Link:
  45.  
  46.    https://ldap.maglab.space/
  47.  
  48. Viel Spass und Willkommen im Verein.
  49.  
  50. [1] https://wiki.maglab.space/wiki/Account
  51.  
  52. '''
  53.  
  54.  
  55.  
  56. def loadMembers():
  57.     for path in os.listdir(MEMBERS_PATH):
  58.         with open(os.path.join(MEMBERS_PATH, path), 'r') as f:
  59.             member = munch.munchify(yaml.load(f))
  60.             member.exists = False
  61.             member.member = False
  62.  
  63.             yield member
  64.  
  65.  
  66. def sendMail(member):
  67.     envelope = envelopes.Envelope(
  68.             to_addr = (member.email, member.nick),
  69.             from_addr = MAIL_FROM,
  70.             bcc_addr = MAIL_COPY,
  71.             subject = MAIL_SUBJ,
  72.             text_body = MAIL_TEXT.format(**member.toDict()),
  73.     )
  74.     envelope.headers['Date'] = str(datetime.datetime.now())
  75.     envelope.send(host=MAIL_HOST, port=MAIL_PORT, login=MAIL_USER, password=MAIL_PASS, tls=True)
  76.  
  77.  
  78. if __name__ == '__main__':
  79.     logging.basicConfig(level=logging.DEBUG)
  80.  
  81.     members = {member.nick: member for member in loadMembers()}
  82.  
  83.     with ldap3.Connection(ldap3.Server("ldap", 636, use_ssl=True),
  84.                           user=LDAP_USER,
  85.                           password=LDAP_PASS,
  86.                           auto_bind=True,
  87.                           raise_exceptions=True,
  88.                           return_empty_attributes=True) as conn:
  89.  
  90.         conn.search(USER_BASE_DN,
  91.                     '(objectClass=inetOrgPerson)',
  92.                     attributes=['cn', 'memberOf'])
  93.         for entry in conn.entries:
  94.             if entry.cn.value not in members:
  95.                 logging.debug('Found non-member entry: %s - ignoring', entry.cn)
  96.                 continue
  97.            
  98.             members[entry.cn.value].exists = True
  99.  
  100.             try:
  101.                 members[entry.cn.value].member = GROUP_DN in entry.memberOf
  102.             except AttributeError:
  103.                 pass
  104.  
  105.  
  106.         for member in members.values():
  107.             logging.debug('Processing member: %s', member.nick)
  108.  
  109.             dn = USER_DN.format(member.nick)
  110.  
  111.             if member.states[-1].member:
  112.                 if not member.exists:
  113.                     logging.debug('Member is active but does not exists - creating entry')
  114.  
  115.                     password = passlib.utils.generate_password(size=24)
  116.                     password_hash = passlib.hash.ldap_salted_sha1.hash(password)
  117.  
  118.                     sn, gn = member.name.rsplit(' ', 1)
  119.  
  120.                     conn.add(dn, 'inetOrgPerson', {
  121.                         'sn': sn,
  122.                         'givenName': gn,
  123.                         'mail': member.email or '',
  124.                         'userPassword': password_hash,
  125.                         'pwdReset': 'TRUE',
  126.                     })
  127.                    
  128.                     member.password = password
  129.  
  130.                     sendMail(member)
  131.  
  132.                 if not member.member:
  133.                     logging.debug('Member is active but not part of group - adding')
  134.  
  135.                     conn.modify(GROUP_DN, {
  136.                         'uniqueMember': [
  137.                             (ldap3.MODIFY_ADD, [dn])
  138.                         ]
  139.                     })
  140.  
  141.             else:
  142.                 if member.member:
  143.                     logging.debug('Member is no longer active but still part of group - removing')
  144.                    
  145.                     conn.modify(GROUP_DN, {
  146.                         'uniqueMember': [
  147.                             (ldap3.MODIFY_DELETE, [dn])
  148.                         ]
  149.                     })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top