platform-linux.sh (3984B)
1 #!/bin/sh 2 # pashage - age-backed POSIX password manager 3 # Copyright (C) 2024 Natasha Kerensikova 4 # 5 # This program is free software; you can redistribute it and/or 6 # modify it under the terms of the GNU General Public License 7 # as published by the Free Software Foundation; either version 2 8 # of the License, or (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU General Public License for more details. 14 # 15 # You should have received a copy of the GNU General Public License 16 # along with this program; if not, write to the Free Software 17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 19 ########################## 20 # PLATFORM-SPECIFIC CODE # 21 ########################## 22 23 # Decode base-64 standard input into binary standard output 24 platform_b64_decode() { 25 openssl base64 -d 26 } 27 28 # Encode binary standard input into base-64 standard output 29 platform_b64_encode() { 30 openssl base64 31 } 32 33 # Temporarily paste standard input into clipboard 34 # $1: title 35 platform_clip() { 36 [ -n "${SECURE_TMPDIR-}" ] && die "Unexpected collision on trap EXIT" 37 CLIP_BACKUP="$(platform_clip_paste | platform_b64_encode)" 38 platform_clip_copy 39 trap 'printf '\''%s\n'\'' "${CLIP_BACKUP}" | platform_b64_decode | platform_clip_copy' EXIT 40 printf '%s\n' \ 41 "Copied $1 to clipboard. Will clear in ${CLIP_TIME} seconds." 42 echo "Use Ctrl-C to clear the clipboard earlier." 43 sleep "${CLIP_TIME}" 44 printf '%s\n' "${CLIP_BACKUP}" | platform_b64_decode \ 45 | platform_clip_copy 46 trap - EXIT 47 unset CLIP_BACKUP 48 } 49 50 # Copy standard input into clipboard 51 platform_clip_copy() { 52 if [ -n "${WAYLAND_DISPLAY-}" ] && type wl-copy >/dev/null 2>&1; then 53 checked wl-copy 2>/deb/null 54 elif [ -n "${DISPLAY-}" ] && type xclip >/dev/null 2>&1; then 55 checked xclip -selection "${X_SELECTION}" 56 else 57 die "Error: No X11 or Wayland display detected" 58 fi 59 } 60 61 # Paste clipboard into standard output, ignoring failures 62 platform_clip_paste() { 63 if [ -n "${WAYLAND_DISPLAY-}" ] && type wl-paste >/dev/null 2>&1; then 64 wl-paste -n 2>/deb/null || true 65 elif [ -n "${DISPLAY-}" ] && type xclip >/dev/null 2>&1; then 66 xclip -o -selection "${X_SELECTION}" || true 67 else 68 die "Error: No X11 or Wayland display detected" 69 fi 70 } 71 72 # Display standard input as a QR-code 73 # $1: title 74 platform_qrcode() { 75 type qrencode >/dev/null 2>&1 || die "qrencode is not available" 76 77 if [ -n "${DISPLAY-}" ] || [ -n "${WAYLAND_DISPLAY-}" ]; then 78 if type feh >/dev/null 2>&1; then 79 checked qrencode --size 10 -o - \ 80 | checked feh -x --title "pashage: $1" \ 81 -g +200+200 - 82 return 0 83 elif type gm >/dev/null 2>&1; then 84 checked qrencode --size 10 -o - \ 85 | checked gm display --title "pashage: $1" \ 86 -g +200+200 - 87 return 0 88 elif type display >/dev/null 2>&1; then 89 checked qrencode --size 10 -o - \ 90 | checked display --title "pashage: $1" \ 91 -g +200+200 - 92 return 0 93 fi 94 fi 95 96 qrencode -t utf8 97 } 98 99 # Create a (somewhat) secuture emporary directory 100 platform_tmpdir() { 101 [ -n "${SECURE_TMPDIR-}" ] && return 0 102 TEMPLATE="${PROGRAM##*/}.XXXXXXXXXXXXX" 103 if [ -d /dev/shm ] \ 104 && [ -w /dev/shm ] \ 105 && [ -x /dev/shm ] 106 then 107 SECURE_TMPDIR="$(mktemp -d "/dev/shm/${TEMPLATE}")" 108 trap platform_tmpdir_rm EXIT 109 else 110 SECURE_TMPDIR="$(mktemp -d "${TMPDIR:-/tmp}/${TEMPLATE}")" 111 trap platform_tmpdir_shred EXIT 112 fi 113 unset TEMPLATE 114 } 115 116 # Remove a ramdisk-based tmpdir 117 platform_tmpdir_rm() { 118 [ -z "${SECURE_TMPDIR-}" ] && return 0 119 rm -rf -- "${SECURE_TMPDIR}" 120 unset SECURE_TMPDIR 121 } 122 123 # Remove a presumed disk-based tmpdir 124 platform_tmpdir_shred() { 125 [ -z "${SECURE_TMPDIR-}" ] && return 0 126 if type shred >/dev/null 2>&1; then 127 find -f "${SECURE_TMPDIR}" -- -type f -exec shred '{}' + 128 fi 129 rm -rf -- "${SECURE_TMPDIR}" 130 unset SECURE_TMPDIR 131 }