Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #include "xmpp/xmpp_proto.h"
7 : #include <iostream>
8 : #include <string>
9 : #include <boost/algorithm/string/replace.hpp>
10 : #include <boost/algorithm/string/predicate.hpp>
11 :
12 : #include "xmpp/xmpp_connection.h"
13 : #include "xmpp/xmpp_log.h"
14 : #include "xmpp/xmpp_session.h"
15 : #include "xmpp/xmpp_str.h"
16 :
17 : #include "sandesh/sandesh_trace.h"
18 : #include "sandesh/common/vns_types.h"
19 : #include "sandesh/common/vns_constants.h"
20 : #include "sandesh/xmpp_message_sandesh_types.h"
21 : #include "sandesh/xmpp_trace_sandesh_types.h"
22 :
23 : using namespace std;
24 :
25 : unique_ptr<XmlBase> XmppProto::open_doc_(AllocXmppXmlImpl(sXMPP_STREAM_OPEN));
26 :
27 0 : XmppStanza::XmppStanza() {
28 0 : }
29 :
30 0 : XmppProto::XmppProto() {
31 0 : }
32 :
33 0 : XmppProto::~XmppProto() {
34 0 : }
35 :
36 748 : int XmppProto::EncodeStream(const XmppStreamMessage &str, string &to,
37 : string &from, const string &xmlns, uint8_t *buf,
38 : size_t size) {
39 748 : int len = 0;
40 :
41 748 : switch (str.strmtype) {
42 264 : case (XmppStanza::XmppStreamMessage::INIT_STREAM_HEADER):
43 264 : len = EncodeOpen(buf, to, from, xmlns, size);
44 264 : break;
45 241 : case (XmppStanza::XmppStreamMessage::INIT_STREAM_HEADER_RESP):
46 241 : len = EncodeOpenResp(buf, to, from, size);
47 241 : break;
48 243 : case (XmppStanza::XmppStreamMessage::FEATURE_TLS):
49 243 : switch (str.strmtlstype) {
50 82 : case (XmppStanza::XmppStreamMessage::TLS_FEATURE_REQUEST):
51 82 : len = EncodeFeatureTlsRequest(buf);
52 82 : break;
53 83 : case (XmppStanza::XmppStreamMessage::TLS_START):
54 83 : len = EncodeFeatureTlsStart(buf);
55 83 : break;
56 78 : case (XmppStanza::XmppStreamMessage::TLS_PROCEED):
57 78 : len = EncodeFeatureTlsProceed(buf);
58 78 : break;
59 : }
60 243 : break;
61 0 : default:
62 0 : break;
63 : }
64 :
65 748 : return len;
66 : }
67 :
68 166 : int XmppProto::EncodeStream(const XmppStanza::XmppMessage &str, uint8_t *buf,
69 : size_t size) {
70 166 : int ret = 0;
71 :
72 166 : if (str.type == XmppStanza::WHITESPACE_MESSAGE_STANZA) {
73 166 : return EncodeWhitespace(buf);
74 : }
75 :
76 0 : return ret;
77 : }
78 :
79 0 : int XmppProto::EncodeMessage(XmlBase *dom, uint8_t *buf, size_t size) {
80 0 : int len = dom->WriteDoc(buf);
81 :
82 0 : return len;
83 : }
84 :
85 0 : int XmppProto::EncodePresence(uint8_t *buf, size_t size) {
86 0 : return 0;
87 : }
88 :
89 0 : int XmppProto::EncodeIq(const XmppStanza::XmppMessageIq *iq,
90 : XmlBase *doc, uint8_t *buf, size_t size) {
91 0 : unique_ptr<XmlBase> send_doc_(AllocXmppXmlImpl());
92 :
93 : // create
94 0 : send_doc_->LoadDoc("");
95 0 : send_doc_->AddNode("iq", "");
96 :
97 0 : switch(iq->stype) {
98 0 : case XmppStanza::XmppMessageIq::GET:
99 0 : send_doc_->AddAttribute("type", "get");
100 0 : break;
101 0 : case XmppStanza::XmppMessageIq::SET:
102 0 : send_doc_->AddAttribute("type", "set");
103 0 : break;
104 0 : case XmppStanza::XmppMessageIq::RESULT:
105 0 : send_doc_->AddAttribute("type", "result");
106 0 : break;
107 0 : case XmppStanza::XmppMessageIq::ERROR:
108 0 : send_doc_->AddAttribute("type", "error");
109 0 : break;
110 0 : default:
111 0 : break;
112 : }
113 0 : send_doc_->AddAttribute("from", iq->from);
114 0 : send_doc_->AddAttribute("to", iq->to);
115 0 : send_doc_->AddAttribute("id", "id1");
116 :
117 0 : send_doc_->AddChildNode("pubsub", "");
118 0 : send_doc_->AddAttribute("xmlns", "http://jabber.org/protocol/pubsub");
119 :
120 0 : send_doc_->AppendDoc("pubsub", doc);
121 :
122 : //Returns byte encoded in the doc
123 0 : int len = send_doc_->WriteDoc(buf);
124 :
125 0 : return len;
126 0 : }
127 :
128 166 : int XmppProto::EncodeWhitespace(uint8_t *buf) {
129 166 : string str(sXMPP_WHITESPACE);
130 :
131 166 : int len = str.size();
132 166 : if (len > 0) {
133 166 : memcpy(buf, str.data(), len);
134 : }
135 :
136 166 : return len;
137 166 : }
138 :
139 241 : int XmppProto::EncodeOpenResp(uint8_t *buf, string &to, string &from,
140 : size_t max_size) {
141 :
142 241 : unique_ptr<XmlBase> resp_doc(XmppStanza::AllocXmppXmlImpl(sXMPP_STREAM_RESP));
143 :
144 241 : if (resp_doc.get() == NULL) {
145 0 : return 0;
146 : }
147 :
148 241 : SetTo(to, resp_doc.get());
149 241 : SetFrom(from, resp_doc.get());
150 :
151 241 : std::stringstream ss;
152 241 : resp_doc->PrintDoc(ss);
153 241 : std::string msg;
154 241 : msg = ss.str();
155 241 : size_t len = msg.size();
156 241 : if (len > max_size) {
157 0 : LOG(ERROR, "\n (Open Confirm) size greater than max buffer size \n");
158 0 : return 0;
159 : } else {
160 241 : boost::algorithm::ireplace_last(msg, "/", " ");
161 241 : memcpy(buf, msg.c_str(), len);
162 241 : return len;
163 : }
164 241 : }
165 :
166 264 : int XmppProto::EncodeOpen(uint8_t *buf, string &to, string &from,
167 : const string &xmlns, size_t max_size) {
168 :
169 264 : if (open_doc_.get() == NULL) {
170 0 : return 0;
171 : }
172 :
173 264 : SetTo(to, open_doc_.get());
174 264 : SetFrom(from, open_doc_.get());
175 264 : SetXmlns(xmlns, open_doc_.get());
176 :
177 : //Returns byte encoded in the doc
178 264 : std::stringstream ss;
179 264 : open_doc_->PrintDoc(ss);
180 264 : std::string msg;
181 264 : msg = ss.str();
182 264 : size_t len = msg.size();
183 264 : if (len > max_size) {
184 0 : LOG(ERROR, "\n (Open Message) size greater than max buffer size \n");
185 0 : return 0;
186 : } else {
187 264 : boost::algorithm::ireplace_last(msg, "/", " ");
188 264 : memcpy(buf, msg.c_str(), len);
189 264 : return len;
190 : }
191 264 : }
192 :
193 82 : int XmppProto::EncodeFeatureTlsRequest(uint8_t *buf) {
194 82 : unique_ptr<XmlBase> resp_doc(XmppStanza::AllocXmppXmlImpl(sXMPP_STREAM_FEATURE_TLS));
195 : //Returns byte encoded in the doc
196 82 : int len = resp_doc->WriteDoc(buf);
197 82 : return len;
198 82 : }
199 :
200 83 : int XmppProto::EncodeFeatureTlsStart(uint8_t *buf) {
201 83 : unique_ptr<XmlBase> resp_doc(XmppStanza::AllocXmppXmlImpl(sXMPP_STREAM_START_TLS));
202 : //Returns byte encoded in the doc
203 83 : int len = resp_doc->WriteDoc(buf);
204 83 : return len;
205 83 : }
206 :
207 78 : int XmppProto::EncodeFeatureTlsProceed(uint8_t *buf) {
208 78 : unique_ptr<XmlBase> resp_doc(XmppStanza::AllocXmppXmlImpl(sXMPP_STREAM_PROCEED_TLS));
209 : //Returns byte encoded in the doc
210 78 : int len = resp_doc->WriteDoc(buf);
211 78 : return len;
212 78 : }
213 :
214 312817 : XmppStanza::XmppMessage *XmppProto::Decode(const XmppConnection *connection,
215 : const string &ts) {
216 312817 : XmlBase *impl = XmppStanza::AllocXmppXmlImpl();
217 312818 : if (impl == nullptr) {
218 0 : return nullptr;
219 : }
220 :
221 312818 : XmppStanza::XmppMessage *msg = DecodeInternal(connection, ts, impl);
222 312820 : if (!msg) {
223 0 : return nullptr;
224 : }
225 :
226 : // transfer ownership of the dom implementation
227 312820 : msg->dom.reset(impl);
228 :
229 312820 : return msg;
230 : }
231 :
232 312818 : XmppStanza::XmppMessage *XmppProto::DecodeInternal(
233 : const XmppConnection *connection, const string &ts, XmlBase *impl) {
234 312818 : XmppStanza::XmppMessage *ret = nullptr;
235 :
236 312818 : string ns(sXMPP_STREAM_O);
237 312817 : string ws(sXMPP_WHITESPACE);
238 312817 : string iq(sXMPP_IQ_KEY);
239 :
240 312817 : if (ts.find(sXMPP_IQ) != string::npos) {
241 2231 : string ts_tmp = ts;
242 :
243 2231 : if (impl->LoadDoc(ts) == -1) {
244 0 : XMPP_WARNING(XmppIqMessageParseFail, connection->ToUVEKey(),
245 : XMPP_PEER_DIR_IN);
246 0 : assert(false);
247 : goto done;
248 : }
249 :
250 2231 : XmppStanza::XmppMessageIq *msg = new XmppStanza::XmppMessageIq;
251 2231 : impl->ReadNode(iq);
252 2231 : msg->to = XmppProto::GetTo(impl);
253 2231 : msg->from = XmppProto::GetFrom(impl);
254 2231 : msg->id = XmppProto::GetId(impl);
255 2231 : msg->iq_type = XmppProto::GetType(impl);
256 : // action is subscribe,publish,collection
257 2231 : const char *action = XmppProto::GetAction(impl, msg->iq_type);
258 2231 : if (action) {
259 2216 : msg->action = action;
260 : }
261 2231 : if (XmppProto::GetNode(impl, msg->action)) {
262 2216 : msg->node = XmppProto::GetNode(impl, msg->action);
263 : }
264 : //associate or dissociate collection node
265 2231 : if (msg->action.compare("collection") == 0) {
266 906 : if (XmppProto::GetAsNode(impl)) {
267 898 : msg->as_node = XmppProto::GetAsNode(impl);
268 898 : msg->is_as_node = true;
269 8 : } else if (XmppProto::GetDsNode(impl)) {
270 0 : msg->as_node = XmppProto::GetDsNode(impl);
271 0 : msg->is_as_node = false;
272 : }
273 : }
274 :
275 : //msg->dom.reset(impl);
276 :
277 2231 : ret = msg;
278 :
279 2231 : XMPP_UTDEBUG(XmppIqMessageProcess, connection->ToUVEKey(),
280 : XMPP_PEER_DIR_IN, msg->node, msg->action, msg->from,
281 : msg->to, msg->id, msg->iq_type);
282 2231 : goto done;
283 :
284 312820 : } else if (ts.find(sXMPP_MESSAGE) != string::npos) {
285 :
286 120080 : if (impl->LoadDoc(ts) == -1) {
287 0 : XMPP_WARNING(XmppChatMessageParseFail, connection->ToUVEKey(),
288 : XMPP_PEER_DIR_IN);
289 0 : goto done;
290 : }
291 : XmppStanza::XmppMessage *msg = new XmppStanza::XmppChatMessage(
292 120080 : STATE_NONE);
293 120080 : impl->ReadNode(sXMPP_MESSAGE_KEY);
294 :
295 120080 : msg->to = XmppProto::GetTo(impl);
296 120080 : msg->from = XmppProto::GetFrom(impl);
297 120080 : ret = msg;
298 :
299 120080 : XMPP_UTDEBUG(XmppChatMessageProcess, connection->ToUVEKey(),
300 : XMPP_PEER_DIR_IN, msg->type, msg->from, msg->to);
301 120080 : goto done;
302 :
303 190509 : } else if (ts.find(sXMPP_STREAM_O) != string::npos) {
304 :
305 : // ensusre stream open is at the beginning of the message
306 436 : string ts_tmp = ts;
307 436 : ts_tmp.erase(std::remove(ts_tmp.begin(), ts_tmp.end(), '\n'), ts_tmp.end());
308 :
309 436 : if ((ts_tmp.compare(0, strlen(sXMPP_STREAM_START),
310 872 : sXMPP_STREAM_START) != 0) &&
311 436 : (ts_tmp.compare(0, strlen(sXMPP_STREAM_START_S),
312 : sXMPP_STREAM_START_S) != 0)) {
313 0 : XMPP_WARNING(XmppBadMessage, connection->ToUVEKey(),
314 : XMPP_PEER_DIR_IN,
315 : "Open message not at the beginning.", ts);
316 0 : goto done;
317 : }
318 :
319 : // check if the buf is xmpp open or response message
320 : // As end tag will be missing we need to modify the
321 : // string for stream open, else dom decoder will fail
322 436 : boost::algorithm::replace_last(ts_tmp, ">", "/>");
323 436 : if (impl->LoadDoc(ts_tmp) == -1) {
324 0 : XMPP_WARNING(XmppBadMessage, connection->ToUVEKey(),
325 : XMPP_PEER_DIR_IN, "Open message parse failed.", ts);
326 0 : goto done;
327 : }
328 :
329 : XmppStanza::XmppStreamMessage *strm =
330 436 : new XmppStanza::XmppStreamMessage();
331 436 : strm->strmtype = XmppStanza::XmppStreamMessage::INIT_STREAM_HEADER;
332 436 : impl->ReadNode(ns);
333 436 : strm->to = XmppProto::GetTo(impl);
334 436 : strm->from = XmppProto::GetFrom(impl);
335 435 : strm->xmlns = XmppProto::GetXmlns(impl);
336 :
337 435 : ret = strm;
338 :
339 435 : XMPP_UTDEBUG(XmppRxOpenMessage, connection->ToUVEKey(),
340 : XMPP_PEER_DIR_IN, strm->from, strm->to);
341 :
342 190509 : } else if (ts.find(sXMPP_STREAM_NS_TLS) != string::npos) {
343 :
344 207 : if (impl->LoadDoc(ts) == -1) {
345 0 : XMPP_WARNING(XmppBadMessage, connection->ToUVEKey(),
346 : XMPP_PEER_DIR_IN, "Stream TLS parse failed.", ts);
347 0 : goto done;
348 : }
349 :
350 : // find stream:features tls required
351 276 : if ((ts.find(sXMPP_STREAM_FEATURES_O) != string::npos) &&
352 276 : (ts.find(sXMPP_STREAM_STARTTLS_O) != string::npos) &&
353 69 : (ts.find(sXMPP_REQUIRED_O) != string::npos)) {
354 :
355 : XmppStanza::XmppStreamMessage *strm =
356 69 : new XmppStanza::XmppStreamMessage();
357 69 : strm->strmtype = XmppStanza::XmppStreamMessage::FEATURE_TLS;
358 69 : strm->strmtlstype = XmppStanza::XmppStreamMessage::TLS_FEATURE_REQUEST;
359 :
360 69 : ret = strm;
361 :
362 69 : XMPP_UTDEBUG(XmppRxStreamTlsRequired, connection->ToUVEKey(),
363 : XMPP_PEER_DIR_IN);
364 :
365 138 : } else if (ts.find(sXMPP_STREAM_STARTTLS_O) != string::npos) {
366 : XmppStanza::XmppStreamMessage *strm =
367 69 : new XmppStanza::XmppStreamMessage();
368 69 : strm->strmtype = XmppStanza::XmppStreamMessage::FEATURE_TLS;
369 69 : strm->strmtlstype = XmppStanza::XmppStreamMessage::TLS_START;
370 69 : ret = strm;
371 :
372 69 : XMPP_UTDEBUG(XmppRxStreamStartTls, connection->ToUVEKey(),
373 : XMPP_PEER_DIR_IN);
374 :
375 69 : } else if (ts.find(sXMPP_STREAM_PROCEED_O) != string::npos) {
376 : XmppStanza::XmppStreamMessage *strm =
377 69 : new XmppStanza::XmppStreamMessage();
378 69 : strm->strmtype = XmppStanza::XmppStreamMessage::FEATURE_TLS;
379 69 : strm->strmtlstype = XmppStanza::XmppStreamMessage::TLS_PROCEED;
380 :
381 69 : ret = strm;
382 :
383 69 : XMPP_UTDEBUG(XmppRxStreamProceed, connection->ToUVEKey(),
384 : XMPP_PEER_DIR_IN);
385 : }
386 207 : goto done;
387 :
388 189866 : } else if (ts.find_first_of(sXMPP_VALIDWS) != string::npos) {
389 :
390 : XmppStanza::XmppMessage *msg =
391 189866 : new XmppStanza::XmppMessage(WHITESPACE_MESSAGE_STANZA);
392 189866 : return msg;
393 : } else {
394 0 : XMPP_WARNING(XmppBadMessage, connection->ToUVEKey(),
395 : XMPP_PEER_DIR_IN, "Message not supported", ts);
396 : }
397 :
398 122954 : done:
399 :
400 122954 : return ret;
401 312820 : }
402 :
403 505 : int XmppProto::SetTo(string &to, XmlBase *doc) {
404 505 : if (!doc) return -1;
405 :
406 505 : string ns(sXMPP_STREAM_O);
407 505 : doc->ReadNode(ns);
408 505 : doc->ModifyAttribute("to", to);
409 :
410 505 : return 0;
411 505 : }
412 :
413 505 : int XmppProto::SetFrom(string &from, XmlBase *doc) {
414 505 : if (!doc) return -1;
415 :
416 505 : string ns(sXMPP_STREAM_O);
417 505 : doc->ReadNode(ns);
418 505 : return doc->ModifyAttribute("from", from);
419 505 : }
420 :
421 264 : int XmppProto::SetXmlns(const string &xmlns, XmlBase *doc) {
422 264 : if (!doc)
423 0 : return -1;
424 :
425 264 : string ns(sXMPP_STREAM_O);
426 264 : doc->ReadNode(ns);
427 264 : return doc->ModifyAttribute("xmlns", xmlns);
428 264 : }
429 :
430 122747 : const char *XmppProto::GetTo(XmlBase *doc) {
431 122747 : if (!doc) return NULL;
432 :
433 122747 : string tmp("to");
434 122746 : return doc->ReadAttrib(tmp);
435 122747 : }
436 :
437 122747 : const char *XmppProto::GetFrom(XmlBase *doc) {
438 122747 : if (!doc) return NULL;
439 :
440 122747 : string tmp("from");
441 122746 : return doc->ReadAttrib(tmp);
442 122747 : }
443 :
444 435 : const char *XmppProto::GetXmlns(XmlBase *doc) {
445 435 : if (!doc)
446 0 : return NULL;
447 :
448 435 : string tmp("xmlns");
449 435 : return doc->ReadAttrib(tmp);
450 435 : }
451 :
452 2231 : const char *XmppProto::GetId(XmlBase *doc) {
453 2231 : if (!doc) return NULL;
454 :
455 2231 : string tmp("id");
456 2231 : return doc->ReadAttrib(tmp);
457 2231 : }
458 :
459 2231 : const char *XmppProto::GetType(XmlBase *doc) {
460 2231 : if (!doc) return NULL;
461 :
462 2231 : string tmp("type");
463 2231 : return doc->ReadAttrib(tmp);
464 2231 : }
465 :
466 2231 : const char *XmppProto::GetAction(XmlBase *doc, const string &str) {
467 2231 : if (!doc) return NULL;
468 :
469 2231 : if (str.compare("set") == 0) {
470 2216 : doc->ReadNode("pubsub");
471 2216 : return(doc->ReadChildNodeName());
472 15 : } else if (str.compare("get") == 0) {
473 : }
474 :
475 15 : return(NULL);
476 : }
477 :
478 4447 : const char *XmppProto::GetNode(XmlBase *doc, const string &str) {
479 4447 : if (!doc) return NULL;
480 :
481 4447 : if (!str.empty()) {
482 4432 : return(doc->ReadAttrib("node"));
483 : }
484 :
485 15 : return(NULL);
486 : }
487 :
488 1804 : const char *XmppProto::GetAsNode(XmlBase *doc) {
489 1804 : if (!doc) return NULL;
490 :
491 1804 : const char *node = doc->ReadNode("associate");
492 1804 : if (node != NULL) {
493 1796 : return(doc->ReadAttrib("node"));
494 : }
495 :
496 8 : return(NULL);
497 : }
498 :
499 8 : const char *XmppProto::GetDsNode(XmlBase *doc) {
500 8 : if (!doc) return NULL;
501 :
502 8 : const char *node = doc->ReadNode("dissociate");
503 8 : if (node != NULL) {
504 0 : return(doc->ReadAttrib("node"));
505 : }
506 :
507 8 : return(NULL);
508 : }
|