/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <synch.h>
#include <time.h>
#include <libintl.h>
#include <thread.h>
#include <syslog.h>
#include <nsswitch.h>
#include <nss_dbdefs.h>
#include "solaris-priv.h"
#include "solaris-int.h"
#include "ns_sldap.h"
#include "ns_internal.h"
#include "ns_cache_door.h"
#include "ns_connmgmt.h"
#include "ldappr.h"
#include <fcntl.h>
#include <procfs.h>
#include <unistd.h>
#define USE_DEFAULT_PORT 0
LDAP *,
int,
ns_ldap_error_t **,
int,
int);
const char *,
int,
LDAP **,
ns_ldap_error_t **);
LDAPControl **, LDAPControl **);
extern int SetDoorInfoToUnixCred(char *buffer,
UnixCred_t **cred);
int, ns_ldap_error_t **, int, int, ns_conn_user_t *, int);
static void
static int sessionPoolSize = 0;
/*
* SSF values are for SASL integrity & privacy.
* JES DS5.2 does not support this feature but DS6 does.
* The values between 0 and 65535 can work with both server versions.
*/
#define MIN_SASL_SSF 0
/* Number of hostnames to allocate memory for */
/*
* This function get the servers from the lists and returns
* the first server with the empty lists of server controls and
* SASL mechanisms. It is invoked if it is not possible to obtain a server
* from ldap_cachemgr or the local list.
*/
static
{
/* get first server from config list unavailable otherwise */
if (ret_code != NS_LDAP_SUCCESS) {
}
return (ret_code);
}
gettext("No server found in configuration"));
return (NS_LDAP_CONFIG);
}
return (NS_LDAP_MEMORY);
}
return (NS_LDAP_SUCCESS);
}
/* very similar to __door_getldapconfig() in ns_config.c */
static int
{
}
/*
* This function requests Admin credentials from the cache manager through
* the door functionality
*/
static int
{
int buflen = 0;
int ret;
if (ret != NS_LDAP_SUCCESS) {
return (ret);
}
/* now convert from door format */
return (ret);
}
/*
* This function requests a server from the cache manager through
* the door functionality
*/
int
{
union {
} space;
int ndata;
int adata;
const char *ireq;
char *dptr;
int len;
return (NS_LDAP_OP_FAILED);
}
ireq = NS_CACHE_NEW;
else
/*
* In the 'Standalone' mode a server will be obtained
* from the local libsldap's list
*/
if (__s_api_isStandalone()) {
ret,
error)) != NS_LDAP_SUCCESS) {
/*
* get first server from local list only once
* to prevent looping
*/
return (ret_code);
"libsldap (\"standalone\" mode): "
"can not find any available server. "
"Return the first one from the lists");
(void) __ns_ldap_freeError(error);
}
if (ret_code != NS_LDAP_SUCCESS) {
return (ret_code);
}
&ret->serverFQDN);
if (ret_code != NS_LDAP_SUCCESS) {
sizeof (errstr),
gettext("The %s address "
"can not be resolved into "
"a host name. Returning "
"the address as it is."),
*error,
return (NS_LDAP_INTERNAL);
}
}
}
return (NS_LDAP_SUCCESS);
}
}
return (NS_LDAP_MEMORY);
len)
return (NS_LDAP_MEMORY);
return (NS_LDAP_MEMORY);
return (NS_LDAP_MEMORY);
}
case NS_CACHE_SUCCESS:
break;
/* this case is for when the $mgr is not running, but ldapclient */
/* is trying to initialize things */
case NS_CACHE_NOSERVER:
if (ret_code != NS_LDAP_SUCCESS) {
return (ret_code);
}
&ret->serverFQDN);
if (ret_code != NS_LDAP_SUCCESS) {
sizeof (errstr),
gettext("The %s address "
"can not be resolved into "
"a host name. Returning "
"the address as it is."),
*error,
return (NS_LDAP_INTERNAL);
}
}
return (NS_LDAP_SUCCESS);
case NS_CACHE_NOTFOUND:
default:
return (NS_LDAP_OP_FAILED);
}
/* copy info from door call return structure here */
/* Get the host */
"ldap_cachemgr"));
return (NS_LDAP_OP_FAILED);
}
return (NS_LDAP_MEMORY);
}
/* Get server type */
"returned from ldap_cachemgr"));
return (NS_LDAP_OP_FAILED);
}
/* Get the host FQDN format */
"returned from ldap_cachemgr"));
return (NS_LDAP_OP_FAILED);
}
return (NS_LDAP_MEMORY);
}
}
mcnt = 0;
ccnt = 0;
for (;;) {
break;
_SASLMECHANISM_LEN) == 0) {
continue;
dptr++;
sizeof (char *) * (mcnt+2));
}
return (NS_LDAP_MEMORY);
}
}
return (NS_LDAP_MEMORY);
}
mcnt++;
}
_SUPPORTEDCONTROL_LEN) == 0) {
continue;
dptr++;
sizeof (char *) * (ccnt+2));
}
return (NS_LDAP_MEMORY);
}
}
return (NS_LDAP_MEMORY);
}
ccnt++;
}
}
}
}
/* clean up door call */
}
return (NS_LDAP_SUCCESS);
}
#ifdef DEBUG
/*
* printCred(): prints the credential structure
*/
static void
{
return;
}
if (cred->hostcertpath)
t, cred->hostcertpath);
}
/*
* printConnection(): prints the connection structure
*/
static void
{
return;
if (con->serverAddr) {
t, con->serverAddr);
}
}
#endif
/*
* addConnection(): inserts a connection in the connection list.
* It will also sets use bit and the thread Id for the thread
* using the connection for the first time.
* Returns: -1 = failure, new Connection ID = success
*/
static int
{
int i;
if (!con)
return (-1);
#ifdef DEBUG
#endif /* DEBUG */
(void) mutex_lock(&sessionPoolLock);
if (sessionPool == NULL) {
sizeof (Connection *));
if (!sessionPool) {
(void) mutex_unlock(&sessionPoolLock);
return (-1);
}
#ifdef DEBUG
#endif /* DEBUG */
}
;
if (i == sessionPoolSize) {
/* run out of array, need to increase sessionPool */
sizeof (Connection *));
if (!cl) {
(void) mutex_unlock(&sessionPoolLock);
return (-1);
}
SESSION_CACHE_INC * sizeof (Connection *));
sessionPool = cl;
#ifdef DEBUG
#endif /* DEBUG */
}
sessionPool[i] = con;
(void) mutex_unlock(&sessionPoolLock);
#ifdef DEBUG
#endif /* DEBUG */
return (i + CONID_OFFSET);
}
/*
* findConnection(): find an available connection from the list
* that matches the criteria specified in Connection structure.
* If serverAddr is NULL, then find a connection to any server
* as long as it matches the rest of the parameters.
* Returns: -1 = failure, the Connection ID found = success.
*/
static int
{
int i;
#ifdef DEBUG
thread_t t;
#endif /* DEBUG */
return (-1);
/*
* If a new connection is requested, no need to continue.
* If the process is not nscd and is not requesting keep
* connections alive, no need to continue.
*/
return (-1);
#ifdef DEBUG
t = thr_self();
if (serverAddr && *serverAddr)
t, serverAddr);
else
#endif /* DEBUG */
if (sessionPool == NULL)
return (-1);
(void) mutex_lock(&sessionPoolLock);
for (i = 0; i < sessionPoolSize; ++i) {
if (sessionPool[i] == NULL)
continue;
cp = sessionPool[i];
#ifdef DEBUG
"tid: %d: checking connection [%d] ....\n", t, i);
#endif /* DEBUG */
continue;
continue;
/* found an available connection */
(void) mutex_unlock(&sessionPoolLock);
#ifdef DEBUG
"tid %d: Connection found cID=%d\n", t, i);
#endif /* DEBUG */
return (i + CONID_OFFSET);
}
(void) mutex_unlock(&sessionPoolLock);
return (-1);
}
/*
* Free a Connection structure
*/
void
{
return;
if (con->serverAddr)
if (con->saslMechanisms) {
}
}
}
/*
* Get the server capabilities from the root attributes
*
* This function is similar to __ns_ldap_getRootDSE
* in ns_standalone.c that it gets the server capabilities
* to send over a door call.
*
* This function is used to obtain the same server information
* when it is not supplied by the ldap_cachemgr.
*
* Returns: LDAP status
*/
static int
{
int ns_rc;
int ldap_rc;
char *attr;
int i;
char **vec;
char **value;
/* get search timeout value */
if (ns_rc == NS_LDAP_SUCCESS) {
(void) __ns_ldap_freeParam(¶mVal);
}
(void) __ns_ldap_freeError(&error);
}
attrs[0] = "supportedControl";
if (ldap_rc == LDAP_SUCCESS &&
continue;
}
continue;
continue;
}
== NULL) {
break;
}
}
continue;
}
if (strcasecmp(attr,
"supportedControl") == 0)
else
sinfo->serverType =
} else if (strcasecmp(attr,
_ISGLOBALCATALOGREADY) == 0) {
sinfo->serverType =
}
}
}
(void) ldap_msgfree(resultMsg);
return (ldap_rc);
}
/*
* Find a connection matching the passed in criteria. If an open
* connection with that criteria exists use it, otherwise open a
* new connection.
* Success: returns the pointer to the Connection structure
* Failure: returns NULL, error code and message should be in errorp
*/
static int
{
int ldap_rc;
int passwd_mgmt = 0;
return (NS_LDAP_INVALID_PARAM);
/* connection found in cache */
#ifdef DEBUG
#endif /* DEBUG */
return (NS_LDAP_SUCCESS);
}
} else {
}
if (serverAddr) {
if (__s_api_isInitializing()) {
/*
* When obtaining the root DSE, connect to the server
* passed here through the serverAddr parameter
*/
return (NS_LDAP_MEMORY);
if (strcmp(serverAddrType,
NS_CACHE_ADDR_HOSTNAME) == 0) {
&sinfo.serverFQDN);
if (rc != NS_LDAP_SUCCESS) {
sizeof (errmsg),
gettext("The %s address "
"can not be resolved into "
"a host name. Returning "
"the address as it is."),
*errorp,
return (NS_LDAP_INTERNAL);
}
}
} else {
/*
* We're given the server address, just use it.
* to be a FQDN. We assume this is the case for now.
*
* Only the server address fields of sinfo structure
* are filled in since these are the only relevant
* data that we have. Other fields of this structure
* (controls, saslMechanisms) are kept to NULL.
*/
return (NS_LDAP_MEMORY);
}
return (NS_LDAP_MEMORY);
}
}
}
if ((rc == NS_LDAP_SUCCESS ||
rc == NS_LDAP_SUCCESS_WITH_INFO) &&
(flags & NS_LDAP_NO_ROOT_DSE_INFO) == 0) {
/* Get the servers capabilities */
/* Check status for a bad connection */
ldap_rc == LDAP_UNAVAILABLE ||
ldap_rc == LDAP_CONNECT_ERROR ||
ldap_rc == LDAP_SERVER_DOWN ||
ldap_rc == LDAP_TIMEOUT) {
/* First free any password info in errorp */
if (rc == NS_LDAP_SUCCESS_WITH_INFO) {
(void) __ns_ldap_freeError(errorp);
}
gettext("makeConnection: connection failed "
"when retrieving rootDSE attributes - %s"),
(void) ldap_unbind(ld);
return (rc);
}
}
goto create_con;
}
"failed to open connection using "
} else {
"failed to open connection to"),
*bindHost);
}
return (rc);
}
/* No cached connection, create one */
for (; ; ) {
hReq = NS_CACHE_NEW;
else
/* Log the error */
if (*errorp) {
"unable to make LDAP connection, "
"request for a server failed"),
}
if (host)
return (NS_LDAP_OP_FAILED);
}
if (host)
return (NS_LDAP_MEMORY);
}
/* check if server supports password management */
/* check if server supports password less account mgmt */
if (nopasswd_acct_mgmt &&
"provide account information without password",
host);
return (NS_LDAP_OP_FAILED);
}
/* make the connection */
/* if success, go to create connection structure */
if (rc == NS_LDAP_SUCCESS ||
rc == NS_LDAP_SUCCESS_WITH_INFO) {
break;
}
/*
* If not able to reach the server, inform the ldap
* cache manager that the server should be removed
* from its server list. Thus, the manager will not
* return this server on the next get-server request
* and will also reduce the server list refresh TTL,
* so that it will find out sooner when the server
* is up again.
*/
/* Reset memory allocation error */
memerr = 0;
/*
* We contacted a server that we could
* not either authenticate to or contact.
* If it is due to authentication, then
* we need to try the server again. So,
* do not remove the server yet, but
* add it to the bad server list.
* The caller routine will remove
* the servers if:
* a). A good server is found or
* b). All the possible methods
* are tried without finding
* a good server
*/
(sizeof (char *) * NUMTOMALLOC))) {
memerr = 1;
}
/* Allocate memory in chunks of NUMTOMALLOC */
} else if ((totalbad % NUMTOMALLOC) ==
NUMTOMALLOC - 1) {
char **tmpptr;
*badsrvrs,
(sizeof (char *) * NUMTOMALLOC *
memerr = 1;
} else {
}
}
/*
* Store host only if there were no unsuccessful
* memory allocations above
*/
if (!memerr &&
memerr = 1;
totalbad--;
}
}
}
/* else, cleanup and go for the next server */
/* Return if we had memory allocation errors */
if (memerr)
return (NS_LDAP_MEMORY);
if (*errorp) {
/*
* If openConnection() failed due to
* password policy, or invalid credential,
* keep *errorp and exit
*/
return (rc);
} else {
(void) __ns_ldap_freeError(errorp);
}
}
}
/* we have created ld, setup con structure */
if (host)
/*
* If password control attached in **errorp,
* e.g. rc == NS_LDAP_SUCCESS_WITH_INFO,
* free the error structure
*/
if (*errorp) {
(void) __ns_ldap_freeError(errorp);
}
(void) ldap_unbind(ld);
return (NS_LDAP_MEMORY);
}
}
(void) ldap_unbind(ld);
/*
* If password control attached in **errorp,
* e.g. rc == NS_LDAP_SUCCESS_WITH_INFO,
* free the error structure
*/
if (*errorp) {
(void) __ns_ldap_freeError(errorp);
}
return (NS_LDAP_MEMORY);
}
/* add MT connection to the MT connection pool */
return (exit_rc);
} else {
(void) ldap_unbind(ld);
}
}
/* MT connection not supported or not required case */
(void) ldap_unbind(ld);
/*
* If password control attached in **errorp,
* e.g. rc == NS_LDAP_SUCCESS_WITH_INFO,
* free the error structure
*/
if (*errorp) {
(void) __ns_ldap_freeError(errorp);
}
return (NS_LDAP_MEMORY);
}
#ifdef DEBUG
#endif /* DEBUG */
return (exit_rc);
}
/*
* Return the specified connection to the pool. If necessary
* delete the connection.
*/
static void
{
int id;
return;
#ifdef DEBUG
"tid %d: Dropping connection cID=%d flag=0x%x\n",
#endif /* DEBUG */
if (use_mutex)
(void) mutex_lock(&sessionPoolLock);
/* sanity check before removing */
if (use_mutex)
(void) mutex_unlock(&sessionPoolLock);
return;
}
if (!fini &&
((flag & NS_LDAP_NEW_CONN) == 0) &&
__s_api_peruser_proc())) {
/* release Connection (keep alive) */
/*
* Do sanity cleanup of remaining results.
*/
(void) ldap_msgfree(res);
}
if (use_mutex)
(void) mutex_unlock(&sessionPoolLock);
} else {
/* delete Connection (disconnect) */
if (use_mutex)
(void) mutex_unlock(&sessionPoolLock);
}
}
void
{
}
/*
* This routine is called after a bind operation is
* done in openConnection() to process the password
* management information, if any.
*
* Input:
* bind_type: "simple" or "sasl/DIGEST-MD5"
* ldaprc : ldap rc from the ldap bind operation
* controls : controls returned by the server
* errmsg : error message from the server
* fail_if_new_pwd_reqd:
* flag indicating if connection should be open
* when password needs to change immediately
* passwd_mgmt:
* flag indicating if server supports password
*
* Output : ns_ldap_error structure, which may contain
* password status and number of seconds until
* expired
*
* return rc:
* NS_LDAP_EXTERNAL: error, connection should not open
* NS_LDAP_SUCCESS_WITH_INFO: OK to open but password info attached
* NS_LDAP_SUCCESS: OK to open connection
*
*/
static int
int fail_if_new_pwd_reqd,
int passwd_mgmt)
{
int exit_rc;
int sec_until_exp = 0;
/*
* errmsg may be an empty string,
* even if ldaprc is LDAP_SUCCESS,
* free the empty string if that's the case
*/
if (errmsg &&
}
if (ldaprc != LDAP_SUCCESS) {
/*
* try to map ldap rc and error message to
* a password status
*/
if (errmsg) {
if (passwd_mgmt)
}
gettext("openConnection: "
"%s bind failed "
if (pwd_status != NS_PASSWD_GOOD) {
pwd_status, 0, NULL);
} else {
}
if (controls)
return (NS_LDAP_INTERNAL);
}
/*
* ldaprc is LDAP_SUCCESS,
* process the password management controls, if any
*/
if (controls && passwd_mgmt) {
/*
* The control with the OID
* 2.16.840.1.113730.3.4.4 (or
* LDAP_CONTROL_PWEXPIRED, as defined
* in the ldap.h header file) is the
* expired password control.
*
* This control is used if the server
* is configured to require users to
* change their passwords when first
* logging in and whenever the
* passwords are reset.
*
* If the user is logging in for the
* first time or if the user's
* password has been reset, the
* server sends this control to
* indicate that the client needs to
* change the password immediately.
*
* At this point, the only operation
* that the client can perform is to
* change the user's password. If the
* client requests any other LDAP
* operation, the server sends back
* an LDAP_UNWILLING_TO_PERFORM
* result code with an expired
* password control.
*
* The control with the OID
* 2.16.840.1.113730.3.4.5 (or
* LDAP_CONTROL_PWEXPIRING, as
* defined in the ldap.h header file)
* is the password expiration warning
* control.
*
* This control is used if the server
* is configured to expire user
* passwords after a certain amount
* of time.
*
* The server sends this control back
* to the client if the client binds
* using a password that will soon
* expire. The ldctl_value field of
* the LDAPControl structure
* specifies the number of seconds
* before the password will expire.
*/
LDAP_CONTROL_PWEXPIRED) == 0) {
/*
* if the caller wants this bind
* to fail, set up the error info.
* If call to this function is
* for searching the LDAP directory,
* e.g., __ns_ldap_list(),
* there's really no sense to
* let a connection open and
* then fail immediately afterward
* on the LDAP search operation with
* the LDAP_UNWILLING_TO_PERFORM rc
*/
if (fail_if_new_pwd_reqd) {
sizeof (errstr),
"openConnection: "
"%s bind "
"failed "
"- password "
"expired. It "
" needs to change "
"immediately!"),
0,
NULL);
} else {
NULL,
0,
NULL);
exit_rc =
}
break;
LDAP_CONTROL_PWEXPIRING) == 0) {
if ((*ctrl)->
ldctl_value.bv_len > 0 &&
(*ctrl)->
NULL,
NULL);
exit_rc =
break;
}
}
}
if (controls)
return (exit_rc);
}
static int
{
const char *name;
int found = 0;
return (-1);
}
/* check for skip and count other backends */
found = 1;
break;
}
}
(void) __nsw_freeconfig(conf);
return (found);
}
static int
int fail_if_new_pwd_reqd, int passwd_mgmt,
{
int zero = 0;
char *s;
int followRef;
/* determine if the host name contains a port number */
if (s != NULL) {
sizeof (errstr),
gettext("openConnection: cannot "
"convert %s into a valid "
"port number for the "
"%s server. A default value "
"will be used."),
s,
} else {
*s = '\0';
}
}
port,
&ld,
errorp);
if (s != NULL) {
*s = ':';
}
if (ret_code != NS_LDAP_SUCCESS) {
return (ret_code);
}
/* check to see if the underlying libsldap supports MT connection */
int rc;
errorp);
if (rc != NS_LDAP_SUCCESS) {
(void) ldap_unbind(ld);
return (rc);
}
}
/*
* This library will handle the referral itself based on API flags or
* configuration file specification. The LDAP bind operation is an
* exception where we rely on the LDAP library to follow the referal.
*
* The LDAP follow referral option must be set to OFF for the libldap5
* to pass the referral info up to this library. This option MUST be
* set to OFF after we have performed a sucessful bind. If we are not
* to follow referrals we MUST also set the LDAP follow referral option
* to OFF before we perform an LDAP bind.
*/
if (ret_code != NS_LDAP_SUCCESS) {
(void) ldap_unbind(ld);
return (ret_code);
}
if (followRef)
else
/* retry if LDAP I/O was interrupted */
ld,
if (ret_code == NS_LDAP_SUCCESS ||
/*
* Turn off LDAP referral following so that this library can
* process referrals.
*/
}
return (ret_code);
}
/*
* FUNCTION: __s_api_getDefaultAuth
*
* Constructs a credential for authentication using the config module.
*
* RETURN VALUES:
*
* NS_LDAP_SUCCESS If successful
* NS_LDAP_CONFIG If there are any config errors.
* NS_LDAP_MEMORY Memory errors.
* NS_LDAP_OP_FAILED If there are no more authentication methods so can
* not build a new authp.
* NS_LDAP_INVALID_PARAM This overloaded return value means that some of the
* necessary fields of a cred for a given auth method
* are not provided.
* INPUT:
*
* cLevel Currently requested credential level to be tried
*
* aMethod Currently requested authentication method to be tried
*
* getAdmin If non 0, get Admin -i.e., not proxyAgent- DN and password
*
* OUTPUT:
*
* authp authentication method to use.
*/
static int
int *cLevel,
int getAdmin)
{
int getUid = 0;
int getPasswd = 0;
int getCertpath = 0;
int rc = 0;
#ifdef DEBUG
#endif
/* Require an Auth */
return (NS_LDAP_INVALID_PARAM);
}
/*
*/
return (NS_LDAP_INVALID_PARAM);
return (NS_LDAP_MEMORY);
case NS_LDAP_AUTH_NONE:
return (NS_LDAP_SUCCESS);
case NS_LDAP_AUTH_SIMPLE:
getUid++;
getPasswd++;
break;
case NS_LDAP_AUTH_SASL:
getUid++;
getPasswd++;
(void) __ns_ldap_freeCred(authp);
return (NS_LDAP_INVALID_PARAM);
}
break;
case NS_LDAP_AUTH_TLS:
getUid++;
getPasswd++;
getCertpath++;
getCertpath++;
} else {
(void) __ns_ldap_freeCred(authp);
return (NS_LDAP_INVALID_PARAM);
}
break;
}
if (getUid) {
if (getAdmin) {
/*
* Assume AdminCred has been retrieved from
* ldap_cachemgr already. It will not work
* without userID or password. Flags getUid
* and getPasswd should always be set
* together.
*/
(void) __ns_ldap_freeCred(authp);
return (NS_LDAP_MEMORY);
}
if (rc != NS_LDAP_SUCCESS) {
(void) __ns_ldap_freeCred(authp);
(void) __ns_ldap_freeUnixCred(&AdminCred);
(void) __ns_ldap_freeError(&errorp);
return (rc);
}
(void) __ns_ldap_freeCred(authp);
(void) __ns_ldap_freeUnixCred(&AdminCred);
return (NS_LDAP_INVALID_PARAM);
}
} else {
if (rc != NS_LDAP_SUCCESS) {
(void) __ns_ldap_freeCred(authp);
(void) __ns_ldap_freeError(&errorp);
return (rc);
}
(void) __ns_ldap_freeCred(authp);
return (NS_LDAP_INVALID_PARAM);
}
(void) __ns_ldap_freeParam(¶mVal);
}
(void) __ns_ldap_freeCred(authp);
(void) __ns_ldap_freeUnixCred(&AdminCred);
return (NS_LDAP_MEMORY);
}
}
if (getPasswd) {
if (getAdmin) {
/*
* Assume AdminCred has been retrieved from
* ldap_cachemgr already. It will not work
* without the userID anyway because for
* getting admin credential, flags getUid
* and getPasswd should always be set
* together.
*/
(void) __ns_ldap_freeCred(authp);
(void) __ns_ldap_freeUnixCred(&AdminCred);
return (NS_LDAP_INVALID_PARAM);
}
} else {
if (rc != NS_LDAP_SUCCESS) {
(void) __ns_ldap_freeCred(authp);
(void) __ns_ldap_freeError(&errorp);
return (rc);
}
(void) __ns_ldap_freeCred(authp);
return (NS_LDAP_INVALID_PARAM);
}
(void) __ns_ldap_freeParam(¶mVal);
}
(void) __ns_ldap_freeCred(authp);
(void) __ns_ldap_freeUnixCred(&AdminCred);
if (modparamVal != NULL)
return (NS_LDAP_INVALID_PARAM);
}
}
if (getCertpath) {
(void) __ns_ldap_freeCred(authp);
(void) __ns_ldap_freeUnixCred(&AdminCred);
(void) __ns_ldap_freeError(&errorp);
return (rc);
}
(void) __ns_ldap_freeCred(authp);
(void) __ns_ldap_freeUnixCred(&AdminCred);
return (NS_LDAP_INVALID_PARAM);
}
(void) __ns_ldap_freeParam(¶mVal);
(void) __ns_ldap_freeCred(authp);
(void) __ns_ldap_freeUnixCred(&AdminCred);
return (NS_LDAP_MEMORY);
}
}
(void) __ns_ldap_freeUnixCred(&AdminCred);
return (NS_LDAP_SUCCESS);
}
/*
* FUNCTION: getConnection
*
* internal version of __s_api_getConnection()
*/
static int
const char *server,
const int flags,
int fail_if_new_pwd_reqd,
int nopasswd_acct_mgmt,
{
int rc;
return (NS_LDAP_INVALID_PARAM);
}
/* reuse MT connection if needed and if available */
if (rc != NS_LDAP_NOTFOUND)
return (rc);
}
/* get profile version number */
return (rc);
"version"));
return (NS_LDAP_CONFIG);
}
(void) __ns_ldap_freeParam((void ***)¶mVal);
/* Get the bind timeout value */
timeoutSec = **((int **)paramVal);
(void) __ns_ldap_freeParam(¶mVal);
}
if (*errorp)
(void) __ns_ldap_freeError(errorp);
/* Get the authentication method list */
return (rc);
return (NS_LDAP_MEMORY);
return (NS_LDAP_MEMORY);
}
if (version == NS_LDAP_V1)
else {
}
}
/* Get the credential level list */
(void) __ns_ldap_freeParam((void ***)&aMethod);
return (rc);
}
return (NS_LDAP_MEMORY);
return (NS_LDAP_MEMORY);
if (version == NS_LDAP_V1)
*(cLevel[0]) = NS_LDAP_CRED_PROXY;
else
*(cLevel[0]) = NS_LDAP_CRED_ANON;
}
}
/* setup the anon credential for anonymous connection */
for (;;) {
/* using specified auth method */
/* not using bad server if credentials were supplied */
}
if (rc == NS_LDAP_SUCCESS ||
rc == NS_LDAP_SUCCESS_WITH_INFO) {
break;
}
} else {
/* for every cred level */
if (self_gssapi_only &&
**cNext != NS_LDAP_CRED_SELF)
continue;
if (**cNext == NS_LDAP_CRED_ANON) {
/*
* make connection anonymously
* Free the down server list before
* looping through
*/
}
if (rc == NS_LDAP_SUCCESS ||
rc ==
goto done;
}
continue;
}
/* for each cred level */
if (self_gssapi_only &&
continue;
/*
* and non-self coexists with non-gssapi
* only
*/
if ((**cNext == NS_LDAP_CRED_SELF &&
(**cNext != NS_LDAP_CRED_SELF &&
continue;
/* make connection and authenticate */
/* with default credentials */
if (rc != NS_LDAP_SUCCESS) {
continue;
}
/*
* Free the down server list before
* looping through
*/
}
(void) __ns_ldap_freeCred(&authp);
if (rc == NS_LDAP_SUCCESS ||
rc ==
goto done;
}
}
}
}
if (flags & NS_LDAP_HARD) {
if (sec < LDAPMAXHARDLOOKUPTIME)
sec *= 2;
} else {
break;
}
}
done:
/*
* configured
*/
rc = NS_LDAP_CONFIG;
}
(void) __ns_ldap_freeParam((void ***)&aMethod);
(void) __ns_ldap_freeParam((void ***)&cLevel);
/*
* At this point, either we have a successful
* connection or exhausted all the possible auths.
* and creds. Mark the problem servers as down
* so that the problem servers are not contacted
* again until the refresh_ttl expires.
*/
(void) __s_api_removeBadServers(badSrvrs);
}
return (rc);
}
/*
* FUNCTION: __s_api_getConnection
*
* Bind to the specified server or one from the server
* list and return the pointer.
*
* This function can rebind or not (NS_LDAP_HARD), it can require a
* credential or bind anonymously
*
* This function follows the DUA configuration schema algorithm
*
* RETURN VALUES:
*
* NS_LDAP_SUCCESS A connection was made successfully.
* NS_LDAP_SUCCESS_WITH_INFO
* A connection was made successfully, but with
* password management info in *errorp
* NS_LDAP_INVALID_PARAM If any invalid arguments were passed to the function.
* NS_LDAP_CONFIG If there are any config errors.
* NS_LDAP_MEMORY Memory errors.
* NS_LDAP_INTERNAL If there was a ldap error.
*
* INPUT:
*
* server Bind to this LDAP server only
* flags If NS_LDAP_HARD is set function will not return until it has
* a connection unless there is a authentication problem.
* If NS_LDAP_NEW_CONN is set the function must force a new
* connection to be created
* If NS_LDAP_KEEP_CONN is set the connection is to be kept open
* auth Credentials for bind. This could be NULL in which case
* a default cred built from the config module is used.
* sessionId cookie that points to a previous session
* fail_if_new_pwd_reqd
* a flag indicating this function should fail if the passwd
* in auth needs to change immediately
* nopasswd_acct_mgmt
* a flag indicating that makeConnection should check before
* binding if server supports LDAP V3 password less
* account management
*
* OUTPUT:
*
* session pointer to a session with connection information
* errorp Set if there are any INTERNAL, or CONFIG error.
*/
int
const char *server,
const int flags,
int fail_if_new_pwd_reqd,
int nopasswd_acct_mgmt,
{
int rc;
}
return (rc);
}
void
{
int id;
(void) mutex_lock(&sessionPoolLock);
if (sessionPool != NULL) {
sessionPool = NULL;
sessionPoolSize = 0;
}
(void) mutex_unlock(&sessionPoolLock);
}
/*
* This function initializes a TLS LDAP session. On success LDAP* is returned
* (pointed by *ldp). Otherwise, the function returns an NS error code and
* provide an additional info pointed by *errorp.
*/
static
{
const char *hostcertpath;
int ldap_rc;
#ifdef DEBUG
thr_self());
#endif /* DEBUG */
timeoutMilliSec) != LDAP_SUCCESS) {
gettext("createTLSSession: failed to initialize "
"TLS security"));
return (NS_LDAP_INTERNAL);
}
if (hostcertpath == NULL) {
}
if (hostcertpath == NULL)
return (NS_LDAP_MEMORY);
}
gettext("createTLSSession: failed to initialize "
"TLS security (%s)"),
return (NS_LDAP_INTERNAL);
}
if (alloc_hcp)
gettext("createTLSSession: failed to connect "
return (NS_LDAP_INTERNAL);
}
return (NS_LDAP_SUCCESS);
}
/*
* Convert (resolve) hostname to IP address.
*
* INPUT:
*
* server - \[IPv6_address\][:port]
* - IPv4_address[:port]
* - hostname[:port]
*
* newaddr - Buffer to which this function writes resulting address,
* including the port number, if specified in server argument.
*
* newaddr_size - Size of the newaddr buffer.
*
* errstr - Buffer to which error string is written if error occurs.
*
* errstr_size - Size of the errstr buffer.
*
* OUTPUT:
*
* Returns 1 for success, 0 in case of error.
*
* newaddr - See above (INPUT section).
*
* errstr - See above (INPUT section).
*/
static int
char *errstr, int errstr_size)
{
char *s;
unsigned short port = 0;
int err;
/* Determine if the host name contains a port number. */
/* Skip over IPv6 address. */
if (s != NULL) {
/* Address misformatted. No port number after : */
gettext("Invalid host:port format"));
return (0);
} else
/* Cut off the :<port> part. */
*s = '\0';
}
buffer[0] = '\0';
/*
* Resolve hostname and fill in hostent structure.
*/
&err)) {
/*
* The only possible error here could be TRY_AGAIN if buffer was
* not big enough. NSS_BUFLEN_HOSTS should have been enough
* though.
*/
gettext("Unable to resolve address."));
return (0);
}
buffer[0] = '\0';
/*
* Convert the address to string.
*/
NSS_BUFLEN_HOSTS)) {
/* There's not much we can do. */
gettext("Unable to convert address to string."));
return (0);
}
/* Put together the address and the port */
if (port > 0) {
switch (result.h_addrtype) {
case AF_INET6:
/* [IP]:<port>\0 */
"[%s]:%hu",
port);
break;
/* AF_INET */
default :
/* IP:<port>\0 */
"%s:%hu",
port);
break;
}
} else {
}
return (1);
}
/*
* This finction initializes a none-TLS LDAP session. On success LDAP*
* is returned (pointed by *ldp). Otherwise, the function returns
* an NS error code and provides an additional info pointed by *errorp.
*/
static
{
char *addr;
int is_ip = 0;
/* [INET6_ADDRSTRLEN]:<port>\0 */
#ifdef DEBUG
thr_self());
#endif /* DEBUG */
if (gssapi == 0) {
__s_api_isipv6((char *)serverAddr));
}
/*
* Let's try to resolve IP address of server.
*/
ldap_in_nss_switch((char *)"ipnodes") > 0)) {
return (NS_LDAP_MEMORY);
svraddr[0] = '\0';
} else {
return (NS_LDAP_INTERNAL);
}
}
/* Warning message IF cannot connect to host(s) */
strdup(p), NS_LDAP_MEMORY);
return (NS_LDAP_INTERNAL);
}
return (NS_LDAP_SUCCESS);
}
/*
* This finction initializes an LDAP session.
*
* INPUT:
* auth - a structure specified an authenticastion method and credentials,
* serverAddr - the address of a server to which a connection
* will be established,
* port - a port being listened by the server,
* timeoutMilliSec - a timeout in milliseconds for the Bind operation.
*
* OUTPUT:
* ldp - a pointer to an LDAP structure which will be used
* for all the subsequent operations against the server.
* If an error occurs, the function returns an NS error code
* and provides an additional info pointed by *errorp.
*/
static
{
case NS_LDAP_AUTH_NONE:
case NS_LDAP_AUTH_SIMPLE:
case NS_LDAP_AUTH_SASL:
break;
case NS_LDAP_AUTH_TLS:
useSSL = 1;
break;
default:
gettext("openConnection: unsupported "
return (NS_LDAP_INTERNAL);
}
if (port == USE_DEFAULT_PORT) {
}
gssapi = 1;
if (useSSL)
else
}
/*
* This finction performs a non-SASL bind operation. If an error accures,
* the function returns an NS error code and provides an additional info
* pointed by *errorp.
*/
static
int timeoutSec,
int fail_if_new_pwd_reqd,
int passwd_mgmt)
{
} else {
"missing credentials for Simple bind"));
(void) ldap_unbind(ld);
return (NS_LDAP_INTERNAL);
}
}
#ifdef DEBUG
thr_self());
#endif /* DEBUG */
if (msgId == -1) {
(void *)&errnum);
gettext("openConnection: simple bind failed "
(void) ldap_unbind(ld);
return (NS_LDAP_INTERNAL);
}
(void *)&errnum);
gettext("openConnection: simple bind failed "
(void) ldap_msgfree(resultMsg);
(void) ldap_unbind(ld);
return (NS_LDAP_INTERNAL);
}
/*
* get ldaprc, controls, and error msg
*/
if (ldap_rc != LDAP_SUCCESS) {
gettext("openConnection: simple bind failed "
"- unable to parse result"));
(void) ldap_unbind(ld);
return (NS_LDAP_INTERNAL);
}
/* process the password management info, if any */
if (ret_code == NS_LDAP_INTERNAL) {
(void) ldap_unbind(ld);
}
/*
* If not SSL connection or error occurred, done.
* Otherwise, reset the I/O max. timeout, which was
* set to the configured bind time limit in createTLSession().
*/
(ret_code != NS_LDAP_SUCCESS &&
return (ret_code);
/*
* Reset the I/O max. timeout to no timeout, so that the configured
* libsldap search time limit will be honored for this connection.
*/
return (ret_code);
}
/*
* This finction performs a SASL bind operation. If an error accures,
* the function returns an NS error code and provides an additional info
* pointed by *errorp.
*/
static
int timeoutSec,
int fail_if_new_pwd_reqd,
int passwd_mgmt)
{
gettext("openConnection: SASL options are "
"not supported (%d) for non-GSSAPI sasl bind"),
(void) ldap_unbind(ld);
return (NS_LDAP_INTERNAL);
}
gettext("openConnection: missing credentials "
"for SASL bind"));
(void) ldap_unbind(ld);
return (NS_LDAP_INTERNAL);
}
}
case NS_LDAP_SASL_CRAM_MD5:
/*
* NOTE: if iDS changes to support cram_md5,
* please add password management code here.
* Since ldap_sasl_cram_md5_bind_s does not
* return anything that could be used to
* determine if bind failed due to password
* policy, a new cram_md5_bind API will need
* to be introduced. See
* ldap_x_sasl_digest_md5_bind() and case
* NS_LDAP_SASL_DIGEST_MD5 below for details.
*/
(void) ldap_get_option(ld,
LDAP_OPT_ERROR_NUMBER, (void *)&errnum);
gettext("openConnection: "
(void) ldap_unbind(ld);
return (NS_LDAP_INTERNAL);
}
break;
case NS_LDAP_SASL_DIGEST_MD5:
/* 5 = strlen("dn: ") + 1 */
if (digest_md5_name == NULL) {
(void) ldap_unbind(ld);
return (NS_LDAP_MEMORY);
}
(void) ldap_get_option(ld,
LDAP_OPT_ERROR_NUMBER, (void *)&errnum);
gettext("openConnection: "
"DIGEST-MD5 bind failed - %s"),
(void) ldap_unbind(ld);
return (NS_LDAP_INTERNAL);
}
/*
* get ldaprc, controls, and error msg
*/
if (ldap_rc != LDAP_SUCCESS) {
gettext("openConnection: "
"DIGEST-MD5 bind failed "
"- unable to parse result"));
(void) ldap_unbind(ld);
return (NS_LDAP_INTERNAL);
}
/* process the password management info, if any */
if (ret_code == NS_LDAP_INTERNAL) {
(void) ldap_unbind(ld);
}
break;
case NS_LDAP_SASL_GSSAPI:
if (sasl_gssapi_inited == 0) {
if (ret_code != NS_LDAP_SUCCESS) {
gettext("openConnection: "
"GSSAPI initialization "
"failed"));
(void) ldap_unbind(ld);
return (ret_code);
}
}
(void) memset(&sasl_param, 0,
sizeof (ns_sasl_cb_param_t));
(void *)&min_ssf);
(void *)&max_ssf);
&sasl_param);
if (ldap_rc != LDAP_SUCCESS) {
gettext("openConnection: "
"GSSAPI bind failed "
"- %d %s"),
(void) ldap_unbind(ld);
return (NS_LDAP_INTERNAL);
}
break;
default:
(void) ldap_unbind(ld);
gettext("openConnection: unsupported SASL "
return (NS_LDAP_INTERNAL);
}
return (ret_code);
}
/*
* This function performs an LDAP Bind operation proceeding
* from a type of the connection specified by auth->auth.type.
*
* INPUT:
* auth - a structure specified an authenticastion method and credentials,
* ld - a pointer returned by the createSession() function,
* timeoutSec - a timeout in seconds for the Bind operation,
* fail_if_new_pwd_reqd - a flag indicating that the call should fail
* if a new password is required,
* passwd_mgmt - a flag indicating that the server supports
* password management.
*
* OUTPUT:
* If an error accures, the function returns an NS error code
* and provides an additional info pointed by *errorp.
*/
static
int timeoutSec,
int fail_if_new_pwd_reqd,
int passwd_mgmt)
{
int bindType;
int timeoutSec,
int fail_if_new_pwd_reqd,
int passwd_mgmt) = NULL;
if (!ld) {
"performBind: LDAP session "
"is not initialized.");
return (NS_LDAP_INTERNAL);
}
switch (bindType) {
case NS_LDAP_AUTH_NONE:
#ifdef DEBUG
thr_self());
#endif /* DEBUG */
break;
case NS_LDAP_AUTH_SIMPLE:
break;
case NS_LDAP_AUTH_SASL:
binder = doSASLBind;
break;
default:
gettext("openConnection: unsupported "
"authentication method "
"(%d)"), bindType);
(void) ldap_unbind(ld);
return (NS_LDAP_INTERNAL);
}
ld,
}
return (NS_LDAP_SUCCESS);
}