X-Git-Url: https://git.adam-barratt.org.uk/?p=mirror%2Fuserdir-ldap.git;a=blobdiff_plain;f=UDLdap.py;fp=UDLdap.py;h=ff47124d33169dd73ef03518f1e72db1461754fb;hp=f07ba361ac929821e82b4e7d947000178db05a23;hb=4761f401f838a00cba01172eeb7183c5b7777b0f;hpb=6ee830ff583d3db2afa0044a378b1737853e54b0 diff --git a/UDLdap.py b/UDLdap.py index f07ba36..ff47124 100644 --- a/UDLdap.py +++ b/UDLdap.py @@ -4,23 +4,24 @@ import datetime import userdir_ldap import sys + class Account: array_values = ['objectClass', 'keyFingerPrint', 'mailWhitelist', 'mailRBL', 'mailRHSBL', 'supplementaryGid', 'sshRSAAuthKey', 'sudoPassword', 'dnsZoneEntry', 'allowedHost'] int_values = ['shadowExpire', 'gidNumber', 'uidNumber'] defaults = { - 'accountStatus': 'active', - 'keyFingerPrint': [] - } + 'accountStatus': 'active', + 'keyFingerPrint': [] + } @staticmethod def from_search(ldap_connection, base, user): - searchresult = ldap_connection.search_s(base, ldap.SCOPE_SUBTREE, 'uid=%s'%(user)) + searchresult = ldap_connection.search_s(base, ldap.SCOPE_SUBTREE, 'uid=%s' % (user,)) if len(searchresult) < 1: - raise IndexError, "No such user: %s\n"%(user) + raise IndexError("No such user: %s\n" % (user,)) elif len(searchresult) > 1: - raise IndexError, "More than one hit when getting %s\n"%(user) + raise IndexError("More than one hit when getting %s\n" % (user,)) else: return Account(searchresult[0][0], searchresult[0][1]) @@ -37,7 +38,7 @@ class Account: if key in self.array_values: self.cache[key] = self.attributes[key] elif not len(self.attributes[key]) == 1: - raise ValueError, 'non-array value has not exactly one value' + raise ValueError('non-array value has not exactly one value') elif key in self.int_values: self.cache[key] = int(self.attributes[key][0]) else: @@ -45,7 +46,7 @@ class Account: elif key in self.defaults: self.cache[key] = self.defaults[key] else: - raise IndexError, "No such key: %s (dn: %s)"%(key, self.dn) + raise IndexError("No such key: %s (dn: %s)" % (key, self.dn)) return self.cache[key] @@ -59,7 +60,7 @@ class Account: # not locked locked, just reset to something invalid like {crypt}*SSLRESET* is still active def pw_active(self): - if not 'userPassword' in self: + if 'userPassword' not in self: return False if self['userPassword'].upper() == '{CRYPT}*LK*': return False @@ -77,7 +78,7 @@ class Account: # not expired def shadow_active(self): if 'shadowExpire' in self and \ - self['shadowExpire'] < (time.time() / 3600 / 24): + self['shadowExpire'] < (time.time() / 3600 / 24): return False return True @@ -92,18 +93,19 @@ class Account: def latitude_dec(self, anonymized=False): return userdir_ldap.DecDegree(self['latitude'], anonymized) + def longitude_dec(self, anonymized=False): return userdir_ldap.DecDegree(self['longitude'], anonymized) def verbose_status(self): status = [] - status.append('mail: %s' %(['disabled', 'active'][ self.has_mail() ])) - status.append('pw: %s' %(['locked', 'active'][ self.pw_active() ])) - status.append('shadow: %s'%(['expired', 'active'][ self.shadow_active() ])) - status.append('keys: %d' %( self.numkeys() )) - status.append('status: %s'%( self['accountStatus'] )) + status.append('mail: %s' % (['disabled', 'active'][self.has_mail()])) + status.append('pw: %s' % (['locked', 'active'][self.pw_active()])) + status.append('shadow: %s' % (['expired', 'active'][self.shadow_active()])) + status.append('keys: %d' % (self.numkeys())) + status.append('status: %s' % (self['accountStatus'])) - return '(%s)'%(', '.join(status)) + return '(%s)' % (', '.join(status)) def delete_mailforward(self): del self.attributes['emailForward'] @@ -114,24 +116,30 @@ class Account: def email_address(self): mailbox = "<%s@%s>" % (self['uid'], userdir_ldap.EmailAppend) tokens = [] - if 'cn' in self: tokens.append(self['cn']) - if 'sn' in self: tokens.append(self['sn']) + if 'cn' in self: + tokens.append(self['cn']) + if 'sn' in self: + tokens.append(self['sn']) tokens.append(mailbox) return ' '.join(tokens) def is_allowed_by_hostacl(self, host): - if not 'allowedHost' in self: return False - if host in self['allowedHost']: return True + if 'allowedHost' not in self: + return False + if host in self['allowedHost']: + return True # or maybe it's a date limited ACL for entry in self['allowedHost']: - list = entry.split(None,1) - if len(list) == 1: continue + list = entry.split(None, 1) + if len(list) == 1: + continue (h, expire) = list - if host != h: continue + if host != h: + continue try: parsed = datetime.datetime.strptime(expire, '%Y%m%d') except ValueError: - print >>sys.stderr, "Cannot parse expiry date in '%s' in hostACL entry for %s."%(entry, self['uid']) + print >>sys.stderr, "Cannot parse expiry date in '%s' in hostACL entry for %s." % (entry, self['uid']) return False return parsed >= datetime.datetime.now() return False