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