#!/bin/sh # # A posix sh CGI script (yeah, that's right) to update the daily # code for CharmBypass. This is not useful to you unless you # host CharmBypass on a public web server and are insane. # # Fail on the first error. set -e # The CGI spec says that the current working directory SHOULD be set # to the directory containing the script. Well, that's not always what # happens. It's true in apache... INDEX_HTML=$(pwd)/../index.html # But not in python's server.http [ -f "${INDEX_HTML}" ] || INDEX_HTML=$(pwd)/index.html # Die with an error. The first argument, an HTTP status code, is # returned as the Status. The second argument, an error message, is # printed after setting the Content-type to text/plain. Finally, we # cease execution. die() { echo "Status: ${1}" echo "Content-type: text/plain" echo "" echo "${2}" exit 1 } # Redirect to the given URL; return a 302 status, and send the # necessary location header. redirect() { echo "Status: 302" echo "Location: ${1}" echo "" exit } # Parse and return the daily code from the index.html file. # This relies on "implementation details" within the HTML; # namely, the field name and its value must appear next # to each other on the same line. parse_code() { sed -n \ -e 's/.*name="code" value="\([A-Za-z0-9]\{2\}\)".*/\1/p' \ "${INDEX_HTML}" \ | head -n 1 } # Update (replace) the daily code in the index.html file. The first # argument is the new code to use. This relies on "implementation # details" within the HTML; namely, the field name and its value must # appear next to each other on the same line. update_code() { _new_code="${1}" _find='name="code" value="[a-zA-Z0-9]\{0,2\}"' _replace="name=\"code\" value=\"${_new_code}\"" _tmp=$(mktemp) # The "-i" flag to sed isn't POSIX, and worse, it tries to use a # temp file in CWD. We don't want to make the parent directory # writable because then this script is writable. sed -e "s/${_find}/${_replace}/g" "${INDEX_HTML}" > "${_tmp}" # mv would also recreate the file here, so cp/rm instead. cp "${_tmp}" "${INDEX_HTML}" rm "${_tmp}" } # Display the "update code" form and exit. display_code_form() { echo "Content-type: text/html" echo "" cat <<-EOF