xref: /freebsd/crypto/krb5/src/plugins/audit/simple/au_simple_main.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* plugins/audit/au_simple_main.c - Sample Audit plugin implementation */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright (C) 2013 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert  * are met:
10*7f2fe78bSCy Schubert  *
11*7f2fe78bSCy Schubert  * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert  *
14*7f2fe78bSCy Schubert  * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert  *   the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert  *   distribution.
18*7f2fe78bSCy Schubert  *
19*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert  * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert  */
32*7f2fe78bSCy Schubert 
33*7f2fe78bSCy Schubert /*
34*7f2fe78bSCy Schubert  * This is a demo implementation of Audit JSON-based module.
35*7f2fe78bSCy Schubert  * It utilizes MIT Kerberos <kdc_j_encode.h> routines for JSON processing and
36*7f2fe78bSCy Schubert  * the Fedora/Debian libaudit library for audit logs.
37*7f2fe78bSCy Schubert  */
38*7f2fe78bSCy Schubert 
39*7f2fe78bSCy Schubert #include <k5-int.h>
40*7f2fe78bSCy Schubert #include <krb5/audit_plugin.h>
41*7f2fe78bSCy Schubert #include <libaudit.h>
42*7f2fe78bSCy Schubert #include <kdc_j_encode.h>
43*7f2fe78bSCy Schubert 
44*7f2fe78bSCy Schubert krb5_error_code
45*7f2fe78bSCy Schubert audit_simple_initvt(krb5_context context, int maj_ver, int min_ver,
46*7f2fe78bSCy Schubert                     krb5_plugin_vtable vtable);
47*7f2fe78bSCy Schubert 
48*7f2fe78bSCy Schubert struct krb5_audit_moddata_st {
49*7f2fe78bSCy Schubert     int fd;
50*7f2fe78bSCy Schubert };
51*7f2fe78bSCy Schubert 
52*7f2fe78bSCy Schubert /* Open connection to the audit system. Returns 0 on success. */
53*7f2fe78bSCy Schubert static krb5_error_code
open_au(krb5_audit_moddata * auctx_out)54*7f2fe78bSCy Schubert open_au(krb5_audit_moddata *auctx_out)
55*7f2fe78bSCy Schubert {
56*7f2fe78bSCy Schubert     krb5_error_code ret;
57*7f2fe78bSCy Schubert     int fd = 0;
58*7f2fe78bSCy Schubert     krb5_audit_moddata auctx;
59*7f2fe78bSCy Schubert 
60*7f2fe78bSCy Schubert     auctx = k5calloc(1, sizeof(*auctx), &ret);
61*7f2fe78bSCy Schubert     if (ret)
62*7f2fe78bSCy Schubert         return ENOMEM;
63*7f2fe78bSCy Schubert     fd = audit_open();
64*7f2fe78bSCy Schubert     if (fd < 0)
65*7f2fe78bSCy Schubert         return KRB5_PLUGIN_NO_HANDLE; /* audit module is unavailable */
66*7f2fe78bSCy Schubert 
67*7f2fe78bSCy Schubert     auctx->fd = fd;
68*7f2fe78bSCy Schubert     *auctx_out = auctx;
69*7f2fe78bSCy Schubert 
70*7f2fe78bSCy Schubert     return 0;
71*7f2fe78bSCy Schubert }
72*7f2fe78bSCy Schubert 
73*7f2fe78bSCy Schubert /* Close connection to the audit system. Returns 0 on success. */
74*7f2fe78bSCy Schubert static krb5_error_code
close_au(krb5_audit_moddata auctx)75*7f2fe78bSCy Schubert close_au(krb5_audit_moddata auctx)
76*7f2fe78bSCy Schubert {
77*7f2fe78bSCy Schubert     int fd = auctx->fd;
78*7f2fe78bSCy Schubert 
79*7f2fe78bSCy Schubert     audit_close(fd);
80*7f2fe78bSCy Schubert     return 0;
81*7f2fe78bSCy Schubert }
82*7f2fe78bSCy Schubert 
83*7f2fe78bSCy Schubert /* Log KDC-start event. Returns 0 on success. */
84*7f2fe78bSCy Schubert static krb5_error_code
j_kdc_start(krb5_audit_moddata auctx,krb5_boolean ev_success)85*7f2fe78bSCy Schubert j_kdc_start(krb5_audit_moddata auctx, krb5_boolean ev_success)
86*7f2fe78bSCy Schubert {
87*7f2fe78bSCy Schubert     krb5_error_code ret = 0;
88*7f2fe78bSCy Schubert     int local_type = AUDIT_USER_START;
89*7f2fe78bSCy Schubert     int fd = auctx->fd;
90*7f2fe78bSCy Schubert     char *jout = NULL;
91*7f2fe78bSCy Schubert 
92*7f2fe78bSCy Schubert     if (fd < 0)
93*7f2fe78bSCy Schubert         return KRB5_PLUGIN_NO_HANDLE; /* audit module is unavailable */
94*7f2fe78bSCy Schubert 
95*7f2fe78bSCy Schubert     ret = kau_j_kdc_start(ev_success, &jout);
96*7f2fe78bSCy Schubert     if (ret)
97*7f2fe78bSCy Schubert         return ret;
98*7f2fe78bSCy Schubert     if (audit_log_user_message(fd, local_type, jout,
99*7f2fe78bSCy Schubert                                NULL, NULL, NULL, ev_success) <= 0)
100*7f2fe78bSCy Schubert         ret = EIO;
101*7f2fe78bSCy Schubert     free(jout);
102*7f2fe78bSCy Schubert     return ret;
103*7f2fe78bSCy Schubert }
104*7f2fe78bSCy Schubert 
105*7f2fe78bSCy Schubert /* Log KDC-stop event. Returns 0 on success. */
106*7f2fe78bSCy Schubert static krb5_error_code
j_kdc_stop(krb5_audit_moddata auctx,krb5_boolean ev_success)107*7f2fe78bSCy Schubert j_kdc_stop(krb5_audit_moddata auctx, krb5_boolean ev_success)
108*7f2fe78bSCy Schubert {
109*7f2fe78bSCy Schubert     krb5_error_code ret = 0;
110*7f2fe78bSCy Schubert     int local_type = AUDIT_USER_END;
111*7f2fe78bSCy Schubert     int fd = auctx->fd;
112*7f2fe78bSCy Schubert     char *jout = NULL;
113*7f2fe78bSCy Schubert 
114*7f2fe78bSCy Schubert     if (fd < 0)
115*7f2fe78bSCy Schubert         return KRB5_PLUGIN_NO_HANDLE; /* audit module is unavailable */
116*7f2fe78bSCy Schubert 
117*7f2fe78bSCy Schubert     ret = kau_j_kdc_stop(ev_success, &jout);
118*7f2fe78bSCy Schubert     if (ret)
119*7f2fe78bSCy Schubert         return ret;
120*7f2fe78bSCy Schubert     if (audit_log_user_message(fd, local_type, jout,
121*7f2fe78bSCy Schubert                                NULL, NULL, NULL, ev_success) <= 0)
122*7f2fe78bSCy Schubert         ret = EIO;
123*7f2fe78bSCy Schubert     free(jout);
124*7f2fe78bSCy Schubert     return ret;
125*7f2fe78bSCy Schubert }
126*7f2fe78bSCy Schubert 
127*7f2fe78bSCy Schubert /* Log AS_REQ event. Returns 0 on success */
128*7f2fe78bSCy Schubert static krb5_error_code
j_as_req(krb5_audit_moddata auctx,krb5_boolean ev_success,krb5_audit_state * state)129*7f2fe78bSCy Schubert j_as_req(krb5_audit_moddata auctx, krb5_boolean ev_success,
130*7f2fe78bSCy Schubert          krb5_audit_state *state)
131*7f2fe78bSCy Schubert {
132*7f2fe78bSCy Schubert     krb5_error_code ret = 0;
133*7f2fe78bSCy Schubert     int local_type = AUDIT_USER_AUTH;
134*7f2fe78bSCy Schubert     int fd = auctx->fd;
135*7f2fe78bSCy Schubert     char *jout = NULL;
136*7f2fe78bSCy Schubert 
137*7f2fe78bSCy Schubert     if (fd < 0)
138*7f2fe78bSCy Schubert         return KRB5_PLUGIN_NO_HANDLE; /* audit module is unavailable */
139*7f2fe78bSCy Schubert 
140*7f2fe78bSCy Schubert     ret = kau_j_as_req(ev_success, state, &jout);
141*7f2fe78bSCy Schubert     if (ret)
142*7f2fe78bSCy Schubert         return ret;
143*7f2fe78bSCy Schubert     if (audit_log_user_message(fd, local_type, jout,
144*7f2fe78bSCy Schubert                                NULL, NULL, NULL, ev_success) <= 0)
145*7f2fe78bSCy Schubert         ret = EIO;
146*7f2fe78bSCy Schubert     free(jout);
147*7f2fe78bSCy Schubert     return ret;
148*7f2fe78bSCy Schubert }
149*7f2fe78bSCy Schubert 
150*7f2fe78bSCy Schubert /* Log TGS_REQ event. Returns 0 on success */
151*7f2fe78bSCy Schubert static krb5_error_code
j_tgs_req(krb5_audit_moddata auctx,krb5_boolean ev_success,krb5_audit_state * state)152*7f2fe78bSCy Schubert j_tgs_req(krb5_audit_moddata auctx, krb5_boolean ev_success,
153*7f2fe78bSCy Schubert           krb5_audit_state *state)
154*7f2fe78bSCy Schubert {
155*7f2fe78bSCy Schubert     krb5_error_code ret = 0;
156*7f2fe78bSCy Schubert     int local_type = AUDIT_USER_AUTH;
157*7f2fe78bSCy Schubert     int fd = auctx->fd;
158*7f2fe78bSCy Schubert     char *jout = NULL;
159*7f2fe78bSCy Schubert 
160*7f2fe78bSCy Schubert     if (fd < 0)
161*7f2fe78bSCy Schubert         return KRB5_PLUGIN_NO_HANDLE; /* audit module is unavailable */
162*7f2fe78bSCy Schubert 
163*7f2fe78bSCy Schubert     ret = kau_j_tgs_req(ev_success, state, &jout);
164*7f2fe78bSCy Schubert     if (ret)
165*7f2fe78bSCy Schubert         return ret;
166*7f2fe78bSCy Schubert     if (audit_log_user_message(fd, local_type, jout,
167*7f2fe78bSCy Schubert                                NULL, NULL, NULL, ev_success) <= 0)
168*7f2fe78bSCy Schubert         ret = EIO;
169*7f2fe78bSCy Schubert     free(jout);
170*7f2fe78bSCy Schubert     return ret;
171*7f2fe78bSCy Schubert }
172*7f2fe78bSCy Schubert 
173*7f2fe78bSCy Schubert /* Log S4U2SELF event. Returns 0 on success */
174*7f2fe78bSCy Schubert static krb5_error_code
j_tgs_s4u2self(krb5_audit_moddata auctx,krb5_boolean ev_success,krb5_audit_state * state)175*7f2fe78bSCy Schubert j_tgs_s4u2self(krb5_audit_moddata auctx, krb5_boolean ev_success,
176*7f2fe78bSCy Schubert                krb5_audit_state *state)
177*7f2fe78bSCy Schubert {
178*7f2fe78bSCy Schubert     krb5_error_code ret = 0;
179*7f2fe78bSCy Schubert     int local_type = AUDIT_USER_AUTH;
180*7f2fe78bSCy Schubert     int fd = auctx->fd;
181*7f2fe78bSCy Schubert     char *jout = NULL;
182*7f2fe78bSCy Schubert 
183*7f2fe78bSCy Schubert     if (fd < 0)
184*7f2fe78bSCy Schubert         return KRB5_PLUGIN_NO_HANDLE; /* audit module is unavailable */
185*7f2fe78bSCy Schubert 
186*7f2fe78bSCy Schubert     ret = kau_j_tgs_s4u2self(ev_success, state, &jout);
187*7f2fe78bSCy Schubert     if (ret)
188*7f2fe78bSCy Schubert         return ret;
189*7f2fe78bSCy Schubert     if (audit_log_user_message(fd, local_type, jout,
190*7f2fe78bSCy Schubert                                NULL, NULL, NULL, ev_success) <= 0)
191*7f2fe78bSCy Schubert         ret = EIO;
192*7f2fe78bSCy Schubert     free(jout);
193*7f2fe78bSCy Schubert     return ret;
194*7f2fe78bSCy Schubert }
195*7f2fe78bSCy Schubert 
196*7f2fe78bSCy Schubert /* Log S4U2PROXY event. Returns 0 on success */
197*7f2fe78bSCy Schubert static krb5_error_code
j_tgs_s4u2proxy(krb5_audit_moddata auctx,krb5_boolean ev_success,krb5_audit_state * state)198*7f2fe78bSCy Schubert j_tgs_s4u2proxy(krb5_audit_moddata auctx, krb5_boolean ev_success,
199*7f2fe78bSCy Schubert                 krb5_audit_state *state)
200*7f2fe78bSCy Schubert {
201*7f2fe78bSCy Schubert     krb5_error_code ret = 0;
202*7f2fe78bSCy Schubert     int local_type = AUDIT_USER_AUTH;
203*7f2fe78bSCy Schubert     int fd = auctx->fd;
204*7f2fe78bSCy Schubert     char *jout = NULL;
205*7f2fe78bSCy Schubert 
206*7f2fe78bSCy Schubert     if (fd < 0)
207*7f2fe78bSCy Schubert         return KRB5_PLUGIN_NO_HANDLE; /* audit module is unavailable */
208*7f2fe78bSCy Schubert 
209*7f2fe78bSCy Schubert     ret = kau_j_tgs_s4u2proxy(ev_success, state, &jout);
210*7f2fe78bSCy Schubert     if (ret)
211*7f2fe78bSCy Schubert         return ret;
212*7f2fe78bSCy Schubert     if (audit_log_user_message(fd, local_type, jout,
213*7f2fe78bSCy Schubert                                NULL, NULL, NULL, ev_success) <= 0)
214*7f2fe78bSCy Schubert         ret = EIO;
215*7f2fe78bSCy Schubert     free(jout);
216*7f2fe78bSCy Schubert     return ret;
217*7f2fe78bSCy Schubert }
218*7f2fe78bSCy Schubert 
219*7f2fe78bSCy Schubert /* Log user-to-user event. Returns 0 on success */
220*7f2fe78bSCy Schubert static krb5_error_code
j_tgs_u2u(krb5_audit_moddata auctx,krb5_boolean ev_success,krb5_audit_state * state)221*7f2fe78bSCy Schubert j_tgs_u2u(krb5_audit_moddata auctx, krb5_boolean ev_success,
222*7f2fe78bSCy Schubert           krb5_audit_state *state)
223*7f2fe78bSCy Schubert {
224*7f2fe78bSCy Schubert     krb5_error_code ret = 0;
225*7f2fe78bSCy Schubert     int local_type = AUDIT_USER_AUTH;
226*7f2fe78bSCy Schubert     int fd = auctx->fd;
227*7f2fe78bSCy Schubert     char *jout = NULL;
228*7f2fe78bSCy Schubert 
229*7f2fe78bSCy Schubert     if (fd < 0)
230*7f2fe78bSCy Schubert         return KRB5_PLUGIN_NO_HANDLE; /* audit module is unavailable */
231*7f2fe78bSCy Schubert 
232*7f2fe78bSCy Schubert     ret = kau_j_tgs_u2u(ev_success, state, &jout);
233*7f2fe78bSCy Schubert     if (ret)
234*7f2fe78bSCy Schubert         return ret;
235*7f2fe78bSCy Schubert     if (audit_log_user_message(fd, local_type, jout,
236*7f2fe78bSCy Schubert                                NULL, NULL, NULL, ev_success) <= 0)
237*7f2fe78bSCy Schubert         ret = EIO;
238*7f2fe78bSCy Schubert     free(jout);
239*7f2fe78bSCy Schubert     return ret;
240*7f2fe78bSCy Schubert }
241*7f2fe78bSCy Schubert 
242*7f2fe78bSCy Schubert krb5_error_code
audit_simple_initvt(krb5_context context,int maj_ver,int min_ver,krb5_plugin_vtable vtable)243*7f2fe78bSCy Schubert audit_simple_initvt(krb5_context context, int maj_ver,
244*7f2fe78bSCy Schubert                     int min_ver, krb5_plugin_vtable vtable)
245*7f2fe78bSCy Schubert {
246*7f2fe78bSCy Schubert     krb5_audit_vtable vt;
247*7f2fe78bSCy Schubert 
248*7f2fe78bSCy Schubert     if (maj_ver != 1)
249*7f2fe78bSCy Schubert         return KRB5_PLUGIN_VER_NOTSUPP;
250*7f2fe78bSCy Schubert 
251*7f2fe78bSCy Schubert     vt = (krb5_audit_vtable)vtable;
252*7f2fe78bSCy Schubert     vt->name = "simple";
253*7f2fe78bSCy Schubert     vt->open = open_au;
254*7f2fe78bSCy Schubert     vt->close = close_au;
255*7f2fe78bSCy Schubert     vt->kdc_start = j_kdc_start;
256*7f2fe78bSCy Schubert     vt->kdc_stop = j_kdc_stop;
257*7f2fe78bSCy Schubert     vt->as_req = j_as_req;
258*7f2fe78bSCy Schubert     vt->tgs_req = j_tgs_req;
259*7f2fe78bSCy Schubert     vt->tgs_s4u2self = j_tgs_s4u2self;
260*7f2fe78bSCy Schubert     vt->tgs_s4u2proxy = j_tgs_s4u2proxy;
261*7f2fe78bSCy Schubert     vt->tgs_u2u = j_tgs_u2u;
262*7f2fe78bSCy Schubert     return 0;
263*7f2fe78bSCy Schubert }
264