2 * tmplout.c: display template library output routines for LDAP clients
3 * 12 April 1994 by Mark C Smith
19 #include <sys/types.h>
25 #include <sys/socket.h>
33 static int do_entry2text( LDAP *ld, char *buf, char *base, LDAPMessage *entry,
34 struct ldap_disptmpl *tmpl, char **defattrs, char ***defvals,
35 writeptype writeproc, void *writeparm, char *eol, int rdncount,
36 unsigned long opts, char *urlprefix );
37 static int do_entry2text_search( LDAP *ld, char *dn, char *base,
38 LDAPMessage *entry, struct ldap_disptmpl *tmpllist, char **defattrs,
39 char ***defvals, writeptype writeproc, void *writeparm, char *eol,
40 int rdncount, unsigned long opts, char *urlprefix );
41 static int do_vals2text( LDAP *ld, char *buf, char **vals, char *label,
42 int labelwidth, unsigned long syntaxid, writeptype writeproc,
43 void *writeparm, char *eol, int rdncount, char *urlprefix );
44 static int max_label_len( struct ldap_disptmpl *tmpl );
45 static int output_label( char *buf, char *label, int width,
46 writeptype writeproc, void *writeparm, char *eol, int html );
47 static int output_dn( char *buf, char *dn, int width, int rdncount,
48 writeptype writeproc, void *writeparm, char *eol, char *urlprefix );
49 static void strcat_escaped( char *s1, char *s2 );
50 static char *time2text( char *ldtimestr, int dateonly );
51 static long gtime( struct tm *tm );
52 static int searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry,
53 char *dn, struct ldap_tmplitem *tip, int labelwidth, int rdncount,
54 writeptype writeproc, void *writeparm, char *eol, char *urlprefix );
55 #else /* NEEDPROTOS */
56 static int do_entry2text();
57 static int do_entry2text_search();
58 static int do_vals2text();
59 static int max_label_len();
60 static int output_label();
61 static int output_dn();
62 static void strcat_escaped();
63 static char *time2text();
65 static int searchaction();
66 #endif /* NEEDPROTOS */
68 #define DEF_LABEL_WIDTH 15
69 #define SEARCH_TIMEOUT_SECS 120
70 #define OCATTRNAME "objectClass"
73 #define NONFATAL_LDAP_ERR( err ) ( err == LDAP_SUCCESS || \
74 err == LDAP_TIMELIMIT_EXCEEDED || err == LDAP_SIZELIMIT_EXCEEDED )
76 #define DEF_LDAP_URL_PREFIX "ldap:///"
82 char *buf, /* NULL for "use internal" */
84 struct ldap_disptmpl *tmpl,
94 Debug( LDAP_DEBUG_TRACE, "ldap_entry2text\n", 0, 0, 0 );
96 return( do_entry2text( ld, buf, NULL, entry, tmpl, defattrs, defvals,
97 writeproc, writeparm, eol, rdncount, opts, NULL ));
106 char *buf, /* NULL for "use internal" */
108 struct ldap_disptmpl *tmpl,
111 writeptype writeproc,
120 Debug( LDAP_DEBUG_TRACE, "ldap_entry2html\n", 0, 0, 0 );
122 if ( urlprefix == NULL ) {
123 urlprefix = DEF_LDAP_URL_PREFIX;
126 return( do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
127 writeproc, writeparm, eol, rdncount, opts, urlprefix ));
134 char *buf, /* NULL for use-internal */
135 char *base, /* used for search actions */
137 struct ldap_disptmpl *tmpl,
140 writeptype writeproc,
145 char *urlprefix /* if non-NULL, do HTML */
148 int i, err, html, show, labelwidth;
149 int freebuf, freevals;
151 struct ldap_tmplitem *rowp, *colp;
153 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
154 return( ld->ld_errno );
158 if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
159 ld->ld_errno = LDAP_NO_MEMORY;
161 return( ld->ld_errno );
168 html = ( urlprefix != NULL );
172 * add HTML intro. and title
174 if (!(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
175 sprintf( buf, "<HTML>%s<HEAD>%s<TITLE>%s%s - ", eol, eol, eol,
176 ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
177 (*writeproc)( writeparm, buf, strlen( buf ));
178 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
179 sprintf( buf, "%s</TITLE>%s</HEAD>%s<BODY>%s<H3>%s - ", eol, eol,
180 eol, eol, ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
181 (*writeproc)( writeparm, buf, strlen( buf ));
182 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
183 sprintf( buf, "</H3>%s", eol );
184 (*writeproc)( writeparm, buf, strlen( buf ));
187 if (( opts & LDAP_DISP_OPT_NONLEAF ) != 0 &&
188 ( vals = ldap_explode_dn( dn, 0 )) != NULL ) {
194 sprintf( buf, "<A HREF=\"%s", urlprefix );
195 for ( i = 1; vals[ i ] != NULL; ++i ) {
197 strcat_escaped( buf, ", " );
199 strcat_escaped( buf, vals[ i ] );
201 if ( vals[ 1 ] != NULL ) {
202 untagged = strchr( vals[ 1 ], '=' );
204 untagged = "=The World";
206 sprintf( buf + strlen( buf ),
207 "%s\">Move Up To <EM>%s</EM></A>%s<BR>",
208 ( vals[ 1 ] == NULL ) ? "??one" : "",
209 ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol, eol );
210 (*writeproc)( writeparm, buf, strlen( buf ));
215 untagged = strchr( vals[ 0 ], '=' );
216 sprintf( buf, "<A HREF=\"%s", urlprefix );
217 strcat_escaped( buf, dn );
218 sprintf( buf + strlen( buf ), "??one?(!(objectClass=dsa))\">Browse Below <EM>%s</EM></A>%s%s",
219 ( untagged != NULL ) ? untagged + 1 : vals[ 0 ], eol, eol );
220 (*writeproc)( writeparm, buf, strlen( buf ));
222 ldap_value_free( vals );
225 (*writeproc)( writeparm, "<HR>", 4 ); /* horizontal rule */
227 (*writeproc)( writeparm, "\"", 1 );
228 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
229 sprintf( buf, "\"%s", eol );
230 (*writeproc)( writeparm, buf, strlen( buf ));
233 if ( tmpl != NULL && ( opts & LDAP_DISP_OPT_AUTOLABELWIDTH ) != 0 ) {
234 labelwidth = max_label_len( tmpl ) + 3;
236 labelwidth = DEF_LABEL_WIDTH;;
241 if ( tmpl == NULL ) {
246 for ( attr = ldap_first_attribute( ld, entry, &ber );
247 NONFATAL_LDAP_ERR( err ) && attr != NULL;
248 attr = ldap_next_attribute( ld, entry, ber )) {
249 if (( vals = ldap_get_values( ld, entry, attr )) == NULL ) {
251 if ( defattrs != NULL ) {
252 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
253 if ( strcasecmp( attr, defattrs[ i ] ) == 0 ) {
257 if ( defattrs[ i ] != NULL ) {
265 if ( islower( *attr )) { /* cosmetic -- upcase attr. name */
266 *attr = toupper( *attr );
269 err = do_vals2text( ld, buf, vals, attr, labelwidth,
270 LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol,
271 rdncount, urlprefix );
273 ldap_value_free( vals );
277 for ( rowp = ldap_first_tmplrow( tmpl );
278 NONFATAL_LDAP_ERR( err ) && rowp != NULLTMPLITEM;
279 rowp = ldap_next_tmplrow( tmpl, rowp )) {
280 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
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( 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( 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 == NULLMSG ) {
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 = 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 if ( LDAP_IS_CLDAP( ld ))
480 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE,
481 "objectClass=*", ocattrs, 0, &ldmp, NULL );
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 );
513 if ( LDAP_IS_CLDAP( ld ))
514 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
515 fetchattrs, 0, &ldmp, NULL );
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 = 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( *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( 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( *p )) {
720 } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
722 while ( isspace( *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 != NULLTMPLITEM;
776 rowp = ldap_next_tmplrow( tmpl, rowp )) {
777 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
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";
890 memset( (char *)&t, 0, sizeof( struct tm ));
891 if ( (int) strlen( ldtimestr ) < 13 ) {
895 for ( p = ldtimestr; p - ldtimestr < 12; ++p ) {
896 if ( !isdigit( *p )) {
902 t.tm_year = GET2BYTENUM( p ); p += 2;
903 t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
904 t.tm_mday = GET2BYTENUM( p ); p += 2;
905 t.tm_hour = GET2BYTENUM( p ); p += 2;
906 t.tm_min = GET2BYTENUM( p ); p += 2;
907 t.tm_sec = GET2BYTENUM( p ); p += 2;
909 if (( zone = *p ) == 'Z' ) { /* GMT */
910 zone = '\0'; /* no need to indicate on screen, so we make it null */
913 gmttime = gtime( &t );
914 timestr = ctime( &gmttime );
916 timestr[ strlen( timestr ) - 1 ] = zone; /* replace trailing newline */
918 strcpy( timestr + 11, timestr + 20 );
926 /* gtime.c - inverse gmtime */
928 #if !defined( MACOS ) && !defined( _WIN32 ) && !defined( DOS )
929 #include <sys/time.h>
932 /* gtime(): the inverse of localtime().
933 This routine was supplied by Mike Accetta at CMU many years ago.
936 static int dmsize[] = {
937 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
941 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
943 #define YEAR(y) ((y) >= 100 ? (y) : (y) + 1900)
947 static long gtime ( struct tm *tm )
956 register long result;
958 if ((sec = tm -> tm_sec) < 0 || sec > 59
959 || (mins = tm -> tm_min) < 0 || mins > 59
960 || (hour = tm -> tm_hour) < 0 || hour > 24
961 || (mday = tm -> tm_mday) < 1 || mday > 31
962 || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
968 year = YEAR (tm -> tm_year);
971 for (i = 1970; i < year; i++)
972 result += dysize (i);
973 if (dysize (year) == 366 && mon >= 3)
976 result += dmsize[mon - 1];
978 result = 24 * result + hour;
979 result = 60 * result + mins;
980 result = 60 * result + sec;
986 searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
987 struct ldap_tmplitem *tip, int labelwidth, int rdncount,
988 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
990 int err, lderr, i, count, html;
991 char **vals, **members;
992 char *value, *filtpattern, *attr, *selectname;
993 char *retattrs[2], filter[ 256 ];
995 struct timeval timeout;
997 html = ( urlprefix != NULL );
999 for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
1003 return( LDAP_PARAM_ERROR );
1005 attr = tip->ti_args[ 0 ];
1006 filtpattern = tip->ti_args[ 1 ];
1007 retattrs[ 0 ] = tip->ti_args[ 2 ];
1008 retattrs[ 1 ] = NULL;
1009 selectname = tip->ti_args[ 3 ];
1012 if ( attr == NULL ) {
1014 } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
1015 return( LDAP_PARAM_ERROR );
1016 } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
1018 } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
1024 ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
1029 * if we are generating HTML, we add an HREF link that embodies this
1030 * search action as an LDAP URL, instead of actually doing the search
1033 sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
1034 if ( base != NULL ) {
1035 strcat_escaped( buf, base );
1037 strcat( buf, "??sub?" );
1038 strcat_escaped( buf, filter );
1039 sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
1040 tip->ti_label, eol );
1041 if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
1042 return( LDAP_LOCAL_ERROR );
1044 return( LDAP_SUCCESS );
1047 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1048 timeout.tv_usec = 0;
1051 if ( LDAP_IS_CLDAP( ld ))
1052 lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1056 lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1057 0, &timeout, &ldmp );
1059 if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
1060 if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
1061 if (( members = (char **)malloc( (count + 1) * sizeof(char *)))
1063 err = LDAP_NO_MEMORY;
1065 for ( i = 0, entry = ldap_first_entry( ld, ldmp );
1067 entry = ldap_next_entry( ld, entry ), ++i ) {
1068 members[ i ] = ldap_get_dn( ld, entry );
1070 members[ i ] = NULL;
1072 ldap_sort_values( ld, members, ldap_sort_strcasecmp );
1074 err = do_vals2text( ld, NULL, members, tip->ti_label,
1075 html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
1076 eol, rdncount, urlprefix );
1078 ldap_value_free( members );
1081 ldap_msgfree( ldmp );
1085 if ( vals != NULL ) {
1086 ldap_value_free( vals );
1089 return(( err == LDAP_SUCCESS ) ? lderr : err );