/*
 * Copyright (c) 1999 by Sun Microsystems, Inc.
 * All rights reserved.
 */


#ifndef	_NS_INTERNAL_H
#define	_NS_INTERNAL_H

#pragma ident	"@(#)ns_internal.h	1.8	99/11/11 SMI"

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <sys/types.h>
#include <lber.h>
#include <ldap.h>
#include "ns_sldap.h"

/*
 * Internal parameters
 */
#define	MAXERROR		2000
#define	TRUE			1
#define	FALSE			0
#define	NSLDAPDIRECTORY		"/var/ldap"
#define	NSCONFIGFILE		"/var/ldap/ldap_client_file"
#define	NSCONFIGREFRESH		"/var/ldap/ldap_client_file.refresh"
#define	NSCREDFILE		"/var/ldap/ldap_client_cred"
#define	NSCREDREFRESH		"/var/ldap/ldap_client_cred.refresh"
#define	ROTORSIZE		256
#define	MASK			0377
#define	LDAPMAXHARDLOOKUPTIME	256
#define	DONOTEDIT		\
	"Do not edit this file manually; your changes will be lost." \
	"Please use ldapclient (1M) instead."
#define	MAXPORTNUMBER		65535
#define	CREDFILE		0
#define	CONFIGFILE		1
#define	UIDNUMFILTER	"(&(objectclass=posixAccount)(uidnumber=%s))"
#define	UIDFILTER	"(&(objectclass=posixAccount)(uid=%s))"
#define	HOSTFILTER	"(&(objectclass=ipHost)(cn=%s))"

#define	SIMPLEPAGECTRLFLAG	1
#define	VLVCTRLFLAG		2

#define	LISTPAGESIZE		1000
#define	ENUMPAGESIZE		100
#define	SORTKEYLIST		"cn uid"


typedef int ConnectionID;

extern int	__ldap_debug_file;
extern int	__ldap_debug_api;
extern int	__ldap_debug_ldap;
extern int	__ldap_debug_servers;

/* MACROS */

/*
 * MKERROR: builds the error structure and fills in the status and
 * the message.  The message must be a freeable (non-static) string.
 * If it fails to allocate memory for the error structure,
 * it will return the retErr.
 */
#define	MKERROR(err, st, mesg, retErr) \
	if (((err) = calloc(1, sizeof (struct ns_ldap_error))) == NULL) \
		return (retErr); \
	(err)->status = (st); \
	(err)->message = (mesg);

#define	NSLDAPTRACE(variable, setequal, message) \
	if (variable > 0 || ((setequal != 0) && (variable == setequal))) { \
		char buf[BUFSIZ]; \
		(void) snprintf(buf, BUFSIZ, message); \
		(void) write(__ldap_debug_file, buf); \
	}

/* internal connection and sldap-api data structures */

typedef struct ns_ldap_cookie {
	int	msgId;
	int	searchScope;
	char	*filter;
	char	**attribute;
	char	**dns;
	char	*currentdn;
	int	flag;
	unsigned long	vlvIndex;
	struct berval   *ctrlCookie;
} ns_ldap_cookie_t;

/* internal connection APIs */
LDAP *MakeConnection(const char *, Auth_t *, int,
	ConnectionID *, ns_ldap_error_t **);

/* Flags for drop conneciton */
#define	DEL_CONNECTION	0x001
int DropConnection(ConnectionID, int);
int __s_api_getCookieInfo(ConnectionID, ns_ldap_cookie_t **, LDAP **);
int __s_api_setCookieInfo(ConnectionID, ns_ldap_cookie_t *);


/* ************ internal sldap-api functions *********** */
int	__s_api_printResult(ns_ldap_result_t *);
int	__s_api_getSearchScope(int *, const char *, ns_ldap_error_t **);
int	__s_api_getServers(char ***, const char *, ns_ldap_error_t **);
int	__s_api_getDNs(char ***, const char *, const char *,
	ns_ldap_error_t **);
int	__s_api_free2dArray(char **);
int	__s_api_getConnection(char **, int,
	const Auth_t *, LDAP **, int *, ns_ldap_error_t **);
int	__s_api_getConnection_ext(char **, const char *, const int,
	const Auth_t *, LDAP **, int *, int *, ns_ldap_error_t **);
char	**__s_api_cp2dArray(char **);

int	__s_api_isCtrlSupported(LDAP *, char *, ns_ldap_error_t **);
int	__s_api_parsePageControl(LDAP *, LDAPMessage *, int *,
	struct berval **, ns_ldap_error_t **);
