Merge branch 'master' of git://git.engineering.redhat.com/users/rcritten/freeipa2

This commit is contained in:
Jason Gerard DeRose 2008-12-20 15:00:00 -07:00
commit 6fdf5d1e7b
13 changed files with 399 additions and 216 deletions

View File

@ -375,7 +375,7 @@ class IPAdmin(SimpleLDAPObject):
except ldap.ALREADY_EXISTS, e: except ldap.ALREADY_EXISTS, e:
raise errors.DuplicateEntry, "Entry already exists" raise errors.DuplicateEntry, "Entry already exists"
except ldap.LDAPError, e: except ldap.LDAPError, e:
raise e raise DatabaseError, e
return True return True
def updateRDN(self, dn, newrdn): def updateRDN(self, dn, newrdn):
@ -392,7 +392,7 @@ class IPAdmin(SimpleLDAPObject):
self.set_option(ldap.OPT_SERVER_CONTROLS, sctrl) self.set_option(ldap.OPT_SERVER_CONTROLS, sctrl)
self.modrdn_s(dn, newrdn, delold=1) self.modrdn_s(dn, newrdn, delold=1)
except ldap.LDAPError, e: except ldap.LDAPError, e:
raise e raise DatabaseError, e
return True return True
def updateEntry(self,dn,oldentry,newentry): def updateEntry(self,dn,oldentry,newentry):
@ -474,7 +474,7 @@ class IPAdmin(SimpleLDAPObject):
self.set_option(ldap.OPT_SERVER_CONTROLS, sctrl) self.set_option(ldap.OPT_SERVER_CONTROLS, sctrl)
self.modify_s(dn, modlist) self.modify_s(dn, modlist)
except ldap.LDAPError, e: except ldap.LDAPError, e:
raise e raise DatabaseError, e
return True return True
def deleteEntry(self,*args): def deleteEntry(self,*args):
@ -486,8 +486,10 @@ class IPAdmin(SimpleLDAPObject):
if sctrl is not None: if sctrl is not None:
self.set_option(ldap.OPT_SERVER_CONTROLS, sctrl) self.set_option(ldap.OPT_SERVER_CONTROLS, sctrl)
self.delete_s(*args) self.delete_s(*args)
except ldap.INSUFFICIENT_ACCESS, e:
raise errors.InsufficientAccess, e
except ldap.LDAPError, e: except ldap.LDAPError, e:
raise e raise errors.DatabaseError, e
return True return True
def modifyPassword(self,dn,oldpass,newpass): def modifyPassword(self,dn,oldpass,newpass):
@ -533,7 +535,10 @@ def notfound(args):
if len(args) > 2: if len(args) > 2:
searchfilter = args[2] searchfilter = args[2]
try: try:
# Python re doesn't do paren counting so the string could
# have a trailing paren "foo)"
target = re.match(r'\(.*=(.*)\)', searchfilter).group(1) target = re.match(r'\(.*=(.*)\)', searchfilter).group(1)
target = target.replace(")","")
except: except:
target = searchfilter target = searchfilter
return "%s not found" % str(target) return "%s not found" % str(target)

View File

@ -267,12 +267,15 @@ class ldap(CrudBackend):
objectclass = kw.get('objectclass') objectclass = kw.get('objectclass')
sfilter = kw.get('filter') sfilter = kw.get('filter')
attributes = kw.get('attributes') attributes = kw.get('attributes')
base = kw.get('base')
if attributes: if attributes:
del kw['attributes'] del kw['attributes']
else: else:
attributes = ['*'] attributes = ['*']
if objectclass: if objectclass:
del kw['objectclass'] del kw['objectclass']
if base:
del kw['base']
if sfilter: if sfilter:
del kw['filter'] del kw['filter']
(exact_match_filter, partial_match_filter) = self._generate_search_filters(**kw) (exact_match_filter, partial_match_filter) = self._generate_search_filters(**kw)
@ -283,7 +286,10 @@ class ldap(CrudBackend):
exact_match_filter = "(%s%s)" % (sfilter, exact_match_filter) exact_match_filter = "(%s%s)" % (sfilter, exact_match_filter)
partial_match_filter = "(%s%s)" % (sfilter, partial_match_filter) partial_match_filter = "(%s%s)" % (sfilter, partial_match_filter)
search_base = "%s, %s" % (self.api.env.container_accounts, self.api.env.basedn) if not base:
base = self.api.env.container_accounts
search_base = "%s, %s" % (base, self.api.env.basedn)
try: try:
exact_results = servercore.search(search_base, exact_results = servercore.search(search_base,
exact_match_filter, attributes) exact_match_filter, attributes)

View File

