db_utils.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * $Id$
  3. *
  4. * POSTGRES module, portions of this code were templated using
  5. * the mysql module, thus it's similarity.
  6. *
  7. *
  8. * Copyright (C) 2003 August.Net Services, LLC
  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
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version
  16. *
  17. * For a license to use the ser software under conditions
  18. * other than those described here, or to purchase support for this
  19. * software, please contact iptel.org by e-mail at the following addresses:
  20. * [email protected]
  21. *
  22. * ser is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. *
  31. * ---
  32. *
  33. * History
  34. * -------
  35. * 2003-04-06 initial code written (Greg Fausak/Andy Fullford)
  36. *
  37. */
  38. #define _GNU_SOURCE /* To avoid strptime warning */
  39. #include <string.h>
  40. #include <time.h>
  41. #include "db_utils.h"
  42. #include "defs.h"
  43. char* trim(char* _s);
  44. /*
  45. * SQL URL parser
  46. */
  47. int parse_sql_url(char* _url, char** _user, char** _pass,
  48. char** _host, char** _port, char** _db)
  49. {
  50. char* slash, *dcolon, *at, *db_slash;
  51. *_user = '\0';
  52. *_pass = '\0';
  53. *_host = '\0';
  54. *_port = '\0';
  55. *_db = '\0';
  56. /* Remove any leading and trailing spaces and tab */
  57. _url = trim(_url);
  58. if (strlen(_url) < 6) return -1;
  59. if (*_url == '\0') return -2; /* Empty string */
  60. slash = strchr(_url, '/');
  61. if (!slash) return -3; /* Invalid string, slashes not found */
  62. if ((*(++slash)) != '/') { /* Invalid URL, 2nd slash not found */
  63. return -4;
  64. }
  65. slash++;
  66. at = strchr(slash, '@');
  67. db_slash = strchr(slash, '/');
  68. if (db_slash) {
  69. *db_slash++ = '\0';
  70. *_db = trim(db_slash);
  71. }
  72. if (!at) {
  73. dcolon = strchr(slash, ':');
  74. if (dcolon) {
  75. *dcolon++ = '\0';
  76. *_port = trim(dcolon);
  77. }
  78. *_host = trim(slash);
  79. } else {
  80. dcolon = strchr(slash, ':');
  81. *at++ = '\0';
  82. if (dcolon) {
  83. *dcolon++ = '\0';
  84. if (dcolon < at) { /* user:passwd */
  85. *_pass = trim(dcolon);
  86. dcolon = strchr(at, ':');
  87. if (dcolon) { /* host:port */
  88. *dcolon++ = '\0';
  89. *_port = trim(dcolon);
  90. }
  91. } else { /* host:port */
  92. *_port = trim(dcolon);
  93. }
  94. }
  95. *_host = trim(at);
  96. *_user = trim(slash);
  97. }
  98. return 0;
  99. }
  100. /*
  101. * Remove any tabs and spaces from the begining and the end of
  102. * a string
  103. */
  104. char* trim(char* _s)
  105. {
  106. int len;
  107. char* end;
  108. /* Null pointer, there is nothing to do */
  109. if (!_s) return _s;
  110. /* Remove spaces and tabs from the begining of string */
  111. while ((*_s == ' ') || (*_s == '\t')) _s++;
  112. len = strlen(_s);
  113. end = _s + len - 1;
  114. /* Remove trailing spaces and tabs */
  115. while ((*end == ' ') || (*end == '\t')) end--;
  116. if (end != (_s + len - 1)) {
  117. *(end+1) = '\0';
  118. }
  119. return _s;
  120. }