2019-06-18 07:28:30 -07:00
|
|
|
package xmpp
|
2016-01-06 07:51:12 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
|
|
|
const xmppStreamOpen = "<?xml version='1.0'?><stream:stream to='%s' xmlns='%s' xmlns:stream='%s' version='1.0'>"
|
|
|
|
|
|
|
|
type Session struct {
|
|
|
|
// Session info
|
|
|
|
BindJid string // Jabber ID as provided by XMPP server
|
|
|
|
StreamId string
|
2019-05-16 08:46:36 -07:00
|
|
|
Features StreamFeatures
|
2016-01-06 07:51:12 -08:00
|
|
|
TlsEnabled bool
|
|
|
|
lastPacketId int
|
|
|
|
|
|
|
|
// Session interface
|
|
|
|
In chan interface{}
|
|
|
|
Out chan interface{}
|
|
|
|
|
|
|
|
// read / write
|
|
|
|
socketProxy io.ReadWriter
|
|
|
|
decoder *xml.Decoder
|
|
|
|
|
|
|
|
// error management
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2018-09-26 07:25:04 -07:00
|
|
|
func NewSession(conn net.Conn, o Config) (net.Conn, *Session, error) {
|
2016-01-06 07:51:12 -08:00
|
|
|
s := new(Session)
|
|
|
|
s.init(conn, o)
|
|
|
|
|
|
|
|
// starttls
|
|
|
|
var tlsConn net.Conn
|
2019-06-07 06:56:41 -07:00
|
|
|
tlsConn = s.startTlsIfSupported(conn, o.parsedJid.Domain)
|
2016-01-06 08:19:16 -08:00
|
|
|
if s.TlsEnabled {
|
|
|
|
s.reset(conn, tlsConn, o)
|
|
|
|
}
|
2016-01-06 07:51:12 -08:00
|
|
|
|
2017-10-21 05:49:25 -07:00
|
|
|
if !s.TlsEnabled && !o.Insecure {
|
2019-06-07 06:23:23 -07:00
|
|
|
return nil, nil, NewConnError(errors.New("failed to negotiate TLS session"), true)
|
2017-10-21 05:49:25 -07:00
|
|
|
}
|
|
|
|
|
2016-01-06 07:51:12 -08:00
|
|
|
// auth
|
|
|
|
s.auth(o)
|
|
|
|
s.reset(tlsConn, tlsConn, o)
|
|
|
|
|
|
|
|
// bind resource and 'start' XMPP session
|
|
|
|
s.bind(o)
|
|
|
|
s.rfc3921Session(o)
|
|
|
|
|
|
|
|
return tlsConn, s, s.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) PacketId() string {
|
|
|
|
s.lastPacketId++
|
|
|
|
return fmt.Sprintf("%x", s.lastPacketId)
|
|
|
|
}
|
|
|
|
|
2018-09-26 07:25:04 -07:00
|
|
|
func (s *Session) init(conn net.Conn, o Config) {
|
2016-01-06 07:51:12 -08:00
|
|
|
s.setProxy(nil, conn, o)
|
2019-06-07 06:56:41 -07:00
|
|
|
s.Features = s.open(o.parsedJid.Domain)
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
|
2018-09-26 07:25:04 -07:00
|
|
|
func (s *Session) reset(conn net.Conn, newConn net.Conn, o Config) {
|
2016-01-06 07:51:12 -08:00
|
|
|
if s.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.setProxy(conn, newConn, o)
|
2019-06-07 06:56:41 -07:00
|
|
|
s.Features = s.open(o.parsedJid.Domain)
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: setProxyLogger ? better name ? This is not a TCP / HTTP proxy
|
2018-09-26 07:25:04 -07:00
|
|
|
func (s *Session) setProxy(conn net.Conn, newConn net.Conn, o Config) {
|
2016-01-06 07:51:12 -08:00
|
|
|
if newConn != conn {
|
|
|
|
s.socketProxy = newSocketProxy(newConn, o.PacketLogger)
|
|
|
|
}
|
|
|
|
s.decoder = xml.NewDecoder(s.socketProxy)
|
2018-10-13 05:45:48 -07:00
|
|
|
s.decoder.CharsetReader = o.CharsetReader
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
|
2019-05-16 08:46:36 -07:00
|
|
|
func (s *Session) open(domain string) (f StreamFeatures) {
|
2016-01-06 07:51:12 -08:00
|
|
|
// Send stream open tag
|
2017-10-04 16:27:35 -07:00
|
|
|
if _, s.err = fmt.Fprintf(s.socketProxy, xmppStreamOpen, domain, NSClient, NSStream); s.err != nil {
|
2016-01-06 07:51:12 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set xml decoder and extract streamID from reply
|
|
|
|
s.StreamId, s.err = initDecoder(s.decoder) // TODO refactor / rename
|
|
|
|
if s.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract stream features
|
|
|
|
if s.err = s.decoder.Decode(&f); s.err != nil {
|
|
|
|
s.err = errors.New("stream open decode features: " + s.err.Error())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) startTlsIfSupported(conn net.Conn, domain string) net.Conn {
|
|
|
|
if s.err != nil {
|
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
2019-06-10 07:27:52 -07:00
|
|
|
if _, ok := s.Features.DoesStartTLS(); ok {
|
2016-01-06 07:51:12 -08:00
|
|
|
fmt.Fprintf(s.socketProxy, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>")
|
|
|
|
|
|
|
|
var k tlsProceed
|
|
|
|
if s.err = s.decoder.DecodeElement(&k, nil); s.err != nil {
|
|
|
|
s.err = errors.New("expecting starttls proceed: " + s.err.Error())
|
|
|
|
return conn
|
|
|
|
}
|
|
|
|
s.TlsEnabled = true
|
|
|
|
|
|
|
|
// TODO: add option to accept all TLS certificates: insecureSkipTlsVerify (DefaultTlsConfig.InsecureSkipVerify)
|
|
|
|
DefaultTlsConfig.ServerName = domain
|
2019-05-16 08:46:36 -07:00
|
|
|
tlsConn := tls.Client(conn, &DefaultTlsConfig)
|
2016-01-06 07:51:12 -08:00
|
|
|
// We convert existing connection to TLS
|
|
|
|
if s.err = tlsConn.Handshake(); s.err != nil {
|
|
|
|
return tlsConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// We check that cert matches hostname
|
|
|
|
s.err = tlsConn.VerifyHostname(domain)
|
|
|
|
return tlsConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// starttls is not supported => we do not upgrade the connection:
|
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
2018-09-26 07:25:04 -07:00
|
|
|
func (s *Session) auth(o Config) {
|
2016-01-06 07:51:12 -08:00
|
|
|
if s.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-07 06:56:41 -07:00
|
|
|
s.err = authSASL(s.socketProxy, s.decoder, s.Features, o.parsedJid.Node, o.Password)
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
|
2018-09-26 07:25:04 -07:00
|
|
|
func (s *Session) bind(o Config) {
|
2016-01-06 07:51:12 -08:00
|
|
|
if s.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send IQ message asking to bind to the local user name.
|
2019-06-07 06:56:41 -07:00
|
|
|
var resource = o.parsedJid.Resource
|
2016-01-06 07:51:12 -08:00
|
|
|
if resource != "" {
|
|
|
|
fmt.Fprintf(s.socketProxy, "<iq type='set' id='%s'><bind xmlns='%s'><resource>%s</resource></bind></iq>",
|
|
|
|
s.PacketId(), nsBind, resource)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(s.socketProxy, "<iq type='set' id='%s'><bind xmlns='%s'/></iq>", s.PacketId(), nsBind)
|
|
|
|
}
|
|
|
|
|
2018-01-13 09:50:17 -08:00
|
|
|
var iq IQ
|
2016-02-15 06:22:51 -08:00
|
|
|
if s.err = s.decoder.Decode(&iq); s.err != nil {
|
|
|
|
s.err = errors.New("error decoding iq bind result: " + s.err.Error())
|
2016-01-06 07:51:12 -08:00
|
|
|
return
|
|
|
|
}
|
2016-02-15 06:22:51 -08:00
|
|
|
|
2018-01-15 03:28:34 -08:00
|
|
|
// TODO Check all elements
|
2019-06-19 01:21:57 -07:00
|
|
|
switch payload := iq.Payload.(type) {
|
2018-01-16 13:33:21 -08:00
|
|
|
case *BindBind:
|
2016-02-15 06:22:51 -08:00
|
|
|
s.BindJid = payload.Jid // our local id (with possibly randomly generated resource
|
|
|
|
default:
|
|
|
|
s.err = errors.New("iq bind result missing")
|
|
|
|
}
|
|
|
|
|
2016-01-06 07:51:12 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove when ejabberd is fixed: https://github.com/processone/ejabberd/issues/869
|
|
|
|
// After the bind, if the session is required (as per old RFC 3921), we send the session open iq
|
2018-09-26 07:25:04 -07:00
|
|
|
func (s *Session) rfc3921Session(o Config) {
|
2016-01-06 07:51:12 -08:00
|
|
|
if s.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-13 09:50:17 -08:00
|
|
|
var iq IQ
|
2017-10-04 16:27:35 -07:00
|
|
|
if s.Features.Session.optional.Local != "" {
|
|
|
|
fmt.Fprintf(s.socketProxy, "<iq type='set' id='%s'><session xmlns='%s'/></iq>", s.PacketId(), nsSession)
|
|
|
|
if s.err = s.decoder.Decode(&iq); s.err != nil {
|
|
|
|
s.err = errors.New("expecting iq result after session open: " + s.err.Error())
|
|
|
|
return
|
|
|
|
}
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
}
|