int	__s_api_free2dArray(char **);
char	*dvalue(char *);
char	*evalue(char *);
extern void	c_setup();
extern void	get_environment();
extern void	ns_ldaperror_init();

/* internal configuration APIs */
void		__ns_ldap_setServer(int set);
ns_ldap_error_t	*__ns_ldap_LoadConfiguration(char *domainname);
void		__ns_ldap_cache_destroy();
ns_ldap_error_t	*__ns_ldap_LoadDoorInfo(LineBuf *configinfo, char *domainname);
ns_ldap_error_t *__ns_ldap_DumpConfiguration(char *filename);
ns_ldap_error_t	*__ns_ldap_DumpLdif(char *filename);
int		__ns_ldap_cache_ping();

#define	DEFMAX			6
#define	DEFLENGTH		32
#define	TOKENSEPARATOR		'='
#define	QUOTETOK		'"'
#define	SPACETOK		' '
#define	COMMATOK		','
#define	DOORLINESEP		";"
#define	LAST_VALUE		(int) NS_LDAP_PROFILE_P
#define	BUFSIZE			1024
#define	DEFAULTCONFIGNAME	"__default_config"
#define	SERVERCONFIG		1
#define	CLIENTCONFIG		2
#define	CREDCONFIG		3
#define	EXP_DEFAULT_TTL		"43200"	/* 12 hours TTL */
#define	CRYPTMARK		"{NS1}"
#define	DOORBUFFERSIZE		8192

typedef enum {
	NS_SUCCESS,
	NS_NOTFOUND,
	NS_PARSE_ERR
} ns_parse_status;

typedef struct ns_default_config {
	char		*name;			/* parameter name */
	char		*profile_name;		/* profile schema name */
	char		*defval;			/* parameter default */
	char		allowed[DEFMAX][DEFLENGTH];	/* allowed values */
} ns_default_config;



/* profile information */
#define	_PROFILE_OBJECTCLASS	"SolarisNamingProfile"
#define	_PROFILE_CONTAINER	"profile"
#define	_PROFILE_FILTER		"(&(objectclass=%s)(cn=%s))"

#define	_P_CN			"cn"
#define	_P_SERVERS		"SolarisLDAPServers"
#define	_P_SEARCHBASEDN		"SolarisSearchBaseDN"
#define	_P_CACHETTL		"SolarisCacheTTL"
#define	_P_BINDDN		"SolarisBindDN"
#define	_P_BINDPASSWORD		"SolarisBindPassword"
#define	_P_AUTHMETHOD		"SolarisAuthMethod"
#define	_P_TRANSPORTSECURITY	"SolarisTransportSecurity"
#define	_P_CERTIFICATEPATH	"SolarisCertificatePath"
#define	_P_CERTIFICATEPASSWORD	"SolarisCertificatePassword"
#define	_P_DATASEARCHDN		"SolarisDataSearchDN"
#define	_P_SEARCHSCOPE		"SolarisSearchScope"
#define	_P_SEARCHTIMELIMIT	"SolarisSearchTimeLimit"
#define	_P_PREFERREDSERVER	"SolarisPreferredServer"
#define	_P_PREFERREDSERVERONLY	"SolarisPreferredServerOnly"
#define	_P_SEARCHREFERRAL	"SolarisSearchReferral"


static const char *profile_attrs[] = {
	_P_SERVERS,			/* Multiple Value */
	_P_SEARCHBASEDN,		/* Single Value */
	_P_CACHETTL,			/* Single Value */
	_P_BINDDN,			/* Single Value */
	_P_BINDPASSWORD,		/* Single Value */
	_P_AUTHMETHOD,			/* Multiple Value */
	_P_TRANSPORTSECURITY,		/* Single Value */
	_P_CERTIFICATEPATH,		/* Single Value */
	_P_CERTIFICATEPASSWORD,		/* Single Value */
	_P_DATASEARCHDN,		/* Multiple Value */
	_P_SEARCHSCOPE,			/* Single Value */
	_P_SEARCHTIMELIMIT,		/* Single Value */
	_P_PREFERREDSERVER,		/* Multiple Value */
	_P_PREFERREDSERVERONLY,		/* Single Value */
	_P_SEARCHREFERRAL,		/* Single Value */
	(char *)NULL
};

/*
 * The order the defconfig[] array *MUST* be exactly the same as the order
 * for the ParamIndexType listed in the ns_sldap.h.
 */
