From 51b5b2e15188ef54120d0ae9032d2e28f7246617 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Thu, 1 Oct 2015 16:39:30 +1300 Subject: Add sue(8) --- Makefile | 4 +++- README.markdown | 4 ++++ bin/sue | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ man/man8/sue.8 | 15 +++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100755 bin/sue create mode 100644 man/man8/sue.8 diff --git a/Makefile b/Makefile index 004e70c2..3b6b0f84 100644 --- a/Makefile +++ b/Makefile @@ -83,9 +83,11 @@ install-bash : test-bash install-bin : test-bin install -m 0755 -d -- \ "$(HOME)"/.local/bin \ - "$(HOME)"/.local/share/man/man1 + "$(HOME)"/.local/share/man/man1 \ + "$(HOME)"/.local/share/man/man8 install -m 0755 -- bin/* "$(HOME)"/.local/bin install -m 0644 -- man/man1/* "$(HOME)"/.local/share/man/man1 + install -m 0644 -- man/man8/* "$(HOME)"/.local/share/man/man8 install-curl : install -m 0644 -- curl/curlrc "$(HOME)"/.curlrc diff --git a/README.markdown b/README.markdown index 7ad945bc..04639b01 100644 --- a/README.markdown +++ b/README.markdown @@ -290,6 +290,10 @@ your `/etc/manpath` configuration, depending on your system. `edda(1)` provides a means to run `ed(1)` over a set of files preserving any options, mostly useful for scripts. There’s `--help` output and a manual page. +`sue(8)` execs `sudoedit(8)` as the owner of all the file arguments given, +perhaps in cases where you may not necessarily have `root` `sudo(8)` +privileges. + There's also a script `han(1)` to provide a `keywordprg` for Bash script development that will look for `help` topics. You could use it from the shell too. It also has a brief manual. diff --git a/bin/sue b/bin/sue new file mode 100755 index 00000000..cd20fa5c --- /dev/null +++ b/bin/sue @@ -0,0 +1,67 @@ +#!/usr/bin/env bash + +# +# sue(1) -- Run sudoedit(8) with an appropriate user on a set of files +# +# Author: Tom Ryder +# Copyright: 2015 +# License: Public domain +# + +# Name self +self=sue + +# Define a function to show usage +usage() { + printf '%s: usage: %s FILE1 [FILE2 ...]\n' \ + "$self" "$self" +} + +# Test the first argument +case $1 in + + # Give help on stdout if requested + -h|--help|-\?) + usage + exit + ;; + + # If no file was given, give help on stderr and bail + '') + usage >&2 + exit 1 + ;; +esac + +# Pull arguments into an array +declare -a files +files=("$@") + +# Iterate through the files and check they all have the same owner +user= +for file in "${files[@]}" ; do + + # Use stat(1) to get the file owner + if ! file_owner=$(stat -c %U "$file") ; then + printf '%s: Failed to run stat(1) on file %s\n' \ + "$self" "$file" >&2 + exit 1 + fi + + # If this is the first file, we'll use its owner as our user + if [[ -z $user ]] ; then + user=$file_owner + + # If not, and the user we're going to use and this file's owner don't + # match, bail with an error + elif [[ $user != $file_owner ]] ; then + printf '%s: Files do not share a common owner\n' \ + "$self" >&2 + exit 1 + fi +done + +# If we got this far, there's at least one file and all the files are owned by +# the same user; we can safely edit them +exec sudoedit -u "$user" "${files[@]}" + diff --git a/man/man8/sue.8 b/man/man8/sue.8 new file mode 100644 index 00000000..257493df --- /dev/null +++ b/man/man8/sue.8 @@ -0,0 +1,15 @@ +.TH SUE 8 "September 2015" "Manual page for sue" +.SH NAME +.B sue +\- run sudoedit(8) as the arguments' owner +.SH SYNOPSIS +.B sue +FILE1 [FILE2...] +.SH DESCRIPTION +Run sudoedit(8) over the arguments given as the user that owns them all; exit +with an error if they're not owned by a common user. This could be useful in +situations where you don't have full root access via sudo(8), or simply want +to be strict about working with least privilege. +.SH AUTHOR +Tom Ryder + -- cgit v1.2.3