Skip to content

Commit

Permalink
ldap: 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 2aa7944 commit 4137c5a
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 391 deletions.
2 changes: 1 addition & 1 deletion modules/ldap/api.h
Expand Up @@ -96,7 +96,7 @@ static inline int load_ldap_api(ldap_api_t *api)
{
load_ldap_t load_ldap;

if (!(load_ldap = (load_ldap_t) find_export("load_ldap", 0, 0)))
if (!(load_ldap = (load_ldap_t) find_export("load_ldap", 0)))
{
LM_ERR("can't import load_ldap\n");
return -1;
Expand Down
28 changes: 20 additions & 8 deletions modules/ldap/ldap_api_fn.c
Expand Up @@ -377,27 +377,33 @@ int ldap_params_search(


int ldap_url_search_async(
char* _ldap_url,
str* _ldap_url,
int* _msgidp,
struct ld_session **ldsp,
struct ld_conn** conn,
int* ld_result_count)
{
LDAPURLDesc *ludp;
int rc;
str ldap_url_nt;

if (ldap_url_parse(_ldap_url, &ludp) != 0) {
LM_ERR("invalid LDAP URL [%s]\n", ZSW(_ldap_url));
if (pkg_nt_str_dup(&ldap_url_nt, _ldap_url) < 0) {
LM_ERR("no more pkg memory\n");
return -2;
}

if (ldap_url_parse(ldap_url_nt.s, &ludp) != 0) {
LM_ERR("invalid LDAP URL [%s]\n", ldap_url_nt.s);
if (ludp != NULL) {
ldap_free_urldesc(ludp);
}
return -2;
goto error;
}
if (ludp->lud_host == NULL)
{
LM_ERR( "no ldap session name found in ldap URL [%s]\n",
ZSW(_ldap_url));
return -2;
ldap_url_nt.s);
goto error;
}


Expand All @@ -418,6 +424,7 @@ int ldap_url_search_async(
if ((rc == 0 && *_msgidp >= 0) || rc == 1) {
if (get_connected_ldap_session(ludp->lud_host, ldsp)) {
LM_ERR("[%s]: couldn't get ldap session\n", ludp->lud_host);
pkg_free(ldap_url_nt.s);
return -1;
}
}
Expand All @@ -427,13 +434,18 @@ int ldap_url_search_async(
*ld_result_count = ldap_count_entries((*ldsp)->conn_s.handle, last_ldap_result);
if (*ld_result_count < 0) {
LM_ERR("[%s]: ldap_count_entries for sync call failed\n", (*ldsp)->name);
pkg_free(ldap_url_nt.s);
return -1;
}
}


ldap_free_urldesc(ludp);
pkg_free(ldap_url_nt.s);
return rc;
error:
pkg_free(ldap_url_nt.s);
return -2;
}


Expand All @@ -446,7 +458,7 @@ int ldap_url_search(
int rc;

if (ldap_url_parse(_ldap_url, &ludp) != 0) {
LM_ERR("invalid LDAP URL [%s]\n", ZSW(_ldap_url));
LM_ERR("invalid LDAP URL [%s]\n", _ldap_url);
if (ludp != NULL) {
ldap_free_urldesc(ludp);
}
Expand All @@ -455,7 +467,7 @@ int ldap_url_search(
if (ludp->lud_host == NULL)
{
LM_ERR( "no ldap session name found in ldap URL [%s]\n",
ZSW(_ldap_url));
_ldap_url);
return -2;
}

Expand Down
2 changes: 1 addition & 1 deletion modules/ldap/ldap_api_fn.h
Expand Up @@ -68,7 +68,7 @@ int ldap_params_search(
...);

int ldap_url_search_async(
char* _ldap_url,
str* _ldap_url,
int* _msgidp,
struct ld_session **ldsp,
struct ld_conn** conn,
Expand Down
128 changes: 36 additions & 92 deletions modules/ldap/ldap_exp_fn.c
Expand Up @@ -65,6 +65,7 @@ int resume_ldap_search(int fd, struct sip_msg *msg, void *param)
switch (rc) {
case -1:
/* error */
pkg_free(as_params->ldap_url.s);
pkg_free(as_params);
return -1;
case 0:
Expand All @@ -73,6 +74,7 @@ int resume_ldap_search(int fd, struct sip_msg *msg, void *param)
return 1;
case 1:
/* successfull */
pkg_free(as_params->ldap_url.s);
pkg_free(as_params);
async_status = ASYNC_DONE;

Expand All @@ -95,9 +97,8 @@ int resume_ldap_search(int fd, struct sip_msg *msg, void *param)
int ldap_search_impl_async(
struct sip_msg* _msg,
async_ctx *ctx,
pv_elem_t* _ldap_url)
str* ldap_url)
{
str ldap_url;
int msgid;
int sockfd;
int rc=-1;
Expand All @@ -106,26 +107,10 @@ int ldap_search_impl_async(
struct ld_session *lds;
struct ld_conn* conn;

/*
* do variable substitution for _ldap_url (pv_printf_s)
*/
if (_ldap_url==NULL) {
LM_ERR("empty ldap_url\n");
return -2;
}
if ( _ldap_url->spec.getf!=NULL) {
if (pv_printf_s( _msg, _ldap_url, &ldap_url)!=0 || ldap_url.len<=0) {
LM_ERR("pv_printf_s failed\n");
return -2;
}
} else {
ldap_url = _ldap_url->text;
}

/*
* perform LDAP search
*/
if ((rc=ldap_url_search_async(ldap_url.s, &msgid, &lds, &conn, &ld_result_count)) < 0)
if ((rc=ldap_url_search_async(ldap_url, &msgid, &lds, &conn, &ld_result_count)) < 0)
{
/* LDAP search error */
rc = -2;
Expand Down Expand Up @@ -160,9 +145,12 @@ int ldap_search_impl_async(
}

as_params->msgid = msgid;
as_params->ldap_url = ldap_url;
as_params->lds = lds;
as_params->conn = conn;
if (pkg_nt_str_dup(&as_params->ldap_url, ldap_url) < 0) {
LM_ERR("no more pkg mem\n");
goto error;
}

ctx->resume_param = as_params;
ctx->resume_f = resume_ldap_search;/* resume function */
Expand All @@ -178,35 +166,28 @@ int ldap_search_impl_async(

int ldap_search_impl(
struct sip_msg* _msg,
pv_elem_t* _ldap_url)
str* ldap_url)
{
str ldap_url;
int ld_result_count = 0;
str ldap_url_nt;

/*
* do variable substitution for _ldap_url (pv_printf_s)
*/
if (_ldap_url==NULL) {
LM_ERR("empty ldap_url\n");
if (pkg_nt_str_dup(&ldap_url_nt, ldap_url) < 0) {
LM_ERR("no more pkg memory\n");
return -2;
}
if ( _ldap_url->spec.getf!=NULL) {
if (pv_printf_s( _msg, _ldap_url, &ldap_url)!=0 || ldap_url.len<=0) {
LM_ERR("pv_printf_s failed\n");
return -2;
}
} else {
ldap_url = _ldap_url->text;
}

/*
* perform LDAP search
*/
if (ldap_url_search(ldap_url.s, &ld_result_count) != 0)
if (ldap_url_search(ldap_url_nt.s, &ld_result_count) != 0)
{
/* LDAP search error */
pkg_free(ldap_url_nt.s);
return -2;
}

pkg_free(ldap_url_nt.s);

if (ld_result_count < 1)
{
/* no LDAP entry found */
Expand All @@ -216,10 +197,8 @@ int ldap_search_impl(
return ld_result_count;
}

int ldap_write_result(
struct sip_msg* _msg,
struct ldap_result_params* _lrp,
struct subst_expr* _se)
int ldap_write_result( struct sip_msg* _msg, str *attr_name, pv_spec_t *dst_avp,
int avp_type, struct subst_expr* _se)
{
int dst_avp_name;
int_str dst_avp_val;
Expand All @@ -234,7 +213,7 @@ int ldap_write_result(
*/

if (pv_get_avp_name( _msg,
&(_lrp->dst_avp_spec.pvp),
&dst_avp->pvp,
&dst_avp_name,
&dst_avp_type)
!= 0)
Expand All @@ -246,7 +225,7 @@ int ldap_write_result(
/*
* get LDAP attr values
*/
if ((rc = ldap_get_attr_vals(&_lrp->ldap_attr_name, &attr_vals)) != 0)
if ((rc = ldap_get_attr_vals(attr_name, &attr_vals)) != 0)
{
if (rc > 0) {
return -1;
Expand Down Expand Up @@ -276,7 +255,7 @@ int ldap_write_result(
avp_val_str = *subst_result;
}

if (_lrp->dst_avp_val_type == 1)
if (avp_type == 1)
{
/* try to convert ldap value to integer */
if (!str2sint(&avp_val_str, &avp_val_int))
Expand Down Expand Up @@ -338,40 +317,19 @@ int ldap_result_next(void)
}
}

int ldap_result_check(
struct sip_msg* _msg,
struct ldap_result_check_params* _lrp,
struct subst_expr* _se)
int ldap_result_check(struct sip_msg* _msg, str* attr_name, str *check_str,
struct subst_expr *_se)
{
str check_str, *subst_result = NULL;
str *subst_result = NULL;
int rc, i, nmatches;
char *attr_val;
str attr_val;
struct berval **attr_vals;

/*
* do variable substitution for check_str
*/

if (_lrp->check_str_elem_p)
{
if (pv_printf_s(_msg, _lrp->check_str_elem_p, &check_str) != 0)
{
LM_ERR("pv_printf_s failed\n");
return -2;
}
} else
{
LM_ERR("empty check string\n");
return -2;
}

LM_DBG("check_str [%s]\n", check_str.s);

/*
* get LDAP attr values
*/

if ((rc = ldap_get_attr_vals(&_lrp->ldap_attr_name, &attr_vals)) != 0)
if ((rc = ldap_get_attr_vals(attr_name, &attr_vals)) != 0)
{
if (rc > 0) {
return -1;
Expand All @@ -388,7 +346,8 @@ int ldap_result_check(
{
if (_se == NULL)
{
attr_val = attr_vals[i]->bv_val;
attr_val.s = attr_vals[i]->bv_val;
attr_val.len = strlen(attr_val.s);
} else
{
subst_result = subst_str(attr_vals[i]->bv_val, _msg, _se,
Expand All @@ -397,11 +356,11 @@ int ldap_result_check(
{
continue;
}
attr_val = subst_result->s;
attr_val = *subst_result;
}

LM_DBG("attr_val [%s]\n", attr_val);
rc = strncmp(check_str.s, attr_val, check_str.len);
LM_DBG("attr_val [%.*s]\n", attr_val.len, attr_val.s);
rc = str_strcmp(check_str, &attr_val);
if (_se != NULL)
{
pkg_free(subst_result->s);
Expand All @@ -417,28 +376,13 @@ int ldap_result_check(
return -1;
}

int ldap_filter_url_encode(
struct sip_msg* _msg,
pv_elem_t* _filter_component,
pv_spec_t* _dst_avp_spec)
int ldap_filter_url_encode(struct sip_msg* _msg, str *filter_component,
pv_spec_t* _dst_avp_spec)
{
str filter_component_str, esc_str;
str esc_str;
int dst_avp_name;
unsigned short dst_avp_type;

/*
* variable substitution for _filter_component
*/
if (_filter_component) {
if (pv_printf_s(_msg, _filter_component, &filter_component_str) != 0) {
LM_ERR("pv_printf_s failed\n");
return -1;
}
} else {
LM_ERR("empty first argument\n");
return -1;
}

/*
* get dst AVP name (dst_avp_name)
*/
Expand All @@ -454,7 +398,7 @@ int ldap_filter_url_encode(
*/
esc_str.s = esc_buf;
esc_str.len = ESC_BUF_SIZE;
if (ldap_rfc4515_escape(&filter_component_str, &esc_str, 1) != 0)
if (ldap_rfc4515_escape(filter_component, &esc_str, 1) != 0)
{
LM_ERR("ldap_rfc4515_escape() failed\n");
return -1;
Expand Down

0 comments on commit 4137c5a

Please sign in to comment.