def do_enable_cache(self, argstr): """Deletes the cache's contents, but does not affect it in any other way. usage: enable_cache [timeout] [maxmem] default is NO_LIMIT for both. Value should be an integer or NO_LIMIT. """ timeout = ldap.NO_LIMIT maxmem = ldap.NO_LIMIT if argstr: args = split_args(argstr) if len(args) > 0: if args[0] == "NO_LIMIT": timeout = ldap.NO_LIMIT else: try: timeout = int(args[0]) except: print "error: invalid timeout format. Should be integer." if len(args) > 1: if args[1] == "NO_LIMIT": maxmem = ldap.NO_LIMIT else: try: maxmem = int(args[1]) except: print "error: invalid maxmem format. Should be integer." try: self.conn.enable_cache(timeout, maxmem) except LDAPError, e: print "error:", sys.exc_type, e
def do_cd(self, argstr): """Change default location in directory.""" if argstr: args = split_args(argstr) dn = args[0] if args[0] == "..": dn_comps = ldap.explode_dn(self.dn) dn = ",".join(dn_comps[1:]) self.dn = dn elif args[0] == ".": return else: dn = self.get_dn(dn) self.dn = dn self.prompt = "ldapsh %s> " % dn
def do_init(self, argstr): """Initialize connection parameters but don't actually connect until necessary. usage: init [host] [port]""" if argstr: args = split_args(argstr) self.host = args[0] if len(args) > 1: self.port = int(args[1]) try: new_conn = ldap.init(self.host, self.port) if self.conn: self.conn.unbind() self.conn = new_conn except LDAPError, e: print "error:", sys.exc_type, e
def do_open(self, argstr): """Open connection to directory usage: open [host] [port] default is localhost port 389""" if argstr: args = split_args(argstr) print "args: ", args self.host = args[0] if len(args) > 1: self.port = int(args[1]) try: new_conn = ldap.open(self.host, self.port) if self.conn: self.conn.unbind() self.conn = new_conn except LDAPError, e: print "error:", sys.exc_type, e
def do_get_option(self, argstr): """Get an LDAP option. usage: get_option option option name does not include the OPT_ prefix. """ args = split_args(argstr) if len(args) < 1: print "error: Not enough arguments" return option_str = "ldap.OPT_%s" % args[0] try: option = eval(option_str) except: print "error: invalid option name" return try: print self.conn.get_option(option) except LDAPError, e: print "error:", sys.exc_type, e
def do_ls(self, argstr): """Display list of entries. usage: ls [location] location defaults to current location """ if not self.conn: print "Not bound to directory." return dn = self.dn if argstr: args = split_args(argstr) if len(args): dn = args[0] try: result = self.conn.search_s(dn, ldap.SCOPE_ONELEVEL, "objectclass=*") for entry in result: rdns = ldap.explode_dn(entry[0]) dn_index = self.cache_dn(entry[0]) print "%d %s" % (dn_index, rdns[0]) except LDAPError, e: print "error:", sys.exc_type, e
def do_bind(self, argstr): """Bind to directory as a specific user. usage: bind [user] [pass] user defaults to "" pass defaults to "" """ if not self.conn: self.conn = ldap.init(self.host, self.port) if argstr: args = split_args(argstr) self.user = self.get_dn(args[0]) if len(args) > 1: self.cred = args[1] else: self.cred = getpass.getpass("password: "******"Binding as", self.user else: print "Binding anonymously" self.conn.simple_bind_s(self.user, self.cred) except LDAPError, e: print "error:", sys.exc_type, e