3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * tmplout.c: display template library output routines for LDAP clients
8 * 12 April 1994 by Mark C Smith
15 #include <ac/socket.h>
16 #include <ac/stdlib.h>
19 #include <ac/string.h>
22 #ifdef HAVE_SYS_FILE_H
32 #include "ldap_defaults.h"
34 #define writeptype ldap_writeptype
37 static int do_entry2text LDAP_P((
38 LDAP *ld, char *buf, char *base, LDAPMessage *entry,
39 struct ldap_disptmpl *tmpl, char **defattrs, char ***defvals,
40 writeptype writeproc, void *writeparm, char *eol, int rdncount,
41 unsigned long opts, char *urlprefix ));
42 static int do_entry2text_search LDAP_P((
43 LDAP *ld, char *dn, char *base,
44 LDAPMessage *entry, struct ldap_disptmpl *tmpllist, char **defattrs,
45 char ***defvals, writeptype writeproc, void *writeparm, char *eol,
46 int rdncount, unsigned long opts, char *urlprefix ));
47 static int do_vals2text LDAP_P((
48 LDAP *ld, char *buf, char **vals, char *label,
49 int labelwidth, unsigned long syntaxid, writeptype writeproc,
50 void *writeparm, char *eol, int rdncount, char *urlprefix ));
51 static int max_label_len LDAP_P(( struct ldap_disptmpl *tmpl ));
52 static int output_label LDAP_P((
53 char *buf, char *label, int width,
54 writeptype writeproc, void *writeparm, char *eol, int html ));
55 static int output_dn LDAP_P((
56 char *buf, char *dn, int width, int rdncount,
57 writeptype writeproc, void *writeparm, char *eol, char *urlprefix ));
58 static void strcat_escaped LDAP_P(( char *s1, char *s2 ));
59 static char *time2text LDAP_P(( char *ldtimestr, int dateonly ));
60 static long gtime LDAP_P(( struct tm *tm ));
61 static int searchaction LDAP_P((
62 LDAP *ld, char *buf, char *base, LDAPMessage *entry,
63 char *dn, struct ldap_tmplitem *tip, int labelwidth, int rdncount,
64 writeptype writeproc, void *writeparm, char *eol, char *urlprefix ));
66 #define DEF_LABEL_WIDTH 15
67 #define SEARCH_TIMEOUT_SECS 120
68 #define OCATTRNAME "objectClass"
71 #define NONFATAL_LDAP_ERR( err ) ( (err) == LDAP_SUCCESS || \
72 (err) == LDAP_TIMELIMIT_EXCEEDED || (err) == LDAP_SIZELIMIT_EXCEEDED )
74 #define DEF_LDAP_URL_PREFIX "ldap:///"
80 char *buf, /* NULL for "use internal" */
82 struct ldap_disptmpl *tmpl,
92 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text\n", 0, 0, 0 );
94 return( do_entry2text( ld, buf, NULL, entry, tmpl, defattrs, defvals,
95 writeproc, writeparm, eol, rdncount, opts, NULL ));
104 char *buf, /* NULL for "use internal" */
106 struct ldap_disptmpl *tmpl,
109 writeptype writeproc,
118 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html\n", 0, 0, 0 );
120 if ( urlprefix == NULL ) {
121 urlprefix = DEF_LDAP_URL_PREFIX;
124 return( do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
125 writeproc, writeparm, eol, rdncount, opts, urlprefix ));
132 char *buf, /* NULL for use-internal */
133 char *base, /* used for search actions */
135 struct ldap_disptmpl *tmpl,
138 writeptype writeproc,
143 char *urlprefix /* if non-NULL, do HTML */
146 int i, err, html, show, labelwidth;
147 int freebuf, freevals;
149 struct ldap_tmplitem *rowp, *colp;
151 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
152 return( ld->ld_errno );
156 if (( buf = LDAP_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
157 ld->ld_errno = LDAP_NO_MEMORY;
159 return( ld->ld_errno );
166 html = ( urlprefix != NULL );
170 * add HTML intro. and title
172 if (!(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
173 sprintf( buf, "<HTML>%s<HEAD>%s<TITLE>%s%s - ", eol, eol, eol,
174 ( 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, "%s</TITLE>%s</HEAD>%s<BODY>%s<H3>%s - ", eol, eol,
178 eol, eol, ( 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, "</H3>%s", eol );
182 (*writeproc)( writeparm, buf, strlen( buf ));
185 if (( opts & LDAP_DISP_OPT_NONLEAF ) != 0 &&
186 ( vals = ldap_explode_dn( dn, 0 )) != NULL ) {
192 sprintf( buf, "<A HREF=\"%s", urlprefix );
193 for ( i = 1; vals[ i ] != NULL; ++i ) {
195 strcat_escaped( buf, ", " );
197 strcat_escaped( buf, vals[ i ] );
199 if ( vals[ 1 ] != NULL ) {
200 untagged = strchr( vals[ 1 ], '=' );
202 untagged = "=The World";
204 sprintf( buf + strlen( buf ),
205 "%s\">Move Up To <EM>%s</EM></A>%s<BR>",
206 ( vals[ 1 ] == NULL ) ? "??one" : "",
207 ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol );
208 (*writeproc)( writeparm, buf, strlen( buf ));
213 untagged = strchr( vals[ 0 ], '=' );
214 sprintf( buf, "<A HREF=\"%s", urlprefix );
215 strcat_escaped( buf, dn );
216 sprintf( buf + strlen( buf ), "??one?(!(objectClass=dsa))\">Browse Below <EM>%s</EM></A>%s%s",
217 ( untagged != NULL ) ? untagged + 1 : vals[ 0 ], eol, eol );
218 (*writeproc)( writeparm, buf, strlen( buf ));
220 ldap_value_free( vals );
223 (*writeproc)( writeparm, "<HR>", 4 ); /* horizontal rule */
225 (*writeproc)( writeparm, "\"", 1 );
226 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
227 sprintf( buf, "\"%s", eol );
228 (*writeproc)( writeparm, buf, strlen( buf ));
231 if ( tmpl != NULL && ( opts & LDAP_DISP_OPT_AUTOLABELWIDTH ) != 0 ) {
232 labelwidth = max_label_len( tmpl ) + 3;
234 labelwidth = DEF_LABEL_WIDTH;;
239 if ( tmpl == NULL ) {
244 for ( attr = ldap_first_attribute( ld, entry, &ber );
245 NONFATAL_LDAP_ERR( err ) && attr != NULL;
246 attr = ldap_next_attribute( ld, entry, ber )) {
247 if (( vals = ldap_get_values( ld, entry, attr )) == NULL ) {
249 if ( defattrs != NULL ) {
250 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
251 if ( strcasecmp( attr, defattrs[ i ] ) == 0 ) {
255 if ( defattrs[ i ] != NULL ) {
263 *attr = TOUPPER( (unsigned char) *attr );
265 err = do_vals2text( ld, buf, vals, attr, labelwidth,
266 LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol,
267 rdncount, urlprefix );
269 ldap_value_free( vals );
277 for ( rowp = ldap_first_tmplrow( tmpl );
278 NONFATAL_LDAP_ERR( err ) && rowp != NULL;
279 rowp = ldap_next_tmplrow( tmpl, rowp )) {
280 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULL;
281 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
283 if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
284 entry, colp->ti_attrname )) == NULL ) {
286 if ( !LDAP_IS_TMPLITEM_OPTION_SET( colp,
287 LDAP_DITEM_OPT_HIDEIFEMPTY ) && defattrs != NULL
288 && colp->ti_attrname != NULL ) {
289 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
290 if ( strcasecmp( colp->ti_attrname, defattrs[ i ] )
295 if ( defattrs[ i ] != NULL ) {
301 if ( LDAP_IS_TMPLITEM_OPTION_SET( colp,
302 LDAP_DITEM_OPT_SORTVALUES ) && vals[ 0 ] != NULL
303 && vals[ 1 ] != NULL ) {
304 ldap_sort_values( ld, vals, ldap_sort_strcasecmp );
309 * don't bother even calling do_vals2text() if no values
310 * or boolean with value false and "hide if false" option set
312 show = ( vals != NULL && vals[ 0 ] != NULL );
313 if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
314 == LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
315 colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
316 TOUPPER( (unsigned char) vals[ 0 ][ 0 ] ) != 'T' ) {
320 if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
321 if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
322 if ( colp->ti_attrname == NULL || ( show &&
323 TOUPPER( (unsigned char) vals[ 0 ][ 0 ] ) == 'T' )) {
324 err = searchaction( ld, buf, base, entry, dn, colp,
325 labelwidth, rdncount, writeproc,
326 writeparm, eol, urlprefix );
333 err = do_vals2text( ld, buf, vals, colp->ti_label,
334 labelwidth, colp->ti_syntaxid, writeproc, writeparm,
335 eol, rdncount, urlprefix );
339 ldap_value_free( vals );
345 if ( html && !(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
346 sprintf( buf, "</BODY>%s</HTML>%s", eol, eol );
347 (*writeproc)( writeparm, buf, strlen( buf ));
360 ldap_entry2text_search(
362 char *dn, /* if NULL, use entry */
363 char *base, /* if NULL, no search actions */
364 LDAPMessage *entry, /* if NULL, use dn */
365 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
368 writeptype writeproc,
371 int rdncount, /* if 0, display full DN */
375 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text_search\n", 0, 0, 0 );
377 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
378 defvals, writeproc, writeparm, eol, rdncount, opts, NULL ));
384 ldap_entry2html_search(
386 char *dn, /* if NULL, use entry */
387 char *base, /* if NULL, no search actions */
388 LDAPMessage *entry, /* if NULL, use dn */
389 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
392 writeptype writeproc,
395 int rdncount, /* if 0, display full DN */
400 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html_search\n", 0, 0, 0 );
402 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
403 defvals, writeproc, writeparm, eol, rdncount, opts, urlprefix ));
408 do_entry2text_search(
410 char *dn, /* if NULL, use entry */
411 char *base, /* if NULL, no search actions */
412 LDAPMessage *entry, /* if NULL, use dn */
413 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
416 writeptype writeproc,
419 int rdncount, /* if 0, display full DN */
424 int err, freedn, freetmpls, html;
425 char *buf, **fetchattrs, **vals;
427 struct ldap_disptmpl *tmpl;
428 struct timeval timeout;
430 if ( dn == NULL && entry == NULL ) {
431 ld->ld_errno = LDAP_PARAM_ERROR;
432 return( ld->ld_errno );
435 html = ( urlprefix != NULL );
437 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
440 if (( buf = LDAP_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
441 ld->ld_errno = LDAP_NO_MEMORY;
442 return( ld->ld_errno );
445 freedn = freetmpls = 0;
448 if ( tmpllist == NULL ) {
449 if (( err = ldap_init_templates( TEMPLATEFILE, &tmpllist )) != 0 ) {
450 sprintf( buf, "%sUnable to read template file %s (error %d)%s%s",
451 html ? "<!-- " : "", TEMPLATEFILE, err,
452 html ? "-->" : "", eol );
453 (*writeproc)( writeparm, buf, strlen( buf ));
459 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
462 ldap_free_templates( tmpllist );
464 return( ld->ld_errno );
470 if ( tmpllist != NULL ) {
473 if ( entry == NULL ) {
476 ocattrs[0] = OCATTRNAME;
479 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
480 NULL, ocattrs, 0, &timeout, &ldmp );
481 if ( err == LDAP_SUCCESS ) {
482 entry = ldap_first_entry( ld, ldmp );
486 if ( entry != NULL ) {
487 vals = ldap_get_values( ld, entry, OCATTRNAME );
488 tmpl = ldap_oc2template( vals, tmpllist );
489 if ( vals != NULL ) {
490 ldap_value_free( vals );
493 if ( ldmp != NULL ) {
494 ldap_msgfree( ldmp );
500 if ( tmpl == NULL ) {
503 fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
506 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, NULL,
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 = LDAP_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( (unsigned char) *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((unsigned char) 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( (unsigned char) *p )) {
708 } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
710 while ( isspace( (unsigned char) *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 != NULL;
764 rowp = ldap_next_tmplrow( tmpl, rowp )) {
765 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULL;
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";
880 if (strlen( ldtimestr ) < 12 ) {
884 for ( ndigits=0; isdigit((unsigned char) ldtimestr[ndigits]); ndigits++) {
888 if ( ndigits != 12 && ndigits != 14) {
892 memset( (char *)&t, '\0', sizeof( struct tm ));
897 /* came with a century */
898 /* POSIX says tm_year should be year - 1900 */
899 t.tm_year = 100 * GET2BYTENUM( p ) - 1900;
901 t.tm_year += GET2BYTENUM( p ); p += 2;
904 /* came without a century */
905 t.tm_year = GET2BYTENUM( p ); p += 2;
907 /* Y2K hack - 2 digit years < 70 are 21st century */
908 if( t.tm_year < 70 ) {
913 t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
914 t.tm_mday = GET2BYTENUM( p ); p += 2;
915 t.tm_hour = GET2BYTENUM( p ); p += 2;
916 t.tm_min = GET2BYTENUM( p ); p += 2;
917 t.tm_sec = GET2BYTENUM( p ); p += 2;
919 if (( zone = *p ) == 'Z' ) { /* GMT */
920 zone = '\0'; /* no need to indicate on screen, so we make it null */
923 gmttime = gtime( &t );
925 timestr = ldap_pvt_ctime( &gmttime, timebuf );
927 timestr[ strlen( timestr ) - 1 ] = zone; /* replace trailing newline */
929 AC_MEMCPY( timestr + 11, timestr + 20, strlen( timestr + 20 ) + 1 );
937 /* gtime.c - inverse gmtime */
938 /* gtime(): the inverse of localtime().
939 This routine was supplied by Mike Accetta at CMU many years ago.
942 static const int dmsize[] = {
943 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
947 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
952 /* per STDC & POSIX tm_year should be offset by 1900 */
953 #define YEAR_POSIX(y) ((y) + 1900)
956 * year is < 1900, year is offset by 1900
958 #define YEAR_CAREFUL(y) ((y) < 1900 ? (y) + 1900 : (y))
960 #define YEAR(y) YEAR_CAREFUL(y)
964 static long gtime ( struct tm *tm )
973 register long result;
975 if ((sec = tm -> tm_sec) < 0 || sec > 59
976 || (mins = tm -> tm_min) < 0 || mins > 59
977 || (hour = tm -> tm_hour) < 0 || hour > 24
978 || (mday = tm -> tm_mday) < 1 || mday > 31
979 || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
985 year = YEAR (tm -> tm_year);
988 for (i = 1970; i < year; i++)
989 result += dysize (i);
990 if (dysize (year) == 366 && mon >= 3)
993 result += dmsize[mon - 1];
995 result = 24 * result + hour;
996 result = 60 * result + mins;
997 result = 60 * result + sec;
1003 searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
1004 struct ldap_tmplitem *tip, int labelwidth, int rdncount,
1005 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
1007 int err = 0, lderr, i, count, html;
1008 char **vals, **members;
1009 char *value, *filtpattern, *attr, *selectname;
1010 char *retattrs[2], filter[ 256 ];
1012 struct timeval timeout;
1014 html = ( urlprefix != NULL );
1016 for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
1020 return( LDAP_PARAM_ERROR );
1022 attr = tip->ti_args[ 0 ];
1023 filtpattern = tip->ti_args[ 1 ];
1024 retattrs[ 0 ] = tip->ti_args[ 2 ];
1025 retattrs[ 1 ] = NULL;
1026 selectname = tip->ti_args[ 3 ];
1029 if ( attr == NULL ) {
1031 } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
1032 return( LDAP_PARAM_ERROR );
1033 } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
1035 } else if ( strcasecmp( attr, "-dn" ) == 0 ) {
1037 } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
1043 ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
1048 * if we are generating HTML, we add an HREF link that embodies this
1049 * search action as an LDAP URL, instead of actually doing the search
1052 sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
1053 if ( base != NULL ) {
1054 strcat_escaped( buf, base );
1056 strcat( buf, "??sub?" );
1057 strcat_escaped( buf, filter );
1058 sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
1059 tip->ti_label, eol );
1060 if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
1061 return( LDAP_LOCAL_ERROR );
1063 return( LDAP_SUCCESS );
1066 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1067 timeout.tv_usec = 0;
1069 lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1070 0, &timeout, &ldmp );
1072 if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
1073 if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
1074 if (( members = (char **)LDAP_MALLOC( (count + 1) * sizeof(char *)))
1076 err = LDAP_NO_MEMORY;
1078 for ( i = 0, entry = ldap_first_entry( ld, ldmp );
1080 entry = ldap_next_entry( ld, entry ), ++i ) {
1081 members[ i ] = ldap_get_dn( ld, entry );
1083 members[ i ] = NULL;
1085 ldap_sort_values( ld, members, ldap_sort_strcasecmp );
1087 err = do_vals2text( ld, NULL, members, tip->ti_label,
1088 html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
1089 eol, rdncount, urlprefix );
1091 ldap_value_free( members );
1094 ldap_msgfree( ldmp );
1098 if ( vals != NULL ) {
1099 ldap_value_free( vals );
1102 return(( err == LDAP_SUCCESS ) ? lderr : err );