1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  *
3*0Sstevel@tonic-gate  * Portions Copyright %G% Sun Microsystems, Inc.
4*0Sstevel@tonic-gate  * All Rights Reserved
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  */
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
9*0Sstevel@tonic-gate 
10*0Sstevel@tonic-gate #include <stdio.h>
11*0Sstevel@tonic-gate #include <string.h>
12*0Sstevel@tonic-gate #include <stdlib.h> /* free() for Solaris */
13*0Sstevel@tonic-gate #ifdef MACOS
14*0Sstevel@tonic-gate #include <stdlib.h>
15*0Sstevel@tonic-gate #else /* MACOS */
16*0Sstevel@tonic-gate #if defined( DOS ) || defined( _WIN32 )
17*0Sstevel@tonic-gate #include <malloc.h>
18*0Sstevel@tonic-gate #include "msdos.h"
19*0Sstevel@tonic-gate #else /* DOS */
20*0Sstevel@tonic-gate #include <sys/types.h>
21*0Sstevel@tonic-gate #include <sys/socket.h>
22*0Sstevel@tonic-gate #endif /* DOS */
23*0Sstevel@tonic-gate #endif /* MACOS */
24*0Sstevel@tonic-gate #include "lber.h"
25*0Sstevel@tonic-gate #include "ldap.h"
26*0Sstevel@tonic-gate #include "ldap-private.h"
27*0Sstevel@tonic-gate #include "ldap-int.h"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate struct ldaperror {
30*0Sstevel@tonic-gate 	int	e_code;
31*0Sstevel@tonic-gate 	char	*e_reason;
32*0Sstevel@tonic-gate };
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate static struct ldaperror ldap_errlist[] = {
35*0Sstevel@tonic-gate #ifdef SUN
36*0Sstevel@tonic-gate 	LDAP_SUCCESS, 			0,
37*0Sstevel@tonic-gate 	LDAP_OPERATIONS_ERROR, 		0,
38*0Sstevel@tonic-gate 	LDAP_PROTOCOL_ERROR, 		0,
39*0Sstevel@tonic-gate 	LDAP_TIMELIMIT_EXCEEDED,	0,
40*0Sstevel@tonic-gate 	LDAP_SIZELIMIT_EXCEEDED, 	0,
41*0Sstevel@tonic-gate 	LDAP_COMPARE_FALSE, 		0,
42*0Sstevel@tonic-gate 	LDAP_COMPARE_TRUE, 		0,
43*0Sstevel@tonic-gate 	LDAP_AUTH_METHOD_NOT_SUPPORTED, 0,
44*0Sstevel@tonic-gate 	LDAP_STRONG_AUTH_REQUIRED, 	0,
45*0Sstevel@tonic-gate 	LDAP_PARTIAL_RESULTS, 		0,
46*0Sstevel@tonic-gate /* new with ldapv3 */
47*0Sstevel@tonic-gate 	LDAP_REFERRAL,			0,
48*0Sstevel@tonic-gate 	LDAP_ADMINLIMIT_EXCEEDED,	0,
49*0Sstevel@tonic-gate 	LDAP_UNAVAILABLE_CRITICAL_EXTENSION, 	0,
50*0Sstevel@tonic-gate 	LDAP_CONFIDENTIALITY_REQUIRED,	0,
51*0Sstevel@tonic-gate /* end of new */
52*0Sstevel@tonic-gate 	LDAP_NO_SUCH_ATTRIBUTE, 	0,
53*0Sstevel@tonic-gate 	LDAP_UNDEFINED_TYPE, 		0,
54*0Sstevel@tonic-gate 	LDAP_INAPPROPRIATE_MATCHING, 	0,
55*0Sstevel@tonic-gate 	LDAP_CONSTRAINT_VIOLATION, 	0,
56*0Sstevel@tonic-gate 	LDAP_TYPE_OR_VALUE_EXISTS, 	0,
57*0Sstevel@tonic-gate 	LDAP_INVALID_SYNTAX, 		0,
58*0Sstevel@tonic-gate 	LDAP_NO_SUCH_OBJECT, 		0,
59*0Sstevel@tonic-gate 	LDAP_ALIAS_PROBLEM, 		0,
60*0Sstevel@tonic-gate 	LDAP_INVALID_DN_SYNTAX,		0,
61*0Sstevel@tonic-gate 	LDAP_IS_LEAF, 			0,
62*0Sstevel@tonic-gate 	LDAP_ALIAS_DEREF_PROBLEM, 	0,
63*0Sstevel@tonic-gate 	LDAP_INAPPROPRIATE_AUTH, 	0,
64*0Sstevel@tonic-gate 	LDAP_INVALID_CREDENTIALS, 	0,
65*0Sstevel@tonic-gate 	LDAP_INSUFFICIENT_ACCESS, 	0,
66*0Sstevel@tonic-gate 	LDAP_BUSY, 			0,
67*0Sstevel@tonic-gate 	LDAP_UNAVAILABLE, 		0,
68*0Sstevel@tonic-gate 	LDAP_UNWILLING_TO_PERFORM, 	0,
69*0Sstevel@tonic-gate 	LDAP_LOOP_DETECT, 		0,
70*0Sstevel@tonic-gate 	LDAP_NAMING_VIOLATION, 		0,
71*0Sstevel@tonic-gate 	LDAP_OBJECT_CLASS_VIOLATION, 	0,
72*0Sstevel@tonic-gate 	LDAP_NOT_ALLOWED_ON_NONLEAF, 	0,
73*0Sstevel@tonic-gate 	LDAP_NOT_ALLOWED_ON_RDN, 	0,
74*0Sstevel@tonic-gate 	LDAP_ALREADY_EXISTS, 		0,
75*0Sstevel@tonic-gate 	LDAP_NO_OBJECT_CLASS_MODS, 	0,
76*0Sstevel@tonic-gate 	LDAP_RESULTS_TOO_LARGE,		0,
77*0Sstevel@tonic-gate /* new with ldapv3 */
78*0Sstevel@tonic-gate 	LDAP_AFFECTS_MULTIPLE_DSAS, 0,
79*0Sstevel@tonic-gate /* end of new */
80*0Sstevel@tonic-gate 	LDAP_OTHER, 			0,
81*0Sstevel@tonic-gate 	LDAP_SERVER_DOWN,		0,
82*0Sstevel@tonic-gate 	LDAP_LOCAL_ERROR,		0,
83*0Sstevel@tonic-gate 	LDAP_ENCODING_ERROR,		0,
84*0Sstevel@tonic-gate 	LDAP_DECODING_ERROR,		0,
85*0Sstevel@tonic-gate 	LDAP_TIMEOUT,			0,
86*0Sstevel@tonic-gate 	LDAP_AUTH_UNKNOWN,		0,
87*0Sstevel@tonic-gate 	LDAP_FILTER_ERROR,		0,
88*0Sstevel@tonic-gate 	LDAP_USER_CANCELLED,		0,
89*0Sstevel@tonic-gate 	LDAP_PARAM_ERROR,		0,
90*0Sstevel@tonic-gate 	LDAP_NO_MEMORY,			0,
91*0Sstevel@tonic-gate /* new with ldapv3 */
92*0Sstevel@tonic-gate 	LDAP_CONNECT_ERROR,		0,
93*0Sstevel@tonic-gate 	LDAP_NOT_SUPPORTED,		0,
94*0Sstevel@tonic-gate 	LDAP_CONTROL_NOT_FOUND,	0,
95*0Sstevel@tonic-gate 	LDAP_NO_RESULTS_RETURNED,	0,
96*0Sstevel@tonic-gate 	LDAP_MORE_RESULTS_TO_RETURN,	0,
97*0Sstevel@tonic-gate 	LDAP_CLIENT_LOOP,		0,
98*0Sstevel@tonic-gate 	LDAP_REFERRAL_LIMIT_EXCEEDED,	0,
99*0Sstevel@tonic-gate /* end of new */
100*0Sstevel@tonic-gate #else
101*0Sstevel@tonic-gate 	LDAP_SUCCESS, 			"Success",
102*0Sstevel@tonic-gate 	LDAP_OPERATIONS_ERROR, 		"Operations error",
103*0Sstevel@tonic-gate 	LDAP_PROTOCOL_ERROR, 		"Protocol error",
104*0Sstevel@tonic-gate 	LDAP_TIMELIMIT_EXCEEDED,	"Timelimit exceeded",
105*0Sstevel@tonic-gate 	LDAP_SIZELIMIT_EXCEEDED, 	"Sizelimit exceeded",
106*0Sstevel@tonic-gate 	LDAP_COMPARE_FALSE, 		"Compare false",
107*0Sstevel@tonic-gate 	LDAP_COMPARE_TRUE, 		"Compare true",
108*0Sstevel@tonic-gate 	LDAP_AUTH_METHOD_NOT_SUPPORTED, "Authentication method not supported",
109*0Sstevel@tonic-gate 	LDAP_STRONG_AUTH_REQUIRED, 	"Strong authentication required",
110*0Sstevel@tonic-gate 	LDAP_PARTIAL_RESULTS, 		"Partial results and referral received",
111*0Sstevel@tonic-gate /* new with ldapv3 */
112*0Sstevel@tonic-gate 	LDAP_REFERRAL,			"Referral received",
113*0Sstevel@tonic-gate 	LDAP_ADMINLIMIT_EXCEEDED,	"Admin. limit exceeded",
114*0Sstevel@tonic-gate 	LDAP_UNAVAILABLE_CRITICAL_EXTENSION, 	"Unavailable critical extension",
115*0Sstevel@tonic-gate 	LDAP_CONFIDENTIALITY_REQUIRED,	"Confidentiality required",
116*0Sstevel@tonic-gate /* end of new */
117*0Sstevel@tonic-gate 	LDAP_NO_SUCH_ATTRIBUTE, 	"No such attribute",
118*0Sstevel@tonic-gate 	LDAP_UNDEFINED_TYPE, 		"Undefined attribute type",
119*0Sstevel@tonic-gate 	LDAP_INAPPROPRIATE_MATCHING, 	"Inappropriate matching",
120*0Sstevel@tonic-gate 	LDAP_CONSTRAINT_VIOLATION, 	"Constraint violation",
121*0Sstevel@tonic-gate 	LDAP_TYPE_OR_VALUE_EXISTS, 	"Type or value exists",
122*0Sstevel@tonic-gate 	LDAP_INVALID_SYNTAX, 		"Invalid syntax",
123*0Sstevel@tonic-gate 	LDAP_NO_SUCH_OBJECT, 		"No such object",
124*0Sstevel@tonic-gate 	LDAP_ALIAS_PROBLEM, 		"Alias problem",
125*0Sstevel@tonic-gate 	LDAP_INVALID_DN_SYNTAX,		"Invalid DN syntax",
126*0Sstevel@tonic-gate 	LDAP_IS_LEAF, 			"Object is a leaf",
127*0Sstevel@tonic-gate 	LDAP_ALIAS_DEREF_PROBLEM, 	"Alias dereferencing problem",
128*0Sstevel@tonic-gate 	LDAP_INAPPROPRIATE_AUTH, 	"Inappropriate authentication",
129*0Sstevel@tonic-gate 	LDAP_INVALID_CREDENTIALS, 	"Invalid credentials",
130*0Sstevel@tonic-gate 	LDAP_INSUFFICIENT_ACCESS, 	"Insufficient access",
131*0Sstevel@tonic-gate 	LDAP_BUSY, 			"DSA is busy",
132*0Sstevel@tonic-gate 	LDAP_UNAVAILABLE, 		"DSA is unavailable",
133*0Sstevel@tonic-gate 	LDAP_UNWILLING_TO_PERFORM, 	"DSA is unwilling to perform",
134*0Sstevel@tonic-gate 	LDAP_LOOP_DETECT, 		"Loop detected",
135*0Sstevel@tonic-gate 	LDAP_NAMING_VIOLATION, 		"Naming violation",
136*0Sstevel@tonic-gate 	LDAP_OBJECT_CLASS_VIOLATION, 	"Object class violation",
137*0Sstevel@tonic-gate 	LDAP_NOT_ALLOWED_ON_NONLEAF, 	"Operation not allowed on nonleaf",
138*0Sstevel@tonic-gate 	LDAP_NOT_ALLOWED_ON_RDN, 	"Operation not allowed on RDN",
139*0Sstevel@tonic-gate 	LDAP_ALREADY_EXISTS, 		"Already exists",
140*0Sstevel@tonic-gate 	LDAP_NO_OBJECT_CLASS_MODS, 	"Cannot modify object class",
141*0Sstevel@tonic-gate 	LDAP_RESULTS_TOO_LARGE,		"Results too large",
142*0Sstevel@tonic-gate /* new with ldapv3 */
143*0Sstevel@tonic-gate 	LDAP_AFFECTS_MULTIPLE_DSAS, "Affects multiple DSAs",
144*0Sstevel@tonic-gate /* end of new */
145*0Sstevel@tonic-gate 	LDAP_OTHER, 			"Unknown error",
146*0Sstevel@tonic-gate 	LDAP_SERVER_DOWN,		"Can't contact LDAP server",
147*0Sstevel@tonic-gate 	LDAP_LOCAL_ERROR,		"Local error",
148*0Sstevel@tonic-gate 	LDAP_ENCODING_ERROR,		"Encoding error",
149*0Sstevel@tonic-gate 	LDAP_DECODING_ERROR,		"Decoding error",
150*0Sstevel@tonic-gate 	LDAP_TIMEOUT,			"Timed out",
151*0Sstevel@tonic-gate 	LDAP_AUTH_UNKNOWN,		"Unknown authentication method",
152*0Sstevel@tonic-gate 	LDAP_FILTER_ERROR,		"Bad search filter",
153*0Sstevel@tonic-gate 	LDAP_USER_CANCELLED,		"User cancelled operation",
154*0Sstevel@tonic-gate 	LDAP_PARAM_ERROR,		"Bad parameter to an ldap routine",
155*0Sstevel@tonic-gate 	LDAP_NO_MEMORY,			"Out of memory",
156*0Sstevel@tonic-gate /* new with ldapv3 */
157*0Sstevel@tonic-gate 	LDAP_CONNECT_ERROR,		"Connection error",
158*0Sstevel@tonic-gate 	LDAP_NOT_SUPPORTED,		"Not supported",
159*0Sstevel@tonic-gate 	LDAP_CONTROL_NOT_FOUND,	"Control not found",
160*0Sstevel@tonic-gate 	LDAP_NO_RESULTS_RETURNED,	"No results have been returned",
161*0Sstevel@tonic-gate 	LDAP_MORE_RESULTS_TO_RETURN,	"More results to return",
162*0Sstevel@tonic-gate 	LDAP_CLIENT_LOOP,		"Loop detected in referrals",
163*0Sstevel@tonic-gate 	LDAP_REFERRAL_LIMIT_EXCEEDED,	"Too many referrals followed",
164*0Sstevel@tonic-gate /* end of new */
165*0Sstevel@tonic-gate #endif
166*0Sstevel@tonic-gate 	-1, 0
167*0Sstevel@tonic-gate };
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate #ifdef SUN
170*0Sstevel@tonic-gate #pragma init	(fill_ldap_errlist)
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate static void fill_ldap_errlist()
173*0Sstevel@tonic-gate {
174*0Sstevel@tonic-gate 	int i=0;
175*0Sstevel@tonic-gate 	Debug(LDAP_DEBUG_TRACE, "fill_ldap_errlist\n", 0, 0, 0 );
176*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 130, "Success");
177*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 131, "Operations error");
178*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 132, "Protocol error");
179*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 133, "Timelimit exceeded");
180*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 134, "Sizelimit exceeded");
181*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 135, "Compare false");
182*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 136, "Compare true");
183*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 137, "Strong authentication not supported");
184*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 138, "Strong authentication required");
185*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 139, "Partial results and referral received");
186*0Sstevel@tonic-gate /* new with ldapv3 */
187*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 1262, "Referral received");
188*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 1263, "Admin. limit exceeded");
189*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 1264, "Unavailable critical extension");
190*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 1265, "Confidentiality required");
191*0Sstevel@tonic-gate /* end of new */
192*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 140, "No such attribute");
193*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 141, "Undefined attribute type");
194*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 142, "Inappropriate matching");
195*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 143, "Constraint violation");
196*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 144, "Type or value exists");
197*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 145, "Invalid syntax");
198*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 146, "No such object");
199*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 147, "Alias problem");
200*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 148, "Invalid DN syntax");
201*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 149, "Object is a leaf");
202*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 150, "Alias dereferencing problem");
203*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 151, "Inappropriate authentication");
204*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 152, "Invalid credentials");
205*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 153, "Insufficient access");
206*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 154, "DSA is busy");
207*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 155, "DSA is unavailable");
208*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 156, "DSA is unwilling to perform");
209*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 157, "Loop detected");
210*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 158, "Naming violation");
211*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 159, "Object class violation");
212*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 160, "Operation not allowed on nonleaf");
213*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 161, "Operation not allowed on RDN");
214*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 162, "Already exists");
215*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 163, "Cannot modify object class");
216*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 164, "Results too large");
217*0Sstevel@tonic-gate /* new with ldapv3 */
218*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 1266, "Affects multiple DSAs");
219*0Sstevel@tonic-gate /* end of new */
220*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 165, "Unknown error");
221*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 166, "Can't contact LDAP server");
222*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 167, "Local error");
223*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 168, "Encoding error");
224*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 169, "Decoding error");
225*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 170, "Timed out");
226*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 171, "Unknown authentication method");
227*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 172, "Bad search filter");
228*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 173, "User cancelled operation");
229*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 174, "Bad parameter to an ldap routine");
230*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 175, "Out of memory");
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 1267, "Connection error");
233*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 1268, "Not supported");
234*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 1269, "Control not found");
235*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 1270, "No results have been returned");
236*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 1271, "More results to return");
237*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 1272, "Loop detected in referrals");
238*0Sstevel@tonic-gate 	ldap_errlist[i++].e_reason = catgets(slapdcat, 1, 1273, "Too many referrals followed");
239*0Sstevel@tonic-gate }
240*0Sstevel@tonic-gate #endif
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate char *
243*0Sstevel@tonic-gate ldap_err2string( int err )
244*0Sstevel@tonic-gate {
245*0Sstevel@tonic-gate 	int	i;
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, "ldap_err2string\n", 0, 0, 0 );
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	for ( i = 0; ldap_errlist[i].e_code != -1; i++ ) {
250*0Sstevel@tonic-gate 		if ( err == ldap_errlist[i].e_code )
251*0Sstevel@tonic-gate 			return( ldap_errlist[i].e_reason );
252*0Sstevel@tonic-gate 	}
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	return( catgets(slapdcat, 1 , 165, "Unknown error") );
255*0Sstevel@tonic-gate }
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate #ifndef NO_USERINTERFACE
258*0Sstevel@tonic-gate void
259*0Sstevel@tonic-gate ldap_perror( LDAP *ld, char *s )
260*0Sstevel@tonic-gate {
261*0Sstevel@tonic-gate 	int	i;
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, "ldap_perror\n", 0, 0, 0 );
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 	if ( ld == NULL ) {
266*0Sstevel@tonic-gate 		perror( s );
267*0Sstevel@tonic-gate 		return;
268*0Sstevel@tonic-gate 	}
269*0Sstevel@tonic-gate #ifdef SUN
270*0Sstevel@tonic-gate 	/* for I18N */
271*0Sstevel@tonic-gate 	if ( ldap_errlist[0].e_reason == NULL ) {
272*0Sstevel@tonic-gate 		fill_ldap_errlist();
273*0Sstevel@tonic-gate 	} /* end if */
274*0Sstevel@tonic-gate #endif
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	for ( i = 0; ldap_errlist[i].e_code != -1; i++ ) {
277*0Sstevel@tonic-gate 		if ( ld->ld_errno == ldap_errlist[i].e_code ) {
278*0Sstevel@tonic-gate 			(void) fprintf( stderr, "%s: %s\n", s,
279*0Sstevel@tonic-gate 			    ldap_errlist[i].e_reason );
280*0Sstevel@tonic-gate 			if ( ld->ld_matched != NULL && *ld->ld_matched != '\0' )
281*0Sstevel@tonic-gate 				(void) fprintf( stderr, catgets(slapdcat, 1, 176, "%1$s: matched: %2$s\n"), s,
282*0Sstevel@tonic-gate 				    ld->ld_matched );
283*0Sstevel@tonic-gate 			if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
284*0Sstevel@tonic-gate 				(void) fprintf( stderr, catgets(slapdcat, 1, 177, "%1$s: additional info: %2$s\n"),
285*0Sstevel@tonic-gate 				    s, ld->ld_error );
286*0Sstevel@tonic-gate 			(void) fflush( stderr );
287*0Sstevel@tonic-gate 			return;
288*0Sstevel@tonic-gate 		}
289*0Sstevel@tonic-gate 	}
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	(void) fprintf( stderr, catgets(slapdcat, 1, 178, "%1$s: Not an LDAP errno %2$d\n"), s, ld->ld_errno );
292*0Sstevel@tonic-gate 	(void) fflush( stderr );
293*0Sstevel@tonic-gate }
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate #else
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate void
298*0Sstevel@tonic-gate ldap_perror( LDAP *ld, char *s )
299*0Sstevel@tonic-gate {
300*0Sstevel@tonic-gate }
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate #endif /* NO_USERINTERFACE */
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate int
306*0Sstevel@tonic-gate ldap_result2error( LDAP *ld, LDAPMessage *r, int freeit )
307*0Sstevel@tonic-gate {
308*0Sstevel@tonic-gate 	LDAPMessage	*lm;
309*0Sstevel@tonic-gate 	BerElement	ber;
310*0Sstevel@tonic-gate 	int		along;
311*0Sstevel@tonic-gate 	int		rc;
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 	Debug( LDAP_DEBUG_TRACE, "ldap_result2error\n", 0, 0, 0 );
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	if ( r == NULLMSG )
316*0Sstevel@tonic-gate 		return( LDAP_PARAM_ERROR );
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 	for ( lm = r; lm->lm_chain != NULL; lm = lm->lm_chain )
319*0Sstevel@tonic-gate 		;	/* NULL */
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate 	if ( ld->ld_error ) {
322*0Sstevel@tonic-gate 		free( ld->ld_error );
323*0Sstevel@tonic-gate 		ld->ld_error = NULL;
324*0Sstevel@tonic-gate 	}
325*0Sstevel@tonic-gate 	if ( ld->ld_matched ) {
326*0Sstevel@tonic-gate 		free( ld->ld_matched );
327*0Sstevel@tonic-gate 		ld->ld_matched = NULL;
328*0Sstevel@tonic-gate 	}
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	ber = *(lm->lm_ber);
331*0Sstevel@tonic-gate 	if ( ld->ld_version == LDAP_VERSION2 ) {
332*0Sstevel@tonic-gate 		rc = ber_scanf( &ber, "{iaa}", &along, &ld->ld_matched,
333*0Sstevel@tonic-gate 		    &ld->ld_error );
334*0Sstevel@tonic-gate 	} else {
335*0Sstevel@tonic-gate 		rc = ber_scanf( &ber, "{ia}", &along, &ld->ld_error );
336*0Sstevel@tonic-gate 	}
337*0Sstevel@tonic-gate 	if ( rc == LBER_ERROR ) {
338*0Sstevel@tonic-gate 		ld->ld_errno = LDAP_DECODING_ERROR;
339*0Sstevel@tonic-gate 	} else {
340*0Sstevel@tonic-gate 		ld->ld_errno = along;
341*0Sstevel@tonic-gate 	}
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 	if ( freeit )
344*0Sstevel@tonic-gate 		ldap_msgfree( r );
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 	return( ld->ld_errno );
347*0Sstevel@tonic-gate }
348