File: authdb_mod.c

package info (click to toggle)
kamailio 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 56,100 kB
  • sloc: ansic: 552,832; 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 (434 lines) | stat: -rw-r--r-- 10,841 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* 
 * $Id$
 *
 * Digest Authentication Module
 *
 * Copyright (C) 2001-2003 FhG Fokus
 *
 * 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:
 * --------
 * 2003-02-26: checks and group moved to separate modules (janakj)
 * 2003-03-11: New module interface (janakj)
 * 2003-03-16: flags export parameter added (janakj)
 * 2003-03-19  all mallocs/frees replaced w/ pkg_malloc/pkg_free (andrei)
 * 2003-04-05: default_uri #define used (jiri)
 * 2004-06-06  cleanup: static & auth_db_{init,bind,close.ver} used (andrei)
 * 2005-05-31  general definition of AVPs in credentials now accepted - ID AVP,
 *             STRING AVP, AVP aliases (bogdan)
 * 2006-03-01 pseudo variables support for domain name (bogdan)
 */

#include <stdio.h>
#include <string.h>
#include "../../sr_module.h"
#include "../../lib/srdb1/db.h"
#include "../../dprint.h"
#include "../../error.h"
#include "../../mod_fix.h"
#include "../../trim.h"
#include "../../mem/mem.h"
#include "../../parser/parse_uri.h"
#include "../../modules/auth/api.h"
#include "authorize.h"

MODULE_VERSION

#define TABLE_VERSION 6

/*
 * Module destroy function prototype
 */
static void destroy(void);


/*
 * Module child-init function prototype
 */
static int child_init(int rank);


/*
 * Module initialization function prototype
 */
static int mod_init(void);


static int w_is_subscriber(sip_msg_t *msg, char *_uri, char* _table,
		char *_flags);
static int auth_fixup(void** param, int param_no);
static int auth_check_fixup(void** param, int param_no);
int parse_aaa_pvs(char *definition, pv_elem_t **pv_def, int *cnt);

#define USER_COL "username"
#define DOMAIN_COL "domain"
#define PASS_COL "ha1"
#define PASS_COL_2 "ha1b"

/*
 * Module parameter variables
 */
static str db_url           = str_init(DEFAULT_RODB_URL);
str user_column             = str_init(USER_COL);
str domain_column           = str_init(DOMAIN_COL);
str pass_column             = str_init(PASS_COL);
str pass_column_2           = str_init(PASS_COL_2);

static int version_table_check = 1;

int calc_ha1                = 0;
int use_domain              = 0; /* Use also domain when looking up in table */

db1_con_t* auth_db_handle    = 0; /* database connection handle */
db_func_t auth_dbf;
auth_api_s_t auth_api;

char *credentials_list      = 0;
pv_elem_t *credentials      = 0; /* Parsed list of credentials to load */
int credentials_n           = 0; /* Number of credentials in the list */

/*
 * Exported functions
 */
static cmd_export_t cmds[] = {
	{"www_authorize",      (cmd_function)www_authenticate,   2, auth_fixup, 0,
		REQUEST_ROUTE},
	{"www_authenticate",   (cmd_function)www_authenticate,   2, auth_fixup, 0,
		REQUEST_ROUTE},
	{"www_authenticate",   (cmd_function)www_authenticate2,  3, auth_fixup, 0,
REQUEST_ROUTE},
	{"proxy_authorize",    (cmd_function)proxy_authenticate, 2, auth_fixup, 0,
		REQUEST_ROUTE},
	{"proxy_authenticate", (cmd_function)proxy_authenticate, 2, auth_fixup, 0,
		REQUEST_ROUTE},
	{"auth_check",         (cmd_function)auth_check,         3, auth_check_fixup, 0,
		REQUEST_ROUTE},
	{"is_subscriber",      (cmd_function)w_is_subscriber,    3, auth_check_fixup, 0,
		ANY_ROUTE},
	{"bind_auth_db",       (cmd_function)bind_auth_db,       0, 0, 0,
		0},
	{0, 0, 0, 0, 0, 0}
};


/*
 * Exported parameters
 */
