Skip to content

Commit

Permalink
group: port script functions to new param interface
Browse files Browse the repository at this point in the history
  • Loading branch information
rvlad-patrascu authored and liviuchircu committed Apr 4, 2019
1 parent 36459a4 commit 863f93b
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 119 deletions.
29 changes: 7 additions & 22 deletions modules/group/group.c
Expand Up @@ -116,7 +116,7 @@ int get_username_domain(struct sip_msg *msg, str *hf_s,
/*
* Check if username in specified header field is in a table
*/
int db_is_user_in(struct sip_msg* _msg, char* _hf, char* _grp)
int db_is_user_in(struct sip_msg* _msg, str* hf_s, str* grp_s)
{
static db_ps_t my_ps = NULL;
db_key_t keys[3];
Expand All @@ -129,20 +129,7 @@ int db_is_user_in(struct sip_msg* _msg, char* _hf, char* _grp)
keys[2] = &domain_column;
col[0] = &group_column;

str hf_s = { NULL, 0 };
str grp_s = { NULL, 0 };

if(_hf == NULL || fixup_get_svalue(_msg, (gparam_p)_hf, &hf_s) != 0) {
LM_ERR("Invalid parameter URI\n");
return -1;
}

if(_grp == NULL || fixup_get_svalue(_msg, (gparam_p)_grp, &grp_s) != 0) {
LM_ERR("Invalid parameter grp\n");
return -1;
}

if ( get_username_domain( _msg, &hf_s, &(VAL_STR(vals)),
if ( get_username_domain( _msg, hf_s, &(VAL_STR(vals)),
&(VAL_STR(vals+2)))!=0) {
LM_ERR("failed to get username@domain\n");
return -1;
Expand All @@ -156,7 +143,7 @@ int db_is_user_in(struct sip_msg* _msg, char* _hf, char* _grp)
VAL_TYPE(vals) = VAL_TYPE(vals + 1) = VAL_TYPE(vals + 2) = DB_STR;
VAL_NULL(vals) = VAL_NULL(vals + 1) = VAL_NULL(vals + 2) = 0;

VAL_STR(vals + 1) = *(&grp_s);
VAL_STR(vals + 1) = *grp_s;

group_dbf.use_table(group_dbh, &table);
CON_PS_REFERENCE(group_dbh) = &my_ps;
Expand All @@ -169,12 +156,12 @@ int db_is_user_in(struct sip_msg* _msg, char* _hf, char* _grp)

if (RES_ROW_N(res) == 0) {
LM_DBG("user is not in group '%.*s'\n",
(grp_s.len), ZSW((grp_s.s)));
(grp_s->len), ZSW((grp_s->s)));
group_dbf.free_result(group_dbh, res);
return -6;
} else {
LM_DBG("user is in group '%.*s'\n",
(grp_s.len), ZSW((grp_s.s)));
(grp_s->len), ZSW((grp_s->s)));
group_dbf.free_result(group_dbh, res);
return 1;
}
Expand Down Expand Up @@ -248,9 +235,9 @@ static unsigned int hf_type( str *str1)
* is Group-Check. SIP-Group is SER specific attribute and Group-Check is
* SER specific service type value.
*/
int aaa_is_user_in(struct sip_msg* _m, char* _hf, char* _group)
int aaa_is_user_in(struct sip_msg* _m, void* _hf, str* grp)
{
str *grp, user_name, user, domain;
str user_name, user, domain;
dig_cred_t* cred = 0;
int hf_type;
uint32_t service;
Expand All @@ -260,8 +247,6 @@ int aaa_is_user_in(struct sip_msg* _m, char* _hf, char* _group)
struct hdr_field* h;
struct sip_uri *turi;

grp = (str*)_group; /* via fixup */

hf_type = (int)(long)_hf;

turi = 0;
Expand Down
4 changes: 2 additions & 2 deletions modules/group/group.h
Expand Up @@ -51,15 +51,15 @@ int get_username_domain(struct sip_msg *msg, str *hf_s,
/*
* Check if username in specified header field is in a table
*/
int db_is_user_in(struct sip_msg* _msg, char* _hf, char* _grp);
int db_is_user_in(struct sip_msg* _msg, str* hf_s, str* grp_s);

/*
* Check from AAA if a user belongs to a group. User-Name is digest
* username or digest username@realm, SIP-Group is group, and Service-Type
* is Group-Check. SIP-Group is SER specific attribute and Group-Check is
* SER specific service type value.
*/
int aaa_is_user_in(struct sip_msg* _msg, char* _hf, char* _group);
int aaa_is_user_in(struct sip_msg* _m, void* _hf, str* grp);

int group_db_init(const str* db_url);
int group_db_bind(const str* db_url);
Expand Down
159 changes: 77 additions & 82 deletions modules/group/group_mod.c
Expand Up @@ -46,7 +46,6 @@
#include "group_mod.h"
#include "group.h"
#include "re_group.h"
#include "../../mod_fix.h"
#include "../../aaa/aaa.h"


Expand All @@ -71,12 +70,12 @@ static int child_init(int rank);
*/
static int mod_init(void);

static int aaa_is_user_fixup(void** param, int param_no);
static int db_is_user_fixup(void** param, int param_no);
static int db_get_gid_fixup(void** param, int param_no);

static int obsolete_fixup_0(void** param, int param_no);
static int obsolete_fixup_1(void** param, int param_no);
static int aaa_is_user_fixup(void** param);
static int db_get_gid_fixup(void** param);
static int check_dburl_fixup(void** param);
static int check_aaaurl_fixup(void** param);
static int obsolete_fixup_0(void** param);
static int obsolete_fixup_1(void** param);

#define TABLE "grp"
#define TABLE_LEN (sizeof(TABLE) - 1)
Expand Down Expand Up @@ -131,21 +130,43 @@ aaa_map vals[V_MAX];
/*
* Exported functions
*/
// static cmd_export_t cmds[] = {
// {"is_user_in", (cmd_function)NULL, 2, obsolete_fixup_0, 0,
// REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
// {"get_user_group", (cmd_function)NULL, 2, obsolete_fixup_1, 0,
// REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
// {"aaa_is_user_in", (cmd_function)aaa_is_user_in, 2, aaa_is_user_fixup, 0,
// REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
// {"db_is_user_in", (cmd_function)db_is_user_in, 2, db_is_user_fixup, 0,
// REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
// {"db_get_user_group", (cmd_function)get_user_group, 2, db_get_gid_fixup, 0,
// REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
// {0, 0, 0, 0, 0, 0}
// };

static cmd_export_t cmds[] = {
{"is_user_in", (cmd_function)NULL, 2, obsolete_fixup_0, 0,
REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"get_user_group", (cmd_function)NULL, 2, obsolete_fixup_1, 0,
REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"aaa_is_user_in", (cmd_function)aaa_is_user_in, 2, aaa_is_user_fixup, 0,
REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"db_is_user_in", (cmd_function)db_is_user_in, 2, db_is_user_fixup, 0,
REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"db_get_user_group", (cmd_function)get_user_group, 2, db_get_gid_fixup, 0,
REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{0, 0, 0, 0, 0, 0}
{"is_user_in", (cmd_function)NULL, {
{CMD_PARAM_STR,obsolete_fixup_0,0},
{CMD_PARAM_STR,obsolete_fixup_0,0}, {0,0,0}},
REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"get_user_group", (cmd_function)NULL, {
{CMD_PARAM_STR,obsolete_fixup_1,0},
{CMD_PARAM_STR,obsolete_fixup_1,0}, {0,0,0}},
REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"aaa_is_user_in", (cmd_function)aaa_is_user_in, {
{CMD_PARAM_STR, aaa_is_user_fixup, 0},
{CMD_PARAM_STR, check_aaaurl_fixup, 0}, {0,0,0}},
REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"db_is_user_in", (cmd_function)db_is_user_in, {
{CMD_PARAM_STR, check_dburl_fixup, 0},
{CMD_PARAM_STR, check_dburl_fixup, 0}, {0,0,0}},
REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"db_get_user_group", (cmd_function)get_user_group, {
{CMD_PARAM_STR, check_dburl_fixup, 0},
{CMD_PARAM_VAR, db_get_gid_fixup, 0}, {0,0,0}},
REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
};


/*
* Exported parameters
*/
Expand Down Expand Up @@ -299,7 +320,7 @@ static void destroy(void)
}


static int obsolete_fixup_0(void** param, int param_no) {
static int obsolete_fixup_0(void** param) {

LM_ERR("You are using is_user_in function that is now obsolete. \
If you want to use it with DB support, use db_is_user_in. \
Expand All @@ -308,99 +329,73 @@ If you want to use it with AAA support, use aaa_is_user_in.\n");
return E_CFG;
}

static int obsolete_fixup_1(void** param, int param_no) {
static int obsolete_fixup_1(void** param) {

LM_ERR("You are get_user_group function that has been renamed\
into db_get_user_group\n");

return E_CFG;
}

static int db_get_gid_fixup(void** param, int param_no)
static int db_get_gid_fixup(void** param)
{
pv_spec_t *sp;
str name;

if (!db_url.s) {
LM_ERR("no database url\n");
return E_CFG;
}

if (param_no == 1) {
return fixup_spve_spve(param, param_no);
} else if (param_no == 2) {
name.s = (char*)*param;
name.len = strlen(name.s);
sp = (pv_spec_t*)pkg_malloc(sizeof(pv_spec_t));
if (sp == NULL) {
LM_ERR("no more pkg memory\n");
return E_UNSPEC;
}
if(pv_parse_spec(&name, sp)==NULL || sp->type!=PVT_AVP)
{
LM_ERR("bad AVP spec <%s>\n", name.s);
pv_spec_free(sp);
return E_UNSPEC;
}

*param = sp;
if (((pv_spec_t *)*param)->type!=PVT_AVP) {
LM_ERR("return parameter must be an AVP\n");
return E_SCRIPT;
}

return 0;
}

static int check_dburl_fixup(void** param)
{
if (!db_url.s) {
LM_ERR("no database url\n");
return E_CFG;
}

return 0;
}

static int aaa_is_user_fixup(void** param, int param_no)
static int aaa_is_user_fixup(void** param)
{
void* ptr;
str* s;
static str ru_s = str_init("Request-URI");
static str to_s = str_init("To");
static str fr_s = str_init("From");
static str cred_s = str_init("Credentials");

if (!aaa_proto_url.s) {
LM_ERR("no aaa protocol url\n");
return E_CFG;
}

if (param_no == 1) {
ptr = *param;

if (!strcasecmp((char*)*param, "Request-URI")) {
*param = (void*)1;
} else if (!strcasecmp((char*)*param, "To")) {
*param = (void*)2;
} else if (!strcasecmp((char*)*param, "From")) {
*param = (void*)3;
} else if (!strcasecmp((char*)*param, "Credentials")) {
*param = (void*)4;
} else {
LM_ERR("unsupported Header Field identifier\n");
return E_UNSPEC;
}

pkg_free(ptr);
} else if (param_no == 2) {

s = (str*)pkg_malloc(sizeof(str));
if (!s) {
LM_ERR("no pkg memory left\n");
return E_UNSPEC;
}

s->s = (char*)*param;
s->len = strlen(s->s);
*param = (void*)s;
if (!str_strcasecmp((str*)*param, &ru_s)) {
*param = (void*)1;
} else if (!str_strcasecmp((str*)*param, &to_s)) {
*param = (void*)2;
} else if (!str_strcasecmp((str*)*param, &fr_s)) {
*param = (void*)3;
} else if (!str_strcasecmp((str*)*param, &cred_s)) {
*param = (void*)4;
} else {
LM_ERR("unsupported Header Field identifier\n");
return E_UNSPEC;
}

return 0;
}


static int db_is_user_fixup(void** param, int param_no) {

if (db_url.s) {
fixup_spve_spve(param, param_no);
return 0;
static int check_aaaurl_fixup(void** param)
{
if (!aaa_proto_url.s) {
LM_ERR("no aaa protocol url\n");
return E_CFG;
}

LM_ERR("no database url\n");
return E_CFG;
return 0;
}
15 changes: 3 additions & 12 deletions modules/group/re_group.c
Expand Up @@ -25,7 +25,6 @@
#include <sys/types.h>
#include <regex.h>

#include "../../mod_fix.h"
#include "../../str.h"
#include "../../mem/mem.h"
#include "../../route_struct.h"
Expand Down Expand Up @@ -127,26 +126,19 @@ int load_re( str *table )



int get_user_group(struct sip_msg *req, char *user, char *avp)
int get_user_group(struct sip_msg *req, str *user_str, pv_spec_t *avp)
{
static char uri_buf[MAX_URI_SIZE];
str user_str;
str username;
str domain;
pv_spec_t *pvs;
pv_value_t val;
struct re_grp *rg;
regmatch_t pmatch;
char *c;
unsigned int aux;
int n;

if(user == NULL || fixup_get_svalue(req, (gparam_p)user, &user_str) != 0){
LM_ERR("Invalid parameter URI\n");
return -1;
}

if (get_username_domain( req, &user_str, &username, &domain)!=0){
if (get_username_domain( req, user_str, &username, &domain)!=0){
LM_ERR("failed to get username@domain\n");
goto error;
}
Expand All @@ -172,7 +164,6 @@ int get_user_group(struct sip_msg *req, char *user, char *avp)
*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;

Expand All @@ -183,7 +174,7 @@ int get_user_group(struct sip_msg *req, char *user, char *avp)

/* match -> add the gid as AVP */
val.ri = rg->gid.n;
if(pv_set_value(req, pvs, (int)EQ_T, &val)<0)
if(pv_set_value(req, avp, (int)EQ_T, &val)<0)
{
LM_ERR("setting PV AVP failed\n");
goto error;
Expand Down
2 changes: 1 addition & 1 deletion modules/group/re_group.h
Expand Up @@ -30,6 +30,6 @@

int load_re(str *table);

int get_user_group(struct sip_msg *req, char *user, char *avp);
int get_user_group(struct sip_msg *req, str *user_str, pv_spec_t *avp);

#endif

0 comments on commit 863f93b

Please sign in to comment.