error.c revision 1.1.1.3
1/*	$NetBSD: error.c,v 1.1.1.3 2010/12/12 15:21:30 adam Exp $	*/
2
3/* OpenLDAP: pkg/ldap/libraries/libldap/error.c,v 1.76.2.8 2010/04/15 20:03:50 quanah Exp */
4/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 *
6 * Copyright 1998-2010 The OpenLDAP Foundation.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17
18#include "portable.h"
19
20#include <stdio.h>
21
22#include <ac/stdlib.h>
23
24#include <ac/socket.h>
25#include <ac/string.h>
26#include <ac/time.h>
27
28#include "ldap-int.h"
29
30void ldap_int_error_init( void ) {
31}
32
33char *
34ldap_err2string( int err )
35{
36	char *m;
37
38	Debug( LDAP_DEBUG_TRACE, "ldap_err2string\n", 0, 0, 0 );
39
40	switch ( err ) {
41#	define C(code, message) case code: m = message; break
42
43	/* LDAPv3 (RFC 4511) codes */
44	C(LDAP_SUCCESS,					N_("Success"));
45	C(LDAP_OPERATIONS_ERROR, 		N_("Operations error"));
46	C(LDAP_PROTOCOL_ERROR, 			N_("Protocol error"));
47	C(LDAP_TIMELIMIT_EXCEEDED,		N_("Time limit exceeded"));
48	C(LDAP_SIZELIMIT_EXCEEDED, 		N_("Size limit exceeded"));
49	C(LDAP_COMPARE_FALSE, 			N_("Compare False"));
50	C(LDAP_COMPARE_TRUE, 			N_("Compare True"));
51	C(LDAP_STRONG_AUTH_NOT_SUPPORTED,N_("Authentication method not supported"));
52	C(LDAP_STRONG_AUTH_REQUIRED,	N_("Strong(er) authentication required"));
53
54	C(LDAP_REFERRAL,				N_("Referral"));
55	C(LDAP_ADMINLIMIT_EXCEEDED,		N_("Administrative limit exceeded"));
56	C(LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
57									N_("Critical extension is unavailable"));
58	C(LDAP_CONFIDENTIALITY_REQUIRED,N_("Confidentiality required"));
59	C(LDAP_SASL_BIND_IN_PROGRESS,	N_("SASL bind in progress"));
60
61	C(LDAP_NO_SUCH_ATTRIBUTE, 		N_("No such attribute"));
62	C(LDAP_UNDEFINED_TYPE, 			N_("Undefined attribute type"));
63	C(LDAP_INAPPROPRIATE_MATCHING, 	N_("Inappropriate matching"));
64	C(LDAP_CONSTRAINT_VIOLATION, 	N_("Constraint violation"));
65	C(LDAP_TYPE_OR_VALUE_EXISTS, 	N_("Type or value exists"));
66	C(LDAP_INVALID_SYNTAX, 			N_("Invalid syntax"));
67
68	C(LDAP_NO_SUCH_OBJECT, 			N_("No such object"));
69	C(LDAP_ALIAS_PROBLEM, 			N_("Alias problem"));
70	C(LDAP_INVALID_DN_SYNTAX,		N_("Invalid DN syntax"));
71
72	C(LDAP_ALIAS_DEREF_PROBLEM,	 	N_("Alias dereferencing problem"));
73
74	C(LDAP_INAPPROPRIATE_AUTH, 		N_("Inappropriate authentication"));
75	C(LDAP_INVALID_CREDENTIALS, 	N_("Invalid credentials"));
76	C(LDAP_INSUFFICIENT_ACCESS, 	N_("Insufficient access"));
77	C(LDAP_BUSY, 					N_("Server is busy"));
78	C(LDAP_UNAVAILABLE, 			N_("Server is unavailable"));
79	C(LDAP_UNWILLING_TO_PERFORM, 	N_("Server is unwilling to perform"));
80	C(LDAP_LOOP_DETECT, 			N_("Loop detected"));
81
82	C(LDAP_NAMING_VIOLATION, 		N_("Naming violation"));
83	C(LDAP_OBJECT_CLASS_VIOLATION, 	N_("Object class violation"));
84	C(LDAP_NOT_ALLOWED_ON_NONLEAF, 	N_("Operation not allowed on non-leaf"));
85	C(LDAP_NOT_ALLOWED_ON_RDN,	 	N_("Operation not allowed on RDN"));
86	C(LDAP_ALREADY_EXISTS, 			N_("Already exists"));
87	C(LDAP_NO_OBJECT_CLASS_MODS, 	N_("Cannot modify object class"));
88
89	C(LDAP_AFFECTS_MULTIPLE_DSAS,	N_("Operation affects multiple DSAs"));
90
91	/* Virtual List View draft */
92	C(LDAP_VLV_ERROR,				N_("Virtual List View error"));
93
94	C(LDAP_OTHER, N_("Other (e.g., implementation specific) error"));
95
96	/* LDAPv2 (RFC 1777) codes */
97	C(LDAP_PARTIAL_RESULTS, N_("Partial results and referral received"));
98	C(LDAP_IS_LEAF, 				N_("Entry is a leaf"));
99
100	/* Connection-less LDAP (CLDAP - RFC 1798) code */
101	C(LDAP_RESULTS_TOO_LARGE,		N_("Results too large"));
102
103	/* Cancel Operation (RFC 3909) codes */
104	C(LDAP_CANCELLED,				N_("Cancelled"));
105	C(LDAP_NO_SUCH_OPERATION,		N_("No Operation to Cancel"));
106	C(LDAP_TOO_LATE,				N_("Too Late to Cancel"));
107	C(LDAP_CANNOT_CANCEL,			N_("Cannot Cancel"));
108
109	/* Assert Control (RFC 4528 and old internet-draft) codes */
110	C(LDAP_ASSERTION_FAILED,		N_("Assertion Failed"));
111	C(LDAP_X_ASSERTION_FAILED,		N_("Assertion Failed (X)"));
112
113	/* Proxied Authorization Control (RFC 4370 and I-D) codes */
114	C(LDAP_PROXIED_AUTHORIZATION_DENIED, N_("Proxied Authorization Denied"));
115	C(LDAP_X_PROXY_AUTHZ_FAILURE,	N_("Proxy Authorization Failure (X)"));
116
117	/* Content Sync Operation (RFC 4533 and I-D) codes */
118	C(LDAP_SYNC_REFRESH_REQUIRED,	N_("Content Sync Refresh Required"));
119	C(LDAP_X_SYNC_REFRESH_REQUIRED,	N_("Content Sync Refresh Required (X)"));
120
121	/* No-Op Control (draft-zeilenga-ldap-noop) code */
122	C(LDAP_X_NO_OPERATION,			N_("No Operation (X)"));
123
124	/* Client Update Protocol (RFC 3928) codes */
125	C(LDAP_CUP_RESOURCES_EXHAUSTED,	N_("LCUP Resources Exhausted"));
126	C(LDAP_CUP_SECURITY_VIOLATION,	N_("LCUP Security Violation"));
127	C(LDAP_CUP_INVALID_DATA,		N_("LCUP Invalid Data"));
128	C(LDAP_CUP_UNSUPPORTED_SCHEME,	N_("LCUP Unsupported Scheme"));
129	C(LDAP_CUP_RELOAD_REQUIRED,		N_("LCUP Reload Required"));
130
131#ifdef LDAP_X_TXN
132	/* Codes related to LDAP Transactions (draft-zeilenga-ldap-txn) */
133	C(LDAP_X_TXN_SPECIFY_OKAY,		N_("TXN specify okay"));
134	C(LDAP_X_TXN_ID_INVALID,		N_("TXN ID is invalid"));
135#endif
136
137	/* API codes - renumbered since draft-ietf-ldapext-ldap-c-api */
138	C(LDAP_SERVER_DOWN,				N_("Can't contact LDAP server"));
139	C(LDAP_LOCAL_ERROR,				N_("Local error"));
140	C(LDAP_ENCODING_ERROR,			N_("Encoding error"));
141	C(LDAP_DECODING_ERROR,			N_("Decoding error"));
142	C(LDAP_TIMEOUT,					N_("Timed out"));
143	C(LDAP_AUTH_UNKNOWN,			N_("Unknown authentication method"));
144	C(LDAP_FILTER_ERROR,			N_("Bad search filter"));
145	C(LDAP_USER_CANCELLED,			N_("User cancelled operation"));
146	C(LDAP_PARAM_ERROR,				N_("Bad parameter to an ldap routine"));
147	C(LDAP_NO_MEMORY,				N_("Out of memory"));
148	C(LDAP_CONNECT_ERROR,			N_("Connect error"));
149	C(LDAP_NOT_SUPPORTED,			N_("Not Supported"));
150	C(LDAP_CONTROL_NOT_FOUND,		N_("Control not found"));
151	C(LDAP_NO_RESULTS_RETURNED,		N_("No results returned"));
152	C(LDAP_MORE_RESULTS_TO_RETURN,	N_("More results to return"));
153	C(LDAP_CLIENT_LOOP,				N_("Client Loop"));
154	C(LDAP_REFERRAL_LIMIT_EXCEEDED,	N_("Referral Limit Exceeded"));
155#	undef C
156
157	default:
158		m = (LDAP_API_ERROR(err) ? N_("Unknown API error")
159			 : LDAP_E_ERROR(err) ? N_("Unknown (extension) error")
160			 : LDAP_X_ERROR(err) ? N_("Unknown (private extension) error")
161			 : N_("Unknown error"));
162		break;
163	}
164
165	return _(m);
166}
167
168/* deprecated */
169void
170ldap_perror( LDAP *ld, LDAP_CONST char *str )
171{
172    int i;
173
174	assert( ld != NULL );
175	assert( LDAP_VALID( ld ) );
176	assert( str != NULL );
177
178	fprintf( stderr, "%s: %s (%d)\n",
179		str ? str : "ldap_perror",
180		ldap_err2string( ld->ld_errno ),
181		ld->ld_errno );
182
183	if ( ld->ld_matched != NULL && ld->ld_matched[0] != '\0' ) {
184		fprintf( stderr, _("\tmatched DN: %s\n"), ld->ld_matched );
185	}
186
187	if ( ld->ld_error != NULL && ld->ld_error[0] != '\0' ) {
188		fprintf( stderr, _("\tadditional info: %s\n"), ld->ld_error );
189	}
190
191	if ( ld->ld_referrals != NULL && ld->ld_referrals[0] != NULL) {
192		fprintf( stderr, _("\treferrals:\n") );
193		for (i=0; ld->ld_referrals[i]; i++) {
194			fprintf( stderr, _("\t\t%s\n"), ld->ld_referrals[i] );
195		}
196	}
197
198	fflush( stderr );
199}
200
201/* deprecated */
202int
203ldap_result2error( LDAP *ld, LDAPMessage *r, int freeit )
204{
205	int rc, err;
206
207	rc = ldap_parse_result( ld, r, &err,
208		NULL, NULL, NULL, NULL, freeit );
209
210	return err != LDAP_SUCCESS ? err : rc;
211}
212
213/*
214 * Parse LDAPResult Messages:
215 *
216 *   LDAPResult ::= SEQUENCE {
217 *     resultCode      ENUMERATED,
218 *     matchedDN       LDAPDN,
219 *     errorMessage    LDAPString,
220 *     referral        [3] Referral OPTIONAL }
221 *
222 * including Bind results:
223 *
224 *   BindResponse ::= [APPLICATION 1] SEQUENCE {
225 *     COMPONENTS OF LDAPResult,
226 *     serverSaslCreds  [7] OCTET STRING OPTIONAL }
227 *
228 * and ExtendedOp results:
229 *
230 *   ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
231 *     COMPONENTS OF LDAPResult,
232 *     responseName     [10] LDAPOID OPTIONAL,
233 *     response         [11] OCTET STRING OPTIONAL }
234 *
235 */
236int
237ldap_parse_result(
238	LDAP			*ld,
239	LDAPMessage		*r,
240	int				*errcodep,
241	char			**matcheddnp,
242	char			**errmsgp,
243	char			***referralsp,
244	LDAPControl		***serverctrls,
245	int				freeit )
246{
247	LDAPMessage	*lm;
248	ber_int_t errcode = LDAP_SUCCESS;
249
250	ber_tag_t tag;
251	BerElement	*ber;
252
253	Debug( LDAP_DEBUG_TRACE, "ldap_parse_result\n", 0, 0, 0 );
254
255	assert( ld != NULL );
256	assert( LDAP_VALID( ld ) );
257	assert( r != NULL );
258
259	if(errcodep != NULL) *errcodep = LDAP_SUCCESS;
260	if(matcheddnp != NULL) *matcheddnp = NULL;
261	if(errmsgp != NULL) *errmsgp = NULL;
262	if(referralsp != NULL) *referralsp = NULL;
263	if(serverctrls != NULL) *serverctrls = NULL;
264
265#ifdef LDAP_R_COMPILE
266	ldap_pvt_thread_mutex_lock( &ld->ld_res_mutex );
267#endif
268	/* Find the result, last msg in chain... */
269	lm = r->lm_chain_tail;
270	/* FIXME: either this is not possible (assert?)
271	 * or it should be handled */
272	if ( lm != NULL ) {
273		switch ( lm->lm_msgtype ) {
274		case LDAP_RES_SEARCH_ENTRY:
275		case LDAP_RES_SEARCH_REFERENCE:
276		case LDAP_RES_INTERMEDIATE:
277			lm = NULL;
278			break;
279
280		default:
281			break;
282		}
283	}
284
285	if( lm == NULL ) {
286		errcode = ld->ld_errno = LDAP_NO_RESULTS_RETURNED;
287#ifdef LDAP_R_COMPILE
288		ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
289#endif
290	    goto done;
291	}
292
293	if ( ld->ld_error ) {
294		LDAP_FREE( ld->ld_error );
295		ld->ld_error = NULL;
296	}
297	if ( ld->ld_matched ) {
298		LDAP_FREE( ld->ld_matched );
299		ld->ld_matched = NULL;
300	}
301	if ( ld->ld_referrals ) {
302		LDAP_VFREE( ld->ld_referrals );
303		ld->ld_referrals = NULL;
304	}
305
306	/* parse results */
307
308	ber = ber_dup( lm->lm_ber );
309
310	if ( ld->ld_version < LDAP_VERSION2 ) {
311		tag = ber_scanf( ber, "{iA}",
312			&ld->ld_errno, &ld->ld_error );
313
314	} else {
315		ber_len_t len;
316
317		tag = ber_scanf( ber, "{iAA" /*}*/,
318			&ld->ld_errno, &ld->ld_matched, &ld->ld_error );
319
320		if( tag != LBER_ERROR ) {
321			/* peek for referrals */
322			if( ber_peek_tag(ber, &len) == LDAP_TAG_REFERRAL ) {
323				tag = ber_scanf( ber, "v", &ld->ld_referrals );
324			}
325		}
326
327		/* need to clean out misc items */
328		if( tag != LBER_ERROR ) {
329			if( lm->lm_msgtype == LDAP_RES_BIND ) {
330				/* look for sasl result creditials */
331				if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SASL_RES_CREDS ) {
332					/* skip 'em */
333					tag = ber_scanf( ber, "x" );
334				}
335
336			} else if( lm->lm_msgtype == LDAP_RES_EXTENDED ) {
337				/* look for exop result oid or value */
338				if ( ber_peek_tag( ber, &len ) == LDAP_TAG_EXOP_RES_OID ) {
339					/* skip 'em */
340					tag = ber_scanf( ber, "x" );
341				}
342
343				if ( tag != LBER_ERROR &&
344					ber_peek_tag( ber, &len ) == LDAP_TAG_EXOP_RES_VALUE )
345				{
346					/* skip 'em */
347					tag = ber_scanf( ber, "x" );
348				}
349			}
350		}
351
352		if( tag != LBER_ERROR ) {
353			int rc = ldap_pvt_get_controls( ber, serverctrls );
354
355			if( rc != LDAP_SUCCESS ) {
356				tag = LBER_ERROR;
357			}
358		}
359
360		if( tag != LBER_ERROR ) {
361			tag = ber_scanf( ber, /*{*/"}" );
362		}
363	}
364
365	if ( tag == LBER_ERROR ) {
366		ld->ld_errno = errcode = LDAP_DECODING_ERROR;
367	}
368
369	if( ber != NULL ) {
370		ber_free( ber, 0 );
371	}
372
373	/* return */
374	if( errcodep != NULL ) {
375		*errcodep = ld->ld_errno;
376	}
377	if ( errcode == LDAP_SUCCESS ) {
378		if( matcheddnp != NULL ) {
379			if ( ld->ld_matched )
380			{
381				*matcheddnp = LDAP_STRDUP( ld->ld_matched );
382			}
383		}
384		if( errmsgp != NULL ) {
385			if ( ld->ld_error )
386			{
387				*errmsgp = LDAP_STRDUP( ld->ld_error );
388			}
389		}
390
391		if( referralsp != NULL) {
392			*referralsp = ldap_value_dup( ld->ld_referrals );
393		}
394	}
395
396#ifdef LDAP_R_COMPILE
397	ldap_pvt_thread_mutex_unlock( &ld->ld_res_mutex );
398#endif
399
400done:
401	if ( freeit ) {
402		ldap_msgfree( r );
403	}
404
405	return errcode;
406}
407