-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_xmpp.go
385 lines (305 loc) · 9.5 KB
/
check_xmpp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package main
import (
"fmt"
"os"
"net"
"encoding/xml"
"encoding/base64"
"log"
"io"
"crypto/tls"
"math/rand"
"strings"
"time"
"io/ioutil"
"flag"
)
/*
* Host and login credentials
*/
var targethost string
var user string
var password string
/*
* Helper functions
*/
/*
* randString() creates random strings for IDs
*/
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
/*
* Message stanza type
*/
type MessageStanza struct {
XMLName xml.Name `xml:"jabber:client message"`
From string `xml:"from,attr"`
Id string `xml:"id,attr"`
To string `xml:"to,attr"`
Type string `xml:"type,attr"`
InnerXML []byte `xml:",innerxml"`
}
// Scan XML token stream to find next StartElement.
func nextStart(p *xml.Decoder) (xml.StartElement, error) {
for {
t, err := p.Token()
if err != nil && err != io.EOF || t == nil {
return xml.StartElement{}, err
}
switch t := t.(type) {
case xml.StartElement:
//log.Println("Next start element:", t.Name.Local, t.Name.Space)
return t, nil
}
}
}
/*
* Stream object which represents XMPP stream
*/
type XMPPStream struct {
Host string
Login struct {
Username string
Password string
}
Conn net.Conn
Channels struct {
Messages chan MessageStanza
}
Decoder *xml.Decoder
}
/*
* Establish basic XMPP stream. This includes:
* - Establishing TCP connection
* - Establishing XMPP stream
* - Starting StartTLS encryption
* - Authentication
* - Resource binding
*/
func (stream *XMPPStream) Establish() {
var err error
stream.Channels.Messages = make(chan MessageStanza)
// Lookup CNAME for SRV record
_, addr, _ := net.LookupSRV("xmpp-client", "tcp", stream.Host)
log.Println("Remote host:", addr[0].Target)
host := addr[0].Target
// Connect to XMPP PubSub server
log.Println("Connecting to XMPP host", host, "...")
stream.Conn, err = net.Dial("tcp", host + ":5222")
if err != nil {
log.Fatal(err)
}
// Set up XML decoder
decoder := xml.NewDecoder(stream.Conn)
// Open stream
fmt.Fprintf(stream.Conn, "<?xml version='1.0'?><stream:stream " +
"from='%s' " +
"to='%s' " +
"version='1.0' " +
"xml:lang='en' "+
"xmlns='jabber:client' " +
"xmlns:stream='http://etherx.jabber.org/streams'>", user, targethost)
// streamstart (<stream>)
nextStart(decoder)
// stream features
nextStart(decoder)
decoder.Skip() // skip nested structures
// Upgrade connection to STARTTLS connection
var tlsconn *tls.Conn
var tlsconfig = &tls.Config{ ServerName: targethost }
tlsconn = tls.Client(stream.Conn, tlsconfig)
// Send: Let's use starttls
fmt.Fprintf(stream.Conn, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>")
response_start, _:= nextStart(decoder)
err = tlsconn.Handshake()
if err != nil {
log.Println("Could not initialize STARTTLS conn:", err)
}
stream.Conn = tlsconn
// Re-open stream
fmt.Fprintf(stream.Conn, "<?xml version='1.0'?><stream:stream " +
"from='%s' " +
"to='%s' " +
"version='1.0' " +
"xml:lang='en' "+
"xmlns='jabber:client' " +
"xmlns:stream='http://etherx.jabber.org/streams'>", user, targethost)
// Re-Init decoder on new, secure connection
decoder = xml.NewDecoder(stream.Conn)
// Now there should be a new <stream> coming in
nextStart(decoder)
// Now we have an encrypted connection.
// Get stream features once again
nextStart(decoder)
decoder.Skip() // skip nested structures
/*
* Authentication
*/
// Let's do plain text auth, cuz this is easy
// Plain authentication: send base64-encoded \x00 user \x00 password.
raw := "\x00" + user + "\x00" + password
enc := make([]byte, base64.StdEncoding.EncodedLen(len(raw)))
base64.StdEncoding.Encode(enc, []byte(raw))
fmt.Fprintf(stream.Conn, "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>%s</auth>", enc)
response_start, _ = nextStart(decoder)
if response_start.Name.Local == "failure" {
terminate("CRITICAL", "Authentication failed.")
} else {
log.Println("Authentication successful")
}
/*
* Open new stream after successful auth
*/
fmt.Fprintf(stream.Conn, "<?xml version='1.0'?><stream:stream " +
"from='%s' " +
"to='%s' " +
"version='1.0' " +
"xml:lang='en' "+
"xmlns='jabber:client' " +
"xmlns:stream='http://etherx.jabber.org/streams'>", user, targethost)
// new <stream> incoming
nextStart(decoder)
// stream features
nextStart(decoder)
/*
* Bind resource
*/
randres := randString(10)
bind_iq_id := randString(10)
fmt.Fprintf(stream.Conn, "<iq id='%s' type='set'>" +
"<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>" +
"<resource>%s</resource>" +
"</bind>" +
"</iq>", bind_iq_id, randres)
/*
* Set online state to be able to receive messages
*/
fmt.Fprintf(stream.Conn, "<presence xml:lang='en'><show>online</show><status>Hey, I'm online.</status></presence>")
/*
* Stream is set up
*/
stream.Decoder = decoder
log.Println("XMPP stream is established")
}
/*
* When stream is set up, loop through incoming stanzas
* (iq, message, presence) and sort these stanzas into
* seperate channels.
* These channels can then be read by other functions
*/
func (stream *XMPPStream) Loop() {
var element_type string
for {
incoming_start, err := nextStart(stream.Decoder)
if err == nil {
// Check element name and namespace of incoming element block
element_type = incoming_start.Name.Space + " " + incoming_start.Name.Local
switch element_type {
case "jabber:client message":
msgstanza := MessageStanza{}
err := stream.Decoder.DecodeElement(&msgstanza, &incoming_start)
if err != nil {
log.Println("Failed to parse incoming <message> stanza:", err)
} else {
stream.Channels.Messages <- msgstanza
}
default:
// We don't care about other stanzas. Ignore them.
stream.Decoder.Skip()
}
} else {
log.Fatal("Received invalid XML start token:", err)
}
}
}
func terminate(status string, message string) {
// os.Exit(0) // ok
// os.Exit(1) // warning
// os.Exit(2) // critical
// os.Exit(3) // unknown
fmt.Println(status, "-", message)
switch(status) {
case "OK":
os.Exit(0)
case "WARNING":
os.Exit(1)
case "CRITICAL":
os.Exit(2)
default:
os.Exit(3)
}
}
/*****************************************************
* Main function of this whole stuff
*****************************************************/
func main() {
var flag_debugging = flag.Bool("debug", false, "Enable debugging output")
var flag_timeout = flag.Duration("timeout", 5 * time.Second, "Timeout")
var flag_userid = flag.String("userid", "user@server.tld", "XMPP ID e.g. user@server.tld")
var flag_password = flag.String("password", "mypassword", "Password for userid")
// Read command line flags
flag.Parse()
// Disable debug logging
if *flag_debugging == false {
log.SetOutput(ioutil.Discard)
}
/*
* Filter and save login credentials
*/
password = *flag_password
userid_split := strings.Split(*flag_userid, "@")
if len(userid_split) < 2 {
terminate("CRITICAL", "Please specify fully qualified user ID such as 'user@server.tld'")
}
user = userid_split[0]
targethost = userid_split[1]
// Start everything
log.Println("XMPP check starting ... ")
/*
* Implements timeout. Process must not take too long.
*/
go func() {
time.Sleep(*flag_timeout)
terminate("CRITICAL", "Timeout")
}()
// If something writes into quit channel, program will be quit.
quit := make(chan bool)
/*
* Establish XMPP stream
*/
// Create new stream object
xmppstream := &XMPPStream{ Host: targethost }
xmppstream.Login.Username = user
xmppstream.Login.Password = password
xmppstream.Establish()
defer xmppstream.Conn.Close()
// Start stream loop
go xmppstream.Loop()
/*
* Send a message to myself
*/
log.Println("Sending message ...")
fmt.Fprintf(xmppstream.Conn, "<message from='%s' to='%s'>" +
"<body>Check</body>" +
"</message>", *flag_userid, *flag_userid)
/*
* Wait until message is received
*/
go func() {
log.Println("Waiting for message to arrive ...")
msgstanza := <- xmppstream.Channels.Messages
if strings.Contains(msgstanza.From, *flag_userid) {
// Exit with "ok" code
log.Println("Received message")
terminate("OK", "XMPP server is okay.")
}
}()
// Quit program if something is written to "quit" channel
_ = <- quit
}