static param_export_t params[] = {
	{"db_url",            PARAM_STR, &db_url            },
	{"user_column",       PARAM_STR, &user_column       },
	{"domain_column",     PARAM_STR, &domain_column     },
	{"password_column",   PARAM_STR, &pass_column       },
	{"password_column_2", PARAM_STR, &pass_column_2     },
	{"calculate_ha1",     INT_PARAM, &calc_ha1            },
	{"use_domain",        INT_PARAM, &use_domain          },
	{"load_credentials",  PARAM_STRING, &credentials_list    },
	{"version_table",     INT_PARAM, &version_table_check },
	{0, 0, 0}
};


/*
 * Module interface
 */
struct module_exports exports = {
	"auth_db", 
	DEFAULT_DLFLAGS, /* dlopen flags */
	cmds,       /* Exported functions */
	params,     /* Exported parameters */
	0,          /* exported statistics */
	0,          /* exported MI functions */
	0,          /* exported pseudo-variables */
	0,          /* extra processes */
	mod_init,   /* module initialization function */
	0,          /* response function */
	destroy,    /* destroy function */
	child_init  /* child initialization function */
};


static int child_init(int rank)
{
	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
		return 0; /* do nothing for the main process */
	
	auth_db_handle = auth_dbf.init(&db_url);
	if (auth_db_handle == 0){
		LM_ERR("unable to connect to the database\n");
		return -1;
	}

	return 0;
}


static int mod_init(void)
{
	bind_auth_s_t bind_auth;

	/* Find a database module */
	if (db_bind_mod(&db_url, &auth_dbf) < 0){
		LM_ERR("unable to bind to a database driver\n");
		return -1;
	}

	/* bind to auth module and import the API */
	bind_auth = (bind_auth_s_t)find_export("bind_auth_s", 0, 0);
	if (!bind_auth) {
		LM_ERR("unable to find bind_auth function. Check if you load"
				" the auth module.\n");
		return -2;
	}

	if (bind_auth(&auth_api) < 0) {
		LM_ERR("unable to bind auth module\n");
		return -3;
	}

	/* process additional list of credentials */
	if (parse_aaa_pvs(credentials_list, &credentials, &credentials_n) != 0) {
		LM_ERR("failed to parse credentials\n");
		return -5;
	}

	return 0;
}


static void destroy(void)
{
	if (auth_db_handle) {
		auth_dbf.close(auth_db_handle);
		auth_db_handle = 0;
	}
	if (credentials) {
		pv_elem_free_all(credentials);
		credentials = 0;
		credentials_n = 0;
	}
}


/**
 * check if the subscriber identified by _uri has a valid record in
 * database table _table
 */
static int w_is_subscriber(sip_msg_t *msg, char *_uri, char* _table,
		char *_flags)
{
	str suri;
	str stable;
	int iflags;
	int ret;
	sip_uri_t puri;

	if(msg==NULL || _uri==NULL || _table==NULL || _flags==NULL) {
		LM_ERR("invalid parameters\n");
		return AUTH_ERROR;
	}

	if (get_str_fparam(&suri, msg, (fparam_t*)_uri) < 0) {
		LM_ERR("failed to get uri value\n");
		return -1;
	}

	if (suri.len==0) {
		LM_ERR("invalid uri parameter - empty value\n");
		return -1;
	}
	if(parse_uri(suri.s, suri.len, &puri)<0){
		LM_ERR("invalid uri parameter format\n");
		return -1;
	}

	if (get_str_fparam(&stable, msg, (fparam_t*)_table) < 0) {
		LM_ERR("failed to get table value\n");
		return -1;
	}

	if (stable.len==0) {
		LM_ERR("invalid table parameter - empty value\n");
		return -1;
	}

	if(fixup_get_ivalue(msg, (gparam_p)_flags, &iflags)!=0)
	{
		LM_ERR("invalid flags parameter\n");
		return -1;
	}

	LM_DBG("uri [%.*s] table [%.*s] flags [%d]\n", suri.len, suri.s,
			stable.len,  stable.s, iflags);
	ret = fetch_credentials(msg, &puri.user, (iflags==1)?&puri.host:NULL,
			&stable);

	if(ret>=0)
		return 1;
	return ret;
}

/*
 * Convert the char* parameters
 */
