]> git.plutz.net Git - cgilite/commitdiff
various refinements in new modules
authorPaul Hänsch <paul@plutz.net>
Sun, 18 Nov 2018 20:59:01 +0000 (21:59 +0100)
committerPaul Hänsch <paul@plutz.net>
Sun, 18 Nov 2018 20:59:01 +0000 (21:59 +0100)
logging.sh
session.sh

index f10ea4c43283842e4e188ec0f0306672caf9f96e..31bb24d3c4d3b9abbcb7bb175bcd455d5a078d2c 100755 (executable)
@@ -12,14 +12,17 @@ LOGLEVEL="${LOGLEVEL:-3}"
 LOGFILE="${LOGFILE:-/dev/stderr}"
 
 logmsg(){
-  if [ "${1:-3}" -le "$LOGLEVEL" ]; then
-    [ "$#" -gt 1 ] \
-    && printf %s\\n "$*" >>"${LOGFILE}" \
-    || tee -a "${LOGFILE}"
+  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
index 3f3839ae8b167975a5e5231582509c5484d1f025..60ae57734a6b1e659460139c7b1b497fae5d866b 100755 (executable)
@@ -5,7 +5,7 @@ include_session="$0"
 
 server_key(){
   IDFILE="${IDFILE:-${_DATA:-.}/serverkey}"
-  if ! grep -m1 -xE '.{512}' "$IDFILE"; then
+  if [ "$(stat -c %s "$IDFILE")" -ne 512 ] || ! cat "$IDFILE"; then
     dd count=1 bs=512 if=/dev/urandom \
     | tee "$IDFILE"
   fi 2>&-
@@ -22,8 +22,8 @@ slopecode(){
 }
 
 randomid(){
-  dd bs=12 count=1 if=/dev/urandom \
-  | slopecode 2>&-
+  dd bs=12 count=1 if=/dev/urandom 2>&- \
+  | slopecode
 }
 
 timeid(){
@@ -35,8 +35,8 @@ timeid(){
         $((d / 256 % 256)) \
         $((d % 256))
     )"
-    dd bs=8 count=1 if=/dev/urandom
-  } | slopecode 2>&-
+    dd bs=8 count=1 if=/dev/urandom 2>&-
+  } | slopecode
 }
 
 checkid(){ grep -m 1 -xE '[0-9a-zA-Z:_]{16}'; }
@@ -53,7 +53,11 @@ update_session(){
   checksig="${checksig%% *}"
   d=$(date +%s)
   
-  if [ "$checksig" != "$sig" -o "$time" -lt "$d" ] 2>&-; then
+  if [ "$checksig" != "$sig" \
+    -o "$time" -lt "$d" \
+    -o ! "$(printf %s "$sid" |checkid)" ] 2>&-
+  then
+    debug Setting up new session
     sid="$(randomid)"
   fi
 
@@ -64,5 +68,5 @@ update_session(){
 }
 
 SESSION_ID="$(update_session)"
-SET_COOKIE 0 "session=$SESSION_ID" HttpOnly
+SET_COOKIE 0 session="$SESSION_ID" Path=/ SameSite=Strict HttpOnly
 SESSION_ID="${SESSION_ID%%-*}"