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
24 def __read_folded_line(f
, buffer):
25 """Read a line from an LDIF file, unfolding it"""
34 # cannot fold an empty line
35 assert(line
!= "" and line
!= "\n")
45 # eof, definitely won't be folded
48 # marks end of a folded line
49 # line contains the now unfolded line
50 # buffer contains the start of the next possibly folded line
56 # Only compile regexp once.
57 # Will not match options after the attribute type.
58 attr_type_re
= re
.compile("^([A-Za-z][A-Za-z0-9-]*):")
60 def __read_raw_entries(f
):
61 """Read an LDIF entry, only unfolding lines"""
69 (l
, buffer) = __read_folded_line(f
, buffer)
74 if l
== "\n" or l
== "":
77 m
= attr_type_re
.match(l
)
85 print("Invalid line: %s" % l
, end
=' ', file=sys
.stderr
)
95 """Fix a string DN to use ${CONFIGDN}"""
97 if dn
.find("<Configuration NC Distinguished Name>") != -1:
98 dn
= dn
.replace("\n ", "")
99 return dn
.replace("<Configuration NC Distinguished Name>", "${CONFIGDN}")
103 def __write_ldif_one(entry
):
104 """Write out entry as LDIF"""
109 out
.append("%s: %s" % (l
[0], l
[1]))
111 # This is a base64-encoded value
112 out
.append("%s:: %s" % (l
[0], l
[1]))
114 return "\n".join(out
)
116 def __transform_entry(entry
):
117 """Perform required transformations to the Microsoft-provided LDIF"""
124 if l
.find("::") != -1:
125 # This is a base64-encoded value
126 t
= l
.split(":: ", 1)
134 if key
== "changetype":
137 if key
== "distinguishedname":
140 if key
== "instancetype":
149 if key
== "objectcategory":
152 if key
== "showinadvancedviewonly":
153 value
= t
[1].upper().lstrip().rstrip()
155 # Remove showInAdvancedViewOnly attribute if it is set to the
156 # default value of TRUE
167 def read_ms_ldif(filename
):
168 """Read and transform Microsoft-provided LDIF file."""
172 f
= open(filename
, "rU")
173 for entry
in __read_raw_entries(f
):
174 out
.append(__write_ldif_one(__transform_entry(entry
)))
176 return "\n\n".join(out
) + "\n\n"
178 if __name__
== '__main__':
182 display_specifiers_file
= sys
.argv
[1]
184 print("Usage: %s display-specifiers-ldif-file.txt" % (sys
.argv
[0]), file=sys
.stderr
)
187 print(read_ms_ldif(display_specifiers_file
))