ld_uri.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * $Id$
  3. *
  4. * LDAP Database Driver for SER
  5. *
  6. * Copyright (C) 2008 iptelorg GmbH
  7. *
  8. * This file is part of SER, a free SIP server.
  9. *
  10. * SER is free software; you can redistribute it and/or modify it under the
  11. * terms of the GNU General Public License as published by the Free Software
  12. * Foundation; either version 2 of the License, or (at your option) any later
  13. * version.
  14. *
  15. * SER is distributed in the hope that it will be useful, but WITHOUT ANY
  16. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. */
  24. /** \addtogroup ldap
  25. * @{
  26. */
  27. /** \file
  28. * The implementation of parser parsing ldap:.. URIs.
  29. */
  30. #include "ld_uri.h"
  31. #include "ld_cfg.h"
  32. #include "../../mem/mem.h"
  33. #include "../../ut.h"
  34. #include <string.h>
  35. /** compare s1 & s2 with a function f (which should return 0 if ==);
  36. * s1 & s2 can be null
  37. * return 0 if match, 1 if not
  38. */
  39. #define cmpstr(s1, s2, f) \
  40. ((s1)!=(s2)) && ((s1)==0 || (s2)==0 || (f)((s1), (s2))!=0)
  41. /** Compares two LDAP connection URIs.
  42. * This function is called whenever the database abstraction layer in
  43. * SER needs to compare to URIs with the ldap scheme. The function
  44. * compares hosts and port numbers of both URIs (host part comparison
  45. * is case insensitive). The URI comparison is mainly used to
  46. * by the connection pool to determine if a connection to a given
  47. * server already exists.
  48. **/
  49. static unsigned char ld_uri_cmp(db_uri_t* uri1, db_uri_t* uri2)
  50. {
  51. struct ld_uri* luri1, *luri2;
  52. if (!uri1 || !uri2) return 0;
  53. luri1 = DB_GET_PAYLOAD(uri1);
  54. luri2 = DB_GET_PAYLOAD(uri2);
  55. if (luri1->ldap_url->lud_port != luri2->ldap_url->lud_port) return 0;
  56. if (cmpstr(luri1->ldap_url->lud_host,
  57. luri2->ldap_url->lud_host, strcasecmp))
  58. return 0;
  59. return 1;
  60. }
  61. /** Duplicate a string
  62. */
  63. static int dupl_string(char** dst, const char* begin, const char* end)
  64. {
  65. if (*dst) pkg_free(*dst);
  66. *dst = pkg_malloc(end - begin + 1);
  67. if ((*dst) == NULL) {
  68. return -1;
  69. }
  70. memcpy(*dst, begin, end - begin);
  71. (*dst)[end - begin] = '\0';
  72. return 0;
  73. }
  74. /** Duplicate a string
  75. */
  76. static char* pkgstrdup(str* s)
  77. {
  78. char* dst;
  79. if (!s)
  80. return NULL;
  81. dst = pkg_malloc(s->len + 1);
  82. if (dst == NULL)
  83. return NULL;
  84. memcpy(dst, s->s, s->len);
  85. dst[s->len] = '\0';
  86. return dst;
  87. }
  88. /*
  89. * Parse ldap URI of form
  90. * //[username[:password]@]hostname[:port]
  91. *
  92. * Returns 0 if parsing was successful and -1 otherwise
  93. */
  94. int parse_ldap_uri(struct ld_uri* res, str* scheme, str* uri)
  95. {
  96. #define SHORTEST_DB_URL "a"
  97. #define SHORTEST_DB_URL_LEN (sizeof(SHORTEST_DB_URL) - 1)
  98. enum state {
  99. ST_BEGIN, /* First state */
  100. ST_SECTION_ID, /* Config section id */
  101. ST_SLASH2, /* Second slash */
  102. ST_USER_HOST, /* Username or hostname */
  103. ST_PASS_PORT, /* Password or port part */
  104. ST_HOST_PORT /* Hostname and port part */
  105. };
  106. enum state st;
  107. int i, ldapurllen;
  108. const char* begin;
  109. const char* ldapbegin;
  110. char* prev_token;
  111. struct ld_con_info* cfg_conn_info;
  112. char* sport, *puri;
  113. int portlen = 0;
  114. prev_token = 0;
  115. if (!res || !scheme || !uri) {
  116. goto err;
  117. }
  118. if (uri->len < SHORTEST_DB_URL_LEN) {
  119. goto err;
  120. }
  121. st = ST_BEGIN;
  122. ldapbegin = begin = uri->s;
  123. for(i = 0; i < uri->len && st != ST_SECTION_ID; i++) {
  124. switch(st) {
  125. case ST_BEGIN:
  126. switch(uri->s[i]) {
  127. case '/':
  128. st = ST_SLASH2;
  129. break;
  130. default:
  131. st = ST_SECTION_ID;
  132. }
  133. break;
  134. case ST_SECTION_ID:
  135. break;
  136. case ST_SLASH2:
  137. switch(uri->s[i]) {
  138. case '/':
  139. st = ST_USER_HOST;
  140. ldapbegin = begin = uri->s + i + 1;
  141. break;
  142. default:
  143. goto err;
  144. }
  145. break;
  146. case ST_USER_HOST:
  147. switch(uri->s[i]) {
  148. case '@':
  149. st = ST_HOST_PORT;
  150. if (dupl_string(&res->username, begin, uri->s + i) < 0) goto err;
  151. ldapbegin = begin = uri->s + i + 1;
  152. break;
  153. case ':':
  154. st = ST_PASS_PORT;
  155. if (dupl_string(&prev_token, begin, uri->s + i) < 0) goto err;
  156. begin = uri->s + i + 1;
  157. break;
  158. }
  159. break;
  160. case ST_PASS_PORT:
  161. switch(uri->s[i]) {
  162. case '@':
  163. st = ST_HOST_PORT;
  164. res->username = prev_token;
  165. if (dupl_string(&res->password, begin, uri->s + i) < 0) goto err;
  166. ldapbegin = begin = uri->s + i + 1;
  167. break;
  168. }
  169. break;
  170. case ST_HOST_PORT:
  171. break;
  172. }
  173. }
  174. switch(st) {
  175. case ST_PASS_PORT:
  176. case ST_USER_HOST:
  177. case ST_HOST_PORT:
  178. ldapurllen = uri->len - (int)(ldapbegin - uri->s);
  179. // +3 for the '://' ldap url snippet
  180. res->uri = pkg_malloc(scheme->len + 3 + ldapurllen + 1);
  181. if (res->uri== NULL) {
  182. ERR("ldap: No memory left\n");
  183. goto err;
  184. }
  185. memcpy(res->uri, scheme->s, scheme->len);
  186. res->uri[scheme->len] = ':';
  187. res->uri[scheme->len + 1] = '/';
  188. res->uri[scheme->len + 2] = '/';
  189. memcpy(res->uri + scheme->len + 3, ldapbegin, ldapurllen);
  190. res->uri[scheme->len + 3 + ldapurllen] = '\0';
  191. if (ldap_url_parse(res->uri, &res->ldap_url) != 0) {
  192. ERR("ldap: Error while parsing URL '%s'\n", res->uri);
  193. goto err;
  194. }
  195. break;
  196. case ST_SECTION_ID:
  197. /* the value of uri is the id of the config
  198. connection section in this case */
  199. cfg_conn_info = ld_find_conn_info(uri);
  200. if (!cfg_conn_info) {
  201. ERR("ldap: connection id '%.*s' not found in ldap config\n", uri->len, uri->s);
  202. goto err;
  203. }
  204. ldapurllen = cfg_conn_info->host.len;
  205. sport = NULL;
  206. if (cfg_conn_info->port) {
  207. sport = int2str(cfg_conn_info->port, &portlen);
  208. // +1: we need space for ':' host and port delimiter
  209. ldapurllen += portlen + 1;
  210. }
  211. // +3 for the '://' ldap url snippet
  212. puri = res->uri = pkg_malloc(scheme->len + 3 + ldapurllen + 1);
  213. if (res->uri== NULL) {
  214. ERR("ldap: No memory left\n");
  215. goto err;
  216. }
  217. memcpy(puri, scheme->s, scheme->len);
  218. puri += scheme->len;
  219. memcpy(puri, "://", strlen("://"));
  220. puri+= strlen("://");
  221. memcpy(puri, cfg_conn_info->host.s, cfg_conn_info->host.len);
  222. puri+=cfg_conn_info->host.len;
  223. if (sport) {
  224. *puri++ = ':';
  225. memcpy(puri, sport, portlen);
  226. }
  227. res->uri[scheme->len + 3 + ldapurllen] = '\0';
  228. if (ldap_url_parse(res->uri, &res->ldap_url) != 0) {
  229. ERR("ldap: Error while parsing URL '%s'\n", res->uri);
  230. goto err;
  231. }
  232. if (cfg_conn_info->username.s) {
  233. if (!(res->username = pkgstrdup(&cfg_conn_info->username))) {
  234. ERR("ldap: No memory left\n");
  235. goto err;
  236. }
  237. }
  238. if (cfg_conn_info->password.s) {
  239. if (!(res->password = pkgstrdup(&cfg_conn_info->password))) {
  240. ERR("ldap: No memory left\n");
  241. goto err;
  242. }
  243. }
  244. res->authmech = cfg_conn_info->authmech;
  245. res->tls = cfg_conn_info->tls;
  246. if (cfg_conn_info->ca_list.s) {
  247. if (!(res->ca_list = pkgstrdup(&cfg_conn_info->ca_list))) {
  248. ERR("ldap: No memory left\n");
  249. goto err;
  250. }
  251. }
  252. if (cfg_conn_info->req_cert.s) {
  253. if (!(res->req_cert = pkgstrdup(&cfg_conn_info->req_cert))) {
  254. ERR("ldap: No memory left\n");
  255. goto err;
  256. }
  257. }
  258. break;
  259. default:
  260. goto err;
  261. }
  262. return 0;
  263. err:
  264. if (prev_token) pkg_free(prev_token);
  265. if (res == NULL) return -1;
  266. if (res->username) {
  267. pkg_free(res->username);
  268. res->username = NULL;
  269. }
  270. if (res->password) {
  271. pkg_free(res->password);
  272. res->password = NULL;
  273. }
  274. if (res->ca_list) {
  275. pkg_free(res->ca_list);
  276. res->ca_list = NULL;
  277. }
  278. if (res->req_cert) {
  279. pkg_free(res->req_cert);
  280. res->req_cert = NULL;
  281. }
  282. return -1;
  283. }
  284. static void ld_uri_free(db_uri_t* uri, struct ld_uri* payload)
  285. {
  286. if (payload == NULL) return;
  287. if (payload->ldap_url) ldap_free_urldesc(payload->ldap_url);
  288. if (payload->uri) pkg_free(payload->uri);
  289. if (payload->username) pkg_free(payload->username);
  290. if (payload->password) pkg_free(payload->password);
  291. if (payload->ca_list) pkg_free(payload->ca_list);
  292. if (payload->req_cert) pkg_free(payload->req_cert);
  293. db_drv_free(&payload->drv);
  294. pkg_free(payload);
  295. }
  296. int ld_uri(db_uri_t* uri)
  297. {
  298. struct ld_uri* luri;
  299. luri = (struct ld_uri*)pkg_malloc(sizeof(struct ld_uri));
  300. if (luri == NULL) {
  301. ERR("ldap: No memory left\n");
  302. goto error;
  303. }
  304. memset(luri, '\0', sizeof(struct ld_uri));
  305. if (db_drv_init(&luri->drv, ld_uri_free) < 0) goto error;
  306. if (parse_ldap_uri(luri, &uri->scheme, &uri->body) < 0) goto error;
  307. DB_SET_PAYLOAD(uri, luri);
  308. uri->cmp = ld_uri_cmp;
  309. return 0;
  310. error:
  311. if (luri) {
  312. if (luri->uri) pkg_free(luri->uri);
  313. if (luri->ldap_url) ldap_free_urldesc(luri->ldap_url);
  314. db_drv_free(&luri->drv);
  315. pkg_free(luri);
  316. }
  317. return -1;
  318. }
  319. /** @} */