diff --git a/README.md b/README.md index 04ce50e..f03a6eb 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ The program: - Checks `valid users` entries against local accounts. - Offers to create missing local accounts with `useradd -M -s /usr/sbin/nologin `. - 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 ` 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`. - Writes a timestamped backup before saving changes. diff --git a/app.go b/app.go index 36888c1..9fa2797 100644 --- a/app.go +++ b/app.go @@ -355,7 +355,9 @@ func (a *App) ensureUsers(users []string) error { } 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 @@ -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) { a.printf("%s: ", label) a.flush() diff --git a/samba-configer b/samba-configer index 1410828..bd21cd5 100755 Binary files a/samba-configer and b/samba-configer differ