web2ldap  1.7.7
About: web2ldap is a full-featured web-based LDAPv3 client.
  Fossies Dox: web2ldap-1.7.7.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

posixautogen.py
Go to the documentation of this file.
1# -*- coding: ascii -*-
2"""
3Auto-generate some posixAccount attribute values
4
5Status:
6Experimental => you have to understand what it internally does when enabling it!
7"""
8
9import ldap0
10from ldap0.res import SearchReference
11
12from .nis import syntax_registry, UidNumber, GidNumber, IA5String
13
14
16 oid: str = 'HomeDirectory-oid'
17 desc: str = 'Path of Unix home directory of the user'
18 uid_attr = 'uid'
19 homeDirectoryTemplate = '/home/{uid}'
20
21 def transmute(self, attr_values):
22 if self.uid_attr not in self._entry:
23 return attr_values
24 if (
25 not attr_values or
26 not attr_values[0] or
27 attr_values[0].decode(self._app.ls.charset) == self.homeDirectoryTemplate.format(
28 **{self.uid_attr:''}
29 )
30 ):
31 fmt_dict = {self.uid_attr:self._entry[self.uid_attr][0].decode(self._app.ls.charset)}
32 attr_values = [
33 self.homeDirectoryTemplate.format(**fmt_dict).encode(self._app.ls.charset)
34 ]
35 return attr_values
36
37syntax_registry.reg_at(
38 HomeDirectory.oid, [
39 '1.3.6.1.1.1.1.3', # homeDirectory
40 ]
41)
42
43
45 input_size = 12
46 minNewValue = 10000
47 maxNewValue = 19999
48 object_class = 'posixAccount'
49
50 def form_value(self) -> str:
51 oc_set = {
52 oc.lower().decode('ascii')
53 for oc in self._entry['objectClass']
54 }
55 if self.object_class.lower() not in oc_set:
56 return ''
57 try:
58 ldap_results = self._app.ls.l.search_s(
59 str(self._app.naming_context),
60 ldap0.SCOPE_SUBTREE,
61 '(&(objectClass={0})({1}>={2})({1}<={3}))'.format(
62 self.object_class,
63 self._at,
64 self.__class__.minNewValue,
65 self.__class__.maxNewValue
66 ),
67 attrlist=[self._at],
68 )
69 except (
70 ldap0.INVALID_SYNTAX,
71 ldap0.NO_SUCH_OBJECT,
72 ldap0.SIZELIMIT_EXCEEDED,
73 ldap0.TIMELIMIT_EXCEEDED,
74 ):
75 # search failed => no value suggested
76 return ''
77 idnumber_set = set()
78 for res in ldap_results:
79 if isinstance(res, SearchReference):
80 continue
81 if res.dn_s == self._dn:
82 return res.entry_s[self._at][0]
83 idnumber_set.add(int(res.entry_s[self._at][0]))
84 for idnumber in range(self.__class__.minNewValue, self.maxNewValue+1):
85 if idnumber in idnumber_set:
86 self.__class__.minNewValue = idnumber
87 else:
88 break
89 if idnumber > self.maxNewValue:
90 # end of valid range reached => no value suggested
91 return ''
92 return str(idnumber)
93
94
96 oid: str = 'AutogenUIDNumber-oid'
97 desc: str = 'numeric Unix-UID'
98 minNewValue = 10000
99 maxNewValue = 19999
100 object_class = 'posixAccount'
101
102 def form_value(self) -> str:
103 fval = UidNumber.form_value(self)
104 if not fval:
105 fval = AutogenNumberMixIn.form_value(self)
106 return fval
107
108syntax_registry.reg_at(
109 AutogenUIDNumber.oid, [
110 '1.3.6.1.1.1.1.0', # uidNumber
111 ]
112)
113
114
116 oid: str = 'AutogenGIDNumber-oid'
117 desc: str = 'numeric Unix-GID'
118 object_class = 'posixGroup'
119
120 def form_value(self) -> str:
121 fval = GidNumber.form_value(self)
122 if not fval:
123 fval = AutogenNumberMixIn.form_value(self)
124 return fval
125
126syntax_registry.reg_at(
127 AutogenGIDNumber.oid, [
128 '1.3.6.1.1.1.1.1', # gidNumber
129 ]
130)
131
132
133# Register all syntax classes in this module
134syntax_registry.reg_syntaxes(__name__)