2020-04-11 20:30:41 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-10-01 00:13:41 +00:00
|
|
|
"log"
|
|
|
|
"log/syslog"
|
2020-04-11 20:30:41 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2021-10-01 00:13:41 +00:00
|
|
|
"strings"
|
2020-04-11 20:30:41 +00:00
|
|
|
|
|
|
|
"github.com/go-ldap/ldap/v3"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-10-01 00:13:41 +00:00
|
|
|
rootDN = "dc=example,dc=com"
|
|
|
|
bindUsername = "cn=sftpgo," + rootDN
|
|
|
|
bindURL = "ldap:///" // That is, the server on the default port of localhost.
|
|
|
|
passwordFile = "/etc/sftpgo/admin-password.txt" // make this file readable only by the server
|
|
|
|
publicDir = "/var/www/webdav/public"
|
2020-04-11 20:30:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type userFilters struct {
|
|
|
|
DeniedLoginMethods []string `json:"denied_login_methods,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type minimalSFTPGoUser struct {
|
|
|
|
Status int `json:"status,omitempty"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
HomeDir string `json:"home_dir,omitempty"`
|
|
|
|
UID int `json:"uid,omitempty"`
|
|
|
|
GID int `json:"gid,omitempty"`
|
|
|
|
Permissions map[string][]string `json:"permissions"`
|
|
|
|
Filters userFilters `json:"filters"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func exitError() {
|
2021-10-01 00:13:41 +00:00
|
|
|
log.Printf("exitError\n")
|
2020-04-11 20:30:41 +00:00
|
|
|
u := minimalSFTPGoUser{
|
|
|
|
Username: "",
|
|
|
|
}
|
2020-08-18 19:21:01 +00:00
|
|
|
resp, _ := json.Marshal(u)
|
|
|
|
fmt.Printf("%v\n", string(resp))
|
2020-04-11 20:30:41 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-10-01 00:13:41 +00:00
|
|
|
func printSuccessResponse(username, homeDir string, uid, gid int, permissions []string) {
|
2020-04-11 20:30:41 +00:00
|
|
|
u := minimalSFTPGoUser{
|
|
|
|
Username: username,
|
|
|
|
HomeDir: homeDir,
|
|
|
|
UID: uid,
|
|
|
|
GID: gid,
|
|
|
|
Status: 1,
|
|
|
|
}
|
|
|
|
u.Permissions = make(map[string][]string)
|
2021-10-01 00:13:41 +00:00
|
|
|
u.Permissions["/"] = permissions
|
2020-04-11 20:30:41 +00:00
|
|
|
// uncomment the next line to require publickey+password authentication
|
|
|
|
//u.Filters.DeniedLoginMethods = []string{"publickey", "password", "keyboard-interactive", "publickey+keyboard-interactive"}
|
2020-08-18 19:21:01 +00:00
|
|
|
resp, _ := json.Marshal(u)
|
2021-10-01 00:13:41 +00:00
|
|
|
log.Printf("%v\n", string(resp))
|
2020-08-18 19:21:01 +00:00
|
|
|
fmt.Printf("%v\n", string(resp))
|
2020-04-11 20:30:41 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-10-01 00:13:41 +00:00
|
|
|
logWriter, err := syslog.New(syslog.LOG_NOTICE, "sftpgo")
|
|
|
|
if err == nil {
|
|
|
|
log.SetOutput(logWriter)
|
|
|
|
}
|
2020-04-11 20:30:41 +00:00
|
|
|
// get credentials from env vars
|
|
|
|
username := os.Getenv("SFTPGO_AUTHD_USERNAME")
|
|
|
|
password := os.Getenv("SFTPGO_AUTHD_PASSWORD")
|
|
|
|
publickey := os.Getenv("SFTPGO_AUTHD_PUBLIC_KEY")
|
2021-10-01 00:13:41 +00:00
|
|
|
if strings.ToLower(username) == "anonymous" {
|
|
|
|
printSuccessResponse("anonymous", publicDir, 0, 0, []string{"list", "download"})
|
|
|
|
return
|
|
|
|
}
|
2020-04-11 20:30:41 +00:00
|
|
|
l, err := ldap.DialURL(bindURL)
|
|
|
|
if err != nil {
|
2021-10-01 00:13:41 +00:00
|
|
|
log.Printf("DialURL: %s\n", err.Error())
|
2020-04-11 20:30:41 +00:00
|
|
|
exitError()
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
// bind to the ldap server with an account that can read users
|
2021-10-01 00:13:41 +00:00
|
|
|
bindPassword, err := os.ReadFile(passwordFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ReadFile(%s): %s\n", passwordFile, err.Error())
|
|
|
|
exitError()
|
|
|
|
}
|
|
|
|
err = l.Bind(bindUsername, string(bindPassword))
|
2020-04-11 20:30:41 +00:00
|
|
|
if err != nil {
|
2021-10-01 00:13:41 +00:00
|
|
|
log.Printf("Bind(%s): %s\n", bindUsername, err.Error())
|
2020-04-11 20:30:41 +00:00
|
|
|
exitError()
|
|
|
|
}
|
|
|
|
|
|
|
|
// search the user trying to login and fetch some attributes, this search string is tested against 389ds using the default configuration
|
2021-10-01 00:13:41 +00:00
|
|
|
log.Printf("username=%s\n", username)
|
2023-02-25 19:27:52 +00:00
|
|
|
searchFilter := fmt.Sprintf("(uid=%s)", ldap.EscapeFilter(username))
|
2020-04-11 20:30:41 +00:00
|
|
|
searchRequest := ldap.NewSearchRequest(
|
2021-10-01 00:13:41 +00:00
|
|
|
"ou=people," + rootDN,
|
2020-04-11 20:30:41 +00:00
|
|
|
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
2021-10-01 00:13:41 +00:00
|
|
|
searchFilter,
|
|
|
|
[]string{"dn", "uid", "homeDirectory", "uidNumber", "gidNumber", "nsSshPublicKey"},
|
2020-04-11 20:30:41 +00:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
sr, err := l.Search(searchRequest)
|
|
|
|
if err != nil {
|
2021-10-01 00:13:41 +00:00
|
|
|
log.Printf("Search(%s): %s\n", searchFilter, err.Error())
|
2020-04-11 20:30:41 +00:00
|
|
|
exitError()
|
|
|
|
}
|
|
|
|
|
|
|
|
// we expect exactly one user
|
|
|
|
if len(sr.Entries) != 1 {
|
2021-10-01 00:13:41 +00:00
|
|
|
log.Printf("Search(%s): %d entries\n", searchFilter, len(sr.Entries))
|
2020-04-11 20:30:41 +00:00
|
|
|
exitError()
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(publickey) > 0 {
|
|
|
|
// check public key
|
|
|
|
userKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(publickey))
|
|
|
|
if err != nil {
|
2021-10-01 00:13:41 +00:00
|
|
|
log.Printf("ParseAuthorizedKey(%s): %s\n", publickey, err.Error())
|
2020-04-11 20:30:41 +00:00
|
|
|
exitError()
|
|
|
|
}
|
|
|
|
authOk := false
|
|
|
|
for _, k := range sr.Entries[0].GetAttributeValues("nsSshPublicKey") {
|
|
|
|
key, _, _, _, err := ssh.ParseAuthorizedKey([]byte(k))
|
|
|
|
// we skip an invalid public key stored inside the LDAP server
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if bytes.Equal(key.Marshal(), userKey.Marshal()) {
|
|
|
|
authOk = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !authOk {
|
2021-10-01 00:13:41 +00:00
|
|
|
log.Printf("publickey %s !authOk\n", publickey)
|
2020-04-11 20:30:41 +00:00
|
|
|
exitError()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// bind to the LDAP server with the user dn and the given password to check the password
|
|
|
|
userdn := sr.Entries[0].DN
|
2021-10-01 00:13:41 +00:00
|
|
|
// log.Printf("password=%s\n", password)
|
2020-04-11 20:30:41 +00:00
|
|
|
err = l.Bind(userdn, password)
|
|
|
|
if err != nil {
|
2021-10-01 00:13:41 +00:00
|
|
|
log.Printf("Bind(%s): %s\n", userdn, err.Error())
|
2020-04-11 20:30:41 +00:00
|
|
|
exitError()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 00:13:41 +00:00
|
|
|
// People in the LDAP directory aren't necessarily Linux users;
|
|
|
|
// so they might not have a uidNumber or gidNumber.
|
|
|
|
uidNumber := sr.Entries[0].GetAttributeValue("uidNumber")
|
|
|
|
uid, err := strconv.Atoi(uidNumber)
|
2020-04-11 20:30:41 +00:00
|
|
|
if err != nil {
|
2021-10-01 00:13:41 +00:00
|
|
|
//log.Printf("uid Atoi(%s) = %s\n", uidNumber, err.Error())
|
|
|
|
uid = 0
|
2020-04-11 20:30:41 +00:00
|
|
|
}
|
2021-10-01 00:13:41 +00:00
|
|
|
gidNumber := sr.Entries[0].GetAttributeValue("gidNumber")
|
|
|
|
gid, err := strconv.Atoi(gidNumber)
|
2020-04-11 20:30:41 +00:00
|
|
|
if err != nil {
|
2021-10-01 00:13:41 +00:00
|
|
|
//log.Printf("gid Atoi(%s) = %s\n", gidNumber, err.Error())
|
|
|
|
gid = 0
|
|
|
|
}
|
|
|
|
homeDir := sr.Entries[0].GetAttributeValue("homeDirectory")
|
|
|
|
if (len(homeDir) <= 0) {
|
|
|
|
homeDir = publicDir // homeDir is a required attribute.
|
2020-04-11 20:30:41 +00:00
|
|
|
}
|
|
|
|
// return the authenticated user
|
2021-10-01 00:13:41 +00:00
|
|
|
printSuccessResponse(sr.Entries[0].GetAttributeValue("uid"), homeDir, uid, gid, []string{"*"})
|
2020-04-11 20:30:41 +00:00
|
|
|
}
|