17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * Copyright (c) 2001 by Sun Microsystems, Inc.
37c478bdstevel@tonic-gate * All rights reserved.
47c478bdstevel@tonic-gate */
57c478bdstevel@tonic-gate
67c478bdstevel@tonic-gate/*
77c478bdstevel@tonic-gate * The contents of this file are subject to the Netscape Public
87c478bdstevel@tonic-gate * License Version 1.1 (the "License"); you may not use this file
97c478bdstevel@tonic-gate * except in compliance with the License. You may obtain a copy of
107c478bdstevel@tonic-gate * the License at http://www.mozilla.org/NPL/
117c478bdstevel@tonic-gate *
127c478bdstevel@tonic-gate * Software distributed under the License is distributed on an "AS
137c478bdstevel@tonic-gate * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
147c478bdstevel@tonic-gate * implied. See the License for the specific language governing
157c478bdstevel@tonic-gate * rights and limitations under the License.
167c478bdstevel@tonic-gate *
177c478bdstevel@tonic-gate * The Original Code is Mozilla Communicator client code, released
187c478bdstevel@tonic-gate * March 31, 1998.
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * The Initial Developer of the Original Code is Netscape
217c478bdstevel@tonic-gate * Communications Corporation. Portions created by Netscape are
227c478bdstevel@tonic-gate * Copyright (C) 1998-1999 Netscape Communications Corporation. All
237c478bdstevel@tonic-gate * Rights Reserved.
247c478bdstevel@tonic-gate *
257c478bdstevel@tonic-gate * Contributor(s):
267c478bdstevel@tonic-gate */
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate/*
297c478bdstevel@tonic-gate * tmplout.c:  display template library output routines for LDAP clients
307c478bdstevel@tonic-gate *
317c478bdstevel@tonic-gate */
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate#include "ldap-int.h"
347c478bdstevel@tonic-gate#include "disptmpl.h"
357c478bdstevel@tonic-gate
367c478bdstevel@tonic-gate#if defined(_WINDOWS) || defined(aix) || defined(SCOOS) || defined(OSF1) || defined(SOLARIS)
377c478bdstevel@tonic-gate#include <time.h> /* for struct tm and ctime */
387c478bdstevel@tonic-gate#endif
397c478bdstevel@tonic-gate
407c478bdstevel@tonic-gate
417c478bdstevel@tonic-gate/* This is totally lame, since it should be coming from time.h, but isn't. */
427c478bdstevel@tonic-gate#if defined(SOLARIS)
437c478bdstevel@tonic-gatechar *ctime_r(const time_t *, char *, int);
447c478bdstevel@tonic-gate#endif
457c478bdstevel@tonic-gate
467c478bdstevel@tonic-gatestatic int do_entry2text( LDAP *ld, char *buf, char *base, LDAPMessage *entry,
477c478bdstevel@tonic-gate	struct ldap_disptmpl *tmpl, char **defattrs, char ***defvals,
487c478bdstevel@tonic-gate	writeptype writeproc, void *writeparm, char *eol, int rdncount,
497c478bdstevel@tonic-gate	unsigned long opts, char *urlprefix );
507c478bdstevel@tonic-gatestatic int do_entry2text_search( LDAP *ld, char *dn, char *base,
517c478bdstevel@tonic-gate	LDAPMessage *entry, struct ldap_disptmpl *tmpllist, char **defattrs,
527c478bdstevel@tonic-gate	char ***defvals, writeptype writeproc, void *writeparm, char *eol,
537c478bdstevel@tonic-gate	int rdncount, unsigned long opts, char *urlprefix );
547c478bdstevel@tonic-gatestatic int do_vals2text( LDAP *ld, char *buf, char **vals, char *label,
557c478bdstevel@tonic-gate	int labelwidth, unsigned long syntaxid, writeptype writeproc,
567c478bdstevel@tonic-gate	void *writeparm, char *eol, int rdncount, char *urlprefix );
577c478bdstevel@tonic-gatestatic int max_label_len( struct ldap_disptmpl *tmpl );
587c478bdstevel@tonic-gatestatic int output_label( char *buf, char *label, int width,
597c478bdstevel@tonic-gate	writeptype writeproc, void *writeparm, char *eol, int html );
607c478bdstevel@tonic-gatestatic int output_dn( char *buf, char *dn, int width, int rdncount,
617c478bdstevel@tonic-gate	writeptype writeproc, void *writeparm, char *eol, char *urlprefix );
627c478bdstevel@tonic-gatestatic void strcat_escaped( char *s1, char *s2 );
637c478bdstevel@tonic-gatestatic char *time2text( char *ldtimestr, int dateonly );
647c478bdstevel@tonic-gatestatic long gtime( struct tm *tm );
657c478bdstevel@tonic-gatestatic int searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry,
667c478bdstevel@tonic-gate	char *dn, struct ldap_tmplitem *tip, int labelwidth, int rdncount,
677c478bdstevel@tonic-gate	writeptype writeproc, void *writeparm, char *eol, char *urlprefix );
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gate#define DEF_LABEL_WIDTH		15
707c478bdstevel@tonic-gate#define SEARCH_TIMEOUT_SECS	120
717c478bdstevel@tonic-gate#define OCATTRNAME		"objectClass"
727c478bdstevel@tonic-gate
737c478bdstevel@tonic-gate
747c478bdstevel@tonic-gate#define NONFATAL_LDAP_ERR( err )	( err == LDAP_SUCCESS || \
757c478bdstevel@tonic-gate	err == LDAP_TIMELIMIT_EXCEEDED || err == LDAP_SIZELIMIT_EXCEEDED )
767c478bdstevel@tonic-gate
777c478bdstevel@tonic-gate#define DEF_LDAP_URL_PREFIX	"ldap:///"
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate
807c478bdstevel@tonic-gateint
817c478bdstevel@tonic-gateLDAP_CALL
827c478bdstevel@tonic-gateldap_entry2text(
837c478bdstevel@tonic-gate	LDAP			*ld,
847c478bdstevel@tonic-gate	char			*buf,		/* NULL for "use internal" */
857c478bdstevel@tonic-gate	LDAPMessage		*entry,
867c478bdstevel@tonic-gate	struct ldap_disptmpl	*tmpl,
877c478bdstevel@tonic-gate	char			**defattrs,
887c478bdstevel@tonic-gate	char			***defvals,
897c478bdstevel@tonic-gate	writeptype		writeproc,
907c478bdstevel@tonic-gate	void			*writeparm,
917c478bdstevel@tonic-gate	char			*eol,
927c478bdstevel@tonic-gate	int			rdncount,
937c478bdstevel@tonic-gate	unsigned long		opts
947c478bdstevel@tonic-gate)
957c478bdstevel@tonic-gate{
967c478bdstevel@tonic-gate    LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2text\n", 0, 0, 0 );
977c478bdstevel@tonic-gate
987c478bdstevel@tonic-gate    return( do_entry2text( ld, buf, NULL, entry, tmpl, defattrs, defvals,
997c478bdstevel@tonic-gate		writeproc, writeparm, eol, rdncount, opts, NULL ));
1007c478bdstevel@tonic-gate
1017c478bdstevel@tonic-gate}
1027c478bdstevel@tonic-gate
1037c478bdstevel@tonic-gate
1047c478bdstevel@tonic-gate
1057c478bdstevel@tonic-gateint
1067c478bdstevel@tonic-gateLDAP_CALL
1077c478bdstevel@tonic-gateldap_entry2html(
1087c478bdstevel@tonic-gate	LDAP			*ld,
1097c478bdstevel@tonic-gate	char			*buf,		/* NULL for "use internal" */
1107c478bdstevel@tonic-gate	LDAPMessage		*entry,
1117c478bdstevel@tonic-gate	struct ldap_disptmpl	*tmpl,
1127c478bdstevel@tonic-gate	char			**defattrs,
1137c478bdstevel@tonic-gate	char			***defvals,
1147c478bdstevel@tonic-gate	writeptype		writeproc,
1157c478bdstevel@tonic-gate	void			*writeparm,
1167c478bdstevel@tonic-gate	char			*eol,
1177c478bdstevel@tonic-gate	int			rdncount,
1187c478bdstevel@tonic-gate	unsigned long		opts,
1197c478bdstevel@tonic-gate	char			*base,
1207c478bdstevel@tonic-gate	char			*urlprefix
1217c478bdstevel@tonic-gate)
1227c478bdstevel@tonic-gate{
1237c478bdstevel@tonic-gate    LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2html\n", 0, 0, 0 );
1247c478bdstevel@tonic-gate
1257c478bdstevel@tonic-gate    if ( urlprefix == NULL ) {
1267c478bdstevel@tonic-gate	urlprefix = DEF_LDAP_URL_PREFIX;
1277c478bdstevel@tonic-gate    }
1287c478bdstevel@tonic-gate
1297c478bdstevel@tonic-gate    return( do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
1307c478bdstevel@tonic-gate		writeproc, writeparm, eol, rdncount, opts, urlprefix ));
1317c478bdstevel@tonic-gate}
1327c478bdstevel@tonic-gate
1337c478bdstevel@tonic-gate
1347c478bdstevel@tonic-gatestatic int
1357c478bdstevel@tonic-gatedo_entry2text(
1367c478bdstevel@tonic-gate	LDAP			*ld,
1377c478bdstevel@tonic-gate	char			*buf,		/* NULL for use-internal */
1387c478bdstevel@tonic-gate	char			*base,		/* used for search actions */
1397c478bdstevel@tonic-gate	LDAPMessage		*entry,
1407c478bdstevel@tonic-gate	struct ldap_disptmpl	*tmpl,
1417c478bdstevel@tonic-gate	char			**defattrs,
1427c478bdstevel@tonic-gate	char			***defvals,
1437c478bdstevel@tonic-gate	writeptype		writeproc,
1447c478bdstevel@tonic-gate	void			*writeparm,
1457c478bdstevel@tonic-gate	char			*eol,
1467c478bdstevel@tonic-gate	int			rdncount,
1477c478bdstevel@tonic-gate	unsigned long		opts,
1487c478bdstevel@tonic-gate	char			*urlprefix	/* if non-NULL, do HTML */
1497c478bdstevel@tonic-gate)
1507c478bdstevel@tonic-gate{
1517c478bdstevel@tonic-gate    int				i, err, html, show, labelwidth;
1527c478bdstevel@tonic-gate    int				freebuf,  freevals;
1537c478bdstevel@tonic-gate    char			*dn, **vals;
1547c478bdstevel@tonic-gate    struct ldap_tmplitem	*rowp, *colp;
1557c478bdstevel@tonic-gate
1567c478bdstevel@tonic-gate    if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
1577c478bdstevel@tonic-gate	return( LDAP_PARAM_ERROR );
1587c478bdstevel@tonic-gate    }
1597c478bdstevel@tonic-gate
1607c478bdstevel@tonic-gate    if ( writeproc == NULL ||
1617c478bdstevel@tonic-gate	    !NSLDAPI_VALID_LDAPMESSAGE_ENTRY_POINTER( entry )) {
1627c478bdstevel@tonic-gate	err = LDAP_PARAM_ERROR;
1637c478bdstevel@tonic-gate	LDAP_SET_LDERRNO( ld, err, NULL, NULL );
1647c478bdstevel@tonic-gate	return( err );
1657c478bdstevel@tonic-gate    }
1667c478bdstevel@tonic-gate
1677c478bdstevel@tonic-gate    if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
1687c478bdstevel@tonic-gate	return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
1697c478bdstevel@tonic-gate    }
1707c478bdstevel@tonic-gate
1717c478bdstevel@tonic-gate    if ( buf == NULL ) {
1727c478bdstevel@tonic-gate	if (( buf = NSLDAPI_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
1737c478bdstevel@tonic-gate	    err = LDAP_NO_MEMORY;
1747c478bdstevel@tonic-gate	    LDAP_SET_LDERRNO( ld, err, NULL, NULL );
1757c478bdstevel@tonic-gate	    NSLDAPI_FREE( dn );
1767c478bdstevel@tonic-gate	    return( err );
1777c478bdstevel@tonic-gate	}
1787c478bdstevel@tonic-gate	freebuf = 1;
1797c478bdstevel@tonic-gate    } else {
1807c478bdstevel@tonic-gate	freebuf = 0;
1817c478bdstevel@tonic-gate    }
1827c478bdstevel@tonic-gate
1837c478bdstevel@tonic-gate    html = ( urlprefix != NULL );
1847c478bdstevel@tonic-gate
1857c478bdstevel@tonic-gate    if ( html ) {
1867c478bdstevel@tonic-gate	/*
1877c478bdstevel@tonic-gate	 * add HTML intro. and title
1887c478bdstevel@tonic-gate	 */
1897c478bdstevel@tonic-gate	if (!(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
1907c478bdstevel@tonic-gate	    sprintf( buf, "<HTML>%s<HEAD>%s<TITLE>%s%s - ", eol, eol, eol,
1917c478bdstevel@tonic-gate		    ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
1927c478bdstevel@tonic-gate	    (*writeproc)( writeparm, buf, strlen( buf ));
1937c478bdstevel@tonic-gate	    output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
1947c478bdstevel@tonic-gate	    sprintf( buf, "%s</TITLE>%s</HEAD>%s<BODY>%s<H3>%s - ", eol, eol,
1957c478bdstevel@tonic-gate		    eol, eol, ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
1967c478bdstevel@tonic-gate	    (*writeproc)( writeparm, buf, strlen( buf ));
1977c478bdstevel@tonic-gate	    output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
1987c478bdstevel@tonic-gate	    sprintf( buf, "</H3>%s", eol );
1997c478bdstevel@tonic-gate	    (*writeproc)( writeparm, buf, strlen( buf ));
2007c478bdstevel@tonic-gate	}
2017c478bdstevel@tonic-gate
2027c478bdstevel@tonic-gate	if (( opts & LDAP_DISP_OPT_NONLEAF ) != 0 &&
2037c478bdstevel@tonic-gate		( vals = ldap_explode_dn( dn, 0 )) != NULL ) {
2047c478bdstevel@tonic-gate	    char	*untagged;
2057c478bdstevel@tonic-gate
2067c478bdstevel@tonic-gate	    /*
2077c478bdstevel@tonic-gate	     * add "Move Up" link
2087c478bdstevel@tonic-gate	     */
2097c478bdstevel@tonic-gate	    sprintf( buf, "<A HREF=\"%s", urlprefix );
2107c478bdstevel@tonic-gate	    for ( i = 1; vals[ i ] != NULL; ++i ) {
2117c478bdstevel@tonic-gate		if ( i > 1 ) {
2127c478bdstevel@tonic-gate		     strcat_escaped( buf, ", " );
2137c478bdstevel@tonic-gate		}
2147c478bdstevel@tonic-gate		strcat_escaped( buf, vals[ i ] );
2157c478bdstevel@tonic-gate	    }
2167c478bdstevel@tonic-gate	    if ( vals[ 1 ] != NULL ) {
2177c478bdstevel@tonic-gate		untagged = strchr( vals[ 1 ], '=' );
2187c478bdstevel@tonic-gate	    } else {
2197c478bdstevel@tonic-gate		untagged = "=The World";
2207c478bdstevel@tonic-gate	    }
2217c478bdstevel@tonic-gate	    sprintf( buf + strlen( buf ),
2227c478bdstevel@tonic-gate		    "%s\">Move Up To <EM>%s</EM></A>%s<BR>",
2237c478bdstevel@tonic-gate		    ( vals[ 1 ] == NULL ) ? "??one" : "",
2247c478bdstevel@tonic-gate		    ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol );
2257c478bdstevel@tonic-gate	    (*writeproc)( writeparm, buf, strlen( buf ));
2267c478bdstevel@tonic-gate
2277c478bdstevel@tonic-gate	    /*
2287c478bdstevel@tonic-gate	     * add "Browse" link
2297c478bdstevel@tonic-gate	     */
2307c478bdstevel@tonic-gate	    untagged = strchr( vals[ 0 ], '=' );
2317c478bdstevel@tonic-gate	    sprintf( buf, "<A HREF=\"%s", urlprefix );
2327c478bdstevel@tonic-gate	    strcat_escaped( buf, dn );
2337c478bdstevel@tonic-gate	    sprintf( buf + strlen( buf ), "??one?(!(objectClass=dsa))\">Browse Below <EM>%s</EM></A>%s%s",
2347c478bdstevel@tonic-gate		    ( untagged != NULL ) ? untagged + 1 : vals[ 0 ], eol, eol );
2357c478bdstevel@tonic-gate	    (*writeproc)( writeparm, buf, strlen( buf ));
2367c478bdstevel@tonic-gate
2377c478bdstevel@tonic-gate	    ldap_value_free( vals );
2387c478bdstevel@tonic-gate	}
2397c478bdstevel@tonic-gate
2407c478bdstevel@tonic-gate	(*writeproc)( writeparm, "<HR>", 4 );	/* horizontal rule */
2417c478bdstevel@tonic-gate    } else {
2427c478bdstevel@tonic-gate	(*writeproc)( writeparm, "\"", 1 );
2437c478bdstevel@tonic-gate	output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
2447c478bdstevel@tonic-gate	sprintf( buf, "\"%s", eol );
2457c478bdstevel@tonic-gate	(*writeproc)( writeparm, buf, strlen( buf ));
2467c478bdstevel@tonic-gate    }
2477c478bdstevel@tonic-gate
2487c478bdstevel@tonic-gate    if ( tmpl != NULL && ( opts & LDAP_DISP_OPT_AUTOLABELWIDTH ) != 0 ) {
2497c478bdstevel@tonic-gate	labelwidth = max_label_len( tmpl ) + 3;
2507c478bdstevel@tonic-gate    } else {
2517c478bdstevel@tonic-gate	labelwidth = DEF_LABEL_WIDTH;;
2527c478bdstevel@tonic-gate    }
2537c478bdstevel@tonic-gate
2547c478bdstevel@tonic-gate    err = LDAP_SUCCESS;
2557c478bdstevel@tonic-gate
2567c478bdstevel@tonic-gate    if ( tmpl == NULL ) {
2577c478bdstevel@tonic-gate	BerElement	*ber;
2587c478bdstevel@tonic-gate	char		*attr;
2597c478bdstevel@tonic-gate
2607c478bdstevel@tonic-gate	ber = NULL;
2617c478bdstevel@tonic-gate	for ( attr = ldap_first_attribute( ld, entry, &ber );
2627c478bdstevel@tonic-gate		NONFATAL_LDAP_ERR( err ) && attr != NULL;
2637c478bdstevel@tonic-gate		attr = ldap_next_attribute( ld, entry, ber )) {
2647c478bdstevel@tonic-gate	    if (( vals = ldap_get_values( ld, entry, attr )) == NULL ) {
2657c478bdstevel@tonic-gate		freevals = 0;
2667c478bdstevel@tonic-gate		if ( defattrs != NULL ) {
2677c478bdstevel@tonic-gate		    for ( i = 0; defattrs[ i ] != NULL; ++i ) {
2687c478bdstevel@tonic-gate			if ( strcasecmp( attr, defattrs[ i ] ) == 0 ) {
2697c478bdstevel@tonic-gate			    break;
2707c478bdstevel@tonic-gate			}
2717c478bdstevel@tonic-gate		    }
2727c478bdstevel@tonic-gate		    if ( defattrs[ i ] != NULL ) {
2737c478bdstevel@tonic-gate			vals = defvals[ i ];
2747c478bdstevel@tonic-gate		    }
2757c478bdstevel@tonic-gate		}
2767c478bdstevel@tonic-gate	    } else {
2777c478bdstevel@tonic-gate		freevals = 1;
2787c478bdstevel@tonic-gate	    }
2797c478bdstevel@tonic-gate
2807c478bdstevel@tonic-gate	    if ( islower( *attr )) {	/* cosmetic -- upcase attr. name */
2817c478bdstevel@tonic-gate		*attr = toupper( *attr );
2827c478bdstevel@tonic-gate	    }
2837c478bdstevel@tonic-gate
2847c478bdstevel@tonic-gate	    err = do_vals2text( ld, buf, vals, attr, labelwidth,
2857c478bdstevel@tonic-gate		    LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol,
2867c478bdstevel@tonic-gate		    rdncount, urlprefix );
2877c478bdstevel@tonic-gate	    if ( freevals ) {
2887c478bdstevel@tonic-gate		ldap_value_free( vals );
2897c478bdstevel@tonic-gate	    }
2907c478bdstevel@tonic-gate	}
2917c478bdstevel@tonic-gate	if ( ber == NULL ) {
2927c478bdstevel@tonic-gate	    ber_free( ber, 0 );
2937c478bdstevel@tonic-gate	}
2947c478bdstevel@tonic-gate	/*
2957c478bdstevel@tonic-gate	 * XXX check for errors in ldap_first_attribute/ldap_next_attribute
2967c478bdstevel@tonic-gate	 * here (but what should we do if there was one?)
2977c478bdstevel@tonic-gate	 */
2987c478bdstevel@tonic-gate
2997c478bdstevel@tonic-gate    } else {
3007c478bdstevel@tonic-gate	for ( rowp = ldap_first_tmplrow( tmpl );
3017c478bdstevel@tonic-gate		NONFATAL_LDAP_ERR( err ) && rowp != NULLTMPLITEM;
3027c478bdstevel@tonic-gate		rowp = ldap_next_tmplrow( tmpl, rowp )) {
3037c478bdstevel@tonic-gate	    for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
3047c478bdstevel@tonic-gate		    colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
3057c478bdstevel@tonic-gate		vals = NULL;
3067c478bdstevel@tonic-gate		if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
3077c478bdstevel@tonic-gate			entry, colp->ti_attrname )) == NULL ) {
3087c478bdstevel@tonic-gate		    freevals = 0;
3097c478bdstevel@tonic-gate		    if ( !LDAP_IS_TMPLITEM_OPTION_SET( colp,
3107c478bdstevel@tonic-gate			    LDAP_DITEM_OPT_HIDEIFEMPTY ) && defattrs != NULL
3117c478bdstevel@tonic-gate			    && colp->ti_attrname != NULL ) {
3127c478bdstevel@tonic-gate			for ( i = 0; defattrs[ i ] != NULL; ++i ) {
3137c478bdstevel@tonic-gate			    if ( strcasecmp( colp->ti_attrname, defattrs[ i ] )
3147c478bdstevel@tonic-gate				    == 0 ) {
3157c478bdstevel@tonic-gate				break;
3167c478bdstevel@tonic-gate			    }
3177c478bdstevel@tonic-gate			}
3187c478bdstevel@tonic-gate			if ( defattrs[ i ] != NULL ) {
3197c478bdstevel@tonic-gate			    vals = defvals[ i ];
3207c478bdstevel@tonic-gate			}
3217c478bdstevel@tonic-gate		    }
3227c478bdstevel@tonic-gate		} else {
3237c478bdstevel@tonic-gate		    freevals = 1;
3247c478bdstevel@tonic-gate		    if ( LDAP_IS_TMPLITEM_OPTION_SET( colp,
3257c478bdstevel@tonic-gate			    LDAP_DITEM_OPT_SORTVALUES ) && vals[ 0 ] != NULL
3267c478bdstevel@tonic-gate			    && vals[ 1 ] != NULL ) {
3277c478bdstevel@tonic-gate			ldap_sort_values(ld, vals, ldap_sort_strcasecmp);
3287c478bdstevel@tonic-gate		    }
3297c478bdstevel@tonic-gate		}
3307c478bdstevel@tonic-gate
3317c478bdstevel@tonic-gate		/*
3327c478bdstevel@tonic-gate		 * don't bother even calling do_vals2text() if no values
3337c478bdstevel@tonic-gate		 * or boolean with value false and "hide if false" option set
3347c478bdstevel@tonic-gate		 */
3357c478bdstevel@tonic-gate		show = ( vals != NULL && vals[ 0 ] != NULL );
3367c478bdstevel@tonic-gate		if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
3377c478bdstevel@tonic-gate			== LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
3387c478bdstevel@tonic-gate			colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
3397c478bdstevel@tonic-gate			toupper( vals[ 0 ][ 0 ] ) != 'T' ) {
3407c478bdstevel@tonic-gate		    show = 0;
3417c478bdstevel@tonic-gate		}
3427c478bdstevel@tonic-gate
3437c478bdstevel@tonic-gate		if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
3447c478bdstevel@tonic-gate		    if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
3457c478bdstevel@tonic-gate			if ( colp->ti_attrname == NULL || ( show &&
3467c478bdstevel@tonic-gate				toupper( vals[ 0 ][ 0 ] ) == 'T' )) {
3477c478bdstevel@tonic-gate			    err = searchaction( ld, buf, base, entry, dn, colp,
3487c478bdstevel@tonic-gate				    labelwidth, rdncount, writeproc,
3497c478bdstevel@tonic-gate				    writeparm, eol, urlprefix );
3507c478bdstevel@tonic-gate			}
3517c478bdstevel@tonic-gate		    }
3527c478bdstevel@tonic-gate		    show = 0;
3537c478bdstevel@tonic-gate		}
3547c478bdstevel@tonic-gate
3557c478bdstevel@tonic-gate		if ( show ) {
3567c478bdstevel@tonic-gate		    err = do_vals2text( ld, buf, vals, colp->ti_label,
3577c478bdstevel@tonic-gate			labelwidth, colp->ti_syntaxid, writeproc, writeparm,
3587c478bdstevel@tonic-gate			eol, rdncount, urlprefix );
3597c478bdstevel@tonic-gate		}
3607c478bdstevel@tonic-gate
3617c478bdstevel@tonic-gate		if ( freevals ) {
3627c478bdstevel@tonic-gate		    ldap_value_free( vals );
3637c478bdstevel@tonic-gate		}
3647c478bdstevel@tonic-gate	    }
3657c478bdstevel@tonic-gate	}
3667c478bdstevel@tonic-gate    }
3677c478bdstevel@tonic-gate
3687c478bdstevel@tonic-gate    if ( html  && !(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
3697c478bdstevel@tonic-gate	sprintf( buf, "</BODY>%s</HTML>%s", eol, eol );
3707c478bdstevel@tonic-gate	(*writeproc)( writeparm, buf, strlen( buf ));
3717c478bdstevel@tonic-gate    }
3727c478bdstevel@tonic-gate
3737c478bdstevel@tonic-gate    NSLDAPI_FREE( dn );
3747c478bdstevel@tonic-gate    if ( freebuf ) {
3757c478bdstevel@tonic-gate	NSLDAPI_FREE( buf );
3767c478bdstevel@tonic-gate    }
3777c478bdstevel@tonic-gate
3787c478bdstevel@tonic-gate    return( err );
3797c478bdstevel@tonic-gate}
3807c478bdstevel@tonic-gate
3817c478bdstevel@tonic-gate
3827c478bdstevel@tonic-gateint
3837c478bdstevel@tonic-gateLDAP_CALL
3847c478bdstevel@tonic-gateldap_entry2text_search(
3857c478bdstevel@tonic-gate	LDAP			*ld,
3867c478bdstevel@tonic-gate	char			*dn,		/* if NULL, use entry */
3877c478bdstevel@tonic-gate	char			*base,		/* if NULL, no search actions */
3887c478bdstevel@tonic-gate	LDAPMessage		*entry,		/* if NULL, use dn */
3897c478bdstevel@tonic-gate	struct ldap_disptmpl*	tmpllist,	/* if NULL, load default file */
3907c478bdstevel@tonic-gate	char			**defattrs,
3917c478bdstevel@tonic-gate	char			***defvals,
3927c478bdstevel@tonic-gate	writeptype		writeproc,
3937c478bdstevel@tonic-gate	void			*writeparm,
3947c478bdstevel@tonic-gate	char			*eol,
3957c478bdstevel@tonic-gate	int			rdncount,	/* if 0, display full DN */
3967c478bdstevel@tonic-gate	unsigned long		opts
3977c478bdstevel@tonic-gate)
3987c478bdstevel@tonic-gate{
3997c478bdstevel@tonic-gate    LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2text_search\n", 0, 0, 0 );
4007c478bdstevel@tonic-gate
4017c478bdstevel@tonic-gate    return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
4027c478bdstevel@tonic-gate	    defvals, writeproc, writeparm, eol, rdncount, opts, NULL ));
4037c478bdstevel@tonic-gate}
4047c478bdstevel@tonic-gate
4057c478bdstevel@tonic-gate
4067c478bdstevel@tonic-gate
4077c478bdstevel@tonic-gateint
4087c478bdstevel@tonic-gateLDAP_CALL
4097c478bdstevel@tonic-gateldap_entry2html_search(
4107c478bdstevel@tonic-gate	LDAP			*ld,
4117c478bdstevel@tonic-gate	char			*dn,		/* if NULL, use entry */
4127c478bdstevel@tonic-gate	char			*base,		/* if NULL, no search actions */
4137c478bdstevel@tonic-gate	LDAPMessage		*entry,		/* if NULL, use dn */
4147c478bdstevel@tonic-gate	struct ldap_disptmpl*	tmpllist,	/* if NULL, load default file */
4157c478bdstevel@tonic-gate	char			**defattrs,
4167c478bdstevel@tonic-gate	char			***defvals,
4177c478bdstevel@tonic-gate	writeptype		writeproc,
4187c478bdstevel@tonic-gate	void			*writeparm,
4197c478bdstevel@tonic-gate	char			*eol,
4207c478bdstevel@tonic-gate	int			rdncount,	/* if 0, display full DN */
4217c478bdstevel@tonic-gate	unsigned long		opts,
4227c478bdstevel@tonic-gate	char			*urlprefix
4237c478bdstevel@tonic-gate)
4247c478bdstevel@tonic-gate{
4257c478bdstevel@tonic-gate    LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2html_search\n", 0, 0, 0 );
4267c478bdstevel@tonic-gate
4277c478bdstevel@tonic-gate    return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
4287c478bdstevel@tonic-gate	    defvals, writeproc, writeparm, eol, rdncount, opts, urlprefix ));
4297c478bdstevel@tonic-gate}
4307c478bdstevel@tonic-gate
4317c478bdstevel@tonic-gate
4327c478bdstevel@tonic-gatestatic int
4337c478bdstevel@tonic-gatedo_entry2text_search(
4347c478bdstevel@tonic-gate	LDAP			*ld,
4357c478bdstevel@tonic-gate	char			*dn,		/* if NULL, use entry */
4367c478bdstevel@tonic-gate	char			*base,		/* if NULL, no search actions */
4377c478bdstevel@tonic-gate	LDAPMessage		*entry,		/* if NULL, use dn */
4387c478bdstevel@tonic-gate	struct ldap_disptmpl*	tmpllist,	/* if NULL, no template used */
4397c478bdstevel@tonic-gate	char			**defattrs,
4407c478bdstevel@tonic-gate	char			***defvals,
4417c478bdstevel@tonic-gate	writeptype		writeproc,
4427c478bdstevel@tonic-gate	void			*writeparm,
4437c478bdstevel@tonic-gate	char			*eol,
4447c478bdstevel@tonic-gate	int			rdncount,	/* if 0, display full DN */
4457c478bdstevel@tonic-gate	unsigned long		opts,
4467c478bdstevel@tonic-gate	char			*urlprefix
4477c478bdstevel@tonic-gate)
4487c478bdstevel@tonic-gate{
44913ddffeToomas Soome    int				err, freedn;
4507c478bdstevel@tonic-gate    char			*buf, **fetchattrs, **vals;
4517c478bdstevel@tonic-gate    LDAPMessage			*ldmp;
4527c478bdstevel@tonic-gate    struct ldap_disptmpl	*tmpl;
4537c478bdstevel@tonic-gate    struct timeval		timeout;
4547c478bdstevel@tonic-gate
4557c478bdstevel@tonic-gate    if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
4567c478bdstevel@tonic-gate	return( LDAP_PARAM_ERROR );
4577c478bdstevel@tonic-gate    }
4587c478bdstevel@tonic-gate
4597c478bdstevel@tonic-gate    if ( dn == NULL && entry == NULLMSG ) {
4607c478bdstevel@tonic-gate	err = LDAP_PARAM_ERROR;
4617c478bdstevel@tonic-gate	LDAP_SET_LDERRNO( ld, err, NULL, NULL );
4627c478bdstevel@tonic-gate	return( err );
4637c478bdstevel@tonic-gate    }
4647c478bdstevel@tonic-gate
4657c478bdstevel@tonic-gate    timeout.tv_sec = SEARCH_TIMEOUT_SECS;
4667c478bdstevel@tonic-gate    timeout.tv_usec = 0;
4677c478bdstevel@tonic-gate
4687c478bdstevel@tonic-gate    if (( buf = NSLDAPI_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
4697c478bdstevel@tonic-gate	err = LDAP_NO_MEMORY;
4707c478bdstevel@tonic-gate	LDAP_SET_LDERRNO( ld, err, NULL, NULL );
4717c478bdstevel@tonic-gate	return( err );
4727c478bdstevel@tonic-gate    }
4737c478bdstevel@tonic-gate
4747c478bdstevel@tonic-gate    freedn = 0;
4757c478bdstevel@tonic-gate    tmpl = NULL;
4767c478bdstevel@tonic-gate
4777c478bdstevel@tonic-gate    if ( dn == NULL ) {
4787c478bdstevel@tonic-gate	if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
4797c478bdstevel@tonic-gate	    NSLDAPI_FREE( buf );
4807c478bdstevel@tonic-gate	    return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
4817c478bdstevel@tonic-gate	}
4827c478bdstevel@tonic-gate	freedn = 1;
4837c478bdstevel@tonic-gate    }
4847c478bdstevel@tonic-gate
4857c478bdstevel@tonic-gate
4867c478bdstevel@tonic-gate    if ( tmpllist != NULL ) {
4877c478bdstevel@tonic-gate	ldmp = NULLMSG;
4887c478bdstevel@tonic-gate
4897c478bdstevel@tonic-gate	if ( entry == NULL ) {
4907c478bdstevel@tonic-gate	    char	*ocattrs[2];
4917c478bdstevel@tonic-gate
4927c478bdstevel@tonic-gate	    ocattrs[0] = OCATTRNAME;
4937c478bdstevel@tonic-gate	    ocattrs[1] = NULL;
4947c478bdstevel@tonic-gate#ifdef CLDAP
4957c478bdstevel@tonic-gate	    if ( LDAP_IS_CLDAP( ld ))
4967c478bdstevel@tonic-gate		    err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE,
4977c478bdstevel@tonic-gate			"objectClass=*", ocattrs, 0, &ldmp, NULL );
4987c478bdstevel@tonic-gate	    else
4997c478bdstevel@tonic-gate#endif /* CLDAP */
5007c478bdstevel@tonic-gate		    err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
5017c478bdstevel@tonic-gate			    "objectClass=*", ocattrs, 0, &timeout, &ldmp );
5027c478bdstevel@tonic-gate
5037c478bdstevel@tonic-gate	    if ( err == LDAP_SUCCESS ) {
5047c478bdstevel@tonic-gate		entry = ldap_first_entry( ld, ldmp );
5057c478bdstevel@tonic-gate	    }
5067c478bdstevel@tonic-gate	}
5077c478bdstevel@tonic-gate
5087c478bdstevel@tonic-gate	if ( entry != NULL ) {
5097c478bdstevel@tonic-gate	    vals = ldap_get_values( ld, entry, OCATTRNAME );
5107c478bdstevel@tonic-gate	    tmpl = ldap_oc2template( vals, tmpllist );
5117c478bdstevel@tonic-gate	    if ( vals != NULL ) {
5127c478bdstevel@tonic-gate		ldap_value_free( vals );
5137c478bdstevel@tonic-gate	    }
5147c478bdstevel@tonic-gate	}
5157c478bdstevel@tonic-gate	if ( ldmp != NULL ) {
5167c478bdstevel@tonic-gate	    ldap_msgfree( ldmp );
5177c478bdstevel@tonic-gate	}
5187c478bdstevel@tonic-gate    }
5197c478bdstevel@tonic-gate
5207c478bdstevel@tonic-gate    entry = NULL;
5217c478bdstevel@tonic-gate
5227c478bdstevel@tonic-gate    if ( tmpl == NULL ) {
5237c478bdstevel@tonic-gate	fetchattrs = NULL;
5247c478bdstevel@tonic-gate    } else {
5257c478bdstevel@tonic-gate	fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
5267c478bdstevel@tonic-gate    }
5277c478bdstevel@tonic-gate
5287c478bdstevel@tonic-gate#ifdef CLDAP
5297c478bdstevel@tonic-gate    if ( LDAP_IS_CLDAP( ld ))
5307c478bdstevel@tonic-gate	err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
5317c478bdstevel@tonic-gate		fetchattrs, 0, &ldmp, NULL );
5327c478bdstevel@tonic-gate    else
5337c478bdstevel@tonic-gate#endif /* CLDAP */
5347c478bdstevel@tonic-gate	err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
5357c478bdstevel@tonic-gate		fetchattrs, 0, &timeout, &ldmp );
5367c478bdstevel@tonic-gate
5377c478bdstevel@tonic-gate    if ( freedn ) {
5387c478bdstevel@tonic-gate	NSLDAPI_FREE( dn );
5397c478bdstevel@tonic-gate    }
5407c478bdstevel@tonic-gate    if ( fetchattrs != NULL ) {
5417c478bdstevel@tonic-gate	ldap_value_free( fetchattrs );
5427c478bdstevel@tonic-gate    }
5437c478bdstevel@tonic-gate
5447c478bdstevel@tonic-gate    if ( err != LDAP_SUCCESS ||
5457c478bdstevel@tonic-gate	    ( entry = ldap_first_entry( ld, ldmp )) == NULL ) {
5467c478bdstevel@tonic-gate	NSLDAPI_FREE( buf );
5477c478bdstevel@tonic-gate	return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
5487c478bdstevel@tonic-gate    }
5497c478bdstevel@tonic-gate
5507c478bdstevel@tonic-gate    err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
5517c478bdstevel@tonic-gate	    writeproc, writeparm, eol, rdncount, opts, urlprefix );
5527c478bdstevel@tonic-gate
5537c478bdstevel@tonic-gate    NSLDAPI_FREE( buf );
5547c478bdstevel@tonic-gate    ldap_msgfree( ldmp );
5557c478bdstevel@tonic-gate    return( err );
5567c478bdstevel@tonic-gate}
5577c478bdstevel@tonic-gate
5587c478bdstevel@tonic-gate
5597c478bdstevel@tonic-gateint
5607c478bdstevel@tonic-gateLDAP_CALL
5617c478bdstevel@tonic-gateldap_vals2text(
5627c478bdstevel@tonic-gate	LDAP			*ld,
5637c478bdstevel@tonic-gate	char			*buf,		/* NULL for "use internal" */
5647c478bdstevel@tonic-gate	char			**vals,
5657c478bdstevel@tonic-gate	char			*label,
5667c478bdstevel@tonic-gate	int			labelwidth,	/* 0 means use default */
5677c478bdstevel@tonic-gate	unsigned long		syntaxid,
5687c478bdstevel@tonic-gate	writeptype		writeproc,
5697c478bdstevel@tonic-gate	void			*writeparm,
5707c478bdstevel@tonic-gate	char			*eol,
5717c478bdstevel@tonic-gate	int			rdncount
5727c478bdstevel@tonic-gate)
5737c478bdstevel@tonic-gate{
5747c478bdstevel@tonic-gate    LDAPDebug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
5757c478bdstevel@tonic-gate
5767c478bdstevel@tonic-gate    return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
5777c478bdstevel@tonic-gate		writeproc, writeparm, eol, rdncount, NULL ));
5787c478bdstevel@tonic-gate}
5797c478bdstevel@tonic-gate
5807c478bdstevel@tonic-gate
5817c478bdstevel@tonic-gateint
5827c478bdstevel@tonic-gateLDAP_CALL
5837c478bdstevel@tonic-gateldap_vals2html(
5847c478bdstevel@tonic-gate	LDAP			*ld,
5857c478bdstevel@tonic-gate	char			*buf,		/* NULL for "use internal" */
5867c478bdstevel@tonic-gate	char			**vals,
5877c478bdstevel@tonic-gate	char			*label,
5887c478bdstevel@tonic-gate	int			labelwidth,	/* 0 means use default */
5897c478bdstevel@tonic-gate	unsigned long		syntaxid,
5907c478bdstevel@tonic-gate	writeptype		writeproc,
5917c478bdstevel@tonic-gate	void			*writeparm,
5927c478bdstevel@tonic-gate	char			*eol,
5937c478bdstevel@tonic-gate	int			rdncount,
5947c478bdstevel@tonic-gate	char			*urlprefix
5957c478bdstevel@tonic-gate)
5967c478bdstevel@tonic-gate{
5977c478bdstevel@tonic-gate    LDAPDebug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
5987c478bdstevel@tonic-gate
5997c478bdstevel@tonic-gate    if ( urlprefix == NULL ) {
6007c478bdstevel@tonic-gate	urlprefix = DEF_LDAP_URL_PREFIX;
6017c478bdstevel@tonic-gate    }
6027c478bdstevel@tonic-gate
6037c478bdstevel@tonic-gate    return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
6047c478bdstevel@tonic-gate		writeproc, writeparm, eol, rdncount, urlprefix ));
6057c478bdstevel@tonic-gate}
6067c478bdstevel@tonic-gate
6077c478bdstevel@tonic-gate
6087c478bdstevel@tonic-gatestatic int
6097c478bdstevel@tonic-gatedo_vals2text(
6107c478bdstevel@tonic-gate	LDAP			*ld,
6117c478bdstevel@tonic-gate	char			*buf,		/* NULL for "use internal" */
6127c478bdstevel@tonic-gate	char			**vals,
6137c478bdstevel@tonic-gate	char			*label,
6147c478bdstevel@tonic-gate	int			labelwidth,	/* 0 means use default */
6157c478bdstevel@tonic-gate	unsigned long		syntaxid,
6167c478bdstevel@tonic-gate	writeptype		writeproc,
6177c478bdstevel@tonic-gate	void			*writeparm,
6187c478bdstevel@tonic-gate	char			*eol,
6197c478bdstevel@tonic-gate	int			rdncount,
6207c478bdstevel@tonic-gate	char			*urlprefix
6217c478bdstevel@tonic-gate)
6227c478bdstevel@tonic-gate{
6237c478bdstevel@tonic-gate    int		err, i, html, writeoutval, freebuf, notascii;
6247c478bdstevel@tonic-gate    char	*p, *s, *outval;
6257c478bdstevel@tonic-gate
6267c478bdstevel@tonic-gate    if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || writeproc == NULL ) {
6277c478bdstevel@tonic-gate	return( LDAP_PARAM_ERROR );
6287c478bdstevel@tonic-gate    }
6297c478bdstevel@tonic-gate
6307c478bdstevel@tonic-gate    if ( vals == NULL ) {
6317c478bdstevel@tonic-gate	return( LDAP_SUCCESS );
6327c478bdstevel@tonic-gate    }
6337c478bdstevel@tonic-gate
6347c478bdstevel@tonic-gate    html = ( urlprefix != NULL );
6357c478bdstevel@tonic-gate
6367c478bdstevel@tonic-gate    switch( LDAP_GET_SYN_TYPE( syntaxid )) {
6377c478bdstevel@tonic-gate    case LDAP_SYN_TYPE_TEXT:
6387c478bdstevel@tonic-gate    case LDAP_SYN_TYPE_BOOLEAN:
6397c478bdstevel@tonic-gate	break;		/* we only bother with these two types... */
6407c478bdstevel@tonic-gate    default:
6417c478bdstevel@tonic-gate	return( LDAP_SUCCESS );
6427c478bdstevel@tonic-gate    }
6437c478bdstevel@tonic-gate
6447c478bdstevel@tonic-gate    if ( labelwidth == 0 || labelwidth < 0 ) {
6457c478bdstevel@tonic-gate	labelwidth = DEF_LABEL_WIDTH;
6467c478bdstevel@tonic-gate    }
6477c478bdstevel@tonic-gate
6487c478bdstevel@tonic-gate    if ( buf == NULL ) {
6497c478bdstevel@tonic-gate	if (( buf = NSLDAPI_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
6507c478bdstevel@tonic-gate	    err = LDAP_NO_MEMORY;
6517c478bdstevel@tonic-gate	    LDAP_SET_LDERRNO( ld, err, NULL, NULL );
6527c478bdstevel@tonic-gate	    return( err );
6537c478bdstevel@tonic-gate	}
6547c478bdstevel@tonic-gate	freebuf = 1;
6557c478bdstevel@tonic-gate    } else {
6567c478bdstevel@tonic-gate	freebuf = 0;
6577c478bdstevel@tonic-gate    }
6587c478bdstevel@tonic-gate
6597c478bdstevel@tonic-gate    output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
6607c478bdstevel@tonic-gate
6617c478bdstevel@tonic-gate    for ( i = 0; vals[ i ] != NULL; ++i ) {
6627c478bdstevel@tonic-gate	for ( p = vals[ i ]; *p != '\0'; ++p ) {
6637c478bdstevel@tonic-gate	    if ( !isascii( *p )) {
6647c478bdstevel@tonic-gate		break;
6657c478bdstevel@tonic-gate	    }
6667c478bdstevel@tonic-gate	}
6677c478bdstevel@tonic-gate	notascii = ( *p != '\0' );
6687c478bdstevel@tonic-gate	outval = notascii ? dgettext(TEXT_DOMAIN,
6697c478bdstevel@tonic-gate		"(unable to display non-ASCII text value)")
6707c478bdstevel@tonic-gate		: vals[ i ];
6717c478bdstevel@tonic-gate
6727c478bdstevel@tonic-gate	writeoutval = 0;	/* if non-zero, write outval after switch */
6737c478bdstevel@tonic-gate
6747c478bdstevel@tonic-gate	switch( syntaxid ) {
6757c478bdstevel@tonic-gate	case LDAP_SYN_CASEIGNORESTR:
6767c478bdstevel@tonic-gate	    ++writeoutval;
6777c478bdstevel@tonic-gate	    break;
6787c478bdstevel@tonic-gate
6797c478bdstevel@tonic-gate	case LDAP_SYN_RFC822ADDR:
6807c478bdstevel@tonic-gate	    if ( html ) {
6817c478bdstevel@tonic-gate		strcpy( buf, "<DD><A HREF=\"mailto:" );
6827c478bdstevel@tonic-gate		strcat_escaped( buf, outval );
6837c478bdstevel@tonic-gate		sprintf( buf + strlen( buf ), "\">%s</A><BR>%s", outval, eol );
6847c478bdstevel@tonic-gate		(*writeproc)( writeparm, buf, strlen( buf ));
6857c478bdstevel@tonic-gate	    } else {
6867c478bdstevel@tonic-gate		++writeoutval;
6877c478bdstevel@tonic-gate	    }
6887c478bdstevel@tonic-gate	    break;
6897c478bdstevel@tonic-gate
6907c478bdstevel@tonic-gate	case LDAP_SYN_DN:	/* for now */
6917c478bdstevel@tonic-gate	    output_dn( buf, outval, labelwidth, rdncount, writeproc,
6927c478bdstevel@tonic-gate		    writeparm, eol, urlprefix );
6937c478bdstevel@tonic-gate	    break;
6947c478bdstevel@tonic-gate
6957c478bdstevel@tonic-gate	case LDAP_SYN_MULTILINESTR:
6967c478bdstevel@tonic-gate	    if ( i > 0 && !html ) {
6977c478bdstevel@tonic-gate		output_label( buf, label, labelwidth, writeproc,
6987c478bdstevel@tonic-gate			writeparm, eol, html );
6997c478bdstevel@tonic-gate	    }
7007c478bdstevel@tonic-gate
7017c478bdstevel@tonic-gate	    p = s = outval;
7027c478bdstevel@tonic-gate	    while (( s = strchr( s, '$' )) != NULL ) {
7037c478bdstevel@tonic-gate		*s++ = '\0';
7047c478bdstevel@tonic-gate		while ( ldap_utf8isspace( s )) {
7057c478bdstevel@tonic-gate		    ++s;
7067c478bdstevel@tonic-gate		}
7077c478bdstevel@tonic-gate		if ( html ) {
7087c478bdstevel@tonic-gate		    sprintf( buf, "<DD>%s<BR>%s", p, eol );
7097c478bdstevel@tonic-gate		} else {
7107c478bdstevel@tonic-gate		    sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
7117c478bdstevel@tonic-gate		}
7127c478bdstevel@tonic-gate		(*writeproc)( writeparm, buf, strlen( buf ));
7137c478bdstevel@tonic-gate		p = s;
7147c478bdstevel@tonic-gate	    }
7157c478bdstevel@tonic-gate	    outval = p;
7167c478bdstevel@tonic-gate	    ++writeoutval;
7177c478bdstevel@tonic-gate	    break;
7187c478bdstevel@tonic-gate
7197c478bdstevel@tonic-gate	case LDAP_SYN_BOOLEAN:
7207c478bdstevel@tonic-gate	    outval = toupper( outval[ 0 ] ) == 'T' ?
7217c478bdstevel@tonic-gate		dgettext(TEXT_DOMAIN, "TRUE") : dgettext(TEXT_DOMAIN, "FALSE");
7227c478bdstevel@tonic-gate	    ++writeoutval;
7237c478bdstevel@tonic-gate	    break;
7247c478bdstevel@tonic-gate
7257c478bdstevel@tonic-gate	case LDAP_SYN_TIME:
7267c478bdstevel@tonic-gate	case LDAP_SYN_DATE:
7277c478bdstevel@tonic-gate	    outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
7287c478bdstevel@tonic-gate	    ++writeoutval;
7297c478bdstevel@tonic-gate	    break;
7307c478bdstevel@tonic-gate
7317c478bdstevel@tonic-gate	case LDAP_SYN_LABELEDURL:
7327c478bdstevel@tonic-gate	    if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
7337c478bdstevel@tonic-gate		*p++ = '\0';
7347c478bdstevel@tonic-gate		while ( ldap_utf8isspace( p )) {
7357c478bdstevel@tonic-gate		    ++p;
7367c478bdstevel@tonic-gate		}
7377c478bdstevel@tonic-gate		s = outval;
7387c478bdstevel@tonic-gate	    } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
7397c478bdstevel@tonic-gate		*s++ = '\0';
7407c478bdstevel@tonic-gate		while ( ldap_utf8isspace( s )) {
7417c478bdstevel@tonic-gate		    ++s;
7427c478bdstevel@tonic-gate		}
7437c478bdstevel@tonic-gate		p = outval;
7447c478bdstevel@tonic-gate	    } else {
7457c478bdstevel@tonic-gate		s = "URL";
7467c478bdstevel@tonic-gate		p = outval;
7477c478bdstevel@tonic-gate	    }
7487c478bdstevel@tonic-gate
7497c478bdstevel@tonic-gate	    /*
7507c478bdstevel@tonic-gate	     * at this point `s' points to the label & `p' to the URL
7517c478bdstevel@tonic-gate	     */
7527c478bdstevel@tonic-gate	    if ( html ) {
7537c478bdstevel@tonic-gate		sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
7547c478bdstevel@tonic-gate	    } else {
7557c478bdstevel@tonic-gate		sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
7567c478bdstevel@tonic-gate		    s, eol, labelwidth + 2, " ",p , eol );
7577c478bdstevel@tonic-gate	    }
7587c478bdstevel@tonic-gate	    (*writeproc)( writeparm, buf, strlen( buf ));
7597c478bdstevel@tonic-gate	    break;
7607c478bdstevel@tonic-gate
7617c478bdstevel@tonic-gate	default:
7627c478bdstevel@tonic-gate	    sprintf( buf, dgettext(TEXT_DOMAIN,
7637c478bdstevel@tonic-gate		" Can't display item type %ld%s"),
7647c478bdstevel@tonic-gate		    syntaxid, eol );
7657c478bdstevel@tonic-gate	    (*writeproc)( writeparm, buf, strlen( buf ));
7667c478bdstevel@tonic-gate	}
7677c478bdstevel@tonic-gate
7687c478bdstevel@tonic-gate	if ( writeoutval ) {
7697c478bdstevel@tonic-gate	    if ( html ) {
7707c478bdstevel@tonic-gate		sprintf( buf, "<DD>%s<BR>%s", outval, eol );
7717c478bdstevel@tonic-gate	    } else {
7727c478bdstevel@tonic-gate		sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
7737c478bdstevel@tonic-gate	    }
7747c478bdstevel@tonic-gate	    (*writeproc)( writeparm, buf, strlen( buf ));
7757c478bdstevel@tonic-gate	}
7767c478bdstevel@tonic-gate    }
7777c478bdstevel@tonic-gate
7787c478bdstevel@tonic-gate    if ( freebuf ) {
7797c478bdstevel@tonic-gate	NSLDAPI_FREE( buf );
7807c478bdstevel@tonic-gate    }
7817c478bdstevel@tonic-gate
7827c478bdstevel@tonic-gate    return( LDAP_SUCCESS );
7837c478bdstevel@tonic-gate}
7847c478bdstevel@tonic-gate
7857c478bdstevel@tonic-gate
7867c478bdstevel@tonic-gatestatic int
7877c478bdstevel@tonic-gatemax_label_len( struct ldap_disptmpl *tmpl )
7887c478bdstevel@tonic-gate{
7897c478bdstevel@tonic-gate    struct ldap_tmplitem	*rowp, *colp;
7907c478bdstevel@tonic-gate    int				len, maxlen;
7917c478bdstevel@tonic-gate
7927c478bdstevel@tonic-gate    maxlen = 0;
7937c478bdstevel@tonic-gate
7947c478bdstevel@tonic-gate    for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
7957c478bdstevel@tonic-gate	    rowp = ldap_next_tmplrow( tmpl, rowp )) {
7967c478bdstevel@tonic-gate	for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
7977c478bdstevel@tonic-gate		colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
7987c478bdstevel@tonic-gate	    if (( len = strlen( colp->ti_label )) > maxlen ) {
7997c478bdstevel@tonic-gate		maxlen = len;
8007c478bdstevel@tonic-gate	    }
8017c478bdstevel@tonic-gate	}
8027c478bdstevel@tonic-gate    }
8037c478bdstevel@tonic-gate
8047c478bdstevel@tonic-gate    return( maxlen );
8057c478bdstevel@tonic-gate}
8067c478bdstevel@tonic-gate
8077c478bdstevel@tonic-gate
8087c478bdstevel@tonic-gatestatic int
8097c478bdstevel@tonic-gateoutput_label( char *buf, char *label, int width, writeptype writeproc,
8107c478bdstevel@tonic-gate	void *writeparm, char *eol, int html )
8117c478bdstevel@tonic-gate{
8127c478bdstevel@tonic-gate    char	*p;
8137c478bdstevel@tonic-gate
8147c478bdstevel@tonic-gate    if ( html ) {
8157c478bdstevel@tonic-gate	sprintf( buf, "<DT><B>%s</B>", label );
8167c478bdstevel@tonic-gate    } else {
8177c478bdstevel@tonic-gate	auto size_t w;
8187c478bdstevel@tonic-gate	sprintf( buf, " %s:", label );
8197c478bdstevel@tonic-gate	p = buf + strlen( buf );
8207c478bdstevel@tonic-gate
8217c478bdstevel@tonic-gate	for (w = ldap_utf8characters(buf); w < (size_t)width; ++w) {
8227c478bdstevel@tonic-gate	    *p++ = ' ';
8237c478bdstevel@tonic-gate	}
8247c478bdstevel@tonic-gate
8257c478bdstevel@tonic-gate	*p = '\0';
8267c478bdstevel@tonic-gate	strcat( buf, eol );
8277c478bdstevel@tonic-gate    }
8287c478bdstevel@tonic-gate
8297c478bdstevel@tonic-gate    return ((*writeproc)( writeparm, buf, strlen( buf )));
8307c478bdstevel@tonic-gate}
8317c478bdstevel@tonic-gate
8327c478bdstevel@tonic-gate
8337c478bdstevel@tonic-gatestatic int
8347c478bdstevel@tonic-gateoutput_dn( char *buf, char *dn, int width, int rdncount,
8357c478bdstevel@tonic-gate	writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
8367c478bdstevel@tonic-gate{
8377c478bdstevel@tonic-gate    char	**dnrdns;
8387c478bdstevel@tonic-gate    int		i;
8397c478bdstevel@tonic-gate
8407c478bdstevel@tonic-gate    if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
8417c478bdstevel@tonic-gate	return( -1 );
8427c478bdstevel@tonic-gate    }
8437c478bdstevel@tonic-gate
8447c478bdstevel@tonic-gate    if ( urlprefix != NULL ) {
8457c478bdstevel@tonic-gate	sprintf( buf, "<DD><A HREF=\"%s", urlprefix );
8467c478bdstevel@tonic-gate	strcat_escaped( buf, dn );
8477c478bdstevel@tonic-gate	strcat( buf, "\">" );
8487c478bdstevel@tonic-gate    } else if ( width > 0 ) {
8497c478bdstevel@tonic-gate	sprintf( buf, "%-*s", width, " " );
8507c478bdstevel@tonic-gate    } else {
8517c478bdstevel@tonic-gate	*buf = '\0';
8527c478bdstevel@tonic-gate    }
8537c478bdstevel@tonic-gate
8547c478bdstevel@tonic-gate    for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
8557c478bdstevel@tonic-gate	    ++i ) {
8567c478bdstevel@tonic-gate	if ( i > 0 ) {
8577c478bdstevel@tonic-gate	    strcat( buf, ", " );
8587c478bdstevel@tonic-gate	}
8597c478bdstevel@tonic-gate	strcat( buf, dnrdns[ i ] );
8607c478bdstevel@tonic-gate    }
8617c478bdstevel@tonic-gate
8627c478bdstevel@tonic-gate    if ( urlprefix != NULL ) {
8637c478bdstevel@tonic-gate	strcat( buf, "</A><BR>" );
8647c478bdstevel@tonic-gate    }
8657c478bdstevel@tonic-gate
8667c478bdstevel@tonic-gate    ldap_value_free( dnrdns );
8677c478bdstevel@tonic-gate
8687c478bdstevel@tonic-gate    strcat( buf, eol );
8697c478bdstevel@tonic-gate
8707c478bdstevel@tonic-gate    return ((*writeproc)( writeparm, buf, strlen( buf )));
8717c478bdstevel@tonic-gate}
8727c478bdstevel@tonic-gate
8737c478bdstevel@tonic-gate
8747c478bdstevel@tonic-gate
8757c478bdstevel@tonic-gate#define HREF_CHAR_ACCEPTABLE( c )	(( c >= '-' && c <= '9' ) ||	\
8767c478bdstevel@tonic-gate					 ( c >= '@' && c <= 'Z' ) ||	\
8777c478bdstevel@tonic-gate					 ( c == '_' ) ||		\
8787c478bdstevel@tonic-gate					 ( c >= 'a' && c <= 'z' ))
8797c478bdstevel@tonic-gate
8807c478bdstevel@tonic-gatestatic void
8817c478bdstevel@tonic-gatestrcat_escaped( char *s1, char *s2 )
8827c478bdstevel@tonic-gate{
8837c478bdstevel@tonic-gate    char	*p, *q;
8847c478bdstevel@tonic-gate    char	*hexdig = "0123456789ABCDEF";
8857c478bdstevel@tonic-gate
8867c478bdstevel@tonic-gate    p = s1 + strlen( s1 );
8877c478bdstevel@tonic-gate    for ( q = s2; *q != '\0'; ++q ) {
8887c478bdstevel@tonic-gate	if ( HREF_CHAR_ACCEPTABLE( *q )) {
8897c478bdstevel@tonic-gate	    *p++ = *q;
8907c478bdstevel@tonic-gate	} else {
8917c478bdstevel@tonic-gate	    *p++ = '%';
8927c478bdstevel@tonic-gate	    *p++ = hexdig[ 0x0F & ((*(unsigned char*)q) >> 4) ];
8937c478bdstevel@tonic-gate	    *p++ = hexdig[ 0x0F & *q ];
8947c478bdstevel@tonic-gate	}
8957c478bdstevel@tonic-gate    }
8967c478bdstevel@tonic-gate
8977c478bdstevel@tonic-gate    *p = '\0';
8987c478bdstevel@tonic-gate}
8997c478bdstevel@tonic-gate
9007c478bdstevel@tonic-gate
9017c478bdstevel@tonic-gate#define GET2BYTENUM( p )	(( *p - '0' ) * 10 + ( *(p+1) - '0' ))
9027c478bdstevel@tonic-gate
9037c478bdstevel@tonic-gatestatic char *
9047c478bdstevel@tonic-gatetime2text( char *ldtimestr, int dateonly )
9057c478bdstevel@tonic-gate{
9067c478bdstevel@tonic-gate    int			len;
9077c478bdstevel@tonic-gate    struct tm		t;
9087c478bdstevel@tonic-gate    char		*p, *timestr, zone, *fmterr =
9097c478bdstevel@tonic-gate				dgettext(TEXT_DOMAIN, "badly formatted time");
9107c478bdstevel@tonic-gate    time_t		gmttime;
9117c478bdstevel@tonic-gate/* CTIME for this platform doesn't use this. */
9127c478bdstevel@tonic-gate#if !defined(SUNOS4) && !defined(BSDI) && !defined(LINUX1_2) && \
9137c478bdstevel@tonic-gate    !defined(SNI) && !defined(_WIN32) && !defined(macintosh) && !defined(LINUX)
9147c478bdstevel@tonic-gate    char		buf[26];
9157c478bdstevel@tonic-gate#endif
9167c478bdstevel@tonic-gate
9177c478bdstevel@tonic-gate    memset( (char *)&t, 0, sizeof( struct tm ));
9187c478bdstevel@tonic-gate    if (( len = (int)strlen( ldtimestr )) < 13 ) {
9197c478bdstevel@tonic-gate	return( fmterr );
9207c478bdstevel@tonic-gate    }
9217c478bdstevel@tonic-gate    if ( len > 15 ) {   /* throw away excess from 4-digit year time string */
9227c478bdstevel@tonic-gate	len = 15;
9237c478bdstevel@tonic-gate    } else if ( len == 14 ) {
9247c478bdstevel@tonic-gate	len = 13;       /* assume we have a time w/2-digit year (len=13) */
9257c478bdstevel@tonic-gate    }
9267c478bdstevel@tonic-gate
9277c478bdstevel@tonic-gate    for ( p = ldtimestr; p - ldtimestr + 1 < len; ++p ) {
9287c478bdstevel@tonic-gate	if ( !isdigit( *p )) {
9297c478bdstevel@tonic-gate	    return( fmterr );
9307c478bdstevel@tonic-gate	}
9317c478bdstevel@tonic-gate    }
9327c478bdstevel@tonic-gate
9337c478bdstevel@tonic-gate    p = ldtimestr;
9347c478bdstevel@tonic-gate    t.tm_year = GET2BYTENUM( p ); p += 2;
9357c478bdstevel@tonic-gate    if ( len == 15 ) {
9367c478bdstevel@tonic-gate	t.tm_year = 100 * (t.tm_year - 19);
9377c478bdstevel@tonic-gate	t.tm_year += GET2BYTENUM( p ); p += 2;
9387c478bdstevel@tonic-gate    }
9397c478bdstevel@tonic-gate    else {
9407c478bdstevel@tonic-gate	/* 2 digit years...assumed to be in the range (19)70 through
9417c478bdstevel@tonic-gate	   (20)69 ...less than 70 (for now, 38) means 20xx */
9427c478bdstevel@tonic-gate	if(t.tm_year < 70) {
9437c478bdstevel@tonic-gate	    t.tm_year += 100;
9447c478bdstevel@tonic-gate	}
9457c478bdstevel@tonic-gate    }
9467c478bdstevel@tonic-gate    t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
9477c478bdstevel@tonic-gate    t.tm_mday = GET2BYTENUM( p ); p += 2;
9487c478bdstevel@tonic-gate    t.tm_hour = GET2BYTENUM( p ); p += 2;
9497c478bdstevel@tonic-gate    t.tm_min = GET2BYTENUM( p ); p += 2;
9507c478bdstevel@tonic-gate    t.tm_sec = GET2BYTENUM( p ); p += 2;
9517c478bdstevel@tonic-gate
9527c478bdstevel@tonic-gate    if (( zone = *p ) == 'Z' ) {	/* GMT */
9537c478bdstevel@tonic-gate	zone = '\0';	/* no need to indicate on screen, so we make it null */
9547c478bdstevel@tonic-gate    }
9557c478bdstevel@tonic-gate
9567c478bdstevel@tonic-gate    gmttime = gtime( &t );
9577c478bdstevel@tonic-gate    timestr = NSLDAPI_CTIME( &gmttime, buf, sizeof(buf) );
9587c478bdstevel@tonic-gate
9597c478bdstevel@tonic-gate    timestr[ strlen( timestr ) - 1 ] = zone;	/* replace trailing newline */
9607c478bdstevel@tonic-gate    if ( dateonly ) {
9617c478bdstevel@tonic-gate	strcpy( timestr + 11, timestr + 20 );
9627c478bdstevel@tonic-gate    }
9637c478bdstevel@tonic-gate
9647c478bdstevel@tonic-gate    return( timestr );
9657c478bdstevel@tonic-gate}
9667c478bdstevel@tonic-gate
9677c478bdstevel@tonic-gate
9687c478bdstevel@tonic-gate
9697c478bdstevel@tonic-gate/* gtime.c - inverse gmtime */
9707c478bdstevel@tonic-gate
9717c478bdstevel@tonic-gate#if !defined( macintosh ) && !defined( _WINDOWS ) && !defined( DOS ) && !defined(XP_OS2)
9727c478bdstevel@tonic-gate#include <sys/time.h>
9737c478bdstevel@tonic-gate#endif /* !macintosh */
9747c478bdstevel@tonic-gate
9757c478bdstevel@tonic-gate/* gtime(): the inverse of localtime().
9767c478bdstevel@tonic-gate	This routine was supplied by Mike Accetta at CMU many years ago.
9777c478bdstevel@tonic-gate */
9787c478bdstevel@tonic-gate
9797c478bdstevel@tonic-gatestatic int	dmsize[] = {
9807c478bdstevel@tonic-gate    31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
9817c478bdstevel@tonic-gate};
9827c478bdstevel@tonic-gate
9837c478bdstevel@tonic-gate#define	dysize(y)	\
9847c478bdstevel@tonic-gate	(((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
9857c478bdstevel@tonic-gate
9867c478bdstevel@tonic-gate/*
9877c478bdstevel@tonic-gate#define	YEAR(y)		((y) >= 100 ? (y) : (y) + 1900)
9887c478bdstevel@tonic-gate*/
9897c478bdstevel@tonic-gate#define YEAR(y)		(((y) < 1900) ? ((y) + 1900) : (y))
9907c478bdstevel@tonic-gate
9917c478bdstevel@tonic-gate/*  */
9927c478bdstevel@tonic-gate
9937c478bdstevel@tonic-gatestatic long	gtime ( struct tm *tm )
9947c478bdstevel@tonic-gate{
9957c478bdstevel@tonic-gate    register int    i,
9967c478bdstevel@tonic-gate                    sec,
9977c478bdstevel@tonic-gate                    mins,
9987c478bdstevel@tonic-gate                    hour,
9997c478bdstevel@tonic-gate                    mday,
10007c478bdstevel@tonic-gate                    mon,
10017c478bdstevel@tonic-gate                    year;
10027c478bdstevel@tonic-gate    register long   result;
10037c478bdstevel@tonic-gate
10047c478bdstevel@tonic-gate    if ((sec = tm -> tm_sec) < 0 || sec > 59
10057c478bdstevel@tonic-gate	    || (mins = tm -> tm_min) < 0 || mins > 59
10067c478bdstevel@tonic-gate	    || (hour = tm -> tm_hour) < 0 || hour > 24
10077c478bdstevel@tonic-gate	    || (mday = tm -> tm_mday) < 1 || mday > 31
10087c478bdstevel@tonic-gate	    || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
10097c478bdstevel@tonic-gate	return ((long) -1);
10107c478bdstevel@tonic-gate    if (hour == 24) {
10117c478bdstevel@tonic-gate	hour = 0;
10127c478bdstevel@tonic-gate	mday++;
10137c478bdstevel@tonic-gate    }
10147c478bdstevel@tonic-gate    year = YEAR (tm -> tm_year);
10157c478bdstevel@tonic-gate
10167c478bdstevel@tonic-gate    result = 0L;
10177c478bdstevel@tonic-gate    for (i = 1970; i < year; i++)
10187c478bdstevel@tonic-gate	result += dysize (i);
10197c478bdstevel@tonic-gate    if (dysize (year) == 366 && mon >= 3)
10207c478bdstevel@tonic-gate	result++;
10217c478bdstevel@tonic-gate    while (--mon)
10227c478bdstevel@tonic-gate	result += dmsize[mon - 1];
10237c478bdstevel@tonic-gate    result += mday - 1;
10247c478bdstevel@tonic-gate    result = 24 * result + hour;
10257c478bdstevel@tonic-gate    result = 60 * result + mins;
10267c478bdstevel@tonic-gate    result = 60 * result + sec;
10277c478bdstevel@tonic-gate
10287c478bdstevel@tonic-gate    return result;
10297c478bdstevel@tonic-gate}
10307c478bdstevel@tonic-gate
10317c478bdstevel@tonic-gatestatic int
10327c478bdstevel@tonic-gatesearchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
10337c478bdstevel@tonic-gate	struct ldap_tmplitem *tip, int labelwidth, int rdncount,
10347c478bdstevel@tonic-gate	writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
10357c478bdstevel@tonic-gate{
10367c478bdstevel@tonic-gate    int			err = LDAP_SUCCESS, lderr, i, count, html;
10377c478bdstevel@tonic-gate    char		**vals, **members;
103813ddffeToomas Soome    char		*value, *filtpattern, *attr;
10397c478bdstevel@tonic-gate    char		*retattrs[2], filter[ 256 ];
10407c478bdstevel@tonic-gate    LDAPMessage		*ldmp;
10417c478bdstevel@tonic-gate    struct timeval	timeout;
10427c478bdstevel@tonic-gate
10437c478bdstevel@tonic-gate    html = ( urlprefix != NULL );
10447c478bdstevel@tonic-gate
10457c478bdstevel@tonic-gate    for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
10467c478bdstevel@tonic-gate	;
10477c478bdstevel@tonic-gate    }
10487c478bdstevel@tonic-gate    if ( i < 3 ) {
10497c478bdstevel@tonic-gate	return( LDAP_PARAM_ERROR );
10507c478bdstevel@tonic-gate    }
10517c478bdstevel@tonic-gate    attr = tip->ti_args[ 0 ];
10527c478bdstevel@tonic-gate    filtpattern = tip->ti_args[ 1 ];
10537c478bdstevel@tonic-gate    retattrs[ 0 ] = tip->ti_args[ 2 ];
10547c478bdstevel@tonic-gate    retattrs[ 1 ] = NULL;
10557c478bdstevel@tonic-gate
10567c478bdstevel@tonic-gate    vals = NULL;
10577c478bdstevel@tonic-gate    if ( attr == NULL ) {
10587c478bdstevel@tonic-gate	value = NULL;
10597c478bdstevel@tonic-gate    } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
10607c478bdstevel@tonic-gate	return( LDAP_PARAM_ERROR );
10617c478bdstevel@tonic-gate    } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
10627c478bdstevel@tonic-gate	value = dn;
10637c478bdstevel@tonic-gate    } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
10647c478bdstevel@tonic-gate	value = vals[ 0 ];
10657c478bdstevel@tonic-gate    } else {
10667c478bdstevel@tonic-gate	value = NULL;
10677c478bdstevel@tonic-gate    }
10687c478bdstevel@tonic-gate
10697c478bdstevel@tonic-gate    ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
10707c478bdstevel@tonic-gate	    value, NULL );
10717c478bdstevel@tonic-gate
10727c478bdstevel@tonic-gate    if ( html ) {
10737c478bdstevel@tonic-gate	/*
10747c478bdstevel@tonic-gate	 * if we are generating HTML, we add an HREF link that embodies this
10757c478bdstevel@tonic-gate	 * search action as an LDAP URL, instead of actually doing the search
10767c478bdstevel@tonic-gate	 * now.
10777c478bdstevel@tonic-gate	 */
10787c478bdstevel@tonic-gate	sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
10797c478bdstevel@tonic-gate	if ( base != NULL ) {
10807c478bdstevel@tonic-gate	    strcat_escaped( buf, base );
10817c478bdstevel@tonic-gate	}
10827c478bdstevel@tonic-gate	strcat( buf, "??sub?" );
10837c478bdstevel@tonic-gate	strcat_escaped( buf, filter );
10847c478bdstevel@tonic-gate	sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
10857c478bdstevel@tonic-gate		tip->ti_label, eol );
10867c478bdstevel@tonic-gate	if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
10877c478bdstevel@tonic-gate	    return( LDAP_LOCAL_ERROR );
10887c478bdstevel@tonic-gate	}
10897c478bdstevel@tonic-gate	return( LDAP_SUCCESS );
10907c478bdstevel@tonic-gate    }
10917c478bdstevel@tonic-gate
10927c478bdstevel@tonic-gate    timeout.tv_sec = SEARCH_TIMEOUT_SECS;
10937c478bdstevel@tonic-gate    timeout.tv_usec = 0;
10947c478bdstevel@tonic-gate
10957c478bdstevel@tonic-gate#ifdef CLDAP
10967c478bdstevel@tonic-gate    if ( LDAP_IS_CLDAP( ld ))
10977c478bdstevel@tonic-gate	lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
10987c478bdstevel@tonic-gate		0, &ldmp, NULL );
10997c478bdstevel@tonic-gate    else
11007c478bdstevel@tonic-gate#endif /* CLDAP */
11017c478bdstevel@tonic-gate	lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter,
11027c478bdstevel@tonic-gate		retattrs, 0, &timeout, &ldmp );
11037c478bdstevel@tonic-gate
11047c478bdstevel@tonic-gate    if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
11057c478bdstevel@tonic-gate	if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
11067c478bdstevel@tonic-gate	    if (( members = (char **)NSLDAPI_MALLOC( (count + 1)
11077c478bdstevel@tonic-gate		    * sizeof(char *))) == NULL ) {
11087c478bdstevel@tonic-gate		err = LDAP_NO_MEMORY;
11097c478bdstevel@tonic-gate	    } else {
11107c478bdstevel@tonic-gate		for ( i = 0, entry = ldap_first_entry( ld, ldmp );
11117c478bdstevel@tonic-gate			entry != NULL;
11127c478bdstevel@tonic-gate			entry = ldap_next_entry( ld, entry ), ++i ) {
11137c478bdstevel@tonic-gate		    members[ i ] = ldap_get_dn( ld, entry );
11147c478bdstevel@tonic-gate		}
11157c478bdstevel@tonic-gate		members[ i ] = NULL;
11167c478bdstevel@tonic-gate
11177c478bdstevel@tonic-gate		ldap_sort_values(ld,members, ldap_sort_strcasecmp);
11187c478bdstevel@tonic-gate
11197c478bdstevel@tonic-gate		err = do_vals2text( ld, NULL, members, tip->ti_label,
11207c478bdstevel@tonic-gate			html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
11217c478bdstevel@tonic-gate			eol, rdncount, urlprefix );
11227c478bdstevel@tonic-gate
11237c478bdstevel@tonic-gate		ldap_value_free( members );
11247c478bdstevel@tonic-gate	    }
11257c478bdstevel@tonic-gate	}
11267c478bdstevel@tonic-gate	ldap_msgfree( ldmp );
11277c478bdstevel@tonic-gate    }
11287c478bdstevel@tonic-gate
11297c478bdstevel@tonic-gate
11307c478bdstevel@tonic-gate    if ( vals != NULL ) {
11317c478bdstevel@tonic-gate	ldap_value_free( vals );
11327c478bdstevel@tonic-gate    }
11337c478bdstevel@tonic-gate
11347c478bdstevel@tonic-gate    return(( err == LDAP_SUCCESS ) ? lderr : err );
11357c478bdstevel@tonic-gate}
1136