#ifdef _SOLARIS_SDK
#include "solaris-int.h"
#include <libintl.h>
#include <syslog.h>
#include <nsswitch.h>
#include <synch.h>
#include <nss_dbdefs.h>
#include <netinet/in.h>
static char *host_service = NULL;
static DEFINE_NSS_DB_ROOT(db_root_hosts);
#endif
#include "ldappr-int.h"
static LDAPHostEnt *prldap_gethostbyname( const char *name,
LDAPHostEnt *result, char *buffer, int buflen, int *statusp,
void *extradata );
static LDAPHostEnt *prldap_gethostbyaddr( const char *addr, int length,
int type, LDAPHostEnt *result, char *buffer, int buflen,
int *statusp, void *extradata );
static int prldap_getpeername( LDAP *ld, struct sockaddr *addr,
char *buffer, int buflen );
static LDAPHostEnt *prldap_convert_hostent( LDAPHostEnt *ldhp,
PRHostEnt *prhp );
#ifdef _SOLARIS_SDK
static LDAPHostEnt *
prldap_gethostbyname1(const char *name, LDAPHostEnt *result,
char *buffer, int buflen, int *statusp, void *extradata);
extern int
str2hostent(const char *instr, int lenstr, void *ent, char *buffer,
int buflen);
#endif
int
prldap_install_dns_functions( LDAP *ld )
{
struct ldap_dns_fns dnsfns;
memset( &dnsfns, '\0', sizeof(struct ldap_dns_fns) );
dnsfns.lddnsfn_bufsize = PR_NETDB_BUF_SIZE;
dnsfns.lddnsfn_gethostbyname = prldap_gethostbyname;
dnsfns.lddnsfn_gethostbyaddr = prldap_gethostbyaddr;
dnsfns.lddnsfn_getpeername = prldap_getpeername;
if ( ldap_set_option( ld, LDAP_OPT_DNS_FN_PTRS, (void *)&dnsfns ) != 0 ) {
return( -1 );
}
return( 0 );
}
static LDAPHostEnt *
prldap_gethostbyname( const char *name, LDAPHostEnt *result,
char *buffer, int buflen, int *statusp, void *extradata )
{
PRHostEnt prhent;
if( !statusp || ( *statusp = (int)PR_GetIPNodeByName( name,
PRLDAP_DEFAULT_ADDRESS_FAMILY, PR_AI_DEFAULT,
buffer, buflen, &prhent )) == PR_FAILURE ) {
return( NULL );
}
return( prldap_convert_hostent( result, &prhent ));
}
static LDAPHostEnt *
prldap_gethostbyaddr( const char *addr, int length, int type,
LDAPHostEnt *result, char *buffer, int buflen, int *statusp,
void *extradata )
{
PRHostEnt prhent;
PRNetAddr iaddr;
if ( PR_SetNetAddr(PR_IpAddrNull, PRLDAP_DEFAULT_ADDRESS_FAMILY,
0, &iaddr) == PR_FAILURE
|| PR_StringToNetAddr( addr, &iaddr ) == PR_FAILURE ) {
return( NULL );
}
if( !statusp || (*statusp = PR_GetHostByAddr(&iaddr, buffer,
buflen, &prhent )) == PR_FAILURE ) {
return( NULL );
}
return( prldap_convert_hostent( result, &prhent ));
}
static int
prldap_getpeername( LDAP *ld, struct sockaddr *addr, char *buffer, int buflen)
{
PRLDAPIOSocketArg *sa;
PRFileDesc *fd;
PRNetAddr iaddr;
int ret;
if (NULL != ld) {
ret = prldap_socket_arg_from_ld( ld, &sa );
if (ret != LDAP_SUCCESS) {
return (-1);
}
ret = PR_GetPeerName(sa->prsock_prfd, &iaddr);
if( ret == PR_FAILURE ) {
return( -1 );
}
*addr = *((struct sockaddr *)&iaddr.raw);
ret = PR_NetAddrToString(&iaddr, buffer, buflen);
if( ret == PR_FAILURE ) {
return( -1 );
}
return (0);
}
return (-1);
}
static LDAPHostEnt *
prldap_convert_hostent( LDAPHostEnt *ldhp, PRHostEnt *prhp )
{
ldhp->ldaphe_name = prhp->h_name;
ldhp->ldaphe_aliases = prhp->h_aliases;
ldhp->ldaphe_addrtype = prhp->h_addrtype;
ldhp->ldaphe_length = prhp->h_length;
ldhp->ldaphe_addr_list = prhp->h_addr_list;
return( ldhp );
}
#ifdef _SOLARIS_SDK
int
prldap_x_install_dns_skipdb(LDAP *ld, const char *skip)
{
enum __nsw_parse_err pserr;
struct __nsw_switchconfig *conf;
struct __nsw_lookup *lkp;
struct ldap_dns_fns dns_fns;
char *name_list = NULL;
char *tmp;
const char *name;
int len;
boolean_t got_skip = B_FALSE;
(void) mutex_lock(&db_root_hosts.lock);
conf = __nsw_getconfig("hosts", &pserr);
if (conf == NULL) {
(void) mutex_unlock(&db_root_hosts.lock);
return (0);
}
for (lkp = conf->lookups; lkp != NULL; lkp = lkp->next) {
name = lkp->service_name;
if (strcmp(name, skip) == 0) {
got_skip = B_TRUE;
continue;
}
if (name_list == NULL)
name_list = strdup(name);
else {
len = strlen(name_list);
tmp = realloc(name_list, len + strlen(name) + 2);
if (tmp == NULL) {
free(name_list);
name_list = NULL;
} else {
name_list = tmp;
name_list[len++] = ' ';
(void) strcpy(name_list+len, name);
}
}
if (name_list == NULL) {
(void) mutex_unlock(&db_root_hosts.lock);
__nsw_freeconfig(conf);
return (-1);
}
}
__nsw_freeconfig(conf);
if (!got_skip) {
(void) mutex_unlock(&db_root_hosts.lock);
if (name_list != NULL)
free(name_list);
return (0);
}
if (host_service != NULL)
free(host_service);
host_service = name_list;
(void) mutex_unlock(&db_root_hosts.lock);
if (ldap_get_option(ld, LDAP_OPT_DNS_FN_PTRS, &dns_fns) != 0)
return (-1);
dns_fns.lddnsfn_bufsize = PR_NETDB_BUF_SIZE;
dns_fns.lddnsfn_gethostbyname = prldap_gethostbyname1;
if (ldap_set_option(ld, LDAP_OPT_DNS_FN_PTRS, &dns_fns) != 0)
return (-1);
return (0);
}
static void
prldap_initf_hosts(nss_db_params_t *p)
{
static char *no_service = "";
p->name = NSS_DBNAM_HOSTS;
p->flags |= NSS_USE_DEFAULT_CONFIG;
p->default_config = host_service == NULL ? no_service : host_service;
}
static int
prldap_switch_gethostbyname_r(const char *name,
struct hostent *result, char *buffer, int buflen,
int *h_errnop)
{
nss_XbyY_args_t arg;
nss_status_t res;
struct hostent *resp;
syslog(LOG_INFO, "libldap: Resolving server name \"%s\"", name);
NSS_XbyY_INIT(&arg, result, buffer, buflen, str2hostent);
arg.key.name = name;
arg.stayopen = 0;
res = nss_search(&db_root_hosts, prldap_initf_hosts,
NSS_DBOP_HOSTS_BYNAME, &arg);
arg.status = res;
*h_errnop = arg.h_errno;
resp = (struct hostent *)NSS_XbyY_FINI(&arg);
return (resp != NULL ? PR_SUCCESS : PR_FAILURE);
}
static LDAPHostEnt *
prldap_gethostbyname1(const char *name, LDAPHostEnt *result,
char *buffer, int buflen, int *statusp, void *extradata)
{
int h_errno;
LDAPHostEnt prhent;
memset(&prhent, '\0', sizeof (prhent));
if (!statusp || ( *statusp = prldap_switch_gethostbyname_r(name,
&prhent, buffer, buflen, &h_errno )) == PR_FAILURE) {
syslog(LOG_WARNING, "libldap: server name \"%s\" could not "
"be resolved", name);
return (NULL);
}
return (prldap_convert_hostent(result, &prhent));
}
#endif