In Linux I can mount a network location programatically with Go like this:
func main() {
var user, pass string
fmt.Println("username:")
fmt.Scanln(&user) // ignore errors for brevity
fmt.Println("password:")
fmt.Scanln(&pass)
cmd := exec.Command("mount", "-t", "cifs", "-o", "username="+user+",password="+pass, "//server/dir", "media/dir")
cmd.Run()
}
The problems:
- I can't run this without elevating privileges with
sudo
- Username and password will be provided by the user. This seems very unsafe. Can anyone confirm on the safety or danger of this approach?
Here's a similar approach with variables:
cmd := exec.Command("mount", "-t", "cifs", "-o", "username=$USER,password=$PASS", "//server/dir", "media/dir")
cmd.Env = []string{"USER="+user, "PASS="+pass}
cmd.Run()
That does not work. It seems that exec.Command()
function escapes the dollar sign, so the values in the env variables aren't replaced there. So this seems to indicate some type of safety or escaping going on here.
Editing the etc/fstab
file would allow me to run mount
without sudo
but then I'd need sudo
to edit the fstab
file, so back to square one.