Back to index

lightning-sunbird  0.9+nobinonly
tmplout.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * tmplout.c:  display template library output routines for LDAP clients
00040  * 
00041  */
00042 
00043 #include "ldap-int.h"
00044 #include "disptmpl.h"
00045 
00046 #if defined(_WINDOWS) || defined(aix) || defined(SCOOS) || defined(OSF1) || defined(SOLARIS)
00047 #include <time.h> /* for struct tm and ctime */
00048 #endif
00049 
00050 
00051 /* This is totally lame, since it should be coming from time.h, but isn't. */
00052 #if defined(SOLARIS) 
00053 char *ctime_r(const time_t *, char *, int);
00054 #endif
00055 
00056 static int do_entry2text( LDAP *ld, char *buf, char *base, LDAPMessage *entry,
00057        struct ldap_disptmpl *tmpl, char **defattrs, char ***defvals,
00058        writeptype writeproc, void *writeparm, char *eol, int rdncount,
00059        unsigned long opts, char *urlprefix );
00060 static int do_entry2text_search( LDAP *ld, char *dn, char *base,
00061        LDAPMessage *entry, struct ldap_disptmpl *tmpllist, char **defattrs,
00062        char ***defvals, writeptype writeproc, void *writeparm, char *eol,
00063        int rdncount, unsigned long opts, char *urlprefix );
00064 static int do_vals2text( LDAP *ld, char *buf, char **vals, char *label,
00065        int labelwidth, unsigned long syntaxid, writeptype writeproc,
00066        void *writeparm, char *eol, int rdncount, char *urlprefix );
00067 static int max_label_len( struct ldap_disptmpl *tmpl );
00068 static int output_label( char *buf, char *label, int width,
00069        writeptype writeproc, void *writeparm, char *eol, int html );
00070 static int output_dn( char *buf, char *dn, int width, int rdncount,
00071        writeptype writeproc, void *writeparm, char *eol, char *urlprefix );
00072 static void strcat_escaped( char *s1, char *s2 );
00073 static char *time2text( char *ldtimestr, int dateonly );
00074 static long gtime( struct tm *tm );
00075 static int searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry,
00076        char *dn, struct ldap_tmplitem *tip, int labelwidth, int rdncount,
00077        writeptype writeproc, void *writeparm, char *eol, char *urlprefix );
00078 
00079 #define DEF_LABEL_WIDTH            15
00080 #define SEARCH_TIMEOUT_SECS 120
00081 #define OCATTRNAME          "objectClass"
00082 
00083 
00084 #define NONFATAL_LDAP_ERR( err )   ( err == LDAP_SUCCESS || \
00085        err == LDAP_TIMELIMIT_EXCEEDED || err == LDAP_SIZELIMIT_EXCEEDED )
00086 
00087 #define DEF_LDAP_URL_PREFIX "ldap:///"
00088 
00089  
00090 int
00091 LDAP_CALL
00092 ldap_entry2text(
00093        LDAP                 *ld,
00094        char                 *buf,         /* NULL for "use internal" */
00095        LDAPMessage          *entry,
00096        struct ldap_disptmpl *tmpl,
00097        char                 **defattrs,
00098        char                 ***defvals,
00099        writeptype           writeproc,
00100        void                 *writeparm,
00101        char                 *eol,
00102        int                  rdncount,
00103        unsigned long        opts
00104 )
00105 {
00106     LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2text\n", 0, 0, 0 );
00107 
00108     return( do_entry2text( ld, buf, NULL, entry, tmpl, defattrs, defvals,
00109               writeproc, writeparm, eol, rdncount, opts, NULL ));
00110 
00111 }
00112 
00113 
00114 
00115 int
00116 LDAP_CALL
00117 ldap_entry2html(
00118        LDAP                 *ld,
00119        char                 *buf,         /* NULL for "use internal" */
00120        LDAPMessage          *entry,
00121        struct ldap_disptmpl *tmpl,
00122        char                 **defattrs,
00123        char                 ***defvals,
00124        writeptype           writeproc,
00125        void                 *writeparm,
00126        char                 *eol,
00127        int                  rdncount,
00128        unsigned long        opts,
00129        char                 *base,
00130        char                 *urlprefix
00131 )
00132 {
00133     LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2html\n", 0, 0, 0 );
00134 
00135     if ( urlprefix == NULL ) {
00136        urlprefix = DEF_LDAP_URL_PREFIX;
00137     }
00138 
00139     return( do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
00140               writeproc, writeparm, eol, rdncount, opts, urlprefix ));
00141 }
00142 
00143 
00144 static int
00145 do_entry2text(
00146        LDAP                 *ld,
00147        char                 *buf,         /* NULL for use-internal */
00148        char                 *base,        /* used for search actions */
00149        LDAPMessage          *entry,
00150        struct ldap_disptmpl *tmpl,
00151        char                 **defattrs,
00152        char                 ***defvals,
00153        writeptype           writeproc,
00154        void                 *writeparm,
00155        char                 *eol,
00156        int                  rdncount,
00157        unsigned long        opts,
00158        char                 *urlprefix    /* if non-NULL, do HTML */
00159 )
00160 {
00161     int                            i, err, html, show, labelwidth;
00162     int                            freebuf,  freevals;
00163     char                    *dn, **vals;
00164     struct ldap_tmplitem    *rowp, *colp;
00165 
00166     if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
00167        return( LDAP_PARAM_ERROR );
00168     }
00169 
00170     if ( writeproc == NULL ||
00171            !NSLDAPI_VALID_LDAPMESSAGE_ENTRY_POINTER( entry )) {
00172        err = LDAP_PARAM_ERROR;
00173        LDAP_SET_LDERRNO( ld, err, NULL, NULL );
00174        return( err );
00175     }
00176 
00177     if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
00178        return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
00179     }
00180 
00181     if ( buf == NULL ) {
00182        if (( buf = NSLDAPI_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
00183            err = LDAP_NO_MEMORY;
00184            LDAP_SET_LDERRNO( ld, err, NULL, NULL );
00185            NSLDAPI_FREE( dn );
00186            return( err );
00187        }
00188        freebuf = 1;
00189     } else {
00190        freebuf = 0;
00191     }
00192 
00193     html = ( urlprefix != NULL );
00194 
00195     if ( html ) {
00196        /*
00197         * add HTML intro. and title
00198         */
00199        if (!(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
00200            sprintf( buf, "<HTML>%s<HEAD>%s<TITLE>%s%s - ", eol, eol, eol,
00201                   ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
00202            (*writeproc)( writeparm, buf, strlen( buf ));
00203            output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
00204            sprintf( buf, "%s</TITLE>%s</HEAD>%s<BODY>%s<H3>%s - ", eol, eol,
00205                   eol, eol, ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
00206            (*writeproc)( writeparm, buf, strlen( buf ));
00207            output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
00208            sprintf( buf, "</H3>%s", eol );
00209            (*writeproc)( writeparm, buf, strlen( buf ));
00210        }
00211 
00212        if (( opts & LDAP_DISP_OPT_NONLEAF ) != 0 &&
00213               ( vals = ldap_explode_dn( dn, 0 )) != NULL ) {
00214            char      *untagged;
00215 
00216            /*
00217             * add "Move Up" link
00218             */
00219            sprintf( buf, "<A HREF=\"%s", urlprefix );
00220            for ( i = 1; vals[ i ] != NULL; ++i ) {
00221               if ( i > 1 ) {
00222                    strcat_escaped( buf, ", " );
00223               }
00224               strcat_escaped( buf, vals[ i ] );
00225            }
00226            if ( vals[ 1 ] != NULL ) {
00227               untagged = strchr( vals[ 1 ], '=' );
00228            } else {
00229               untagged = "=The World";
00230            }
00231            sprintf( buf + strlen( buf ),
00232                   "%s\">Move Up To <EM>%s</EM></A>%s<BR>",
00233                   ( vals[ 1 ] == NULL ) ? "??one" : "",
00234                   ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol );
00235            (*writeproc)( writeparm, buf, strlen( buf ));
00236 
00237            /*
00238             * add "Browse" link
00239             */
00240            untagged = strchr( vals[ 0 ], '=' );
00241            sprintf( buf, "<A HREF=\"%s", urlprefix );
00242            strcat_escaped( buf, dn );
00243            sprintf( buf + strlen( buf ), "??one?(!(objectClass=dsa))\">Browse Below <EM>%s</EM></A>%s%s",
00244                   ( untagged != NULL ) ? untagged + 1 : vals[ 0 ], eol, eol );
00245            (*writeproc)( writeparm, buf, strlen( buf ));
00246 
00247            ldap_value_free( vals );
00248        }
00249 
00250        (*writeproc)( writeparm, "<HR>", 4 );     /* horizontal rule */
00251     } else {
00252        (*writeproc)( writeparm, "\"", 1 );
00253        output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
00254        sprintf( buf, "\"%s", eol );
00255        (*writeproc)( writeparm, buf, strlen( buf ));
00256     }
00257 
00258     if ( tmpl != NULL && ( opts & LDAP_DISP_OPT_AUTOLABELWIDTH ) != 0 ) {
00259        labelwidth = max_label_len( tmpl ) + 3;
00260     } else {
00261        labelwidth = DEF_LABEL_WIDTH;;
00262     }
00263 
00264     err = LDAP_SUCCESS;
00265 
00266     if ( tmpl == NULL ) {
00267        BerElement    *ber;
00268        char          *attr;
00269 
00270        ber = NULL;
00271        for ( attr = ldap_first_attribute( ld, entry, &ber );
00272               NONFATAL_LDAP_ERR( err ) && attr != NULL;
00273               attr = ldap_next_attribute( ld, entry, ber )) {
00274            if (( vals = ldap_get_values( ld, entry, attr )) == NULL ) {
00275               freevals = 0;
00276               if ( defattrs != NULL ) {
00277                   for ( i = 0; defattrs[ i ] != NULL; ++i ) {
00278                      if ( strcasecmp( attr, defattrs[ i ] ) == 0 ) {
00279                          break;
00280                      }
00281                   }
00282                   if ( defattrs[ i ] != NULL ) {
00283                      vals = defvals[ i ];
00284                   }
00285               }
00286            } else {
00287               freevals = 1;
00288            }
00289 
00290            if ( islower( *attr )) {       /* cosmetic -- upcase attr. name */
00291               *attr = toupper( *attr );
00292            }
00293 
00294            err = do_vals2text( ld, buf, vals, attr, labelwidth,
00295                   LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol, 
00296                   rdncount, urlprefix );
00297            if ( freevals ) {
00298               ldap_value_free( vals );
00299            }
00300        }
00301        if ( ber == NULL ) {
00302            ber_free( ber, 0 );
00303        }
00304        /*
00305         * XXX check for errors in ldap_first_attribute/ldap_next_attribute
00306         * here (but what should we do if there was one?)
00307         */
00308 
00309     } else {
00310        for ( rowp = ldap_first_tmplrow( tmpl );
00311               NONFATAL_LDAP_ERR( err ) && rowp != NULLTMPLITEM;
00312               rowp = ldap_next_tmplrow( tmpl, rowp )) {
00313            for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
00314                   colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
00315               vals = NULL;
00316               if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
00317                      entry, colp->ti_attrname )) == NULL ) {
00318                   freevals = 0;
00319                   if ( !LDAP_IS_TMPLITEM_OPTION_SET( colp,
00320                          LDAP_DITEM_OPT_HIDEIFEMPTY ) && defattrs != NULL
00321                          && colp->ti_attrname != NULL ) {
00322                      for ( i = 0; defattrs[ i ] != NULL; ++i ) {
00323                          if ( strcasecmp( colp->ti_attrname, defattrs[ i ] )
00324                                 == 0 ) {
00325                             break;
00326                          }
00327                      }
00328                      if ( defattrs[ i ] != NULL ) {
00329                          vals = defvals[ i ];
00330                      }
00331                   }
00332               } else {
00333                   freevals = 1;
00334                   if ( LDAP_IS_TMPLITEM_OPTION_SET( colp,
00335                          LDAP_DITEM_OPT_SORTVALUES ) && vals[ 0 ] != NULL
00336                          && vals[ 1 ] != NULL ) {
00337                      ldap_sort_values(ld, vals, ldap_sort_strcasecmp);
00338                   }
00339               }
00340 
00341               /*
00342                * don't bother even calling do_vals2text() if no values
00343                * or boolean with value false and "hide if false" option set
00344                */
00345               show = ( vals != NULL && vals[ 0 ] != NULL );
00346               if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
00347                      == LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
00348                      colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
00349                      toupper( vals[ 0 ][ 0 ] ) != 'T' ) {
00350                   show = 0;
00351               }
00352 
00353               if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
00354                   if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
00355                      if ( colp->ti_attrname == NULL || ( show &&
00356                             toupper( vals[ 0 ][ 0 ] ) == 'T' )) {
00357                          err = searchaction( ld, buf, base, entry, dn, colp,
00358                                 labelwidth, rdncount, writeproc,
00359                                 writeparm, eol, urlprefix );
00360                      }
00361                   }
00362                   show = 0;
00363               }
00364 
00365               if ( show ) {
00366                   err = do_vals2text( ld, buf, vals, colp->ti_label,
00367                      labelwidth, colp->ti_syntaxid, writeproc, writeparm,
00368                      eol, rdncount, urlprefix );
00369               }
00370 
00371               if ( freevals ) {
00372                   ldap_value_free( vals );
00373               }
00374            }
00375        }
00376     }
00377 
00378     if ( html  && !(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
00379        sprintf( buf, "</BODY>%s</HTML>%s", eol, eol );
00380        (*writeproc)( writeparm, buf, strlen( buf ));
00381     }
00382 
00383     NSLDAPI_FREE( dn );
00384     if ( freebuf ) {
00385        NSLDAPI_FREE( buf );
00386     }
00387 
00388     return( err );
00389 }
00390 
00391        
00392 int
00393 LDAP_CALL
00394 ldap_entry2text_search(
00395        LDAP                 *ld,
00396        char                 *dn,          /* if NULL, use entry */
00397        char                 *base,        /* if NULL, no search actions */
00398        LDAPMessage          *entry,              /* if NULL, use dn */
00399        struct ldap_disptmpl*       tmpllist,     /* if NULL, load default file */
00400        char                 **defattrs,
00401        char                 ***defvals,
00402        writeptype           writeproc,
00403        void                 *writeparm,
00404        char                 *eol,
00405        int                  rdncount,     /* if 0, display full DN */
00406        unsigned long        opts
00407 )
00408 {
00409     LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2text_search\n", 0, 0, 0 );
00410 
00411     return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
00412            defvals, writeproc, writeparm, eol, rdncount, opts, NULL ));
00413 }
00414 
00415 
00416 
00417 int
00418 LDAP_CALL
00419 ldap_entry2html_search(
00420        LDAP                 *ld,
00421        char                 *dn,          /* if NULL, use entry */
00422        char                 *base,        /* if NULL, no search actions */
00423        LDAPMessage          *entry,              /* if NULL, use dn */
00424        struct ldap_disptmpl*       tmpllist,     /* if NULL, load default file */
00425        char                 **defattrs,
00426        char                 ***defvals,
00427        writeptype           writeproc,
00428        void                 *writeparm,
00429        char                 *eol,
00430        int                  rdncount,     /* if 0, display full DN */
00431        unsigned long        opts,
00432        char                 *urlprefix
00433 )
00434 {
00435     LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2html_search\n", 0, 0, 0 );
00436 
00437     return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
00438            defvals, writeproc, writeparm, eol, rdncount, opts, urlprefix ));
00439 }
00440 
00441 
00442 static int
00443 do_entry2text_search(
00444        LDAP                 *ld,
00445        char                 *dn,          /* if NULL, use entry */
00446        char                 *base,        /* if NULL, no search actions */
00447        LDAPMessage          *entry,              /* if NULL, use dn */
00448        struct ldap_disptmpl*       tmpllist,     /* if NULL, no template used */
00449        char                 **defattrs,
00450        char                 ***defvals,
00451        writeptype           writeproc,
00452        void                 *writeparm,
00453        char                 *eol,
00454        int                  rdncount,     /* if 0, display full DN */
00455        unsigned long        opts,
00456        char                 *urlprefix
00457 )
00458 {
00459     int                            err, freedn, html;
00460     char                    *buf, **fetchattrs, **vals;
00461     LDAPMessage                    *ldmp;
00462     struct ldap_disptmpl    *tmpl;
00463     struct timeval          timeout;
00464 
00465     if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
00466        return( LDAP_PARAM_ERROR );
00467     }
00468 
00469     if ( dn == NULL && entry == NULLMSG ) {
00470        err = LDAP_PARAM_ERROR;
00471        LDAP_SET_LDERRNO( ld, err, NULL, NULL );
00472        return( err );
00473     }
00474 
00475     html = ( urlprefix != NULL );
00476 
00477     timeout.tv_sec = SEARCH_TIMEOUT_SECS;
00478     timeout.tv_usec = 0;
00479 
00480     if (( buf = NSLDAPI_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
00481        err = LDAP_NO_MEMORY;
00482        LDAP_SET_LDERRNO( ld, err, NULL, NULL );
00483        return( err );
00484     }
00485 
00486     freedn = 0;
00487     tmpl = NULL;
00488 
00489     if ( dn == NULL ) {
00490        if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
00491            NSLDAPI_FREE( buf );
00492            return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
00493        }
00494        freedn = 1;
00495     }
00496 
00497 
00498     if ( tmpllist != NULL ) {
00499        ldmp = NULLMSG;
00500 
00501        if ( entry == NULL ) {
00502            char      *ocattrs[2];
00503 
00504            ocattrs[0] = OCATTRNAME;
00505            ocattrs[1] = NULL;
00506 #ifdef CLDAP
00507            if ( LDAP_IS_CLDAP( ld ))
00508                   err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE,
00509                      "objectClass=*", ocattrs, 0, &ldmp, NULL );
00510            else
00511 #endif /* CLDAP */
00512                   err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
00513                          "objectClass=*", ocattrs, 0, &timeout, &ldmp );
00514 
00515            if ( err == LDAP_SUCCESS ) {
00516               entry = ldap_first_entry( ld, ldmp );
00517            }
00518        }
00519 
00520        if ( entry != NULL ) {
00521            vals = ldap_get_values( ld, entry, OCATTRNAME );
00522            tmpl = ldap_oc2template( vals, tmpllist );
00523            if ( vals != NULL ) {
00524               ldap_value_free( vals );
00525            }
00526        }
00527        if ( ldmp != NULL ) {
00528            ldap_msgfree( ldmp );
00529        }
00530     }
00531 
00532     entry = NULL;
00533 
00534     if ( tmpl == NULL ) {
00535        fetchattrs = NULL;
00536     } else {
00537        fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
00538     }
00539 
00540 #ifdef CLDAP
00541     if ( LDAP_IS_CLDAP( ld ))
00542        err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
00543               fetchattrs, 0, &ldmp, NULL );
00544     else
00545 #endif /* CLDAP */
00546        err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
00547               fetchattrs, 0, &timeout, &ldmp );
00548 
00549     if ( freedn ) {
00550        NSLDAPI_FREE( dn );
00551     }
00552     if ( fetchattrs != NULL ) {
00553        ldap_value_free( fetchattrs );
00554     }
00555 
00556     if ( err != LDAP_SUCCESS ||
00557            ( entry = ldap_first_entry( ld, ldmp )) == NULL ) {
00558        NSLDAPI_FREE( buf );
00559        return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
00560     }
00561 
00562     err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
00563            writeproc, writeparm, eol, rdncount, opts, urlprefix );
00564 
00565     NSLDAPI_FREE( buf );
00566     ldap_msgfree( ldmp );
00567     return( err );
00568 }
00569            
00570 
00571 int
00572 LDAP_CALL
00573 ldap_vals2text(
00574        LDAP                 *ld,
00575        char                 *buf,         /* NULL for "use internal" */
00576        char                 **vals,
00577        char                 *label,
00578        int                  labelwidth,   /* 0 means use default */
00579        unsigned long        syntaxid,
00580        writeptype           writeproc,
00581        void                 *writeparm,
00582        char                 *eol,
00583        int                  rdncount
00584 )
00585 {
00586     LDAPDebug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
00587 
00588     return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
00589               writeproc, writeparm, eol, rdncount, NULL ));
00590 }
00591 
00592 
00593 int
00594 LDAP_CALL
00595 ldap_vals2html(
00596        LDAP                 *ld,
00597        char                 *buf,         /* NULL for "use internal" */
00598        char                 **vals,
00599        char                 *label,
00600        int                  labelwidth,   /* 0 means use default */
00601        unsigned long        syntaxid,
00602        writeptype           writeproc,
00603        void                 *writeparm,
00604        char                 *eol,
00605        int                  rdncount,
00606        char                 *urlprefix
00607 )
00608 {
00609     LDAPDebug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
00610 
00611     if ( urlprefix == NULL ) {
00612        urlprefix = DEF_LDAP_URL_PREFIX;
00613     }
00614 
00615     return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
00616               writeproc, writeparm, eol, rdncount, urlprefix ));
00617 }
00618 
00619 
00620 static int
00621 do_vals2text(
00622        LDAP                 *ld,
00623        char                 *buf,         /* NULL for "use internal" */
00624        char                 **vals,
00625        char                 *label,
00626        int                  labelwidth,   /* 0 means use default */
00627        unsigned long        syntaxid,
00628        writeptype           writeproc,
00629        void                 *writeparm,
00630        char                 *eol,
00631        int                  rdncount,
00632        char                 *urlprefix
00633 )
00634 {
00635     int              err, i, html, writeoutval, freebuf, notascii;
00636     char      *p, *s, *outval;
00637 
00638     if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || writeproc == NULL ) {
00639        return( LDAP_PARAM_ERROR );
00640     }
00641 
00642     if ( vals == NULL ) {
00643        return( LDAP_SUCCESS );
00644     }
00645 
00646     html = ( urlprefix != NULL );
00647 
00648     switch( LDAP_GET_SYN_TYPE( syntaxid )) {
00649     case LDAP_SYN_TYPE_TEXT:
00650     case LDAP_SYN_TYPE_BOOLEAN:
00651        break;        /* we only bother with these two types... */
00652     default:
00653        return( LDAP_SUCCESS );
00654     }
00655 
00656     if ( labelwidth == 0 || labelwidth < 0 ) {
00657        labelwidth = DEF_LABEL_WIDTH;
00658     }
00659 
00660     if ( buf == NULL ) {
00661        if (( buf = NSLDAPI_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
00662            err = LDAP_NO_MEMORY;
00663            LDAP_SET_LDERRNO( ld, err, NULL, NULL );
00664            return( err );
00665        }
00666        freebuf = 1;
00667     } else {
00668        freebuf = 0;
00669     }
00670 
00671     output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
00672 
00673     for ( i = 0; vals[ i ] != NULL; ++i ) {
00674        for ( p = vals[ i ]; *p != '\0'; ++p ) {
00675            if ( !isascii( *p )) {
00676               break;
00677            }
00678        }
00679        notascii = ( *p != '\0' );
00680        outval = notascii ? "(unable to display non-ASCII text value)"
00681               : vals[ i ];
00682 
00683        writeoutval = 0;     /* if non-zero, write outval after switch */
00684 
00685        switch( syntaxid ) {
00686        case LDAP_SYN_CASEIGNORESTR:
00687            ++writeoutval;
00688            break;
00689 
00690        case LDAP_SYN_RFC822ADDR:
00691            if ( html ) {
00692               strcpy( buf, "<DD><A HREF=\"mailto:" );
00693               strcat_escaped( buf, outval );
00694               sprintf( buf + strlen( buf ), "\">%s</A><BR>%s", outval, eol );
00695               (*writeproc)( writeparm, buf, strlen( buf ));
00696            } else {
00697               ++writeoutval;
00698            }
00699            break;
00700 
00701        case LDAP_SYN_DN:    /* for now */
00702            output_dn( buf, outval, labelwidth, rdncount, writeproc,
00703                   writeparm, eol, urlprefix );
00704            break;
00705 
00706        case LDAP_SYN_MULTILINESTR:
00707            if ( i > 0 && !html ) {
00708               output_label( buf, label, labelwidth, writeproc,
00709                      writeparm, eol, html );
00710            }
00711 
00712            p = s = outval;
00713            while (( s = strchr( s, '$' )) != NULL ) {
00714               *s++ = '\0';
00715               while ( ldap_utf8isspace( s )) {
00716                   ++s;
00717               }
00718               if ( html ) {
00719                   sprintf( buf, "<DD>%s<BR>%s", p, eol );
00720               } else {
00721                   sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
00722               }
00723               (*writeproc)( writeparm, buf, strlen( buf ));
00724               p = s;
00725            }
00726            outval = p;
00727            ++writeoutval;
00728            break;
00729 
00730        case LDAP_SYN_BOOLEAN:
00731            outval = toupper( outval[ 0 ] ) == 'T' ? "TRUE" : "FALSE";
00732            ++writeoutval;
00733            break;
00734 
00735        case LDAP_SYN_TIME:
00736        case LDAP_SYN_DATE:
00737            outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
00738            ++writeoutval;
00739            break;
00740 
00741        case LDAP_SYN_LABELEDURL:
00742            if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
00743               *p++ = '\0';
00744               while ( ldap_utf8isspace( p )) {
00745                   ++p;
00746               }
00747               s = outval;
00748            } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
00749               *s++ = '\0';
00750               while ( ldap_utf8isspace( s )) {
00751                   ++s;
00752               }
00753               p = outval;
00754            } else {
00755               s = "URL";
00756               p = outval;
00757            }
00758 
00759            /*
00760             * at this point `s' points to the label & `p' to the URL
00761             */
00762            if ( html ) {
00763               sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
00764            } else {
00765               sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
00766                   s, eol, labelwidth + 2, " ",p , eol );
00767            }
00768            (*writeproc)( writeparm, buf, strlen( buf ));
00769            break;
00770 
00771        default:
00772            sprintf( buf, " Can't display item type %ld%s",
00773                   syntaxid, eol );
00774            (*writeproc)( writeparm, buf, strlen( buf ));
00775        }
00776 
00777        if ( writeoutval ) {
00778            if ( html ) {
00779               sprintf( buf, "<DD>%s<BR>%s", outval, eol );
00780            } else {
00781               sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
00782            }
00783            (*writeproc)( writeparm, buf, strlen( buf ));
00784        }
00785     }
00786 
00787     if ( freebuf ) {
00788        NSLDAPI_FREE( buf );
00789     }
00790 
00791     return( LDAP_SUCCESS );
00792 }
00793 
00794 
00795 static int
00796 max_label_len( struct ldap_disptmpl *tmpl )
00797 {
00798     struct ldap_tmplitem    *rowp, *colp;
00799     int                            len, maxlen;
00800 
00801     maxlen = 0;
00802 
00803     for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
00804            rowp = ldap_next_tmplrow( tmpl, rowp )) {
00805        for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
00806               colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
00807            if (( len = strlen( colp->ti_label )) > maxlen ) {
00808               maxlen = len;
00809            }
00810        }
00811     }
00812 
00813     return( maxlen );
00814 }
00815 
00816 
00817 static int
00818 output_label( char *buf, char *label, int width, writeptype writeproc,
00819        void *writeparm, char *eol, int html )
00820 {
00821     char      *p;
00822 
00823     if ( html ) {
00824        sprintf( buf, "<DT><B>%s</B>", label );
00825     } else {
00826        auto size_t w;
00827        sprintf( buf, " %s:", label );
00828        p = buf + strlen( buf );
00829 
00830        for (w = ldap_utf8characters(buf); w < (size_t)width; ++w) {
00831            *p++ = ' ';
00832        }
00833 
00834        *p = '\0';
00835        strcat( buf, eol );
00836     }
00837 
00838     return ((*writeproc)( writeparm, buf, strlen( buf )));
00839 }
00840 
00841 
00842 static int
00843 output_dn( char *buf, char *dn, int width, int rdncount,
00844        writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
00845 {
00846     char      **dnrdns;
00847     int              i;
00848 
00849     if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
00850        return( -1 );
00851     }
00852 
00853     if ( urlprefix != NULL ) {
00854        sprintf( buf, "<DD><A HREF=\"%s", urlprefix );
00855        strcat_escaped( buf, dn );
00856        strcat( buf, "\">" );
00857     } else if ( width > 0 ) {
00858        sprintf( buf, "%-*s", width, " " );
00859     } else {
00860        *buf = '\0';
00861     }
00862 
00863     for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
00864            ++i ) {
00865        if ( i > 0 ) {
00866            strcat( buf, ", " );
00867        }
00868        strcat( buf, dnrdns[ i ] );
00869     }
00870 
00871     if ( urlprefix != NULL ) {
00872        strcat( buf, "</A><BR>" );
00873     }
00874 
00875     ldap_value_free( dnrdns );
00876 
00877     strcat( buf, eol );
00878 
00879     return ((*writeproc)( writeparm, buf, strlen( buf )));
00880 }
00881 
00882 
00883 
00884 #define HREF_CHAR_ACCEPTABLE( c )  (( c >= '-' && c <= '9' ) ||       \
00885                                     ( c >= '@' && c <= 'Z' ) ||       \
00886                                     ( c == '_' ) ||            \
00887                                     ( c >= 'a' && c <= 'z' ))
00888 
00889 static void
00890 strcat_escaped( char *s1, char *s2 )
00891 {
00892     char      *p, *q;
00893     char      *hexdig = "0123456789ABCDEF";
00894 
00895     p = s1 + strlen( s1 );
00896     for ( q = s2; *q != '\0'; ++q ) {
00897        if ( HREF_CHAR_ACCEPTABLE( *q )) {
00898            *p++ = *q;
00899        } else {
00900            *p++ = '%';
00901            *p++ = hexdig[ 0x0F & ((*(unsigned char*)q) >> 4) ];
00902            *p++ = hexdig[ 0x0F & *q ];
00903        }
00904     }
00905 
00906     *p = '\0';
00907 }
00908 
00909 
00910 #define GET2BYTENUM( p )    (( *p - '0' ) * 10 + ( *(p+1) - '0' ))
00911 
00912 static char *
00913 time2text( char *ldtimestr, int dateonly )
00914 {
00915     int                     len;
00916     struct tm        t;
00917     char             *p, *timestr, zone, *fmterr = "badly formatted time";
00918     time_t           gmttime;
00919 /* CTIME for this platform doesn't use this. */
00920 #if !defined(SUNOS4) && !defined(BSDI) && !defined(LINUX1_2) && \
00921     !defined(SNI) && !defined(_WIN32) && !defined(macintosh) && !defined(LINUX)
00922     char             buf[26];
00923 #endif
00924 
00925     memset( (char *)&t, 0, sizeof( struct tm ));
00926     if (( len = (int)strlen( ldtimestr )) < 13 ) {
00927        return( fmterr );
00928     }
00929     if ( len > 15 ) {   /* throw away excess from 4-digit year time string */
00930        len = 15;
00931     } else if ( len == 14 ) {
00932        len = 13;       /* assume we have a time w/2-digit year (len=13) */
00933     }
00934 
00935     for ( p = ldtimestr; p - ldtimestr + 1 < len; ++p ) {
00936        if ( !isdigit( *p )) {
00937            return( fmterr );
00938        }
00939     }
00940 
00941     p = ldtimestr;
00942     t.tm_year = GET2BYTENUM( p ); p += 2;
00943     if ( len == 15 ) {
00944        t.tm_year = 100 * (t.tm_year - 19);
00945        t.tm_year += GET2BYTENUM( p ); p += 2;
00946     }
00947     else {
00948        /* 2 digit years...assumed to be in the range (19)70 through
00949           (20)69 ...less than 70 (for now, 38) means 20xx */
00950        if(t.tm_year < 70) {
00951            t.tm_year += 100;
00952        }
00953     }
00954     t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
00955     t.tm_mday = GET2BYTENUM( p ); p += 2;
00956     t.tm_hour = GET2BYTENUM( p ); p += 2;
00957     t.tm_min = GET2BYTENUM( p ); p += 2;
00958     t.tm_sec = GET2BYTENUM( p ); p += 2;
00959 
00960     if (( zone = *p ) == 'Z' ) {   /* GMT */
00961        zone = '\0';  /* no need to indicate on screen, so we make it null */
00962     }
00963 
00964     gmttime = gtime( &t );
00965     timestr = NSLDAPI_CTIME( &gmttime, buf, sizeof(buf) );
00966 
00967     timestr[ strlen( timestr ) - 1 ] = zone;     /* replace trailing newline */
00968     if ( dateonly ) {
00969        strcpy( timestr + 11, timestr + 20 );
00970     }
00971 
00972     return( timestr );
00973 }
00974 
00975 
00976 
00977 /* gtime.c - inverse gmtime */
00978 
00979 #if !defined( macintosh ) && !defined( _WINDOWS ) && !defined( DOS ) && !defined(XP_OS2)
00980 #include <sys/time.h>
00981 #endif /* !macintosh */
00982 
00983 /* gtime(): the inverse of localtime().
00984        This routine was supplied by Mike Accetta at CMU many years ago.
00985  */
00986 
00987 static int    dmsize[] = {
00988     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
00989 };
00990 
00991 #define       dysize(y)     \
00992        (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
00993 
00994 /*
00995 #define       YEAR(y)              ((y) >= 100 ? (y) : (y) + 1900)
00996 */
00997 #define YEAR(y)             (((y) < 1900) ? ((y) + 1900) : (y)) 
00998 
00999 /*  */
01000 
01001 static long   gtime ( struct tm *tm )
01002 {
01003     register int    i,
01004                     sec,
01005                     mins,
01006                     hour,
01007                     mday,
01008                     mon,
01009                     year;
01010     register long   result;
01011 
01012     if ((sec = tm -> tm_sec) < 0 || sec > 59
01013            || (mins = tm -> tm_min) < 0 || mins > 59
01014            || (hour = tm -> tm_hour) < 0 || hour > 24
01015            || (mday = tm -> tm_mday) < 1 || mday > 31
01016            || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
01017        return ((long) -1);
01018     if (hour == 24) {
01019        hour = 0;
01020        mday++;
01021     }
01022     year = YEAR (tm -> tm_year);
01023 
01024     result = 0L;
01025     for (i = 1970; i < year; i++)
01026        result += dysize (i);
01027     if (dysize (year) == 366 && mon >= 3)
01028        result++;
01029     while (--mon)
01030        result += dmsize[mon - 1];
01031     result += mday - 1;
01032     result = 24 * result + hour;
01033     result = 60 * result + mins;
01034     result = 60 * result + sec;
01035 
01036     return result;
01037 }
01038 
01039 static int
01040 searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
01041        struct ldap_tmplitem *tip, int labelwidth, int rdncount,
01042        writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
01043 {
01044     int                     err = LDAP_SUCCESS, lderr, i, count, html;
01045     char             **vals, **members;
01046     char             *value, *filtpattern, *attr, *selectname;
01047     char             *retattrs[2], filter[ 256 ];
01048     LDAPMessage             *ldmp;
01049     struct timeval   timeout;
01050 
01051     html = ( urlprefix != NULL );
01052 
01053     for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
01054        ;
01055     }
01056     if ( i < 3 ) {
01057        return( LDAP_PARAM_ERROR );
01058     }
01059     attr = tip->ti_args[ 0 ];
01060     filtpattern = tip->ti_args[ 1 ];
01061     retattrs[ 0 ] = tip->ti_args[ 2 ];
01062     retattrs[ 1 ] = NULL;
01063     selectname = tip->ti_args[ 3 ];
01064 
01065     vals = NULL;
01066     if ( attr == NULL ) {
01067        value = NULL;
01068     } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
01069        return( LDAP_PARAM_ERROR );
01070     } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
01071        value = dn;
01072     } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
01073        value = vals[ 0 ];
01074     } else {
01075        value = NULL;
01076     }
01077 
01078     ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
01079            value, NULL );
01080 
01081     if ( html ) {
01082        /*
01083         * if we are generating HTML, we add an HREF link that embodies this
01084         * search action as an LDAP URL, instead of actually doing the search
01085         * now.
01086         */
01087        sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
01088        if ( base != NULL ) {
01089            strcat_escaped( buf, base );
01090        }
01091        strcat( buf, "??sub?" );
01092        strcat_escaped( buf, filter );
01093        sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
01094               tip->ti_label, eol );
01095        if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
01096            return( LDAP_LOCAL_ERROR );
01097        }
01098        return( LDAP_SUCCESS );
01099     }
01100 
01101     timeout.tv_sec = SEARCH_TIMEOUT_SECS;
01102     timeout.tv_usec = 0;
01103 
01104 #ifdef CLDAP
01105     if ( LDAP_IS_CLDAP( ld ))
01106        lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
01107               0, &ldmp, NULL );
01108     else
01109 #endif /* CLDAP */
01110        lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter,
01111               retattrs, 0, &timeout, &ldmp );
01112 
01113     if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
01114        if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
01115            if (( members = (char **)NSLDAPI_MALLOC( (count + 1)
01116                   * sizeof(char *))) == NULL ) {
01117               err = LDAP_NO_MEMORY;
01118            } else {
01119               for ( i = 0, entry = ldap_first_entry( ld, ldmp );
01120                      entry != NULL;
01121                      entry = ldap_next_entry( ld, entry ), ++i ) {
01122                   members[ i ] = ldap_get_dn( ld, entry );
01123               }
01124               members[ i ] = NULL;
01125 
01126               ldap_sort_values(ld,members, ldap_sort_strcasecmp);
01127 
01128               err = do_vals2text( ld, NULL, members, tip->ti_label,
01129                      html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
01130                      eol, rdncount, urlprefix );
01131 
01132               ldap_value_free( members );
01133            }
01134        }
01135        ldap_msgfree( ldmp );
01136     }
01137 
01138     
01139     if ( vals != NULL ) {
01140        ldap_value_free( vals );
01141     }
01142 
01143     return(( err == LDAP_SUCCESS ) ? lderr : err );
01144 }