]> eyrie.org Git - kerberos/krb5-strength.git/blob - util/messages-krb5.c
Add dependabot configuration
[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-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  * SPDX-License-Identifier: MIT
34  */
35
36 #include <config.h>
37 #include <portable/krb5.h>
38 #include <portable/system.h>
39
40 #include <util/messages-krb5.h>
41 #include <util/messages.h>
42 #include <util/xmalloc.h>
43
44
45 /*
46  * Report a Kerberos error and exit.
47  */
48 void
49 die_krb5(krb5_context ctx, krb5_error_code code, const char *format, ...)
50 {
51     const char *k5_msg = NULL;
52     char *message;
53     va_list args;
54
55     if (ctx != NULL)
56         k5_msg = krb5_get_error_message(ctx, code);
57     va_start(args, format);
58     xvasprintf(&message, format, args);
59     va_end(args);
60     if (k5_msg == NULL)
61         die("%s", message);
62     else
63         die("%s: %s", message, k5_msg);
64 }
65
66
67 /*
68  * Report a Kerberos error.
69  */
70 void
71 warn_krb5(krb5_context ctx, krb5_error_code code, const char *format, ...)
72 {
73     const char *k5_msg = NULL;
74     char *message;
75     va_list args;
76
77     if (ctx != NULL)
78         k5_msg = krb5_get_error_message(ctx, code);
79     va_start(args, format);
80     xvasprintf(&message, format, args);
81     va_end(args);
82     if (k5_msg == NULL)
83         warn("%s", message);
84     else
85         warn("%s: %s", message, k5_msg);
86     free(message);
87     if (k5_msg != NULL)
88         krb5_free_error_message(ctx, k5_msg);
89 }