17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright (c) 2001 by Sun Microsystems, Inc.
37c478bd9Sstevel@tonic-gate * All rights reserved.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public
87c478bd9Sstevel@tonic-gate * License Version 1.1 (the "License"); you may not use this file
97c478bd9Sstevel@tonic-gate * except in compliance with the License. You may obtain a copy of
107c478bd9Sstevel@tonic-gate * the License at http://www.mozilla.org/NPL/
117c478bd9Sstevel@tonic-gate *
127c478bd9Sstevel@tonic-gate * Software distributed under the License is distributed on an "AS
137c478bd9Sstevel@tonic-gate * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
147c478bd9Sstevel@tonic-gate * implied. See the License for the specific language governing
157c478bd9Sstevel@tonic-gate * rights and limitations under the License.
167c478bd9Sstevel@tonic-gate *
177c478bd9Sstevel@tonic-gate * The Original Code is Mozilla Communicator client code, released
187c478bd9Sstevel@tonic-gate * March 31, 1998.
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * The Initial Developer of the Original Code is Netscape
217c478bd9Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are
227c478bd9Sstevel@tonic-gate * Copyright (C) 1998-1999 Netscape Communications Corporation. All
237c478bd9Sstevel@tonic-gate * Rights Reserved.
247c478bd9Sstevel@tonic-gate *
257c478bd9Sstevel@tonic-gate * Contributor(s):
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * errormap.c - map NSPR and OS errors to strings
307c478bd9Sstevel@tonic-gate *
317c478bd9Sstevel@tonic-gate * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF NETSCAPE COMMUNICATIONS
32*1da57d55SToomas Soome * CORPORATION
33*1da57d55SToomas Soome *
34*1da57d55SToomas Soome * Copyright (C) 1998-9 Netscape Communications Corporation. All Rights Reserved.
357c478bd9Sstevel@tonic-gate *
367c478bd9Sstevel@tonic-gate * Use of this Source Code is subject to the terms of the applicable license
37*1da57d55SToomas Soome * agreement from Netscape Communications Corporation.
387c478bd9Sstevel@tonic-gate *
397c478bd9Sstevel@tonic-gate * The copyright notice(s) in this Source Code does not indicate actual or
40*1da57d55SToomas Soome * intended publication of this Source Code.
417c478bd9Sstevel@tonic-gate */
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate /* XXX ceb
44*1da57d55SToomas Soome * This code was stolen from Directory server.
457c478bd9Sstevel@tonic-gate * ns/netsite/ldap/servers/slapd/errormap.c
467c478bd9Sstevel@tonic-gate * OS errors are not handled, so the os error has been removed.
477c478bd9Sstevel@tonic-gate */
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate #if defined( _WINDOWS )
517c478bd9Sstevel@tonic-gate #include <windows.h>
527c478bd9Sstevel@tonic-gate #include "proto-ntutil.h"
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #include <nspr.h>
567c478bd9Sstevel@tonic-gate #include <ssl.h>
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate #include <ldap.h>
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
617c478bd9Sstevel@tonic-gate #include <synch.h>
627c478bd9Sstevel@tonic-gate #include <libintl.h>
637c478bd9Sstevel@tonic-gate #endif /* _SOLARIS_SDK */
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate * function protoypes
687c478bd9Sstevel@tonic-gate */
697c478bd9Sstevel@tonic-gate static const char *SECU_Strerror(PRErrorCode errNum);
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate * return the string equivalent of an NSPR error
757c478bd9Sstevel@tonic-gate */
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate const char *
787c478bd9Sstevel@tonic-gate LDAP_CALL
ldapssl_err2string(const int prerrno)797c478bd9Sstevel@tonic-gate ldapssl_err2string( const int prerrno )
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate const char *s;
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate if (( s = SECU_Strerror( (PRErrorCode)prerrno )) == NULL ) {
847c478bd9Sstevel@tonic-gate s = dgettext(TEXT_DOMAIN, "unknown");
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate return( s );
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate ****************************************************************************
927c478bd9Sstevel@tonic-gate * The code below this point was provided by Nelson Bolyard <nelsonb> of the
937c478bd9Sstevel@tonic-gate * Netscape Certificate Server team on 27-March-1998.
947c478bd9Sstevel@tonic-gate * Taken from the file ns/security/cmd/lib/secerror.c on NSS_1_BRANCH.
957c478bd9Sstevel@tonic-gate * Last updated from there: 24-July-1998 by Mark Smith <mcs>
967c478bd9Sstevel@tonic-gate * Last updated from there: 14-July-1999 by chuck boatwright <cboatwri>
97*1da57d55SToomas Soome *
987c478bd9Sstevel@tonic-gate *
997c478bd9Sstevel@tonic-gate * All of the Directory Server specific changes are enclosed inside
1007c478bd9Sstevel@tonic-gate * #ifdef NS_DIRECTORY.
1017c478bd9Sstevel@tonic-gate ****************************************************************************
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate #include "nspr.h"
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate * XXXceb as a hack, we will locally define NS_DIRECTORY
1077c478bd9Sstevel@tonic-gate */
1087c478bd9Sstevel@tonic-gate #define NS_DIRECTORY 1
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate struct tuple_str {
1117c478bd9Sstevel@tonic-gate PRErrorCode errNum;
1127c478bd9Sstevel@tonic-gate const char * errString;
1137c478bd9Sstevel@tonic-gate };
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate typedef struct tuple_str tuple_str;
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate #ifndef _SOLARIS_SDK
1187c478bd9Sstevel@tonic-gate #define ER2(a,b) {a, b},
1197c478bd9Sstevel@tonic-gate #define ER3(a,b,c) {a, c},
1207c478bd9Sstevel@tonic-gate #else
1217c478bd9Sstevel@tonic-gate #define ER2(a,b) {a, NULL},
1227c478bd9Sstevel@tonic-gate #define ER3(a,b,c) {a, NULL},
1237c478bd9Sstevel@tonic-gate #endif
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate #include "secerr.h"
1267c478bd9Sstevel@tonic-gate #include "sslerr.h"
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate #ifndef _SOLARIS_SDK
1297c478bd9Sstevel@tonic-gate const tuple_str errStrings[] = {
1307c478bd9Sstevel@tonic-gate #else
1317c478bd9Sstevel@tonic-gate tuple_str errStrings[] = {
1327c478bd9Sstevel@tonic-gate #endif
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate /* keep this list in asceding order of error numbers */
1357c478bd9Sstevel@tonic-gate #ifdef NS_DIRECTORY
1367c478bd9Sstevel@tonic-gate #include "sslerrstrs.h"
1377c478bd9Sstevel@tonic-gate #include "secerrstrs.h"
1387c478bd9Sstevel@tonic-gate #include "prerrstrs.h"
139*1da57d55SToomas Soome /*
140*1da57d55SToomas Soome * XXXceb -- LDAPSDK won't care about disconnect
1417c478bd9Sstevel@tonic-gate #include "disconnect_error_strings.h"
1427c478bd9Sstevel@tonic-gate */
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate #else /* NS_DIRECTORY */
1457c478bd9Sstevel@tonic-gate #include "SSLerrs.h"
1467c478bd9Sstevel@tonic-gate #include "SECerrs.h"
1477c478bd9Sstevel@tonic-gate #include "NSPRerrs.h"
1487c478bd9Sstevel@tonic-gate #endif /* NS_DIRECTORY */
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate };
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate const PRInt32 numStrings = sizeof(errStrings) / sizeof(tuple_str);
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate /* Returns a UTF-8 encoded constant error string for "errNum".
1557c478bd9Sstevel@tonic-gate * Returns NULL of errNum is unknown.
1567c478bd9Sstevel@tonic-gate */
1577c478bd9Sstevel@tonic-gate #ifndef _SOLARIS_SDK
1587c478bd9Sstevel@tonic-gate #ifdef NS_DIRECTORY
1597c478bd9Sstevel@tonic-gate static
1607c478bd9Sstevel@tonic-gate #endif /* NS_DIRECTORY */
1617c478bd9Sstevel@tonic-gate const char *
SECU_Strerror(PRErrorCode errNum)1627c478bd9Sstevel@tonic-gate SECU_Strerror(PRErrorCode errNum) {
1637c478bd9Sstevel@tonic-gate PRInt32 low = 0;
1647c478bd9Sstevel@tonic-gate PRInt32 high = numStrings - 1;
1657c478bd9Sstevel@tonic-gate PRInt32 i;
1667c478bd9Sstevel@tonic-gate PRErrorCode num;
1677c478bd9Sstevel@tonic-gate static int initDone;
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate /* make sure table is in ascending order.
1707c478bd9Sstevel@tonic-gate * binary search depends on it.
1717c478bd9Sstevel@tonic-gate */
1727c478bd9Sstevel@tonic-gate if (!initDone) {
1737c478bd9Sstevel@tonic-gate PRErrorCode lastNum = 0x80000000;
1747c478bd9Sstevel@tonic-gate for (i = low; i <= high; ++i) {
1757c478bd9Sstevel@tonic-gate num = errStrings[i].errNum;
1767c478bd9Sstevel@tonic-gate if (num <= lastNum) {
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate * XXXceb
1807c478bd9Sstevel@tonic-gate * We aren't handling out of sequence errors.
1817c478bd9Sstevel@tonic-gate */
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate #if 0
1857c478bd9Sstevel@tonic-gate #ifdef NS_DIRECTORY
1867c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_ANY,
1877c478bd9Sstevel@tonic-gate "sequence error in error strings at item %d\n"
1887c478bd9Sstevel@tonic-gate "error %d (%s)\n",
1897c478bd9Sstevel@tonic-gate i, lastNum, errStrings[i-1].errString );
1907c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_ANY,
1917c478bd9Sstevel@tonic-gate "should come after \n"
1927c478bd9Sstevel@tonic-gate "error %d (%s)\n",
1937c478bd9Sstevel@tonic-gate num, errStrings[i].errString, 0 );
1947c478bd9Sstevel@tonic-gate #else /* NS_DIRECTORY */
195*1da57d55SToomas Soome fprintf(stderr,
1967c478bd9Sstevel@tonic-gate "sequence error in error strings at item %d\n"
1977c478bd9Sstevel@tonic-gate "error %d (%s)\n"
1987c478bd9Sstevel@tonic-gate "should come after \n"
1997c478bd9Sstevel@tonic-gate "error %d (%s)\n",
200*1da57d55SToomas Soome i, lastNum, errStrings[i-1].errString,
2017c478bd9Sstevel@tonic-gate num, errStrings[i].errString);
2027c478bd9Sstevel@tonic-gate #endif /* NS_DIRECTORY */
2037c478bd9Sstevel@tonic-gate #endif /* 0 */
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate lastNum = num;
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate initDone = 1;
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate /* Do binary search of table. */
2117c478bd9Sstevel@tonic-gate while (low + 1 < high) {
2127c478bd9Sstevel@tonic-gate i = (low + high) / 2;
2137c478bd9Sstevel@tonic-gate num = errStrings[i].errNum;
214*1da57d55SToomas Soome if (errNum == num)
2157c478bd9Sstevel@tonic-gate return errStrings[i].errString;
2167c478bd9Sstevel@tonic-gate if (errNum < num)
2177c478bd9Sstevel@tonic-gate high = i;
218*1da57d55SToomas Soome else
2197c478bd9Sstevel@tonic-gate low = i;
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate if (errNum == errStrings[low].errNum)
2227c478bd9Sstevel@tonic-gate return errStrings[low].errString;
2237c478bd9Sstevel@tonic-gate if (errNum == errStrings[high].errNum)
2247c478bd9Sstevel@tonic-gate return errStrings[high].errString;
2257c478bd9Sstevel@tonic-gate return NULL;
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate #else /* _SOLARIS_SDK */
2287c478bd9Sstevel@tonic-gate #undef ER3
2297c478bd9Sstevel@tonic-gate #define ER3(x, y, z) case (x): \
2307c478bd9Sstevel@tonic-gate s = (z); \
2317c478bd9Sstevel@tonic-gate break;
2327c478bd9Sstevel@tonic-gate #undef ER2
2337c478bd9Sstevel@tonic-gate #define ER2(x, y) case (x): \
2347c478bd9Sstevel@tonic-gate s = (y); \
2357c478bd9Sstevel@tonic-gate break;
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate static mutex_t err_mutex = DEFAULTMUTEX;
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate static const char *
getErrString(PRInt32 i,PRErrorCode errNum)2407c478bd9Sstevel@tonic-gate getErrString(PRInt32 i, PRErrorCode errNum)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate char *s;
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate mutex_lock(&err_mutex);
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate if (errStrings[i].errString != NULL) {
2477c478bd9Sstevel@tonic-gate mutex_unlock(&err_mutex);
2487c478bd9Sstevel@tonic-gate return (errStrings[i].errString);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate switch (errNum) {
2527c478bd9Sstevel@tonic-gate #include "sslerrstrs.h"
2537c478bd9Sstevel@tonic-gate #include "secerrstrs.h"
2547c478bd9Sstevel@tonic-gate #include "prerrstrs.h"
2557c478bd9Sstevel@tonic-gate default:
2567c478bd9Sstevel@tonic-gate s = NULL;
2577c478bd9Sstevel@tonic-gate break;
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate errStrings[i].errString = s;
2607c478bd9Sstevel@tonic-gate mutex_unlock(&err_mutex);
2617c478bd9Sstevel@tonic-gate return (s);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate static
2657c478bd9Sstevel@tonic-gate const char *
SECU_Strerror(PRErrorCode errNum)2667c478bd9Sstevel@tonic-gate SECU_Strerror(PRErrorCode errNum) {
2677c478bd9Sstevel@tonic-gate PRInt32 low = 0;
2687c478bd9Sstevel@tonic-gate PRInt32 high = numStrings - 1;
2697c478bd9Sstevel@tonic-gate PRInt32 i;
2707c478bd9Sstevel@tonic-gate PRErrorCode num;
2717c478bd9Sstevel@tonic-gate
2727c478bd9Sstevel@tonic-gate /* ASSUME table is in ascending order.
2737c478bd9Sstevel@tonic-gate * binary search depends on it.
2747c478bd9Sstevel@tonic-gate */
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate /* Do binary search of table. */
2777c478bd9Sstevel@tonic-gate while (low + 1 < high) {
2787c478bd9Sstevel@tonic-gate i = (low + high) / 2;
2797c478bd9Sstevel@tonic-gate num = errStrings[i].errNum;
280*1da57d55SToomas Soome if (errNum == num)
2817c478bd9Sstevel@tonic-gate return getErrString(i, errNum);
2827c478bd9Sstevel@tonic-gate if (errNum < num)
2837c478bd9Sstevel@tonic-gate high = i;
284*1da57d55SToomas Soome else
2857c478bd9Sstevel@tonic-gate low = i;
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate if (errNum == errStrings[low].errNum)
2887c478bd9Sstevel@tonic-gate return getErrString(low, errNum);
2897c478bd9Sstevel@tonic-gate if (errNum == errStrings[high].errNum)
2907c478bd9Sstevel@tonic-gate return getErrString(high, errNum);
2917c478bd9Sstevel@tonic-gate return NULL;
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate #endif
294