package app
import (
"bytes"
"encoding/xml"
"io"
"log"
"net/http"
"strings"
"text/template"
"github.com/gin-gonic/gin"
)
const outlookTpl = `
email
settings
{{- if .IMAPEnabled }}
IMAP
{{ .IMAPServer }}
{{ .IMAPPort }}
on
off
{{ if .IMAPSSL }}on{{ else }}off{{ end }}
on
{{ .Username }}@{{ .Domain }}
{{- end }}
{{- if .POP3Enabled }}
POP3
mail.mdfmultimedia.com
995
on
off
{{ if .POP3SSL }}on{{ else }}off{{ end }}
on
{{ .Username }}@{{ .Domain }}
{{- end }}
{{- if .SMTPEnabled }}
SMTP
mail.mdfmultimedia.com
465
on
off
{{ if .SMTPSSL }}on{{ else }}off{{ end }}
on
{{ .Username }}@{{ .Domain }}
{{- end }}
`
var outlookTemplate *template.Template
func RenderOutlook(g *gin.Context) {
var err error
var body []byte
var domain Domain
var ok bool
var b bytes.Buffer
body, err = io.ReadAll(g.Request.Body)
if err != nil {
g.JSON(404, gin.H{"code": "WRONG_BODY", "message": "malformed request"})
return
}
var e struct {
Address string `xml:"Request>EMailAddress"`
}
err = xml.Unmarshal(body, &e)
if err != nil {
g.JSON(404, gin.H{"code": "PAGE_NOT_FOUND", "message": "Page not found"})
return
}
components := strings.Split(e.Address, "@")
username, reqDomain := components[0], components[1]
domain, ok = Domains[reqDomain]
if !ok {
g.JSON(404, gin.H{"code": "PAGE_NOT_FOUND", "message": "Page not found"})
return
}
domain.Username = username
err = outlookTemplate.Execute(&b, domain)
if err != nil {
log.Fatal(err)
}
g.Header("Content-Type", "application/xml; charset=utf-8")
g.String(http.StatusOK, b.String())
}
// curl -X POST -d @req.xml http://127.0.0.1:8888/autodiscover/autodiscover.xml
// curl --basic -X "POST" -u XXX@YYY -v https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml -H "Content-Type: text/xml" -d @a.txt
/*
testuser@company.tld
http://schemas.microsoft.com/exchange/autodiscover/mobilesync/responseschema/2006
$ curl -d @request.xml -u testuser@company.tld -H "Content-Type: text/xml" -v https://autodiscover.company.tld/autodiscover/autodiscover.xml
*/
/*
1 - Lookup di un record A (o CNAME) per contoso.com che punta ad un web server che risponderà con un URL HTTPS https://contoso.com/Autodiscover/Autodiscover.xml.
2 - Lookup di un record A (o CNAME) per autodiscover.contoso.com che punta ad un web server che risponderà con un URL HTTPS https://autodiscover.contoso.com/Autodiscover/Autodiscover.xml
3 - Lookup di un record A (o CNAME) per contoso.com che punta ad un web server che risponderà con URL HTTP http://autodiscover.contoso.com/Autodiscover/Autodiscover.xml (se avete un reverse proxy o direttamente su Exchange, dovrete fare la configurazione necessaria per implementare l’http to https redirect)
4 - Lookup di un record SRV per autodiscover._tcp.contoso.com (Questo record deve contenere la porta 443 e l’hostname, ad esempio mail.contoso.com, permetendo cosi al client di fare una request in https all’URL https://mail.contoso.com/Autodiscover/Autodiscover.xml)
*/