Compare commits
2 commits
0d0438135c
...
b014c3fb3c
| Author | SHA1 | Date | |
|---|---|---|---|
| b014c3fb3c | |||
| fa494910cf |
6 changed files with 79 additions and 24 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,3 +1,4 @@
|
|||
vendor/
|
||||
chldapasswd
|
||||
config.json
|
||||
bindata.go
|
||||
11
go.mod
Normal file
11
go.mod
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
module git.nemunai.re/chldapasswd
|
||||
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/amoghe/go-crypt v0.0.0-20191109212615-b2ff80594b7f
|
||||
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
|
||||
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect
|
||||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
|
||||
gopkg.in/ldap.v2 v2.5.1
|
||||
)
|
||||
10
go.sum
Normal file
10
go.sum
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
github.com/amoghe/go-crypt v0.0.0-20191109212615-b2ff80594b7f h1:JxPBJknH9/9Yp0BPLZII8Cn4vaWPNsFOdkmpIwPhO8A=
|
||||
github.com/amoghe/go-crypt v0.0.0-20191109212615-b2ff80594b7f/go.mod h1:eFiR01PwTcpbzXtdMces7zxg6utvFM5puiWHpWB8D/k=
|
||||
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
|
||||
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
|
||||
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d h1:TxyelI5cVkbREznMhfzycHdkp5cLA7DpE+GKjSslYhM=
|
||||
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d/go.mod h1:cuepJuh7vyXfUyUwEgHQXw849cJrilpS5NeIjOWESAw=
|
||||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
|
||||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
|
||||
gopkg.in/ldap.v2 v2.5.1 h1:wiu0okdNfjlBzg6UWvd1Hn8Y+Ux17/u/4nlk4CQr6tU=
|
||||
gopkg.in/ldap.v2 v2.5.1/go.mod h1:oI0cpe/D7HRtBQl8aTg+ZmzFUAvu4lsv3eLXMLGFxWk=
|
||||
4
ldap.go
4
ldap.go
|
|
@ -19,6 +19,10 @@ type LDAP struct {
|
|||
BaseDN string
|
||||
ServiceDN string
|
||||
ServicePassword string
|
||||
MailHost string
|
||||
MailPort int
|
||||
MailUser string
|
||||
MailPassword string
|
||||
}
|
||||
|
||||
func (l LDAP) Connect() (*LDAPConn, error) {
|
||||
|
|
|
|||
53
lost.go
53
lost.go
|
|
@ -116,33 +116,44 @@ func lostPassword(w http.ResponseWriter, r *http.Request) {
|
|||
m.SetHeader("Subject", "SSO nemunai.re: password recovery")
|
||||
m.SetBody("text/plain", "Hello "+cn+"!\n\nSomeone, and we hope it's you, requested to reset your account password. \nIn order to continue, go to:\nhttps://ldap.nemunai.re/reset?l="+r.PostFormValue("login")+"&t="+token+"\n\nBest regards,\n-- \nnemunai.re SSO")
|
||||
|
||||
// Using local sendmail: delegate to the local admin sys the responsability to transport the mail
|
||||
s := gomail.SendFunc(func(from string, to []string, msg io.WriterTo) error {
|
||||
cmd := exec.Command("sendmail", "-t")
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
pw, err := cmd.StdinPipe()
|
||||
var s gomail.Sender
|
||||
if myLDAP.MailHost != "" {
|
||||
d := gomail.NewDialer(myLDAP.MailHost, myLDAP.MailPort, myLDAP.MailUser, myLDAP.MailPassword)
|
||||
s, err = d.Dial()
|
||||
if err != nil {
|
||||
return err
|
||||
log.Println("Unable to connect to email server: " + err.Error())
|
||||
displayTmplError(w, http.StatusInternalServerError, "lost.html", map[string]interface{}{"error": "Unable to connect to email server: " + err.Error()})
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// Using local sendmail: delegate to the local admin sys the responsability to transport the mail
|
||||
s = gomail.SendFunc(func(from string, to []string, msg io.WriterTo) error {
|
||||
cmd := exec.Command("sendmail", "-t")
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var errs [3]error
|
||||
_, errs[0] = m.WriteTo(pw)
|
||||
errs[1] = pw.Close()
|
||||
errs[2] = cmd.Wait()
|
||||
for _, err = range errs {
|
||||
pw, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var errs [3]error
|
||||
_, errs[0] = m.WriteTo(pw)
|
||||
errs[1] = pw.Close()
|
||||
errs[2] = cmd.Wait()
|
||||
for _, err = range errs {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
if err := gomail.Send(s, m); err != nil {
|
||||
log.Println("Unable to send email: " + err.Error())
|
||||
|
|
|
|||
24
main.go
24
main.go
|
|
@ -18,9 +18,10 @@ import (
|
|||
)
|
||||
|
||||
var myLDAP = LDAP{
|
||||
Host: "localhost",
|
||||
Port: 389,
|
||||
BaseDN: "dc=example,dc=com",
|
||||
Host: "localhost",
|
||||
Port: 389,
|
||||
BaseDN: "dc=example,dc=com",
|
||||
MailPort: 587,
|
||||
}
|
||||
|
||||
type ResponseWriterPrefix struct {
|
||||
|
|
@ -125,6 +126,23 @@ func main() {
|
|||
myLDAP.ServicePassword = val
|
||||
}
|
||||
|
||||
if val, ok := os.LookupEnv("SMTP_HOST"); ok {
|
||||
myLDAP.MailHost = val
|
||||
}
|
||||
if val, ok := os.LookupEnv("SMTP_PORT"); ok {
|
||||
if port, err := strconv.Atoi(val); err == nil {
|
||||
myLDAP.MailPort = port
|
||||
} else {
|
||||
log.Println("Invalid value for SMTP_PORT:", val)
|
||||
}
|
||||
}
|
||||
if val, ok := os.LookupEnv("SMTP_USER"); ok {
|
||||
myLDAP.MailUser = val
|
||||
}
|
||||
if val, ok := os.LookupEnv("SMTP_PASSWORD"); ok {
|
||||
myLDAP.MailPassword = val
|
||||
}
|
||||
|
||||
// Prepare graceful shutdown
|
||||
interrupt := make(chan os.Signal, 1)
|
||||
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue