Python LDB authentication via LDAP

William Brown william.e.brown at adelaide.edu.au
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()
creds.guess(lp)
creds.set_username('Administrator')
creds.set_password('12345')

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

domain_dn = samdb.domain_dn()
print domain_dn
res = samdb.search(domain_dn, scope=ldb.SCOPE_SUBTREE,
                   expression=("(&(objectClass=user)(userAccountControl:%s:=%u))"
                   % (ldb.OID_COMPARATOR_AND, dsdb.UF_NORMAL_ACCOUNT)),
                   attrs=["samaccountname"])
for msg in res:
	print("%s\n" % msg.get("samaccountname", idx=0))



Sincerely,

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.

pgp.mit.edu




-------------- 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: <http://lists.samba.org/pipermail/samba-technical/attachments/20120106/9f010a8c/attachment.pgp>


More information about the samba-technical mailing list