]> git.sur5r.net Git - openldap/blob - libraries/libldap/tmplout.c
-lldap reentrantancy improvements from Bart Hartgers <A.Hartgers@phys.tue.nl>
[openldap] / libraries / libldap / tmplout.c
1 /*
2  * tmplout.c:  display template library output routines for LDAP clients
3  * 12 April 1994 by Mark C Smith
4  */
5
6 #include "portable.h"
7
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include <ac/ctype.h>
12 #include <ac/string.h>
13 #include <ac/time.h>
14
15 #ifdef HAVE_SYS_FILE_H
16 #include <sys/file.h>
17 #endif
18
19 #include "lber.h"
20 #include "ldap.h"
21
22 #include "ldap_log.h"
23 #include "disptmpl.h"
24
25 #include "ldap-int.h"
26 #include "ldapconfig.h"
27
28 /* local functions */
29 static int do_entry2text LDAP_P((
30         LDAP *ld, char *buf, char *base, LDAPMessage *entry,
31         struct ldap_disptmpl *tmpl, char **defattrs, char ***defvals,
32         writeptype writeproc, void *writeparm, char *eol, int rdncount,
33         unsigned long opts, char *urlprefix ));
34 static int do_entry2text_search LDAP_P((
35         LDAP *ld, char *dn, char *base,
36         LDAPMessage *entry, struct ldap_disptmpl *tmpllist, char **defattrs,
37         char ***defvals, writeptype writeproc, void *writeparm, char *eol,
38         int rdncount, unsigned long opts, char *urlprefix ));
39 static int do_vals2text LDAP_P((
40         LDAP *ld, char *buf, char **vals, char *label,
41         int labelwidth, unsigned long syntaxid, writeptype writeproc,
42         void *writeparm, char *eol, int rdncount, char *urlprefix ));
43 static int max_label_len LDAP_P(( struct ldap_disptmpl *tmpl ));
44 static int output_label LDAP_P((
45         char *buf, char *label, int width,
46         writeptype writeproc, void *writeparm, char *eol, int html ));
47 static int output_dn LDAP_P((
48         char *buf, char *dn, int width, int rdncount,
49         writeptype writeproc, void *writeparm, char *eol, char *urlprefix ));
50 static void strcat_escaped LDAP_P(( char *s1, char *s2 ));
51 static char *time2text LDAP_P(( char *ldtimestr, int dateonly ));
52 static long gtime LDAP_P(( struct tm *tm ));
53 static int searchaction LDAP_P((
54         LDAP *ld, char *buf, char *base, LDAPMessage *entry,
55         char *dn, struct ldap_tmplitem *tip, int labelwidth, int rdncount,
56         writeptype writeproc, void *writeparm, char *eol, char *urlprefix ));
57
58 #define DEF_LABEL_WIDTH         15
59 #define SEARCH_TIMEOUT_SECS     120
60 #define OCATTRNAME              "objectClass"
61
62
63 #define NONFATAL_LDAP_ERR( err )        ( err == LDAP_SUCCESS || \
64         err == LDAP_TIMELIMIT_EXCEEDED || err == LDAP_SIZELIMIT_EXCEEDED )
65
66 #define DEF_LDAP_URL_PREFIX     "ldap:///"
67
68  
69 int
70 ldap_entry2text(
71         LDAP                    *ld,
72         char                    *buf,           /* NULL for "use internal" */
73         LDAPMessage             *entry,
74         struct ldap_disptmpl    *tmpl,
75         char                    **defattrs,
76         char                    ***defvals,
77         writeptype              writeproc,
78         void                    *writeparm,
79         char                    *eol,
80         int                     rdncount,
81         unsigned long           opts
82 )
83 {
84     Debug( LDAP_DEBUG_TRACE, "ldap_entry2text\n", 0, 0, 0 );
85
86     return( do_entry2text( ld, buf, NULL, entry, tmpl, defattrs, defvals,
87                 writeproc, writeparm, eol, rdncount, opts, NULL ));
88
89 }
90
91
92
93 int
94 ldap_entry2html(
95         LDAP                    *ld,
96         char                    *buf,           /* NULL for "use internal" */
97         LDAPMessage             *entry,
98         struct ldap_disptmpl    *tmpl,
99         char                    **defattrs,
100         char                    ***defvals,
101         writeptype              writeproc,
102         void                    *writeparm,
103         char                    *eol,
104         int                     rdncount,
105         unsigned long           opts,
106         char                    *base,
107         char                    *urlprefix
108 )
109 {
110     Debug( LDAP_DEBUG_TRACE, "ldap_entry2html\n", 0, 0, 0 );
111
112     if ( urlprefix == NULL ) {
113         urlprefix = DEF_LDAP_URL_PREFIX;
114     }
115
116     return( do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
117                 writeproc, writeparm, eol, rdncount, opts, urlprefix ));
118 }
119
120
121 static int
122 do_entry2text(
123         LDAP                    *ld,
124         char                    *buf,           /* NULL for use-internal */
125         char                    *base,          /* used for search actions */
126         LDAPMessage             *entry,
127         struct ldap_disptmpl    *tmpl,
128         char                    **defattrs,
129         char                    ***defvals,
130         writeptype              writeproc,
131         void                    *writeparm,
132         char                    *eol,
133         int                     rdncount,
134         unsigned long           opts,
135         char                    *urlprefix      /* if non-NULL, do HTML */
136 )
137 {
138     int                         i, err, html, show, labelwidth;
139     int                         freebuf,  freevals;
140     char                        *dn, **vals;
141     struct ldap_tmplitem        *rowp, *colp;
142
143     if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
144         return( ld->ld_errno );
145     }
146
147     if ( buf == NULL ) {
148         if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
149             ld->ld_errno = LDAP_NO_MEMORY;
150             free( dn );
151             return( ld->ld_errno );
152         }
153         freebuf = 1;
154     } else {
155         freebuf = 0;
156     }
157
158     html = ( urlprefix != NULL );
159
160     if ( html ) {
161         /*
162          * add HTML intro. and title
163          */
164         if (!(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
165             sprintf( buf, "<HTML>%s<HEAD>%s<TITLE>%s%s - ", eol, eol, eol,
166                     ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
167             (*writeproc)( writeparm, buf, strlen( buf ));
168             output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
169             sprintf( buf, "%s</TITLE>%s</HEAD>%s<BODY>%s<H3>%s - ", eol, eol,
170                     eol, eol, ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
171             (*writeproc)( writeparm, buf, strlen( buf ));
172             output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
173             sprintf( buf, "</H3>%s", eol );
174             (*writeproc)( writeparm, buf, strlen( buf ));
175         }
176
177         if (( opts & LDAP_DISP_OPT_NONLEAF ) != 0 &&
178                 ( vals = ldap_explode_dn( dn, 0 )) != NULL ) {
179             char        *untagged;
180
181             /*
182              * add "Move Up" link
183              */
184             sprintf( buf, "<A HREF=\"%s", urlprefix );
185             for ( i = 1; vals[ i ] != NULL; ++i ) {
186                 if ( i > 1 ) {
187                      strcat_escaped( buf, ", " );
188                 }
189                 strcat_escaped( buf, vals[ i ] );
190             }
191             if ( vals[ 1 ] != NULL ) {
192                 untagged = strchr( vals[ 1 ], '=' );
193             } else {
194                 untagged = "=The World";
195             }
196             sprintf( buf + strlen( buf ),
197                     "%s\">Move Up To <EM>%s</EM></A>%s<BR>",
198                     ( vals[ 1 ] == NULL ) ? "??one" : "",
199                     ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol );
200             (*writeproc)( writeparm, buf, strlen( buf ));
201
202             /*
203              * add "Browse" link
204              */
205             untagged = strchr( vals[ 0 ], '=' );
206             sprintf( buf, "<A HREF=\"%s", urlprefix );
207             strcat_escaped( buf, dn );
208             sprintf( buf + strlen( buf ), "??one?(!(objectClass=dsa))\">Browse Below <EM>%s</EM></A>%s%s",
209                     ( untagged != NULL ) ? untagged + 1 : vals[ 0 ], eol, eol );
210             (*writeproc)( writeparm, buf, strlen( buf ));
211
212             ldap_value_free( vals );
213         }
214
215         (*writeproc)( writeparm, "<HR>", 4 );   /* horizontal rule */
216     } else {
217         (*writeproc)( writeparm, "\"", 1 );
218         output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
219         sprintf( buf, "\"%s", eol );
220         (*writeproc)( writeparm, buf, strlen( buf ));
221     }
222
223     if ( tmpl != NULL && ( opts & LDAP_DISP_OPT_AUTOLABELWIDTH ) != 0 ) {
224         labelwidth = max_label_len( tmpl ) + 3;
225     } else {
226         labelwidth = DEF_LABEL_WIDTH;;
227     }
228
229     err = LDAP_SUCCESS;
230
231     if ( tmpl == NULL ) {
232         BerElement      *ber;
233         char            *attr;
234
235         ber = NULL;
236         for ( attr = ldap_first_attribute( ld, entry, &ber );
237                 NONFATAL_LDAP_ERR( err ) && attr != NULL;
238                 attr = ldap_next_attribute( ld, entry, ber )) {
239             if (( vals = ldap_get_values( ld, entry, attr )) == NULL ) {
240                 freevals = 0;
241                 if ( defattrs != NULL ) {
242                     for ( i = 0; defattrs[ i ] != NULL; ++i ) {
243                         if ( strcasecmp( attr, defattrs[ i ] ) == 0 ) {
244                             break;
245                         }
246                     }
247                     if ( defattrs[ i ] != NULL ) {
248                         vals = defvals[ i ];
249                     }
250                 }
251             } else {
252                 freevals = 1;
253             }
254
255                 *attr = TOUPPER( *attr );
256
257             err = do_vals2text( ld, buf, vals, attr, labelwidth,
258                     LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol, 
259                     rdncount, urlprefix );
260             if ( freevals ) {
261                 ldap_value_free( vals );
262             }
263         }
264     } else {
265         for ( rowp = ldap_first_tmplrow( tmpl );
266                 NONFATAL_LDAP_ERR( err ) && rowp != NULLTMPLITEM;
267                 rowp = ldap_next_tmplrow( tmpl, rowp )) {
268             for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
269                     colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
270                 vals = NULL;
271                 if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
272                         entry, colp->ti_attrname )) == NULL ) {
273                     freevals = 0;
274                     if ( !LDAP_IS_TMPLITEM_OPTION_SET( colp,
275                             LDAP_DITEM_OPT_HIDEIFEMPTY ) && defattrs != NULL
276                             && colp->ti_attrname != NULL ) {
277                         for ( i = 0; defattrs[ i ] != NULL; ++i ) {
278                             if ( strcasecmp( colp->ti_attrname, defattrs[ i ] )
279                                     == 0 ) {
280                                 break;
281                             }
282                         }
283                         if ( defattrs[ i ] != NULL ) {
284                             vals = defvals[ i ];
285                         }
286                     }
287                 } else {
288                     freevals = 1;
289                     if ( LDAP_IS_TMPLITEM_OPTION_SET( colp,
290                             LDAP_DITEM_OPT_SORTVALUES ) && vals[ 0 ] != NULL
291                             && vals[ 1 ] != NULL ) {
292                         ldap_sort_values( ld, vals, ldap_sort_strcasecmp );
293                     }
294                 }
295
296                 /*
297                  * don't bother even calling do_vals2text() if no values
298                  * or boolean with value false and "hide if false" option set
299                  */
300                 show = ( vals != NULL && vals[ 0 ] != NULL );
301                 if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
302                         == LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
303                         colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
304                         TOUPPER( vals[ 0 ][ 0 ] ) != 'T' ) {
305                     show = 0;
306                 }
307
308                 if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
309                     if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
310                         if ( colp->ti_attrname == NULL || ( show &&
311                                 TOUPPER( vals[ 0 ][ 0 ] ) == 'T' )) {
312                             err = searchaction( ld, buf, base, entry, dn, colp,
313                                     labelwidth, rdncount, writeproc,
314                                     writeparm, eol, urlprefix );
315                         }
316                     }
317                     show = 0;
318                 }
319
320                 if ( show ) {
321                     err = do_vals2text( ld, buf, vals, colp->ti_label,
322                         labelwidth, colp->ti_syntaxid, writeproc, writeparm,
323                         eol, rdncount, urlprefix );
324                 }
325
326                 if ( freevals ) {
327                     ldap_value_free( vals );
328                 }
329             }
330         }
331     }
332
333     if ( html  && !(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
334         sprintf( buf, "</BODY>%s</HTML>%s", eol, eol );
335         (*writeproc)( writeparm, buf, strlen( buf ));
336     }
337
338     free( dn );
339     if ( freebuf ) {
340         free( buf );
341     }
342
343     return( err );
344 }
345
346         
347 int
348 ldap_entry2text_search(
349         LDAP                    *ld,
350         char                    *dn,            /* if NULL, use entry */
351         char                    *base,          /* if NULL, no search actions */
352         LDAPMessage             *entry,         /* if NULL, use dn */
353         struct ldap_disptmpl*   tmpllist,       /* if NULL, load default file */
354         char                    **defattrs,
355         char                    ***defvals,
356         writeptype              writeproc,
357         void                    *writeparm,
358         char                    *eol,
359         int                     rdncount,       /* if 0, display full DN */
360         unsigned long           opts
361 )
362 {
363     Debug( LDAP_DEBUG_TRACE, "ldap_entry2text_search\n", 0, 0, 0 );
364
365     return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
366             defvals, writeproc, writeparm, eol, rdncount, opts, NULL ));
367 }
368
369
370
371 int
372 ldap_entry2html_search(
373         LDAP                    *ld,
374         char                    *dn,            /* if NULL, use entry */
375         char                    *base,          /* if NULL, no search actions */
376         LDAPMessage             *entry,         /* if NULL, use dn */
377         struct ldap_disptmpl*   tmpllist,       /* if NULL, load default file */
378         char                    **defattrs,
379         char                    ***defvals,
380         writeptype              writeproc,
381         void                    *writeparm,
382         char                    *eol,
383         int                     rdncount,       /* if 0, display full DN */
384         unsigned long           opts,
385         char                    *urlprefix
386 )
387 {
388     Debug( LDAP_DEBUG_TRACE, "ldap_entry2html_search\n", 0, 0, 0 );
389
390     return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
391             defvals, writeproc, writeparm, eol, rdncount, opts, urlprefix ));
392 }
393
394
395 static int
396 do_entry2text_search(
397         LDAP                    *ld,
398         char                    *dn,            /* if NULL, use entry */
399         char                    *base,          /* if NULL, no search actions */
400         LDAPMessage             *entry,         /* if NULL, use dn */
401         struct ldap_disptmpl*   tmpllist,       /* if NULL, load default file */
402         char                    **defattrs,
403         char                    ***defvals,
404         writeptype              writeproc,
405         void                    *writeparm,
406         char                    *eol,
407         int                     rdncount,       /* if 0, display full DN */
408         unsigned long           opts,
409         char                    *urlprefix
410 )
411 {
412     int                         err, freedn, freetmpls, html;
413     char                        *buf, **fetchattrs, **vals;
414     LDAPMessage                 *ldmp;
415     struct ldap_disptmpl        *tmpl;
416     struct timeval              timeout;
417
418     if ( dn == NULL && entry == NULLMSG ) {
419         ld->ld_errno = LDAP_PARAM_ERROR;
420         return( ld->ld_errno );
421     }
422
423     html = ( urlprefix != NULL );
424
425     timeout.tv_sec = SEARCH_TIMEOUT_SECS;
426     timeout.tv_usec = 0;
427
428     if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
429         ld->ld_errno = LDAP_NO_MEMORY;
430         return( ld->ld_errno );
431     }
432
433     freedn = freetmpls = 0;
434     tmpl = NULL;
435
436     if ( tmpllist == NULL ) {
437         if (( err = ldap_init_templates( TEMPLATEFILE, &tmpllist )) != 0 ) {
438             sprintf( buf, "%sUnable to read template file %s (error %d)%s%s",
439                     html ? "<!-- " : "", TEMPLATEFILE, err,
440                     html ? "-->" : "", eol );
441             (*writeproc)( writeparm, buf, strlen( buf ));
442         }
443         freetmpls = 1;
444     }
445
446     if ( dn == NULL ) {
447         if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
448             free( buf );
449             if ( freetmpls ) {
450                 ldap_free_templates( tmpllist );
451             }
452             return( ld->ld_errno );
453         }
454         freedn = 1;
455     }
456
457
458     if ( tmpllist != NULL ) {
459         ldmp = NULLMSG;
460
461         if ( entry == NULL ) {
462             char        *ocattrs[2];
463
464             ocattrs[0] = OCATTRNAME;
465             ocattrs[1] = NULL;
466 #ifdef LDAP_CONNECTIONLESS
467             if ( LDAP_IS_CLDAP( ld ))
468                     err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE,
469                         "objectClass=*", ocattrs, 0, &ldmp, NULL );
470             else
471 #endif /* LDAP_CONNECTIONLESS */
472                     err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
473                             "objectClass=*", ocattrs, 0, &timeout, &ldmp );
474
475             if ( err == LDAP_SUCCESS ) {
476                 entry = ldap_first_entry( ld, ldmp );
477             }
478         }
479
480         if ( entry != NULL ) {
481             vals = ldap_get_values( ld, entry, OCATTRNAME );
482             tmpl = ldap_oc2template( vals, tmpllist );
483             if ( vals != NULL ) {
484                 ldap_value_free( vals );
485             }
486         }
487         if ( ldmp != NULL ) {
488             ldap_msgfree( ldmp );
489         }
490     }
491
492     entry = NULL;
493
494     if ( tmpl == NULL ) {
495         fetchattrs = NULL;
496     } else {
497         fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
498     }
499
500 #ifdef LDAP_CONNECTIONLESS
501     if ( LDAP_IS_CLDAP( ld ))
502         err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
503                 fetchattrs, 0, &ldmp, NULL );
504     else
505 #endif /* LDAP_CONNECTIONLESS */
506         err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
507                 fetchattrs, 0, &timeout, &ldmp );
508
509     if ( freedn ) {
510         free( dn );
511     }
512     if ( fetchattrs != NULL ) {
513         ldap_value_free( fetchattrs );
514     }
515
516     if ( err != LDAP_SUCCESS ||
517             ( entry = ldap_first_entry( ld, ldmp )) == NULL ) {
518         if ( freetmpls ) {
519             ldap_free_templates( tmpllist );
520         }
521         free( buf );
522         return( ld->ld_errno );
523     }
524
525     err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
526             writeproc, writeparm, eol, rdncount, opts, urlprefix );
527
528     free( buf );
529     if ( freetmpls ) {
530         ldap_free_templates( tmpllist );
531     }
532     ldap_msgfree( ldmp );
533     return( err );
534 }
535             
536
537 int
538 ldap_vals2text(
539         LDAP                    *ld,
540         char                    *buf,           /* NULL for "use internal" */
541         char                    **vals,
542         char                    *label,
543         int                     labelwidth,     /* 0 means use default */
544         unsigned long           syntaxid,
545         writeptype              writeproc,
546         void                    *writeparm,
547         char                    *eol,
548         int                     rdncount
549 )
550 {
551     Debug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
552
553     return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
554                 writeproc, writeparm, eol, rdncount, NULL ));
555 }
556
557
558 int
559 ldap_vals2html(
560         LDAP                    *ld,
561         char                    *buf,           /* NULL for "use internal" */
562         char                    **vals,
563         char                    *label,
564         int                     labelwidth,     /* 0 means use default */
565         unsigned long           syntaxid,
566         writeptype              writeproc,
567         void                    *writeparm,
568         char                    *eol,
569         int                     rdncount,
570         char                    *urlprefix
571 )
572 {
573     Debug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
574
575     if ( urlprefix == NULL ) {
576         urlprefix = DEF_LDAP_URL_PREFIX;
577     }
578
579     return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
580                 writeproc, writeparm, eol, rdncount, urlprefix ));
581 }
582
583
584 static int
585 do_vals2text(
586         LDAP                    *ld,
587         char                    *buf,           /* NULL for "use internal" */
588         char                    **vals,
589         char                    *label,
590         int                     labelwidth,     /* 0 means use default */
591         unsigned long           syntaxid,
592         writeptype              writeproc,
593         void                    *writeparm,
594         char                    *eol,
595         int                     rdncount,
596         char                    *urlprefix
597 )
598 {
599     int         i, html, writeoutval, freebuf, notascii;
600     char        *p, *s, *outval;
601
602
603     if ( vals == NULL ) {
604         return( LDAP_SUCCESS );
605     }
606
607     html = ( urlprefix != NULL );
608
609     switch( LDAP_GET_SYN_TYPE( syntaxid )) {
610     case LDAP_SYN_TYPE_TEXT:
611     case LDAP_SYN_TYPE_BOOLEAN:
612         break;          /* we only bother with these two types... */
613     default:
614         return( LDAP_SUCCESS );
615     }
616
617     if ( labelwidth == 0 || labelwidth < 0 ) {
618         labelwidth = DEF_LABEL_WIDTH;
619     }
620
621     if ( buf == NULL ) {
622         if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
623             ld->ld_errno = LDAP_NO_MEMORY;
624             return( ld->ld_errno );
625         }
626         freebuf = 1;
627     } else {
628         freebuf = 0;
629     }
630
631     output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
632
633     for ( i = 0; vals[ i ] != NULL; ++i ) {
634         for ( p = vals[ i ]; *p != '\0'; ++p ) {
635             if ( !isascii( *p )) {
636                 break;
637             }
638         }
639         notascii = ( *p != '\0' );
640         outval = notascii ? "(unable to display non-ASCII text value)"
641                 : vals[ i ];
642
643         writeoutval = 0;        /* if non-zero, write outval after switch */
644
645         switch( syntaxid ) {
646         case LDAP_SYN_CASEIGNORESTR:
647             ++writeoutval;
648             break;
649
650         case LDAP_SYN_RFC822ADDR:
651             if ( html ) {
652                 strcpy( buf, "<DD><A HREF=\"mailto:" );
653                 strcat_escaped( buf, outval );
654                 sprintf( buf + strlen( buf ), "\">%s</A><BR>%s", outval, eol );
655                 (*writeproc)( writeparm, buf, strlen( buf ));
656             } else {
657                 ++writeoutval;
658             }
659             break;
660
661         case LDAP_SYN_DN:       /* for now */
662             output_dn( buf, outval, labelwidth, rdncount, writeproc,
663                     writeparm, eol, urlprefix );
664             break;
665
666         case LDAP_SYN_MULTILINESTR:
667             if ( i > 0 && !html ) {
668                 output_label( buf, label, labelwidth, writeproc,
669                         writeparm, eol, html );
670             }
671
672             p = s = outval;
673             while (( s = strchr( s, '$' )) != NULL ) {
674                 *s++ = '\0';
675                 while ( isspace( *s )) {
676                     ++s;
677                 }
678                 if ( html ) {
679                     sprintf( buf, "<DD>%s<BR>%s", p, eol );
680                 } else {
681                     sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
682                 }
683                 (*writeproc)( writeparm, buf, strlen( buf ));
684                 p = s;
685             }
686             outval = p;
687             ++writeoutval;
688             break;
689
690         case LDAP_SYN_BOOLEAN:
691             outval = TOUPPER( outval[ 0 ] ) == 'T' ? "TRUE" : "FALSE";
692             ++writeoutval;
693             break;
694
695         case LDAP_SYN_TIME:
696         case LDAP_SYN_DATE:
697             outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
698             ++writeoutval;
699             break;
700
701         case LDAP_SYN_LABELEDURL:
702             if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
703                 *p++ = '\0';
704                 while ( isspace( *p )) {
705                     ++p;
706                 }
707                 s = outval;
708             } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
709                 *s++ = '\0';
710                 while ( isspace( *s )) {
711                     ++s;
712                 }
713                 p = outval;
714             } else {
715                 s = "URL";
716                 p = outval;
717             }
718
719             /*
720              * at this point `s' points to the label & `p' to the URL
721              */
722             if ( html ) {
723                 sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
724             } else {
725                 sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
726                     s, eol, labelwidth + 2, " ",p , eol );
727             }
728             (*writeproc)( writeparm, buf, strlen( buf ));
729             break;
730
731         default:
732             sprintf( buf, " Can't display item type %ld%s",
733                     syntaxid, eol );
734             (*writeproc)( writeparm, buf, strlen( buf ));
735         }
736
737         if ( writeoutval ) {
738             if ( html ) {
739                 sprintf( buf, "<DD>%s<BR>%s", outval, eol );
740             } else {
741                 sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
742             }
743             (*writeproc)( writeparm, buf, strlen( buf ));
744         }
745     }
746
747     if ( freebuf ) {
748         free( buf );
749     }
750
751     return( LDAP_SUCCESS );
752 }
753
754
755 static int
756 max_label_len( struct ldap_disptmpl *tmpl )
757 {
758     struct ldap_tmplitem        *rowp, *colp;
759     int                         len, maxlen;
760
761     maxlen = 0;
762
763     for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
764             rowp = ldap_next_tmplrow( tmpl, rowp )) {
765         for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
766                 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
767             if (( len = strlen( colp->ti_label )) > maxlen ) {
768                 maxlen = len;
769             }
770         }
771     }
772
773     return( maxlen );
774 }
775
776
777 static int
778 output_label( char *buf, char *label, int width, writeptype writeproc,
779         void *writeparm, char *eol, int html )
780 {
781     char        *p;
782
783     if ( html ) {
784         sprintf( buf, "<DT><B>%s</B>", label );
785     } else {
786         sprintf( buf, " %s:", label );
787         p = buf + strlen( buf );
788
789         while ( p - buf < width ) {
790             *p++ = ' ';
791         }
792
793         *p = '\0';
794         strcat( buf, eol );
795     }
796
797     return ((*writeproc)( writeparm, buf, strlen( buf )));
798 }
799
800
801 static int
802 output_dn( char *buf, char *dn, int width, int rdncount,
803         writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
804 {
805     char        **dnrdns;
806     int         i;
807
808     if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
809         return( -1 );
810     }
811
812     if ( urlprefix != NULL ) {
813         sprintf( buf, "<DD><A HREF=\"%s", urlprefix );
814         strcat_escaped( buf, dn );
815         strcat( buf, "\">" );
816     } else if ( width > 0 ) {
817         sprintf( buf, "%-*s", width, " " );
818     } else {
819         *buf = '\0';
820     }
821
822     for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
823             ++i ) {
824         if ( i > 0 ) {
825             strcat( buf, ", " );
826         }
827         strcat( buf, dnrdns[ i ] );
828     }
829
830     if ( urlprefix != NULL ) {
831         strcat( buf, "</A><BR>" );
832     }
833
834     ldap_value_free( dnrdns );
835
836     strcat( buf, eol );
837
838     return ((*writeproc)( writeparm, buf, strlen( buf )));
839 }
840
841
842
843 #define HREF_CHAR_ACCEPTABLE( c )       (( c >= '-' && c <= '9' ) ||    \
844                                          ( c >= '@' && c <= 'Z' ) ||    \
845                                          ( c == '_' ) ||                \
846                                          ( c >= 'a' && c <= 'z' ))
847
848 static void
849 strcat_escaped( char *s1, char *s2 )
850 {
851     char        *p, *q;
852     char        *hexdig = "0123456789ABCDEF";
853
854     p = s1 + strlen( s1 );
855     for ( q = s2; *q != '\0'; ++q ) {
856         if ( HREF_CHAR_ACCEPTABLE( *q )) {
857             *p++ = *q;
858         } else {
859             *p++ = '%';
860             *p++ = hexdig[ *q >> 4 ];
861             *p++ = hexdig[ *q & 0x0F ];
862         }
863     }
864
865     *p = '\0';
866 }
867
868
869 #define GET2BYTENUM( p )        (( *p - '0' ) * 10 + ( *(p+1) - '0' ))
870
871 static char *
872 time2text( char *ldtimestr, int dateonly )
873 {
874     struct tm           t;
875     char                *p, *timestr, zone, *fmterr = "badly formatted time";
876     time_t              gmttime;
877     char                timebuf[32];
878
879     memset( (char *)&t, 0, sizeof( struct tm ));
880     if ( (int) strlen( ldtimestr ) < 13 ) {
881         return( fmterr );
882     }
883
884     for ( p = ldtimestr; p - ldtimestr < 12; ++p ) {
885         if ( !isdigit( *p )) {
886             return( fmterr );
887         }
888     }
889
890     p = ldtimestr;
891     t.tm_year = GET2BYTENUM( p ); p += 2;
892     t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
893     t.tm_mday = GET2BYTENUM( p ); p += 2;
894     t.tm_hour = GET2BYTENUM( p ); p += 2;
895     t.tm_min = GET2BYTENUM( p ); p += 2;
896     t.tm_sec = GET2BYTENUM( p ); p += 2;
897
898     if (( zone = *p ) == 'Z' ) {        /* GMT */
899         zone = '\0';    /* no need to indicate on screen, so we make it null */
900     }
901
902     gmttime = gtime( &t );
903
904     timestr = ldap_int_ctime( &gmttime, timebuf );
905    
906     timestr[ strlen( timestr ) - 1 ] = zone;    /* replace trailing newline */
907     if ( dateonly ) {
908         strcpy( timestr + 11, timestr + 20 );
909     }
910
911     return( timestr );
912 }
913
914
915
916 /* gtime.c - inverse gmtime */
917 /* gtime(): the inverse of localtime().
918         This routine was supplied by Mike Accetta at CMU many years ago.
919  */
920
921 static int      dmsize[] = {
922     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
923 };
924
925 #define dysize(y)       \
926         (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
927
928 #define YEAR(y)         ((y) >= 100 ? (y) : (y) + 1900)
929
930 /* \f */
931
932 static long     gtime ( struct tm *tm )
933 {
934     register int    i,
935                     sec,
936                     mins,
937                     hour,
938                     mday,
939                     mon,
940                     year;
941     register long   result;
942
943     if ((sec = tm -> tm_sec) < 0 || sec > 59
944             || (mins = tm -> tm_min) < 0 || mins > 59
945             || (hour = tm -> tm_hour) < 0 || hour > 24
946             || (mday = tm -> tm_mday) < 1 || mday > 31
947             || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
948         return ((long) -1);
949     if (hour == 24) {
950         hour = 0;
951         mday++;
952     }
953     year = YEAR (tm -> tm_year);
954
955     result = 0L;
956     for (i = 1970; i < year; i++)
957         result += dysize (i);
958     if (dysize (year) == 366 && mon >= 3)
959         result++;
960     while (--mon)
961         result += dmsize[mon - 1];
962     result += mday - 1;
963     result = 24 * result + hour;
964     result = 60 * result + mins;
965     result = 60 * result + sec;
966
967     return result;
968 }
969
970 static int
971 searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
972         struct ldap_tmplitem *tip, int labelwidth, int rdncount,
973         writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
974 {
975     int                 err = 0, lderr, i, count, html;
976     char                **vals, **members;
977     char                *value, *filtpattern, *attr, *selectname;
978     char                *retattrs[2], filter[ 256 ];
979     LDAPMessage         *ldmp;
980     struct timeval      timeout;
981
982     html = ( urlprefix != NULL );
983
984     for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
985         ;
986     }
987     if ( i < 3 ) {
988         return( LDAP_PARAM_ERROR );
989     }
990     attr = tip->ti_args[ 0 ];
991     filtpattern = tip->ti_args[ 1 ];
992     retattrs[ 0 ] = tip->ti_args[ 2 ];
993     retattrs[ 1 ] = NULL;
994     selectname = tip->ti_args[ 3 ];
995
996     vals = NULL;
997     if ( attr == NULL ) {
998         value = NULL;
999     } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
1000         return( LDAP_PARAM_ERROR );
1001     } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
1002         value = dn;
1003     } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
1004         value = vals[ 0 ];
1005     } else {
1006         value = NULL;
1007     }
1008
1009     ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
1010             value, NULL );
1011
1012     if ( html ) {
1013         /*
1014          * if we are generating HTML, we add an HREF link that embodies this
1015          * search action as an LDAP URL, instead of actually doing the search
1016          * now.
1017          */
1018         sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
1019         if ( base != NULL ) {
1020             strcat_escaped( buf, base );
1021         }
1022         strcat( buf, "??sub?" );
1023         strcat_escaped( buf, filter );
1024         sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
1025                 tip->ti_label, eol );
1026         if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
1027             return( LDAP_LOCAL_ERROR );
1028         }
1029         return( LDAP_SUCCESS );
1030     }
1031
1032     timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1033     timeout.tv_usec = 0;
1034
1035 #ifdef LDAP_CONNECTIONLESS
1036     if ( LDAP_IS_CLDAP( ld ))
1037         lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1038                 0, &ldmp, NULL );
1039     else
1040 #endif /* LDAP_CONNECTIONLESS */
1041         lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1042                 0, &timeout, &ldmp );
1043
1044     if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
1045         if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
1046             if (( members = (char **)malloc( (count + 1) * sizeof(char *)))
1047                     == NULL ) {
1048                 err = LDAP_NO_MEMORY;
1049             } else {
1050                 for ( i = 0, entry = ldap_first_entry( ld, ldmp );
1051                         entry != NULL;
1052                         entry = ldap_next_entry( ld, entry ), ++i ) {
1053                     members[ i ] = ldap_get_dn( ld, entry );
1054                 }
1055                 members[ i ] = NULL;
1056
1057                 ldap_sort_values( ld, members, ldap_sort_strcasecmp );
1058
1059                 err = do_vals2text( ld, NULL, members, tip->ti_label,
1060                         html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
1061                         eol, rdncount, urlprefix );
1062
1063                 ldap_value_free( members );
1064             }
1065         }
1066         ldap_msgfree( ldmp );
1067     }
1068
1069     
1070     if ( vals != NULL ) {
1071         ldap_value_free( vals );
1072     }
1073
1074     return(( err == LDAP_SUCCESS ) ? lderr : err );
1075 }