@ -697,14 +697,25 @@ class CLI(object):
if self.options.interactive: if self.options.interactive:
self.prompt_interactively(cmd, kw) self.prompt_interactively(cmd, kw)
self.prompt_for_passwords(cmd, kw) self.prompt_for_passwords(cmd, kw)
self.set_defaults(cmd, kw)
result = cmd(**kw) result = cmd(**kw)
if callable(cmd.output_for_cli): if callable(cmd.output_for_cli):
for param in cmd.params(): for param in cmd.params():
if param.ispassword(): if param.ispassword():
try:
del kw[param.name] del kw[param.name]
except KeyError:
pass
(args, options) = cmd.params_2_args_options(kw) (args, options) = cmd.params_2_args_options(kw)
cmd.output_for_cli(self.api.Backend.textui, result, *args, **options) cmd.output_for_cli(self.api.Backend.textui, result, *args, **options)
def set_defaults(self, cmd, kw):
for param in cmd.params():
if not kw.get(param.name):
value = param.get_default(**kw)
if value:
kw[param.name] = value
def prompt_for_passwords(self, cmd, kw): def prompt_for_passwords(self, cmd, kw):
for param in cmd.params(): for param in cmd.params():
if 'password' not in param.flags: if 'password' not in param.flags:

View File

@ -423,6 +423,14 @@ class HostService(ConfigurationError):
"""You must enroll a host in order to create a host service""" """You must enroll a host in order to create a host service"""
faultCode = 1026 faultCode = 1026
class InsufficientAccess(GenericError):
"""You do not have permission to perform this task"""
faultCode = 1027
class InvalidUserPrincipal(GenericError):
"""Invalid user principal"""
faultCode = 1028
class FunctionDeprecated(GenericError): class FunctionDeprecated(GenericError):
"""Raised by a deprecated function""" """Raised by a deprecated function"""
faultCode = 2000 faultCode = 2000

View File

