errormap.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. /*
  39. * errormap.c - map NSPR and OS errors to strings
  40. *
  41. */
  42. #include "slap.h"
  43. #ifndef SYSERRLIST_IN_STDIO
  44. extern int sys_nerr;
  45. extern char *sys_errlist[];
  46. #endif
  47. /*
  48. * function protoypes
  49. */
  50. static const char *SECU_Strerror(PRErrorCode errNum);
  51. /*
  52. * return the string equivalent of an NSPR error
  53. */
  54. char *
  55. slapd_pr_strerror( const int prerrno )
  56. {
  57. char *s;
  58. if (( s = (char *)SECU_Strerror( (PRErrorCode)prerrno )) == NULL ) {
  59. s = "unknown";
  60. }
  61. return( s );
  62. }
  63. /*
  64. * return the string equivalent of a system error
  65. */
  66. const char *
  67. slapd_system_strerror( const int syserrno )
  68. {
  69. const char *s;
  70. /* replaced
  71. if ( syserrno > -1 && syserrno < sys_nerr ) {
  72. s = sys_errlist[ syserrno ];
  73. } else {
  74. s = "unknown";
  75. }
  76. with s= strerror(syserrno)*/
  77. s=strerror(syserrno);
  78. return( s );
  79. }
  80. /*
  81. * return the string equivalent of an NSPR error. If "prerrno" is not
  82. * an NSPR error, assume it is a system error. Please use slapd_pr_strerror()
  83. * or slapd_system_strerror() if you can since the concept behind this
  84. * function is a bit of a kludge -- one should *really* know what kind of
  85. * error code they have.
  86. */
  87. const char *
  88. slapd_versatile_strerror( const PRErrorCode prerrno )
  89. {
  90. const char *s;
  91. if (( s = (const char *)SECU_Strerror( prerrno )) == NULL ) {
  92. s = slapd_system_strerror( (const int)prerrno );
  93. }
  94. return( s );
  95. }
  96. /*
  97. ****************************************************************************
  98. * The code below this point was provided by Nelson Bolyard <nelsonb> of the
  99. * Netscape Certificate Server team on 27-March-1998.
  100. * Taken from the file ns/security/cmd/lib/secerror.c on NSS_1_BRANCH.
  101. * Last updated from there: 24-July-1998 by Mark Smith <mcs>
  102. *
  103. * All of the Directory Server specific changes are enclosed inside
  104. * #ifdef NS_DS.
  105. ****************************************************************************
  106. */
  107. #include "nspr.h"
  108. struct tuple_str {
  109. PRErrorCode errNum;
  110. const char * errString;
  111. };
  112. typedef struct tuple_str tuple_str;
  113. #define ER2(a,b) {a, b},
  114. #define ER3(a,b,c) {a, c},
  115. #include "secerr.h"
  116. #include "sslerr.h"
  117. static const tuple_str errStrings[] = {
  118. /* keep this list in ascending order of error numbers */
  119. #ifdef NS_DS
  120. #include "dberrstrs.h"
  121. #include "sslerrstrs.h"
  122. #include "secerrstrs.h"
  123. #include "prerrstrs.h"
  124. #include "disconnect_error_strings.h"
  125. #else /* NS_DS */
  126. #include "SSLerrs.h"
  127. #include "SECerrs.h"
  128. #include "NSPRerrs.h"
  129. #endif /* NS_DS */
  130. };
  131. static const PRInt32 numStrings = sizeof(errStrings) / sizeof(tuple_str);
  132. /* Returns a UTF-8 encoded constant error string for "errNum".
  133. * Returns NULL of errNum is unknown.
  134. */
  135. #ifdef NS_DS
  136. static
  137. #endif /* NS_DS */
  138. const char *
  139. SECU_Strerror(PRErrorCode errNum) {
  140. PRInt32 low = 0;
  141. PRInt32 high = numStrings - 1;
  142. PRInt32 i;
  143. PRErrorCode num;
  144. static int initDone;
  145. /* make sure table is in ascending order.
  146. * binary search depends on it.
  147. */
  148. if (!initDone) {
  149. PRErrorCode lastNum = errStrings[low].errNum;
  150. for (i = low + 1; i <= high; ++i) {
  151. num = errStrings[i].errNum;
  152. if (num <= lastNum) {
  153. #ifdef NS_DS
  154. LDAPDebug( LDAP_DEBUG_ANY,
  155. "sequence error in error strings at item %d\n"
  156. "error %d (%s)\n",
  157. i, lastNum, errStrings[i-1].errString );
  158. LDAPDebug( LDAP_DEBUG_ANY,
  159. "should come after \n"
  160. "error %d (%s)\n",
  161. num, errStrings[i].errString, 0 );
  162. #else /* NS_DS */
  163. fprintf(stderr,
  164. "sequence error in error strings at item %d\n"
  165. "error %d (%s)\n"
  166. "should come after \n"
  167. "error %d (%s)\n",
  168. i, lastNum, errStrings[i-1].errString,
  169. num, errStrings[i].errString);
  170. #endif /* NS_DS */
  171. }
  172. lastNum = num;
  173. }
  174. initDone = 1;
  175. }
  176. /* Do binary search of table. */
  177. while (low + 1 < high) {
  178. i = (low + high) / 2;
  179. num = errStrings[i].errNum;
  180. if (errNum == num)
  181. return errStrings[i].errString;
  182. if (errNum < num)
  183. high = i;
  184. else
  185. low = i;
  186. }
  187. if (errNum == errStrings[low].errNum)
  188. return errStrings[low].errString;
  189. if (errNum == errStrings[high].errNum)
  190. return errStrings[high].errString;
  191. return NULL;
  192. }