summaryrefslogtreecommitdiff
path: root/httprecv.sh
blob: ec5032722d6dbbd13625cd6a16b3d42abb80e4ac (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297

#!/bin/sh -eu
#
#  httprecv -- A simple shell script that receives a file over HTTP
#
#  Copyright (c) 2019 Samuel Lidén Borell <samuel@kodafritt.se>
#
#  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.
#

export LC_ALL=C.UTF-8

log() {
    level="$1"
    message="$2"
    date="$(date --rfc-3339=seconds)"
    printf '%s\n' "$date [$level] $message" >&2
}

if [ $# = 0 ]; then
    port=1111
    log WARNING "This script does NOT offer any security at all!"
    # Determine IP address, so we can show a IP
    # FIXME this is IPv4 only for now, but this script is intended for LAN use only
    # example.com = 93.184.216.34
    ip=$(ip route get 93.184.216.34 | grep -oE 'src [^ \t]+' | cut -c '5-' || true)
    urltext="${ip:+, URL is http://$ip:$port/}"
    log INFO "Listening at port $port$urltext"
    log INFO "Uploaded files will be saved as \"$PWD/upload.bin\""
    while nc.traditional -c "'$0' --internal-recv" -n -l -p "$port"; do
        true
    done
    exit
elif [ $# != 1 ] || [ "$1" != --internal-recv ]; then
    cat <<EOF
usage: $0

Listens on port 1111 and presents a file upload form. Files received will
be saved as "upload.bin" in the current directory.

Note: This script is intended for transferring files over a firewalled
network (or a secure point-to-point network link). It does not offer any
security at all, and it does not handle multiple users well.
EOF
    [ "$1" = --help ]
    exit
fi

cr=$(printf '\r')

resp_header() {
    respcode="$1"
    statusline="$2"
    cat <<EOF
HTTP/1.0 $respcode $statusline$cr
Content-Type: text/html; charset=UTF-8$cr
X-Frame-Options: DENY$cr
X-Content-Type-Options: nosniff$cr
Content-Security-Policy: default-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'$cr
$cr
EOF
}

html_head() {
    title="$1"
    cat <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>$title</title>
<style type="text/css">
html, body { background: Window; color: WindowText; font: 90% sans-serif; }
form { width: 25em; margin: 1em auto; padding: 1.5em; background: #fff; color: #000; }
h2 { border-bottom: 1px solid #000; }
h2.err { border: none; text-align: center; margin-top: 2em; }
h2.err::before { content: "⛔ "; }
input { padding: 0.5em; display: block; width: auto; }
#st { text-align: center; font-weight: bold; }
@media(max-width: 35em) {
  html, body, form { margin: 0; }
  form { background: transparent; color: inherit; }
  form, input { display: block; width: auto; }
  p { margin-bottom: 2em; }
  h2 { text-align: center; border: none; }
}
@media(pointer: coarse) {
  p { margin-bottom: 2em; }
  input { padding: 1.5em; }
}
@media(max-width: 10cm) {
  p, div, input { font-size: 100%; font-weight: bold; }
}
</style>
</head>
<body>
EOF
}

html_footer() {
cat <<EOF
</body>
</html>
EOF
}

resp_message() {
    respcode="$1"
    statusline="$2"
    resp_header "$respcode" "$statusline"
    html_head "$statusline"
    echo "<h2 class=\"err\">$statusline</h2>"
    html_footer
}

skip_headers() {
    while read -r line; do
        line=${line%$cr}
        [ -n "$line" ] || break
    done
}

read_multipart_boundary() {
    boundary="$1"
    read -r line
    line="${line%$cr}"
    if [ "$line" != "--$boundary" ]; then
        resp_message 400 "Bad request"
        echo "Error: Unexpected multipart boundary from client." >&2
        exit 1
    fi
}

read -r reqline
without_get=${reqline#GET }
without_get=${without_get#get }
without_head=${reqline#HEAD }
without_head=${without_head#head }
without_post=${reqline#POST }
without_post=${without_post#post }
if [ "${without_get}" != "$reqline" ]; then
    # GET request
    method=GET
    url_ver=$without_get
elif [ "${without_head}" != "$reqline" ]; then
    # HEAD request
    method=HEAD
    url_ver=$without_head
elif [ "${without_post}" != "$reqline" ]; then
    # POST request
    method=POST
    url_ver=$without_post
else
    skip_headers
    resp_message 405 'Method not allowed'
    exit
fi

if [ "${url_ver#/ }" = "$url_ver" ]; then
    skip_headers
    resp_message 404 'Incorrect URL'
    exit
fi

# Read headers
content_type=""
content_type_boundary=""
content_length=""
while read -r hdr; do
    hdr="${hdr%$cr}"
    [ -n "$hdr" ] || break
    hdrlower=$(printf %s "$hdr" | tr '[:upper:]' '[:lower:]')
    hdrname=$(printf %s "$hdrlower" | cut -d ':' -f 1 | tr -s ' \t')
    hdrname=${hdrname% }
    hdrvalue=$(printf %s "$hdr" | cut -d ':' -f 2- | tr -s ' \t')
    hdrvalue=${hdrvalue# }
    case "$hdrname" in
        content-length)
            tmp=$(printf %s "$hdrvalue" | tr -cd 0123456789)
            if [ "$tmp" = "$hdrvalue" ]; then
                content_length=$tmp
            fi
            unset tmp;;
        content-type)
            content_type=$(printf %s "$hdrvalue" | cut -d ';' -f 1 | tr -s ' \t' | tr '[:upper:]' '[:lower:]')
            remaining="$hdrvalue"
            while true; do
                remaining=$(printf %s "$remaining" | cut -sd ';' -f 2- | tr -s ' \t')
                remaining=${remaining# }
                [ -n "$remaining" ] || break
                if [ "${remaining#boundary=}" != "$remaining" ]; then
                    content_type_boundary=$(printf %s "${remaining#boundary=}" | cut -d ';' -f 1 | tr -s ' \t')
                fi
            done
            unset remaining;;
    esac
done

if [ "$method" = HEAD ]; then
    cat <<EOF
HTTP/1.0 200 Ok$cr
Content-Type: text/html; charset=UTF-8$cr
X-Frame-Options: DENY$cr
X-Content-Type-Options: nosniff$cr
$cr
EOF
    exit
fi

# Handle POST
if [ "$method" = POST ] && [ "$content_length" = 0 ]; then
    uploadstatus='Error: No data sent from browser'
elif [ "$method" = POST ]; then
    log INFO "Receiving file data ($content_length bytes, including multipart headers)" # header is sanitized, so this is OK
    uploadstatus='Upload failed'
    if [ "$content_type" != multipart/form-data ]; then
        resp_message 415 'Unsupported form encoding'
        exit
    fi
    uploadstatus='Error: Upload was malformed'
    if [ -n "$content_length" ]; then
        head -c "$content_length" 2>/dev/null
    else
        cat
    fi | {
        read_multipart_boundary "$content_type_boundary"
        part_length=""
        while read -r line; do
            line="${line%$cr}"
            [ -n "$line" ] || break
            hdrlower=$(printf %s "$hdr" | tr '[:upper:]' '[:lower:]')
            hdrname=$(printf %s "$hdrlower" | cut -d ':' -f 1 | tr -s ' \t')
            hdrname=${hdrname% }
            hdrvalue=$(printf %s "$hdr" | cut -d ':' -f 2- | tr -s ' \t')
            hdrvalue=${hdrvalue# }
            case "$hdrname" in
                content-length)
                    tmp=$(printf %s "$hdrvalue" | tr -cd 0123456789)
                    if [ "$tmp" = "$hdrvalue" ]; then
                        part_length=$tmp
                    fi
                    unset tmp;;
                content-disposition)
                    true # TODO
                    ;;
            esac
        done
#        if [ -z "$part_length" ]; then
#            # TODO
#            true
#        else
#            dd of=upload.bin bs=1 count="$part_length"
#        fi
#        read_multipart_boundary "$content_type_boundary--"
        boundary_length=$(printf '%s' "RN--$boundary--RN" | wc -c) # include space for CR-LF before and after
        head -c -"$boundary_length" > upload.bin # FIXME this does not check the final multipart boundary
    }
    # TODO check for empty file and show a different message
    uploadstatus='File was uploaded successfully!'
    log INFO "$uploadstatus"
    unset line
else
    log INFO "Sending start page"
    uploadstatus=''
fi

title=${uploadstatus:-File upload}
statusdiv=${uploadstatus:+<div id="st">$uploadstatus</div>}
# Show form
resp_header 200 Ok
html_head "$title"
cat <<EOF
<form enctype="multipart/form-data" method="POST">
<h2>Upload file</h2>
<p><input type="file" name="filedata"></p>
<p><input type="submit" value="Upload file" onclick="if (document.getElementById) { var st = document.getElementById('st'); st.style.display = 'none'; } return true"></p>
$statusdiv
</form>
EOF
html_footer