00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 #include <string.h>
00050 #include "../../sr_module.h"
00051 #include "../../dprint.h"
00052 #include "../../ut.h"
00053 #include "../../error.h"
00054 #include "../../mem/mem.h"
00055 #include "../../usr_avp.h"
00056 #include "group_mod.h"
00057 #include "group.h"
00058 #include "re_group.h"
00059
00060 MODULE_VERSION
00061
00062 #define TABLE_VERSION 2
00063 #define RE_TABLE_VERSION 1
00064
00065
00066
00067
00068 static void destroy(void);
00069
00070
00071
00072
00073
00074 static int child_init(int rank);
00075
00076
00077
00078
00079
00080 static int mod_init(void);
00081
00082
00083 static int hf_fixup(void** param, int param_no);
00084
00085
00086 static int get_gid_fixup(void** param, int param_no);
00087
00088
00089 #define TABLE "grp"
00090 #define TABLE_LEN (sizeof(TABLE) - 1)
00091
00092 #define USER_COL "username"
00093 #define USER_COL_LEN (sizeof(USER_COL) - 1)
00094
00095 #define DOMAIN_COL "domain"
00096 #define DOMAIN_COL_LEN (sizeof(DOMAIN_COL) - 1)
00097
00098 #define GROUP_COL "grp"
00099 #define GROUP_COL_LEN (sizeof(GROUP_COL) - 1)
00100
00101 #define RE_TABLE "re_grp"
00102 #define RE_TABLE_LEN (sizeof(TABLE) - 1)
00103
00104 #define RE_EXP_COL "reg_exp"
00105 #define RE_EXP_COL_LEN (sizeof(USER_COL) - 1)
00106
00107 #define RE_GID_COL "group_id"
00108 #define RE_GID_COL_LEN (sizeof(DOMAIN_COL) - 1)
00109
00110
00111
00112
00113 static str db_url = {DEFAULT_RODB_URL, DEFAULT_RODB_URL_LEN};
00114
00115 str table = {TABLE, TABLE_LEN};
00116 str user_column = {USER_COL, USER_COL_LEN};
00117 str domain_column = {DOMAIN_COL, DOMAIN_COL_LEN};
00118 str group_column = {GROUP_COL, GROUP_COL_LEN};
00119 int use_domain = 0;
00120
00121
00122 str re_table = {0, 0};
00123 str re_exp_column = {RE_EXP_COL, RE_EXP_COL_LEN};
00124 str re_gid_column = {RE_GID_COL, RE_GID_COL_LEN};
00125 int multiple_gid = 1;
00126
00127
00128 db_func_t group_dbf;
00129 db_con_t* group_dbh = 0;
00130
00131
00132
00133
00134
00135 static cmd_export_t cmds[] = {
00136 {"is_user_in", (cmd_function)is_user_in, 2, hf_fixup, 0,
00137 REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
00138 {"get_user_group", (cmd_function)get_user_group, 2, get_gid_fixup, 0,
00139 REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
00140 {0, 0, 0, 0, 0, 0}
00141 };
00142
00143
00144
00145
00146
00147 static param_export_t params[] = {
00148 {"db_url", STR_PARAM, &db_url.s },
00149 {"table", STR_PARAM, &table.s },
00150 {"user_column", STR_PARAM, &user_column.s },
00151 {"domain_column", STR_PARAM, &domain_column.s},
00152 {"group_column", STR_PARAM, &group_column.s },
00153 {"use_domain", INT_PARAM, &use_domain },
00154 {"re_table", STR_PARAM, &re_table.s },
00155 {"re_exp_column", STR_PARAM, &re_exp_column.s},
00156 {"re_gid_column", STR_PARAM, &re_gid_column.s},
00157 {"multiple_gid", INT_PARAM, &multiple_gid },
00158 {0, 0, 0}
00159 };
00160
00161
00162
00163
00164
00165 struct module_exports exports = {
00166 "group",
00167 DEFAULT_DLFLAGS,
00168 cmds,
00169 params,
00170 0,
00171 0,
00172 0,
00173 0,
00174 mod_init,
00175 0,
00176 destroy,
00177 child_init
00178 };
00179
00180
00181 static int child_init(int rank)
00182 {
00183 return group_db_init(&db_url);
00184 }
00185
00186
00187 static int mod_init(void)
00188 {
00189
00190 db_url.len = strlen(db_url.s);
00191 table.len = strlen(table.s);
00192 user_column.len = strlen(user_column.s);
00193 domain_column.len = strlen(domain_column.s);
00194 group_column.len = strlen(group_column.s);
00195
00196 re_table.len = (re_table.s && re_table.s[0])?strlen(re_table.s):0;
00197 re_exp_column.len = strlen(re_exp_column.s);
00198 re_gid_column.len = strlen(re_gid_column.s);
00199
00200
00201 if (group_db_bind(&db_url)) {
00202 return -1;
00203 }
00204
00205 if (group_db_init(&db_url) < 0 ){
00206 LM_ERR("unable to open database connection\n");
00207 return -1;
00208 }
00209
00210
00211 if (db_check_table_version(&group_dbf, group_dbh, &table, TABLE_VERSION) < 0) {
00212 LM_ERR("error during group table version check.\n");
00213 return -1;
00214 }
00215
00216 if (re_table.len) {
00217
00218 if (db_check_table_version(&group_dbf, group_dbh, &re_table, RE_TABLE_VERSION) < 0) {
00219 LM_ERR("error during re_group table version check.\n");
00220 return -1;
00221 }
00222 if (load_re( &re_table )!=0 ) {
00223 LM_ERR("failed to load <%s> table\n", re_table.s);
00224 return -1;
00225 }
00226 }
00227
00228 group_db_close();
00229 return 0;
00230 }
00231
00232
00233 static void destroy(void)
00234 {
00235 group_db_close();
00236 }
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 static group_check_p get_hf( char *str1)
00248 {
00249 group_check_p gcp=NULL;
00250 str s;
00251
00252 gcp = (group_check_p)pkg_malloc(sizeof(group_check_t));
00253 if(gcp == NULL) {
00254 LM_ERR("no pkg more memory\n");
00255 return 0;
00256 }
00257 memset(gcp, 0, sizeof(group_check_t));
00258
00259 if (!strcasecmp( str1, "Request-URI")) {
00260 gcp->id = 1;
00261 } else if (!strcasecmp( str1, "To")) {
00262 gcp->id = 2;
00263 } else if (!strcasecmp( str1, "From")) {
00264 gcp->id = 3;
00265 } else if (!strcasecmp( str1, "Credentials")) {
00266 gcp->id = 4;
00267 } else {
00268 s.s = str1; s.len = strlen(s.s);
00269 if(pv_parse_spec( &s, &gcp->sp)==NULL
00270 || gcp->sp.type!=PVT_AVP)
00271 {
00272 LM_ERR("unsupported User Field identifier\n");
00273 pkg_free( gcp );
00274 return 0;
00275 }
00276 gcp->id = 5;
00277 }
00278
00279
00280 if(gcp->id!=5)
00281 pkg_free(str1);
00282
00283 return gcp;
00284 }
00285
00286
00287
00288
00289
00290
00291
00292
00293 static int hf_fixup(void** param, int param_no)
00294 {
00295 void* ptr;
00296 str* s;
00297
00298 if (param_no == 1) {
00299 ptr = *param;
00300 if ( (*param = (void*)get_hf( ptr ))==0 )
00301 return E_UNSPEC;
00302 } else if (param_no == 2) {
00303 s = (str*)pkg_malloc(sizeof(str));
00304 if (!s) {
00305 LM_ERR("no pkg memory left\n");
00306 return E_UNSPEC;
00307 }
00308 s->s = (char*)*param;
00309 s->len = strlen(s->s);
00310 *param = (void*)s;
00311 }
00312
00313 return 0;
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323 static int get_gid_fixup(void** param, int param_no)
00324 {
00325 pv_spec_t *sp;
00326 void *ptr;
00327 str name;
00328
00329 if (param_no == 1) {
00330 ptr = *param;
00331 if ( (*param = (void*)get_hf( ptr ))==0 )
00332 return E_UNSPEC;
00333 } else if (param_no == 2) {
00334 name.s = (char*)*param;
00335 name.len = strlen(name.s);
00336 sp = (pv_spec_t*)pkg_malloc(sizeof(pv_spec_t));
00337 if (sp == NULL) {
00338 LM_ERR("no more pkg memory\n");
00339 return E_UNSPEC;
00340 }
00341 if(pv_parse_spec(&name, sp)==NULL || sp->type!=PVT_AVP)
00342 {
00343 LM_ERR("bad AVP spec <%s>\n", name.s);
00344 pv_spec_free(sp);
00345 return E_UNSPEC;
00346 }
00347
00348 *param = sp;
00349 }
00350
00351 return 0;
00352 }