h350: coherent indentation, removed dead code and init lenght for str
[sip-router] / src / modules / h350 / h350_mod.c
1 /* 
2  * Kamailio H.350 Module
3  *
4  * Copyright (C) 2007 University of North Carolina
5  *
6  * Original author: Christian Schlatter, cs@unc.edu
7  * 
8  *
9  * This file is part of Kamailio, a free SIP server.
10  *
11  * Kamailio is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version
15  *
16  * Kamailio is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License 
22  * along with this program; if not, write to the Free Software 
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
24  *
25  */
26
27 #include "../../core/ut.h"
28 #include "../../core/sr_module.h"
29 #include "h350_mod.h"
30 #include "h350_exp_fn.h"
31
32
33 MODULE_VERSION
34
35 /*
36  * Module management function prototypes
37  */
38 static int mod_init(void);
39 static int child_init(int rank);
40
41 /*
42  * fixup functions
43  */
44 static int one_str_pv_elem_fixup(void** param, int param_no);
45 static int h350_auth_lookup_fixup(void** param, int param_no);
46
47 /*
48  * exported functions
49  */
50
51 static int w_h350_sipuri_lookup(struct sip_msg* msg, char* sip_uri, char* s2);
52 static int w_h350_auth_lookup(struct sip_msg* msg, char* digest_username, char* avp_specs);
53 static int w_h350_call_preferences(struct sip_msg* msg, char* avp_name_prefix, char* s2);
54 static int w_h350_service_level(struct sip_msg* msg, char* avp_name_prefix, char* s2);
55
56 /*
57  * Module parameter variables
58  */
59 char* h350_ldap_session = H350_LDAP_SESSION;
60 char* h350_base_dn = H350_BASE_DN;
61 char* h350_search_scope = H350_SEARCH_SCOPE;
62 int h350_search_scope_int = -1;
63
64
65 /*
66  * LDAP API
67  */
68 ldap_api_t ldap_api;
69
70 /*
71  * Exported functions
72  */
73 static cmd_export_t cmds[] = {
74         {"h350_sipuri_lookup",           (cmd_function)w_h350_sipuri_lookup,     1,
75          one_str_pv_elem_fixup, 0,
76          REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE|LOCAL_ROUTE},
77         {"h350_auth_lookup",             (cmd_function)w_h350_auth_lookup,       2,
78          h350_auth_lookup_fixup, 0,
79          REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE|LOCAL_ROUTE},
80         {"h350_result_call_preferences", (cmd_function)w_h350_call_preferences,  1,
81          one_str_pv_elem_fixup, 0,
82          REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE|LOCAL_ROUTE},
83         {"h350_result_service_level",    (cmd_function)w_h350_service_level,     1,
84          one_str_pv_elem_fixup, 0,
85          REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE|LOCAL_ROUTE},
86         {0, 0, 0, 0, 0, 0}
87 };
88
89
90 /*
91  * Exported parameters
92  */
93 static param_export_t params[] = {
94         {"ldap_session",     PARAM_STRING, &h350_ldap_session},
95         {"base_dn",          PARAM_STRING, &h350_base_dn},
96         {"search_scope",     PARAM_STRING, &h350_search_scope},
97         {0, 0, 0}
98 };
99
100
101 /*
102  * Module interface
103  */
104 struct module_exports exports = {
105         "h350", 
106         DEFAULT_DLFLAGS, /* dlopen flags */
107         cmds,       /* Exported functions */
108         params,     /* Exported parameters */
109         0,          /* exported statistics */
110         0,          /* exported MI functions */
111         0,          /* exported pseudo-variables */
112         0,          /* extra processes */
113         mod_init,   /* module initialization function */
114         0,          /* response function */
115         0,          /* destroy function */
116         child_init  /* child initialization function */
117 };
118
119 static int child_init(int rank)
120 {
121
122         /* don't do anything for non-worker process */
123         if(rank < 1) {
124                 return 0;
125         }
126
127         h350_search_scope_int = ldap_api.ldap_str2scope(h350_search_scope);
128
129         /*
130          * initialize h350_exp_fn
131          */
132         if(h350_exp_fn_init() != 0) {
133                 LM_ERR("h350_exp_fn_init failed\n");
134                 return -1;
135         }
136
137
138         return 0;
139 }
140
141
142 static int mod_init(void)
143 {
144         /*
145          * load the LDAP API
146          */
147         if(load_ldap_api(&ldap_api) != 0) {
148                 LM_ERR("Unable to load LDAP API - this module requires ldap module\n");
149                 return -1;
150         }
151
152         return 0;
153 }
154
155
156 /*
157  * EXPORTED functions
158  */
159 static int w_h350_sipuri_lookup(struct sip_msg *msg, char *sip_uri, char *s2)
160 {
161         return h350_sipuri_lookup(msg, (pv_elem_t *)sip_uri);
162 }
163
164 static int w_h350_auth_lookup(
165                 struct sip_msg *msg, char *digest_username, char *avp_specs)
166 {
167         return h350_auth_lookup(msg, (pv_elem_t *)digest_username,
168                         (struct h350_auth_lookup_avp_params *)avp_specs);
169 }
170
171 static int w_h350_call_preferences(
172                 struct sip_msg *msg, char *avp_name_prefix, char *s2)
173 {
174         return h350_call_preferences(msg, (pv_elem_t *)avp_name_prefix);
175 }
176
177 static int w_h350_service_level(
178                 struct sip_msg *msg, char *avp_name_prefix, char *s2)
179 {
180         return h350_service_level(msg, (pv_elem_t *)avp_name_prefix);
181 }
182
183 /*
184  * FIXUP functions
185  */
186
187 static int one_str_pv_elem_fixup(void **param, int param_no)
188 {
189         pv_elem_t *model;
190         str s;
191
192         if(param_no == 1) {
193                 s.s = (char *)*param;
194                 if(s.s == 0 || s.s[0] == 0) {
195                         model = 0;
196                 } else {
197                         s.len = strlen(s.s);
198                         if(pv_parse_format(&s, &model) < 0) {
199                                 LM_ERR("pv_parse_format failed\n");
200                                 return E_OUT_OF_MEM;
201                         }
202                 }
203                 *param = (void *)model;
204         }
205
206         return 0;
207 }
208
209 static int h350_auth_lookup_fixup(void **param, int param_no)
210 {
211         pv_elem_t *model;
212         char *p, *username_avp_spec_str, *pwd_avp_spec_str;
213         str s;
214         struct h350_auth_lookup_avp_params *params;
215
216         if(param_no == 1) {
217                 s.s = (char *)*param;
218                 if(s.s == 0 || s.s[0] == 0) {
219                         model = 0;
220                 } else {
221                         s.len = strlen(s.s);
222                         if(pv_parse_format(&s, &model) < 0) {
223                                 LM_ERR("pv_parse_format failed\n");
224                                 return E_OUT_OF_MEM;
225                         }
226                 }
227                 *param = (void *)model;
228         } else if(param_no == 2) {
229                 /*
230                  * parse *param into username_avp_spec_str and pwd_avp_spec_str
231                  */
232
233                 username_avp_spec_str = (char *)*param;
234                 if((pwd_avp_spec_str = strchr(username_avp_spec_str, '/')) == 0) {
235                         /* no '/' found in username_avp_spec_str */
236                         LM_ERR("invalid second argument [%s]\n", username_avp_spec_str);
237                         return E_UNSPEC;
238                 }
239                 *(pwd_avp_spec_str++) = 0;
240
241                 /*
242                  * parse avp specs into pv_spec_t and store in params
243                  */
244                 params = (struct h350_auth_lookup_avp_params *)pkg_malloc(
245                                 sizeof(struct h350_auth_lookup_avp_params));
246                 if(params == NULL) {
247                         LM_ERR("no memory\n");
248                         return E_OUT_OF_MEM;
249                 }
250                 memset(params, 0, sizeof(struct h350_auth_lookup_avp_params));
251                 s.s = username_avp_spec_str;
252                 s.len = strlen(s.s);
253                 p = pv_parse_spec(&s, &params->username_avp_spec);
254                 if(p == 0) {
255                         pkg_free(params);
256                         LM_ERR("parse error for [%s]\n", username_avp_spec_str);
257                         return E_UNSPEC;
258                 }
259                 if(params->username_avp_spec.type != PVT_AVP) {
260                         pkg_free(params);
261                         LM_ERR("invalid AVP specification [%s]\n", username_avp_spec_str);
262                         return E_UNSPEC;
263                 }
264                 s.s = pwd_avp_spec_str;
265                 s.len = strlen(s.s);
266                 p = pv_parse_spec(&s, &params->password_avp_spec);
267                 if(p == 0) {
268                         pkg_free(params);
269                         LM_ERR("parse error for [%s]\n", pwd_avp_spec_str);
270                         return E_UNSPEC;
271                 }
272                 if(params->password_avp_spec.type != PVT_AVP) {
273                         pkg_free(params);
274                         LM_ERR("invalid AVP specification [%s]\n", pwd_avp_spec_str);
275                         return E_UNSPEC;
276                 }
277
278                 *param = (void *)params;
279         }
280
281         return 0;
282 }