Пример #1
0
xmlNodePtr get_rule_node(subs_t* subs, xmlDocPtr xcap_tree)
{
    str w_uri = {0, 0};
    char* id = NULL, *domain = NULL, *time_cont= NULL;
    int apply_rule = -1;
    xmlNodePtr ruleset_node = NULL, node1= NULL, node2= NULL;
    xmlNodePtr cond_node = NULL, except_node = NULL;
    xmlNodePtr identity_node = NULL;
    xmlNodePtr iden_child;
    xmlNodePtr validity_node, time_node;
    time_t t_init, t_fin, t;
    int valid= 0;

    uandd_to_uri(subs->from_user, subs->from_domain, &w_uri);
    if (w_uri.s == NULL) {
	LM_ERR("while creating uri\n");
	return NULL;
    }
    ruleset_node = xmlDocGetNodeByName(xcap_tree, "ruleset", NULL);
    if (ruleset_node == NULL) {
	LM_DBG("ruleset_node NULL\n");
	goto error;
    }	
    for (node1 = ruleset_node->children; node1; node1 = node1->next) {
	if (xmlStrcasecmp(node1->name, (unsigned char*)"text") == 0)
	    continue;

	/* process conditions */
	LM_DBG("node1->name= %s\n", node1->name);
	
	cond_node = xmlNodeGetChildByName(node1, "conditions");
	if(cond_node == NULL) {
	    LM_DBG("cond node NULL\n");
	    goto error;
	}
	LM_DBG("cond_node->name= %s\n", cond_node->name);

	validity_node = xmlNodeGetChildByName(cond_node, "validity");
	if (validity_node != NULL) {
	    LM_DBG("found validity tag\n");
	    
	    t= time(NULL);
		
	    /* search all from-until pair */
	    for (time_node = validity_node->children; time_node;
		time_node = time_node->next) {
		if (xmlStrcasecmp(time_node->name, (unsigned char*)"from")!= 0)
		    continue;

		time_cont= (char*)xmlNodeGetContent(time_node);
		t_init= xml_parse_dateTime(time_cont);
		xmlFree(time_cont);
		if (t_init< 0) {
		    LM_ERR("failed to parse xml dateTime\n");
		    goto error;
		}

		if (t< t_init) {
		    LM_DBG("the lower time limit is not respected\n");
		    continue;
		}
				
		time_node= time_node->next;
		while (1) {
		    if (time_node == NULL) {
			LM_ERR("bad formatted xml doc:until child not found in"
			       " validity pair\n");
			goto error;
		    }
		    if( xmlStrcasecmp(time_node->name, 
				      (unsigned char*)"until")== 0)
			break;
		    time_node= time_node->next;
		}
				
		time_cont = (char*)xmlNodeGetContent(time_node);
		t_fin= xml_parse_dateTime(time_cont);
		xmlFree(time_cont);
		
		if (t_fin< 0) {
		    LM_ERR("failed to parse xml dateTime\n");
		    goto error;
		}
			
		if (t <= t_fin) {
		    LM_DBG("the rule is active at this time\n");
		    valid= 1;
		}
			
	    }
		
	    if (!valid) {
		LM_DBG("the rule is not active at this time\n");
		continue;
	    }
	    
	}	

	identity_node = xmlNodeGetChildByName(cond_node, "identity");
	if (identity_node == NULL) {
	    LM_ERR("didn't find identity tag\n");
	    goto error;
	}	
		
	iden_child = xmlNodeGetChildByName(identity_node, "one");
	if(iden_child) {
	    for (node2 = identity_node->children; node2; node2 = node2->next) {
		if(xmlStrcasecmp(node2->name, (unsigned char*)"one")!= 0)
		    continue;
				
		id = xmlNodeGetAttrContentByName(node2, "id");	
		if(id== NULL) {
		    LM_ERR("while extracting attribute\n");
		    goto error;
		}
		if ((strlen(id)== w_uri.len && 
		     (strncmp(id, w_uri.s, w_uri.len)==0))) {
		    apply_rule = 1;
		    xmlFree(id);
		    break;
		}
		xmlFree(id);
	    }
	}	

	/* search for many node*/
	iden_child = xmlNodeGetChildByName(identity_node, "many");
	if (iden_child)	{
	    domain = NULL;
	    for (node2 = identity_node->children; node2; node2 = node2->next) {
		if (xmlStrcasecmp(node2->name, (unsigned char*)"many") != 0)
		    continue;
	
		domain = xmlNodeGetAttrContentByName(node2, "domain");
		if(domain == NULL) {
		    LM_DBG("No domain attribute to many\n");
		} else	{
		    LM_DBG("<many domain= %s>\n", domain);
		    if((strlen(domain)!= subs->from_domain.len && 
			strncmp(domain, subs->from_domain.s,
				subs->from_domain.len) )) {
			xmlFree(domain);
			continue;
		    }	
		}
		xmlFree(domain);
		apply_rule = 1;
		if (node2->children == NULL)       /* there is no exception */
		    break;

		for (except_node = node2->children; except_node;
		     except_node= except_node->next) {
		    if(xmlStrcasecmp(except_node->name,
				     (unsigned char*)"except"))
			continue;

		    id = xmlNodeGetAttrContentByName(except_node, "id");	
		    if (id != NULL) {
			if((strlen(id)- 1== w_uri.len && 
			    (strncmp(id, w_uri.s, w_uri.len)==0))) {
			    xmlFree(id);
			    apply_rule = 0;
			    break;
			}
			xmlFree(id);
		    } else {
			domain = NULL;
			domain = xmlNodeGetAttrContentByName(except_node,
							     "domain");
			if(domain!=NULL) {
			    LM_DBG("Found except domain= %s\n- strlen(domain)= %d\n",
				   domain, (int)strlen(domain));
			    if (strlen(domain)==subs->from_domain.len &&
				(strncmp(domain,subs->from_domain.s ,
					 subs->from_domain.len)==0)) {
				LM_DBG("except domain match\n");
				xmlFree(domain);
				apply_rule = 0;
				break;
			    }
			    xmlFree(domain);
			}	
		    }	
		}
		if (apply_rule == 1)  /* if a match was found no need to keep searching*/
		    break;
	    }
	}
	if (apply_rule ==1)
	    break;
    }

    LM_DBG("apply_rule= %d\n", apply_rule);
    if(w_uri.s!=NULL)
	pkg_free(w_uri.s);

    if( !apply_rule || !node1)
	return NULL;
    
    return node1;

 error:
    if(w_uri.s)
	pkg_free(w_uri.s);
    return NULL;
}
Пример #2
0
static void ietf_get_rules(subs_t* subs, xmlDocPtr xcap_tree, xcap_rule_t **rules)
{
	str w_uri= {0, 0};
	char* id = NULL, *domain = NULL, *time_cont= NULL;
	int apply_rule = -1;
	xmlNodePtr ruleset_node = NULL, node1= NULL, node2= NULL;
	xmlNodePtr cond_node = NULL, except_node = NULL;
	xmlNodePtr identity_node = NULL, sphere_node = NULL;
	xmlNodePtr iden_child;
	xmlNodePtr validity_node, time_node;
	time_t t_init, t_fin, t;
	int valid= 0;
	xcap_rule_t *rule = NULL;

        *rules = NULL;

	uandd_to_uri(subs->from_user, subs->from_domain, &w_uri);
	if(w_uri.s == NULL)
	{
		LM_ERR("while creating uri\n");
		return;
	}
	ruleset_node = xmlDocGetNodeByName(xcap_tree, "ruleset", NULL);
	if(ruleset_node == NULL)
	{
		LM_DBG("ruleset_node NULL\n");
		goto error;

	}
	for(node1 = ruleset_node->children ; node1; node1 = node1->next)
	{
		if(xmlStrcasecmp(node1->name, (unsigned char*)"text")==0 )
				continue;

		/* process conditions */
		LM_DBG("node1->name= %s\n", node1->name);

		cond_node = xmlNodeGetChildByName(node1, "conditions");
		if(cond_node == NULL)
		{
			LM_DBG("cond node NULL\n");
			goto error;
		}
		LM_DBG("cond_node->name= %s\n", cond_node->name);

		validity_node = xmlNodeGetChildByName(cond_node, "validity");
		if(validity_node !=NULL)
		{
			LM_DBG("found validity tag\n");

			t= time(NULL);

			/* search all from-until pair */
			for(time_node= validity_node->children; time_node;
					time_node= time_node->next)
			{
				if(xmlStrcasecmp(time_node->name, (unsigned char*)"from")!= 0)
				{
					continue;
				}
				time_cont= (char*)xmlNodeGetContent(time_node);
				t_init= xml_parse_dateTime(time_cont);
				xmlFree(time_cont);
				if(t_init< 0)
				{
					LM_ERR("failed to parse xml dateTime\n");
					goto error;
				}

				if(t< t_init)
				{
					LM_DBG("the lower time limit is not respected\n");
					continue;
				}

				time_node= time_node->next;
				while(1)
				{
					if(time_node== NULL)
					{
						LM_ERR("bad formatted xml doc:until child not found in"
								" validity pair\n");
						goto error;
					}
					if( xmlStrcasecmp(time_node->name,
								(unsigned char*)"until")== 0)
						break;
					time_node= time_node->next;
				}

				time_cont= (char*)xmlNodeGetContent(time_node);
				t_fin= xml_parse_dateTime(time_cont);
				xmlFree(time_cont);

				if(t_fin< 0)
				{
					LM_ERR("failed to parse xml dateTime\n");
					goto error;
				}

				if(t <= t_fin)
				{
					LM_DBG("the rule is active at this time\n");
					valid= 1;
				}

			}

			if(!valid)
			{
				LM_DBG("the rule is not active at this time\n");
				continue;
			}

		}

		sphere_node = xmlNodeGetChildByName(cond_node, "sphere");
		if(sphere_node!= NULL)
		{
			/* check to see if matches presentity current sphere */
			/* ask presence for sphere information */

			char* sphere= pres_get_sphere(&subs->pres_uri);
			if(sphere)
			{
				char* attr= (char*)xmlNodeGetContent(sphere_node);
				if(xmlStrcasecmp((unsigned char*)attr, (unsigned char*)sphere)!= 0)
				{
					LM_DBG("sphere condition not respected\n");
					pkg_free(sphere);
					xmlFree(attr);
					continue;
				}
				pkg_free(sphere);
				xmlFree(attr);

			}
			else
			{
				LM_DBG("Noo sphere definition found\n");
				continue;
			}
			/* if the user has not define a sphere
			 *						consider the condition false*/
		}

		identity_node = xmlNodeGetChildByName(cond_node, "identity");
		if(identity_node == NULL)
		{
			LM_ERR("didn't find identity tag\n");
			goto error;
		}

		iden_child= xmlNodeGetChildByName(identity_node, "one");
		if(iden_child)
		{
			for(node2 = identity_node->children; node2; node2 = node2->next)
			{
				if(xmlStrcasecmp(node2->name, (unsigned char*)"one")!= 0)
					continue;

				id = xmlNodeGetAttrContentByName(node2, "id");
				if(id== NULL)
				{
					LM_ERR("while extracting attribute\n");
					goto error;
				}
				if((strlen(id)== w_uri.len &&
							(strncmp(id, w_uri.s, w_uri.len)==0)))
				{
					apply_rule = 1;
					xmlFree(id);
					break;
				}
				xmlFree(id);
			}
		}

		/* search for many node*/
		iden_child= xmlNodeGetChildByName(identity_node, "many");
		if(iden_child)
		{
			domain = NULL;
			for(node2 = identity_node->children; node2; node2 = node2->next)
			{
				if(xmlStrcasecmp(node2->name, (unsigned char*)"many")!= 0)
					continue;

				domain = xmlNodeGetAttrContentByName(node2, "domain");
				if(domain == NULL)
				{
					LM_DBG("No domain attribute to many\n");
				}
				else
				{
					LM_DBG("<many domain= %s>\n", domain);
					if((strlen(domain)!= subs->from_domain.len &&
								strncmp(domain, subs->from_domain.s,
									subs->from_domain.len) ))
					{
						xmlFree(domain);
						continue;
					}
				}
				xmlFree(domain);
				apply_rule = 1;
				if(node2->children == NULL)       /* there is no exception */
					break;

				for(except_node = node2->children; except_node;
						except_node= except_node->next)
				{
					if(xmlStrcasecmp(except_node->name, (unsigned char*)"except"))
						continue;

					id = xmlNodeGetAttrContentByName(except_node, "id");
					if(id!=NULL)
					{
						if((strlen(id)- 1== w_uri.len &&
								(strncmp(id, w_uri.s, w_uri.len)==0)))
						{
							xmlFree(id);
							apply_rule = 0;
							break;
						}
						xmlFree(id);
					}
					else
					{
						domain = NULL;
						domain = xmlNodeGetAttrContentByName(except_node, "domain");
						if(domain!=NULL)
						{
							LM_DBG("Found except domain= %s\n- strlen(domain)= %d\n",
									domain, (int)strlen(domain));
							if(strlen(domain)==subs->from_domain.len &&
								(strncmp(domain,subs->from_domain.s , subs->from_domain.len)==0))
							{
								LM_DBG("except domain match\n");
								xmlFree(domain);
								apply_rule = 0;
								break;
							}
							xmlFree(domain);
						}

					}
				}
				if(apply_rule== 1)  /* if a match was found no need to keep searching*/
					break;

			}
		}
		if(apply_rule ==1)
			break;
	}

	LM_DBG("apply_rule= %d\n", apply_rule);
	if(w_uri.s!=NULL)
		pkg_free(w_uri.s);

	if( !apply_rule || !node1)
		return;

	rule = (xcap_rule_t *)pkg_malloc(sizeof(*rule));
	if (rule == NULL)
	{
		LM_ERR("cannot allocate pkg_mem\n");
		return;
	}

	/* TODO: in IETF mode only the first matching rule is returned */
	rule->node = node1;
	rule->next = NULL;
	*rules = rule;

	return;
error:
	if(w_uri.s)
		pkg_free(w_uri.s);
}