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.

459 lines
16 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. ---
  2. title: OpenLDAP
  3. next: /docs/selfhosting/nextcloud
  4. prev: /docs/interception-vimproved
  5. ---
  6. ### Directory Service
  7. {{< callout type="info" >}}
  8. [Difference between a folder and a directory](https://stackoverflow.com/questions/5078676/what-is-the-difference-between-a-directory-and-a-folder)
  9. Folder is for grouping items.
  10. Directory has index. It is for finding specific item,Directory is a filesystem concept.
  11. In simple terms think ```directory``` like a telephone directory which is in a hierarchial structure.
  12. {{< /callout >}}
  13. The term directory service refers to the collection of software, hardware, and processes that store information about an enterprise, subscribers, or both, and make that information available to users. A directory service consists of at least one instance of Directory Server and at least one directory client program. Client programs can access names, phone numbers, addresses, and other data stored in the directory service.
  14. A directory is similar to database,which is attribute-based data;where data is read more often than write.
  15. Directory Server provides Global Directory Services which means it provides information to wide variety of applications,rather than using databases with different applications,which is very hard to administrate.Directory server is a single solution to manage the same information
  16. {{<callout type="info">}}
  17. For example, an organization has three different applications running like nextcloud,email and matrix server and all the applications are accessed by same credentials,if separate database schema's are used for each application it would be hard to manage,if user requesting a password change in one application maybe not be replicated into another application;this problem is solved single,centralized repository of directory information.
  18. {{</callout>}}
  19. LDAP provides a common language that client applications and servers use to communicate with one another. LDAP is a "lightweight" version of the Directory Access Protocol (DAP)
  20. ### RDBMS vs Directory Service
  21. {{% details title="1. How often does your data change?"%}}
  22. Directory servers are used for ```reads```,if your data changes often and have many write operations directory service is not a ideal choice,RDBMS would be the ideal choice.
  23. {{% /details %}}
  24. {{% details title="2. Type of Data? "%}}
  25. If data is defined in ```Key:Value``` pair or ```Attribute:Value``` pair, Directory service would be the best choice,like user profile.
  26. {{% /details %}}
  27. {{% details title="3. Data in Hierarchial tree like structure" %}}
  28. If data can be modeled into a tree like structure,accessing the parent and child node in the tree,directory service
  29. {{% /details %}}
  30. ### OpenLDAP
  31. LDAP stands for Lightweight Directory Access Protocol, for accessing directory services.OpenLDAP is the implementation of the LDAP protocol,is a communications protocol that defines the methods in which a directory service can be accessed.
  32. The LDAP information model is based on entries, which is a collection of attributes that has a globally unique Distinguished Name```(DN)```
  33. OpenLDAP is the implementation of the LDAP protocol which belong to User Management and Authentication in tech.
  34. The LDAP protocol both authenticates and authorize's users to their resources.The protocol authenticates users with a bind operation that allows users to communicate with LDAP directory
  35. then authorizes the authenticated user to resources they need if they have access that are defined in rules.Once a user is successfully authenticated, they need to be authorized to access the resource(s) requested.
  36. With OpenLDAP, for example, users belong to groups that can be assigned different permissions. If the authenticating user is assigned the correct permissions to access a certain resource, the LDAP protocol will authorize them to it; if not, the protocol will deny access.
  37. #### LDAP Data components
  38. 1. Directory: an LDAP server
  39. 2. DIT: the tree of entries stored within a directory server
  40. 3. Attributes
  41. Data in LDAP system is stored in elements called attributes,like Key Value pair.Data in the attribute must match to the type defined in the attribute's initial declaration.
  42. ```bash
  43. mail: user@example.com
  44. dc:example,dc:com
  45. ```
  46. 4. Entries
  47. Attributes by themselves are not useful, a group or collection of ```attributes``` under a name represents an entry.
  48. ```bash
  49. dn: ou=people,dc=example,dc=com
  50. objectClass: person
  51. sn: Ramesh
  52. cn: Varma
  53. ```
  54. An example entry displayed in LDIF ( LDAP Data Interchange Format).
  55. ```bash
  56. $ cat ldif/user.ldif
  57. dn: uid=vinay.m,ou=People,dc=vinay,dc=im
  58. objectClass: top
  59. objectClass: inetOrgPerson
  60. uid: vinay.m
  61. cn: vinay
  62. sn: m
  63. userPassword: test
  64. ou: People
  65. dn: uid=akshay,ou=People,dc=vinay,dc=im
  66. objectClass: top
  67. objectClass: inetOrgPerson
  68. uid: akshay
  69. cn: akshay
  70. sn: p
  71. userPassword: test
  72. ou: People
  73. ```
  74. 5. ObjectClass
  75. Object class: a collection of required (MUST) and optional (MAY) attributes. Object classes are further subdivided into STRUCTURAL and AUXILIARY classes, and they may inherit from each other.Every entry has a structural Object class which indicates what type of object an entry is and also can have more auxiliary object that have additional characteristics for that entry.
  76. The ObjectClass definitions are stored in the schema files.Object class must have an object identifier (OID) Object classes may also list a set of required attribute types (so that any entry with that object class must also include those attributes) and/or a set of optional attribute types (so that any entry with that object class may optionally include those attributes).OID's are sequence of numbers separated by periods(.), “1.2.840.113556.1.4.473”
  77. 6. Schema
  78. Schema's define the directory, specifying the configuration of the directories including syntax,object classes,attribute types and matching rules.
  79. #### slapd - Standalone LDAP Daemon
  80. ```slapd``` is a LDAP directory server,which stands for Standalone LDAP daemon.Providing simple auth and security layer.
  81. ```bash
  82. $ sudo apt install slapd ldapvi ldap-utils
  83. ```
  84. {{< callout type="warning" >}}
  85. when asked for administration password prompt during installation just press ```Enter```,we reconfigure slapd using dpkg-reconfigure after the installation.
  86. {{< /callout >}}
  87. ```bash
  88. $sudo dpkg-reconfigure slapd
  89. ```
  90. {{< callout type="info" >}}
  91. Reconfiguration:
  92. 1. Omit initial LDAP server config : ```No``` we obviously want to create intial configuration.
  93. 2. DNS Domain Name : domain name to build the base DN of LDAP directory in this case we are choosing ```vinay.im```.
  94. 3. Organization Name: Type down the organization name( here XYZ Pvt Ltd)
  95. 5. Choose an Admin Password of your choice( for tutorial purpose i've choosed test) and choose MDB as backend database
  96. 6. If asked to purge database when slapd is removed we choose ```No```,will be helpful when we want to switch to a different LDAP server.
  97. 7. Choose Yes if you want to backup the current existing database to ```/var/backups```.
  98. {{< /callout >}}
  99. To have a look at the LDAP database , simple execute ```slapcat``` with sudo privileges.
  100. ```bash{filename="$ sudo slapcat"}
  101. $ sudo slapcat
  102. dn: dc=vinay,dc=im
  103. objectClass: top
  104. objectClass: dcObject
  105. objectClass: organization
  106. o: XYZ Pvt Ltd
  107. dc: vinay
  108. structuralObjectClass: organization
  109. entryUUID: 8057316c-ed6e-103d-8b93-b9da23579469
  110. creatorsName: cn=admin,dc=vinay,dc=im
  111. createTimestamp: 20230922083350Z
  112. modifiersName: cn=admin,dc=vinay,dc=im
  113. modifyTimestamp: 20230922083350Z
  114. ```
  115. {{< callout type="info" >}}
  116. Config files are present in ```/etc/ldap``` directory.
  117. Schemas can be added within the ```slap.d``` directory for server customization.
  118. Database is stored in ```/var/lib/ldap``` having two files ```data.mdb``` and ```lock.mdb```.
  119. {{< /callout >}}
  120. ```bash
  121. $ sudo cp /usr/share/doc/slapd/examples/slapd.conf /etc/ldap/
  122. ```
  123. Copy the example config file ```slapd.conf``` to ```/etc/ldap```, and replace DNS domain components ```dc=example``` to ```dc=vinay``` and ```dc=com``` to ```dc=im``` everywhere in the config, also
  124. update ```/etc/default/slapd``` from ```SLAPD_CONF``` to ```SLAPD_CONF=/etc/ldap/slapd.conf``` and update slapd service by ```sudo systemctl restart slapd```
  125. In ```/etc/ldap/slapd.conf``` under ```suffix "dc=vinay,dc=com"``` add the following lines
  126. ```bash
  127. rootdn "cn=admin,dc=vinay,dc=com"
  128. rootpw "test"
  129. ```
  130. Restart the slapd service again.
  131. ```bash
  132. $ sudo systemctl restart slapd
  133. ```
  134. #### ldapsearch
  135. ```bash{filename="ldapsearch anonymous query"}
  136. $ldapsearch -x -b "dc=vinay,dc=im"
  137. # vinay.im
  138. dn: dc=vinay,dc=im
  139. objectClass: top
  140. objectClass: dcObject
  141. objectClass: organization
  142. o: XYZ Pvt Ltd
  143. dc: vinay
  144. # search result
  145. search: 2
  146. result: 0 Success
  147. # numResponses: 2
  148. ```
  149. ```bash{filename="ldapsearch authenticating with admin user"}
  150. $ ldapsearch -D cn=admin,dc=vinay,dc=im -w test -b dc=vinay,dc=im
  151. # extended LDIF
  152. #
  153. # LDAPv3
  154. # base <dc=vinay,dc=im> with scope subtree
  155. # filter: (objectclass=*)
  156. # requesting: ALL
  157. #
  158. # vinay.im
  159. dn: dc=vinay,dc=im
  160. objectClass: top
  161. objectClass: dcObject
  162. objectClass: organization
  163. o: XYZ Pvt Ltd
  164. dc: vinay
  165. # search result
  166. search: 2
  167. result: 0 Success
  168. # numResponses: 2
  169. # numEntries: 1
  170. ```
  171. 1. ```-D``` {dn} / ```--bindDN``` {dn} — The DN to use to bind to the directory server when performing simple authentication,to use the distinguished binddn name to bind the LDAP directory.
  172. 2. ```-w``` - this option is used to provide the password on the command line for auth, ```-W``` option is used to ask for prompt for typing invisible password without actualling having to type the pass on cli.
  173. 3. ```-b``` - search base as the starting point for the search instead of default.
  174. 4. ```-x``` option in ldapsearch is used for simple authentication instead of SASL.
  175. The above command search's through the ldap directory server with ```admin``` distinguished name providing password with the ```-w``` option and setting the searchbase to start from the rootdn.
  176. -- To list all users on ldap
  177. ```bash
  178. $ ldapsearch -D "cn=admin,dc=vinay,dc=com" -W -b "dc=vinay,dc=com"
  179. $ slapcat
  180. ```
  181. lists all users from the base dn
  182. #### Adding OU (Organization Unit)
  183. Organizational units (OUs) are used to organize entries within the directory tree and can be used to delegate administrative responsibilities within your organization. It’s important to keep your directory organized and well-structured from the beginning; otherwise it will quickly become unwieldy and difficult to manage.
  184. Create a directory called ldif(LDAP Interchange Format) in ```/etc/ldap``` and create a file called people.ldif and paste the following contents.
  185. ```bash
  186. $ cat /etc/ldap/ldif/people.ldif
  187. dn: ou=People,dc=vinay,dc=com
  188. ou: People
  189. cn: people
  190. sn: people
  191. objectClass: top
  192. objectClass: inetOrgPerson
  193. $ ldapadd -D cn=admin,dc=vinay,dc=im -w test -f /etc/ldap/ldif/people.ldif
  194. adding new entry "ou=People,dc=vinay,dc=im"
  195. ```
  196. now ```slapcat``` command shows the OU added within the command output.
  197. #### Add new User
  198. Adding new user within the newly created OU(Organizational Unit)
  199. ```bash{filename="/etc/ldap/john.ldif"}
  200. # cat john.ldif
  201. dn: uid=john,ou=People,dc=vinay,dc=com
  202. objectClass: top
  203. objectClass: inetOrgPerson
  204. uid: john
  205. cn: john
  206. ou: People
  207. sn: abraham
  208. mail: john@vinay.com
  209. userPassword: john
  210. ```
  211. Adding the .ldif file using ldapadd command
  212. ```
  213. $ sudo ldapadd -D "cn=admin,dc=vinay,dc=com" -W -f john.ldif
  214. Enter LDAP Password:
  215. adding new entry "uid=john,ou=People,dc=vinay,dc=com"
  216. ```
  217. #### Read entries within OU as admin
  218. Now we have added an ```OU``` and a user ```john``` to ```People``` OU,lets try to ```ldapsearch``` the users within the OU as admin
  219. ```bash{filename="display users of an OU as admin"}
  220. $ ldapsearch -D "cn=admin,dc=vinay,dc=com" -w vinay.com -b "ou=People,dc=vinay,dc=com"
  221. # extended LDIF
  222. #
  223. # LDAPv3
  224. # base <ou=People,dc=vinay,dc=com> with scope subtree
  225. # filter: (objectclass=*)
  226. # requesting: ALL
  227. #
  228. # People, vinay.com
  229. dn: ou=People,dc=vinay,dc=com
  230. ou: People
  231. cn: people
  232. sn: people
  233. objectClass: top
  234. objectClass: inetOrgPerson
  235. # john, People, vinay.com
  236. dn: uid=john,ou=People,dc=vinay,dc=com
  237. objectClass: top
  238. objectClass: inetOrgPerson
  239. uid: john
  240. cn: john
  241. ou: People
  242. ou: Support
  243. sn: abraham
  244. mail: john@vinay.com
  245. userPassword:: am9obg==
  246. ```
  247. #### Read entries within OU as normal user.
  248. ```bash
  249. $ ldapsearch -D "uid=john,ou=People,dc=vinay,dc=com" -w john -b "ou=People,dc=vinay,dc=com"
  250. # extended LDIF
  251. #
  252. # LDAPv3
  253. # base <ou=People,dc=vinay,dc=com> with scope subtree
  254. # filter: (objectclass=*)
  255. # requesting: ALL
  256. #
  257. # People, vinay.com
  258. dn: ou=People,dc=vinay,dc=com
  259. ou: People
  260. cn: people
  261. sn: people
  262. objectClass: top
  263. objectClass: inetOrgPerson
  264. # john, People, vinay.com
  265. dn: uid=john,ou=People,dc=vinay,dc=com
  266. objectClass: top
  267. objectClass: inetOrgPerson
  268. uid: john
  269. cn: john
  270. ou: People
  271. ou: Support
  272. sn: abraham
  273. mail: john@vinay.com
  274. userPassword:: am9obg==
  275. ```
  276. #### Modifying existing entries
  277. 1. Using ```ldapmodify``` to update entries.
  278. Now to modify an already added record we use ldapmodify and the attributes that are to be modified are put into a separate file,here ```john-modify.ldif``` and to demonstrate here an OU ```Support```
  279. is added to the existing entry,along with ```People``` OU.
  280. ```bash{filename="john-modify.ldif"}
  281. $ cat /etc/ldap/ldif/john-modify.ldif
  282. dn: uid=john,ou=People,dc=vinay,dc=com
  283. changetype: modify
  284. add: ou
  285. ou: Support
  286. ```
  287. ```bash{filename="ldapmodify command for john-modify.ldif"}\
  288. $ ldapmodify -D "cn=admin,dc=vinay,dc=com" -W -f john-modify.ldif
  289. Enter LDAP Password:
  290. modifying entry "uid=john,ou=People,dc=vinay,dc=com"
  291. ```
  292. Now running a slapcat command shows the updated OU ```Support```
  293. ```bash{linenos=table}
  294. dn: uid=john,ou=People,dc=vinay,dc=com
  295. objectClass: top
  296. objectClass: inetOrgPerson
  297. uid: john
  298. cn: john
  299. ou: People
  300. ou: Support
  301. sn: abraham
  302. mail: john@vinay.com
  303. userPassword:: am9obg==
  304. structuralObjectClass: inetOrgPerson
  305. entryUUID: 50ea0ea8-f23d-103d-816b-4d9c39504958
  306. creatorsName: cn=admin,dc=vinay,dc=com
  307. createTimestamp: 20230928112421Z
  308. entryCSN: 20230928120656.291224Z#000000#000#000000
  309. modifiersName: cn=admin,dc=vinay,dc=com
  310. modifyTimestamp: 20230928120656Z
  311. ```
  312. 2.Using ```ldapvi``` to update LDAP entries with a text editor.
  313. ```bash{filename="ldapvi example"}
  314. $ ldapvi -d --host vinay.im
  315. ```
  316. ```ldapvi``` is a ldap client using which we can search,modify and delete entries which is easier than ```ldapmodify``` instead of adding the updated records in a separate ```ldif``` file.
  317. ldapvi prompts to open text editor to modify entries,just similar to text editor.
  318. The above command will bind anonmously to hostname, here the hostname is ```vinay.im```.After making necessary changes in the entry save from the text editor.
  319. ```
  320. # ldapvi -d --host nextcloud.vinay.com
  321. 3 entries read
  322. add: 0, rename: 0, modify: 1, delete: 0
  323. Action? [yYqQvVebB*rsf+?] b
  324. --- Login
  325. --- Login
  326. --- Login
  327. Type M-h for help on key bindings.
  328. Filter or DN: cn=admin,dc=vinay,dc=im
  329. Password: *****
  330. Bound as cn=admin,dc=vinay,dc=im.
  331. add: 0, rename: 0, modify: 1, delete: 0
  332. Action? [yYqQvVebB*rsf+?] y
  333. Done.
  334. ```
  335. after saving and exiting from text editor, an interactive bash prompt ``` [yYqQvVebB*rsf+?]```
  336. ```y``` to commit changes.
  337. ```e``` to edit changes.
  338. ```v``` to view changes as LDIF change records.
  339. ```b``` to show login and rebind - we are trying to auth from admin and save the changes to LDAP entries.
  340. ```
  341. [Reference serverfault] https://serverfault.com/questions/290296/ldapadd-ldapmodify-clarifications-needed-about-these-commands
  342. #### Verifying the ```slapd.conf``` Configuration file
  343. ```bash
  344. $sudo slaptest -v -f /etc/ldap/slapd.conf
  345. config file testing succeeded
  346. ```
  347. ```-f``` : Specifying an alternative configuration file.
  348. ```-v``` : enable verbose mode.
  349. #### Conventions in OpenLDAP
  350. dn - Distinguished Name
  351. RDN - Relative Distinguished Name
  352. cn - Common Name
  353. dc - Domain Component
  354. mail - Email Address
  355. ou - Organization Unit
  356. ldif - LDAP Data Interchange Format
  357. ldap - Lightweight Directory Access Protocol
  358. ### References
  359. 1. https://access.redhat.com/documentation/en-us/red_hat_directory_server/11/html/deployment_guide/introduction_to_directory_services
  360. 2. https://www.zytrax.com/books/ldap
  361. 3. https://tylersguides.com/guides/openldap-how-to-add-a-user/
  362. 4. https://www.zytrax.com/books/ldap/