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