stream.go
author Chris Jones <chris@cjones.org>
Tue, 27 Dec 2011 20:42:44 -0700
changeset 12 122ab6208c3c
parent 11 48be1ae93fd4
child 13 c9527bbe99a6
permissions -rw-r--r--
Added resource binding and structures for <iq>, <message>, and <presence>.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     1
// Copyright 2011 The Go Authors.  All rights reserved.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     2
// Use of this source code is governed by a BSD-style
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     3
// license that can be found in the LICENSE file.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     4
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     5
// This file contains the three layers of processing for the
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     6
// communication with the server: transport (where TLS happens), XML
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     7
// (where strings are converted to go structures), and Stream (where
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     8
// we respond to XMPP events on behalf of the library client).
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
     9
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    10
package xmpp
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    11
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    12
import (
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
    13
	"big"
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
    14
	"crypto/md5"
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
    15
	"crypto/rand"
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    16
	"crypto/tls"
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
    17
	"encoding/base64"
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
    18
	"fmt"
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    19
	"io"
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    20
	"log"
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    21
	"net"
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    22
	"os"
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
    23
	"regexp"
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
    24
	"strings"
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    25
	"time"
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    26
	"xml"
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    27
)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    28
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    29
func (cl *Client) readTransport(w io.Writer) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    30
	defer tryClose(cl.socket, w)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    31
	cl.socket.SetReadTimeout(1e8)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    32
	p := make([]byte, 1024)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    33
	for {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    34
		if cl.socket == nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    35
			cl.waitForSocket()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    36
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    37
		nr, err := cl.socket.Read(p)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    38
		if nr == 0 {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    39
			if errno, ok := err.(*net.OpError) ; ok {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    40
				if errno.Timeout() {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    41
					continue
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    42
				}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    43
			}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    44
			log.Printf("read: %s", err.String())
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    45
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    46
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    47
		nw, err := w.Write(p[:nr])
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    48
		if nw < nr {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    49
			log.Println("read: %s", err.String())
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    50
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    51
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    52
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    53
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    54
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    55
func (cl *Client) writeTransport(r io.Reader) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    56
	defer tryClose(r, cl.socket)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    57
	p := make([]byte, 1024)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    58
	for {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    59
		nr, err := r.Read(p)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    60
		if nr == 0 {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    61
			log.Printf("write: %s", err.String())
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    62
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    63
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    64
		nw, err := cl.socket.Write(p[:nr])
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    65
		if nw < nr {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    66
			log.Println("write: %s", err.String())
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    67
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    68
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    69
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    70
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    71
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    72
func readXml(r io.Reader, ch chan<- interface{}) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    73
	if debug {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    74
		pr, pw := io.Pipe()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    75
		go tee(r, pw, "S: ")
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    76
		r = pr
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    77
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    78
	defer tryClose(r, ch)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    79
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    80
	p := xml.NewParser(r)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    81
	for {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    82
		// Sniff the next token on the stream.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    83
		t, err := p.Token()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    84
		if t == nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    85
			if err != os.EOF {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    86
				log.Printf("read: %v", err)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    87
			}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    88
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    89
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    90
		var se xml.StartElement
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    91
		var ok bool
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    92
		if se, ok = t.(xml.StartElement) ; !ok {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    93
			continue
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    94
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    95
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    96
		// Allocate the appropriate structure for this token.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    97
		var obj interface{}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    98
		switch se.Name.Space + " " + se.Name.Local {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
    99
		case nsStream + " stream":
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   100
			st, err := parseStream(se)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   101
			if err != nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   102
				log.Printf("unmarshal stream: %v",
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   103
					err)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   104
				break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   105
			}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   106
			ch <- st
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   107
			continue
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   108
		case "stream error", nsStream + " error":
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   109
			obj = &StreamError{}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   110
		case nsStream + " features":
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   111
			obj = &Features{}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   112
		case nsTLS + " proceed", nsTLS + " failure":
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   113
			obj = &starttls{}
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   114
		case nsSASL + " challenge", nsSASL + " failure",
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   115
			nsSASL + " success":
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   116
			obj = &auth{}
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   117
		case "jabber:client iq":
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   118
			obj = &Iq{}
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   119
		default:
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   120
			obj = &Unrecognized{}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   121
			log.Printf("Ignoring unrecognized: %s %s\n",
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   122
				se.Name.Space, se.Name.Local)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   123
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   124
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   125
		// Read the complete XML stanza.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   126
		err = p.Unmarshal(obj, &se)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   127
		if err != nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   128
			log.Printf("unmarshal: %v", err)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   129
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   130
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   131
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   132
		// Put it on the channel.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   133
		ch <- obj
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   134
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   135
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   136
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   137
func writeXml(w io.Writer, ch <-chan interface{}) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   138
	if debug {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   139
		pr, pw := io.Pipe()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   140
		go tee(pr, w, "C: ")
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   141
		w = pw
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   142
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   143
	defer tryClose(w, ch)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   144
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   145
	for obj := range ch {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   146
		err := xml.Marshal(w, obj)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   147
		if err != nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   148
			log.Printf("write: %v", err)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   149
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   150
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   151
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   152
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   153
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   154
func writeText(w io.Writer, ch <-chan *string) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   155
	if debug {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   156
		pr, pw := io.Pipe()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   157
		go tee(pr, w, "C: ")
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   158
		w = pw
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   159
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   160
	defer tryClose(w, ch)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   161
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   162
	for str := range ch {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   163
		_, err := w.Write([]byte(*str))
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   164
		if err != nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   165
			log.Printf("writeStr: %v", err)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   166
			break
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   167
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   168
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   169
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   170
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   171
func (cl *Client) readStream(srvIn <-chan interface{}, cliOut chan<- interface{}) {
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   172
	defer tryClose(srvIn, cliOut)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   173
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   174
	for x := range srvIn {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   175
		switch obj := x.(type) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   176
		case *Stream:
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   177
			handleStream(obj)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   178
		case *Features:
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   179
			cl.handleFeatures(obj)
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   180
		case *starttls:
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   181
			cl.handleTls(obj)
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   182
		case *auth:
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   183
			cl.handleSasl(obj)
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   184
		default:
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   185
			cliOut <- x
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   186
		}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   187
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   188
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   189
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   190
func writeStream(srvOut chan<- interface{}, cliIn <-chan interface{}) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   191
	defer tryClose(srvOut, cliIn)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   192
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   193
	for x := range cliIn {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   194
		srvOut <- x
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   195
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   196
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   197
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   198
func handleStream(ss *Stream) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   199
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   200
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   201
func (cl *Client) handleFeatures(fe *Features) {
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   202
	if fe.Starttls != nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   203
		start := &starttls{XMLName: xml.Name{Space: nsTLS,
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   204
			Local: "starttls"}}
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   205
		cl.xmlOut <- start
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   206
		return
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   207
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   208
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   209
	if len(fe.Mechanisms.Mechanism) > 0 {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   210
		cl.chooseSasl(fe)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   211
		return
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   212
	}
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   213
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   214
	if fe.Bind != nil {
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   215
		cl.bind(fe.Bind)
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   216
	}
10
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   217
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   218
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   219
// readTransport() is running concurrently. We need to stop it,
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   220
// negotiate TLS, then start it again. It calls waitForSocket() in
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   221
// its inner loop; see below.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   222
func (cl *Client) handleTls(t *starttls) {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   223
	tcp := cl.socket
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   224
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   225
	// Set the socket to nil, and wait for the reader routine to
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   226
	// signal that it's paused.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   227
	cl.socket = nil
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   228
	cl.socketSync.Add(1)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   229
	cl.socketSync.Wait()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   230
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   231
	// Negotiate TLS with the server.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   232
	tls := tls.Client(tcp, nil)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   233
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   234
	// Make the TLS connection available to the reader, and wait
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   235
	// for it to signal that it's working again.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   236
	cl.socketSync.Add(1)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   237
	cl.socket = tls
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   238
	cl.socketSync.Wait()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   239
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   240
	// Reset the read timeout on the (underlying) socket so the
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   241
	// reader doesn't get woken up unnecessarily.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   242
	tcp.SetReadTimeout(0)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   243
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   244
	log.Println("TLS negotiation succeeded.")
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   245
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   246
	// Now re-send the initial handshake message to start the new
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   247
	// session.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   248
	hsOut := &Stream{To: cl.Jid.Domain, Version: Version}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   249
	cl.xmlOut <- hsOut
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   250
}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   251
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   252
// Synchronize with handleTls(). Called from readTransport() when
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   253
// cl.socket is nil.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   254
func (cl *Client) waitForSocket() {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   255
	// Signal that we've stopped reading from the socket.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   256
	cl.socketSync.Done()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   257
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   258
	// Wait until the socket is available again.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   259
	for cl.socket == nil {
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   260
		time.Sleep(1e8)
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   261
	}
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   262
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   263
	// Signal that we're going back to the read loop.
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   264
	cl.socketSync.Done()
f38b0ee7b1c1 Added TLS negotiation.
Chris Jones <chris@cjones.org>
parents:
diff changeset
   265
}
11
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   266
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   267
func (cl *Client) chooseSasl(fe *Features) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   268
	var digestMd5 bool
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   269
	for _, m := range(fe.Mechanisms.Mechanism) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   270
		switch strings.ToLower(m) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   271
		case "digest-md5":
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   272
			digestMd5 = true
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   273
		}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   274
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   275
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   276
	if digestMd5 {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   277
		auth := &auth{XMLName: xml.Name{Space: nsSASL, Local:
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   278
				"auth"}, Mechanism: "DIGEST-MD5"}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   279
		cl.xmlOut <- auth
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   280
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   281
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   282
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   283
func (cl *Client) handleSasl(srv *auth) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   284
	switch strings.ToLower(srv.XMLName.Local) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   285
	case "challenge":
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   286
		b64 := base64.StdEncoding
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   287
		str, err := b64.DecodeString(srv.Chardata)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   288
		if err != nil {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   289
			log.Printf("SASL challenge decode: %s",
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   290
				err.String())
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   291
			return;
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   292
		}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   293
		srvMap := parseSasl(string(str))
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   294
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   295
		if cl.saslExpected == "" {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   296
			cl.saslDigest1(srvMap)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   297
		} else {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   298
			cl.saslDigest2(srvMap)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   299
		}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   300
	case "failure":
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   301
		log.Println("SASL authentication failed")
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   302
	case "success":
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   303
		log.Println("SASL authentication succeeded")
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   304
		ss := &Stream{To: cl.Jid.Domain, Version: Version}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   305
		cl.xmlOut <- ss
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   306
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   307
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   308
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   309
func (cl *Client) saslDigest1(srvMap map[string] string) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   310
	// Make sure it supports qop=auth
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   311
	var hasAuth bool
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   312
	for _, qop := range(strings.Fields(srvMap["qop"])) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   313
		if qop == "auth" {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   314
			hasAuth = true
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   315
		}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   316
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   317
	if !hasAuth {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   318
		log.Println("Server doesn't support SASL auth")
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   319
		return;
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   320
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   321
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   322
	// Pick a realm.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   323
	var realm string
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   324
	if srvMap["realm"] != "" {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   325
		realm = strings.Fields(srvMap["realm"])[0]
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   326
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   327
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   328
	passwd := cl.password
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   329
	nonce := srvMap["nonce"]
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   330
	digestUri := "xmpp/" + cl.Jid.Domain
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   331
	nonceCount := int32(1)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   332
	nonceCountStr := fmt.Sprintf("%08x", nonceCount)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   333
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   334
	// Begin building the response. Username is
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   335
	// user@domain or just domain.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   336
	var username string
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   337
	if cl.Jid.Node == nil {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   338
		username = cl.Jid.Domain
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   339
	} else {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   340
		username = *cl.Jid.Node
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   341
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   342
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   343
	// Generate our own nonce from random data.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   344
	randSize := big.NewInt(0)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   345
	randSize.Lsh(big.NewInt(1), 64)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   346
	cnonce, err := rand.Int(rand.Reader, randSize)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   347
	if err != nil {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   348
		log.Println("SASL rand: %s", err.String())
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   349
		return
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   350
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   351
	cnonceStr := fmt.Sprintf("%016x", cnonce)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   352
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   353
	/* Now encode the actual password response, as well as the
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   354
	 * expected next challenge from the server. */
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   355
	response := saslDigestResponse(username, realm, passwd, nonce,
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   356
		cnonceStr, "AUTHENTICATE", digestUri, nonceCountStr)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   357
	next := saslDigestResponse(username, realm, passwd, nonce,
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   358
		cnonceStr, "", digestUri, nonceCountStr)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   359
	cl.saslExpected = next
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   360
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   361
	// Build the map which will be encoded.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   362
	clMap := make(map[string]string)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   363
	clMap["realm"] = `"` + realm + `"`
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   364
	clMap["username"] = `"` + username + `"`
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   365
	clMap["nonce"] = `"` + nonce + `"`
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   366
	clMap["cnonce"] = `"` + cnonceStr + `"`
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   367
	clMap["nc"] =  nonceCountStr
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   368
	clMap["qop"] = "auth"
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   369
	clMap["digest-uri"] = `"` + digestUri + `"`
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   370
	clMap["response"] = response
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   371
	if srvMap["charset"] == "utf-8" {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   372
		clMap["charset"] = "utf-8"
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   373
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   374
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   375
	// Encode the map and send it.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   376
	clStr := packSasl(clMap)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   377
	b64 := base64.StdEncoding
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   378
	clObj := &auth{XMLName: xml.Name{Space: nsSASL, Local:
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   379
			"response"}, Chardata:
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   380
		b64.EncodeToString([]byte(clStr))}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   381
	cl.xmlOut <- clObj
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   382
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   383
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   384
func (cl *Client) saslDigest2(srvMap map[string] string) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   385
	if cl.saslExpected == srvMap["rspauth"] {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   386
		clObj := &auth{XMLName: xml.Name{Space: nsSASL, Local:
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   387
				"response"}}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   388
		cl.xmlOut <- clObj
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   389
	} else {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   390
		clObj := &auth{XMLName: xml.Name{Space: nsSASL, Local:
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   391
				"failure"}, Any:
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   392
			&Unrecognized{XMLName: xml.Name{Space: nsSASL,
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   393
				Local: "abort"}}}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   394
		cl.xmlOut <- clObj
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   395
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   396
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   397
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   398
// Takes a string like `key1=value1,key2="value2"...` and returns a
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   399
// key/value map.
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   400
func parseSasl(in string) map[string]string {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   401
	re := regexp.MustCompile(`([^=]+)="?([^",]+)"?,?`)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   402
	strs := re.FindAllStringSubmatch(in, -1)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   403
	m := make(map[string]string)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   404
	for _, pair := range(strs) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   405
		key := strings.ToLower(string(pair[1]))
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   406
		value := string(pair[2])
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   407
		m[key] = value
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   408
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   409
	return m
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   410
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   411
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   412
func packSasl(m map[string]string) string {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   413
	var terms []string
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   414
	for key, value := range(m) {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   415
		if key == "" || value == "" || value == `""` {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   416
			continue
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   417
		}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   418
		terms = append(terms, key + "=" + value)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   419
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   420
	return strings.Join(terms, ",")
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   421
}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   422
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   423
func saslDigestResponse(username, realm, passwd, nonce, cnonceStr,
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   424
	authenticate, digestUri, nonceCountStr string) string {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   425
	h := func(text string) []byte {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   426
		h := md5.New()
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   427
		h.Write([]byte(text))
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   428
		return h.Sum()
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   429
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   430
	hex := func(bytes []byte) string {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   431
		return fmt.Sprintf("%x", bytes)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   432
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   433
	kd := func(secret, data string) []byte {
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   434
		return h(secret + ":" + data)
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   435
	}
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   436
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   437
	a1 := string(h(username + ":" + realm + ":" + passwd)) + ":" +
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   438
		nonce + ":" + cnonceStr
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   439
	a2 := authenticate + ":" + digestUri
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   440
	response := hex(kd(hex(h(a1)), nonce + ":" +
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   441
		nonceCountStr + ":" + cnonceStr + ":auth:" +
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   442
		hex(h(a2))))
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   443
	return response
48be1ae93fd4 Added SASL digest authentication.
Chris Jones <chris@cjones.org>
parents: 10
diff changeset
   444
}
12
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   445
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   446
func (cl *Client) bind(bind *Unrecognized) {
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   447
	res := cl.Jid.Resource
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   448
	msg := &Iq{Type: "set", Id: cl.NextId(), Any:
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   449
		&Unrecognized{XMLName: xml.Name{Space: nsBind, Local:
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   450
					"bind"}}}
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   451
	if res != "" {
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   452
		msg.Any.Any = &Unrecognized{XMLName: xml.Name{Local:
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   453
				"resource"}, Chardata: res}
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   454
	}
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   455
	cl.xmlOut <- msg
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   456
	// TODO Grab the iq result from the server and update cl.Jid.
122ab6208c3c Added resource binding and structures for <iq>, <message>, and <presence>.
Chris Jones <chris@cjones.org>
parents: 11
diff changeset
   457
}