2 * tmplout.c: display template library output routines for LDAP clients
3 * 12 April 1994 by Mark C Smith
19 #include <sys/types.h>
25 #include <sys/socket.h>
32 #include "ldapconfig.h"
35 static int do_entry2text( LDAP *ld, char *buf, char *base, LDAPMessage *entry,
36 struct ldap_disptmpl *tmpl, char **defattrs, char ***defvals,
37 writeptype writeproc, void *writeparm, char *eol, int rdncount,
38 unsigned long opts, char *urlprefix );
39 static int do_entry2text_search( LDAP *ld, char *dn, char *base,
40 LDAPMessage *entry, struct ldap_disptmpl *tmpllist, char **defattrs,
41 char ***defvals, writeptype writeproc, void *writeparm, char *eol,
42 int rdncount, unsigned long opts, char *urlprefix );
43 static int do_vals2text( LDAP *ld, char *buf, char **vals, char *label,
44 int labelwidth, unsigned long syntaxid, writeptype writeproc,
45 void *writeparm, char *eol, int rdncount, char *urlprefix );
46 static int max_label_len( struct ldap_disptmpl *tmpl );
47 static int output_label( char *buf, char *label, int width,
48 writeptype writeproc, void *writeparm, char *eol, int html );
49 static int output_dn( char *buf, char *dn, int width, int rdncount,
50 writeptype writeproc, void *writeparm, char *eol, char *urlprefix );
51 static void strcat_escaped( char *s1, char *s2 );
52 static char *time2text( char *ldtimestr, int dateonly );
53 static long gtime( struct tm *tm );
54 static int searchaction( 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 );
57 #else /* NEEDPROTOS */
58 static int do_entry2text();
59 static int do_entry2text_search();
60 static int do_vals2text();
61 static int max_label_len();
62 static int output_label();
63 static int output_dn();
64 static void strcat_escaped();
65 static char *time2text();
67 static int searchaction();
68 #endif /* NEEDPROTOS */
70 #define DEF_LABEL_WIDTH 15
71 #define SEARCH_TIMEOUT_SECS 120
72 #define OCATTRNAME "objectClass"
75 #define NONFATAL_LDAP_ERR( err ) ( err == LDAP_SUCCESS || \
76 err == LDAP_TIMELIMIT_EXCEEDED || err == LDAP_SIZELIMIT_EXCEEDED )
78 #define DEF_LDAP_URL_PREFIX "ldap:///"
84 char *buf, /* NULL for "use internal" */
86 struct ldap_disptmpl *tmpl,
96 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text\n", 0, 0, 0 );
98 return( do_entry2text( ld, buf, NULL, entry, tmpl, defattrs, defvals,
99 writeproc, writeparm, eol, rdncount, opts, NULL ));
108 char *buf, /* NULL for "use internal" */
110 struct ldap_disptmpl *tmpl,
113 writeptype writeproc,
122 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html\n", 0, 0, 0 );
124 if ( urlprefix == NULL ) {
125 urlprefix = DEF_LDAP_URL_PREFIX;
128 return( do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
129 writeproc, writeparm, eol, rdncount, opts, urlprefix ));
136 char *buf, /* NULL for use-internal */
137 char *base, /* used for search actions */
139 struct ldap_disptmpl *tmpl,
142 writeptype writeproc,
147 char *urlprefix /* if non-NULL, do HTML */
150 int i, err, html, show, labelwidth;
151 int freebuf, freevals;
153 struct ldap_tmplitem *rowp, *colp;
155 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
156 return( ld->ld_errno );
160 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
161 ld->ld_errno = LDAP_NO_MEMORY;
163 return( ld->ld_errno );
170 html = ( urlprefix != NULL );
174 * add HTML intro. and title
176 if (!(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
177 sprintf( buf, "<HTML>%s<HEAD>%s<TITLE>%s%s - ", eol, eol, eol,
178 ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
179 (*writeproc)( writeparm, buf, strlen( buf ));
180 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
181 sprintf( buf, "%s</TITLE>%s</HEAD>%s<BODY>%s<H3>%s - ", eol, eol,
182 eol, eol, ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
183 (*writeproc)( writeparm, buf, strlen( buf ));
184 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
185 sprintf( buf, "</H3>%s", eol );
186 (*writeproc)( writeparm, buf, strlen( buf ));
189 if (( opts & LDAP_DISP_OPT_NONLEAF ) != 0 &&
190 ( vals = ldap_explode_dn( dn, 0 )) != NULL ) {
196 sprintf( buf, "<A HREF=\"%s", urlprefix );
197 for ( i = 1; vals[ i ] != NULL; ++i ) {
199 strcat_escaped( buf, ", " );
201 strcat_escaped( buf, vals[ i ] );
203 if ( vals[ 1 ] != NULL ) {
204 untagged = strchr( vals[ 1 ], '=' );
206 untagged = "=The World";
208 sprintf( buf + strlen( buf ),
209 "%s\">Move Up To <EM>%s</EM></A>%s<BR>",
210 ( vals[ 1 ] == NULL ) ? "??one" : "",
211 ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol, eol );
212 (*writeproc)( writeparm, buf, strlen( buf ));
217 untagged = strchr( vals[ 0 ], '=' );
218 sprintf( buf, "<A HREF=\"%s", urlprefix );
219 strcat_escaped( buf, dn );
220 sprintf( buf + strlen( buf ), "??one?(!(objectClass=dsa))\">Browse Below <EM>%s</EM></A>%s%s",
221 ( untagged != NULL ) ? untagged + 1 : vals[ 0 ], eol, eol );
222 (*writeproc)( writeparm, buf, strlen( buf ));
224 ldap_value_free( vals );
227 (*writeproc)( writeparm, "<HR>", 4 ); /* horizontal rule */
229 (*writeproc)( writeparm, "\"", 1 );
230 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
231 sprintf( buf, "\"%s", eol );
232 (*writeproc)( writeparm, buf, strlen( buf ));
235 if ( tmpl != NULL && ( opts & LDAP_DISP_OPT_AUTOLABELWIDTH ) != 0 ) {
236 labelwidth = max_label_len( tmpl ) + 3;
238 labelwidth = DEF_LABEL_WIDTH;;
243 if ( tmpl == NULL ) {
248 for ( attr = ldap_first_attribute( ld, entry, &ber );
249 NONFATAL_LDAP_ERR( err ) && attr != NULL;
250 attr = ldap_next_attribute( ld, entry, ber )) {
251 if (( vals = ldap_get_values( ld, entry, attr )) == NULL ) {
253 if ( defattrs != NULL ) {
254 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
255 if ( strcasecmp( attr, defattrs[ i ] ) == 0 ) {
259 if ( defattrs[ i ] != NULL ) {
267 if ( islower( *attr )) { /* cosmetic -- upcase attr. name */
268 *attr = toupper( *attr );
271 err = do_vals2text( ld, buf, vals, attr, labelwidth,
272 LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol,
273 rdncount, urlprefix );
275 ldap_value_free( vals );
279 for ( rowp = ldap_first_tmplrow( tmpl );
280 NONFATAL_LDAP_ERR( err ) && rowp != NULLTMPLITEM;
281 rowp = ldap_next_tmplrow( tmpl, rowp )) {
282 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
283 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
285 if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
286 entry, colp->ti_attrname )) == NULL ) {
288 if ( !LDAP_IS_TMPLITEM_OPTION_SET( colp,
289 LDAP_DITEM_OPT_HIDEIFEMPTY ) && defattrs != NULL
290 && colp->ti_attrname != NULL ) {
291 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
292 if ( strcasecmp( colp->ti_attrname, defattrs[ i ] )
297 if ( defattrs[ i ] != NULL ) {
303 if ( LDAP_IS_TMPLITEM_OPTION_SET( colp,
304 LDAP_DITEM_OPT_SORTVALUES ) && vals[ 0 ] != NULL
305 && vals[ 1 ] != NULL ) {
306 ldap_sort_values( ld, vals, ldap_sort_strcasecmp );
311 * don't bother even calling do_vals2text() if no values
312 * or boolean with value false and "hide if false" option set
314 show = ( vals != NULL && vals[ 0 ] != NULL );
315 if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
316 == LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
317 colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
318 toupper( vals[ 0 ][ 0 ] ) != 'T' ) {
322 if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
323 if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
324 if ( colp->ti_attrname == NULL || ( show &&
325 toupper( vals[ 0 ][ 0 ] ) == 'T' )) {
326 err = searchaction( ld, buf, base, entry, dn, colp,
327 labelwidth, rdncount, writeproc,
328 writeparm, eol, urlprefix );
335 err = do_vals2text( ld, buf, vals, colp->ti_label,
336 labelwidth, colp->ti_syntaxid, writeproc, writeparm,
337 eol, rdncount, urlprefix );
341 ldap_value_free( vals );
347 if ( html && !(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
348 sprintf( buf, "</BODY>%s</HTML>%s", eol, eol );
349 (*writeproc)( writeparm, buf, strlen( buf ));
362 ldap_entry2text_search(
364 char *dn, /* if NULL, use entry */
365 char *base, /* if NULL, no search actions */
366 LDAPMessage *entry, /* if NULL, use dn */
367 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
370 writeptype writeproc,
373 int rdncount, /* if 0, display full DN */
377 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text_search\n", 0, 0, 0 );
379 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
380 defvals, writeproc, writeparm, eol, rdncount, opts, NULL ));
386 ldap_entry2html_search(
388 char *dn, /* if NULL, use entry */
389 char *base, /* if NULL, no search actions */
390 LDAPMessage *entry, /* if NULL, use dn */
391 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
394 writeptype writeproc,
397 int rdncount, /* if 0, display full DN */
402 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html_search\n", 0, 0, 0 );
404 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
405 defvals, writeproc, writeparm, eol, rdncount, opts, urlprefix ));
410 do_entry2text_search(
412 char *dn, /* if NULL, use entry */
413 char *base, /* if NULL, no search actions */
414 LDAPMessage *entry, /* if NULL, use dn */
415 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
418 writeptype writeproc,
421 int rdncount, /* if 0, display full DN */
426 int err, freedn, freetmpls, html;
427 char *buf, **fetchattrs, **vals;
429 struct ldap_disptmpl *tmpl;
430 struct timeval timeout;
432 if ( dn == NULL && entry == NULLMSG ) {
433 ld->ld_errno = LDAP_PARAM_ERROR;
434 return( ld->ld_errno );
437 html = ( urlprefix != NULL );
439 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
442 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
443 ld->ld_errno = LDAP_NO_MEMORY;
444 return( ld->ld_errno );
447 freedn = freetmpls = 0;
450 if ( tmpllist == NULL ) {
451 if (( err = ldap_init_templates( TEMPLATEFILE, &tmpllist )) != 0 ) {
452 sprintf( buf, "%sUnable to read template file %s (error %d)%s%s",
453 html ? "<!-- " : "", TEMPLATEFILE, err,
454 html ? "-->" : "", eol );
455 (*writeproc)( writeparm, buf, strlen( buf ));
461 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
464 ldap_free_templates( tmpllist );
466 return( ld->ld_errno );
472 if ( tmpllist != NULL ) {
475 if ( entry == NULL ) {
478 ocattrs[0] = OCATTRNAME;
481 if ( LDAP_IS_CLDAP( ld ))
482 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE,
483 "objectClass=*", ocattrs, 0, &ldmp, NULL );
486 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
487 "objectClass=*", ocattrs, 0, &timeout, &ldmp );
489 if ( err == LDAP_SUCCESS ) {
490 entry = ldap_first_entry( ld, ldmp );
494 if ( entry != NULL ) {
495 vals = ldap_get_values( ld, entry, OCATTRNAME );
496 tmpl = ldap_oc2template( vals, tmpllist );
497 if ( vals != NULL ) {
498 ldap_value_free( vals );
501 if ( ldmp != NULL ) {
502 ldap_msgfree( ldmp );
508 if ( tmpl == NULL ) {
511 fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
515 if ( LDAP_IS_CLDAP( ld ))
516 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
517 fetchattrs, 0, &ldmp, NULL );
520 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
521 fetchattrs, 0, &timeout, &ldmp );
526 if ( fetchattrs != NULL ) {
527 ldap_value_free( fetchattrs );
530 if ( err != LDAP_SUCCESS ||
531 ( entry = ldap_first_entry( ld, ldmp )) == NULL ) {
533 ldap_free_templates( tmpllist );
536 return( ld->ld_errno );
539 err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
540 writeproc, writeparm, eol, rdncount, opts, urlprefix );
544 ldap_free_templates( tmpllist );
546 ldap_msgfree( ldmp );
554 char *buf, /* NULL for "use internal" */
557 int labelwidth, /* 0 means use default */
558 unsigned long syntaxid,
559 writeptype writeproc,
565 Debug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
567 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
568 writeproc, writeparm, eol, rdncount, NULL ));
575 char *buf, /* NULL for "use internal" */
578 int labelwidth, /* 0 means use default */
579 unsigned long syntaxid,
580 writeptype writeproc,
587 Debug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
589 if ( urlprefix == NULL ) {
590 urlprefix = DEF_LDAP_URL_PREFIX;
593 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
594 writeproc, writeparm, eol, rdncount, urlprefix ));
601 char *buf, /* NULL for "use internal" */
604 int labelwidth, /* 0 means use default */
605 unsigned long syntaxid,
606 writeptype writeproc,
613 int i, html, writeoutval, freebuf, notascii;
614 char *p, *s, *outval;
617 if ( vals == NULL ) {
618 return( LDAP_SUCCESS );
621 html = ( urlprefix != NULL );
623 switch( LDAP_GET_SYN_TYPE( syntaxid )) {
624 case LDAP_SYN_TYPE_TEXT:
625 case LDAP_SYN_TYPE_BOOLEAN:
626 break; /* we only bother with these two types... */
628 return( LDAP_SUCCESS );
631 if ( labelwidth == 0 || labelwidth < 0 ) {
632 labelwidth = DEF_LABEL_WIDTH;
636 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
637 ld->ld_errno = LDAP_NO_MEMORY;
638 return( ld->ld_errno );
645 output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
647 for ( i = 0; vals[ i ] != NULL; ++i ) {
648 for ( p = vals[ i ]; *p != '\0'; ++p ) {
649 if ( !isascii( *p )) {
653 notascii = ( *p != '\0' );
654 outval = notascii ? "(unable to display non-ASCII text value)"
657 writeoutval = 0; /* if non-zero, write outval after switch */
660 case LDAP_SYN_CASEIGNORESTR:
664 case LDAP_SYN_RFC822ADDR:
666 strcpy( buf, "<DD><A HREF=\"mailto:" );
667 strcat_escaped( buf, outval );
668 sprintf( buf + strlen( buf ), "\">%s</A><BR>%s", outval, eol );
669 (*writeproc)( writeparm, buf, strlen( buf ));
675 case LDAP_SYN_DN: /* for now */
676 output_dn( buf, outval, labelwidth, rdncount, writeproc,
677 writeparm, eol, urlprefix );
680 case LDAP_SYN_MULTILINESTR:
681 if ( i > 0 && !html ) {
682 output_label( buf, label, labelwidth, writeproc,
683 writeparm, eol, html );
687 while (( s = strchr( s, '$' )) != NULL ) {
689 while ( isspace( *s )) {
693 sprintf( buf, "<DD>%s<BR>%s", p, eol );
695 sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
697 (*writeproc)( writeparm, buf, strlen( buf ));
704 case LDAP_SYN_BOOLEAN:
705 outval = toupper( outval[ 0 ] ) == 'T' ? "TRUE" : "FALSE";
711 outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
715 case LDAP_SYN_LABELEDURL:
716 if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
718 while ( isspace( *p )) {
722 } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
724 while ( isspace( *s )) {
734 * at this point `s' points to the label & `p' to the URL
737 sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
739 sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
740 s, eol, labelwidth + 2, " ",p , eol );
742 (*writeproc)( writeparm, buf, strlen( buf ));
746 sprintf( buf, " Can't display item type %ld%s",
748 (*writeproc)( writeparm, buf, strlen( buf ));
753 sprintf( buf, "<DD>%s<BR>%s", outval, eol );
755 sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
757 (*writeproc)( writeparm, buf, strlen( buf ));
765 return( LDAP_SUCCESS );
770 max_label_len( struct ldap_disptmpl *tmpl )
772 struct ldap_tmplitem *rowp, *colp;
777 for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
778 rowp = ldap_next_tmplrow( tmpl, rowp )) {
779 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
780 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
781 if (( len = strlen( colp->ti_label )) > maxlen ) {
792 output_label( char *buf, char *label, int width, writeptype writeproc,
793 void *writeparm, char *eol, int html )
798 sprintf( buf, "<DT><B>%s</B>", label );
800 sprintf( buf, " %s:", label );
801 p = buf + strlen( buf );
803 while ( p - buf < width ) {
811 return ((*writeproc)( writeparm, buf, strlen( buf )));
816 output_dn( char *buf, char *dn, int width, int rdncount,
817 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
822 if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
826 if ( urlprefix != NULL ) {
827 sprintf( buf, "<DD><A HREF=\"%s", urlprefix );
828 strcat_escaped( buf, dn );
829 strcat( buf, "\">" );
830 } else if ( width > 0 ) {
831 sprintf( buf, "%-*s", width, " " );
836 for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
841 strcat( buf, dnrdns[ i ] );
844 if ( urlprefix != NULL ) {
845 strcat( buf, "</A><BR>" );
848 ldap_value_free( dnrdns );
852 return ((*writeproc)( writeparm, buf, strlen( buf )));
857 #define HREF_CHAR_ACCEPTABLE( c ) (( c >= '-' && c <= '9' ) || \
858 ( c >= '@' && c <= 'Z' ) || \
860 ( c >= 'a' && c <= 'z' ))
863 strcat_escaped( char *s1, char *s2 )
866 char *hexdig = "0123456789ABCDEF";
868 p = s1 + strlen( s1 );
869 for ( q = s2; *q != '\0'; ++q ) {
870 if ( HREF_CHAR_ACCEPTABLE( *q )) {
874 *p++ = hexdig[ *q >> 4 ];
875 *p++ = hexdig[ *q & 0x0F ];
883 #define GET2BYTENUM( p ) (( *p - '0' ) * 10 + ( *(p+1) - '0' ))
886 time2text( char *ldtimestr, int dateonly )
889 char *p, *timestr, zone, *fmterr = "badly formatted time";
892 memset( (char *)&t, 0, sizeof( struct tm ));
893 if ( (int) strlen( ldtimestr ) < 13 ) {
897 for ( p = ldtimestr; p - ldtimestr < 12; ++p ) {
898 if ( !isdigit( *p )) {
904 t.tm_year = GET2BYTENUM( p ); p += 2;
905 t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
906 t.tm_mday = GET2BYTENUM( p ); p += 2;
907 t.tm_hour = GET2BYTENUM( p ); p += 2;
908 t.tm_min = GET2BYTENUM( p ); p += 2;
909 t.tm_sec = GET2BYTENUM( p ); p += 2;
911 if (( zone = *p ) == 'Z' ) { /* GMT */
912 zone = '\0'; /* no need to indicate on screen, so we make it null */
915 gmttime = gtime( &t );
916 timestr = ctime( &gmttime );
918 timestr[ strlen( timestr ) - 1 ] = zone; /* replace trailing newline */
920 strcpy( timestr + 11, timestr + 20 );
928 /* gtime.c - inverse gmtime */
930 #if !defined( MACOS ) && !defined( _WIN32 ) && !defined( DOS )
931 #include <sys/time.h>
934 /* gtime(): the inverse of localtime().
935 This routine was supplied by Mike Accetta at CMU many years ago.
938 static int dmsize[] = {
939 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
943 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
945 #define YEAR(y) ((y) >= 100 ? (y) : (y) + 1900)
949 static long gtime ( struct tm *tm )
958 register long result;
960 if ((sec = tm -> tm_sec) < 0 || sec > 59
961 || (mins = tm -> tm_min) < 0 || mins > 59
962 || (hour = tm -> tm_hour) < 0 || hour > 24
963 || (mday = tm -> tm_mday) < 1 || mday > 31
964 || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
970 year = YEAR (tm -> tm_year);
973 for (i = 1970; i < year; i++)
974 result += dysize (i);
975 if (dysize (year) == 366 && mon >= 3)
978 result += dmsize[mon - 1];
980 result = 24 * result + hour;
981 result = 60 * result + mins;
982 result = 60 * result + sec;
988 searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
989 struct ldap_tmplitem *tip, int labelwidth, int rdncount,
990 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
992 int err, lderr, i, count, html;
993 char **vals, **members;
994 char *value, *filtpattern, *attr, *selectname;
995 char *retattrs[2], filter[ 256 ];
997 struct timeval timeout;
999 html = ( urlprefix != NULL );
1001 for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
1005 return( LDAP_PARAM_ERROR );
1007 attr = tip->ti_args[ 0 ];
1008 filtpattern = tip->ti_args[ 1 ];
1009 retattrs[ 0 ] = tip->ti_args[ 2 ];
1010 retattrs[ 1 ] = NULL;
1011 selectname = tip->ti_args[ 3 ];
1014 if ( attr == NULL ) {
1016 } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
1017 return( LDAP_PARAM_ERROR );
1018 } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
1020 } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
1026 ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
1031 * if we are generating HTML, we add an HREF link that embodies this
1032 * search action as an LDAP URL, instead of actually doing the search
1035 sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
1036 if ( base != NULL ) {
1037 strcat_escaped( buf, base );
1039 strcat( buf, "??sub?" );
1040 strcat_escaped( buf, filter );
1041 sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
1042 tip->ti_label, eol );
1043 if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
1044 return( LDAP_LOCAL_ERROR );
1046 return( LDAP_SUCCESS );
1049 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1050 timeout.tv_usec = 0;
1053 if ( LDAP_IS_CLDAP( ld ))
1054 lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1058 lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1059 0, &timeout, &ldmp );
1061 if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
1062 if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
1063 if (( members = (char **)malloc( (count + 1) * sizeof(char *)))
1065 err = LDAP_NO_MEMORY;
1067 for ( i = 0, entry = ldap_first_entry( ld, ldmp );
1069 entry = ldap_next_entry( ld, entry ), ++i ) {
1070 members[ i ] = ldap_get_dn( ld, entry );
1072 members[ i ] = NULL;
1074 ldap_sort_values( ld, members, ldap_sort_strcasecmp );
1076 err = do_vals2text( ld, NULL, members, tip->ti_label,
1077 html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
1078 eol, rdncount, urlprefix );
1080 ldap_value_free( members );
1083 ldap_msgfree( ldmp );
1087 if ( vals != NULL ) {
1088 ldap_value_free( vals );
1091 return(( err == LDAP_SUCCESS ) ? lderr : err );