[PATCH] fix for bug 12292
Rowland Penny
repenny241155 at gmail.com
Tue Sep 27 21:33:25 UTC 2016
On Wed, 28 Sep 2016 10:16:56 +1300
Douglas Bagnall <douglas.bagnall at catalyst.net.nz> wrote:
> hi Rowland,
>
> > From c475d71dc27d4c8bb95953ca6f67c3ffd871e6ed Mon Sep 17 00:00:00
> > 2001 From: Rowland Penny <rpenny at samba.org>
> > Date: Tue, 27 Sep 2016 19:48:50 +0100
> > Subject: [PATCH 1/2] Bug 12292: stop <unknown_user> throwing error
> >
> > Signed-off-by: Rowland Penny <rpenny at samba.org>
> > ---
> > python/samba/samdb.py | 15 ++++-----------
> > 1 file changed, 4 insertions(+), 11 deletions(-)
> >
> > diff --git a/python/samba/samdb.py b/python/samba/samdb.py
> > index 3d7ea3e..42b39f3 100644
> > --- a/python/samba/samdb.py
> > +++ b/python/samba/samdb.py
> > @@ -463,22 +463,15 @@ member: %s
> > else:
> > self.transaction_commit()
> >
> > + def deleteobject(self, object_dn):
> > + """Deletes an AD object
> >
> > - def deleteuser(self, username):
> > - """Deletes a user
>
> Won't this also break python/samba/tests/samba_tool/rodc.py?
Why ?? can you point me to documentation that backs up this question.
I have since had thoughts about these patches, so I am going to alter
them.
When you delete an object in AD, all you need is the DN, so it doesn't
matter if the object is a user or a group or a computer, you just
delete the DN, so plan is create something in samdb.py to get an
objects DN from a filter, return the resultant dn and then delete the
object.
This will save on duplication of code.
Thanks for looking at my patches.
Rowland
>
> > -
> > - :param username: Name of the target user
> > + :param username: The DN of the target object
>
> The param is now "object_dn", not "username".
OOPs
>
> > """
> >
> > - filter = "(&(sAMAccountName=%s)(objectCategory=%s,%s))" %
> > (ldb.binary_encode(username),
> > "CN=Person,CN=Schema,CN=Configuration", self.domain_dn())
> > self.transaction_start() try:
> > - target = self.search(base=self.domain_dn(),
> > scope=ldb.SCOPE_SUBTREE,
> > - expression=filter, attrs=[])
> > - if len(target) == 0:
> > - raise Exception('Unable to find user "%s"' %
> > username)
>
> It seems to me a better approach would be to keep this stuff but raise
> a specific Exception here. That is, make one like this:
>
> class UserNotFoundException(Exception):
> pass
>
> and raise it in that line above. Then...
>
> > - assert(len(target) == 1)
> > - self.delete(target[0].dn)
> > + self.delete(object_dn)
> > except:
> > self.transaction_cancel()
> > raise
> > -- 2.1.4
> >
> >
> > 0002-Bug-12292-stop-unknown_user-throwing-error.patch
> >
> >
> > From 86ab6a24c012883874cd82d900a6338bf1cf6515 Mon Sep 17 00:00:00
> > 2001 From: Rowland Penny <rpenny at samba.org>
> > Date: Tue, 27 Sep 2016 19:56:21 +0100
> > Subject: [PATCH 2/2] Bug 12292: stop <unknown_user> throwing error
> >
> > Signed-off-by: Rowland Penny <rpenny at samba.org>
> > ---
> > python/samba/netcmd/user.py | 20 ++++++++++++++++----
> > 1 file changed, 16 insertions(+), 4 deletions(-)
> >
> > diff --git a/python/samba/netcmd/user.py
> > b/python/samba/netcmd/user.py index 5adc287..8ce4312 100644
> > --- a/python/samba/netcmd/user.py
> > +++ b/python/samba/netcmd/user.py
> > @@ -404,12 +404,24 @@ Example2 shows how to delete a user in the
> > domain against the local server. su def run(self, username,
> > credopts=None, sambaopts=None, versionopts=None, H=None):
> > lp = sambaopts.get_loadparm()
> > - creds = credopts.get_credentials(lp, fallback_machine=True)
> > + samdb = SamDB(url=H, session_info=system_session(),
> > + credentials=creds, lp=lp)
> > +
> > + domaindn = samdb.domain_dn()
> > +
> > + filter =
> > ("(&(sAMAccountName=%s)(sAMAccountType=805306368))" %
> > + (ldb.binary_encode(username)))
> > +
> > + res = samdb.search(base=domaindn, expression=filter,
> > + scope=ldb.SCOPE_SUBTREE, attrs=["dn"])
> > + if len(res) == 0:
> > + raise CommandError('Unable to find user "%s"' %
> > (username))
> > +
> > + for msg in res:
> > + user_dn = str(msg.get("dn"))
> >
> > try:
> > - samdb = SamDB(url=H, session_info=system_session(),
> > - credentials=creds, lp=lp)
> > - samdb.deleteuser(username)
> > + samdb.deleteobject(user_dn)
>
> here you would go:
>
> except samdb.UserNotFoundException:
> raise CommandError('Unable to find user "%s"' %
> username)
>
> or whatever. You can string a series of excepts together to catch
> specific cases.
>
> > except Exception, e:
> > raise CommandError('Failed to remove user "%s"' %
> > username, e) self.outf.write("Deleted user %s\n" % username)
> > -- 2.1.4
> >
>
> cheers,
> Douglas
More information about the samba-technical
mailing list