Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.03 KB | None | 0 0
  1. from ldap3 import Server, Connection, SUBTREE, ServerPool, FIRST
  2.  
  3. @api.route('/ldap/groupmembers_recursive')
  4. class groupmembers_recursive(Resource):
  5.     def post(self):
  6.         response = {}
  7.         """
  8.        if not request.headers.get('X-Forwarded-For'):
  9.            abort(400, {"error": "invalid request _"})
  10.        else:
  11.            if not ipaddress.ip_address(request.headers.get('X-Forwarded-For')).is_private:
  12.                abort(401, {"error": "not authorized"})
  13.        """
  14.  
  15.         if not request.headers.get('X-Secret'):
  16.             abort(400, {"error": "invalid request"})
  17.         else:
  18.             if request.headers.get('X-Secret') != api_key:
  19.                 abort(401, {"error": "not authorized"})
  20.  
  21.         if not request.get_json():
  22.             abort(400,{"error": "invalid json"})
  23.         if not 'search_base' in request.get_json():
  24.             abort(400,{"error": "search base not provided"})
  25.         if not 'attributes' in request.get_json():
  26.             abort(400,{"error": "ldap attributes not provided"})
  27.         manifest = request.get_json()
  28.  
  29.         user_objects = []
  30.         try:
  31.             if manifest["protocol"] == 'ldap' or manifest["protocol"] == 'LDAP':
  32.                 use_ssl = False
  33.             else:
  34.                 use_ssl = True
  35.  
  36.             ldap_server_list = []
  37.             ldap_server_list.append(
  38.                 Server(manifest["server_a_address"], port=manifest["server_a_port"], use_ssl=use_ssl,
  39.                        connect_timeout=5))
  40.             ldap_server_list.append(
  41.                 Server(manifest["server_b_address"], port=manifest["server_b_port"], use_ssl=use_ssl,
  42.                        connect_timeout=5))
  43.  
  44.             server_pool = ServerPool(ldap_server_list, FIRST, active=3, exhaust=True)
  45.  
  46.             ldap_connection = Connection(server_pool, user=manifest["username"], password=manifest["password"],
  47.                                          version=manifest["ldap_version"], auto_range=True, receive_timeout=20)
  48.  
  49.             if not ldap_connection.bind():
  50.                 abort(400,{"error": "LDAP bind error " + str(ldap_connection.result)})
  51.                 #print('error in bind', ldap_connection.result)  # return non 200 response w/result
  52.  
  53.             group_dn_list = ldap_connection.extend.standard.paged_search(search_base=manifest["groups_search_base"],
  54.                                                                          search_filter='(&(objectCategory=group)(cn=' +
  55.                                                                                        manifest["group_name"] + '))',
  56.                                                                          search_scope=SUBTREE,
  57.                                                                          attributes=manifest["attributes"],
  58.                                                                          paged_size=100000,
  59.                                                                          generator=False)
  60.             for group_entry in group_dn_list:
  61.                 if 'dn' in group_entry.keys():
  62.                     #print()
  63.                     #print('dn: ' + group_entry['dn'])
  64.                     #ldap_connection.bind()
  65.                     user_list = ldap_connection.extend.standard.paged_search(search_base=manifest["search_base"],
  66.                                                                              search_filter='(&(objectCategory=Person)(sAMAccountName=*)(memberOf:1.2.840.113556.1.4.1941:=' +
  67.                                                                                            group_entry['dn'] + '))',
  68.                                                                              search_scope=SUBTREE,
  69.                                                                              attributes=manifest["attributes"],
  70.                                                                              paged_size=100000,
  71.                                                                              generator=False)
  72.                     for user_entry in user_list:
  73.                         if 'dn' in user_entry.keys():
  74.                             #print('attributes: ' + str(user_entry['attributes']))
  75.                             user_object = {}
  76.                             for attribute in user_entry['attributes']:
  77.                                 if user_entry['attributes'][attribute] != []:
  78.                                     user_object[attribute] = user_entry['attributes'][attribute]
  79.                                 else:
  80.                                     user_object[attribute] = None
  81.                             #print('user objects: ' + str(user_object))
  82.                             user_objects.append(user_object)
  83.              
  84.             return (user_objects)
  85.  
  86.             ldap_connection.unbind()
  87.  
  88.         except:
  89.             abort(400, {"error": {"exception: ": str(error_handling())}})
  90.  
  91.  
  92. @api.route('/ldap/groupdn')
  93. class groupdn(Resource):
  94.     def post(self):
  95.         response = {}
  96.         """
  97.        if not request.headers.get('X-Forwarded-For'):
  98.            abort(400, {"error": "invalid request _"})
  99.        else:
  100.            if not ipaddress.ip_address(request.headers.get('X-Forwarded-For')).is_private:
  101.                abort(401, {"error": "not authorized"})
  102.        """
  103.  
  104.         if not request.headers.get('X-Secret'):
  105.             abort(400, {"error": "invalid request"})
  106.         else:
  107.             if request.headers.get('X-Secret') != api_key:
  108.                 abort(401, {"error": "not authorized"})
  109.  
  110.         if not request.get_json():
  111.             abort(400, {"error": "invalid json"})
  112.         if not 'search_base' in request.get_json():
  113.             abort(400, {"error": "search base not provided"})
  114.         if not 'attributes' in request.get_json():
  115.             abort(400, {"error": "ldap attributes not provided"})
  116.         manifest = request.get_json()
  117.  
  118.         user_objects = []
  119.         try:
  120.             if manifest["protocol"] == 'ldap' or manifest["protocol"] == 'LDAP':
  121.                 use_ssl = False
  122.             else:
  123.                 use_ssl = True
  124.  
  125.             ldap_server_list = []
  126.             ldap_server_list.append(
  127.                 Server(manifest["server_a_address"], port=manifest["server_a_port"], use_ssl=use_ssl,
  128.                        connect_timeout=5))
  129.             ldap_server_list.append(
  130.                 Server(manifest["server_b_address"], port=manifest["server_b_port"], use_ssl=use_ssl,
  131.                        connect_timeout=5))
  132.  
  133.             server_pool = ServerPool(ldap_server_list, FIRST, active=3, exhaust=True)
  134.  
  135.             ldap_connection = Connection(server_pool, user=manifest["username"], password=manifest["password"],
  136.                                          version=manifest["ldap_version"], auto_range=True, receive_timeout=20)
  137.  
  138.             if not ldap_connection.bind():
  139.                 abort(400, {"error": "LDAP bind error " + str(ldap_connection.result)})
  140.                 # print('error in bind', ldap_connection.result)  # return non 200 response w/result
  141.  
  142.             group_dn_list = ldap_connection.extend.standard.paged_search(search_base=manifest["groups_search_base"],
  143.                                                                          search_filter='(&(objectCategory=group)(cn=' +
  144.                                                                                        manifest["group_name"] + '))',
  145.                                                                          search_scope=SUBTREE,
  146.                                                                          attributes=manifest["attributes"],
  147.                                                                          paged_size=100000,
  148.                                                                          generator=False)
  149.  
  150.             for group_entry in group_dn_list:
  151.                 if 'dn' in group_entry.keys():
  152.                     return group_entry['dn']
  153.  
  154.             ldap_connection.unbind()
  155.             abort(400, {"error": "group dn not found"})
  156.  
  157.         except:
  158.             abort(400, {"error": {"exception: ": str(error_handling())}})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement