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