double validate username

pull/1/head
vilmibm 2023-03-04 00:18:45 +00:00
parent e5cf8a5521
commit 21e51a829f
2 changed files with 11 additions and 2 deletions

View File

@ -32,11 +32,20 @@ func quit(msg string) {
} }
func main() { func main() {
username := os.Args[1]
if username == "" {
quit("expected username as argument")
}
u, err := user.Current() u, err := user.Current()
if err != nil { if err != nil {
quit(err.Error()) quit(err.Error())
} }
if u.Username != username {
quit("that's my purse; I don't know you")
}
sshPath := path.Join("/home", u.Username, ".ssh") sshPath := path.Join("/home", u.Username, ".ssh")
keyfilePath := path.Join(sshPath, keyfileName) keyfilePath := path.Join(sshPath, keyfileName)
@ -44,7 +53,7 @@ func main() {
quit(err.Error()) quit(err.Error())
} }
_, err := os.Open(keyfileName) _, err = os.Open(keyfileName)
if err == nil { if err == nil {
quit(fmt.Sprintf("%s already exists", keyfileName)) quit(fmt.Sprintf("%s already exists", keyfileName))
} }

View File

@ -275,7 +275,7 @@ func createUser(data newUserData) (err error) {
return fmt.Errorf("usermod failed: %w", err) return fmt.Errorf("usermod failed: %w", err)
} }
cmd = exec.Command("sudo", "/town/bin/createkeyfile") cmd = exec.Command("sudo", "/town/bin/createkeyfile", data.Username)
cmd.Stdin = bytes.NewBufferString(keyfileText(data)) cmd.Stdin = bytes.NewBufferString(keyfileText(data))
if err = cmd.Run(); err != nil { if err = cmd.Run(); err != nil {
return fmt.Errorf("createkeyfile failed: %w", err) return fmt.Errorf("createkeyfile failed: %w", err)