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