summaryrefslogtreecommitdiffstats
path: root/update_windows_man_page.sh
blob: 68750305f94157d96482be6d921ad2d73ed0361c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash

#
#   The MIT License (MIT)
#
#   Copyright (C) 2021 Joe Testa (jtesta@positronsecurity.com)
#   Copyright (C) 2021 Adam Russell (<adam[at]thecliguy[dot]co[dot]uk>)
#
#   Permission is hereby granted, free of charge, to any person obtaining a copy
#   of this software and associated documentation files (the "Software"), to deal
#   in the Software without restriction, including without limitation the rights
#   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#   copies of the Software, and to permit persons to whom the Software is
#   furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included in
#   all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#   THE SOFTWARE.
#

################################################################################
# update_windows_man_page.sh
#
# PURPOSE
#   Since Windows lacks a manual reader it's necessary to provide an alternative
#   means of reading the man page. 
#
#   This script should be run as part of the ssh-audit packaging process for 
#   Windows. It populates the 'WINDOWS_MAN_PAGE' variable in 'globals.py' with 
#   the contents of the man page. Windows users can then print the content of 
#   'WINDOWS_MAN_PAGE' by invoking ssh-audit with the manual parameters 
#   (--manual / -m).
#
#   Cygwin is required.
#
# USAGE
#   update_windows_man_page.sh [-m <path-to-man-page>] [-g <path-to-globals.py>]
#
################################################################################

function usage {
    echo >&2 "Usage: $0 [-m <path-to-man-page>] [-g <path-to-globals.py>] [-h]"
    echo >&2 "  -m    Specify an alternate man page path (default: ./ssh-audit.1)"
    echo >&2 "  -g    Specify an alternate globals.py path (default: ./src/ssh_audit/globals.py)"
    echo >&2 "  -h    This help message"
}

PLATFORM="$(uname -s)"

# This script is intended for use on Linux and Cygwin only.
case "$PLATFORM" in
    Linux | CYGWIN*) ;;
    *) echo "Platform not supported: $PLATFORM"
	exit 1
	;;
esac

MAN_PAGE=./ssh-audit.1
GLOBALS_PY=./src/ssh_audit/globals.py

while getopts "m: g: h" OPTION
do
    case "$OPTION" in
        m)
            MAN_PAGE="$OPTARG"
            ;;
        g)
            GLOBALS_PY="$OPTARG"
            ;;
        h)
            usage
            exit 0
            ;;
        *)
            echo >&2 "Invalid parameter(s) provided"
            usage
            exit 1
            ;;
    esac
done

# Check that the specified files exist.
[ -f "$MAN_PAGE" ] || { echo >&2 "man page file not found: $MAN_PAGE"; exit 1; }
[ -f "$GLOBALS_PY" ] || { echo >&2 "globals.py file not found: $GLOBALS_PY"; exit 1; }

# Check that the 'ul' (do underlining) binary exists.
if [[ "$PLATFORM" = Linux ]]; then
    command -v ul >/dev/null 2>&1 || { echo >&2 "ul not found."; exit 1; }
fi

# Check that the 'sed' (stream editor) binary exists.
command -v sed >/dev/null 2>&1 || { echo >&2 "sed not found."; exit 1; }

# Reset the globals.py file, in case it was modified from a prior run.
git checkout $GLOBALS_PY > /dev/null 2>&1

# Remove the Windows man page placeholder from 'globals.py'.
sed -i '/^WINDOWS_MAN_PAGE/d' "$GLOBALS_PY"

echo "Processing man page at ${MAN_PAGE} and placing output into ${GLOBALS_PY}..."

# Append the man page content to 'globals.py'.
#   * man outputs a backspace-overwrite sequence rather than an ANSI escape 
#     sequence.
#   * 'MAN_KEEP_FORMATTING' preserves the backspace-overwrite sequence when 
#     redirected to a file or a pipe.
#   * sed converts unicode hyphens into an ASCI equivalent.
#   * The 'ul' command converts the backspace-overwrite sequence to an ANSI 
#     escape sequence. Not required under Cygwin because man outputs ANSI escape 
#     codes automatically.

echo WINDOWS_MAN_PAGE = '"""' >> "$GLOBALS_PY"

if [[ "$PLATFORM" = CYGWIN* ]]; then
	MANWIDTH=80 MAN_KEEP_FORMATTING=1 man "$MAN_PAGE" | sed $'s/\u2010/-/g' >> "$GLOBALS_PY"
else
	MANWIDTH=80 MAN_KEEP_FORMATTING=1 man "$MAN_PAGE" | ul | sed $'s/\u2010/-/g' >> "$GLOBALS_PY"
fi

echo '"""' >> "$GLOBALS_PY"

echo "Done."
exit 0