]> eyrie.org Git - kerberos/krb5-strength.git/blob - util/messages-krb5.c
Change CrackLib tests for system CrackLib
[kerberos/krb5-strength.git] / util / messages-krb5.c
1 /*
2  * Error handling for Kerberos.
3  *
4  * Provides versions of die and warn that take a Kerberos context and a
5  * Kerberos error code and append the Kerberos error message to the provided
6  * formatted message.
7  *
8  * The canonical version of this file is maintained in the rra-c-util package,
9  * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
10  *
11  * Written by Russ Allbery <eagle@eyrie.org>
12  * Copyright 2006, 2007, 2008, 2009, 2010, 2013
13  *     The Board of Trustees of the Leland Stanford Junior University
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a
16  * copy of this software and associated documentation files (the "Software"),
17  * to deal in the Software without restriction, including without limitation
18  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19  * and/or sell copies of the Software, and to permit persons to whom the
20  * Software is furnished to do so, subject to the following conditions:
21  *
22  * The above copyright notice and this permission notice shall be included in
23  * all copies or substantial portions of the Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
28  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31  * DEALINGS IN THE SOFTWARE.
32  */
33
34 #include <config.h>
35 #include <portable/krb5.h>
36 #include <portable/system.h>
37
38 #include <util/messages.h>
39 #include <util/messages-krb5.h>
40 #include <util/xmalloc.h>
41
42
43 /*
44  * Report a Kerberos error and exit.
45  */
46 void
47 die_krb5(krb5_context ctx, krb5_error_code code, const char *format, ...)
48 {
49     const char *k5_msg = NULL;
50     char *message;
51     va_list args;
52
53     if (ctx != NULL)
54         k5_msg = krb5_get_error_message(ctx, code);
55     va_start(args, format);
56     xvasprintf(&message, format, args);
57     va_end(args);
58     if (k5_msg == NULL)
59         die("%s", message);
60     else
61         die("%s: %s", message, k5_msg);
62 }
63
64
65 /*
66  * Report a Kerberos error.
67  */
68 void
69 warn_krb5(krb5_context ctx, krb5_error_code code, const char *format, ...)
70 {
71     const char *k5_msg = NULL;
72     char *message;
73     va_list args;
74
75     if (ctx != NULL)
76         k5_msg = krb5_get_error_message(ctx, code);
77     va_start(args, format);
78     xvasprintf(&message, format, args);
79     va_end(args);
80     if (k5_msg == NULL)
81         warn("%s", message);
82     else
83         warn("%s: %s", message, k5_msg);
84     free(message);
85     if (k5_msg != NULL)
86         krb5_free_error_message(ctx, k5_msg);
87 }