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/string.h>
19 #ifdef HAVE_SYS_FILE_H
30 #include "ldapconfig.h"
33 static int do_entry2text LDAP_P((
34 LDAP *ld, char *buf, char *base, LDAPMessage *entry,
35 struct ldap_disptmpl *tmpl, char **defattrs, char ***defvals,
36 writeptype writeproc, void *writeparm, char *eol, int rdncount,
37 unsigned long opts, char *urlprefix ));
38 static int do_entry2text_search LDAP_P((
39 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_P((
44 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 LDAP_P(( struct ldap_disptmpl *tmpl ));
48 static int output_label LDAP_P((
49 char *buf, char *label, int width,
50 writeptype writeproc, void *writeparm, char *eol, int html ));
51 static int output_dn LDAP_P((
52 char *buf, char *dn, int width, int rdncount,
53 writeptype writeproc, void *writeparm, char *eol, char *urlprefix ));
54 static void strcat_escaped LDAP_P(( char *s1, char *s2 ));
55 static char *time2text LDAP_P(( char *ldtimestr, int dateonly ));
56 static long gtime LDAP_P(( struct tm *tm ));
57 static int searchaction LDAP_P((
58 LDAP *ld, char *buf, char *base, LDAPMessage *entry,
59 char *dn, struct ldap_tmplitem *tip, int labelwidth, int rdncount,
60 writeptype writeproc, void *writeparm, char *eol, char *urlprefix ));
62 #define DEF_LABEL_WIDTH 15
63 #define SEARCH_TIMEOUT_SECS 120
64 #define OCATTRNAME "objectClass"
67 #define NONFATAL_LDAP_ERR( err ) ( err == LDAP_SUCCESS || \
68 err == LDAP_TIMELIMIT_EXCEEDED || err == LDAP_SIZELIMIT_EXCEEDED )
70 #define DEF_LDAP_URL_PREFIX "ldap:///"
76 char *buf, /* NULL for "use internal" */
78 struct ldap_disptmpl *tmpl,
88 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text\n", 0, 0, 0 );
90 return( do_entry2text( ld, buf, NULL, entry, tmpl, defattrs, defvals,
91 writeproc, writeparm, eol, rdncount, opts, NULL ));
100 char *buf, /* NULL for "use internal" */
102 struct ldap_disptmpl *tmpl,
105 writeptype writeproc,
114 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html\n", 0, 0, 0 );
116 if ( urlprefix == NULL ) {
117 urlprefix = DEF_LDAP_URL_PREFIX;
120 return( do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
121 writeproc, writeparm, eol, rdncount, opts, urlprefix ));
128 char *buf, /* NULL for use-internal */
129 char *base, /* used for search actions */
131 struct ldap_disptmpl *tmpl,
134 writeptype writeproc,
139 char *urlprefix /* if non-NULL, do HTML */
142 int i, err, html, show, labelwidth;
143 int freebuf, freevals;
145 struct ldap_tmplitem *rowp, *colp;
147 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
148 return( ld->ld_errno );
152 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
153 ld->ld_errno = LDAP_NO_MEMORY;
155 return( ld->ld_errno );
162 html = ( urlprefix != NULL );
166 * add HTML intro. and title
168 if (!(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
169 sprintf( buf, "<HTML>%s<HEAD>%s<TITLE>%s%s - ", eol, eol, eol,
170 ( 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, "%s</TITLE>%s</HEAD>%s<BODY>%s<H3>%s - ", eol, eol,
174 eol, eol, ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
175 (*writeproc)( writeparm, buf, strlen( buf ));
176 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
177 sprintf( buf, "</H3>%s", eol );
178 (*writeproc)( writeparm, buf, strlen( buf ));
181 if (( opts & LDAP_DISP_OPT_NONLEAF ) != 0 &&
182 ( vals = ldap_explode_dn( dn, 0 )) != NULL ) {
188 sprintf( buf, "<A HREF=\"%s", urlprefix );
189 for ( i = 1; vals[ i ] != NULL; ++i ) {
191 strcat_escaped( buf, ", " );
193 strcat_escaped( buf, vals[ i ] );
195 if ( vals[ 1 ] != NULL ) {
196 untagged = strchr( vals[ 1 ], '=' );
198 untagged = "=The World";
200 sprintf( buf + strlen( buf ),
201 "%s\">Move Up To <EM>%s</EM></A>%s<BR>",
202 ( vals[ 1 ] == NULL ) ? "??one" : "",
203 ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol );
204 (*writeproc)( writeparm, buf, strlen( buf ));
209 untagged = strchr( vals[ 0 ], '=' );
210 sprintf( buf, "<A HREF=\"%s", urlprefix );
211 strcat_escaped( buf, dn );
212 sprintf( buf + strlen( buf ), "??one?(!(objectClass=dsa))\">Browse Below <EM>%s</EM></A>%s%s",
213 ( untagged != NULL ) ? untagged + 1 : vals[ 0 ], eol, eol );
214 (*writeproc)( writeparm, buf, strlen( buf ));
216 ldap_value_free( vals );
219 (*writeproc)( writeparm, "<HR>", 4 ); /* horizontal rule */
221 (*writeproc)( writeparm, "\"", 1 );
222 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
223 sprintf( buf, "\"%s", eol );
224 (*writeproc)( writeparm, buf, strlen( buf ));
227 if ( tmpl != NULL && ( opts & LDAP_DISP_OPT_AUTOLABELWIDTH ) != 0 ) {
228 labelwidth = max_label_len( tmpl ) + 3;
230 labelwidth = DEF_LABEL_WIDTH;;
235 if ( tmpl == NULL ) {
240 for ( attr = ldap_first_attribute( ld, entry, &ber );
241 NONFATAL_LDAP_ERR( err ) && attr != NULL;
242 attr = ldap_next_attribute( ld, entry, ber )) {
243 if (( vals = ldap_get_values( ld, entry, attr )) == NULL ) {
245 if ( defattrs != NULL ) {
246 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
247 if ( strcasecmp( attr, defattrs[ i ] ) == 0 ) {
251 if ( defattrs[ i ] != NULL ) {
259 *attr = TOUPPER( *attr );
261 err = do_vals2text( ld, buf, vals, attr, labelwidth,
262 LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol,
263 rdncount, urlprefix );
265 ldap_value_free( vals );
269 for ( rowp = ldap_first_tmplrow( tmpl );
270 NONFATAL_LDAP_ERR( err ) && rowp != NULLTMPLITEM;
271 rowp = ldap_next_tmplrow( tmpl, rowp )) {
272 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
273 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
275 if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
276 entry, colp->ti_attrname )) == NULL ) {
278 if ( !LDAP_IS_TMPLITEM_OPTION_SET( colp,
279 LDAP_DITEM_OPT_HIDEIFEMPTY ) && defattrs != NULL
280 && colp->ti_attrname != NULL ) {
281 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
282 if ( strcasecmp( colp->ti_attrname, defattrs[ i ] )
287 if ( defattrs[ i ] != NULL ) {
293 if ( LDAP_IS_TMPLITEM_OPTION_SET( colp,
294 LDAP_DITEM_OPT_SORTVALUES ) && vals[ 0 ] != NULL
295 && vals[ 1 ] != NULL ) {
296 ldap_sort_values( ld, vals, ldap_sort_strcasecmp );
301 * don't bother even calling do_vals2text() if no values
302 * or boolean with value false and "hide if false" option set
304 show = ( vals != NULL && vals[ 0 ] != NULL );
305 if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
306 == LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
307 colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
308 TOUPPER( vals[ 0 ][ 0 ] ) != 'T' ) {
312 if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
313 if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
314 if ( colp->ti_attrname == NULL || ( show &&
315 TOUPPER( vals[ 0 ][ 0 ] ) == 'T' )) {
316 err = searchaction( ld, buf, base, entry, dn, colp,
317 labelwidth, rdncount, writeproc,
318 writeparm, eol, urlprefix );
325 err = do_vals2text( ld, buf, vals, colp->ti_label,
326 labelwidth, colp->ti_syntaxid, writeproc, writeparm,
327 eol, rdncount, urlprefix );
331 ldap_value_free( vals );
337 if ( html && !(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
338 sprintf( buf, "</BODY>%s</HTML>%s", eol, eol );
339 (*writeproc)( writeparm, buf, strlen( buf ));
352 ldap_entry2text_search(
354 char *dn, /* if NULL, use entry */
355 char *base, /* if NULL, no search actions */
356 LDAPMessage *entry, /* if NULL, use dn */
357 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
360 writeptype writeproc,
363 int rdncount, /* if 0, display full DN */
367 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text_search\n", 0, 0, 0 );
369 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
370 defvals, writeproc, writeparm, eol, rdncount, opts, NULL ));
376 ldap_entry2html_search(
378 char *dn, /* if NULL, use entry */
379 char *base, /* if NULL, no search actions */
380 LDAPMessage *entry, /* if NULL, use dn */
381 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
384 writeptype writeproc,
387 int rdncount, /* if 0, display full DN */
392 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html_search\n", 0, 0, 0 );
394 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
395 defvals, writeproc, writeparm, eol, rdncount, opts, urlprefix ));
400 do_entry2text_search(
402 char *dn, /* if NULL, use entry */
403 char *base, /* if NULL, no search actions */
404 LDAPMessage *entry, /* if NULL, use dn */
405 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
408 writeptype writeproc,
411 int rdncount, /* if 0, display full DN */
416 int err, freedn, freetmpls, html;
417 char *buf, **fetchattrs, **vals;
419 struct ldap_disptmpl *tmpl;
420 struct timeval timeout;
422 if ( dn == NULL && entry == NULLMSG ) {
423 ld->ld_errno = LDAP_PARAM_ERROR;
424 return( ld->ld_errno );
427 html = ( urlprefix != NULL );
429 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
432 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
433 ld->ld_errno = LDAP_NO_MEMORY;
434 return( ld->ld_errno );
437 freedn = freetmpls = 0;
440 if ( tmpllist == NULL ) {
441 if (( err = ldap_init_templates( TEMPLATEFILE, &tmpllist )) != 0 ) {
442 sprintf( buf, "%sUnable to read template file %s (error %d)%s%s",
443 html ? "<!-- " : "", TEMPLATEFILE, err,
444 html ? "-->" : "", eol );
445 (*writeproc)( writeparm, buf, strlen( buf ));
451 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
454 ldap_free_templates( tmpllist );
456 return( ld->ld_errno );
462 if ( tmpllist != NULL ) {
465 if ( entry == NULL ) {
468 ocattrs[0] = OCATTRNAME;
470 #ifdef LDAP_CONNECTIONLESS
471 if ( LDAP_IS_CLDAP( ld ))
472 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE,
473 "objectClass=*", ocattrs, 0, &ldmp, NULL );
475 #endif /* LDAP_CONNECTIONLESS */
476 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
477 "objectClass=*", ocattrs, 0, &timeout, &ldmp );
479 if ( err == LDAP_SUCCESS ) {
480 entry = ldap_first_entry( ld, ldmp );
484 if ( entry != NULL ) {
485 vals = ldap_get_values( ld, entry, OCATTRNAME );
486 tmpl = ldap_oc2template( vals, tmpllist );
487 if ( vals != NULL ) {
488 ldap_value_free( vals );
491 if ( ldmp != NULL ) {
492 ldap_msgfree( ldmp );
498 if ( tmpl == NULL ) {
501 fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
504 #ifdef LDAP_CONNECTIONLESS
505 if ( LDAP_IS_CLDAP( ld ))
506 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
507 fetchattrs, 0, &ldmp, NULL );
509 #endif /* LDAP_CONNECTIONLESS */
510 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
511 fetchattrs, 0, &timeout, &ldmp );
516 if ( fetchattrs != NULL ) {
517 ldap_value_free( fetchattrs );
520 if ( err != LDAP_SUCCESS ||
521 ( entry = ldap_first_entry( ld, ldmp )) == NULL ) {
523 ldap_free_templates( tmpllist );
526 return( ld->ld_errno );
529 err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
530 writeproc, writeparm, eol, rdncount, opts, urlprefix );
534 ldap_free_templates( tmpllist );
536 ldap_msgfree( ldmp );
544 char *buf, /* NULL for "use internal" */
547 int labelwidth, /* 0 means use default */
548 unsigned long syntaxid,
549 writeptype writeproc,
555 Debug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
557 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
558 writeproc, writeparm, eol, rdncount, NULL ));
565 char *buf, /* NULL for "use internal" */
568 int labelwidth, /* 0 means use default */
569 unsigned long syntaxid,
570 writeptype writeproc,
577 Debug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
579 if ( urlprefix == NULL ) {
580 urlprefix = DEF_LDAP_URL_PREFIX;
583 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
584 writeproc, writeparm, eol, rdncount, urlprefix ));
591 char *buf, /* NULL for "use internal" */
594 int labelwidth, /* 0 means use default */
595 unsigned long syntaxid,
596 writeptype writeproc,
603 int i, html, writeoutval, freebuf, notascii;
604 char *p, *s, *outval;
607 if ( vals == NULL ) {
608 return( LDAP_SUCCESS );
611 html = ( urlprefix != NULL );
613 switch( LDAP_GET_SYN_TYPE( syntaxid )) {
614 case LDAP_SYN_TYPE_TEXT:
615 case LDAP_SYN_TYPE_BOOLEAN:
616 break; /* we only bother with these two types... */
618 return( LDAP_SUCCESS );
621 if ( labelwidth == 0 || labelwidth < 0 ) {
622 labelwidth = DEF_LABEL_WIDTH;
626 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
627 ld->ld_errno = LDAP_NO_MEMORY;
628 return( ld->ld_errno );
635 output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
637 for ( i = 0; vals[ i ] != NULL; ++i ) {
638 for ( p = vals[ i ]; *p != '\0'; ++p ) {
639 if ( !isascii( *p )) {
643 notascii = ( *p != '\0' );
644 outval = notascii ? "(unable to display non-ASCII text value)"
647 writeoutval = 0; /* if non-zero, write outval after switch */
650 case LDAP_SYN_CASEIGNORESTR:
654 case LDAP_SYN_RFC822ADDR:
656 strcpy( buf, "<DD><A HREF=\"mailto:" );
657 strcat_escaped( buf, outval );
658 sprintf( buf + strlen( buf ), "\">%s</A><BR>%s", outval, eol );
659 (*writeproc)( writeparm, buf, strlen( buf ));
665 case LDAP_SYN_DN: /* for now */
666 output_dn( buf, outval, labelwidth, rdncount, writeproc,
667 writeparm, eol, urlprefix );
670 case LDAP_SYN_MULTILINESTR:
671 if ( i > 0 && !html ) {
672 output_label( buf, label, labelwidth, writeproc,
673 writeparm, eol, html );
677 while (( s = strchr( s, '$' )) != NULL ) {
679 while ( isspace( *s )) {
683 sprintf( buf, "<DD>%s<BR>%s", p, eol );
685 sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
687 (*writeproc)( writeparm, buf, strlen( buf ));
694 case LDAP_SYN_BOOLEAN:
695 outval = TOUPPER( outval[ 0 ] ) == 'T' ? "TRUE" : "FALSE";
701 outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
705 case LDAP_SYN_LABELEDURL:
706 if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
708 while ( isspace( *p )) {
712 } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
714 while ( isspace( *s )) {
724 * at this point `s' points to the label & `p' to the URL
727 sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
729 sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
730 s, eol, labelwidth + 2, " ",p , eol );
732 (*writeproc)( writeparm, buf, strlen( buf ));
736 sprintf( buf, " Can't display item type %ld%s",
738 (*writeproc)( writeparm, buf, strlen( buf ));
743 sprintf( buf, "<DD>%s<BR>%s", outval, eol );
745 sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
747 (*writeproc)( writeparm, buf, strlen( buf ));
755 return( LDAP_SUCCESS );
760 max_label_len( struct ldap_disptmpl *tmpl )
762 struct ldap_tmplitem *rowp, *colp;
767 for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
768 rowp = ldap_next_tmplrow( tmpl, rowp )) {
769 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
770 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
771 if (( len = strlen( colp->ti_label )) > maxlen ) {
782 output_label( char *buf, char *label, int width, writeptype writeproc,
783 void *writeparm, char *eol, int html )
788 sprintf( buf, "<DT><B>%s</B>", label );
790 sprintf( buf, " %s:", label );
791 p = buf + strlen( buf );
793 while ( p - buf < width ) {
801 return ((*writeproc)( writeparm, buf, strlen( buf )));
806 output_dn( char *buf, char *dn, int width, int rdncount,
807 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
812 if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
816 if ( urlprefix != NULL ) {
817 sprintf( buf, "<DD><A HREF=\"%s", urlprefix );
818 strcat_escaped( buf, dn );
819 strcat( buf, "\">" );
820 } else if ( width > 0 ) {
821 sprintf( buf, "%-*s", width, " " );
826 for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
831 strcat( buf, dnrdns[ i ] );
834 if ( urlprefix != NULL ) {
835 strcat( buf, "</A><BR>" );
838 ldap_value_free( dnrdns );
842 return ((*writeproc)( writeparm, buf, strlen( buf )));
847 #define HREF_CHAR_ACCEPTABLE( c ) (( c >= '-' && c <= '9' ) || \
848 ( c >= '@' && c <= 'Z' ) || \
850 ( c >= 'a' && c <= 'z' ))
853 strcat_escaped( char *s1, char *s2 )
856 char *hexdig = "0123456789ABCDEF";
858 p = s1 + strlen( s1 );
859 for ( q = s2; *q != '\0'; ++q ) {
860 if ( HREF_CHAR_ACCEPTABLE( *q )) {
864 *p++ = hexdig[ *q >> 4 ];
865 *p++ = hexdig[ *q & 0x0F ];
873 #define GET2BYTENUM( p ) (( *p - '0' ) * 10 + ( *(p+1) - '0' ))
876 time2text( char *ldtimestr, int dateonly )
879 char *p, *timestr, zone, *fmterr = "badly formatted time";
883 memset( (char *)&t, 0, sizeof( struct tm ));
884 if ( (int) strlen( ldtimestr ) < 13 ) {
888 for ( p = ldtimestr; p - ldtimestr < 12; ++p ) {
889 if ( !isdigit( *p )) {
895 t.tm_year = GET2BYTENUM( p ); p += 2;
896 t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
897 t.tm_mday = GET2BYTENUM( p ); p += 2;
898 t.tm_hour = GET2BYTENUM( p ); p += 2;
899 t.tm_min = GET2BYTENUM( p ); p += 2;
900 t.tm_sec = GET2BYTENUM( p ); p += 2;
902 if (( zone = *p ) == 'Z' ) { /* GMT */
903 zone = '\0'; /* no need to indicate on screen, so we make it null */
906 gmttime = gtime( &t );
908 timestr = ldap_pvt_ctime( &gmttime, timebuf );
910 timestr[ strlen( timestr ) - 1 ] = zone; /* replace trailing newline */
912 SAFEMEMCPY( timestr + 11, timestr + 20, strlen( timestr + 20 ) + 1 );
920 /* gtime.c - inverse gmtime */
921 /* gtime(): the inverse of localtime().
922 This routine was supplied by Mike Accetta at CMU many years ago.
925 static int dmsize[] = {
926 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
930 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
932 #define YEAR(y) ((y) >= 100 ? (y) : (y) + 1900)
936 static long gtime ( struct tm *tm )
945 register long result;
947 if ((sec = tm -> tm_sec) < 0 || sec > 59
948 || (mins = tm -> tm_min) < 0 || mins > 59
949 || (hour = tm -> tm_hour) < 0 || hour > 24
950 || (mday = tm -> tm_mday) < 1 || mday > 31
951 || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
957 year = YEAR (tm -> tm_year);
960 for (i = 1970; i < year; i++)
961 result += dysize (i);
962 if (dysize (year) == 366 && mon >= 3)
965 result += dmsize[mon - 1];
967 result = 24 * result + hour;
968 result = 60 * result + mins;
969 result = 60 * result + sec;
975 searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
976 struct ldap_tmplitem *tip, int labelwidth, int rdncount,
977 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
979 int err = 0, lderr, i, count, html;
980 char **vals, **members;
981 char *value, *filtpattern, *attr, *selectname;
982 char *retattrs[2], filter[ 256 ];
984 struct timeval timeout;
986 html = ( urlprefix != NULL );
988 for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
992 return( LDAP_PARAM_ERROR );
994 attr = tip->ti_args[ 0 ];
995 filtpattern = tip->ti_args[ 1 ];
996 retattrs[ 0 ] = tip->ti_args[ 2 ];
997 retattrs[ 1 ] = NULL;
998 selectname = tip->ti_args[ 3 ];
1001 if ( attr == NULL ) {
1003 } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
1004 return( LDAP_PARAM_ERROR );
1005 } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
1007 } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
1013 ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
1018 * if we are generating HTML, we add an HREF link that embodies this
1019 * search action as an LDAP URL, instead of actually doing the search
1022 sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
1023 if ( base != NULL ) {
1024 strcat_escaped( buf, base );
1026 strcat( buf, "??sub?" );
1027 strcat_escaped( buf, filter );
1028 sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
1029 tip->ti_label, eol );
1030 if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
1031 return( LDAP_LOCAL_ERROR );
1033 return( LDAP_SUCCESS );
1036 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1037 timeout.tv_usec = 0;
1039 #ifdef LDAP_CONNECTIONLESS
1040 if ( LDAP_IS_CLDAP( ld ))
1041 lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1044 #endif /* LDAP_CONNECTIONLESS */
1045 lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1046 0, &timeout, &ldmp );
1048 if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
1049 if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
1050 if (( members = (char **)malloc( (count + 1) * sizeof(char *)))
1052 err = LDAP_NO_MEMORY;
1054 for ( i = 0, entry = ldap_first_entry( ld, ldmp );
1056 entry = ldap_next_entry( ld, entry ), ++i ) {
1057 members[ i ] = ldap_get_dn( ld, entry );
1059 members[ i ] = NULL;
1061 ldap_sort_values( ld, members, ldap_sort_strcasecmp );
1063 err = do_vals2text( ld, NULL, members, tip->ti_label,
1064 html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
1065 eol, rdncount, urlprefix );
1067 ldap_value_free( members );
1070 ldap_msgfree( ldmp );
1074 if ( vals != NULL ) {
1075 ldap_value_free( vals );
1078 return(( err == LDAP_SUCCESS ) ? lderr : err );