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
|
/*
* protocol.h - XMPP-Core protocol state machine
* Copyright (C) 2004 Justin Karneges
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef PROTOCOL_H
#define PROTOCOL_H
#include<tqpair.h>
#include"xmlprotocol.h"
#include"xmpp.h"
#define NS_ETHERX "http://etherx.jabber.org/streams"
#define NS_CLIENT "jabber:client"
#define NS_SERVER "jabber:server"
#define NS_DIALBACK "jabber:server:dialback"
#define NS_STREAMS "urn:ietf:params:xml:ns:xmpp-streams"
#define NS_TLS "urn:ietf:params:xml:ns:xmpp-tls"
#define NS_SASL "urn:ietf:params:xml:ns:xmpp-sasl"
#define NS_SESSION "urn:ietf:params:xml:ns:xmpp-session"
#define NS_STANZAS "urn:ietf:params:xml:ns:xmpp-stanzas"
#define NS_BIND "urn:ietf:params:xml:ns:xmpp-bind"
#define NS_XHTML_IM "http://jabber.org/protocol/xhtml-im"
#define NS_XHTML "http://www.w3.org/1999/xhtml"
#define NS_CHATSTATES "http://jabber.org/protocol/chatstates"
namespace XMPP
{
class Version
{
public:
Version(int maj=0, int min=0);
int major, minor;
};
class StreamFeatures
{
public:
StreamFeatures();
bool tls_supported, sasl_supported, bind_supported;
bool tls_required;
TQStringList sasl_mechs;
};
class BasicProtocol : public XmlProtocol
{
public:
// xmpp 1.0 error conditions
enum SASLCond {
Aborted,
IncorrectEncoding,
InvalidAuthzid,
InvalidMech,
MechTooWeak,
NotAuthorized,
TemporaryAuthFailure
};
enum StreamCond {
BadFormat,
BadNamespacePrefix,
Conflict,
ConnectionTimeout,
HostGone,
HostUnknown,
ImproperAddressing,
InternalServerError,
InvalidFrom,
InvalidId,
InvalidNamespace,
InvalidXml,
StreamNotAuthorized,
PolicyViolation,
RemoteConnectionFailed,
ResourceConstraint,
RestrictedXml,
SeeOtherHost,
SystemShutdown,
UndefinedCondition,
UnsupportedEncoding,
UnsupportedStanzaType,
UnsupportedVersion,
XmlNotWellFormed
};
enum BindCond {
BindBadRequest,
BindNotAllowed,
BindConflict
};
// extend the XmlProtocol enums
enum Need {
NSASLMechs = XmlProtocol::NCustom, // need SASL mechlist
NStartTLS, // need to switch on TLS layer
NSASLFirst, // need SASL first step
NSASLNext, // need SASL next step
NSASLLayer, // need to switch on SASL layer
NCustom = XmlProtocol::NCustom+10
};
enum Event {
EFeatures = XmlProtocol::ECustom, // breakpoint after features packet is received
ESASLSuccess, // breakpoint after successful sasl auth
EStanzaReady, // a stanza was received
EStanzaSent, // a stanza was sent
EReady, // stream is ready for stanza use
ECustom = XmlProtocol::ECustom+10
};
enum Error {
ErrProtocol = XmlProtocol::ErrCustom, // there was an error in the xmpp-core protocol exchange
ErrStream, // <stream:error>, see errCond, errText, and errAppSpec for details
ErrStartTLS, // server refused starttls
ErrAuth, // authorization error. errCond holds sasl condition (or numeric code for old-protocol)
ErrBind, // server refused resource bind
ErrCustom = XmlProtocol::ErrCustom+10
};
BasicProtocol();
~BasicProtocol();
void reset();
// for outgoing xml
TQDomDocument doc;
// sasl-related
TQString saslMech() const;
TQByteArray saslStep() const;
void setSASLMechList(const TQStringList &list);
void setSASLFirst(const TQString &mech, const TQByteArray &step);
void setSASLNext(const TQByteArray &step);
void setSASLAuthed();
// send / recv
void sendStanza(const TQDomElement &e);
void sendDirect(const TQString &s);
void sendWhitespace();
TQDomElement recvStanza();
// shutdown
void shutdown();
void shutdownWithError(int cond, const TQString &otherHost="");
// <stream> information
TQString to, from, id, lang;
Version version;
// error output
int errCond;
TQString errText;
TQDomElement errAppSpec;
TQString otherHost;
TQByteArray spare; // filled with unprocessed data on NStartTLS and NSASLLayer
bool isReady() const;
enum { TypeElement, TypeStanza, TypeDirect, TypePing };
protected:
static int stringToSASLCond(const TQString &s);
static int stringToStreamCond(const TQString &s);
static TQString saslCondToString(int);
static TQString streamCondToString(int);
void send(const TQDomElement &e, bool clip=false);
void sendStreamError(int cond, const TQString &text="", const TQDomElement &appSpec=TQDomElement());
void sendStreamError(const TQString &text); // old-style
bool errorAndClose(int cond, const TQString &text="", const TQDomElement &appSpec=TQDomElement());
bool error(int code);
void delayErrorAndClose(int cond, const TQString &text="", const TQDomElement &appSpec=TQDomElement());
void delayError(int code);
// reimplemented
TQDomElement docElement();
void handleDocOpen(const Parser::Event &pe);
bool handleError();
bool handleCloseFinished();
bool doStep(const TQDomElement &e);
void itemWritten(int id, int size);
virtual TQString defaultNamespace();
virtual TQStringList extraNamespaces(); // stringlist: prefix,uri,prefix,uri, [...]
virtual void handleStreamOpen(const Parser::Event &pe);
virtual bool doStep2(const TQDomElement &e)=0;
void setReady(bool b);
TQString sasl_mech;
TQStringList sasl_mechlist;
TQByteArray sasl_step;
bool sasl_authed;
TQDomElement stanzaToRecv;
private:
struct SASLCondEntry
{
const char *str;
int cond;
};
static SASLCondEntry saslCondTable[];
struct StreamCondEntry
{
const char *str;
int cond;
};
static StreamCondEntry streamCondTable[];
struct SendItem
{
TQDomElement stanzaToSend;
TQString stringToSend;
bool doWhitespace;
};
TQValueList<SendItem> sendList;
bool doShutdown, delayedError, closeError, ready;
int stanzasPending, stanzasWritten;
void init();
void extractStreamError(const TQDomElement &e);
};
class CoreProtocol : public BasicProtocol
{
public:
enum {
NPassword = NCustom, // need password for old-mode
EDBVerify = ECustom, // breakpoint after db:verify request
ErrPlain = ErrCustom // server only supports plain, but allowPlain is false locally
};
CoreProtocol();
~CoreProtocol();
void reset();
void startClientOut(const Jid &jid, bool oldOnly, bool tlsActive, bool doAuth);
void startServerOut(const TQString &to);
void startDialbackOut(const TQString &to, const TQString &from);
void startDialbackVerifyOut(const TQString &to, const TQString &from, const TQString &id, const TQString &key);
void startClientIn(const TQString &id);
void startServerIn(const TQString &id);
void setLang(const TQString &s);
void setAllowTLS(bool b);
void setAllowBind(bool b);
void setAllowPlain(bool b); // old-mode
void setPassword(const TQString &s);
void setFrom(const TQString &s);
void setDialbackKey(const TQString &s);
// input
TQString user, host;
// status
bool old;
StreamFeatures features;
//static TQString xmlToString(const TQDomElement &e, bool clip=false);
class DBItem
{
public:
enum { ResultRequest, ResultGrant, VerifyRequest, VerifyGrant, Validated };
int type;
Jid to, from;
TQString key, id;
bool ok;
};
private:
enum Step {
Start,
Done,
SendFeatures,
GetRequest,
HandleTLS,
GetSASLResponse,
IncHandleSASLSuccess,
GetFeatures, // read features packet
HandleFeatures, // act on features, by initiating tls, sasl, or bind
GetTLSProceed, // read <proceed/> tls response
GetSASLFirst, // perform sasl first step using provided data
GetSASLChallenge, // read server sasl challenge
GetSASLNext, // perform sasl next step using provided data
HandleSASLSuccess, // handle what must be done after reporting sasl success
GetBindResponse, // read bind response
HandleAuthGet, // send old-protocol auth-get
GetAuthGetResponse, // read auth-get response
HandleAuthSet, // send old-protocol auth-set
GetAuthSetResponse // read auth-set response
};
TQValueList<DBItem> dbrequests, dbpending, dbvalidated;
bool server, dialback, dialback_verify;
int step;
bool digest;
bool tls_started, sasl_started;
Jid jid;
bool oldOnly;
bool allowPlain;
bool doTLS, doAuth, doBinding;
TQString password;
TQString dialback_id, dialback_key;
TQString self_from;
void init();
static int getOldErrorCode(const TQDomElement &e);
bool loginComplete();
bool isValidStanza(const TQDomElement &e) const;
bool grabPendingItem(const Jid &to, const Jid &from, int type, DBItem *item);
bool normalStep(const TQDomElement &e);
bool dialbackStep(const TQDomElement &e);
// reimplemented
bool stepAdvancesParser() const;
bool stepRequiresElement() const;
void stringSend(const TQString &s);
void stringRecv(const TQString &s);
TQString defaultNamespace();
TQStringList extraNamespaces();
void handleStreamOpen(const Parser::Event &pe);
bool doStep2(const TQDomElement &e);
void elementSend(const TQDomElement &e);
void elementRecv(const TQDomElement &e);
};
}
#endif
|