summaryrefslogtreecommitdiff
path: root/scripts/su-to-root
blob: 375975e3a490417f46524ffdf0aefa446293e1aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash

if test -r /etc/su-to-rootrc; then
. /etc/su-to-rootrc
fi

if test -r ~/.su-to-rootrc; then
. ~/.su-to-rootrc
fi

PRIV=root
COMMAND=
NEEDS=text

gettext=$(which gettext 2>/dev/null)

transl() {
  txt="$1";
  shift;
  if [ -n "$gettext" ]; then 
    txt="$(gettext su-to-root "$txt")";
  fi
  printf "$txt" "$@"
}

eshell() {
   getent passwd $1 | cut -f7 -d:
}

usage () {
  transl 'usage: %s [-X] [-p <user>] -c <command>
  -c command: command to execute as a string (mandatory)
  -p <user>: user to switch to (default: root)
  -X: command is a X11 program\n' "$0" >&2
  exit 1
}

for i in "$@"; do
   case "$prev" in
     -p)
       PRIV="$i";;
     -c)
       COMMAND="$i";;
     -X) 
       NEEDS="X11";;
   esac
   prev="$i"
done

if [ -z "$COMMAND" ] ; then
   usage;
fi

euid=$(id -u)
privid=$(id -u $PRIV)
if test "$euid" = "$privid"; then
  sh -c "$COMMAND"
else
  case $NEEDS in
  text)
    if test "$euid" != 0; then
      transl 'About to execute %s.\n' "$COMMAND"
      transl 'This command needs %s privileges to be executed.\n' "$PRIV"
    fi
    PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/bin/X11:/usr/local/sbin:/usr/local/bin
    SHELL=`eshell $PRIV`
    case $SU_TO_ROOT_SU in
      sux)  suname=sux; pwuser="$PRIV"; cmd='sux  -p "$PRIV" -c "$COMMAND"';;
      sudo) suname=sudo;pwuser="$USER"; cmd='sudo -u "$PRIV" sh -c "$COMMAND"';;
      *)    suname=su;  pwuser="$PRIV"; cmd='su   -p "$PRIV" -c "$COMMAND"';;
    esac
    transl 'Using %s...\n' "$suname"
    transl 'Enter %s password at prompt.\n' "$pwuser"
    yesexpr=$(locale yesexpr)
    while ! eval $cmd; do
      transl 'Incorrect password or command failed. Try again? (y/N)'
      read ans
      if echo "$ans" | perl -e "<> =~ /$yesexpr/ and exit(1);"; then
        exit 1
      fi
    done;;
  X11)
    if test -z "$SU_TO_ROOT_X"; then
      if which gksu >/dev/null 2>&1 ; then
        SU_TO_ROOT_X=gksu
        if test "X$KDE_FULL_SESSION" = "Xtrue" ; then
          if which kdesu >/dev/null 2>&1 ; then
            SU_TO_ROOT_X=kdesu
          elif test -x /usr/lib/kde4/libexec/kdesu ; then
            SU_TO_ROOT_X=kde4su
          fi;
        fi;
      elif which kdesu >/dev/null 2>&1 ; then 
        SU_TO_ROOT_X=kdesu
      elif test -x /usr/lib/kde4/libexec/kdesu ; then
        SU_TO_ROOT_X=kde4su
      elif which ktsuss >/dev/null 2>&1 ; then
        SU_TO_ROOT_X=ktsuss
      elif which sux >/dev/null 2>&1 ; then 
        SU_TO_ROOT_X=sux
      else
        SU_TO_ROOT_X=su-to-root
      fi
    fi
    case $SU_TO_ROOT_X in
      gksu) gksu -u "$PRIV" "$COMMAND";;
      gksudo) gksudo -u "$PRIV" "$COMMAND";;
      kdesu) kdesu -u "$PRIV" "$COMMAND";;
      kdesudo) kdesudo -u "$PRIV" "$COMMAND";;
      kde4su) /usr/lib/kde4/libexec/kdesu -u "$PRIV" "$COMMAND";;
      ktsuss) ktsuss -u "$PRIV" "$COMMAND";;
      sux) env SU_TO_ROOT_SU=sux \
        x-terminal-emulator -e su-to-root -p "$PRIV" -c "$COMMAND";;
  # As a last resort, open a new x-terminal-emulator and prompt for the password
  # Do not use -X here!
      *) x-terminal-emulator -e su-to-root -p "$PRIV" -c "$COMMAND";;
    esac;;
  esac
fi