aclerror.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. * Description (aclerror.c)
  40. *
  41. * This module provides error-handling facilities for ACL-related
  42. * errors.
  43. */
  44. #include "base/systems.h"
  45. #include "public/nsapi.h"
  46. #ifdef NSPR20
  47. #include "prprf.h"
  48. #else
  49. #include "nspr/prprf.h"
  50. #endif
  51. #include "prlog.h"
  52. #include "libaccess/nserror.h"
  53. #include "libaccess/nsautherr.h"
  54. #include "libaccess/aclerror.h"
  55. #include <libaccess/dbtlibaccess.h>
  56. #include <libaccess/aclerror.h>
  57. #define aclerrnomem XP_GetAdminStr(DBT_AclerrfmtAclerrnomem)
  58. #define aclerropen XP_GetAdminStr(DBT_AclerrfmtAclerropen)
  59. #define aclerrdupsym1 XP_GetAdminStr(DBT_AclerrfmtAclerrdupsym1)
  60. #define aclerrdupsym3 XP_GetAdminStr(DBT_AclerrfmtAclerrdupsym3)
  61. #define aclerrsyntax XP_GetAdminStr(DBT_AclerrfmtAclerrsyntax)
  62. #define aclerrundef XP_GetAdminStr(DBT_AclerrfmtAclerrundef)
  63. #define aclaclundef XP_GetAdminStr(DBT_AclerrfmtAclaclundef)
  64. #define aclerradb XP_GetAdminStr(DBT_AclerrfmtAclerradb)
  65. #define aclerrparse1 XP_GetAdminStr(DBT_AclerrfmtAclerrparse1)
  66. #define aclerrparse2 XP_GetAdminStr(DBT_AclerrfmtAclerrparse2)
  67. #define aclerrparse3 XP_GetAdminStr(DBT_AclerrfmtAclerrparse3)
  68. #define aclerrnorlm XP_GetAdminStr(DBT_AclerrfmtAclerrnorlm)
  69. #define unknownerr XP_GetAdminStr(DBT_AclerrfmtUnknownerr)
  70. #define aclerrinternal XP_GetAdminStr(DBT_AclerrfmtAclerrinternal)
  71. #define aclerrinval XP_GetAdminStr(DBT_AclerrfmtAclerrinval)
  72. #define aclerrfail XP_GetAdminStr(DBT_AclerrfmtAclerrfail)
  73. #define aclerrio XP_GetAdminStr(DBT_AclerrfmtAclerrio)
  74. char * NSAuth_Program = "NSAUTH";
  75. char * ACL_Program = "NSACL"; /* ACL facility name */
  76. /*
  77. * Description (aclErrorFmt)
  78. *
  79. * This function formats an ACL error message into a buffer provided
  80. * by the caller. The ACL error information is passed in an error
  81. * list structure. The caller can indicate how many error frames
  82. * should be processed. A newline is inserted between messages for
  83. * different error frames. The error frames on the error list are
  84. * all freed, regardless of the maximum depth for traceback.
  85. *
  86. * Arguments:
  87. *
  88. * errp - error frame list pointer
  89. * msgbuf - pointer to error message buffer
  90. * maxlen - maximum length of generated message
  91. * maxdepth - maximum depth for traceback
  92. */
  93. void aclErrorFmt(NSErr_t * errp, char * msgbuf, int maxlen, int maxdepth)
  94. {
  95. NSEFrame_t * efp; /* error frame pointer */
  96. int len; /* length of error message text */
  97. int depth = 0; /* current depth */
  98. msgbuf[0] = 0;
  99. while ((efp = errp->err_first) != 0) {
  100. /* Stop if the message buffer is full */
  101. if (maxlen <= 0) break;
  102. if (depth > 0) {
  103. /* Put a newline & tab between error frame messages */
  104. *msgbuf++ = '\n';
  105. if (--maxlen <= 0) break;
  106. *msgbuf++ = '\t';
  107. if (--maxlen <= 0) break;
  108. }
  109. if (!strcmp(efp->ef_program, ACL_Program)) {
  110. /* Identify the facility generating the error and the id number */
  111. len = PR_snprintf(msgbuf, maxlen,
  112. "[%s%d] ", efp->ef_program, efp->ef_errorid);
  113. msgbuf += len;
  114. maxlen -= len;
  115. if (maxlen <= 0) break;
  116. len = 0;
  117. switch (efp->ef_retcode) {
  118. case ACLERRFAIL:
  119. case ACLERRNOMEM:
  120. case ACLERRINTERNAL:
  121. case ACLERRINVAL:
  122. switch (efp->ef_errc) {
  123. case 3:
  124. PR_snprintf(msgbuf, maxlen, efp->ef_errv[0], efp->ef_errv[1], efp->ef_errv[2]);
  125. break;
  126. case 2:
  127. PR_snprintf(msgbuf, maxlen, efp->ef_errv[0], efp->ef_errv[1]);
  128. break;
  129. case 1:
  130. strncpy(msgbuf, efp->ef_errv[0], maxlen);
  131. break;
  132. default:
  133. PR_ASSERT(0); /* don't break -- continue into case 0 */
  134. case 0:
  135. switch (efp->ef_retcode) {
  136. case ACLERRFAIL:
  137. strncpy(msgbuf, XP_GetAdminStr(DBT_AclerrfmtAclerrfail), maxlen);
  138. break;
  139. case ACLERRNOMEM:
  140. strncpy(msgbuf, aclerrnomem, maxlen);
  141. break;
  142. case ACLERRINTERNAL:
  143. strncpy(msgbuf, aclerrinternal, maxlen);
  144. break;
  145. case ACLERRINVAL:
  146. strncpy(msgbuf, aclerrinval, maxlen);
  147. break;
  148. }
  149. break;
  150. }
  151. msgbuf[maxlen-1] = '\0';
  152. len = strlen(msgbuf);
  153. break;
  154. case ACLERROPEN:
  155. /* File open error: filename, system_errmsg */
  156. if (efp->ef_errc == 2) {
  157. len = PR_snprintf(msgbuf, maxlen, aclerropen,
  158. efp->ef_errv[0], efp->ef_errv[1]);
  159. }
  160. break;
  161. case ACLERRDUPSYM:
  162. /* Duplicate symbol */
  163. if (efp->ef_errc == 1) {
  164. /* Duplicate symbol: filename, line#, symbol */
  165. len = PR_snprintf(msgbuf, maxlen, aclerrdupsym1,
  166. efp->ef_errv[0]);
  167. }
  168. else if (efp->ef_errc == 3) {
  169. /* Duplicate symbol: symbol */
  170. len = PR_snprintf(msgbuf, maxlen, aclerrdupsym3,
  171. efp->ef_errv[0], efp->ef_errv[1],
  172. efp->ef_errv[2]);
  173. }
  174. break;
  175. case ACLERRSYNTAX:
  176. if (efp->ef_errc == 2) {
  177. /* Syntax error: filename, line# */
  178. len = PR_snprintf(msgbuf, maxlen, aclerrsyntax,
  179. efp->ef_errv[0], efp->ef_errv[1]);
  180. }
  181. break;
  182. case ACLERRUNDEF:
  183. if (efp->ef_errorid == ACLERR3800) {
  184. /* Undefined symbol: acl, method/database name */
  185. len = PR_snprintf(msgbuf, maxlen, aclaclundef,
  186. efp->ef_errv[0], efp->ef_errv[1],
  187. efp->ef_errv[2]);
  188. }
  189. else if (efp->ef_errc == 3) {
  190. /* Undefined symbol: filename, line#, symbol */
  191. len = PR_snprintf(msgbuf, maxlen, aclerrundef,
  192. efp->ef_errv[0], efp->ef_errv[1],
  193. efp->ef_errv[2]);
  194. }
  195. break;
  196. case ACLERRADB:
  197. if (efp->ef_errc == 2) {
  198. /* Authentication database error: DB name, symbol */
  199. len = PR_snprintf(msgbuf, maxlen, aclerradb,
  200. efp->ef_errv[0], efp->ef_errv[1]);
  201. }
  202. break;
  203. case ACLERRPARSE:
  204. if (efp->ef_errc == 2) {
  205. /* Parse error: filename, line# */
  206. len = PR_snprintf(msgbuf, maxlen, aclerrparse2,
  207. efp->ef_errv[0], efp->ef_errv[1]);
  208. }
  209. else if (efp->ef_errc == 3) {
  210. /* Parse error: filename, line#, token */
  211. len = PR_snprintf(msgbuf, maxlen, aclerrparse3,
  212. efp->ef_errv[0], efp->ef_errv[1],
  213. efp->ef_errv[2]);
  214. }
  215. else if (efp->ef_errc == 1) {
  216. /* Parse error: line or pointer */
  217. len = PR_snprintf(msgbuf, maxlen, aclerrparse1,
  218. efp->ef_errv[0]);
  219. }
  220. break;
  221. case ACLERRNORLM:
  222. if (efp->ef_errc == 1) {
  223. /* No realm: name */
  224. len = PR_snprintf(msgbuf, maxlen, aclerrnorlm,
  225. efp->ef_errv[0]);
  226. }
  227. break;
  228. case ACLERRIO:
  229. if (efp->ef_errc == 2) {
  230. len = PR_snprintf(msgbuf, maxlen, aclerrio,
  231. efp->ef_errv[0], efp->ef_errv[1]);
  232. }
  233. break;
  234. default:
  235. len = PR_snprintf(msgbuf, maxlen, unknownerr, efp->ef_retcode);
  236. break;
  237. }
  238. }
  239. else if (!strcmp(efp->ef_program, NSAuth_Program)) {
  240. nsadbErrorFmt(errp, msgbuf, maxlen, maxdepth - depth);
  241. }
  242. else {
  243. len = PR_snprintf(msgbuf, maxlen, unknownerr, efp->ef_retcode);
  244. }
  245. msgbuf += len;
  246. maxlen -= len;
  247. /* Free this frame */
  248. nserrFFree(errp, efp);
  249. if (++depth >= maxdepth) break;
  250. }
  251. /* Free any remaining error frames */
  252. nserrDispose(errp);
  253. }