modules/group/re_group.c
43d3bb92
 /*
  * Copyright (C) 2005-2007 Voice Sistem SRL
  *
27642a08
  * This file is part of Kamailio, a free SIP server.
43d3bb92
  *
27642a08
  * Kamailio is free software; you can redistribute it and/or modify
43d3bb92
  * 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,
43d3bb92
  * 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
43d3bb92
  *
  */
 
9bcfe5d3
 /**
  * \file
  * \brief Group membership module
  * \ingroup group
  * - Module: \ref group
  */
 
dd87d152
 #include <sys/types.h>
43d3bb92
 #include <regex.h>
 
 #include "../../str.h"
 #include "../../mem/mem.h"
6505c9d3
 #include "../../route_struct.h"
30ba8f90
 #include "../../lvalue.h"
43d3bb92
 #include "group_mod.h"
 #include "re_group.h"
 #include "group.h"
 
9bcfe5d3
 /*! regular expression for groups */
43d3bb92
 struct re_grp {
 	regex_t       re;
c65d4d0e
 	int_str       gid;
43d3bb92
 	struct re_grp *next;
 };
 
9bcfe5d3
 /*! global regexp list */
43d3bb92
 static struct re_grp *re_list = 0;
 
 
9bcfe5d3
 /*!
  * \brief Create a group regexp and add it to the list
  * \param re regular expression string
  * \param gid group ID
  * \return 0 on success, -1 on failure
  */
6f675953
 static int add_re(const char *re, int gid)
43d3bb92
 {
 	struct re_grp *rg;
 
789504a6
 	LM_DBG("adding <%s> with %d\n",re, gid);
43d3bb92
 
 	rg = (struct re_grp*)pkg_malloc(sizeof(struct re_grp));
 	if (rg==0) {
789504a6
 		LM_ERR("no more pkg mem\n");
43d3bb92
 		goto error;
 	}
 	memset( rg, 0, sizeof(struct re_grp));
 
 	if (regcomp(&rg->re, re, REG_EXTENDED|REG_ICASE|REG_NEWLINE) ) {
789504a6
 		LM_ERR("bad re %s\n", re);
43d3bb92
 		pkg_free(rg);
 		goto error;
 	}
 
c65d4d0e
 	rg->gid.n = gid;
43d3bb92
 
 	rg->next = re_list;
 	re_list = rg;
 
 	return 0;
 error:
 	return -1;
 }
 
 
9bcfe5d3
 /*!
  * \brief Load regular expression rules from a database
  * \param table DB table
  * \return 0 on success, -1 on failure
  */
43d3bb92
 int load_re( str *table )
 {
 	db_key_t cols[2];
de95faa4
 	db1_res_t* res = NULL;
43d3bb92
 	db_row_t* row;
 	int n;
 
e2cf6343
 	cols[0] = &re_exp_column;
 	cols[1] = &re_gid_column;
43d3bb92
 
e2cf6343
 	if (group_dbf.use_table(group_dbh, table) < 0) {
789504a6
 		LM_ERR("failed to set table <%s>\n", table->s);
43d3bb92
 		goto error;
 	}
 
 	if (group_dbf.query(group_dbh, 0, 0, 0, cols, 0, 2, 0, &res) < 0) {
789504a6
 		LM_ERR("failed to query database\n");
43d3bb92
 		goto error;
 	}
 
 	for( n=0 ; n<RES_ROW_N(res) ; n++) {
 		row = &res->rows[n];
 		/* validate row */
de95faa4
 		if (row->values[0].nul || row->values[0].type!=DB1_STRING) {
789504a6
 			LM_ERR("empty or non-string "
43d3bb92
 				"value for <%s>(re) column\n",re_exp_column.s);
 			goto error1;
 		}
de95faa4
 		if (row->values[1].nul || row->values[1].type!=DB1_INT) {
789504a6
 			LM_ERR("empty or non-integer "
43d3bb92
 				"value for <%s>(gid) column\n",re_gid_column.s);
 			goto error1;
 		}
 
6f675953
 		if ( add_re( row->values[0].val.string_val,
43d3bb92
 		row->values[1].val.int_val)!=0 ) {
789504a6
 			LM_ERR("failed to add row\n");
43d3bb92
 			goto error1;
 		}
 	}
789504a6
 	LM_DBG("%d rules were loaded\n", n);
43d3bb92
 
 	group_dbf.free_result(group_dbh, res);
 	return 0;
 error1:
 	group_dbf.free_result(group_dbh, res);
 error:
 	return -1;
 }
 
 
9bcfe5d3
 /*!
  * \brief Get the user group and compare to the regexp list
  * \param req SIP message
  * \param user user string
  * \param avp AVP value
  * \return number of all matches (positive), -1 on errors or when not found
  */
43d3bb92
 int get_user_group(struct sip_msg *req, char *user, char *avp)
 {
 	static char uri_buf[MAX_URI_SIZE];
 	str  username;
 	str  domain;
6505c9d3
 	pv_spec_t *pvs;
 	pv_value_t val;
43d3bb92
 	struct re_grp *rg;
 	regmatch_t pmatch;
 	char *c;
 	int n;
221e8785
 	int* pi;
43d3bb92
 
 	if (get_username_domain( req, (group_check_p)user, &username, &domain)!=0){
789504a6
 		LM_ERR("failed to get username@domain\n");
43d3bb92
 		goto error;
 	}
 
52edebfc
 	if (username.s==NULL || username.len==0 ) {
 		LM_DBG("no username part\n");
 		return -1;
 	}
 
43d3bb92
 	if ( 4 + username.len + 1 + domain.len + 1 > MAX_URI_SIZE ) {
789504a6
 		LM_ERR("URI to large!!\n");
43d3bb92
 		goto error;
 	}
 
221e8785
 	pi=(int*)uri_buf;
 	*pi = htonl(('s'<<24) + ('i'<<16) + ('p'<<8) + ':');
43d3bb92
 	c = uri_buf + 4;
 	memcpy( c, username.s, username.len);
 	c += username.len;
 	*(c++) = '@';
 	memcpy( c, domain.s, domain.len);
 	c += domain.len;
 	*c = 0;
 
789504a6
 	LM_DBG("getting groups for <%s>\n",uri_buf);
6505c9d3
 	pvs = (pv_spec_t*)avp;
 	memset(&val, 0, sizeof(pv_value_t));
 	val.flags = PV_VAL_INT|PV_TYPE_INT;
43d3bb92
 
 	/* check against all re groups */
 	for( rg=re_list,n=0 ; rg ; rg=rg->next ) {
 		if (regexec( &rg->re, uri_buf, 1, &pmatch, 0)==0) {
789504a6
 			LM_DBG("user matched to group %d!\n", rg->gid.n);
6505c9d3
 
43d3bb92
 			/* match -> add the gid as AVP */
6505c9d3
 			val.ri = rg->gid.n;
 			if(pvs->setf(req, &pvs->pvp, (int)EQ_T, &val)<0)
 			{
 				LM_ERR("setting PV AVP failed\n");
43d3bb92
 				goto error;
 			}
 			n++;
 			/* continue? */
 			if (multiple_gid==0)
 				break;
 		}
 	}
 
 	return n?n:-1;
 error:
 	return -1;
 }