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