]> git.plutz.net Git - serve0/commitdiff
Squashed 'cgilite/' content from commit f0383ee
authorPaul Hänsch <paul@plutz.net>
Mon, 6 Apr 2020 14:42:15 +0000 (16:42 +0200)
committerPaul Hänsch <paul@plutz.net>
Mon, 6 Apr 2020 14:42:15 +0000 (16:42 +0200)
git-subtree-dir: cgilite
git-subtree-split: f0383eeb634ce1ae71bfb409a9bc25d84b6d79e9

cgilite.sh [new file with mode: 0755]
file.sh [new file with mode: 0755]
html-sh.sed [new file with mode: 0755]
logging.sh [new file with mode: 0755]
session.sh [new file with mode: 0755]
storage.sh [new file with mode: 0755]

diff --git a/cgilite.sh b/cgilite.sh
new file mode 100755 (executable)
index 0000000..333334c
--- /dev/null
@@ -0,0 +1,217 @@
+#!/bin/sh
+
+# Copyright 2017 - 2018 Paul Hänsch
+#
+# This is CGIlite.
+# A collection of posix shell functions for writing CGI scripts.
+# 
+# CGIlite is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+# 
+# CGIlite is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+# 
+# You should have received a copy of the GNU Affero General Public License
+# along with CGIlite.  If not, see <http://www.gnu.org/licenses/>. 
+
+# ksh and zsh workaround
+# set -o posix # ksh, not portable
+setopt -o OCTAL_ZEROES 2>&-
+
+CR="\r"
+BR='
+'
+cgilite_timeout=2
+
+HEADER(){
+  # Read value of header line. Use this instead of
+  # referencing HTTP_* environment variables.
+  if [ -n "${cgilite_headers+x}" ]; then
+    printf %s "$cgilite_headers" \
+    | sed -En 's;^'"${1}"': ([^\r]+)\r?$;\1;i; tX; d; :X;p;q;'
+  else
+    eval "printf %s \"\$HTTP_$(printf %s "${1}" |tr a-z A-Z |tr -c A-Z _)\""
+  fi
+}
+
+HEX_DECODE='
+  s;\\;\\\\;g; :HEXDECODE_X; s;%([^0-9A-F]);\\045\1;g; tHEXDECODE_X;
+  # Hexadecimal { %00 - %FF } will be transformed to octal { \000 - \377 } for posix printf
+  s;%[0123].;&\\0;g; s;%[4567].;&\\1;g; s;%[89AB].;&\\2;g; s;%[CDEF].;&\\3;g;
+  s;%[048C][0-7]\\.;&0;g; s;%[048C][89A-F]\\.;&1;g; s;%[159D][0-7]\\.;&2;g; s;%[159D][89A-F]\\.;&3;g;
+  s;%[26AE][0-7]\\.;&4;g; s;%[26AE][89A-F]\\.;&5;g; s;%[37BF][0-7]\\.;&6;g; s;%[37BF][89A-F]\\.;&7;g;
+  s;%.[08](\\..);\10;g; s;%.[19](\\..);\11;g; s;%.[2A](\\..);\12;g; s;%.[3B](\\..);\13;g;
+  s;%.[4C](\\..);\14;g; s;%.[5D](\\..);\15;g; s;%.[6E](\\..);\16;g; s;%.[7F](\\..);\17;g;
+'
+
+HEX_DECODE(){
+  printf -- "$(printf %s "$1" |sed -E "$HEX_DECODE")"
+}
+
+if [ -z "$REQUEST_METHOD" ]; then
+  # no webserver variables means we are running via inetd / ncat
+  # so use builtin web server
+
+  # Use env from inetd as webserver variables
+  REMOTE_ADDR="${TCPREMOTEIP}"
+  SERVER_NAME="${TCPLOCALIP}"
+  SERVER_PORT="${TCPLOCALPORT}"
+
+  # Wait 2 seconds for request or kill connection through watchdog.
+  # Once Request is received the watchdog will be suspended (killed).
+  # At the end of the loop the watchdog will be restarted to enable
+  # timeout for the subsequent request.
+
+  (sleep $cgilite_timeout && kill $$) & cgilite_watchdog=$!
+  while read REQUEST_METHOD REQUEST_URI SERVER_PROTOCOL; do
+    kill $cgilite_watchdog
+    PATH_INFO="$(HEX_DECODE "${REQUEST_URI%\?*}")"
+    QUERY_STRING="${REQUEST_URI#*\?}"
+    cgilite_headers="$(while read -r hl; do [ "${hl%${CR}}" ] && printf '%s\n' "$hl" || break; done )"
+
+    HTTP_CONTENT_LENGTH="$(HEADER Content-Length |grep -xE '[0-9]+')"
+
+    export REMOTE_ADDR SERVER_NAME SERVER_PORT REQUEST_METHOD REQUEST_URI SERVER_PROTOCOL \
+           PATH_INFO QUERY_STRING HTTP_CONTENT_LENGTH
+
+    # Try to serve multiple requests, provided that script serves a
+    # Content-Length header.
+    # Without Content-Length header, connection will terminate after
+    # script.
+
+    cgilite_status='200 OK'; cgilite_response=''; cgilite_cl="Connection: close${CR}";
+    . "$0" | while read -r l; do case $l in
+      Status:*) cgilite_status="${l#Status: }";;
+      Content-Length:*) cgilite_cl="${l}";;
+      $CR) printf '%s %s\r\n%s\n%s\n\r\n' \
+             'HTTP/1.1' "${cgilite_status%${CR}}" \
+             "$cgilite_response" "${cgilite_cl}"
+           cat || kill $$
+           [ "${cgilite_cl#Connection}" = "${cgilite_cl}" ]; exit;;
+      *) cgilite_response="${cgilite_response:+${cgilite_response}${BR}}${l}";;
+    esac; done || exit 0;
+    (sleep $cgilite_timeout && kill $$) & cgilite_watchdog=$!
+  done
+fi
+
+if [ "$REQUEST_METHOD" = POST -a "${HTTP_CONTENT_LENGTH:=${CONTENT_LENGTH:=0}}" -gt 0 ]; then
+  cgilite_post="$(head -c "$HTTP_CONTENT_LENGTH")"
+fi
+
+[ -n "${DEBUG+x}" ] && env
+
+cgilite_count(){
+  printf %s "&$1" \
+  | grep -oE '&'"$2"'=[^&]*' \
+  | wc -l
+}
+
+cgilite_value(){
+  local str="&$1" name="$2" cnt="${3:-1}"
+  while [ $cnt -gt 0 ]; do
+    str=${str#*&${name}=}
+    cnt=$((cnt - 1))
+  done
+  printf -- "$(printf %s "${str%%&*}" |sed -E 's;\+; ;g;'"$HEX_DECODE")"
+}
+
+cgilite_keys(){
+  local str="&$1"
+  while [ "${str#*&}" != "${str}" ]; do
+    str="${str#*&}"
+    printf '%s\n' "${str%%=*}"
+  done \
+  | sort -u
+}
+
+GET(){ cgilite_value "${QUERY_STRING}" $@; }
+GET_COUNT(){ cgilite_count "${QUERY_STRING}" $1; }
+GET_KEYS(){ cgilite_keys "${QUERY_STRING}"; }
+
+POST(){ cgilite_value "${cgilite_post}" $@; }
+POST_COUNT(){ cgilite_count "${cgilite_post}" $1; }
+POST_KEYS(){ cgilite_keys "${cgilite_post}"; }
+
+REF(){ cgilite_value "${HTTP_REFERER#*\?}" $@; }
+REF_COUNT(){ cgilite_count "${HTTP_REFERER#*\?}" $1; }
+REF_KEYS(){ cgilite_keys "${HTTP_REFERER#*\?}"; }
+
+COOKIE(){
+  HEX_DECODE "$(
+    HEADER Cookie \
+    | grep -oE '(^|; ?)'"$1"'=[^;]*' \
+    | sed -En "${2:-1}"'{s;^[^=]+=;;; s;\+; ;g; p;}'
+  )"
+}
+
+HTML(){
+  # HTML Entity Coding
+  # Prints UTF-8 string as decimal Unicode Code Points
+  # Useful for escaping user input for use in HTML text and attributes
+  { [ $# -eq 0 ] && cat || printf %s "$*"; } \
+  | hexdump -ve '/1 "%03o\n"' \
+  | while read n; do
+    case $n in
+      # bitbanging octal UTF-8 chains into singular 7 digit octal numbers
+      [01]??) printf '0000%s' $n;; # 7 bit ASCII character, nothing to do
+      2??)    printf '%s' ${n#2};; # tail fragment, append 6 bit
+      3[0123]?) printf '000%s' ${n#3};; # 2 octet (11 bit) chain start
+      34?) printf '00%s' ${n#34};; # 3 octet (16 bit) chain start
+      35?) printf '01%s' ${n#35};; # 3 octet chain start, high
+      36?) printf '%s' ${n#36};;   # 4 octet (21 bit) chain start
+    esac
+  done \
+  | sed -E 's;.{7};&\n;g;' \
+  | while read n; do
+    printf '&#%d;' $((0$n))
+  done
+}
+
+URL(){
+  # Code every character in URL escape hex format
+  # except alphanumeric ascii
+
+  { [ $# -eq 0 ] && cat || printf %s "$*"; } \
+  | hexdump -v -e '/1 ",%02X"' \
+  | sed 's;,;%;g; s;%2F;/;g;'
+}
+
+PATH(){
+  { [ $# -eq 0 ] && cat || printf %s "$*"; } \
+  | sed -E 's;^.*$;/&/;; s;/+;/;g;
+            :X;
+            s;^/\.\./;/;; s;/\./;/;g;
+            tX;
+            s;/[^/]+/\.\./;/;;
+            tX;
+            s;^(/.*)/$;\1;'
+}
+
+
+SET_COOKIE(){
+  local expire cookie
+  case "$1" in
+    ''|0|session) expire='';;
+    [+-][0-9]*)   expire="$(date -R -d @$(($(date +%s) + $1)))";;
+    *)            expire="$(date -R -d "$1")";;
+  esac
+  cookie="$2"
+
+  printf 'Set-Cookie: %s' "$cookie"
+  [ -n "$expire" ] && printf '; Expires=%s' "${expire%+????}${expire:+GMT}"
+  [ $# -ge 3 ] && shift 2 && printf '; %s' "$@"
+  printf '\r\n'
+}
+
+REDIRECT(){
+  printf '%s: %s\r\n' \
+    Status "303 See Other" \
+    Content-Length 0 \
+    Location "$*"
+  printf '\r\n'
+  exit 0
+}
diff --git a/file.sh b/file.sh
new file mode 100755 (executable)
index 0000000..51ec245
--- /dev/null
+++ b/file.sh
@@ -0,0 +1,121 @@
+#!/bin/sh
+
+# Copyright 2016 - 2019 Paul Hänsch
+#
+# This file is part of cgilite.
+# 
+# cgilite is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+# 
+# cgilite is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+# 
+# You should have received a copy of the GNU Affero General Public License
+# along with cgilite.  If not, see <http://www.gnu.org/licenses/>. 
+
+[ -n "$include_fileserve" ] && return 0
+include_fileserve="$0"
+
+file_type(){
+  case ${1##*.} in
+    html|html) printf 'text/html';;
+    css)       printf 'text/css';;
+    js)        printf 'text/javascript';;
+    txt)       printf 'text/plain';;
+    sh)        printf 'text/shellscript';;
+    jpg|jpeg)  printf 'image/jpeg';;
+    png)       printf 'image/png';;
+    svg)       printf 'image/svg+xml';;
+    gif)       printf 'image/gif';;
+    webm)      printf 'video/webm';;
+    mp4)       printf 'video/mp4';;
+    ogg)       printf 'audio/ogg';;
+    xml)       printf 'application/xml';;
+    *)         printf 'application/octet-stream';;
+  esac
+}
+
+FILE(){
+  local file file_size file_date http_date cachedate range mime
+  file="$1" mime="$2"
+
+  if ! [ -f "$file" ]; then
+    printf 'Content-Length: 0\r\nStatus: 404 Not Found\r\n\r\n'
+    exit 0
+  elif ! [ -r "$file" ]; then
+    printf 'Content-Length: 0\r\nStatus: 403 Forbidden\r\n\r\n'
+    exit 0
+  fi
+
+  file_size="$(stat -Lc %s "$file")"
+  file_date="$(stat -Lc %Y "$file")"
+  http_date="$(date -uRd @$file_date)"
+  http_date="${http_date%+0000}GMT"
+  cachedate="$(
+    # Parse the allowable date formats from Section 3.3.1 of
+    # https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html
+    HEADER If-Modified-Since \
+    | sed -E 's;^[^ ]+, ([0-9]{2}) (...) ([0-9]{4}) (..:..:..) GMT$;\3-\2-\1 \4;;
+              s;^[^ ]+, ([0-9]{2})-(...)-([789][0-9]) (..:..:..) GMT$;19\3-\2-\1 \4;;
+              s;^[^ ]+, ([0-9]{2})-(...)-([0-6][0-9]) (..:..:..) GMT$;20\3-\2-\1 \4;;
+              s;^[^ ]+ (...) ([0-9]{2}) (..:..:..) ([0-9]{4})$;\4-\1-\2 \3;;
+              s;^[^ ]+ (...)  ([0-9]) (..:..:..) ([0-9]{4})$;\4-\1-\2 \3;;
+              s;Jan;01;; s;Feb;02;; s;Mar;03;; s;Apr;04;; s;May;05;; s;Jun;06;;
+              s;Jul;07;; s;Aug;08;; s;Sep;09;; s;Oct;10;; s;Nov;11;; s;Dec;12;;' \
+    | xargs -r0 date +%s -ud 2>&-
+  )"
+
+  range="$(HEADER Range |sed -nE 's;^bytes=([0-9]+-[0-9]*|-[0-9]+)$;\1;p;q;')"
+  case "$range" in
+    *-) range="${range}$((file_size - 1))";;
+    -*) [ ${range#-} -le $file_size ] \
+        && range="$((file_size - ${range#-}))-$((file_size - 1))" \
+        || range="0-$((file_size - 1))";;
+    *-*) [ ${range#*-} -ge $file_size ] \
+         && range="${range%-*}-$((file_size - 1))";;
+  esac
+
+  if [ "$file_date" -lt "$cachedate" ] 2>&-; then
+    printf '%s: %s\r\n' \
+      Status '304 Not Modified' \
+      Content-Length 0 \
+      Last-Modified "$http_date"
+    printf '\r\n'
+  
+  elif [ -z "$range" ]; then
+    printf '%s: %s\r\n' \
+      Status "200 OK" \
+      Accept-Ranges bytes \
+      Last-Modified "$http_date" \
+      Content-Type "${mime:-$(file_type "$file")}" \
+      Content-Length $file_size
+    printf '\r\n'
+  
+    [ "$REQUEST_METHOD" != HEAD ] && cat "$file"
+
+  elif [ "${range%-*}" -le "${range#*-}" ]; then
+    printf '%s: %s\r\n' \
+      Status "206 Partial Content" \
+      Accept-Ranges bytes \
+      Last-Modified "$http_date" \
+      Content-Type "${mime:-$(file_type "$file")}" \
+      Content-Range "bytes ${range}/${file_size}" \
+      Content-Length "$((${range#*-} - ${range%-*} + 1))"
+    printf '\r\n'
+  
+    [ "$REQUEST_METHOD" != HEAD ] \
+    && tail -c+$((${range%-*} + 1)) "$file" \
+     | head -c "$((${range#*-} - ${range%-*} + 1))"
+
+  elif [ "${range%-*}" -gt "${range#*-}" ]; then
+    printf '%s: %s\r\n' \
+      Status "216 Range Not Satisfiable" \
+      Content-Length 0 \
+      Content-Range \*/${file_size}
+    printf '\r\n'
+  fi
+}
diff --git a/html-sh.sed b/html-sh.sed
new file mode 100755 (executable)
index 0000000..8d7b61c
--- /dev/null
@@ -0,0 +1,69 @@
+#!/bin/sed -nEf
+
+:Escapes
+s,\\\\,\&#92;,g; s,\\&,\&amp;,g;
+s,\\<,\&lt;,g; s,\\>,\&gt;,g;
+s,\\",\&quot;,g; s,\\',\&apos;,g;
+s,\\\[,\&#91;,g; s,\\\],\&#93;,g;
+s,\\\.,\&#46;,g; s,\\#,\&#35;,g;
+s,\\,,g;
+
+:CommentHandle
+x; /^<\/!-->/{
+  x; /--]/{
+    H; s;^(.*)--].*$;\1-->;p;
+    g; s;^.*--]([^\n]*)$;\1;
+    x; s;^</!-->\n(.*)\n[^\n]*$;\1;; x;
+    bCommentEnd
+  }
+  p; b;
+}
+x;
+:CommentEnd
+
+:shortcuts
+s;\[hidden[ \t]+"([^"]*)"[ \t]+"([^"]*)";[input type="hidden" name="\1" value="\2";g;
+s;\[checkbox[ \t]+"([^"]*)"[ \t]+"([^"]*)";[input type="checkbox" name="\1" value="\2";g;
+s;\[radio[ \t]+"([^"]*)"[ \t]+"([^"]*)";[input type="radio" name="\1" value="\2";g;
+s;\[submit[ \t]+"([^"]*)"[ \t]+"([^"]*)";[button type="submit" name="\1" value="\2";g;
+s;\[a[ \t]+"([^"]*)";[a href="\1";g;
+s;\[img[ \t]+"([^"]*)"[ \t]+"([^"]*)";[img src="\1" alt="\2";g;
+
+s;\[!([^]\[]*)\];<!\1>;g;
+s;\[!--([^]\[]*)--\];<!--\1-->;g;
+
+:tags
+s;\[([^]\[< \t]+)([^]\[]*)\];<\1>\2</\1>;g;
+t tags;
+
+G;
+:tagclose
+s;^([^]\n]*)\]([^\n]*)\n([^\n]+);\1\3\2;
+t tagclose;
+h; s;^([^\n]*)\n;;; x; s;\n.*$;;;
+
+:tagopen
+s;^([^\[\n]*)\[([^]\[< \t\n]+)([^\n]*);\1<\2>\3\n</\2>;
+t tagopen;
+G; h; s;^[^\n]*\n+;;; x; s;\n.*$;;;
+
+:attribs
+s;class="([^>]+)>[ \t]*\.([^< \t]+);class="\2 \1>;g; t attribs;
+s;(<[^/][^>]*)>[ \t]*\.([^< \t]+);\1 class="\2">;g;
+s;(<[^/][^>]*)>[ \t]*#([^< \t]+);\1 id="\2">;g;
+s;(<[^/][^>]*)>[ \t]*([^ \t=<]+=("[^"]*"|'[^']*'|[^< \t]*));\1 \2>;g;
+t attribs;
+s;(<input ([^>]+ )?type=(radio|"radio"|'radio')( [^>]+)?)>[ \t]*(checked|selected);\1 checked="checked">;g;
+s;(<input ([^>]+ )?type=(checkbox|"checkbox"|'checkbox')( [^>]+)?)>[ \t]*(checked|selected);\1 checked="checked">;g;
+s;(<option( [^>]+)?)>[ \t]*(checked|selected);\1 selected="selected">;g;
+s;(<select( [^>]+)?)>[ \t]*multiple;\1 multiple="multiple">;g;
+t attribs;
+s;(<[^/][^>]*>)[ \t]*\.[ \t];\1;g;
+
+s;(<[^/][^>]*>)[ \t]*;\1;g;
+# s;(<[^/][^>]*)>[ \t]*</[^>]+>;\1/>;g;
+s;(<(br|hr|img|input|link|meta|area|base|col|command|embed|keygen|param|source|track|wbr)[^>]*)>[ \t]*</\1>;\1>;g;
+
+s;<!-->;<!--;;
+
+p; ${g; s;\n+;;g; p;}
diff --git a/logging.sh b/logging.sh
new file mode 100755 (executable)
index 0000000..31bb24d
--- /dev/null
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+# LOGLEVEL 1: Crash condition
+# LOGLEVEL 2: Unexpected condition
+# LOGLEVEL 3: Failed action (i.e. due to config error)
+# LOGLEVEL 4: Debug
+
+[ -n "$include_logging" ] && return 0
+include_logging="$0"
+
+LOGLEVEL="${LOGLEVEL:-3}"
+LOGFILE="${LOGFILE:-/dev/stderr}"
+
+logmsg(){
+  local ll="${1:-3}"
+  shift 1
+  if [ "$ll" -le "$LOGLEVEL" -a "$#" -gt 0 ]; then
+    printf %s\\n "$*" >>"${LOGFILE}"
+  elif [ "$ll" -le "$LOGLEVEL" ]; then
+    tee -a "${LOGFILE}"
+  elif [ ! "$#" -gt 0 ]; then
+    cat
+  fi
+}
+
+die(){
+  [ "$#" -gt 0 ] && logmsg 1 "$@"
+  exit 1
+}
+panic(){ logmsg 2 "$@"; }
+error(){ logmsg 3 "$@"; }
+debug(){ logmsg 4 "$@"; }
diff --git a/session.sh b/session.sh
new file mode 100755 (executable)
index 0000000..ee5c499
--- /dev/null
@@ -0,0 +1,83 @@
+#!/bin/sh
+
+[ -n "$include_session" ] && return 0
+include_session="$0"
+
+_DATE="$(date +%s)"
+SESSION_TIMEOUT="${SESSION_TIMEOUT:-7200}"
+
+server_key(){
+  IDFILE="${IDFILE:-${_DATA:-.}/serverkey}"
+  if [ "$(stat -c %s "$IDFILE")" -ne 512 ] || ! cat "$IDFILE"; then
+    dd count=1 bs=512 if=/dev/urandom \
+    | tee "$IDFILE"
+  fi 2>&-
+}
+
+slopecode(){
+  # 6-Bit Code that retains sort order of input data, while beeing safe to use
+  # in ascii transmissions, unix file names, HTTP URLs, and HTML attributes
+
+  uuencode -m - | sed '
+    1d;$d; 
+    y;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/;0123456789:=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz;
+  '
+}
+
+randomid(){
+  dd bs=12 count=1 if=/dev/urandom 2>&- \
+  | slopecode
+}
+
+timeid(){
+  d=$(($_DATE % 4294967296))
+  { printf "$(
+      printf \\%o \
+        $((d / 16777216 % 256)) \
+        $((d / 65536 % 256)) \
+        $((d / 256 % 256)) \
+        $((d % 256))
+    )"
+    dd bs=8 count=1 if=/dev/urandom 2>&-
+  } | slopecode
+}
+
+checkid(){ grep -m 1 -xE '[0-9a-zA-Z:=]{16}'; }
+
+transid(){
+  # transaction ID to modify a given file
+  local file="$1"
+  { stat -c %F%i%n%N%s%Y "$file" 2>&-
+    printf %s "$SESSION_ID"
+    server_key
+  } | sha256sum | cut -d\  -f1
+}
+
+update_session(){
+  local session sid time sig serverkey checksig
+
+  IFS=- read -r sid time sig <<-END
+       $(COOKIE session)
+       END
+  serverkey="$(server_key)"
+  
+  checksig="$(printf %s "$sid" "$time" "$serverkey" | sha256sum)"
+  checksig="${checksig%% *}"
+  
+  if ! [ "$checksig" = "$sig" \
+    -a "$time" -ge "$_DATE" \
+    -a "$(printf %s "$sid" |checkid)" ] 2>&-
+  then
+    debug Setting up new session
+    sid="$(randomid)"
+  fi
+
+  time=$(( $_DATE + $SESSION_TIMEOUT ))
+  sig="$(printf %s "$sid" "$time" "$serverkey" |sha256sum)"
+  sig="${sig%% *}"
+  printf %s\\n "${sid}-${time}-${sig}"
+}
+
+SESSION_ID="$(update_session)"
+SET_COOKIE 0 session="$SESSION_ID" Path=/ SameSite=Strict HttpOnly
+SESSION_ID="${SESSION_ID%%-*}"
diff --git a/storage.sh b/storage.sh
new file mode 100755 (executable)
index 0000000..7f70e64
--- /dev/null
@@ -0,0 +1,129 @@
+#!/bin/sh
+
+# Copyright 2018, 2019 Paul Hänsch
+#
+# This is a file format helper, part of CGIlite.
+# 
+# CGIlite is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+# 
+# CGIlite is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+# 
+# You should have received a copy of the GNU Affero General Public License
+# along with CGIlite.  If not, see <http://www.gnu.org/licenses/>. 
+
+[ -n "$include_storage" ] && return 0
+include_storage="$0"
+
+CR="\r"
+BR='
+'
+
+LOCK(){
+  local lock timeout block
+  lock="${1}.lock"
+  timeout="${2-20}"
+  if [ \! -w "${lock%/*}" ] || [ -e "$lock" -a \! -d "$lock" ]; then
+    printf 'Impossible to get lock: %s\n' "$lock" >&2
+    return 1
+  fi
+
+  while ! mkdir "$lock" 2>&-; do
+    block="$(cat "$lock/pid" || printf 1)"
+    if ! { ps -eo pid |grep -qwF "$block"; }; then
+      printf 'Overriding stale lock: %s\n' "$lock" >&2
+      break
+    fi
+    if [ $timeout -le 0 ]; then
+      printf 'Timeout while trying to get lock: %s\n' "$lock" >&2
+      return 1
+    fi
+    timeout=$((timeout - 1))
+    sleep 1
+  done
+  printf '%i\n' $$ >"${lock}/pid"
+  return 0
+}
+
+RELEASE(){
+  local lock
+  lock="${1}.lock"
+  if [ "$(cat "$lock/pid")" = "$$" ]; then
+    rm "$lock/pid"
+    if ! rmdir "$lock"; then
+      printf 'Cannot remove tainted lock: %s\n' "$lock" >&2
+      printf '%i\n' $$ >"${lock}/pid"
+      return 1
+    fi
+    return 0
+  else
+    printf 'Refusing to release foreign lock: %s\n' "$lock" >&2
+    return 1
+  fi
+}
+
+STRING='
+  s;\\;\\\\;g;
+  s;\n;\\n;g;
+  s;\t;\\t;g;
+  s;\r;\\r;g;
+  s;\+;\\+;g;
+  s; ;+;g;
+'
+
+STRING_OLD(){
+  { [ $# -eq 0 ] && cat || printf %s "$*"; } \
+  | sed -E ':X; $!{N;bX;}'"$STRING"
+}
+
+STRING(){
+  local in out=''
+  [ $# -gt 0 ] && in="$*" || in="$(cat)"
+  while [ "$in" ]; do case $in in
+    \\*) out="${out}\\\\"; in="${in#\\}" ;;
+    "$BR"*) out="${out}\\n"; in="${in#${BR}}" ;;
+    "$CR"*) out="${out}\\r"; in="${in#${CR}}" ;;
+    "  "*) out="${out}\\t"; in="${in#  }" ;;
+    +*) out="${out}\\+"; in="${in#+}" ;;
+    " "*) out="${out}+"; in="${in# }" ;;
+    *) out="${out}${in%%[\\${CR}${BR}  + ]*}"; in="${in#"${in%%[\\${BR}${CR}   + ]*}"}" ;;
+  esac; done
+  printf '%s' "$out"
+}
+
+
+UNSTRING='
+  :UNSTRING_X
+  s;((^|[^\\])(\\\\)*)\\n;\1\n;g;
+  s;((^|[^\\])(\\\\)*)\\t;\1\t;g;
+  s;((^|[^\\])(\\\\)*)\\r;\1\r;g;
+  s;((^|[^\\])(\\\\)*)\+;\1 ;g;
+  tUNSTRING_X;
+  s;((^|[^\\])(\\\\)*)\\\+;\1+;g;
+  s;\\\\;\\;g;
+'
+UNSTRING_OLD(){
+  { [ $# -eq 0 ] && cat || printf %s "$*"; } \
+  | sed -E "$UNSTRING"
+}
+UNSTRING(){
+  local in out=''
+  [ $# -gt 0 ] && in="$*" || in="$(cat)"
+  while [ "$in" ]; do case $in in
+    \\\\*) out="${out}\\"; in="${in#\\\\}" ;;
+    \\n*) out="${out}${BR}"; in="${in#\\n}" ;;
+    \\r*) out="${out}${CR}"; in="${in#\\r}" ;;
+    \\t*) out="${out}  "; in="${in#\\t}" ;;
+    \\+) out="${out}+"; in="${in#\\+}" ;;
+    +*) out="${out} "; in="${in#+}" ;;
+    \\*) in="${in#\\}" ;;
+    *) out="${out}${in%%[\\+]*}"; in="${in#"${in%%[\\+]*}"}" ;;
+  esac; done
+  printf '%s' "$out"
+}
+