CVE-2021-3671 HEIMDAL kdc: validate sname in TGS-REQ
[Samba.git] / script / generate_param.py
blobc29a29df57e7a96585c3db3e3df284076a40e936
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) 2014 Catalyst.Net Ltd
4 # Auto generate param_functions.c
6 # ** NOTE! The following LGPL license applies to the ldb
7 # ** library. This does NOT imply that all of Samba is released
8 # ** under the LGPL
10 # This library is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public
12 # License as published by the Free Software Foundation; either
13 # version 3 of the License, or (at your option) any later version.
15 # This library is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # Lesser General Public License for more details.
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 import os
25 import xml.etree.ElementTree as ET
26 import optparse
28 # parse command line arguments
29 parser = optparse.OptionParser()
30 parser.add_option("-f", "--file", dest="filename",
31 help="input file", metavar="FILE")
32 parser.add_option("-o", "--output", dest="output",
33 help='output file', metavar="FILE")
34 parser.add_option("--mode", type="choice", metavar="<FUNCTIONS|S3PROTO|LIBPROTO|PARAMDEFS|PARAMTABLE>",
35 choices=["FUNCTIONS", "S3PROTO", "LIBPROTO", "PARAMDEFS", "PARAMTABLE"], default="FUNCTIONS")
36 parser.add_option("--scope", metavar="<GLOBAL|LOCAL>",
37 choices=["GLOBAL", "LOCAL"], default="GLOBAL")
39 (options, args) = parser.parse_args()
41 if options.filename is None:
42 parser.error("No input file specified")
43 if options.output is None:
44 parser.error("No output file specified")
47 def iterate_all(path):
48 """Iterate and yield all the parameters.
50 :param path: path to parameters xml file
51 """
53 try:
54 p = open(path, 'r')
55 except IOError as e:
56 raise Exception("Error opening parameters file")
57 out = p.read()
59 # parse the parameters xml file
60 root = ET.fromstring(out)
61 for parameter in root:
62 name = parameter.attrib.get("name")
63 param_type = parameter.attrib.get("type")
64 context = parameter.attrib.get("context")
65 func = parameter.attrib.get("function")
66 synonym = parameter.attrib.get("synonym")
67 removed = parameter.attrib.get("removed")
68 generated = parameter.attrib.get("generated_function")
69 handler = parameter.attrib.get("handler")
70 enumlist = parameter.attrib.get("enumlist")
71 deprecated = parameter.attrib.get("deprecated")
72 synonyms = parameter.findall('synonym')
74 if removed == "1":
75 continue
77 constant = parameter.attrib.get("constant")
78 substitution = parameter.attrib.get("substitution")
79 parm = parameter.attrib.get("parm")
80 if name is None or param_type is None or context is None:
81 raise Exception("Error parsing parameter: " + name)
82 if func is None:
83 func = name.replace(" ", "_").lower()
84 if enumlist is None:
85 enumlist = "NULL"
86 if handler is None:
87 handler = "NULL"
88 yield {'name': name,
89 'type': param_type,
90 'context': context,
91 'function': func,
92 'constant': (constant == '1'),
93 'substitution': (substitution == '1'),
94 'parm': (parm == '1'),
95 'synonym' : synonym,
96 'generated' : generated,
97 'enumlist' : enumlist,
98 'handler' : handler,
99 'deprecated' : deprecated,
100 'synonyms' : synonyms }
103 # map doc attributes to a section of the generated function
104 context_dict = {"G": "_GLOBAL", "S": "_LOCAL"}
105 param_type_dict = {
106 "boolean" : "_BOOL",
107 "list" : "_LIST",
108 "string" : "_STRING",
109 "integer" : "_INTEGER",
110 "enum" : "_INTEGER",
111 "char" : "_CHAR",
112 "boolean-auto" : "_INTEGER",
113 "cmdlist" : "_LIST",
114 "bytes" : "_INTEGER",
115 "octal" : "_INTEGER",
116 "ustring" : "_STRING",
120 def generate_functions(path_in, path_out):
121 f = open(path_out, 'w')
122 try:
123 f.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
124 for parameter in iterate_all(options.filename):
125 # filter out parameteric options
126 if ':' in parameter['name']:
127 continue
128 if parameter['synonym'] == "1":
129 continue
130 if parameter['generated'] == "0":
131 continue
133 output_string = "FN"
134 temp = context_dict.get(parameter['context'])
135 if temp is None:
136 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
137 output_string += temp
138 if parameter['type'] == "string" or parameter['type'] == "ustring":
139 if parameter['substitution']:
140 output_string += "_SUBSTITUTED"
141 else:
142 output_string += "_CONST"
143 if parameter['parm']:
144 output_string += "_PARM"
145 temp = param_type_dict.get(parameter['type'])
146 if temp is None:
147 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
148 output_string += temp
149 f.write(output_string + "(" + parameter['function'] + ", " + parameter['function'] + ')\n')
150 finally:
151 f.close()
154 mapping = {
155 'boolean' : 'bool ',
156 'string' : 'char *',
157 'integer' : 'int ',
158 'char' : 'char ',
159 'list' : 'const char **',
160 'enum' : 'int ',
161 'boolean-auto' : 'int ',
162 'cmdlist' : 'const char **',
163 'bytes' : 'int ',
164 'octal' : 'int ',
165 'ustring' : 'char *',
169 def make_s3_param_proto(path_in, path_out):
170 file_out = open(path_out, 'w')
171 try:
172 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
173 header = get_header(path_out)
174 file_out.write("#ifndef %s\n" % header)
175 file_out.write("#define %s\n\n" % header)
176 for parameter in iterate_all(path_in):
177 # filter out parameteric options
178 if ':' in parameter['name']:
179 continue
180 if parameter['synonym'] == "1":
181 continue
182 if parameter['generated'] == "0":
183 continue
185 output_string = ""
186 param_type = mapping.get(parameter['type'])
187 if param_type is None:
188 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
189 output_string += param_type
190 output_string += "lp_%s" % parameter['function']
192 param = None
193 if parameter['parm']:
194 param = "const struct share_params *p"
195 else:
196 param = "int"
198 if parameter['type'] == 'string' or parameter['type'] == 'ustring':
199 if parameter['substitution']:
200 if parameter['context'] == 'G':
201 output_string += '(TALLOC_CTX *ctx, const struct loadparm_substitution *lp_sub);\n'
202 elif parameter['context'] == 'S':
203 output_string += '(TALLOC_CTX *ctx, const struct loadparm_substitution *lp_sub, %s);\n' % param
204 else:
205 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
206 else:
207 if parameter['context'] == 'G':
208 output_string = 'const ' + output_string + '(void);\n'
209 elif parameter['context'] == 'S':
210 output_string = 'const ' + output_string + '(%s);\n' % param
211 else:
212 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
213 else:
214 if parameter['context'] == 'G':
215 output_string += '(void);\n'
216 elif parameter['context'] == 'S':
217 output_string += '(%s);\n' % param
218 else:
219 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
221 file_out.write(output_string)
223 file_out.write("\n#endif /* %s */\n\n" % header)
224 finally:
225 file_out.close()
228 def make_lib_proto(path_in, path_out):
229 file_out = open(path_out, 'w')
230 try:
231 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
232 for parameter in iterate_all(path_in):
233 # filter out parameteric options
234 if ':' in parameter['name']:
235 continue
236 if parameter['synonym'] == "1":
237 continue
238 if parameter['generated'] == "0":
239 continue
241 output_string = ""
242 param_type = mapping.get(parameter['type'])
243 if param_type is None:
244 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
245 output_string += param_type
247 output_string += "lpcfg_%s" % parameter['function']
249 if parameter['type'] == 'string' or parameter['type'] == 'ustring':
250 if parameter['substitution']:
251 if parameter['context'] == 'G':
252 output_string += '(struct loadparm_context *, const struct loadparm_substitution *lp_sub, TALLOC_CTX *ctx);\n'
253 elif parameter['context'] == 'S':
254 output_string += '(struct loadparm_service *, struct loadparm_service *, TALLOC_CTX *ctx);\n'
255 else:
256 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
257 else:
258 if parameter['context'] == 'G':
259 output_string = 'const ' + output_string + '(struct loadparm_context *);\n'
260 elif parameter['context'] == 'S':
261 output_string = 'const ' + output_string + '(struct loadparm_service *, struct loadparm_service *);\n'
262 else:
263 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
264 else:
265 if parameter['context'] == 'G':
266 output_string += '(struct loadparm_context *);\n'
267 elif parameter['context'] == 'S':
268 output_string += '(struct loadparm_service *, struct loadparm_service *);\n'
269 else:
270 raise Exception(parameter['name'] + " has an invalid param type " + parameter['type'])
272 file_out.write(output_string)
273 finally:
274 file_out.close()
277 def get_header(path):
278 header = os.path.basename(path).upper()
279 header = header.replace(".", "_").replace("\\", "_").replace("-", "_")
280 return "__%s__" % header
283 def make_param_defs(path_in, path_out, scope):
284 file_out = open(path_out, 'w')
285 try:
286 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
287 header = get_header(path_out)
288 file_out.write("#ifndef %s\n" % header)
289 file_out.write("#define %s\n\n" % header)
290 if scope == "GLOBAL":
291 file_out.write("/**\n")
292 file_out.write(" * This structure describes global (ie., server-wide) parameters.\n")
293 file_out.write(" */\n")
294 file_out.write("struct loadparm_global \n")
295 file_out.write("{\n")
296 file_out.write("\tTALLOC_CTX *ctx; /* Context for talloced members */\n")
297 elif scope == "LOCAL":
298 file_out.write("/**\n")
299 file_out.write(" * This structure describes a single service.\n")
300 file_out.write(" */\n")
301 file_out.write("struct loadparm_service \n")
302 file_out.write("{\n")
303 file_out.write("\tbool autoloaded;\n")
305 for parameter in iterate_all(path_in):
306 # filter out parameteric options
307 if ':' in parameter['name']:
308 continue
309 if parameter['synonym'] == "1":
310 continue
312 if (scope == "GLOBAL" and parameter['context'] != "G" or
313 scope == "LOCAL" and parameter['context'] != "S"):
314 continue
316 output_string = "\t"
317 param_type = mapping.get(parameter['type'])
318 if param_type is None:
319 raise Exception(parameter['name'] + " has an invalid context " + parameter['context'])
320 output_string += param_type
322 output_string += " %s;\n" % parameter['function']
323 file_out.write(output_string)
325 file_out.write("LOADPARM_EXTRA_%sS\n" % scope)
326 file_out.write("};\n")
327 file_out.write("\n#endif /* %s */\n\n" % header)
328 finally:
329 file_out.close()
332 type_dict = {
333 "boolean" : "P_BOOL",
334 "boolean-rev" : "P_BOOLREV",
335 "boolean-auto" : "P_ENUM",
336 "list" : "P_LIST",
337 "string" : "P_STRING",
338 "integer" : "P_INTEGER",
339 "enum" : "P_ENUM",
340 "char" : "P_CHAR",
341 "cmdlist" : "P_CMDLIST",
342 "bytes" : "P_BYTES",
343 "octal" : "P_OCTAL",
344 "ustring" : "P_USTRING",
348 def make_param_table(path_in, path_out):
349 file_out = open(path_out, 'w')
350 try:
351 file_out.write('/* This file was automatically generated by generate_param.py. DO NOT EDIT */\n\n')
352 header = get_header(path_out)
353 file_out.write("#ifndef %s\n" % header)
354 file_out.write("#define %s\n\n" % header)
356 file_out.write("struct parm_struct parm_table[] = {\n")
358 for parameter in iterate_all(path_in):
359 # filter out parameteric options
360 if ':' in parameter['name']:
361 continue
362 if parameter['context'] == 'G':
363 p_class = "P_GLOBAL"
364 else:
365 p_class = "P_LOCAL"
367 p_type = type_dict.get(parameter['type'])
369 if parameter['context'] == 'G':
370 temp = "GLOBAL"
371 else:
372 temp = "LOCAL"
373 offset = "%s_VAR(%s)" % (temp, parameter['function'])
375 enumlist = parameter['enumlist']
376 handler = parameter['handler']
377 synonym = parameter['synonym']
378 deprecated = parameter['deprecated']
379 flags_list = []
380 if synonym == "1":
381 flags_list.append("FLAG_SYNONYM")
382 if deprecated == "1":
383 flags_list.append("FLAG_DEPRECATED")
384 flags = "|".join(flags_list)
385 synonyms = parameter['synonyms']
387 file_out.write("\t{\n")
388 file_out.write("\t\t.label\t\t= \"%s\",\n" % parameter['name'])
389 file_out.write("\t\t.type\t\t= %s,\n" % p_type)
390 file_out.write("\t\t.p_class\t= %s,\n" % p_class)
391 file_out.write("\t\t.offset\t\t= %s,\n" % offset)
392 file_out.write("\t\t.special\t= %s,\n" % handler)
393 file_out.write("\t\t.enum_list\t= %s,\n" % enumlist)
394 if flags != "":
395 file_out.write("\t\t.flags\t\t= %s,\n" % flags)
396 file_out.write("\t},\n")
398 if synonyms is not None:
399 # for synonyms, we only list the synonym flag:
400 flags = "FLAG_SYNONYM"
401 for syn in synonyms:
402 file_out.write("\t{\n")
403 file_out.write("\t\t.label\t\t= \"%s\",\n" % syn.text)
404 file_out.write("\t\t.type\t\t= %s,\n" % p_type)
405 file_out.write("\t\t.p_class\t= %s,\n" % p_class)
406 file_out.write("\t\t.offset\t\t= %s,\n" % offset)
407 file_out.write("\t\t.special\t= %s,\n" % handler)
408 file_out.write("\t\t.enum_list\t= %s,\n" % enumlist)
409 if flags != "":
410 file_out.write("\t\t.flags\t\t= %s,\n" % flags)
411 file_out.write("\t},\n")
413 file_out.write("\n\t{ .label = NULL }\n")
414 file_out.write("};\n")
415 file_out.write("\n#endif /* %s */\n\n" % header)
416 finally:
417 file_out.close()
420 if options.mode == 'FUNCTIONS':
421 generate_functions(options.filename, options.output)
422 elif options.mode == 'S3PROTO':
423 make_s3_param_proto(options.filename, options.output)
424 elif options.mode == 'LIBPROTO':
425 make_lib_proto(options.filename, options.output)
426 elif options.mode == 'PARAMDEFS':
427 make_param_defs(options.filename, options.output, options.scope)
428 elif options.mode == 'PARAMTABLE':
429 make_param_table(options.filename, options.output)