Skip to content

Commit bef9781

Browse files
authored
Merge pull request #50 from grrttedwards/master
Fix #49. Sync upstream changes to root/app/nginx-ldap-auth-daemon.py, and expose X-Ldap-DisableReferrals
2 parents 49c451d + d13ece8 commit bef9781

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

readme-vars.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ app_setup_block: |
4242
4343
# changelog
4444
changelogs:
45+
- { date: "20.06.23:", desc: "Sync upstream changes, including the ability to disable referrals with `X-Ldap-DisableReferrals`." }
4546
- { date: "25.05.23:", desc: "Rebase to Alpine 3.18, deprecate armhf." }
4647
- { date: "30.12.22:", desc: "Rebase to alpine 3.17." }
4748
- { date: "19.09.22:", desc: "Rebase to alpine 3.15." }

root/app/nginx-ldap-auth-daemon.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Copyright (C) 2014-2015 Nginx, Inc.
66
# Copyright (C) 2018 LinuxServer.io
77

8-
import sys, os, signal, base64, ldap, argparse
8+
import sys, os, signal, base64, ldap, ldap.filter, argparse
99
if sys.version_info.major == 2:
1010
from Cookie import BaseCookie
1111
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
@@ -101,7 +101,7 @@ def do_GET(self):
101101
self.log_error(e)
102102
return True
103103

104-
ctx['user'] = user
104+
ctx['user'] = ldap.filter.escape_filter_chars(user)
105105
ctx['pass'] = passwd
106106

107107
# Continue request processing
@@ -172,6 +172,7 @@ class LDAPAuthHandler(AuthHandler):
172172
'realm': ('X-Ldap-Realm', 'Restricted'),
173173
'url': ('X-Ldap-URL', None),
174174
'starttls': ('X-Ldap-Starttls', 'false'),
175+
'disable_referrals': ('X-Ldap-DisableReferrals', 'false'),
175176
'basedn': ('X-Ldap-BaseDN', None),
176177
'template': ('X-Ldap-Template', '(cn=%(username)s)'),
177178
'binddn': ('X-Ldap-BindDN', ''),
@@ -233,9 +234,9 @@ def do_GET(self):
233234
if ctx['starttls'] == 'true':
234235
ldap_obj.start_tls_s()
235236

236-
# See http://www.python-ldap.org/faq.shtml
237-
# uncomment, if required
238-
# ldap_obj.set_option(ldap.OPT_REFERRALS, 0)
237+
# See https://www.python-ldap.org/en/latest/faq.html
238+
if ctx['disable_referrals'] == 'true':
239+
ldap_obj.set_option(ldap.OPT_REFERRALS, 0)
239240

240241
ctx['action'] = 'binding as search user'
241242
ldap_obj.bind_s(ctx['binddn'], ctx['bindpasswd'], ldap.AUTH_SIMPLE)
@@ -252,13 +253,27 @@ def do_GET(self):
252253
searchfilter, ['objectclass'], 1)
253254

254255
ctx['action'] = 'verifying search query results'
255-
if len(results) < 1:
256+
257+
nres = len(results)
258+
259+
if nres < 1:
256260
self.auth_failed(ctx, 'no objects found')
257261
return
258262

259-
ctx['action'] = 'binding as an existing user'
260-
ldap_dn = results[0][0]
261-
ctx['action'] += ' "%s"' % ldap_dn
263+
if nres > 1:
264+
self.log_message("note: filter match multiple objects: %d, using first" % nres)
265+
266+
user_entry = results[0]
267+
ldap_dn = user_entry[0]
268+
269+
if ldap_dn == None:
270+
self.auth_failed(ctx, 'matched object has no dn')
271+
return
272+
273+
self.log_message('attempting to bind using dn "%s"' % (ldap_dn))
274+
275+
ctx['action'] = 'binding as an existing user "%s"' % ldap_dn
276+
262277
ldap_obj.bind_s(ldap_dn, ctx['pass'], ldap.AUTH_SIMPLE)
263278

264279
self.log_message('Auth OK for user "%s"' % (ctx['user']))
@@ -328,6 +343,7 @@ def exit_handler(signal, frame):
328343
'realm': ('X-Ldap-Realm', args.realm),
329344
'url': ('X-Ldap-URL', args.url),
330345
'starttls': ('X-Ldap-Starttls', args.starttls),
346+
'disable_referrals': ('X-Ldap-DisableReferrals', args.disable_referrals),
331347
'basedn': ('X-Ldap-BaseDN', args.basedn),
332348
'template': ('X-Ldap-Template', args.filter),
333349
'binddn': ('X-Ldap-BindDN', args.binddn),

0 commit comments

Comments
 (0)