@ -34,14 +34,14 @@ from ldap import explode_dn
map_attributes = ['automountMapName', 'description', ] map_attributes = ['automountMapName', 'description', ]
key_attributes = ['description', 'automountKey', 'automountInformation'] key_attributes = ['description', 'automountKey', 'automountInformation']
def display_entry(entry): def display_entry(textui, entry):
# FIXME: for now delete dn here. In the future pass in the kw to # FIXME: for now delete dn here. In the future pass in the kw to
# output_for_cli() # output_for_cli()
attr = sorted(entry.keys()) attr = sorted(entry.keys())
for a in attr: for a in attr:
if a != 'dn': if a != 'dn':
print "%s: %s" % (a, entry[a]) textui.print_plain("%s: %s" % (a, entry[a]))
def make_automount_dn(mapname): def make_automount_dn(mapname):
""" """
@ -96,12 +96,11 @@ class automount_addmap(crud.Add):
kw['objectClass'] = ['automountMap'] kw['objectClass'] = ['automountMap']
return ldap.create(**kw) return ldap.create(**kw)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, map, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret: textui.print_plain("Automount map %s added" % map)
print "Automount map added"
api.register(automount_addmap) api.register(automount_addmap)
@ -139,12 +138,11 @@ class automount_addkey(crud.Add):
kw['objectClass'] = ['automount'] kw['objectClass'] = ['automount']
return ldap.create(**kw) return ldap.create(**kw)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret: textui.print_plain("Automount key added")
print "Automount key added"
api.register(automount_addkey) api.register(automount_addkey)
@ -161,17 +159,16 @@ class automount_delmap(crud.Del):
:param kw: Not used. :param kw: Not used.
""" """
ldap = self.api.Backend.ldap ldap = self.api.Backend.ldap
dn = ldap.find_entry_dn("automountmapname", mapname, "automountmap") dn = ldap.find_entry_dn("automountmapname", mapname, "automountmap", api.env.container_automount)
keys = api.Command['automount_getkeys'](mapname) keys = api.Command['automount_getkeys'](mapname)
if keys: if keys:
for k in keys: for k in keys:
ldap.delete(k.get('dn')) ldap.delete(k.get('dn'))
return ldap.delete(dn) return ldap.delete(dn)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret:
print "Automount map and associated keys deleted" print "Automount map and associated keys deleted"
api.register(automount_delmap) api.register(automount_delmap)
@ -205,11 +202,10 @@ class automount_delkey(crud.Del):
if not keydn: if not keydn:
raise errors.NotFound raise errors.NotFound
return ldap.delete(keydn) return ldap.delete(keydn)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret:
print "Automount key deleted" print "Automount key deleted"
api.register(automount_delkey) api.register(automount_delkey)
@ -238,11 +234,10 @@ class automount_modmap(crud.Mod):
dn = ldap.find_entry_dn("automountmapname", mapname, "automountmap", api.env.container_automount) dn = ldap.find_entry_dn("automountmapname", mapname, "automountmap", api.env.container_automount)
return ldap.update(dn, **kw) return ldap.update(dn, **kw)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret:
print "Automount map updated" print "Automount map updated"
api.register(automount_modmap) api.register(automount_modmap)
@ -286,12 +281,12 @@ class automount_modkey(crud.Mod):
raise errors.NotFound raise errors.NotFound
return ldap.update(keydn, **kw) return ldap.update(keydn, **kw)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret:
print "Automount key updated" print "Automount key updated"
api.register(automount_modkey) api.register(automount_modkey)
@ -309,26 +304,27 @@ class automount_findmap(crud.Find):
kw[s] = term kw[s] = term
kw['objectclass'] = 'automountMap' kw['objectclass'] = 'automountMap'
kw['base'] = api.env.container_automount
if kw.get('all', False): if kw.get('all', False):
kw['attributes'] = ['*'] kw['attributes'] = ['*']
else: else:
kw['attributes'] = map_attributes kw['attributes'] = map_attributes
return ldap.search(**kw) return ldap.search(**kw)
def output_for_cli(self, entries):
if not entries: def output_for_cli(self, textui, result, *args, **options):
return counter = result[0]
counter = entries[0] entries = result[1:]
entries = entries[1:]
if counter == 0: if counter == 0:
print "No entries found" textui.print_plain("No entries found")
return return
elif counter == -1: elif counter == -1:
print "These results are truncated." textui.print_plain("These results are truncated.")
print "Please refine your search and try again." textui.print_plain("Please refine your search and try again.")
for e in entries: for e in entries:
display_entry(e) display_entry(textui, e)
print "" textui.print_plain("")
api.register(automount_findmap) api.register(automount_findmap)
@ -350,26 +346,26 @@ class automount_findkey(crud.Find):
kw[s] = term kw[s] = term
kw['objectclass'] = 'automount' kw['objectclass'] = 'automount'
kw['base'] = api.env.container_automount
if kw.get('all', False): if kw.get('all', False):
kw['attributes'] = ['*'] kw['attributes'] = ['*']
else: else:
kw['attributes'] = key_attributes kw['attributes'] = key_attributes
return ldap.search(**kw) return ldap.search(**kw)
def output_for_cli(self, entries): def output_for_cli(self, textui, result, *args, **options):
if not entries: counter = result[0]
return entries = result[1:]
counter = entries[0]
entries = entries[1:]
if counter == 0: if counter == 0:
print "No entries found" textui.print_plain("No entries found")
return return
elif counter == -1: elif counter == -1:
print "These results are truncated." textui.print_plain("These results are truncated.")
print "Please refine your search and try again." textui.print_plain("Please refine your search and try again.")
for e in entries: for e in entries:
display_entry(e) display_entry(textui, e)
print "" textui.print_plain("")
api.register(automount_findkey) api.register(automount_findkey)
@ -394,9 +390,9 @@ class automount_showmap(crud.Get):
return ldap.retrieve(dn) return ldap.retrieve(dn)
else: else:
return ldap.retrieve(dn, map_attributes) return ldap.retrieve(dn, map_attributes)
def output_for_cli(self, entry): def output_for_cli(self, textui, result, *args, **options):
if entry: if result:
display_entry(entry) display_entry(textui, result)
api.register(automount_showmap) api.register(automount_showmap)
@ -436,16 +432,16 @@ class automount_showkey(crud.Get):
return ldap.retrieve(keydn) return ldap.retrieve(keydn)
else: else:
return ldap.retrieve(keydn, key_attributes) return ldap.retrieve(keydn, key_attributes)
def output_for_cli(self, entry): def output_for_cli(self, textui, result, *args, **options):
# The automount map name associated with this key is available only # The automount map name associated with this key is available only
# in the dn. Add it as an attribute to display instead. # in the dn. Add it as an attribute to display instead.
if entry and not entry.get('automountmapname'): if result and not result.get('automountmapname'):
elements = explode_dn(entry.get('dn').lower()) elements = explode_dn(result.get('dn').lower())
for e in elements: for e in elements:
(attr, value) = e.split('=',1) (attr, value) = e.split('=',1)
if attr == 'automountmapname': if attr == 'automountmapname':
entry['automountmapname'] = value result['automountmapname'] = value
display_entry(entry) display_entry(textui, result)
api.register(automount_showkey) api.register(automount_showkey)
@ -475,9 +471,89 @@ class automount_getkeys(frontend.Command):
keys = [] keys = []
return keys return keys
def output_for_cli(self, keys): def output_for_cli(self, textui, result, *args, **options):
if keys: for k in result:
for k in keys: textui.print_plain('%s' % k.get('automountkey'))
print k.get('automountkey')
api.register(automount_getkeys) api.register(automount_getkeys)
class automount_getmaps(frontend.Command):
'Retrieve all automount maps'
takes_args = (
Param('automountmapname?',
cli_name='mapname',
primary_key=True,
doc='A group of related automount objects',
),
)
def execute(self, mapname, **kw):
"""
Execute the automount-getmaps operation.
Return a list of all automount maps.
"""
ldap = self.api.Backend.ldap
base = api.env.container_automount + "," + api.env.basedn
if not mapname:
mapname = "auto.master"
search_base = "automountmapname=%s,%s" % (mapname, base)
maps = ldap.get_one_entry(search_base, "objectClass=*", ["*"])
return maps
def output_for_cli(self, textui, result, *args, **options):
for k in result:
textui.print_plain('%s: %s' % (k.get('automountinformation'), k.get('automountkey')))
api.register(automount_getmaps)
class automount_addindirectmap(crud.Add):
'Add a new automap indirect mount point.'
takes_options = (
Param('parentmap?',
cli_name='parentmap',
default='auto.master',
doc='The parent map to connect this to. Default: auto.master'),
Param('automountkey',
cli_name='key',
doc='An entry in an automount map'),
Param('description?',
doc='A description of the automount map'),
)
def execute(self, mapname, **kw):
"""
Execute the automount-addindirectmap operation.
Returns the key entry as it will be created in LDAP.
This function creates 2 LDAP entries. It creates an
automountmapname entry and an automountkey entry.
:param mapname: The map name being added.
:param kw['parentmap'] is the top-level map to add this to.
defaulting to auto.master
:param kw['automountkey'] is the mount point
:param kw['description'] is a textual description of this map
"""
mapkw = {}
if kw.get('description'):
mapkw['description'] = kw.get('description')
newmap = api.Command['automount_addmap'](mapname, **mapkw)
keykw = {'automountkey': kw['automountkey'], 'automountinformation': mapname}
if kw.get('description'):
keykw['description'] = kw.get('description')
newkey = api.Command['automount_addkey'](kw['parentmap'], **keykw)
return newkey
def output_for_cli(self, textui, result, map, **options):
"""
Output result of this command to command line interface.
"""
textui.print_plain("Indirect automount map %s added" % map)
api.register(automount_addindirectmap)

