2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07: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-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-09-01 13:51:44 -07:00
|
|
|
|
2019-03-26 12:53:58 -07:00
|
|
|
package api
|
2014-09-01 13:51:44 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-09-11 14:25:24 -07:00
|
|
|
"crypto/tls"
|
2014-09-01 13:51:44 -07:00
|
|
|
"encoding/base64"
|
2018-09-11 14:25:24 -07:00
|
|
|
"fmt"
|
2014-09-01 13:51:44 -07:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
2015-11-08 13:05:36 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2016-05-26 00:02:56 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/rand"
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2014-11-29 16:17:00 -07:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2019-02-02 02:11:42 -07:00
|
|
|
ldap "gopkg.in/ldap.v2"
|
2014-09-01 13:51:44 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-04-28 13:32:10 -07:00
|
|
|
sessions = make(map[string]bool)
|
|
|
|
sessionsMut = sync.NewMutex()
|
2014-09-01 13:51:44 -07:00
|
|
|
)
|
|
|
|
|
2015-11-08 13:05:36 -07:00
|
|
|
func emitLoginAttempt(success bool, username string) {
|
|
|
|
events.Default.Log(events.LoginAttempt, map[string]interface{}{
|
|
|
|
"success": success,
|
|
|
|
"username": username,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-11 14:25:24 -07:00
|
|
|
func basicAuthAndSessionMiddleware(cookieName string, guiCfg config.GUIConfiguration, ldapCfg config.LDAPConfiguration, next http.Handler) http.Handler {
|
2014-09-01 13:51:44 -07:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2018-09-11 14:25:24 -07:00
|
|
|
if guiCfg.IsValidAPIKey(r.Header.Get("X-API-Key")) {
|
2014-09-01 13:51:44 -07:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-22 08:57:08 -07:00
|
|
|
cookie, err := r.Cookie(cookieName)
|
2014-09-01 13:51:44 -07:00
|
|
|
if err == nil && cookie != nil {
|
|
|
|
sessionsMut.Lock()
|
|
|
|
_, ok := sessions[cookie.Value]
|
|
|
|
sessionsMut.Unlock()
|
|
|
|
if ok {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 12:53:58 -07:00
|
|
|
l.Debugln("Sessionless HTTP request with authentication; this is expensive.")
|
2015-04-19 22:29:38 -07:00
|
|
|
|
2014-09-01 13:51:44 -07:00
|
|
|
error := func() {
|
|
|
|
time.Sleep(time.Duration(rand.Intn(100)+100) * time.Millisecond)
|
|
|
|
w.Header().Set("WWW-Authenticate", "Basic realm=\"Authorization Required\"")
|
|
|
|
http.Error(w, "Not Authorized", http.StatusUnauthorized)
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr := r.Header.Get("Authorization")
|
|
|
|
if !strings.HasPrefix(hdr, "Basic ") {
|
|
|
|
error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr = hdr[6:]
|
|
|
|
bs, err := base64.StdEncoding.DecodeString(hdr)
|
|
|
|
if err != nil {
|
|
|
|
error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := bytes.SplitN(bs, []byte(":"), 2)
|
|
|
|
if len(fields) != 2 {
|
|
|
|
error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-08 13:05:36 -07:00
|
|
|
username := string(fields[0])
|
2018-09-11 14:25:24 -07:00
|
|
|
password := string(fields[1])
|
|
|
|
|
2019-02-02 02:11:42 -07:00
|
|
|
authOk := auth(username, password, guiCfg, ldapCfg)
|
2018-09-11 14:25:24 -07:00
|
|
|
if !authOk {
|
|
|
|
usernameIso := string(iso88591ToUTF8([]byte(username)))
|
|
|
|
passwordIso := string(iso88591ToUTF8([]byte(password)))
|
|
|
|
authOk = auth(usernameIso, passwordIso, guiCfg, ldapCfg)
|
|
|
|
if authOk {
|
|
|
|
username = usernameIso
|
|
|
|
}
|
2016-04-18 13:24:38 -07:00
|
|
|
}
|
|
|
|
|
2018-09-11 14:25:24 -07:00
|
|
|
if !authOk {
|
|
|
|
emitLoginAttempt(false, username)
|
|
|
|
error()
|
|
|
|
return
|
2014-09-01 13:51:44 -07:00
|
|
|
}
|
|
|
|
|
2016-05-26 00:02:56 -07:00
|
|
|
sessionid := rand.String(32)
|
2014-09-01 13:51:44 -07:00
|
|
|
sessionsMut.Lock()
|
|
|
|
sessions[sessionid] = true
|
|
|
|
sessionsMut.Unlock()
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
2015-06-22 08:57:08 -07:00
|
|
|
Name: cookieName,
|
2014-09-01 13:51:44 -07:00
|
|
|
Value: sessionid,
|
|
|
|
MaxAge: 0,
|
|
|
|
})
|
|
|
|
|
2015-11-08 13:05:36 -07:00
|
|
|
emitLoginAttempt(true, username)
|
2014-09-01 13:51:44 -07:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
2016-04-18 13:24:38 -07:00
|
|
|
|
2018-09-11 14:25:24 -07: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 {
|
|
|
|
return authStatic(username, password, guiCfg.User, guiCfg.Password)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func authStatic(username string, password string, configUser string, configPassword string) bool {
|
|
|
|
configPasswordBytes := []byte(configPassword)
|
|
|
|
passwordBytes := []byte(password)
|
|
|
|
return bcrypt.CompareHashAndPassword(configPasswordBytes, passwordBytes) == nil && username == configUser
|
|
|
|
}
|
|
|
|
|
|
|
|
func authLDAP(username string, password string, cfg config.LDAPConfiguration) bool {
|
|
|
|
address := cfg.Address
|
|
|
|
var connection *ldap.Conn
|
|
|
|
var err error
|
|
|
|
if cfg.Transport == config.LDAPTransportTLS {
|
|
|
|
connection, err = ldap.DialTLS("tcp", address, &tls.Config{InsecureSkipVerify: cfg.InsecureSkipVerify})
|
|
|
|
} 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()
|
|
|
|
|
|
|
|
err = connection.Bind(fmt.Sprintf(cfg.BindDN, username), password)
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln("LDAP Bind:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:24:38 -07: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))
|
|
|
|
}
|