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