ldaptree.py 3.02 KB
#!/usr/bin/python
from os.path import dirname, realpath
import getopt, sys
reload(sys)
sys.path.append(dirname(realpath(__file__)) + '/lib')
sys.setdefaultencoding('utf-8')

import getpass
from LdapTree import LdapTree

def usage():
    print "Usage: " + sys.argv[0] + " ARGUMENT... [OPTION]...\n"
    print "Create a tree representation of all DNs starting with a given base DN."
    print "Supports simple and kerberos binds via GSSAPI."
    print "If no password OPTION is given for simple binds the password will be"
    print "asked interactive."
    print "If no outfile the given the result will be written to stdout.\n"
    print "ARGUMENTS:\n"
    print "  {:30s} : {:s}".format('-H, --hosturi=URI', 'The URI to the ldap server to query in the form:')
    print "  {:30s}   {:s}".format('', 'ldap[s]://host.uri[:port]')
    print "  {:30s} : {:s}".format('-D, --binddn=DN', 'The DN to use for the LDAP bind.')
    print "  {:30s} : {:s}\n".format('-b, --basedn=DN', 'The DN to start the tree with.')
    print "OPTIONS:\n"
    print "  {:30s} : {:s}".format('-h, --help', 'Show this help page')
    print "  {:30s} : {:s}".format('-p, --password=PASSWORD', 'The password to use for the LDAP bind.')
    print "  {:30s} : {:s}".format('-o, --outfile=FILENAME', 'File to write the result to.')
    print "  {:30s} : {:s}".format('-k, --kerberos', 'Use gssapi auth.')

def main():
    try:
        opts, args = getopt.getopt(
                sys.argv[1:],
                'hkgH:D:b:p:o:',
                ['help', 'kerberos', 'hosturi=', 'binddn=', 'basedn=', 'password=', 'outfile='])
    except getopt.GetoptError as err:
        print str(err)
        usage()
        sys.exit(2)

    hosturi = binddn = basedn = password = outfile = None
    creategraph = False
    use_gssapi = False

    for o, a in opts:
        if o in ["-h", "--help"]:
            usage()
            sys.exit(0)
        elif o in ["-H", "--hosturi"]:
            hosturi = a
        elif o in ["-D", "--binddn"]:
            binddn = a
        elif o in ["-b", "--basedn"]:
            basedn = a
        elif o in ["-p", "--password"]:
            password = a
        elif o in ["-o", "--outfile"]:
            outfile = a
        elif o == "-g":
            creategraph = True
        elif o in ["-k", "--kerberos"]:
            use_gssapi = True;
        else:
            print "unknown parameter: " + a
            usage()
            sys.exit(2)

    if not hosturi or (not binddn and not use_gssapi) or not basedn:
        usage()
        sys.exit(2)

    if not password and not use_gssapi:
        password = getpass.getpass()

    info = LdapTree(hosturi, binddn, basedn, password, use_gssapi)

    try:
        if not creategraph:
            if outfile:
                info.text(outfile)
            else:
                print info.text()
        else:
            if outfile:
                info.graph(outfile)
            else:
                print info.graph()
    except UnicodeDecodeError as e:
        print e.object
        raise TypeError('failed')

if __name__ == "__main__":
    main()