Blame nslcd/host.c

Packit 6bd9ab
/*
Packit 6bd9ab
   host.c - host name lookup routines
Packit 6bd9ab
   Parts of this file were part of the nss_ldap library (as ldap-hosts.c)
Packit 6bd9ab
   which has been forked into the nss-pam-ldapd library.
Packit 6bd9ab
Packit 6bd9ab
   Copyright (C) 1997-2005 Luke Howard
Packit 6bd9ab
   Copyright (C) 2006 West Consulting
Packit 6bd9ab
   Copyright (C) 2006-2014 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 <stdio.h>
Packit 6bd9ab
#include <stdlib.h>
Packit 6bd9ab
#include <string.h>
Packit 6bd9ab
#include <sys/types.h>
Packit 6bd9ab
#include <sys/socket.h>
Packit 6bd9ab
#include <arpa/inet.h>
Packit 6bd9ab
Packit 6bd9ab
#include "common.h"
Packit 6bd9ab
#include "log.h"
Packit 6bd9ab
#include "myldap.h"
Packit 6bd9ab
#include "cfg.h"
Packit 6bd9ab
#include "attmap.h"
Packit 6bd9ab
Packit 6bd9ab
/* ( nisSchema.2.6 NAME 'ipHost' SUP top AUXILIARY
Packit 6bd9ab
 *   DESC 'Abstraction of a host, an IP device. The distinguished
Packit 6bd9ab
 *         value of the cn attribute denotes the host's canonical
Packit 6bd9ab
 *         name. Device SHOULD be used as a structural class'
Packit 6bd9ab
 *   MUST ( cn $ ipHostNumber )
Packit 6bd9ab
 *   MAY ( l $ description $ manager ) )
Packit 6bd9ab
 */
Packit 6bd9ab
Packit 6bd9ab
/* the search base for searches */
Packit 6bd9ab
const char *host_bases[NSS_LDAP_CONFIG_MAX_BASES] = { NULL };
Packit 6bd9ab
Packit 6bd9ab
/* the search scope for searches */
Packit 6bd9ab
int host_scope = LDAP_SCOPE_DEFAULT;
Packit 6bd9ab
Packit 6bd9ab
/* the basic search filter for searches */
Packit 6bd9ab
const char *host_filter = "(objectClass=ipHost)";
Packit 6bd9ab
Packit 6bd9ab
/* the attributes to request with searches */
Packit 6bd9ab
const char *attmap_host_cn           = "cn";
Packit 6bd9ab
const char *attmap_host_ipHostNumber = "ipHostNumber";
Packit 6bd9ab
Packit 6bd9ab
/* the attribute list to request with searches */
Packit 6bd9ab
static const char *host_attrs[3];
Packit 6bd9ab
Packit 6bd9ab
/* create a search filter for searching a host entry
Packit 6bd9ab
   by name, return -1 on errors */
Packit 6bd9ab
static int mkfilter_host_byname(const char *name, char *buffer, size_t buflen)
Packit 6bd9ab
{
Packit 6bd9ab
  char safename[BUFLEN_HOSTNAME];
Packit 6bd9ab
  /* escape attribute */
Packit 6bd9ab
  if (myldap_escape(name, safename, sizeof(safename)))
Packit 6bd9ab
  {
Packit 6bd9ab
    log_log(LOG_ERR, "mkfilter_host_byname(): safename buffer too small");
Packit 6bd9ab
    return -1;
Packit 6bd9ab
  }
Packit 6bd9ab
  /* build filter */
Packit 6bd9ab
  return mysnprintf(buffer, buflen, "(&%s(%s=%s))",
Packit 6bd9ab
                    host_filter, attmap_host_cn, safename);
Packit 6bd9ab
}
Packit 6bd9ab
Packit 6bd9ab
static int mkfilter_host_byaddr(const char *addrstr,
Packit 6bd9ab
                                char *buffer, size_t buflen)
