Files

132 lines
3.5 KiB
Go
Raw Permalink Normal View History

2022-08-09 07:28:03 +00:00
package command
import (
"context"
"errors"
"fmt"
"os"
"syscall"
"time"
"github.com/go-ldap/ldap/v3"
"github.com/libregraph/idm/pkg/ldbbolt"
"github.com/libregraph/idm/server"
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
2022-08-09 07:28:03 +00:00
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/services/idm/pkg/config"
"github.com/owncloud/ocis/v2/services/idm/pkg/config/parser"
"github.com/owncloud/ocis/v2/services/idm/pkg/logging"
"github.com/urfave/cli/v2"
bolt "go.etcd.io/bbolt"
"golang.org/x/term"
)
// ResetPassword is the entrypoint for the resetpassword command
func ResetPassword(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "resetpassword",
Usage: "Reset user password",
2022-08-09 07:28:03 +00:00
Category: "password reset",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "user-name",
Aliases: []string{"u"},
Usage: "User name",
Value: "admin",
},
&cli.StringFlag{
Name: "user-type",
Usage: "Type of user account: 'user' (ou=users) or 'service' (ou=sysusers)",
Value: "user",
},
},
Before: func(_ *cli.Context) error {
2022-08-22 08:08:18 +02:00
return configlog.ReturnFatal(parser.ParseConfig(cfg))
2022-08-09 07:28:03 +00:00
},
Action: func(c *cli.Context) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
2024-07-24 13:37:39 +02:00
ctx, cancel := context.WithCancel(c.Context)
2022-08-09 07:28:03 +00:00
defer cancel()
userType := c.String("user-type")
if userType != "user" && userType != "service" {
return fmt.Errorf("invalid --user-type %q: must be 'user' or 'service'", userType)
}
return resetPassword(ctx, logger, cfg, c.String("user-name"), userType)
2022-08-09 07:28:03 +00:00
},
}
}
func resetPassword(_ context.Context, logger log.Logger, cfg *config.Config, userName string, userType string) error {
2022-08-09 07:28:03 +00:00
servercfg := server.Config{
Logger: log.LogrusWrap(logger.Logger),
LDAPHandler: "boltdb",
LDAPBaseDN: "o=libregraph-idm",
BoltDBFile: cfg.IDM.DatabasePath,
}
ou := "users"
if userType == "service" {
ou = "sysusers"
}
userDN := fmt.Sprintf("uid=%s,ou=%s,%s", userName, ou, servercfg.LDAPBaseDN)
fmt.Printf("Resetting password for user '%s'.\n", userDN)
2022-08-09 07:28:03 +00:00
if _, err := os.Stat(servercfg.BoltDBFile); errors.Is(err, os.ErrNotExist) {
fmt.Fprintf(os.Stderr, "IDM database does not exist.\n")
return err
}
newPw, err := getPassword()
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading password: %v\n", err)
return err
}
bdb := &ldbbolt.LdbBolt{}
opts := bolt.Options{
Timeout: 1 * time.Millisecond,
}
if err := bdb.Configure(servercfg.Logger, servercfg.LDAPBaseDN, servercfg.BoltDBFile, &opts); err != nil {
fmt.Fprintf(os.Stderr, "Failed to open database: '%s'. Please stop any running ocis/idm instance, as this tool requires exclusive access to the database.\n", err)
return err
}
defer bdb.Close()
if err := bdb.Initialize(); err != nil {
return err
}
pwRequest := ldap.NewPasswordModifyRequest(userDN, "", newPw)
2022-08-09 07:28:03 +00:00
if err := bdb.UpdatePassword(pwRequest); err != nil {
fmt.Fprintf(os.Stderr, "Failed to update user password: %v\n", err)
2022-08-09 07:28:03 +00:00
}
fmt.Printf("Password for user '%s' updated.\n", userDN)
2022-08-09 07:28:03 +00:00
return nil
}
func getPassword() (string, error) {
fmt.Print("Enter new password: ")
bytePassword, err := term.ReadPassword(syscall.Stdin)
2022-08-09 07:28:03 +00:00
if err != nil {
return "", err
}
fmt.Println("")
fmt.Print("Re-enter new password: ")
bytePasswordVerify, err := term.ReadPassword(syscall.Stdin)
2022-08-09 07:28:03 +00:00
if err != nil {
return "", err
}
fmt.Println("")
password := string(bytePassword)
passwordVerify := string(bytePasswordVerify)
if password != passwordVerify {
return "", errors.New("Passwords do not match")
}
return password, nil
}