Skip to content

Instantly share code, notes, and snippets.

@tawateer
Last active July 4, 2016 14:15
Show Gist options
  • Save tawateer/2ec4b470961dd869ae3ba6c8ef7fdaed to your computer and use it in GitHub Desktop.
Save tawateer/2ec4b470961dd869ae3ba6c8ef7fdaed to your computer and use it in GitHub Desktop.
ldap example
#!/usr/bin/env python
import ldap
import base64
import hashlib
import binascii
import ldap.modlist as modlist
import subprocess
LDAP_HOST = "ldap.corp.nosa.com"
LDAP_DN = "ou=People,dc=nosa,dc=com"
LDAP_USER = "cn=root,dc=nosa,dc=com"
LDAP_PASS = ""
class WDJLdap(object):
def __init__(self, ldap_host=LDAP_HOST, ldap_dn=LDAP_DN, ldap_user=LDAP_USER, ldap_pass=LDAP_PASS):
self.ldapconn = ldap.initialize('ldap://%s' % ldap_host)
self.ldapconn.simple_bind_s("cn=root,dc=nosa,dc=com", ldap_pass)
def list_user(self):
retval = self.ldapconn.search_s(
'ou=People,dc=nosa,dc=com', ldap.SCOPE_SUBTREE, '(uid=*)', ['*'])
return retval
def search_user(self, uid):
retval = self.ldapconn.search_s(
'ou=People,dc=nosa,dc=com', ldap.SCOPE_SUBTREE, '(uid=%s)' % uid, ['*'])
return retval
def get_email(self, uid):
retval = self.ldapconn.search_s(
'ou=People,dc=nosa,dc=com', ldap.SCOPE_SUBTREE, '(uid=%s)' % uid, ['mail'])
return retval[0][1]['mail'][0]
def list_staff_user(self):
retval = self.ldapconn.search_s(
'ou=Group,dc=nosa,dc=com', ldap.SCOPE_SUBTREE, '(cn=staff)', ['*'])
return retval
def add_user(self, firstname, lastname, email, password):
name = firstname + " " + lastname
uid = email[:-14]
shatmp = hashlib.sha1()
shatmp.update(password)
shatmp1 = shatmp.hexdigest()
shatmp2 = binascii.unhexlify(shatmp1)
shatmp3 = base64.encodestring(shatmp2)
shatmp4 = shatmp3.strip()
shapasswd = "{SHA}" + shatmp4
info = {'cn': [name,],
'displayName': [name,],
'givenName': [firstname,],
'sn': [lastname,],
'mail': [email,],
'uid': [uid,],
'userPassword': [shapasswd,],
'objectclass': ['top', 'person', 'organizationalPerson', 'inetOrgPerson']
}
dn = 'uid=%s,ou=People,dc=nosa,dc=com' % uid
attr = [(k, v) for (k, v) in info.items()]
self.ldapconn.add_s(dn, attr)
group_dn = "cn=staff,ou=Group,dc=nosa,dc=com"
mod_attr = [ (ldap.MOD_ADD,'member',dn )]
self.ldapconn.modify_s(group_dn, mod_attr)
def change_passwd(self, username, newpass):
dn = 'uid=%s,ou=People,dc=nosa,dc=com' % username
shatmp = hashlib.sha1()
shatmp.update(newpass)
shatmp1 = shatmp.hexdigest()
shatmp2 = binascii.unhexlify(shatmp1)
shatmp3 = base64.encodestring(shatmp2)
shatmp4 = shatmp3.strip()
shapasswd = "{SHA}" + shatmp4
mod_attr = [ (ldap.MOD_REPLACE,'userPassword',shapasswd )]
self.ldapconn.modify_s(dn,mod_attr)
def delete_user_in_group(self, uid):
dn = 'uid=%s,ou=People,dc=nosa,dc=com' % uid
group_dn = "cn=staff,ou=Group,dc=nosa,dc=com"
mod_attr = [ (ldap.MOD_DELETE,'member',dn )]
self.ldapconn.modify_s(group_dn, mod_attr)
def del_user(self, uid):
dn = 'uid=%s,ou=People,dc=nosa,dc=com' % uid
email = self.get_email(uid)
self.ldapconn.delete_s(dn)
self.delete_user_in_group(uid)
cmd = "bash -x delete_gerrit_user.sh %s" % email
ret = subprocess.call(cmd,shell=True)
if ret != 0:
return False
def has_user(self, uid):
retval = self.ldapconn.search_s(
'ou=People,dc=nosa,dc=com', ldap.SCOPE_SUBTREE, '(uid=%s)' % uid, ['*'])
print retval
if len(retval):
return True
else:
return False
def unbind_link(self):
"""release the connection to ldap server"""
self.ldapconn.unbind()
def main():
demo = WDJLdap()
print demo.search_user("username")
if __name__ == '__main__':
main()
package main
import (
"fmt"
"gopkg.in/ldap.v2"
)
func ldapSearch(uid string) (bool, error) {
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", "ldap.corp.nosa.com", 389))
if err != nil {
return false, err
}
defer l.Close()
err = l.Bind("cn=root,dc=nosa,dc=com", "PASSWD")
if err != nil {
return false, err
}
filter := fmt.Sprintf("(uid=%s)", uid)
searchRequest := ldap.NewSearchRequest(
"ou=People,dc=nosa,dc=com", // The base dn to search
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
filter, // The filter to apply
[]string{"dn", "cn"}, // A list attributes to retrieve
nil,
)
sr, err := l.Search(searchRequest)
if err != nil {
return false, err
}
// for _, entry := range sr.Entries {
// fmt.Printf("%s: %v\n", entry.DN, entry.GetAttributeValue("cn"))
// }
return len(sr.Entries) != 0, nil
}
func main() {
x, _ := ldapSearch("liningning")
fmt.Println(x)
y, _ := ldapSearch("qinguoan")
fmt.Println(y)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment