1 # Create DisplaySpecifiers LDIF (as a string) from the documents provided by
2 # Microsoft under the WSPP.
4 # Copyright (C) Andrew Kroeger <andrew@id10ts.net> 2009
6 # Based on ms_schema.py
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from __future__ import print_function
25 def __read_folded_line(f, buffer):
26 """Read a line from an LDIF file, unfolding it"""
35 # cannot fold an empty line
36 assert(line != "" and line != "\n")
46 # eof, definitely won't be folded
49 # marks end of a folded line
50 # line contains the now unfolded line
51 # buffer contains the start of the next possibly folded line
57 # Only compile regexp once.
58 # Will not match options after the attribute type.
59 attr_type_re = re.compile("^([A-Za-z][A-Za-z0-9-]*):")
62 def __read_raw_entries(f):
63 """Read an LDIF entry, only unfolding lines"""
71 (l, buffer) = __read_folded_line(f, buffer)
76 if l == "\n" or l == "":
79 m = attr_type_re.match(l)
87 print("Invalid line: %s" % l, end=' ', file=sys.stderr)
98 """Fix a string DN to use ${CONFIGDN}"""
100 if dn.find("<Configuration NC Distinguished Name>") != -1:
101 dn = dn.replace("\n ", "")
102 return dn.replace("<Configuration NC Distinguished Name>", "${CONFIGDN}")
107 def __write_ldif_one(entry):
108 """Write out entry as LDIF"""
113 out.append("%s: %s" % (l[0], l[1]))
115 # This is a base64-encoded value
116 out.append("%s:: %s" % (l[0], l[1]))
118 return "\n".join(out)
121 def __transform_entry(entry):
122 """Perform required transformations to the Microsoft-provided LDIF"""
129 if l.find("::") != -1:
130 # This is a base64-encoded value
131 t = l.split(":: ", 1)
139 if key == "changetype":
142 if key == "distinguishedname":
145 if key == "instancetype":
154 if key == "objectcategory":
157 if key == "showinadvancedviewonly":
158 value = t[1].upper().lstrip().rstrip()
160 # Remove showInAdvancedViewOnly attribute if it is set to the
161 # default value of TRUE
173 def read_ms_ldif(filename):
174 """Read and transform Microsoft-provided LDIF file."""
178 f = open(filename, "rU")
179 for entry in __read_raw_entries(f):
180 out.append(__write_ldif_one(__transform_entry(entry)))
182 return "\n\n".join(out) + "\n\n"
184 if __name__ == '__main__':
188 display_specifiers_file = sys.argv[1]
190 print("Usage: %s display-specifiers-ldif-file.txt" % (sys.argv[0]), file=sys.stderr)
193 print(read_ms_ldif(display_specifiers_file))