1 /*
2    ldap_initialize.c - replacement function for ldap_initialize()
3 
4    Copyright (C) 2009, 2012, 2013 Arthur de Jong
5 
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10 
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15 
16    You should have received a copy of the GNU Lesser General Public
17    License along with this library; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301 USA
20 */
21 
22 #include "config.h"
23 
24 #include <stdlib.h>
25 #include <string.h>
26 #include <strings.h>
27 #include <lber.h>
28 #include <ldap.h>
29 
30 #include "compat/ldap_compat.h"
31 #include "nslcd/log.h"
32 
33 
34 /* provide a wrapper around ldap_init() if the system doesn't have
35    ldap_initialize() */
ldap_initialize(LDAP ** ldp,const char * url)36 int ldap_initialize(LDAP **ldp, const char *url)
37 {
38   char host[80];
39   /* check schema part */
40   if (strncasecmp(url, "ldap://", 7) == 0)
41   {
42     strncpy(host, url + 7, sizeof(host));
43     host[sizeof(host) - 1] = '\0';
44   }
45   else if (strncasecmp(url, "ldaps://", 8) == 0)
46   {
47     strncpy(host, url + 8, sizeof(host));
48     host[sizeof(host) - 1] = '\0';
49   }
50   else
51   {
52     log_log(LOG_ERR, "ldap_initialize(): schema not supported: %s", url);
53     exit(EXIT_FAILURE);
54   }
55   /* strip trailing slash */
56   if ((strlen(host) > 0) && (host[strlen(host) - 1] == '/'))
57     host[strlen(host) - 1] = '\0';
58   /* call ldap_init() */
59   *ldp = ldap_init(host, LDAP_PORT);
60   return (*ldp == NULL) ? LDAP_OPERATIONS_ERROR : LDAP_SUCCESS;
61 }
62