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