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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
|
/*
* $Id$
*
* XMPP Module
* This file is part of Kamailio, a free SIP server.
*
* Copyright (C) 2006 Voice Sistem S.R.L.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author: Andreea Spirea
*
*/
/*! \file
* \brief Kamailio XMPP :: XMPP server implementation (limited functionality)
* \ingroup xmpp
*/
/*
* An inbound SIP message:
* from sip:user1@domain1 to sip:user2*domain2@gateway_domain
* is translated to an XMPP message:
* from user1*domain1@xmpp_domain to user2@domain2
*
* An inbound XMPP message:
* from user1@domain1 to user2*domain2@xmpp_domain
* is translated to a SIP message:
* from sip:user1*domain1@gateway_domain to sip:user2@domain2
*
* Where '*' is the domain_separator, and gateway_domain and
* xmpp_domain are defined below.
*/
/*
* 2-way dialback sequence with xmppd2:
*
* Originating server (us) Receiving server (them) Authoritative server (us)
* ----------------------- ----------------------- -------------------------
* | | |
* | establish connection | |
* |------------------------------>| |
* | send stream header | |
* |------------------------------>| |
* | send stream header | |
* |<------------------------------| |
* | send db:result request | |
* |------------------------------>| |
* | establish connection |
* |------------------------------>|
* | send stream header |
* |------------------------------>|
* | send stream header |
* |<------------------------------|
* | send db:result request |
* |------------------------------>|
* | send db:verify request |
* |------------------------------>|
* | send db:verify response |
* |<------------------------------|
* | send db:result response |
* |------------------------------>|
* | send db:verify request |
* |<------------------------------|
* | send db:verify response |
* |------------------------------>|
* | send db:result response |
* |<------------------------------|
* : : :
* : : :
* | outgoing <message/> | :
* |------------------------------>| :
* | incoming <message/> |
* |------------------------------>|
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "../../sr_module.h"
#include "../../cfg/cfg_struct.h"
#include "xmpp.h"
#include "xmpp_api.h"
#include "network.h"
#include "xode.h"
#include <arpa/inet.h>
/* XXX hack */
#define DB_KEY "this-be-a-random-key"
#define CONN_DEAD 0
#define CONN_INBOUND 1
#define CONN_OUTBOUND 2
struct xmpp_private_data {
int fd; /* outgoing stream socket */
int listen_fd; /* listening socket */
int in_fd; /* incoming stream socket */
int running;
};
struct xmpp_connection {
struct xmpp_connection *next;
char *domain;
int type;
int fd;
char *stream_id;
xode_pool pool;
xode_stream stream;
xode todo; /* backlog of outgoing messages, if any */
};
static char local_secret[64] = { 0, };
static void in_stream_node_callback(int type, xode node, void *arg);
static void out_stream_node_callback(int type, xode node, void *arg);
static struct xmpp_connection *conn_list = NULL;
static struct xmpp_connection *conn_new(int type, int fd, char *domain)
{
struct xmpp_connection *conn = NULL;
conn = malloc(sizeof(struct xmpp_connection));
if(conn==NULL)
{
LM_ERR("out of memory\n");
return NULL;
}
memset(conn, 0, sizeof(struct xmpp_connection));
conn->domain = domain ? strdup(domain) : NULL;
conn->type = type;
conn->fd = fd;
conn->todo = xode_new_tag("todo");
conn->pool = xode_pool_new();
conn->stream = xode_stream_new(conn->pool,
(type==CONN_INBOUND)?in_stream_node_callback:out_stream_node_callback,
conn);
conn->next = conn_list;
conn_list = conn;
return conn;
}
static void conn_free(struct xmpp_connection *conn)
{
struct xmpp_connection **last_p, *link;
last_p = &conn_list;
for (link = conn_list; link; link = link->next) {
if (link == conn) {
*last_p = link->next;
break;
}
last_p = &link->next;
}
if (conn->todo)
xode_free(conn->todo);
xode_pool_free(conn->pool);
if (conn->fd != -1)
close(conn->fd);
if (conn->stream_id)
free(conn->stream_id);
if (conn->domain)
free(conn->domain);
free(conn);
}
static struct xmpp_connection *conn_find_domain(char *domain, int type)
{
struct xmpp_connection *conn;
for (conn = conn_list; conn; conn = conn->next)
if (conn->domain && !strcasecmp(conn->domain, domain)
&& conn->type == type)
return conn;
return NULL;
}
/*
static struct xmpp_connection *conn_find_fd(int fd)
{
struct xmpp_connection *conn;
for (conn = conn_list; conn; conn = conn->next)
if (conn->fd == fd)
return conn;
return NULL;
}
*/
/*****************************************************************************/
static int xode_send(int fd, xode x)
{
char *str = xode_to_str(x);
int len = strlen(str);
LM_DBG("xode_send->%d [%s]\n", fd, str);
if (net_send(fd, str, len) != len) {
LM_ERR("send() failed: %s\n", strerror(errno));
return -1;
}
return len;
}
static int xode_send_domain(char *domain, xode x)
{
struct xmpp_connection *conn;
if ((conn = conn_find_domain(domain, CONN_OUTBOUND))) {
xode_send(conn->fd, x);
xode_free(x);
} else {
if((conn = conn_new(CONN_OUTBOUND, -1, domain))==0)
return -1;
xode_insert_node(conn->todo, x);
}
return 1;
}
static void out_stream_node_callback(int type, xode node, void *arg)
{
struct xmpp_connection *conn = (struct xmpp_connection *) arg;
struct xmpp_connection *in_conn = NULL;
char *tag;
xode x;
LM_DBG("outstream callback: %d: %s\n", type,
node?xode_get_name(node):"n/a");
if (conn->domain)
in_conn = conn_find_domain(conn->domain, CONN_INBOUND);
switch (type) {
case XODE_STREAM_ROOT:
x = xode_new_tag("db:result");
xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
xode_put_attrib(x, "from", xmpp_domain);
xode_put_attrib(x, "to", conn->domain);
//xode_insert_cdata(x, DB_KEY, -1);
xode_insert_cdata(x, db_key(local_secret, conn->domain,
xode_get_attrib(node, "id")), -1);
xode_send(conn->fd, x);
xode_free(x);
break;
case XODE_STREAM_NODE:
tag = xode_get_name(node);
if (!strcmp(tag, "db:verify")) {
char *from = xode_get_attrib(node, "from");
char *to = xode_get_attrib(node, "to");
char *id = xode_get_attrib(node, "id");
char *type = xode_get_attrib(node, "type");
/* char *cdata = xode_get_data(node); */
if (!strcmp(type, "valid") || !strcmp(type, "invalid")) {
/* got a reply, report it */
x = xode_new_tag("db:result");
xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
xode_put_attrib(x, "from", to);
xode_put_attrib(x, "to", from);
xode_put_attrib(x, "id", id);
xode_put_attrib(x, "type", type);
if (in_conn)
xode_send(in_conn->fd, x);
else
LM_ERR("need to send reply to domain '%s', but no inbound"
" connection found\n", from);
xode_free(x);
}
} else if (!strcmp(tag, "db:result")) {
char *type = xode_get_attrib(node, "type");
if (type && !strcmp(type, "valid")) {
/* the remote server has successfully authenticated us,
* we can now send data */
for (x = xode_get_firstchild(conn->todo); x;
x = xode_get_nextsibling(x)) {
LM_DBG("sending todo tag '%s'\n", xode_get_name(x));
xode_send(conn->fd, x);
}
xode_free(conn->todo);
conn->todo = NULL;
}
}
break;
case XODE_STREAM_ERROR:
LM_ERR("outstream error\n");
/* fall-through */
case XODE_STREAM_CLOSE:
conn->type = CONN_DEAD;
break;
}
xode_free(node);
}
static void in_stream_node_callback(int type, xode node, void *arg)
{
struct xmpp_connection *conn = (struct xmpp_connection *) arg;
char *tag;
xode x;
LM_DBG("instream callback: %d: %s\n",
type, node ? xode_get_name(node) : "n/a");
switch (type) {
case XODE_STREAM_ROOT:
conn->stream_id = strdup(random_secret());
net_printf(conn->fd,
"<?xml version='1.0'?>"
"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:server' version='1.0'"
" xmlns:db='jabber:server:dialback' id='%s' from='%s'>", conn->stream_id, xmpp_domain);
net_printf(conn->fd,"<stream:features xmlns:stream='http://etherx.jabber.org/streams'/>");
break;
case XODE_STREAM_NODE:
tag = xode_get_name(node);
if (!strcmp(tag, "db:result")) {
char *from = xode_get_attrib(node, "from");
char *to = xode_get_attrib(node, "to");
/* char *id = xode_get_attrib(node, "id"); */
char *type = xode_get_attrib(node, "type");
char *cdata = xode_get_data(node);
if (!type) {
if (conn->domain) {
LM_DBG("connection %d has old domain '%s'\n",conn->fd,
conn->domain);
free(conn->domain);
}
conn->domain = strdup(from);
LM_DBG("connection %d set domain '%s'\n",
conn->fd, conn->domain);
/* it's a request; send verification over outgoing connection */
x = xode_new_tag("db:verify");
xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
xode_put_attrib(x, "from", to);
xode_put_attrib(x, "to", from);
//xode_put_attrib(x, "id", "someid"); /* XXX fix ID */
xode_put_attrib(x, "id", conn->stream_id);
xode_insert_cdata(x, cdata, -1);
xode_send_domain(from, x);
}
} else if (!strcmp(tag, "db:verify")) {
char *from = xode_get_attrib(node, "from");
char *to = xode_get_attrib(node, "to");
char *id = xode_get_attrib(node, "id");
char *type = xode_get_attrib(node, "type");
char *cdata = xode_get_data(node);
if (!type) {
/* it's a request */
x = xode_new_tag("db:verify");
xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
xode_put_attrib(x, "from", to);
xode_put_attrib(x, "to", from);
xode_put_attrib(x, "id", id);
//if (cdata && !strcmp(cdata, DB_KEY)) {
if (cdata && !strcmp(cdata, db_key(local_secret, from, id))) {
xode_put_attrib(x, "type", "valid");
} else {
xode_put_attrib(x, "type", "invalid");
}
xode_send(conn->fd, x);
xode_free(x);
}
} else if (!strcmp(tag, "message")) {
char *from = xode_get_attrib(node, "from");
char *to = xode_get_attrib(node, "to");
char *type = xode_get_attrib(node, "type");
xode body = xode_get_tag(node, "body");
char *msg;
if (!type)
type = "chat";
if (!strcmp(type, "error")) {
LM_DBG("received message error stanza\n");
goto out;
}
if (!from || !to || !body) {
LM_DBG("invalid <message/> attributes\n");
goto out;
}
if (!(msg = xode_get_data(body)))
msg = "";
xmpp_send_sip_msg(
encode_uri_xmpp_sip(from),
decode_uri_xmpp_sip(to),
msg);
} else if (!strcmp(tag, "presence")) {
/* run presence callbacks */
}
break;
break;
case XODE_STREAM_ERROR:
LM_ERR("instream error\n");
/* fall-through */
case XODE_STREAM_CLOSE:
conn->type = CONN_DEAD;
break;
}
out:
xode_free(node);
}
static void do_send_message_server(struct xmpp_pipe_cmd *cmd)
{
char *domain;
xode x;
LM_DBG("rom=[%s] to=[%s] body=[%s]\n", cmd->from,cmd->to, cmd->body);
x = xode_new_tag("message");
xode_put_attrib(x, "xmlns", "jabber:client");
xode_put_attrib(x, "id", cmd->id); // XXX
xode_put_attrib(x, "from", encode_uri_sip_xmpp(cmd->from));
xode_put_attrib(x, "to", decode_uri_sip_xmpp(cmd->to));
xode_put_attrib(x, "type", "chat");
xode_insert_cdata(xode_insert_tag(x, "body"), cmd->body, -1);
domain = extract_domain(decode_uri_sip_xmpp(cmd->to));
xode_send_domain(domain, x);
}
int xmpp_server_child_process(int data_pipe)
{
int rv;
int listen_fd;
fd_set fdset;
struct xmpp_connection *conn;
snprintf(local_secret, sizeof(local_secret), "%s", random_secret());
while ((listen_fd = net_listen(xmpp_domain, xmpp_port)) < 0) {
/* ugh. */
sleep(3);
}
while (1) {
FD_ZERO(&fdset);
FD_SET(data_pipe, &fdset);
FD_SET(listen_fd, &fdset);
/* check for dead connections */
for (conn = conn_list; conn; ) {
struct xmpp_connection *next = conn->next;
if (conn->type == CONN_DEAD)
conn_free(conn);
conn = next;
}
for (conn = conn_list; conn; conn = conn->next) {
/* check if we need to set up a connection */
if (conn->type == CONN_OUTBOUND && conn->fd == -1) {
if ((conn->fd = net_connect(conn->domain, xmpp_port)) >= 0)
{
net_printf(conn->fd,
"<?xml version='1.0'?>"
"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:server' version='1.0' "
"xmlns:db='jabber:server:dialback' to='%s' from='%s'>",
conn->domain, xmpp_domain);
net_printf(conn->fd,
"<stream:features xmlns:stream='http://etherx.jabber.org/streams'/>");
} else {
conn->type = CONN_DEAD;
}
}
if (conn->fd != -1)
FD_SET(conn->fd, &fdset);
}
rv = select(FD_SETSIZE, &fdset, NULL, NULL, NULL);
/* update the local config framework structures */
cfg_update();
if (rv < 0) {
LM_ERR("select() failed: %s\n", strerror(errno));
} else if (!rv) {
/* timeout */
} else {
for (conn = conn_list; conn; conn = conn->next) {
if (conn->fd != -1 && FD_ISSET(conn->fd, &fdset)) {
char *buf = net_read_static(conn->fd);
if (!buf) {
conn->type = CONN_DEAD;
} else {
LM_DBG("stream (fd %d, domain '%s') read\n[%s]\n",
conn->fd, conn->domain, buf);
xode_stream_eat(conn->stream, buf, strlen(buf));
}
}
}
if (FD_ISSET(listen_fd, &fdset)) {
struct sockaddr_in sin;
unsigned int len = sizeof(sin);
int fd;
if ((fd = accept(listen_fd,(struct sockaddr*)&sin, &len))<0) {
LM_ERR("accept() failed: %s\n", strerror(errno));
} else {
LM_DBG("accept()ed connection from %s:%d\n",
inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
conn_new(CONN_INBOUND, fd, NULL);
}
}
if (FD_ISSET(data_pipe, &fdset)) {
struct xmpp_pipe_cmd *cmd;
if (read(data_pipe, &cmd, sizeof(cmd)) != sizeof(cmd)) {
LM_ERR("failed to read from command pipe: %s\n",
strerror(errno));
} else {
LM_DBG("got pipe cmd %d\n", cmd->type);
switch (cmd->type) {
case XMPP_PIPE_SEND_MESSAGE:
do_send_message_server(cmd);
break;
case XMPP_PIPE_SEND_PACKET:
case XMPP_PIPE_SEND_PSUBSCRIBE:
case XMPP_PIPE_SEND_PNOTIFY:
break;
}
xmpp_free_pipe_cmd(cmd);
}
}
}
}
return 0;
}
|