2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-09-01 20:51:44 +00:00
|
|
|
|
2019-03-26 19:53:58 +00:00
|
|
|
package api
|
2014-09-01 20:51:44 +00:00
|
|
|
|
|
|
|
import (
|
2018-09-11 21:25:24 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
2020-03-24 11:56:43 +00:00
|
|
|
"net"
|
2014-09-01 20:51:44 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-03-31 07:56:04 +00:00
|
|
|
ldap "github.com/go-ldap/ldap/v3"
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2024-01-04 10:07:12 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
2015-11-08 20:05:36 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2016-05-26 07:02:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2023-10-06 11:00:58 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2014-09-01 20:51:44 +00:00
|
|
|
)
|
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
const (
|
|
|
|
maxSessionLifetime = 7 * 24 * time.Hour
|
|
|
|
maxActiveSessions = 25
|
|
|
|
randomTokenLength = 64
|
2014-09-01 20:51:44 +00:00
|
|
|
)
|
|
|
|
|
2021-04-13 08:14:44 +00:00
|
|
|
func emitLoginAttempt(success bool, username, address string, evLogger events.Logger) {
|
2019-08-15 14:29:37 +00:00
|
|
|
evLogger.Log(events.LoginAttempt, map[string]interface{}{
|
2021-04-13 08:14:44 +00:00
|
|
|
"success": success,
|
|
|
|
"username": username,
|
|
|
|
"remoteAddress": address,
|
2015-11-08 20:05:36 +00:00
|
|
|
})
|
2021-04-15 05:33:02 +00:00
|
|
|
if !success {
|
|
|
|
l.Infof("Wrong credentials supplied during API authorization from %s", address)
|
|
|
|
}
|
2015-11-08 20:05:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 11:00:58 +00:00
|
|
|
func antiBruteForceSleep() {
|
|
|
|
time.Sleep(time.Duration(rand.Intn(100)+100) * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2023-11-14 10:57:39 +00:00
|
|
|
func unauthorized(w http.ResponseWriter, shortID string) {
|
|
|
|
w.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm="Authorization Required (%s)"`, shortID))
|
2023-10-06 11:00:58 +00:00
|
|
|
http.Error(w, "Not Authorized", http.StatusUnauthorized)
|
|
|
|
}
|
|
|
|
|
|
|
|
func forbidden(w http.ResponseWriter) {
|
|
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isNoAuthPath(path string) bool {
|
|
|
|
// Local variable instead of module var to prevent accidental mutation
|
|
|
|
noAuthPaths := []string{
|
|
|
|
"/",
|
|
|
|
"/index.html",
|
|
|
|
"/modal.html",
|
|
|
|
"/rest/svc/lang", // Required to load language settings on login page
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local variable instead of module var to prevent accidental mutation
|
|
|
|
noAuthPrefixes := []string{
|
|
|
|
// Static assets
|
|
|
|
"/assets/",
|
|
|
|
"/syncthing/",
|
|
|
|
"/vendor/",
|
|
|
|
"/theme-assets/", // This leaks information from config, but probably not sensitive
|
|
|
|
|
|
|
|
// No-auth API endpoints
|
|
|
|
"/rest/noauth",
|
|
|
|
}
|
|
|
|
|
|
|
|
return slices.Contains(noAuthPaths, path) ||
|
|
|
|
slices.ContainsFunc(noAuthPrefixes, func(prefix string) bool {
|
|
|
|
return strings.HasPrefix(path, prefix)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
type basicAuthAndSessionMiddleware struct {
|
|
|
|
cookieName string
|
|
|
|
shortID string
|
|
|
|
guiCfg config.GUIConfiguration
|
|
|
|
ldapCfg config.LDAPConfiguration
|
|
|
|
next http.Handler
|
|
|
|
evLogger events.Logger
|
|
|
|
tokens *tokenManager
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBasicAuthAndSessionMiddleware(cookieName, shortID string, guiCfg config.GUIConfiguration, ldapCfg config.LDAPConfiguration, next http.Handler, evLogger events.Logger, miscDB *db.NamespacedKV) *basicAuthAndSessionMiddleware {
|
|
|
|
return &basicAuthAndSessionMiddleware{
|
|
|
|
cookieName: cookieName,
|
|
|
|
shortID: shortID,
|
|
|
|
guiCfg: guiCfg,
|
|
|
|
ldapCfg: ldapCfg,
|
|
|
|
next: next,
|
|
|
|
evLogger: evLogger,
|
|
|
|
tokens: newTokenManager("sessions", miscDB, maxSessionLifetime, maxActiveSessions),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *basicAuthAndSessionMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if hasValidAPIKeyHeader(r, m.guiCfg) {
|
|
|
|
m.next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2014-09-01 20:51:44 +00:00
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
for _, cookie := range r.Cookies() {
|
|
|
|
// We iterate here since there may, historically, be multiple
|
|
|
|
// cookies with the same name but different path. Any "old" ones
|
|
|
|
// won't match an existing session and will be ignored, then
|
|
|
|
// later removed on logout or when timing out.
|
|
|
|
if cookie.Name == m.cookieName {
|
|
|
|
if m.tokens.Check(cookie.Value) {
|
|
|
|
m.next.ServeHTTP(w, r)
|
|
|
|
return
|
2014-09-01 20:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-04 10:07:12 +00:00
|
|
|
}
|
2014-09-01 20:51:44 +00:00
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
// Fall back to Basic auth if provided
|
|
|
|
if username, ok := attemptBasicAuth(r, m.guiCfg, m.ldapCfg, m.evLogger); ok {
|
|
|
|
m.createSession(username, false, w, r)
|
|
|
|
m.next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2014-09-01 20:51:44 +00:00
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
// Exception for static assets and REST calls that don't require authentication.
|
|
|
|
if isNoAuthPath(r.URL.Path) {
|
|
|
|
m.next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2023-10-10 05:48:35 +00:00
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
// Some browsers don't send the Authorization request header unless prompted by a 401 response.
|
|
|
|
// This enables https://user:pass@localhost style URLs to keep working.
|
|
|
|
if m.guiCfg.SendBasicAuthPrompt {
|
|
|
|
unauthorized(w, m.shortID)
|
|
|
|
return
|
|
|
|
}
|
2014-09-01 20:51:44 +00:00
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
forbidden(w)
|
2023-10-06 11:00:58 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
func (m *basicAuthAndSessionMiddleware) passwordAuthHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var req struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
StayLoggedIn bool
|
|
|
|
}
|
|
|
|
if err := unmarshalTo(r.Body, &req); err != nil {
|
|
|
|
l.Debugln("Failed to parse username and password:", err)
|
|
|
|
http.Error(w, "Failed to parse username and password.", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2016-04-18 20:24:38 +00:00
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
if auth(req.Username, req.Password, m.guiCfg, m.ldapCfg) {
|
|
|
|
m.createSession(req.Username, req.StayLoggedIn, w, r)
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
2014-09-01 20:51:44 +00:00
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
emitLoginAttempt(false, req.Username, r.RemoteAddr, m.evLogger)
|
|
|
|
antiBruteForceSleep()
|
|
|
|
forbidden(w)
|
2023-10-06 11:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func attemptBasicAuth(r *http.Request, guiCfg config.GUIConfiguration, ldapCfg config.LDAPConfiguration, evLogger events.Logger) (string, bool) {
|
|
|
|
username, password, ok := r.BasicAuth()
|
|
|
|
if !ok {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Debugln("Sessionless HTTP request with authentication; this is expensive.")
|
|
|
|
|
|
|
|
if auth(username, password, guiCfg, ldapCfg) {
|
|
|
|
return username, true
|
|
|
|
}
|
|
|
|
|
|
|
|
usernameFromIso := string(iso88591ToUTF8([]byte(username)))
|
|
|
|
passwordFromIso := string(iso88591ToUTF8([]byte(password)))
|
|
|
|
if auth(usernameFromIso, passwordFromIso, guiCfg, ldapCfg) {
|
|
|
|
return usernameFromIso, true
|
|
|
|
}
|
|
|
|
|
|
|
|
emitLoginAttempt(false, username, r.RemoteAddr, evLogger)
|
2023-10-10 05:48:35 +00:00
|
|
|
antiBruteForceSleep()
|
2023-10-06 11:00:58 +00:00
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
func (m *basicAuthAndSessionMiddleware) createSession(username string, persistent bool, w http.ResponseWriter, r *http.Request) {
|
|
|
|
sessionid := m.tokens.New()
|
2023-10-06 11:00:58 +00:00
|
|
|
|
|
|
|
// Best effort detection of whether the connection is HTTPS --
|
|
|
|
// either directly to us, or as used by the client towards a reverse
|
|
|
|
// proxy who sends us headers.
|
|
|
|
connectionIsHTTPS := r.TLS != nil ||
|
|
|
|
strings.ToLower(r.Header.Get("x-forwarded-proto")) == "https" ||
|
|
|
|
strings.Contains(strings.ToLower(r.Header.Get("forwarded")), "proto=https")
|
|
|
|
// If the connection is HTTPS, or *should* be HTTPS, set the Secure
|
|
|
|
// bit in cookies.
|
2024-01-04 10:07:12 +00:00
|
|
|
useSecureCookie := connectionIsHTTPS || m.guiCfg.UseTLS()
|
2023-10-06 11:00:58 +00:00
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
maxAge := 0
|
|
|
|
if persistent {
|
|
|
|
maxAge = int(maxSessionLifetime.Seconds())
|
|
|
|
}
|
2023-10-06 11:00:58 +00:00
|
|
|
http.SetCookie(w, &http.Cookie{
|
2024-01-04 10:07:12 +00:00
|
|
|
Name: m.cookieName,
|
2023-10-06 11:00:58 +00:00
|
|
|
Value: sessionid,
|
|
|
|
// In HTTP spec Max-Age <= 0 means delete immediately,
|
|
|
|
// but in http.Cookie MaxAge = 0 means unspecified (session) and MaxAge < 0 means delete immediately
|
2024-01-04 10:07:12 +00:00
|
|
|
MaxAge: maxAge,
|
2023-10-06 11:00:58 +00:00
|
|
|
Secure: useSecureCookie,
|
|
|
|
Path: "/",
|
|
|
|
})
|
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
emitLoginAttempt(true, username, r.RemoteAddr, m.evLogger)
|
2023-10-06 11:00:58 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
func (m *basicAuthAndSessionMiddleware) handleLogout(w http.ResponseWriter, r *http.Request) {
|
|
|
|
for _, cookie := range r.Cookies() {
|
|
|
|
// We iterate here since there may, historically, be multiple
|
|
|
|
// cookies with the same name but different path. We drop them
|
|
|
|
// all.
|
|
|
|
if cookie.Name == m.cookieName {
|
|
|
|
m.tokens.Delete(cookie.Value)
|
|
|
|
|
|
|
|
// Delete the cookie
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: m.cookieName,
|
|
|
|
Value: "",
|
|
|
|
MaxAge: -1,
|
|
|
|
Secure: cookie.Secure,
|
|
|
|
Path: cookie.Path,
|
|
|
|
})
|
2023-10-06 11:00:58 +00:00
|
|
|
}
|
2024-01-04 10:07:12 +00:00
|
|
|
}
|
2023-11-13 19:37:29 +00:00
|
|
|
|
2024-01-04 10:07:12 +00:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2014-09-01 20:51:44 +00:00
|
|
|
}
|
2016-04-18 20:24:38 +00:00
|
|
|
|
2018-09-11 21:25:24 +00:00
|
|
|
func auth(username string, password string, guiCfg config.GUIConfiguration, ldapCfg config.LDAPConfiguration) bool {
|
|
|
|
if guiCfg.AuthMode == config.AuthModeLDAP {
|
|
|
|
return authLDAP(username, password, ldapCfg)
|
|
|
|
} else {
|
2021-11-08 12:32:04 +00:00
|
|
|
return authStatic(username, password, guiCfg)
|
2018-09-11 21:25:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:32:04 +00:00
|
|
|
func authStatic(username string, password string, guiCfg config.GUIConfiguration) bool {
|
|
|
|
return guiCfg.CompareHashedPassword(password) == nil && username == guiCfg.User
|
2018-09-11 21:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func authLDAP(username string, password string, cfg config.LDAPConfiguration) bool {
|
|
|
|
address := cfg.Address
|
2020-03-24 11:56:43 +00:00
|
|
|
hostname, _, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
hostname = address
|
|
|
|
}
|
2018-09-11 21:25:24 +00:00
|
|
|
var connection *ldap.Conn
|
|
|
|
if cfg.Transport == config.LDAPTransportTLS {
|
2020-03-24 11:56:43 +00:00
|
|
|
connection, err = ldap.DialTLS("tcp", address, &tls.Config{
|
|
|
|
ServerName: hostname,
|
|
|
|
InsecureSkipVerify: cfg.InsecureSkipVerify,
|
|
|
|
})
|
2018-09-11 21:25:24 +00:00
|
|
|
} else {
|
|
|
|
connection, err = ldap.Dial("tcp", address)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln("LDAP Dial:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Transport == config.LDAPTransportStartTLS {
|
|
|
|
err = connection.StartTLS(&tls.Config{InsecureSkipVerify: cfg.InsecureSkipVerify})
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln("LDAP Start TLS:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer connection.Close()
|
|
|
|
|
2023-10-07 02:29:53 +00:00
|
|
|
bindDN := formatOptionalPercentS(cfg.BindDN, escapeForLDAPDN(username))
|
|
|
|
err = connection.Bind(bindDN, password)
|
2018-09-11 21:25:24 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Warnln("LDAP Bind:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
lib/api: Add LDAP search filters (fixes #5376) (#6488)
This adds the functionality to run a user search with a filter for LDAP
authentication. The search is done after successful bind, as the binding
user. The typical use case is to limit authentication to users who are
member of a group or under a certain OU. For example, to only match
users in the "Syncthing" group in otherwise default Active Directory
set up for example.com:
<searchBaseDN>CN=Users,DC=example,DC=com</searchBaseDN>
<searchFilter>(&(sAMAccountName=%s)(memberOf=CN=Syncthing,CN=Users,DC=example,DC=com))</searchFilter>
The search filter is an "and" of two criteria (with the ampersand being
XML quoted),
- "(sAMAccountName=%s)" matches the user logging in
- "(memberOf=CN=Syncthing,CN=Users,DC=example,DC=com)" matches members
of the group in question.
Authentication will only proceed if the search filter matches precisely
one user.
2020-04-04 09:33:43 +00:00
|
|
|
if cfg.SearchFilter == "" && cfg.SearchBaseDN == "" {
|
|
|
|
// We're done here.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.SearchFilter == "" || cfg.SearchBaseDN == "" {
|
|
|
|
l.Warnln("LDAP configuration: both searchFilter and searchBaseDN must be set, or neither.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a search filter and search base is set we do an LDAP search for
|
|
|
|
// the user. If this matches precisely one user then we are good to go.
|
|
|
|
// The search filter uses the same %s interpolation as the bind DN.
|
|
|
|
|
2023-10-07 02:29:53 +00:00
|
|
|
searchString := formatOptionalPercentS(cfg.SearchFilter, escapeForLDAPFilter(username))
|
lib/api: Add LDAP search filters (fixes #5376) (#6488)
This adds the functionality to run a user search with a filter for LDAP
authentication. The search is done after successful bind, as the binding
user. The typical use case is to limit authentication to users who are
member of a group or under a certain OU. For example, to only match
users in the "Syncthing" group in otherwise default Active Directory
set up for example.com:
<searchBaseDN>CN=Users,DC=example,DC=com</searchBaseDN>
<searchFilter>(&(sAMAccountName=%s)(memberOf=CN=Syncthing,CN=Users,DC=example,DC=com))</searchFilter>
The search filter is an "and" of two criteria (with the ampersand being
XML quoted),
- "(sAMAccountName=%s)" matches the user logging in
- "(memberOf=CN=Syncthing,CN=Users,DC=example,DC=com)" matches members
of the group in question.
Authentication will only proceed if the search filter matches precisely
one user.
2020-04-04 09:33:43 +00:00
|
|
|
const sizeLimit = 2 // we search for up to two users -- we only want to match one, so getting any number >1 is a failure.
|
|
|
|
const timeLimit = 60 // Search for up to a minute...
|
|
|
|
searchReq := ldap.NewSearchRequest(cfg.SearchBaseDN, ldap.ScopeWholeSubtree, ldap.DerefFindingBaseObj, sizeLimit, timeLimit, false, searchString, nil, nil)
|
|
|
|
|
|
|
|
res, err := connection.Search(searchReq)
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln("LDAP Search:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(res.Entries) != 1 {
|
|
|
|
l.Infof("Wrong number of LDAP search results, %d != 1", len(res.Entries))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-09-11 21:25:24 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-10-07 02:29:53 +00:00
|
|
|
// escapeForLDAPFilter escapes a value that will be used in a filter clause
|
|
|
|
func escapeForLDAPFilter(value string) string {
|
|
|
|
// https://social.technet.microsoft.com/wiki/contents/articles/5392.active-directory-ldap-syntax-filters.aspx#Special_Characters
|
|
|
|
// Backslash must always be first in the list so we don't double escape them.
|
|
|
|
return escapeRunes(value, []rune{'\\', '*', '(', ')', 0})
|
|
|
|
}
|
|
|
|
|
|
|
|
// escapeForLDAPDN escapes a value that will be used in a bind DN
|
|
|
|
func escapeForLDAPDN(value string) string {
|
|
|
|
// https://social.technet.microsoft.com/wiki/contents/articles/5312.active-directory-characters-to-escape.aspx
|
|
|
|
// Backslash must always be first in the list so we don't double escape them.
|
|
|
|
return escapeRunes(value, []rune{'\\', ',', '#', '+', '<', '>', ';', '"', '=', ' ', 0})
|
|
|
|
}
|
|
|
|
|
|
|
|
func escapeRunes(value string, runes []rune) string {
|
|
|
|
for _, e := range runes {
|
|
|
|
value = strings.ReplaceAll(value, string(e), fmt.Sprintf("\\%X", e))
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatOptionalPercentS(template string, username string) string {
|
|
|
|
var replacements []any
|
|
|
|
nReps := strings.Count(template, "%s") - strings.Count(template, "%%s")
|
|
|
|
if nReps < 0 {
|
|
|
|
nReps = 0
|
|
|
|
}
|
|
|
|
for i := 0; i < nReps; i++ {
|
|
|
|
replacements = append(replacements, username)
|
2023-05-10 05:52:02 +00:00
|
|
|
}
|
2023-10-07 02:29:53 +00:00
|
|
|
return fmt.Sprintf(template, replacements...)
|
2023-05-10 05:52:02 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 20:24:38 +00:00
|
|
|
// Convert an ISO-8859-1 encoded byte string to UTF-8. Works by the
|
|
|
|
// principle that ISO-8859-1 bytes are equivalent to unicode code points,
|
|
|
|
// that a rune slice is a list of code points, and that stringifying a slice
|
|
|
|
// of runes generates UTF-8 in Go.
|
|
|
|
func iso88591ToUTF8(s []byte) []byte {
|
|
|
|
runes := make([]rune, len(s))
|
|
|
|
for i := range s {
|
|
|
|
runes[i] = rune(s[i])
|
|
|
|
}
|
|
|
|
return []byte(string(runes))
|
|
|
|
}
|