Package: devscripts
Version: 2.12.6+deb7u2
Severity: wishlist
Tags: patch

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Hello,

I have written a script that can be used to replace sudo by using su
instead, or to replace su by using sudo instead.

This is useful for commands such as pdebuild, which normally use sudo,
can be configured to use another command, but that will then use it like
sudo (sudo-like-command command-to-run-as-root), so su cannot be used
instead (as it is meant to be used as su -c "command-to-run-as-root").

Since my scripts are not specific to pdebuild, but can be used for other
purposes, I suggest they are added to devscripts. Attached is a tarball
with: the scripts (in fact, a single one with a symlink, which acts
differently depending on how it is called), a manpage (in DocBook, and
compiled in Troff), and the Makefile to compile this manpage to Troff.

For convenience, I am also attaching the main script itself, so it can
be reviewed without having to extract the tarball.

Librement,

- -- 
 ,--.
: /` )   ن Tanguy Ortolo    <xmpp:[email protected]>
| `-'    Debian Developer   <irc://irc.oftc.net/Tanguy>
 \_

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iQIcBAEBCgAGBQJUP5qaAAoJEOryzVHFAGgZ5coP/28jueFraUOfrE2am0HAcHlw
Qx4Uoejx2NCtkt8qVAGUBdt9RQO9vbhelfgfR125CbDZsbYOym/oCq7kPjpi4VQU
L7Db9bV66z9yrvryw/7Ujx+arstWE3w1d9mUG/yjAe5rQUOs3eVp+mY27iueP41B
SRvxG7s22KrKZbkR95kRDC3dqS2XwA1rw6h+Yuita1ZeB62g9mAvxxCxZEHSBAlV
5t0uQZAfNzBDnGZH1WH1yfoX+rgnUp3B0u2dy3lgx+3Kgtd1B9moS9vPiJllLj0f
U+ojzmSK1Y7wDHfGVIng1sxXiYPE2CmH4IU4ClYXNS8rdEVO4QzOKW8QmDOVvlWO
XkDsWvsSx1VbcNG+g2m6AwDTUriCMqD9h4HXbJTMJoDsF/SWaejhaj7J6i9hg5h7
qFr+kT8AYW93AH1qF+Y+wP2nUghhHi9vwtIyhMxvJ3aWJW0yc2C5GE6TZ/2KMsCt
5TKmSa+o2HRaEmekWyHAAcNfezGWh6uAtx9PV9yVI87oeANqRCDC+kFTPEVA4e5j
t0csF5rFKbZ9VWVsgZP7ueDhx7SiGuHmU6ImQq+Gw6nq05hbBj5K81MG5M0/aBkS
QQ8CqThJQMSpVQDbBZrlYMhAIZqseOIKLqhxMRJy8lXNKudji9f2zanPhhWDvIvp
6Xtl84d79YWkLbvbQnww
=GBV/
-----END PGP SIGNATURE-----

Attachment: sudo-su.tar.xz
Description: application/xz

#! /bin/sh
set -e

# A wrapper around su and sudo to implement a basic sudo-style interface with
# su and a basic su-style interface with sudo.
# 
# This can be useful when using tools such as pdebuild(1) that integrate
# privilege escalation only with sudo, or only with su: by having it run
# this program instead, you actually use su, or sudo instead seemlessly.
#
# Copyright (C) Tanguy Ortolo <[email protected]> 2011
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details. 


usage()
{
    rc=$1
    echo "Usage: sudo-su [SUDO_OPTIONS] COMMAND" >&2
    echo "       su-sudo [SUDO_OPTIONS] -- [SU_OPTIONS] -c \"COMMAND\"" >&2
    echo "sudo-su uses su to implement a basic sudo-style interface" >&2
    echo "su-sudo uses sudo to implement a basic su-style interface" >&2
    exit $rc
}

fake_sudo()
{

    args="$(getopt -s sh -o +hisEu:o: --long help,login,shell,preserve-env,user:,options: -n "$0" -- "$@")"
    if [ "$?" != 0 ]
    then
        usage $?
    fi
    eval set -- "$args"
    while [ "$1" != "--" ]
    do
        case "$1"
        in
            -h|--help) usage 0 ; shift ;;
            -i) su_options="$su_options -l" ; shift ;;
            --login) su_options="$su_options --login" ; shift ;;
            -s|--shell)
                # This options is to get a shell instead of running a command,
                # which is done with sudo by specifying no command to run.
                shift ;;
            -E) su_options="$su_options -p" ; shift ;;
            --preserve-env) su_options="$su_options --preserve-environment"
                shift ;;
            -u|--user) user="$2" ; shift 2 ;;
            -o|--options) su_options="$su_options $2" ; shift 2 ;;
        esac
    done
    shift
    if [ "$#" -eq 0 ]
    then
        # No command specified, the user wants to get a shell
        exec su $su_options $user
    else # The remaining arguments are the command to run
        # Build a single string that will be used by su to run that command
        # through a shell's -c options
        command=""
        for arg in "$@"
        do
            # To represent single quotes from single-quote mode:
            # leave single-quote mode, enter double-quote mode, write the
            # single quote, leave double-quote mode, enter single-quote mode.
            arg="$(echo "$arg" | sed -e "s/'/'\"'\"'/g")"
            command="$command${command:+ } '$arg'"
        done
        
        exec su $su_options -c "$command" $user
    fi
}

fake_su()
{
    args="$(getopt -s sh -o hc:ls:mpo: --long help,command:,login,shell:,preserve-environment,options: -n "$0" -- "$@")"
    if [ "$?" != 0 ]
    then
        usage $?
    fi
    eval set -- "$args"
    while [ "$1" != "--" ]
    do
        case "$1"
        in
            -h|--help) usage 0 ; shift ;;
            -c|--command) command="$2" ; shift 2 ;;
            -l) sudo_options="$sudo_options -i" ; shift ;;
            --login) sudo_options="$sudo_options --login" ; shift ;;
            -s) export SHELL="$2" ; shift 2 ;;
            --shell) export SHELL="$2" ; shift 2 ;;
            -m|-p) sudo_options="$sudo_options -E" ; shift ;;
            --preserve-environment)
                sudo_options="$sudo_options --preserve-env" ; shift ;;
            -o|--options) sudo_options="$sudo_options $2" ; shift 2 ;;
        esac
    done
    shift
    if [ "$#" -gt 1 ] # That is too much, su only takes zero or one argument
    then
        usage 1
    elif [ "$#" -eq 1 ] # That is the username to use
    then
        sudo_options="$sudo_options -u $1"
    fi

    if [ -z "$command" ]
    then
        # No command specified, the user wants a shell
        exec sudo "$sudo_options" -s
    else
        exec sudo sh -c "$command"
    fi
}


case "$(basename $0)"
in
    sudo*) fake_sudo "$@" ;;
    su*) fake_su "$@" ;;
esac
_______________________________________________
devscripts-devel mailing list
[email protected]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/devscripts-devel

Reply via email to