From e4d9b92061c7d51d1a237ee0cbea6ff236e30589 Mon Sep 17 00:00:00 2001 From: Shin'ya Minazuki Date: Wed, 7 Jan 2026 16:37:02 -0300 Subject: [PATCH] * Rename fsldap to efsldap * Use gopkg.in/ini.v1 over github.com/joho/godotenv * Fix typos Signed-off-by: Shin'ya Minazuki --- .env.default | 5 ----- .gitignore | 1 + COPYING | 19 +++++++++++++++++- Makefile | 12 ++++++----- README.org | 6 +++--- config/config.go | 43 ++++++++++++++++++++++++++++++++++++++++ efsldap.ini | 5 +++++ go.mod | 8 +++++--- go.sum | 25 +++++++++++++++++++++-- handler/ldap_handlers.go | 13 +++++++----- fsldap.go => main.go | 22 +++++++++++++++++--- utils/dotenv.go | 39 ------------------------------------ utils/ldap.go | 18 ++++++++++++++--- 13 files changed, 147 insertions(+), 69 deletions(-) delete mode 100644 .env.default create mode 100644 .gitignore create mode 100644 config/config.go create mode 100644 efsldap.ini rename fsldap.go => main.go (58%) delete mode 100644 utils/dotenv.go diff --git a/.env.default b/.env.default deleted file mode 100644 index 33cc766..0000000 --- a/.env.default +++ /dev/null @@ -1,5 +0,0 @@ -SMTP_HOSTNAME = "localhost" -SMTP_PORT = "25" -SMTP_DOMAIN = "example.com" -LDAP_BASE = "dn=example,dn=com" -LDAP_UID = "cn" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c2495a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/efsldap diff --git a/COPYING b/COPYING index 4291d78..54b7b51 100644 --- a/COPYING +++ b/COPYING @@ -1,6 +1,7 @@ Discordian Public License 2.3 (DPL-2.3) All Rites Reversed (ΔΈ) 3189 Czar +All Rites Reversed (k) 3192 Shin'ya Minazuki Permission is hereby granted, to any person obtaining a copy of this material without restriction, including but not limited the rights to @@ -9,4 +10,20 @@ copies of the material, subject to the following conditions: YOU AGREE THAT THERE IS NO GODDESS BUT GODDESS AND SHE IS YOUR GODDESS & THAT THERE IS NO ERISIAN MOVEMENT BUT THE ERISIAN MOVEMENT AND IT IS THE -ERISIAN MOVEMENT. \ No newline at end of file +ERISIAN MOVEMENT. + +--- + +Minazuki License + +Copyright (c) 2026 Shin'ya Minazuki + +You can do as you please with these files so long as you keep this notice +intact and make no false claims about the ownership of said files. + +Should you believe this is useful to you, feel free to reach out +the author via any of the available contact methods. + +There is no express and/or implied warranties whatsoever, so +whoever makes you think otherwise, is at fault for believing +it is. diff --git a/Makefile b/Makefile index 202d293..783cb4f 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,9 @@ -BINARY_NAME = fsldap +GO ?= go +GOFLAGS ?= -v -buildvcs=false -buildmode=exe build: - go build -v - -run: build - ./$(BINARY_NAME) + ${GO} build ${GOFLAGS} +clean: + rm -f esfldap +run: + ./efsldap diff --git a/README.org b/README.org index a40975c..25228d6 100644 --- a/README.org +++ b/README.org @@ -1,4 +1,4 @@ -#+TITLE: Fake SMTP LDAP Server +#+TITLE: Enhanced Fake SMTP LDAP Server #+AUTHOR: czar A LDAP server that use SMTP as authentication source @@ -8,13 +8,13 @@ A LDAP server that use SMTP as authentication source - LDAP SEARCH * Build - fsldap is a written in Go, so you will need to install the Golang compiler + efsldap is written in Go, so you will need to install the Golang compiler #+BEGIN_QUOTE $ make build #+END_QUOTE * Configuration - copy the =.env.default= to =.env= and set your variables accordly + copy the =efsldap.ini= to =/usr/pkg/etc/efsldap.ini= and set your variables accordly * Running #+BEGIN_QUOTE diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..43f8749 --- /dev/null +++ b/config/config.go @@ -0,0 +1,43 @@ +package config + +import ( + "errors" + "log" + "os" + "gopkg.in/ini.v1" +) + +var Config struct { + LDAPBase string + LDAPUID string + SMTPDomain string + SMTPHost string + SMTPPort string +} + +var ( + ConfigPath string +) + +func ConfInit() { + if _, err := os.Stat(ConfigPath); errors.Is(err, os.ErrNotExist) { + log.Fatal(err) + } + Parse(ConfigPath) +} + +func Parse(fn string) error { + cfg, err := ini.Load(fn) + if err != nil { + return err + } + + Config.LDAPBase = cfg.Section("").Key("ldap_base").String() + Config.LDAPUID = cfg.Section("").Key("ldap_uid").String() + Config.SMTPDomain = cfg.Section("").Key("smtp_domain").String() + Config.SMTPHost = cfg.Section("").Key("smtp_host").String() + Config.SMTPPort = cfg.Section("").Key("smtp_port").String() + + return nil +} + diff --git a/efsldap.ini b/efsldap.ini new file mode 100644 index 0000000..9d33b3e --- /dev/null +++ b/efsldap.ini @@ -0,0 +1,5 @@ +ldap_base = "dn=example,dn=com" +ldap_uid = "cn" +smtp_domain = "example.com" +smtp_host = "localhost" +smtp_port = "25" diff --git a/go.mod b/go.mod index 7aebf32..4096582 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,11 @@ -module gt.kalli.st/czar/fsldap +module git.laidback.moe/YakumoLabs/efsldap go 1.14 require ( - github.com/joho/godotenv v1.3.0 // indirect - github.com/lor00x/goldap v0.0.0-20180618054307-a546dffdd1a3 // indirect + github.com/lor00x/goldap v0.0.0-20180618054307-a546dffdd1a3 + github.com/spf13/pflag v1.0.10 + github.com/stretchr/testify v1.11.1 // indirect github.com/vjeantet/ldapserver v1.0.1 + gopkg.in/ini.v1 v1.67.0 ) diff --git a/go.sum b/go.sum index 45938f4..4778a0c 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,27 @@ -github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= -github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/lor00x/goldap v0.0.0-20180618054307-a546dffdd1a3 h1:wIONC+HMNRqmWBjuMxhatuSzHaljStc4gjDeKycxy0A= github.com/lor00x/goldap v0.0.0-20180618054307-a546dffdd1a3/go.mod h1:37YR9jabpiIxsb8X9VCIx8qFOjTDIIrIHHODa8C4gz0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= +github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/vjeantet/ldapserver v1.0.1 h1:3z+TCXhwwDLJC3pZCNbuECPDqC2x1R7qQQbswB1Qwoc= github.com/vjeantet/ldapserver v1.0.1/go.mod h1:YvUqhu5vYhmbcLReMLrm/Tq3S7Yj43kSVFvvol6Lh6k= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/handler/ldap_handlers.go b/handler/ldap_handlers.go index 0e8ad70..4e69a99 100644 --- a/handler/ldap_handlers.go +++ b/handler/ldap_handlers.go @@ -8,13 +8,16 @@ import ( "github.com/lor00x/goldap/message" ldap "github.com/vjeantet/ldapserver" - "gt.kalli.st/czar/fsldap/utils" + "git.laidback.moe/YakumoLabs/efsldap/config" + "git.laidback.moe/YakumoLabs/efsldap/utils" ) -var smtpHost = utils.Env("SMTP_HOSTNAME") -var smtpPort = utils.Env("SMTP_PORT") -var base = utils.Env("LDAP_BASE") -var smtpHostPort = fmt.Sprintf("%s:%s", smtpHost,smtpPort) +var ( + base = config.Config.LDAPBase + smtpHost = config.Config.SMTPHost + smtpPort = config.Config.SMTPPort + smtpHostPort = fmt.Sprintf("%s:%s", smtpHost,smtpPort) +) var tlsconfig = &tls.Config { InsecureSkipVerify: true, diff --git a/fsldap.go b/main.go similarity index 58% rename from fsldap.go rename to main.go index f554cd1..126fa5e 100644 --- a/fsldap.go +++ b/main.go @@ -7,12 +7,28 @@ import ( "syscall" ldap "github.com/vjeantet/ldapserver" - - "gt.kalli.st/czar/fsldap/handler" + + cfg "git.laidback.moe/YakumoLabs/efsldap/config" + "git.laidback.moe/YakumoLabs/efsldap/handler" + + flag "github.com/spf13/pflag" ) +var ( + configPath string +) func main() { + flag.StringVarP(&configPath, "config", "c", "", "Path to configuration file") + flag.Parse() + + if configPath != "" { + cfg.ConfigPath = configPath + cfg.ConfInit() + } else { + log.Fatal("Unable to load configuration file") + } + server := ldap.NewServer() routes := ldap.NewRouteMux() routes.Bind(handler.Bind) @@ -22,7 +38,7 @@ func main() { server.Handle(routes) - log.Print("Starting FSLDAP server...") + log.Print("Starting EFSLDAP server...") go func(){ err := server.ListenAndServe("127.0.0.1:389") if err != nil{ diff --git a/utils/dotenv.go b/utils/dotenv.go deleted file mode 100644 index c5d77cf..0000000 --- a/utils/dotenv.go +++ /dev/null @@ -1,39 +0,0 @@ -package utils - -import ( - "log" - "os" - - "github.com/joho/godotenv" -) - -var SmtpHostname, SmtpPort, Domain, Base, Uid string -var BaseLenght, UidLenght int - -func init(){ - loadEnv() -} - -func loadEnv(){ - log.Print("Loading .env file...") - err := godotenv.Load() - if err != nil { - log.Print("error loading .env file") - } - - Domain = Env("SMTP_DOMAIN") - Base = Env("LDAP_BASE") - Uid = Env("LDAP_UID") - BaseLenght = len(Base) + 1 - UidLenght = len(Uid) + 1 - log.Print("Base ",Base," BaseLenght ",BaseLenght) -} - - -func Env(name string) string{ - envVar := os.Getenv(name) - if envVar == "" { - log.Fatalf("error getting %s env variable", name) - } - return envVar -} diff --git a/utils/ldap.go b/utils/ldap.go index d4ba18a..ae3f8eb 100644 --- a/utils/ldap.go +++ b/utils/ldap.go @@ -3,6 +3,18 @@ package utils import ( "fmt" "strings" + "git.laidback.moe/YakumoLabs/efsldap/config" +) + +var ( + Base = config.Config.LDAPBase + Domain = config.Config.SMTPDomain + Uid = config.Config.LDAPUID +) + +var ( + BaseLength = len(Base) + 1 + UidLength = len(Uid) + 1 ) func GetMail(user string) string { @@ -14,11 +26,11 @@ func GetLdapName(user string) string { } func GetUser(name string) string { - endIndex := len(name) - BaseLenght + endIndex := len(name) - BaseLength if endIndex < 0 { return "" } - user := name[UidLenght:endIndex] + user := name[UidLength:endIndex] user = strings.TrimSuffix(user, "@"+Domain) @@ -26,7 +38,7 @@ func GetUser(name string) string { } func GetSearchUser(name string) string { - startIndex := UidLenght + 1 + startIndex := UidLength + 1 endIndex := len(name) - 1 if endIndex < 0 { return ""