Exemplo n.º 1
0
int get_rules_doc(str* user, str* domain, str *file_uri, int type, str** rules_doc)
{
	db_key_t query_cols[3];
	db_val_t query_vals[3];
	db_key_t result_cols[1];
	int n_query_cols = 0;
	db1_res_t *result = 0;
	db_row_t *row;
	db_val_t *row_vals;
	str body;
	str* doc= NULL;
	int n_result_cols= 0, xcap_doc_col;
	static str tmp1 = str_init("doc_type");
	static str tmp2 = str_init("doc_uri");
	static str tmp3 = str_init("username");
	static str tmp4 = str_init("domain");
	static str tmp5 = str_init("doc");

	if(force_active)
	{
		*rules_doc= NULL;
		return 0;
	}
	LM_DBG("[user]= %.*s\t[domain]= %.*s", 
			user->len, user->s,	domain->len, domain->s);

	/* first search in database */
	query_cols[n_query_cols] = &tmp1;
	query_vals[n_query_cols].type = DB1_INT;
	query_vals[n_query_cols].nul = 0;
	query_vals[n_query_cols].val.int_val= type;
	n_query_cols++;

	if (file_uri != NULL)
	{
		query_cols[n_query_cols] = &tmp2;
		query_vals[n_query_cols].type = DB1_STR;
		query_vals[n_query_cols].nul = 0;
		query_vals[n_query_cols].val.str_val = *file_uri;
		n_query_cols++;
	}
	else if (user != NULL && domain != NULL)
	{
		query_cols[n_query_cols] = &tmp3;
		query_vals[n_query_cols].type = DB1_STR;
		query_vals[n_query_cols].nul = 0;
		query_vals[n_query_cols].val.str_val = *user;
		n_query_cols++;
	
		query_cols[n_query_cols] = &tmp4;
		query_vals[n_query_cols].type = DB1_STR;
		query_vals[n_query_cols].nul = 0;
		query_vals[n_query_cols].val.str_val = *domain;
		n_query_cols++;
	}
	else
	{
		LM_ERR("Need to specify file uri _OR_ username and domain\n");
		return -1;
	}
	
	result_cols[xcap_doc_col= n_result_cols++] = &tmp5;
	
	if (pxml_dbf.use_table(pxml_db, &xcap_table) < 0) 
	{
		LM_ERR("in use_table-[table]= %.*s\n", xcap_table.len, xcap_table.s);
		return -1;
	}

	if( pxml_dbf.query(pxml_db, query_cols, 0 , query_vals, result_cols, 
				n_query_cols, 1, 0, &result)<0)
	{
		LM_ERR("while querying table xcap for [user]=%.*s\t[domain]= %.*s\n",
				user->len, user->s,	domain->len, domain->s);
		if(result)
			pxml_dbf.free_result(pxml_db, result);
		return -1;
	}
	if(result== NULL)
		return -1;

	if(result->n<= 0)
	{
		LM_DBG("No document found in db table for [user]=%.*s"
			"\t[domain]= %.*s\t[doc_type]= %d\n",user->len, user->s,
			domain->len, domain->s, type);
		
		if (!integrated_xcap_server && type != PRES_RULES)
		{
			LM_WARN("Cannot retrieve non pres-rules documents from"
				"external XCAP server\n");
		}
		else if(!integrated_xcap_server)
		{
			if(http_get_rules_doc(*user, *domain, &body)< 0)
			{
				LM_ERR("sending http GET request to xcap server\n");		
				goto error;
			}
			if(body.s && body.len)
				goto done; 
		}
		pxml_dbf.free_result(pxml_db, result);
		return 0;
	}	
	
	row = &result->rows[xcap_doc_col];
	row_vals = ROW_VALUES(row);

	body.s = (char*)row_vals[0].val.string_val;
	if(body.s== NULL)
	{
		LM_ERR("Xcap doc NULL\n");
		goto error;
	}	
	body.len = strlen(body.s);
	if(body.len== 0)
	{
		LM_ERR("Xcap doc empty\n");
		goto error;
	}			
	LM_DBG("xcap document:\n%.*s", body.len,body.s);

done:
	doc= (str*)pkg_malloc(sizeof(str));
	if(doc== NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}
	doc->s= (char*)pkg_malloc(body.len* sizeof(char));
	if(doc->s== NULL)
	{
		pkg_free(doc);
		ERR_MEM(PKG_MEM_STR);
	}
	memcpy(doc->s, body.s, body.len);
	doc->len= body.len;

	*rules_doc= doc;

	if(result)
		pxml_dbf.free_result(pxml_db, result);

	return 1;

error:
	if(result)
		pxml_dbf.free_result(pxml_db, result);

	return -1;

}
Exemplo n.º 2
0
int get_rules_doc(str* user, str* domain, int type, str** rules_doc)
{
	db_key_t query_cols[5];
	db_val_t query_vals[5];
	db_key_t result_cols[3];
	int n_query_cols = 0;
	db_res_t *result = 0;
	db_row_t *row;
	db_val_t *row_vals;
	str body;
	str* doc= NULL;
	int n_result_cols= 0, xcap_doc_col;
	static str tmp1 = str_init("username");
	static str tmp2 = str_init("domain");
	static str tmp3 = str_init("doc_type");
	static str tmp4 = str_init("doc");

	*rules_doc= NULL;
	
	if(force_active)
		return 0;
	
	LM_DBG("[user]= %.*s\t[domain]= %.*s\n", 
			user->len, user->s,	domain->len, domain->s);
	/* first search in database */
	query_cols[n_query_cols] = &tmp1;
	query_vals[n_query_cols].type = DB_STR;
	query_vals[n_query_cols].nul = 0;
	query_vals[n_query_cols].val.str_val = *user;
	n_query_cols++;
	
	query_cols[n_query_cols] = &tmp2;
	query_vals[n_query_cols].type = DB_STR;
	query_vals[n_query_cols].nul = 0;
	query_vals[n_query_cols].val.str_val = *domain;
	n_query_cols++;
	
	query_cols[n_query_cols] = &tmp3;
	query_vals[n_query_cols].type = DB_INT;
	query_vals[n_query_cols].nul = 0;
	query_vals[n_query_cols].val.int_val= type;
	n_query_cols++;

	result_cols[xcap_doc_col= n_result_cols++] = &tmp4;
	
	if (pxml_dbf.use_table(pxml_db, &xcap_table) < 0) 
	{
		LM_ERR("in use_table-[table]= %.*s\n", xcap_table.len, xcap_table.s);
		return -1;
	}

	if( pxml_dbf.query(pxml_db, query_cols, 0 , query_vals, result_cols, 
				n_query_cols, 1, 0, &result)<0)
	{
		LM_ERR("while querying table xcap for [user]=%.*s\t[domain]= %.*s\n",
				user->len, user->s,	domain->len, domain->s);
		if(result)
			pxml_dbf.free_result(pxml_db, result);
		return -1;
	}
	if(result== NULL)
		return -1;

	if(result->n<= 0)
	{
		LM_DBG("No document found in db table for [user]=%.*s"
			"\t[domain]= %.*s\t[doc_type]= %d\n",user->len, user->s,
			domain->len, domain->s, type);
		
		if(!integrated_xcap_server)
		{
			if(http_get_rules_doc(*user, *domain, &body)< 0)
			{
				LM_ERR("fetching document with HTTP request from xcap server\n");
				goto error;
			}
			if(body.s && body.len)  /* if the document was found */
				goto done;
		}
		pxml_dbf.free_result(pxml_db, result);
		return 0;
	}
	
	row = &result->rows[xcap_doc_col];
	row_vals = ROW_VALUES(row);

	switch (row_vals[0].type) {
		case DB_STRING:
			LM_DBG("extracted db_string\n");
			body.s = (char*)row_vals[0].val.string_val;
			if (body.s)
				body.len = strlen(body.s);
			break;
		case DB_STR:
			LM_DBG("extracted db_str\n");
			body = row_vals[0].val.str_val;
			break;
		case DB_BLOB:
			LM_DBG("extracted db_blob\n");
			body = row_vals[0].val.blob_val;
			break;
		default:
			LM_ERR("unexpected column type %d\n",row_vals[0].type);
			goto error;
	}

	if(body.s== NULL)
	{
		LM_ERR("Xcap doc NULL\n");
		goto error;
	}	

	if(body.len== 0)
	{
		LM_ERR("Xcap doc empty\n");
		goto error;
	}			
	LM_DBG("xcap document:\n%.*s", body.len,body.s);

done:
	doc= (str*)pkg_malloc(sizeof(str));
	if(doc== NULL)
	{
		ERR_MEM(PKG_MEM_STR);
	}
	doc->s= (char*)pkg_malloc(body.len* sizeof(char));
	if(doc->s== NULL)
	{
		pkg_free(doc);
		ERR_MEM(PKG_MEM_STR);
	}
	memcpy(doc->s, body.s, body.len);
	doc->len= body.len;

	*rules_doc= doc;

	if(result)
		pxml_dbf.free_result(pxml_db, result);

	return 0;

error:
	if(result)
		pxml_dbf.free_result(pxml_db, result);

	return -1;

}