File: re_group.c

package info (click to toggle)
kamailio 4.2.0-2%2Bdeb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 56,276 kB
  • sloc: ansic: 552,836; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (222 lines) | stat: -rw-r--r-- 4,929 bytes parent folder | download | duplicates (2)
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
/*
 * $Id$ 
 *
 * Copyright (C) 2005-2007 Voice Sistem SRL
 *
 * This file is part of Kamailio, a free SIP server.
 *
 * 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
 *
 * History:
 * --------
 *  2005-10-06 - created by bogdan
 */

/**
 * \file
 * \brief Group membership module
 * \ingroup group
 * - Module: \ref group
 */

#include <sys/types.h>
#include <regex.h>

#include "../../str.h"
#include "../../mem/mem.h"
#include "../../route_struct.h"
#include "../../lvalue.h"
#include "group_mod.h"
#include "re_group.h"
#include "group.h"

/*! regular expression for groups */
struct re_grp {
	regex_t       re;
	int_str       gid;
	struct re_grp *next;
};

/*! global regexp list */
static struct re_grp *re_list = 0;


/*!
 * \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
 */
static int add_re(const char *re, int gid)
{
	struct re_grp *rg;

	LM_DBG("adding <%s> with %d\n",re, gid);

	rg = (struct re_grp*)pkg_malloc(sizeof(struct re_grp));
	if (rg==0) {
		LM_ERR("no more pkg mem\n");
		goto error;
	}
	memset( rg, 0, sizeof(struct re_grp));

	if (regcomp(&rg->re, re, REG_EXTENDED|REG_ICASE|REG_NEWLINE) ) {
		LM_ERR("bad re %s\n", re);
		pkg_free(rg);
		goto error;
	}

	rg->gid.n = gid;

	rg->next = re_list;
	re_list = rg;

	return 0;
error:
	return -1;
}


/*!
 * \brief Load regular expression rules from a database
 * \param table DB table
 * \return 0 on success, -1 on failure
 */
int load_re( str *table )
{
	db_key_t cols[2];
	db1_res_t* res = NULL;
	db_row_t* row;
	int n;

	cols[0] = &re_exp_column;
	cols[1] = &re_gid_column;

	if (group_dbf.use_table(group_dbh, table) < 0) {
		LM_ERR("failed to set table <%s>\n", table->s);
		goto error;
	}

	if (group_dbf.query(group_dbh, 0, 0, 0, cols, 0, 2, 0, &res) < 0) {
		LM_ERR("failed to query database\n");
		goto error;
	}

	for( n=0 ; n<RES_ROW_N(res) ; n++) {
		row = &res->rows[n];
		/* validate row */
		if (row->values[0].nul || row->values[0].type!=DB1_STRING) {
			LM_ERR("empty or non-string "
				"value for <%s>(re) column\n",re_exp_column.s);
			goto error1;
		}
		if (row->values[1].nul || row->values[1].type!=DB1_INT) {
			LM_ERR("empty or non-integer "
				"value for <%s>(gid) column\n",re_gid_column.s);
			goto error1;
		}

		if ( add_re( row->values[0].val.string_val,
		row->values[1].val.int_val)!=0 ) {
			LM_ERR("failed to add row\n");
			goto error1;
		}
	}
	LM_DBG("%d rules were loaded\n", n);

	group_dbf.free_result(group_dbh, res);
	return 0;
error1:
	group_dbf.free_result(group_dbh, res);
error:
	return -1;
}


/*!
 * \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
 */
int get_user_group(struct sip_msg *req, char *user, char *avp)
{
	static char uri_buf[MAX_URI_SIZE];
	str  username;
	str  domain;
	pv_spec_t *pvs;
	pv_value_t val;
	struct re_grp *rg;
	regmatch_t pmatch;
	char *c;
	int n;
	int* pi;

	if (get_username_domain( req, (group_check_p)user, &username, &domain)!=0){
		LM_ERR("failed to get username@domain\n");
		goto error;
	}

	if (username.s==NULL || username.len==0 ) {
		LM_DBG("no username part\n");
		return -1;
	}

	if ( 4 + username.len + 1 + domain.len + 1 > MAX_URI_SIZE ) {
		LM_ERR("URI to large!!\n");
		goto error;
	}

	pi=(int*)uri_buf;
	*pi = htonl(('s'<<24) + ('i'<<16) + ('p'<<8) + ':');
	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;

	LM_DBG("getting groups for <%s>\n",uri_buf);
	pvs = (pv_spec_t*)avp;
	memset(&val, 0, sizeof(pv_value_t));
	val.flags = PV_VAL_INT|PV_TYPE_INT;

	/* 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) {
			LM_DBG("user matched to group %d!\n", rg->gid.n);

			/* match -> add the gid as AVP */
			val.ri = rg->gid.n;
			if(pvs->setf(req, &pvs->pvp, (int)EQ_T, &val)<0)
			{
				LM_ERR("setting PV AVP failed\n");
				goto error;
			}
			n++;
			/* continue? */
			if (multiple_gid==0)
				break;
		}
	}

	return n?n:-1;
error:
	return -1;
}