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