Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- # Copyright 2011, Ante Karamatic
- # Init, Zagreb, Croatia
- # All rights reserved
- # Permission to use, copy, modify, and distribute this software and its
- # documentation for any purpose and without fee is hereby granted,
- # provided that the above copyright notice appear in all copies and that
- # both that copyright notice and this permission notice appear in
- # supporting documentation, and that the name of Ante Karamatic
- # not be used in advertising or publicity pertaining to distribution
- # of the software without specific, written prior permission.
- # ANTE KARAMATIC DISCLAIMS ALL WARRANTIES WITH REGARD TO
- # THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
- # FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE CENTRUM BE LIABLE
- # FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
- # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- from lxml import etree
- from StringIO import StringIO
- from suds.client import Client
- import base64
- import vars
- client = Client(vars.url)
- # FIXME: naci pametniji nacin za generiranje XML-a
- # AOSIsearch - returns attributes and values for a specific user
- #
- # Returns result as a dictionary
- # FIXME: malo to pametnije izvesti (error code i objasnjenje, i sl)
- def AOSIsearch(searchQ='(objectclass=*)', attributeQ=""):
- result = client.service.ldapSearch(vars.username, vars.passwordb64, vars.ldap_tree, searchQ, attributeQ)
- if result.code == 0:
- content = base64.b64decode(result.result)
- feed = StringIO(content)
- tree = etree.parse(feed)
- values = tree.xpath('/ldap/entry/attribute[@ldapname="' + attributeQ + '"]/value')
- attributes = attributeQ.split(",")
- result = {}
- for field in attributes:
- values_listed = ""
- values = tree.xpath('/ldap/entry/attribute[@ldapname="' + field + '"]/value')
- i = 0
- while i < len(values):
- if i == 0:
- values_listed = values_listed + values[i].text
- else:
- values_listed = values_listed + ',' + values[i].text
- i = i + 1
- print values_listed
- result[field] = values_listed
- else:
- result = {}
- return result
- # AOSImodify - modify one user's attribute
- #
- # Modifies multiple attributes at the time
- # It takes dictionary as an input
- # {'attribue': 'new value'}
- def AOSImodify(uidQ, dictionaryQ):
- ldap_xml = '<ldap><entry dn="uid=' + uidQ + ',' + vars.ldap_tree + '">'
- ldap_xml_footer = '</entry></ldap>'
- for attribute, value in dictionaryQ.iteritems():
- ldap_xml = ldap_xml + '<attribute ldapname="' + attribute + '"><value>' + value.decode("UTF-8") + '</value></attribute>'
- ldap_xml = ldap_xml + ldap_xml_footer
- result = client.service.ldapModifyAttribute(vars.username, vars.passwordb64, vars.ldap_tree, ldap_xml)
- return result.code
- # AOSImultimodify - modify users from dictionary
- #
- # Dictionary in the form of:
- # {'uid': {'attribute1': 'value1,value2', 'attribute2': 'value'}, 'uid2': {'attribute1': 'value'},...}
- def AOSImultimodify(dictionaryQ):
- ldap_xml = '<ldap>'
- for uid, attributes in dictionaryQ.iteritems():
- ldap_xml = ldap_xml + '<entry dn="uid=' + uid + ',' + vars.ldap_tree + '">'
- for attribute, value in attributes.iteritems():
- ldap_xml = ldap_xml + '<attribute ldapname="' + attribute + '">'
- values = value.split(",")
- for value in values:
- ldap_xml = ldap_xml + '<value>' + value.decode("UTF-8") + '</value>'
- ldap_xml = ldap_xml + '</attribute>'
- ldap_xml = ldap_xml + '</entry>'
- ldap_xml = ldap_xml + '</ldap>'
- print ldap_xml
- result = client.service.ldapModifyAttribute(vars.username, vars.passwordb64, vars.ldap_tree, ldap_xml)
- return result.code
- # AOSInewusers - create new users from dictionary
- #
- # Dictionary in the form of:
- # {'uid': {'attribute1': 'value1,value2', 'attribute2': 'value'}, 'uid2': {'attribute1': 'value'},...}
- def AOSInewusers(dictionaryQ):
- ldap_xml = '<ldap>'
- for uid, attributes in dictionaryQ.iteritems():
- ldap_xml = ldap_xml + '<entry dn="uid=' + uid + ',' + vars.ldap_tree + '">'
- for attribute, value in attributes.iteritems():
- ldap_xml = ldap_xml + '<attribute ldapname="' + attribute + '">'
- values = value.split(",")
- for value in values:
- ldap_xml = ldap_xml + '<value>' + value.decode("UTF-8") + '</value>'
- ldap_xml = ldap_xml + '</attribute>'
- ldap_xml = ldap_xml + '</entry>'
- ldap_xml = ldap_xml + '</ldap>'
- result = client.service.ldapAddUserLE(vars.username, vars.passwordb64, vars.ldap_tree, ldap_xml)
- return result.code
- # AOSIdeleteusers - deletes users based on a list of uids
- #
- # Returns dictionary or error codes for each uid
- def AOSIdeleteusers(listQ):
- results = {}
- for uid in listQ:
- ldap_xml = '<ldap><entry dn="uid=' + uid + ',' + vars.ldap_tree + '"></entry></ldap>'
- result = client.service.ldapDeleteUser(vars.username, vars.passwordb64, vars.ldap_tree, ldap_xml)
- results[uid] = result.code
- print results
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement