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