From 00716fdadf1fcfeff923cf7d3da2b17bd7305ced Mon Sep 17 00:00:00 2001 From: AluminumTank Date: Wed, 3 Feb 2021 16:22:41 -0500 Subject: [PATCH] add argument checking; fix case where user doesn't have read access to directory --- doasedit | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/doasedit b/doasedit index 3ce4800..3049534 100755 --- a/doasedit +++ b/doasedit @@ -1,15 +1,24 @@ -#!/bin/sh +#!/bin/bash +if [ ! -z "${2}" ]; then + echo "Expected only one argument" + exit 1 +elif [ -z "${1}" ]; then + echo "No file path provided" + exit 1 +fi -destfile_pfx="$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 32)" +set -euo pipefail + +destfile_pfx="$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 32)" || true while [ -d "/tmp/doasedit/$destfile_pfx" ]; do destfile_pfx="$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 32)" done mkdir -p /tmp/doasedit/$destfile_pfx -srcfile="$(realpath $1)" +srcfile="$(doas realpath $1)" -if [ -f "$srcfile" ]; then +if doas [ -f "$srcfile" ]; then doas cp $srcfile /tmp/doasedit/$destfile_pfx/edit doas chown -R $USER:$USER /tmp/doasedit/$destfile_pfx/edit doas cp $srcfile /tmp/doasedit/$destfile_pfx/file