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"
28 static int do_entry2text LDAP_P((
29 LDAP *ld, char *buf, char *base, LDAPMessage *entry,
30 struct ldap_disptmpl *tmpl, char **defattrs, char ***defvals,
31 writeptype writeproc, void *writeparm, char *eol, int rdncount,
32 unsigned long opts, char *urlprefix ));
33 static int do_entry2text_search LDAP_P((
34 LDAP *ld, char *dn, char *base,
35 LDAPMessage *entry, struct ldap_disptmpl *tmpllist, char **defattrs,
36 char ***defvals, writeptype writeproc, void *writeparm, char *eol,
37 int rdncount, unsigned long opts, char *urlprefix ));
38 static int do_vals2text LDAP_P((
39 LDAP *ld, char *buf, char **vals, char *label,
40 int labelwidth, unsigned long syntaxid, writeptype writeproc,
41 void *writeparm, char *eol, int rdncount, char *urlprefix ));
42 static int max_label_len LDAP_P(( struct ldap_disptmpl *tmpl ));
43 static int output_label LDAP_P((
44 char *buf, char *label, int width,
45 writeptype writeproc, void *writeparm, char *eol, int html ));
46 static int output_dn LDAP_P((
47 char *buf, char *dn, int width, int rdncount,
48 writeptype writeproc, void *writeparm, char *eol, char *urlprefix ));
49 static void strcat_escaped LDAP_P(( char *s1, char *s2 ));
50 static char *time2text LDAP_P(( char *ldtimestr, int dateonly ));
51 static long gtime LDAP_P(( struct tm *tm ));
52 static int searchaction LDAP_P((
53 LDAP *ld, char *buf, char *base, LDAPMessage *entry,
54 char *dn, struct ldap_tmplitem *tip, int labelwidth, int rdncount,
55 writeptype writeproc, void *writeparm, char *eol, char *urlprefix ));
57 #define DEF_LABEL_WIDTH 15
58 #define SEARCH_TIMEOUT_SECS 120
59 #define OCATTRNAME "objectClass"
62 #define NONFATAL_LDAP_ERR( err ) ( err == LDAP_SUCCESS || \
63 err == LDAP_TIMELIMIT_EXCEEDED || err == LDAP_SIZELIMIT_EXCEEDED )
65 #define DEF_LDAP_URL_PREFIX "ldap:///"
71 char *buf, /* NULL for "use internal" */
73 struct ldap_disptmpl *tmpl,
83 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text\n", 0, 0, 0 );
85 return( do_entry2text( ld, buf, NULL, entry, tmpl, defattrs, defvals,
86 writeproc, writeparm, eol, rdncount, opts, NULL ));
95 char *buf, /* NULL for "use internal" */
97 struct ldap_disptmpl *tmpl,
100 writeptype writeproc,
109 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html\n", 0, 0, 0 );
111 if ( urlprefix == NULL ) {
112 urlprefix = DEF_LDAP_URL_PREFIX;
115 return( do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
116 writeproc, writeparm, eol, rdncount, opts, urlprefix ));
123 char *buf, /* NULL for use-internal */
124 char *base, /* used for search actions */
126 struct ldap_disptmpl *tmpl,
129 writeptype writeproc,
134 char *urlprefix /* if non-NULL, do HTML */
137 int i, err, html, show, labelwidth;
138 int freebuf, freevals;
140 struct ldap_tmplitem *rowp, *colp;
142 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
143 return( ld->ld_errno );
147 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
148 ld->ld_errno = LDAP_NO_MEMORY;
150 return( ld->ld_errno );
157 html = ( urlprefix != NULL );
161 * add HTML intro. and title
163 if (!(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
164 sprintf( buf, "<HTML>%s<HEAD>%s<TITLE>%s%s - ", eol, eol, eol,
165 ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
166 (*writeproc)( writeparm, buf, strlen( buf ));
167 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
168 sprintf( buf, "%s</TITLE>%s</HEAD>%s<BODY>%s<H3>%s - ", eol, eol,
169 eol, eol, ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
170 (*writeproc)( writeparm, buf, strlen( buf ));
171 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
172 sprintf( buf, "</H3>%s", eol );
173 (*writeproc)( writeparm, buf, strlen( buf ));
176 if (( opts & LDAP_DISP_OPT_NONLEAF ) != 0 &&
177 ( vals = ldap_explode_dn( dn, 0 )) != NULL ) {
183 sprintf( buf, "<A HREF=\"%s", urlprefix );
184 for ( i = 1; vals[ i ] != NULL; ++i ) {
186 strcat_escaped( buf, ", " );
188 strcat_escaped( buf, vals[ i ] );
190 if ( vals[ 1 ] != NULL ) {
191 untagged = strchr( vals[ 1 ], '=' );
193 untagged = "=The World";
195 sprintf( buf + strlen( buf ),
196 "%s\">Move Up To <EM>%s</EM></A>%s<BR>",
197 ( vals[ 1 ] == NULL ) ? "??one" : "",
198 ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol, eol );
199 (*writeproc)( writeparm, buf, strlen( buf ));
204 untagged = strchr( vals[ 0 ], '=' );
205 sprintf( buf, "<A HREF=\"%s", urlprefix );
206 strcat_escaped( buf, dn );
207 sprintf( buf + strlen( buf ), "??one?(!(objectClass=dsa))\">Browse Below <EM>%s</EM></A>%s%s",
208 ( untagged != NULL ) ? untagged + 1 : vals[ 0 ], eol, eol );
209 (*writeproc)( writeparm, buf, strlen( buf ));
211 ldap_value_free( vals );
214 (*writeproc)( writeparm, "<HR>", 4 ); /* horizontal rule */
216 (*writeproc)( writeparm, "\"", 1 );
217 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
218 sprintf( buf, "\"%s", eol );
219 (*writeproc)( writeparm, buf, strlen( buf ));
222 if ( tmpl != NULL && ( opts & LDAP_DISP_OPT_AUTOLABELWIDTH ) != 0 ) {
223 labelwidth = max_label_len( tmpl ) + 3;
225 labelwidth = DEF_LABEL_WIDTH;;
230 if ( tmpl == NULL ) {
235 for ( attr = ldap_first_attribute( ld, entry, &ber );
236 NONFATAL_LDAP_ERR( err ) && attr != NULL;
237 attr = ldap_next_attribute( ld, entry, ber )) {
238 if (( vals = ldap_get_values( ld, entry, attr )) == NULL ) {
240 if ( defattrs != NULL ) {
241 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
242 if ( strcasecmp( attr, defattrs[ i ] ) == 0 ) {
246 if ( defattrs[ i ] != NULL ) {
254 if ( islower( *attr )) { /* cosmetic -- upcase attr. name */
255 *attr = toupper( *attr );
258 err = do_vals2text( ld, buf, vals, attr, labelwidth,
259 LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol,
260 rdncount, urlprefix );
262 ldap_value_free( vals );
266 for ( rowp = ldap_first_tmplrow( tmpl );
267 NONFATAL_LDAP_ERR( err ) && rowp != NULLTMPLITEM;
268 rowp = ldap_next_tmplrow( tmpl, rowp )) {
269 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
270 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
272 if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
273 entry, colp->ti_attrname )) == NULL ) {
275 if ( !LDAP_IS_TMPLITEM_OPTION_SET( colp,
276 LDAP_DITEM_OPT_HIDEIFEMPTY ) && defattrs != NULL
277 && colp->ti_attrname != NULL ) {
278 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
279 if ( strcasecmp( colp->ti_attrname, defattrs[ i ] )
284 if ( defattrs[ i ] != NULL ) {
290 if ( LDAP_IS_TMPLITEM_OPTION_SET( colp,
291 LDAP_DITEM_OPT_SORTVALUES ) && vals[ 0 ] != NULL
292 && vals[ 1 ] != NULL ) {
293 ldap_sort_values( ld, vals, ldap_sort_strcasecmp );
298 * don't bother even calling do_vals2text() if no values
299 * or boolean with value false and "hide if false" option set
301 show = ( vals != NULL && vals[ 0 ] != NULL );
302 if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
303 == LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
304 colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
305 toupper( vals[ 0 ][ 0 ] ) != 'T' ) {
309 if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
310 if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
311 if ( colp->ti_attrname == NULL || ( show &&
312 toupper( vals[ 0 ][ 0 ] ) == 'T' )) {
313 err = searchaction( ld, buf, base, entry, dn, colp,
314 labelwidth, rdncount, writeproc,
315 writeparm, eol, urlprefix );
322 err = do_vals2text( ld, buf, vals, colp->ti_label,
323 labelwidth, colp->ti_syntaxid, writeproc, writeparm,
324 eol, rdncount, urlprefix );
328 ldap_value_free( vals );
334 if ( html && !(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
335 sprintf( buf, "</BODY>%s</HTML>%s", eol, eol );
336 (*writeproc)( writeparm, buf, strlen( buf ));
349 ldap_entry2text_search(
351 char *dn, /* if NULL, use entry */
352 char *base, /* if NULL, no search actions */
353 LDAPMessage *entry, /* if NULL, use dn */
354 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
357 writeptype writeproc,
360 int rdncount, /* if 0, display full DN */
364 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text_search\n", 0, 0, 0 );
366 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
367 defvals, writeproc, writeparm, eol, rdncount, opts, NULL ));
373 ldap_entry2html_search(
375 char *dn, /* if NULL, use entry */
376 char *base, /* if NULL, no search actions */
377 LDAPMessage *entry, /* if NULL, use dn */
378 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
381 writeptype writeproc,
384 int rdncount, /* if 0, display full DN */
389 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html_search\n", 0, 0, 0 );
391 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
392 defvals, writeproc, writeparm, eol, rdncount, opts, urlprefix ));
397 do_entry2text_search(
399 char *dn, /* if NULL, use entry */
400 char *base, /* if NULL, no search actions */
401 LDAPMessage *entry, /* if NULL, use dn */
402 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
405 writeptype writeproc,
408 int rdncount, /* if 0, display full DN */
413 int err, freedn, freetmpls, html;
414 char *buf, **fetchattrs, **vals;
416 struct ldap_disptmpl *tmpl;
417 struct timeval timeout;
419 if ( dn == NULL && entry == NULLMSG ) {
420 ld->ld_errno = LDAP_PARAM_ERROR;
421 return( ld->ld_errno );
424 html = ( urlprefix != NULL );
426 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
429 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
430 ld->ld_errno = LDAP_NO_MEMORY;
431 return( ld->ld_errno );
434 freedn = freetmpls = 0;
437 if ( tmpllist == NULL ) {
438 if (( err = ldap_init_templates( TEMPLATEFILE, &tmpllist )) != 0 ) {
439 sprintf( buf, "%sUnable to read template file %s (error %d)%s%s",
440 html ? "<!-- " : "", TEMPLATEFILE, err,
441 html ? "-->" : "", eol );
442 (*writeproc)( writeparm, buf, strlen( buf ));
448 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
451 ldap_free_templates( tmpllist );
453 return( ld->ld_errno );
459 if ( tmpllist != NULL ) {
462 if ( entry == NULL ) {
465 ocattrs[0] = OCATTRNAME;
467 #ifdef LDAP_CONNECTIONLESS
468 if ( LDAP_IS_CLDAP( ld ))
469 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE,
470 "objectClass=*", ocattrs, 0, &ldmp, NULL );
472 #endif /* LDAP_CONNECTIONLESS */
473 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
474 "objectClass=*", ocattrs, 0, &timeout, &ldmp );
476 if ( err == LDAP_SUCCESS ) {
477 entry = ldap_first_entry( ld, ldmp );
481 if ( entry != NULL ) {
482 vals = ldap_get_values( ld, entry, OCATTRNAME );
483 tmpl = ldap_oc2template( vals, tmpllist );
484 if ( vals != NULL ) {
485 ldap_value_free( vals );
488 if ( ldmp != NULL ) {
489 ldap_msgfree( ldmp );
495 if ( tmpl == NULL ) {
498 fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
501 #ifdef LDAP_CONNECTIONLESS
502 if ( LDAP_IS_CLDAP( ld ))
503 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
504 fetchattrs, 0, &ldmp, NULL );
506 #endif /* LDAP_CONNECTIONLESS */
507 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
508 fetchattrs, 0, &timeout, &ldmp );
513 if ( fetchattrs != NULL ) {
514 ldap_value_free( fetchattrs );
517 if ( err != LDAP_SUCCESS ||
518 ( entry = ldap_first_entry( ld, ldmp )) == NULL ) {
520 ldap_free_templates( tmpllist );
523 return( ld->ld_errno );
526 err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
527 writeproc, writeparm, eol, rdncount, opts, urlprefix );
531 ldap_free_templates( tmpllist );
533 ldap_msgfree( ldmp );
541 char *buf, /* NULL for "use internal" */
544 int labelwidth, /* 0 means use default */
545 unsigned long syntaxid,
546 writeptype writeproc,
552 Debug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
554 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
555 writeproc, writeparm, eol, rdncount, NULL ));
562 char *buf, /* NULL for "use internal" */
565 int labelwidth, /* 0 means use default */
566 unsigned long syntaxid,
567 writeptype writeproc,
574 Debug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
576 if ( urlprefix == NULL ) {
577 urlprefix = DEF_LDAP_URL_PREFIX;
580 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
581 writeproc, writeparm, eol, rdncount, urlprefix ));
588 char *buf, /* NULL for "use internal" */
591 int labelwidth, /* 0 means use default */
592 unsigned long syntaxid,
593 writeptype writeproc,
600 int i, html, writeoutval, freebuf, notascii;
601 char *p, *s, *outval;
604 if ( vals == NULL ) {
605 return( LDAP_SUCCESS );
608 html = ( urlprefix != NULL );
610 switch( LDAP_GET_SYN_TYPE( syntaxid )) {
611 case LDAP_SYN_TYPE_TEXT:
612 case LDAP_SYN_TYPE_BOOLEAN:
613 break; /* we only bother with these two types... */
615 return( LDAP_SUCCESS );
618 if ( labelwidth == 0 || labelwidth < 0 ) {
619 labelwidth = DEF_LABEL_WIDTH;
623 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
624 ld->ld_errno = LDAP_NO_MEMORY;
625 return( ld->ld_errno );
632 output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
634 for ( i = 0; vals[ i ] != NULL; ++i ) {
635 for ( p = vals[ i ]; *p != '\0'; ++p ) {
636 if ( !isascii( *p )) {
640 notascii = ( *p != '\0' );
641 outval = notascii ? "(unable to display non-ASCII text value)"
644 writeoutval = 0; /* if non-zero, write outval after switch */
647 case LDAP_SYN_CASEIGNORESTR:
651 case LDAP_SYN_RFC822ADDR:
653 strcpy( buf, "<DD><A HREF=\"mailto:" );
654 strcat_escaped( buf, outval );
655 sprintf( buf + strlen( buf ), "\">%s</A><BR>%s", outval, eol );
656 (*writeproc)( writeparm, buf, strlen( buf ));
662 case LDAP_SYN_DN: /* for now */
663 output_dn( buf, outval, labelwidth, rdncount, writeproc,
664 writeparm, eol, urlprefix );
667 case LDAP_SYN_MULTILINESTR:
668 if ( i > 0 && !html ) {
669 output_label( buf, label, labelwidth, writeproc,
670 writeparm, eol, html );
674 while (( s = strchr( s, '$' )) != NULL ) {
676 while ( isspace( *s )) {
680 sprintf( buf, "<DD>%s<BR>%s", p, eol );
682 sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
684 (*writeproc)( writeparm, buf, strlen( buf ));
691 case LDAP_SYN_BOOLEAN:
692 outval = toupper( outval[ 0 ] ) == 'T' ? "TRUE" : "FALSE";
698 outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
702 case LDAP_SYN_LABELEDURL:
703 if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
705 while ( isspace( *p )) {
709 } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
711 while ( isspace( *s )) {
721 * at this point `s' points to the label & `p' to the URL
724 sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
726 sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
727 s, eol, labelwidth + 2, " ",p , eol );
729 (*writeproc)( writeparm, buf, strlen( buf ));
733 sprintf( buf, " Can't display item type %ld%s",
735 (*writeproc)( writeparm, buf, strlen( buf ));
740 sprintf( buf, "<DD>%s<BR>%s", outval, eol );
742 sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
744 (*writeproc)( writeparm, buf, strlen( buf ));
752 return( LDAP_SUCCESS );
757 max_label_len( struct ldap_disptmpl *tmpl )
759 struct ldap_tmplitem *rowp, *colp;
764 for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
765 rowp = ldap_next_tmplrow( tmpl, rowp )) {
766 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
767 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
768 if (( len = strlen( colp->ti_label )) > maxlen ) {
779 output_label( char *buf, char *label, int width, writeptype writeproc,
780 void *writeparm, char *eol, int html )
785 sprintf( buf, "<DT><B>%s</B>", label );
787 sprintf( buf, " %s:", label );
788 p = buf + strlen( buf );
790 while ( p - buf < width ) {
798 return ((*writeproc)( writeparm, buf, strlen( buf )));
803 output_dn( char *buf, char *dn, int width, int rdncount,
804 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
809 if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
813 if ( urlprefix != NULL ) {
814 sprintf( buf, "<DD><A HREF=\"%s", urlprefix );
815 strcat_escaped( buf, dn );
816 strcat( buf, "\">" );
817 } else if ( width > 0 ) {
818 sprintf( buf, "%-*s", width, " " );
823 for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
828 strcat( buf, dnrdns[ i ] );
831 if ( urlprefix != NULL ) {
832 strcat( buf, "</A><BR>" );
835 ldap_value_free( dnrdns );
839 return ((*writeproc)( writeparm, buf, strlen( buf )));
844 #define HREF_CHAR_ACCEPTABLE( c ) (( c >= '-' && c <= '9' ) || \
845 ( c >= '@' && c <= 'Z' ) || \
847 ( c >= 'a' && c <= 'z' ))
850 strcat_escaped( char *s1, char *s2 )
853 char *hexdig = "0123456789ABCDEF";
855 p = s1 + strlen( s1 );
856 for ( q = s2; *q != '\0'; ++q ) {
857 if ( HREF_CHAR_ACCEPTABLE( *q )) {
861 *p++ = hexdig[ *q >> 4 ];
862 *p++ = hexdig[ *q & 0x0F ];
870 #define GET2BYTENUM( p ) (( *p - '0' ) * 10 + ( *(p+1) - '0' ))
873 time2text( char *ldtimestr, int dateonly )
876 char *p, *timestr, zone, *fmterr = "badly formatted time";
879 memset( (char *)&t, 0, sizeof( struct tm ));
880 if ( (int) strlen( ldtimestr ) < 13 ) {
884 for ( p = ldtimestr; p - ldtimestr < 12; ++p ) {
885 if ( !isdigit( *p )) {
891 t.tm_year = GET2BYTENUM( p ); p += 2;
892 t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
893 t.tm_mday = GET2BYTENUM( p ); p += 2;
894 t.tm_hour = GET2BYTENUM( p ); p += 2;
895 t.tm_min = GET2BYTENUM( p ); p += 2;
896 t.tm_sec = GET2BYTENUM( p ); p += 2;
898 if (( zone = *p ) == 'Z' ) { /* GMT */
899 zone = '\0'; /* no need to indicate on screen, so we make it null */
902 gmttime = gtime( &t );
903 timestr = ctime( &gmttime );
905 timestr[ strlen( timestr ) - 1 ] = zone; /* replace trailing newline */
907 strcpy( timestr + 11, timestr + 20 );
915 /* gtime.c - inverse gmtime */
916 /* gtime(): the inverse of localtime().
917 This routine was supplied by Mike Accetta at CMU many years ago.
920 static int dmsize[] = {
921 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
925 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
927 #define YEAR(y) ((y) >= 100 ? (y) : (y) + 1900)
931 static long gtime ( struct tm *tm )
940 register long result;
942 if ((sec = tm -> tm_sec) < 0 || sec > 59
943 || (mins = tm -> tm_min) < 0 || mins > 59
944 || (hour = tm -> tm_hour) < 0 || hour > 24
945 || (mday = tm -> tm_mday) < 1 || mday > 31
946 || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
952 year = YEAR (tm -> tm_year);
955 for (i = 1970; i < year; i++)
956 result += dysize (i);
957 if (dysize (year) == 366 && mon >= 3)
960 result += dmsize[mon - 1];
962 result = 24 * result + hour;
963 result = 60 * result + mins;
964 result = 60 * result + sec;
970 searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
971 struct ldap_tmplitem *tip, int labelwidth, int rdncount,
972 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
974 int err = 0, lderr, i, count, html;
975 char **vals, **members;
976 char *value, *filtpattern, *attr, *selectname;
977 char *retattrs[2], filter[ 256 ];
979 struct timeval timeout;
981 html = ( urlprefix != NULL );
983 for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
987 return( LDAP_PARAM_ERROR );
989 attr = tip->ti_args[ 0 ];
990 filtpattern = tip->ti_args[ 1 ];
991 retattrs[ 0 ] = tip->ti_args[ 2 ];
992 retattrs[ 1 ] = NULL;
993 selectname = tip->ti_args[ 3 ];
996 if ( attr == NULL ) {
998 } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
999 return( LDAP_PARAM_ERROR );
1000 } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
1002 } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
1008 ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
1013 * if we are generating HTML, we add an HREF link that embodies this
1014 * search action as an LDAP URL, instead of actually doing the search
1017 sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
1018 if ( base != NULL ) {
1019 strcat_escaped( buf, base );
1021 strcat( buf, "??sub?" );
1022 strcat_escaped( buf, filter );
1023 sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
1024 tip->ti_label, eol );
1025 if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
1026 return( LDAP_LOCAL_ERROR );
1028 return( LDAP_SUCCESS );
1031 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1032 timeout.tv_usec = 0;
1034 #ifdef LDAP_CONNECTIONLESS
1035 if ( LDAP_IS_CLDAP( ld ))
1036 lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1039 #endif /* LDAP_CONNECTIONLESS */
1040 lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1041 0, &timeout, &ldmp );
1043 if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
1044 if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
1045 if (( members = (char **)malloc( (count + 1) * sizeof(char *)))
1047 err = LDAP_NO_MEMORY;
1049 for ( i = 0, entry = ldap_first_entry( ld, ldmp );
1051 entry = ldap_next_entry( ld, entry ), ++i ) {
1052 members[ i ] = ldap_get_dn( ld, entry );
1054 members[ i ] = NULL;
1056 ldap_sort_values( ld, members, ldap_sort_strcasecmp );
1058 err = do_vals2text( ld, NULL, members, tip->ti_label,
1059 html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
1060 eol, rdncount, urlprefix );
1062 ldap_value_free( members );
1065 ldap_msgfree( ldmp );
1069 if ( vals != NULL ) {
1070 ldap_value_free( vals );
1073 return(( err == LDAP_SUCCESS ) ? lderr : err );