<commit_before>/*
   pam.c - pam processing routines

   Copyright (C) 2009 <NAME>
   Copyright (C) 2009, 2010, 2011 <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 <unistd.h>
#include <time.h>

#include "common.h"
#include "log.h"
#include "myldap.h"
#include "cfg.h"
#include "attmap.h"
#include "common/dict.h"
#include "common/expr.h"

/* set up a connection and try to bind with the specified DN and password,
   returns an LDAP result code */
static int try_bind(const char *userdn,const char *password)
{
  MYLDAP_SESSION *session;
  MYLDAP_SEARCH *search;
  MYLDAP_ENTRY *entry;
  static const char *attrs[2];
  int rc;
  /* set up a new connection */
  session=myldap_create_session();
  if (session==NULL)
    return LDAP_UNAVAILABLE;
  /* set up credentials for the session */
  myldap_set_credentials(session,userdn,password);
  /* perform search for own object (just to do any kind of search) */
  attrs[0]="dn";
  attrs[1]=NULL;
  search=myldap_search(session,userdn,LDAP_SCOPE_BASE,"(objectClass=*)",attrs,&rc);
  if ((search==NULL)||(rc!=LDAP_SUCCESS))
  {
    if (rc==LDAP_SUCCESS)
      rc=LDAP_LOCAL_ERROR;
    log_log(LOG_WARNING,"%s: lookup failed: %s",userdn,ldap_err2string(rc));
  }
  else
  {
    entry=myldap_get_entry(search,&rc);
    if ((entry==NULL)||(rc!=LDAP_SUCCESS))
    {
      if (rc==LDAP_SUCCESS)
        rc=LDAP_NO_RESULTS_RETURNED;
      log_log(LOG_WARNING,"%s: lookup failed: %s",userdn,ldap_err2string(rc));
    }
  }
  /* close the session */
  myldap_session_close(session);
  /* return results */
  return rc;
}

/* ensure that both userdn and username are filled in from the entry,
   returns an LDAP result code */
static MYLDAP_ENTRY *validate_user(MYLDAP_SESSION *session,
                         char *username,int *rcp)
{
  int rc;
  MYLDAP_ENTRY *entry=NULL;
  /* check username for validity */
  if (!isvalidname(username))
  {
    log_log(LOG_WARNING,"request denied by validnames option");
    *rcp=LDAP_NO_SUCH_OBJECT;
    return NULL;
  }
  /* get the user entry based on the username */
  entry=uid2entry(session,username,&rc);
  if (entry==NULL)
  {
    if (rc==LDAP_SUCCESS)
      rc=LDAP_NO_SUCH_OBJECT;
    log_log(LOG_WARNING,"\"%s\": user not found: %s",username,ldap_err2string(rc));
    *rcp=rc;
  }
  return entry;
}

/* update the username value from the entry if needed */
static void update_username(MYLDAP_ENTRY *entry,char *username,size_t username_len)
{
  const char **values;
  const char *value;
  /* get the "real" username */
  value=myldap_get_rdn_value(entry,attmap_passwd_uid);
  if (value==NULL)
  {
    /* get the username from the uid attribute */
    values=myldap_get_values(entry,attmap_passwd_uid);
    if ((values==NULL)||(values[0]==NULL))
      log_log(LOG_WARNING,"%s: %s: missing",
                          myldap_get_dn(entry),attmap_passwd_uid);
    value=values[0];
  }
  /* check the username */<commit_msg>reduce loglevel of user not found messages to avoid spamming the logs with useless information (thanks Wakko Warner)<commit_after>/*
   pam.c - pam processing routines

   Copyright (C) 2009 <NAME>
   Copyright (C) 2009, 2010, 2011 <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 <unistd.h>
#include <time.h>

#include "common.h"
#include "log.h"
#include "myldap.h"
#include "cfg.h"
#include "attmap.h"
#include "common/dict.h"
#include "common/expr.h"

/* set up a connection and try to bind with the specified DN and password,
   returns an LDAP result code */
static int try_bind(const char *userdn,const char *password)
{
  MYLDAP_SESSION *session;
  MYLDAP_SEARCH *search;
  MYLDAP_ENTRY *entry;
  static const char *attrs[2];
  int rc;
  /* set up a new connection */
  session=myldap_create_session();
  if (session==NULL)
    return LDAP_UNAVAILABLE;
  /* set up credentials for the session */
  myldap_set_credentials(session,userdn,password);
  /* perform search for own object (just to do any kind of search) */
  attrs[0]="dn";
  attrs[1]=NULL;
  search=myldap_search(session,userdn,LDAP_SCOPE_BASE,"(objectClass=*)",attrs,&rc);
  if ((search==NULL)||(rc!=LDAP_SUCCESS))
  {
    if (rc==LDAP_SUCCESS)
      rc=LDAP_LOCAL_ERROR;
    log_log(LOG_WARNING,"%s: lookup failed: %s",userdn,ldap_err2string(rc));
  }
  else
  {
    entry=myldap_get_entry(search,&rc);
    if ((entry==NULL)||(rc!=LDAP_SUCCESS))
    {
      if (rc==LDAP_SUCCESS)
        rc=LDAP_NO_RESULTS_RETURNED;
      log_log(LOG_WARNING,"%s: lookup failed: %s",userdn,ldap_err2string(rc));
    }
  }
  /* close the session */
  myldap_session_close(session);
  /* return results */
  return rc;
}

/* ensure that both userdn and username are filled in from the entry,
   returns an LDAP result code */
static MYLDAP_ENTRY *validate_user(MYLDAP_SESSION *session,
                         char *username,int *rcp)
{
  int rc;
  MYLDAP_ENTRY *entry=NULL;
  /* check username for validity */
  if (!isvalidname(username))
  {
    log_log(LOG_WARNING,"request denied by validnames option");
    *rcp=LDAP_NO_SUCH_OBJECT;
    return NULL;
  }
  /* get the user entry based on the username */
  entry=uid2entry(session,username,&rc);
  if (entry==NULL)
  {
    if (rc==LDAP_SUCCESS)
      rc=LDAP_NO_SUCH_OBJECT;
    log_log(LOG_DEBUG,"\"%s\": user not found: %s",username,ldap_err2string(rc));
    *rcp=rc;
  }
  return entry;
}

/* update the username value from the entry if needed */
static void update_username(MYLDAP_ENTRY *entry,char *username,size_t username_len)
{
  const char **values;
  const char *value;
  /* get the "real" username */
  value=myldap_get_rdn_value(entry,attmap_passwd_uid);
  if (value==NULL)
  {
    /* get the username from the uid attribute */
    values=myldap_get_values(entry,attmap_passwd_uid);
    if ((values==NULL)||(values[0]==NULL))
      log_log(LOG_WARNING,"%s: %s: missing",
                          myldap_get_dn(entry),attmap_passwd_uid);
    value=values[0];
  }
  /* check the username */
{'fraction_non_alphanumeric': 0.11345473601467312, 'fraction_numerical': 0.00995676667103367, 'mean_word_length': 3.831645569620253, 'pattern_counts': {'":': 2, '<': 19, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4385214', 'n_tokens_mistral': 2610, 'n_tokens_neox': 2538, 'n_words': 759}