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.

409 lines
15 KiB

1 year ago
  1. ---
  2. title: OpenLDAP
  3. prev: /docs/selfhosting/nextcloud
  4. ---
  5. ### Directory Service
  6. {{< callout type="info" >}}
  7. [Difference between a folder and a directory](https://stackoverflow.com/questions/5078676/what-is-the-difference-between-a-directory-and-a-folder)
  8. Folder is for grouping items.
  9. Directory has index. It is for finding specific item,Directory is a filesystem concept.
  10. In simple terms think ```directory``` like a telephone directory which is in a hierarchial structure.
  11. {{< /callout >}}
  12. 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.
  13. A directory is similar to database,which is attribute-based data;where data is read more often than write.
  14. 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
  15. {{<callout type="info">}}
  16. 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.
  17. {{</callout>}}
  18. 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)
  19. ### RDBMS vs Directory Service
  20. {{% details title="1. How often does your data change?"%}}
  21. 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.
  22. {{% /details %}}
  23. {{% details title="2. Type of Data? "%}}
  24. If data is defined in ```Key:Value``` pair or ```Attribute:Value``` pair, Directory service would be the best choice,like user profile.
  25. {{% /details %}}
  26. {{% details title="3. Data in Hierarchial tree like structure" %}}
  27. If data can be modeled into a tree like structure,accessing the parent and child node in the tree,directory service
  28. {{% /details %}}
  29. ### OpenLDAP
  30. 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.
  31. The LDAP information model is based on entries, which is a collection of attributes that has a globally unique Distinguished Name```(DN)```
  32. OpenLDAP is the implementation of the LDAP protocol which belong to User Management and Authentication in tech.
  33. 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
  34. 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.
  35. 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.
  36. #### LDAP Data components
  37. 1. Directory: an LDAP server
  38. 2. DIT: the tree of entries stored within a directory server
  39. 3. Attributes
  40. 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.
  41. ```bash
  42. mail: user@example.com
  43. dc:example,dc:com
  44. ```
  45. 4. Entries
  46. Attributes by themselves are not useful, a group or collection of ```attributes``` under a name represents an entry.
  47. ```bash
  48. dn: ou=people,dc=example,dc=com
  49. objectClass: person
  50. sn: Ramesh
  51. cn: Varma
  52. ```
  53. An example entry displayed in LDIF ( LDAP Data Interchange Format).
  54. ```bash
  55. $ cat ldif/user.ldif
  56. dn: uid=vinay.m,ou=People,dc=vinay,dc=im
  57. objectClass: top
  58. objectClass: inetOrgPerson
  59. uid: vinay.m
  60. cn: vinay
  61. sn: m
  62. userPassword: test
  63. ou: People
  64. dn: uid=akshay,ou=People,dc=vinay,dc=im
  65. objectClass: top
  66. objectClass: inetOrgPerson
  67. uid: akshay
  68. cn: akshay
  69. sn: p
  70. userPassword: test
  71. ou: People
  72. ```
  73. 5. ObjectClass
  74. 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.
  75. 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”
  76. 6. Schema
  77. Schema's define the directory, specifying the configuration of the directories including syntax,object classes,attribute types and matching rules.
  78. #### slapd - Standalone LDAP Daemon
  79. ```slapd``` is a LDAP directory server,which stands for Standalone LDAP daemon.Providing simple auth and security layer.
  80. ```bash
  81. $ sudo apt install slapd ldapvi ldap-utils
  82. ```
  83. {{< callout type="warning" >}}
  84. when asked for administration password prompt during installation just press ```Enter```,we reconfigure slapd using dpkg-reconfigure after the installation.
  85. {{< /callout >}}
  86. ```bash
  87. $sudo dpkg-reconfigure slapd
  88. ```
  89. {{< callout type="info" >}}
  90. Reconfiguration:
  91. 1. Omit initial LDAP server config : ```No``` we obviously want to create intial configuration.
  92. 2. DNS Domain Name : domain name to build the base DN of LDAP directory in this case we are choosing ```vinay.im```.
  93. 3. Organization Name: Type down the organization name( here XYZ Pvt Ltd)
  94. 5. Choose an Admin Password of your choice( for tutorial purpose i've choosed test) and choose MDB as backend database
  95. 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.
  96. 7. Choose Yes if you want to backup the current existing database to ```/var/backups```.
  97. {{< /callout >}}
  98. To have a look at the LDAP database , simple execute ```slapcat``` with sudo privileges.
  99. ```bash{filename="$ sudo slapcat"}
  100. $ sudo slapcat
  101. dn: dc=vinay,dc=im
  102. objectClass: top
  103. objectClass: dcObject
  104. objectClass: organization
  105. o: XYZ Pvt Ltd
  106. dc: vinay
  107. structuralObjectClass: organization
  108. entryUUID: 8057316c-ed6e-103d-8b93-b9da23579469
  109. creatorsName: cn=admin,dc=vinay,dc=im
  110. createTimestamp: 20230922083350Z
  111. modifiersName: cn=admin,dc=vinay,dc=im
  112. modifyTimestamp: 20230922083350Z
  113. ```
  114. {{< callout type="info" >}}
  115. Config files are present in ```/etc/ldap``` directory.
  116. Schemas can be added within the ```slap.d``` directory for server customization.
  117. Database is stored in ```/var/lib/ldap``` having two files ```data.mdb``` and ```lock.mdb```.
  118. {{< /callout >}}
  119. ```bash
  120. $ sudo cp /usr/share/doc/slapd/examples/slapd.conf /etc/ldap/
  121. ```
  122. 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
  123. update ```/etc/default/slapd``` from ```SLAPD_CONF``` to ```SLAPD_CONF=/etc/ldap/slapd.conf``` and update slapd service by ```sudo systemctl restart slapd```
  124. In ```/etc/ldap/slapd.conf``` under ```suffix "dc=vinay,dc=com"``` add the following lines
  125. ```bash
  126. rootdn "cn=admin,dc=vinay,dc=com"
  127. rootpw "test"
  128. ```
  129. Restart the slapd service again.
  130. ```bash
  131. $ sudo systemctl restart slapd
  132. ```
  133. #### ldapsearch
  134. ```bash{filename="ldapsearch anonymous query"}
  135. $ldapsearch -x -b "dc=vinay,dc=im"
  136. # vinay.im
  137. dn: dc=vinay,dc=im
  138. objectClass: top
  139. objectClass: dcObject
  140. objectClass: organization
  141. o: XYZ Pvt Ltd
  142. dc: vinay
  143. # search result
  144. search: 2
  145. result: 0 Success
  146. # numResponses: 2
  147. ```
  148. ```bash{filename="ldapsearch authenticating with admin user"}
  149. $ ldapsearch -D cn=admin,dc=vinay,dc=im -w test -b dc=vinay,dc=im
  150. # extended LDIF
  151. #
  152. # LDAPv3
  153. # base <dc=vinay,dc=im> with scope subtree
  154. # filter: (objectclass=*)
  155. # requesting: ALL
  156. #
  157. # vinay.im
  158. dn: dc=vinay,dc=im
  159. objectClass: top
  160. objectClass: dcObject
  161. objectClass: organization
  162. o: XYZ Pvt Ltd
  163. dc: vinay
  164. # search result
  165. search: 2
  166. result: 0 Success
  167. # numResponses: 2
  168. # numEntries: 1
  169. ```
  170. 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.
  171. 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.
  172. 3. ```-b``` - search base as the starting point for the search instead of default.
  173. 4. ```-x``` option in ldapsearch is used for simple authentication instead of SASL.
  174. 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.
  175. -- To list all users on ldap
  176. ```bash
  177. $ ldapsearch -D "cn=admin,dc=vinay,dc=com" -W -b "dc=vinay,dc=com"
  178. $ slapcat
  179. ```
  180. lists all users from the base dn
  181. #### Adding OU (Organization Unit)
  182. 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.
  183. Create a directory called ldif(LDAP Interchange Format) in ```/etc/ldap``` and create a file called people.ldif and paste the following contents.
  184. ```bash
  185. $ cat /etc/ldap/ldif/people.ldif
  186. dn: ou=People,dc=vinay,dc=com
  187. ou: People
  188. cn: people
  189. sn: people
  190. objectClass: top
  191. objectClass: inetOrgPerson
  192. $ ldapadd -D cn=admin,dc=vinay,dc=im -w test -f /etc/ldap/ldif/people.ldif
  193. adding new entry "ou=People,dc=vinay,dc=im"
  194. ```
  195. now ```slapcat``` command shows the OU added within the command output.
  196. #### Add new User
  197. Adding new user within the newly created OU(Organizational Unit)
  198. ```bash{filename="/etc/ldap/john.ldif"}
  199. # cat john.ldif
  200. dn: uid=john,ou=People,dc=vinay,dc=com
  201. objectClass: top
  202. objectClass: inetOrgPerson
  203. uid: john
  204. cn: john
  205. ou: People
  206. sn: abraham
  207. mail: john@vinay.com
  208. userPassword: john
  209. ```
  210. Adding the .ldif file using ldapadd command
  211. ```
  212. $ sudo ldapadd -D "cn=admin,dc=vinay,dc=com" -W -f john.ldif
  213. Enter LDAP Password:
  214. adding new entry "uid=john,ou=People,dc=vinay,dc=com"
  215. ```
  216. #### Read entries within OU as admin
  217. Now we have added an ```OU``` and a user ```john``` to ```People``` OU,lets try to ```ldapsearch``` the users within the OU as admin
  218. ```bash{filename="display users of an OU as admin"}
  219. $ ldapsearch -D "cn=admin,dc=vinay,dc=com" -w vinay.com -b "ou=People,dc=vinay,dc=com"
  220. # extended LDIF
  221. #
  222. # LDAPv3
  223. # base <ou=People,dc=vinay,dc=com> with scope subtree
  224. # filter: (objectclass=*)
  225. # requesting: ALL
  226. #
  227. # People, vinay.com
  228. dn: ou=People,dc=vinay,dc=com
  229. ou: People
  230. cn: people
  231. sn: people
  232. objectClass: top
  233. objectClass: inetOrgPerson
  234. # john, People, vinay.com
  235. dn: uid=john,ou=People,dc=vinay,dc=com
  236. objectClass: top
  237. objectClass: inetOrgPerson
  238. uid: john
  239. cn: john
  240. ou: People
  241. ou: Support
  242. sn: abraham
  243. mail: john@vinay.com
  244. userPassword:: am9obg==
  245. ```
  246. #### Read entries within OU as normal user.
  247. ```bash
  248. $ ldapsearch -D "uid=john,ou=People,dc=vinay,dc=com" -w john -b "ou=People,dc=vinay,dc=com"
  249. # extended LDIF
  250. #
  251. # LDAPv3
  252. # base <ou=People,dc=vinay,dc=com> with scope subtree
  253. # filter: (objectclass=*)
  254. # requesting: ALL
  255. #
  256. # People, vinay.com
  257. dn: ou=People,dc=vinay,dc=com
  258. ou: People
  259. cn: people
  260. sn: people
  261. objectClass: top
  262. objectClass: inetOrgPerson
  263. # john, People, vinay.com
  264. dn: uid=john,ou=People,dc=vinay,dc=com
  265. objectClass: top
  266. objectClass: inetOrgPerson
  267. uid: john
  268. cn: john
  269. ou: People
  270. ou: Support
  271. sn: abraham
  272. mail: john@vinay.com
  273. userPassword:: am9obg==
  274. ```
  275. #### Modifying existing entries
  276. 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```
  277. is added to the existing entry,along with ```People``` OU.
  278. ```bash{filename="john-modify.ldif"}
  279. $ cat /etc/ldap/ldif/john-modify.ldif
  280. dn: uid=john,ou=People,dc=vinay,dc=com
  281. changetype: modify
  282. add: ou
  283. ou: Support
  284. ```
  285. ```bash{filename="ldapmodify command for john-modify.ldif"}\
  286. $ ldapmodify -D "cn=admin,dc=vinay,dc=com" -W -f john-modify.ldif
  287. Enter LDAP Password:
  288. modifying entry "uid=john,ou=People,dc=vinay,dc=com"
  289. ```
  290. Now running a slapcat command shows the updated OU ```Support```
  291. ```bash{linenos=table}
  292. dn: uid=john,ou=People,dc=vinay,dc=com
  293. objectClass: top
  294. objectClass: inetOrgPerson
  295. uid: john
  296. cn: john
  297. ou: People
  298. ou: Support
  299. sn: abraham
  300. mail: john@vinay.com
  301. userPassword:: am9obg==
  302. structuralObjectClass: inetOrgPerson
  303. entryUUID: 50ea0ea8-f23d-103d-816b-4d9c39504958
  304. creatorsName: cn=admin,dc=vinay,dc=com
  305. createTimestamp: 20230928112421Z
  306. entryCSN: 20230928120656.291224Z#000000#000#000000
  307. modifiersName: cn=admin,dc=vinay,dc=com
  308. modifyTimestamp: 20230928120656Z
  309. ```
  310. [Reference serverfault] https://serverfault.com/questions/290296/ldapadd-ldapmodify-clarifications-needed-about-these-commands
  311. #### Verifying the ```slapd.conf``` Configuration file
  312. ```bash
  313. $sudo slaptest -v -f /etc/ldap/slapd.conf
  314. config file testing succeeded
  315. ```
  316. ```-f``` : Specifying an alternative configuration file.
  317. ```-v``` : enable verbose mode.
  318. #### Conventions in OpenLDAP
  319. dn - Distinguished Name
  320. RDN - Relative Distinguished Name
  321. cn - Common Name
  322. dc - Domain Component
  323. mail - Email Address
  324. ou - Organization Unit
  325. ldif - LDAP Data Interchange Format
  326. ldap - Lightweight Directory Access Protocol
  327. ### References
  328. 1. https://access.redhat.com/documentation/en-us/red_hat_directory_server/11/html/deployment_guide/introduction_to_directory_services
  329. 2. https://www.zytrax.com/books/ldap
  330. 3. https://tylersguides.com/guides/openldap-how-to-add-a-user/
  331. 4. https://www.zytrax.com/books/ldap/