37
37
#include "../../route.h"
38
38
#include "../../pvar.h"
39
39
#include "../../str.h"
40
+ #include "../../name_alias.h"
40
41
41
42
#define DOMAIN_TABLE_VERSION 3
42
43
@@ -92,54 +93,58 @@ int domain_db_ver(str* name, int version)
92
93
return db_check_table_version (& domain_dbf , db_handle , name , version );
93
94
}
94
95
95
-
96
- /*
97
- * Check if domain is local and store attributes in a pvar
98
- */
99
- int is_domain_local_pvar (struct sip_msg * msg , str * _host , pv_spec_t * pv )
96
+ int db_table_lookup (struct sip_msg * msg , str * lookup_domain , str * host , pv_spec_t * pv )
100
97
{
101
98
pv_value_t val ;
102
99
db_val_t * values ;
103
100
104
- if (db_mode == 0 ) {
105
- db_key_t keys [1 ];
106
- db_val_t vals [1 ];
107
- db_key_t cols [2 ];
108
- db_res_t * res = NULL ;
101
+ db_key_t keys [1 ];
102
+ db_val_t vals [1 ];
103
+ db_key_t cols [3 ];
104
+ db_res_t * res = NULL ;
109
105
110
- keys [0 ] = & domain_col ;
111
- cols [0 ] = & domain_col ;
112
- cols [1 ] = & domain_attrs_col ;
106
+ int accept_subdomain ;
113
107
114
- if ( domain_dbf . use_table ( db_handle , & domain_table ) < 0 ) {
115
- LM_ERR ( "Error while trying to use domain table\n" ) ;
116
- return -3 ;
117
- }
108
+ keys [ 0 ] = & domain_col ;
109
+ cols [ 0 ] = & domain_col ;
110
+ cols [ 1 ] = & domain_attrs_col ;
111
+ cols [ 2 ] = & domain_accept_subdomain_col ;
118
112
119
- VAL_TYPE (vals ) = DB_STR ;
120
- VAL_NULL (vals ) = 0 ;
113
+ if (domain_dbf .use_table (db_handle , & domain_table ) < 0 ) {
114
+ LM_ERR ("Error while trying to use domain table\n" );
115
+ return -3 ;
116
+ }
121
117
122
- VAL_STR (vals ). s = _host -> s ;
123
- VAL_STR (vals ). len = _host -> len ;
118
+ VAL_TYPE (vals ) = DB_STR ;
119
+ VAL_NULL (vals ) = 0 ;
124
120
125
- if (domain_dbf .query (db_handle , keys , 0 , vals , cols , 1 , 2 , 0 , & res ) < 0
126
- ) {
127
- LM_ERR ("Error while querying database\n" );
128
- return -3 ;
129
- }
121
+ VAL_STR (vals ).s = lookup_domain -> s ;
122
+ VAL_STR (vals ).len = lookup_domain -> len ;
130
123
131
- if (RES_ROW_N (res ) == 0 ) {
132
- LM_DBG ("Realm '%.*s' is not local\n" ,
133
- _host -> len , ZSW (_host -> s ));
134
- domain_dbf .free_result (db_handle , res );
135
- return -1 ;
124
+ if (domain_dbf .query (db_handle , keys , 0 , vals , cols , 1 , 3 , 0 , & res ) < 0 ) {
125
+ LM_ERR ("Error while querying database\n" );
126
+ return -3 ;
127
+ }
128
+
129
+ if (RES_ROW_N (res ) > 0 ) {
130
+ values = ROW_VALUES (RES_ROWS (res ));
131
+
132
+ if (VAL_NULL (values + 2 )) {
133
+ accept_subdomain = 0 ;
134
+ } else if (VAL_TYPE (values + 2 ) == DB_INT ) {
135
+ accept_subdomain = VAL_INT (values + 2 );
136
136
} else {
137
- LM_DBG ("Realm '%.*s' is local\n" ,
138
- _host -> len , ZSW (_host -> s ));
137
+ LM_ERR ("Error setting accept_subdomain, default to 0\n" );
138
+ accept_subdomain = 0 ;
139
+ }
140
+
141
+ LM_DBG ("Checking realm '%.*s' against domain entry '%.*s' accept_subdomain is '%d' \n" ,
142
+ host -> len , ZSW (host -> s ), lookup_domain -> len , ZSW (lookup_domain -> s ), accept_subdomain );
143
+
144
+ if (match_domain (lookup_domain -> s , lookup_domain -> len , host -> s , host -> len , accept_subdomain )) {
139
145
if (pv ) {
140
146
/* XXX: what shall we do if there are duplicate entries? */
141
147
/* we only check the first row - razvanc */
142
- values = ROW_VALUES (RES_ROWS (res ));
143
148
if (!VAL_NULL (values + 1 )) {
144
149
if (VAL_TYPE (values + 1 ) == DB_STR ) {
145
150
val .rs = VAL_STR (values + 1 );
@@ -152,13 +157,47 @@ int is_domain_local_pvar(struct sip_msg *msg, str* _host, pv_spec_t *pv)
152
157
LM_ERR ("Cannot set attributes value\n" );
153
158
}
154
159
}
160
+
155
161
domain_dbf .free_result (db_handle , res );
156
162
return 1 ;
157
163
}
158
- } else {
159
- return hash_table_lookup (msg , _host , pv );
160
164
}
161
165
166
+ domain_dbf .free_result (db_handle , res );
167
+ return -1 ;
168
+ }
169
+
170
+ /*
171
+ * Check if domain is local and store attributes in a pvar
172
+ */
173
+ int is_domain_local_pvar (struct sip_msg * msg , str * _host , pv_spec_t * pv )
174
+ {
175
+ char * next_domain ;
176
+ str lookup_domain = { _host -> s , _host -> len };
177
+
178
+ do {
179
+ if (db_mode == 0 ) {
180
+ if (db_table_lookup (msg , & lookup_domain , _host , pv ) == 1 )
181
+ return 1 ;
182
+ } else {
183
+ if (hash_table_lookup (msg , & lookup_domain , _host , pv ) == 1 )
184
+ return 1 ;
185
+ }
186
+
187
+ LM_DBG ("Realm '%.*s' is not local\n" , lookup_domain .len , ZSW (lookup_domain .s ));
188
+
189
+ next_domain = strchr (lookup_domain .s , '.' );
190
+
191
+ if (next_domain == NULL )
192
+ break ;
193
+
194
+ next_domain ++ ;
195
+
196
+ lookup_domain .len = lookup_domain .len - (next_domain - lookup_domain .s );
197
+ lookup_domain .s = next_domain ;
198
+ } while (strrchr (next_domain , '.' ) != NULL );
199
+
200
+ return -1 ;
162
201
}
163
202
164
203
/*
@@ -214,25 +253,27 @@ int w_is_domain_local(struct sip_msg* _msg, str *domain, pv_spec_t* _s2)
214
253
*/
215
254
int reload_domain_table ( void )
216
255
{
217
- db_key_t cols [2 ];
256
+ db_key_t cols [3 ];
218
257
db_res_t * res = NULL ;
219
258
db_row_t * row ;
220
259
db_val_t * val ;
221
260
222
261
struct domain_list * * new_hash_table ;
223
262
int i ;
263
+ int accept_subdomain ;
224
264
225
265
str domain , attrs ;
226
266
227
267
cols [0 ] = & domain_col ;
228
268
cols [1 ] = & domain_attrs_col ;
269
+ cols [2 ] = & domain_accept_subdomain_col ;
229
270
230
271
if (domain_dbf .use_table (db_handle , & domain_table ) < 0 ) {
231
272
LM_ERR ("Error while trying to use domain table\n" );
232
273
return -3 ;
233
274
}
234
275
235
- if (domain_dbf .query (db_handle , NULL , 0 , NULL , cols , 0 , 2 , 0 , & res ) < 0 ) {
276
+ if (domain_dbf .query (db_handle , NULL , 0 , NULL , cols , 0 , 3 , 0 , & res ) < 0 ) {
236
277
LM_ERR ("Error while querying database\n" );
237
278
return -3 ;
238
279
}
@@ -252,6 +293,7 @@ int reload_domain_table ( void )
252
293
253
294
for (i = 0 ; i < RES_ROW_N (res ); i ++ ) {
254
295
val = ROW_VALUES (row + i );
296
+
255
297
if (VAL_TYPE (val ) == DB_STRING ) {
256
298
domain .s = (char * )VAL_STRING (val );
257
299
domain .len = strlen (domain .s );
@@ -262,6 +304,7 @@ int reload_domain_table ( void )
262
304
domain_dbf .free_result (db_handle , res );
263
305
return -3 ;
264
306
}
307
+
265
308
if (VAL_NULL (val + 1 )) {
266
309
/* add a marker to determine whether the attributes exist or not */
267
310
attrs .len = 0 ;
@@ -278,9 +321,20 @@ int reload_domain_table ( void )
278
321
domain_dbf .free_result (db_handle , res );
279
322
return -3 ;
280
323
}
281
- LM_DBG ("Value: %s inserted into domain hash table\n" ,VAL_STRING (val ));
282
324
283
- if (hash_table_install (new_hash_table , & domain , & attrs )== -1 ){
325
+ if (VAL_NULL (val + 2 )) {
326
+ accept_subdomain = 0 ;
327
+ } else if (VAL_TYPE (val + 2 ) == DB_INT ) {
328
+ accept_subdomain = VAL_INT (val + 2 );
329
+ } else {
330
+ LM_ERR ("Database problem on accept_subdomain column\n" );
331
+ domain_dbf .free_result (db_handle , res );
332
+ return -3 ;
333
+ }
334
+
335
+ LM_DBG ("Value: %s with accept_subdomain %d inserted into domain hash table\n" , VAL_STRING (val ), accept_subdomain );
336
+
337
+ if (hash_table_install (new_hash_table , & domain , & attrs , accept_subdomain )== -1 ){
284
338
LM_ERR ("Hash table problem\n" );
285
339
domain_dbf .free_result (db_handle , res );
286
340
return -3 ;
0 commit comments