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 = LDAP_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( (unsigned char) *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 );
273 for ( rowp = ldap_first_tmplrow( tmpl );
274 NONFATAL_LDAP_ERR( err ) && rowp != NULLTMPLITEM;
275 rowp = ldap_next_tmplrow( tmpl, rowp )) {
276 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
277 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
279 if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
280 entry, colp->ti_attrname )) == NULL ) {
282 if ( !LDAP_IS_TMPLITEM_OPTION_SET( colp,
283 LDAP_DITEM_OPT_HIDEIFEMPTY ) && defattrs != NULL
284 && colp->ti_attrname != NULL ) {
285 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
286 if ( strcasecmp( colp->ti_attrname, defattrs[ i ] )
291 if ( defattrs[ i ] != NULL ) {
297 if ( LDAP_IS_TMPLITEM_OPTION_SET( colp,
298 LDAP_DITEM_OPT_SORTVALUES ) && vals[ 0 ] != NULL
299 && vals[ 1 ] != NULL ) {
300 ldap_sort_values( ld, vals, ldap_sort_strcasecmp );
305 * don't bother even calling do_vals2text() if no values
306 * or boolean with value false and "hide if false" option set
308 show = ( vals != NULL && vals[ 0 ] != NULL );
309 if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
310 == LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
311 colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
312 TOUPPER( (unsigned char) vals[ 0 ][ 0 ] ) != 'T' ) {
316 if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
317 if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
318 if ( colp->ti_attrname == NULL || ( show &&
319 TOUPPER( (unsigned char) vals[ 0 ][ 0 ] ) == 'T' )) {
320 err = searchaction( ld, buf, base, entry, dn, colp,
321 labelwidth, rdncount, writeproc,
322 writeparm, eol, urlprefix );
329 err = do_vals2text( ld, buf, vals, colp->ti_label,
330 labelwidth, colp->ti_syntaxid, writeproc, writeparm,
331 eol, rdncount, urlprefix );
335 ldap_value_free( vals );
341 if ( html && !(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
342 sprintf( buf, "</BODY>%s</HTML>%s", eol, eol );
343 (*writeproc)( writeparm, buf, strlen( buf ));
356 ldap_entry2text_search(
358 char *dn, /* if NULL, use entry */
359 char *base, /* if NULL, no search actions */
360 LDAPMessage *entry, /* if NULL, use dn */
361 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
364 writeptype writeproc,
367 int rdncount, /* if 0, display full DN */
371 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text_search\n", 0, 0, 0 );
373 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
374 defvals, writeproc, writeparm, eol, rdncount, opts, NULL ));
380 ldap_entry2html_search(
382 char *dn, /* if NULL, use entry */
383 char *base, /* if NULL, no search actions */
384 LDAPMessage *entry, /* if NULL, use dn */
385 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
388 writeptype writeproc,
391 int rdncount, /* if 0, display full DN */
396 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html_search\n", 0, 0, 0 );
398 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
399 defvals, writeproc, writeparm, eol, rdncount, opts, urlprefix ));
404 do_entry2text_search(
406 char *dn, /* if NULL, use entry */
407 char *base, /* if NULL, no search actions */
408 LDAPMessage *entry, /* if NULL, use dn */
409 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
412 writeptype writeproc,
415 int rdncount, /* if 0, display full DN */
420 int err, freedn, freetmpls, html;
421 char *buf, **fetchattrs, **vals;
423 struct ldap_disptmpl *tmpl;
424 struct timeval timeout;
426 if ( dn == NULL && entry == NULLMSG ) {
427 ld->ld_errno = LDAP_PARAM_ERROR;
428 return( ld->ld_errno );
431 html = ( urlprefix != NULL );
433 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
436 if (( buf = LDAP_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
437 ld->ld_errno = LDAP_NO_MEMORY;
438 return( ld->ld_errno );
441 freedn = freetmpls = 0;
444 if ( tmpllist == NULL ) {
445 if (( err = ldap_init_templates( TEMPLATEFILE, &tmpllist )) != 0 ) {
446 sprintf( buf, "%sUnable to read template file %s (error %d)%s%s",
447 html ? "<!-- " : "", TEMPLATEFILE, err,
448 html ? "-->" : "", eol );
449 (*writeproc)( writeparm, buf, strlen( buf ));
455 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
458 ldap_free_templates( tmpllist );
460 return( ld->ld_errno );
466 if ( tmpllist != NULL ) {
469 if ( entry == NULL ) {
472 ocattrs[0] = OCATTRNAME;
474 #ifdef LDAP_CONNECTIONLESS
475 if ( LDAP_IS_CLDAP( ld ))
476 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE,
477 "objectClass=*", ocattrs, 0, &ldmp, NULL );
479 #endif /* LDAP_CONNECTIONLESS */
480 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
481 "objectClass=*", ocattrs, 0, &timeout, &ldmp );
483 if ( err == LDAP_SUCCESS ) {
484 entry = ldap_first_entry( ld, ldmp );
488 if ( entry != NULL ) {
489 vals = ldap_get_values( ld, entry, OCATTRNAME );
490 tmpl = ldap_oc2template( vals, tmpllist );
491 if ( vals != NULL ) {
492 ldap_value_free( vals );
495 if ( ldmp != NULL ) {
496 ldap_msgfree( ldmp );
502 if ( tmpl == NULL ) {
505 fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
508 #ifdef LDAP_CONNECTIONLESS
509 if ( LDAP_IS_CLDAP( ld ))
510 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
511 fetchattrs, 0, &ldmp, NULL );
513 #endif /* LDAP_CONNECTIONLESS */
514 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
515 fetchattrs, 0, &timeout, &ldmp );
520 if ( fetchattrs != NULL ) {
521 ldap_value_free( fetchattrs );
524 if ( err != LDAP_SUCCESS ||
525 ( entry = ldap_first_entry( ld, ldmp )) == NULL ) {
527 ldap_free_templates( tmpllist );
530 return( ld->ld_errno );
533 err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
534 writeproc, writeparm, eol, rdncount, opts, urlprefix );
538 ldap_free_templates( tmpllist );
540 ldap_msgfree( ldmp );
548 char *buf, /* NULL for "use internal" */
551 int labelwidth, /* 0 means use default */
552 unsigned long syntaxid,
553 writeptype writeproc,
559 Debug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
561 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
562 writeproc, writeparm, eol, rdncount, NULL ));
569 char *buf, /* NULL for "use internal" */
572 int labelwidth, /* 0 means use default */
573 unsigned long syntaxid,
574 writeptype writeproc,
581 Debug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
583 if ( urlprefix == NULL ) {
584 urlprefix = DEF_LDAP_URL_PREFIX;
587 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
588 writeproc, writeparm, eol, rdncount, urlprefix ));
595 char *buf, /* NULL for "use internal" */
598 int labelwidth, /* 0 means use default */
599 unsigned long syntaxid,
600 writeptype writeproc,
607 int i, html, writeoutval, freebuf, notascii;
608 char *p, *s, *outval;
611 if ( vals == NULL ) {
612 return( LDAP_SUCCESS );
615 html = ( urlprefix != NULL );
617 switch( LDAP_GET_SYN_TYPE( syntaxid )) {
618 case LDAP_SYN_TYPE_TEXT:
619 case LDAP_SYN_TYPE_BOOLEAN:
620 break; /* we only bother with these two types... */
622 return( LDAP_SUCCESS );
625 if ( labelwidth == 0 || labelwidth < 0 ) {
626 labelwidth = DEF_LABEL_WIDTH;
630 if (( buf = LDAP_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
631 ld->ld_errno = LDAP_NO_MEMORY;
632 return( ld->ld_errno );
639 output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
641 for ( i = 0; vals[ i ] != NULL; ++i ) {
642 for ( p = vals[ i ]; *p != '\0'; ++p ) {
643 if ( !isascii( *p )) {
647 notascii = ( *p != '\0' );
648 outval = notascii ? "(unable to display non-ASCII text value)"
651 writeoutval = 0; /* if non-zero, write outval after switch */
654 case LDAP_SYN_CASEIGNORESTR:
658 case LDAP_SYN_RFC822ADDR:
660 strcpy( buf, "<DD><A HREF=\"mailto:" );
661 strcat_escaped( buf, outval );
662 sprintf( buf + strlen( buf ), "\">%s</A><BR>%s", outval, eol );
663 (*writeproc)( writeparm, buf, strlen( buf ));
669 case LDAP_SYN_DN: /* for now */
670 output_dn( buf, outval, labelwidth, rdncount, writeproc,
671 writeparm, eol, urlprefix );
674 case LDAP_SYN_MULTILINESTR:
675 if ( i > 0 && !html ) {
676 output_label( buf, label, labelwidth, writeproc,
677 writeparm, eol, html );
681 while (( s = strchr( s, '$' )) != NULL ) {
683 while ( isspace( (unsigned char) *s )) {
687 sprintf( buf, "<DD>%s<BR>%s", p, eol );
689 sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
691 (*writeproc)( writeparm, buf, strlen( buf ));
698 case LDAP_SYN_BOOLEAN:
699 outval = TOUPPER((unsigned char) outval[0]) == 'T' ? "TRUE" : "FALSE";
705 outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
709 case LDAP_SYN_LABELEDURL:
710 if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
712 while ( isspace( (unsigned char) *p )) {
716 } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
718 while ( isspace( (unsigned char) *s )) {
728 * at this point `s' points to the label & `p' to the URL
731 sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
733 sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
734 s, eol, labelwidth + 2, " ",p , eol );
736 (*writeproc)( writeparm, buf, strlen( buf ));
740 sprintf( buf, " Can't display item type %ld%s",
742 (*writeproc)( writeparm, buf, strlen( buf ));
747 sprintf( buf, "<DD>%s<BR>%s", outval, eol );
749 sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
751 (*writeproc)( writeparm, buf, strlen( buf ));
759 return( LDAP_SUCCESS );
764 max_label_len( struct ldap_disptmpl *tmpl )
766 struct ldap_tmplitem *rowp, *colp;
771 for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
772 rowp = ldap_next_tmplrow( tmpl, rowp )) {
773 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
774 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
775 if (( len = strlen( colp->ti_label )) > maxlen ) {
786 output_label( char *buf, char *label, int width, writeptype writeproc,
787 void *writeparm, char *eol, int html )
792 sprintf( buf, "<DT><B>%s</B>", label );
794 sprintf( buf, " %s:", label );
795 p = buf + strlen( buf );
797 while ( p - buf < width ) {
805 return ((*writeproc)( writeparm, buf, strlen( buf )));
810 output_dn( char *buf, char *dn, int width, int rdncount,
811 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
816 if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
820 if ( urlprefix != NULL ) {
821 sprintf( buf, "<DD><A HREF=\"%s", urlprefix );
822 strcat_escaped( buf, dn );
823 strcat( buf, "\">" );
824 } else if ( width > 0 ) {
825 sprintf( buf, "%-*s", width, " " );
830 for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
835 strcat( buf, dnrdns[ i ] );
838 if ( urlprefix != NULL ) {
839 strcat( buf, "</A><BR>" );
842 ldap_value_free( dnrdns );
846 return ((*writeproc)( writeparm, buf, strlen( buf )));
851 #define HREF_CHAR_ACCEPTABLE( c ) (( (c) >= '-' && (c) <= '9' ) || \
852 ( (c) >= '@' && (c) <= 'Z' ) || \
854 ( (c) >= 'a' && (c) <= 'z' ))
857 strcat_escaped( char *s1, char *s2 )
860 char *hexdig = "0123456789ABCDEF";
862 p = s1 + strlen( s1 );
863 for ( q = s2; *q != '\0'; ++q ) {
864 if ( HREF_CHAR_ACCEPTABLE( *q )) {
868 *p++ = hexdig[ *q >> 4 ];
869 *p++ = hexdig[ *q & 0x0F ];
877 #define GET2BYTENUM( p ) (( *(p) - '0' ) * 10 + ( *((p)+1) - '0' ))
880 time2text( char *ldtimestr, int dateonly )
883 char *p, *timestr, zone, *fmterr = "badly formatted time";
888 if (strlen( ldtimestr ) < 12 ) {
892 for ( ndigits=0; isdigit((unsigned char) ldtimestr[ndigits]); ndigits++) {
896 if ( ndigits != 12 && ndigits != 14) {
900 memset( (char *)&t, 0, sizeof( struct tm ));
905 /* came with a century */
906 /* POSIX says tm_year should be year - 1900 */
907 t.tm_year = 100 * GET2BYTENUM( p ) - 1900;
910 t.tm_year = GET2BYTENUM( p ); p += 2;
912 t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
913 t.tm_mday = GET2BYTENUM( p ); p += 2;
914 t.tm_hour = GET2BYTENUM( p ); p += 2;
915 t.tm_min = GET2BYTENUM( p ); p += 2;
916 t.tm_sec = GET2BYTENUM( p ); p += 2;
918 if (( zone = *p ) == 'Z' ) { /* GMT */
919 zone = '\0'; /* no need to indicate on screen, so we make it null */
922 gmttime = gtime( &t );
924 timestr = ldap_pvt_ctime( &gmttime, timebuf );
926 timestr[ strlen( timestr ) - 1 ] = zone; /* replace trailing newline */
928 SAFEMEMCPY( timestr + 11, timestr + 20, strlen( timestr + 20 ) + 1 );
936 /* gtime.c - inverse gmtime */
937 /* gtime(): the inverse of localtime().
938 This routine was supplied by Mike Accetta at CMU many years ago.
941 static const int dmsize[] = {
942 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
946 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
951 /* per POSIX tm_year should be offset by 1900 */
952 #define YEAR_POSIX(y) ((y) + 1900)
955 * year is < 1900, year is offset by 1900
957 #define YEAR_CAREFUL(y) ((y) < 1900 ? (y) + 1900 : (y))
960 ** if year is < 1990 and < 70 must be offset by 2000 as Unix epoch
961 ** started in 1970. if year is < 1990 but >= 70, offset by 1900.
962 ** if year is >= 1900, it must be the real year.
964 #define YEAR_PEDANTIC(y) ((y) < 1900 \
965 ? ((y) < 70 ? (y) + 2000 ? (y) + 1900) \
968 #define YEAR(y) YEAR_CAREFUL(y)
972 static long gtime ( struct tm *tm )
981 register long result;
983 if ((sec = tm -> tm_sec) < 0 || sec > 59
984 || (mins = tm -> tm_min) < 0 || mins > 59
985 || (hour = tm -> tm_hour) < 0 || hour > 24
986 || (mday = tm -> tm_mday) < 1 || mday > 31
987 || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
993 year = YEAR (tm -> tm_year);
996 for (i = 1970; i < year; i++)
997 result += dysize (i);
998 if (dysize (year) == 366 && mon >= 3)
1001 result += dmsize[mon - 1];
1003 result = 24 * result + hour;
1004 result = 60 * result + mins;
1005 result = 60 * result + sec;
1011 searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
1012 struct ldap_tmplitem *tip, int labelwidth, int rdncount,
1013 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
1015 int err = 0, lderr, i, count, html;
1016 char **vals, **members;
1017 char *value, *filtpattern, *attr, *selectname;
1018 char *retattrs[2], filter[ 256 ];
1020 struct timeval timeout;
1022 html = ( urlprefix != NULL );
1024 for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
1028 return( LDAP_PARAM_ERROR );
1030 attr = tip->ti_args[ 0 ];
1031 filtpattern = tip->ti_args[ 1 ];
1032 retattrs[ 0 ] = tip->ti_args[ 2 ];
1033 retattrs[ 1 ] = NULL;
1034 selectname = tip->ti_args[ 3 ];
1037 if ( attr == NULL ) {
1039 } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
1040 return( LDAP_PARAM_ERROR );
1041 } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
1043 } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
1049 ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
1054 * if we are generating HTML, we add an HREF link that embodies this
1055 * search action as an LDAP URL, instead of actually doing the search
1058 sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
1059 if ( base != NULL ) {
1060 strcat_escaped( buf, base );
1062 strcat( buf, "??sub?" );
1063 strcat_escaped( buf, filter );
1064 sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
1065 tip->ti_label, eol );
1066 if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
1067 return( LDAP_LOCAL_ERROR );
1069 return( LDAP_SUCCESS );
1072 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1073 timeout.tv_usec = 0;
1075 #ifdef LDAP_CONNECTIONLESS
1076 if ( LDAP_IS_CLDAP( ld ))
1077 lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1080 #endif /* LDAP_CONNECTIONLESS */
1081 lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1082 0, &timeout, &ldmp );
1084 if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
1085 if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
1086 if (( members = (char **)LDAP_MALLOC( (count + 1) * sizeof(char *)))
1088 err = LDAP_NO_MEMORY;
1090 for ( i = 0, entry = ldap_first_entry( ld, ldmp );
1092 entry = ldap_next_entry( ld, entry ), ++i ) {
1093 members[ i ] = ldap_get_dn( ld, entry );
1095 members[ i ] = NULL;
1097 ldap_sort_values( ld, members, ldap_sort_strcasecmp );
1099 err = do_vals2text( ld, NULL, members, tip->ti_label,
1100 html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
1101 eol, rdncount, urlprefix );
1103 ldap_value_free( members );
1106 ldap_msgfree( ldmp );
1110 if ( vals != NULL ) {
1111 ldap_value_free( vals );
1114 return(( err == LDAP_SUCCESS ) ? lderr : err );