[PATCH] Port of dsdb, dsdb_dns and samdb Python modules

Andrew Bartlett abartlet at samba.org
Fri Feb 9 19:26:29 UTC 2018


On Sat, 2018-02-10 at 08:11 +1300, Douglas Bagnall wrote:
> On 10/02/18 07:01, Andrew Bartlett via samba-technical wrote:
> > Yes, I was hoping you could clarify here (and probably in the commit
> > message) why there is no common way to do this:
> 
> Warning: your continued attention to this is attracting bikeshedders.

:-)

> > -            pw = unicode('"' + password.encode('utf-8') + '"', 'utf-8').encode('utf-16-le')
> > +
> > +            if PY3:
> > +                pw = str('"' + password + '"').encode('utf-16-le')
> > +            else:
> > +                pw = unicode('"' + password.encode('utf-8') + '"', 'utf-8').encode('utf-16-le')
> > 
> > The 'if PY3:' stuff just bothers me because we will live with it
> > forever, and if we must do it I would prefer it in a helper function
> > somewhere. 
> 
> I don't think this situation is very common or something we want to keep.
> 
> By my unpacking of the snowball:
> 
> -            pw = unicode('"' + password.encode('utf-8') + '"', 'utf-8').encode('utf-16-le')
> 
> there are 3 cases before the .encode('utf-16-le'):
> 
> 1. password is unicode (somehow): it goes through a .encode().decode() cycle to unicode.
> 2. password is ascii str: it goes through .encode() NOOP, .decode() to unicode.
> 3. password is non-ascii (perhaps utf-8) str: encode ERROR
> 
> I think something like this is wanted:
> 
> +            if not PY3 and isinstance(password, str):
> +                password = unicode(password, 'utf-8')
> +            pw = (u"%s" % password).encode('utf-16-le')

OK.  

I have to say that is at least more clear.

For those preparing the final patch, just remember that the goal is to
have UTF-16-LE " (double-quote) chars around the UTF-16-LE password in
the actual LDAP attribute being set.

that is, presumably, untested:

+            if not PY3 and isinstance(password, str): 
+                password = unicode(password, 'utf-8')
+            pw = (u'"%s"' % password).encode('utf-16-le')

Thanks,

Andrew Bartlett

-- 
Andrew Bartlett                       http://samba.org/~abartlet/
Authentication Developer, Samba Team  http://samba.org
Samba Developer, Catalyst IT          http://catalyst.net.nz/services/samba




More information about the samba-technical mailing list