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