1N/A/*
1N/A * Copyright (c) 2001 by Sun Microsystems, Inc.
1N/A * All rights reserved.
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A/*
1N/A * The contents of this file are subject to the Netscape Public
1N/A * License Version 1.1 (the "License"); you may not use this file
1N/A * except in compliance with the License. You may obtain a copy of
1N/A * the License at http://www.mozilla.org/NPL/
1N/A *
1N/A * Software distributed under the License is distributed on an "AS
1N/A * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
1N/A * implied. See the License for the specific language governing
1N/A * rights and limitations under the License.
1N/A *
1N/A * The Original Code is Mozilla Communicator client code, released
1N/A * March 31, 1998.
1N/A *
1N/A * The Initial Developer of the Original Code is Netscape
1N/A * Communications Corporation. Portions created by Netscape are
1N/A * Copyright (C) 1998-1999 Netscape Communications Corporation. All
1N/A * Rights Reserved.
1N/A *
1N/A * Contributor(s):
1N/A */
1N/A
1N/A/*
1N/A * tmplout.c: display template library output routines for LDAP clients
1N/A *
1N/A */
1N/A
1N/A#include "ldap-int.h"
1N/A#include "disptmpl.h"
1N/A
1N/A#if defined(_WINDOWS) || defined(aix) || defined(SCOOS) || defined(OSF1) || defined(SOLARIS)
1N/A#include <time.h> /* for struct tm and ctime */
1N/A#endif
1N/A
1N/A
1N/A/* This is totally lame, since it should be coming from time.h, but isn't. */
1N/A#if defined(SOLARIS)
1N/Achar *ctime_r(const time_t *, char *, int);
1N/A#endif
1N/A
1N/Astatic int do_entry2text( LDAP *ld, char *buf, char *base, LDAPMessage *entry,
1N/A struct ldap_disptmpl *tmpl, char **defattrs, char ***defvals,
1N/A writeptype writeproc, void *writeparm, char *eol, int rdncount,
1N/A unsigned long opts, char *urlprefix );
1N/Astatic int do_entry2text_search( LDAP *ld, char *dn, char *base,
1N/A LDAPMessage *entry, struct ldap_disptmpl *tmpllist, char **defattrs,
1N/A char ***defvals, writeptype writeproc, void *writeparm, char *eol,
1N/A int rdncount, unsigned long opts, char *urlprefix );
1N/Astatic int do_vals2text( LDAP *ld, char *buf, char **vals, char *label,
1N/A int labelwidth, unsigned long syntaxid, writeptype writeproc,
1N/A void *writeparm, char *eol, int rdncount, char *urlprefix );
1N/Astatic int max_label_len( struct ldap_disptmpl *tmpl );
1N/Astatic int output_label( char *buf, char *label, int width,
1N/A writeptype writeproc, void *writeparm, char *eol, int html );
1N/Astatic int output_dn( char *buf, char *dn, int width, int rdncount,
1N/A writeptype writeproc, void *writeparm, char *eol, char *urlprefix );
1N/Astatic void strcat_escaped( char *s1, char *s2 );
1N/Astatic char *time2text( char *ldtimestr, int dateonly );
1N/Astatic long gtime( struct tm *tm );
1N/Astatic int searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry,
1N/A char *dn, struct ldap_tmplitem *tip, int labelwidth, int rdncount,
1N/A writeptype writeproc, void *writeparm, char *eol, char *urlprefix );
1N/A
1N/A#define DEF_LABEL_WIDTH 15
1N/A#define SEARCH_TIMEOUT_SECS 120
1N/A#define OCATTRNAME "objectClass"
1N/A
1N/A
1N/A#define NONFATAL_LDAP_ERR( err ) ( err == LDAP_SUCCESS || \
1N/A err == LDAP_TIMELIMIT_EXCEEDED || err == LDAP_SIZELIMIT_EXCEEDED )
1N/A
1N/A#define DEF_LDAP_URL_PREFIX "ldap:///"
1N/A
1N/A
1N/Aint
1N/ALDAP_CALL
1N/Aldap_entry2text(
1N/A LDAP *ld,
1N/A char *buf, /* NULL for "use internal" */
1N/A LDAPMessage *entry,
1N/A struct ldap_disptmpl *tmpl,
1N/A char **defattrs,
1N/A char ***defvals,
1N/A writeptype writeproc,
1N/A void *writeparm,
1N/A char *eol,
1N/A int rdncount,
1N/A unsigned long opts
1N/A)
1N/A{
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2text\n", 0, 0, 0 );
1N/A
1N/A return( do_entry2text( ld, buf, NULL, entry, tmpl, defattrs, defvals,
1N/A writeproc, writeparm, eol, rdncount, opts, NULL ));
1N/A
1N/A}
1N/A
1N/A
1N/A
1N/Aint
1N/ALDAP_CALL
1N/Aldap_entry2html(
1N/A LDAP *ld,
1N/A char *buf, /* NULL for "use internal" */
1N/A LDAPMessage *entry,
1N/A struct ldap_disptmpl *tmpl,
1N/A char **defattrs,
1N/A char ***defvals,
1N/A writeptype writeproc,
1N/A void *writeparm,
1N/A char *eol,
1N/A int rdncount,
1N/A unsigned long opts,
1N/A char *base,
1N/A char *urlprefix
1N/A)
1N/A{
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2html\n", 0, 0, 0 );
1N/A
1N/A if ( urlprefix == NULL ) {
1N/A urlprefix = DEF_LDAP_URL_PREFIX;
1N/A }
1N/A
1N/A return( do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
1N/A writeproc, writeparm, eol, rdncount, opts, urlprefix ));
1N/A}
1N/A
1N/A
1N/Astatic int
1N/Ado_entry2text(
1N/A LDAP *ld,
1N/A char *buf, /* NULL for use-internal */
1N/A char *base, /* used for search actions */
1N/A LDAPMessage *entry,
1N/A struct ldap_disptmpl *tmpl,
1N/A char **defattrs,
1N/A char ***defvals,
1N/A writeptype writeproc,
1N/A void *writeparm,
1N/A char *eol,
1N/A int rdncount,
1N/A unsigned long opts,
1N/A char *urlprefix /* if non-NULL, do HTML */
1N/A)
1N/A{
1N/A int i, err, html, show, labelwidth;
1N/A int freebuf, freevals;
1N/A char *dn, **vals;
1N/A struct ldap_tmplitem *rowp, *colp;
1N/A
1N/A if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A if ( writeproc == NULL ||
1N/A !NSLDAPI_VALID_LDAPMESSAGE_ENTRY_POINTER( entry )) {
1N/A err = LDAP_PARAM_ERROR;
1N/A LDAP_SET_LDERRNO( ld, err, NULL, NULL );
1N/A return( err );
1N/A }
1N/A
1N/A if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
1N/A return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
1N/A }
1N/A
1N/A if ( buf == NULL ) {
1N/A if (( buf = NSLDAPI_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
1N/A err = LDAP_NO_MEMORY;
1N/A LDAP_SET_LDERRNO( ld, err, NULL, NULL );
1N/A NSLDAPI_FREE( dn );
1N/A return( err );
1N/A }
1N/A freebuf = 1;
1N/A } else {
1N/A freebuf = 0;
1N/A }
1N/A
1N/A html = ( urlprefix != NULL );
1N/A
1N/A if ( html ) {
1N/A /*
1N/A * add HTML intro. and title
1N/A */
1N/A if (!(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
1N/A sprintf( buf, "<HTML>%s<HEAD>%s<TITLE>%s%s - ", eol, eol, eol,
1N/A ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
1N/A (*writeproc)( writeparm, buf, strlen( buf ));
1N/A output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
1N/A sprintf( buf, "%s</TITLE>%s</HEAD>%s<BODY>%s<H3>%s - ", eol, eol,
1N/A eol, eol, ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
1N/A (*writeproc)( writeparm, buf, strlen( buf ));
1N/A output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
1N/A sprintf( buf, "</H3>%s", eol );
1N/A (*writeproc)( writeparm, buf, strlen( buf ));
1N/A }
1N/A
1N/A if (( opts & LDAP_DISP_OPT_NONLEAF ) != 0 &&
1N/A ( vals = ldap_explode_dn( dn, 0 )) != NULL ) {
1N/A char *untagged;
1N/A
1N/A /*
1N/A * add "Move Up" link
1N/A */
1N/A sprintf( buf, "<A HREF=\"%s", urlprefix );
1N/A for ( i = 1; vals[ i ] != NULL; ++i ) {
1N/A if ( i > 1 ) {
1N/A strcat_escaped( buf, ", " );
1N/A }
1N/A strcat_escaped( buf, vals[ i ] );
1N/A }
1N/A if ( vals[ 1 ] != NULL ) {
1N/A untagged = strchr( vals[ 1 ], '=' );
1N/A } else {
1N/A untagged = "=The World";
1N/A }
1N/A sprintf( buf + strlen( buf ),
1N/A "%s\">Move Up To <EM>%s</EM></A>%s<BR>",
1N/A ( vals[ 1 ] == NULL ) ? "??one" : "",
1N/A ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol );
1N/A (*writeproc)( writeparm, buf, strlen( buf ));
1N/A
1N/A /*
1N/A * add "Browse" link
1N/A */
1N/A untagged = strchr( vals[ 0 ], '=' );
1N/A sprintf( buf, "<A HREF=\"%s", urlprefix );
1N/A strcat_escaped( buf, dn );
1N/A sprintf( buf + strlen( buf ), "??one?(!(objectClass=dsa))\">Browse Below <EM>%s</EM></A>%s%s",
1N/A ( untagged != NULL ) ? untagged + 1 : vals[ 0 ], eol, eol );
1N/A (*writeproc)( writeparm, buf, strlen( buf ));
1N/A
1N/A ldap_value_free( vals );
1N/A }
1N/A
1N/A (*writeproc)( writeparm, "<HR>", 4 ); /* horizontal rule */
1N/A } else {
1N/A (*writeproc)( writeparm, "\"", 1 );
1N/A output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
1N/A sprintf( buf, "\"%s", eol );
1N/A (*writeproc)( writeparm, buf, strlen( buf ));
1N/A }
1N/A
1N/A if ( tmpl != NULL && ( opts & LDAP_DISP_OPT_AUTOLABELWIDTH ) != 0 ) {
1N/A labelwidth = max_label_len( tmpl ) + 3;
1N/A } else {
1N/A labelwidth = DEF_LABEL_WIDTH;;
1N/A }
1N/A
1N/A err = LDAP_SUCCESS;
1N/A
1N/A if ( tmpl == NULL ) {
1N/A BerElement *ber;
1N/A char *attr;
1N/A
1N/A ber = NULL;
1N/A for ( attr = ldap_first_attribute( ld, entry, &ber );
1N/A NONFATAL_LDAP_ERR( err ) && attr != NULL;
1N/A attr = ldap_next_attribute( ld, entry, ber )) {
1N/A if (( vals = ldap_get_values( ld, entry, attr )) == NULL ) {
1N/A freevals = 0;
1N/A if ( defattrs != NULL ) {
1N/A for ( i = 0; defattrs[ i ] != NULL; ++i ) {
1N/A if ( strcasecmp( attr, defattrs[ i ] ) == 0 ) {
1N/A break;
1N/A }
1N/A }
1N/A if ( defattrs[ i ] != NULL ) {
1N/A vals = defvals[ i ];
1N/A }
1N/A }
1N/A } else {
1N/A freevals = 1;
1N/A }
1N/A
1N/A if ( islower( *attr )) { /* cosmetic -- upcase attr. name */
1N/A *attr = toupper( *attr );
1N/A }
1N/A
1N/A err = do_vals2text( ld, buf, vals, attr, labelwidth,
1N/A LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol,
1N/A rdncount, urlprefix );
1N/A if ( freevals ) {
1N/A ldap_value_free( vals );
1N/A }
1N/A }
1N/A if ( ber == NULL ) {
1N/A ber_free( ber, 0 );
1N/A }
1N/A /*
1N/A * XXX check for errors in ldap_first_attribute/ldap_next_attribute
1N/A * here (but what should we do if there was one?)
1N/A */
1N/A
1N/A } else {
1N/A for ( rowp = ldap_first_tmplrow( tmpl );
1N/A NONFATAL_LDAP_ERR( err ) && rowp != NULLTMPLITEM;
1N/A rowp = ldap_next_tmplrow( tmpl, rowp )) {
1N/A for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
1N/A colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
1N/A vals = NULL;
1N/A if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
1N/A entry, colp->ti_attrname )) == NULL ) {
1N/A freevals = 0;
1N/A if ( !LDAP_IS_TMPLITEM_OPTION_SET( colp,
1N/A LDAP_DITEM_OPT_HIDEIFEMPTY ) && defattrs != NULL
1N/A && colp->ti_attrname != NULL ) {
1N/A for ( i = 0; defattrs[ i ] != NULL; ++i ) {
1N/A if ( strcasecmp( colp->ti_attrname, defattrs[ i ] )
1N/A == 0 ) {
1N/A break;
1N/A }
1N/A }
1N/A if ( defattrs[ i ] != NULL ) {
1N/A vals = defvals[ i ];
1N/A }
1N/A }
1N/A } else {
1N/A freevals = 1;
1N/A if ( LDAP_IS_TMPLITEM_OPTION_SET( colp,
1N/A LDAP_DITEM_OPT_SORTVALUES ) && vals[ 0 ] != NULL
1N/A && vals[ 1 ] != NULL ) {
1N/A ldap_sort_values(ld, vals, ldap_sort_strcasecmp);
1N/A }
1N/A }
1N/A
1N/A /*
1N/A * don't bother even calling do_vals2text() if no values
1N/A * or boolean with value false and "hide if false" option set
1N/A */
1N/A show = ( vals != NULL && vals[ 0 ] != NULL );
1N/A if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
1N/A == LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
1N/A colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
1N/A toupper( vals[ 0 ][ 0 ] ) != 'T' ) {
1N/A show = 0;
1N/A }
1N/A
1N/A if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
1N/A if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
1N/A if ( colp->ti_attrname == NULL || ( show &&
1N/A toupper( vals[ 0 ][ 0 ] ) == 'T' )) {
1N/A err = searchaction( ld, buf, base, entry, dn, colp,
1N/A labelwidth, rdncount, writeproc,
1N/A writeparm, eol, urlprefix );
1N/A }
1N/A }
1N/A show = 0;
1N/A }
1N/A
1N/A if ( show ) {
1N/A err = do_vals2text( ld, buf, vals, colp->ti_label,
1N/A labelwidth, colp->ti_syntaxid, writeproc, writeparm,
1N/A eol, rdncount, urlprefix );
1N/A }
1N/A
1N/A if ( freevals ) {
1N/A ldap_value_free( vals );
1N/A }
1N/A }
1N/A }
1N/A }
1N/A
1N/A if ( html && !(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
1N/A sprintf( buf, "</BODY>%s</HTML>%s", eol, eol );
1N/A (*writeproc)( writeparm, buf, strlen( buf ));
1N/A }
1N/A
1N/A NSLDAPI_FREE( dn );
1N/A if ( freebuf ) {
1N/A NSLDAPI_FREE( buf );
1N/A }
1N/A
1N/A return( err );
1N/A}
1N/A
1N/A
1N/Aint
1N/ALDAP_CALL
1N/Aldap_entry2text_search(
1N/A LDAP *ld,
1N/A char *dn, /* if NULL, use entry */
1N/A char *base, /* if NULL, no search actions */
1N/A LDAPMessage *entry, /* if NULL, use dn */
1N/A struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
1N/A char **defattrs,
1N/A char ***defvals,
1N/A writeptype writeproc,
1N/A void *writeparm,
1N/A char *eol,
1N/A int rdncount, /* if 0, display full DN */
1N/A unsigned long opts
1N/A)
1N/A{
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2text_search\n", 0, 0, 0 );
1N/A
1N/A return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
1N/A defvals, writeproc, writeparm, eol, rdncount, opts, NULL ));
1N/A}
1N/A
1N/A
1N/A
1N/Aint
1N/ALDAP_CALL
1N/Aldap_entry2html_search(
1N/A LDAP *ld,
1N/A char *dn, /* if NULL, use entry */
1N/A char *base, /* if NULL, no search actions */
1N/A LDAPMessage *entry, /* if NULL, use dn */
1N/A struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
1N/A char **defattrs,
1N/A char ***defvals,
1N/A writeptype writeproc,
1N/A void *writeparm,
1N/A char *eol,
1N/A int rdncount, /* if 0, display full DN */
1N/A unsigned long opts,
1N/A char *urlprefix
1N/A)
1N/A{
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2html_search\n", 0, 0, 0 );
1N/A
1N/A return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
1N/A defvals, writeproc, writeparm, eol, rdncount, opts, urlprefix ));
1N/A}
1N/A
1N/A
1N/Astatic int
1N/Ado_entry2text_search(
1N/A LDAP *ld,
1N/A char *dn, /* if NULL, use entry */
1N/A char *base, /* if NULL, no search actions */
1N/A LDAPMessage *entry, /* if NULL, use dn */
1N/A struct ldap_disptmpl* tmpllist, /* if NULL, no template used */
1N/A char **defattrs,
1N/A char ***defvals,
1N/A writeptype writeproc,
1N/A void *writeparm,
1N/A char *eol,
1N/A int rdncount, /* if 0, display full DN */
1N/A unsigned long opts,
1N/A char *urlprefix
1N/A)
1N/A{
1N/A int err, freedn, html;
1N/A char *buf, **fetchattrs, **vals;
1N/A LDAPMessage *ldmp;
1N/A struct ldap_disptmpl *tmpl;
1N/A struct timeval timeout;
1N/A
1N/A if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A if ( dn == NULL && entry == NULLMSG ) {
1N/A err = LDAP_PARAM_ERROR;
1N/A LDAP_SET_LDERRNO( ld, err, NULL, NULL );
1N/A return( err );
1N/A }
1N/A
1N/A html = ( urlprefix != NULL );
1N/A
1N/A timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1N/A timeout.tv_usec = 0;
1N/A
1N/A if (( buf = NSLDAPI_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
1N/A err = LDAP_NO_MEMORY;
1N/A LDAP_SET_LDERRNO( ld, err, NULL, NULL );
1N/A return( err );
1N/A }
1N/A
1N/A freedn = 0;
1N/A tmpl = NULL;
1N/A
1N/A if ( dn == NULL ) {
1N/A if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
1N/A NSLDAPI_FREE( buf );
1N/A return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
1N/A }
1N/A freedn = 1;
1N/A }
1N/A
1N/A
1N/A if ( tmpllist != NULL ) {
1N/A ldmp = NULLMSG;
1N/A
1N/A if ( entry == NULL ) {
1N/A char *ocattrs[2];
1N/A
1N/A ocattrs[0] = OCATTRNAME;
1N/A ocattrs[1] = NULL;
1N/A#ifdef CLDAP
1N/A if ( LDAP_IS_CLDAP( ld ))
1N/A err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE,
1N/A "objectClass=*", ocattrs, 0, &ldmp, NULL );
1N/A else
1N/A#endif /* CLDAP */
1N/A err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
1N/A "objectClass=*", ocattrs, 0, &timeout, &ldmp );
1N/A
1N/A if ( err == LDAP_SUCCESS ) {
1N/A entry = ldap_first_entry( ld, ldmp );
1N/A }
1N/A }
1N/A
1N/A if ( entry != NULL ) {
1N/A vals = ldap_get_values( ld, entry, OCATTRNAME );
1N/A tmpl = ldap_oc2template( vals, tmpllist );
1N/A if ( vals != NULL ) {
1N/A ldap_value_free( vals );
1N/A }
1N/A }
1N/A if ( ldmp != NULL ) {
1N/A ldap_msgfree( ldmp );
1N/A }
1N/A }
1N/A
1N/A entry = NULL;
1N/A
1N/A if ( tmpl == NULL ) {
1N/A fetchattrs = NULL;
1N/A } else {
1N/A fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
1N/A }
1N/A
1N/A#ifdef CLDAP
1N/A if ( LDAP_IS_CLDAP( ld ))
1N/A err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
1N/A fetchattrs, 0, &ldmp, NULL );
1N/A else
1N/A#endif /* CLDAP */
1N/A err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
1N/A fetchattrs, 0, &timeout, &ldmp );
1N/A
1N/A if ( freedn ) {
1N/A NSLDAPI_FREE( dn );
1N/A }
1N/A if ( fetchattrs != NULL ) {
1N/A ldap_value_free( fetchattrs );
1N/A }
1N/A
1N/A if ( err != LDAP_SUCCESS ||
1N/A ( entry = ldap_first_entry( ld, ldmp )) == NULL ) {
1N/A NSLDAPI_FREE( buf );
1N/A return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
1N/A }
1N/A
1N/A err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
1N/A writeproc, writeparm, eol, rdncount, opts, urlprefix );
1N/A
1N/A NSLDAPI_FREE( buf );
1N/A ldap_msgfree( ldmp );
1N/A return( err );
1N/A}
1N/A
1N/A
1N/Aint
1N/ALDAP_CALL
1N/Aldap_vals2text(
1N/A LDAP *ld,
1N/A char *buf, /* NULL for "use internal" */
1N/A char **vals,
1N/A char *label,
1N/A int labelwidth, /* 0 means use default */
1N/A unsigned long syntaxid,
1N/A writeptype writeproc,
1N/A void *writeparm,
1N/A char *eol,
1N/A int rdncount
1N/A)
1N/A{
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
1N/A
1N/A return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
1N/A writeproc, writeparm, eol, rdncount, NULL ));
1N/A}
1N/A
1N/A
1N/Aint
1N/ALDAP_CALL
1N/Aldap_vals2html(
1N/A LDAP *ld,
1N/A char *buf, /* NULL for "use internal" */
1N/A char **vals,
1N/A char *label,
1N/A int labelwidth, /* 0 means use default */
1N/A unsigned long syntaxid,
1N/A writeptype writeproc,
1N/A void *writeparm,
1N/A char *eol,
1N/A int rdncount,
1N/A char *urlprefix
1N/A)
1N/A{
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
1N/A
1N/A if ( urlprefix == NULL ) {
1N/A urlprefix = DEF_LDAP_URL_PREFIX;
1N/A }
1N/A
1N/A return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
1N/A writeproc, writeparm, eol, rdncount, urlprefix ));
1N/A}
1N/A
1N/A
1N/Astatic int
1N/Ado_vals2text(
1N/A LDAP *ld,
1N/A char *buf, /* NULL for "use internal" */
1N/A char **vals,
1N/A char *label,
1N/A int labelwidth, /* 0 means use default */
1N/A unsigned long syntaxid,
1N/A writeptype writeproc,
1N/A void *writeparm,
1N/A char *eol,
1N/A int rdncount,
1N/A char *urlprefix
1N/A)
1N/A{
1N/A int err, i, html, writeoutval, freebuf, notascii;
1N/A char *p, *s, *outval;
1N/A
1N/A if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || writeproc == NULL ) {
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A if ( vals == NULL ) {
1N/A return( LDAP_SUCCESS );
1N/A }
1N/A
1N/A html = ( urlprefix != NULL );
1N/A
1N/A switch( LDAP_GET_SYN_TYPE( syntaxid )) {
1N/A case LDAP_SYN_TYPE_TEXT:
1N/A case LDAP_SYN_TYPE_BOOLEAN:
1N/A break; /* we only bother with these two types... */
1N/A default:
1N/A return( LDAP_SUCCESS );
1N/A }
1N/A
1N/A if ( labelwidth == 0 || labelwidth < 0 ) {
1N/A labelwidth = DEF_LABEL_WIDTH;
1N/A }
1N/A
1N/A if ( buf == NULL ) {
1N/A if (( buf = NSLDAPI_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
1N/A err = LDAP_NO_MEMORY;
1N/A LDAP_SET_LDERRNO( ld, err, NULL, NULL );
1N/A return( err );
1N/A }
1N/A freebuf = 1;
1N/A } else {
1N/A freebuf = 0;
1N/A }
1N/A
1N/A output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
1N/A
1N/A for ( i = 0; vals[ i ] != NULL; ++i ) {
1N/A for ( p = vals[ i ]; *p != '\0'; ++p ) {
1N/A if ( !isascii( *p )) {
1N/A break;
1N/A }
1N/A }
1N/A notascii = ( *p != '\0' );
1N/A outval = notascii ? dgettext(TEXT_DOMAIN,
1N/A "(unable to display non-ASCII text value)")
1N/A : vals[ i ];
1N/A
1N/A writeoutval = 0; /* if non-zero, write outval after switch */
1N/A
1N/A switch( syntaxid ) {
1N/A case LDAP_SYN_CASEIGNORESTR:
1N/A ++writeoutval;
1N/A break;
1N/A
1N/A case LDAP_SYN_RFC822ADDR:
1N/A if ( html ) {
1N/A strcpy( buf, "<DD><A HREF=\"mailto:" );
1N/A strcat_escaped( buf, outval );
1N/A sprintf( buf + strlen( buf ), "\">%s</A><BR>%s", outval, eol );
1N/A (*writeproc)( writeparm, buf, strlen( buf ));
1N/A } else {
1N/A ++writeoutval;
1N/A }
1N/A break;
1N/A
1N/A case LDAP_SYN_DN: /* for now */
1N/A output_dn( buf, outval, labelwidth, rdncount, writeproc,
1N/A writeparm, eol, urlprefix );
1N/A break;
1N/A
1N/A case LDAP_SYN_MULTILINESTR:
1N/A if ( i > 0 && !html ) {
1N/A output_label( buf, label, labelwidth, writeproc,
1N/A writeparm, eol, html );
1N/A }
1N/A
1N/A p = s = outval;
1N/A while (( s = strchr( s, '$' )) != NULL ) {
1N/A *s++ = '\0';
1N/A while ( ldap_utf8isspace( s )) {
1N/A ++s;
1N/A }
1N/A if ( html ) {
1N/A sprintf( buf, "<DD>%s<BR>%s", p, eol );
1N/A } else {
1N/A sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
1N/A }
1N/A (*writeproc)( writeparm, buf, strlen( buf ));
1N/A p = s;
1N/A }
1N/A outval = p;
1N/A ++writeoutval;
1N/A break;
1N/A
1N/A case LDAP_SYN_BOOLEAN:
1N/A outval = toupper( outval[ 0 ] ) == 'T' ?
1N/A dgettext(TEXT_DOMAIN, "TRUE") : dgettext(TEXT_DOMAIN, "FALSE");
1N/A ++writeoutval;
1N/A break;
1N/A
1N/A case LDAP_SYN_TIME:
1N/A case LDAP_SYN_DATE:
1N/A outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
1N/A ++writeoutval;
1N/A break;
1N/A
1N/A case LDAP_SYN_LABELEDURL:
1N/A if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
1N/A *p++ = '\0';
1N/A while ( ldap_utf8isspace( p )) {
1N/A ++p;
1N/A }
1N/A s = outval;
1N/A } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
1N/A *s++ = '\0';
1N/A while ( ldap_utf8isspace( s )) {
1N/A ++s;
1N/A }
1N/A p = outval;
1N/A } else {
1N/A s = "URL";
1N/A p = outval;
1N/A }
1N/A
1N/A /*
1N/A * at this point `s' points to the label & `p' to the URL
1N/A */
1N/A if ( html ) {
1N/A sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
1N/A } else {
1N/A sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
1N/A s, eol, labelwidth + 2, " ",p , eol );
1N/A }
1N/A (*writeproc)( writeparm, buf, strlen( buf ));
1N/A break;
1N/A
1N/A default:
1N/A sprintf( buf, dgettext(TEXT_DOMAIN,
1N/A " Can't display item type %ld%s"),
1N/A syntaxid, eol );
1N/A (*writeproc)( writeparm, buf, strlen( buf ));
1N/A }
1N/A
1N/A if ( writeoutval ) {
1N/A if ( html ) {
1N/A sprintf( buf, "<DD>%s<BR>%s", outval, eol );
1N/A } else {
1N/A sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
1N/A }
1N/A (*writeproc)( writeparm, buf, strlen( buf ));
1N/A }
1N/A }
1N/A
1N/A if ( freebuf ) {
1N/A NSLDAPI_FREE( buf );
1N/A }
1N/A
1N/A return( LDAP_SUCCESS );
1N/A}
1N/A
1N/A
1N/Astatic int
1N/Amax_label_len( struct ldap_disptmpl *tmpl )
1N/A{
1N/A struct ldap_tmplitem *rowp, *colp;
1N/A int len, maxlen;
1N/A
1N/A maxlen = 0;
1N/A
1N/A for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
1N/A rowp = ldap_next_tmplrow( tmpl, rowp )) {
1N/A for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
1N/A colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
1N/A if (( len = strlen( colp->ti_label )) > maxlen ) {
1N/A maxlen = len;
1N/A }
1N/A }
1N/A }
1N/A
1N/A return( maxlen );
1N/A}
1N/A
1N/A
1N/Astatic int
1N/Aoutput_label( char *buf, char *label, int width, writeptype writeproc,
1N/A void *writeparm, char *eol, int html )
1N/A{
1N/A char *p;
1N/A
1N/A if ( html ) {
1N/A sprintf( buf, "<DT><B>%s</B>", label );
1N/A } else {
1N/A auto size_t w;
1N/A sprintf( buf, " %s:", label );
1N/A p = buf + strlen( buf );
1N/A
1N/A for (w = ldap_utf8characters(buf); w < (size_t)width; ++w) {
1N/A *p++ = ' ';
1N/A }
1N/A
1N/A *p = '\0';
1N/A strcat( buf, eol );
1N/A }
1N/A
1N/A return ((*writeproc)( writeparm, buf, strlen( buf )));
1N/A}
1N/A
1N/A
1N/Astatic int
1N/Aoutput_dn( char *buf, char *dn, int width, int rdncount,
1N/A writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
1N/A{
1N/A char **dnrdns;
1N/A int i;
1N/A
1N/A if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
1N/A return( -1 );
1N/A }
1N/A
1N/A if ( urlprefix != NULL ) {
1N/A sprintf( buf, "<DD><A HREF=\"%s", urlprefix );
1N/A strcat_escaped( buf, dn );
1N/A strcat( buf, "\">" );
1N/A } else if ( width > 0 ) {
1N/A sprintf( buf, "%-*s", width, " " );
1N/A } else {
1N/A *buf = '\0';
1N/A }
1N/A
1N/A for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
1N/A ++i ) {
1N/A if ( i > 0 ) {
1N/A strcat( buf, ", " );
1N/A }
1N/A strcat( buf, dnrdns[ i ] );
1N/A }
1N/A
1N/A if ( urlprefix != NULL ) {
1N/A strcat( buf, "</A><BR>" );
1N/A }
1N/A
1N/A ldap_value_free( dnrdns );
1N/A
1N/A strcat( buf, eol );
1N/A
1N/A return ((*writeproc)( writeparm, buf, strlen( buf )));
1N/A}
1N/A
1N/A
1N/A
1N/A#define HREF_CHAR_ACCEPTABLE( c ) (( c >= '-' && c <= '9' ) || \
1N/A ( c >= '@' && c <= 'Z' ) || \
1N/A ( c == '_' ) || \
1N/A ( c >= 'a' && c <= 'z' ))
1N/A
1N/Astatic void
1N/Astrcat_escaped( char *s1, char *s2 )
1N/A{
1N/A char *p, *q;
1N/A char *hexdig = "0123456789ABCDEF";
1N/A
1N/A p = s1 + strlen( s1 );
1N/A for ( q = s2; *q != '\0'; ++q ) {
1N/A if ( HREF_CHAR_ACCEPTABLE( *q )) {
1N/A *p++ = *q;
1N/A } else {
1N/A *p++ = '%';
1N/A *p++ = hexdig[ 0x0F & ((*(unsigned char*)q) >> 4) ];
1N/A *p++ = hexdig[ 0x0F & *q ];
1N/A }
1N/A }
1N/A
1N/A *p = '\0';
1N/A}
1N/A
1N/A
1N/A#define GET2BYTENUM( p ) (( *p - '0' ) * 10 + ( *(p+1) - '0' ))
1N/A
1N/Astatic char *
1N/Atime2text( char *ldtimestr, int dateonly )
1N/A{
1N/A int len;
1N/A struct tm t;
1N/A char *p, *timestr, zone, *fmterr =
1N/A dgettext(TEXT_DOMAIN, "badly formatted time");
1N/A time_t gmttime;
1N/A/* CTIME for this platform doesn't use this. */
1N/A#if !defined(SUNOS4) && !defined(BSDI) && !defined(LINUX1_2) && \
1N/A !defined(SNI) && !defined(_WIN32) && !defined(macintosh) && !defined(LINUX)
1N/A char buf[26];
1N/A#endif
1N/A
1N/A memset( (char *)&t, 0, sizeof( struct tm ));
1N/A if (( len = (int)strlen( ldtimestr )) < 13 ) {
1N/A return( fmterr );
1N/A }
1N/A if ( len > 15 ) { /* throw away excess from 4-digit year time string */
1N/A len = 15;
1N/A } else if ( len == 14 ) {
1N/A len = 13; /* assume we have a time w/2-digit year (len=13) */
1N/A }
1N/A
1N/A for ( p = ldtimestr; p - ldtimestr + 1 < len; ++p ) {
1N/A if ( !isdigit( *p )) {
1N/A return( fmterr );
1N/A }
1N/A }
1N/A
1N/A p = ldtimestr;
1N/A t.tm_year = GET2BYTENUM( p ); p += 2;
1N/A if ( len == 15 ) {
1N/A t.tm_year = 100 * (t.tm_year - 19);
1N/A t.tm_year += GET2BYTENUM( p ); p += 2;
1N/A }
1N/A else {
1N/A /* 2 digit years...assumed to be in the range (19)70 through
1N/A (20)69 ...less than 70 (for now, 38) means 20xx */
1N/A if(t.tm_year < 70) {
1N/A t.tm_year += 100;
1N/A }
1N/A }
1N/A t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
1N/A t.tm_mday = GET2BYTENUM( p ); p += 2;
1N/A t.tm_hour = GET2BYTENUM( p ); p += 2;
1N/A t.tm_min = GET2BYTENUM( p ); p += 2;
1N/A t.tm_sec = GET2BYTENUM( p ); p += 2;
1N/A
1N/A if (( zone = *p ) == 'Z' ) { /* GMT */
1N/A zone = '\0'; /* no need to indicate on screen, so we make it null */
1N/A }
1N/A
1N/A gmttime = gtime( &t );
1N/A timestr = NSLDAPI_CTIME( &gmttime, buf, sizeof(buf) );
1N/A
1N/A timestr[ strlen( timestr ) - 1 ] = zone; /* replace trailing newline */
1N/A if ( dateonly ) {
1N/A strcpy( timestr + 11, timestr + 20 );
1N/A }
1N/A
1N/A return( timestr );
1N/A}
1N/A
1N/A
1N/A
1N/A/* gtime.c - inverse gmtime */
1N/A
1N/A#if !defined( macintosh ) && !defined( _WINDOWS ) && !defined( DOS ) && !defined(XP_OS2)
1N/A#include <sys/time.h>
1N/A#endif /* !macintosh */
1N/A
1N/A/* gtime(): the inverse of localtime().
1N/A This routine was supplied by Mike Accetta at CMU many years ago.
1N/A */
1N/A
1N/Astatic int dmsize[] = {
1N/A 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1N/A};
1N/A
1N/A#define dysize(y) \
1N/A (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
1N/A
1N/A/*
1N/A#define YEAR(y) ((y) >= 100 ? (y) : (y) + 1900)
1N/A*/
1N/A#define YEAR(y) (((y) < 1900) ? ((y) + 1900) : (y))
1N/A
1N/A/* */
1N/A
1N/Astatic long gtime ( struct tm *tm )
1N/A{
1N/A register int i,
1N/A sec,
1N/A mins,
1N/A hour,
1N/A mday,
1N/A mon,
1N/A year;
1N/A register long result;
1N/A
1N/A if ((sec = tm -> tm_sec) < 0 || sec > 59
1N/A || (mins = tm -> tm_min) < 0 || mins > 59
1N/A || (hour = tm -> tm_hour) < 0 || hour > 24
1N/A || (mday = tm -> tm_mday) < 1 || mday > 31
1N/A || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
1N/A return ((long) -1);
1N/A if (hour == 24) {
1N/A hour = 0;
1N/A mday++;
1N/A }
1N/A year = YEAR (tm -> tm_year);
1N/A
1N/A result = 0L;
1N/A for (i = 1970; i < year; i++)
1N/A result += dysize (i);
1N/A if (dysize (year) == 366 && mon >= 3)
1N/A result++;
1N/A while (--mon)
1N/A result += dmsize[mon - 1];
1N/A result += mday - 1;
1N/A result = 24 * result + hour;
1N/A result = 60 * result + mins;
1N/A result = 60 * result + sec;
1N/A
1N/A return result;
1N/A}
1N/A
1N/Astatic int
1N/Asearchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
1N/A struct ldap_tmplitem *tip, int labelwidth, int rdncount,
1N/A writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
1N/A{
1N/A int err = LDAP_SUCCESS, lderr, i, count, html;
1N/A char **vals, **members;
1N/A char *value, *filtpattern, *attr, *selectname;
1N/A char *retattrs[2], filter[ 256 ];
1N/A LDAPMessage *ldmp;
1N/A struct timeval timeout;
1N/A
1N/A html = ( urlprefix != NULL );
1N/A
1N/A for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
1N/A ;
1N/A }
1N/A if ( i < 3 ) {
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A attr = tip->ti_args[ 0 ];
1N/A filtpattern = tip->ti_args[ 1 ];
1N/A retattrs[ 0 ] = tip->ti_args[ 2 ];
1N/A retattrs[ 1 ] = NULL;
1N/A selectname = tip->ti_args[ 3 ];
1N/A
1N/A vals = NULL;
1N/A if ( attr == NULL ) {
1N/A value = NULL;
1N/A } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
1N/A return( LDAP_PARAM_ERROR );
1N/A } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
1N/A value = dn;
1N/A } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
1N/A value = vals[ 0 ];
1N/A } else {
1N/A value = NULL;
1N/A }
1N/A
1N/A ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
1N/A value, NULL );
1N/A
1N/A if ( html ) {
1N/A /*
1N/A * if we are generating HTML, we add an HREF link that embodies this
1N/A * search action as an LDAP URL, instead of actually doing the search
1N/A * now.
1N/A */
1N/A sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
1N/A if ( base != NULL ) {
1N/A strcat_escaped( buf, base );
1N/A }
1N/A strcat( buf, "??sub?" );
1N/A strcat_escaped( buf, filter );
1N/A sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
1N/A tip->ti_label, eol );
1N/A if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
1N/A return( LDAP_LOCAL_ERROR );
1N/A }
1N/A return( LDAP_SUCCESS );
1N/A }
1N/A
1N/A timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1N/A timeout.tv_usec = 0;
1N/A
1N/A#ifdef CLDAP
1N/A if ( LDAP_IS_CLDAP( ld ))
1N/A lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1N/A 0, &ldmp, NULL );
1N/A else
1N/A#endif /* CLDAP */
1N/A lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter,
1N/A retattrs, 0, &timeout, &ldmp );
1N/A
1N/A if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
1N/A if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
1N/A if (( members = (char **)NSLDAPI_MALLOC( (count + 1)
1N/A * sizeof(char *))) == NULL ) {
1N/A err = LDAP_NO_MEMORY;
1N/A } else {
1N/A for ( i = 0, entry = ldap_first_entry( ld, ldmp );
1N/A entry != NULL;
1N/A entry = ldap_next_entry( ld, entry ), ++i ) {
1N/A members[ i ] = ldap_get_dn( ld, entry );
1N/A }
1N/A members[ i ] = NULL;
1N/A
1N/A ldap_sort_values(ld,members, ldap_sort_strcasecmp);
1N/A
1N/A err = do_vals2text( ld, NULL, members, tip->ti_label,
1N/A html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
1N/A eol, rdncount, urlprefix );
1N/A
1N/A ldap_value_free( members );
1N/A }
1N/A }
1N/A ldap_msgfree( ldmp );
1N/A }
1N/A
1N/A
1N/A if ( vals != NULL ) {
1N/A ldap_value_free( vals );
1N/A }
1N/A
1N/A return(( err == LDAP_SUCCESS ) ? lderr : err );
1N/A}