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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么