View File

@ -29,6 +29,19 @@ from ipalib import errors
from ipalib import ipa_types from ipalib import ipa_types
def get_members(members):
"""
Return a list of members.
It is possible that the value passed in is None.
"""
if members:
members = members.split(',')
else:
members = []
return members
class group(frontend.Object): class group(frontend.Object):
""" """
Group object. Group object.
@ -83,12 +96,13 @@ class group_add(crud.Add):
return ldap.create(**kw) return ldap.create(**kw)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret: textui.print_name(self.name)
print "Group added" textui.print_entry(result)
textui.print_dashed('Added group "%s"' % result['cn'])
api.register(group_add) api.register(group_add)
@ -121,12 +135,11 @@ class group_del(crud.Del):
return ldap.delete(dn) return ldap.delete(dn)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, cn):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret: textui.print_plain("Deleted group %s" % cn)
print "Group deleted"
api.register(group_del) api.register(group_del)
@ -151,12 +164,12 @@ class group_mod(crud.Mod):
dn = ldap.find_entry_dn("cn", cn, "posixGroup") dn = ldap.find_entry_dn("cn", cn, "posixGroup")
return ldap.update(dn, **kw) return ldap.update(dn, **kw)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, cn, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret: if result:
print "Group updated" textui.print_plain("Group updated")
api.register(group_mod) api.register(group_mod)
@ -171,30 +184,33 @@ class group_find(crud.Find):
search_fields_conf_str = config.get('ipagroupsearchfields') search_fields_conf_str = config.get('ipagroupsearchfields')
search_fields = search_fields_conf_str.split(",") search_fields = search_fields_conf_str.split(",")
search_kw = {}
for s in search_fields: for s in search_fields:
kw[s] = term search_kw[s] = term
object_type = ldap.get_object_type("cn") object_type = ldap.get_object_type("cn")
if object_type and not kw.get('objectclass'): if object_type and not kw.get('objectclass'):
kw['objectclass'] = object_type search_kw['objectclass'] = object_type
return ldap.search(**kw) return ldap.search(**search_kw)
def output_for_cli(self, groups): def output_for_cli(self, textui, result, uid, **options):
if not groups: counter = result[0]
groups = result[1:]
if counter == 0 or len(groups) == 0:
textui.print_plain("No entries found")
return return
if len(groups) == 1:
counter = groups[0] textui.print_entry(groups[0])
groups = groups[1:]
if counter == 0:
print "No entries found"
return return
elif counter == -1: textui.print_name(self.name)
print "These results are truncated."
print "Please refine your search and try again."
for g in groups: for g in groups:
for a in g.keys(): textui.print_entry(g)
print "%s: %s" % (a, g[a]) textui.print_plain('')
if counter == -1:
textui.print_plain("These results are truncated.")
textui.print_plain("Please refine your search and try again.")
textui.print_count(groups, '%d groups matched')
api.register(group_find) api.register(group_find)
@ -218,12 +234,24 @@ class group_show(crud.Get):
# FIXME: should kw contain the list of attributes to display? # FIXME: should kw contain the list of attributes to display?
return ldap.retrieve(dn) return ldap.retrieve(dn)
def output_for_cli(self, group): def output_for_cli(self, textui, result, *args, **options):
if not group: counter = result[0]
groups = result[1:]
if counter == 0 or len(groups) == 0:
textui.print_plain("No entries found")
return return
if len(groups) == 1:
for a in group.keys(): textui.print_entry(groups[0])
print "%s: %s" % (a, group[a]) return
textui.print_name(self.name)
for u in groups:
textui.print_plain('%(givenname)s %(sn)s:' % u)
textui.print_entry(u)
textui.print_plain('')
if counter == -1:
textui.print_plain('These results are truncated.')
textui.print_plain('Please refine your search and try again.')
textui.print_count(groups, '%d groups matched')
api.register(group_show) api.register(group_show)
@ -253,7 +281,7 @@ class group_add_member(frontend.Command):
to_add = [] to_add = []
completed = 0 completed = 0
members = kw.get('groups', '').split(',') members = get_members(kw.get('groups', ''))
for m in members: for m in members:
if not m: continue if not m: continue
try: try:
@ -263,7 +291,7 @@ class group_add_member(frontend.Command):
add_failed.append(m) add_failed.append(m)
continue continue
members = kw.get('users', '').split(',') members = get_members(kw.get('users', ''))
for m in members: for m in members:
if not m: continue if not m: continue
try: try:
@ -282,11 +310,11 @@ class group_add_member(frontend.Command):
return add_failed return add_failed
def output_for_cli(self, add_failed): def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if add_failed: if result:
print "These entries failed to add to the group:" print "These entries failed to add to the group:"
for a in add_failed: for a in add_failed:
print "\t'%s'" % a print "\t'%s'" % a
@ -320,7 +348,7 @@ class group_remove_member(frontend.Command):
remove_failed = [] remove_failed = []
completed = 0 completed = 0
members = kw.get('groups', '').split(',') members = get_members(kw.get('groups', ''))
for m in members: for m in members:
if not m: continue if not m: continue
try: try:
@ -330,7 +358,7 @@ class group_remove_member(frontend.Command):
remove_failed.append(m) remove_failed.append(m)
continue continue
members = kw.get('users', '').split(',') members = get_members(kw.get('users', ''))
for m in members: for m in members:
try: try:
member_dn = ldap.find_entry_dn("uid", m,) member_dn = ldap.find_entry_dn("uid", m,)
@ -348,11 +376,11 @@ class group_remove_member(frontend.Command):
return remove_failed return remove_failed
def output_for_cli(self, remove_failed): def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if remove_failed: if result:
print "These entries failed to be removed from the group:" print "These entries failed to be removed from the group:"
for a in remove_failed: for a in remove_failed:
print "\t'%s'" % a print "\t'%s'" % a

