2 * tmplout.c: display template library output routines for LDAP clients
3 * 12 April 1994 by Mark C Smith
12 #include <ac/string.h>
15 #ifdef HAVE_SYS_FILE_H
26 #include "ldapconfig.h"
29 static int do_entry2text LDAP_P((
30 LDAP *ld, char *buf, char *base, LDAPMessage *entry,
31 struct ldap_disptmpl *tmpl, char **defattrs, char ***defvals,
32 writeptype writeproc, void *writeparm, char *eol, int rdncount,
33 unsigned long opts, char *urlprefix ));
34 static int do_entry2text_search LDAP_P((
35 LDAP *ld, char *dn, char *base,
36 LDAPMessage *entry, struct ldap_disptmpl *tmpllist, char **defattrs,
37 char ***defvals, writeptype writeproc, void *writeparm, char *eol,
38 int rdncount, unsigned long opts, char *urlprefix ));
39 static int do_vals2text LDAP_P((
40 LDAP *ld, char *buf, char **vals, char *label,
41 int labelwidth, unsigned long syntaxid, writeptype writeproc,
42 void *writeparm, char *eol, int rdncount, char *urlprefix ));
43 static int max_label_len LDAP_P(( struct ldap_disptmpl *tmpl ));
44 static int output_label LDAP_P((
45 char *buf, char *label, int width,
46 writeptype writeproc, void *writeparm, char *eol, int html ));
47 static int output_dn LDAP_P((
48 char *buf, char *dn, int width, int rdncount,
49 writeptype writeproc, void *writeparm, char *eol, char *urlprefix ));
50 static void strcat_escaped LDAP_P(( char *s1, char *s2 ));
51 static char *time2text LDAP_P(( char *ldtimestr, int dateonly ));
52 static long gtime LDAP_P(( struct tm *tm ));
53 static int searchaction LDAP_P((
54 LDAP *ld, char *buf, char *base, LDAPMessage *entry,
55 char *dn, struct ldap_tmplitem *tip, int labelwidth, int rdncount,
56 writeptype writeproc, void *writeparm, char *eol, char *urlprefix ));
58 #define DEF_LABEL_WIDTH 15
59 #define SEARCH_TIMEOUT_SECS 120
60 #define OCATTRNAME "objectClass"
63 #define NONFATAL_LDAP_ERR( err ) ( err == LDAP_SUCCESS || \
64 err == LDAP_TIMELIMIT_EXCEEDED || err == LDAP_SIZELIMIT_EXCEEDED )
66 #define DEF_LDAP_URL_PREFIX "ldap:///"
72 char *buf, /* NULL for "use internal" */
74 struct ldap_disptmpl *tmpl,
84 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text\n", 0, 0, 0 );
86 return( do_entry2text( ld, buf, NULL, entry, tmpl, defattrs, defvals,
87 writeproc, writeparm, eol, rdncount, opts, NULL ));
96 char *buf, /* NULL for "use internal" */
98 struct ldap_disptmpl *tmpl,
101 writeptype writeproc,
110 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html\n", 0, 0, 0 );
112 if ( urlprefix == NULL ) {
113 urlprefix = DEF_LDAP_URL_PREFIX;
116 return( do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
117 writeproc, writeparm, eol, rdncount, opts, urlprefix ));
124 char *buf, /* NULL for use-internal */
125 char *base, /* used for search actions */
127 struct ldap_disptmpl *tmpl,
130 writeptype writeproc,
135 char *urlprefix /* if non-NULL, do HTML */
138 int i, err, html, show, labelwidth;
139 int freebuf, freevals;
141 struct ldap_tmplitem *rowp, *colp;
143 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
144 return( ld->ld_errno );
148 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
149 ld->ld_errno = LDAP_NO_MEMORY;
151 return( ld->ld_errno );
158 html = ( urlprefix != NULL );
162 * add HTML intro. and title
164 if (!(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
165 sprintf( buf, "<HTML>%s<HEAD>%s<TITLE>%s%s - ", eol, eol, eol,
166 ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
167 (*writeproc)( writeparm, buf, strlen( buf ));
168 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
169 sprintf( buf, "%s</TITLE>%s</HEAD>%s<BODY>%s<H3>%s - ", eol, eol,
170 eol, eol, ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
171 (*writeproc)( writeparm, buf, strlen( buf ));
172 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
173 sprintf( buf, "</H3>%s", eol );
174 (*writeproc)( writeparm, buf, strlen( buf ));
177 if (( opts & LDAP_DISP_OPT_NONLEAF ) != 0 &&
178 ( vals = ldap_explode_dn( dn, 0 )) != NULL ) {
184 sprintf( buf, "<A HREF=\"%s", urlprefix );
185 for ( i = 1; vals[ i ] != NULL; ++i ) {
187 strcat_escaped( buf, ", " );
189 strcat_escaped( buf, vals[ i ] );
191 if ( vals[ 1 ] != NULL ) {
192 untagged = strchr( vals[ 1 ], '=' );
194 untagged = "=The World";
196 sprintf( buf + strlen( buf ),
197 "%s\">Move Up To <EM>%s</EM></A>%s<BR>",
198 ( vals[ 1 ] == NULL ) ? "??one" : "",
199 ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol );
200 (*writeproc)( writeparm, buf, strlen( buf ));
205 untagged = strchr( vals[ 0 ], '=' );
206 sprintf( buf, "<A HREF=\"%s", urlprefix );
207 strcat_escaped( buf, dn );
208 sprintf( buf + strlen( buf ), "??one?(!(objectClass=dsa))\">Browse Below <EM>%s</EM></A>%s%s",
209 ( untagged != NULL ) ? untagged + 1 : vals[ 0 ], eol, eol );
210 (*writeproc)( writeparm, buf, strlen( buf ));
212 ldap_value_free( vals );
215 (*writeproc)( writeparm, "<HR>", 4 ); /* horizontal rule */
217 (*writeproc)( writeparm, "\"", 1 );
218 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
219 sprintf( buf, "\"%s", eol );
220 (*writeproc)( writeparm, buf, strlen( buf ));
223 if ( tmpl != NULL && ( opts & LDAP_DISP_OPT_AUTOLABELWIDTH ) != 0 ) {
224 labelwidth = max_label_len( tmpl ) + 3;
226 labelwidth = DEF_LABEL_WIDTH;;
231 if ( tmpl == NULL ) {
236 for ( attr = ldap_first_attribute( ld, entry, &ber );
237 NONFATAL_LDAP_ERR( err ) && attr != NULL;
238 attr = ldap_next_attribute( ld, entry, ber )) {
239 if (( vals = ldap_get_values( ld, entry, attr )) == NULL ) {
241 if ( defattrs != NULL ) {
242 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
243 if ( strcasecmp( attr, defattrs[ i ] ) == 0 ) {
247 if ( defattrs[ i ] != NULL ) {
255 *attr = TOUPPER( *attr );
257 err = do_vals2text( ld, buf, vals, attr, labelwidth,
258 LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol,
259 rdncount, urlprefix );
261 ldap_value_free( vals );
265 for ( rowp = ldap_first_tmplrow( tmpl );
266 NONFATAL_LDAP_ERR( err ) && rowp != NULLTMPLITEM;
267 rowp = ldap_next_tmplrow( tmpl, rowp )) {
268 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
269 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
271 if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
272 entry, colp->ti_attrname )) == NULL ) {
274 if ( !LDAP_IS_TMPLITEM_OPTION_SET( colp,
275 LDAP_DITEM_OPT_HIDEIFEMPTY ) && defattrs != NULL
276 && colp->ti_attrname != NULL ) {
277 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
278 if ( strcasecmp( colp->ti_attrname, defattrs[ i ] )
283 if ( defattrs[ i ] != NULL ) {
289 if ( LDAP_IS_TMPLITEM_OPTION_SET( colp,
290 LDAP_DITEM_OPT_SORTVALUES ) && vals[ 0 ] != NULL
291 && vals[ 1 ] != NULL ) {
292 ldap_sort_values( ld, vals, ldap_sort_strcasecmp );
297 * don't bother even calling do_vals2text() if no values
298 * or boolean with value false and "hide if false" option set
300 show = ( vals != NULL && vals[ 0 ] != NULL );
301 if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
302 == LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
303 colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
304 TOUPPER( vals[ 0 ][ 0 ] ) != 'T' ) {
308 if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
309 if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
310 if ( colp->ti_attrname == NULL || ( show &&
311 TOUPPER( vals[ 0 ][ 0 ] ) == 'T' )) {
312 err = searchaction( ld, buf, base, entry, dn, colp,
313 labelwidth, rdncount, writeproc,
314 writeparm, eol, urlprefix );
321 err = do_vals2text( ld, buf, vals, colp->ti_label,
322 labelwidth, colp->ti_syntaxid, writeproc, writeparm,
323 eol, rdncount, urlprefix );
327 ldap_value_free( vals );
333 if ( html && !(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
334 sprintf( buf, "</BODY>%s</HTML>%s", eol, eol );
335 (*writeproc)( writeparm, buf, strlen( buf ));
348 ldap_entry2text_search(
350 char *dn, /* if NULL, use entry */
351 char *base, /* if NULL, no search actions */
352 LDAPMessage *entry, /* if NULL, use dn */
353 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
356 writeptype writeproc,
359 int rdncount, /* if 0, display full DN */
363 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text_search\n", 0, 0, 0 );
365 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
366 defvals, writeproc, writeparm, eol, rdncount, opts, NULL ));
372 ldap_entry2html_search(
374 char *dn, /* if NULL, use entry */
375 char *base, /* if NULL, no search actions */
376 LDAPMessage *entry, /* if NULL, use dn */
377 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
380 writeptype writeproc,
383 int rdncount, /* if 0, display full DN */
388 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html_search\n", 0, 0, 0 );
390 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
391 defvals, writeproc, writeparm, eol, rdncount, opts, urlprefix ));
396 do_entry2text_search(
398 char *dn, /* if NULL, use entry */
399 char *base, /* if NULL, no search actions */
400 LDAPMessage *entry, /* if NULL, use dn */
401 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
404 writeptype writeproc,
407 int rdncount, /* if 0, display full DN */
412 int err, freedn, freetmpls, html;
413 char *buf, **fetchattrs, **vals;
415 struct ldap_disptmpl *tmpl;
416 struct timeval timeout;
418 if ( dn == NULL && entry == NULLMSG ) {
419 ld->ld_errno = LDAP_PARAM_ERROR;
420 return( ld->ld_errno );
423 html = ( urlprefix != NULL );
425 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
428 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
429 ld->ld_errno = LDAP_NO_MEMORY;
430 return( ld->ld_errno );
433 freedn = freetmpls = 0;
436 if ( tmpllist == NULL ) {
437 if (( err = ldap_init_templates( TEMPLATEFILE, &tmpllist )) != 0 ) {
438 sprintf( buf, "%sUnable to read template file %s (error %d)%s%s",
439 html ? "<!-- " : "", TEMPLATEFILE, err,
440 html ? "-->" : "", eol );
441 (*writeproc)( writeparm, buf, strlen( buf ));
447 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
450 ldap_free_templates( tmpllist );
452 return( ld->ld_errno );
458 if ( tmpllist != NULL ) {
461 if ( entry == NULL ) {
464 ocattrs[0] = OCATTRNAME;
466 #ifdef LDAP_CONNECTIONLESS
467 if ( LDAP_IS_CLDAP( ld ))
468 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE,
469 "objectClass=*", ocattrs, 0, &ldmp, NULL );
471 #endif /* LDAP_CONNECTIONLESS */
472 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
473 "objectClass=*", ocattrs, 0, &timeout, &ldmp );
475 if ( err == LDAP_SUCCESS ) {
476 entry = ldap_first_entry( ld, ldmp );
480 if ( entry != NULL ) {
481 vals = ldap_get_values( ld, entry, OCATTRNAME );
482 tmpl = ldap_oc2template( vals, tmpllist );
483 if ( vals != NULL ) {
484 ldap_value_free( vals );
487 if ( ldmp != NULL ) {
488 ldap_msgfree( ldmp );
494 if ( tmpl == NULL ) {
497 fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
500 #ifdef LDAP_CONNECTIONLESS
501 if ( LDAP_IS_CLDAP( ld ))
502 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
503 fetchattrs, 0, &ldmp, NULL );
505 #endif /* LDAP_CONNECTIONLESS */
506 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
507 fetchattrs, 0, &timeout, &ldmp );
512 if ( fetchattrs != NULL ) {
513 ldap_value_free( fetchattrs );
516 if ( err != LDAP_SUCCESS ||
517 ( entry = ldap_first_entry( ld, ldmp )) == NULL ) {
519 ldap_free_templates( tmpllist );
522 return( ld->ld_errno );
525 err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
526 writeproc, writeparm, eol, rdncount, opts, urlprefix );
530 ldap_free_templates( tmpllist );
532 ldap_msgfree( ldmp );
540 char *buf, /* NULL for "use internal" */
543 int labelwidth, /* 0 means use default */
544 unsigned long syntaxid,
545 writeptype writeproc,
551 Debug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
553 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
554 writeproc, writeparm, eol, rdncount, NULL ));
561 char *buf, /* NULL for "use internal" */
564 int labelwidth, /* 0 means use default */
565 unsigned long syntaxid,
566 writeptype writeproc,
573 Debug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
575 if ( urlprefix == NULL ) {
576 urlprefix = DEF_LDAP_URL_PREFIX;
579 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
580 writeproc, writeparm, eol, rdncount, urlprefix ));
587 char *buf, /* NULL for "use internal" */
590 int labelwidth, /* 0 means use default */
591 unsigned long syntaxid,
592 writeptype writeproc,
599 int i, html, writeoutval, freebuf, notascii;
600 char *p, *s, *outval;
603 if ( vals == NULL ) {
604 return( LDAP_SUCCESS );
607 html = ( urlprefix != NULL );
609 switch( LDAP_GET_SYN_TYPE( syntaxid )) {
610 case LDAP_SYN_TYPE_TEXT:
611 case LDAP_SYN_TYPE_BOOLEAN:
612 break; /* we only bother with these two types... */
614 return( LDAP_SUCCESS );
617 if ( labelwidth == 0 || labelwidth < 0 ) {
618 labelwidth = DEF_LABEL_WIDTH;
622 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
623 ld->ld_errno = LDAP_NO_MEMORY;
624 return( ld->ld_errno );
631 output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
633 for ( i = 0; vals[ i ] != NULL; ++i ) {
634 for ( p = vals[ i ]; *p != '\0'; ++p ) {
635 if ( !isascii( *p )) {
639 notascii = ( *p != '\0' );
640 outval = notascii ? "(unable to display non-ASCII text value)"
643 writeoutval = 0; /* if non-zero, write outval after switch */
646 case LDAP_SYN_CASEIGNORESTR:
650 case LDAP_SYN_RFC822ADDR:
652 strcpy( buf, "<DD><A HREF=\"mailto:" );
653 strcat_escaped( buf, outval );
654 sprintf( buf + strlen( buf ), "\">%s</A><BR>%s", outval, eol );
655 (*writeproc)( writeparm, buf, strlen( buf ));
661 case LDAP_SYN_DN: /* for now */
662 output_dn( buf, outval, labelwidth, rdncount, writeproc,
663 writeparm, eol, urlprefix );
666 case LDAP_SYN_MULTILINESTR:
667 if ( i > 0 && !html ) {
668 output_label( buf, label, labelwidth, writeproc,
669 writeparm, eol, html );
673 while (( s = strchr( s, '$' )) != NULL ) {
675 while ( isspace( *s )) {
679 sprintf( buf, "<DD>%s<BR>%s", p, eol );
681 sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
683 (*writeproc)( writeparm, buf, strlen( buf ));
690 case LDAP_SYN_BOOLEAN:
691 outval = TOUPPER( outval[ 0 ] ) == 'T' ? "TRUE" : "FALSE";
697 outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
701 case LDAP_SYN_LABELEDURL:
702 if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
704 while ( isspace( *p )) {
708 } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
710 while ( isspace( *s )) {
720 * at this point `s' points to the label & `p' to the URL
723 sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
725 sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
726 s, eol, labelwidth + 2, " ",p , eol );
728 (*writeproc)( writeparm, buf, strlen( buf ));
732 sprintf( buf, " Can't display item type %ld%s",
734 (*writeproc)( writeparm, buf, strlen( buf ));
739 sprintf( buf, "<DD>%s<BR>%s", outval, eol );
741 sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
743 (*writeproc)( writeparm, buf, strlen( buf ));
751 return( LDAP_SUCCESS );
756 max_label_len( struct ldap_disptmpl *tmpl )
758 struct ldap_tmplitem *rowp, *colp;
763 for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
764 rowp = ldap_next_tmplrow( tmpl, rowp )) {
765 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
766 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
767 if (( len = strlen( colp->ti_label )) > maxlen ) {
778 output_label( char *buf, char *label, int width, writeptype writeproc,
779 void *writeparm, char *eol, int html )
784 sprintf( buf, "<DT><B>%s</B>", label );
786 sprintf( buf, " %s:", label );
787 p = buf + strlen( buf );
789 while ( p - buf < width ) {
797 return ((*writeproc)( writeparm, buf, strlen( buf )));
802 output_dn( char *buf, char *dn, int width, int rdncount,
803 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
808 if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
812 if ( urlprefix != NULL ) {
813 sprintf( buf, "<DD><A HREF=\"%s", urlprefix );
814 strcat_escaped( buf, dn );
815 strcat( buf, "\">" );
816 } else if ( width > 0 ) {
817 sprintf( buf, "%-*s", width, " " );
822 for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
827 strcat( buf, dnrdns[ i ] );
830 if ( urlprefix != NULL ) {
831 strcat( buf, "</A><BR>" );
834 ldap_value_free( dnrdns );
838 return ((*writeproc)( writeparm, buf, strlen( buf )));
843 #define HREF_CHAR_ACCEPTABLE( c ) (( c >= '-' && c <= '9' ) || \
844 ( c >= '@' && c <= 'Z' ) || \
846 ( c >= 'a' && c <= 'z' ))
849 strcat_escaped( char *s1, char *s2 )
852 char *hexdig = "0123456789ABCDEF";
854 p = s1 + strlen( s1 );
855 for ( q = s2; *q != '\0'; ++q ) {
856 if ( HREF_CHAR_ACCEPTABLE( *q )) {
860 *p++ = hexdig[ *q >> 4 ];
861 *p++ = hexdig[ *q & 0x0F ];
869 #define GET2BYTENUM( p ) (( *p - '0' ) * 10 + ( *(p+1) - '0' ))
872 time2text( char *ldtimestr, int dateonly )
875 char *p, *timestr, zone, *fmterr = "badly formatted time";
878 memset( (char *)&t, 0, sizeof( struct tm ));
879 if ( (int) strlen( ldtimestr ) < 13 ) {
883 for ( p = ldtimestr; p - ldtimestr < 12; ++p ) {
884 if ( !isdigit( *p )) {
890 t.tm_year = GET2BYTENUM( p ); p += 2;
891 t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
892 t.tm_mday = GET2BYTENUM( p ); p += 2;
893 t.tm_hour = GET2BYTENUM( p ); p += 2;
894 t.tm_min = GET2BYTENUM( p ); p += 2;
895 t.tm_sec = GET2BYTENUM( p ); p += 2;
897 if (( zone = *p ) == 'Z' ) { /* GMT */
898 zone = '\0'; /* no need to indicate on screen, so we make it null */
901 gmttime = gtime( &t );
902 timestr = ctime( &gmttime );
904 timestr[ strlen( timestr ) - 1 ] = zone; /* replace trailing newline */
906 strcpy( timestr + 11, timestr + 20 );
914 /* gtime.c - inverse gmtime */
915 /* gtime(): the inverse of localtime().
916 This routine was supplied by Mike Accetta at CMU many years ago.
919 static int dmsize[] = {
920 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
924 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
926 #define YEAR(y) ((y) >= 100 ? (y) : (y) + 1900)
930 static long gtime ( struct tm *tm )
939 register long result;
941 if ((sec = tm -> tm_sec) < 0 || sec > 59
942 || (mins = tm -> tm_min) < 0 || mins > 59
943 || (hour = tm -> tm_hour) < 0 || hour > 24
944 || (mday = tm -> tm_mday) < 1 || mday > 31
945 || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
951 year = YEAR (tm -> tm_year);
954 for (i = 1970; i < year; i++)
955 result += dysize (i);
956 if (dysize (year) == 366 && mon >= 3)
959 result += dmsize[mon - 1];
961 result = 24 * result + hour;
962 result = 60 * result + mins;
963 result = 60 * result + sec;
969 searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
970 struct ldap_tmplitem *tip, int labelwidth, int rdncount,
971 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
973 int err = 0, lderr, i, count, html;
974 char **vals, **members;
975 char *value, *filtpattern, *attr, *selectname;
976 char *retattrs[2], filter[ 256 ];
978 struct timeval timeout;
980 html = ( urlprefix != NULL );
982 for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
986 return( LDAP_PARAM_ERROR );
988 attr = tip->ti_args[ 0 ];
989 filtpattern = tip->ti_args[ 1 ];
990 retattrs[ 0 ] = tip->ti_args[ 2 ];
991 retattrs[ 1 ] = NULL;
992 selectname = tip->ti_args[ 3 ];
995 if ( attr == NULL ) {
997 } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
998 return( LDAP_PARAM_ERROR );
999 } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
1001 } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
1007 ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
1012 * if we are generating HTML, we add an HREF link that embodies this
1013 * search action as an LDAP URL, instead of actually doing the search
1016 sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
1017 if ( base != NULL ) {
1018 strcat_escaped( buf, base );
1020 strcat( buf, "??sub?" );
1021 strcat_escaped( buf, filter );
1022 sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
1023 tip->ti_label, eol );
1024 if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
1025 return( LDAP_LOCAL_ERROR );
1027 return( LDAP_SUCCESS );
1030 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1031 timeout.tv_usec = 0;
1033 #ifdef LDAP_CONNECTIONLESS
1034 if ( LDAP_IS_CLDAP( ld ))
1035 lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1038 #endif /* LDAP_CONNECTIONLESS */
1039 lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1040 0, &timeout, &ldmp );
1042 if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
1043 if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
1044 if (( members = (char **)malloc( (count + 1) * sizeof(char *)))
1046 err = LDAP_NO_MEMORY;
1048 for ( i = 0, entry = ldap_first_entry( ld, ldmp );
1050 entry = ldap_next_entry( ld, entry ), ++i ) {
1051 members[ i ] = ldap_get_dn( ld, entry );
1053 members[ i ] = NULL;
1055 ldap_sort_values( ld, members, ldap_sort_strcasecmp );
1057 err = do_vals2text( ld, NULL, members, tip->ti_label,
1058 html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
1059 eol, rdncount, urlprefix );
1061 ldap_value_free( members );
1064 ldap_msgfree( ldmp );
1068 if ( vals != NULL ) {
1069 ldap_value_free( vals );
1072 return(( err == LDAP_SUCCESS ) ? lderr : err );