xmpp_server.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * $Id$
  3. *
  4. * XMPP Module
  5. * This file is part of Kamailio, a free SIP server.
  6. *
  7. * Copyright (C) 2006 Voice Sistem S.R.L.
  8. *
  9. * Kamailio is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * Kamailio is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * Author: Andreea Spirea
  24. *
  25. */
  26. /*! \file
  27. * \brief Kamailio XMPP :: XMPP server implementation (limited functionality)
  28. * \ingroup xmpp
  29. */
  30. /*
  31. * An inbound SIP message:
  32. * from sip:user1@domain1 to sip:user2*domain2@gateway_domain
  33. * is translated to an XMPP message:
  34. * from user1*domain1@xmpp_domain to user2@domain2
  35. *
  36. * An inbound XMPP message:
  37. * from user1@domain1 to user2*domain2@xmpp_domain
  38. * is translated to a SIP message:
  39. * from sip:user1*domain1@gateway_domain to sip:user2@domain2
  40. *
  41. * Where '*' is the domain_separator, and gateway_domain and
  42. * xmpp_domain are defined below.
  43. */
  44. /*
  45. * 2-way dialback sequence with xmppd2:
  46. *
  47. * Originating server (us) Receiving server (them) Authoritative server (us)
  48. * ----------------------- ----------------------- -------------------------
  49. * | | |
  50. * | establish connection | |
  51. * |------------------------------>| |
  52. * | send stream header | |
  53. * |------------------------------>| |
  54. * | send stream header | |
  55. * |<------------------------------| |
  56. * | send db:result request | |
  57. * |------------------------------>| |
  58. * | establish connection |
  59. * |------------------------------>|
  60. * | send stream header |
  61. * |------------------------------>|
  62. * | send stream header |
  63. * |<------------------------------|
  64. * | send db:result request |
  65. * |------------------------------>|
  66. * | send db:verify request |
  67. * |------------------------------>|
  68. * | send db:verify response |
  69. * |<------------------------------|
  70. * | send db:result response |
  71. * |------------------------------>|
  72. * | send db:verify request |
  73. * |<------------------------------|
  74. * | send db:verify response |
  75. * |------------------------------>|
  76. * | send db:result response |
  77. * |<------------------------------|
  78. * : : :
  79. * : : :
  80. * | outgoing <message/> | :
  81. * |------------------------------>| :
  82. * | incoming <message/> |
  83. * |------------------------------>|
  84. */
  85. #include <stdio.h>
  86. #include <stdlib.h>
  87. #include <errno.h>
  88. #include <string.h>
  89. #include "../../sr_module.h"
  90. #include "../../cfg/cfg_struct.h"
  91. #include "xmpp.h"
  92. #include "xmpp_api.h"
  93. #include "network.h"
  94. #include "xode.h"
  95. #include <arpa/inet.h>
  96. /* XXX hack */
  97. #define DB_KEY "this-be-a-random-key"
  98. #define CONN_DEAD 0
  99. #define CONN_INBOUND 1
  100. #define CONN_OUTBOUND 2
  101. struct xmpp_private_data {
  102. int fd; /* outgoing stream socket */
  103. int listen_fd; /* listening socket */
  104. int in_fd; /* incoming stream socket */
  105. int running;
  106. };
  107. struct xmpp_connection {
  108. struct xmpp_connection *next;
  109. char *domain;
  110. int type;
  111. int fd;
  112. char *stream_id;
  113. xode_pool pool;
  114. xode_stream stream;
  115. xode todo; /* backlog of outgoing messages, if any */
  116. };
  117. static char local_secret[64] = { 0, };
  118. static void in_stream_node_callback(int type, xode node, void *arg);
  119. static void out_stream_node_callback(int type, xode node, void *arg);
  120. static struct xmpp_connection *conn_list = NULL;
  121. static struct xmpp_connection *conn_new(int type, int fd, char *domain)
  122. {
  123. struct xmpp_connection *conn = NULL;
  124. conn = malloc(sizeof(struct xmpp_connection));
  125. if(conn==NULL)
  126. {
  127. LM_ERR("out of memory\n");
  128. return NULL;
  129. }
  130. memset(conn, 0, sizeof(struct xmpp_connection));
  131. conn->domain = domain ? strdup(domain) : NULL;
  132. conn->type = type;
  133. conn->fd = fd;
  134. conn->todo = xode_new_tag("todo");
  135. conn->pool = xode_pool_new();
  136. conn->stream = xode_stream_new(conn->pool,
  137. (type==CONN_INBOUND)?in_stream_node_callback:out_stream_node_callback,
  138. conn);
  139. conn->next = conn_list;
  140. conn_list = conn;
  141. return conn;
  142. }
  143. static void conn_free(struct xmpp_connection *conn)
  144. {
  145. struct xmpp_connection **last_p, *link;
  146. last_p = &conn_list;
  147. for (link = conn_list; link; link = link->next) {
  148. if (link == conn) {
  149. *last_p = link->next;
  150. break;
  151. }
  152. last_p = &link->next;
  153. }
  154. if (conn->todo)
  155. xode_free(conn->todo);
  156. xode_pool_free(conn->pool);
  157. if (conn->fd != -1)
  158. close(conn->fd);
  159. if (conn->stream_id)
  160. free(conn->stream_id);
  161. if (conn->domain)
  162. free(conn->domain);
  163. free(conn);
  164. }
  165. static struct xmpp_connection *conn_find_domain(char *domain, int type)
  166. {
  167. struct xmpp_connection *conn;
  168. for (conn = conn_list; conn; conn = conn->next)
  169. if (conn->domain && !strcasecmp(conn->domain, domain)
  170. && conn->type == type)
  171. return conn;
  172. return NULL;
  173. }
  174. /*
  175. static struct xmpp_connection *conn_find_fd(int fd)
  176. {
  177. struct xmpp_connection *conn;
  178. for (conn = conn_list; conn; conn = conn->next)
  179. if (conn->fd == fd)
  180. return conn;
  181. return NULL;
  182. }
  183. */
  184. /*****************************************************************************/
  185. static int xode_send(int fd, xode x)
  186. {
  187. char *str = xode_to_str(x);
  188. int len = strlen(str);
  189. LM_DBG("xode_send->%d [%s]\n", fd, str);
  190. if (net_send(fd, str, len) != len) {
  191. LM_ERR("send() failed: %s\n", strerror(errno));
  192. return -1;
  193. }
  194. return len;
  195. }
  196. static int xode_send_domain(char *domain, xode x)
  197. {
  198. struct xmpp_connection *conn;
  199. if ((conn = conn_find_domain(domain, CONN_OUTBOUND))) {
  200. xode_send(conn->fd, x);
  201. xode_free(x);
  202. } else {
  203. if((conn = conn_new(CONN_OUTBOUND, -1, domain))==0)
  204. return -1;
  205. xode_insert_node(conn->todo, x);
  206. }
  207. return 1;
  208. }
  209. static void out_stream_node_callback(int type, xode node, void *arg)
  210. {
  211. struct xmpp_connection *conn = (struct xmpp_connection *) arg;
  212. struct xmpp_connection *in_conn = NULL;
  213. char *tag;
  214. xode x;
  215. LM_DBG("outstream callback: %d: %s\n", type,
  216. node?xode_get_name(node):"n/a");
  217. if (conn->domain)
  218. in_conn = conn_find_domain(conn->domain, CONN_INBOUND);
  219. switch (type) {
  220. case XODE_STREAM_ROOT:
  221. x = xode_new_tag("db:result");
  222. xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
  223. xode_put_attrib(x, "from", xmpp_domain);
  224. xode_put_attrib(x, "to", conn->domain);
  225. //xode_insert_cdata(x, DB_KEY, -1);
  226. xode_insert_cdata(x, db_key(local_secret, conn->domain,
  227. xode_get_attrib(node, "id")), -1);
  228. xode_send(conn->fd, x);
  229. xode_free(x);
  230. break;
  231. case XODE_STREAM_NODE:
  232. tag = xode_get_name(node);
  233. if (!strcmp(tag, "db:verify")) {
  234. char *from = xode_get_attrib(node, "from");
  235. char *to = xode_get_attrib(node, "to");
  236. char *id = xode_get_attrib(node, "id");
  237. char *type = xode_get_attrib(node, "type");
  238. /* char *cdata = xode_get_data(node); */
  239. if (!strcmp(type, "valid") || !strcmp(type, "invalid")) {
  240. /* got a reply, report it */
  241. x = xode_new_tag("db:result");
  242. xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
  243. xode_put_attrib(x, "from", to);
  244. xode_put_attrib(x, "to", from);
  245. xode_put_attrib(x, "id", id);
  246. xode_put_attrib(x, "type", type);
  247. if (in_conn)
  248. xode_send(in_conn->fd, x);
  249. else
  250. LM_ERR("need to send reply to domain '%s', but no inbound"
  251. " connection found\n", from);
  252. xode_free(x);
  253. }
  254. } else if (!strcmp(tag, "db:result")) {
  255. char *type = xode_get_attrib(node, "type");
  256. if (type && !strcmp(type, "valid")) {
  257. /* the remote server has successfully authenticated us,
  258. * we can now send data */
  259. for (x = xode_get_firstchild(conn->todo); x;
  260. x = xode_get_nextsibling(x)) {
  261. LM_DBG("sending todo tag '%s'\n", xode_get_name(x));
  262. xode_send(conn->fd, x);
  263. }
  264. xode_free(conn->todo);
  265. conn->todo = NULL;
  266. }
  267. }
  268. break;
  269. case XODE_STREAM_ERROR:
  270. LM_ERR("outstream error\n");
  271. /* fall-through */
  272. case XODE_STREAM_CLOSE:
  273. conn->type = CONN_DEAD;
  274. break;
  275. }
  276. xode_free(node);
  277. }
  278. static void in_stream_node_callback(int type, xode node, void *arg)
  279. {
  280. struct xmpp_connection *conn = (struct xmpp_connection *) arg;
  281. char *tag;
  282. xode x;
  283. LM_DBG("instream callback: %d: %s\n",
  284. type, node ? xode_get_name(node) : "n/a");
  285. switch (type) {
  286. case XODE_STREAM_ROOT:
  287. conn->stream_id = strdup(random_secret());
  288. net_printf(conn->fd,
  289. "<?xml version='1.0'?>"
  290. "<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:server' version='1.0'"
  291. " xmlns:db='jabber:server:dialback' id='%s' from='%s'>", conn->stream_id, xmpp_domain);
  292. net_printf(conn->fd,"<stream:features xmlns:stream='http://etherx.jabber.org/streams'/>");
  293. break;
  294. case XODE_STREAM_NODE:
  295. tag = xode_get_name(node);
  296. if (!strcmp(tag, "db:result")) {
  297. char *from = xode_get_attrib(node, "from");
  298. char *to = xode_get_attrib(node, "to");
  299. /* char *id = xode_get_attrib(node, "id"); */
  300. char *type = xode_get_attrib(node, "type");
  301. char *cdata = xode_get_data(node);
  302. if (!type) {
  303. if (conn->domain) {
  304. LM_DBG("connection %d has old domain '%s'\n",conn->fd,
  305. conn->domain);
  306. free(conn->domain);
  307. }
  308. conn->domain = strdup(from);
  309. LM_DBG("connection %d set domain '%s'\n",
  310. conn->fd, conn->domain);
  311. /* it's a request; send verification over outgoing connection */
  312. x = xode_new_tag("db:verify");
  313. xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
  314. xode_put_attrib(x, "from", to);
  315. xode_put_attrib(x, "to", from);
  316. //xode_put_attrib(x, "id", "someid"); /* XXX fix ID */
  317. xode_put_attrib(x, "id", conn->stream_id);
  318. xode_insert_cdata(x, cdata, -1);
  319. xode_send_domain(from, x);
  320. }
  321. } else if (!strcmp(tag, "db:verify")) {
  322. char *from = xode_get_attrib(node, "from");
  323. char *to = xode_get_attrib(node, "to");
  324. char *id = xode_get_attrib(node, "id");
  325. char *type = xode_get_attrib(node, "type");
  326. char *cdata = xode_get_data(node);
  327. if (!type) {
  328. /* it's a request */
  329. x = xode_new_tag("db:verify");
  330. xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
  331. xode_put_attrib(x, "from", to);
  332. xode_put_attrib(x, "to", from);
  333. xode_put_attrib(x, "id", id);
  334. //if (cdata && !strcmp(cdata, DB_KEY)) {
  335. if (cdata && !strcmp(cdata, db_key(local_secret, from, id))) {
  336. xode_put_attrib(x, "type", "valid");
  337. } else {
  338. xode_put_attrib(x, "type", "invalid");
  339. }
  340. xode_send(conn->fd, x);
  341. xode_free(x);
  342. }
  343. } else if (!strcmp(tag, "message")) {
  344. char *from = xode_get_attrib(node, "from");
  345. char *to = xode_get_attrib(node, "to");
  346. char *type = xode_get_attrib(node, "type");
  347. xode body = xode_get_tag(node, "body");
  348. char *msg;
  349. if (!type)
  350. type = "chat";
  351. if (!strcmp(type, "error")) {
  352. LM_DBG("received message error stanza\n");
  353. goto out;
  354. }
  355. if (!from || !to || !body) {
  356. LM_DBG("invalid <message/> attributes\n");
  357. goto out;
  358. }
  359. if (!(msg = xode_get_data(body)))
  360. msg = "";
  361. xmpp_send_sip_msg(
  362. encode_uri_xmpp_sip(from),
  363. decode_uri_xmpp_sip(to),
  364. msg);
  365. } else if (!strcmp(tag, "presence")) {
  366. /* run presence callbacks */
  367. }
  368. break;
  369. break;
  370. case XODE_STREAM_ERROR:
  371. LM_ERR("instream error\n");
  372. /* fall-through */
  373. case XODE_STREAM_CLOSE:
  374. conn->type = CONN_DEAD;
  375. break;
  376. }
  377. out:
  378. xode_free(node);
  379. }
  380. static void do_send_message_server(struct xmpp_pipe_cmd *cmd)
  381. {
  382. char *domain;
  383. xode x;
  384. LM_DBG("rom=[%s] to=[%s] body=[%s]\n", cmd->from,cmd->to, cmd->body);
  385. x = xode_new_tag("message");
  386. xode_put_attrib(x, "xmlns", "jabber:client");
  387. xode_put_attrib(x, "id", cmd->id); // XXX
  388. xode_put_attrib(x, "from", encode_uri_sip_xmpp(cmd->from));
  389. xode_put_attrib(x, "to", decode_uri_sip_xmpp(cmd->to));
  390. xode_put_attrib(x, "type", "chat");
  391. xode_insert_cdata(xode_insert_tag(x, "body"), cmd->body, -1);
  392. domain = extract_domain(decode_uri_sip_xmpp(cmd->to));
  393. xode_send_domain(domain, x);
  394. }
  395. int xmpp_server_child_process(int data_pipe)
  396. {
  397. int rv;
  398. int listen_fd;
  399. fd_set fdset;
  400. struct xmpp_connection *conn;
  401. snprintf(local_secret, sizeof(local_secret), "%s", random_secret());
  402. while ((listen_fd = net_listen(xmpp_domain, xmpp_port)) < 0) {
  403. /* ugh. */
  404. sleep(3);
  405. }
  406. while (1) {
  407. FD_ZERO(&fdset);
  408. FD_SET(data_pipe, &fdset);
  409. FD_SET(listen_fd, &fdset);
  410. /* check for dead connections */
  411. for (conn = conn_list; conn; ) {
  412. struct xmpp_connection *next = conn->next;
  413. if (conn->type == CONN_DEAD)
  414. conn_free(conn);
  415. conn = next;
  416. }
  417. for (conn = conn_list; conn; conn = conn->next) {
  418. /* check if we need to set up a connection */
  419. if (conn->type == CONN_OUTBOUND && conn->fd == -1) {
  420. if ((conn->fd = net_connect(conn->domain, xmpp_port)) >= 0)
  421. {
  422. net_printf(conn->fd,
  423. "<?xml version='1.0'?>"
  424. "<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:server' version='1.0' "
  425. "xmlns:db='jabber:server:dialback' to='%s' from='%s'>",
  426. conn->domain, xmpp_domain);
  427. net_printf(conn->fd,
  428. "<stream:features xmlns:stream='http://etherx.jabber.org/streams'/>");
  429. } else {
  430. conn->type = CONN_DEAD;
  431. }
  432. }
  433. if (conn->fd != -1)
  434. FD_SET(conn->fd, &fdset);
  435. }
  436. rv = select(FD_SETSIZE, &fdset, NULL, NULL, NULL);
  437. /* update the local config framework structures */
  438. cfg_update();
  439. if (rv < 0) {
  440. LM_ERR("select() failed: %s\n", strerror(errno));
  441. } else if (!rv) {
  442. /* timeout */
  443. } else {
  444. for (conn = conn_list; conn; conn = conn->next) {
  445. if (conn->fd != -1 && FD_ISSET(conn->fd, &fdset)) {
  446. char *buf = net_read_static(conn->fd);
  447. if (!buf) {
  448. conn->type = CONN_DEAD;
  449. } else {
  450. LM_DBG("stream (fd %d, domain '%s') read\n[%s]\n",
  451. conn->fd, conn->domain, buf);
  452. xode_stream_eat(conn->stream, buf, strlen(buf));
  453. }
  454. }
  455. }
  456. if (FD_ISSET(listen_fd, &fdset)) {
  457. struct sockaddr_in sin;
  458. unsigned int len = sizeof(sin);
  459. int fd;
  460. if ((fd = accept(listen_fd,(struct sockaddr*)&sin, &len))<0) {
  461. LM_ERR("accept() failed: %s\n", strerror(errno));
  462. } else {
  463. LM_DBG("accept()ed connection from %s:%d\n",
  464. inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
  465. conn_new(CONN_INBOUND, fd, NULL);
  466. }
  467. }
  468. if (FD_ISSET(data_pipe, &fdset)) {
  469. struct xmpp_pipe_cmd *cmd;
  470. if (read(data_pipe, &cmd, sizeof(cmd)) != sizeof(cmd)) {
  471. LM_ERR("failed to read from command pipe: %s\n",
  472. strerror(errno));
  473. } else {
  474. LM_DBG("got pipe cmd %d\n", cmd->type);
  475. switch (cmd->type) {
  476. case XMPP_PIPE_SEND_MESSAGE:
  477. do_send_message_server(cmd);
  478. break;
  479. case XMPP_PIPE_SEND_PACKET:
  480. case XMPP_PIPE_SEND_PSUBSCRIBE:
  481. case XMPP_PIPE_SEND_PNOTIFY:
  482. break;
  483. }
  484. xmpp_free_pipe_cmd(cmd);
  485. }
  486. }
  487. }
  488. }
  489. return 0;
  490. }