File: su-to-root

package info (click to toggle)
menu 2.1.5-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,476 kB
  • ctags: 1,251
  • sloc: cpp: 6,222; ansic: 2,306; sh: 453; makefile: 296; sed: 93
file content (42 lines) | stat: -rwxr-xr-x 780 bytes parent folder | download | duplicates (2)
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
#!/bin/sh

PRIV=root
COMMAND=


eusername () {
   grep "^[^:]*:[^:]*:$1:" /etc/passwd|cut -f1 -d:
}
#with getopt I cannot get multy-word commands to work (like -c "ls -al").
#
#set -- `getopt p:c: "$@"`
#if test $? != 0; then
#    echo 'Usage: ...'
#  exit 2
#fi
for i in "$@"; do
   case "$prev" in
     -p)
       PRIV="$i";;
     -c)
       COMMAND="$i";;
   esac
   prev="$i"
done

eus=$(eusername $(id -u))
if test $eus = "root" -o $eus = $PRIV; then
  $COMMAND
else
  echo About to execute $COMMAND. 
  echo This command needs $PRIV privileges to be executed.
  echo enter $PRIV passwd:
  while ! su $PRIV -c "$COMMAND"; do
    echo -n 'Incorrect password. Try again? (y/n)'
    read ans
    if test "$ans" != "y" -a "$ans" != "Y"; then
      exit 1
    fi
  done  
fi