{"id":1691,"date":"2022-02-22T14:05:32","date_gmt":"2022-02-22T22:05:32","guid":{"rendered":"https:\/\/surfrock66.com\/?p=1691"},"modified":"2024-07-16T10:18:31","modified_gmt":"2024-07-16T17:18:31","slug":"my-experience-guide-setting-up-openldap-for-pc-webapp-authentication-on-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/surfrock66.com\/?p=1691","title":{"rendered":"My experience\/guide setting up OpenLDAP for PC\/webapp authentication on Ubuntu 20.04."},"content":{"rendered":"<p>This was originally posted <a href=\"https:\/\/www.reddit.com\/r\/selfhosted\/comments\/nnam1x\/my_experienceguide_setting_up_openldap_for\/\">Here<\/a> but I'm cross-posting it to my site for preservation and self-reliance.<\/p>\n<p>I've been experimenting with user and account management solutions for my home lab, and I was looking for something that could manage posix users and web users.  I wanted something with group management and OU hierarchies that sort of follow what you would use in Active Directory.  I found a lot of issues with some of the more well recommended solutions out there:<\/p>\n<p><!--more--><\/p>\n<ul>\n<li>Samba: It's basically an AD Domain Controller which was more than I needed; also while being feature-familiar with AD is nice, I'm not looking to implement AD, I'm looking to really stick closer to just LDAP.<\/li>\n<li>GLAuth: Interesting project, but is somewhat limited in the attributes it can handle, how group management works, how the OU structure is reflected in group membership, and usability.  GLauth-ui is an interesting component for usability in that it allows for user password resets and such, however it's not mature enough yet for what I need.<\/li>\n<li>FreeIPA: It isn't an option on Ubuntu Servers anymore which is my target platform; since I believe 18.04 there are dependency resolution issues and the issue trackers indicate there is no path to resolution.<\/li>\n<li>\u00c6-DIR: This seemed interesting, but at the time I looked into it there was a deal breaker which I can no longer remember; I think it didn't do both posix auth and web-app integration well enough, but it did one or the other well?  I'm not sure, it's been a while.<\/li>\n<\/ul>\n<p>Part of all this was wanting to learn more about how LDAP worked in general, so I had a driver in the back of my mind to stick to pure openLDAP.  While that seemed very intimidating at first, it's become the solution at the top of my list.  That being said, I don't think it's great out-of-the-box for the average homelab setup.  The default objectClasses inherit from top and have attributes that overlap in undesireable ways, so a user created in one of these classes for posix authentication may not work well for, say, a webapp whose privileges are assigned by OU or group.  To this end, I was driven to create a custom schema with objectClasses that are more reflective of what I believe someone would want in a homelab.  There's a lot of counter-intuitive stuff here, which I want to document for anyone else going down this path.  This link has been a tremendous resource: <a href=\"https:\/\/www.openldap.org\/doc\/admin22\/schema.html\">https:\/\/www.openldap.org\/doc\/admin22\/schema.html<\/a><\/p>\n<p>Custom LDAP schemas should be globally unique, and that is handled through an OID.  To avoid OID conflict, you should go here and request your own OID: <a href=\"https:\/\/pen.iana.org\/pen\/PenApplication.page\">https:\/\/pen.iana.org\/pen\/PenApplication.page<\/a> The turnaround is like a week.  With this, I could create objectClasses like &quot;surfrock66User&quot; just for me, or if someone wanted to be more enterprising and create a worldwide-useful objectClass like &quot;homeLabUser&quot; they could get an OID to hold.  For my purposes, since I'm experimenting with changing schema, it makes sense to have my own id space to play in.  There is also room for using 1.1 based OID's as kind of unreserved space, kind of like 192.168.1.* for IPs.  When you get your final OID, it'll be of the format &quot;1.3.6.1.4.1._____.&quot; and you append your structure after that.<\/p>\n<p>I need 2 custom objectClasses (Users and Groups), but will be experimenting with a third as well later (Devices); I separate the third as I don't really have a use case, so I don't feel confident in an attribute list that would represent a custom class yet.  One thing that is an issue, I expect group membership to be reflected both in the group and the user; that's not part of OpenLDAP to start, you need to install the memberOf overlay.  Here's a good guide: <a href=\"https:\/\/kifarunix.com\/how-to-create-openldap-member-groups\/\">https:\/\/kifarunix.com\/how-to-create-openldap-member-groups\/<\/a><\/p>\n<p>Below are the attributes in the default relevant objectClasses:<\/p>\n<pre><code>objectclass ( 2.5.6.6 NAME 'person'\n    DESC 'RFC2256: a person'\n    SUP top STRUCTURAL\n    MUST ( sn $ cn )\n    MAY ( userPassword $ telephoneNumber $ seeAlso $ description ) )\nobjectclass ( 2.5.6.7 NAME 'organizationalPerson'\n    DESC 'RFC2256: an organizational person'\n    SUP person STRUCTURAL\n    MAY ( title $ x121Address $ registeredAddress $ destinationIndicator $\n        preferredDeliveryMethod $ telexNumber $ teletexTerminalIdentifier $\n        telephoneNumber $ internationaliSDNNumber $ \n        facsimileTelephoneNumber $ street $ postOfficeBox $ postalCode $\n        postalAddress $ physicalDeliveryOfficeName $ ou $ st $ l ) )\nobjectclass ( 2.5.6.10 NAME 'residentialPerson'\n    DESC 'RFC2256: an residential person'\n    SUP person STRUCTURAL\n    MUST l\n    MAY ( businessCategory $ x121Address $ registeredAddress $\n        destinationIndicator $ preferredDeliveryMethod $ telexNumber $\n        teletexTerminalIdentifier $ telephoneNumber $ internationaliSDNNumber $\n        facsimileTelephoneNumber $ preferredDeliveryMethod $ street $\n        postOfficeBox $ postalCode $ postalAddress $\n        physicalDeliveryOfficeName $ st $ l ) )\nobjectclass ( 1.3.6.1.1.1.2.0 NAME 'posixAccount'\n    DESC 'Abstraction of an account with POSIX attributes'\n    SUP top AUXILIARY\n    MUST ( cn $ uid $ uidNumber $ gidNumber $ homeDirectory )\n    MAY ( userPassword $ loginShell $ gecos $ description ) )\nobjectclass ( 1.3.6.1.1.1.2.1 NAME 'shadowAccount'\n    DESC 'Additional attributes for shadow passwords'\n    SUP top AUXILIARY\n    MUST uid\n    MAY ( userPassword $ shadowLastChange $ shadowMin $\n        shadowMax $ shadowWarning $ shadowInactive $\n        shadowExpire $ shadowFlag $ description ) )\nobjectclass     ( 2.16.840.1.113730.3.2.2\n    NAME 'inetOrgPerson'\n    DESC 'RFC2798: Internet Organizational Person'\n    SUP organizationalPerson\n    STRUCTURAL\n        MAY (\n        audio $ businessCategory $ carLicense $ departmentNumber $\n        displayName $ employeeNumber $ employeeType $ givenName $\n        homePhone $ homePostalAddress $ initials $ jpegPhoto $\n        labeledURI $ mail $ manager $ mobile $ o $ pager $\n        photo $ roomNumber $ secretary $ uid $ userCertificate $\n        x500uniqueIdentifier $ preferredLanguage $\n        userSMIMECertificate $ userPKCS12 ) )\n\nobjectclass ( 2.5.6.9 NAME 'groupOfNames'\n    DESC 'RFC2256: a group of names (DNs)'\n    SUP top STRUCTURAL\n    MUST ( member $ cn )\n    MAY ( businessCategory $ seeAlso $ owner $ ou $ o $ description ) )\nobjectclass ( 1.3.6.1.1.1.2.2 NAME 'posixGroup'\n    DESC 'Abstraction of a group of accounts'\n    SUP top STRUCTURAL\n    MUST ( cn $ gidNumber )\n    MAY ( userPassword $ memberUid $ description ) )<\/code><\/pre>\n<p>Additionally, I wanted the sshPublicKey option from an optional schema <a href=\"http:\/\/pig.made-it.com\/ldap-openssh.html\">http:\/\/pig.made-it.com\/ldap-openssh.html<\/a><\/p>\n<p>Out of this, I am proposing the following new schema for my lab...it has a lot of the other attributes in as MAY even though I have no plan to populate them, that being said if a day comes where I use something requiring an LDAP attribute I didn't build in I could see it not working:<\/p>\n<pre><code>objectclass ( 1.1.2.2.1 NAME 'domainAccount'\n    DESC 'A user\/account\/person in the organization'\n    SUP top STRUCTURAL\n    MUST ( cn $ name $ sn $ uid )\n    MAY ( \n        audio $ businessCategory $ carLicense $ departmentNumber $ \n        description $ destinationIndicator $ displayName $ \n        employeeNumber $ employeeType $ facsimileTelephoneNumber $ \n        gecos $ gidNumber $ givenName $ homeDirectory $ homePhone $ \n        homePostalAddress $ initials $ internationaliSDNNumber $ \n        jpegPhoto $ l $ labeledURI $ ldapPublicKey $ loginShell $ \n        mail $ manager $ mobile $ o $ ou $ pager $ photo $ \n        physicalDeliveryOfficeName $ postalAddress $ postalCode $ \n        postOfficeBox $ preferredDeliveryMethod $ \n        preferredLanguage $ registeredAddress $ roomNumber $ \n        secretary $ seeAlso $ shadowExpire $ shadowInactive $ \n        shadowLastChange $ shadowMax $ shadowMin $ shadowWarning $ \n        sshPublicKey $ st $ street $ telephoneNumber $ \n        teletexTerminalIdentifier $ telexNumber $ title $ \n        uidNumber $ userCertificate $ userPassword $ userPKCS12 $ \n        userSMIMECertificate $ x121Address $ x500uniqueIdentifier ) )\n\nobjectclass ( 1.1.2.2.2 NAME 'domainGroup'\n    DESC 'A group of names in the organization'\n    SUP top STRUCTURAL\n    MUST ( cn $ member )\n    MAY ( \n        businessCategory $ description $ gidNumber $ o $ ou $ \n        owner $ seeAlso ) )<\/code><\/pre>\n<p>Here's a more commented breakdown of the classes:<\/p>\n<ul>\n<li><a href=\"https:\/\/surfrock66.com\/ldap\/domainAccount.description.txt\">https:\/\/surfrock66.com\/ldap\/domainAccount.description.txt<\/a><\/li>\n<li><a href=\"https:\/\/surfrock66.com\/ldap\/domainGroup.description.txt\">https:\/\/surfrock66.com\/ldap\/domainGroup.description.txt<\/a><\/li>\n<\/ul>\n<p>Per the documents, it's recommended to add this to your schema directory in its own .schema file, then include that in the slapd config AFTER the other includes (as attributes need to be previously defined).  On ubuntu, you can install the schema2ldif package and it will help to install custom schema; you put the .schema file in the schema directory (\/etc\/ldap\/schema\/) and do &quot;ldap-schema-manager -i \/etc\/ldap\/schema\/surfrock66Schema.schema&quot;.<\/p>\n<p>To manage an LDAP environment, especially for things like user password resets, you'll want a simple web interface.  Everyone will tell you to use LDAP Account Manager (LAM) which is awesome and more modern, however if you're using custom schema it will not support it unless you pay for LAM Pro which is like $260\/year, and is not worth it in my opinion for a small homelab.  To that end I fell back to phpLDAPAdmin which is kind of abandoned, but still works.  The OS packages in the Ubuntu repositories won't work as they don't work with php7.2+, but there are installation instructions for the 1.2 branch on the github here: <a href=\"https:\/\/github.com\/leenooks\/phpLDAPadmin\">https:\/\/github.com\/leenooks\/phpLDAPadmin<\/a> If you have an existing apache environment, it is very straightforward, just extract the zip file, create a site in apache, and edit the config.php.  You'll do initial configuration with the admin user, using session bind.  Once we get down the road, users can reset their passwords using phpLDAPAdmin, but it's not super easy...given the scope of my home lab this guide is good enough: <a href=\"https:\/\/www.linvirtshell.com\/2017\/11\/openldap-user-password-reset-by-using.html\">https:\/\/www.linvirtshell.com\/2017\/11\/openldap-user-password-reset-by-using.html<\/a><\/p>\n<p>If I remove all the comments from my config.php file, this is the resultant configuration and it works well for me:<\/p>\n<pre><code>$config->custom->appearance['hide_template_warning'] = true;\n$config->custom->appearance['friendly_attrs'] = array(\n        'facsimileTelephoneNumber' => 'Fax',\n        'gid'                      => 'Group',\n        'mail'                     => 'Email',\n        'telephoneNumber'          => 'Telephone',\n        'uid'                      => 'User Name',\n        'userPassword'             => 'Password'\n);\n$servers = new Datastore();\n$servers->newServer('ldap_pla');\n$servers->setValue('server','name','Surfrock66\\'s LDAP Server');\n$servers->setValue('server','host','127.0.0.1');\n$servers->setValue('server','port',389);\n$servers->setValue('server','base',array('dc=example,dc=com'));\n$servers->setValue('login','auth_type','session');\n$servers->setValue('login','attr','dn');\n$servers->setValue('login','base',array('ou=accounts,dc=example,dc=com'));\n$servers->setValue('login','class',array('domainAccount'));\n$servers->setValue('login','bind_dn_template','cn=%s,ou=accounts,dc=example,dc=com');\n$servers->setValue('appearance','show_create',false);\n$servers->setValue('appearance','open_tree',true);\n$config->custom->session['reCAPTCHA-enable'] = false;\n$config->custom->session['reCAPTCHA-key-site'] = '<put-here-key-site>';\n$config->custom->session['reCAPTCHA-key-server'] = '<put-here-key-server>';<\/code><\/pre>\n<p>Since we're using custom schema, we need to add templates for them so the UI can create my users.  I'm not gonna dump the xml for that here, but it's well documented, you just create a copy of existing templates.  <a href=\"http:\/\/phpldapadmin.sourceforge.net\/wiki\/index.php\/Templates\">http:\/\/phpldapadmin.sourceforge.net\/wiki\/index.php\/Templates<\/a> I will provide links to my templates below.<\/p>\n<p>SO. TLDR; Let's say you just want to do everything I did.  Here's some prototype commands that should get you started, with downloads of the files I made\/collected:<\/p>\n<pre><code>sudo apt update\nsudo apt -y install slapd ldap-utils schema2ldif \n# cd web root\nsudo wget https:\/\/github.com\/leenooks\/phpLDAPadmin\/archive\/refs\/tags\/1.2.6.2.zip\nsudo unzip 1.2.6.2.zip\nsudo rm 1.2.6.2.zip\nsudo mv phpLDAPadmin-1.2.6.2 phpLDAPadmin\nsudo cp phpLDAPadmin\/config\/config.php.example phpLDAPadmin\/config\/config.php\nsudo vi phpLDAPadmin\/config\/config.php\n# Configure for your needs, ask google and there are lots of guides\nsudo cd phpLDAPadmin\/templates\/creation\nsudo wget https:\/\/surfrock66.com\/ldap\/custom_domainAccount.xml\n# Edit the above file if you want to re-order or remove any attributes from the account creation form\nsudo wget https:\/\/surfrock66.com\/ldap\/custom_domainGroup.xml\n# Edit the above file if you want to re-order or remove any attributes from the group creation form\nsudo cd ..\/..\/..\/\n# if your apache is not running under www-data, change this command to that user\nsudo chown -R www-data:www-data phpLDAPadmin\nsudo cd \/etc\/ldap\/schema\nsudo wget https:\/\/surfrock66.com\/ldap\/2.schema.attribute.sshPublicKey.schema\nsudo wget https:\/\/surfrock66.com\/ldap\/3.schema.objectClass.domainAccount.schema\n# Edit the above file with your own OID if you requested one\nsudo wget https:\/\/surfrock66.com\/ldap\/4.schema.objectClass.domainGroup.schema\n# Edit the above file with your own OID if you requested one\nmv 2.schema.attribute.sshPublicKey.schema ldapPublicKey.schema\nmv 3.schema.objectClass.domainAccount.schema domainAccount.schema\nmv 4.schema.objectClass.domainGroup.schema domainGroup.schema\ncd ~\/\nwget https:\/\/surfrock66.com\/ldap\/1.config.InstallMemberOf.ldif\nwget https:\/\/surfrock66.com\/ldap\/5.ous.ldif\n# Edit this file with your actual domain name\nsudo ldapadd -Y EXTERNAL -H ldapi:\/\/\/ -f 1.config.InstallMemberOf.ldif\nsudo ldapadd -Y EXTERNAL -H ldapi:\/\/\/ -f 5.ous.ldif\nsudo ldap-schema-manager -i \/etc\/ldap\/schema\/ldapPublicKey.schema\nsudo ldap-schema-manager -i \/etc\/ldap\/schema\/domainAccount.schema\nsudo ldap-schema-manager -i \/etc\/ldap\/schema\/domainGroup.schema\nsudo systemctl restart slapd<\/code><\/pre>\n<p>At this point, you should be able to log into phpLDAPadmin using the admin account created in the slapd package config.  You should make your groups before making your users; this could be changed if you moved the gidNumber from the &quot;Must&quot; to &quot;May&quot; of the domainAccount schema.<\/p>\n<p>Once this is done, create 2 users using the GUI to test that things work; I made one for me, and one for ldap binds (so I can tattoo a minimally privileged account with an unimportant password to apps for binding) called ldapbinduser.  The last step is to tattoo olcAccess rules to your configuration.  My desired rules are the following:<\/p>\n<ol>\n<li>My user, for now, is like a Domain Admin that can manage everyting<\/li>\n<li>The ldapbinduser account will be used for binds, meaning it needs to authenticate and read the users and groups (basically the whole directory)<\/li>\n<li>Any user should be able to read their own attributes, authenticate, and change their own password<\/li>\n<li>Everything else is denied.<\/li>\n<\/ol>\n<p>To that end, I made an ldif file called &quot;accessRules.ldif&quot; with the following contents, this article was instrucmental in making this: <a href=\"https:\/\/medium.com\/@moep\/keeping-your-sanity-while-designing-openldap-acls-9132068ed55c\">https:\/\/medium.com\/@moep\/keeping-your-sanity-while-designing-openldap-acls-9132068ed55c<\/a> :<\/p>\n<p>dn: olcDatabase={1}mdb,cn=config<\/p>\n<pre><code>changetype: modify\nreplace: olcAccess\nolcAccess: {0}to *\n  by dn=\"cn=admin,dc=example,dc=com\" manage\n  by dn=\"cn=surfrock66,ou=accounts,dc=example,dc=com\" manage\n  by dn=\"cn=ldapbinduser,ou=accounts,dc=example,dc=com\" read\n  by * break\n-\nadd: olcAccess\nolcAccess: {1}to dn.children=\"ou=accounts,dc=example,dc=com\" attrs=userPassword,shadowExpire,shadowInactive,shadowLastChange,shadowMax,shadowMin,shadowWarning\n  by self write\n  by anonymous auth\n-\nadd: olcAccess\nolcAccess: {2}to dn.subtree=\"dc=example,dc=com\"\n  by self read<\/code><\/pre>\n<p>To get that into your configuration, use the following command:<\/p>\n<p><code>ldapmodify -Y EXTERNAL -H ldapi:\/\/\/ -f accessRules.ldif<\/code><\/p>\n<p>With that, I have a working openLDAP setup.  Applications and Linux computers are authenticating and pulling attributes.  Based on what I've done here, it can be pretty easily adapted at a bunch of different points, but this is all the info I collected over the course of my research and should help to guide someone starting a similar journey.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This was originally posted Here but I&#8217;m cross-posting it to my site for preservation and self-reliance. I&#8217;ve been experimenting with user and account management solutions for my home lab, and I was looking for something that could manage posix users and web users. I wanted something with group management and OU hierarchies that sort of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1691","post","type-post","status-publish","format-standard","hentry","category-geek"],"_links":{"self":[{"href":"https:\/\/surfrock66.com\/index.php?rest_route=\/wp\/v2\/posts\/1691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/surfrock66.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/surfrock66.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/surfrock66.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/surfrock66.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1691"}],"version-history":[{"count":3,"href":"https:\/\/surfrock66.com\/index.php?rest_route=\/wp\/v2\/posts\/1691\/revisions"}],"predecessor-version":[{"id":1816,"href":"https:\/\/surfrock66.com\/index.php?rest_route=\/wp\/v2\/posts\/1691\/revisions\/1816"}],"wp:attachment":[{"href":"https:\/\/surfrock66.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/surfrock66.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/surfrock66.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}