cpl_db.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser 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. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. /*
  28. * History:
  29. * --------
  30. * 2004-06-06 updated to the new DB api (andrei)
  31. */
  32. #include "../../mem/shm_mem.h"
  33. #include "../../lib/srdb2/db.h"
  34. #include "../../dprint.h"
  35. #include "cpl_db.h"
  36. static db_ctx_t* ctx = NULL;
  37. static db_cmd_t* get_script;
  38. static db_cmd_t* write_script;
  39. static db_cmd_t* delete_user;
  40. void cpl_db_close()
  41. {
  42. if (delete_user) db_cmd_free(delete_user);
  43. delete_user = NULL;
  44. if (write_script) db_cmd_free(write_script);
  45. write_script = NULL;
  46. if (get_script) db_cmd_free(get_script);
  47. get_script = NULL;
  48. if (ctx) {
  49. db_disconnect(ctx);
  50. db_ctx_free(ctx);
  51. ctx = NULL;
  52. }
  53. }
  54. int cpl_db_init(char* db_url, char* db_table)
  55. {
  56. db_fld_t cols[] = {
  57. {.name = "cpl_bin", .type = DB_BLOB},
  58. {.name = "cpl_xml", .type = DB_STR},
  59. {.name = 0}
  60. };
  61. db_fld_t match[] = {
  62. {.name = "uid", .type = DB_CSTR},
  63. {.name = 0}
  64. };
  65. db_fld_t vals[] = {
  66. {.name = "uid", .type = DB_CSTR},
  67. {.name = "cpl_bin", .type = DB_BLOB},
  68. {.name = "cpl_xml", .type = DB_STR },
  69. {.name = 0}
  70. };
  71. ctx = db_ctx("cpl-c");
  72. if (ctx == NULL) goto error;
  73. if (db_add_db(ctx, db_url) < 0) goto error;
  74. if (db_connect(ctx) < 0) goto error;
  75. get_script = db_cmd(DB_GET, ctx, db_table, cols, match, NULL);
  76. if (!get_script) goto error;
  77. write_script = db_cmd(DB_PUT, ctx, db_table, NULL, NULL, vals);
  78. if (!write_script) goto error;
  79. delete_user = db_cmd(DB_DEL, ctx, db_table, NULL, match, NULL);
  80. if (!delete_user) goto error;
  81. return 0;
  82. error:
  83. ERR("cpl-c: Error while initializing db layer\n");
  84. cpl_db_close();
  85. return -1;
  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 *user, str *script, int bin)
  93. {
  94. db_res_t* res = 0;
  95. db_rec_t* rec;
  96. int i;
  97. if (bin) i = 0;
  98. else i = 1;
  99. get_script->match[0].v.cstr = user->s;
  100. DBG("DEBUG:get_user_script: fetching script for user <%s>\n",user->s);
  101. if (db_exec(&res, get_script) < 0) {
  102. LOG(L_ERR,"ERROR:cpl-c:get_user_script: db_query failed\n");
  103. goto error;
  104. }
  105. if (!res || !(rec = db_first(res))) {
  106. DBG("DEBUG:get_user_script: user <%.*s> not found in db -> probably "
  107. "he has no script\n",user->len, user->s);
  108. script->s = 0;
  109. script->len = 0;
  110. } else {
  111. if (rec->fld[i].flags & DB_NULL) {
  112. DBG("DEBUG:get_user_script: user <%.*s> has a NULL script\n",
  113. user->len, user->s);
  114. script->s = 0;
  115. script->len = 0;
  116. } else {
  117. DBG("DEBUG:get_user_script: we got the script len=%d\n",
  118. rec->fld[i].v.blob.len);
  119. script->len = rec->fld[i].v.blob.len;
  120. script->s = shm_malloc( script->len );
  121. if (!script->s) {
  122. LOG(L_ERR,"ERROR:cpl-c:get_user_script: no free sh_mem\n");
  123. goto error;
  124. }
  125. memcpy( script->s, rec->fld[i].v.blob.s,
  126. script->len);
  127. }
  128. }
  129. if (res) db_res_free(res);
  130. return 1;
  131. error:
  132. if (res)
  133. db_res_free(res);
  134. script->s = 0;
  135. script->len = 0;
  136. return -1;
  137. }
  138. /* inserts into database a cpl script in XML format(xml) along with its binary
  139. * format (bin)
  140. * Returns: 1 - success
  141. * -1 - error
  142. */
  143. int write_to_db(char *usr, str *xml, str *bin)
  144. {
  145. write_script->vals[0].v.cstr = usr;
  146. write_script->vals[1].v.blob = *bin;
  147. write_script->vals[2].v.lstr = *xml;
  148. /* No need to do update/insert here, the db layer does that
  149. * automatically without the need to query the db
  150. */
  151. if (db_exec(NULL, write_script) < 0) {
  152. ERR("cpl-c: Error while writing script into database\n");
  153. return -1;
  154. }
  155. return 0;
  156. }
  157. /* delete from database the entity record for a given user - if a user has no
  158. * script, he will be removed completely from db; users without script are not
  159. * allowed into db ;-)
  160. * Returns: 1 - success
  161. * -1 - error
  162. */
  163. int rmv_from_db(char *usr)
  164. {
  165. delete_user->match[0].v.cstr = usr;
  166. if (db_exec(NULL, delete_user) < 0) {
  167. LOG(L_ERR,"ERROR:cpl-c:rmv_from_db: error when deleting script for "
  168. "user \"%s\"\n",usr);
  169. return -1;
  170. }
  171. return 1;
  172. }