]> git.sur5r.net Git - openldap/blob - libraries/libldap/error.c
"const"ify some static arrays, and related parameters/variables
[openldap] / libraries / libldap / error.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5
6 #include "portable.h"
7
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include <ac/socket.h>
12 #include <ac/string.h>
13 #include <ac/time.h>
14
15 #include "ldap-int.h"
16
17 struct ldaperror {
18         int     e_code;
19         char    *e_reason;
20 };
21
22 static const struct ldaperror ldap_errlist[] = {
23         {LDAP_SUCCESS,                                  "Success" },
24         {LDAP_OPERATIONS_ERROR,                 "Operations error" },
25         {LDAP_PROTOCOL_ERROR,                   "Protocol error" },
26         {LDAP_TIMELIMIT_EXCEEDED,               "Time limit exceeded" },
27         {LDAP_SIZELIMIT_EXCEEDED,               "Size limit exceeded" },
28         {LDAP_COMPARE_FALSE,                    "Compare false" },
29         {LDAP_COMPARE_TRUE,                     "Compare true" },
30         {LDAP_STRONG_AUTH_NOT_SUPPORTED, "Strong authentication not supported" },
31         {LDAP_STRONG_AUTH_REQUIRED,     "Strong authentication required" },
32         {LDAP_PARTIAL_RESULTS,                  "Partial results and referral received" },
33
34         {LDAP_REFERRAL,                                 "Referral"},
35         {LDAP_ADMINLIMIT_EXCEEDED,              "Administrative limit exceeded"},
36         {LDAP_UNAVAILABLE_CRITICIAL_EXTENSION,
37                                                                         "Criticial extension is unavailable"},
38         {LDAP_CONFIDENTIALITY_REQUIRED, "Confidentiality required"},
39         {LDAP_SASL_BIND_IN_PROGRESS,    "SASL bind in progress"},
40
41         {LDAP_NO_SUCH_ATTRIBUTE,                "No such attribute" },
42         {LDAP_UNDEFINED_TYPE,                   "Undefined attribute type" },
43         {LDAP_INAPPROPRIATE_MATCHING,   "Inappropriate matching" },
44         {LDAP_CONSTRAINT_VIOLATION,     "Constraint violation" },
45         {LDAP_TYPE_OR_VALUE_EXISTS,     "Type or value exists" },
46         {LDAP_INVALID_SYNTAX,                   "Invalid syntax" },
47
48         {LDAP_NO_SUCH_OBJECT,                   "No such object" },
49         {LDAP_ALIAS_PROBLEM,                    "Alias problem" },
50         {LDAP_INVALID_DN_SYNTAX,                "Invalid DN syntax" },
51         {LDAP_IS_LEAF,                                  "Object is a leaf" },
52         {LDAP_ALIAS_DEREF_PROBLEM,              "Alias dereferencing problem" },
53
54         {LDAP_INAPPROPRIATE_AUTH,               "Inappropriate authentication" },
55         {LDAP_INVALID_CREDENTIALS,              "Invalid credentials" },
56         {LDAP_INSUFFICIENT_ACCESS,              "Insufficient access" },
57         {LDAP_BUSY,                                     "DSA is busy" },
58         {LDAP_UNAVAILABLE,                              "DSA is unavailable" },
59         {LDAP_UNWILLING_TO_PERFORM,     "DSA is unwilling to perform" },
60         {LDAP_LOOP_DETECT,                              "Loop detected" },
61
62         {LDAP_NAMING_VIOLATION,                 "Naming violation" },
63         {LDAP_OBJECT_CLASS_VIOLATION,   "Object class violation" },
64         {LDAP_NOT_ALLOWED_ON_NONLEAF,   "Operation not allowed on nonleaf" },
65         {LDAP_NOT_ALLOWED_ON_RDN,               "Operation not allowed on RDN" },
66         {LDAP_ALREADY_EXISTS,                   "Already exists" },
67         {LDAP_NO_OBJECT_CLASS_MODS,     "Cannot modify object class" },
68         {LDAP_RESULTS_TOO_LARGE,                "Results too large" },
69         {LDAP_AFFECTS_MULTIPLE_DSAS,    "Operation affects multiple DSAs" },
70
71         {LDAP_OTHER,                                    "Unknown error" },
72         {LDAP_SERVER_DOWN,                              "Can't contact LDAP server" },
73         {LDAP_LOCAL_ERROR,                              "Local error" },
74         {LDAP_ENCODING_ERROR,                   "Encoding error" },
75         {LDAP_DECODING_ERROR,                   "Decoding error" },
76         {LDAP_TIMEOUT,                                  "Timed out" },
77         {LDAP_AUTH_UNKNOWN,                             "Unknown authentication method" },
78         {LDAP_FILTER_ERROR,                             "Bad search filter" },
79         {LDAP_USER_CANCELLED,                   "User cancelled operation" },
80         {LDAP_PARAM_ERROR,                              "Bad parameter to an ldap routine" },
81         {LDAP_NO_MEMORY,                                "Out of memory" },
82
83         {LDAP_CONNECT_ERROR,                    "Connect error" },
84         {LDAP_NOT_SUPPORTED,                    "Not Supported" },
85         {LDAP_CONTROL_NOT_FOUND,                "Control not found" },
86         {LDAP_NO_RESULTS_RETURNED,              "No results returned" },
87         {LDAP_MORE_RESULTS_TO_RETURN,   "More results to return" },
88         {LDAP_CLIENT_LOOP,                              "Client Loop" },
89         {LDAP_REFERRAL_LIMIT_EXCEEDED,  "Referral Limit Exceeded" },
90
91         {-1, 0 }
92 };
93
94 char *
95 ldap_err2string( int err )
96 {
97         int     i;
98
99         Debug( LDAP_DEBUG_TRACE, "ldap_err2string\n", 0, 0, 0 );
100
101         for ( i = 0; ldap_errlist[i].e_code != -1; i++ ) {
102                 if ( err == ldap_errlist[i].e_code )
103                         return( ldap_errlist[i].e_reason );
104         }
105
106         return( "Unknown error" );
107 }
108
109 /* depreciated */
110 void
111 ldap_perror( LDAP *ld, char *s )
112 {
113         int     i;
114
115         Debug( LDAP_DEBUG_TRACE, "ldap_perror\n", 0, 0, 0 );
116
117         if ( ld == NULL ) {
118                 perror( s );
119                 return;
120         }
121
122         for ( i = 0; ldap_errlist[i].e_code != -1; i++ ) {
123                 if ( ld->ld_errno == ldap_errlist[i].e_code ) {
124                         fprintf( stderr, "%s: %s\n", s,
125                             ldap_errlist[i].e_reason );
126                         if ( ld->ld_matched != NULL && *ld->ld_matched != '\0' )
127                                 fprintf( stderr, "%s: matched: %s\n", s,
128                                     ld->ld_matched );
129                         if ( ld->ld_error != NULL && *ld->ld_error != '\0' )
130                                 fprintf( stderr, "%s: additional info: %s\n",
131                                     s, ld->ld_error );
132                         fflush( stderr );
133                         return;
134                 }
135         }
136
137         fprintf( stderr, "%s: Not an LDAP errno %d\n", s, ld->ld_errno );
138         fflush( stderr );
139 }
140
141 int
142 ldap_result2error( LDAP *ld, LDAPMessage *r, int freeit )
143 {
144         LDAPMessage     *lm;
145         BerElement      ber;
146         long            along;
147         unsigned long   rc;
148
149         Debug( LDAP_DEBUG_TRACE, "ldap_result2error\n", 0, 0, 0 );
150
151         if ( r == NULLMSG )
152                 return( LDAP_PARAM_ERROR );
153
154         for ( lm = r; lm->lm_chain != NULL; lm = lm->lm_chain )
155                 ;       /* NULL */
156
157         if ( ld->ld_error ) {
158                 free( ld->ld_error );
159                 ld->ld_error = NULL;
160         }
161         if ( ld->ld_matched ) {
162                 free( ld->ld_matched );
163                 ld->ld_matched = NULL;
164         }
165
166         ber = *(lm->lm_ber);
167         if ( ld->ld_version == LDAP_VERSION2 ) {
168                 rc = ber_scanf( &ber, "{iaa}", &along, &ld->ld_matched,
169                     &ld->ld_error );
170         } else {
171                 rc = ber_scanf( &ber, "{ia}", &along, &ld->ld_error );
172         }
173         if ( rc == LBER_ERROR ) {
174                 ld->ld_errno = LDAP_DECODING_ERROR;
175         } else {
176                 ld->ld_errno = (int) along;
177         }
178
179         if ( freeit )
180                 ldap_msgfree( r );
181
182         return( ld->ld_errno );
183 }