Debian Codegraph @

xsl/legacy-fix.xsl

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!--
3 ## THIS FILE HAS BEEN DEPRECATED! ##
4 OpenSCAP doesn't use this file anymore, but it's still installed
5 for compatibility reasons.
6
7
8 Copyright 2010 Red Hat Inc., Durham, North Carolina.
9 All Rights Reserved.
10
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version.
15
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24
25 Authors:
26 Lukas Kuklinek <[email protected]>
27 -->
28
29 <!--
30
31 FIX TEMPLATE WRITING GUIDE
32 **************************
33
34 This XSL uses a simple template engine to generate fix files.
35 Take a look at legacy-fixtpl-bash.xml file for an example.
36 All elements in such a file reside in the 'http://open-scap.org/xml/fixtemplate#' namespace.
37
38 Element hierarchy:
39 + fixtemplate - The toplevel element.
40 Attribute 'system' is a fix system identifier this template accepts.
41 It can be a whitespace-separated list of systems.
42 + linecomment - String that introduces a single-line comment.
43 (e.g. <linecomment>// </linecomment> for C++)
44 + before - This will be instantiated before actual fixes are outputted.
45 Can include for example a shebang, library imports, etc.
46 + fixentry - Template for single fix entry.
47 Element <sub select='$val'/> will be substituted with actual fix value.
48 + after - This will be instantiated after actual fixes are outputted.
49
50 Substitution elements can be placed to any of the child elements of the root element (before, fixentry, ...).
51 There are two of them:
52 + <sub select='xpath'/> will be substituted with a string value of the XPath expression.
53 + <if test='xpath'>conditional content, can include other <if> and <sub> elements</if>
54
55 Note that the XCCDF elements have to be accessed using the 'cdf:' prefix in the XPath expressions.
56 To obtain XCCDF context, several variables can be used in the XPath expressions:
57
58 $benchmark - XCCDF benchmark
59 $testresult - TestResult element being processed (can be unavailible)
60 $result - rule result associated with the fix (<fixentry> only, can be unavailible)
61 $rule - XCCDF rule associated with the fix (<fixentry> only)
62 $fix - XCCDF fix element (<fixentry> only)
63 $val - actual XCCDF fix text value (substitutions already performed, <fixentry> only)
64
65 -->
66
67 <xsl:stylesheet version="1.0"
68 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
69 xmlns:htm="http://www.w3.org/1999/xhtml"
70 xmlns:exsl="http://exslt.org/common"
71 xmlns:tpl="http://open-scap.org/xml/fixtemplate#"
72 xmlns:dyn="http://exslt.org/dynamic"
73 xmlns="http://www.w3.org/1999/xhtml"
74 xmlns:cdf="http://checklists.nist.gov/xccdf/1.2"
75 >
76
77 <xsl:import href="legacy-xccdf-share.xsl" />
78
79 <xsl:output method="text" encoding="UTF-8"/>
80
81 <!-- params -->
82 <xsl:param name='result-id'/>
83 <xsl:param name='profile' select='string(/cdf:Benchmark/cdf:TestResult[@id=$result-id]/cdf:profile/@idref)'/>
84 <xsl:param name='template' select='"bash"'/>
85
86 <!-- variables -->
87 <xsl:variable name='tplfile'>
88 <xsl:message>legacy-fix.xsl has been deprecated!</xsl:message>
89 <xsl:choose>
90 <xsl:when test='contains($template, ".")'>
91 <xsl:if test='not(contains($template, "://") or starts-with($template, "/")) and $pwd'><xsl:value-of select='$pwd'/>/</xsl:if><xsl:value-of select='$template'/>
92 </xsl:when>
93 <xsl:otherwise>legacy-fixtpl-<xsl:value-of select='$template'/>.xml</xsl:otherwise>
94 </xsl:choose>
95 </xsl:variable>
96
97 <xsl:variable name='tpl' select='document($tplfile)'/>
98 <xsl:variable name='sys' select='concat(" ", $tpl/tpl:fixtemplate/@system, " ")'/>
99
100 <xsl:variable name='dummy'>
101 <xsl:if test='not($tpl)'><xsl:message terminate='yes'>Fix template '<xsl:value-of select='$template'/>' failed to load. Aborting.</xsl:message></xsl:if>
102 </xsl:variable>
103
104 <xsl:variable name='benchmark' select='$root'/>
105 <xsl:variable name='testresult' select='$root/cdf:TestResult[@id=$result-id]'/>
106 <xsl:variable name='dummy2'>
107 <xsl:if test='$result-id and not($testresult)'><xsl:message terminate='yes'>No TestResult '<xsl:value-of select='$result-id'/>'. Aborting.</xsl:message></xsl:if>
108 </xsl:variable>
109
110 <!-- keys -->
111 <xsl:key name='item' match='cdf:Rule|cdf:Group|cdf:Value' use='@id'/>
112
113 <!-- templates -->
114 <xsl:template match='/'>
115 <xsl:call-template name='tpl.elem'><xsl:with-param name='elem' select='"before"'/></xsl:call-template>
116 <xsl:choose>
117 <xsl:when test='$testresult'>
118 # Generating fixes for all failed rules in test result '<xsl:value-of select="$result-id"/>'.
119
120 <xsl:apply-templates select='$testresult/cdf:rule-result[cdf:result="fail"]'/></xsl:when>
121 <xsl:otherwise>
122 # Generating fixes for all Rules that are selected in the default profile.
123
124 <xsl:apply-templates select='$root//cdf:Rule[@selected="true" or @selected="1"]'/></xsl:otherwise>
125 </xsl:choose>
126 <xsl:call-template name='tpl.elem'><xsl:with-param name='elem' select='"after"'/></xsl:call-template>
127 </xsl:template>
128
129 <xsl:template match='cdf:Rule'>
130 <xsl:variable name='fix' select='cdf:fix[contains($sys, concat(" ", @system, ""))][1]'/>
131 <xsl:variable name='val'><xsl:apply-templates mode='text' select='$fix/node()'/></xsl:variable>
132
133 <xsl:if test='$fix'>
134 <xsl:choose>
135 <xsl:when test='count($fix//cdf:instance) != 0'>
136 <xsl:variable name='msg'>Skipping fix generation for rule <xsl:value-of select='@id'/> because it contains instance substitutions</xsl:variable>
137 <xsl:message><xsl:value-of select='$msg'/></xsl:message>
138 <xsl:call-template name='linecomment'><xsl:with-param name='content' select='$msg'/></xsl:call-template>
139 </xsl:when>
140 <xsl:otherwise>
141 <xsl:call-template name='tpl.elem'>
142 <xsl:with-param name='elem' select='"fixentry"'/>
143 <xsl:with-param name='val' select='string($val)'/>
144 <xsl:with-param name='fix' select='$fix'/>
145 <xsl:with-param name='rule' select='.'/>
146 </xsl:call-template>
147 </xsl:otherwise>
148 </xsl:choose>
149 </xsl:if>
150 </xsl:template>
151
152 <xsl:template match='cdf:rule-result'>
153 <xsl:variable name='rule' select='key("item", @idref)'/>
154 <xsl:variable name='fix' select='(cdf:fix|$rule/cdf:fix)[contains($sys, concat(" ", @system, ""))][last()]'/>
155 <xsl:variable name='val'><xsl:apply-templates mode='text' select='$fix/node()'/></xsl:variable>
156
157 <xsl:if test='$fix'>
158 <xsl:call-template name='tpl.elem'>
159 <xsl:with-param name='elem' select='"fixentry"'/>
160 <xsl:with-param name='val' select='string($val)'/>
161 <xsl:with-param name='fix' select='$fix'/>
162 <xsl:with-param name='rule' select='$rule'/>
163 <xsl:with-param name='result' select='.'/>
164 </xsl:call-template>
165 </xsl:if>
166 </xsl:template>
167
168 <xsl:template name='linecomment'>
169 <xsl:param name='content'/>
170 <xsl:variable name='cnt' select='normalize-space($content)'/>
171 <xsl:variable name='commentbeg' select='$tpl/tpl:fixtemplate/tpl:linecomment[1]'/>
172 <xsl:if test='$cnt and $commentbeg'>
173 <xsl:value-of select='$commentbeg'/><xsl:value-of select='$cnt'/><xsl:text>&#x0a;</xsl:text>
174 </xsl:if>
175 </xsl:template>
176
177 <xsl:template match='text()'><xsl:value-of select='normalize-space(.)'/></xsl:template>
178
179 <!-- fixing template execution engine -->
180 <xsl:template name='tpl.elem'>
181 <xsl:param name='elem'/>
182 <xsl:param name='val'/>
183 <xsl:param name='fix'/>
184 <xsl:param name='rule'/>
185 <xsl:param name='result'/>
186
187 <xsl:apply-templates mode='fixtpl' select='$tpl/tpl:fixtemplate/tpl:*[local-name()=$elem][1]'>
188 <xsl:with-param name='val' select='$val'/>
189 <xsl:with-param name='fix' select='$fix'/>
190 <xsl:with-param name='rule' select='$rule'/>
191 <xsl:with-param name='result' select='$result'/>
192 </xsl:apply-templates>
193 </xsl:template>
194
195 <xsl:template mode='fixtpl' match='tpl:*'>
196 <xsl:param name='val'/>
197 <xsl:param name='rule'/>
198 <xsl:param name='fix'/>
199 <xsl:param name='result'/>
200 <xsl:apply-templates mode='fixtpl' select='node()'>
201 <xsl:with-param name='val' select='$val'/>
202 <xsl:with-param name='rule' select='$rule'/>
203 <xsl:with-param name='fix' select='$fix'/>
204 <xsl:with-param name='result' select='$result'/>
205 </xsl:apply-templates>
206 </xsl:template>
207
208 <xsl:template mode='fixtpl' match='tpl:sub'>
209 <xsl:param name='val'/>
210 <xsl:param name='rule'/>
211 <xsl:param name='fix'/>
212 <xsl:param name='result'/>
213 <xsl:value-of select='dyn:evaluate(@select)'/>
214 </xsl:template>
215
216 <xsl:template mode='fixtpl' match='tpl:if'>
217 <xsl:param name='val'/>
218 <xsl:param name='rule'/>
219 <xsl:param name='fix'/>
220 <xsl:param name='result'/>
221 <xsl:if test='dyn:evaluate(@test)'>
222 <xsl:apply-templates mode='fixtpl' select='node()'>
223 <xsl:with-param name='val' select='$val'/>
224 <xsl:with-param name='rule' select='$rule'/>
225 <xsl:with-param name='fix' select='$fix'/>
226 <xsl:with-param name='result' select='$result'/>
227 </xsl:apply-templates>
228 </xsl:if>
229 </xsl:template>
230
231 <xsl:template mode='fixtpl' match='text()'><xsl:value-of select='.'/></xsl:template>
232
233
234 </xsl:stylesheet>