This post will be ready in a few days.... be patient:
in advance, here is available the program code
========================================================
Installing Python-LDAP requires only one commnad:
$ sudo apt-get install python-ldap
#!/usr/bin/python
import ldap
import ldif
import MySQLdb
import sys
from myldapClass import *
from keyGenerator import *
class myLDIF:
def __init__(self, database, user_db, passw_db, db):
self.database = database
self.user_db = user_db
self.passw_db = passw_db
self.db = db
def connect(self):
try:
print 'connecting to database...'
self.conn = MySQLdb.connect( host=self.database, user=self.user_db,passwd=self.passw_db, db=self.db )
self.conn.set_character_set('utf8')
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
print 'succesfully connected...'
def get_row(self, ldap_conn, base_dn_toAdd):
try:
cursor = self.conn.cursor()
cursor.execute ("SELECT * FROM princi ORDER BY legajo")
result_set = cursor.fetchall()
for row in result_set:
#print "%s, %s, %s" % (row[0], row[1], row[2])
if ( row[1].find(',') != -1):
#split by the first comma character
apellido, nombre = row[1].split(',',1)
else:
#split by the first whitespace
apellido1, nombre1 = row[1].split(' ',1)
dni = row[5].__str__()
dn = 'uid=' + dni + ',' + base_dn_toAdd
search_filter = '(objectclass=*)'
attrs = ['cn','sn', 'employeeNumber']
ldap_conn.search(dn, ldap.SCOPE_SUBTREE, search_filter, attrs)
if (ldap_conn.result_count > 0):
print 'This user already exist in the Ldap Server'
else:
# add this user to Ldap Server
kg = keyGenerator(9)
kg.generatePassword()
password_user = kg.password_user
legajo = row[0].__str__()
entry={'objectClass':['top','person','organizationalPerson','inetOrgPerson'],'cn':[nombre[1:]+' '+ apellido],'sn':[apellido], 'employeeNumber':[legajo],'mail':['cambiar@noexiste.com'],'uid':[dni],'userPassword':[password_user]}
ldif_writer=ldif.LDIFWriter(sys.stdout)
ldif_writer.unparse(dn,entry)
ldap_conn.addEntry(dn, entry)
cursor.close()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
def close(self):
self.conn.close()
======================
change this references!
References:
http://www.grotan.com/ldap/python-ldap-samples.html
http://python-ldap.sourceforge.net/doc/html/index.html
http://www.linuxjournal.com/article/6988
http://python-ldap.sourceforge.net/apps.shtml
http://www.iaeste.or.at/doc/python-ldap-doc/html/node3.html
http://www.packtpub.com/article/installing-and-configuring-the-python-ldap-library-and-binding-to-an-ldap-directory
Monday, November 3, 2008
Python - LDAP user management
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment