Data
- I want operator users on this machine to mount their own cifs shares
- The
sudoers
file already contains the/bin/mount -t cifs //*/* /media/* -o username=*
command for all operators - I want the users to mount a
cifs
share through a script typing the password only once, not twice. - The sudo password and the cifs password are identical.
What I already have
This script works:
#!/bin/bash
sudo 'mount -t cifs //192.168.1.1/home /media/$USER/home -o username=$USER'
...but it requires the users to type the same password twice!
- Once for
sudo
- Once for the mount itself
This would also work:
#!/bin/bash
echo -n Password:
read -s szPassword
echo $szPassword | sudo -S sh -c 'echo $szPassword | mount -t cifs //192.168.1.1/home /media/$USER/home -o username=$USER'
...but this would require me to allow all operator users to be able to sudo sh
(major security problem)
Question
How to mount a cifs share in bash¹ without putting sh
in the sudoers
file nor creating a permanent/temporary file???
Note 1: no python, perl, C, Go, ... please?
Note 2: I know I can just remove the password through the sudoers
file, but I'm trying to tighten security, not loosen it, without giving up convenience...
You should instead make the user do the call of using sudo as
sudo script
. just check if the script is being run as root, if not ask for itDon't try to capture the password of your users.
I'm dumb!
The following script:
just works and:
Require no
sudo
password for executing this command; the password prompt formount
remains.In
sudoers
, include something likeAfter including this,
sudo
will no longer ask for a password for this specific command; the user still needs to provide a password to themount
command.Note: I took the command verbatim from what you included in the question; I didn't check whether its wildcards would allow for users to do something nasty. Read the
sudoers
manpage for examples of nastiness. In particular, note that this line insudoers
allows the user to add any number of-o
switches or other arguments tomount
. You may want to rethink your approach, e.g. by adding a script such as @Braiam proposes and allow running that throughsudo
without extra authentication. The script then ensures that users can only run the specific form ofmount
that you want them to run.Also, instead of allowing this for all users, you could also limit this to members of a certain group, e.g. you could create a group
cifsmount
and then haveA general solution to these issues is to put the following preamble at the top of your sudo requiring scripts:
Obviously, this has a downside in that if some commands in the script don't require
sudo
to run, there's an unnecessary elevation of privileges here.Anyway, thought I would share this little tip. The nicest thing about it, is that if you're already effective-uid root (e.g. if you already called it under sudo) it gracefully does the right thing. Also giving an error and forcing you to retype/rerun (with sudo) is less friendly.
You may also want to check out the
timestamp_timeout
variable inman 5 sudoers
which tellssudo
to remember user credentials for a limited number of minutes (and can be fractional).