[pacman-dev] [PATCH] makepkg: fall back to su if sudo is not available

Ray Kohler ataraxia937 at gmail.com
Thu Feb 25 21:05:11 EST 2010


On Thu, Feb 25, 2010 at 7:25 PM, Allan McRae <allan at archlinux.org> wrote:
> On 21/02/10 12:20, Ray Kohler wrote:
>>
>> There's a bit of a hack here with the "fullcmd" temporary
>> variable to get around the need to pass the entire command line
>> to su -c as one argument.
>>
>> Signed-off-by: Ray Kohler<ataraxia937 at gmail.com>
>> ---
>>  scripts/makepkg.sh.in |   19 +++++++------------
>>  1 files changed, 7 insertions(+), 12 deletions(-)
>>
>> diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in
>> index 5bd294c..2dc9262 100644
>> --- a/scripts/makepkg.sh.in
>> +++ b/scripts/makepkg.sh.in
>> @@ -344,8 +344,13 @@ download_file() {
>>
>>  run_pacman() {
>>        local ret=0
>> -       if (( ! ASROOT ))&&  [[ $1 != "-T" ]]&&  sudo -l
>> $PACMAN&>/dev/null; then
>> -               sudo $PACMAN $PACMAN_OPTS "$@" || ret=$?
>> +       if (( ! ASROOT ))&&  [[ $1 != "-T" ]]; then
>> +               if [ "$(type -p sudo)" ]&&  sudo -l $PACMAN&>/dev/null;
>> then
>> +                       sudo $PACMAN $PACMAN_OPTS "$@" || ret=$?
>> +               else
>> +                       fullcmd="$PACMAN $PACMAN_OPTS $@"
>> +                       su -c "$fullcmd" || ret=$?
>
> does:
> su -c "$PACMAN $PACMAN_OPTS $@"
> not work?

No, that somehow gets parsed as:

su -c 'pacman -U' pkgname

Which makes su complain that there's no such user as pkgname. I found
no other way to get the whole command into a single string other than
stuffing it into another variable as I did, and I spent over an hour
trying various things.


>> +               fi
>>        else
>>                $PACMAN $PACMAN_OPTS "$@" || ret=$?
>>        fi
>> @@ -1686,16 +1691,6 @@ else
>>        fi
>>  fi
>>
>> -# check for sudo if we will need it during makepkg execution
>> -if (( ! ASROOT&&  ( DEP_BIN || RMDEPS || INSTALL ) )); then
>> -       if [ ! "$(type -p sudo)" ]; then
>> -               error "$(gettext "Cannot find the sudo binary! Is sudo
>> installed?")"
>> -               plain "$(gettext "Missing dependencies cannot be installed
>> or removed as a normal user")"
>> -               plain "$(gettext "without sudo; install and configure sudo
>> to auto-resolve dependencies.")"
>> -               exit 1
>> -       fi
>> -fi
>> -
>
> Maybe we should keep this somewhat...  With this patch I will be asked for a
> password.  It could be my user password if I have sudo install or it could
> be the root password if not.  That there is no indication which password is
> needed does not seem great from a usability perspective.  We could keep the
> check for sudo and print that we are falling back to using su.

That's fine. A new version of this patch follows.

>From f5c7f3ec7cd7c79cff2df2bec125d81577d6b30b Mon Sep 17 00:00:00 2001
From: Ray Kohler <ataraxia937 at gmail.com>
Date: Sat, 20 Feb 2010 21:08:25 -0500
Subject: [PATCH] makepkg: fall back to su if sudo is not available

There's a bit of a hack here with the "fullcmd" temporary
variable to get around the need to pass the entire command line
to su -c as one argument.

Signed-off-by: Ray Kohler <ataraxia937 at gmail.com>
---
 scripts/makepkg.sh.in |   14 ++++++++------
 1 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in
index 5bd294c..f80ebc5 100644
--- a/scripts/makepkg.sh.in
+++ b/scripts/makepkg.sh.in
@@ -344,8 +344,13 @@ download_file() {

 run_pacman() {
 	local ret=0
-	if (( ! ASROOT )) && [[ $1 != "-T" ]] && sudo -l $PACMAN &>/dev/null; then
-		sudo $PACMAN $PACMAN_OPTS "$@" || ret=$?
+	if (( ! ASROOT )) && [[ $1 != "-T" ]]; then
+		if [ "$(type -p sudo)" ] && sudo -l $PACMAN &>/dev/null; then
+			sudo $PACMAN $PACMAN_OPTS "$@" || ret=$?
+		else
+			fullcmd="$PACMAN $PACMAN_OPTS $@"
+			su -c "$fullcmd" || ret=$?
+		fi
 	else
 		$PACMAN $PACMAN_OPTS "$@" || ret=$?
 	fi
@@ -1689,10 +1694,7 @@ fi
 # check for sudo if we will need it during makepkg execution
 if (( ! ASROOT && ( DEP_BIN || RMDEPS || INSTALL ) )); then
 	if [ ! "$(type -p sudo)" ]; then
-		error "$(gettext "Cannot find the sudo binary! Is sudo installed?")"
-		plain "$(gettext "Missing dependencies cannot be installed or
removed as a normal user")"
-		plain "$(gettext "without sudo; install and configure sudo to
auto-resolve dependencies.")"
-		exit 1
+		warning "$(gettext "sudo can not be found; falling back to su for
acquiring root privileges.")"
 	fi
 fi

-- 
1.7.0


More information about the pacman-dev mailing list