pg_uri.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * $Id$
  3. *
  4. * PostgreSQL Database Driver for SER
  5. *
  6. * Portions Copyright (C) 2001-2003 FhG FOKUS
  7. * Copyright (C) 2003 August.Net Services, LLC
  8. * Portions Copyright (C) 2005-2008 iptelorg GmbH
  9. *
  10. * This file is part of SER, a free SIP server.
  11. *
  12. * SER is free software; you can redistribute it and/or modify it under the
  13. * terms of the GNU General Public License as published by the Free Software
  14. * Foundation; either version 2 of the License, or (at your option) any later
  15. * version
  16. *
  17. * For a license to use the ser software under conditions other than those
  18. * described here, or to purchase support for this software, please contact
  19. * iptel.org by e-mail at the following addresses: [email protected]
  20. *
  21. * SER is distributed in the hope that it will be useful, but WITHOUT ANY
  22. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  23. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  24. * details.
  25. *
  26. * You should have received a copy of the GNU General Public License along
  27. * with this program; if not, write to the Free Software Foundation, Inc., 59
  28. * Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. /** \addtogroup postgres
  31. * @{
  32. */
  33. /** \file
  34. * The implementation of parser parsing postgres://.. URIs.
  35. */
  36. #include "pg_uri.h"
  37. #include "../../dprint.h"
  38. #include "../../mem/mem.h"
  39. #include "../../ut.h"
  40. #include "../../lib/srdb2/db_gen.h"
  41. #include <stdlib.h>
  42. #include <string.h>
  43. /** compare s1 & s2 with a function f (which should return 0 if ==);
  44. * s1 & s2 can be null
  45. * return 0 if match, 1 if not
  46. */
  47. #define cmpstr(s1, s2, f) \
  48. ((s1)!=(s2)) && ((s1)==0 || (s2)==0 || (f)((s1), (s2))!=0)
  49. /** Compare two connection URIs */
  50. static unsigned char pg_uri_cmp(db_uri_t* uri1, db_uri_t* uri2)
  51. {
  52. struct pg_uri* puri1, *puri2;
  53. if (!uri1 || !uri2) return 0;
  54. puri1 = DB_GET_PAYLOAD(uri1);
  55. puri2 = DB_GET_PAYLOAD(uri2);
  56. if (puri1->port != puri2->port) return 0;
  57. if (cmpstr(puri1->username, puri2->username, strcmp)) return 0;
  58. if (cmpstr(puri1->password, puri2->password, strcmp)) return 0;
  59. if (cmpstr(puri1->host, puri2->host, strcasecmp)) return 0;
  60. if (cmpstr(puri1->database, puri2->database, strcmp)) return 0;
  61. return 1;
  62. }
  63. /** Duplicate a string
  64. */
  65. static int dupl_string(char** dst, const char* begin, const char* end)
  66. {
  67. if (*dst) pkg_free(*dst);
  68. *dst = pkg_malloc(end - begin + 1);
  69. if ((*dst) == NULL) {
  70. return -1;
  71. }
  72. memcpy(*dst, begin, end - begin);
  73. (*dst)[end - begin] = '\0';
  74. return 0;
  75. }
  76. /** Parses postgres URI of form
  77. * //[username[:password]@]hostname[:port]/database
  78. *
  79. * Returns 0 if parsing was successful and -1 otherwise
  80. */
  81. static int parse_postgres_uri(struct pg_uri* res, str* uri)
  82. {
  83. #define SHORTEST_DB_URL "//a/b"
  84. #define SHORTEST_DB_URL_LEN (sizeof(SHORTEST_DB_URL) - 1)
  85. enum state {
  86. ST_SLASH1, /* First slash */
  87. ST_SLASH2, /* Second slash */
  88. ST_USER_HOST, /* Username or hostname */
  89. ST_PASS_PORT, /* Password or port part */
  90. ST_HOST, /* Hostname part */
  91. ST_PORT, /* Port part */
  92. ST_DB /* Database part */
  93. };
  94. enum state st;
  95. int i;
  96. const char* begin;
  97. char* prev_token;
  98. prev_token = 0;
  99. if (!res || !uri) {
  100. goto err;
  101. }
  102. if (uri->len < SHORTEST_DB_URL_LEN) {
  103. goto err;
  104. }
  105. st = ST_SLASH1;
  106. begin = uri->s;
  107. for(i = 0; i < uri->len; i++) {
  108. switch(st) {
  109. case ST_SLASH1:
  110. switch(uri->s[i]) {
  111. case '/':
  112. st = ST_SLASH2;
  113. break;
  114. default:
  115. goto err;
  116. }
  117. break;
  118. case ST_SLASH2:
  119. switch(uri->s[i]) {
  120. case '/':
  121. st = ST_USER_HOST;
  122. begin = uri->s + i + 1;
  123. break;
  124. default:
  125. goto err;
  126. }
  127. break;
  128. case ST_USER_HOST:
  129. switch(uri->s[i]) {
  130. case '@':
  131. st = ST_HOST;
  132. if (dupl_string(&res->username, begin, uri->s + i) < 0) goto err;
  133. begin = uri->s + i + 1;
  134. break;
  135. case ':':
  136. st = ST_PASS_PORT;
  137. if (dupl_string(&prev_token, begin, uri->s + i) < 0) goto err;
  138. begin = uri->s + i + 1;
  139. break;
  140. case '/':
  141. if (memchr(uri->s + i + 1, '/', uri->len - i - 1) != NULL)
  142. break;
  143. if (dupl_string(&res->host, begin, uri->s + i) < 0) goto err;
  144. if (dupl_string(&res->database, uri->s + i + 1, uri->s + uri->len) < 0)
  145. goto err;
  146. return 0;
  147. }
  148. break;
  149. case ST_PASS_PORT:
  150. switch(uri->s[i]) {
  151. case '@':
  152. st = ST_HOST;
  153. res->username = prev_token;
  154. if (dupl_string(&res->password, begin, uri->s + i) < 0) goto err;
  155. begin = uri->s + i + 1;
  156. break;
  157. case '/':
  158. if (memchr(uri->s + i + 1, '/', uri->len - i - 1) != NULL)
  159. break;
  160. res->host = prev_token;
  161. res->port = str2s(begin, uri->s + i - begin, 0);
  162. if (dupl_string(&res->database, uri->s + i + 1, uri->s + uri->len) < 0)
  163. goto err;
  164. return 0;
  165. }
  166. break;
  167. case ST_HOST:
  168. switch(uri->s[i]) {
  169. case ':':
  170. st = ST_PORT;
  171. if (dupl_string(&res->host, begin, uri->s + i) < 0) goto err;
  172. begin = uri->s + i + 1;
  173. break;
  174. case '/':
  175. if (memchr(uri->s + i + 1, '/', uri->len - i - 1) != NULL)
  176. break;
  177. if (dupl_string(&res->host, begin, uri->s + i) < 0) goto err;
  178. if (dupl_string(&res->database, uri->s + i + 1, uri->s + uri->len) < 0)
  179. goto err;
  180. return 0;
  181. }
  182. break;
  183. case ST_PORT:
  184. switch(uri->s[i]) {
  185. case '/':
  186. res->port = str2s(begin, uri->s + i - begin, 0);
  187. if (dupl_string(&res->database, uri->s + i + 1, uri->s + uri->len) < 0)
  188. goto err;
  189. return 0;
  190. }
  191. break;
  192. case ST_DB:
  193. break;
  194. }
  195. }
  196. if (st != ST_DB) goto err;
  197. return 0;
  198. err:
  199. if (prev_token) pkg_free(prev_token);
  200. if (res == NULL) return -1;
  201. if (res->username) {
  202. pkg_free(res->username);
  203. res->username = NULL;
  204. }
  205. if (res->password) {
  206. pkg_free(res->password);
  207. res->password = NULL;
  208. }
  209. if (res->host) {
  210. pkg_free(res->host);
  211. res->host = NULL;
  212. }
  213. if (res->database) {
  214. pkg_free(res->database);
  215. res->database = NULL;
  216. }
  217. return -1;
  218. }
  219. static void pg_uri_free(db_uri_t* uri, struct pg_uri* payload)
  220. {
  221. if (payload == NULL) return;
  222. db_drv_free(&payload->drv);
  223. if (payload->username) pkg_free(payload->username);
  224. if (payload->password) pkg_free(payload->password);
  225. if (payload->host) pkg_free(payload->host);
  226. if (payload->database) pkg_free(payload->database);
  227. pkg_free(payload);
  228. }
  229. int pg_uri(db_uri_t* uri)
  230. {
  231. struct pg_uri* puri;
  232. puri = (struct pg_uri*)pkg_malloc(sizeof(struct pg_uri));
  233. if (puri == NULL) {
  234. ERR("postgres: No memory left\n");
  235. goto error;
  236. }
  237. memset(puri, '\0', sizeof(struct pg_uri));
  238. if (db_drv_init(&puri->drv, pg_uri_free) < 0) goto error;
  239. if (parse_postgres_uri(puri, &uri->body) < 0) goto error;
  240. DB_SET_PAYLOAD(uri, puri);
  241. uri->cmp = pg_uri_cmp;
  242. return 0;
  243. error:
  244. if (puri) {
  245. db_drv_free(&puri->drv);
  246. if (puri) pkg_free(puri);
  247. }
  248. return -1;
  249. }
  250. /** @} */