Browse code

ldap: several functions exported to kemi framework

(cherry picked from commit 2d4b4939d1845fffdc3fa3ced9889720d0213d47)

Daniel-Constantin Mierla authored on 31/05/2021 11:36:11
Showing 3 changed files
... ...
@@ -79,40 +79,19 @@ int ldap_search_impl(struct sip_msg *_msg, str *ldap_url)
79 79
 	return ld_result_count;
80 80
 }
81 81
 
82
-int ldap_write_result(struct sip_msg *_msg, struct ldap_result_params *_lrp,
83
-		struct subst_expr *_se)
82
+int ldap_result_toavp(sip_msg_t *_msg, str *attrname, struct subst_expr *_se,
83
+		int_str *dst_avp_name, int dst_avp_type, int dst_avp_val_type)
84 84
 {
85
-	int_str dst_avp_name, dst_avp_val;
86
-	unsigned short dst_avp_type;
85
+	int_str dst_avp_val;
87 86
 	int nmatches, rc, i, added_avp_count = 0;
88 87
 	struct berval **attr_vals;
89 88
 	str avp_val_str, *subst_result = NULL;
90 89
 	int avp_val_int;
91 90
 
92
-	/*
93
-	* get dst AVP name (dst_avp_name)
94
-	*/
95
-
96
-	if(pv_get_avp_name(
97
-			   _msg, &(_lrp->dst_avp_spec.pvp), &dst_avp_name, &dst_avp_type)
98
-			!= 0) {
99
-		LM_ERR("error getting dst AVP name\n");
100
-		return -2;
101
-	}
102
-	if(dst_avp_type & AVP_NAME_STR) {
103
-		if(dst_avp_name.s.len >= STR_BUF_SIZE) {
104
-			LM_ERR("dst AVP name too long\n");
105
-			return -2;
106
-		}
107
-		strncpy(str_buf, dst_avp_name.s.s, dst_avp_name.s.len);
108
-		str_buf[dst_avp_name.s.len] = '\0';
109
-		dst_avp_name.s.s = str_buf;
110
-	}
111
-
112 91
 	/*
113 92
 	* get LDAP attr values
114 93
 	*/
115
-	if((rc = ldap_get_attr_vals(&_lrp->ldap_attr_name, &attr_vals)) != 0) {
94
+	if((rc = ldap_get_attr_vals(attrname, &attr_vals)) != 0) {
116 95
 		if(rc > 0) {
117 96
 			return -1;
118 97
 		} else {
... ...
@@ -136,18 +115,18 @@ int ldap_write_result(struct sip_msg *_msg, struct ldap_result_params *_lrp,
136 115
 			avp_val_str = *subst_result;
137 116
 		}
138 117
 
139
-		if(_lrp->dst_avp_val_type == 1) {
118
+		if(dst_avp_val_type == 1) {
140 119
 			/* try to convert ldap value to integer */
141 120
 			if(!str2sint(&avp_val_str, &avp_val_int)) {
142 121
 				dst_avp_val.n = avp_val_int;
143
-				rc = add_avp(dst_avp_type, dst_avp_name, dst_avp_val);
122
+				rc = add_avp(dst_avp_type, *dst_avp_name, dst_avp_val);
144 123
 			} else {
145 124
 				continue;
146 125
 			}
147 126
 		} else {
148 127
 			/* save ldap value as string */
149 128
 			dst_avp_val.s = avp_val_str;
150
-			rc = add_avp(dst_avp_type | AVP_VAL_STR, dst_avp_name, dst_avp_val);
129
+			rc = add_avp(dst_avp_type | AVP_VAL_STR, *dst_avp_name, dst_avp_val);
151 130
 		}
152 131
 
153 132
 		if(subst_result != NULL) {
... ...
@@ -174,6 +153,35 @@ int ldap_write_result(struct sip_msg *_msg, struct ldap_result_params *_lrp,
174 153
 	}
175 154
 }
176 155
 
156
+int ldap_write_result(struct sip_msg *_msg, struct ldap_result_params *_lrp,
157
+		struct subst_expr *_se)
158
+{
159
+	int_str dst_avp_name;
160
+	unsigned short dst_avp_type;
161
+
162
+	/*
163
+	* get dst AVP name (dst_avp_name)
164
+	*/
165
+	if(pv_get_avp_name(
166
+			   _msg, &(_lrp->dst_avp_spec.pvp), &dst_avp_name, &dst_avp_type)
167
+			!= 0) {
168
+		LM_ERR("error getting dst AVP name\n");
169
+		return -2;
170
+	}
171
+	if(dst_avp_type & AVP_NAME_STR) {
172
+		if(dst_avp_name.s.len >= STR_BUF_SIZE) {
173
+			LM_ERR("dst AVP name too long\n");
174
+			return -2;
175
+		}
176
+		strncpy(str_buf, dst_avp_name.s.s, dst_avp_name.s.len);
177
+		str_buf[dst_avp_name.s.len] = '\0';
178
+		dst_avp_name.s.s = str_buf;
179
+	}
180
+
181
+	return ldap_result_toavp(_msg, &_lrp->ldap_attr_name, _se,
182
+			&dst_avp_name, dst_avp_type, _lrp->dst_avp_val_type);
183
+}
184
+
177 185
 int ldap_result_next(void)
178 186
 {
179 187
 	int rc;
... ...
@@ -52,6 +52,9 @@ int ldap_search_impl(struct sip_msg *_msg, str *ldap_url);
52 52
 int ldap_write_result(struct sip_msg *_msg, struct ldap_result_params *_lrp,
53 53
 		struct subst_expr *_se);
54 54
 
55
+int ldap_result_toavp(sip_msg_t *_msg, str *attrname, struct subst_expr *_se,
56
+		int_str *dst_avp_name, int dst_avp_type, int dst_avp_val_type);
57
+
55 58
 int ldap_result_next(void);
56 59
 
57 60
 int ldap_filter_url_encode(struct sip_msg *_msg, pv_elem_t *_filter_component,
... ...
@@ -34,6 +34,7 @@
34 34
 #include "../../core/sr_module.h"
35 35
 #include "../../core/pvar.h"
36 36
 #include "../../core/mod_fix.h"
37
+#include "../../core/kemi.h"
37 38
 #include "../../core/mem/mem.h"
38 39
 
39 40
 #include "ld_session.h"
... ...
@@ -465,3 +466,65 @@ static int ldap_filter_url_encode_fixup(void **param, int param_no)
465 466
 
466 467
 	return 0;
467 468
 }
469
+
470
+/**
471
+ *
472
+ */
473
+static int ki_ldap_search(sip_msg_t *msg, str *ldapurl)
474
+{
475
+	return ldap_search_impl(msg, ldapurl);
476
+}
477
+
478
+/**
479
+ *
480
+ */
481
+static int ki_ldap_result_str(sip_msg_t *msg, str *attrname, str *avpname)
482
+{
483
+	int_str dst_avp_name;
484
+
485
+	dst_avp_name.s = *avpname;
486
+	return ldap_result_toavp(msg, attrname, NULL, &dst_avp_name,
487
+			AVP_NAME_STR, 0 /*str result*/);
488
+}
489
+
490
+/**
491
+ *
492
+ */
493
+static int ki_ldap_result_next(sip_msg_t *msg)
494
+{
495
+	return ldap_result_next();
496
+}
497
+
498
+/**
499
+ *
500
+ */
501
+/* clang-format off */
502
+static sr_kemi_t sr_kemi_ldap_exports[] = {
503
+	{ str_init("ldap"), str_init("search"),
504
+		SR_KEMIP_INT, ki_ldap_search,
505
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
506
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
507
+	},
508
+	{ str_init("secsipid"), str_init("result_str"),
509
+		SR_KEMIP_INT, ki_ldap_result_str,
510
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
511
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
512
+	},
513
+	{ str_init("ldap"), str_init("result_next"),
514
+		SR_KEMIP_INT, ki_ldap_result_next,
515
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
516
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
517
+	},
518
+
519
+	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
520
+};
521
+/* clang-format on */
522
+
523
+/**
524
+ *
525
+ */
526
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
527
+{
528
+	sr_kemi_modules_add(sr_kemi_ldap_exports);
529
+	return 0;
530
+}