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