modules/xcap_client/xcap_client.c
fef26baf
 /*
6c780225
  * xcap_client module - XCAP client for Kamailio
fef26baf
  *
  * Copyright (C) 2007 Voice Sistem S.R.L.
  *
27642a08
  * This file is part of Kamailio, a free SIP server.
fef26baf
  *
27642a08
  * Kamailio is free software; you can redistribute it and/or modify
fef26baf
  * 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
  *
27642a08
  * Kamailio is distributed in the hope that it will be useful,
fef26baf
  * 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 
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
fef26baf
  *
  */
 
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/ipc.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <time.h>
 #include <curl/curl.h>
 
 #include "../../pt.h"
6ffa6267
 #include "../../lib/srdb1/db.h"
fef26baf
 #include "../../sr_module.h"
 #include "../../dprint.h"
 #include "../../error.h"
 #include "../../ut.h"
 #include "../../mem/mem.h"
 #include "../../mem/shm_mem.h"
d54b0b8a
 #include "../../lib/kmi/mi.h"
053eb4ff
 #include "../presence/utils_func.h"
fef26baf
 #include "xcap_functions.h"
 #include "xcap_client.h"
 
 MODULE_VERSION
 
ab36b2b1
 #define XCAP_TABLE_VERSION   4
053eb4ff
 
fef26baf
 static int mod_init(void);
7e2bf0f1
 static int child_init(int rank);
81fad30a
 static void destroy(void);
fef26baf
 struct mi_root* refreshXcapDoc(struct mi_root* cmd, void* param);
 int get_auid_flag(str auid);
e2cf6343
 str xcap_db_table = str_init("xcap");
eb39fdf0
 str xcap_db_url = str_init(DEFAULT_DB_URL);
fef26baf
 xcap_callback_t* xcapcb_list= NULL;
053eb4ff
 int periodical_query= 1;
 unsigned int query_period= 100;
 
e2cf6343
 str str_source_col = str_init("source");
 str str_path_col = str_init("path");
 str str_doc_col = str_init("doc");
 str str_etag_col = str_init("etag");
 str str_username_col = str_init("username");
 str str_domain_col = str_init("domain");
 str str_doc_type_col = str_init("doc_type");
 str str_doc_uri_col = str_init("doc_uri");
 str str_port_col = str_init("port");
 
 
053eb4ff
 /* database connection */
6ffa6267
 db1_con_t *xcap_db = NULL;
053eb4ff
 db_func_t xcap_dbf;
 
 void query_xcap_update(unsigned int ticks, void* param);
 
 static param_export_t params[]={
c2154c70
 	{ "db_url",					PARAM_STR,         &xcap_db_url    },
 	{ "xcap_table",				PARAM_STR,         &xcap_db_table  },
053eb4ff
 	{ "periodical_query",		INT_PARAM,         &periodical_query },
 	{ "query_period",	       	INT_PARAM,         &query_period     },
 	{    0,                     0,                      0            }
 };
 
