cpl_db.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. /*
  23. * History:
  24. * --------
  25. * 2004-06-06 updated to the new DB api (andrei)
  26. */
  27. #include "../../mem/shm_mem.h"
  28. #include "../../lib/srdb1/db.h"
  29. #include "../../dprint.h"
  30. #include "../../str.h"
  31. #include "cpl_db.h"
  32. #define TABLE_VERSION 1
  33. static db1_con_t* db_hdl=0;
  34. static db_func_t cpl_dbf;
  35. str cpl_username_col = str_init("username");
  36. str cpl_domain_col = str_init("domain");
  37. str cpl_xml_col = str_init("cpl_xml");
  38. str cpl_bin_col = str_init("cpl_bin");
  39. int cpl_db_bind(const str* db_url, const str *db_table)
  40. {
  41. if (db_bind_mod(db_url, &cpl_dbf )) {
  42. LM_CRIT("cannot bind to database module! "
  43. "Did you forget to load a database module ?\n");
  44. return -1;
  45. }
  46. /* CPL module uses all database functions */
  47. if (!DB_CAPABILITY(cpl_dbf, DB_CAP_ALL)) {
  48. LM_CRIT("Database modules does not "
  49. "provide all functions needed by cpl-c module\n");
  50. return -1;
  51. }
  52. if ( cpl_db_init( db_url, db_table) )
  53. return -1;
  54. if(db_check_table_version(&cpl_dbf, db_hdl, db_table, TABLE_VERSION) < 0) {
  55. LM_ERR("error during table version check.\n");
  56. cpl_db_close();
  57. return -1;
  58. }
  59. cpl_db_close();
  60. return 0;
  61. }
  62. int cpl_db_init(const str* db_url, const str* db_table)
  63. {
  64. if (cpl_dbf.init==0){
  65. LM_CRIT("BUG - unbound database module\n");
  66. return -1;
  67. }
  68. db_hdl=cpl_dbf.init(db_url);
  69. if (db_hdl==0){
  70. LM_CRIT("cannot initialize database connection\n");
  71. return -1;
  72. }
  73. if (cpl_dbf.use_table(db_hdl, db_table)<0) {
  74. LM_CRIT("cannot select table \"%.*s\"\n",db_table->len, db_table->s);
  75. cpl_db_close();
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. void cpl_db_close(void)
  81. {
  82. if (db_hdl && cpl_dbf.close){
  83. cpl_dbf.close(db_hdl);
  84. db_hdl=0;
  85. }
  86. }
  87. /* gets from database the cpl script in binary format; the returned script is
  88. * allocated in shared memory
  89. * Returns: 1 - success
  90. * -1 - error
  91. */
  92. int get_user_script(str *username, str *domain, str *script, str* key)
  93. {
  94. db_key_t keys_cmp[2];
  95. db_key_t keys_ret[1];
  96. db_val_t vals[2];
  97. db1_res_t *res = NULL;
  98. int n;
  99. keys_cmp[0] = &cpl_username_col;
  100. keys_cmp[1] = &cpl_domain_col;
  101. keys_ret[0] = key;
  102. LM_DBG("fetching script for user <%.*s>\n",
  103. username->len,username->s);
  104. vals[0].type = DB1_STR;
  105. vals[0].nul = 0;
  106. vals[0].val.str_val = *username;
  107. n = 1;
  108. if (domain) {
  109. vals[1].type = DB1_STR;
  110. vals[1].nul = 0;
  111. vals[1].val.str_val = *domain;
  112. n++;
  113. }
  114. if (cpl_dbf.query(db_hdl, keys_cmp, 0, vals, keys_ret, n, 1, NULL, &res)
  115. < 0){
  116. LM_ERR("db_query failed\n");
  117. goto error;
  118. }
  119. if (res->n==0) {
  120. LM_DBG("user <%.*s> not found in db -> probably "
  121. "he has no script\n",username->len, username->s);
  122. script->s = 0;
  123. script->len = 0;
  124. } else {
  125. if (res->rows[0].values[0].nul) {
  126. LM_DBG("user <%.*s> has a NULL script\n",
  127. username->len, username->s);
  128. script->s = 0;
  129. script->len = 0;
  130. } else {
  131. LM_DBG("we got the script len=%d\n",
  132. res->rows[0].values[0].val.blob_val.len);
  133. script->len = res->rows[0].values[0].val.blob_val.len;
  134. script->s = shm_malloc( script->len );
  135. if (!script->s) {
  136. LM_ERR("no free sh_mem\n");
  137. goto error;
  138. }
  139. memcpy( script->s, res->rows[0].values[0].val.blob_val.s,
  140. script->len);
  141. }
  142. }
  143. cpl_dbf.free_result( db_hdl, res);
  144. return 1;
  145. error:
  146. if (res)
  147. cpl_dbf.free_result( db_hdl, res);
  148. script->s = 0;
  149. script->len = 0;
  150. return -1;
  151. }
  152. /* inserts into database a cpl script in XML format(xml) along with its binary
  153. * format (bin)
  154. * Returns: 1 - success
  155. * -1 - error
  156. */
  157. int write_to_db(str *username, str *domain, str *xml, str *bin)
  158. {
  159. db_key_t keys[4];
  160. db_val_t vals[4];
  161. db1_res_t *res = NULL;
  162. int n;
  163. /* lets see if the user is already in database */
  164. keys[2] = &cpl_username_col;
  165. vals[2].type = DB1_STR;
  166. vals[2].nul = 0;
  167. vals[2].val.str_val = *username;
  168. n = 1;
  169. if (domain) {
  170. keys[3] = &cpl_domain_col;
  171. vals[3].type = DB1_STR;
  172. vals[3].nul = 0;
  173. vals[3].val.str_val = *domain;
  174. n++;
  175. }
  176. if (cpl_dbf.query(db_hdl, keys+2, 0, vals+2, keys+2, n, 1, NULL, &res)<0) {
  177. LM_ERR("db_query failed\n");
  178. goto error;
  179. }
  180. if (res->n>1) {
  181. LM_ERR("Inconsistent CPL database:"
  182. " %d records for user %.*s\n",res->n,username->len,username->s);
  183. goto error;
  184. }
  185. /* cpl text */
  186. keys[0] = &cpl_xml_col;
  187. vals[0].type = DB1_BLOB;
  188. vals[0].nul = 0;
  189. vals[0].val.blob_val.s = xml->s;
  190. vals[0].val.blob_val.len = xml->len;
  191. n++;
  192. /* cpl bin */
  193. keys[1] = &cpl_bin_col;
  194. vals[1].type = DB1_BLOB;
  195. vals[1].nul = 0;
  196. vals[1].val.blob_val.s = bin->s;
  197. vals[1].val.blob_val.len = bin->len;
  198. n++;
  199. /* insert or update ? */
  200. if (res->n==0) {
  201. LM_DBG("no user %.*s in CPL database->insert\n",
  202. username->len,username->s);
  203. if (cpl_dbf.insert(db_hdl, keys, vals, n) < 0) {
  204. LM_ERR("insert failed !\n");
  205. goto error;
  206. }
  207. } else {
  208. LM_DBG("user %.*s already in CPL database ->"
  209. " update\n",username->len,username->s);
  210. if (cpl_dbf.update(db_hdl, keys+2, 0, vals+2, keys, vals, n-2, 2) < 0) {
  211. LM_ERR("update failed !\n");
  212. goto error;
  213. }
  214. }
  215. return 1;
  216. error:
  217. return -1;
  218. }
  219. /* delete from database the entity record for a given user - if a user has no
  220. * script, he will be removed completely from db; users without script are not
  221. * allowed into db ;-)
  222. * Returns: 1 - success
  223. * -1 - error
  224. */
  225. int rmv_from_db(str *username, str *domain)
  226. {
  227. db_key_t keys[2];
  228. db_val_t vals[2];
  229. int n;
  230. /* username */
  231. keys[0] = &cpl_username_col;
  232. vals[0].type = DB1_STR;
  233. vals[0].nul = 0;
  234. vals[0].val.str_val = *username;
  235. n = 1;
  236. if (domain) {
  237. keys[1] = &cpl_domain_col;
  238. vals[1].type = DB1_STR;
  239. vals[1].nul = 0;
  240. vals[1].val.str_val = *domain;
  241. n++;
  242. }
  243. if (cpl_dbf.delete(db_hdl, keys, NULL, vals, n) < 0) {
  244. LM_ERR("failed to delete script for "
  245. "user \"%.*s\"\n",username->len,username->s);
  246. return -1;
  247. }
  248. return 1;
  249. }