View File

@ -149,12 +149,11 @@ class host_add(crud.Add):
kw['objectclass'].remove('krbprincipalaux') kw['objectclass'].remove('krbprincipalaux')
return ldap.create(**kw) return ldap.create(**kw)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret: textui.print_plain("Host added")
print "Host added"
api.register(host_add) api.register(host_add)
@ -172,12 +171,11 @@ class host_del(crud.Del):
ldap = self.api.Backend.ldap ldap = self.api.Backend.ldap
dn = get_host(hostname) dn = get_host(hostname)
return ldap.delete(dn) return ldap.delete(dn)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret: textui.print_plain("Host deleted")
print "Host deleted"
api.register(host_del) api.register(host_del)
@ -202,12 +200,11 @@ class host_mod(crud.Mod):
dn = get_host(hostname) dn = get_host(hostname)
return ldap.update(dn, **kw) return ldap.update(dn, **kw)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret: textui.print_plain("Host updated")
print "Host updated"
api.register(host_mod) api.register(host_mod)
@ -232,31 +229,29 @@ class host_find(crud.Find):
#search_fields = search_fields_conf_str.split(",") #search_fields = search_fields_conf_str.split(",")
search_fields = ['cn','serverhostname','description','localityname','nshostlocation','nshardwareplatform','nsosversion'] search_fields = ['cn','serverhostname','description','localityname','nshostlocation','nshardwareplatform','nsosversion']
search_kw = {}
for s in search_fields: for s in search_fields:
kw[s] = term search_kw[s] = term
# Can't use ldap.get_object_type() since cn is also used for group dns # Can't use ldap.get_object_type() since cn is also used for group dns
kw['objectclass'] = "ipaHost" search_kw['objectclass'] = "ipaHost"
if kw.get('all', False): if kw.get('all', False):
kw['attributes'] = ['*'] search_kw['attributes'] = ['*']
else: else:
kw['attributes'] = default_attributes search_kw['attributes'] = default_attributes
return ldap.search(**kw) return ldap.search(**search_kw)
def output_for_cli(self, hosts): def output_for_cli(self, textui, result, *args, **options):
if not hosts: counter = result[0]
return hosts = result[1:]
counter = hosts[0]
hosts = hosts[1:]
if counter == 0: if counter == 0:
print "No entries found" textui.print_plain("No entries found")
return return
elif counter == -1:
print "These results are truncated."
print "Please refine your search and try again."
for h in hosts: for h in hosts:
for a in h.keys(): textui.print_entry(h)
print "%s: %s" % (a, h[a]) if counter == -1:
textui.print_plain("These results are truncated.")
textui.print_plain("Please refine your search and try again.")
api.register(host_find) api.register(host_find)
@ -286,9 +281,7 @@ class host_show(crud.Get):
value = ldap.retrieve(dn, default_attributes) value = ldap.retrieve(dn, default_attributes)
del value['dn'] del value['dn']
return value return value
def output_for_cli(self, host): def output_for_cli(self, textui, result, *args, **options):
if host: textui.print_entry(result)
for a in host.keys():
print "%s: %s" % (a, host[a])
api.register(host_show) api.register(host_show)

