2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * tmplout.c: display template library output routines for LDAP clients
7 * 12 April 1994 by Mark C Smith
16 #include <ac/socket.h>
17 #include <ac/string.h>
20 #ifdef HAVE_SYS_FILE_H
28 #include "ldapconfig.h"
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 ));
60 #define DEF_LABEL_WIDTH 15
61 #define SEARCH_TIMEOUT_SECS 120
62 #define OCATTRNAME "objectClass"
65 #define NONFATAL_LDAP_ERR( err ) ( err == LDAP_SUCCESS || \
66 err == LDAP_TIMELIMIT_EXCEEDED || err == LDAP_SIZELIMIT_EXCEEDED )
68 #define DEF_LDAP_URL_PREFIX "ldap:///"
74 char *buf, /* NULL for "use internal" */
76 struct ldap_disptmpl *tmpl,
86 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text\n", 0, 0, 0 );
88 return( do_entry2text( ld, buf, NULL, entry, tmpl, defattrs, defvals,
89 writeproc, writeparm, eol, rdncount, opts, NULL ));
98 char *buf, /* NULL for "use internal" */
100 struct ldap_disptmpl *tmpl,
103 writeptype writeproc,
112 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html\n", 0, 0, 0 );
114 if ( urlprefix == NULL ) {
115 urlprefix = DEF_LDAP_URL_PREFIX;
118 return( do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
119 writeproc, writeparm, eol, rdncount, opts, urlprefix ));
126 char *buf, /* NULL for use-internal */
127 char *base, /* used for search actions */
129 struct ldap_disptmpl *tmpl,
132 writeptype writeproc,
137 char *urlprefix /* if non-NULL, do HTML */
140 int i, err, html, show, labelwidth;
141 int freebuf, freevals;
143 struct ldap_tmplitem *rowp, *colp;
145 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
146 return( ld->ld_errno );
150 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
151 ld->ld_errno = LDAP_NO_MEMORY;
153 return( ld->ld_errno );
160 html = ( urlprefix != NULL );
164 * add HTML intro. and title
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 ));
179 if (( opts & LDAP_DISP_OPT_NONLEAF ) != 0 &&
180 ( vals = ldap_explode_dn( dn, 0 )) != NULL ) {
186 sprintf( buf, "<A HREF=\"%s", urlprefix );
187 for ( i = 1; vals[ i ] != NULL; ++i ) {
189 strcat_escaped( buf, ", " );
191 strcat_escaped( buf, vals[ i ] );
193 if ( vals[ 1 ] != NULL ) {
194 untagged = strchr( vals[ 1 ], '=' );
196 untagged = "=The World";
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 ));
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 ));
214 ldap_value_free( vals );
217 (*writeproc)( writeparm, "<HR>", 4 ); /* horizontal rule */
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 ));
225 if ( tmpl != NULL && ( opts & LDAP_DISP_OPT_AUTOLABELWIDTH ) != 0 ) {
226 labelwidth = max_label_len( tmpl ) + 3;
228 labelwidth = DEF_LABEL_WIDTH;;
233 if ( tmpl == 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 ) {
243 if ( defattrs != NULL ) {
244 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
245 if ( strcasecmp( attr, defattrs[ i ] ) == 0 ) {
249 if ( defattrs[ i ] != NULL ) {
257 *attr = TOUPPER( (unsigned char) *attr );
259 err = do_vals2text( ld, buf, vals, attr, labelwidth,
260 LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol,
261 rdncount, urlprefix );
263 ldap_value_free( vals );
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 )) {
273 if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
274 entry, colp->ti_attrname )) == NULL ) {
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 ] )
285 if ( defattrs[ i ] != NULL ) {
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 );
299 * don't bother even calling do_vals2text() if no values
300 * or boolean with value false and "hide if false" option set
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' ) {
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 );
323 err = do_vals2text( ld, buf, vals, colp->ti_label,
324 labelwidth, colp->ti_syntaxid, writeproc, writeparm,
325 eol, rdncount, urlprefix );
329 ldap_value_free( vals );
335 if ( html && !(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
336 sprintf( buf, "</BODY>%s</HTML>%s", eol, eol );
337 (*writeproc)( writeparm, buf, strlen( buf ));
350 ldap_entry2text_search(
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 */
358 writeptype writeproc,
361 int rdncount, /* if 0, display full DN */
365 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text_search\n", 0, 0, 0 );
367 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
368 defvals, writeproc, writeparm, eol, rdncount, opts, NULL ));
374 ldap_entry2html_search(
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 */
382 writeptype writeproc,
385 int rdncount, /* if 0, display full DN */
390 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html_search\n", 0, 0, 0 );
392 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
393 defvals, writeproc, writeparm, eol, rdncount, opts, urlprefix ));
398 do_entry2text_search(
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 */
406 writeptype writeproc,
409 int rdncount, /* if 0, display full DN */
414 int err, freedn, freetmpls, html;
415 char *buf, **fetchattrs, **vals;
417 struct ldap_disptmpl *tmpl;
418 struct timeval timeout;
420 if ( dn == NULL && entry == NULLMSG ) {
421 ld->ld_errno = LDAP_PARAM_ERROR;
422 return( ld->ld_errno );
425 html = ( urlprefix != NULL );
427 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
430 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
431 ld->ld_errno = LDAP_NO_MEMORY;
432 return( ld->ld_errno );
435 freedn = freetmpls = 0;
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 ));
449 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
452 ldap_free_templates( tmpllist );
454 return( ld->ld_errno );
460 if ( tmpllist != NULL ) {
463 if ( entry == NULL ) {
466 ocattrs[0] = OCATTRNAME;
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 );
473 #endif /* LDAP_CONNECTIONLESS */
474 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
475 "objectClass=*", ocattrs, 0, &timeout, &ldmp );
477 if ( err == LDAP_SUCCESS ) {
478 entry = ldap_first_entry( ld, ldmp );
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 );
489 if ( ldmp != NULL ) {
490 ldap_msgfree( ldmp );
496 if ( tmpl == NULL ) {
499 fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
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 );
507 #endif /* LDAP_CONNECTIONLESS */
508 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
509 fetchattrs, 0, &timeout, &ldmp );
514 if ( fetchattrs != NULL ) {
515 ldap_value_free( fetchattrs );
518 if ( err != LDAP_SUCCESS ||
519 ( entry = ldap_first_entry( ld, ldmp )) == NULL ) {
521 ldap_free_templates( tmpllist );
524 return( ld->ld_errno );
527 err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
528 writeproc, writeparm, eol, rdncount, opts, urlprefix );
532 ldap_free_templates( tmpllist );
534 ldap_msgfree( ldmp );
542 char *buf, /* NULL for "use internal" */
545 int labelwidth, /* 0 means use default */
546 unsigned long syntaxid,
547 writeptype writeproc,
553 Debug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
555 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
556 writeproc, writeparm, eol, rdncount, NULL ));
563 char *buf, /* NULL for "use internal" */
566 int labelwidth, /* 0 means use default */
567 unsigned long syntaxid,
568 writeptype writeproc,
575 Debug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
577 if ( urlprefix == NULL ) {
578 urlprefix = DEF_LDAP_URL_PREFIX;
581 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
582 writeproc, writeparm, eol, rdncount, urlprefix ));
589 char *buf, /* NULL for "use internal" */
592 int labelwidth, /* 0 means use default */
593 unsigned long syntaxid,
594 writeptype writeproc,
601 int i, html, writeoutval, freebuf, notascii;
602 char *p, *s, *outval;
605 if ( vals == NULL ) {
606 return( LDAP_SUCCESS );
609 html = ( urlprefix != NULL );
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... */
616 return( LDAP_SUCCESS );
619 if ( labelwidth == 0 || labelwidth < 0 ) {
620 labelwidth = DEF_LABEL_WIDTH;
624 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
625 ld->ld_errno = LDAP_NO_MEMORY;
626 return( ld->ld_errno );
633 output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
635 for ( i = 0; vals[ i ] != NULL; ++i ) {
636 for ( p = vals[ i ]; *p != '\0'; ++p ) {
637 if ( !isascii( *p )) {
641 notascii = ( *p != '\0' );
642 outval = notascii ? "(unable to display non-ASCII text value)"
645 writeoutval = 0; /* if non-zero, write outval after switch */
648 case LDAP_SYN_CASEIGNORESTR:
652 case LDAP_SYN_RFC822ADDR:
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 ));
663 case LDAP_SYN_DN: /* for now */
664 output_dn( buf, outval, labelwidth, rdncount, writeproc,
665 writeparm, eol, urlprefix );
668 case LDAP_SYN_MULTILINESTR:
669 if ( i > 0 && !html ) {
670 output_label( buf, label, labelwidth, writeproc,
671 writeparm, eol, html );
675 while (( s = strchr( s, '$' )) != NULL ) {
677 while ( isspace( (unsigned char) *s )) {
681 sprintf( buf, "<DD>%s<BR>%s", p, eol );
683 sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
685 (*writeproc)( writeparm, buf, strlen( buf ));
692 case LDAP_SYN_BOOLEAN:
693 outval = TOUPPER((unsigned char) outval[0]) == 'T' ? "TRUE" : "FALSE";
699 outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
703 case LDAP_SYN_LABELEDURL:
704 if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
706 while ( isspace( (unsigned char) *p )) {
710 } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
712 while ( isspace( (unsigned char) *s )) {
722 * at this point `s' points to the label & `p' to the URL
725 sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
727 sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
728 s, eol, labelwidth + 2, " ",p , eol );
730 (*writeproc)( writeparm, buf, strlen( buf ));
734 sprintf( buf, " Can't display item type %ld%s",
736 (*writeproc)( writeparm, buf, strlen( buf ));
741 sprintf( buf, "<DD>%s<BR>%s", outval, eol );
743 sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
745 (*writeproc)( writeparm, buf, strlen( buf ));
753 return( LDAP_SUCCESS );
758 max_label_len( struct ldap_disptmpl *tmpl )
760 struct ldap_tmplitem *rowp, *colp;
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 ) {
780 output_label( char *buf, char *label, int width, writeptype writeproc,
781 void *writeparm, char *eol, int html )
786 sprintf( buf, "<DT><B>%s</B>", label );
788 sprintf( buf, " %s:", label );
789 p = buf + strlen( buf );
791 while ( p - buf < width ) {
799 return ((*writeproc)( writeparm, buf, strlen( buf )));
804 output_dn( char *buf, char *dn, int width, int rdncount,
805 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
810 if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
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, " " );
824 for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
829 strcat( buf, dnrdns[ i ] );
832 if ( urlprefix != NULL ) {
833 strcat( buf, "</A><BR>" );
836 ldap_value_free( dnrdns );
840 return ((*writeproc)( writeparm, buf, strlen( buf )));
845 #define HREF_CHAR_ACCEPTABLE( c ) (( c >= '-' && c <= '9' ) || \
846 ( c >= '@' && c <= 'Z' ) || \
848 ( c >= 'a' && c <= 'z' ))
851 strcat_escaped( char *s1, char *s2 )
854 char *hexdig = "0123456789ABCDEF";
856 p = s1 + strlen( s1 );
857 for ( q = s2; *q != '\0'; ++q ) {
858 if ( HREF_CHAR_ACCEPTABLE( *q )) {
862 *p++ = hexdig[ *q >> 4 ];
863 *p++ = hexdig[ *q & 0x0F ];
871 #define GET2BYTENUM( p ) (( *p - '0' ) * 10 + ( *(p+1) - '0' ))
874 time2text( char *ldtimestr, int dateonly )
877 char *p, *timestr, zone, *fmterr = "badly formatted time";
882 if (strlen( ldtimestr ) < 12 ) {
886 for ( ndigits=0; isdigit((unsigned char) ldtimestr[ndigits]); ndigits++) {
890 if ( ndigits != 12 && ndigits != 14) {
894 memset( (char *)&t, 0, sizeof( struct tm ));
899 /* came with a century */
900 /* POSIX says tm_year should be year - 1900 */
901 t.tm_year = 100 * GET2BYTENUM( p ) - 1900;
904 t.tm_year = GET2BYTENUM( p ); p += 2;
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;
912 if (( zone = *p ) == 'Z' ) { /* GMT */
913 zone = '\0'; /* no need to indicate on screen, so we make it null */
916 gmttime = gtime( &t );
917 timestr = ctime( &gmttime );
919 timestr[ strlen( timestr ) - 1 ] = zone; /* replace trailing newline */
921 SAFEMEMCPY( timestr + 11, timestr + 20, strlen( timestr + 20 ) + 1 );
929 /* gtime.c - inverse gmtime */
930 /* gtime(): the inverse of localtime().
931 This routine was supplied by Mike Accetta at CMU many years ago.
934 static int dmsize[] = {
935 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
939 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
944 /* per POSIX tm_year should be offset by 1900 */
945 #define YEAR_POSIX(y) ((y) + 1900)
948 * year is < 1900, year is offset by 1900
950 #define YEAR_CAREFUL(y) ((y) < 1900 ? (y) + 1900 : (y))
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.
957 #define YEAR_PEDANTIC(y) ((y) < 1900 \
958 ? ((y) < 70 ? (y) + 2000 ? (y) + 1900) \
961 #define YEAR(y) YEAR_CAREFUL(y)
965 static long gtime ( struct tm *tm )
974 register long result;
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)
986 year = YEAR (tm -> tm_year);
989 for (i = 1970; i < year; i++)
990 result += dysize (i);
991 if (dysize (year) == 366 && mon >= 3)
994 result += dmsize[mon - 1];
996 result = 24 * result + hour;
997 result = 60 * result + mins;
998 result = 60 * result + sec;
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 )
1008 int err = 0, lderr, i, count, html;
1009 char **vals, **members;
1010 char *value, *filtpattern, *attr, *selectname;
1011 char *retattrs[2], filter[ 256 ];
1013 struct timeval timeout;
1015 html = ( urlprefix != NULL );
1017 for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
1021 return( LDAP_PARAM_ERROR );
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 ];
1030 if ( attr == NULL ) {
1032 } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
1033 return( LDAP_PARAM_ERROR );
1034 } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
1036 } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
1042 ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
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
1051 sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
1052 if ( base != NULL ) {
1053 strcat_escaped( buf, base );
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 );
1062 return( LDAP_SUCCESS );
1065 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1066 timeout.tv_usec = 0;
1068 #ifdef LDAP_CONNECTIONLESS
1069 if ( LDAP_IS_CLDAP( ld ))
1070 lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1073 #endif /* LDAP_CONNECTIONLESS */
1074 lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1075 0, &timeout, &ldmp );
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 *)))
1081 err = LDAP_NO_MEMORY;
1083 for ( i = 0, entry = ldap_first_entry( ld, ldmp );
1085 entry = ldap_next_entry( ld, entry ), ++i ) {
1086 members[ i ] = ldap_get_dn( ld, entry );
1088 members[ i ] = NULL;
1090 ldap_sort_values( ld, members, ldap_sort_strcasecmp );
1092 err = do_vals2text( ld, NULL, members, tip->ti_label,
1093 html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
1094 eol, rdncount, urlprefix );
1096 ldap_value_free( members );
1099 ldap_msgfree( ldmp );
1103 if ( vals != NULL ) {
1104 ldap_value_free( vals );
1107 return(( err == LDAP_SUCCESS ) ? lderr : err );