Skip to content

Commit

Permalink
instead of introducing verbose mode, just add a lambda log function t…
Browse files Browse the repository at this point in the history
…o the ad support module
  • Loading branch information
oetiker committed Oct 13, 2015
1 parent e1f77bf commit 3b22db2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 18 deletions.
11 changes: 3 additions & 8 deletions PyAuthenNTLM2/ntlm_ad_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function
import socket
import datetime
import sys
from gssapi import *
from ntlm_proxy import NTLM_Proxy, NTLM_Proxy_Exception
Expand Down Expand Up @@ -222,17 +220,14 @@ class NTLM_AD_Proxy(NTLM_Proxy):
"""This is a class that handles one single NTLM authentication request like it was
a domain controller. However, it is just a proxy for the real, remote DC.
"""
def __init__(self, ipad, domain, socketFactory=socket, ldapFactory=None, base='', verbose=False, portAD=389):
global debug
def __init__(self, ipad, domain, socketFactory=socket, ldapFactory=None, base='', portAD=389, logFn=None):
NTLM_Proxy.__init__(self, ipad, portAD, domain, lambda: LDAP_Context(), socketFactory)
self.logFn = logFn
self.base = base
self.debug = verbose
#self.smbFactory = smbFactory or (lambda: SMB_Context())

def log(self,*msg):
if self.debug == False: return
st = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(st,*msg, file=sys.stderr)
if self.logFn: self.logFn(*msg)

def check_membership(self, user, groups, base=None, tabs=0, checked=[]):
"""Check if the given user belong to ANY of the given groups.
Expand Down
10 changes: 6 additions & 4 deletions PyAuthenNTLM2/ntlm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,13 @@ def print_help():
print_help()

if config['address'].startswith('ldap:'):
print "Using Active Directory (LDAP) to verify credentials."
url = urlparse(config['address'])
port = 389
if url.port: port = url.port
proxy = NTLM_AD_Proxy(url.netloc, config['domain'], base=urllib.unquote(url.path)[1:], verbose=config['verbose'], portAD=port)
port = url.port or 389
host = url.hostname
print "Using Active Directory (LDAP) to verify credentials: %s:%s." % (host,port)
logFn = None
if config['verbose']: logFn = lambda *msg: sys.stdout.write("* " + " ".join(map(str,msg)) + "\n")
proxy = NTLM_AD_Proxy(host, config['domain'], base=urllib.unquote(url.path)[1:], logFn = logFn, portAD=port)
else:
print "Using Domain Controller to verify credentials."
proxy = NTLM_DC_Proxy(config['address'], config['domain'], verbose=config['verbose'])
Expand Down
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ PythonOption NameFmt SAM|LogOn Set REMOTE_USER to the user name only (SA
legacy Logon format (domain\username).
This entry is optional. SAM is the default.
PythonOption WebProxyMode ON Work in the context of mod_proxy requests (default is OFF)
PythonOption VerboseMode ON Talk more while working (default is OFF)
===================================== ======

Apache needs to be configured to send keep alives (directive ``KeepAlive On``).
Expand Down
8 changes: 3 additions & 5 deletions pyntlm.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ def connect_to_proxy(req, type1):
for server in (pdc, bdc):
if not server: continue
try:
verbose_mode = req.get_options().get('VerboseMode','off').lower() == 'on'
if server.startswith('ldap:'):
url = urlparse(server)
decoded_path =urllib.unquote(url.path)[1:]
Expand All @@ -231,13 +230,12 @@ def connect_to_proxy(req, type1):
port = 389
req.log_error('PYTNLM: Initiating connection to Active Directory server %s:%s (domain %s) using base DN "%s".' %
(url.hostname, port, domain, decoded_path), apache.APLOG_INFO)


proxy = NTLM_AD_Proxy(url.hostname, domain, base=decoded_path, portAD=port, verbose=verbose_mode)
logFn = lambda *msg: req.log_error('PYNTLM: ' + " ".join(map(str,x)), apache.APLOG_INFO)
proxy = NTLM_AD_Proxy(url.hostname, domain, base=decoded_path, portAD=port, verbose=verbose_mode, logFn=logFn)
else:
req.log_error('PYTNLM: Initiating connection to Domain Controller server %s (domain %s).' %
(server, domain), apache.APLOG_INFO)
proxy = NTLM_DC_Proxy(server, domain,verbose=verbose_mode)
proxy = NTLM_DC_Proxy(server, domain)
ntlm_challenge = proxy.negotiate(type1)
except Exception, e:
req.log_error('PYNTLM: Error when retrieving Type 2 message from server(%s) = %s' % (server,str(e)), apache.APLOG_CRIT)
Expand Down

0 comments on commit 3b22db2

Please sign in to comment.