summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/libiris/iris/xmpp-core/protocol.h
blob: 8511ce3231a05444912874c76d0ecb9e21d3c0a6 (plain)
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifndef PROTOCOL_H
#define PROTOCOL_H

#include<qpair.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;
		QStringList 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
		QDomDocument doc;

		// sasl-related
		QString saslMech() const;
		QByteArray saslStep() const;
		void setSASLMechList(const QStringList &list);
		void setSASLFirst(const QString &mech, const QByteArray &step);
		void setSASLNext(const QByteArray &step);
		void setSASLAuthed();

		// send / recv
		void sendStanza(const QDomElement &e);
		void sendDirect(const QString &s);
		void sendWhitespace();
		QDomElement recvStanza();

		// shutdown
		void shutdown();
		void shutdownWithError(int cond, const QString &otherHost="");

		// <stream> information
		QString to, from, id, lang;
		Version version;

		// error output
		int errCond;
		QString errText;
		QDomElement errAppSpec;
		QString otherHost;

		QByteArray spare; // filled with unprocessed data on NStartTLS and NSASLLayer

		bool isReady() const;

		enum { TypeElement, TypeStanza, TypeDirect, TypePing };

	protected:
		static int stringToSASLCond(const QString &s);
		static int stringToStreamCond(const QString &s);
		static QString saslCondToString(int);
		static QString streamCondToString(int);

		void send(const QDomElement &e, bool clip=false);
		void sendStreamError(int cond, const QString &text="", const QDomElement &appSpec=QDomElement());
		void sendStreamError(const QString &text); // old-style

		bool errorAndClose(int cond, const QString &text="", const QDomElement &appSpec=QDomElement());
		bool error(int code);
		void delayErrorAndClose(int cond, const QString &text="", const QDomElement &appSpec=QDomElement());
		void delayError(int code);

		// reimplemented
		QDomElement docElement();
		void handleDocOpen(const Parser::Event &pe);
		bool handleError();
		bool handleCloseFinished();
		bool doStep(const QDomElement &e);
		void itemWritten(int id, int size);

		virtual QString defaultNamespace();
		virtual QStringList extraNamespaces(); // stringlist: prefix,uri,prefix,uri, [...]
		virtual void handleStreamOpen(const Parser::Event &pe);
		virtual bool doStep2(const QDomElement &e)=0;

		void setReady(bool b);

		QString sasl_mech;
		QStringList sasl_mechlist;
		QByteArray sasl_step;
		bool sasl_authed;

		QDomElement stanzaToRecv;

	private:
		struct SASLCondEntry
		{
			const char *str;
			int cond;
		};
		static SASLCondEntry saslCondTable[];

		struct StreamCondEntry
		{
			const char *str;
			int cond;
		};
		static StreamCondEntry streamCondTable[];

		struct SendItem
		{
			QDomElement stanzaToSend;
			QString stringToSend;
			bool doWhitespace;
		};
		QValueList<SendItem> sendList;

		bool doShutdown, delayedError, closeError, ready;
		int stanzasPending, stanzasWritten;

		void init();
		void extractStreamError(const QDomElement &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 QString &to);
		void startDialbackOut(const QString &to, const QString &from);
		void startDialbackVerifyOut(const QString &to, const QString &from, const QString &id, const QString &key);
		void startClientIn(const QString &id);
		void startServerIn(const QString &id);

		void setLang(const QString &s);
		void setAllowTLS(bool b);
		void setAllowBind(bool b);
		void setAllowPlain(bool b); // old-mode

		void setPassword(const QString &s);
		void setFrom(const QString &s);
		void setDialbackKey(const QString &s);

		// input
		QString user, host;

		// status
		bool old;

		StreamFeatures features;

		//static QString xmlToString(const QDomElement &e, bool clip=false);

		class DBItem
		{
		public:
			enum { ResultRequest, ResultGrant, VerifyRequest, VerifyGrant, Validated };
			int type;
			Jid to, from;
			QString 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
		};

		QValueList<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;
		QString password;

		QString dialback_id, dialback_key;
		QString self_from;

		void init();
		static int getOldErrorCode(const QDomElement &e);
		bool loginComplete();

		bool isValidStanza(const QDomElement &e) const;
		bool grabPendingItem(const Jid &to, const Jid &from, int type, DBItem *item);
		bool normalStep(const QDomElement &e);
		bool dialbackStep(const QDomElement &e);

		// reimplemented
		bool stepAdvancesParser() const;
		bool stepRequiresElement() const;
		void stringSend(const QString &s);
		void stringRecv(const QString &s);
		QString defaultNamespace();
		QStringList extraNamespaces();
		void handleStreamOpen(const Parser::Event &pe);
		bool doStep2(const QDomElement &e);
		void elementSend(const QDomElement &e);
		void elementRecv(const QDomElement &e);
	};
}

#endif