Solved

sudo su [-] <list of users>

Posted on 2010-09-04
4
762 Views
Last Modified: 2013-12-16
I want a good syntax for allowing one group of users to be able to access a list of functional accounts using sudo.  I wrote something like

User_Alias ADMIN = a, b, c
ADMIN  ALL = (ALL) /bin/su - functional1, /bin/su - functional2, ...

I want something more elegant with '-' after su to be optional
0
Comment
Question by:farzanj
  • 3
4 Comments
 
LVL 6

Accepted Solution

by:
apresence earned 125 total points
Comment Utility
Hate to break the bad news, but I don't believe it is possible to do this because the sudoers file uses glob wildcards, not regular expressions.  Globs allow you to do things with allowing one or more of a certain character, etc, but do not allow you to provide alternate strings as options.  Unfortunately, if it supported regular expressions, then this would be trivial to do.

More info here:
http://www.sudo.ws/sudo/sudoers.man.html
http://jamesthornton.com/linux/man/glob.7.html

Alternatively, you could write a script that expands a list of users into the required syntax and updates the sudoers file
0
 
LVL 6

Assisted Solution

by:apresence
apresence earned 125 total points
Comment Utility
Another (possibly more elegant) way to do this is allow the users to run a script using sudo.  Save the attached code into /bin/su-wrap and chmod it to 700.

Your sudoers file then becomes just this:
User_Alias ADMIN = a, b, c
ADMIN ALL = (ALL) /bin/su-wrap

In my case, I'm using foo1 and foo2 as "ADMIN" users, and allowing either of them to switch to the other.  You can change the 2nd lline of the su-wrap script to list whatever users you want your admins to be able to su over to.  Using your example, you'd put:
ALLOWED_USERS="functional1 functional2".

Testing:
foo1@npx1600734:~ $ sudo su-wrap - foo2
Password:
Executing: /bin/su - foo2
foo2@npx1600734:~ $ exit
foo1@npx1600734:~ $ sudo su-wrap foo2
Executing: /bin/su foo2
foo2@npx1600734:~ $
#!/bin/sh

ALLOWED_USERS="foo1 foo2"

USING_ENV=0

if [ "$1" = "-" ]; then

  USING_ENV=1

  shift

fi

if [ $# -ne 1 ]; then

  echo "Usage: su-wrap [-] username" >&2

  exit 1

fi

for i in $ALLOWED_USERS; do

  if [ $1 = $i ]; then

    if [ $USING_ENV -eq 1 ]; then

      CMD="/bin/su - $i"

    else

      CMD="/bin/su $i"

    fi

    echo "Executing: $CMD"

    $CMD

    break

  fi

done

Open in new window

0
 
LVL 6

Assisted Solution

by:apresence
apresence earned 125 total points
Comment Utility
Two follow-up notes:
1. Make sure the /bin/su-wrap script is owned by root:root.
2. Remove the trailing "." from this line:
ALLOWED_USERS="functional1 functional2".
so it becomes:
ALLOWED_USERS="functional1 functional2"

Hope it works!
0
 
LVL 31

Author Closing Comment

by:farzanj
Comment Utility
I think you are right.  Kindly explain one more time why something like

/bin/su [ -] user   (notice a space)
OR
/bin/su ? user

doesn't work.  In the first one I tried to give an option between a space and -.  In the second, I tried to use any character, hoping it would allow either a space or a hyphen.  May be I am thinking too much like regular expressions.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now