static ns_default_config defconfig[] = {
	{"NS_LDAP_FILE_VERSION",
		NULL,	/* not defined in the profile */
		NS_LDAP_VERSION,
		{ NULL, NULL, NULL, NULL, NULL, NULL}},
	{"NS_LDAP_BINDDN",
		_P_BINDDN,
		NULL,
		{ NULL, NULL, NULL, NULL, NULL, NULL}},
	{"NS_LDAP_BINDPASSWD",
		_P_BINDPASSWORD,
		NULL,
		{ NULL, NULL, NULL, NULL, NULL, NULL}},
	{"NS_LDAP_SERVERS",
		_P_SERVERS,
		NULL,
		{ NULL, NULL, NULL, NULL, NULL, NULL}},
	{"NS_LDAP_SEARCH_BASEDN",
		_P_SEARCHBASEDN,
		NULL,
		{ NULL, NULL, NULL, NULL, NULL, NULL}},
	{"NS_LDAP_AUTH",
		_P_AUTHMETHOD,
		"NS_LDAP_AUTH_NONE",
		{ "NS_LDAP_AUTH_NONE", "NS_LDAP_AUTH_SIMPLE",
		"NS_LDAP_AUTH_SASL_CRAM_MD5", "NS_LDAP_AUTH_SASL_GSSAPI",
		"NS_LDAP_AUTH_SASL_SPNEGO", "NS_LDAP_AUTH_TLS"}},
	{"NS_LDAP_TRANSPORT_SEC",
		_P_TRANSPORTSECURITY,
		"NS_LDAP_SEC_NONE",
		{ "NS_LDAP_SEC_SASL_INTEGRITY", "NS_LDAP_SEC_SASL_PRIVACY",
		"NS_LDAP_SEC_TLS", "NS_LDAP_SEC_NONE"}},
	{"NS_LDAP_SEARCH_REF",
		_P_SEARCHREFERRAL,
		"NS_LDAP_FOLLOWREF",
		{ "NS_LDAP_FOLLOWREF", "NS_LDAP_NOREF", NULL, NULL}},
	{"NS_LDAP_DOMAIN",
		NULL,	/* not defined in the Profile */
		NULL,
		{ NULL, NULL, NULL, NULL, NULL, NULL}},
	{"NS_LDAP_EXP",
		NULL,	/* not defined in the Profile */
		NULL,	/* current time + NS_LDAP_CACHETTL */
		{ NULL, NULL, NULL, NULL, NULL, NULL}},
	{"NS_LDAP_CERT_PATH",
		_P_CERTIFICATEPATH,
		NULL,
		{ NULL, NULL, NULL, NULL, NULL, NULL}},
	{"NS_LDAP_CERT_PASS",
		_P_CERTIFICATEPASSWORD,
		NULL,
		{ NULL, NULL, NULL, NULL, NULL, NULL}},
	{"NS_LDAP_SEARCH_DN",
		_P_DATASEARCHDN,
		NULL,
		{ NULL, NULL, NULL, NULL, NULL, NULL}},
	{"NS_LDAP_SEARCH_SCOPE",
		_P_SEARCHSCOPE,
		"NS_LDAP_SCOPE_ONELEVEL",
		{ "NS_LDAP_SCOPE_BASE", "NS_LDAP_SCOPE_ONELEVEL",
		"NS_LDAP_SCOPE_SUBTREE", NULL}},
	{"NS_LDAP_SEARCH_TIME",
		_P_SEARCHTIMELIMIT,
		"30",
		{ NULL, NULL, NULL, NULL, NULL, NULL}},
	{"NS_LDAP_SERVER_PREF",
		_P_PREFERREDSERVER,
		NULL,
		{ NULL, NULL, NULL, NULL, NULL, NULL}},
	{"NS_LDAP_PREF_ONLY",
		_P_PREFERREDSERVERONLY,
		NULL,
		{ "NS_LDAP_TRUE", "NS_LDAP_FALSE", NULL, NULL}},
	{"NS_LDAP_CACHETTL",
		_P_CACHETTL,
		EXP_DEFAULT_TTL,
		{ NULL, NULL, NULL, NULL, NULL, NULL}},
	{"NS_LDAP_PROFILE",
		_P_CN,
		NULL,
		{ NULL, NULL, NULL, NULL, NULL, NULL}},
	{"", "",
		NULL,
		{ NULL, NULL, NULL, NULL, NULL, NULL}}
};

#ifdef __cplusplus
}
#endif

#endif /* _NS_INTERNAL_H */
