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