static int auth_fixup(void** param, int param_no)
{
	db1_con_t* dbh = NULL;
	str name;

	if(strlen((char*)*param)<=0) {
		LM_ERR("empty parameter %d not allowed\n", param_no);
		return -1;
	}

	if (param_no == 1 || param_no == 3) {
		return fixup_var_str_12(param, 1);
	} else if (param_no == 2) {
		name.s = (char*)*param;
		name.len = strlen(name.s);

		dbh = auth_dbf.init(&db_url);
		if (!dbh) {
			LM_ERR("unable to open database connection\n");
			return -1;
		}
		if(version_table_check!=0
				&& db_check_table_version(&auth_dbf, dbh, &name,
					TABLE_VERSION) < 0) {
			LM_ERR("error during table version check.\n");
			auth_dbf.close(dbh);
			return -1;
		}
		auth_dbf.close(dbh);
	}
	return 0;
}

/*
 * Convert cfg parameters to run-time structures
 */
static int auth_check_fixup(void** param, int param_no)
{
	if(strlen((char*)*param)<=0) {
		LM_ERR("empty parameter %d not allowed\n", param_no);
		return -1;
	}
	if (param_no == 1) {
		return fixup_var_str_12(param, 1);
	}
	if (param_no == 2) {
		return fixup_var_str_12(param, 2);
	}
	if (param_no == 3) {
		return fixup_igp_null(param, 1);
	}
	return 0;
}

/*
 * Parse extra credentials list
 */
int parse_aaa_pvs(char *definition, pv_elem_t **pv_def, int *cnt)
{
	pv_elem_t *pve;
	str pv;
	char *p;
	char *end;
	char *sep;

	p = definition;
	if (p==0 || *p==0)
		return 0;

	*pv_def = 0;
	*cnt = 0;

	/* get element by element */
	while ( (end=strchr(p,';'))!=0 || (end=p+strlen(p))!=p ) {
		/* new pv_elem_t */
		if ( (pve=(pv_elem_t*)pkg_malloc(sizeof(pv_elem_t)))==0 ) {
			LM_ERR("no more pkg mem\n");
			goto error;
		}
		memset( pve, 0, sizeof(pv_elem_t));

		/* definition is between p and e */
		/* search backwards because PV definition may contain '=' characters */
		for (sep = end; sep >= p && *sep != '='; sep--); 
		if (sep > p) {
			/* pv=column style */
			/* set column name */
			pve->text.s = sep + 1;
			pve->text.len = end - pve->text.s;
			trim(&pve->text);
			if (pve->text.len == 0)
				goto parse_error;
			/* set pv spec */
			pv.s = p;
			pv.len = sep - p;
			trim(&pv);
			if (pv.len == 0)
				goto parse_error;
		} else {
			/* no pv, only column name */
			pve->text.s = p;
			pve->text.len = end - pve->text.s;
			trim(&pve->text);
			if (pve->text.len == 0)
				goto parse_error;
			/* create an avp definition for the spec parser */
			pv.s = (char*)pkg_malloc(pve->text.len + 7);
			if (pv.s == NULL) {
				LM_ERR("no more pkg mem\n");
				goto parse_error;
			}
			pv.len = snprintf(pv.s, pve->text.len + 7, "$avp(%.*s)",
			                  pve->text.len, pve->text.s);
		}

		/* create a pv spec */
		LM_DBG("column: %.*s  pv: %.*s\n", pve->text.len, pve->text.s, pv.len, pv.s);
		pve->spec = pv_spec_lookup(&pv, NULL);
		if(pve->spec==NULL || pve->spec->setf == NULL) {
			LM_ERR("PV is not writeable: %.*s\n", pv.len, pv.s);
			goto parse_error;
		}

		/* link the element */
		pve->next = *pv_def;
		*pv_def = pve;
		(*cnt)++;
		pve = 0;
		/* go to the end */
		p = end;
		if (*p==';')
			p++;
		if (*p==0)
			break;
	}

	return 0;
parse_error:
	LM_ERR("parse failed in \"%s\" at pos %d(%s)\n",
		definition, (int)(long)(p-definition),p);
error:
	pkg_free( pve );
	pv_elem_free_all( *pv_def );
	*pv_def = 0;
	*cnt = 0;
	return -1;
}