Debian Codegraph @

bugxml/bugrecord.py

raw
1 #!/usr/bin/python3
2 # Copyright (c) 2016-2016 David Anderson.
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 # * Neither the name of the example nor the
13 # names of its contributors may be used to endorse or promote products
14 # derived from this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY David Anderson ''AS IS'' AND ANY
17 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 # DISCLAIMED. IN NO EVENT SHALL David Anderson BE LIABLE FOR ANY
20 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
26 # OF SUCH DAMAGE.
27
28
29 import sys
30
31 # Use only <pre> or </pre> all by itself in data.xml.
32 # No other data on either of such lines.
33 # All the lines between these two markers should be
34 # shown in individual lines.
35 def xmlize(linea, inhtml, inpre):
36 outi = []
37 l = linea
38 if l.find("<pre>") != -1:
39 if inhtml == "y":
40 s2 = "</p>" + l + "\n"
41 else:
42 s2 = l + "\n"
43 inpre = "y"
44 return s2, inpre
45 if l.find("</pre>") != -1:
46 if inhtml == "y":
47 s2 = l + "\n" + "<p>"
48 else:
49 s2 = l + "\n"
50 inpre = "n"
51 return s2, inpre
52 if inpre == "y" and inhtml == "n":
53 outi += ["<preline>"]
54 for c in l:
55 if c == "<":
56 outi += ["&lt;"]
57 elif c == ">":
58 outi += ["&gt;"]
59 elif c == "&":
60 outi += ["&amp;"]
61 # elif c == "'":
62 # outi += ["&apos;"]
63 elif c == '"':
64 outi += ["&quot;"]
65 else:
66 outi += [c]
67 if inpre == "y" and inhtml == "n":
68 outi += ["</preline>"]
69 outi += ["\n"]
70 s2 = "".join(outi)
71 return s2, inpre
72
73
74 def paraline(name, linea):
75 out = ""
76 strp = linea.strip()
77 if len(strp) < 1:
78 out = "<p>" + name + ":" + "</p>"
79 return out
80 out = "<p>" + name + ": "
81 out += linea
82 out += "</p>"
83 return out
84
85
86 def paralines(name, lines):
87 inpre = "n"
88 if len(lines) < 1:
89 out = "<p>" + name + ":" + "</p>"
90 return out
91 out = "<p>" + name + ":"
92 for lin in lines:
93 f, inpre = xmlize(lin, "y", inpre)
94 out += f
95 out += "</p>"
96 return out
97
98
99 def para(name, str):
100 if str == None:
101 out = "<p>" + name + ":" + "</p>"
102 elif len(str) > 0:
103 out = "<p>" + name + ":&nbsp" + str + "</p>"
104 else:
105 out = "<p>" + name + ":" + "</p>"
106 return out
107
108
109 class bugrecord:
110 def __init__(self, dwid):
111 self._id = dwid.strip()
112 self._cve = ""
113 self._fuzzer = ""
114 self._datereported = ""
115 self._reportedby = ""
116 self._vulnerability = []
117 self._product = ""
118 self._description = []
119 self._datefixed = ""
120 self._references = []
121 self._gitfixid = ""
122 self._tarrelease = ""
123
124 def setcve(self, pubid):
125 if self._cve != "":
126 print("Duplicate cve ", self._cve, pubid)
127 sys.exit(1)
128 self._cve = pubid.strip()
129 def setfuzzer(self, id):
130 if self._fuzzer != "":
131 print("Duplicate fuzzer ", self._fuzzer, id)
132 sys.exit(1)
133 self._fuzzer = id.strip()
134 def setdatereported(self, rep):
135 if self._datereported != "":
136 print("Duplicate datereported ", self._datereported, rep)
137 sys.exit(1)
138 self._datereported = rep.strip()
139
140 def setreportedby(self, rep):
141 if self._reportedby != "":
142 print("Duplicate reportedby ", self._reportedby, rep)
143 sys.exit(1)
144 self._reportedby = rep.strip()
145
146 def setvulnerability(self, vuln):
147 if len(self._vulnerability) != 0:
148 print("Duplicate vulnerability ", self._vulnerability, vuln)
149 sys.exit(1)
150 self._vulnerability = vuln
151
152 def setproduct(self, p):
153 if len(self._product) != 0:
154 print("Duplicate product ", self._product, p)
155 sys.exit(1)
156 self._product = p.strip()
157
158 def setdescription(self, d):
159 if len(self._description) != 0:
160 print("Duplicate description ", self._description, d)
161 sys.exit(1)
162 self._description = d
163
164 def setdatefixed(self, d):
165 if len(self._datefixed) != 0:
166 print("Duplicate datefixed ", self._datefixed, d)
167 sys.exit(1)
168 self._datefixed = d.strip()
169
170 def setreferences(self, r):
171 if len(self._references) != 0:
172 print("Duplicate references ", self._references, r)
173 sys.exit(1)
174 self._references = r
175
176 def setgitfixid(self, g):
177 if len(self._gitfixid) != 0:
178 print("Duplicate gitfixid ", self._gitfixid, g)
179 sys.exit(1)
180 self._gitfixid = g.strip()
181
182 def settarrelease(self, g):
183 if len(self._tarrelease) != 0:
184 print("Duplicate tarrelease ", self._tarrelease, g)
185 sys.exit(1)
186 self._tarrelease = g.strip()
187
188 def plist(self, title, lines):
189 if lines == None:
190 print(title)
191 return
192 if len(lines) == 1:
193 print(title, lines[0])
194 return
195 print(title)
196 for l in lines:
197 print(l)
198
199 def printbug(self):
200 print("")
201 print("id:", self._id)
202 print("cve:", self._cve)
203 print("fuzzer:", self._fuzzer)
204 print("datereported:", self._datereported)
205 print("reportedby:", self._reportedby)
206 self.plist("vulnerability:", self._vulnerability)
207 print("product:", self._product)
208 self.plist("description:", self._description)
209 print("datefixed:", self._datefixed)
210 self.plist("references:", self._references)
211 print("gitfixid:", self._gitfixid)
212 print("tarrelease:", self._tarrelease)
213
214 def generate_html(self,num):
215 s5 = "".join(self._id)
216 idhead = ''.join([str(num),") ",self._id])
217 t = "".join(['<h3 id="',s5, '">',idhead, "</h3>"])
218 txt = [t]
219
220 inpre = "n"
221 s, inp = xmlize(self._id, "y", inpre)
222 t = paraline("id", s)
223 txt += [t]
224
225 s, inp = xmlize(self._cve, "y", inpre)
226 t = paraline("cve", s)
227 txt += [t]
228
229 s, inp = xmlize(self._fuzzer, "y", inpre)
230 t = paraline("fuzzer", s)
231 txt += [t]
232
233 s, inp = xmlize(self._datereported, "y", inpre)
234 t = paraline("datereported", s)
235 txt += [t]
236
237 s, inp = xmlize(self._reportedby, "y", inpre)
238 t = paraline("reportedby", s)
239 txt += [t]
240
241 # MULTI
242 t = paralines("vulnerability", self._vulnerability)
243 txt += [t]
244
245 s, inp = xmlize(self._product, "y", inpre)
246 t = paraline("product", s)
247 txt += [t]
248
249 # MULTI
250 t = paralines("description", self._description)
251 txt += [t]
252
253 s, inp = xmlize(self._datefixed, "y", inpre)
254 t = paraline("datefixed", s)
255 txt += [t]
256
257 # MULTI
258 t = paralines("references", self._references)
259 txt += [t]
260
261 s, inp = xmlize(self._gitfixid, "y", inpre)
262 t = paraline("gitfixid", s)
263 txt += [t]
264
265 s, inp = xmlize(self._tarrelease, "y", inpre)
266 t = paraline("tarrelease", s)
267 txt += [t]
268
269 t = '<p> <a href="#top">[top]</a> </p>'
270 txt += [t]
271 return txt
272
273 def paraxml(self, start, main, term):
274 # For single line xml remove the newline from the main text line.
275 out = start
276 l = main.strip()
277 if len(l) > 0:
278 out += l
279 out += term
280 return out
281
282 def paraxmlN(self, start, main, term):
283 # For multi line xml leave newlines present.
284 out = start
285 inpre = "n"
286 for x in main:
287 l = x.rstrip()
288 t, inpre = xmlize(l, "n", inpre)
289 if len(t) > 0:
290 out += t
291 out += term
292 return out
293
294 def generate_xml(self):
295 txt = []
296 t = "<dwbug>"
297 txt += [t]
298
299 inpre = "n"
300 s, inpre = xmlize(self._id, "n", inpre)
301 t = self.paraxml("<dwid>", s, "</dwid>")
302 txt += [t]
303
304 s, inpre = xmlize(self._cve, "n", inpre)
305 t = self.paraxml("<cve>", s, "</cve>")
306 txt += [t]
307
308 s, inpre = xmlize(self._fuzzer, "n", inpre)
309 t = self.paraxml("<fuzzer>", s, "</fuzzer>")
310 txt += [t]
311
312 s, inpre = xmlize(self._datereported, "n", inpre)
313 t = self.paraxml("<datereported>", s, "</datereported>")
314 txt += [t]
315
316 s, inpre = xmlize(self._reportedby, "n", inpre)
317 t = self.paraxml("<reportedby>", s, "</reportedby>")
318 txt += [t]
319
320 s, inpre = xmlize(self._product, "n", inpre)
321 t = self.paraxml("<product>", s, "</product>")
322 txt += [t]
323
324 # MULTI
325 p = self._vulnerability
326 t = self.paraxmlN("<vulnerability>", p, "</vulnerability>")
327 txt += [t]
328
329 # MULTI
330 p = self._description
331 t = self.paraxmlN("<description>", p, "</description>")
332 txt += [t]
333
334 s, inpre = xmlize(self._datefixed, "n", inpre)
335 t = self.paraxml("<datefixed>", s, "</datefixed>")
336 txt += [t]
337
338 # MULTI
339 p = self._references
340 t = self.paraxmlN("<references>", p, "</references>")
341 txt += [t]
342
343 s, inpre = xmlize(self._gitfixid, "n", inpre)
344 t = self.paraxml("<gitfixid>", s, "</gitfixid>")
345 txt += [t]
346
347 s, inpre = xmlize(self._tarrelease, "n", inpre)
348 t = self.paraxml("<tarrelease>", s, "</tarrelease>")
349 txt += [t]
350
351 t = "</dwbug>"
352 txt += [t]
353 return txt