View File

@ -30,6 +30,19 @@ from ipalib import ipa_types
hostgroup_filter = "groupofnames)(!(objectclass=posixGroup)" hostgroup_filter = "groupofnames)(!(objectclass=posixGroup)"
def get_members(members):
"""
Return a list of members.
It is possible that the value passed in is None.
"""
if members:
members = members.split(',')
else:
members = []
return members
class hostgroup(frontend.Object): class hostgroup(frontend.Object):
""" """
Host Group object. Host Group object.
@ -80,12 +93,11 @@ class hostgroup_add(crud.Add):
return ldap.create(**kw) return ldap.create(**kw)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret: textui.print_plain("Group added")
print "Group added"
api.register(hostgroup_add) api.register(hostgroup_add)
@ -107,12 +119,11 @@ class hostgroup_del(crud.Del):
return ldap.delete(dn) return ldap.delete(dn)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret: textui.print_plain("Group deleted")
print "Group deleted"
api.register(hostgroup_del) api.register(hostgroup_del)
@ -137,12 +148,11 @@ class hostgroup_mod(crud.Mod):
dn = ldap.find_entry_dn("cn", cn, hostgroup_filter) dn = ldap.find_entry_dn("cn", cn, hostgroup_filter)
return ldap.update(dn, **kw) return ldap.update(dn, **kw)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if ret: texui.print_plain("Group updated")
print "Group updated"
api.register(hostgroup_mod) api.register(hostgroup_mod)
@ -159,28 +169,26 @@ class hostgroup_find(crud.Find):
search_fields_conf_str = config.get('ipagroupsearchfields') search_fields_conf_str = config.get('ipagroupsearchfields')
search_fields = search_fields_conf_str.split(",") search_fields = search_fields_conf_str.split(",")
search_kw = {}
for s in search_fields: for s in search_fields:
kw[s] = term search_kw[s] = term
kw['objectclass'] = hostgroup_filter search_kw['objectclass'] = hostgroup_filter
return ldap.search(**kw) return ldap.search(**search_kw)
def output_for_cli(self, groups): def output_for_cli(self, textui, result, *args, **options):
if not groups: counter = result[0]
return groups = result[1:]
counter = groups[0]
groups = groups[1:]
if counter == 0: if counter == 0:
print "No entries found" textui.print_plain("No entries found")
return return
elif counter == -1:
print "These results are truncated."
print "Please refine your search and try again."
for g in groups: for g in groups:
for a in g.keys(): textui.print_entry(g)
print "%s: %s" % (a, g[a])
if counter == -1:
textui.print_plain("These results are truncated.")
textui.print_plain("Please refine your search and try again.")
api.register(hostgroup_find) api.register(hostgroup_find)
@ -206,12 +214,8 @@ class hostgroup_show(crud.Get):
# FIXME: should kw contain the list of attributes to display? # FIXME: should kw contain the list of attributes to display?
return ldap.retrieve(dn) return ldap.retrieve(dn)
def output_for_cli(self, group): def output_for_cli(self, textui, result, *args, **options):
if not group: textui.print_entry(result)
return
for a in group.keys():
print "%s: %s" % (a, group[a])
api.register(hostgroup_show) api.register(hostgroup_show)
@ -241,7 +245,7 @@ class hostgroup_add_member(frontend.Command):
to_add = [] to_add = []
completed = 0 completed = 0
members = kw.get('groups', '').split(',') members = get_members(kw.get('groups', ''))
for m in members: for m in members:
if not m: continue if not m: continue
try: try:
@ -251,7 +255,7 @@ class hostgroup_add_member(frontend.Command):
add_failed.append(m) add_failed.append(m)
continue continue
members = kw.get('hosts', '').split(',') members = get_members(kw.get('hosts', ''))
for m in members: for m in members:
if not m: continue if not m: continue
try: try:
@ -270,16 +274,16 @@ class hostgroup_add_member(frontend.Command):
return add_failed return add_failed
def output_for_cli(self, add_failed): def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if add_failed: if result:
print "These entries failed to add to the group:" textui.print_plain("These entries failed to add to the group:")
for a in add_failed: for a in result:
print "\t'%s'" % a print "\t'%s'" % a
else: else:
print "Group membership updated." textui.print_entry("Group membership updated.")
api.register(hostgroup_add_member) api.register(hostgroup_add_member)
@ -309,7 +313,7 @@ class hostgroup_remove_member(frontend.Command):
remove_failed = [] remove_failed = []
completed = 0 completed = 0
members = kw.get('groups', '').split(',') members = get_members(kw.get('groups', ''))
for m in members: for m in members:
if not m: continue if not m: continue
try: try:
@ -319,7 +323,7 @@ class hostgroup_remove_member(frontend.Command):
remove_failed.append(m) remove_failed.append(m)
continue continue
members = kw.get('hosts', '').split(',') members = get_members(kw.get('hosts', ''))
for m in members: for m in members:
if not m: continue if not m: continue
try: try:
@ -338,15 +342,15 @@ class hostgroup_remove_member(frontend.Command):
return remove_failed return remove_failed
def output_for_cli(self, remove_failed): def output_for_cli(self, textui, result, *args, **options):
""" """
Output result of this command to command line interface. Output result of this command to command line interface.
""" """
if remove_failed: if result:
print "These entries failed to be removed from the group:" textui.print_plain("These entries failed to be removed from the group:")
for a in remove_failed: for a in result:
print "\t'%s'" % a print "\t'%s'" % a
else: else:
print "Group membership updated." textui.print_plain("Group membership updated.")
api.register(hostgroup_remove_member) api.register(hostgroup_remove_member)

