group.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  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. * 2003-02-25 - created by janakj
  25. * 2004-06-07 updated to the new DB api, added group_db_{bind,init,close,ver}
  26. * (andrei)
  27. */
  28. /**
  29. * \file
  30. * \brief Group membership module
  31. * \ingroup group
  32. * - Module: \ref group
  33. */
  34. #include <string.h>
  35. #include "../../dprint.h" /* Logging */
  36. #include "../../lib/srdb1/db.h" /* Generic database API */
  37. #include "../../ut.h"
  38. #include "../../parser/digest/digest.h" /* get_authorized_cred */
  39. #include "../../parser/hf.h" /* Header Field types */
  40. #include "../../parser/parse_from.h" /* From parser */
  41. #include "../../parser/parse_uri.h"
  42. #include "group.h"
  43. #include "group_mod.h" /* Module parameters */
  44. /*!
  45. * \brief Extract the username and domain from the SIP message
  46. *
  47. * Set the username and domain depending on the value of the SIP
  48. * message and the group check structure.
  49. * \param msg SIP message
  50. * \param gcp group check structure
  51. * \param username stored username
  52. * \param domain stored domain
  53. * \return 0 on success, -1 on failure
  54. */
  55. int get_username_domain(struct sip_msg *msg, group_check_p gcp,
  56. str *username, str *domain)
  57. {
  58. struct sip_uri puri;
  59. struct sip_uri *turi;
  60. struct hdr_field* h;
  61. struct auth_body* c = 0;
  62. pv_value_t value;
  63. turi = NULL;
  64. switch(gcp->id) {
  65. case 1: /* Request-URI */
  66. if(parse_sip_msg_uri(msg)<0) {
  67. LM_ERR("failed to get Request-URI\n");
  68. return -1;
  69. }
  70. turi = &msg->parsed_uri;
  71. break;
  72. case 2: /* To */
  73. if((turi=parse_to_uri(msg))==NULL) {
  74. LM_ERR("failed to get To URI\n");
  75. return -1;
  76. }
  77. break;
  78. case 3: /* From */
  79. if((turi=parse_from_uri(msg))==NULL) {
  80. LM_ERR("failed to get From URI\n");
  81. return -1;
  82. }
  83. break;
  84. case 4: /* Credentials */
  85. get_authorized_cred( msg->authorization, &h);
  86. if (!h) {
  87. get_authorized_cred( msg->proxy_auth, &h);
  88. if (!h) {
  89. LM_ERR("no authorized credentials found "
  90. "(error in scripts)\n");
  91. return -1;
  92. }
  93. }
  94. c = (auth_body_t*)(h->parsed);
  95. break;
  96. case 5: /* AVP spec */
  97. if(pv_get_spec_value( msg, &gcp->sp, &value)!=0
  98. || value.flags&PV_VAL_NULL || value.rs.len<=0)
  99. {
  100. LM_ERR("no AVP found (error in scripts)\n");
  101. return -1;
  102. }
  103. if (parse_uri(value.rs.s, value.rs.len, &puri) < 0) {
  104. LM_ERR("failed to parse URI <%.*s>\n",value.rs.len, value.rs.s);
  105. return -1;
  106. }
  107. turi = &puri;
  108. break;
  109. default: {
  110. LM_ERR("incorrect check id %d\n", gcp->id);
  111. return -1;
  112. }
  113. }
  114. if (gcp->id != 4) {
  115. *username = turi->user;
  116. *domain = turi->host;
  117. } else {
  118. *username = c->digest.username.user;
  119. *domain = *(GET_REALM(&c->digest));
  120. }
  121. return 0;
  122. }
  123. /*!
  124. * \brief Check if username in specified header field is in a table
  125. * \param _msg SIP message
  126. * \param _hf Header field
  127. * \param _grp checked table
  128. * \return 1 on success, negative on failure
  129. */
  130. int is_user_in(struct sip_msg* _msg, char* _hf, char* _grp)
  131. {
  132. db_key_t keys[3];
  133. db_val_t vals[3];
  134. db_key_t col[1];
  135. db1_res_t* res = NULL;
  136. keys[0] = &user_column;
  137. keys[1] = &group_column;
  138. keys[2] = &domain_column;
  139. col[0] = &group_column;
  140. if ( get_username_domain( _msg, (group_check_p)_hf, &(VAL_STR(vals)),
  141. &(VAL_STR(vals+2)))!=0) {
  142. LM_ERR("failed to get username@domain\n");
  143. return -1;
  144. }
  145. if (VAL_STR(vals).s==NULL || VAL_STR(vals).len==0 ) {
  146. LM_DBG("no username part\n");
  147. return -1;
  148. }
  149. VAL_TYPE(vals) = VAL_TYPE(vals + 1) = VAL_TYPE(vals + 2) = DB1_STR;
  150. VAL_NULL(vals) = VAL_NULL(vals + 1) = VAL_NULL(vals + 2) = 0;
  151. VAL_STR(vals + 1) = *((str*)_grp);
  152. if (group_dbf.use_table(group_dbh, &table) < 0) {
  153. LM_ERR("failed to use_table\n");
  154. return -5;
  155. }
  156. if (group_dbf.query(group_dbh, keys, 0, vals, col, (use_domain) ? (3): (2),
  157. 1, 0, &res) < 0) {
  158. LM_ERR("failed to query database\n");
  159. return -5;
  160. }
  161. if (RES_ROW_N(res) == 0) {
  162. LM_DBG("user is not in group '%.*s'\n",
  163. ((str*)_grp)->len, ZSW(((str*)_grp)->s));
  164. group_dbf.free_result(group_dbh, res);
  165. return -6;
  166. } else {
  167. LM_DBG("user is in group '%.*s'\n",
  168. ((str*)_grp)->len, ZSW(((str*)_grp)->s));
  169. group_dbf.free_result(group_dbh, res);
  170. return 1;
  171. }
  172. }
  173. /*!
  174. * \brief Initialize the DB connection
  175. * \param db_url database URL
  176. * \return 0 on success, -1 on failure
  177. */
  178. int group_db_init(const str* db_url)
  179. {
  180. if (group_dbf.init==0){
  181. LM_CRIT("null dbf \n");
  182. goto error;
  183. }
  184. group_dbh=group_dbf.init(db_url);
  185. if (group_dbh==0){
  186. LM_ERR("unable to connect to the database\n");
  187. goto error;
  188. }
  189. return 0;
  190. error:
  191. return -1;
  192. }
  193. /*!
  194. * \brief Bind the DB connection
  195. * \param db_url database URL
  196. * \return 0 on success, -1 on failure
  197. */
  198. int group_db_bind(const str* db_url)
  199. {
  200. if (db_bind_mod(db_url, &group_dbf)<0){
  201. LM_ERR("unable to bind to the database module\n");
  202. return -1;
  203. }
  204. if (!DB_CAPABILITY(group_dbf, DB_CAP_QUERY)) {
  205. LM_ERR("database module does not implement 'query' function\n");
  206. return -1;
  207. }
  208. return 0;
  209. }
  210. /*!
  211. * \brief Close the DB connection
  212. */
  213. void group_db_close(void)
  214. {
  215. if (group_dbh && group_dbf.close){
  216. group_dbf.close(group_dbh);
  217. group_dbh=0;
  218. }
  219. }