“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
In the work, ldap server docking, may involve synchronization of organizational structure, authentication, authentication post-processing, etc. Authentication is not what I want to write recently, continue to look at the idea of organizational structure
For example, to synchronize the organization structure of an AD domain or an enterprise organization structure corresponding to other LDAP products, I simply create some oU and CN as follows
View the current LDAP organization structure
We use the command to view the organizational structure data on the LDAP server. We only need to view the DN and do not need other attributes
# ldapsearch -x -LLL -b dc=xiaomotong,dc=com objectClass=* d:
Copy the code
We can see that the DATA returned by the LDAP server is a list. Each element in the list is an entry, a record, and a unique identifier
So the question is, how do I turn this DN list into a tree?
We can draw a picture according to the above print results for analysis
Let’s draw a tree
Dc =xiaomotong,dc=com, and ou = cn from right to left
The graph for the first five pieces of data looks like this:
It looks like it’s working. Keep drawing to see how it looks:
- The red box is OU
- The green box is DC
- The blue box is CN
Yeah, it’s a tree like this, it’s drawn, but how do we do that in code?
coded
Encoding implementation of lDIF format data into a tree, and json output, check the effect
implementation
1. Iterate through each entry
2. When processing each entry, obtain the corresponding RDN (corresponding key and value) from right to left, and create a node in the multi-fork tree for each RDN
3. Store nodes corresponding to the base N and node addresses corresponding to each OU in a map (key is string, value is node address) for subsequent traversal of other entries. You can directly obtain the node address based on the OU name
4. For users under a node, directly connect to the node
Draw a picture to illustrate what a map can do:
For example, if we need to draw an organizational structure, let’s draw the root node first
Now we iterate through the corresponding entry, for example:
- cn=B,dc=A
- ou=C,dc=A
- ou=D,dc=A
We need to draw B, C and D, and we create the corresponding nodes respectively, but how do we attach them to A? At this time, do we need the address of A? This entry can obtain the name of A, which is also the key of the map. In this way, we can obtain the address of A
Continue parsing entry
- ou=E,ou=C,dc=A
- cn=F,ou=E,ou=C,dc=A
The principle of same
Through the diagram above, it is possible to convert THE LDIF format data of LDAP into the tree we want. The idea is to have the code quickly, and the specific code can be seen in the next article
If there is any deviation, please feel free to comment
Welcome to like, follow and favorites
Friends, your support and encouragement, I insist on sharing, improve the quality of the power
All right, that’s it for this time
Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.
I am Nezha, welcome to like, see you next time ~