From 0f9b1f4c75317680989aa4a44a32e6efe256dd56 Mon Sep 17 00:00:00 2001
From: merakor <cem@ckyln.com>
Date: Thu, 14 May 2020 10:45:18 +0000
Subject: kiss: don't elevate permissions if it isn't required, KISS_ROOT fix

Co-authored-by: Dylan Araps <dylan.araps@gmail.com>

FossilOrigin-Name: d111e5f22b95c4688390daf2dc7c24cdc1316648e991a7c5be9e6de1d12ab184
---
 kiss | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

(limited to 'kiss')

diff --git a/kiss b/kiss
index 6a61ea5..39aea51 100755
--- a/kiss
+++ b/kiss
@@ -800,9 +800,10 @@ pkg_conflicts() {
     # (/bin/ls -> /usr/bin/ls).
     while read -r file; do
         case $file in */) continue; esac
+        dirname="$(kiss-readlink "$KISS_ROOT/${file%/*}" 2>/dev/null)"
 
         printf '%s/%s\n' \
-            "$(kiss-readlink "$KISS_ROOT/${file%/*}" 2>/dev/null)" \
+            "${dirname#$KISS_ROOT}" \
             "${file##*/}"
     done < "$tar_dir/$1/$pkg_db/$1/manifest" > "$KISS_TMPDIR/$pid-m"
 
@@ -928,16 +929,16 @@ pkg_swap() {
         # Convert the current owner to an alternative and rewrite
         # its manifest file to reflect this. We then resort this file
         # so no issues arise when removing packages.
-        cp  -Pf "$2" "$pkg_owns>${alt#*>}"
-        sed "s#^$2#$PWD/$pkg_owns>${alt#*>}#" \
+        cp  -Pf "$KISS_ROOT/$2" "$pkg_owns>${alt#*>}"
+        sed "s#^$2#${PWD#$KISS_ROOT}/$pkg_owns>${alt#*>}#" \
             "../installed/$pkg_owns/manifest" |
             sort -r -o "../installed/$pkg_owns/manifest"
     fi
 
     # Convert the desired alternative to a real file and rewrite
     # the manifest file to reflect this. The reverse of above.
-    mv  -f "$alt" "$2"
-    sed "s#^$PWD/$alt#$2#" "../installed/$1/manifest" |
+    mv -f "$alt" "$KISS_ROOT/$2"
+    sed "s#^${PWD#$KISS_ROOT}/$alt#$2#" "../installed/$1/manifest" |
         sort -r -o "../installed/$1/manifest"
 }
 
@@ -1117,7 +1118,7 @@ pkg_install() {
 
     # This is repeated multiple times. Better to make it a function.
     pkg_rsync() {
-        rsync --chown=root:root --chmod=Du-s,Dg-s,Do-s \
+        rsync "--chown=$USER:$USER" --chmod=Du-s,Dg-s,Do-s \
               -WhHKa --no-compress --exclude /etc "${1:---}" \
               "$tar_dir/$pkg_name/" "$KISS_ROOT/"
     }
@@ -1363,16 +1364,17 @@ args() {
         a|alternatives)
             # Rerun the script with 'su' if the user isn't root.
             # Cheeky but 'su' can't be used on shell functions themselves.
-            [ -z "$1" ] || [ "$uid" = 0 ] || {
+            [ -z "$1" ] || [ -w "$KISS_ROOT/" ] || [ "$uid" = 0 ] || {
                 as_root "$0" "$action" "$@"
                 return
             }
         ;;
 
         i|install|r|remove)
-            # Rerun the script with 'su' if the user isn't root.
-            # Cheeky but 'su' can't be used on shell functions themselves.
-            [ "$uid" = 0 ] || {
+            # Rerun the script with 'su' if the user does not have write
+            # permissions for the root. Cheeky but 'su' can't be used on
+            # shell functions themselves.
+            [ -w "$KISS_ROOT/" ] || [ "$uid" = 0 ] || {
                 KISS_FORCE="$KISS_FORCE" as_root "$0" "$action" "$@"
                 return
             }
-- 
cgit v1.2.3