Packit 6bd9ab
{
Packit 6bd9ab
  char safeaddr[64];
Packit 6bd9ab
  /* escape attribute */
Packit 6bd9ab
  if (myldap_escape(addrstr, safeaddr, sizeof(safeaddr)))
Packit 6bd9ab
  {
Packit 6bd9ab
    log_log(LOG_ERR, "mkfilter_host_byaddr(): safeaddr buffer too small");
Packit 6bd9ab
    return -1;
Packit 6bd9ab
  }
Packit 6bd9ab
  /* build filter */
Packit 6bd9ab
  return mysnprintf(buffer, buflen, "(&%s(%s=%s))",
Packit 6bd9ab
                    host_filter, attmap_host_ipHostNumber, safeaddr);
Packit 6bd9ab
}
Packit 6bd9ab
Packit 6bd9ab
void host_init(void)
Packit 6bd9ab
{
Packit 6bd9ab
  int i;
Packit 6bd9ab
  /* set up search bases */
Packit 6bd9ab
  if (host_bases[0] == NULL)
Packit 6bd9ab
    for (i = 0; i < NSS_LDAP_CONFIG_MAX_BASES; i++)
Packit 6bd9ab
      host_bases[i] = nslcd_cfg->bases[i];
Packit 6bd9ab
  /* set up scope */
Packit 6bd9ab
  if (host_scope == LDAP_SCOPE_DEFAULT)
Packit 6bd9ab
    host_scope = nslcd_cfg->scope;
Packit 6bd9ab
  /* set up attribute list */
Packit 6bd9ab
  host_attrs[0] = attmap_host_cn;
Packit 6bd9ab
  host_attrs[1] = attmap_host_ipHostNumber;
Packit 6bd9ab
  host_attrs[2] = NULL;
Packit 6bd9ab
}
Packit 6bd9ab
Packit 6bd9ab
/* write a single host entry to the stream */
Packit 6bd9ab
static int write_host(TFILE *fp, MYLDAP_ENTRY *entry)
Packit 6bd9ab
{
Packit 6bd9ab
  int32_t tmpint32, tmp2int32, tmp3int32;
Packit 6bd9ab
  int numaddr, i;
Packit 6bd9ab
  const char *hostname;
Packit 6bd9ab
  const char **hostnames;
Packit 6bd9ab
  const char **addresses;
Packit 6bd9ab
  /* get the most canonical name */
Packit 6bd9ab
  hostname = myldap_get_rdn_value(entry, attmap_host_cn);
Packit 6bd9ab
  /* get the other names for the host */
Packit 6bd9ab
  hostnames = myldap_get_values(entry, attmap_host_cn);
Packit 6bd9ab
  if ((hostnames == NULL) || (hostnames[0] == NULL))
Packit 6bd9ab
  {
Packit 6bd9ab
    log_log(LOG_WARNING, "%s: %s: missing",
Packit 6bd9ab
            myldap_get_dn(entry), attmap_host_cn);
Packit 6bd9ab
    return 0;
Packit 6bd9ab
  }
Packit 6bd9ab
  /* if the hostname is not yet found, get the first entry from hostnames */
Packit 6bd9ab
  if (hostname == NULL)
Packit 6bd9ab
    hostname = hostnames[0];
Packit 6bd9ab
  /* get the addresses */
Packit 6bd9ab
  addresses = myldap_get_values(entry, attmap_host_ipHostNumber);
Packit 6bd9ab
  if ((addresses == NULL) || (addresses[0] == NULL))
Packit 6bd9ab
  {
Packit 6bd9ab
    log_log(LOG_WARNING, "%s: %s: missing",
Packit 6bd9ab
            myldap_get_dn(entry), attmap_host_ipHostNumber);
Packit 6bd9ab
    return 0;
Packit 6bd9ab
  }
Packit 6bd9ab
  /* write the entry */
Packit 6bd9ab
  WRITE_INT32(fp, NSLCD_RESULT_BEGIN);
Packit 6bd9ab
  WRITE_STRING(fp, hostname);
Packit 6bd9ab
  WRITE_STRINGLIST_EXCEPT(fp, hostnames, hostname);
Packit 6bd9ab
  for (numaddr = 0; addresses[numaddr] != NULL; numaddr++)
Packit 6bd9ab
    /* noting */ ;
Packit 6bd9ab
  WRITE_INT32(fp, numaddr);
Packit 6bd9ab
  for (i = 0; i < numaddr; i++)
Packit 6bd9ab
  {
Packit 6bd9ab
    WRITE_ADDRESS(fp, entry, attmap_host_ipHostNumber, addresses[i]);
Packit 6bd9ab
  }
Packit 6bd9ab
  return 0;
Packit 6bd9ab
}
Packit 6bd9ab
Packit 6bd9ab
NSLCD_HANDLE(
Packit 6bd9ab
  host, byname, NSLCD_ACTION_HOST_BYNAME,
Packit 6bd9ab
  char name[BUFLEN_HOSTNAME];
Packit 6bd9ab
  char filter[BUFLEN_FILTER];
Packit 6bd9ab
  READ_STRING(fp, name);
Packit 6bd9ab
  log_setrequest("host=\"%s\"", name);,
Packit 6bd9ab
  mkfilter_host_byname(name, filter, sizeof(filter)),
Packit 6bd9ab
  write_host(fp, entry)
Packit 6bd9ab
)
Packit 6bd9ab
Packit 6bd9ab
NSLCD_HANDLE(
Packit 6bd9ab
  host, byaddr, NSLCD_ACTION_HOST_BYADDR,
Packit 6bd9ab
  int af;
Packit 6bd9ab
  char addr[64];
Packit 6bd9ab
  int len = sizeof(addr);
Packit 6bd9ab
  char addrstr[64];
Packit 6bd9ab
  char filter[BUFLEN_FILTER];
Packit 6bd9ab
  READ_ADDRESS(fp, addr, len, af);
Packit 6bd9ab
  /* translate the address to a string */
Packit 6bd9ab
  if (inet_ntop(af, addr, addrstr, sizeof(addrstr)) == NULL)
Packit 6bd9ab
  {
Packit 6bd9ab
    log_log(LOG_WARNING, "unable to convert address to string");
Packit 6bd9ab
    return -1;
Packit 6bd9ab
  }
Packit 6bd9ab
  log_setrequest("host=%s", addrstr);,
Packit 6bd9ab
  mkfilter_host_byaddr(addrstr, filter, sizeof(filter)),
Packit 6bd9ab
  write_host(fp, entry)
Packit 6bd9ab
)
Packit 6bd9ab
Packit 6bd9ab
Packit 6bd9ab
NSLCD_HANDLE(
Packit 6bd9ab
  host, all, NSLCD_ACTION_HOST_ALL,
Packit 6bd9ab
  const char *filter;
Packit 6bd9ab
  log_setrequest("host(all)");,
Packit 6bd9ab
  (filter = host_filter, 0),
Packit 6bd9ab
  write_host(fp, entry)
Packit 6bd9ab
)