URI: 
       Create mailboxes - postreich - Unnamed repository; edit this file 'description' to name the repository.
   DIR Log
   DIR Files
   DIR Refs
   DIR README
       ---
   DIR commit 8330df15a5ef86740b64621ddd57ce8060647f18
   DIR parent 0ad10cb546a1b1f266b7c3014960ef28fa8d12f0
  HTML Author: Scarlett McAllister <no+reply@roygbyte.com>
       Date:   Wed, 17 Jan 2024 19:21:23 -0400
       
       Create mailboxes
       
       Diffstat:
         A geomyidae/postoffice/common         |      24 ++++++++++++++++++++++++
         A geomyidae/postoffice/create-mailbox |      48 +++++++++++++++++++++++++++++++
         A geomyidae/postoffice/get-mailbox    |      37 +++++++++++++++++++++++++++++++
         A geomyidae/postoffice/index.cgi      |      15 +++++++++++++++
         A geomyidae/postoffice/send-mail      |      29 +++++++++++++++++++++++++++++
       
       5 files changed, 153 insertions(+), 0 deletions(-)
       ---
   DIR diff --git a/geomyidae/postoffice/common b/geomyidae/postoffice/common
       @@ -0,0 +1,24 @@
       +#!/bin/sh
       +
       +MAILBOXES="mailboxes"
       +MAILROOM="mailroom"
       +
       +sanitize_restful_path() {
       +    printf "%s" "$1" \
       +        | sed -E 's/[^a-zA-Z0-9\-\/_]//g'
       +}
       +
       +sanitize_base64() {
       +    printf "%s" "$1" \
       +        | sed -E 's/[^a-zA-Z0-9\-\/_?+=]//g'
       +}
       +
       +sanitize_handle() {
       +    printf "%s" "$1" \
       +        | sed -E 's/[^a-zA-Z0-9\-\\_+=]//g' \
       +        | head -c 16
       +}
       +
       +decode_and_verify_pubkey() {
       +
       +}
   DIR diff --git a/geomyidae/postoffice/create-mailbox b/geomyidae/postoffice/create-mailbox
       @@ -0,0 +1,48 @@
       +#!/bin/sh
       +# Arguments:
       +#        $1 should be a handle no longer than 16 bytes.
       +#        $2 should be a base64 encoded RSA public key.
       +# Return:
       +#        
       +. ./common
       +
       +handle=$( sanitize_handle "$1" )
       +base64_payload=$( sanitize_base64 "$2" )
       +decoded_payload=$( printf "%s" "$base64_payload" \
       +                       | base64 -d --ignore-garbage ) # wrap? -w 0
       +# test result of `base64` invocation.
       +if [ ! $? ]; then
       +    printf "Invalid input given. Payload was not base 64.\n"
       +    return 1
       +fi
       +
       +printf "%s" "$decoded_payload" \
       +    | openssl pkey -pubcheck -pubin > /dev/null
       +# test result of `openssl` invocation. 
       +if [ ! $? ]; then
       +    printf "Key is not valid.\n"
       +    return 1
       +fi
       +public_key="$decoded_payload"
       +
       +result=$( ./get-mailbox "$handle" )
       +exit_code=$?
       +case $exit_code in
       +    0)
       +        printf "That mailbox already exists.\n"
       +        return 1
       +        ;;
       +    1)
       +        printf "$result"
       +        return 1
       +        ;;
       +    2)
       +        printf "%s\t%s\n" "$handle" "$base64_payload" >> "$MAILBOXES"
       +        return 0
       +        ;;
       +    *)
       +        printf "Undocumented behavior.\n"
       +        return 1
       +        ;;
       +esac
       +
   DIR diff --git a/geomyidae/postoffice/get-mailbox b/geomyidae/postoffice/get-mailbox
       @@ -0,0 +1,37 @@
       +#!/bin/sh
       +# Args:
       +#        $1 handle
       +
       +. ./common
       +
       +if [ ! -r $MAILBOXES ]; then
       +    printf "Can't find or read mailbox file.\n"
       +    return 1
       +fi
       +
       +handle=$( sanitize_handle "$1" )
       +if [ ! -n "$handle" ]; then
       +    printf "Specify a handle.\n"
       +    return 1
       +fi
       +
       +mailbox=$( awk -v handle="$handle" '
       +BEGIN {
       +      FS = "\t"
       +}
       +{
       +        if ( handle == $1 ) {
       +           print $2 # return public key
       +           exit 0
       +        }
       +}
       +' "$MAILBOXES" )
       +
       +if [ ! -n "$mailbox" ]; then
       +    printf "Mailbox not found.\n"
       +    return 2
       +fi
       +
       +printf "%s\n" "$mailbox"
       +return 0
       +
   DIR diff --git a/geomyidae/postoffice/index.cgi b/geomyidae/postoffice/index.cgi
       @@ -0,0 +1,15 @@
       +#!/bin/sh
       +. ./common
       +
       +path=$( sanitize_restful_path "$TRAVERSAL" )
       +args="$2"
       +
       +case "$path" in
       +    /mailbox/create/*)
       +        handle=$( printf "%s" "$path" \
       +            | awk -F/ '{ print $4 }' )
       +        ./create-mailbox "$handle" "$args"
       +        ;;    
       +esac
       +
       +
   DIR diff --git a/geomyidae/postoffice/send-mail b/geomyidae/postoffice/send-mail
       @@ -0,0 +1,29 @@
       +#!/bin/sh
       +# Args:
       +#        $1 handle of recipient
       +#        $2 message
       +#
       +. ./common
       +
       +# I am sanitizing inside the ./get-mailbox program, so do I need
       +# to do it here for any reason?
       +handle=$( sanitize_handle "$1" ) 
       +result=$( ./get-mailbox "$handle" )
       +exit_code=$?
       +if [ ! $exit_code ]; then
       +    printf "$result"
       +    return 1
       +fi
       +
       +public_key=$( printf "%s" "$result" \
       +                     | base64 -d --ignore-garbage )
       +
       +# need to decode the mailbox pubkey
       +# need to encrypt the message with the pubkey
       +# need to add the message to a mailheap/mailroom file, <pubkey>\t<message>
       +#                                        or should it be handle\tmessage
       +
       +# need to verify the message is below a certain length
       +# need to verify the chosen border exists
       +
       +