iptables.xslt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!-- Converts from simple xml iptables format to iptables-save format
  3. Copyright 2006 UfoMechanic
  4. Author: azez@ufomechanic.net
  5. This code is distributed and licensed under the terms of GNU GPL v2
  6. This sample usage outputs roughly want goes in
  7. iptables-save | iptables-xml -c | xsltproc iptables.xslt -
  8. -->
  9. <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  10. <xsl:output method = "text" />
  11. <xsl:strip-space elements="*" />
  12. <!-- output conditions of a rule but not an action -->
  13. <xsl:template match="iptables-rules/table/chain/rule/conditions/*">
  14. <!-- <match> is the psuedo module when a match module doesn't need to be loaded
  15. and when -m does not need to be inserted -->
  16. <xsl:if test="name() != 'match'">
  17. <xsl:text> -m </xsl:text><xsl:value-of select="name()"/>
  18. </xsl:if>
  19. <xsl:apply-templates select="node()"/>
  20. </xsl:template>
  21. <!-- delete the actions or conditions containers, and process child nodes -->
  22. <xsl:template match="iptables-rules/table/chain/rule/actions|table/chain/rule/conditions">
  23. <xsl:apply-templates select="*"/>
  24. </xsl:template>
  25. <xsl:template match="iptables-rules/table/chain/rule/actions/goto">
  26. <xsl:text> -g </xsl:text>
  27. <xsl:apply-templates select="*"/>
  28. <xsl:text>&#xA;</xsl:text>
  29. </xsl:template>
  30. <xsl:template match="iptables-rules/table/chain/rule/actions/call">
  31. <xsl:text> -j </xsl:text>
  32. <xsl:apply-templates select="*"/>
  33. <xsl:text>&#xA;</xsl:text>
  34. </xsl:template>
  35. <!-- all other actions are module actions -->
  36. <xsl:template match="iptables-rules/table/chain/rule/actions/*">
  37. <xsl:text> -j </xsl:text><xsl:value-of select="name()"/>
  38. <xsl:apply-templates select="*"/>
  39. <xsl:text>&#xA;</xsl:text>
  40. </xsl:template>
  41. <!-- all child action nodes -->
  42. <xsl:template match="iptables-rules/table/chain/rule/actions//*|iptables-rules/table/chain/rule/conditions//*" priority="0">
  43. <xsl:if test="@invert=1"><xsl:text> !</xsl:text></xsl:if>
  44. <xsl:text> -</xsl:text>
  45. <!-- if length of name is 1 character, then only do 1 - not 2 -->
  46. <xsl:if test="string-length(name())&gt;1">
  47. <xsl:text>-</xsl:text>
  48. </xsl:if>
  49. <xsl:value-of select="name()"/>
  50. <xsl:text> </xsl:text>
  51. <xsl:apply-templates select="node()"/>
  52. </xsl:template>
  53. <xsl:template match="iptables-rules/table/chain/rule/actions/call/*|iptables-rules/table/chain/rule/actions/goto/*">
  54. <xsl:value-of select="name()"/>
  55. <!-- I bet there are no child nodes, should we risk it? -->
  56. <xsl:apply-templates select="node()"/>
  57. </xsl:template>
  58. <!-- output the head of the rule, and any conditions -->
  59. <xsl:template name="rule-head">
  60. <xsl:if test="string-length(@packet-count)+string-length(@byte-count)">
  61. <xsl:call-template name="counters"><xsl:with-param name="node" select="."/></xsl:call-template>
  62. <xsl:text> </xsl:text>
  63. </xsl:if>
  64. <xsl:text>-A </xsl:text><!-- a rule must be under a chain -->
  65. <xsl:value-of select="../@name" />
  66. <xsl:apply-templates select="conditions"/>
  67. </xsl:template>
  68. <!-- Output a single rule, perhaps as multiple rules if we have more than one action -->
  69. <xsl:template match="iptables-rules/table/chain/rule">
  70. <xsl:choose>
  71. <xsl:when test="count(actions/*)&gt;0">
  72. <xsl:for-each select="actions/*">
  73. <!-- and a for-each to re-select the rule as the current node, to write the rule-head -->
  74. <xsl:for-each select="../..">
  75. <xsl:call-template name="rule-head"/>
  76. </xsl:for-each>
  77. <!-- now write the this action -->
  78. <xsl:apply-templates select="."/>
  79. </xsl:for-each>
  80. </xsl:when>
  81. <xsl:otherwise>
  82. <!-- no need to loop if there are no actions, just output conditions -->
  83. <xsl:call-template name="rule-head"/>
  84. <xsl:text>&#xA;</xsl:text>
  85. </xsl:otherwise>
  86. </xsl:choose>
  87. </xsl:template>
  88. <xsl:template match="iptables-rules/table">
  89. <xsl:text># Generated by iptables.xslt&#xA;</xsl:text>
  90. <xsl:text>*</xsl:text><xsl:value-of select="@name"/><xsl:text>&#xA;</xsl:text>
  91. <!-- Loop through each chain and output the chain header -->
  92. <xsl:for-each select="chain">
  93. <xsl:text>:</xsl:text>
  94. <xsl:value-of select="@name"/>
  95. <xsl:text> </xsl:text>
  96. <xsl:choose>
  97. <xsl:when test="not(string-length(@policy))"><xsl:text>-</xsl:text></xsl:when>
  98. <xsl:otherwise><xsl:value-of select="@policy"/></xsl:otherwise>
  99. </xsl:choose>
  100. <xsl:text> </xsl:text>
  101. <xsl:call-template name="counters"><xsl:with-param name="node" select="."/></xsl:call-template>
  102. <xsl:text>&#xA;</xsl:text>
  103. </xsl:for-each>
  104. <!-- Loop through each chain and output the rules -->
  105. <xsl:apply-templates select="node()"/>
  106. <xsl:text>COMMIT&#xA;# Completed&#xA;</xsl:text>
  107. </xsl:template>
  108. <xsl:template name="counters">
  109. <xsl:param name="node"/>
  110. <xsl:text>[</xsl:text>
  111. <xsl:if test="string-length($node/@packet-count)"><xsl:value-of select="$node/@packet-count"/></xsl:if>
  112. <xsl:if test="string-length($node/@packet-count)=0">0</xsl:if>
  113. <xsl:text>:</xsl:text>
  114. <xsl:if test="string-length($node/@byte-count)"><xsl:value-of select="$node/@byte-count"/></xsl:if>
  115. <xsl:if test="string-length($node/@byte-count)=0">0</xsl:if>
  116. <xsl:text>]</xsl:text>
  117. </xsl:template>
  118. <!-- the bit that automatically recurses for us, NOTE: we use * not node(), we don't want to copy every white space text -->
  119. <xsl:template match="@*|node()">
  120. <xsl:copy>
  121. <!-- with libxslt xsltproc we can't do @*|node() or the nodes may get processed before the attributes -->
  122. <xsl:apply-templates select="@*"/>
  123. <xsl:apply-templates select="node()"/>
  124. </xsl:copy>
  125. </xsl:template>
  126. </xsl:transform>