3 * Copyright 1998-1999 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;
478 #ifdef LDAP_CONNECTIONLESS
479 if ( LDAP_IS_CLDAP( ld ))
480 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE,
481 "objectClass=*", ocattrs, 0, &ldmp, NULL );
483 #endif /* LDAP_CONNECTIONLESS */
484 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
485 "objectClass=*", ocattrs, 0, &timeout, &ldmp );
487 if ( err == LDAP_SUCCESS ) {
488 entry = ldap_first_entry( ld, ldmp );
492 if ( entry != NULL ) {
493 vals = ldap_get_values( ld, entry, OCATTRNAME );
494 tmpl = ldap_oc2template( vals, tmpllist );
495 if ( vals != NULL ) {
496 ldap_value_free( vals );
499 if ( ldmp != NULL ) {
500 ldap_msgfree( ldmp );
506 if ( tmpl == NULL ) {
509 fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
512 #ifdef LDAP_CONNECTIONLESS
513 if ( LDAP_IS_CLDAP( ld ))
514 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
515 fetchattrs, 0, &ldmp, NULL );
517 #endif /* LDAP_CONNECTIONLESS */
518 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
519 fetchattrs, 0, &timeout, &ldmp );
524 if ( fetchattrs != NULL ) {
525 ldap_value_free( fetchattrs );
528 if ( err != LDAP_SUCCESS ||
529 ( entry = ldap_first_entry( ld, ldmp )) == NULL ) {
531 ldap_free_templates( tmpllist );
534 return( ld->ld_errno );
537 err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
538 writeproc, writeparm, eol, rdncount, opts, urlprefix );
542 ldap_free_templates( tmpllist );
544 ldap_msgfree( ldmp );
552 char *buf, /* NULL for "use internal" */
555 int labelwidth, /* 0 means use default */
556 unsigned long syntaxid,
557 writeptype writeproc,
563 Debug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
565 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
566 writeproc, writeparm, eol, rdncount, NULL ));
573 char *buf, /* NULL for "use internal" */
576 int labelwidth, /* 0 means use default */
577 unsigned long syntaxid,
578 writeptype writeproc,
585 Debug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
587 if ( urlprefix == NULL ) {
588 urlprefix = DEF_LDAP_URL_PREFIX;
591 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
592 writeproc, writeparm, eol, rdncount, urlprefix ));
599 char *buf, /* NULL for "use internal" */
602 int labelwidth, /* 0 means use default */
603 unsigned long syntaxid,
604 writeptype writeproc,
611 int i, html, writeoutval, freebuf, notascii;
612 char *p, *s, *outval;
615 if ( vals == NULL ) {
616 return( LDAP_SUCCESS );
619 html = ( urlprefix != NULL );
621 switch( LDAP_GET_SYN_TYPE( syntaxid )) {
622 case LDAP_SYN_TYPE_TEXT:
623 case LDAP_SYN_TYPE_BOOLEAN:
624 break; /* we only bother with these two types... */
626 return( LDAP_SUCCESS );
629 if ( labelwidth == 0 || labelwidth < 0 ) {
630 labelwidth = DEF_LABEL_WIDTH;
634 if (( buf = LDAP_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
635 ld->ld_errno = LDAP_NO_MEMORY;
636 return( ld->ld_errno );
643 output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
645 for ( i = 0; vals[ i ] != NULL; ++i ) {
646 for ( p = vals[ i ]; *p != '\0'; ++p ) {
647 if ( !isascii( *p )) {
651 notascii = ( *p != '\0' );
652 outval = notascii ? "(unable to display non-ASCII text value)"
655 writeoutval = 0; /* if non-zero, write outval after switch */
658 case LDAP_SYN_CASEIGNORESTR:
662 case LDAP_SYN_RFC822ADDR:
664 strcpy( buf, "<DD><A HREF=\"mailto:" );
665 strcat_escaped( buf, outval );
666 sprintf( buf + strlen( buf ), "\">%s</A><BR>%s", outval, eol );
667 (*writeproc)( writeparm, buf, strlen( buf ));
673 case LDAP_SYN_DN: /* for now */
674 output_dn( buf, outval, labelwidth, rdncount, writeproc,
675 writeparm, eol, urlprefix );
678 case LDAP_SYN_MULTILINESTR:
679 if ( i > 0 && !html ) {
680 output_label( buf, label, labelwidth, writeproc,
681 writeparm, eol, html );
685 while (( s = strchr( s, '$' )) != NULL ) {
687 while ( isspace( (unsigned char) *s )) {
691 sprintf( buf, "<DD>%s<BR>%s", p, eol );
693 sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
695 (*writeproc)( writeparm, buf, strlen( buf ));
702 case LDAP_SYN_BOOLEAN:
703 outval = TOUPPER((unsigned char) outval[0]) == 'T' ? "TRUE" : "FALSE";
709 outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
713 case LDAP_SYN_LABELEDURL:
714 if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
716 while ( isspace( (unsigned char) *p )) {
720 } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
722 while ( isspace( (unsigned char) *s )) {
732 * at this point `s' points to the label & `p' to the URL
735 sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
737 sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
738 s, eol, labelwidth + 2, " ",p , eol );
740 (*writeproc)( writeparm, buf, strlen( buf ));
744 sprintf( buf, " Can't display item type %ld%s",
746 (*writeproc)( writeparm, buf, strlen( buf ));
751 sprintf( buf, "<DD>%s<BR>%s", outval, eol );
753 sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
755 (*writeproc)( writeparm, buf, strlen( buf ));
763 return( LDAP_SUCCESS );
768 max_label_len( struct ldap_disptmpl *tmpl )
770 struct ldap_tmplitem *rowp, *colp;
775 for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULL;
776 rowp = ldap_next_tmplrow( tmpl, rowp )) {
777 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULL;
778 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
779 if (( len = strlen( colp->ti_label )) > maxlen ) {
790 output_label( char *buf, char *label, int width, writeptype writeproc,
791 void *writeparm, char *eol, int html )
796 sprintf( buf, "<DT><B>%s</B>", label );
798 sprintf( buf, " %s:", label );
799 p = buf + strlen( buf );
801 while ( p - buf < width ) {
809 return ((*writeproc)( writeparm, buf, strlen( buf )));
814 output_dn( char *buf, char *dn, int width, int rdncount,
815 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
820 if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
824 if ( urlprefix != NULL ) {
825 sprintf( buf, "<DD><A HREF=\"%s", urlprefix );
826 strcat_escaped( buf, dn );
827 strcat( buf, "\">" );
828 } else if ( width > 0 ) {
829 sprintf( buf, "%-*s", width, " " );
834 for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
839 strcat( buf, dnrdns[ i ] );
842 if ( urlprefix != NULL ) {
843 strcat( buf, "</A><BR>" );
846 ldap_value_free( dnrdns );
850 return ((*writeproc)( writeparm, buf, strlen( buf )));
855 #define HREF_CHAR_ACCEPTABLE( c ) (( (c) >= '-' && (c) <= '9' ) || \
856 ( (c) >= '@' && (c) <= 'Z' ) || \
858 ( (c) >= 'a' && (c) <= 'z' ))
861 strcat_escaped( char *s1, char *s2 )
864 char *hexdig = "0123456789ABCDEF";
866 p = s1 + strlen( s1 );
867 for ( q = s2; *q != '\0'; ++q ) {
868 if ( HREF_CHAR_ACCEPTABLE( *q )) {
872 *p++ = hexdig[ *q >> 4 ];
873 *p++ = hexdig[ *q & 0x0F ];
881 #define GET2BYTENUM( p ) (( *(p) - '0' ) * 10 + ( *((p)+1) - '0' ))
884 time2text( char *ldtimestr, int dateonly )
887 char *p, *timestr, zone, *fmterr = "badly formatted time";
892 if (strlen( ldtimestr ) < 12 ) {
896 for ( ndigits=0; isdigit((unsigned char) ldtimestr[ndigits]); ndigits++) {
900 if ( ndigits != 12 && ndigits != 14) {
904 memset( (char *)&t, 0, sizeof( struct tm ));
909 /* came with a century */
910 /* POSIX says tm_year should be year - 1900 */
911 t.tm_year = 100 * GET2BYTENUM( p ) - 1900;
913 t.tm_year += GET2BYTENUM( p ); p += 2;
916 /* came without a century */
917 t.tm_year = GET2BYTENUM( p ); p += 2;
919 /* Y2K hack - 2 digit years < 70 are 21st century */
920 if( t.tm_year < 70 ) {
925 t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
926 t.tm_mday = GET2BYTENUM( p ); p += 2;
927 t.tm_hour = GET2BYTENUM( p ); p += 2;
928 t.tm_min = GET2BYTENUM( p ); p += 2;
929 t.tm_sec = GET2BYTENUM( p ); p += 2;
931 if (( zone = *p ) == 'Z' ) { /* GMT */
932 zone = '\0'; /* no need to indicate on screen, so we make it null */
935 gmttime = gtime( &t );
937 timestr = ldap_pvt_ctime( &gmttime, timebuf );
939 timestr[ strlen( timestr ) - 1 ] = zone; /* replace trailing newline */
941 SAFEMEMCPY( timestr + 11, timestr + 20, strlen( timestr + 20 ) + 1 );
949 /* gtime.c - inverse gmtime */
950 /* gtime(): the inverse of localtime().
951 This routine was supplied by Mike Accetta at CMU many years ago.
954 static const int dmsize[] = {
955 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
959 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
964 /* per STDC & POSIX tm_year should be offset by 1900 */
965 #define YEAR_POSIX(y) ((y) + 1900)
968 * year is < 1900, year is offset by 1900
970 #define YEAR_CAREFUL(y) ((y) < 1900 ? (y) + 1900 : (y))
972 #define YEAR(y) YEAR_CAREFUL(y)
976 static long gtime ( struct tm *tm )
985 register long result;
987 if ((sec = tm -> tm_sec) < 0 || sec > 59
988 || (mins = tm -> tm_min) < 0 || mins > 59
989 || (hour = tm -> tm_hour) < 0 || hour > 24
990 || (mday = tm -> tm_mday) < 1 || mday > 31
991 || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
997 year = YEAR (tm -> tm_year);
1000 for (i = 1970; i < year; i++)
1001 result += dysize (i);
1002 if (dysize (year) == 366 && mon >= 3)
1005 result += dmsize[mon - 1];
1007 result = 24 * result + hour;
1008 result = 60 * result + mins;
1009 result = 60 * result + sec;
1015 searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
1016 struct ldap_tmplitem *tip, int labelwidth, int rdncount,
1017 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
1019 int err = 0, lderr, i, count, html;
1020 char **vals, **members;
1021 char *value, *filtpattern, *attr, *selectname;
1022 char *retattrs[2], filter[ 256 ];
1024 struct timeval timeout;
1026 html = ( urlprefix != NULL );
1028 for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
1032 return( LDAP_PARAM_ERROR );
1034 attr = tip->ti_args[ 0 ];
1035 filtpattern = tip->ti_args[ 1 ];
1036 retattrs[ 0 ] = tip->ti_args[ 2 ];
1037 retattrs[ 1 ] = NULL;
1038 selectname = tip->ti_args[ 3 ];
1041 if ( attr == NULL ) {
1043 } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
1044 return( LDAP_PARAM_ERROR );
1045 } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
1047 } else if ( strcasecmp( attr, "-dn" ) == 0 ) {
1049 } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
1055 ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
1060 * if we are generating HTML, we add an HREF link that embodies this
1061 * search action as an LDAP URL, instead of actually doing the search
1064 sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
1065 if ( base != NULL ) {
1066 strcat_escaped( buf, base );
1068 strcat( buf, "??sub?" );
1069 strcat_escaped( buf, filter );
1070 sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
1071 tip->ti_label, eol );
1072 if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
1073 return( LDAP_LOCAL_ERROR );
1075 return( LDAP_SUCCESS );
1078 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1079 timeout.tv_usec = 0;
1081 #ifdef LDAP_CONNECTIONLESS
1082 if ( LDAP_IS_CLDAP( ld ))
1083 lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1086 #endif /* LDAP_CONNECTIONLESS */
1087 lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1088 0, &timeout, &ldmp );
1090 if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
1091 if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
1092 if (( members = (char **)LDAP_MALLOC( (count + 1) * sizeof(char *)))
1094 err = LDAP_NO_MEMORY;
1096 for ( i = 0, entry = ldap_first_entry( ld, ldmp );
1098 entry = ldap_next_entry( ld, entry ), ++i ) {
1099 members[ i ] = ldap_get_dn( ld, entry );
1101 members[ i ] = NULL;
1103 ldap_sort_values( ld, members, ldap_sort_strcasecmp );
1105 err = do_vals2text( ld, NULL, members, tip->ti_label,
1106 html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
1107 eol, rdncount, urlprefix );
1109 ldap_value_free( members );
1112 ldap_msgfree( ldmp );
1116 if ( vals != NULL ) {
1117 ldap_value_free( vals );
1120 return(( err == LDAP_SUCCESS ) ? lderr : err );