| 1 |
/* |
| 2 |
* Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the Institute nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this software |
| 19 |
* without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 22 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 25 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 |
* SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "krb5_locl.h" |
| 35 |
#include <err.h> |
| 36 |
|
| 37 |
static krb5_error_code _warnerr(krb5_context context, int do_errtext, |
| 38 |
krb5_error_code code, int level, const char *fmt, va_list ap) |
| 39 |
__attribute__ ((__format__ (__printf__, 5, 0))); |
| 40 |
|
| 41 |
static krb5_error_code |
| 42 |
_warnerr(krb5_context context, int do_errtext, |
| 43 |
krb5_error_code code, int level, const char *fmt, va_list ap) |
| 44 |
{ |
| 45 |
char xfmt[7] = ""; |
| 46 |
const char *args[2], **arg; |
| 47 |
char *msg = NULL; |
| 48 |
const char *err_str = NULL; |
| 49 |
krb5_error_code ret; |
| 50 |
|
| 51 |
args[0] = args[1] = NULL; |
| 52 |
arg = args; |
| 53 |
if(fmt){ |
| 54 |
strlcat(xfmt, "%s", sizeof(xfmt)); |
| 55 |
if(do_errtext) |
| 56 |
strlcat(xfmt, ": ", sizeof(xfmt)); |
| 57 |
ret = vasprintf(&msg, fmt, ap); |
| 58 |
if(ret < 0 || msg == NULL) |
| 59 |
return ENOMEM; |
| 60 |
*arg++ = msg; |
| 61 |
} |
| 62 |
if(context && do_errtext){ |
| 63 |
strlcat(xfmt, "%s", sizeof(xfmt)); |
| 64 |
|
| 65 |
err_str = krb5_get_error_message(context, code); |
| 66 |
if (err_str != NULL) { |
| 67 |
*arg = err_str; |
| 68 |
} else { |
| 69 |
*arg= "<unknown error>"; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
if(context && context->warn_dest) |
| 74 |
krb5_log(context, context->warn_dest, level, xfmt, args[0], args[1]); |
| 75 |
else |
| 76 |
warnx(xfmt, args[0], args[1]); |
| 77 |
free(msg); |
| 78 |
krb5_free_error_message(context, err_str); |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
#define FUNC(ETEXT, CODE, LEVEL) \ |
| 83 |
krb5_error_code ret; \ |
| 84 |
va_list ap; \ |
| 85 |
va_start(ap, fmt); \ |
| 86 |
ret = _warnerr(context, ETEXT, CODE, LEVEL, fmt, ap); \ |
| 87 |
va_end(ap); |
| 88 |
|
| 89 |
#define FUNC_NORET(ETEXT, CODE, LEVEL) \ |
| 90 |
va_list ap; \ |
| 91 |
va_start(ap, fmt); \ |
| 92 |
(void) _warnerr(context, ETEXT, CODE, LEVEL, fmt, ap); \ |
| 93 |
va_end(ap); |
| 94 |
|
| 95 |
#undef __attribute__ |
| 96 |
#define __attribute__(X) |
| 97 |
|
| 98 |
/** |
| 99 |
* Log a warning to the log, default stderr, include the error from |
| 100 |
* the last failure. |
| 101 |
* |
| 102 |
* @param context A Kerberos 5 context. |
| 103 |
* @param code error code of the last error |
| 104 |
* @param fmt message to print |
| 105 |
* @param ap arguments |
| 106 |
* |
| 107 |
* @ingroup krb5_error |
| 108 |
*/ |
| 109 |
|
| 110 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 111 |
krb5_vwarn(krb5_context context, krb5_error_code code, |
| 112 |
const char *fmt, va_list ap) |
| 113 |
__attribute__ ((__format__ (__printf__, 3, 0))) |
| 114 |
{ |
| 115 |
return _warnerr(context, 1, code, 1, fmt, ap); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Log a warning to the log, default stderr, include the error from |
| 120 |
* the last failure. |
| 121 |
* |
| 122 |
* @param context A Kerberos 5 context. |
| 123 |
* @param code error code of the last error |
| 124 |
* @param fmt message to print |
| 125 |
* |
| 126 |
* @ingroup krb5_error |
| 127 |
*/ |
| 128 |
|
| 129 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 130 |
krb5_warn(krb5_context context, krb5_error_code code, const char *fmt, ...) |
| 131 |
__attribute__ ((__format__ (__printf__, 3, 4))) |
| 132 |
{ |
| 133 |
FUNC(1, code, 1); |
| 134 |
return ret; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Log a warning to the log, default stderr. |
| 139 |
* |
| 140 |
* @param context A Kerberos 5 context. |
| 141 |
* @param fmt message to print |
| 142 |
* @param ap arguments |
| 143 |
* |
| 144 |
* @ingroup krb5_error |
| 145 |
*/ |
| 146 |
|
| 147 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 148 |
krb5_vwarnx(krb5_context context, const char *fmt, va_list ap) |
| 149 |
__attribute__ ((__format__ (__printf__, 2, 0))) |
| 150 |
{ |
| 151 |
return _warnerr(context, 0, 0, 1, fmt, ap); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Log a warning to the log, default stderr. |
| 156 |
* |
| 157 |
* @param context A Kerberos 5 context. |
| 158 |
* @param fmt message to print |
| 159 |
* |
| 160 |
* @ingroup krb5_error |
| 161 |
*/ |
| 162 |
|
| 163 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 164 |
krb5_warnx(krb5_context context, const char *fmt, ...) |
| 165 |
__attribute__ ((__format__ (__printf__, 2, 3))) |
| 166 |
{ |
| 167 |
FUNC(0, 0, 1); |
| 168 |
return ret; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Log a warning to the log, default stderr, include bthe error from |
| 173 |
* the last failure and then exit. |
| 174 |
* |
| 175 |
* @param context A Kerberos 5 context |
| 176 |
* @param eval the exit code to exit with |
| 177 |
* @param code error code of the last error |
| 178 |
* @param fmt message to print |
| 179 |
* @param ap arguments |
| 180 |
* |
| 181 |
* @ingroup krb5_error |
| 182 |
*/ |
| 183 |
|
| 184 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 185 |
krb5_verr(krb5_context context, int eval, krb5_error_code code, |
| 186 |
const char *fmt, va_list ap) |
| 187 |
__attribute__ ((__noreturn__, __format__ (__printf__, 4, 0))) |
| 188 |
{ |
| 189 |
_warnerr(context, 1, code, 0, fmt, ap); |
| 190 |
exit(eval); |
| 191 |
UNREACHABLE(return 0); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Log a warning to the log, default stderr, include bthe error from |
| 196 |
* the last failure and then exit. |
| 197 |
* |
| 198 |
* @param context A Kerberos 5 context |
| 199 |
* @param eval the exit code to exit with |
| 200 |
* @param code error code of the last error |
| 201 |
* @param fmt message to print |
| 202 |
* |
| 203 |
* @ingroup krb5_error |
| 204 |
*/ |
| 205 |
|
| 206 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 207 |
krb5_err(krb5_context context, int eval, krb5_error_code code, |
| 208 |
const char *fmt, ...) |
| 209 |
__attribute__ ((__noreturn__, __format__ (__printf__, 4, 5))) |
| 210 |
{ |
| 211 |
FUNC_NORET(1, code, 0); |
| 212 |
exit(eval); |
| 213 |
UNREACHABLE(return 0); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Log a warning to the log, default stderr, and then exit. |
| 218 |
* |
| 219 |
* @param context A Kerberos 5 context |
| 220 |
* @param eval the exit code to exit with |
| 221 |
* @param fmt message to print |
| 222 |
* @param ap arguments |
| 223 |
* |
| 224 |
* @ingroup krb5_error |
| 225 |
*/ |
| 226 |
|
| 227 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 228 |
krb5_verrx(krb5_context context, int eval, const char *fmt, va_list ap) |
| 229 |
__attribute__ ((__noreturn__, __format__ (__printf__, 3, 0))) |
| 230 |
{ |
| 231 |
_warnerr(context, 0, 0, 0, fmt, ap); |
| 232 |
exit(eval); |
| 233 |
UNREACHABLE(return 0); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Log a warning to the log, default stderr, and then exit. |
| 238 |
* |
| 239 |
* @param context A Kerberos 5 context |
| 240 |
* @param eval the exit code to exit with |
| 241 |
* @param fmt message to print |
| 242 |
* |
| 243 |
* @ingroup krb5_error |
| 244 |
*/ |
| 245 |
|
| 246 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 247 |
krb5_errx(krb5_context context, int eval, const char *fmt, ...) |
| 248 |
__attribute__ ((__noreturn__, __format__ (__printf__, 3, 4))) |
| 249 |
{ |
| 250 |
FUNC_NORET(0, 0, 0); |
| 251 |
exit(eval); |
| 252 |
UNREACHABLE(return 0); |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Log a warning to the log, default stderr, include bthe error from |
| 257 |
* the last failure and then abort. |
| 258 |
* |
| 259 |
* @param context A Kerberos 5 context |
| 260 |
* @param code error code of the last error |
| 261 |
* @param fmt message to print |
| 262 |
* @param ap arguments |
| 263 |
* |
| 264 |
* @ingroup krb5_error |
| 265 |
*/ |
| 266 |
|
| 267 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 268 |
krb5_vabort(krb5_context context, krb5_error_code code, |
| 269 |
const char *fmt, va_list ap) |
| 270 |
__attribute__ ((__noreturn__, __format__ (__printf__, 3, 0))) |
| 271 |
{ |
| 272 |
_warnerr(context, 1, code, 0, fmt, ap); |
| 273 |
abort(); |
| 274 |
UNREACHABLE(return 0); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Log a warning to the log, default stderr, include the error from |
| 279 |
* the last failure and then abort. |
| 280 |
* |
| 281 |
* @param context A Kerberos 5 context |
| 282 |
* @param code error code of the last error |
| 283 |
* @param fmt message to print |
| 284 |
* @param ... arguments for format string |
| 285 |
* |
| 286 |
* @ingroup krb5_error |
| 287 |
*/ |
| 288 |
|
| 289 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 290 |
krb5_abort(krb5_context context, krb5_error_code code, const char *fmt, ...) |
| 291 |
__attribute__ ((__noreturn__, __format__ (__printf__, 3, 4))) |
| 292 |
{ |
| 293 |
FUNC_NORET(1, code, 0); |
| 294 |
abort(); |
| 295 |
UNREACHABLE(return 0); |
| 296 |
} |
| 297 |
|
| 298 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 299 |
krb5_vabortx(krb5_context context, const char *fmt, va_list ap) |
| 300 |
__attribute__ ((__noreturn__, __format__ (__printf__, 2, 0))) |
| 301 |
{ |
| 302 |
_warnerr(context, 0, 0, 0, fmt, ap); |
| 303 |
abort(); |
| 304 |
UNREACHABLE(return 0); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Log a warning to the log, default stderr, and then abort. |
| 309 |
* |
| 310 |
* @param context A Kerberos 5 context |
| 311 |
* @param fmt printf format string of message to print |
| 312 |
* @param ... arguments for format string |
| 313 |
* |
| 314 |
* @ingroup krb5_error |
| 315 |
*/ |
| 316 |
|
| 317 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 318 |
krb5_abortx(krb5_context context, const char *fmt, ...) |
| 319 |
__attribute__ ((__noreturn__, __format__ (__printf__, 2, 3))) |
| 320 |
{ |
| 321 |
FUNC_NORET(0, 0, 0); |
| 322 |
abort(); |
| 323 |
UNREACHABLE(return 0); |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Set the default logging facility. |
| 328 |
* |
| 329 |
* @param context A Kerberos 5 context |
| 330 |
* @param fac Facility to use for logging. |
| 331 |
* |
| 332 |
* @ingroup krb5_error |
| 333 |
*/ |
| 334 |
|
| 335 |
KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL |
| 336 |
krb5_set_warn_dest(krb5_context context, krb5_log_facility *fac) |
| 337 |
{ |
| 338 |
context->warn_dest = fac; |
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Get the default logging facility. |
| 344 |
* |
| 345 |
* @param context A Kerberos 5 context |
| 346 |
* |
| 347 |
* @ingroup krb5_error |
| 348 |
*/ |
| 349 |
|
| 350 |
KRB5_LIB_FUNCTION krb5_log_facility * KRB5_LIB_CALL |
| 351 |
krb5_get_warn_dest(krb5_context context) |
| 352 |
{ |
| 353 |
return context->warn_dest; |
| 354 |
} |