dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libldap5 / sources / ldap / common / tmplout.c
blob5a3cbb990e7c4fb5eb89800bfb32ca918bc58752
1 /*
2 * Copyright (c) 2001 by Sun Microsystems, Inc.
3 * All rights reserved.
4 */
6 #pragma ident "%Z%%M% %I% %E% SMI"
8 /*
9 * The contents of this file are subject to the Netscape Public
10 * License Version 1.1 (the "License"); you may not use this file
11 * except in compliance with the License. You may obtain a copy of
12 * the License at http://www.mozilla.org/NPL/
14 * Software distributed under the License is distributed on an "AS
15 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
16 * implied. See the License for the specific language governing
17 * rights and limitations under the License.
19 * The Original Code is Mozilla Communicator client code, released
20 * March 31, 1998.
22 * The Initial Developer of the Original Code is Netscape
23 * Communications Corporation. Portions created by Netscape are
24 * Copyright (C) 1998-1999 Netscape Communications Corporation. All
25 * Rights Reserved.
27 * Contributor(s):
31 * tmplout.c: display template library output routines for LDAP clients
35 #include "ldap-int.h"
36 #include "disptmpl.h"
38 #if defined(_WINDOWS) || defined(aix) || defined(SCOOS) || defined(OSF1) || defined(SOLARIS)
39 #include <time.h> /* for struct tm and ctime */
40 #endif
42 static int do_entry2text( LDAP *ld, char *buf, char *base, LDAPMessage *entry,
43 struct ldap_disptmpl *tmpl, char **defattrs, char ***defvals,
44 writeptype writeproc, void *writeparm, char *eol, int rdncount,
45 unsigned long opts, char *urlprefix );
46 static int do_entry2text_search( LDAP *ld, char *dn, char *base,
47 LDAPMessage *entry, struct ldap_disptmpl *tmpllist, char **defattrs,
48 char ***defvals, writeptype writeproc, void *writeparm, char *eol,
49 int rdncount, unsigned long opts, char *urlprefix );
50 static int do_vals2text( LDAP *ld, char *buf, char **vals, char *label,
51 int labelwidth, unsigned long syntaxid, writeptype writeproc,
52 void *writeparm, char *eol, int rdncount, char *urlprefix );
53 static int max_label_len( struct ldap_disptmpl *tmpl );
54 static int output_label( char *buf, char *label, int width,
55 writeptype writeproc, void *writeparm, char *eol, int html );
56 static int output_dn( char *buf, char *dn, int width, int rdncount,
57 writeptype writeproc, void *writeparm, char *eol, char *urlprefix );
58 static void strcat_escaped( char *s1, char *s2 );
59 static char *time2text( char *ldtimestr, int dateonly );
60 static long gtime( struct tm *tm );
61 static int searchaction( 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:///"
76 int
77 LDAP_CALL
78 ldap_entry2text(
79 LDAP *ld,
80 char *buf, /* NULL for "use internal" */
81 LDAPMessage *entry,
82 struct ldap_disptmpl *tmpl,
83 char **defattrs,
84 char ***defvals,
85 writeptype writeproc,
86 void *writeparm,
87 char *eol,
88 int rdncount,
89 unsigned long opts
92 LDAPDebug( 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 ));
102 LDAP_CALL
103 ldap_entry2html(
104 LDAP *ld,
105 char *buf, /* NULL for "use internal" */
106 LDAPMessage *entry,
107 struct ldap_disptmpl *tmpl,
108 char **defattrs,
109 char ***defvals,
110 writeptype writeproc,
111 void *writeparm,
112 char *eol,
113 int rdncount,
114 unsigned long opts,
115 char *base,
116 char *urlprefix
119 LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2html\n", 0, 0, 0 );
121 if ( urlprefix == NULL ) {
122 urlprefix = DEF_LDAP_URL_PREFIX;
125 return( do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
126 writeproc, writeparm, eol, rdncount, opts, urlprefix ));
130 static int
131 do_entry2text(
132 LDAP *ld,
133 char *buf, /* NULL for use-internal */
134 char *base, /* used for search actions */
135 LDAPMessage *entry,
136 struct ldap_disptmpl *tmpl,
137 char **defattrs,
138 char ***defvals,
139 writeptype writeproc,
140 void *writeparm,
141 char *eol,
142 int rdncount,
143 unsigned long opts,
144 char *urlprefix /* if non-NULL, do HTML */
147 int i, err, html, show, labelwidth;
148 int freebuf, freevals;
149 char *dn, **vals;
150 struct ldap_tmplitem *rowp, *colp;
152 if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
153 return( LDAP_PARAM_ERROR );
156 if ( writeproc == NULL ||
157 !NSLDAPI_VALID_LDAPMESSAGE_ENTRY_POINTER( entry )) {
158 err = LDAP_PARAM_ERROR;
159 LDAP_SET_LDERRNO( ld, err, NULL, NULL );
160 return( err );
163 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
164 return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
167 if ( buf == NULL ) {
168 if (( buf = NSLDAPI_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
169 err = LDAP_NO_MEMORY;
170 LDAP_SET_LDERRNO( ld, err, NULL, NULL );
171 NSLDAPI_FREE( dn );
172 return( err );
174 freebuf = 1;
175 } else {
176 freebuf = 0;
179 html = ( urlprefix != NULL );
181 if ( html ) {
183 * add HTML intro. and title
185 if (!(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
186 sprintf( buf, "<HTML>%s<HEAD>%s<TITLE>%s%s - ", eol, eol, eol,
187 ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
188 (*writeproc)( writeparm, buf, strlen( buf ));
189 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
190 sprintf( buf, "%s</TITLE>%s</HEAD>%s<BODY>%s<H3>%s - ", eol, eol,
191 eol, eol, ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
192 (*writeproc)( writeparm, buf, strlen( buf ));
193 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
194 sprintf( buf, "</H3>%s", eol );
195 (*writeproc)( writeparm, buf, strlen( buf ));
198 if (( opts & LDAP_DISP_OPT_NONLEAF ) != 0 &&
199 ( vals = ldap_explode_dn( dn, 0 )) != NULL ) {
200 char *untagged;
203 * add "Move Up" link
205 sprintf( buf, "<A HREF=\"%s", urlprefix );
206 for ( i = 1; vals[ i ] != NULL; ++i ) {
207 if ( i > 1 ) {
208 strcat_escaped( buf, ", " );
210 strcat_escaped( buf, vals[ i ] );
212 if ( vals[ 1 ] != NULL ) {
213 untagged = strchr( vals[ 1 ], '=' );
214 } else {
215 untagged = "=The World";
217 sprintf( buf + strlen( buf ),
218 "%s\">Move Up To <EM>%s</EM></A>%s<BR>",
219 ( vals[ 1 ] == NULL ) ? "??one" : "",
220 ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol );
221 (*writeproc)( writeparm, buf, strlen( buf ));
224 * add "Browse" link
226 untagged = strchr( vals[ 0 ], '=' );
227 sprintf( buf, "<A HREF=\"%s", urlprefix );
228 strcat_escaped( buf, dn );
229 sprintf( buf + strlen( buf ), "??one?(!(objectClass=dsa))\">Browse Below <EM>%s</EM></A>%s%s",
230 ( untagged != NULL ) ? untagged + 1 : vals[ 0 ], eol, eol );
231 (*writeproc)( writeparm, buf, strlen( buf ));
233 ldap_value_free( vals );
236 (*writeproc)( writeparm, "<HR>", 4 ); /* horizontal rule */
237 } else {
238 (*writeproc)( writeparm, "\"", 1 );
239 output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
240 sprintf( buf, "\"%s", eol );
241 (*writeproc)( writeparm, buf, strlen( buf ));
244 if ( tmpl != NULL && ( opts & LDAP_DISP_OPT_AUTOLABELWIDTH ) != 0 ) {
245 labelwidth = max_label_len( tmpl ) + 3;
246 } else {
247 labelwidth = DEF_LABEL_WIDTH;;
250 err = LDAP_SUCCESS;
252 if ( tmpl == NULL ) {
253 BerElement *ber;
254 char *attr;
256 ber = NULL;
257 for ( attr = ldap_first_attribute( ld, entry, &ber );
258 NONFATAL_LDAP_ERR( err ) && attr != NULL;
259 attr = ldap_next_attribute( ld, entry, ber )) {
260 if (( vals = ldap_get_values( ld, entry, attr )) == NULL ) {
261 freevals = 0;
262 if ( defattrs != NULL ) {
263 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
264 if ( strcasecmp( attr, defattrs[ i ] ) == 0 ) {
265 break;
268 if ( defattrs[ i ] != NULL ) {
269 vals = defvals[ i ];
272 } else {
273 freevals = 1;
276 if ( islower( *attr )) { /* cosmetic -- upcase attr. name */
277 *attr = toupper( *attr );
280 err = do_vals2text( ld, buf, vals, attr, labelwidth,
281 LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol,
282 rdncount, urlprefix );
283 if ( freevals ) {
284 ldap_value_free( vals );
287 if ( ber == NULL ) {
288 ber_free( ber, 0 );
291 * XXX check for errors in ldap_first_attribute/ldap_next_attribute
292 * here (but what should we do if there was one?)
295 } else {
296 for ( rowp = ldap_first_tmplrow( tmpl );
297 NONFATAL_LDAP_ERR( err ) && rowp != NULLTMPLITEM;
298 rowp = ldap_next_tmplrow( tmpl, rowp )) {
299 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
300 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
301 vals = NULL;
302 if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
303 entry, colp->ti_attrname )) == NULL ) {
304 freevals = 0;
305 if ( !LDAP_IS_TMPLITEM_OPTION_SET( colp,
306 LDAP_DITEM_OPT_HIDEIFEMPTY ) && defattrs != NULL
307 && colp->ti_attrname != NULL ) {
308 for ( i = 0; defattrs[ i ] != NULL; ++i ) {
309 if ( strcasecmp( colp->ti_attrname, defattrs[ i ] )
310 == 0 ) {
311 break;
314 if ( defattrs[ i ] != NULL ) {
315 vals = defvals[ i ];
318 } else {
319 freevals = 1;
320 if ( LDAP_IS_TMPLITEM_OPTION_SET( colp,
321 LDAP_DITEM_OPT_SORTVALUES ) && vals[ 0 ] != NULL
322 && vals[ 1 ] != NULL ) {
323 ldap_sort_values(ld, vals, ldap_sort_strcasecmp);
328 * don't bother even calling do_vals2text() if no values
329 * or boolean with value false and "hide if false" option set
331 show = ( vals != NULL && vals[ 0 ] != NULL );
332 if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
333 == LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
334 colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
335 toupper( vals[ 0 ][ 0 ] ) != 'T' ) {
336 show = 0;
339 if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
340 if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
341 if ( colp->ti_attrname == NULL || ( show &&
342 toupper( vals[ 0 ][ 0 ] ) == 'T' )) {
343 err = searchaction( ld, buf, base, entry, dn, colp,
344 labelwidth, rdncount, writeproc,
345 writeparm, eol, urlprefix );
348 show = 0;
351 if ( show ) {
352 err = do_vals2text( ld, buf, vals, colp->ti_label,
353 labelwidth, colp->ti_syntaxid, writeproc, writeparm,
354 eol, rdncount, urlprefix );
357 if ( freevals ) {
358 ldap_value_free( vals );
364 if ( html && !(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
365 sprintf( buf, "</BODY>%s</HTML>%s", eol, eol );
366 (*writeproc)( writeparm, buf, strlen( buf ));
369 NSLDAPI_FREE( dn );
370 if ( freebuf ) {
371 NSLDAPI_FREE( buf );
374 return( err );
379 LDAP_CALL
380 ldap_entry2text_search(
381 LDAP *ld,
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 */
386 char **defattrs,
387 char ***defvals,
388 writeptype writeproc,
389 void *writeparm,
390 char *eol,
391 int rdncount, /* if 0, display full DN */
392 unsigned long opts
395 LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2text_search\n", 0, 0, 0 );
397 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
398 defvals, writeproc, writeparm, eol, rdncount, opts, NULL ));
404 LDAP_CALL
405 ldap_entry2html_search(
406 LDAP *ld,
407 char *dn, /* if NULL, use entry */
408 char *base, /* if NULL, no search actions */
409 LDAPMessage *entry, /* if NULL, use dn */
410 struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
411 char **defattrs,
412 char ***defvals,
413 writeptype writeproc,
414 void *writeparm,
415 char *eol,
416 int rdncount, /* if 0, display full DN */
417 unsigned long opts,
418 char *urlprefix
421 LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2html_search\n", 0, 0, 0 );
423 return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
424 defvals, writeproc, writeparm, eol, rdncount, opts, urlprefix ));
428 static int
429 do_entry2text_search(
430 LDAP *ld,
431 char *dn, /* if NULL, use entry */
432 char *base, /* if NULL, no search actions */
433 LDAPMessage *entry, /* if NULL, use dn */
434 struct ldap_disptmpl* tmpllist, /* if NULL, no template used */
435 char **defattrs,
436 char ***defvals,
437 writeptype writeproc,
438 void *writeparm,
439 char *eol,
440 int rdncount, /* if 0, display full DN */
441 unsigned long opts,
442 char *urlprefix
445 int err, freedn, html;
446 char *buf, **fetchattrs, **vals;
447 LDAPMessage *ldmp;
448 struct ldap_disptmpl *tmpl;
449 struct timeval timeout;
451 if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
452 return( LDAP_PARAM_ERROR );
455 if ( dn == NULL && entry == NULLMSG ) {
456 err = LDAP_PARAM_ERROR;
457 LDAP_SET_LDERRNO( ld, err, NULL, NULL );
458 return( err );
461 html = ( urlprefix != NULL );
463 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
464 timeout.tv_usec = 0;
466 if (( buf = NSLDAPI_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
467 err = LDAP_NO_MEMORY;
468 LDAP_SET_LDERRNO( ld, err, NULL, NULL );
469 return( err );
472 freedn = 0;
473 tmpl = NULL;
475 if ( dn == NULL ) {
476 if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
477 NSLDAPI_FREE( buf );
478 return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
480 freedn = 1;
484 if ( tmpllist != NULL ) {
485 ldmp = NULLMSG;
487 if ( entry == NULL ) {
488 char *ocattrs[2];
490 ocattrs[0] = OCATTRNAME;
491 ocattrs[1] = NULL;
492 #ifdef CLDAP
493 if ( LDAP_IS_CLDAP( ld ))
494 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE,
495 "objectClass=*", ocattrs, 0, &ldmp, NULL );
496 else
497 #endif /* CLDAP */
498 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
499 "objectClass=*", ocattrs, 0, &timeout, &ldmp );
501 if ( err == LDAP_SUCCESS ) {
502 entry = ldap_first_entry( ld, ldmp );
506 if ( entry != NULL ) {
507 vals = ldap_get_values( ld, entry, OCATTRNAME );
508 tmpl = ldap_oc2template( vals, tmpllist );
509 if ( vals != NULL ) {
510 ldap_value_free( vals );
513 if ( ldmp != NULL ) {
514 ldap_msgfree( ldmp );
518 entry = NULL;
520 if ( tmpl == NULL ) {
521 fetchattrs = NULL;
522 } else {
523 fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
526 #ifdef CLDAP
527 if ( LDAP_IS_CLDAP( ld ))
528 err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
529 fetchattrs, 0, &ldmp, NULL );
530 else
531 #endif /* CLDAP */
532 err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
533 fetchattrs, 0, &timeout, &ldmp );
535 if ( freedn ) {
536 NSLDAPI_FREE( dn );
538 if ( fetchattrs != NULL ) {
539 ldap_value_free( fetchattrs );
542 if ( err != LDAP_SUCCESS ||
543 ( entry = ldap_first_entry( ld, ldmp )) == NULL ) {
544 NSLDAPI_FREE( buf );
545 return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
548 err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
549 writeproc, writeparm, eol, rdncount, opts, urlprefix );
551 NSLDAPI_FREE( buf );
552 ldap_msgfree( ldmp );
553 return( err );
558 LDAP_CALL
559 ldap_vals2text(
560 LDAP *ld,
561 char *buf, /* NULL for "use internal" */
562 char **vals,
563 char *label,
564 int labelwidth, /* 0 means use default */
565 unsigned long syntaxid,
566 writeptype writeproc,
567 void *writeparm,
568 char *eol,
569 int rdncount
572 LDAPDebug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
574 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
575 writeproc, writeparm, eol, rdncount, NULL ));
580 LDAP_CALL
581 ldap_vals2html(
582 LDAP *ld,
583 char *buf, /* NULL for "use internal" */
584 char **vals,
585 char *label,
586 int labelwidth, /* 0 means use default */
587 unsigned long syntaxid,
588 writeptype writeproc,
589 void *writeparm,
590 char *eol,
591 int rdncount,
592 char *urlprefix
595 LDAPDebug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
597 if ( urlprefix == NULL ) {
598 urlprefix = DEF_LDAP_URL_PREFIX;
601 return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
602 writeproc, writeparm, eol, rdncount, urlprefix ));
606 static int
607 do_vals2text(
608 LDAP *ld,
609 char *buf, /* NULL for "use internal" */
610 char **vals,
611 char *label,
612 int labelwidth, /* 0 means use default */
613 unsigned long syntaxid,
614 writeptype writeproc,
615 void *writeparm,
616 char *eol,
617 int rdncount,
618 char *urlprefix
621 int err, i, html, writeoutval, freebuf, notascii;
622 char *p, *s, *outval;
624 if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || writeproc == NULL ) {
625 return( LDAP_PARAM_ERROR );
628 if ( vals == NULL ) {
629 return( LDAP_SUCCESS );
632 html = ( urlprefix != NULL );
634 switch( LDAP_GET_SYN_TYPE( syntaxid )) {
635 case LDAP_SYN_TYPE_TEXT:
636 case LDAP_SYN_TYPE_BOOLEAN:
637 break; /* we only bother with these two types... */
638 default:
639 return( LDAP_SUCCESS );
642 if ( labelwidth == 0 || labelwidth < 0 ) {
643 labelwidth = DEF_LABEL_WIDTH;
646 if ( buf == NULL ) {
647 if (( buf = NSLDAPI_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
648 err = LDAP_NO_MEMORY;
649 LDAP_SET_LDERRNO( ld, err, NULL, NULL );
650 return( err );
652 freebuf = 1;
653 } else {
654 freebuf = 0;
657 output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
659 for ( i = 0; vals[ i ] != NULL; ++i ) {
660 for ( p = vals[ i ]; *p != '\0'; ++p ) {
661 if ( !isascii( *p )) {
662 break;
665 notascii = ( *p != '\0' );
666 outval = notascii ? dgettext(TEXT_DOMAIN,
667 "(unable to display non-ASCII text value)")
668 : vals[ i ];
670 writeoutval = 0; /* if non-zero, write outval after switch */
672 switch( syntaxid ) {
673 case LDAP_SYN_CASEIGNORESTR:
674 ++writeoutval;
675 break;
677 case LDAP_SYN_RFC822ADDR:
678 if ( html ) {
679 strcpy( buf, "<DD><A HREF=\"mailto:" );
680 strcat_escaped( buf, outval );
681 sprintf( buf + strlen( buf ), "\">%s</A><BR>%s", outval, eol );
682 (*writeproc)( writeparm, buf, strlen( buf ));
683 } else {
684 ++writeoutval;
686 break;
688 case LDAP_SYN_DN: /* for now */
689 output_dn( buf, outval, labelwidth, rdncount, writeproc,
690 writeparm, eol, urlprefix );
691 break;
693 case LDAP_SYN_MULTILINESTR:
694 if ( i > 0 && !html ) {
695 output_label( buf, label, labelwidth, writeproc,
696 writeparm, eol, html );
699 p = s = outval;
700 while (( s = strchr( s, '$' )) != NULL ) {
701 *s++ = '\0';
702 while ( ldap_utf8isspace( s )) {
703 ++s;
705 if ( html ) {
706 sprintf( buf, "<DD>%s<BR>%s", p, eol );
707 } else {
708 sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
710 (*writeproc)( writeparm, buf, strlen( buf ));
711 p = s;
713 outval = p;
714 ++writeoutval;
715 break;
717 case LDAP_SYN_BOOLEAN:
718 outval = toupper( outval[ 0 ] ) == 'T' ?
719 dgettext(TEXT_DOMAIN, "TRUE") : dgettext(TEXT_DOMAIN, "FALSE");
720 ++writeoutval;
721 break;
723 case LDAP_SYN_TIME:
724 case LDAP_SYN_DATE:
725 outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
726 ++writeoutval;
727 break;
729 case LDAP_SYN_LABELEDURL:
730 if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
731 *p++ = '\0';
732 while ( ldap_utf8isspace( p )) {
733 ++p;
735 s = outval;
736 } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
737 *s++ = '\0';
738 while ( ldap_utf8isspace( s )) {
739 ++s;
741 p = outval;
742 } else {
743 s = "URL";
744 p = outval;
748 * at this point `s' points to the label & `p' to the URL
750 if ( html ) {
751 sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
752 } else {
753 sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
754 s, eol, labelwidth + 2, " ",p , eol );
756 (*writeproc)( writeparm, buf, strlen( buf ));
757 break;
759 default:
760 sprintf( buf, dgettext(TEXT_DOMAIN,
761 " Can't display item type %ld%s"),
762 syntaxid, eol );
763 (*writeproc)( writeparm, buf, strlen( buf ));
766 if ( writeoutval ) {
767 if ( html ) {
768 sprintf( buf, "<DD>%s<BR>%s", outval, eol );
769 } else {
770 sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
772 (*writeproc)( writeparm, buf, strlen( buf ));
776 if ( freebuf ) {
777 NSLDAPI_FREE( buf );
780 return( LDAP_SUCCESS );
784 static int
785 max_label_len( struct ldap_disptmpl *tmpl )
787 struct ldap_tmplitem *rowp, *colp;
788 int len, maxlen;
790 maxlen = 0;
792 for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
793 rowp = ldap_next_tmplrow( tmpl, rowp )) {
794 for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
795 colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
796 if (( len = strlen( colp->ti_label )) > maxlen ) {
797 maxlen = len;
802 return( maxlen );
806 static int
807 output_label( char *buf, char *label, int width, writeptype writeproc,
808 void *writeparm, char *eol, int html )
810 char *p;
812 if ( html ) {
813 sprintf( buf, "<DT><B>%s</B>", label );
814 } else {
815 auto size_t w;
816 sprintf( buf, " %s:", label );
817 p = buf + strlen( buf );
819 for (w = ldap_utf8characters(buf); w < (size_t)width; ++w) {
820 *p++ = ' ';
823 *p = '\0';
824 strcat( buf, eol );
827 return ((*writeproc)( writeparm, buf, strlen( buf )));
831 static int
832 output_dn( char *buf, char *dn, int width, int rdncount,
833 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
835 char **dnrdns;
836 int i;
838 if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
839 return( -1 );
842 if ( urlprefix != NULL ) {
843 sprintf( buf, "<DD><A HREF=\"%s", urlprefix );
844 strcat_escaped( buf, dn );
845 strcat( buf, "\">" );
846 } else if ( width > 0 ) {
847 sprintf( buf, "%-*s", width, " " );
848 } else {
849 *buf = '\0';
852 for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
853 ++i ) {
854 if ( i > 0 ) {
855 strcat( buf, ", " );
857 strcat( buf, dnrdns[ i ] );
860 if ( urlprefix != NULL ) {
861 strcat( buf, "</A><BR>" );
864 ldap_value_free( dnrdns );
866 strcat( buf, eol );
868 return ((*writeproc)( writeparm, buf, strlen( buf )));
873 #define HREF_CHAR_ACCEPTABLE( c ) (( c >= '-' && c <= '9' ) || \
874 ( c >= '@' && c <= 'Z' ) || \
875 ( c == '_' ) || \
876 ( c >= 'a' && c <= 'z' ))
878 static void
879 strcat_escaped( char *s1, char *s2 )
881 char *p, *q;
882 char *hexdig = "0123456789ABCDEF";
884 p = s1 + strlen( s1 );
885 for ( q = s2; *q != '\0'; ++q ) {
886 if ( HREF_CHAR_ACCEPTABLE( *q )) {
887 *p++ = *q;
888 } else {
889 *p++ = '%';
890 *p++ = hexdig[ 0x0F & ((*(unsigned char*)q) >> 4) ];
891 *p++ = hexdig[ 0x0F & *q ];
895 *p = '\0';
899 #define GET2BYTENUM( p ) (( *p - '0' ) * 10 + ( *(p+1) - '0' ))
901 static char *
902 time2text( char *ldtimestr, int dateonly )
904 int len;
905 struct tm t;
906 char *p, *timestr, zone, *fmterr =
907 dgettext(TEXT_DOMAIN, "badly formatted time");
908 time_t gmttime;
909 /* CTIME for this platform doesn't use this. */
910 #if !defined(SUNOS4) && !defined(BSDI) && !defined(LINUX1_2) && \
911 !defined(SNI) && !defined(_WIN32) && !defined(macintosh) && !defined(LINUX)
912 char buf[26];
913 #endif
915 memset( (char *)&t, 0, sizeof( struct tm ));
916 if (( len = (int)strlen( ldtimestr )) < 13 ) {
917 return( fmterr );
919 if ( len > 15 ) { /* throw away excess from 4-digit year time string */
920 len = 15;
921 } else if ( len == 14 ) {
922 len = 13; /* assume we have a time w/2-digit year (len=13) */
925 for ( p = ldtimestr; p - ldtimestr + 1 < len; ++p ) {
926 if ( !isdigit( *p )) {
927 return( fmterr );
931 p = ldtimestr;
932 t.tm_year = GET2BYTENUM( p ); p += 2;
933 if ( len == 15 ) {
934 t.tm_year = 100 * (t.tm_year - 19);
935 t.tm_year += GET2BYTENUM( p ); p += 2;
937 else {
938 /* 2 digit years...assumed to be in the range (19)70 through
939 (20)69 ...less than 70 (for now, 38) means 20xx */
940 if(t.tm_year < 70) {
941 t.tm_year += 100;
944 t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
945 t.tm_mday = GET2BYTENUM( p ); p += 2;
946 t.tm_hour = GET2BYTENUM( p ); p += 2;
947 t.tm_min = GET2BYTENUM( p ); p += 2;
948 t.tm_sec = GET2BYTENUM( p ); p += 2;
950 if (( zone = *p ) == 'Z' ) { /* GMT */
951 zone = '\0'; /* no need to indicate on screen, so we make it null */
954 gmttime = gtime( &t );
955 timestr = NSLDAPI_CTIME( &gmttime, buf, sizeof(buf) );
957 timestr[ strlen( timestr ) - 1 ] = zone; /* replace trailing newline */
958 if ( dateonly ) {
959 strcpy( timestr + 11, timestr + 20 );
962 return( timestr );
967 /* gtime.c - inverse gmtime */
969 #if !defined( macintosh ) && !defined( _WINDOWS ) && !defined( DOS ) && !defined(XP_OS2)
970 #include <sys/time.h>
971 #endif /* !macintosh */
973 /* gtime(): the inverse of localtime().
974 This routine was supplied by Mike Accetta at CMU many years ago.
977 static int dmsize[] = {
978 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
981 #define dysize(y) \
982 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
985 #define YEAR(y) ((y) >= 100 ? (y) : (y) + 1900)
987 #define YEAR(y) (((y) < 1900) ? ((y) + 1900) : (y))
989 /* \f */
991 static long gtime ( struct tm *tm )
993 register int i,
994 sec,
995 mins,
996 hour,
997 mday,
998 mon,
999 year;
1000 register long result;
1002 if ((sec = tm -> tm_sec) < 0 || sec > 59
1003 || (mins = tm -> tm_min) < 0 || mins > 59
1004 || (hour = tm -> tm_hour) < 0 || hour > 24
1005 || (mday = tm -> tm_mday) < 1 || mday > 31
1006 || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
1007 return ((long) -1);
1008 if (hour == 24) {
1009 hour = 0;
1010 mday++;
1012 year = YEAR (tm -> tm_year);
1014 result = 0L;
1015 for (i = 1970; i < year; i++)
1016 result += dysize (i);
1017 if (dysize (year) == 366 && mon >= 3)
1018 result++;
1019 while (--mon)
1020 result += dmsize[mon - 1];
1021 result += mday - 1;
1022 result = 24 * result + hour;
1023 result = 60 * result + mins;
1024 result = 60 * result + sec;
1026 return result;
1029 static int
1030 searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
1031 struct ldap_tmplitem *tip, int labelwidth, int rdncount,
1032 writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
1034 int err = LDAP_SUCCESS, lderr, i, count, html;
1035 char **vals, **members;
1036 char *value, *filtpattern, *attr, *selectname;
1037 char *retattrs[2], filter[ 256 ];
1038 LDAPMessage *ldmp;
1039 struct timeval timeout;
1041 html = ( urlprefix != NULL );
1043 for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
1046 if ( i < 3 ) {
1047 return( LDAP_PARAM_ERROR );
1049 attr = tip->ti_args[ 0 ];
1050 filtpattern = tip->ti_args[ 1 ];
1051 retattrs[ 0 ] = tip->ti_args[ 2 ];
1052 retattrs[ 1 ] = NULL;
1053 selectname = tip->ti_args[ 3 ];
1055 vals = NULL;
1056 if ( attr == NULL ) {
1057 value = NULL;
1058 } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
1059 return( LDAP_PARAM_ERROR );
1060 } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
1061 value = dn;
1062 } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
1063 value = vals[ 0 ];
1064 } else {
1065 value = NULL;
1068 ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
1069 value, NULL );
1071 if ( html ) {
1073 * if we are generating HTML, we add an HREF link that embodies this
1074 * search action as an LDAP URL, instead of actually doing the search
1075 * now.
1077 sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
1078 if ( base != NULL ) {
1079 strcat_escaped( buf, base );
1081 strcat( buf, "??sub?" );
1082 strcat_escaped( buf, filter );
1083 sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
1084 tip->ti_label, eol );
1085 if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
1086 return( LDAP_LOCAL_ERROR );
1088 return( LDAP_SUCCESS );
1091 timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1092 timeout.tv_usec = 0;
1094 #ifdef CLDAP
1095 if ( LDAP_IS_CLDAP( ld ))
1096 lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1097 0, &ldmp, NULL );
1098 else
1099 #endif /* CLDAP */
1100 lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter,
1101 retattrs, 0, &timeout, &ldmp );
1103 if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
1104 if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
1105 if (( members = (char **)NSLDAPI_MALLOC( (count + 1)
1106 * sizeof(char *))) == NULL ) {
1107 err = LDAP_NO_MEMORY;
1108 } else {
1109 for ( i = 0, entry = ldap_first_entry( ld, ldmp );
1110 entry != NULL;
1111 entry = ldap_next_entry( ld, entry ), ++i ) {
1112 members[ i ] = ldap_get_dn( ld, entry );
1114 members[ i ] = NULL;
1116 ldap_sort_values(ld,members, ldap_sort_strcasecmp);
1118 err = do_vals2text( ld, NULL, members, tip->ti_label,
1119 html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
1120 eol, rdncount, urlprefix );
1122 ldap_value_free( members );
1125 ldap_msgfree( ldmp );
1129 if ( vals != NULL ) {
1130 ldap_value_free( vals );
1133 return(( err == LDAP_SUCCESS ) ? lderr : err );