fef26baf
 
 static cmd_export_t  cmds[]=
 {	
80998a7f
 	{"bind_xcap",  (cmd_function)bind_xcap,  1,    0, 0,            0},
 	{    0,                     0,           0,    0, 0,           0}
fef26baf
 };
 
 static mi_export_t mi_cmds[] = {
 	{ "refreshXcapDoc", refreshXcapDoc,      0,  0,  0},
 	{ 0,                 0,                  0,  0,  0}
 };
 
 /** module exports */
 struct module_exports exports= {
 	"xcap_client",				/* module name */
 	DEFAULT_DLFLAGS,			/* dlopen flags */
 	cmds,  						/* exported functions */
053eb4ff
 	params,						/* exported parameters */
fef26baf
 	0,      					/* exported statistics */
 	mi_cmds,   					/* exported MI functions */
 	0,							/* exported pseudo-variables */
 	0,							/* extra processes */
 	mod_init,					/* module initialization function */
3ed44241
 	0,							/* response handling function */
fef26baf
 	(destroy_function) destroy, /* destroy function */
7e2bf0f1
 	child_init					/* per-child init function */
fef26baf
 };
 
 /**
  * init module function
  */
 static int mod_init(void)
 {
f9fe1066
 	if(register_mi_mod(exports.name, mi_cmds)!=0)
 	{
 		LM_ERR("failed to register MI commands\n");
 		return -1;
 	}
053eb4ff
 	
 	/* binding to mysql module  */
e2cf6343
 	if (db_bind_mod(&xcap_db_url, &xcap_dbf))
053eb4ff
 	{
 		LM_ERR("Database module not found\n");
 		return -1;
 	}
 	
 	if (!DB_CAPABILITY(xcap_dbf, DB_CAP_ALL)) {
 		LM_ERR("Database module does not implement all functions"
 				" needed by the module\n");
 		return -1;
 	}
 
e2cf6343
 	xcap_db = xcap_dbf.init(&xcap_db_url);
053eb4ff
 	if (!xcap_db)
 	{
 		LM_ERR("while connecting to database\n");
 		return -1;
 	}
 
7e2bf0f1
 	if(db_check_table_version(&xcap_dbf, xcap_db, &xcap_db_table,
 				XCAP_TABLE_VERSION) < 0) {
924f2089
 		LM_ERR("error during table version check.\n");
053eb4ff
 		return -1;
 	}
7e2bf0f1
 	xcap_dbf.close(xcap_db);
 	xcap_db = NULL;
053eb4ff
 
fef26baf
 	curl_global_init(CURL_GLOBAL_ALL);
053eb4ff
 
 	if(periodical_query)
 	{
 		register_timer(query_xcap_update, 0, query_period);
 	}
fef26baf
 	return 0;
 }
 
7e2bf0f1
 static int child_init(int rank)
 {
82a5ef65
 	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
 		return 0; /* do nothing for the main process */
 
7e2bf0f1
 	if((xcap_db = xcap_dbf.init(&xcap_db_url))==NULL)
 	{
 		LM_ERR("cannot connect to db\n");
 		return -1;
 	}
2e51695a
 	return 0;
7e2bf0f1
 }
 
81fad30a
 static void destroy(void)
fef26baf
 {
 	curl_global_cleanup();
7e2bf0f1
 	if(xcap_db != NULL)
 		xcap_dbf.close(xcap_db);
fef26baf
 }
 
