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