View File

@ -52,14 +52,12 @@ class passwd(frontend.Command):
:param param uid: The login name of the user being updated. :param param uid: The login name of the user being updated.
:param kw: Not used. :param kw: Not used.
""" """
if principal.find('@') < 0: if principal.find('@') > 0:
u = principal.split('@') u = principal.split('@')
if len(u) > 2 or len(u) == 0: if len(u) > 2:
print "Invalid user name (%s)" % principal raise errors.InvalidUserPrincipal, principal
if len(u) == 1:
principal = principal+"@"+self.api.env.realm
else: else:
principal = principal principal = principal+"@"+self.api.env.realm
dn = self.Backend.ldap.find_entry_dn( dn = self.Backend.ldap.find_entry_dn(
"krbprincipalname", "krbprincipalname",
principal, principal,

View File

@ -88,9 +88,8 @@ class pwpolicy_mod(frontend.Command):
return ldap.update(dn, **kw) return ldap.update(dn, **kw)
def output_for_cli(self, ret): def output_for_cli(self, textui, result, *args, **options):
if ret: textui.print_plain("Policy modified")
print "Policy modified"
api.register(pwpolicy_mod) api.register(pwpolicy_mod)
@ -120,14 +119,12 @@ class pwpolicy_show(frontend.Command):
return policy return policy
def output_for_cli(self, policy): def output_for_cli(self, textui, result, *args, **options):
if not policy: return textui.print_plain("Password Policy")
textui.print_plain("Min. Password Lifetime (hours): %s" % result.get('krbminpwdlife'))
print "Password Policy" textui.print_plain("Max. Password Lifetime (days): %s" % result.get('krbmaxpwdlife'))
print "Min. Password Lifetime (hours): %s" % policy.get('krbminpwdlife') textui.print_plain("Min. Number of Character Classes: %s" % result.get('krbpwdmindiffchars'))
print "Max. Password Lifetime (days): %s" % policy.get('krbmaxpwdlife') textui.print_plain("Min. Length of Password: %s" % result.get('krbpwdminlength'))
print "Min. Number of Character Classes: %s" % policy.get('krbpwdmindiffchars') textui.print_plain("Password History Size: %s" % result.get('krbpwdhistorylength'))
print "Min. Length of Password: %s" % policy.get('krbpwdminlength')
print "Password History Size: %s" % policy.get('krbpwdhistorylength')
api.register(pwpolicy_show) api.register(pwpolicy_show)

View File

@ -149,31 +149,30 @@ class service_find(crud.Find):
def execute(self, principal, **kw): def execute(self, principal, **kw):
ldap = self.api.Backend.ldap ldap = self.api.Backend.ldap
kw['filter'] = "&(objectclass=krbPrincipalAux)(!(objectClass=posixAccount))(!(|(krbprincipalname=kadmin/*)(krbprincipalname=K/M@*)(krbprincipalname=krbtgt/*)))" search_kw = {}
kw['krbprincipalname'] = principal search_kw['filter'] = "&(objectclass=krbPrincipalAux)(!(objectClass=posixAccount))(!(|(krbprincipalname=kadmin/*)(krbprincipalname=K/M@*)(krbprincipalname=krbtgt/*)))"
search_kw['krbprincipalname'] = principal
object_type = ldap.get_object_type("krbprincipalname") object_type = ldap.get_object_type("krbprincipalname")
if object_type and not kw.get('objectclass'): if object_type and not kw.get('objectclass'):
kw['objectclass'] = object_type search_kw['objectclass'] = object_type
return ldap.search(**kw) return ldap.search(**search_kw)
def output_for_cli(self, services): def output_for_cli(self, textui, result, *args, **options):
if not services: counter = result[0]
return services = result[1:]
counter = services[0]
services = services[1:]
if counter == 0: if counter == 0:
print "No entries found" textui.print_plain("No entries found")
return return
elif counter == -1:
print "These results are truncated."
print "Please refine your search and try again."
for s in services: for s in services:
for a in s.keys(): textui.print_entry(s)
print "%s: %s" % (a, s[a])
if counter == -1:
textui.print_plain("These results are truncated.")
textui.print_plain("Please refine your search and try again.")
textui.print_count(services, '%d services matched')
api.register(service_find) api.register(service_find)
@ -196,11 +195,7 @@ class service_show(crud.Get):
dn = ldap.find_entry_dn("krbprincipalname", principal) dn = ldap.find_entry_dn("krbprincipalname", principal)
# FIXME: should kw contain the list of attributes to display? # FIXME: should kw contain the list of attributes to display?
return ldap.retrieve(dn) return ldap.retrieve(dn)
def output_for_cli(self, service): def output_for_cli(self, textui, result, *args, **options):
if not service: textui.print_entry(result)
return
for a in service.keys():
print "%s: %s" % (a, service[a])
api.register(service_show) api.register(service_show)

View File

@ -264,17 +264,18 @@ class user_find(crud.Find):
search_fields_conf_str = config.get('ipausersearchfields') search_fields_conf_str = config.get('ipausersearchfields')
search_fields = search_fields_conf_str.split(",") search_fields = search_fields_conf_str.split(",")
search_kw = {}
for s in search_fields: for s in search_fields:
kw[s] = term search_kw[s] = term
object_type = ldap.get_object_type("uid") object_type = ldap.get_object_type("uid")
if object_type and not kw.get('objectclass'): if object_type and not kw.get('objectclass'):
kw['objectclass'] = object_type search_kw['objectclass'] = object_type
if kw.get('all', False): if kw.get('all', False):
kw['attributes'] = ['*'] search_kw['attributes'] = ['*']
else: else:
kw['attributes'] = default_attributes search_kw['attributes'] = default_attributes
return ldap.search(**kw) return ldap.search(**search_kw)
def output_for_cli(self, textui, result, uid, **options): def output_for_cli(self, textui, result, uid, **options):
counter = result[0] counter = result[0]
@ -287,7 +288,9 @@ class user_find(crud.Find):
return return
textui.print_name(self.name) textui.print_name(self.name)
for u in users: for u in users:
textui.print_plain('%(givenname)s %(sn)s:' % u) gn = u.get('givenname', '')
sn= u.get('sn', '')
textui.print_plain('%s %s:' % (gn, sn))
textui.print_entry(u) textui.print_entry(u)
textui.print_plain('') textui.print_plain('')
if counter == -1: if counter == -1:

View File

@ -182,3 +182,62 @@ class test_Service(XMLRPC_test):
pass pass
else: else:
assert False assert False
class test_Indirect(XMLRPC_test):
"""
Test the `f_automount` plugin Indirect map function.
"""
mapname='auto.home'
keyname='/home'
parentmap='auto.master'
description='Home directories'
map_kw={'automountkey': keyname, 'parentmap': parentmap, 'description': description}
def test_add_indirect(self):
"""
Test adding an indirect map.
"""
res = api.Command['automount_addindirectmap'](self.mapname, **self.map_kw)
assert res
assert res.get('automountinformation','') == self.mapname
def test_doshowkey(self):
"""
Test the `xmlrpc.automount_showkey` method.
"""
showkey_kw={'automountmapname': self.parentmap, 'automountkey': self.keyname}
res = api.Command['automount_showkey'](**showkey_kw)
assert res
assert res.get('automountkey','') == self.keyname
def test_remove_key(self):
"""
Remove the indirect key /home
"""
delkey_kw={'automountmapname': self.parentmap, 'automountkey': self.keyname}
res = api.Command['automount_delkey'](**delkey_kw)
assert res == True
# Verify that it is gone
try:
res = api.Command['automount_showkey'](**delkey_kw)
except errors.NotFound:
pass
else:
assert False
def test_remove_map(self):
"""
Remove the indirect map for auto.home
"""
res = api.Command['automount_delmap'](self.mapname)
assert res == True
# Verify that it is gone
try:
res = api.Command['automount_showmap'](self.mapname)
except errors.NotFound:
pass
else:
assert False