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