<commit_before>/*
   ether.c - ethernet address entry lookup routines
   Parts of this file were part of the nss_ldap library (as ldap-ethers.c)
   which has been forked into the nss-pam-ldapd library.

   Copyright (C) 1997-2005 <NAME>
   Copyright (C) 2006 West Consulting
   Copyright (C) 2006, 2007, 2009, 2010, 2011, 2012 <NAME>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301 USA
*/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif /* HAVE_STDINT_H */

#include "common.h"
#include "log.h"
#include "myldap.h"
#include "cfg.h"
#include "attmap.h"
#include "compat/ether.h"

/* ( nisSchema.2.11 NAME 'ieee802Device' SUP top AUXILIARY
 *   DESC 'A device with a MAC address; device SHOULD be
 *         used as a structural class'
 *   MAY macAddress )
 */

/* the search base for searches */
const char *ether_bases[NSS_LDAP_CONFIG_MAX_BASES] = { NULL };

/* the search scope for searches */
int ether_scope = LDAP_SCOPE_DEFAULT;

/* the basic search filter for searches */
const char *ether_filter = "(objectClass=ieee802Device)";

/* the attributes to request with searches */
const char *attmap_ether_cn         = "cn";
const char *attmap_ether_macAddress = "macAddress";

/* the attribute list to request with searches */
static const char *ether_attrs[3];

/* create a search filter for searching an ethernet address
   by name, return -1 on errors */
static int mkfilter_ether_byname(const char *name,
                                 char *buffer, size_t buflen)
{
  char safename[300];
  /* escape attribute */
  if (myldap_escape(name, safename, sizeof(safename)))
    return -1;
  /* build filter */
  return mysnprintf(buffer, buflen, "(&%s(%s=%s))",
                    ether_filter, attmap_ether_cn, safename);
}

static int mkfilter_ether_byether(const char *addrstr,
                                  char *buffer, size_t buflen)
{
  /* FIXME: this has a bug when the directory has 01:00:0e:...
     and we're looking for 1:0:e:... (leading zeros) */
  /* there should be no characters that need escaping */
  /* build filter */
  return mysnprintf(buffer, buflen, "(&%s(%s=%s))",
                    ether_filter, attmap_ether_macAddress, addrstr);
}

void ether_init(void)
{
  int i;
  /* set up search bases */
  if (ether_bases[0] == NULL)
    for (i = 0; i < NSS_LDAP_CONFIG_MAX_BASES; i++)
      ether_bases[i] = nslcd_cfg->bases[i];
  /* set up scope */
  if (ether_scope == LDAP_SCOPE_DEFAULT)
    ether_scope = nslcd_cfg->scope;
  /* set up attribute list */
  ether_attrs[0] = attmap_ether_cn;
  ether_attrs[1] = attmap_ether_macAddress;
  ether_attrs[2] = NULL;
}

/* TODO: check for errors in aton() */
#define WRITE_ETHER(fp, addr)                                               \
  ether_aton_r(addr, &tmpaddr);                                             \
  WRITE(fp, &tmpaddr, sizeof(uint8_t[6]));<commit_msg>change ethernet address formatting from FIXME to note<commit_after>/*
   ether.c - ethernet address entry lookup routines
   Parts of this file were part of the nss_ldap library (as ldap-ethers.c)
   which has been forked into the nss-pam-ldapd library.

   Copyright (C) 1997-2005 <NAME>
   Copyright (C) 2006 West Consulting
   Copyright (C) 2006, 2007, 2009, 2010, 2011, 2012, 2013 <NAME>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301 USA
*/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif /* HAVE_STDINT_H */

#include "common.h"
#include "log.h"
#include "myldap.h"
#include "cfg.h"
#include "attmap.h"
#include "compat/ether.h"

/* ( nisSchema.2.11 NAME 'ieee802Device' SUP top AUXILIARY
 *   DESC 'A device with a MAC address; device SHOULD be
 *         used as a structural class'
 *   MAY macAddress )
 */

/* the search base for searches */
const char *ether_bases[NSS_LDAP_CONFIG_MAX_BASES] = { NULL };

/* the search scope for searches */
int ether_scope = LDAP_SCOPE_DEFAULT;

/* the basic search filter for searches */
const char *ether_filter = "(objectClass=ieee802Device)";

/* the attributes to request with searches */
const char *attmap_ether_cn         = "cn";
const char *attmap_ether_macAddress = "macAddress";

/* the attribute list to request with searches */
static const char *ether_attrs[3];

/* create a search filter for searching an ethernet address
   by name, return -1 on errors */
static int mkfilter_ether_byname(const char *name,
                                 char *buffer, size_t buflen)
{
  char safename[300];
  /* escape attribute */
  if (myldap_escape(name, safename, sizeof(safename)))
    return -1;
  /* build filter */
  return mysnprintf(buffer, buflen, "(&%s(%s=%s))",
                    ether_filter, attmap_ether_cn, safename);
}

static int mkfilter_ether_byether(const char *addrstr,
                                  char *buffer, size_t buflen)
{
  /* Note: this only works if the address in LDAP has the preferred minimal
     representation (e.g. 1:0:e:...) and not with extra leading zeros
     (e.g. 01:00:0e:...) */
  /* there should be no characters that need escaping */
  return mysnprintf(buffer, buflen, "(&%s(%s=%s))",
                    ether_filter, attmap_ether_macAddress, addrstr);
}

void ether_init(void)
{
  int i;
  /* set up search bases */
  if (ether_bases[0] == NULL)
    for (i = 0; i < NSS_LDAP_CONFIG_MAX_BASES; i++)
      ether_bases[i] = nslcd_cfg->bases[i];
  /* set up scope */
  if (ether_scope == LDAP_SCOPE_DEFAULT)
    ether_scope = nslcd_cfg->scope;
  /* set up attribute list */
  ether_attrs[0] = attmap_ether_cn;
  ether_attrs[1] = attmap_ether_macAddress;
  ether_attrs[2] = NULL;
}

/* TODO: check for errors in aton() */
#define WRITE_ETHER(fp, addr)                                               \
  ether_aton_r(addr, &tmpaddr);                                             \
  WRITE(fp, &tmpaddr, sizeof(uint8_t[6]));
{'fraction_non_alphanumeric': 0.10076670317634173, 'fraction_numerical': 0.02190580503833516, 'mean_word_length': 3.024793388429752, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3655470', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2263, 'n_words': 810}