2 * tmplout.c: display template library output routines for LDAP clients
3 * 12 April 1994 by Mark C Smith
20 #include <sys/types.h>
26 #include <sys/socket.h>
33 #include "ldapconfig.h"
36 static int do_entry2text( LDAP *ld, char *buf, char *base, LDAPMessage *entry,
37 struct ldap_disptmpl *tmpl, char **defattrs, char ***defvals,
38 writeptype writeproc, void *writeparm, char *eol, int rdncount,
39 unsigned long opts, char *urlprefix );
40 static int do_entry2text_search( LDAP *ld, char *dn, char *base,
41 LDAPMessage *entry, struct ldap_disptmpl *tmpllist, char **defattrs,
42 char ***defvals, writeptype writeproc, void *writeparm, char *eol,
43 int rdncount, unsigned long opts, char *urlprefix );
44 static int do_vals2text( LDAP *ld, char *buf, char **vals, char *label,
45 int labelwidth, unsigned long syntaxid, writeptype writeproc,
46 void *writeparm, char *eol, int rdncount, char *urlprefix );
47 static int max_label_len( struct ldap_disptmpl *tmpl );
48 static int output_label( char *buf, char *label, int width,
49 writeptype writeproc, void *writeparm, char *eol, int html );
50 static int output_dn( char *buf, char *dn, int width, int rdncount,
51 writeptype writeproc, void *writeparm, char *eol, char *urlprefix );
52 static void strcat_escaped( char *s1, char *s2 );
53 static char *time2text( char *ldtimestr, int dateonly );
54 static long gtime( struct tm *tm );
55 static int searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry,
56 char *dn, struct ldap_tmplitem *tip, int labelwidth, int rdncount,
57 writeptype writeproc, void *writeparm, char *eol, char *urlprefix );
58 #else /* NEEDPROTOS */
59 static int do_entry2text();
60 static int do_entry2text_search();
61 static int do_vals2text();
62 static int max_label_len();
63 static int output_label();
64 static int output_dn();
65 static void strcat_escaped();
66 static char *time2text();
68 static int searchaction();
69 #endif /* NEEDPROTOS */
71 #define DEF_LABEL_WIDTH 15
72 #define SEARCH_TIMEOUT_SECS 120
73 #define OCATTRNAME "objectClass"
76 #define NONFATAL_LDAP_ERR( err ) ( err == LDAP_SUCCESS || \
77 err == LDAP_TIMELIMIT_EXCEEDED || err == LDAP_SIZELIMIT_EXCEEDED )
79 #define DEF_LDAP_URL_PREFIX "ldap:///"
85 char *buf, /* NULL for "use internal" */
87 struct ldap_disptmpl *tmpl,
97 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text\n", 0, 0, 0 );
99 return( do_entry2text( ld, buf, NULL, entry, tmpl, defattrs, defvals,
100 writeproc, writeparm, eol, rdncount, opts, NULL ));
109 char *buf, /* NULL for "use internal" */
111 struct ldap_disptmpl *tmpl,
114 writeptype writeproc,
123 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html\n", 0, 0, 0 );
125 if ( urlprefix == NULL ) {
126 urlprefix = DEF_LDAP_URL_PREFIX;
129 return( do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
130 writeproc, writeparm, eol, rdncount, opts, urlprefix ));
137 char *buf, /* NULL for use-internal */
138 char *base, /* used for search actions */
140 struct ldap_disptmpl *tmpl,
143 writeptype writeproc,
148 char *urlprefix /* if non-NULL, do HTML */
151 int i, err, html, show, labelwidth;
152 int freebuf, freevals;
154 struct ldap_tmplitem *rowp, *colp;
156 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
157 return( ld->ld_errno );
161 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
162 ld->ld_errno = LDAP_NO_MEMORY;
164 return( ld->ld_errno );
171 html = ( urlprefix != NULL );
175 * add HTML intro. and title
177 if (!(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
178 sprintf( buf, "<HTML>%s<HEAD>%s<TITLE>%s%s - ", eol, eol, eol,
179 ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
180 (*writeproc)( writeparm, buf, strlen( buf ));
181 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
182 sprintf( buf, "%s</TITLE>%s</HEAD>%s<BODY>%s<H3>%s - ", eol, eol,
183 eol, eol, ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
184 (*writeproc)( writeparm, buf, strlen( buf ));
185 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
186 sprintf( buf, "</H3>%s", eol );
187 (*writeproc)( writeparm, buf, strlen( buf ));
190 if (( opts & LDAP_DISP_OPT_NONLEAF ) != 0 &&
191 ( vals = ldap_explode_dn( dn, 0 )) != NULL ) {
197 sprintf( buf, "<A HREF=\"%s", urlprefix );
198 for ( i = 1; vals[ i ] != NULL; ++i ) {
200 strcat_escaped( buf, ", " );
202 strcat_escaped( buf, vals[ i ] );
204 if ( vals[ 1 ] != NULL ) {
205 untagged = strchr( vals[ 1 ], '=' );
207 untagged = "=The World";
209 sprintf( buf + strlen( buf ),
210 "%s\">Move Up To <EM>%s</EM></A>%s<BR>",
211 ( vals[ 1 ] == NULL ) ? "??one" : "",
212 ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol, eol );
213 (*writeproc)( writeparm, buf, strlen( buf ));
218 untagged = strchr( vals[ 0 ], '=' );
219 sprintf( buf, "<A HREF=\"%s", urlprefix );
220 strcat_escaped( buf, dn );
221 sprintf( buf + strlen( buf ), "??one?(!(objectClass=dsa))\">Browse Below <EM>%s</EM></A>%s%s",
222 ( untagged != NULL ) ? untagged + 1 : vals[ 0 ], eol, eol );
223 (*writeproc)( writeparm, buf, strlen( buf ));
225 ldap_value_free( vals );
228 (*writeproc)( writeparm, "<HR>", 4 ); /* horizontal rule */
230 (*writeproc)( writeparm, "\"", 1 );
231 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
232 sprintf( buf, "\"%s", eol );
233 (*writeproc)( writeparm, buf, strlen( buf ));
236 if ( tmpl != NULL && ( opts & LDAP_DISP_OPT_AUTOLABELWIDTH ) != 0 ) {
237 labelwidth = max_label_len( tmpl ) + 3;
239 labelwidth = DEF_LABEL_WIDTH;;
244 if ( tmpl == NULL ) {
249 for ( attr = ldap_first_attribute( ld, entry, &ber );
250 NONFATAL_LDAP_ERR( err ) && attr != NULL;
251 attr = ldap_next_attribute( ld, entry, ber )) {
252 if (( vals = ldap_get_values( ld, entry, attr )) == NULL ) {
254 if ( defattrs != NULL ) {
255 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
256 if ( strcasecmp( attr, defattrs[ i ] ) == 0 ) {
260 if ( defattrs[ i ] != NULL ) {
268 if ( islower( *attr )) { /* cosmetic -- upcase attr. name */
269 *attr = toupper( *attr );
272 err = do_vals2text( ld, buf, vals, attr, labelwidth,
273 LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol,
274 rdncount, urlprefix );
276 ldap_value_free( vals );
280 for ( rowp = ldap_first_tmplrow( tmpl );
281 NONFATAL_LDAP_ERR( err ) && rowp != NULLTMPLITEM;
282 rowp = ldap_next_tmplrow( tmpl, rowp )) {
283 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
284 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
286 if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
287 entry, colp->ti_attrname )) == NULL ) {
289 if ( !LDAP_IS_TMPLITEM_OPTION_SET( colp,
290 LDAP_DITEM_OPT_HIDEIFEMPTY ) && defattrs != NULL
291 && colp->ti_attrname != NULL ) {
292 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
293 if ( strcasecmp( colp->ti_attrname, defattrs[ i ] )
298 if ( defattrs[ i ] != NULL ) {
304 if ( LDAP_IS_TMPLITEM_OPTION_SET( colp,
305 LDAP_DITEM_OPT_SORTVALUES ) && vals[ 0 ] != NULL
306 && vals[ 1 ] != NULL ) {
307 ldap_sort_values( ld, vals, ldap_sort_strcasecmp );
312 * don't bother even calling do_vals2text() if no values
313 * or boolean with value false and "hide if false" option set
315 show = ( vals != NULL && vals[ 0 ] != NULL );
316 if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
317 == LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
318 colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
319 toupper( vals[ 0 ][ 0 ] ) != 'T' ) {
323 if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
324 if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
325 if ( colp->ti_attrname == NULL || ( show &&
326 toupper( vals[ 0 ][ 0 ] ) == 'T' )) {
327 err = searchaction( ld, buf, base, entry, dn, colp,
328 labelwidth, rdncount, writeproc,
329 writeparm, eol, urlprefix );
336 err = do_vals2text( ld, buf, vals, colp->ti_label,
337 labelwidth, colp->ti_syntaxid, writeproc, writeparm,
338 eol, rdncount, urlprefix );
342 ldap_value_free( vals );
348 if ( html && !(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
349 sprintf( buf, "</BODY>%s</HTML>%s", eol, eol );
350 (*writeproc)( writeparm, buf, strlen( buf ));
363 ldap_entry2text_search(
365 char *dn, /* if NULL, use entry */
366 char *base, /* if NULL, no search actions */
367 LDAPMessage *entry, /* if NULL, use dn */
368 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
371 writeptype writeproc,
374 int rdncount, /* if 0, display full DN */
378 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text_search\n", 0, 0, 0 );
380 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
381 defvals, writeproc, writeparm, eol, rdncount, opts, NULL ));
387 ldap_entry2html_search(
389 char *dn, /* if NULL, use entry */
390 char *base, /* if NULL, no search actions */
391 LDAPMessage *entry, /* if NULL, use dn */
392 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
395 writeptype writeproc,
398 int rdncount, /* if 0, display full DN */
403 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html_search\n", 0, 0, 0 );
405 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
406 defvals, writeproc, writeparm, eol, rdncount, opts, urlprefix ));
411 do_entry2text_search(
413 char *dn, /* if NULL, use entry */
414 char *base, /* if NULL, no search actions */
415 LDAPMessage *entry, /* if NULL, use dn */
416 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
419 writeptype writeproc,
422 int rdncount, /* if 0, display full DN */
427 int err, freedn, freetmpls, html;
428 char *buf, **fetchattrs, **vals;
430 struct ldap_disptmpl *tmpl;
431 struct timeval timeout;
433 if ( dn == NULL && entry == NULLMSG ) {
434 ld->ld_errno = LDAP_PARAM_ERROR;
435 return( ld->ld_errno );
438 html = ( urlprefix != NULL );
440 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
443 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
444 ld->ld_errno = LDAP_NO_MEMORY;
445 return( ld->ld_errno );
448 freedn = freetmpls = 0;
451 if ( tmpllist == NULL ) {
452 if (( err = ldap_init_templates( TEMPLATEFILE, &tmpllist )) != 0 ) {
453 sprintf( buf, "%sUnable to read template file %s (error %d)%s%s",
454 html ? "<!-- " : "", TEMPLATEFILE, err,
455 html ? "-->" : "", eol );
456 (*writeproc)( writeparm, buf, strlen( buf ));
462 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
465 ldap_free_templates( tmpllist );
467 return( ld->ld_errno );
473 if ( tmpllist != NULL ) {
476 if ( entry == NULL ) {
479 ocattrs[0] = OCATTRNAME;
482 if ( LDAP_IS_CLDAP( ld ))
483 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE,
484 "objectClass=*", ocattrs, 0, &ldmp, NULL );
487 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
488 "objectClass=*", ocattrs, 0, &timeout, &ldmp );
490 if ( err == LDAP_SUCCESS ) {
491 entry = ldap_first_entry( ld, ldmp );
495 if ( entry != NULL ) {
496 vals = ldap_get_values( ld, entry, OCATTRNAME );
497 tmpl = ldap_oc2template( vals, tmpllist );
498 if ( vals != NULL ) {
499 ldap_value_free( vals );
502 if ( ldmp != NULL ) {
503 ldap_msgfree( ldmp );
509 if ( tmpl == NULL ) {
512 fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
516 if ( LDAP_IS_CLDAP( ld ))
517 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
518 fetchattrs, 0, &ldmp, NULL );
521 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
522 fetchattrs, 0, &timeout, &ldmp );
527 if ( fetchattrs != NULL ) {
528 ldap_value_free( fetchattrs );
531 if ( err != LDAP_SUCCESS ||
532 ( entry = ldap_first_entry( ld, ldmp )) == NULL ) {
534 ldap_free_templates( tmpllist );
537 return( ld->ld_errno );
540 err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
541 writeproc, writeparm, eol, rdncount, opts, urlprefix );
545 ldap_free_templates( tmpllist );
547 ldap_msgfree( ldmp );
555 char *buf, /* NULL for "use internal" */
558 int labelwidth, /* 0 means use default */
559 unsigned long syntaxid,
560 writeptype writeproc,
566 Debug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
568 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
569 writeproc, writeparm, eol, rdncount, NULL ));
576 char *buf, /* NULL for "use internal" */
579 int labelwidth, /* 0 means use default */
580 unsigned long syntaxid,
581 writeptype writeproc,
588 Debug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
590 if ( urlprefix == NULL ) {
591 urlprefix = DEF_LDAP_URL_PREFIX;
594 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
595 writeproc, writeparm, eol, rdncount, urlprefix ));
602 char *buf, /* NULL for "use internal" */
605 int labelwidth, /* 0 means use default */
606 unsigned long syntaxid,
607 writeptype writeproc,
614 int i, html, writeoutval, freebuf, notascii;
615 char *p, *s, *outval;
618 if ( vals == NULL ) {
619 return( LDAP_SUCCESS );
622 html = ( urlprefix != NULL );
624 switch( LDAP_GET_SYN_TYPE( syntaxid )) {
625 case LDAP_SYN_TYPE_TEXT:
626 case LDAP_SYN_TYPE_BOOLEAN:
627 break; /* we only bother with these two types... */
629 return( LDAP_SUCCESS );
632 if ( labelwidth == 0 || labelwidth < 0 ) {
633 labelwidth = DEF_LABEL_WIDTH;
637 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
638 ld->ld_errno = LDAP_NO_MEMORY;
639 return( ld->ld_errno );
646 output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
648 for ( i = 0; vals[ i ] != NULL; ++i ) {
649 for ( p = vals[ i ]; *p != '\0'; ++p ) {
650 if ( !isascii( *p )) {
654 notascii = ( *p != '\0' );
655 outval = notascii ? "(unable to display non-ASCII text value)"
658 writeoutval = 0; /* if non-zero, write outval after switch */
661 case LDAP_SYN_CASEIGNORESTR:
665 case LDAP_SYN_RFC822ADDR:
667 strcpy( buf, "<DD><A HREF=\"mailto:" );
668 strcat_escaped( buf, outval );
669 sprintf( buf + strlen( buf ), "\">%s</A><BR>%s", outval, eol );
670 (*writeproc)( writeparm, buf, strlen( buf ));
676 case LDAP_SYN_DN: /* for now */
677 output_dn( buf, outval, labelwidth, rdncount, writeproc,
678 writeparm, eol, urlprefix );
681 case LDAP_SYN_MULTILINESTR:
682 if ( i > 0 && !html ) {
683 output_label( buf, label, labelwidth, writeproc,
684 writeparm, eol, html );
688 while (( s = strchr( s, '$' )) != NULL ) {
690 while ( isspace( *s )) {
694 sprintf( buf, "<DD>%s<BR>%s", p, eol );
696 sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
698 (*writeproc)( writeparm, buf, strlen( buf ));
705 case LDAP_SYN_BOOLEAN:
706 outval = toupper( outval[ 0 ] ) == 'T' ? "TRUE" : "FALSE";
712 outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
716 case LDAP_SYN_LABELEDURL:
717 if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
719 while ( isspace( *p )) {
723 } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
725 while ( isspace( *s )) {
735 * at this point `s' points to the label & `p' to the URL
738 sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
740 sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
741 s, eol, labelwidth + 2, " ",p , eol );
743 (*writeproc)( writeparm, buf, strlen( buf ));
747 sprintf( buf, " Can't display item type %ld%s",
749 (*writeproc)( writeparm, buf, strlen( buf ));
754 sprintf( buf, "<DD>%s<BR>%s", outval, eol );
756 sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
758 (*writeproc)( writeparm, buf, strlen( buf ));
766 return( LDAP_SUCCESS );
771 max_label_len( struct ldap_disptmpl *tmpl )
773 struct ldap_tmplitem *rowp, *colp;
778 for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
779 rowp = ldap_next_tmplrow( tmpl, rowp )) {
780 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
781 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
782 if (( len = strlen( colp->ti_label )) > maxlen ) {
793 output_label( char *buf, char *label, int width, writeptype writeproc,
794 void *writeparm, char *eol, int html )
799 sprintf( buf, "<DT><B>%s</B>", label );
801 sprintf( buf, " %s:", label );
802 p = buf + strlen( buf );
804 while ( p - buf < width ) {
812 return ((*writeproc)( writeparm, buf, strlen( buf )));
817 output_dn( char *buf, char *dn, int width, int rdncount,
818 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
823 if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
827 if ( urlprefix != NULL ) {
828 sprintf( buf, "<DD><A HREF=\"%s", urlprefix );
829 strcat_escaped( buf, dn );
830 strcat( buf, "\">" );
831 } else if ( width > 0 ) {
832 sprintf( buf, "%-*s", width, " " );
837 for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
842 strcat( buf, dnrdns[ i ] );
845 if ( urlprefix != NULL ) {
846 strcat( buf, "</A><BR>" );
849 ldap_value_free( dnrdns );
853 return ((*writeproc)( writeparm, buf, strlen( buf )));
858 #define HREF_CHAR_ACCEPTABLE( c ) (( c >= '-' && c <= '9' ) || \
859 ( c >= '@' && c <= 'Z' ) || \
861 ( c >= 'a' && c <= 'z' ))
864 strcat_escaped( char *s1, char *s2 )
867 char *hexdig = "0123456789ABCDEF";
869 p = s1 + strlen( s1 );
870 for ( q = s2; *q != '\0'; ++q ) {
871 if ( HREF_CHAR_ACCEPTABLE( *q )) {
875 *p++ = hexdig[ *q >> 4 ];
876 *p++ = hexdig[ *q & 0x0F ];
884 #define GET2BYTENUM( p ) (( *p - '0' ) * 10 + ( *(p+1) - '0' ))
887 time2text( char *ldtimestr, int dateonly )
890 char *p, *timestr, zone, *fmterr = "badly formatted time";
893 memset( (char *)&t, 0, sizeof( struct tm ));
894 if ( (int) strlen( ldtimestr ) < 13 ) {
898 for ( p = ldtimestr; p - ldtimestr < 12; ++p ) {
899 if ( !isdigit( *p )) {
905 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 strcpy( timestr + 11, timestr + 20 );
929 /* gtime.c - inverse gmtime */
931 #if !defined( MACOS ) && !defined( _WIN32 ) && !defined( DOS )
932 #include <sys/time.h>
935 /* gtime(): the inverse of localtime().
936 This routine was supplied by Mike Accetta at CMU many years ago.
939 static int dmsize[] = {
940 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
944 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
946 #define YEAR(y) ((y) >= 100 ? (y) : (y) + 1900)
950 static long gtime ( struct tm *tm )
959 register long result;
961 if ((sec = tm -> tm_sec) < 0 || sec > 59
962 || (mins = tm -> tm_min) < 0 || mins > 59
963 || (hour = tm -> tm_hour) < 0 || hour > 24
964 || (mday = tm -> tm_mday) < 1 || mday > 31
965 || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
971 year = YEAR (tm -> tm_year);
974 for (i = 1970; i < year; i++)
975 result += dysize (i);
976 if (dysize (year) == 366 && mon >= 3)
979 result += dmsize[mon - 1];
981 result = 24 * result + hour;
982 result = 60 * result + mins;
983 result = 60 * result + sec;
989 searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
990 struct ldap_tmplitem *tip, int labelwidth, int rdncount,
991 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
993 int err = 0, lderr, i, count, html;
994 char **vals, **members;
995 char *value, *filtpattern, *attr, *selectname;
996 char *retattrs[2], filter[ 256 ];
998 struct timeval timeout;
1000 html = ( urlprefix != NULL );
1002 for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
1006 return( LDAP_PARAM_ERROR );
1008 attr = tip->ti_args[ 0 ];
1009 filtpattern = tip->ti_args[ 1 ];
1010 retattrs[ 0 ] = tip->ti_args[ 2 ];
1011 retattrs[ 1 ] = NULL;
1012 selectname = tip->ti_args[ 3 ];
1015 if ( attr == NULL ) {
1017 } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
1018 return( LDAP_PARAM_ERROR );
1019 } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
1021 } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
1027 ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
1032 * if we are generating HTML, we add an HREF link that embodies this
1033 * search action as an LDAP URL, instead of actually doing the search
1036 sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
1037 if ( base != NULL ) {
1038 strcat_escaped( buf, base );
1040 strcat( buf, "??sub?" );
1041 strcat_escaped( buf, filter );
1042 sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
1043 tip->ti_label, eol );
1044 if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
1045 return( LDAP_LOCAL_ERROR );
1047 return( LDAP_SUCCESS );
1050 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1051 timeout.tv_usec = 0;
1054 if ( LDAP_IS_CLDAP( ld ))
1055 lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1059 lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1060 0, &timeout, &ldmp );
1062 if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
1063 if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
1064 if (( members = (char **)malloc( (count + 1) * sizeof(char *)))
1066 err = LDAP_NO_MEMORY;
1068 for ( i = 0, entry = ldap_first_entry( ld, ldmp );
1070 entry = ldap_next_entry( ld, entry ), ++i ) {
1071 members[ i ] = ldap_get_dn( ld, entry );
1073 members[ i ] = NULL;
1075 ldap_sort_values( ld, members, ldap_sort_strcasecmp );
1077 err = do_vals2text( ld, NULL, members, tip->ti_label,
1078 html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
1079 eol, rdncount, urlprefix );
1081 ldap_value_free( members );
1084 ldap_msgfree( ldmp );
1088 if ( vals != NULL ) {
1089 ldap_value_free( vals );
1092 return(( err == LDAP_SUCCESS ) ? lderr : err );