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
14 #include <ac/stdlib.h>
17 #include <ac/string.h>
20 #ifdef HAVE_SYS_FILE_H
31 #include "ldap_defaults.h"
33 #define writeptype ldap_writeptype
36 static int do_entry2text LDAP_P((
37 LDAP *ld, char *buf, char *base, LDAPMessage *entry,
38 struct ldap_disptmpl *tmpl, char **defattrs, char ***defvals,
39 writeptype writeproc, void *writeparm, char *eol, int rdncount,
40 unsigned long opts, char *urlprefix ));
41 static int do_entry2text_search LDAP_P((
42 LDAP *ld, char *dn, char *base,
43 LDAPMessage *entry, struct ldap_disptmpl *tmpllist, char **defattrs,
44 char ***defvals, writeptype writeproc, void *writeparm, char *eol,
45 int rdncount, unsigned long opts, char *urlprefix ));
46 static int do_vals2text LDAP_P((
47 LDAP *ld, char *buf, char **vals, char *label,
48 int labelwidth, unsigned long syntaxid, writeptype writeproc,
49 void *writeparm, char *eol, int rdncount, char *urlprefix ));
50 static int max_label_len LDAP_P(( struct ldap_disptmpl *tmpl ));
51 static int output_label LDAP_P((
52 char *buf, char *label, int width,
53 writeptype writeproc, void *writeparm, char *eol, int html ));
54 static int output_dn LDAP_P((
55 char *buf, char *dn, int width, int rdncount,
56 writeptype writeproc, void *writeparm, char *eol, char *urlprefix ));
57 static void strcat_escaped LDAP_P(( char *s1, char *s2 ));
58 static char *time2text LDAP_P(( char *ldtimestr, int dateonly ));
59 static long gtime LDAP_P(( struct tm *tm ));
60 static int searchaction LDAP_P((
61 LDAP *ld, char *buf, char *base, LDAPMessage *entry,
62 char *dn, struct ldap_tmplitem *tip, int labelwidth, int rdncount,
63 writeptype writeproc, void *writeparm, char *eol, char *urlprefix ));
65 #define DEF_LABEL_WIDTH 15
66 #define SEARCH_TIMEOUT_SECS 120
67 #define OCATTRNAME "objectClass"
70 #define NONFATAL_LDAP_ERR( err ) ( (err) == LDAP_SUCCESS || \
71 (err) == LDAP_TIMELIMIT_EXCEEDED || (err) == LDAP_SIZELIMIT_EXCEEDED )
73 #define DEF_LDAP_URL_PREFIX "ldap:///"
79 char *buf, /* NULL for "use internal" */
81 struct ldap_disptmpl *tmpl,
91 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text\n", 0, 0, 0 );
93 return( do_entry2text( ld, buf, NULL, entry, tmpl, defattrs, defvals,
94 writeproc, writeparm, eol, rdncount, opts, NULL ));
103 char *buf, /* NULL for "use internal" */
105 struct ldap_disptmpl *tmpl,
108 writeptype writeproc,
117 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html\n", 0, 0, 0 );
119 if ( urlprefix == NULL ) {
120 urlprefix = DEF_LDAP_URL_PREFIX;
123 return( do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
124 writeproc, writeparm, eol, rdncount, opts, urlprefix ));
131 char *buf, /* NULL for use-internal */
132 char *base, /* used for search actions */
134 struct ldap_disptmpl *tmpl,
137 writeptype writeproc,
142 char *urlprefix /* if non-NULL, do HTML */
145 int i, err, html, show, labelwidth;
146 int freebuf, freevals;
148 struct ldap_tmplitem *rowp, *colp;
150 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
151 return( ld->ld_errno );
155 if (( buf = LDAP_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
156 ld->ld_errno = LDAP_NO_MEMORY;
158 return( ld->ld_errno );
165 html = ( urlprefix != NULL );
169 * add HTML intro. and title
171 if (!(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
172 sprintf( buf, "<HTML>%s<HEAD>%s<TITLE>%s%s - ", eol, eol, eol,
173 ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
174 (*writeproc)( writeparm, buf, strlen( buf ));
175 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
176 sprintf( buf, "%s</TITLE>%s</HEAD>%s<BODY>%s<H3>%s - ", eol, eol,
177 eol, eol, ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
178 (*writeproc)( writeparm, buf, strlen( buf ));
179 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
180 sprintf( buf, "</H3>%s", eol );
181 (*writeproc)( writeparm, buf, strlen( buf ));
184 if (( opts & LDAP_DISP_OPT_NONLEAF ) != 0 &&
185 ( vals = ldap_explode_dn( dn, 0 )) != NULL ) {
191 sprintf( buf, "<A HREF=\"%s", urlprefix );
192 for ( i = 1; vals[ i ] != NULL; ++i ) {
194 strcat_escaped( buf, ", " );
196 strcat_escaped( buf, vals[ i ] );
198 if ( vals[ 1 ] != NULL ) {
199 untagged = strchr( vals[ 1 ], '=' );
201 untagged = "=The World";
203 sprintf( buf + strlen( buf ),
204 "%s\">Move Up To <EM>%s</EM></A>%s<BR>",
205 ( vals[ 1 ] == NULL ) ? "??one" : "",
206 ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol );
207 (*writeproc)( writeparm, buf, strlen( buf ));
212 untagged = strchr( vals[ 0 ], '=' );
213 sprintf( buf, "<A HREF=\"%s", urlprefix );
214 strcat_escaped( buf, dn );
215 sprintf( buf + strlen( buf ), "??one?(!(objectClass=dsa))\">Browse Below <EM>%s</EM></A>%s%s",
216 ( untagged != NULL ) ? untagged + 1 : vals[ 0 ], eol, eol );
217 (*writeproc)( writeparm, buf, strlen( buf ));
219 ldap_value_free( vals );
222 (*writeproc)( writeparm, "<HR>", 4 ); /* horizontal rule */
224 (*writeproc)( writeparm, "\"", 1 );
225 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
226 sprintf( buf, "\"%s", eol );
227 (*writeproc)( writeparm, buf, strlen( buf ));
230 if ( tmpl != NULL && ( opts & LDAP_DISP_OPT_AUTOLABELWIDTH ) != 0 ) {
231 labelwidth = max_label_len( tmpl ) + 3;
233 labelwidth = DEF_LABEL_WIDTH;;
238 if ( tmpl == NULL ) {
243 for ( attr = ldap_first_attribute( ld, entry, &ber );
244 NONFATAL_LDAP_ERR( err ) && attr != NULL;
245 attr = ldap_next_attribute( ld, entry, ber )) {
246 if (( vals = ldap_get_values( ld, entry, attr )) == NULL ) {
248 if ( defattrs != NULL ) {
249 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
250 if ( strcasecmp( attr, defattrs[ i ] ) == 0 ) {
254 if ( defattrs[ i ] != NULL ) {
262 *attr = TOUPPER( (unsigned char) *attr );
264 err = do_vals2text( ld, buf, vals, attr, labelwidth,
265 LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol,
266 rdncount, urlprefix );
268 ldap_value_free( vals );
276 for ( rowp = ldap_first_tmplrow( tmpl );
277 NONFATAL_LDAP_ERR( err ) && rowp != NULL;
278 rowp = ldap_next_tmplrow( tmpl, rowp )) {
279 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULL;
280 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
282 if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
283 entry, colp->ti_attrname )) == NULL ) {
285 if ( !LDAP_IS_TMPLITEM_OPTION_SET( colp,
286 LDAP_DITEM_OPT_HIDEIFEMPTY ) && defattrs != NULL
287 && colp->ti_attrname != NULL ) {
288 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
289 if ( strcasecmp( colp->ti_attrname, defattrs[ i ] )
294 if ( defattrs[ i ] != NULL ) {
300 if ( LDAP_IS_TMPLITEM_OPTION_SET( colp,
301 LDAP_DITEM_OPT_SORTVALUES ) && vals[ 0 ] != NULL
302 && vals[ 1 ] != NULL ) {
303 ldap_sort_values( ld, vals, ldap_sort_strcasecmp );
308 * don't bother even calling do_vals2text() if no values
309 * or boolean with value false and "hide if false" option set
311 show = ( vals != NULL && vals[ 0 ] != NULL );
312 if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
313 == LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
314 colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
315 TOUPPER( (unsigned char) vals[ 0 ][ 0 ] ) != 'T' ) {
319 if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
320 if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
321 if ( colp->ti_attrname == NULL || ( show &&
322 TOUPPER( (unsigned char) vals[ 0 ][ 0 ] ) == 'T' )) {
323 err = searchaction( ld, buf, base, entry, dn, colp,
324 labelwidth, rdncount, writeproc,
325 writeparm, eol, urlprefix );
332 err = do_vals2text( ld, buf, vals, colp->ti_label,
333 labelwidth, colp->ti_syntaxid, writeproc, writeparm,
334 eol, rdncount, urlprefix );
338 ldap_value_free( vals );
344 if ( html && !(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
345 sprintf( buf, "</BODY>%s</HTML>%s", eol, eol );
346 (*writeproc)( writeparm, buf, strlen( buf ));
359 ldap_entry2text_search(
361 char *dn, /* if NULL, use entry */
362 char *base, /* if NULL, no search actions */
363 LDAPMessage *entry, /* if NULL, use dn */
364 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
367 writeptype writeproc,
370 int rdncount, /* if 0, display full DN */
374 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text_search\n", 0, 0, 0 );
376 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
377 defvals, writeproc, writeparm, eol, rdncount, opts, NULL ));
383 ldap_entry2html_search(
385 char *dn, /* if NULL, use entry */
386 char *base, /* if NULL, no search actions */
387 LDAPMessage *entry, /* if NULL, use dn */
388 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
391 writeptype writeproc,
394 int rdncount, /* if 0, display full DN */
399 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html_search\n", 0, 0, 0 );
401 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
402 defvals, writeproc, writeparm, eol, rdncount, opts, urlprefix ));
407 do_entry2text_search(
409 char *dn, /* if NULL, use entry */
410 char *base, /* if NULL, no search actions */
411 LDAPMessage *entry, /* if NULL, use dn */
412 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
415 writeptype writeproc,
418 int rdncount, /* if 0, display full DN */
423 int err, freedn, freetmpls, html;
424 char *buf, **fetchattrs, **vals;
426 struct ldap_disptmpl *tmpl;
427 struct timeval timeout;
429 if ( dn == NULL && entry == NULL ) {
430 ld->ld_errno = LDAP_PARAM_ERROR;
431 return( ld->ld_errno );
434 html = ( urlprefix != NULL );
436 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
439 if (( buf = LDAP_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
440 ld->ld_errno = LDAP_NO_MEMORY;
441 return( ld->ld_errno );
444 freedn = freetmpls = 0;
447 if ( tmpllist == NULL ) {
448 if (( err = ldap_init_templates( TEMPLATEFILE, &tmpllist )) != 0 ) {
449 sprintf( buf, "%sUnable to read template file %s (error %d)%s%s",
450 html ? "<!-- " : "", TEMPLATEFILE, err,
451 html ? "-->" : "", eol );
452 (*writeproc)( writeparm, buf, strlen( buf ));
458 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
461 ldap_free_templates( tmpllist );
463 return( ld->ld_errno );
469 if ( tmpllist != NULL ) {
472 if ( entry == NULL ) {
475 ocattrs[0] = OCATTRNAME;
477 #ifdef LDAP_CONNECTIONLESS
478 if ( LDAP_IS_CLDAP( ld ))
479 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE,
480 "objectClass=*", ocattrs, 0, &ldmp, NULL );
482 #endif /* LDAP_CONNECTIONLESS */
483 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
484 "objectClass=*", ocattrs, 0, &timeout, &ldmp );
486 if ( err == LDAP_SUCCESS ) {
487 entry = ldap_first_entry( ld, ldmp );
491 if ( entry != NULL ) {
492 vals = ldap_get_values( ld, entry, OCATTRNAME );
493 tmpl = ldap_oc2template( vals, tmpllist );
494 if ( vals != NULL ) {
495 ldap_value_free( vals );
498 if ( ldmp != NULL ) {
499 ldap_msgfree( ldmp );
505 if ( tmpl == NULL ) {
508 fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
511 #ifdef LDAP_CONNECTIONLESS
512 if ( LDAP_IS_CLDAP( ld ))
513 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
514 fetchattrs, 0, &ldmp, NULL );
516 #endif /* LDAP_CONNECTIONLESS */
517 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
518 fetchattrs, 0, &timeout, &ldmp );
523 if ( fetchattrs != NULL ) {
524 ldap_value_free( fetchattrs );
527 if ( err != LDAP_SUCCESS ||
528 ( entry = ldap_first_entry( ld, ldmp )) == NULL ) {
530 ldap_free_templates( tmpllist );
533 return( ld->ld_errno );
536 err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
537 writeproc, writeparm, eol, rdncount, opts, urlprefix );
541 ldap_free_templates( tmpllist );
543 ldap_msgfree( ldmp );
551 char *buf, /* NULL for "use internal" */
554 int labelwidth, /* 0 means use default */
555 unsigned long syntaxid,
556 writeptype writeproc,
562 Debug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
564 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
565 writeproc, writeparm, eol, rdncount, NULL ));
572 char *buf, /* NULL for "use internal" */
575 int labelwidth, /* 0 means use default */
576 unsigned long syntaxid,
577 writeptype writeproc,
584 Debug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
586 if ( urlprefix == NULL ) {
587 urlprefix = DEF_LDAP_URL_PREFIX;
590 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
591 writeproc, writeparm, eol, rdncount, urlprefix ));
598 char *buf, /* NULL for "use internal" */
601 int labelwidth, /* 0 means use default */
602 unsigned long syntaxid,
603 writeptype writeproc,
610 int i, html, writeoutval, freebuf, notascii;
611 char *p, *s, *outval;
614 if ( vals == NULL ) {
615 return( LDAP_SUCCESS );
618 html = ( urlprefix != NULL );
620 switch( LDAP_GET_SYN_TYPE( syntaxid )) {
621 case LDAP_SYN_TYPE_TEXT:
622 case LDAP_SYN_TYPE_BOOLEAN:
623 break; /* we only bother with these two types... */
625 return( LDAP_SUCCESS );
628 if ( labelwidth == 0 || labelwidth < 0 ) {
629 labelwidth = DEF_LABEL_WIDTH;
633 if (( buf = LDAP_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
634 ld->ld_errno = LDAP_NO_MEMORY;
635 return( ld->ld_errno );
642 output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
644 for ( i = 0; vals[ i ] != NULL; ++i ) {
645 for ( p = vals[ i ]; *p != '\0'; ++p ) {
646 if ( !isascii( *p )) {
650 notascii = ( *p != '\0' );
651 outval = notascii ? "(unable to display non-ASCII text value)"
654 writeoutval = 0; /* if non-zero, write outval after switch */
657 case LDAP_SYN_CASEIGNORESTR:
661 case LDAP_SYN_RFC822ADDR:
663 strcpy( buf, "<DD><A HREF=\"mailto:" );
664 strcat_escaped( buf, outval );
665 sprintf( buf + strlen( buf ), "\">%s</A><BR>%s", outval, eol );
666 (*writeproc)( writeparm, buf, strlen( buf ));
672 case LDAP_SYN_DN: /* for now */
673 output_dn( buf, outval, labelwidth, rdncount, writeproc,
674 writeparm, eol, urlprefix );
677 case LDAP_SYN_MULTILINESTR:
678 if ( i > 0 && !html ) {
679 output_label( buf, label, labelwidth, writeproc,
680 writeparm, eol, html );
684 while (( s = strchr( s, '$' )) != NULL ) {
686 while ( isspace( (unsigned char) *s )) {
690 sprintf( buf, "<DD>%s<BR>%s", p, eol );
692 sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
694 (*writeproc)( writeparm, buf, strlen( buf ));
701 case LDAP_SYN_BOOLEAN:
702 outval = TOUPPER((unsigned char) outval[0]) == 'T' ? "TRUE" : "FALSE";
708 outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
712 case LDAP_SYN_LABELEDURL:
713 if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
715 while ( isspace( (unsigned char) *p )) {
719 } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
721 while ( isspace( (unsigned char) *s )) {
731 * at this point `s' points to the label & `p' to the URL
734 sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
736 sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
737 s, eol, labelwidth + 2, " ",p , eol );
739 (*writeproc)( writeparm, buf, strlen( buf ));
743 sprintf( buf, " Can't display item type %ld%s",
745 (*writeproc)( writeparm, buf, strlen( buf ));
750 sprintf( buf, "<DD>%s<BR>%s", outval, eol );
752 sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
754 (*writeproc)( writeparm, buf, strlen( buf ));
762 return( LDAP_SUCCESS );
767 max_label_len( struct ldap_disptmpl *tmpl )
769 struct ldap_tmplitem *rowp, *colp;
774 for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULL;
775 rowp = ldap_next_tmplrow( tmpl, rowp )) {
776 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULL;
777 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
778 if (( len = strlen( colp->ti_label )) > maxlen ) {
789 output_label( char *buf, char *label, int width, writeptype writeproc,
790 void *writeparm, char *eol, int html )
795 sprintf( buf, "<DT><B>%s</B>", label );
797 sprintf( buf, " %s:", label );
798 p = buf + strlen( buf );
800 while ( p - buf < width ) {
808 return ((*writeproc)( writeparm, buf, strlen( buf )));
813 output_dn( char *buf, char *dn, int width, int rdncount,
814 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
819 if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
823 if ( urlprefix != NULL ) {
824 sprintf( buf, "<DD><A HREF=\"%s", urlprefix );
825 strcat_escaped( buf, dn );
826 strcat( buf, "\">" );
827 } else if ( width > 0 ) {
828 sprintf( buf, "%-*s", width, " " );
833 for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
838 strcat( buf, dnrdns[ i ] );
841 if ( urlprefix != NULL ) {
842 strcat( buf, "</A><BR>" );
845 ldap_value_free( dnrdns );
849 return ((*writeproc)( writeparm, buf, strlen( buf )));
854 #define HREF_CHAR_ACCEPTABLE( c ) (( (c) >= '-' && (c) <= '9' ) || \
855 ( (c) >= '@' && (c) <= 'Z' ) || \
857 ( (c) >= 'a' && (c) <= 'z' ))
860 strcat_escaped( char *s1, char *s2 )
863 char *hexdig = "0123456789ABCDEF";
865 p = s1 + strlen( s1 );
866 for ( q = s2; *q != '\0'; ++q ) {
867 if ( HREF_CHAR_ACCEPTABLE( *q )) {
871 *p++ = hexdig[ *q >> 4 ];
872 *p++ = hexdig[ *q & 0x0F ];
880 #define GET2BYTENUM( p ) (( *(p) - '0' ) * 10 + ( *((p)+1) - '0' ))
883 time2text( char *ldtimestr, int dateonly )
886 char *p, *timestr, zone, *fmterr = "badly formatted time";
891 if (strlen( ldtimestr ) < 12 ) {
895 for ( ndigits=0; isdigit((unsigned char) ldtimestr[ndigits]); ndigits++) {
899 if ( ndigits != 12 && ndigits != 14) {
903 memset( (char *)&t, 0, sizeof( struct tm ));
908 /* came with a century */
909 /* POSIX says tm_year should be year - 1900 */
910 t.tm_year = 100 * GET2BYTENUM( p ) - 1900;
913 t.tm_year = GET2BYTENUM( p ); p += 2;
915 t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
916 t.tm_mday = GET2BYTENUM( p ); p += 2;
917 t.tm_hour = GET2BYTENUM( p ); p += 2;
918 t.tm_min = GET2BYTENUM( p ); p += 2;
919 t.tm_sec = GET2BYTENUM( p ); p += 2;
921 if (( zone = *p ) == 'Z' ) { /* GMT */
922 zone = '\0'; /* no need to indicate on screen, so we make it null */
925 gmttime = gtime( &t );
927 timestr = ldap_pvt_ctime( &gmttime, timebuf );
929 timestr[ strlen( timestr ) - 1 ] = zone; /* replace trailing newline */
931 SAFEMEMCPY( timestr + 11, timestr + 20, strlen( timestr + 20 ) + 1 );
939 /* gtime.c - inverse gmtime */
940 /* gtime(): the inverse of localtime().
941 This routine was supplied by Mike Accetta at CMU many years ago.
944 static const int dmsize[] = {
945 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
949 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
954 /* per STDC & POSIX tm_year should be offset by 1900 */
955 #define YEAR_POSIX(y) ((y) + 1900)
958 * year is < 1900, year is offset by 1900
960 #define YEAR_CAREFUL(y) ((y) < 1900 ? (y) + 1900 : (y))
962 #define YEAR(y) YEAR_CAREFUL(y)
966 static long gtime ( struct tm *tm )
975 register long result;
977 if ((sec = tm -> tm_sec) < 0 || sec > 59
978 || (mins = tm -> tm_min) < 0 || mins > 59
979 || (hour = tm -> tm_hour) < 0 || hour > 24
980 || (mday = tm -> tm_mday) < 1 || mday > 31
981 || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
987 year = YEAR (tm -> tm_year);
990 for (i = 1970; i < year; i++)
991 result += dysize (i);
992 if (dysize (year) == 366 && mon >= 3)
995 result += dmsize[mon - 1];
997 result = 24 * result + hour;
998 result = 60 * result + mins;
999 result = 60 * result + sec;
1005 searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
1006 struct ldap_tmplitem *tip, int labelwidth, int rdncount,
1007 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
1009 int err = 0, lderr, i, count, html;
1010 char **vals, **members;
1011 char *value, *filtpattern, *attr, *selectname;
1012 char *retattrs[2], filter[ 256 ];
1014 struct timeval timeout;
1016 html = ( urlprefix != NULL );
1018 for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
1022 return( LDAP_PARAM_ERROR );
1024 attr = tip->ti_args[ 0 ];
1025 filtpattern = tip->ti_args[ 1 ];
1026 retattrs[ 0 ] = tip->ti_args[ 2 ];
1027 retattrs[ 1 ] = NULL;
1028 selectname = tip->ti_args[ 3 ];
1031 if ( attr == NULL ) {
1033 } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
1034 return( LDAP_PARAM_ERROR );
1035 } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
1037 } else if ( strcasecmp( attr, "-dn" ) == 0 ) {
1039 } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
1045 ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
1050 * if we are generating HTML, we add an HREF link that embodies this
1051 * search action as an LDAP URL, instead of actually doing the search
1054 sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
1055 if ( base != NULL ) {
1056 strcat_escaped( buf, base );
1058 strcat( buf, "??sub?" );
1059 strcat_escaped( buf, filter );
1060 sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
1061 tip->ti_label, eol );
1062 if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
1063 return( LDAP_LOCAL_ERROR );
1065 return( LDAP_SUCCESS );
1068 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1069 timeout.tv_usec = 0;
1071 #ifdef LDAP_CONNECTIONLESS
1072 if ( LDAP_IS_CLDAP( ld ))
1073 lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1076 #endif /* LDAP_CONNECTIONLESS */
1077 lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1078 0, &timeout, &ldmp );
1080 if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
1081 if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
1082 if (( members = (char **)LDAP_MALLOC( (count + 1) * sizeof(char *)))
1084 err = LDAP_NO_MEMORY;
1086 for ( i = 0, entry = ldap_first_entry( ld, ldmp );
1088 entry = ldap_next_entry( ld, entry ), ++i ) {
1089 members[ i ] = ldap_get_dn( ld, entry );
1091 members[ i ] = NULL;
1093 ldap_sort_values( ld, members, ldap_sort_strcasecmp );
1095 err = do_vals2text( ld, NULL, members, tip->ti_label,
1096 html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
1097 eol, rdncount, urlprefix );
1099 ldap_value_free( members );
1102 ldap_msgfree( ldmp );
1106 if ( vals != NULL ) {
1107 ldap_value_free( vals );
1110 return(( err == LDAP_SUCCESS ) ? lderr : err );