[pacman-dev] [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@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=$? + 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 - unset pkgname pkgbase pkgver pkgrel pkgdesc url license groups provides unset md5sums replaces depends conflicts backup source install changelog build unset makedepends optdepends options noextract -- 1.7.0
On 21/02/10 12:20, Ray Kohler wrote:
does: su -c "$PACMAN $PACMAN_OPTS $@" not work?
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. Allan
On Thu, Feb 25, 2010 at 7:25 PM, Allan McRae <allan@archlinux.org> wrote:
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.
That's fine. A new version of this patch follows.
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@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
On Thu, Feb 25, 2010 at 09:05:11PM -0500, Ray Kohler wrote:
That's the result of the "$@". This should work: su -c "$PACMAN $PACMAN_OPTS $*" The normal advantages of using "$@" over using "$*" would have been eliminated in this context, anyway. -- Jim Pryor profjim@jimpryor.net
On Thu, Feb 25, 2010 at 9:52 PM, Jim Pryor <lists+pacman-dev@jimpryor.net> wrote:
Excellent, that does work. Third, and cleanest, patch version follows.
Signed-off-by: Ray Kohler <ataraxia937@gmail.com> --- scripts/makepkg.sh.in | 13 +++++++------ 1 files changed, 7 insertions(+), 6 deletions(-) diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 5bd294c..dcea3a0 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -344,8 +344,12 @@ 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 + su -c "$PACMAN $PACMAN_OPTS $*" || ret=$? + fi else $PACMAN $PACMAN_OPTS "$@" || ret=$? fi @@ -1689,10 +1693,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
On Thu, Feb 25, 2010 at 10:15 PM, Ray Kohler <ataraxia937@gmail.com> wrote:
Anything further needed from me on this, or is this patch ok now?
I just came across a nuisance with this patch: it warns about the use of su both inside, and outside, of the fakeroot run. Here's another version that checks INFAKEROOT before printing that, such that it'll only do so outside of the fakeroot.
Signed-off-by: Ray Kohler <ataraxia937@gmail.com> --- scripts/makepkg.sh.in | 15 ++++++++------- 1 files changed, 8 insertions(+), 7 deletions(-) diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in index 5bd294c..75ce7e9 100644 --- a/scripts/makepkg.sh.in +++ b/scripts/makepkg.sh.in @@ -344,8 +344,12 @@ 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 + su -c "$PACMAN $PACMAN_OPTS $*" || ret=$? + fi else $PACMAN $PACMAN_OPTS "$@" || ret=$? fi @@ -1687,12 +1691,9 @@ else fi # check for sudo if we will need it during makepkg execution -if (( ! ASROOT && ( DEP_BIN || RMDEPS || INSTALL ) )); then +if (( ! ( ASROOT || INFAKEROOT ) && ( 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.1
On 05/03/10 12:57, Ray Kohler wrote:
That message looked a bit ugly in a standard 80 wide terminal so I changed it to: "Sudo can not be found. Using su to acquire root privileges." Otherwise, this looks fine. Pulled to my working branch. Allan
participants (3)
-
Allan McRae
-
Jim Pryor
-
Ray Kohler