error.c 11 KB

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