seterror.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* seterror.c - sasl_seterror split out because glue libraries
  2. * can't pass varargs lists
  3. * Rob Siemborski
  4. * Tim Martin
  5. * split from common.c by Rolf Braun
  6. */
  7. /*
  8. * Copyright (c) 1998-2016 Carnegie Mellon University. All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * 3. The name "Carnegie Mellon University" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For permission or any other legal
  25. * details, please contact
  26. * Carnegie Mellon University
  27. * Center for Technology Transfer and Enterprise Creation
  28. * 4615 Forbes Avenue
  29. * Suite 302
  30. * Pittsburgh, PA 15213
  31. * (412) 268-7393, fax: (412) 268-7395
  32. * innovation@andrew.cmu.edu
  33. *
  34. * 4. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by Computing Services
  37. * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
  38. *
  39. * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
  40. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  41. * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
  42. * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  43. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  44. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  45. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  46. */
  47. #include <config.h>
  48. #include <stdio.h>
  49. #include <string.h>
  50. #include <stdlib.h>
  51. #include <limits.h>
  52. #ifdef HAVE_SYSLOG
  53. #include <syslog.h>
  54. #endif
  55. #include <stdarg.h>
  56. #include <ctype.h>
  57. #include <sasl.h>
  58. #include <saslutil.h>
  59. #include <saslplug.h>
  60. #include "saslint.h"
  61. #ifdef WIN32
  62. /* need to handle the fact that errno has been defined as a function
  63. in a dll, not an extern int */
  64. # ifdef errno
  65. # undef errno
  66. # endif /* errno */
  67. #endif /* WIN32 */
  68. #ifdef HAVE_UNISTD_H
  69. #include <unistd.h>
  70. #endif
  71. /* this is apparently no longer a user function */
  72. static int _sasl_seterror_usererr(int saslerr)
  73. {
  74. /* Hide the difference in a username failure and a password failure */
  75. if (saslerr == SASL_NOUSER)
  76. return SASL_BADAUTH;
  77. /* otherwise return the error given; no transform necessary */
  78. return saslerr;
  79. }
  80. /* set the error string which will be returned by sasl_errdetail() using
  81. * syslog()-style formatting (e.g. printf-style with %m as the string form
  82. * of an errno error)
  83. *
  84. * primarily for use by server callbacks such as the sasl_authorize_t
  85. * callback and internally to plug-ins
  86. *
  87. * This will also trigger a call to the SASL logging callback (if any)
  88. * with a level of SASL_LOG_FAIL unless the SASL_NOLOG flag is set.
  89. *
  90. * Messages should be sensitive to the current language setting. If there
  91. * is no SASL_CB_LANGUAGE callback messages MUST be US-ASCII otherwise UTF-8
  92. * is used and use of RFC 2482 for mixed-language text is encouraged.
  93. *
  94. * if conn is NULL, function does nothing
  95. */
  96. void sasl_seterror(sasl_conn_t *conn,
  97. unsigned flags,
  98. const char *fmt, ...)
  99. {
  100. size_t outlen=0; /* current length of output buffer */
  101. size_t pos = 0; /* current position in format string */
  102. size_t formatlen;
  103. int result;
  104. sasl_log_t *log_cb = NULL;
  105. void *log_ctx;
  106. int ival;
  107. char *cval;
  108. va_list ap; /* varargs thing */
  109. char **error_buf;
  110. size_t *error_buf_len;
  111. if(!conn) {
  112. #ifndef SASL_OSX_CFMGLUE
  113. if(!(flags & SASL_NOLOG)) {
  114. /* See if we have a logging callback... */
  115. result = _sasl_getcallback(NULL, SASL_CB_LOG, (sasl_callback_ft *)&log_cb, &log_ctx);
  116. if (result == SASL_OK && ! log_cb)
  117. result = SASL_FAIL;
  118. if (result != SASL_OK)
  119. return;
  120. log_cb(log_ctx, SASL_LOG_FAIL,
  121. "No sasl_conn_t passed to sasl_seterror");
  122. }
  123. #endif /* SASL_OSX_CFMGLUE */
  124. return;
  125. } else if(!fmt) return;
  126. /* we need to use a back end function to get the buffer because the
  127. cfm glue can't be rooting around in the internal structs */
  128. _sasl_get_errorbuf(conn, &error_buf, &error_buf_len);
  129. formatlen = strlen(fmt);
  130. va_start(ap, fmt); /* start varargs */
  131. while(pos<formatlen)
  132. {
  133. if (fmt[pos]!='%') /* regular character */
  134. {
  135. result = _buf_alloc(error_buf, error_buf_len, outlen+1);
  136. if (result != SASL_OK)
  137. goto done;
  138. (*error_buf)[outlen]=fmt[pos];
  139. outlen++;
  140. pos++;
  141. } else { /* formating thing */
  142. int done=0;
  143. char frmt[10];
  144. int frmtpos=1;
  145. char tempbuf[21];
  146. frmt[0]='%';
  147. pos++;
  148. while (done==0)
  149. {
  150. switch(fmt[pos])
  151. {
  152. case 's': /* need to handle this */
  153. cval = va_arg(ap, char *); /* get the next arg */
  154. result = _sasl_add_string(error_buf, error_buf_len,
  155. &outlen, cval);
  156. if (result != SASL_OK) /* add the string */
  157. goto done;
  158. done=1;
  159. break;
  160. case '%': /* double % output the '%' character */
  161. result = _buf_alloc(error_buf, error_buf_len, outlen+1);
  162. if (result != SASL_OK)
  163. goto done;
  164. (*error_buf)[outlen]='%';
  165. outlen++;
  166. done=1;
  167. break;
  168. case 'm': /* insert the errno string */
  169. result = _sasl_add_string(error_buf, error_buf_len,
  170. &outlen,
  171. strerror(va_arg(ap, int)));
  172. if (result != SASL_OK)
  173. goto done;
  174. done=1;
  175. break;
  176. case 'z': /* insert the sasl error string */
  177. result = _sasl_add_string(error_buf, error_buf_len, &outlen,
  178. (char *)sasl_errstring(_sasl_seterror_usererr(
  179. va_arg(ap, int)),NULL,NULL));
  180. if (result != SASL_OK)
  181. goto done;
  182. done=1;
  183. break;
  184. case 'c':
  185. frmt[frmtpos++]=fmt[pos];
  186. frmt[frmtpos]=0;
  187. tempbuf[0] = (char) va_arg(ap, int); /* get the next arg */
  188. tempbuf[1]='\0';
  189. /* now add the character */
  190. result = _sasl_add_string(error_buf, error_buf_len,
  191. &outlen, tempbuf);
  192. if (result != SASL_OK)
  193. goto done;
  194. done=1;
  195. break;
  196. case 'd':
  197. case 'i':
  198. frmt[frmtpos++]=fmt[pos];
  199. frmt[frmtpos]=0;
  200. ival = va_arg(ap, int); /* get the next arg */
  201. snprintf(tempbuf,20,frmt,ival); /* have snprintf do the work */
  202. /* now add the string */
  203. result = _sasl_add_string(error_buf, error_buf_len,
  204. &outlen, tempbuf);
  205. if (result != SASL_OK)
  206. goto done;
  207. done=1;
  208. break;
  209. default:
  210. frmt[frmtpos++]=fmt[pos]; /* add to the formating */
  211. frmt[frmtpos]=0;
  212. if (frmtpos>9)
  213. done=1;
  214. }
  215. pos++;
  216. if (pos>formatlen)
  217. done=1;
  218. }
  219. }
  220. }
  221. (*error_buf)[outlen]='\0'; /* put 0 at end */
  222. #ifndef SASL_OSX_CFMGLUE
  223. if(!(flags & SASL_NOLOG)) {
  224. /* See if we have a logging callback... */
  225. result = _sasl_getcallback(conn, SASL_CB_LOG, (sasl_callback_ft *)&log_cb, &log_ctx);
  226. if (result == SASL_OK && ! log_cb)
  227. result = SASL_FAIL;
  228. if (result != SASL_OK)
  229. goto done;
  230. result = log_cb(log_ctx, SASL_LOG_FAIL, conn->error_buf);
  231. }
  232. #endif /* SASL_OSX_CFMGLUE */
  233. done:
  234. va_end(ap);
  235. }