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