053eb4ff
 void query_xcap_update(unsigned int ticks, void* param)
 {
 	db_key_t query_cols[3], update_cols[3];
 	db_val_t query_vals[3], update_vals[3];
 	db_key_t result_cols[7];
 	int n_result_cols = 0, n_query_cols= 0, n_update_cols= 0;
6ffa6267
 	db1_res_t* result= NULL;
053eb4ff
 	int user_col, domain_col, doc_type_col, etag_col, doc_uri_col, port_col; 
 	db_row_t *row ;	
 	db_val_t *row_vals ;
 	unsigned int port;
 	char* etag, *path, *new_etag= NULL, *doc= NULL;
 	int u_doc_col, u_etag_col;
 	str user, domain, uri;
 	int i;
 
 	/* query the ones I have to handle */
e2cf6343
 	query_cols[n_query_cols] = &str_source_col;
6ffa6267
 	query_vals[n_query_cols].type = DB1_INT;
053eb4ff
 	query_vals[n_query_cols].nul = 0;
 	query_vals[n_query_cols].val.int_val= XCAP_CL_MOD;
 	n_query_cols++;
 
e2cf6343
 	query_cols[n_query_cols] = &str_path_col;
6ffa6267
 	query_vals[n_query_cols].type = DB1_STR;
053eb4ff
 	query_vals[n_query_cols].nul = 0;
 
e2cf6343
 	update_cols[u_doc_col=n_update_cols] = &str_doc_col;
6ffa6267
 	update_vals[n_update_cols].type = DB1_STRING;
053eb4ff
 	update_vals[n_update_cols].nul = 0;
 	n_update_cols++;
 
e2cf6343
 	update_cols[u_etag_col=n_update_cols] = &str_etag_col;
6ffa6267
 	update_vals[n_update_cols].type = DB1_STRING;
053eb4ff
 	update_vals[n_update_cols].nul = 0;
 	n_update_cols++;
 
e2cf6343
 	result_cols[user_col= n_result_cols++]     = &str_username_col;
 	result_cols[domain_col=n_result_cols++]    = &str_domain_col;
 	result_cols[doc_type_col=n_result_cols++]  = &str_doc_type_col;
 	result_cols[etag_col=n_result_cols++]      = &str_etag_col;
 	result_cols[doc_uri_col= n_result_cols++]  = &str_doc_uri_col;
 	result_cols[port_col= n_result_cols++]     = &str_port_col;
053eb4ff
 	
e2cf6343
 	if (xcap_dbf.use_table(xcap_db, &xcap_db_table) < 0) 
053eb4ff
 	{
e2cf6343
 		LM_ERR("in use_table-[table]= %.*s\n", xcap_db_table.len, xcap_db_table.s);
053eb4ff
 		goto error;
 	}
 
 	if(xcap_dbf.query(xcap_db, query_cols, 0, query_vals, result_cols, 1,
 				n_result_cols, 0, &result)< 0)
 	{
 		LM_ERR("in sql query\n");
 		goto error;
 	}
 	if(result== NULL)
 	{
 		LM_ERR("in sql query- null result\n");
 		return;
 	}
 	if(result->n<= 0)
 	{
 		xcap_dbf.free_result(xcap_db, result);
 		return;
 	}
 	n_query_cols++;
 	
 	/* ask if updated */
 	for(i= 0; i< result->n; i++)
 	{
 		row = &result->rows[i];
 		row_vals = ROW_VALUES(row);
 	
 		path= (char*)row_vals[doc_uri_col].val.string_val;
 		port= row_vals[port_col].val.int_val;
 		etag= (char*)row_vals[etag_col].val.string_val;	
 
 		user.s= (char*)row_vals[user_col].val.string_val;
 		user.len= strlen(user.s);
 
 		domain.s= (char*)row_vals[domain_col].val.string_val;
 		domain.len= strlen(domain.s);
 
 		/* send HTTP request */
 		doc= send_http_get(path, port, etag, IF_NONE_MATCH, &new_etag);
 		if(doc== NULL)
 		{
 			LM_DBG("document not update\n");
 			continue;
 		}
 		if(new_etag== NULL)
 		{
 			LM_ERR("etag not found\n");
 			pkg_free(doc);
 			goto error;
 		}
 		/* update in xcap db table */
 		update_vals[u_doc_col].val.string_val= doc;
 		update_vals[u_etag_col].val.string_val= etag;
 		
 		if(xcap_dbf.update(xcap_db, query_cols, 0, query_vals, update_cols,
 					update_vals, n_query_cols, n_update_cols)< 0)
 		{
 			LM_ERR("in sql update\n");
 			pkg_free(doc);
 			goto error;
 		}
 		/* call registered callbacks */
 		if(uandd_to_uri(user, domain, &uri)< 0)
 		{
 			LM_ERR("converting user and domain to uri\n");
 			pkg_free(doc);
 			goto error;
 		}
 		run_xcap_update_cb(row_vals[doc_type_col].val.int_val, uri, doc);
 		pkg_free(doc);
 
 	}
 
 	xcap_dbf.free_result(xcap_db, result);
 	return;
 
 error:
 	if(result)
 		xcap_dbf.free_result(xcap_db, result);
 }
