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