re_group.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2005-2007 Voice Sistem SRL
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * History:
  23. * --------
  24. * 2005-10-06 - created by bogdan
  25. */
  26. /**
  27. * \file
  28. * \brief Group membership module
  29. * \ingroup group
  30. * - Module: \ref group
  31. */
  32. #include <sys/types.h>
  33. #include <regex.h>
  34. #include "../../str.h"
  35. #include "../../mem/mem.h"
  36. #include "../../route_struct.h"
  37. #include "../../lvalue.h"
  38. #include "group_mod.h"
  39. #include "re_group.h"
  40. #include "group.h"
  41. /*! regular expression for groups */
  42. struct re_grp {
  43. regex_t re;
  44. int_str gid;
  45. struct re_grp *next;
  46. };
  47. /*! global regexp list */
  48. static struct re_grp *re_list = 0;
  49. /*!
  50. * \brief Create a group regexp and add it to the list
  51. * \param re regular expression string
  52. * \param gid group ID
  53. * \return 0 on success, -1 on failure
  54. */
  55. static int add_re(const char *re, int gid)
  56. {
  57. struct re_grp *rg;
  58. LM_DBG("adding <%s> with %d\n",re, gid);
  59. rg = (struct re_grp*)pkg_malloc(sizeof(struct re_grp));
  60. if (rg==0) {
  61. LM_ERR("no more pkg mem\n");
  62. goto error;
  63. }
  64. memset( rg, 0, sizeof(struct re_grp));
  65. if (regcomp(&rg->re, re, REG_EXTENDED|REG_ICASE|REG_NEWLINE) ) {
  66. LM_ERR("bad re %s\n", re);
  67. pkg_free(rg);
  68. goto error;
  69. }
  70. rg->gid.n = gid;
  71. rg->next = re_list;
  72. re_list = rg;
  73. return 0;
  74. error:
  75. return -1;
  76. }
  77. /*!
  78. * \brief Load regular expression rules from a database
  79. * \param table DB table
  80. * \return 0 on success, -1 on failure
  81. */
  82. int load_re( str *table )
  83. {
  84. db_key_t cols[2];
  85. db1_res_t* res = NULL;
  86. db_row_t* row;
  87. int n;
  88. cols[0] = &re_exp_column;
  89. cols[1] = &re_gid_column;
  90. if (group_dbf.use_table(group_dbh, table) < 0) {
  91. LM_ERR("failed to set table <%s>\n", table->s);
  92. goto error;
  93. }
  94. if (group_dbf.query(group_dbh, 0, 0, 0, cols, 0, 2, 0, &res) < 0) {
  95. LM_ERR("failed to query database\n");
  96. goto error;
  97. }
  98. for( n=0 ; n<RES_ROW_N(res) ; n++) {
  99. row = &res->rows[n];
  100. /* validate row */
  101. if (row->values[0].nul || row->values[0].type!=DB1_STRING) {
  102. LM_ERR("empty or non-string "
  103. "value for <%s>(re) column\n",re_exp_column.s);
  104. goto error1;
  105. }
  106. if (row->values[1].nul || row->values[1].type!=DB1_INT) {
  107. LM_ERR("empty or non-integer "
  108. "value for <%s>(gid) column\n",re_gid_column.s);
  109. goto error1;
  110. }
  111. if ( add_re( row->values[0].val.string_val,
  112. row->values[1].val.int_val)!=0 ) {
  113. LM_ERR("failed to add row\n");
  114. goto error1;
  115. }
  116. }
  117. LM_DBG("%d rules were loaded\n", n);
  118. group_dbf.free_result(group_dbh, res);
  119. return 0;
  120. error1:
  121. group_dbf.free_result(group_dbh, res);
  122. error:
  123. return -1;
  124. }
  125. /*!
  126. * \brief Get the user group and compare to the regexp list
  127. * \param req SIP message
  128. * \param user user string
  129. * \param avp AVP value
  130. * \return number of all matches (positive), -1 on errors or when not found
  131. */
  132. int get_user_group(struct sip_msg *req, char *user, char *avp)
  133. {
  134. static char uri_buf[MAX_URI_SIZE];
  135. str username;
  136. str domain;
  137. pv_spec_t *pvs;
  138. pv_value_t val;
  139. struct re_grp *rg;
  140. regmatch_t pmatch;
  141. char *c;
  142. int n;
  143. int* pi;
  144. if (get_username_domain( req, (group_check_p)user, &username, &domain)!=0){
  145. LM_ERR("failed to get username@domain\n");
  146. goto error;
  147. }
  148. if (username.s==NULL || username.len==0 ) {
  149. LM_DBG("no username part\n");
  150. return -1;
  151. }
  152. if ( 4 + username.len + 1 + domain.len + 1 > MAX_URI_SIZE ) {
  153. LM_ERR("URI to large!!\n");
  154. goto error;
  155. }
  156. pi=(int*)uri_buf;
  157. *pi = htonl(('s'<<24) + ('i'<<16) + ('p'<<8) + ':');
  158. c = uri_buf + 4;
  159. memcpy( c, username.s, username.len);
  160. c += username.len;
  161. *(c++) = '@';
  162. memcpy( c, domain.s, domain.len);
  163. c += domain.len;
  164. *c = 0;
  165. LM_DBG("getting groups for <%s>\n",uri_buf);
  166. pvs = (pv_spec_t*)avp;
  167. memset(&val, 0, sizeof(pv_value_t));
  168. val.flags = PV_VAL_INT|PV_TYPE_INT;
  169. /* check against all re groups */
  170. for( rg=re_list,n=0 ; rg ; rg=rg->next ) {
  171. if (regexec( &rg->re, uri_buf, 1, &pmatch, 0)==0) {
  172. LM_DBG("user matched to group %d!\n", rg->gid.n);
  173. /* match -> add the gid as AVP */
  174. val.ri = rg->gid.n;
  175. if(pvs->setf(req, &pvs->pvp, (int)EQ_T, &val)<0)
  176. {
  177. LM_ERR("setting PV AVP failed\n");
  178. goto error;
  179. }
  180. n++;
  181. /* continue? */
  182. if (multiple_gid==0)
  183. break;
  184. }
  185. }
  186. return n?n:-1;
  187. error:
  188. return -1;
  189. }