fef26baf
 
 int parse_doc_url(str doc_url, char** serv_addr, xcap_doc_sel_t* doc_sel)
 {
 	char* sl, *str_type;	
 	
 	sl= strchr(doc_url.s, '/');
 	*sl= '\0';
 	*serv_addr= doc_url.s;
 	
 	sl++;
 	doc_sel->auid.s= sl;
 	sl= strchr(sl, '/');
 	doc_sel->auid.len= sl- doc_sel->auid.s;
 	
 	sl++;
 	str_type= sl;
 	sl= strchr(sl, '/');
 	*sl= '\0';
 
 	if(strcasecmp(str_type, "users")== 0)
 		doc_sel->type= USERS_TYPE;
 	else
 	if(strcasecmp(str_type, "group")== 0)
 		doc_sel->type= GLOBAL_TYPE;
 
 	sl++;
 
 	return 0;
 
 }
 /*
  * mi cmd: refreshXcapDoc
053eb4ff
  *			<document uri> 
  *			<xcap_port>
fef26baf
  * */
 
 struct mi_root* refreshXcapDoc(struct mi_root* cmd, void* param)
 {
 	struct mi_node* node= NULL;
 	str doc_url;
 	xcap_doc_sel_t doc_sel;
 	char* serv_addr;
053eb4ff
 	char* stream= NULL;
fef26baf
 	int type;
053eb4ff
 	unsigned int xcap_port;
 	char* etag= NULL;
fef26baf
 
 	node = cmd->node.kids;
 	if(node == NULL)
 		return 0;
 
 	doc_url = node->value;
 	if(doc_url.s == NULL || doc_url.len== 0)
 	{
c3d78e5e
 		LM_ERR("empty uri\n");
fef26baf
 		return init_mi_tree(404, "Empty document URL", 20);
 	}
053eb4ff
 	node= node->next;
 	if(node== NULL)
 		return 0;
 	if(node->value.s== NULL || node->value.len== 0)
 	{
 		LM_ERR("port number\n");
 		return init_mi_tree(404, "Empty document URL", 20);
 	}
 	if(str2int(&node->value, &xcap_port)< 0)
 	{
 		LM_ERR("while converting string to int\n");
 		goto error;
 	}
 
fef26baf
 	if(node->next!= NULL)
 		return 0;
 
 	/* send GET HTTP request to the server */
053eb4ff
 	stream=	send_http_get(doc_url.s, xcap_port, NULL, 0, &etag);
fef26baf
 	if(stream== NULL)
 	{
c3d78e5e
 		LM_ERR("in http get\n");
fef26baf
 		return 0;
 	}
 	
 	/* call registered functions with document argument */
 	if(parse_doc_url(doc_url, &serv_addr, &doc_sel)< 0)
 	{
c3d78e5e
 		LM_ERR("parsing document url\n");
fef26baf
 		return 0;
 	}
 
 	type= get_auid_flag(doc_sel.auid);
 	if(type< 0)
 	{
c3d78e5e
 		LM_ERR("incorect auid: %.*s\n",
 				doc_sel.auid.len, doc_sel.auid.s);
fef26baf
 		goto error;
 	}
 
 	run_xcap_update_cb(type, doc_sel.xid, stream);
 
 	return init_mi_tree(200, "OK", 2);
 
 error:
 	if(stream)
 		pkg_free(stream);
 	return 0;
 }
 
3f70b926
 #define STR_MATCH(s1, s2)   ((s1).len==(s2).len && memcmp((s1).s, (s2).s, (s1).len)==0)
4f3e8cb6
 
fef26baf
 int get_auid_flag(str auid)
 {
4f3e8cb6
 	static str pres_rules = str_init("pres-rules");
 	static str rls_services = str_init("rls-services");
fef26baf
 
4f3e8cb6
 	if (STR_MATCH(auid, pres_rules))
 		return PRES_RULES;
 	else if (STR_MATCH(auid, rls_services))
 		return RESOURCE_LIST;
fef26baf
 
 	return -1;
 }