This is a fork of mattv8/yourls-ldap-plugin that adds two features for production LDAPS deployments:
- Full
ldaps://URI support via a newLDAPAUTH_LDAP_URIconstant. - Strict TLS certificate verification (
LDAP_OPT_X_TLS_DEMAND) for StartTLS, instead ofLDAP_OPT_X_TLS_NEVER.
The original plugin only supports ldap_connect(host, port) and, if LDAPAUTH_START_TLS is set, it used LDAP_OPT_X_TLS_NEVER. This fork lets you point YOURLS at a real ldaps:// endpoint and still verifies the server certificate.
--- a/plugin.php
+++ b/plugin.php
@@ -126,7 +126,10 @@ function ldapauth_get_ldap_connection()
} else {
- return ldap_connect(LDAPAUTH_HOST, LDAPAUTH_PORT);
+ if (defined("LDAPAUTH_LDAP_URI") && LDAPAUTH_LDAP_URI) {
+ return ldap_connect(LDAPAUTH_LDAP_URI);
+ }
+ return ldap_connect(LDAPAUTH_HOST, LDAPAUTH_PORT);
}
@@
ldap_set_option($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, 3);
+ ldap_set_option($ldapConnection, LDAP_OPT_X_TLS_REQUIRE_CERT, LDAP_OPT_X_TLS_DEMAND);
if (defined("LDAPAUTH_START_TLS") && LDAPAUTH_START_TLS) {
@ldap_start_tls($ldapConnection);
}- Download
plugin.php. - Copy the plugin folder into your
user/plugins/folder for YOURLS. - Set up the parameters for the plugin in
user/config.php(see below). - Activate the plugin with the plugin manager in the admin interface.
When the plugin is enabled and a user was not successfully authenticated using data specified in $yourls_user_passwords, an LDAP authentication attempt will be made. If LDAP authentication is successful, then you will immediately go to the admin interface.
You can also set a privileged account to search the LDAP directory with. This is useful for directories that don't allow anonymous binding. If you define a suitable template, the current user will be used for binding. This is useful for Active Directory / Samba.
Setting the groups settings will check if the user is a member of that group before logging them in and storing their credentials. This check is only performed the first time they authenticate or when their password changes.
By default, the plugin implements a simple cache of LDAP users. As well as reducing requests to the LDAP server, this has the effect of allowing the YOURLS API to work with LDAP users.
These are the available constants for user/config.php.
define( 'LDAPAUTH_LDAP_URI', 'ldaps://ldap.domain.com:636' );(new) Full LDAP URI, including protocol and port. Use this forldaps://. If set, it overridesLDAPAUTH_HOST/LDAPAUTH_PORT.define( 'LDAPAUTH_HOST', 'ldap.domain.com' );(fallback) LDAP host name or IP. Used only ifLDAPAUTH_LDAP_URIis not defined.define( 'LDAPAUTH_PORT', 636 );(fallback) LDAP server port — often389forldap://or636forldaps://.define( 'LDAPAUTH_BASE', 'dc=domain,dc=com' );Base DN (location of users).define( 'LDAPAUTH_USERNAME_FIELD', 'uid');(optional) LDAP field name in which the username is stored.define( 'LDAPAUTH_START_TLS', false );(optional) Set totrueto use StartTLS onldap://port 389. Not used whenLDAPAUTH_LDAP_URIis anldaps://URI.
- For
ldaps://, setLDAPAUTH_LDAP_URIto the full URI and keepLDAPAUTH_START_TLSasfalse. - For StartTLS on port 389, leave
LDAPAUTH_LDAP_URIunset and setLDAPAUTH_START_TLStotrue. - The CA certificate must be in the system's trust store (e.g.
/etc/ssl/certs, updated withupdate-ca-certificates, orSSL_CERT_FILE/CURL_CA_BUNDLEfor libldap). This plugin now usesLDAP_OPT_X_TLS_DEMAND, so a valid certificate is required.
define( 'LDAPAUTH_SEARCH_USER', 'cn=your-user,dc=domain,dc=com' );(optional) Privileged user to search with.define( 'LDAPAUTH_SEARCH_PASS', 'the-pass');(optional) Privileged user password (only ifLDAPAUTH_SEARCH_USERis set).
define( 'LDAPAUTH_BIND_WITH_USER_TEMPLATE', '%s@myad.domain' );(optional) Use%sas the placeholder for the current username.
define( 'LDAPAUTH_SEARCH_FILTER', '(&(samaccountname=%s)(memberof=YOURLS-ADMINS,OU=Groups,DC=example,DC=com))' );Use%sas the placeholder for the current username. If this option is not set, the filter is based only onLDAPAUTH_USERNAME_FIELD.
For AD nested groups, the LDAP_MATCHING_RULE_IN_CHAIN OID (1.2.840.113556.1.4.1941) can be used:
define( 'LDAPAUTH_SEARCH_FILTER', '(&(samaccountname=%s)(memberof:1.2.840.113556.1.4.1941:=YOURLS-ADMINS,OU=Groups,DC=example,DC=com))' );Attribute based (default):
define( 'LDAPAUTH_GROUP_MODE', 'attribute');define( 'LDAPAUTH_GROUP_ATTR', 'memberof' );(optional)define( 'LDAPAUTH_GROUP_REQ', 'the-group;another-admin-group');Group/s the user must be in. Allows multiple, semicolon-delimited.
Group based:
define( 'LDAPAUTH_GROUP_MODE', 'group');define( 'LDAPAUTH_GROUP_BASE', 'ou=groups,dc=example,dc=org' );define( 'LDAPAUTH_GROUP_ATTR', 'cn' );define( 'LDAPAUTH_GROUP_MEMBER', 'member' );define( 'LDAPAUTH_GROUP_MEMBER_TYPE', 'dn' );eitherdn(default) oruiddefine( 'LDAPAUTH_GROUP_REQ', 'yourlsadmin');Group/s user must be in. Semicolon-delimited.
Group-based authorization requires LDAPAUTH_SEARCH_USER and LDAPAUTH_SEARCH_PASS.
define( 'LDAPAUTH_GROUP_SCOP', 'sub' );Scope of group search:sub(default, checks all the subtree) orbase(only the exact group).define( 'LDAPAUTH_USERCACHE_TYPE', 1);1caches users in the options table (default).0turns off caching.define( 'LDAPAUTH_ADD_NEW', true );(optional) Add LDAP users toconfig.php.define( 'LDAPAUTH_DNS_SITES_AND_SERVICES', '_ldap._tcp.corporate._sites.yourdomain.com' );If using AD with multiple DCs, this DNS SRV lookup is used to find active LDAP server names. If set, it overrides the hostname portion ofLDAPAUTH_HOST.define( 'LDAPAUTH_LDAP_OPT_REFERRALS', 0 );(optional) Defaults to1. Set to0to prevent following AD referrals, which can sometimes causeldap_searchto hang.
define( 'LDAPAUTH_LDAP_URI', 'ldaps://ad.example.com:636' );
define( 'LDAPAUTH_BASE', 'DC=example,DC=com' );
define( 'LDAPAUTH_USERNAME_FIELD', 'sAMAccountName' );
define( 'LDAPAUTH_SEARCH_USER', 'CN=ldap,DC=example,DC=com' );
define( 'LDAPAUTH_SEARCH_PASS', 'your-secure-password' );
define( 'LDAPAUTH_START_TLS', false );
define( 'LDAPAUTH_ALL_USERS_ADMIN', true );
define( 'LDAPAUTH_USERCACHE_TYPE', 1 );define( 'LDAPAUTH_HOST', 'ad.example.com' );
define( 'LDAPAUTH_PORT', 389 );
define( 'LDAPAUTH_BASE', 'DC=example,DC=com' );
define( 'LDAPAUTH_USERNAME_FIELD', 'sAMAccountName' );
define( 'LDAPAUTH_SEARCH_USER', 'CN=ldap,DC=example,DC=com' );
define( 'LDAPAUTH_SEARCH_PASS', 'your-secure-password' );
define( 'LDAPAUTH_START_TLS', true );
define( 'LDAPAUTH_ALL_USERS_ADMIN', true );
define( 'LDAPAUTH_USERCACHE_TYPE', 1 );- Check the PHP error log, usually at
/var/log/php.log. - Check your web server logs.
- You can enable
LDAPAUTH_DEBUGto print more debug info.
When a successful login is made against an LDAP server, the plugin will cache the username and encrypted password. Currently, this is done by saving them in an array in the YOURLS options table. This has some advantages:
- It reduces requests to the LDAP server.
- It means that users can still log in even if the LDAP server is unreachable.
- It means that the YOURLS API can be used by LDAP users.
Unfortunately, the cache will not scale well. This is because it integrates tightly with YOURLS's internal auth mechanism, and that does not scale. If you have a few tens of LDAP users likely to use your YOURLS installation, it should be fine. Much more than that, and you may see performance issues. If so, you should probably disable the cache. This will mean that your LDAP users will not be able to use the API. At least not unless they are also listed in users/config.php, which suffers from the same scaling problems.
Original Plugin Author(s):
Copyright 2013 K3A, #1davoaust
Copyright 2013 Nicholas Waller (code@nicwaller.com) as I used some parts of his CAS authentication plugin :)
Maintainer(s):
Matt Visnovsky #mattv8
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.