1 /* seterror.c - sasl_seterror split out because glue libraries
2 * can't pass varargs lists
5 * split from common.c by Rolf Braun
6 * $Id: seterror.c,v 1.9 2003/12/12 17:30:18 rjs3 Exp $
10 * Copyright (c) 1998-2003 Carnegie Mellon University. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
24 * 3. The name "Carnegie Mellon University" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For permission or any other legal
27 * details, please contact
28 * Office of Technology Transfer
29 * Carnegie Mellon University
31 * Pittsburgh, PA 15213-3890
32 * (412) 268-4387, fax: (412) 268-7395
33 * tech-transfer@andrew.cmu.edu
35 * 4. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by Computing Services
38 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
40 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
41 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
42 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
43 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
44 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
45 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
46 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
66 /* need to handle the fact that errno has been defined as a function
67 in a dll, not an extern int */
76 /* this is apparently no longer a user function */
77 static int _sasl_seterror_usererr(int saslerr)
79 /* Hide the difference in a username failure and a password failure */
80 if (saslerr == SASL_NOUSER)
83 /* otherwise return the error given; no transform necessary */
87 /* set the error string which will be returned by sasl_errdetail() using
88 * syslog()-style formatting (e.g. printf-style with %m as the string form
91 * primarily for use by server callbacks such as the sasl_authorize_t
92 * callback and internally to plug-ins
94 * This will also trigger a call to the SASL logging callback (if any)
95 * with a level of SASL_LOG_FAIL unless the SASL_NOLOG flag is set.
97 * Messages should be sensitive to the current language setting. If there
98 * is no SASL_CB_LANGUAGE callback messages MUST be US-ASCII otherwise UTF-8
99 * is used and use of RFC 2482 for mixed-language text is encouraged.
101 * if conn is NULL, function does nothing
103 void sasl_seterror(sasl_conn_t *conn,
105 const char *fmt, ...)
107 size_t outlen=0; /* current length of output buffer */
108 size_t pos = 0; /* current position in format string */
111 sasl_log_t *log_cb = NULL;
115 va_list ap; /* varargs thing */
117 size_t *error_buf_len;
120 #ifndef SASL_OSX_CFMGLUE
121 if(!(flags & SASL_NOLOG)) {
122 /* See if we have a logging callback... */
123 result = _sasl_getcallback(NULL, SASL_CB_LOG, &log_cb, &log_ctx);
124 if (result == SASL_OK && ! log_cb)
126 if (result != SASL_OK)
129 log_cb(log_ctx, SASL_LOG_FAIL,
130 "No sasl_conn_t passed to sasl_seterror");
132 #endif /* SASL_OSX_CFMGLUE */
134 } else if(!fmt) return;
136 /* we need to use a back end function to get the buffer because the
137 cfm glue can't be rooting around in the internal structs */
138 _sasl_get_errorbuf(conn, &error_buf, &error_buf_len);
140 formatlen = strlen(fmt);
142 va_start(ap, fmt); /* start varargs */
146 if (fmt[pos]!='%') /* regular character */
148 result = _buf_alloc(error_buf, error_buf_len, outlen+1);
149 if (result != SASL_OK)
151 (*error_buf)[outlen]=fmt[pos];
154 } else { /* formating thing */
166 case 's': /* need to handle this */
167 cval = va_arg(ap, char *); /* get the next arg */
168 result = _sasl_add_string(error_buf, error_buf_len,
171 if (result != SASL_OK) /* add the string */
177 case '%': /* double % output the '%' character */
178 result = _buf_alloc(error_buf, error_buf_len, outlen+1);
179 if (result != SASL_OK)
181 (*error_buf)[outlen]='%';
186 case 'm': /* insert the errno string */
187 result = _sasl_add_string(error_buf, error_buf_len,
189 strerror(va_arg(ap, int)));
190 if (result != SASL_OK)
195 case 'z': /* insert the sasl error string */
196 result = _sasl_add_string(error_buf, error_buf_len, &outlen,
197 (char *)sasl_errstring(_sasl_seterror_usererr(
198 va_arg(ap, int)),NULL,NULL));
199 if (result != SASL_OK)
205 frmt[frmtpos++]=fmt[pos];
207 tempbuf[0] = (char) va_arg(ap, int); /* get the next arg */
210 /* now add the character */
211 result = _sasl_add_string(error_buf, error_buf_len,
213 if (result != SASL_OK)
220 frmt[frmtpos++]=fmt[pos];
222 ival = va_arg(ap, int); /* get the next arg */
224 snprintf(tempbuf,20,frmt,ival); /* have snprintf do the work */
225 /* now add the string */
226 result = _sasl_add_string(error_buf, error_buf_len,
228 if (result != SASL_OK)
234 frmt[frmtpos++]=fmt[pos]; /* add to the formating */
247 (*error_buf)[outlen]='\0'; /* put 0 at end */
251 #ifndef SASL_OSX_CFMGLUE
252 if(!(flags & SASL_NOLOG)) {
253 /* See if we have a logging callback... */
254 result = _sasl_getcallback(conn, SASL_CB_LOG, &log_cb, &log_ctx);
255 if (result == SASL_OK && ! log_cb)
257 if (result != SASL_OK)
260 result = log_cb(log_ctx, SASL_LOG_FAIL, conn->error_buf);
262 #endif /* SASL_OSX_CFMGLUE */