1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * Copyright (c) 2001 by Sun Microsystems, Inc.
3*0Sstevel@tonic-gate * All rights reserved.
4*0Sstevel@tonic-gate */
5*0Sstevel@tonic-gate
6*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gate /*
9*0Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public
10*0Sstevel@tonic-gate * License Version 1.1 (the "License"); you may not use this file
11*0Sstevel@tonic-gate * except in compliance with the License. You may obtain a copy of
12*0Sstevel@tonic-gate * the License at http://www.mozilla.org/NPL/
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * Software distributed under the License is distributed on an "AS
15*0Sstevel@tonic-gate * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
16*0Sstevel@tonic-gate * implied. See the License for the specific language governing
17*0Sstevel@tonic-gate * rights and limitations under the License.
18*0Sstevel@tonic-gate *
19*0Sstevel@tonic-gate * The Original Code is Mozilla Communicator client code, released
20*0Sstevel@tonic-gate * March 31, 1998.
21*0Sstevel@tonic-gate *
22*0Sstevel@tonic-gate * The Initial Developer of the Original Code is Netscape
23*0Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are
24*0Sstevel@tonic-gate * Copyright (C) 1998-1999 Netscape Communications Corporation. All
25*0Sstevel@tonic-gate * Rights Reserved.
26*0Sstevel@tonic-gate *
27*0Sstevel@tonic-gate * Contributor(s):
28*0Sstevel@tonic-gate */
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate * tmplout.c: display template library output routines for LDAP clients
32*0Sstevel@tonic-gate *
33*0Sstevel@tonic-gate */
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate #include "ldap-int.h"
36*0Sstevel@tonic-gate #include "disptmpl.h"
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate #if defined(_WINDOWS) || defined(aix) || defined(SCOOS) || defined(OSF1) || defined(SOLARIS)
39*0Sstevel@tonic-gate #include <time.h> /* for struct tm and ctime */
40*0Sstevel@tonic-gate #endif
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate /* This is totally lame, since it should be coming from time.h, but isn't. */
44*0Sstevel@tonic-gate #if defined(SOLARIS)
45*0Sstevel@tonic-gate char *ctime_r(const time_t *, char *, int);
46*0Sstevel@tonic-gate #endif
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate static int do_entry2text( LDAP *ld, char *buf, char *base, LDAPMessage *entry,
49*0Sstevel@tonic-gate struct ldap_disptmpl *tmpl, char **defattrs, char ***defvals,
50*0Sstevel@tonic-gate writeptype writeproc, void *writeparm, char *eol, int rdncount,
51*0Sstevel@tonic-gate unsigned long opts, char *urlprefix );
52*0Sstevel@tonic-gate static int do_entry2text_search( LDAP *ld, char *dn, char *base,
53*0Sstevel@tonic-gate LDAPMessage *entry, struct ldap_disptmpl *tmpllist, char **defattrs,
54*0Sstevel@tonic-gate char ***defvals, writeptype writeproc, void *writeparm, char *eol,
55*0Sstevel@tonic-gate int rdncount, unsigned long opts, char *urlprefix );
56*0Sstevel@tonic-gate static int do_vals2text( LDAP *ld, char *buf, char **vals, char *label,
57*0Sstevel@tonic-gate int labelwidth, unsigned long syntaxid, writeptype writeproc,
58*0Sstevel@tonic-gate void *writeparm, char *eol, int rdncount, char *urlprefix );
59*0Sstevel@tonic-gate static int max_label_len( struct ldap_disptmpl *tmpl );
60*0Sstevel@tonic-gate static int output_label( char *buf, char *label, int width,
61*0Sstevel@tonic-gate writeptype writeproc, void *writeparm, char *eol, int html );
62*0Sstevel@tonic-gate static int output_dn( char *buf, char *dn, int width, int rdncount,
63*0Sstevel@tonic-gate writeptype writeproc, void *writeparm, char *eol, char *urlprefix );
64*0Sstevel@tonic-gate static void strcat_escaped( char *s1, char *s2 );
65*0Sstevel@tonic-gate static char *time2text( char *ldtimestr, int dateonly );
66*0Sstevel@tonic-gate static long gtime( struct tm *tm );
67*0Sstevel@tonic-gate static int searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry,
68*0Sstevel@tonic-gate char *dn, struct ldap_tmplitem *tip, int labelwidth, int rdncount,
69*0Sstevel@tonic-gate writeptype writeproc, void *writeparm, char *eol, char *urlprefix );
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate #define DEF_LABEL_WIDTH 15
72*0Sstevel@tonic-gate #define SEARCH_TIMEOUT_SECS 120
73*0Sstevel@tonic-gate #define OCATTRNAME "objectClass"
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate #define NONFATAL_LDAP_ERR( err ) ( err == LDAP_SUCCESS || \
77*0Sstevel@tonic-gate err == LDAP_TIMELIMIT_EXCEEDED || err == LDAP_SIZELIMIT_EXCEEDED )
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate #define DEF_LDAP_URL_PREFIX "ldap:///"
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate int
83*0Sstevel@tonic-gate LDAP_CALL
ldap_entry2text(LDAP * ld,char * buf,LDAPMessage * entry,struct ldap_disptmpl * tmpl,char ** defattrs,char *** defvals,writeptype writeproc,void * writeparm,char * eol,int rdncount,unsigned long opts)84*0Sstevel@tonic-gate ldap_entry2text(
85*0Sstevel@tonic-gate LDAP *ld,
86*0Sstevel@tonic-gate char *buf, /* NULL for "use internal" */
87*0Sstevel@tonic-gate LDAPMessage *entry,
88*0Sstevel@tonic-gate struct ldap_disptmpl *tmpl,
89*0Sstevel@tonic-gate char **defattrs,
90*0Sstevel@tonic-gate char ***defvals,
91*0Sstevel@tonic-gate writeptype writeproc,
92*0Sstevel@tonic-gate void *writeparm,
93*0Sstevel@tonic-gate char *eol,
94*0Sstevel@tonic-gate int rdncount,
95*0Sstevel@tonic-gate unsigned long opts
96*0Sstevel@tonic-gate )
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2text\n", 0, 0, 0 );
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate return( do_entry2text( ld, buf, NULL, entry, tmpl, defattrs, defvals,
101*0Sstevel@tonic-gate writeproc, writeparm, eol, rdncount, opts, NULL ));
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate }
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate
107*0Sstevel@tonic-gate int
108*0Sstevel@tonic-gate LDAP_CALL
ldap_entry2html(LDAP * ld,char * buf,LDAPMessage * entry,struct ldap_disptmpl * tmpl,char ** defattrs,char *** defvals,writeptype writeproc,void * writeparm,char * eol,int rdncount,unsigned long opts,char * base,char * urlprefix)109*0Sstevel@tonic-gate ldap_entry2html(
110*0Sstevel@tonic-gate LDAP *ld,
111*0Sstevel@tonic-gate char *buf, /* NULL for "use internal" */
112*0Sstevel@tonic-gate LDAPMessage *entry,
113*0Sstevel@tonic-gate struct ldap_disptmpl *tmpl,
114*0Sstevel@tonic-gate char **defattrs,
115*0Sstevel@tonic-gate char ***defvals,
116*0Sstevel@tonic-gate writeptype writeproc,
117*0Sstevel@tonic-gate void *writeparm,
118*0Sstevel@tonic-gate char *eol,
119*0Sstevel@tonic-gate int rdncount,
120*0Sstevel@tonic-gate unsigned long opts,
121*0Sstevel@tonic-gate char *base,
122*0Sstevel@tonic-gate char *urlprefix
123*0Sstevel@tonic-gate )
124*0Sstevel@tonic-gate {
125*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2html\n", 0, 0, 0 );
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gate if ( urlprefix == NULL ) {
128*0Sstevel@tonic-gate urlprefix = DEF_LDAP_URL_PREFIX;
129*0Sstevel@tonic-gate }
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate return( do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
132*0Sstevel@tonic-gate writeproc, writeparm, eol, rdncount, opts, urlprefix ));
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gate static int
do_entry2text(LDAP * ld,char * buf,char * base,LDAPMessage * entry,struct ldap_disptmpl * tmpl,char ** defattrs,char *** defvals,writeptype writeproc,void * writeparm,char * eol,int rdncount,unsigned long opts,char * urlprefix)137*0Sstevel@tonic-gate do_entry2text(
138*0Sstevel@tonic-gate LDAP *ld,
139*0Sstevel@tonic-gate char *buf, /* NULL for use-internal */
140*0Sstevel@tonic-gate char *base, /* used for search actions */
141*0Sstevel@tonic-gate LDAPMessage *entry,
142*0Sstevel@tonic-gate struct ldap_disptmpl *tmpl,
143*0Sstevel@tonic-gate char **defattrs,
144*0Sstevel@tonic-gate char ***defvals,
145*0Sstevel@tonic-gate writeptype writeproc,
146*0Sstevel@tonic-gate void *writeparm,
147*0Sstevel@tonic-gate char *eol,
148*0Sstevel@tonic-gate int rdncount,
149*0Sstevel@tonic-gate unsigned long opts,
150*0Sstevel@tonic-gate char *urlprefix /* if non-NULL, do HTML */
151*0Sstevel@tonic-gate )
152*0Sstevel@tonic-gate {
153*0Sstevel@tonic-gate int i, err, html, show, labelwidth;
154*0Sstevel@tonic-gate int freebuf, freevals;
155*0Sstevel@tonic-gate char *dn, **vals;
156*0Sstevel@tonic-gate struct ldap_tmplitem *rowp, *colp;
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
159*0Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate if ( writeproc == NULL ||
163*0Sstevel@tonic-gate !NSLDAPI_VALID_LDAPMESSAGE_ENTRY_POINTER( entry )) {
164*0Sstevel@tonic-gate err = LDAP_PARAM_ERROR;
165*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, err, NULL, NULL );
166*0Sstevel@tonic-gate return( err );
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
170*0Sstevel@tonic-gate return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
171*0Sstevel@tonic-gate }
172*0Sstevel@tonic-gate
173*0Sstevel@tonic-gate if ( buf == NULL ) {
174*0Sstevel@tonic-gate if (( buf = NSLDAPI_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
175*0Sstevel@tonic-gate err = LDAP_NO_MEMORY;
176*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, err, NULL, NULL );
177*0Sstevel@tonic-gate NSLDAPI_FREE( dn );
178*0Sstevel@tonic-gate return( err );
179*0Sstevel@tonic-gate }
180*0Sstevel@tonic-gate freebuf = 1;
181*0Sstevel@tonic-gate } else {
182*0Sstevel@tonic-gate freebuf = 0;
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate html = ( urlprefix != NULL );
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gate if ( html ) {
188*0Sstevel@tonic-gate /*
189*0Sstevel@tonic-gate * add HTML intro. and title
190*0Sstevel@tonic-gate */
191*0Sstevel@tonic-gate if (!(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
192*0Sstevel@tonic-gate sprintf( buf, "<HTML>%s<HEAD>%s<TITLE>%s%s - ", eol, eol, eol,
193*0Sstevel@tonic-gate ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
194*0Sstevel@tonic-gate (*writeproc)( writeparm, buf, strlen( buf ));
195*0Sstevel@tonic-gate output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
196*0Sstevel@tonic-gate sprintf( buf, "%s</TITLE>%s</HEAD>%s<BODY>%s<H3>%s - ", eol, eol,
197*0Sstevel@tonic-gate eol, eol, ( tmpl == NULL ) ? "Entry" : tmpl->dt_name );
198*0Sstevel@tonic-gate (*writeproc)( writeparm, buf, strlen( buf ));
199*0Sstevel@tonic-gate output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
200*0Sstevel@tonic-gate sprintf( buf, "</H3>%s", eol );
201*0Sstevel@tonic-gate (*writeproc)( writeparm, buf, strlen( buf ));
202*0Sstevel@tonic-gate }
203*0Sstevel@tonic-gate
204*0Sstevel@tonic-gate if (( opts & LDAP_DISP_OPT_NONLEAF ) != 0 &&
205*0Sstevel@tonic-gate ( vals = ldap_explode_dn( dn, 0 )) != NULL ) {
206*0Sstevel@tonic-gate char *untagged;
207*0Sstevel@tonic-gate
208*0Sstevel@tonic-gate /*
209*0Sstevel@tonic-gate * add "Move Up" link
210*0Sstevel@tonic-gate */
211*0Sstevel@tonic-gate sprintf( buf, "<A HREF=\"%s", urlprefix );
212*0Sstevel@tonic-gate for ( i = 1; vals[ i ] != NULL; ++i ) {
213*0Sstevel@tonic-gate if ( i > 1 ) {
214*0Sstevel@tonic-gate strcat_escaped( buf, ", " );
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate strcat_escaped( buf, vals[ i ] );
217*0Sstevel@tonic-gate }
218*0Sstevel@tonic-gate if ( vals[ 1 ] != NULL ) {
219*0Sstevel@tonic-gate untagged = strchr( vals[ 1 ], '=' );
220*0Sstevel@tonic-gate } else {
221*0Sstevel@tonic-gate untagged = "=The World";
222*0Sstevel@tonic-gate }
223*0Sstevel@tonic-gate sprintf( buf + strlen( buf ),
224*0Sstevel@tonic-gate "%s\">Move Up To <EM>%s</EM></A>%s<BR>",
225*0Sstevel@tonic-gate ( vals[ 1 ] == NULL ) ? "??one" : "",
226*0Sstevel@tonic-gate ( untagged != NULL ) ? untagged + 1 : vals[ 1 ], eol );
227*0Sstevel@tonic-gate (*writeproc)( writeparm, buf, strlen( buf ));
228*0Sstevel@tonic-gate
229*0Sstevel@tonic-gate /*
230*0Sstevel@tonic-gate * add "Browse" link
231*0Sstevel@tonic-gate */
232*0Sstevel@tonic-gate untagged = strchr( vals[ 0 ], '=' );
233*0Sstevel@tonic-gate sprintf( buf, "<A HREF=\"%s", urlprefix );
234*0Sstevel@tonic-gate strcat_escaped( buf, dn );
235*0Sstevel@tonic-gate sprintf( buf + strlen( buf ), "??one?(!(objectClass=dsa))\">Browse Below <EM>%s</EM></A>%s%s",
236*0Sstevel@tonic-gate ( untagged != NULL ) ? untagged + 1 : vals[ 0 ], eol, eol );
237*0Sstevel@tonic-gate (*writeproc)( writeparm, buf, strlen( buf ));
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gate ldap_value_free( vals );
240*0Sstevel@tonic-gate }
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate (*writeproc)( writeparm, "<HR>", 4 ); /* horizontal rule */
243*0Sstevel@tonic-gate } else {
244*0Sstevel@tonic-gate (*writeproc)( writeparm, "\"", 1 );
245*0Sstevel@tonic-gate output_dn( buf, dn, 0, rdncount, writeproc, writeparm, "", NULL );
246*0Sstevel@tonic-gate sprintf( buf, "\"%s", eol );
247*0Sstevel@tonic-gate (*writeproc)( writeparm, buf, strlen( buf ));
248*0Sstevel@tonic-gate }
249*0Sstevel@tonic-gate
250*0Sstevel@tonic-gate if ( tmpl != NULL && ( opts & LDAP_DISP_OPT_AUTOLABELWIDTH ) != 0 ) {
251*0Sstevel@tonic-gate labelwidth = max_label_len( tmpl ) + 3;
252*0Sstevel@tonic-gate } else {
253*0Sstevel@tonic-gate labelwidth = DEF_LABEL_WIDTH;;
254*0Sstevel@tonic-gate }
255*0Sstevel@tonic-gate
256*0Sstevel@tonic-gate err = LDAP_SUCCESS;
257*0Sstevel@tonic-gate
258*0Sstevel@tonic-gate if ( tmpl == NULL ) {
259*0Sstevel@tonic-gate BerElement *ber;
260*0Sstevel@tonic-gate char *attr;
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate ber = NULL;
263*0Sstevel@tonic-gate for ( attr = ldap_first_attribute( ld, entry, &ber );
264*0Sstevel@tonic-gate NONFATAL_LDAP_ERR( err ) && attr != NULL;
265*0Sstevel@tonic-gate attr = ldap_next_attribute( ld, entry, ber )) {
266*0Sstevel@tonic-gate if (( vals = ldap_get_values( ld, entry, attr )) == NULL ) {
267*0Sstevel@tonic-gate freevals = 0;
268*0Sstevel@tonic-gate if ( defattrs != NULL ) {
269*0Sstevel@tonic-gate for ( i = 0; defattrs[ i ] != NULL; ++i ) {
270*0Sstevel@tonic-gate if ( strcasecmp( attr, defattrs[ i ] ) == 0 ) {
271*0Sstevel@tonic-gate break;
272*0Sstevel@tonic-gate }
273*0Sstevel@tonic-gate }
274*0Sstevel@tonic-gate if ( defattrs[ i ] != NULL ) {
275*0Sstevel@tonic-gate vals = defvals[ i ];
276*0Sstevel@tonic-gate }
277*0Sstevel@tonic-gate }
278*0Sstevel@tonic-gate } else {
279*0Sstevel@tonic-gate freevals = 1;
280*0Sstevel@tonic-gate }
281*0Sstevel@tonic-gate
282*0Sstevel@tonic-gate if ( islower( *attr )) { /* cosmetic -- upcase attr. name */
283*0Sstevel@tonic-gate *attr = toupper( *attr );
284*0Sstevel@tonic-gate }
285*0Sstevel@tonic-gate
286*0Sstevel@tonic-gate err = do_vals2text( ld, buf, vals, attr, labelwidth,
287*0Sstevel@tonic-gate LDAP_SYN_CASEIGNORESTR, writeproc, writeparm, eol,
288*0Sstevel@tonic-gate rdncount, urlprefix );
289*0Sstevel@tonic-gate if ( freevals ) {
290*0Sstevel@tonic-gate ldap_value_free( vals );
291*0Sstevel@tonic-gate }
292*0Sstevel@tonic-gate }
293*0Sstevel@tonic-gate if ( ber == NULL ) {
294*0Sstevel@tonic-gate ber_free( ber, 0 );
295*0Sstevel@tonic-gate }
296*0Sstevel@tonic-gate /*
297*0Sstevel@tonic-gate * XXX check for errors in ldap_first_attribute/ldap_next_attribute
298*0Sstevel@tonic-gate * here (but what should we do if there was one?)
299*0Sstevel@tonic-gate */
300*0Sstevel@tonic-gate
301*0Sstevel@tonic-gate } else {
302*0Sstevel@tonic-gate for ( rowp = ldap_first_tmplrow( tmpl );
303*0Sstevel@tonic-gate NONFATAL_LDAP_ERR( err ) && rowp != NULLTMPLITEM;
304*0Sstevel@tonic-gate rowp = ldap_next_tmplrow( tmpl, rowp )) {
305*0Sstevel@tonic-gate for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
306*0Sstevel@tonic-gate colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
307*0Sstevel@tonic-gate vals = NULL;
308*0Sstevel@tonic-gate if ( colp->ti_attrname == NULL || ( vals = ldap_get_values( ld,
309*0Sstevel@tonic-gate entry, colp->ti_attrname )) == NULL ) {
310*0Sstevel@tonic-gate freevals = 0;
311*0Sstevel@tonic-gate if ( !LDAP_IS_TMPLITEM_OPTION_SET( colp,
312*0Sstevel@tonic-gate LDAP_DITEM_OPT_HIDEIFEMPTY ) && defattrs != NULL
313*0Sstevel@tonic-gate && colp->ti_attrname != NULL ) {
314*0Sstevel@tonic-gate for ( i = 0; defattrs[ i ] != NULL; ++i ) {
315*0Sstevel@tonic-gate if ( strcasecmp( colp->ti_attrname, defattrs[ i ] )
316*0Sstevel@tonic-gate == 0 ) {
317*0Sstevel@tonic-gate break;
318*0Sstevel@tonic-gate }
319*0Sstevel@tonic-gate }
320*0Sstevel@tonic-gate if ( defattrs[ i ] != NULL ) {
321*0Sstevel@tonic-gate vals = defvals[ i ];
322*0Sstevel@tonic-gate }
323*0Sstevel@tonic-gate }
324*0Sstevel@tonic-gate } else {
325*0Sstevel@tonic-gate freevals = 1;
326*0Sstevel@tonic-gate if ( LDAP_IS_TMPLITEM_OPTION_SET( colp,
327*0Sstevel@tonic-gate LDAP_DITEM_OPT_SORTVALUES ) && vals[ 0 ] != NULL
328*0Sstevel@tonic-gate && vals[ 1 ] != NULL ) {
329*0Sstevel@tonic-gate ldap_sort_values(ld, vals, ldap_sort_strcasecmp);
330*0Sstevel@tonic-gate }
331*0Sstevel@tonic-gate }
332*0Sstevel@tonic-gate
333*0Sstevel@tonic-gate /*
334*0Sstevel@tonic-gate * don't bother even calling do_vals2text() if no values
335*0Sstevel@tonic-gate * or boolean with value false and "hide if false" option set
336*0Sstevel@tonic-gate */
337*0Sstevel@tonic-gate show = ( vals != NULL && vals[ 0 ] != NULL );
338*0Sstevel@tonic-gate if ( show && LDAP_GET_SYN_TYPE( colp->ti_syntaxid )
339*0Sstevel@tonic-gate == LDAP_SYN_TYPE_BOOLEAN && LDAP_IS_TMPLITEM_OPTION_SET(
340*0Sstevel@tonic-gate colp, LDAP_DITEM_OPT_HIDEIFFALSE ) &&
341*0Sstevel@tonic-gate toupper( vals[ 0 ][ 0 ] ) != 'T' ) {
342*0Sstevel@tonic-gate show = 0;
343*0Sstevel@tonic-gate }
344*0Sstevel@tonic-gate
345*0Sstevel@tonic-gate if ( colp->ti_syntaxid == LDAP_SYN_SEARCHACTION ) {
346*0Sstevel@tonic-gate if (( opts & LDAP_DISP_OPT_DOSEARCHACTIONS ) != 0 ) {
347*0Sstevel@tonic-gate if ( colp->ti_attrname == NULL || ( show &&
348*0Sstevel@tonic-gate toupper( vals[ 0 ][ 0 ] ) == 'T' )) {
349*0Sstevel@tonic-gate err = searchaction( ld, buf, base, entry, dn, colp,
350*0Sstevel@tonic-gate labelwidth, rdncount, writeproc,
351*0Sstevel@tonic-gate writeparm, eol, urlprefix );
352*0Sstevel@tonic-gate }
353*0Sstevel@tonic-gate }
354*0Sstevel@tonic-gate show = 0;
355*0Sstevel@tonic-gate }
356*0Sstevel@tonic-gate
357*0Sstevel@tonic-gate if ( show ) {
358*0Sstevel@tonic-gate err = do_vals2text( ld, buf, vals, colp->ti_label,
359*0Sstevel@tonic-gate labelwidth, colp->ti_syntaxid, writeproc, writeparm,
360*0Sstevel@tonic-gate eol, rdncount, urlprefix );
361*0Sstevel@tonic-gate }
362*0Sstevel@tonic-gate
363*0Sstevel@tonic-gate if ( freevals ) {
364*0Sstevel@tonic-gate ldap_value_free( vals );
365*0Sstevel@tonic-gate }
366*0Sstevel@tonic-gate }
367*0Sstevel@tonic-gate }
368*0Sstevel@tonic-gate }
369*0Sstevel@tonic-gate
370*0Sstevel@tonic-gate if ( html && !(( opts & LDAP_DISP_OPT_HTMLBODYONLY ) != 0 )) {
371*0Sstevel@tonic-gate sprintf( buf, "</BODY>%s</HTML>%s", eol, eol );
372*0Sstevel@tonic-gate (*writeproc)( writeparm, buf, strlen( buf ));
373*0Sstevel@tonic-gate }
374*0Sstevel@tonic-gate
375*0Sstevel@tonic-gate NSLDAPI_FREE( dn );
376*0Sstevel@tonic-gate if ( freebuf ) {
377*0Sstevel@tonic-gate NSLDAPI_FREE( buf );
378*0Sstevel@tonic-gate }
379*0Sstevel@tonic-gate
380*0Sstevel@tonic-gate return( err );
381*0Sstevel@tonic-gate }
382*0Sstevel@tonic-gate
383*0Sstevel@tonic-gate
384*0Sstevel@tonic-gate int
385*0Sstevel@tonic-gate LDAP_CALL
ldap_entry2text_search(LDAP * ld,char * dn,char * base,LDAPMessage * entry,struct ldap_disptmpl * tmpllist,char ** defattrs,char *** defvals,writeptype writeproc,void * writeparm,char * eol,int rdncount,unsigned long opts)386*0Sstevel@tonic-gate ldap_entry2text_search(
387*0Sstevel@tonic-gate LDAP *ld,
388*0Sstevel@tonic-gate char *dn, /* if NULL, use entry */
389*0Sstevel@tonic-gate char *base, /* if NULL, no search actions */
390*0Sstevel@tonic-gate LDAPMessage *entry, /* if NULL, use dn */
391*0Sstevel@tonic-gate struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
392*0Sstevel@tonic-gate char **defattrs,
393*0Sstevel@tonic-gate char ***defvals,
394*0Sstevel@tonic-gate writeptype writeproc,
395*0Sstevel@tonic-gate void *writeparm,
396*0Sstevel@tonic-gate char *eol,
397*0Sstevel@tonic-gate int rdncount, /* if 0, display full DN */
398*0Sstevel@tonic-gate unsigned long opts
399*0Sstevel@tonic-gate )
400*0Sstevel@tonic-gate {
401*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2text_search\n", 0, 0, 0 );
402*0Sstevel@tonic-gate
403*0Sstevel@tonic-gate return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
404*0Sstevel@tonic-gate defvals, writeproc, writeparm, eol, rdncount, opts, NULL ));
405*0Sstevel@tonic-gate }
406*0Sstevel@tonic-gate
407*0Sstevel@tonic-gate
408*0Sstevel@tonic-gate
409*0Sstevel@tonic-gate int
410*0Sstevel@tonic-gate LDAP_CALL
ldap_entry2html_search(LDAP * ld,char * dn,char * base,LDAPMessage * entry,struct ldap_disptmpl * tmpllist,char ** defattrs,char *** defvals,writeptype writeproc,void * writeparm,char * eol,int rdncount,unsigned long opts,char * urlprefix)411*0Sstevel@tonic-gate ldap_entry2html_search(
412*0Sstevel@tonic-gate LDAP *ld,
413*0Sstevel@tonic-gate char *dn, /* if NULL, use entry */
414*0Sstevel@tonic-gate char *base, /* if NULL, no search actions */
415*0Sstevel@tonic-gate LDAPMessage *entry, /* if NULL, use dn */
416*0Sstevel@tonic-gate struct ldap_disptmpl* tmpllist, /* if NULL, load default file */
417*0Sstevel@tonic-gate char **defattrs,
418*0Sstevel@tonic-gate char ***defvals,
419*0Sstevel@tonic-gate writeptype writeproc,
420*0Sstevel@tonic-gate void *writeparm,
421*0Sstevel@tonic-gate char *eol,
422*0Sstevel@tonic-gate int rdncount, /* if 0, display full DN */
423*0Sstevel@tonic-gate unsigned long opts,
424*0Sstevel@tonic-gate char *urlprefix
425*0Sstevel@tonic-gate )
426*0Sstevel@tonic-gate {
427*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_entry2html_search\n", 0, 0, 0 );
428*0Sstevel@tonic-gate
429*0Sstevel@tonic-gate return( do_entry2text_search( ld, dn, base, entry, tmpllist, defattrs,
430*0Sstevel@tonic-gate defvals, writeproc, writeparm, eol, rdncount, opts, urlprefix ));
431*0Sstevel@tonic-gate }
432*0Sstevel@tonic-gate
433*0Sstevel@tonic-gate
434*0Sstevel@tonic-gate static int
do_entry2text_search(LDAP * ld,char * dn,char * base,LDAPMessage * entry,struct ldap_disptmpl * tmpllist,char ** defattrs,char *** defvals,writeptype writeproc,void * writeparm,char * eol,int rdncount,unsigned long opts,char * urlprefix)435*0Sstevel@tonic-gate do_entry2text_search(
436*0Sstevel@tonic-gate LDAP *ld,
437*0Sstevel@tonic-gate char *dn, /* if NULL, use entry */
438*0Sstevel@tonic-gate char *base, /* if NULL, no search actions */
439*0Sstevel@tonic-gate LDAPMessage *entry, /* if NULL, use dn */
440*0Sstevel@tonic-gate struct ldap_disptmpl* tmpllist, /* if NULL, no template used */
441*0Sstevel@tonic-gate char **defattrs,
442*0Sstevel@tonic-gate char ***defvals,
443*0Sstevel@tonic-gate writeptype writeproc,
444*0Sstevel@tonic-gate void *writeparm,
445*0Sstevel@tonic-gate char *eol,
446*0Sstevel@tonic-gate int rdncount, /* if 0, display full DN */
447*0Sstevel@tonic-gate unsigned long opts,
448*0Sstevel@tonic-gate char *urlprefix
449*0Sstevel@tonic-gate )
450*0Sstevel@tonic-gate {
451*0Sstevel@tonic-gate int err, freedn, html;
452*0Sstevel@tonic-gate char *buf, **fetchattrs, **vals;
453*0Sstevel@tonic-gate LDAPMessage *ldmp;
454*0Sstevel@tonic-gate struct ldap_disptmpl *tmpl;
455*0Sstevel@tonic-gate struct timeval timeout;
456*0Sstevel@tonic-gate
457*0Sstevel@tonic-gate if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
458*0Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
459*0Sstevel@tonic-gate }
460*0Sstevel@tonic-gate
461*0Sstevel@tonic-gate if ( dn == NULL && entry == NULLMSG ) {
462*0Sstevel@tonic-gate err = LDAP_PARAM_ERROR;
463*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, err, NULL, NULL );
464*0Sstevel@tonic-gate return( err );
465*0Sstevel@tonic-gate }
466*0Sstevel@tonic-gate
467*0Sstevel@tonic-gate html = ( urlprefix != NULL );
468*0Sstevel@tonic-gate
469*0Sstevel@tonic-gate timeout.tv_sec = SEARCH_TIMEOUT_SECS;
470*0Sstevel@tonic-gate timeout.tv_usec = 0;
471*0Sstevel@tonic-gate
472*0Sstevel@tonic-gate if (( buf = NSLDAPI_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
473*0Sstevel@tonic-gate err = LDAP_NO_MEMORY;
474*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, err, NULL, NULL );
475*0Sstevel@tonic-gate return( err );
476*0Sstevel@tonic-gate }
477*0Sstevel@tonic-gate
478*0Sstevel@tonic-gate freedn = 0;
479*0Sstevel@tonic-gate tmpl = NULL;
480*0Sstevel@tonic-gate
481*0Sstevel@tonic-gate if ( dn == NULL ) {
482*0Sstevel@tonic-gate if (( dn = ldap_get_dn( ld, entry )) == NULL ) {
483*0Sstevel@tonic-gate NSLDAPI_FREE( buf );
484*0Sstevel@tonic-gate return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
485*0Sstevel@tonic-gate }
486*0Sstevel@tonic-gate freedn = 1;
487*0Sstevel@tonic-gate }
488*0Sstevel@tonic-gate
489*0Sstevel@tonic-gate
490*0Sstevel@tonic-gate if ( tmpllist != NULL ) {
491*0Sstevel@tonic-gate ldmp = NULLMSG;
492*0Sstevel@tonic-gate
493*0Sstevel@tonic-gate if ( entry == NULL ) {
494*0Sstevel@tonic-gate char *ocattrs[2];
495*0Sstevel@tonic-gate
496*0Sstevel@tonic-gate ocattrs[0] = OCATTRNAME;
497*0Sstevel@tonic-gate ocattrs[1] = NULL;
498*0Sstevel@tonic-gate #ifdef CLDAP
499*0Sstevel@tonic-gate if ( LDAP_IS_CLDAP( ld ))
500*0Sstevel@tonic-gate err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE,
501*0Sstevel@tonic-gate "objectClass=*", ocattrs, 0, &ldmp, NULL );
502*0Sstevel@tonic-gate else
503*0Sstevel@tonic-gate #endif /* CLDAP */
504*0Sstevel@tonic-gate err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE,
505*0Sstevel@tonic-gate "objectClass=*", ocattrs, 0, &timeout, &ldmp );
506*0Sstevel@tonic-gate
507*0Sstevel@tonic-gate if ( err == LDAP_SUCCESS ) {
508*0Sstevel@tonic-gate entry = ldap_first_entry( ld, ldmp );
509*0Sstevel@tonic-gate }
510*0Sstevel@tonic-gate }
511*0Sstevel@tonic-gate
512*0Sstevel@tonic-gate if ( entry != NULL ) {
513*0Sstevel@tonic-gate vals = ldap_get_values( ld, entry, OCATTRNAME );
514*0Sstevel@tonic-gate tmpl = ldap_oc2template( vals, tmpllist );
515*0Sstevel@tonic-gate if ( vals != NULL ) {
516*0Sstevel@tonic-gate ldap_value_free( vals );
517*0Sstevel@tonic-gate }
518*0Sstevel@tonic-gate }
519*0Sstevel@tonic-gate if ( ldmp != NULL ) {
520*0Sstevel@tonic-gate ldap_msgfree( ldmp );
521*0Sstevel@tonic-gate }
522*0Sstevel@tonic-gate }
523*0Sstevel@tonic-gate
524*0Sstevel@tonic-gate entry = NULL;
525*0Sstevel@tonic-gate
526*0Sstevel@tonic-gate if ( tmpl == NULL ) {
527*0Sstevel@tonic-gate fetchattrs = NULL;
528*0Sstevel@tonic-gate } else {
529*0Sstevel@tonic-gate fetchattrs = ldap_tmplattrs( tmpl, NULL, 1, LDAP_SYN_OPT_DEFER );
530*0Sstevel@tonic-gate }
531*0Sstevel@tonic-gate
532*0Sstevel@tonic-gate #ifdef CLDAP
533*0Sstevel@tonic-gate if ( LDAP_IS_CLDAP( ld ))
534*0Sstevel@tonic-gate err = cldap_search_s( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
535*0Sstevel@tonic-gate fetchattrs, 0, &ldmp, NULL );
536*0Sstevel@tonic-gate else
537*0Sstevel@tonic-gate #endif /* CLDAP */
538*0Sstevel@tonic-gate err = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "objectClass=*",
539*0Sstevel@tonic-gate fetchattrs, 0, &timeout, &ldmp );
540*0Sstevel@tonic-gate
541*0Sstevel@tonic-gate if ( freedn ) {
542*0Sstevel@tonic-gate NSLDAPI_FREE( dn );
543*0Sstevel@tonic-gate }
544*0Sstevel@tonic-gate if ( fetchattrs != NULL ) {
545*0Sstevel@tonic-gate ldap_value_free( fetchattrs );
546*0Sstevel@tonic-gate }
547*0Sstevel@tonic-gate
548*0Sstevel@tonic-gate if ( err != LDAP_SUCCESS ||
549*0Sstevel@tonic-gate ( entry = ldap_first_entry( ld, ldmp )) == NULL ) {
550*0Sstevel@tonic-gate NSLDAPI_FREE( buf );
551*0Sstevel@tonic-gate return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
552*0Sstevel@tonic-gate }
553*0Sstevel@tonic-gate
554*0Sstevel@tonic-gate err = do_entry2text( ld, buf, base, entry, tmpl, defattrs, defvals,
555*0Sstevel@tonic-gate writeproc, writeparm, eol, rdncount, opts, urlprefix );
556*0Sstevel@tonic-gate
557*0Sstevel@tonic-gate NSLDAPI_FREE( buf );
558*0Sstevel@tonic-gate ldap_msgfree( ldmp );
559*0Sstevel@tonic-gate return( err );
560*0Sstevel@tonic-gate }
561*0Sstevel@tonic-gate
562*0Sstevel@tonic-gate
563*0Sstevel@tonic-gate int
564*0Sstevel@tonic-gate LDAP_CALL
ldap_vals2text(LDAP * ld,char * buf,char ** vals,char * label,int labelwidth,unsigned long syntaxid,writeptype writeproc,void * writeparm,char * eol,int rdncount)565*0Sstevel@tonic-gate ldap_vals2text(
566*0Sstevel@tonic-gate LDAP *ld,
567*0Sstevel@tonic-gate char *buf, /* NULL for "use internal" */
568*0Sstevel@tonic-gate char **vals,
569*0Sstevel@tonic-gate char *label,
570*0Sstevel@tonic-gate int labelwidth, /* 0 means use default */
571*0Sstevel@tonic-gate unsigned long syntaxid,
572*0Sstevel@tonic-gate writeptype writeproc,
573*0Sstevel@tonic-gate void *writeparm,
574*0Sstevel@tonic-gate char *eol,
575*0Sstevel@tonic-gate int rdncount
576*0Sstevel@tonic-gate )
577*0Sstevel@tonic-gate {
578*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
579*0Sstevel@tonic-gate
580*0Sstevel@tonic-gate return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
581*0Sstevel@tonic-gate writeproc, writeparm, eol, rdncount, NULL ));
582*0Sstevel@tonic-gate }
583*0Sstevel@tonic-gate
584*0Sstevel@tonic-gate
585*0Sstevel@tonic-gate int
586*0Sstevel@tonic-gate LDAP_CALL
ldap_vals2html(LDAP * ld,char * buf,char ** vals,char * label,int labelwidth,unsigned long syntaxid,writeptype writeproc,void * writeparm,char * eol,int rdncount,char * urlprefix)587*0Sstevel@tonic-gate ldap_vals2html(
588*0Sstevel@tonic-gate LDAP *ld,
589*0Sstevel@tonic-gate char *buf, /* NULL for "use internal" */
590*0Sstevel@tonic-gate char **vals,
591*0Sstevel@tonic-gate char *label,
592*0Sstevel@tonic-gate int labelwidth, /* 0 means use default */
593*0Sstevel@tonic-gate unsigned long syntaxid,
594*0Sstevel@tonic-gate writeptype writeproc,
595*0Sstevel@tonic-gate void *writeparm,
596*0Sstevel@tonic-gate char *eol,
597*0Sstevel@tonic-gate int rdncount,
598*0Sstevel@tonic-gate char *urlprefix
599*0Sstevel@tonic-gate )
600*0Sstevel@tonic-gate {
601*0Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
602*0Sstevel@tonic-gate
603*0Sstevel@tonic-gate if ( urlprefix == NULL ) {
604*0Sstevel@tonic-gate urlprefix = DEF_LDAP_URL_PREFIX;
605*0Sstevel@tonic-gate }
606*0Sstevel@tonic-gate
607*0Sstevel@tonic-gate return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
608*0Sstevel@tonic-gate writeproc, writeparm, eol, rdncount, urlprefix ));
609*0Sstevel@tonic-gate }
610*0Sstevel@tonic-gate
611*0Sstevel@tonic-gate
612*0Sstevel@tonic-gate static int
do_vals2text(LDAP * ld,char * buf,char ** vals,char * label,int labelwidth,unsigned long syntaxid,writeptype writeproc,void * writeparm,char * eol,int rdncount,char * urlprefix)613*0Sstevel@tonic-gate do_vals2text(
614*0Sstevel@tonic-gate LDAP *ld,
615*0Sstevel@tonic-gate char *buf, /* NULL for "use internal" */
616*0Sstevel@tonic-gate char **vals,
617*0Sstevel@tonic-gate char *label,
618*0Sstevel@tonic-gate int labelwidth, /* 0 means use default */
619*0Sstevel@tonic-gate unsigned long syntaxid,
620*0Sstevel@tonic-gate writeptype writeproc,
621*0Sstevel@tonic-gate void *writeparm,
622*0Sstevel@tonic-gate char *eol,
623*0Sstevel@tonic-gate int rdncount,
624*0Sstevel@tonic-gate char *urlprefix
625*0Sstevel@tonic-gate )
626*0Sstevel@tonic-gate {
627*0Sstevel@tonic-gate int err, i, html, writeoutval, freebuf, notascii;
628*0Sstevel@tonic-gate char *p, *s, *outval;
629*0Sstevel@tonic-gate
630*0Sstevel@tonic-gate if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || writeproc == NULL ) {
631*0Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
632*0Sstevel@tonic-gate }
633*0Sstevel@tonic-gate
634*0Sstevel@tonic-gate if ( vals == NULL ) {
635*0Sstevel@tonic-gate return( LDAP_SUCCESS );
636*0Sstevel@tonic-gate }
637*0Sstevel@tonic-gate
638*0Sstevel@tonic-gate html = ( urlprefix != NULL );
639*0Sstevel@tonic-gate
640*0Sstevel@tonic-gate switch( LDAP_GET_SYN_TYPE( syntaxid )) {
641*0Sstevel@tonic-gate case LDAP_SYN_TYPE_TEXT:
642*0Sstevel@tonic-gate case LDAP_SYN_TYPE_BOOLEAN:
643*0Sstevel@tonic-gate break; /* we only bother with these two types... */
644*0Sstevel@tonic-gate default:
645*0Sstevel@tonic-gate return( LDAP_SUCCESS );
646*0Sstevel@tonic-gate }
647*0Sstevel@tonic-gate
648*0Sstevel@tonic-gate if ( labelwidth == 0 || labelwidth < 0 ) {
649*0Sstevel@tonic-gate labelwidth = DEF_LABEL_WIDTH;
650*0Sstevel@tonic-gate }
651*0Sstevel@tonic-gate
652*0Sstevel@tonic-gate if ( buf == NULL ) {
653*0Sstevel@tonic-gate if (( buf = NSLDAPI_MALLOC( LDAP_DTMPL_BUFSIZ )) == NULL ) {
654*0Sstevel@tonic-gate err = LDAP_NO_MEMORY;
655*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, err, NULL, NULL );
656*0Sstevel@tonic-gate return( err );
657*0Sstevel@tonic-gate }
658*0Sstevel@tonic-gate freebuf = 1;
659*0Sstevel@tonic-gate } else {
660*0Sstevel@tonic-gate freebuf = 0;
661*0Sstevel@tonic-gate }
662*0Sstevel@tonic-gate
663*0Sstevel@tonic-gate output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
664*0Sstevel@tonic-gate
665*0Sstevel@tonic-gate for ( i = 0; vals[ i ] != NULL; ++i ) {
666*0Sstevel@tonic-gate for ( p = vals[ i ]; *p != '\0'; ++p ) {
667*0Sstevel@tonic-gate if ( !isascii( *p )) {
668*0Sstevel@tonic-gate break;
669*0Sstevel@tonic-gate }
670*0Sstevel@tonic-gate }
671*0Sstevel@tonic-gate notascii = ( *p != '\0' );
672*0Sstevel@tonic-gate outval = notascii ? dgettext(TEXT_DOMAIN,
673*0Sstevel@tonic-gate "(unable to display non-ASCII text value)")
674*0Sstevel@tonic-gate : vals[ i ];
675*0Sstevel@tonic-gate
676*0Sstevel@tonic-gate writeoutval = 0; /* if non-zero, write outval after switch */
677*0Sstevel@tonic-gate
678*0Sstevel@tonic-gate switch( syntaxid ) {
679*0Sstevel@tonic-gate case LDAP_SYN_CASEIGNORESTR:
680*0Sstevel@tonic-gate ++writeoutval;
681*0Sstevel@tonic-gate break;
682*0Sstevel@tonic-gate
683*0Sstevel@tonic-gate case LDAP_SYN_RFC822ADDR:
684*0Sstevel@tonic-gate if ( html ) {
685*0Sstevel@tonic-gate strcpy( buf, "<DD><A HREF=\"mailto:" );
686*0Sstevel@tonic-gate strcat_escaped( buf, outval );
687*0Sstevel@tonic-gate sprintf( buf + strlen( buf ), "\">%s</A><BR>%s", outval, eol );
688*0Sstevel@tonic-gate (*writeproc)( writeparm, buf, strlen( buf ));
689*0Sstevel@tonic-gate } else {
690*0Sstevel@tonic-gate ++writeoutval;
691*0Sstevel@tonic-gate }
692*0Sstevel@tonic-gate break;
693*0Sstevel@tonic-gate
694*0Sstevel@tonic-gate case LDAP_SYN_DN: /* for now */
695*0Sstevel@tonic-gate output_dn( buf, outval, labelwidth, rdncount, writeproc,
696*0Sstevel@tonic-gate writeparm, eol, urlprefix );
697*0Sstevel@tonic-gate break;
698*0Sstevel@tonic-gate
699*0Sstevel@tonic-gate case LDAP_SYN_MULTILINESTR:
700*0Sstevel@tonic-gate if ( i > 0 && !html ) {
701*0Sstevel@tonic-gate output_label( buf, label, labelwidth, writeproc,
702*0Sstevel@tonic-gate writeparm, eol, html );
703*0Sstevel@tonic-gate }
704*0Sstevel@tonic-gate
705*0Sstevel@tonic-gate p = s = outval;
706*0Sstevel@tonic-gate while (( s = strchr( s, '$' )) != NULL ) {
707*0Sstevel@tonic-gate *s++ = '\0';
708*0Sstevel@tonic-gate while ( ldap_utf8isspace( s )) {
709*0Sstevel@tonic-gate ++s;
710*0Sstevel@tonic-gate }
711*0Sstevel@tonic-gate if ( html ) {
712*0Sstevel@tonic-gate sprintf( buf, "<DD>%s<BR>%s", p, eol );
713*0Sstevel@tonic-gate } else {
714*0Sstevel@tonic-gate sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
715*0Sstevel@tonic-gate }
716*0Sstevel@tonic-gate (*writeproc)( writeparm, buf, strlen( buf ));
717*0Sstevel@tonic-gate p = s;
718*0Sstevel@tonic-gate }
719*0Sstevel@tonic-gate outval = p;
720*0Sstevel@tonic-gate ++writeoutval;
721*0Sstevel@tonic-gate break;
722*0Sstevel@tonic-gate
723*0Sstevel@tonic-gate case LDAP_SYN_BOOLEAN:
724*0Sstevel@tonic-gate outval = toupper( outval[ 0 ] ) == 'T' ?
725*0Sstevel@tonic-gate dgettext(TEXT_DOMAIN, "TRUE") : dgettext(TEXT_DOMAIN, "FALSE");
726*0Sstevel@tonic-gate ++writeoutval;
727*0Sstevel@tonic-gate break;
728*0Sstevel@tonic-gate
729*0Sstevel@tonic-gate case LDAP_SYN_TIME:
730*0Sstevel@tonic-gate case LDAP_SYN_DATE:
731*0Sstevel@tonic-gate outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
732*0Sstevel@tonic-gate ++writeoutval;
733*0Sstevel@tonic-gate break;
734*0Sstevel@tonic-gate
735*0Sstevel@tonic-gate case LDAP_SYN_LABELEDURL:
736*0Sstevel@tonic-gate if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
737*0Sstevel@tonic-gate *p++ = '\0';
738*0Sstevel@tonic-gate while ( ldap_utf8isspace( p )) {
739*0Sstevel@tonic-gate ++p;
740*0Sstevel@tonic-gate }
741*0Sstevel@tonic-gate s = outval;
742*0Sstevel@tonic-gate } else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
743*0Sstevel@tonic-gate *s++ = '\0';
744*0Sstevel@tonic-gate while ( ldap_utf8isspace( s )) {
745*0Sstevel@tonic-gate ++s;
746*0Sstevel@tonic-gate }
747*0Sstevel@tonic-gate p = outval;
748*0Sstevel@tonic-gate } else {
749*0Sstevel@tonic-gate s = "URL";
750*0Sstevel@tonic-gate p = outval;
751*0Sstevel@tonic-gate }
752*0Sstevel@tonic-gate
753*0Sstevel@tonic-gate /*
754*0Sstevel@tonic-gate * at this point `s' points to the label & `p' to the URL
755*0Sstevel@tonic-gate */
756*0Sstevel@tonic-gate if ( html ) {
757*0Sstevel@tonic-gate sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
758*0Sstevel@tonic-gate } else {
759*0Sstevel@tonic-gate sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
760*0Sstevel@tonic-gate s, eol, labelwidth + 2, " ",p , eol );
761*0Sstevel@tonic-gate }
762*0Sstevel@tonic-gate (*writeproc)( writeparm, buf, strlen( buf ));
763*0Sstevel@tonic-gate break;
764*0Sstevel@tonic-gate
765*0Sstevel@tonic-gate default:
766*0Sstevel@tonic-gate sprintf( buf, dgettext(TEXT_DOMAIN,
767*0Sstevel@tonic-gate " Can't display item type %ld%s"),
768*0Sstevel@tonic-gate syntaxid, eol );
769*0Sstevel@tonic-gate (*writeproc)( writeparm, buf, strlen( buf ));
770*0Sstevel@tonic-gate }
771*0Sstevel@tonic-gate
772*0Sstevel@tonic-gate if ( writeoutval ) {
773*0Sstevel@tonic-gate if ( html ) {
774*0Sstevel@tonic-gate sprintf( buf, "<DD>%s<BR>%s", outval, eol );
775*0Sstevel@tonic-gate } else {
776*0Sstevel@tonic-gate sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
777*0Sstevel@tonic-gate }
778*0Sstevel@tonic-gate (*writeproc)( writeparm, buf, strlen( buf ));
779*0Sstevel@tonic-gate }
780*0Sstevel@tonic-gate }
781*0Sstevel@tonic-gate
782*0Sstevel@tonic-gate if ( freebuf ) {
783*0Sstevel@tonic-gate NSLDAPI_FREE( buf );
784*0Sstevel@tonic-gate }
785*0Sstevel@tonic-gate
786*0Sstevel@tonic-gate return( LDAP_SUCCESS );
787*0Sstevel@tonic-gate }
788*0Sstevel@tonic-gate
789*0Sstevel@tonic-gate
790*0Sstevel@tonic-gate static int
max_label_len(struct ldap_disptmpl * tmpl)791*0Sstevel@tonic-gate max_label_len( struct ldap_disptmpl *tmpl )
792*0Sstevel@tonic-gate {
793*0Sstevel@tonic-gate struct ldap_tmplitem *rowp, *colp;
794*0Sstevel@tonic-gate int len, maxlen;
795*0Sstevel@tonic-gate
796*0Sstevel@tonic-gate maxlen = 0;
797*0Sstevel@tonic-gate
798*0Sstevel@tonic-gate for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
799*0Sstevel@tonic-gate rowp = ldap_next_tmplrow( tmpl, rowp )) {
800*0Sstevel@tonic-gate for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
801*0Sstevel@tonic-gate colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
802*0Sstevel@tonic-gate if (( len = strlen( colp->ti_label )) > maxlen ) {
803*0Sstevel@tonic-gate maxlen = len;
804*0Sstevel@tonic-gate }
805*0Sstevel@tonic-gate }
806*0Sstevel@tonic-gate }
807*0Sstevel@tonic-gate
808*0Sstevel@tonic-gate return( maxlen );
809*0Sstevel@tonic-gate }
810*0Sstevel@tonic-gate
811*0Sstevel@tonic-gate
812*0Sstevel@tonic-gate static int
output_label(char * buf,char * label,int width,writeptype writeproc,void * writeparm,char * eol,int html)813*0Sstevel@tonic-gate output_label( char *buf, char *label, int width, writeptype writeproc,
814*0Sstevel@tonic-gate void *writeparm, char *eol, int html )
815*0Sstevel@tonic-gate {
816*0Sstevel@tonic-gate char *p;
817*0Sstevel@tonic-gate
818*0Sstevel@tonic-gate if ( html ) {
819*0Sstevel@tonic-gate sprintf( buf, "<DT><B>%s</B>", label );
820*0Sstevel@tonic-gate } else {
821*0Sstevel@tonic-gate auto size_t w;
822*0Sstevel@tonic-gate sprintf( buf, " %s:", label );
823*0Sstevel@tonic-gate p = buf + strlen( buf );
824*0Sstevel@tonic-gate
825*0Sstevel@tonic-gate for (w = ldap_utf8characters(buf); w < (size_t)width; ++w) {
826*0Sstevel@tonic-gate *p++ = ' ';
827*0Sstevel@tonic-gate }
828*0Sstevel@tonic-gate
829*0Sstevel@tonic-gate *p = '\0';
830*0Sstevel@tonic-gate strcat( buf, eol );
831*0Sstevel@tonic-gate }
832*0Sstevel@tonic-gate
833*0Sstevel@tonic-gate return ((*writeproc)( writeparm, buf, strlen( buf )));
834*0Sstevel@tonic-gate }
835*0Sstevel@tonic-gate
836*0Sstevel@tonic-gate
837*0Sstevel@tonic-gate static int
output_dn(char * buf,char * dn,int width,int rdncount,writeptype writeproc,void * writeparm,char * eol,char * urlprefix)838*0Sstevel@tonic-gate output_dn( char *buf, char *dn, int width, int rdncount,
839*0Sstevel@tonic-gate writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
840*0Sstevel@tonic-gate {
841*0Sstevel@tonic-gate char **dnrdns;
842*0Sstevel@tonic-gate int i;
843*0Sstevel@tonic-gate
844*0Sstevel@tonic-gate if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
845*0Sstevel@tonic-gate return( -1 );
846*0Sstevel@tonic-gate }
847*0Sstevel@tonic-gate
848*0Sstevel@tonic-gate if ( urlprefix != NULL ) {
849*0Sstevel@tonic-gate sprintf( buf, "<DD><A HREF=\"%s", urlprefix );
850*0Sstevel@tonic-gate strcat_escaped( buf, dn );
851*0Sstevel@tonic-gate strcat( buf, "\">" );
852*0Sstevel@tonic-gate } else if ( width > 0 ) {
853*0Sstevel@tonic-gate sprintf( buf, "%-*s", width, " " );
854*0Sstevel@tonic-gate } else {
855*0Sstevel@tonic-gate *buf = '\0';
856*0Sstevel@tonic-gate }
857*0Sstevel@tonic-gate
858*0Sstevel@tonic-gate for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
859*0Sstevel@tonic-gate ++i ) {
860*0Sstevel@tonic-gate if ( i > 0 ) {
861*0Sstevel@tonic-gate strcat( buf, ", " );
862*0Sstevel@tonic-gate }
863*0Sstevel@tonic-gate strcat( buf, dnrdns[ i ] );
864*0Sstevel@tonic-gate }
865*0Sstevel@tonic-gate
866*0Sstevel@tonic-gate if ( urlprefix != NULL ) {
867*0Sstevel@tonic-gate strcat( buf, "</A><BR>" );
868*0Sstevel@tonic-gate }
869*0Sstevel@tonic-gate
870*0Sstevel@tonic-gate ldap_value_free( dnrdns );
871*0Sstevel@tonic-gate
872*0Sstevel@tonic-gate strcat( buf, eol );
873*0Sstevel@tonic-gate
874*0Sstevel@tonic-gate return ((*writeproc)( writeparm, buf, strlen( buf )));
875*0Sstevel@tonic-gate }
876*0Sstevel@tonic-gate
877*0Sstevel@tonic-gate
878*0Sstevel@tonic-gate
879*0Sstevel@tonic-gate #define HREF_CHAR_ACCEPTABLE( c ) (( c >= '-' && c <= '9' ) || \
880*0Sstevel@tonic-gate ( c >= '@' && c <= 'Z' ) || \
881*0Sstevel@tonic-gate ( c == '_' ) || \
882*0Sstevel@tonic-gate ( c >= 'a' && c <= 'z' ))
883*0Sstevel@tonic-gate
884*0Sstevel@tonic-gate static void
strcat_escaped(char * s1,char * s2)885*0Sstevel@tonic-gate strcat_escaped( char *s1, char *s2 )
886*0Sstevel@tonic-gate {
887*0Sstevel@tonic-gate char *p, *q;
888*0Sstevel@tonic-gate char *hexdig = "0123456789ABCDEF";
889*0Sstevel@tonic-gate
890*0Sstevel@tonic-gate p = s1 + strlen( s1 );
891*0Sstevel@tonic-gate for ( q = s2; *q != '\0'; ++q ) {
892*0Sstevel@tonic-gate if ( HREF_CHAR_ACCEPTABLE( *q )) {
893*0Sstevel@tonic-gate *p++ = *q;
894*0Sstevel@tonic-gate } else {
895*0Sstevel@tonic-gate *p++ = '%';
896*0Sstevel@tonic-gate *p++ = hexdig[ 0x0F & ((*(unsigned char*)q) >> 4) ];
897*0Sstevel@tonic-gate *p++ = hexdig[ 0x0F & *q ];
898*0Sstevel@tonic-gate }
899*0Sstevel@tonic-gate }
900*0Sstevel@tonic-gate
901*0Sstevel@tonic-gate *p = '\0';
902*0Sstevel@tonic-gate }
903*0Sstevel@tonic-gate
904*0Sstevel@tonic-gate
905*0Sstevel@tonic-gate #define GET2BYTENUM( p ) (( *p - '0' ) * 10 + ( *(p+1) - '0' ))
906*0Sstevel@tonic-gate
907*0Sstevel@tonic-gate static char *
time2text(char * ldtimestr,int dateonly)908*0Sstevel@tonic-gate time2text( char *ldtimestr, int dateonly )
909*0Sstevel@tonic-gate {
910*0Sstevel@tonic-gate int len;
911*0Sstevel@tonic-gate struct tm t;
912*0Sstevel@tonic-gate char *p, *timestr, zone, *fmterr =
913*0Sstevel@tonic-gate dgettext(TEXT_DOMAIN, "badly formatted time");
914*0Sstevel@tonic-gate time_t gmttime;
915*0Sstevel@tonic-gate /* CTIME for this platform doesn't use this. */
916*0Sstevel@tonic-gate #if !defined(SUNOS4) && !defined(BSDI) && !defined(LINUX1_2) && \
917*0Sstevel@tonic-gate !defined(SNI) && !defined(_WIN32) && !defined(macintosh) && !defined(LINUX)
918*0Sstevel@tonic-gate char buf[26];
919*0Sstevel@tonic-gate #endif
920*0Sstevel@tonic-gate
921*0Sstevel@tonic-gate memset( (char *)&t, 0, sizeof( struct tm ));
922*0Sstevel@tonic-gate if (( len = (int)strlen( ldtimestr )) < 13 ) {
923*0Sstevel@tonic-gate return( fmterr );
924*0Sstevel@tonic-gate }
925*0Sstevel@tonic-gate if ( len > 15 ) { /* throw away excess from 4-digit year time string */
926*0Sstevel@tonic-gate len = 15;
927*0Sstevel@tonic-gate } else if ( len == 14 ) {
928*0Sstevel@tonic-gate len = 13; /* assume we have a time w/2-digit year (len=13) */
929*0Sstevel@tonic-gate }
930*0Sstevel@tonic-gate
931*0Sstevel@tonic-gate for ( p = ldtimestr; p - ldtimestr + 1 < len; ++p ) {
932*0Sstevel@tonic-gate if ( !isdigit( *p )) {
933*0Sstevel@tonic-gate return( fmterr );
934*0Sstevel@tonic-gate }
935*0Sstevel@tonic-gate }
936*0Sstevel@tonic-gate
937*0Sstevel@tonic-gate p = ldtimestr;
938*0Sstevel@tonic-gate t.tm_year = GET2BYTENUM( p ); p += 2;
939*0Sstevel@tonic-gate if ( len == 15 ) {
940*0Sstevel@tonic-gate t.tm_year = 100 * (t.tm_year - 19);
941*0Sstevel@tonic-gate t.tm_year += GET2BYTENUM( p ); p += 2;
942*0Sstevel@tonic-gate }
943*0Sstevel@tonic-gate else {
944*0Sstevel@tonic-gate /* 2 digit years...assumed to be in the range (19)70 through
945*0Sstevel@tonic-gate (20)69 ...less than 70 (for now, 38) means 20xx */
946*0Sstevel@tonic-gate if(t.tm_year < 70) {
947*0Sstevel@tonic-gate t.tm_year += 100;
948*0Sstevel@tonic-gate }
949*0Sstevel@tonic-gate }
950*0Sstevel@tonic-gate t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
951*0Sstevel@tonic-gate t.tm_mday = GET2BYTENUM( p ); p += 2;
952*0Sstevel@tonic-gate t.tm_hour = GET2BYTENUM( p ); p += 2;
953*0Sstevel@tonic-gate t.tm_min = GET2BYTENUM( p ); p += 2;
954*0Sstevel@tonic-gate t.tm_sec = GET2BYTENUM( p ); p += 2;
955*0Sstevel@tonic-gate
956*0Sstevel@tonic-gate if (( zone = *p ) == 'Z' ) { /* GMT */
957*0Sstevel@tonic-gate zone = '\0'; /* no need to indicate on screen, so we make it null */
958*0Sstevel@tonic-gate }
959*0Sstevel@tonic-gate
960*0Sstevel@tonic-gate gmttime = gtime( &t );
961*0Sstevel@tonic-gate timestr = NSLDAPI_CTIME( &gmttime, buf, sizeof(buf) );
962*0Sstevel@tonic-gate
963*0Sstevel@tonic-gate timestr[ strlen( timestr ) - 1 ] = zone; /* replace trailing newline */
964*0Sstevel@tonic-gate if ( dateonly ) {
965*0Sstevel@tonic-gate strcpy( timestr + 11, timestr + 20 );
966*0Sstevel@tonic-gate }
967*0Sstevel@tonic-gate
968*0Sstevel@tonic-gate return( timestr );
969*0Sstevel@tonic-gate }
970*0Sstevel@tonic-gate
971*0Sstevel@tonic-gate
972*0Sstevel@tonic-gate
973*0Sstevel@tonic-gate /* gtime.c - inverse gmtime */
974*0Sstevel@tonic-gate
975*0Sstevel@tonic-gate #if !defined( macintosh ) && !defined( _WINDOWS ) && !defined( DOS ) && !defined(XP_OS2)
976*0Sstevel@tonic-gate #include <sys/time.h>
977*0Sstevel@tonic-gate #endif /* !macintosh */
978*0Sstevel@tonic-gate
979*0Sstevel@tonic-gate /* gtime(): the inverse of localtime().
980*0Sstevel@tonic-gate This routine was supplied by Mike Accetta at CMU many years ago.
981*0Sstevel@tonic-gate */
982*0Sstevel@tonic-gate
983*0Sstevel@tonic-gate static int dmsize[] = {
984*0Sstevel@tonic-gate 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
985*0Sstevel@tonic-gate };
986*0Sstevel@tonic-gate
987*0Sstevel@tonic-gate #define dysize(y) \
988*0Sstevel@tonic-gate (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
989*0Sstevel@tonic-gate
990*0Sstevel@tonic-gate /*
991*0Sstevel@tonic-gate #define YEAR(y) ((y) >= 100 ? (y) : (y) + 1900)
992*0Sstevel@tonic-gate */
993*0Sstevel@tonic-gate #define YEAR(y) (((y) < 1900) ? ((y) + 1900) : (y))
994*0Sstevel@tonic-gate
995*0Sstevel@tonic-gate /* */
996*0Sstevel@tonic-gate
gtime(struct tm * tm)997*0Sstevel@tonic-gate static long gtime ( struct tm *tm )
998*0Sstevel@tonic-gate {
999*0Sstevel@tonic-gate register int i,
1000*0Sstevel@tonic-gate sec,
1001*0Sstevel@tonic-gate mins,
1002*0Sstevel@tonic-gate hour,
1003*0Sstevel@tonic-gate mday,
1004*0Sstevel@tonic-gate mon,
1005*0Sstevel@tonic-gate year;
1006*0Sstevel@tonic-gate register long result;
1007*0Sstevel@tonic-gate
1008*0Sstevel@tonic-gate if ((sec = tm -> tm_sec) < 0 || sec > 59
1009*0Sstevel@tonic-gate || (mins = tm -> tm_min) < 0 || mins > 59
1010*0Sstevel@tonic-gate || (hour = tm -> tm_hour) < 0 || hour > 24
1011*0Sstevel@tonic-gate || (mday = tm -> tm_mday) < 1 || mday > 31
1012*0Sstevel@tonic-gate || (mon = tm -> tm_mon + 1) < 1 || mon > 12)
1013*0Sstevel@tonic-gate return ((long) -1);
1014*0Sstevel@tonic-gate if (hour == 24) {
1015*0Sstevel@tonic-gate hour = 0;
1016*0Sstevel@tonic-gate mday++;
1017*0Sstevel@tonic-gate }
1018*0Sstevel@tonic-gate year = YEAR (tm -> tm_year);
1019*0Sstevel@tonic-gate
1020*0Sstevel@tonic-gate result = 0L;
1021*0Sstevel@tonic-gate for (i = 1970; i < year; i++)
1022*0Sstevel@tonic-gate result += dysize (i);
1023*0Sstevel@tonic-gate if (dysize (year) == 366 && mon >= 3)
1024*0Sstevel@tonic-gate result++;
1025*0Sstevel@tonic-gate while (--mon)
1026*0Sstevel@tonic-gate result += dmsize[mon - 1];
1027*0Sstevel@tonic-gate result += mday - 1;
1028*0Sstevel@tonic-gate result = 24 * result + hour;
1029*0Sstevel@tonic-gate result = 60 * result + mins;
1030*0Sstevel@tonic-gate result = 60 * result + sec;
1031*0Sstevel@tonic-gate
1032*0Sstevel@tonic-gate return result;
1033*0Sstevel@tonic-gate }
1034*0Sstevel@tonic-gate
1035*0Sstevel@tonic-gate static int
searchaction(LDAP * ld,char * buf,char * base,LDAPMessage * entry,char * dn,struct ldap_tmplitem * tip,int labelwidth,int rdncount,writeptype writeproc,void * writeparm,char * eol,char * urlprefix)1036*0Sstevel@tonic-gate searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
1037*0Sstevel@tonic-gate struct ldap_tmplitem *tip, int labelwidth, int rdncount,
1038*0Sstevel@tonic-gate writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
1039*0Sstevel@tonic-gate {
1040*0Sstevel@tonic-gate int err = LDAP_SUCCESS, lderr, i, count, html;
1041*0Sstevel@tonic-gate char **vals, **members;
1042*0Sstevel@tonic-gate char *value, *filtpattern, *attr, *selectname;
1043*0Sstevel@tonic-gate char *retattrs[2], filter[ 256 ];
1044*0Sstevel@tonic-gate LDAPMessage *ldmp;
1045*0Sstevel@tonic-gate struct timeval timeout;
1046*0Sstevel@tonic-gate
1047*0Sstevel@tonic-gate html = ( urlprefix != NULL );
1048*0Sstevel@tonic-gate
1049*0Sstevel@tonic-gate for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
1050*0Sstevel@tonic-gate ;
1051*0Sstevel@tonic-gate }
1052*0Sstevel@tonic-gate if ( i < 3 ) {
1053*0Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
1054*0Sstevel@tonic-gate }
1055*0Sstevel@tonic-gate attr = tip->ti_args[ 0 ];
1056*0Sstevel@tonic-gate filtpattern = tip->ti_args[ 1 ];
1057*0Sstevel@tonic-gate retattrs[ 0 ] = tip->ti_args[ 2 ];
1058*0Sstevel@tonic-gate retattrs[ 1 ] = NULL;
1059*0Sstevel@tonic-gate selectname = tip->ti_args[ 3 ];
1060*0Sstevel@tonic-gate
1061*0Sstevel@tonic-gate vals = NULL;
1062*0Sstevel@tonic-gate if ( attr == NULL ) {
1063*0Sstevel@tonic-gate value = NULL;
1064*0Sstevel@tonic-gate } else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
1065*0Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
1066*0Sstevel@tonic-gate } else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
1067*0Sstevel@tonic-gate value = dn;
1068*0Sstevel@tonic-gate } else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
1069*0Sstevel@tonic-gate value = vals[ 0 ];
1070*0Sstevel@tonic-gate } else {
1071*0Sstevel@tonic-gate value = NULL;
1072*0Sstevel@tonic-gate }
1073*0Sstevel@tonic-gate
1074*0Sstevel@tonic-gate ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
1075*0Sstevel@tonic-gate value, NULL );
1076*0Sstevel@tonic-gate
1077*0Sstevel@tonic-gate if ( html ) {
1078*0Sstevel@tonic-gate /*
1079*0Sstevel@tonic-gate * if we are generating HTML, we add an HREF link that embodies this
1080*0Sstevel@tonic-gate * search action as an LDAP URL, instead of actually doing the search
1081*0Sstevel@tonic-gate * now.
1082*0Sstevel@tonic-gate */
1083*0Sstevel@tonic-gate sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
1084*0Sstevel@tonic-gate if ( base != NULL ) {
1085*0Sstevel@tonic-gate strcat_escaped( buf, base );
1086*0Sstevel@tonic-gate }
1087*0Sstevel@tonic-gate strcat( buf, "??sub?" );
1088*0Sstevel@tonic-gate strcat_escaped( buf, filter );
1089*0Sstevel@tonic-gate sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
1090*0Sstevel@tonic-gate tip->ti_label, eol );
1091*0Sstevel@tonic-gate if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
1092*0Sstevel@tonic-gate return( LDAP_LOCAL_ERROR );
1093*0Sstevel@tonic-gate }
1094*0Sstevel@tonic-gate return( LDAP_SUCCESS );
1095*0Sstevel@tonic-gate }
1096*0Sstevel@tonic-gate
1097*0Sstevel@tonic-gate timeout.tv_sec = SEARCH_TIMEOUT_SECS;
1098*0Sstevel@tonic-gate timeout.tv_usec = 0;
1099*0Sstevel@tonic-gate
1100*0Sstevel@tonic-gate #ifdef CLDAP
1101*0Sstevel@tonic-gate if ( LDAP_IS_CLDAP( ld ))
1102*0Sstevel@tonic-gate lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
1103*0Sstevel@tonic-gate 0, &ldmp, NULL );
1104*0Sstevel@tonic-gate else
1105*0Sstevel@tonic-gate #endif /* CLDAP */
1106*0Sstevel@tonic-gate lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter,
1107*0Sstevel@tonic-gate retattrs, 0, &timeout, &ldmp );
1108*0Sstevel@tonic-gate
1109*0Sstevel@tonic-gate if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
1110*0Sstevel@tonic-gate if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
1111*0Sstevel@tonic-gate if (( members = (char **)NSLDAPI_MALLOC( (count + 1)
1112*0Sstevel@tonic-gate * sizeof(char *))) == NULL ) {
1113*0Sstevel@tonic-gate err = LDAP_NO_MEMORY;
1114*0Sstevel@tonic-gate } else {
1115*0Sstevel@tonic-gate for ( i = 0, entry = ldap_first_entry( ld, ldmp );
1116*0Sstevel@tonic-gate entry != NULL;
1117*0Sstevel@tonic-gate entry = ldap_next_entry( ld, entry ), ++i ) {
1118*0Sstevel@tonic-gate members[ i ] = ldap_get_dn( ld, entry );
1119*0Sstevel@tonic-gate }
1120*0Sstevel@tonic-gate members[ i ] = NULL;
1121*0Sstevel@tonic-gate
1122*0Sstevel@tonic-gate ldap_sort_values(ld,members, ldap_sort_strcasecmp);
1123*0Sstevel@tonic-gate
1124*0Sstevel@tonic-gate err = do_vals2text( ld, NULL, members, tip->ti_label,
1125*0Sstevel@tonic-gate html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
1126*0Sstevel@tonic-gate eol, rdncount, urlprefix );
1127*0Sstevel@tonic-gate
1128*0Sstevel@tonic-gate ldap_value_free( members );
1129*0Sstevel@tonic-gate }
1130*0Sstevel@tonic-gate }
1131*0Sstevel@tonic-gate ldap_msgfree( ldmp );
1132*0Sstevel@tonic-gate }
1133*0Sstevel@tonic-gate
1134*0Sstevel@tonic-gate
1135*0Sstevel@tonic-gate if ( vals != NULL ) {
1136*0Sstevel@tonic-gate ldap_value_free( vals );
1137*0Sstevel@tonic-gate }
1138*0Sstevel@tonic-gate
1139*0Sstevel@tonic-gate return(( err == LDAP_SUCCESS ) ? lderr : err );
1140*0Sstevel@tonic-gate }
1141