2 * tmplout.c: display template library output routines for LDAP clients
3 * 12 April 1994 by Mark C Smith
11 #include <ac/socket.h>
12 #include <ac/string.h>
16 #ifdef HAVE_SYS_FILE_H
24 #include "ldapconfig.h"
26 static int do_entry2text LDAP_P((
27 LDAP *ld, char *buf, char *base, LDAPMessage *entry,
28 struct ldap_disptmpl *tmpl, char **defattrs, char ***defvals,
29 writeptype writeproc, void *writeparm, char *eol, int rdncount,
30 unsigned long opts, char *urlprefix ));
31 static int do_entry2text_search LDAP_P((
32 LDAP *ld, char *dn, char *base,
33 LDAPMessage *entry, struct ldap_disptmpl *tmpllist, char **defattrs,
34 char ***defvals, writeptype writeproc, void *writeparm, char *eol,
35 int rdncount, unsigned long opts, char *urlprefix ));
36 static int do_vals2text LDAP_P((
37 LDAP *ld, char *buf, char **vals, char *label,
38 int labelwidth, unsigned long syntaxid, writeptype writeproc,
39 void *writeparm, char *eol, int rdncount, char *urlprefix ));
40 static int max_label_len LDAP_P(( struct ldap_disptmpl *tmpl ));
41 static int output_label LDAP_P((
42 char *buf, char *label, int width,
43 writeptype writeproc, void *writeparm, char *eol, int html ));
44 static int output_dn LDAP_P((
45 char *buf, char *dn, int width, int rdncount,
46 writeptype writeproc, void *writeparm, char *eol, char *urlprefix ));
47 static void strcat_escaped LDAP_P(( char *s1, char *s2 ));
48 static char *time2text LDAP_P(( char *ldtimestr, int dateonly ));
49 static long gtime LDAP_P(( struct tm *tm ));
50 static int searchaction LDAP_P((
51 LDAP *ld, char *buf, char *base, LDAPMessage *entry,
52 char *dn, struct ldap_tmplitem *tip, int labelwidth, int rdncount,
53 writeptype writeproc, void *writeparm, char *eol, char *urlprefix ));
55 #define DEF_LABEL_WIDTH 15
56 #define SEARCH_TIMEOUT_SECS 120
57 #define OCATTRNAME "objectClass"
60 #define NONFATAL_LDAP_ERR( err ) ( err == LDAP_SUCCESS || \
61 err == LDAP_TIMELIMIT_EXCEEDED || err == LDAP_SIZELIMIT_EXCEEDED )
63 #define DEF_LDAP_URL_PREFIX "ldap:///"
69 char *buf, /* NULL for "use internal" */
71 struct ldap_disptmpl *tmpl,
81 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text\n", 0, 0, 0 );
83 return( do_entry2text( ld, buf, NULL, entry, tmpl, defattrs, defvals,
84 writeproc, writeparm, eol, rdncount, opts, NULL ));
93 char *buf, /* NULL for "use internal" */
95 struct ldap_disptmpl *tmpl,
107 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html\n", 0, 0, 0 );
109 if ( urlprefix == NULL ) {
110 urlprefix = DEF_LDAP_URL_PREFIX;
113 return( do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
114 writeproc, writeparm, eol, rdncount, opts, urlprefix ));
121 char *buf, /* NULL for use-internal */
122 char *base, /* used for search actions */
124 struct ldap_disptmpl *tmpl,
127 writeptype writeproc,
132 char *urlprefix /* if non-NULL, do HTML */
135 int i, err, html, show, labelwidth;
136 int freebuf, freevals;
138 struct ldap_tmplitem *rowp, *colp;
140 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
141 return( ld->ld_errno );
145 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
146 ld->ld_errno = LDAP_NO_MEMORY;
148 return( ld->ld_errno );
155 html = ( urlprefix != NULL );
159 * add HTML intro. and title
161 if (!(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
162 sprintf( buf, "<HTML>%s<HEAD>%s<TITLE>%s%s - ", eol, eol, eol,
163 ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
164 (*writeproc)( writeparm, buf, strlen( buf ));
165 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
166 sprintf( buf, "%s</TITLE>%s</HEAD>%s<BODY>%s<H3>%s - ", eol, eol,
167 eol, eol, ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
168 (*writeproc)( writeparm, buf, strlen( buf ));
169 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
170 sprintf( buf, "</H3>%s", eol );
171 (*writeproc)( writeparm, buf, strlen( buf ));
174 if (( opts & LDAP_DISP_OPT_NONLEAF ) != 0 &&
175 ( vals = ldap_explode_dn( dn, 0 )) != NULL ) {
181 sprintf( buf, "<A HREF=\"%s", urlprefix );
182 for ( i = 1; vals[ i ] != NULL; ++i ) {
184 strcat_escaped( buf, ", " );
186 strcat_escaped( buf, vals[ i ] );
188 if ( vals[ 1 ] != NULL ) {
189 untagged = strchr( vals[ 1 ], '=' );
191 untagged = "=The World";
193 sprintf( buf + strlen( buf ),
194 "%s\">Move Up To <EM>%s</EM></A>%s<BR>",
195 ( vals[ 1 ] == NULL ) ? "??one" : "",
196 ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol, eol );
197 (*writeproc)( writeparm, buf, strlen( buf ));
202 untagged = strchr( vals[ 0 ], '=' );
203 sprintf( buf, "<A HREF=\"%s", urlprefix );
204 strcat_escaped( buf, dn );
205 sprintf( buf + strlen( buf ), "??one?(!(objectClass=dsa))\">Browse Below <EM>%s</EM></A>%s%s",
206 ( untagged != NULL ) ? untagged + 1 : vals[ 0 ], eol, eol );
207 (*writeproc)( writeparm, buf, strlen( buf ));
209 ldap_value_free( vals );
212 (*writeproc)( writeparm, "<HR>", 4 ); /* horizontal rule */
214 (*writeproc)( writeparm, "\"", 1 );
215 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
216 sprintf( buf, "\"%s", eol );
217 (*writeproc)( writeparm, buf, strlen( buf ));
220 if ( tmpl != NULL && ( opts & LDAP_DISP_OPT_AUTOLABELWIDTH ) != 0 ) {
221 labelwidth = max_label_len( tmpl ) + 3;
223 labelwidth = DEF_LABEL_WIDTH;;
228 if ( tmpl == NULL ) {
233 for ( attr = ldap_first_attribute( ld, entry, &ber );
234 NONFATAL_LDAP_ERR( err ) && attr != NULL;
235 attr = ldap_next_attribute( ld, entry, ber )) {
236 if (( vals = ldap_get_values( ld, entry, attr )) == NULL ) {
238 if ( defattrs != NULL ) {
239 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
240 if ( strcasecmp( attr, defattrs[ i ] ) == 0 ) {
244 if ( defattrs[ i ] != NULL ) {
252 if ( islower( *attr )) { /* cosmetic -- upcase attr. name */
253 *attr = toupper( *attr );
256 err = do_vals2text( ld, buf, vals, attr, labelwidth,
257 LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol,
258 rdncount, urlprefix );
260 ldap_value_free( vals );
264 for ( rowp = ldap_first_tmplrow( tmpl );
265 NONFATAL_LDAP_ERR( err ) && rowp != NULLTMPLITEM;
266 rowp = ldap_next_tmplrow( tmpl, rowp )) {
267 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
268 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
270 if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
271 entry, colp->ti_attrname )) == NULL ) {
273 if ( !LDAP_IS_TMPLITEM_OPTION_SET( colp,
274 LDAP_DITEM_OPT_HIDEIFEMPTY ) && defattrs != NULL
275 && colp->ti_attrname != NULL ) {
276 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
277 if ( strcasecmp( colp->ti_attrname, defattrs[ i ] )
282 if ( defattrs[ i ] != NULL ) {
288 if ( LDAP_IS_TMPLITEM_OPTION_SET( colp,
289 LDAP_DITEM_OPT_SORTVALUES ) && vals[ 0 ] != NULL
290 && vals[ 1 ] != NULL ) {
291 ldap_sort_values( ld, vals, ldap_sort_strcasecmp );
296 * don't bother even calling do_vals2text() if no values
297 * or boolean with value false and "hide if false" option set
299 show = ( vals != NULL && vals[ 0 ] != NULL );
300 if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
301 == LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
302 colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
303 toupper( vals[ 0 ][ 0 ] ) != 'T' ) {
307 if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
308 if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
309 if ( colp->ti_attrname == NULL || ( show &&
310 toupper( vals[ 0 ][ 0 ] ) == 'T' )) {
311 err = searchaction( ld, buf, base, entry, dn, colp,
312 labelwidth, rdncount, writeproc,
313 writeparm, eol, urlprefix );
320 err = do_vals2text( ld, buf, vals, colp->ti_label,
321 labelwidth, colp->ti_syntaxid, writeproc, writeparm,
322 eol, rdncount, urlprefix );
326 ldap_value_free( vals );
332 if ( html && !(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
333 sprintf( buf, "</BODY>%s</HTML>%s", eol, eol );
334 (*writeproc)( writeparm, buf, strlen( buf ));
347 ldap_entry2text_search(
349 char *dn, /* if NULL, use entry */
350 char *base, /* if NULL, no search actions */
351 LDAPMessage *entry, /* if NULL, use dn */
352 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
355 writeptype writeproc,
358 int rdncount, /* if 0, display full DN */
362 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text_search\n", 0, 0, 0 );
364 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
365 defvals, writeproc, writeparm, eol, rdncount, opts, NULL ));
371 ldap_entry2html_search(
373 char *dn, /* if NULL, use entry */
374 char *base, /* if NULL, no search actions */
375 LDAPMessage *entry, /* if NULL, use dn */
376 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
379 writeptype writeproc,
382 int rdncount, /* if 0, display full DN */
387 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html_search\n", 0, 0, 0 );
389 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
390 defvals, writeproc, writeparm, eol, rdncount, opts, urlprefix ));
395 do_entry2text_search(
397 char *dn, /* if NULL, use entry */
398 char *base, /* if NULL, no search actions */
399 LDAPMessage *entry, /* if NULL, use dn */
400 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
403 writeptype writeproc,
406 int rdncount, /* if 0, display full DN */
411 int err, freedn, freetmpls, html;
412 char *buf, **fetchattrs, **vals;
414 struct ldap_disptmpl *tmpl;
415 struct timeval timeout;
417 if ( dn == NULL && entry == NULLMSG ) {
418 ld->ld_errno = LDAP_PARAM_ERROR;
419 return( ld->ld_errno );
422 html = ( urlprefix != NULL );
424 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
427 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
428 ld->ld_errno = LDAP_NO_MEMORY;
429 return( ld->ld_errno );
432 freedn = freetmpls = 0;
435 if ( tmpllist == NULL ) {
436 if (( err = ldap_init_templates( TEMPLATEFILE, &tmpllist )) != 0 ) {
437 sprintf( buf, "%sUnable to read template file %s (error %d)%s%s",
438 html ? "<!-- " : "", TEMPLATEFILE, err,
439 html ? "-->" : "", eol );
440 (*writeproc)( writeparm, buf, strlen( buf ));
446 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
449 ldap_free_templates( tmpllist );
451 return( ld->ld_errno );
457 if ( tmpllist != NULL ) {
460 if ( entry == NULL ) {
463 ocattrs[0] = OCATTRNAME;
465 #ifdef LDAP_CONNECTIONLESS
466 if ( LDAP_IS_CLDAP( ld ))
467 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE,
468 "objectClass=*", ocattrs, 0, &ldmp, NULL );
470 #endif /* LDAP_CONNECTIONLESS */
471 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
472 "objectClass=*", ocattrs, 0, &timeout, &ldmp );
474 if ( err == LDAP_SUCCESS ) {
475 entry = ldap_first_entry( ld, ldmp );
479 if ( entry != NULL ) {
480 vals = ldap_get_values( ld, entry, OCATTRNAME );
481 tmpl = ldap_oc2template( vals, tmpllist );
482 if ( vals != NULL ) {
483 ldap_value_free( vals );
486 if ( ldmp != NULL ) {
487 ldap_msgfree( ldmp );
493 if ( tmpl == NULL ) {
496 fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
499 #ifdef LDAP_CONNECTIONLESS
500 if ( LDAP_IS_CLDAP( ld ))
501 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
502 fetchattrs, 0, &ldmp, NULL );
504 #endif /* LDAP_CONNECTIONLESS */
505 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
506 fetchattrs, 0, &timeout, &ldmp );
511 if ( fetchattrs != NULL ) {
512 ldap_value_free( fetchattrs );
515 if ( err != LDAP_SUCCESS ||
516 ( entry = ldap_first_entry( ld, ldmp )) == NULL ) {
518 ldap_free_templates( tmpllist );
521 return( ld->ld_errno );
524 err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
525 writeproc, writeparm, eol, rdncount, opts, urlprefix );
529 ldap_free_templates( tmpllist );
531 ldap_msgfree( ldmp );
539 char *buf, /* NULL for "use internal" */
542 int labelwidth, /* 0 means use default */
543 unsigned long syntaxid,
544 writeptype writeproc,
550 Debug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
552 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
553 writeproc, writeparm, eol, rdncount, NULL ));
560 char *buf, /* NULL for "use internal" */
563 int labelwidth, /* 0 means use default */
564 unsigned long syntaxid,
565 writeptype writeproc,
572 Debug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
574 if ( urlprefix == NULL ) {
575 urlprefix = DEF_LDAP_URL_PREFIX;
578 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
579 writeproc, writeparm, eol, rdncount, urlprefix ));
586 char *buf, /* NULL for "use internal" */
589 int labelwidth, /* 0 means use default */
590 unsigned long syntaxid,
591 writeptype writeproc,
598 int i, html, writeoutval, freebuf, notascii;
599 char *p, *s, *outval;
602 if ( vals == NULL ) {
603 return( LDAP_SUCCESS );
606 html = ( urlprefix != NULL );
608 switch( LDAP_GET_SYN_TYPE( syntaxid )) {
609 case LDAP_SYN_TYPE_TEXT:
610 case LDAP_SYN_TYPE_BOOLEAN:
611 break; /* we only bother with these two types... */
613 return( LDAP_SUCCESS );
616 if ( labelwidth == 0 || labelwidth < 0 ) {
617 labelwidth = DEF_LABEL_WIDTH;
621 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
622 ld->ld_errno = LDAP_NO_MEMORY;
623 return( ld->ld_errno );
630 output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
632 for ( i = 0; vals[ i ] != NULL; ++i ) {
633 for ( p = vals[ i ]; *p != '\0'; ++p ) {
634 if ( !isascii( *p )) {
638 notascii = ( *p != '\0' );
639 outval = notascii ? "(unable to display non-ASCII text value)"
642 writeoutval = 0; /* if non-zero, write outval after switch */
645 case LDAP_SYN_CASEIGNORESTR:
649 case LDAP_SYN_RFC822ADDR:
651 strcpy( buf, "<DD><A HREF=\"mailto:" );
652 strcat_escaped( buf, outval );
653 sprintf( buf + strlen( buf ), "\">%s</A><BR>%s", outval, eol );
654 (*writeproc)( writeparm, buf, strlen( buf ));
660 case LDAP_SYN_DN: /* for now */
661 output_dn( buf, outval, labelwidth, rdncount, writeproc,
662 writeparm, eol, urlprefix );
665 case LDAP_SYN_MULTILINESTR:
666 if ( i > 0 && !html ) {
667 output_label( buf, label, labelwidth, writeproc,
668 writeparm, eol, html );
672 while (( s = strchr( s, '$' )) != NULL ) {
674 while ( isspace( *s )) {
678 sprintf( buf, "<DD>%s<BR>%s", p, eol );
680 sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
682 (*writeproc)( writeparm, buf, strlen( buf ));
689 case LDAP_SYN_BOOLEAN:
690 outval = toupper( outval[ 0 ] ) == 'T' ? "TRUE" : "FALSE";
696 outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
700 case LDAP_SYN_LABELEDURL:
701 if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
703 while ( isspace( *p )) {
707 } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
709 while ( isspace( *s )) {
719 * at this point `s' points to the label & `p' to the URL
722 sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
724 sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
725 s, eol, labelwidth + 2, " ",p , eol );
727 (*writeproc)( writeparm, buf, strlen( buf ));
731 sprintf( buf, " Can't display item type %ld%s",
733 (*writeproc)( writeparm, buf, strlen( buf ));
738 sprintf( buf, "<DD>%s<BR>%s", outval, eol );
740 sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
742 (*writeproc)( writeparm, buf, strlen( buf ));
750 return( LDAP_SUCCESS );
755 max_label_len( struct ldap_disptmpl *tmpl )
757 struct ldap_tmplitem *rowp, *colp;
762 for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
763 rowp = ldap_next_tmplrow( tmpl, rowp )) {
764 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
765 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
766 if (( len = strlen( colp->ti_label )) > maxlen ) {
777 output_label( char *buf, char *label, int width, writeptype writeproc,
778 void *writeparm, char *eol, int html )
783 sprintf( buf, "<DT><B>%s</B>", label );
785 sprintf( buf, " %s:", label );
786 p = buf + strlen( buf );
788 while ( p - buf < width ) {
796 return ((*writeproc)( writeparm, buf, strlen( buf )));
801 output_dn( char *buf, char *dn, int width, int rdncount,
802 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
807 if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
811 if ( urlprefix != NULL ) {
812 sprintf( buf, "<DD><A HREF=\"%s", urlprefix );
813 strcat_escaped( buf, dn );
814 strcat( buf, "\">" );
815 } else if ( width > 0 ) {
816 sprintf( buf, "%-*s", width, " " );
821 for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
826 strcat( buf, dnrdns[ i ] );
829 if ( urlprefix != NULL ) {
830 strcat( buf, "</A><BR>" );
833 ldap_value_free( dnrdns );
837 return ((*writeproc)( writeparm, buf, strlen( buf )));
842 #define HREF_CHAR_ACCEPTABLE( c ) (( c >= '-' && c <= '9' ) || \
843 ( c >= '@' && c <= 'Z' ) || \
845 ( c >= 'a' && c <= 'z' ))
848 strcat_escaped( char *s1, char *s2 )
851 char *hexdig = "0123456789ABCDEF";
853 p = s1 + strlen( s1 );
854 for ( q = s2; *q != '\0'; ++q ) {
855 if ( HREF_CHAR_ACCEPTABLE( *q )) {
859 *p++ = hexdig[ *q >> 4 ];
860 *p++ = hexdig[ *q & 0x0F ];
868 #define GET2BYTENUM( p ) (( *p - '0' ) * 10 + ( *(p+1) - '0' ))
871 time2text( char *ldtimestr, int dateonly )
874 char *p, *timestr, zone, *fmterr = "badly formatted time";
877 memset( (char *)&t, 0, sizeof( struct tm ));
878 if ( (int) strlen( ldtimestr ) < 13 ) {
882 for ( p = ldtimestr; p - ldtimestr < 12; ++p ) {
883 if ( !isdigit( *p )) {
889 t.tm_year = GET2BYTENUM( p ); p += 2;
890 t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
891 t.tm_mday = GET2BYTENUM( p ); p += 2;
892 t.tm_hour = GET2BYTENUM( p ); p += 2;
893 t.tm_min = GET2BYTENUM( p ); p += 2;
894 t.tm_sec = GET2BYTENUM( p ); p += 2;
896 if (( zone = *p ) == 'Z' ) { /* GMT */
897 zone = '\0'; /* no need to indicate on screen, so we make it null */
900 gmttime = gtime( &t );
901 timestr = ctime( &gmttime );
903 timestr[ strlen( timestr ) - 1 ] = zone; /* replace trailing newline */
905 strcpy( timestr + 11, timestr + 20 );
913 /* gtime.c - inverse gmtime */
915 #if !defined( MACOS ) && !defined( _WIN32 ) && !defined( DOS )
916 #include <sys/time.h>
919 /* gtime(): the inverse of localtime().
920 This routine was supplied by Mike Accetta at CMU many years ago.
923 static int dmsize[] = {
924 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
928 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
930 #define YEAR(y) ((y) >= 100 ? (y) : (y) + 1900)
934 static long gtime ( struct tm *tm )
943 register long result;
945 if ((sec = tm -> tm_sec) < 0 || sec > 59
946 || (mins = tm -> tm_min) < 0 || mins > 59
947 || (hour = tm -> tm_hour) < 0 || hour > 24
948 || (mday = tm -> tm_mday) < 1 || mday > 31
949 || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
955 year = YEAR (tm -> tm_year);
958 for (i = 1970; i < year; i++)
959 result += dysize (i);
960 if (dysize (year) == 366 && mon >= 3)
963 result += dmsize[mon - 1];
965 result = 24 * result + hour;
966 result = 60 * result + mins;
967 result = 60 * result + sec;
973 searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
974 struct ldap_tmplitem *tip, int labelwidth, int rdncount,
975 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
977 int err = 0, lderr, i, count, html;
978 char **vals, **members;
979 char *value, *filtpattern, *attr, *selectname;
980 char *retattrs[2], filter[ 256 ];
982 struct timeval timeout;
984 html = ( urlprefix != NULL );
986 for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
990 return( LDAP_PARAM_ERROR );
992 attr = tip->ti_args[ 0 ];
993 filtpattern = tip->ti_args[ 1 ];
994 retattrs[ 0 ] = tip->ti_args[ 2 ];
995 retattrs[ 1 ] = NULL;
996 selectname = tip->ti_args[ 3 ];
999 if ( attr == NULL ) {
1001 } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
1002 return( LDAP_PARAM_ERROR );
1003 } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
1005 } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
1011 ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
1016 * if we are generating HTML, we add an HREF link that embodies this
1017 * search action as an LDAP URL, instead of actually doing the search
1020 sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
1021 if ( base != NULL ) {
1022 strcat_escaped( buf, base );
1024 strcat( buf, "??sub?" );
1025 strcat_escaped( buf, filter );
1026 sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
1027 tip->ti_label, eol );
1028 if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
1029 return( LDAP_LOCAL_ERROR );
1031 return( LDAP_SUCCESS );
1034 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1035 timeout.tv_usec = 0;
1037 #ifdef LDAP_CONNECTIONLESS
1038 if ( LDAP_IS_CLDAP( ld ))
1039 lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1042 #endif /* LDAP_CONNECTIONLESS */
1043 lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1044 0, &timeout, &ldmp );
1046 if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
1047 if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
1048 if (( members = (char **)malloc( (count + 1) * sizeof(char *)))
1050 err = LDAP_NO_MEMORY;
1052 for ( i = 0, entry = ldap_first_entry( ld, ldmp );
1054 entry = ldap_next_entry( ld, entry ), ++i ) {
1055 members[ i ] = ldap_get_dn( ld, entry );
1057 members[ i ] = NULL;
1059 ldap_sort_values( ld, members, ldap_sort_strcasecmp );
1061 err = do_vals2text( ld, NULL, members, tip->ti_label,
1062 html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
1063 eol, rdncount, urlprefix );
1065 ldap_value_free( members );
1068 ldap_msgfree( ldmp );
1072 if ( vals != NULL ) {
1073 ldap_value_free( vals );
1076 return(( err == LDAP_SUCCESS ) ? lderr : err );