dream752614590 2018-02-16 14:59
浏览 52
已采纳

使用Go在Linux中以编程方式安全地安装网络位置

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:

  1. I can't run this without elevating privileges with sudo
  2. 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.

  • 写回答

1条回答 默认 最新

  • douti9253 2019-05-30 14:33
    关注

    We can use gvfs to mount shares in userspace, which means we don't need to elevate privileges with sudo. The gio command can be used for this.

    The code snippet below excludes error handling for brevity:

    cmd := exec.Command("gio", "mount", "smb://server/share")
    inPipe, _ := cmd.StdinPipe()
    cmd.Start()
    
    // Get credentials whichever way you find best, including scanning the Stdin.
    // Concatenate them together with line breaks in between and a line break at the end.
    auth := "Username
    Domain
    Password
    "
    inPipe.Write([]byte(auth))
    
    // Wait for the command to finish.
    cmd.Wait()
    

    Scanning the Stdin seems to be an acceptable way to capture credentials, since that's how the gio command works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置