Python LDB authentication via LDAP

William Brown william.e.brown at
Thu Jan 5 21:53:18 MST 2012

> You can build a credentials object manually.  it does not need to be
> done with the options parsing code.

After a bit of help from IRC the final test script is a standalone implementation of the samba-tool user list command, but without the OptionParser Libraries involved. You should be able to use this in any application that needs to connect to the SamDB, that isn't from the command line (IE a web application, gtk application etc ...). 

import samba
import sys, ldb

from samba import dsdb
from samba.samdb import SamDB
from samba.param import LoadParm
from samba.auth import system_session
from samba.credentials import Credentials

lp = LoadParm()
creds = Credentials()

samdb = SamDB(url='ldap://', session_info=system_session(), credentials=creds, lp=lp)

domain_dn = samdb.domain_dn()
print domain_dn
res =, scope=ldb.SCOPE_SUBTREE,
                   % (ldb.OID_COMPARATOR_AND, dsdb.UF_NORMAL_ACCOUNT)),
for msg in res:
	print("%s\n" % msg.get("samaccountname", idx=0))


William Brown

Research & Teaching, Technology Services
The University of Adelaide, AUSTRALIA 5005

CRICOS Provider Number 00123M
IMPORTANT: This message may contain confidential or legally privileged
information. If you think it was sent to you by mistake, please delete all
copies and advise the sender. For the purposes of the SPAM Act 2003, this
email is authorised by The University of Adelaide.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 841 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <>

More information about the samba-technical mailing list