diff --git a/ldap/ldap.go b/ldap/ldap.go index 8be9565..945535c 100644 --- a/ldap/ldap.go +++ b/ldap/ldap.go @@ -11,6 +11,7 @@ import ( "github.com/go-ldap/ldap/v3" ) +//nolint:gochecknoglobals var ldapStore = make(LDAPStore) // LDAPStore maps users to a cached password @@ -128,7 +129,7 @@ func createUserFromLDAP(username string, dbc *db.DB, config Config, l *ldap.Conn isAdmin := doesLDAPAdminExist(username, config, l) log.Println(username, isAdmin) - if doesLDAPUserExist(username, config, l) && !isAdmin { + if !doesLDAPUserExist(username, config, l) { return errors.New("no such user") } diff --git a/server/ctrladmin/handlers_raw.go b/server/ctrladmin/handlers_raw.go index d304afd..8d496f1 100644 --- a/server/ctrladmin/handlers_raw.go +++ b/server/ctrladmin/handlers_raw.go @@ -25,13 +25,15 @@ func (c *Controller) ServeLoginDo(w http.ResponseWriter, r *http.Request) { if err != nil { log.Println("Failed to check LDAP credentials:", err) sessAddFlashW(session, []string{"failed to check LDAP credentials"}) + sessLogSave(session, w, r) + http.Redirect(w, r, r.Referer(), http.StatusSeeOther) + return } else if !ok { sessAddFlashW(session, []string{"invalid username / password"}) + sessLogSave(session, w, r) + http.Redirect(w, r, r.Referer(), http.StatusSeeOther) + return } - - sessLogSave(session, w, r) - http.Redirect(w, r, r.Referer(), http.StatusSeeOther) - return } else if user == nil || user.Password != password { sessAddFlashW(session, []string{"invalid username / password"}) sessLogSave(session, w, r) diff --git a/server/ctrlsubsonic/ctrl.go b/server/ctrlsubsonic/ctrl.go index dcb903a..dca5265 100644 --- a/server/ctrlsubsonic/ctrl.go +++ b/server/ctrlsubsonic/ctrl.go @@ -10,6 +10,7 @@ import ( "io" "log" "net/http" + "strings" "time" "go.senan.xyz/gonic/db" @@ -231,18 +232,35 @@ func withUser(dbc *db.DB, ldapConfig ldap.Config) handlerutil.Middleware { params := r.Context().Value(CtxParams).(params.Params) // ignoring errors here, a middleware has already ensured they exist username, _ := params.Get("u") - password, _ := params.Get("p") + passwordHex, _ := params.Get("p") token, _ := params.Get("t") salt, _ := params.Get("s") passwordAuth := token == "" && salt == "" - tokenAuth := password == "" + tokenAuth := passwordHex == "" if tokenAuth == passwordAuth { _ = writeResp(w, r, spec.NewError(10, "please provide `t` and `s`, or just `p`")) return } + var password string + if passwordAuth && passwordHex != "" { + if strings.HasPrefix(passwordHex, "enc:") { + raw := strings.TrimPrefix(passwordHex, "enc:") + decoded, err := hex.DecodeString(raw) + if err != nil { + log.Println("Failed to decode hex password:", err) + _ = writeResp(w, r, spec.NewError(40, "invalid password encoding")) + return + } + password = string(decoded) + } else { + // if not prefixed with "enc:", treat as plain text + password = passwordHex + } + } + user := dbc.GetUserByName(username) if ldapConfig.IsSetup() {