]> eyrie.org Git - kerberos/krb5-strength.git/blob - plugin/error.c
Merge pull request #4 from dariaphoebe/main
[kerberos/krb5-strength.git] / plugin / error.c
1 /*
2  * Store errors in the Kerberos context.
3  *
4  * Provides helper functions for the rest of the plugin code to store an error
5  * message in the Kerberos context.
6  *
7  * Written by Russ Allbery <eagle@eyrie.org>
8  * Copyright 2016 Russ Allbery <eagle@eyrie.org>
9  * Copyright 2013
10  *     The Board of Trustees of the Leland Stanford Junior University
11  *
12  * SPDX-License-Identifier: MIT
13  */
14
15 #include <config.h>
16 #include <portable/kadmin.h>
17 #include <portable/krb5.h>
18 #include <portable/system.h>
19
20 #include <errno.h>
21
22 #include <plugin/internal.h>
23
24
25 /*
26  * Internal helper function to set the Kerberos error message given a format,
27  * an error code, and a variable argument structure.
28  */
29 static void __attribute__((__format__(printf, 3, 0)))
30 set_error(krb5_context ctx, krb5_error_code code, const char *format,
31           va_list args)
32 {
33     char *message;
34
35     if (vasprintf(&message, format, args) < 0) {
36         strength_error_system(ctx, "cannot allocate memory");
37         return;
38     }
39     krb5_set_error_message(ctx, code, "%s", message);
40     free(message);
41 }
42
43
44 /*
45  * The following functions handle various common error codes for failed
46  * password quality checks.  They allow the code to be simpler and not embed
47  * lots of long Kerberos error code defines.
48  *
49  * Each function has the same basic form: take a Kerberos context, a format,
50  * and variable arguments and set the Kerberos error code and message,
51  * returning the appropriate code.
52  */
53 #define ERROR_FUNC(name, code)                                     \
54     krb5_error_code strength_error_##name(krb5_context ctx,        \
55                                           const char *format, ...) \
56     {                                                              \
57         va_list args;                                              \
58         va_start(args, format);                                    \
59         set_error(ctx, code, format, args);                        \
60         va_end(args);                                              \
61         return code;                                               \
62     }
63 ERROR_FUNC(class, KADM5_PASS_Q_CLASS)
64 ERROR_FUNC(config, KADM5_MISSING_KRB5_CONF_PARAMS)
65 ERROR_FUNC(dict, KADM5_PASS_Q_DICT)
66 ERROR_FUNC(generic, KADM5_PASS_Q_GENERIC)
67 ERROR_FUNC(tooshort, KADM5_PASS_Q_TOOSHORT)
68
69
70 /*
71  * Set the Kerberos error code to the current errno and the message to the
72  * format and arguments passed to this function.
73  */
74 krb5_error_code
75 strength_error_system(krb5_context ctx, const char *format, ...)
76 {
77     va_list args;
78     char *message;
79     bool okay = true;
80     int oerrno = errno;
81
82     va_start(args, format);
83     if (vasprintf(&message, format, args) < 0) {
84         oerrno = errno;
85         krb5_set_error_message(ctx, errno, "cannot allocate memory: %s",
86                                strerror(errno));
87         okay = false;
88     }
89     va_end(args);
90     if (!okay)
91         return oerrno;
92     krb5_set_error_message(ctx, oerrno, "%s: %s", message, strerror(oerrno));
93     free(message);
94     return oerrno;
95 }