Copy became script to target directory.
authorIain Patterson <me@iain.cx>
Fri, 20 May 2016 15:45:28 +0000 (16:45 +0100)
committerIain Patterson <me@iain.cx>
Tue, 24 May 2016 09:15:24 +0000 (10:15 +0100)
Ensure the became script can be run when the source user's home
directory is not readable, by copying the script itself to the
target directory if the -D flag is used instead of -d.

opt/bin/become

index ea4926d..07801c3 100755 (executable)
@@ -3,9 +3,14 @@
 chdir=0
 kerberos=0
 x11=0
+copy=0
 dir=
-while getopts ":cd:kx" opt; do
+while getopts ":D:cd:kx" opt; do
   case $opt in
+    D)
+      copy=1
+      dir=$OPTARG
+    ;;
     c) chdir=1;;
     d) dir=$OPTARG;;
     k) kerberos=1;;
@@ -14,9 +19,17 @@ while getopts ":cd:kx" opt; do
 done
 shift $((OPTIND-1))
 
-if [ -z "$dir" ]; then
-  dir=${0%/*}
-  [ "$dir" = "$0" -o "$dir" = "." ] && dir=$PWD
+srcdir=${0%/*}
+[ "$srcdir" = "$0" -o "$srcdir" = "." ] && srcdir=$PWD
+if [ -n "$dir" ]; then
+  if [ $copy = 1 ]; then
+    if ! cp "$srcdir/became" "$dir/became"; then
+      echo >&2 "Failed to copy $srcdir/became to $dir!"
+      exit 100
+    fi
+  fi
+else
+  dir=$srcdir
 fi
 
 user="$1"; shift