group_mod.c

Go to the documentation of this file.
00001 /*
00002  * $Id: group_mod.c 5299 2008-12-04 18:12:33Z henningw $ 
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  * History:
00023  * --------
00024  *  2003-02-25 - created by janakj
00025  *  2003-03-11 - New module interface (janakj)
00026  *  2003-03-16 - flags export parameter added (janakj)
00027  *  2003-03-19  all mallocs/frees replaced w/ pkg_malloc/pkg_free
00028  *  2003-04-05  default_uri #define used (jiri)
00029  *  2004-06-07  updated to the new DB api: calls to group_db_* (andrei)
00030  *  2005-10-06 - added support for regexp-based groups (bogdan)
00031  */
00032 
00033 /**
00034  * \file
00035  * \brief Group membership module
00036  * \ingroup group
00037  * - Module: \ref group
00038  */
00039 
00040 /*!
00041  * \defgroup group GROUP :: The Kamailio group Module
00042  * This module provides functions to check if a certain user belongs to a
00043  * group. This group definitions are read from a DB table.
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  * \brief Module destroy function prototype
00067  */
00068 static void destroy(void);
00069 
00070 
00071 /*!
00072  * \brief Module child-init function prototype
00073  */
00074 static int child_init(int rank);
00075 
00076 
00077 /*!
00078  * \brief Module initialization function prototype
00079  */
00080 static int mod_init(void);
00081 
00082 /*! Header field fixup */
00083 static int hf_fixup(void** param, int param_no);
00084 
00085 /*! get user group ID fixup */
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  * Module parameter variables
00112  */
00113 static str db_url = {DEFAULT_RODB_URL, DEFAULT_RODB_URL_LEN};
00114 /*! Table name where group definitions are stored */
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 /* table and columns used for regular expression-based groups */
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 /* DB functions and handlers */
00128 db_func_t group_dbf;
00129 db_con_t* group_dbh = 0;
00130 
00131 
00132 /*!
00133  * Exported functions
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  * Exported parameters
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  * Module interface
00164  */
00165 struct module_exports exports = {
00166    "group", 
00167    DEFAULT_DLFLAGS, /* dlopen flags */
00168    cmds,       /* Exported functions */
00169    params,     /* Exported parameters */
00170    0,          /* exported statistics */
00171    0,          /* exported MI functions */
00172    0,          /* exported pseudo-variables */
00173    0,          /* extra processes */
00174    mod_init,   /* module initialization function */
00175    0,          /* response function */
00176    destroy,    /* destroy function */
00177    child_init  /* child initialization function */
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    /* Calculate lengths */
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    /* Find a database module */
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    /* check version for group table */
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       /* check version for group re_group table */
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  * \brief Convert HF description string to hdr_field pointer
00241  *
00242  * Convert a header field description string to hdr_field structure
00243  * Supported strings: "Request-URI", "To", "From", "Credentials"
00244  * \param str1 header field description string
00245  * \return hdr_field structure on success, NULL on failure
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    /* do not free all the time, needed by pseudo-variable spec */
00280    if(gcp->id!=5)
00281       pkg_free(str1);
00282 
00283    return gcp;
00284 }
00285 
00286 
00287 /*!
00288  * \brief Header fixup function
00289  * \param param fixed parameter
00290  * \param param_no number of parameters
00291  * \return 0 on success, negative on failure
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  * \brief Group ID fixup
00319  * \param param fixed parameter
00320  * \param param_no number of parameters
00321  * \return 0 on success, negative on failure
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 }

Generated on Wed May 23 06:00:45 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6