xref: /onnv-gate/usr/src/lib/libldap5/sources/ldap/ssldap/errormap.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*
7*0Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
8*0Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
9*0Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
10*0Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
13*0Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14*0Sstevel@tonic-gate  * implied. See the License for the specific language governing
15*0Sstevel@tonic-gate  * rights and limitations under the License.
16*0Sstevel@tonic-gate  *
17*0Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
18*0Sstevel@tonic-gate  * March 31, 1998.
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
21*0Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
22*0Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
23*0Sstevel@tonic-gate  * Rights Reserved.
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * Contributor(s):
26*0Sstevel@tonic-gate  */
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * errormap.c - map NSPR and OS errors to strings
32*0Sstevel@tonic-gate  *
33*0Sstevel@tonic-gate  * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF NETSCAPE COMMUNICATIONS
34*0Sstevel@tonic-gate  * CORPORATION
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  * Copyright (C) 1998-9 Netscape Communications Corporation. All Rights Reserved.
37*0Sstevel@tonic-gate  *
38*0Sstevel@tonic-gate  * Use of this Source Code is subject to the terms of the applicable license
39*0Sstevel@tonic-gate  * agreement from Netscape Communications Corporation.
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * The copyright notice(s) in this Source Code does not indicate actual or
42*0Sstevel@tonic-gate  * intended publication of this Source Code.
43*0Sstevel@tonic-gate  */
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate /* XXX ceb
46*0Sstevel@tonic-gate  * This code was stolen from Directory server.
47*0Sstevel@tonic-gate  * ns/netsite/ldap/servers/slapd/errormap.c
48*0Sstevel@tonic-gate  * OS errors are not handled, so the os error has been removed.
49*0Sstevel@tonic-gate  */
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate #if defined( _WINDOWS )
53*0Sstevel@tonic-gate #include <windows.h>
54*0Sstevel@tonic-gate #include "proto-ntutil.h"
55*0Sstevel@tonic-gate #endif
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate #include <nspr.h>
58*0Sstevel@tonic-gate #include <ssl.h>
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate #include <ldap.h>
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate #ifdef _SOLARIS_SDK
63*0Sstevel@tonic-gate #include <synch.h>
64*0Sstevel@tonic-gate #include <libintl.h>
65*0Sstevel@tonic-gate #endif /* _SOLARIS_SDK */
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  * function protoypes
70*0Sstevel@tonic-gate  */
71*0Sstevel@tonic-gate static const char *SECU_Strerror(PRErrorCode errNum);
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate /*
76*0Sstevel@tonic-gate  * return the string equivalent of an NSPR error
77*0Sstevel@tonic-gate  */
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate const char *
80*0Sstevel@tonic-gate LDAP_CALL
ldapssl_err2string(const int prerrno)81*0Sstevel@tonic-gate ldapssl_err2string( const int prerrno )
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate     const char	*s;
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate     if (( s = SECU_Strerror( (PRErrorCode)prerrno )) == NULL ) {
86*0Sstevel@tonic-gate 	s = dgettext(TEXT_DOMAIN, "unknown");
87*0Sstevel@tonic-gate     }
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate     return( s );
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate /*
93*0Sstevel@tonic-gate  ****************************************************************************
94*0Sstevel@tonic-gate  * The code below this point was provided by Nelson Bolyard <nelsonb> of the
95*0Sstevel@tonic-gate  *	Netscape Certificate Server team on 27-March-1998.
96*0Sstevel@tonic-gate  *	Taken from the file ns/security/cmd/lib/secerror.c on NSS_1_BRANCH.
97*0Sstevel@tonic-gate  *	Last updated from there: 24-July-1998 by Mark Smith <mcs>
98*0Sstevel@tonic-gate  *	Last updated from there: 14-July-1999 by chuck boatwright <cboatwri>
99*0Sstevel@tonic-gate  *
100*0Sstevel@tonic-gate  *
101*0Sstevel@tonic-gate  * All of the Directory Server specific changes are enclosed inside
102*0Sstevel@tonic-gate  *	#ifdef NS_DIRECTORY.
103*0Sstevel@tonic-gate  ****************************************************************************
104*0Sstevel@tonic-gate  */
105*0Sstevel@tonic-gate #include "nspr.h"
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate /*
108*0Sstevel@tonic-gate  * XXXceb as a hack, we will locally define NS_DIRECTORY
109*0Sstevel@tonic-gate  */
110*0Sstevel@tonic-gate #define NS_DIRECTORY 1
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate struct tuple_str {
113*0Sstevel@tonic-gate     PRErrorCode	 errNum;
114*0Sstevel@tonic-gate     const char * errString;
115*0Sstevel@tonic-gate };
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate typedef struct tuple_str tuple_str;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate #ifndef _SOLARIS_SDK
120*0Sstevel@tonic-gate #define ER2(a,b)   {a, b},
121*0Sstevel@tonic-gate #define ER3(a,b,c) {a, c},
122*0Sstevel@tonic-gate #else
123*0Sstevel@tonic-gate #define ER2(a,b)   {a, NULL},
124*0Sstevel@tonic-gate #define ER3(a,b,c) {a, NULL},
125*0Sstevel@tonic-gate #endif
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate #include "secerr.h"
128*0Sstevel@tonic-gate #include "sslerr.h"
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate #ifndef _SOLARIS_SDK
131*0Sstevel@tonic-gate const tuple_str errStrings[] = {
132*0Sstevel@tonic-gate #else
133*0Sstevel@tonic-gate tuple_str errStrings[] = {
134*0Sstevel@tonic-gate #endif
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate /* keep this list in asceding order of error numbers */
137*0Sstevel@tonic-gate #ifdef NS_DIRECTORY
138*0Sstevel@tonic-gate #include "sslerrstrs.h"
139*0Sstevel@tonic-gate #include "secerrstrs.h"
140*0Sstevel@tonic-gate #include "prerrstrs.h"
141*0Sstevel@tonic-gate /*
142*0Sstevel@tonic-gate  * XXXceb -- LDAPSDK won't care about disconnect
143*0Sstevel@tonic-gate #include "disconnect_error_strings.h"
144*0Sstevel@tonic-gate  */
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate #else /* NS_DIRECTORY */
147*0Sstevel@tonic-gate #include "SSLerrs.h"
148*0Sstevel@tonic-gate #include "SECerrs.h"
149*0Sstevel@tonic-gate #include "NSPRerrs.h"
150*0Sstevel@tonic-gate #endif /* NS_DIRECTORY */
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate };
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate const PRInt32 numStrings = sizeof(errStrings) / sizeof(tuple_str);
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate /* Returns a UTF-8 encoded constant error string for "errNum".
157*0Sstevel@tonic-gate  * Returns NULL of errNum is unknown.
158*0Sstevel@tonic-gate  */
159*0Sstevel@tonic-gate #ifndef _SOLARIS_SDK
160*0Sstevel@tonic-gate #ifdef NS_DIRECTORY
161*0Sstevel@tonic-gate static
162*0Sstevel@tonic-gate #endif /* NS_DIRECTORY */
163*0Sstevel@tonic-gate const char *
SECU_Strerror(PRErrorCode errNum)164*0Sstevel@tonic-gate SECU_Strerror(PRErrorCode errNum) {
165*0Sstevel@tonic-gate     PRInt32 low  = 0;
166*0Sstevel@tonic-gate     PRInt32 high = numStrings - 1;
167*0Sstevel@tonic-gate     PRInt32 i;
168*0Sstevel@tonic-gate     PRErrorCode num;
169*0Sstevel@tonic-gate     static int initDone;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate     /* make sure table is in ascending order.
172*0Sstevel@tonic-gate      * binary search depends on it.
173*0Sstevel@tonic-gate      */
174*0Sstevel@tonic-gate     if (!initDone) {
175*0Sstevel@tonic-gate 	PRErrorCode lastNum = 0x80000000;
176*0Sstevel@tonic-gate     	for (i = low; i <= high; ++i) {
177*0Sstevel@tonic-gate 	    num = errStrings[i].errNum;
178*0Sstevel@tonic-gate 	    if (num <= lastNum) {
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate /*
181*0Sstevel@tonic-gate  * XXXceb
182*0Sstevel@tonic-gate  * We aren't handling out of sequence errors.
183*0Sstevel@tonic-gate  */
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate #if 0
187*0Sstevel@tonic-gate #ifdef NS_DIRECTORY
188*0Sstevel@tonic-gate 		LDAPDebug( LDAP_DEBUG_ANY,
189*0Sstevel@tonic-gate 			"sequence error in error strings at item %d\n"
190*0Sstevel@tonic-gate 			"error %d (%s)\n",
191*0Sstevel@tonic-gate 			i, lastNum, errStrings[i-1].errString );
192*0Sstevel@tonic-gate 		LDAPDebug( LDAP_DEBUG_ANY,
193*0Sstevel@tonic-gate 			"should come after \n"
194*0Sstevel@tonic-gate 			"error %d (%s)\n",
195*0Sstevel@tonic-gate 			num, errStrings[i].errString, 0 );
196*0Sstevel@tonic-gate #else /* NS_DIRECTORY */
197*0Sstevel@tonic-gate 	    	fprintf(stderr,
198*0Sstevel@tonic-gate "sequence error in error strings at item %d\n"
199*0Sstevel@tonic-gate "error %d (%s)\n"
200*0Sstevel@tonic-gate "should come after \n"
201*0Sstevel@tonic-gate "error %d (%s)\n",
202*0Sstevel@tonic-gate 		        i, lastNum, errStrings[i-1].errString,
203*0Sstevel@tonic-gate 			num, errStrings[i].errString);
204*0Sstevel@tonic-gate #endif /* NS_DIRECTORY */
205*0Sstevel@tonic-gate #endif /* 0 */
206*0Sstevel@tonic-gate 	    }
207*0Sstevel@tonic-gate 	    lastNum = num;
208*0Sstevel@tonic-gate 	}
209*0Sstevel@tonic-gate 	initDone = 1;
210*0Sstevel@tonic-gate     }
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate     /* Do binary search of table. */
213*0Sstevel@tonic-gate     while (low + 1 < high) {
214*0Sstevel@tonic-gate     	i = (low + high) / 2;
215*0Sstevel@tonic-gate 	num = errStrings[i].errNum;
216*0Sstevel@tonic-gate 	if (errNum == num)
217*0Sstevel@tonic-gate 	    return errStrings[i].errString;
218*0Sstevel@tonic-gate         if (errNum < num)
219*0Sstevel@tonic-gate 	    high = i;
220*0Sstevel@tonic-gate 	else
221*0Sstevel@tonic-gate 	    low = i;
222*0Sstevel@tonic-gate     }
223*0Sstevel@tonic-gate     if (errNum == errStrings[low].errNum)
224*0Sstevel@tonic-gate     	return errStrings[low].errString;
225*0Sstevel@tonic-gate     if (errNum == errStrings[high].errNum)
226*0Sstevel@tonic-gate     	return errStrings[high].errString;
227*0Sstevel@tonic-gate     return NULL;
228*0Sstevel@tonic-gate }
229*0Sstevel@tonic-gate #else /* _SOLARIS_SDK */
230*0Sstevel@tonic-gate #undef ER3
231*0Sstevel@tonic-gate #define	ER3(x, y, z) case (x):		\
232*0Sstevel@tonic-gate 			s = (z);	\
233*0Sstevel@tonic-gate 			break;
234*0Sstevel@tonic-gate #undef ER2
235*0Sstevel@tonic-gate #define	ER2(x, y) case (x):		\
236*0Sstevel@tonic-gate 			s = (y);	\
237*0Sstevel@tonic-gate 			break;
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate static mutex_t		err_mutex = DEFAULTMUTEX;
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate static const char *
getErrString(PRInt32 i,PRErrorCode errNum)242*0Sstevel@tonic-gate getErrString(PRInt32 i, PRErrorCode errNum)
243*0Sstevel@tonic-gate {
244*0Sstevel@tonic-gate 	char *s;
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	mutex_lock(&err_mutex);
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	if (errStrings[i].errString != NULL) {
249*0Sstevel@tonic-gate 		mutex_unlock(&err_mutex);
250*0Sstevel@tonic-gate 		return (errStrings[i].errString);
251*0Sstevel@tonic-gate 	}
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	switch (errNum) {
254*0Sstevel@tonic-gate #include "sslerrstrs.h"
255*0Sstevel@tonic-gate #include "secerrstrs.h"
256*0Sstevel@tonic-gate #include "prerrstrs.h"
257*0Sstevel@tonic-gate 		default:
258*0Sstevel@tonic-gate 			s = NULL;
259*0Sstevel@tonic-gate 			break;
260*0Sstevel@tonic-gate 	}
261*0Sstevel@tonic-gate 	errStrings[i].errString = s;
262*0Sstevel@tonic-gate 	mutex_unlock(&err_mutex);
263*0Sstevel@tonic-gate 	return (s);
264*0Sstevel@tonic-gate }
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate static
267*0Sstevel@tonic-gate const char *
SECU_Strerror(PRErrorCode errNum)268*0Sstevel@tonic-gate SECU_Strerror(PRErrorCode errNum) {
269*0Sstevel@tonic-gate     PRInt32 low  = 0;
270*0Sstevel@tonic-gate     PRInt32 high = numStrings - 1;
271*0Sstevel@tonic-gate     PRInt32 i;
272*0Sstevel@tonic-gate     PRErrorCode num;
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate     /* ASSUME table is in ascending order.
275*0Sstevel@tonic-gate      * binary search depends on it.
276*0Sstevel@tonic-gate      */
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate     /* Do binary search of table. */
279*0Sstevel@tonic-gate     while (low + 1 < high) {
280*0Sstevel@tonic-gate     	i = (low + high) / 2;
281*0Sstevel@tonic-gate 	num = errStrings[i].errNum;
282*0Sstevel@tonic-gate 	if (errNum == num)
283*0Sstevel@tonic-gate 	    return getErrString(i, errNum);
284*0Sstevel@tonic-gate         if (errNum < num)
285*0Sstevel@tonic-gate 	    high = i;
286*0Sstevel@tonic-gate 	else
287*0Sstevel@tonic-gate 	    low = i;
288*0Sstevel@tonic-gate     }
289*0Sstevel@tonic-gate     if (errNum == errStrings[low].errNum)
290*0Sstevel@tonic-gate     	return getErrString(low, errNum);
291*0Sstevel@tonic-gate     if (errNum == errStrings[high].errNum)
292*0Sstevel@tonic-gate     	return getErrString(high, errNum);
293*0Sstevel@tonic-gate     return NULL;
294*0Sstevel@tonic-gate }
295*0Sstevel@tonic-gate #endif
296