Blame compat/ldap_initialize.c

Packit 6bd9ab
/*
Packit 6bd9ab
   ldap_initialize.c - replacement function for ldap_initialize()
Packit 6bd9ab
Packit 6bd9ab
   Copyright (C) 2009, 2012, 2013 Arthur de Jong
Packit 6bd9ab
Packit 6bd9ab
   This library is free software; you can redistribute it and/or
Packit 6bd9ab
   modify it under the terms of the GNU Lesser General Public
Packit 6bd9ab
   License as published by the Free Software Foundation; either
Packit 6bd9ab
   version 2.1 of the License, or (at your option) any later version.
Packit 6bd9ab
Packit 6bd9ab
   This library is distributed in the hope that it will be useful,
Packit 6bd9ab
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6bd9ab
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6bd9ab
   Lesser General Public License for more details.
Packit 6bd9ab
Packit 6bd9ab
   You should have received a copy of the GNU Lesser General Public
Packit 6bd9ab
   License along with this library; if not, write to the Free Software
Packit 6bd9ab
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 6bd9ab
   02110-1301 USA
Packit 6bd9ab
*/
Packit 6bd9ab
Packit 6bd9ab
#include "config.h"
Packit 6bd9ab
Packit 6bd9ab
#include <stdlib.h>
Packit 6bd9ab
#include <string.h>
Packit 6bd9ab
#include <strings.h>
Packit 6bd9ab
#include <lber.h>
Packit 6bd9ab
#include <ldap.h>
Packit 6bd9ab
Packit 6bd9ab
#include "compat/ldap_compat.h"
Packit 6bd9ab
#include "nslcd/log.h"
Packit 6bd9ab
Packit 6bd9ab
Packit 6bd9ab
/* provide a wrapper around ldap_init() if the system doesn't have
Packit 6bd9ab
   ldap_initialize() */
Packit 6bd9ab
int ldap_initialize(LDAP **ldp, const char *url)
Packit 6bd9ab
{
Packit 6bd9ab
  char host[80];
Packit 6bd9ab
  /* check schema part */
Packit 6bd9ab
  if (strncasecmp(url, "ldap://", 7) == 0)
Packit 6bd9ab
  {
Packit 6bd9ab
    strncpy(host, url + 7, sizeof(host));
Packit 6bd9ab
    host[sizeof(host) - 1] = '\0';
Packit 6bd9ab
  }
Packit 6bd9ab
  else if (strncasecmp(url, "ldaps://", 8) == 0)
Packit 6bd9ab
  {
Packit 6bd9ab
    strncpy(host, url + 8, sizeof(host));
Packit 6bd9ab
    host[sizeof(host) - 1] = '\0';
Packit 6bd9ab
  }
Packit 6bd9ab
  else
Packit 6bd9ab
  {
Packit 6bd9ab
    log_log(LOG_ERR, "ldap_initialize(): schema not supported: %s", url);
Packit 6bd9ab
    exit(EXIT_FAILURE);
Packit 6bd9ab
  }
Packit 6bd9ab
  /* strip trailing slash */
Packit 6bd9ab
  if ((strlen(host) > 0) && (host[strlen(host) - 1] == '/'))
Packit 6bd9ab
    host[strlen(host) - 1] = '\0';
Packit 6bd9ab
  /* call ldap_init() */
Packit 6bd9ab
  *ldp = ldap_init(host, LDAP_PORT);
Packit 6bd9ab
  return (*ldp == NULL) ? LDAP_OPERATIONS_ERROR : LDAP_SUCCESS;
Packit 6bd9ab
}