offer set user password on creation

This commit is contained in:
2026-03-19 16:13:48 +00:00
parent 1b8807010d
commit 6fbe5389c2
3 changed files with 68 additions and 1 deletions

View File

@@ -25,6 +25,7 @@ The program:
- Checks `valid users` entries against local accounts. - Checks `valid users` entries against local accounts.
- Offers to create missing local accounts with `useradd -M -s /usr/sbin/nologin <user>`. - Offers to create missing local accounts with `useradd -M -s /usr/sbin/nologin <user>`.
- If user creation fails because admin rights are needed, explains the issue and offers to retry with `sudo`. - If user creation fails because admin rights are needed, explains the issue and offers to retry with `sudo`.
- After creating a user, offers to launch `smbpasswd -a <user>` immediately so the Samba password can be set right away.
- If saving the Samba config or its backup needs admin rights, explains the issue and offers to retry with `sudo`. - If saving the Samba config or its backup needs admin rights, explains the issue and offers to retry with `sudo`.
- Writes a timestamped backup before saving changes. - Writes a timestamped backup before saving changes.

68
app.go
View File

@@ -355,7 +355,9 @@ func (a *App) ensureUsers(users []string) error {
} }
a.printf("Created local user %s\n", name) a.printf("Created local user %s\n", name)
a.println("If Samba authentication is required, add a Samba password with: smbpasswd -a " + name) if err := a.offerSetSambaPassword(name); err != nil {
return err
}
} }
return nil return nil
@@ -397,6 +399,70 @@ func (a *App) createUser(name string) error {
} }
} }
func (a *App) offerSetSambaPassword(name string) error {
a.println("")
a.println("This share can use a Samba password for sign-in.")
a.println("I can set that up now and ask you to type the password you want to use.")
a.flush()
setPassword, err := a.confirm(fmt.Sprintf("Set a Samba password for %q now", name), true)
if err != nil {
return err
}
if !setPassword {
a.println("You can do this later with: smbpasswd -a " + name)
return nil
}
return a.setSambaPassword(name)
}
func (a *App) setSambaPassword(name string) error {
if a.lookPath != nil {
if _, err := a.lookPath("smbpasswd"); err != nil {
a.println("I couldn't find the smbpasswd tool yet.")
a.println("If Samba was just installed, try opening the app again after the installation finishes.")
return fmt.Errorf("set Samba password for %s: smbpasswd command not found", name)
}
}
if err := a.runner.Run("smbpasswd", "-a", name); err == nil {
a.printf("Samba password set for %s\n", name)
return nil
} else if shouldOfferPrivilegeRetry(err) {
a.println("")
a.println("Setting a Samba password usually needs administrator permission.")
a.println("I can try again using sudo so you can enter your admin password.")
a.flush()
canUseSudo := false
if a.lookPath != nil {
_, sudoErr := a.lookPath("sudo")
canUseSudo = sudoErr == nil
}
if !canUseSudo {
return fmt.Errorf("set Samba password for %s: %w", name, err)
}
retry, promptErr := a.confirm("Retry Samba password setup with sudo", true)
if promptErr != nil {
return promptErr
}
if !retry {
return fmt.Errorf("set Samba password for %s: %w", name, err)
}
if sudoErr := a.runner.Run("sudo", "smbpasswd", "-a", name); sudoErr != nil {
return fmt.Errorf("set Samba password for %s with sudo: %w", name, sudoErr)
}
a.printf("Samba password set for %s\n", name)
return nil
} else {
return fmt.Errorf("set Samba password for %s: %w", name, err)
}
}
func (a *App) prompt(label string) (string, error) { func (a *App) prompt(label string) (string, error) {
a.printf("%s: ", label) a.printf("%s: ", label)
a.flush() a.flush()

Binary file not shown.