#!/bin/cat

#? NAME
#?      filter_examples  - examples for formatting o-saft.pl's output
#?
#? DESCRIPTION
#?      This file contains examples for more sophisticated convertions of
#?      o-saft.pl's output.
#?      All examples should work just by copy&paste.
#?
#? LIMITATIONS
#?      Some of the provided filter scripts are awk scripts. They most likely
#?      require Gnu awk (or compatible) instead of the traditional AT&T awk.
#?
#?      Depending on the operating system and/or it's configuration,  the awk
#?      scripts need to be used like:
#?           gawk -f usr/script
#?      instead of (as shown in our examples):
#?           usr/script
#?

###############################################################################
###
### Using filters (overview)
###

o-saft.pl +cipher localhost | usr/bunt.pl
o-saft.pl +info   localhost | usr/HTML-simple.awk
o-saft.pl +check  localhost | usr/HTML-table.awk
o-saft.pl +info   localhost --tracekey --tab | usr/JSON-array.awk 
o-saft.pl +info   localhost --tracekey --tab | usr/JSON-struct.awk 
o-saft.pl +check  localhost --tracekey --tab | usr/JSON-array.awk 
o-saft.pl +info   localhost | usr/XML-value.awk
o-saft.pl +check  localhost | usr/XML-attribute.awk


###############################################################################
###
### CSV convertions
###

### +info output: separate label and value, one per line

o-saft.pl +info --legacy=quick localhost \
| gawk -F'\t' '/^\s*$/{next}($1~/^[#=]/){next}{print $1","$2}'

# or

o-saft.pl +info localhost --legacy=compact --no-header --sep=,


###############################################################################
###
### HTML convertions
###

### +info output: label and value per line (full html)

o-saft.pl +info localhost | usr/HTML-simple.awk

# or

o-saft.pl +info --legacy=quick localhost \
| gawk 'BEGIN{FS="\t";print "<html><body><table>"}(NF>0){gsub(/&/,"\\&amp;");gsub(/"/,"\\&quot;");gsub(/</,"\\&lt;");gsub(/>/,"\\&gt;");}/^\s*$/{next}($1~/^[#=]/){print "<! "$0" -->";next}{print " <tr><th>"$1"</th><td>"$2"</td></tr>"}END{print "</table></body></html>"}'

### +info output: label and value per line (html table lines only)

o-saft.pl +info --legacy=quick localhost \
| gawk -F'\t' '/^\s*$/{next}(NF>0){gsub(/&/,"\\&amp;");gsub(/</,"\\&lt;");gsub(/>/,"\\&gt;");}($1~/^[#=]/){print "<! "$0" -->";next}{print " <tr><th>"$1"</th><td>"$2"</td></tr>"}'

### +check output: label and value in table; header lines

o-saft.pl +check localhost | usr/HTML-table.awk

# or

o-saft.pl +check --legacy=quick localhost \
| gawk 'BEGIN{FS="\t";print "<html><body><table>"}(NF>0){gsub(/&/,"\\&amp;");gsub(/</,"\\&lt;");gsub(/>/,"\\&gt;");}/^\s*$/{next}($1~/ reading /){next;}($1~/^===/ && $NF~/===/){printf("</table><h2>%s</h2>\n<table>",$0);next}($1~/^== /){print "<tr><th colspan=2>"$0"</th></tr>";next;}($1~/^[#=]/){print "<! "$0" -->";next}{print " <tr><th>"$1"</th><td>"$2"</td></tr>"}END{print "</table></body></html>"}'


###############################################################################
###
### JSON convertions
###

### +info output: label and value per line in JSON style array

o-saft.pl +info --legacy=quick localhost \
| gawk 'BEGIN{FS="\t";print "info=["}(NF>0){gsub(/\\/,"&&");gsub(/"/,"\\\"");}/^\s*$/{next}($1~/^[#=]/){print "// "$0;next}{printf(" \"%s\": \"%s\",\n",$1,$NF)}END{print " dumm:\"dumm\"\n];"}'

o-saft.pl +info --tracekey --tab localhost | usr/JSON-array.awk 
o-saft.pl +info --tracekey --tab localhost | usr/JSON-struct.awk 

###############################################################################
###
### XML convertions
###

### +info output: label and value per line

o-saft.pl +info localhost | usr/XML-value.awk

# or

o-saft.pl +info --legacy=quick localhost \
| gawk 'BEGIN{FS="\t";print "<infos xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"SSLResults.xsd\" >"}(NF>0){gsub(/&/,"\\&amp;");gsub(/</,"\\&lt;");gsub(/>/,"\\&gt;");}/^\s*$/{next}($1~/^[#=]/){print "<! "$0" -->";next}{print " <info>\n  <label>"$1"</label>\n  <value>"$2"</value>\n </info>"}END{print "</info>"}'


### +info output: one line per label and value

o-saft.pl +info localhost \
| gawk 'BEGIN{FS="\t";print "<infos xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"SSLResults.xsd\" >"}(NF>0){gsub(/&/,"\\&amp;");gsub(/</,"\\&lt;");gsub(/>/,"\\&gt;");}/^\s*$/{next}($1~/^[#=]/){print "<! "$0" -->";next}{print " <info><label>"$1"</label><value>"$2"</value></info>"}END{print "</info>"}'

# same as before a bit more compact

o-saft.pl +info --legacy=quick localhost \
| gawk 'BEGIN{FS="\t";print "<infos xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"SSLResults.xsd\" >"}(NF>0){gsub(/&/,"\\&amp;");gsub(/</,"\\&lt;");gsub(/>/,"\\&gt;");}/^\s*$/{next}($1~/^[#=]/){next}{print "<info><label>"$1"</label><value>"$2"</value></info>"}END{print "</info>"}'


### +info output: one line per label and value as attribute

o-saft.pl +info localhost | usr/XML-attribute.awk

# or

o-saft.pl +info --legacy=quick localhost \
| gawk 'BEGIN{FS="\t";print "<infos xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"SSLResults.xsd\" >"}(NF>0){gsub(/&/,"\\&amp;");gsub(/"/,"\\&quot;");gsub(/</,"\\&lt;");gsub(/>/,"\\&gt;");}/^\s*$/{next}($1~/^[#=]/){print "<! "$0" -->";next}{i++;printf(" <info id=\"%s\" label=\"%s\" value=\"%s\" />\n",i,$1,$2);}END{print "</info>"}'


###############################################################################
###
### Parsable SSL protocol in output
###

### +cipher output: cipher lines with SSL protocol version

o-saft.pl +cipher localhost --header --legacy=sslaudit
o-saft.pl +cipher localhost --header --legacy=ssldiagnose
o-saft.pl +cipher localhost --header --legacy=full


### +cipher output: all lines are prefixed with SSL protocol version

o-saft.pl +cipher localhost --header --legacy=quick \
| awk '/^=== Ciphers:/{p=$(NF-1)}/^/{printf"%s\t%s\n",p,$0;}'

### +cipher output: all lines are prefixed with SSL protocol version, no headers

o-saft.pl +cipher localhost --header --legacy=quick \
| awk '/^=== Ciphers:/{p=$(NF-1)}/^=/{next}/^/{printf"%s\t%s\n",p,$0;}'


