2009-04-21 08:22:56 -05:00
# Authors:
# Jason Gerard DeRose <jderose@redhat.com>
# Pavel Zuna <pzuna@redhat.com>
#
# Copyright (C) 2008 Red Hat
# see file 'COPYING' for use and warranty information
#
2010-12-09 06:59:11 -06:00
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
2009-04-21 08:22:56 -05:00
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
2010-12-09 06:59:11 -06:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2011-08-24 21:48:30 -05:00
2018-04-05 02:21:16 -05:00
from __future__ import absolute_import
2017-05-23 11:35:57 -05:00
import logging
2015-12-16 12:04:20 -06:00
import time
2013-01-08 03:10:35 -06:00
from time import gmtime , strftime
import posixpath
2017-02-23 03:44:04 -06:00
2015-09-11 06:43:28 -05:00
import six
2015-12-16 12:04:20 -06:00
from ipalib import api
from ipalib import errors
from ipalib import Bool , Flag , Str
2016-04-20 08:41:34 -05:00
from . baseuser import (
2015-12-16 09:06:03 -06:00
baseuser ,
baseuser_add ,
baseuser_del ,
baseuser_mod ,
baseuser_find ,
baseuser_show ,
NO_UPG_MAGIC ,
UPG_DEFINITION_DN ,
baseuser_output_params ,
validate_nsaccountlock ,
convert_nsaccountlock ,
fix_addressbook_permission_bindrule ,
baseuser_add_manager ,
2016-06-23 13:06:42 -05:00
baseuser_remove_manager ,
2017-01-18 06:24:29 -06:00
baseuser_add_cert ,
baseuser_remove_cert ,
2016-06-23 13:06:42 -05:00
baseuser_add_principal ,
2016-12-20 09:21:58 -06:00
baseuser_remove_principal ,
baseuser_add_certmapdata ,
baseuser_remove_certmapdata )
2016-04-20 08:41:34 -05:00
from . idviews import remove_ipaobject_overrides
2014-06-10 10:27:51 -05:00
from ipalib . plugable import Registry
2016-04-20 08:41:34 -05:00
from . baseldap import (
2016-06-29 23:37:52 -05:00
LDAPObject ,
2015-12-16 12:04:20 -06:00
pkey_to_value ,
LDAPCreate ,
LDAPSearch ,
LDAPQuery ,
2017-01-18 06:24:29 -06:00
LDAPMultiQuery )
2016-04-20 08:41:34 -05:00
from . import baseldap
2011-08-24 21:48:30 -05:00
from ipalib . request import context
from ipalib import _ , ngettext
2012-02-14 08:41:25 -06:00
from ipalib import output
2014-05-29 07:47:17 -05:00
from ipaplatform . paths import paths
2019-06-15 09:25:51 -05:00
from ipaplatform . constants import constants as platformconstants
2015-12-16 12:04:20 -06:00
from ipapython . dn import DN
2017-02-23 03:44:04 -06:00
from ipapython . ipaldap import LDAPClient
2016-12-21 08:07:34 -06:00
from ipapython . ipautil import ipa_generate_password , TMP_PWD_ENTROPY_BITS
2013-01-08 03:10:35 -06:00
from ipalib . capabilities import client_has_capability
2019-03-27 05:30:40 -05:00
from ipaserver . masters import get_masters
2015-12-16 09:06:03 -06:00
2015-09-11 06:43:28 -05:00
if six . PY3 :
unicode = str
2011-08-24 21:48:30 -05:00
__doc__ = _ ( """
2010-08-24 22:40:32 -05:00
Users
2010-06-02 13:08:50 -05:00
2010-08-24 22:40:32 -05:00
Manage user entries . All users are POSIX users .
2010-12-21 11:23:40 -06:00
IPA supports a wide range of username formats , but you need to be aware of any
restrictions that may apply to your particular environment . For example ,
2011-03-04 10:08:54 -06:00
usernames that start with a digit or usernames that exceed a certain length
2010-12-21 11:23:40 -06:00
may cause problems for some UNIX systems .
Use ' ipa config-mod ' to change the username format allowed by IPA tools .
2010-11-08 21:45:27 -06:00
Disabling a user account prevents that user from obtaining new Kerberos
2010-08-24 22:40:32 -05:00
credentials . It does not invalidate any credentials that have already
been issued .
2010-06-02 13:08:50 -05:00
2011-02-16 07:47:42 -06:00
Password management is not a part of this module . For more information
about this topic please see : ipa help passwd
2011-02-16 02:10:14 -06:00
2012-04-23 03:16:45 -05:00
Account lockout on password failure happens per IPA master . The user - status
command can be used to identify which master the user is locked out on .
2012-09-16 11:35:56 -05:00
It is on that master the administrator must unlock the user .
2012-04-23 03:16:45 -05:00
2010-06-02 13:08:50 -05:00
EXAMPLES :
2010-08-24 22:40:32 -05:00
Add a new user :
2011-02-17 06:25:57 -06:00
ipa user - add - - first = Tim - - last = User - - password tuser1
2010-06-02 13:08:50 -05:00
2010-08-24 22:40:32 -05:00
Find all users whose entries include the string " Tim " :
2010-06-02 13:08:50 -05:00
ipa user - find Tim
2010-08-24 22:40:32 -05:00
Find all users with " Tim " as the first name :
2010-06-02 13:08:50 -05:00
ipa user - find - - first = Tim
2010-10-05 14:37:37 -05:00
Disable a user account :
ipa user - disable tuser1
2010-06-02 13:08:50 -05:00
2010-10-05 14:37:37 -05:00
Enable a user account :
ipa user - enable tuser1
2010-06-02 13:08:50 -05:00
Delete a user :
ipa user - del tuser1
2011-08-24 21:48:30 -05:00
""" )
2009-04-21 08:22:56 -05:00
2017-05-23 11:35:57 -05:00
logger = logging . getLogger ( __name__ )
2014-06-10 10:27:51 -05:00
register = Registry ( )
2011-05-16 05:56:04 -05:00
2011-09-16 08:35:48 -05:00
2015-03-05 07:25:33 -06:00
user_output_params = baseuser_output_params
2011-09-16 08:35:48 -05:00
2020-06-10 11:16:54 -05:00
MEMBEROF_ADMINS = " (memberOf= {} ) " . format (
DN ( ' cn=admins ' , api . env . container_group , api . env . basedn )
)
NOT_MEMBEROF_ADMINS = ' (! {} ) ' . format ( MEMBEROF_ADMINS )
2011-09-16 08:35:48 -05:00
2012-08-16 19:28:44 -05:00
def check_protected_member ( user , protected_group_name = u ' admins ' ) :
'''
Ensure the last enabled member of a protected group cannot be deleted or
disabled by raising LastMemberError .
'''
# Get all users in the protected group
result = api . Command . user_find ( in_group = protected_group_name )
# Build list of users in the protected group who are enabled
result = result [ ' result ' ]
enabled_users = [ entry [ ' uid ' ] [ 0 ] for entry in result if not entry [ ' nsaccountlock ' ] ]
# If the user is the last enabled user raise LastMemberError exception
if enabled_users == [ user ] :
raise errors . LastMemberError ( key = user , label = _ ( u ' group ' ) ,
container = protected_group_name )
2014-06-10 10:27:51 -05:00
@register ( )
2015-03-05 07:25:33 -06:00
class user ( baseuser ) :
2009-04-21 08:22:56 -05:00
"""
User object .
"""
2015-03-05 07:25:33 -06:00
container_dn = baseuser . active_container_dn
label = _ ( ' Users ' )
label_singular = _ ( ' User ' )
object_name = _ ( ' user ' )
object_name_plural = _ ( ' users ' )
2020-06-10 11:16:54 -05:00
permission_filter_objectclasses_string = ' (objectclass=posixaccount) '
2014-03-26 11:11:23 -05:00
managed_permissions = {
' System: Read User Standard Attributes ' : {
' replaces_global_anonymous_aci ' : True ,
' ipapermbindruletype ' : ' anonymous ' ,
' ipapermright ' : { ' read ' , ' search ' , ' compare ' } ,
' ipapermdefaultattr ' : {
' objectclass ' , ' cn ' , ' sn ' , ' description ' , ' title ' , ' uid ' ,
' displayname ' , ' givenname ' , ' initials ' , ' manager ' , ' gecos ' ,
2014-06-19 05:25:56 -05:00
' gidnumber ' , ' homedirectory ' , ' loginshell ' , ' uidnumber ' ,
' ipantsecurityidentifier '
2014-03-26 11:11:23 -05:00
} ,
} ,
' System: Read User Addressbook Attributes ' : {
' replaces_global_anonymous_aci ' : True ,
' ipapermbindruletype ' : ' all ' ,
' ipapermright ' : { ' read ' , ' search ' , ' compare ' } ,
' ipapermdefaultattr ' : {
' seealso ' , ' telephonenumber ' ,
2015-02-19 10:10:37 -06:00
' facsimiletelephonenumber ' , ' l ' , ' ou ' , ' st ' , ' postalcode ' , ' street ' ,
2014-03-26 11:11:23 -05:00
' destinationindicator ' , ' internationalisdnnumber ' ,
' physicaldeliveryofficename ' , ' postaladdress ' , ' postofficebox ' ,
' preferreddeliverymethod ' , ' registeredaddress ' ,
' teletexterminalidentifier ' , ' telexnumber ' , ' x121address ' ,
' carlicense ' , ' departmentnumber ' , ' employeenumber ' ,
' employeetype ' , ' preferredlanguage ' , ' mail ' , ' mobile ' , ' pager ' ,
' audio ' , ' businesscategory ' , ' homephone ' , ' homepostaladdress ' ,
' jpegphoto ' , ' labeleduri ' , ' o ' , ' photo ' , ' roomnumber ' ,
' secretary ' , ' usercertificate ' ,
' usersmimecertificate ' , ' x500uniqueidentifier ' ,
' inetuserhttpurl ' , ' inetuserstatus ' ,
2016-12-20 09:21:58 -06:00
' ipacertmapdata ' ,
2014-03-26 11:11:23 -05:00
} ,
2014-04-29 14:32:29 -05:00
' fixup_function ' : fix_addressbook_permission_bindrule ,
2014-03-26 11:11:23 -05:00
} ,
' System: Read User IPA Attributes ' : {
' replaces_global_anonymous_aci ' : True ,
' ipapermbindruletype ' : ' all ' ,
' ipapermright ' : { ' read ' , ' search ' , ' compare ' } ,
' ipapermdefaultattr ' : {
' ipauniqueid ' , ' ipasshpubkey ' , ' ipauserauthtype ' , ' userclass ' ,
} ,
2014-04-29 14:32:29 -05:00
' fixup_function ' : fix_addressbook_permission_bindrule ,
2014-03-26 11:11:23 -05:00
} ,
' System: Read User Kerberos Attributes ' : {
' replaces_global_anonymous_aci ' : True ,
' ipapermbindruletype ' : ' all ' ,
' ipapermright ' : { ' read ' , ' search ' , ' compare ' } ,
' ipapermdefaultattr ' : {
' krbprincipalname ' , ' krbcanonicalname ' , ' krbprincipalaliases ' ,
' krbprincipalexpiration ' , ' krbpasswordexpiration ' ,
' krblastpwdchange ' , ' nsaccountlock ' , ' krbprincipaltype ' ,
} ,
} ,
' System: Read User Kerberos Login Attributes ' : {
' replaces_global_anonymous_aci ' : True ,
' ipapermright ' : { ' read ' , ' search ' , ' compare ' } ,
' ipapermdefaultattr ' : {
' krblastsuccessfulauth ' , ' krblastfailedauth ' ,
' krblastpwdchange ' , ' krblastadminunlock ' ,
' krbloginfailedcount ' , ' krbpwdpolicyreference ' ,
' krbticketpolicyreference ' , ' krbupenabled ' ,
} ,
' default_privileges ' : { ' User Administrators ' } ,
} ,
' System: Read User Membership ' : {
' replaces_global_anonymous_aci ' : True ,
' ipapermbindruletype ' : ' all ' ,
' ipapermright ' : { ' read ' , ' search ' , ' compare ' } ,
' ipapermdefaultattr ' : {
' memberof ' ,
} ,
} ,
2014-05-28 05:42:02 -05:00
' System: Read UPG Definition ' : {
# Required for adding users
' replaces_global_anonymous_aci ' : True ,
' non_object ' : True ,
' ipapermlocation ' : UPG_DEFINITION_DN ,
' ipapermtarget ' : UPG_DEFINITION_DN ,
' ipapermright ' : { ' read ' , ' search ' , ' compare ' } ,
' ipapermdefaultattr ' : { ' * ' } ,
' default_privileges ' : { ' User Administrators ' } ,
} ,
2014-06-04 08:21:26 -05:00
' System: Add Users ' : {
' ipapermright ' : { ' add ' } ,
' replaces ' : [
' (target = " ldap:///uid=*,cn=users,cn=accounts,$SUFFIX " )(version 3.0;acl " permission:Add Users " ;allow (add) groupdn = " ldap:///cn=Add Users,cn=permissions,cn=pbac,$SUFFIX " ;) ' ,
] ,
' default_privileges ' : { ' User Administrators ' } ,
} ,
' System: Add User to default group ' : {
' non_object ' : True ,
' ipapermright ' : { ' write ' } ,
' ipapermlocation ' : DN ( api . env . container_group , api . env . basedn ) ,
' ipapermtarget ' : DN ( ' cn=ipausers ' , api . env . container_group ,
api . env . basedn ) ,
' ipapermdefaultattr ' : { ' member ' } ,
' replaces ' : [
' (targetattr = " member " )(target = " ldap:///cn=ipausers,cn=groups,cn=accounts,$SUFFIX " )(version 3.0;acl " permission:Add user to default group " ;allow (write) groupdn = " ldap:///cn=Add user to default group,cn=permissions,cn=pbac,$SUFFIX " ;) ' ,
] ,
' default_privileges ' : { ' User Administrators ' } ,
} ,
' System: Change User password ' : {
' ipapermright ' : { ' write ' } ,
' ipapermtargetfilter ' : [
2020-06-10 11:16:54 -05:00
permission_filter_objectclasses_string ,
NOT_MEMBEROF_ADMINS ,
2014-06-04 08:21:26 -05:00
] ,
' ipapermdefaultattr ' : {
' krbprincipalkey ' , ' passwordhistory ' , ' sambalmpassword ' ,
2017-03-30 19:21:47 -05:00
' sambantpassword ' , ' userpassword ' , ' krbpasswordexpiration '
2014-06-04 08:21:26 -05:00
} ,
' replaces ' : [
' (target = " ldap:///uid=*,cn=users,cn=accounts,$SUFFIX " )(targetattr = " userpassword || krbprincipalkey || sambalmpassword || sambantpassword || passwordhistory " )(version 3.0;acl " permission:Change a user password " ;allow (write) groupdn = " ldap:///cn=Change a user password,cn=permissions,cn=pbac,$SUFFIX " ;) ' ,
' (targetfilter = " (!(memberOf=cn=admins,cn=groups,cn=accounts,$SUFFIX)) " )(target = " ldap:///uid=*,cn=users,cn=accounts,$SUFFIX " )(targetattr = " userpassword || krbprincipalkey || sambalmpassword || sambantpassword || passwordhistory " )(version 3.0;acl " permission:Change a user password " ;allow (write) groupdn = " ldap:///cn=Change a user password,cn=permissions,cn=pbac,$SUFFIX " ;) ' ,
2015-01-13 11:09:17 -06:00
' (targetattr = " userPassword || krbPrincipalKey || sambaLMPassword || sambaNTPassword || passwordHistory " )(version 3.0; acl " Windows PassSync service can write passwords " ; allow (write) userdn= " ldap:///uid=passsync,cn=sysaccounts,cn=etc,$SUFFIX " ;) ' ,
2014-06-04 08:21:26 -05:00
] ,
' default_privileges ' : {
' User Administrators ' ,
' Modify Users and Reset passwords ' ,
2015-01-13 11:09:17 -06:00
' PassSync Service ' ,
2014-06-04 08:21:26 -05:00
} ,
} ,
2020-06-10 11:16:54 -05:00
' System: Change Admin User password ' : {
' ipapermright ' : { ' write ' } ,
' ipapermtargetfilter ' : [
MEMBEROF_ADMINS ,
] ,
' ipapermdefaultattr ' : {
' krbprincipalkey ' , ' passwordhistory ' , ' sambalmpassword ' ,
' sambantpassword ' , ' userpassword ' , ' krbpasswordexpiration '
} ,
' default_privileges ' : {
' PassSync Service ' ,
} ,
} ,
2014-06-04 08:21:26 -05:00
' System: Manage User SSH Public Keys ' : {
' ipapermright ' : { ' write ' } ,
2020-06-10 11:16:54 -05:00
' ipapermtargetfilter ' : [
permission_filter_objectclasses_string ,
NOT_MEMBEROF_ADMINS ,
] ,
2014-06-04 08:21:26 -05:00
' ipapermdefaultattr ' : { ' ipasshpubkey ' } ,
' replaces ' : [
' (targetattr = " ipasshpubkey " )(target = " ldap:///uid=*,cn=users,cn=accounts,$SUFFIX " )(version 3.0;acl " permission:Manage User SSH Public Keys " ;allow (write) groupdn = " ldap:///cn=Manage User SSH Public Keys,cn=permissions,cn=pbac,$SUFFIX " ;) ' ,
] ,
' default_privileges ' : { ' User Administrators ' } ,
} ,
2015-08-12 07:48:09 -05:00
' System: Manage User Certificates ' : {
' ipapermright ' : { ' write ' } ,
2020-06-10 11:16:54 -05:00
' ipapermtargetfilter ' : [
permission_filter_objectclasses_string ,
NOT_MEMBEROF_ADMINS ,
] ,
2015-08-12 07:48:09 -05:00
' ipapermdefaultattr ' : { ' usercertificate ' } ,
' default_privileges ' : {
' User Administrators ' ,
' Modify Users and Reset passwords ' ,
} ,
} ,
2016-06-23 13:06:42 -05:00
' System: Manage User Principals ' : {
' ipapermright ' : { ' write ' } ,
2020-06-10 11:16:54 -05:00
' ipapermtargetfilter ' : [
permission_filter_objectclasses_string ,
NOT_MEMBEROF_ADMINS ,
] ,
2016-06-23 13:06:42 -05:00
' ipapermdefaultattr ' : { ' krbprincipalname ' , ' krbcanonicalname ' } ,
' default_privileges ' : {
' User Administrators ' ,
' Modify Users and Reset passwords ' ,
} ,
} ,
2014-06-04 08:21:26 -05:00
' System: Modify Users ' : {
' ipapermright ' : { ' write ' } ,
2020-06-10 11:16:54 -05:00
' ipapermtargetfilter ' : [
permission_filter_objectclasses_string ,
NOT_MEMBEROF_ADMINS ,
] ,
2014-06-04 08:21:26 -05:00
' ipapermdefaultattr ' : {
2016-05-27 06:27:03 -05:00
' businesscategory ' , ' carlicense ' , ' cn ' , ' departmentnumber ' ,
' description ' , ' displayname ' , ' employeetype ' ,
' employeenumber ' , ' facsimiletelephonenumber ' ,
2018-05-09 05:26:12 -05:00
' gecos ' , ' givenname ' , ' homedirectory ' , ' homephone ' ,
' inetuserhttpurl ' , ' initials ' , ' l ' , ' labeleduri ' , ' loginshell ' ,
' manager ' , ' mail ' , ' mepmanagedentry ' , ' mobile ' , ' objectclass ' ,
' ou ' , ' pager ' , ' postalcode ' , ' roomnumber ' , ' secretary ' ,
' seealso ' , ' sn ' , ' st ' , ' street ' , ' telephonenumber ' , ' title ' ,
' userclass ' , ' preferredlanguage '
2014-06-04 08:21:26 -05:00
} ,
' replaces ' : [
' (targetattr = " givenname || sn || cn || displayname || title || initials || loginshell || gecos || homephone || mobile || pager || facsimiletelephonenumber || telephonenumber || street || roomnumber || l || st || postalcode || manager || secretary || description || carlicense || labeleduri || inetuserhttpurl || seealso || employeetype || businesscategory || ou || mepmanagedentry || objectclass " )(target = " ldap:///uid=*,cn=users,cn=accounts,$SUFFIX " )(version 3.0;acl " permission:Modify Users " ;allow (write) groupdn = " ldap:///cn=Modify Users,cn=permissions,cn=pbac,$SUFFIX " ;) ' ,
] ,
' default_privileges ' : {
' User Administrators ' ,
' Modify Users and Reset passwords ' ,
} ,
} ,
' System: Remove Users ' : {
' ipapermright ' : { ' delete ' } ,
2020-06-10 11:16:54 -05:00
' ipapermtargetfilter ' : [
permission_filter_objectclasses_string ,
NOT_MEMBEROF_ADMINS ,
] ,
2014-06-04 08:21:26 -05:00
' replaces ' : [
' (target = " ldap:///uid=*,cn=users,cn=accounts,$SUFFIX " )(version 3.0;acl " permission:Remove Users " ;allow (delete) groupdn = " ldap:///cn=Remove Users,cn=permissions,cn=pbac,$SUFFIX " ;) ' ,
] ,
' default_privileges ' : { ' User Administrators ' } ,
} ,
' System: Unlock User ' : {
' ipapermright ' : { ' write ' } ,
2020-06-10 11:16:54 -05:00
' ipapermtargetfilter ' : [
permission_filter_objectclasses_string ,
NOT_MEMBEROF_ADMINS ,
] ,
2014-06-04 08:21:26 -05:00
' ipapermdefaultattr ' : {
2014-06-04 08:35:31 -05:00
' krblastadminunlock ' , ' krbloginfailedcount ' , ' nsaccountlock ' ,
2014-06-04 08:21:26 -05:00
} ,
' replaces ' : [
' (targetattr = " krbLastAdminUnlock || krbLoginFailedCount " )(target = " ldap:///uid=*,cn=users,cn=accounts,$SUFFIX " )(version 3.0;acl " permission:Unlock user accounts " ;allow (write) groupdn = " ldap:///cn=Unlock user accounts,cn=permissions,cn=pbac,$SUFFIX " ;) ' ,
] ,
' default_privileges ' : { ' User Administrators ' } ,
} ,
2014-09-03 03:54:50 -05:00
' System: Read User Compat Tree ' : {
' non_object ' : True ,
' ipapermbindruletype ' : ' anonymous ' ,
' ipapermlocation ' : api . env . basedn ,
' ipapermtarget ' : DN ( ' cn=users ' , ' cn=compat ' , api . env . basedn ) ,
' ipapermright ' : { ' read ' , ' search ' , ' compare ' } ,
' ipapermdefaultattr ' : {
' objectclass ' , ' uid ' , ' cn ' , ' gecos ' , ' gidnumber ' , ' uidnumber ' ,
' homedirectory ' , ' loginshell ' ,
} ,
} ,
2014-09-30 06:54:50 -05:00
' System: Read User Views Compat Tree ' : {
' non_object ' : True ,
' ipapermbindruletype ' : ' anonymous ' ,
' ipapermlocation ' : api . env . basedn ,
' ipapermtarget ' : DN ( ' cn=users ' , ' cn=* ' , ' cn=views ' , ' cn=compat ' , api . env . basedn ) ,
' ipapermright ' : { ' read ' , ' search ' , ' compare ' } ,
' ipapermdefaultattr ' : {
' objectclass ' , ' uid ' , ' cn ' , ' gecos ' , ' gidnumber ' , ' uidnumber ' ,
' homedirectory ' , ' loginshell ' ,
} ,
} ,
2015-01-13 11:09:17 -06:00
' System: Read User NT Attributes ' : {
' ipapermbindruletype ' : ' permission ' ,
' ipapermright ' : { ' read ' , ' search ' , ' compare ' } ,
' ipapermdefaultattr ' : {
' ntuserdomainid ' , ' ntuniqueid ' , ' ntuseracctexpires ' ,
' ntusercodepage ' , ' ntuserdeleteaccount ' , ' ntuserlastlogoff ' ,
' ntuserlastlogon ' ,
} ,
' default_privileges ' : { ' PassSync Service ' } ,
} ,
2016-12-20 09:21:58 -06:00
' System: Manage User Certificate Mappings ' : {
' ipapermright ' : { ' write ' } ,
' ipapermdefaultattr ' : { ' ipacertmapdata ' , ' objectclass ' } ,
' default_privileges ' : {
' Certificate Identity Mapping Administrators '
} ,
} ,
2014-03-26 11:11:23 -05:00
}
2009-04-21 08:22:56 -05:00
2015-03-05 07:25:33 -06:00
takes_params = baseuser . takes_params + (
2010-11-03 14:31:46 -05:00
Bool ( ' nsaccountlock? ' ,
2017-02-11 18:13:08 -06:00
cli_name = ( ' disabled ' ) ,
2017-04-25 08:51:17 -05:00
default = False ,
2011-01-18 02:49:01 -06:00
label = _ ( ' Account disabled ' ) ,
2010-11-03 14:31:46 -05:00
) ,
2015-05-20 03:12:07 -05:00
Bool ( ' preserved? ' ,
label = _ ( ' Preserved user ' ) ,
default = False ,
flags = [ ' virtual_attribute ' , ' no_create ' , ' no_update ' ] ,
) ,
2009-04-21 08:22:56 -05:00
)
2016-08-19 03:39:40 -05:00
def get_delete_dn ( self , * keys , * * options ) :
active_dn = self . get_dn ( * keys , * * options )
return DN ( active_dn [ 0 ] , self . delete_container_dn , api . env . basedn )
2015-08-12 04:03:40 -05:00
def get_either_dn ( self , * keys , * * options ) :
2015-04-16 04:56:11 -05:00
'''
Returns the DN of a user
The user can be active ( active container ) or delete ( delete container )
If the user does not exist , returns the Active user DN
'''
ldap = self . backend
# Check that this value is a Active user
try :
2015-08-12 04:03:40 -05:00
active_dn = self . get_dn ( * keys , * * options )
2015-04-16 04:56:11 -05:00
ldap . get_entry ( active_dn , [ ' dn ' ] )
# The Active user exists
dn = active_dn
except errors . NotFound :
# Check that this value is a Delete user
2016-08-19 03:39:40 -05:00
delete_dn = self . get_delete_dn ( * keys , * * options )
2015-04-16 04:56:11 -05:00
try :
ldap . get_entry ( delete_dn , [ ' dn ' ] )
# The Delete user exists
dn = delete_dn
except errors . NotFound :
# The user is neither Active/Delete -> returns that Active DN
dn = active_dn
return dn
2011-02-04 08:12:34 -06:00
2011-04-22 08:43:31 -05:00
def _normalize_manager ( self , manager ) :
"""
Given a userid verify the user ' s existence and return the dn.
"""
2015-03-05 07:25:33 -06:00
return super ( user , self ) . normalize_manager ( manager , self . active_container_dn )
2011-04-22 08:43:31 -05:00
2015-05-20 03:12:07 -05:00
def get_preserved_attribute ( self , entry , options ) :
if options . get ( ' raw ' , False ) :
return
delete_container_dn = DN ( self . delete_container_dn , api . env . basedn )
if entry . dn . endswith ( delete_container_dn ) :
entry [ ' preserved ' ] = True
elif options . get ( ' all ' , False ) :
entry [ ' preserved ' ] = False
2009-04-21 08:22:56 -05:00
2014-06-10 10:27:51 -05:00
@register ( )
2015-03-05 07:25:33 -06:00
class user_add ( baseuser_add ) :
2011-08-24 21:48:30 -05:00
__doc__ = _ ( ' Add a new user. ' )
2009-12-09 10:09:53 -06:00
msg_summary = _ ( ' Added user " %(value)s " ' )
2015-03-05 07:25:33 -06:00
has_output_params = baseuser_add . has_output_params + user_output_params
2011-11-10 06:46:16 -06:00
2011-05-16 05:56:04 -05:00
takes_options = LDAPCreate . takes_options + (
Flag ( ' noprivate ' ,
cli_name = ' noprivate ' ,
doc = _ ( ' Don \' t create user private group ' ) ,
) ,
)
2017-02-11 18:13:08 -06:00
def get_options ( self ) :
for option in super ( user_add , self ) . get_options ( ) :
if option . name == " nsaccountlock " :
flags = set ( option . flags )
flags . add ( " no_option " )
option = option . clone ( flags = flags )
yield option
2009-12-10 09:39:24 -06:00
def pre_callback ( self , ldap , dn , entry_attrs , attrs_list , * keys , * * options ) :
2016-08-19 03:39:40 -05:00
delete_dn = self . obj . get_delete_dn ( * keys , * * options )
try :
ldap . get_entry ( delete_dn , [ ' ' ] )
except errors . NotFound :
pass
else :
raise self . obj . handle_duplicate_entry ( * keys )
2019-09-26 05:12:39 -05:00
if not options . get ( ' noprivate ' , False ) and ldap . has_upg ( ) :
2010-12-13 08:53:29 -06:00
try :
2011-05-16 05:56:04 -05:00
# The Managed Entries plugin will allow a user to be created
# even if a group has a duplicate name. This would leave a user
# without a private group. Check for both the group and the user.
2013-06-25 07:58:37 -05:00
self . api . Object [ ' group ' ] . get_dn_if_exists ( keys [ - 1 ] )
2011-05-16 05:56:04 -05:00
try :
self . api . Command [ ' user_show ' ] ( keys [ - 1 ] )
self . obj . handle_duplicate_entry ( * keys )
except errors . NotFound :
raise errors . ManagedGroupExistsError ( group = keys [ - 1 ] )
2010-12-13 08:53:29 -06:00
except errors . NotFound :
2011-05-16 05:56:04 -05:00
pass
else :
# we don't want an user private group to be created for this user
# add NO_UPG_MAGIC description attribute to let the DS plugin know
entry_attrs . setdefault ( ' description ' , [ ] )
entry_attrs [ ' description ' ] . append ( NO_UPG_MAGIC )
2013-01-08 03:10:35 -06:00
entry_attrs . setdefault ( ' uidnumber ' , baseldap . DNA_MAGIC )
if not client_has_capability (
options [ ' version ' ] , ' optional_uid_params ' ) :
# https://fedorahosted.org/freeipa/ticket/2886
# Old clients say 999 (OLD_DNA_MAGIC) when they really mean
# "assign a value dynamically".
OLD_DNA_MAGIC = 999
if entry_attrs . get ( ' uidnumber ' ) == OLD_DNA_MAGIC :
entry_attrs [ ' uidnumber ' ] = baseldap . DNA_MAGIC
if entry_attrs . get ( ' gidnumber ' ) == OLD_DNA_MAGIC :
entry_attrs [ ' gidnumber ' ] = baseldap . DNA_MAGIC
2011-02-17 10:58:53 -06:00
validate_nsaccountlock ( entry_attrs )
2013-10-31 11:54:21 -05:00
config = ldap . get_ipa_config ( )
2010-10-11 10:16:52 -05:00
if ' ipamaxusernamelength ' in config :
if len ( keys [ - 1 ] ) > int ( config . get ( ' ipamaxusernamelength ' ) [ 0 ] ) :
2010-12-21 11:14:38 -06:00
raise errors . ValidationError (
2011-02-23 15:47:49 -06:00
name = self . obj . primary_key . cli_name ,
error = _ ( ' can be at most %(len)d characters ' ) % dict (
len = int ( config . get ( ' ipamaxusernamelength ' ) [ 0 ] )
2010-12-21 11:14:38 -06:00
)
)
2019-06-15 09:25:51 -05:00
default_shell = config . get ( ' ipadefaultloginshell ' ,
[ platformconstants . DEFAULT_SHELL ] ) [ 0 ]
2012-02-07 06:13:52 -06:00
entry_attrs . setdefault ( ' loginshell ' , default_shell )
2009-04-21 08:22:56 -05:00
# hack so we can request separate first and last name in CLI
2009-08-27 08:50:49 -05:00
full_name = ' %s %s ' % ( entry_attrs [ ' givenname ' ] , entry_attrs [ ' sn ' ] )
entry_attrs . setdefault ( ' cn ' , full_name )
2009-04-21 08:22:56 -05:00
if ' homedirectory ' not in entry_attrs :
# get home's root directory from config
2014-05-29 07:47:17 -05:00
homes_root = config . get ( ' ipahomesrootdir ' , [ paths . HOME_DIR ] ) [ 0 ]
2009-04-21 08:22:56 -05:00
# build user's home directory based on his uid
2012-02-07 06:13:52 -06:00
entry_attrs [ ' homedirectory ' ] = posixpath . join ( homes_root , keys [ - 1 ] )
2011-06-15 06:41:41 -05:00
entry_attrs . setdefault ( ' krbprincipalname ' , ' %s @ %s ' % ( entry_attrs [ ' uid ' ] , api . env . realm ) )
2009-04-21 08:22:56 -05:00
2013-01-08 03:10:35 -06:00
if entry_attrs . get ( ' gidnumber ' ) is None :
2011-03-27 15:38:16 -05:00
# gidNumber wasn't specified explicity, find out what it should be
2011-05-16 05:56:04 -05:00
if not options . get ( ' noprivate ' , False ) and ldap . has_upg ( ) :
2011-03-27 15:38:16 -05:00
# User Private Groups - uidNumber == gidNumber
entry_attrs [ ' gidnumber ' ] = entry_attrs [ ' uidnumber ' ]
else :
# we're adding new users to a default group, get its gidNumber
# get default group name from config
def_primary_group = config . get ( ' ipadefaultprimarygroup ' )
group_dn = self . api . Object [ ' group ' ] . get_dn ( def_primary_group )
try :
2013-10-31 11:54:21 -05:00
group_attrs = ldap . get_entry ( group_dn , [ ' gidnumber ' ] )
2011-03-27 15:38:16 -05:00
except errors . NotFound :
2012-03-29 08:12:36 -05:00
error_msg = _ ( ' Default group for new users not found ' )
raise errors . NotFound ( reason = error_msg )
if ' gidnumber ' not in group_attrs :
error_msg = _ ( ' Default group for new users is not POSIX ' )
2011-03-27 15:38:16 -05:00
raise errors . NotFound ( reason = error_msg )
entry_attrs [ ' gidnumber ' ] = group_attrs [ ' gidnumber ' ]
2011-02-16 07:47:42 -06:00
2011-12-12 05:59:06 -06:00
if ' userpassword ' not in entry_attrs and options . get ( ' random ' ) :
2016-07-22 09:41:29 -05:00
entry_attrs [ ' userpassword ' ] = ipa_generate_password (
2016-12-21 08:07:34 -06:00
entropy_bits = TMP_PWD_ENTROPY_BITS )
2011-12-12 05:59:06 -06:00
# save the password so it can be displayed in post_callback
setattr ( context , ' randompassword ' , entry_attrs [ ' userpassword ' ] )
2011-02-04 08:12:34 -06:00
if ' mail ' in entry_attrs :
2015-03-05 07:25:33 -06:00
entry_attrs [ ' mail ' ] = self . obj . normalize_and_validate_email ( entry_attrs [ ' mail ' ] , config )
2012-07-23 13:00:51 -05:00
else :
# No e-mail passed in. If we have a default e-mail domain set
# then we'll add it automatically.
defaultdomain = config . get ( ' ipadefaultemaildomain ' , [ None ] ) [ 0 ]
if defaultdomain :
2015-03-05 07:25:33 -06:00
entry_attrs [ ' mail ' ] = self . obj . normalize_and_validate_email ( keys [ - 1 ] , config )
2009-04-21 08:22:56 -05:00
2011-04-22 08:43:31 -05:00
if ' manager ' in entry_attrs :
2015-03-05 07:25:33 -06:00
entry_attrs [ ' manager ' ] = self . obj . normalize_manager ( entry_attrs [ ' manager ' ] , self . obj . active_container_dn )
2011-04-22 08:43:31 -05:00
2014-08-19 15:32:33 -05:00
if ' userclass ' in entry_attrs and \
' ipauser ' not in entry_attrs [ ' objectclass ' ] :
2013-11-12 04:03:28 -06:00
entry_attrs [ ' objectclass ' ] . append ( ' ipauser ' )
2014-08-19 15:32:33 -05:00
if ' ipauserauthtype ' in entry_attrs and \
' ipauserauthtypeclass ' not in entry_attrs [ ' objectclass ' ] :
entry_attrs [ ' objectclass ' ] . append ( ' ipauserauthtypeclass ' )
2013-11-11 15:58:02 -06:00
2014-08-19 15:32:33 -05:00
rcl = entry_attrs . get ( ' ipatokenradiusconfiglink ' , None )
if rcl :
if ' ipatokenradiusproxyuser ' not in entry_attrs [ ' objectclass ' ] :
entry_attrs [ ' objectclass ' ] . append ( ' ipatokenradiusproxyuser ' )
2013-11-11 15:58:02 -06:00
2014-08-19 15:32:33 -05:00
answer = self . api . Object [ ' radiusproxy ' ] . get_dn_if_exists ( rcl )
entry_attrs [ ' ipatokenradiusconfiglink ' ] = answer
2013-11-11 15:58:02 -06:00
2016-03-14 11:42:56 -05:00
self . pre_common_callback ( ldap , dn , entry_attrs , attrs_list , * keys ,
* * options )
2015-08-03 06:36:29 -05:00
2009-08-27 08:50:49 -05:00
return dn
2009-04-21 08:22:56 -05:00
2009-08-27 08:50:49 -05:00
def post_callback ( self , ldap , dn , entry_attrs , * keys , * * options ) :
Use DN objects instead of strings
* Convert every string specifying a DN into a DN object
* Every place a dn was manipulated in some fashion it was replaced by
the use of DN operators
* Add new DNParam parameter type for parameters which are DN's
* DN objects are used 100% of the time throughout the entire data
pipeline whenever something is logically a dn.
* Many classes now enforce DN usage for their attributes which are
dn's. This is implmented via ipautil.dn_attribute_property(). The
only permitted types for a class attribute specified to be a DN are
either None or a DN object.
* Require that every place a dn is used it must be a DN object.
This translates into lot of::
assert isinstance(dn, DN)
sprinkled through out the code. Maintaining these asserts is
valuable to preserve DN type enforcement. The asserts can be
disabled in production.
The goal of 100% DN usage 100% of the time has been realized, these
asserts are meant to preserve that.
The asserts also proved valuable in detecting functions which did
not obey their function signatures, such as the baseldap pre and
post callbacks.
* Moved ipalib.dn to ipapython.dn because DN class is shared with all
components, not just the server which uses ipalib.
* All API's now accept DN's natively, no need to convert to str (or
unicode).
* Removed ipalib.encoder and encode/decode decorators. Type conversion
is now explicitly performed in each IPASimpleLDAPObject method which
emulates a ldap.SimpleLDAPObject method.
* Entity & Entry classes now utilize DN's
* Removed __getattr__ in Entity & Entity clases. There were two
problems with it. It presented synthetic Python object attributes
based on the current LDAP data it contained. There is no way to
validate synthetic attributes using code checkers, you can't search
the code to find LDAP attribute accesses (because synthetic
attriutes look like Python attributes instead of LDAP data) and
error handling is circumscribed. Secondly __getattr__ was hiding
Python internal methods which broke class semantics.
* Replace use of methods inherited from ldap.SimpleLDAPObject via
IPAdmin class with IPAdmin methods. Directly using inherited methods
was causing us to bypass IPA logic. Mostly this meant replacing the
use of search_s() with getEntry() or getList(). Similarly direct
access of the LDAP data in classes using IPAdmin were replaced with
calls to getValue() or getValues().
* Objects returned by ldap2.find_entries() are now compatible with
either the python-ldap access methodology or the Entity/Entry access
methodology.
* All ldap operations now funnel through the common
IPASimpleLDAPObject giving us a single location where we interface
to python-ldap and perform conversions.
* The above 4 modifications means we've greatly reduced the
proliferation of multiple inconsistent ways to perform LDAP
operations. We are well on the way to having a single API in IPA for
doing LDAP (a long range goal).
* All certificate subject bases are now DN's
* DN objects were enhanced thusly:
- find, rfind, index, rindex, replace and insert methods were added
- AVA, RDN and DN classes were refactored in immutable and mutable
variants, the mutable variants are EditableAVA, EditableRDN and
EditableDN. By default we use the immutable variants preserving
important semantics. To edit a DN cast it to an EditableDN and
cast it back to DN when done editing. These issues are fully
described in other documentation.
- first_key_match was removed
- DN equalty comparison permits comparison to a basestring
* Fixed ldapupdate to work with DN's. This work included:
- Enhance test_updates.py to do more checking after applying
update. Add test for update_from_dict(). Convert code to use
unittest classes.
- Consolidated duplicate code.
- Moved code which should have been in the class into the class.
- Fix the handling of the 'deleteentry' update action. It's no longer
necessary to supply fake attributes to make it work. Detect case
where subsequent update applies a change to entry previously marked
for deletetion. General clean-up and simplification of the
'deleteentry' logic.
- Rewrote a couple of functions to be clearer and more Pythonic.
- Added documentation on the data structure being used.
- Simplfy the use of update_from_dict()
* Removed all usage of get_schema() which was being called prior to
accessing the .schema attribute of an object. If a class is using
internal lazy loading as an optimization it's not right to require
users of the interface to be aware of internal
optimization's. schema is now a property and when the schema
property is accessed it calls a private internal method to perform
the lazy loading.
* Added SchemaCache class to cache the schema's from individual
servers. This was done because of the observation we talk to
different LDAP servers, each of which may have it's own
schema. Previously we globally cached the schema from the first
server we connected to and returned that schema in all contexts. The
cache includes controls to invalidate it thus forcing a schema
refresh.
* Schema caching is now senstive to the run time context. During
install and upgrade the schema can change leading to errors due to
out-of-date cached schema. The schema cache is refreshed in these
contexts.
* We are aware of the LDAP syntax of all LDAP attributes. Every
attribute returned from an LDAP operation is passed through a
central table look-up based on it's LDAP syntax. The table key is
the LDAP syntax it's value is a Python callable that returns a
Python object matching the LDAP syntax. There are a handful of LDAP
attributes whose syntax is historically incorrect
(e.g. DistguishedNames that are defined as DirectoryStrings). The
table driven conversion mechanism is augmented with a table of
hard coded exceptions.
Currently only the following conversions occur via the table:
- dn's are converted to DN objects
- binary objects are converted to Python str objects (IPA
convention).
- everything else is converted to unicode using UTF-8 decoding (IPA
convention).
However, now that the table driven conversion mechanism is in place
it would be trivial to do things such as converting attributes
which have LDAP integer syntax into a Python integer, etc.
* Expected values in the unit tests which are a DN no longer need to
use lambda expressions to promote the returned value to a DN for
equality comparison. The return value is automatically promoted to
a DN. The lambda expressions have been removed making the code much
simpler and easier to read.
* Add class level logging to a number of classes which did not support
logging, less need for use of root_logger.
* Remove ipaserver/conn.py, it was unused.
* Consolidated duplicate code wherever it was found.
* Fixed many places that used string concatenation to form a new
string rather than string formatting operators. This is necessary
because string formatting converts it's arguments to a string prior
to building the result string. You can't concatenate a string and a
non-string.
* Simplify logic in rename_managed plugin. Use DN operators to edit
dn's.
* The live version of ipa-ldap-updater did not generate a log file.
The offline version did, now both do.
https://fedorahosted.org/freeipa/ticket/1670
https://fedorahosted.org/freeipa/ticket/1671
https://fedorahosted.org/freeipa/ticket/1672
https://fedorahosted.org/freeipa/ticket/1673
https://fedorahosted.org/freeipa/ticket/1674
https://fedorahosted.org/freeipa/ticket/1392
https://fedorahosted.org/freeipa/ticket/2872
2012-05-13 06:36:35 -05:00
assert isinstance ( dn , DN )
2013-10-31 11:54:21 -05:00
config = ldap . get_ipa_config ( )
2009-08-27 08:50:49 -05:00
# add the user we just created into the default primary group
def_primary_group = config . get ( ' ipadefaultprimarygroup ' )
group_dn = self . api . Object [ ' group ' ] . get_dn ( def_primary_group )
2012-09-25 05:20:49 -05:00
# if the user is already a member of default primary group,
# do not raise error
# this can happen if automember rule or default group is set
try :
ldap . add_entry_to_group ( dn , group_dn )
except errors . AlreadyGroupMember :
pass
2012-11-15 20:38:26 -06:00
# Fetch the entry again to update memberof, mep data, etc updated
# at the end of the transaction.
2013-10-31 11:54:21 -05:00
newentry = ldap . get_entry ( dn , [ ' * ' ] )
2017-05-24 03:56:15 -05:00
# delete description attribute NO_UPG_MAGIC if present
2019-09-26 05:12:39 -05:00
if ( options . get ( ' noprivate ' , False ) or not ldap . has_upg ( ) ) and \
' description ' in newentry and \
2017-05-24 03:56:15 -05:00
NO_UPG_MAGIC in newentry [ ' description ' ] :
newentry [ ' description ' ] . remove ( NO_UPG_MAGIC )
ldap . update_entry ( newentry )
2012-11-15 20:38:26 -06:00
entry_attrs . update ( newentry )
2011-07-01 14:32:31 -05:00
2011-12-12 05:59:06 -06:00
if options . get ( ' random ' , False ) :
try :
entry_attrs [ ' randompassword ' ] = unicode ( getattr ( context , ' randompassword ' ) )
except AttributeError :
# if both randompassword and userpassword options were used
pass
2015-05-20 03:12:07 -05:00
self . obj . get_preserved_attribute ( entry_attrs , options )
2015-08-03 06:36:29 -05:00
2016-03-14 11:42:56 -05:00
self . post_common_callback ( ldap , dn , entry_attrs , * keys , * * options )
2015-08-03 06:36:29 -05:00
2009-08-27 08:50:49 -05:00
return dn
2009-04-21 08:22:56 -05:00
2014-06-10 10:27:51 -05:00
@register ( )
2015-03-05 07:25:33 -06:00
class user_del ( baseuser_del ) :
2011-08-24 21:48:30 -05:00
__doc__ = _ ( ' Delete a user. ' )
2009-12-09 10:09:53 -06:00
msg_summary = _ ( ' Deleted user " %(value)s " ' )
2015-04-16 04:56:11 -05:00
takes_options = baseuser_del . takes_options + (
2015-06-18 02:20:27 -05:00
Bool ( ' preserve? ' ,
exclude = ' cli ' ,
) ,
2015-04-16 04:56:11 -05:00
)
2015-08-19 07:43:14 -05:00
def _preserve_user ( self , pkey , delete_container , * * options ) :
assert isinstance ( delete_container , DN )
dn = self . obj . get_either_dn ( pkey , * * options )
delete_dn = DN ( dn [ 0 ] , delete_container )
ldap = self . obj . backend
2017-05-23 11:35:57 -05:00
logger . debug ( " preserve move %s -> %s " , dn , delete_dn )
2015-08-19 07:43:14 -05:00
if dn . endswith ( delete_container ) :
raise errors . ExecutionError (
_ ( ' %s : user is already preserved ' % pkey )
)
# Check that this value is a Active user
try :
original_entry_attrs = self . _exc_wrapper (
pkey , options , ldap . get_entry ) ( dn , [ ' dn ' ] )
except errors . NotFound :
2018-01-03 05:11:15 -06:00
raise self . obj . handle_not_found ( pkey )
2015-08-19 07:43:14 -05:00
2015-10-16 12:16:46 -05:00
for callback in self . get_callbacks ( ' pre ' ) :
dn = callback ( self , ldap , dn , pkey , * * options )
assert isinstance ( dn , DN )
2015-08-19 07:43:14 -05:00
# start to move the entry to Delete container
self . _exc_wrapper ( pkey , options , ldap . move_entry ) ( dn , delete_dn ,
del_old = True )
# Then clear the credential attributes
attrs_to_clear = [ ' krbPrincipalKey ' , ' krbLastPwdChange ' ,
' krbPasswordExpiration ' , ' userPassword ' ]
entry_attrs = self . _exc_wrapper ( pkey , options , ldap . get_entry ) (
delete_dn , attrs_to_clear )
clearedCredential = False
for attr in attrs_to_clear :
if attr . lower ( ) in entry_attrs :
del entry_attrs [ attr ]
clearedCredential = True
if clearedCredential :
self . _exc_wrapper ( pkey , options , ldap . update_entry ) ( entry_attrs )
# Then restore some original entry attributes
attrs_to_restore = [ ' secretary ' , ' managedby ' , ' manager ' , ' ipauniqueid ' ,
' uidnumber ' , ' gidnumber ' , ' passwordHistory ' ]
entry_attrs = self . _exc_wrapper (
pkey , options , ldap . get_entry ) ( delete_dn , attrs_to_restore )
restoreAttr = False
for attr in attrs_to_restore :
if ( ( attr . lower ( ) in original_entry_attrs ) and
not ( attr . lower ( ) in entry_attrs ) ) :
restoreAttr = True
entry_attrs [ attr . lower ( ) ] = original_entry_attrs [ attr . lower ( ) ]
if restoreAttr :
self . _exc_wrapper ( pkey , options , ldap . update_entry ) ( entry_attrs )
2012-05-23 04:44:53 -05:00
def pre_callback ( self , ldap , dn , * keys , * * options ) :
2015-08-12 04:03:40 -05:00
dn = self . obj . get_either_dn ( * keys , * * options )
2015-04-16 04:56:11 -05:00
# For User life Cycle: user-del is a common plugin
# command to delete active user (active container) and
# delete user (delete container).
2015-10-16 12:16:46 -05:00
# If the target entry is a Delete entry, skip the orphaning/removal
# of OTP tokens.
check_protected_member ( keys [ - 1 ] )
if not options . get ( ' preserve ' , False ) :
# Remove any ID overrides tied with this user
try :
remove_ipaobject_overrides ( self . obj . backend , self . obj . api , dn )
except errors . NotFound :
2018-01-03 05:11:15 -06:00
raise self . obj . handle_not_found ( * keys )
2015-10-16 12:16:46 -05:00
if dn . endswith ( DN ( self . obj . delete_container_dn , api . env . basedn ) ) :
return dn
# Delete all tokens owned and managed by this user.
# Orphan all tokens owned but not managed by this user.
owner = self . api . Object . user . get_primary_key_from_dn ( dn )
2016-05-19 06:50:38 -05:00
results = self . api . Command . otptoken_find (
ipatokenowner = owner , no_members = False ) [ ' result ' ]
2015-10-16 12:16:46 -05:00
for token in results :
orphan = not [ x for x in token . get ( ' managedby_user ' , [ ] ) if x == owner ]
token = self . api . Object . otptoken . get_primary_key_from_dn ( token [ ' dn ' ] )
if orphan :
self . api . Command . otptoken_mod ( token , ipatokenowner = None )
else :
self . api . Command . otptoken_del ( token )
2015-10-13 05:31:13 -05:00
2012-05-23 04:44:53 -05:00
return dn
2009-04-21 08:22:56 -05:00
2015-04-16 04:56:11 -05:00
def execute ( self , * keys , * * options ) :
2015-05-27 09:30:48 -05:00
# We are going to permanent delete or the user is already in the delete container.
delete_container = DN ( self . obj . delete_container_dn , self . api . env . basedn )
2015-04-16 04:56:11 -05:00
2015-06-18 02:20:27 -05:00
# The user to delete is active and there is no 'no_preserve' option
if options . get ( ' preserve ' , False ) :
2015-08-19 07:43:14 -05:00
failed = [ ]
preserved = [ ]
for pkey in keys [ - 1 ] :
try :
self . _preserve_user ( pkey , delete_container , * * options )
preserved . append ( pkey_to_value ( pkey , options ) )
2016-03-11 12:51:07 -06:00
except Exception :
2015-08-19 07:43:14 -05:00
if not options . get ( ' continue ' , False ) :
raise
failed . append ( pkey_to_value ( pkey , options ) )
val = dict ( result = dict ( failed = failed ) , value = preserved )
2015-04-16 04:56:11 -05:00
return val
else :
return super ( user_del , self ) . execute ( * keys , * * options )
2009-04-21 08:22:56 -05:00
2014-06-10 10:27:51 -05:00
@register ( )
2015-03-05 07:25:33 -06:00
class user_mod ( baseuser_mod ) :
2011-08-24 21:48:30 -05:00
__doc__ = _ ( ' Modify a user. ' )
2009-04-21 08:22:56 -05:00
2009-12-09 10:09:53 -06:00
msg_summary = _ ( ' Modified user " %(value)s " ' )
2015-03-05 07:25:33 -06:00
has_output_params = baseuser_mod . has_output_params + user_output_params
2011-11-10 06:46:16 -06:00
2017-02-11 18:13:08 -06:00
def get_options ( self ) :
for option in super ( user_mod , self ) . get_options ( ) :
if option . name == " nsaccountlock " :
flags = set ( option . flags )
flags . add ( " no_option " )
option = option . clone ( flags = flags )
yield option
2011-02-04 08:12:34 -06:00
def pre_callback ( self , ldap , dn , entry_attrs , attrs_list , * keys , * * options ) :
2015-08-12 04:03:40 -05:00
dn = self . obj . get_either_dn ( * keys , * * options )
2016-03-14 11:42:56 -05:00
self . pre_common_callback ( ldap , dn , entry_attrs , attrs_list , * keys ,
* * options )
2011-02-17 10:58:53 -06:00
validate_nsaccountlock ( entry_attrs )
2011-02-04 08:12:34 -06:00
return dn
2010-11-03 14:31:46 -05:00
def post_callback ( self , ldap , dn , entry_attrs , * keys , * * options ) :
2016-03-14 11:42:56 -05:00
self . post_common_callback ( ldap , dn , entry_attrs , * keys , * * options )
2015-05-20 03:12:07 -05:00
self . obj . get_preserved_attribute ( entry_attrs , options )
2010-11-03 14:31:46 -05:00
return dn
2009-04-21 08:22:56 -05:00
2014-06-10 10:27:51 -05:00
@register ( )
2015-03-05 07:25:33 -06:00
class user_find ( baseuser_find ) :
2011-08-24 21:48:30 -05:00
__doc__ = _ ( ' Search for users. ' )
2011-01-04 14:15:54 -06:00
member_attributes = [ ' memberof ' ]
2015-03-05 07:25:33 -06:00
has_output_params = baseuser_find . has_output_params + user_output_params
2009-04-21 08:22:56 -05:00
2015-05-20 03:12:07 -05:00
msg_summary = ngettext (
' %(count)d user matched ' , ' %(count)d users matched ' , 0
)
2010-09-30 16:46:58 -05:00
takes_options = LDAPSearch . takes_options + (
2010-09-14 15:50:00 -05:00
Flag ( ' whoami ' ,
label = _ ( ' Self ' ) ,
doc = _ ( ' Display user record for current Kerberos principal ' ) ,
) ,
)
2011-01-04 14:15:54 -06:00
2010-11-23 08:02:54 -06:00
def pre_callback ( self , ldap , filter , attrs_list , base_dn , scope , * keys , * * options ) :
Use DN objects instead of strings
* Convert every string specifying a DN into a DN object
* Every place a dn was manipulated in some fashion it was replaced by
the use of DN operators
* Add new DNParam parameter type for parameters which are DN's
* DN objects are used 100% of the time throughout the entire data
pipeline whenever something is logically a dn.
* Many classes now enforce DN usage for their attributes which are
dn's. This is implmented via ipautil.dn_attribute_property(). The
only permitted types for a class attribute specified to be a DN are
either None or a DN object.
* Require that every place a dn is used it must be a DN object.
This translates into lot of::
assert isinstance(dn, DN)
sprinkled through out the code. Maintaining these asserts is
valuable to preserve DN type enforcement. The asserts can be
disabled in production.
The goal of 100% DN usage 100% of the time has been realized, these
asserts are meant to preserve that.
The asserts also proved valuable in detecting functions which did
not obey their function signatures, such as the baseldap pre and
post callbacks.
* Moved ipalib.dn to ipapython.dn because DN class is shared with all
components, not just the server which uses ipalib.
* All API's now accept DN's natively, no need to convert to str (or
unicode).
* Removed ipalib.encoder and encode/decode decorators. Type conversion
is now explicitly performed in each IPASimpleLDAPObject method which
emulates a ldap.SimpleLDAPObject method.
* Entity & Entry classes now utilize DN's
* Removed __getattr__ in Entity & Entity clases. There were two
problems with it. It presented synthetic Python object attributes
based on the current LDAP data it contained. There is no way to
validate synthetic attributes using code checkers, you can't search
the code to find LDAP attribute accesses (because synthetic
attriutes look like Python attributes instead of LDAP data) and
error handling is circumscribed. Secondly __getattr__ was hiding
Python internal methods which broke class semantics.
* Replace use of methods inherited from ldap.SimpleLDAPObject via
IPAdmin class with IPAdmin methods. Directly using inherited methods
was causing us to bypass IPA logic. Mostly this meant replacing the
use of search_s() with getEntry() or getList(). Similarly direct
access of the LDAP data in classes using IPAdmin were replaced with
calls to getValue() or getValues().
* Objects returned by ldap2.find_entries() are now compatible with
either the python-ldap access methodology or the Entity/Entry access
methodology.
* All ldap operations now funnel through the common
IPASimpleLDAPObject giving us a single location where we interface
to python-ldap and perform conversions.
* The above 4 modifications means we've greatly reduced the
proliferation of multiple inconsistent ways to perform LDAP
operations. We are well on the way to having a single API in IPA for
doing LDAP (a long range goal).
* All certificate subject bases are now DN's
* DN objects were enhanced thusly:
- find, rfind, index, rindex, replace and insert methods were added
- AVA, RDN and DN classes were refactored in immutable and mutable
variants, the mutable variants are EditableAVA, EditableRDN and
EditableDN. By default we use the immutable variants preserving
important semantics. To edit a DN cast it to an EditableDN and
cast it back to DN when done editing. These issues are fully
described in other documentation.
- first_key_match was removed
- DN equalty comparison permits comparison to a basestring
* Fixed ldapupdate to work with DN's. This work included:
- Enhance test_updates.py to do more checking after applying
update. Add test for update_from_dict(). Convert code to use
unittest classes.
- Consolidated duplicate code.
- Moved code which should have been in the class into the class.
- Fix the handling of the 'deleteentry' update action. It's no longer
necessary to supply fake attributes to make it work. Detect case
where subsequent update applies a change to entry previously marked
for deletetion. General clean-up and simplification of the
'deleteentry' logic.
- Rewrote a couple of functions to be clearer and more Pythonic.
- Added documentation on the data structure being used.
- Simplfy the use of update_from_dict()
* Removed all usage of get_schema() which was being called prior to
accessing the .schema attribute of an object. If a class is using
internal lazy loading as an optimization it's not right to require
users of the interface to be aware of internal
optimization's. schema is now a property and when the schema
property is accessed it calls a private internal method to perform
the lazy loading.
* Added SchemaCache class to cache the schema's from individual
servers. This was done because of the observation we talk to
different LDAP servers, each of which may have it's own
schema. Previously we globally cached the schema from the first
server we connected to and returned that schema in all contexts. The
cache includes controls to invalidate it thus forcing a schema
refresh.
* Schema caching is now senstive to the run time context. During
install and upgrade the schema can change leading to errors due to
out-of-date cached schema. The schema cache is refreshed in these
contexts.
* We are aware of the LDAP syntax of all LDAP attributes. Every
attribute returned from an LDAP operation is passed through a
central table look-up based on it's LDAP syntax. The table key is
the LDAP syntax it's value is a Python callable that returns a
Python object matching the LDAP syntax. There are a handful of LDAP
attributes whose syntax is historically incorrect
(e.g. DistguishedNames that are defined as DirectoryStrings). The
table driven conversion mechanism is augmented with a table of
hard coded exceptions.
Currently only the following conversions occur via the table:
- dn's are converted to DN objects
- binary objects are converted to Python str objects (IPA
convention).
- everything else is converted to unicode using UTF-8 decoding (IPA
convention).
However, now that the table driven conversion mechanism is in place
it would be trivial to do things such as converting attributes
which have LDAP integer syntax into a Python integer, etc.
* Expected values in the unit tests which are a DN no longer need to
use lambda expressions to promote the returned value to a DN for
equality comparison. The return value is automatically promoted to
a DN. The lambda expressions have been removed making the code much
simpler and easier to read.
* Add class level logging to a number of classes which did not support
logging, less need for use of root_logger.
* Remove ipaserver/conn.py, it was unused.
* Consolidated duplicate code wherever it was found.
* Fixed many places that used string concatenation to form a new
string rather than string formatting operators. This is necessary
because string formatting converts it's arguments to a string prior
to building the result string. You can't concatenate a string and a
non-string.
* Simplify logic in rename_managed plugin. Use DN operators to edit
dn's.
* The live version of ipa-ldap-updater did not generate a log file.
The offline version did, now both do.
https://fedorahosted.org/freeipa/ticket/1670
https://fedorahosted.org/freeipa/ticket/1671
https://fedorahosted.org/freeipa/ticket/1672
https://fedorahosted.org/freeipa/ticket/1673
https://fedorahosted.org/freeipa/ticket/1674
https://fedorahosted.org/freeipa/ticket/1392
https://fedorahosted.org/freeipa/ticket/2872
2012-05-13 06:36:35 -05:00
assert isinstance ( base_dn , DN )
2016-03-14 11:42:56 -05:00
self . pre_common_callback ( ldap , filter , attrs_list , base_dn , scope ,
* keys , * * options )
2015-05-20 03:12:07 -05:00
2010-09-14 15:50:00 -05:00
if options . get ( ' whoami ' ) :
2010-11-24 15:36:36 -06:00
return ( " (&(objectclass=posixaccount)(krbprincipalname= %s )) " % \
getattr ( context , ' principal ' ) , base_dn , scope )
2015-05-20 03:12:07 -05:00
preserved = options . get ( ' preserved ' , False )
if preserved is None :
base_dn = self . api . env . basedn
scope = ldap . SCOPE_SUBTREE
elif preserved :
base_dn = DN ( self . obj . delete_container_dn , self . api . env . basedn )
else :
base_dn = DN ( self . obj . active_container_dn , self . api . env . basedn )
2010-11-23 08:02:54 -06:00
return ( filter , base_dn , scope )
2010-09-14 15:50:00 -05:00
2010-11-03 14:31:46 -05:00
def post_callback ( self , ldap , entries , truncated , * args , * * options ) :
2011-10-26 04:12:38 -05:00
if options . get ( ' pkey_only ' , False ) :
2012-05-23 10:00:24 -05:00
return truncated
2015-05-20 03:12:07 -05:00
if options . get ( ' preserved ' , False ) is None :
base_dns = (
DN ( self . obj . active_container_dn , self . api . env . basedn ) ,
DN ( self . obj . delete_container_dn , self . api . env . basedn ) ,
)
2018-07-12 09:21:34 -05:00
entries [ : ] = list (
e for e in entries if any ( e . dn . endswith ( bd ) for bd in base_dns )
)
2015-05-20 03:12:07 -05:00
2015-05-12 08:03:58 -05:00
self . post_common_callback ( ldap , entries , lockout = False , * * options )
2015-05-20 03:12:07 -05:00
for entry in entries :
self . obj . get_preserved_attribute ( entry , options )
2010-11-03 14:31:46 -05:00
2015-05-20 03:12:07 -05:00
return truncated
2009-12-09 10:09:53 -06:00
2009-04-21 08:22:56 -05:00
2014-06-10 10:27:51 -05:00
@register ( )
2015-03-05 07:25:33 -06:00
class user_show ( baseuser_show ) :
2011-08-24 21:48:30 -05:00
__doc__ = _ ( ' Display information about a user. ' )
2015-03-05 07:25:33 -06:00
has_output_params = baseuser_show . has_output_params + user_output_params
2015-07-24 08:31:26 -05:00
takes_options = baseuser_show . takes_options + (
Str ( ' out? ' ,
doc = _ ( ' file to store certificate in ' ) ,
) ,
)
2011-11-10 06:46:16 -06:00
2015-08-12 04:03:40 -05:00
def pre_callback ( self , ldap , dn , attrs_list , * keys , * * options ) :
dn = self . obj . get_either_dn ( * keys , * * options )
2016-03-14 11:42:56 -05:00
self . pre_common_callback ( ldap , dn , attrs_list , * keys , * * options )
2015-08-12 04:03:40 -05:00
return dn
2010-11-03 14:31:46 -05:00
def post_callback ( self , ldap , dn , entry_attrs , * keys , * * options ) :
2011-07-07 10:58:18 -05:00
convert_nsaccountlock ( entry_attrs )
2016-03-14 11:42:56 -05:00
self . post_common_callback ( ldap , dn , entry_attrs , * keys , * * options )
2015-05-20 03:12:07 -05:00
self . obj . get_preserved_attribute ( entry_attrs , options )
2010-11-03 14:31:46 -05:00
return dn
2009-04-21 08:22:56 -05:00
2015-07-24 08:31:26 -05:00
2015-05-08 11:58:57 -05:00
@register ( )
class user_undel ( LDAPQuery ) :
__doc__ = _ ( ' Undelete a delete user account. ' )
has_output = output . standard_value
msg_summary = _ ( ' Undeleted user account " %(value)s " ' )
def execute ( self , * keys , * * options ) :
ldap = self . obj . backend
# First check that the user exists and is a delete one
2015-08-12 04:03:40 -05:00
delete_dn = self . obj . get_either_dn ( * keys , * * options )
2015-05-08 11:58:57 -05:00
try :
2016-10-04 13:02:32 -05:00
self . _exc_wrapper ( keys , options , ldap . get_entry ) ( delete_dn )
2015-05-08 11:58:57 -05:00
except errors . NotFound :
2018-01-03 05:11:15 -06:00
raise self . obj . handle_not_found ( * keys )
2015-08-13 01:11:38 -05:00
if delete_dn . endswith ( DN ( self . obj . active_container_dn ,
api . env . basedn ) ) :
raise errors . InvocationError (
message = _ ( ' user " %s " is already active ' ) % keys [ - 1 ] )
2015-05-08 11:58:57 -05:00
active_dn = DN ( delete_dn [ 0 ] , self . obj . active_container_dn , api . env . basedn )
# start to move the entry to the Active container
self . _exc_wrapper ( keys , options , ldap . move_entry ) ( delete_dn , active_dn , del_old = True )
# add the user we just undelete into the default primary group
config = ldap . get_ipa_config ( )
def_primary_group = config . get ( ' ipadefaultprimarygroup ' )
group_dn = self . api . Object [ ' group ' ] . get_dn ( def_primary_group )
# if the user is already a member of default primary group,
# do not raise error
# this can happen if automember rule or default group is set
try :
ldap . add_entry_to_group ( active_dn , group_dn )
except errors . AlreadyGroupMember :
pass
return dict (
result = True ,
value = pkey_to_value ( keys [ 0 ] , options ) ,
)
2009-04-21 08:22:56 -05:00
2015-08-17 13:11:21 -05:00
@register ( )
class user_stage ( LDAPMultiQuery ) :
__doc__ = _ ( ' Move deleted user into staged area ' )
has_output = output . standard_multi_delete
msg_summary = _ ( ' Staged user account " %(value)s " ' )
2019-07-23 02:31:53 -05:00
# when moving from preserved to stage, some attributes may be
# present in the preserved entry but cannot be provided to
# stageuser_add
# For instance: dn and uid are derived from LOGIN argument
# has_keytab, has_password, preserved are virtual attributes
# ipauniqueid, krbcanonicalname, sshpubkeyfp, krbextradata
# are automatically generated
# ipacertmapdata can only be provided with user_add_certmapdata
ignore_attrs = [ u ' dn ' , u ' uid ' ,
u ' has_keytab ' , u ' has_password ' , u ' preserved ' ,
u ' ipauniqueid ' , u ' krbcanonicalname ' ,
u ' sshpubkeyfp ' , u ' krbextradata ' ,
u ' ipacertmapdata ' ,
u ' nsaccountlock ' ]
2015-08-17 13:11:21 -05:00
def execute ( self , * keys , * * options ) :
2019-07-23 02:31:53 -05:00
def _build_setattr_arg ( key , val ) :
if isinstance ( val , bytes ) :
return u " {} = {} " . format ( key , val . decode ( ' UTF-8 ' ) )
else :
return u " {} = {} " . format ( key , val )
2015-08-17 13:11:21 -05:00
staged = [ ]
failed = [ ]
for key in keys [ - 1 ] :
single_keys = keys [ : - 1 ] + ( key , )
multi_keys = keys [ : - 1 ] + ( ( key , ) , )
user = self . api . Command . user_show ( * single_keys , all = True ) [ ' result ' ]
new_options = { }
for param in self . api . Command . stageuser_add . options ( ) :
try :
value = user [ param . name ]
except KeyError :
continue
if param . multivalue and not isinstance ( value , ( list , tuple ) ) :
value = [ value ]
elif not param . multivalue and isinstance ( value , ( list , tuple ) ) :
value = value [ 0 ]
new_options [ param . name ] = value
2019-07-23 02:31:53 -05:00
# Some attributes may not be accessible through the Command
# options and need to be added with --setattr
set_attr = [ ]
for userkey in user . keys ( ) :
if userkey in new_options or userkey in self . ignore_attrs :
continue
value = user [ userkey ]
if isinstance ( value , ( list , tuple ) ) :
for val in value :
set_attr . append ( _build_setattr_arg ( userkey , val ) )
else :
set_attr . append ( _build_setattr_arg ( userkey , val ) )
if set_attr :
new_options [ u ' setattr ' ] = set_attr
2015-08-17 13:11:21 -05:00
try :
self . api . Command . stageuser_add ( * single_keys , * * new_options )
2019-07-23 02:31:53 -05:00
# special handling for certmapdata
certmapdata = user . get ( u ' ipacertmapdata ' )
if certmapdata :
self . api . Command . stageuser_add_certmapdata (
* single_keys ,
ipacertmapdata = certmapdata )
2015-08-17 13:11:21 -05:00
try :
self . api . Command . user_del ( * multi_keys , preserve = False )
except errors . ExecutionError :
self . api . Command . stageuser_del ( * multi_keys )
raise
except errors . ExecutionError :
if not options [ ' continue ' ] :
raise
failed . append ( key )
else :
staged . append ( key )
return dict (
result = dict (
failed = pkey_to_value ( failed , options ) ,
) ,
value = pkey_to_value ( staged , options ) ,
)
2014-06-10 10:27:51 -05:00
@register ( )
2010-10-05 14:37:37 -05:00
class user_disable ( LDAPQuery ) :
2011-08-24 21:48:30 -05:00
__doc__ = _ ( ' Disable a user account. ' )
2009-12-09 10:09:53 -06:00
has_output = output . standard_value
2010-10-05 14:37:37 -05:00
msg_summary = _ ( ' Disabled user account " %(value)s " ' )
2009-12-09 10:09:53 -06:00
2009-08-27 08:50:49 -05:00
def execute ( self , * keys , * * options ) :
ldap = self . obj . backend
2009-04-21 08:22:56 -05:00
2012-08-16 19:28:44 -05:00
check_protected_member ( keys [ - 1 ] )
2009-04-21 08:22:56 -05:00
2015-08-12 04:03:40 -05:00
dn = self . obj . get_either_dn ( * keys , * * options )
2010-11-03 14:31:46 -05:00
ldap . deactivate_entry ( dn )
2009-04-21 08:22:56 -05:00
2009-12-09 10:09:53 -06:00
return dict (
result = True ,
2014-03-27 08:04:00 -05:00
value = pkey_to_value ( keys [ 0 ] , options ) ,
2009-12-09 10:09:53 -06:00
)
2009-04-21 08:22:56 -05:00
2014-06-10 10:27:51 -05:00
@register ( )
2010-10-05 14:37:37 -05:00
class user_enable ( LDAPQuery ) :
2011-08-24 21:48:30 -05:00
__doc__ = _ ( ' Enable a user account. ' )
2009-12-09 10:09:53 -06:00
has_output = output . standard_value
2011-11-10 06:46:16 -06:00
has_output_params = LDAPQuery . has_output_params + user_output_params
2010-10-05 14:37:37 -05:00
msg_summary = _ ( ' Enabled user account " %(value)s " ' )
2009-12-09 10:09:53 -06:00
2009-08-27 08:50:49 -05:00
def execute ( self , * keys , * * options ) :
ldap = self . obj . backend
2009-04-21 08:22:56 -05:00
2015-08-12 04:03:40 -05:00
dn = self . obj . get_either_dn ( * keys , * * options )
2009-04-21 08:22:56 -05:00
2010-11-03 14:31:46 -05:00
ldap . activate_entry ( dn )
2009-04-21 08:22:56 -05:00
2009-12-09 10:09:53 -06:00
return dict (
result = True ,
2014-03-27 08:04:00 -05:00
value = pkey_to_value ( keys [ 0 ] , options ) ,
2009-12-09 10:09:53 -06:00
)
2009-04-21 08:22:56 -05:00
2011-01-21 02:07:53 -06:00
2014-06-10 10:27:51 -05:00
@register ( )
2011-01-21 02:07:53 -06:00
class user_unlock ( LDAPQuery ) :
2011-08-24 21:48:30 -05:00
__doc__ = _ ( """
2011-03-04 10:08:54 -06:00
Unlock a user account
2011-01-21 02:07:53 -06:00
2011-03-04 10:08:54 -06:00
An account may become locked if the password is entered incorrectly too
many times within a specific time period as controlled by password
policy . A locked account is a temporary condition and may be unlocked by
2011-08-24 21:48:30 -05:00
an administrator . """ )
2011-01-21 02:07:53 -06:00
has_output = output . standard_value
msg_summary = _ ( ' Unlocked account " %(value)s " ' )
def execute ( self , * keys , * * options ) :
2015-08-12 04:03:40 -05:00
dn = self . obj . get_either_dn ( * keys , * * options )
2013-10-31 11:54:21 -05:00
entry = self . obj . backend . get_entry (
dn , [ ' krbLastAdminUnlock ' , ' krbLoginFailedCount ' ] )
2011-01-21 02:07:53 -06:00
2013-10-31 11:54:21 -05:00
entry [ ' krbLastAdminUnlock ' ] = [ strftime ( " % Y % m %d % H % M % SZ " , gmtime ( ) ) ]
entry [ ' krbLoginFailedCount ' ] = [ ' 0 ' ]
self . obj . backend . update_entry ( entry )
2011-01-21 02:07:53 -06:00
return dict (
result = True ,
2014-03-27 08:04:00 -05:00
value = pkey_to_value ( keys [ 0 ] , options ) ,
2011-01-21 02:07:53 -06:00
)
2012-02-14 08:41:25 -06:00
2016-06-29 23:37:52 -05:00
@register ( )
class userstatus ( LDAPObject ) :
parent_object = ' user '
takes_params = (
Bool ( ' preserved? ' ,
label = _ ( ' Preserved user ' ) ,
flags = { ' virtual_attribute ' , ' no_create ' , ' no_update ' , ' no_search ' } ,
) ,
Str ( ' server ' ,
label = _ ( ' Server ' ) ,
flags = { ' virtual_attribute ' , ' no_create ' , ' no_update ' , ' no_search ' } ,
) ,
Str ( ' krbloginfailedcount ' ,
label = _ ( ' Failed logins ' ) ,
flags = { ' no_create ' , ' no_update ' , ' no_search ' } ,
) ,
Str ( ' krblastsuccessfulauth ' ,
label = _ ( ' Last successful authentication ' ) ,
flags = { ' no_create ' , ' no_update ' , ' no_search ' } ,
) ,
Str ( ' krblastfailedauth ' ,
label = _ ( ' Last failed authentication ' ) ,
flags = { ' no_create ' , ' no_update ' , ' no_search ' } ,
) ,
Str ( ' now ' ,
label = _ ( ' Time now ' ) ,
flags = { ' virtual_attribute ' , ' no_create ' , ' no_update ' , ' no_search ' } ,
) ,
)
2014-06-10 10:27:51 -05:00
@register ( )
2012-02-14 08:41:25 -06:00
class user_status ( LDAPQuery ) :
__doc__ = _ ( """
Lockout status of a user account
An account may become locked if the password is entered incorrectly too
many times within a specific time period as controlled by password
policy . A locked account is a temporary condition and may be unlocked by
an administrator .
This connects to each IPA master and displays the lockout status on
2012-04-23 03:16:45 -05:00
each one .
To determine whether an account is locked on a given server you need
to compare the number of failed logins and the time of the last failure .
For an account to be locked it must exceed the maxfail failures within
the failinterval duration as specified in the password policy associated
with the user .
The failed login counter is modified only when a user attempts a log in
so it is possible that an account may appear locked but the last failed
login attempt is older than the lockouttime of the password policy . This
means that the user may attempt a login again . """ )
2012-02-14 08:41:25 -06:00
2016-06-29 23:37:52 -05:00
obj_name = ' userstatus '
attr_name = ' find '
2012-02-14 08:41:25 -06:00
has_output = output . standard_list_of_entries
2016-06-29 23:37:52 -05:00
def get_args ( self ) :
for arg in super ( user_status , self ) . get_args ( ) :
if arg . name == ' useruid ' :
arg = arg . clone ( cli_name = ' login ' )
yield arg
2012-02-14 08:41:25 -06:00
def execute ( self , * keys , * * options ) :
ldap = self . obj . backend
2016-06-29 23:37:52 -05:00
dn = self . api . Object . user . get_either_dn ( * keys , * * options )
2012-04-23 03:16:45 -05:00
attr_list = [ ' krbloginfailedcount ' , ' krblastsuccessfulauth ' , ' krblastfailedauth ' , ' nsaccountlock ' ]
2012-02-14 08:41:25 -06:00
2012-04-23 03:16:45 -05:00
disabled = False
2019-03-27 05:30:40 -05:00
masters = get_masters ( ldap )
2012-02-14 08:41:25 -06:00
entries = [ ]
count = 0
2019-03-27 05:30:40 -05:00
for host in masters :
2012-02-14 08:41:25 -06:00
if host == api . env . host :
other_ldap = self . obj . backend
else :
try :
2017-02-23 03:44:04 -06:00
other_ldap = LDAPClient ( ldap_uri = ' ldap:// %s ' % host )
other_ldap . gssapi_bind ( )
2015-07-30 09:49:29 -05:00
except Exception as e :
2017-05-23 11:35:57 -05:00
logger . error ( " user_status: Connecting to %s failed with "
" %s " , host , str ( e ) )
2013-12-05 07:43:19 -06:00
newresult = { ' dn ' : dn }
2012-02-14 08:41:25 -06:00
newresult [ ' server ' ] = _ ( " %(host)s failed: %(error)s " ) % dict ( host = host , error = str ( e ) )
entries . append ( newresult )
count + = 1
continue
try :
entry = other_ldap . get_entry ( dn , attr_list )
2013-12-05 07:43:19 -06:00
newresult = { ' dn ' : dn }
2012-02-14 08:41:25 -06:00
for attr in [ ' krblastsuccessfulauth ' , ' krblastfailedauth ' ] :
2013-10-31 11:54:21 -05:00
newresult [ attr ] = entry . get ( attr , [ u ' N/A ' ] )
newresult [ ' krbloginfailedcount ' ] = entry . get ( ' krbloginfailedcount ' , u ' 0 ' )
2012-02-14 08:41:25 -06:00
if not options . get ( ' raw ' , False ) :
for attr in [ ' krblastsuccessfulauth ' , ' krblastfailedauth ' ] :
try :
if newresult [ attr ] [ 0 ] == u ' N/A ' :
continue
newtime = time . strptime ( newresult [ attr ] [ 0 ] , ' % Y % m %d % H % M % SZ ' )
newresult [ attr ] [ 0 ] = unicode ( time . strftime ( ' % Y- % m- %d T % H: % M: % SZ ' , newtime ) )
2015-07-30 09:49:29 -05:00
except Exception as e :
2017-05-23 11:35:57 -05:00
logger . debug ( " time conversion failed with %s " ,
str ( e ) )
2012-02-14 08:41:25 -06:00
newresult [ ' server ' ] = host
2012-04-23 03:16:45 -05:00
if options . get ( ' raw ' , False ) :
time_format = ' % Y % m %d % H % M % SZ '
else :
time_format = ' % Y- % m- %d T % H: % M: % SZ '
newresult [ ' now ' ] = unicode ( strftime ( time_format , gmtime ( ) ) )
2013-10-31 11:54:21 -05:00
convert_nsaccountlock ( entry )
if ' nsaccountlock ' in entry :
disabled = entry [ ' nsaccountlock ' ]
2016-06-29 23:37:52 -05:00
self . api . Object . user . get_preserved_attribute ( entry , options )
2012-02-14 08:41:25 -06:00
entries . append ( newresult )
count + = 1
except errors . NotFound :
2018-01-03 05:11:15 -06:00
raise self . api . Object . user . handle_not_found ( * keys )
2015-07-30 09:49:29 -05:00
except Exception as e :
2017-05-23 11:35:57 -05:00
logger . error ( " user_status: Retrieving status for %s failed "
" with %s " , dn , str ( e ) )
2013-12-05 07:43:19 -06:00
newresult = { ' dn ' : dn }
2012-02-14 08:41:25 -06:00
newresult [ ' server ' ] = _ ( " %(host)s failed " ) % dict ( host = host )
entries . append ( newresult )
count + = 1
if host != api . env . host :
2017-02-23 03:44:04 -06:00
other_ldap . close ( )
2012-02-14 08:41:25 -06:00
return dict ( result = entries ,
count = count ,
truncated = False ,
2012-04-23 03:16:45 -05:00
summary = unicode ( _ ( ' Account disabled: %(disabled)s ' %
dict ( disabled = disabled ) ) ) ,
2012-02-14 08:41:25 -06:00
)
2015-06-23 06:42:01 -05:00
@register ( )
2017-01-18 06:24:29 -06:00
class user_add_cert ( baseuser_add_cert ) :
2015-06-23 06:42:01 -05:00
__doc__ = _ ( ' Add one or more certificates to the user entry ' )
msg_summary = _ ( ' Added certificates to user " %(value)s " ' )
@register ( )
2017-01-18 06:24:29 -06:00
class user_remove_cert ( baseuser_remove_cert ) :
2015-06-23 06:42:01 -05:00
__doc__ = _ ( ' Remove one or more certificates to the user entry ' )
msg_summary = _ ( ' Removed certificates from user " %(value)s " ' )
2015-11-05 10:11:23 -06:00
2016-12-20 09:21:58 -06:00
@register ( )
class user_add_certmapdata ( baseuser_add_certmapdata ) :
__doc__ = _ ( " Add one or more certificate mappings to the user entry. " )
@register ( )
class user_remove_certmapdata ( baseuser_remove_certmapdata ) :
__doc__ = _ ( " Remove one or more certificate mappings from the user entry. " )
2015-11-05 10:11:23 -06:00
@register ( )
class user_add_manager ( baseuser_add_manager ) :
__doc__ = _ ( " Add a manager to the user entry " )
@register ( )
class user_remove_manager ( baseuser_remove_manager ) :
__doc__ = _ ( " Remove a manager to the user entry " )
2016-06-23 13:06:42 -05:00
@register ( )
class user_add_principal ( baseuser_add_principal ) :
__doc__ = _ ( ' Add new principal alias to the user entry ' )
msg_summary = _ ( ' Added new aliases to user " %(value)s " ' )
@register ( )
class user_remove_principal ( baseuser_remove_principal ) :
__doc__ = _ ( ' Remove principal alias from the user entry ' )
msg_summary = _ ( ' Removed aliases from user " %(value)s " ' )