xref: /freebsd/crypto/krb5/src/plugins/audit/test/au_test.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* plugins/audit/au_test.c - Test 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  * This test is to verify the JSON-based KDC audit functionality.
34*7f2fe78bSCy Schubert  * It utilized MIT Kerberos <kdc_j_encode.h> routines for JSON processing.
35*7f2fe78bSCy Schubert  */
36*7f2fe78bSCy Schubert 
37*7f2fe78bSCy Schubert #include <k5-int.h>
38*7f2fe78bSCy Schubert #include <krb5/audit_plugin.h>
39*7f2fe78bSCy Schubert #include <kdc_j_encode.h>
40*7f2fe78bSCy Schubert #include "k5-thread.h"
41*7f2fe78bSCy Schubert 
42*7f2fe78bSCy Schubert struct krb5_audit_moddata_st {
43*7f2fe78bSCy Schubert     int au_fd;
44*7f2fe78bSCy Schubert };
45*7f2fe78bSCy Schubert 
46*7f2fe78bSCy Schubert krb5_error_code
47*7f2fe78bSCy Schubert audit_test_initvt(krb5_context context, int maj_ver, int min_ver,
48*7f2fe78bSCy Schubert                   krb5_plugin_vtable vtable);
49*7f2fe78bSCy Schubert 
50*7f2fe78bSCy Schubert static FILE *au_fd;
51*7f2fe78bSCy Schubert static k5_mutex_t lock = K5_MUTEX_PARTIAL_INITIALIZER;
52*7f2fe78bSCy Schubert 
53*7f2fe78bSCy Schubert /* Open connection to the audit system. Returns 0 on success. */
54*7f2fe78bSCy Schubert static krb5_error_code
open_au(krb5_audit_moddata * auctx)55*7f2fe78bSCy Schubert open_au(krb5_audit_moddata *auctx)
56*7f2fe78bSCy Schubert {
57*7f2fe78bSCy Schubert     au_fd = fopen("au.log", "a+");
58*7f2fe78bSCy Schubert     if (au_fd == NULL)
59*7f2fe78bSCy Schubert         return KRB5_PLUGIN_NO_HANDLE; /* audit module is unavailable */
60*7f2fe78bSCy Schubert     k5_mutex_init(&lock);
61*7f2fe78bSCy Schubert     return 0;
62*7f2fe78bSCy Schubert }
63*7f2fe78bSCy Schubert 
64*7f2fe78bSCy Schubert /* Close connection to the audit system. Returns 0. */
65*7f2fe78bSCy Schubert static krb5_error_code
close_au(krb5_audit_moddata auctx)66*7f2fe78bSCy Schubert close_au(krb5_audit_moddata auctx)
67*7f2fe78bSCy Schubert {
68*7f2fe78bSCy Schubert     fclose(au_fd);
69*7f2fe78bSCy Schubert     k5_mutex_destroy(&lock);
70*7f2fe78bSCy Schubert     return 0;
71*7f2fe78bSCy Schubert }
72*7f2fe78bSCy Schubert 
73*7f2fe78bSCy Schubert /* Log KDC-start event. Returns 0 on success. */
74*7f2fe78bSCy Schubert static krb5_error_code
j_kdc_start(krb5_audit_moddata auctx,krb5_boolean ev_success)75*7f2fe78bSCy Schubert j_kdc_start(krb5_audit_moddata auctx, krb5_boolean ev_success)
76*7f2fe78bSCy Schubert {
77*7f2fe78bSCy Schubert     krb5_error_code ret = 0;
78*7f2fe78bSCy Schubert     char *jout = NULL;
79*7f2fe78bSCy Schubert 
80*7f2fe78bSCy Schubert     ret = kau_j_kdc_start(ev_success, &jout);
81*7f2fe78bSCy Schubert     if (ret)
82*7f2fe78bSCy Schubert         return ret;
83*7f2fe78bSCy Schubert     k5_mutex_lock(&lock);
84*7f2fe78bSCy Schubert     fprintf(au_fd,"%s\n", jout);
85*7f2fe78bSCy Schubert     fflush(au_fd);
86*7f2fe78bSCy Schubert     k5_mutex_unlock(&lock);
87*7f2fe78bSCy Schubert     free(jout);
88*7f2fe78bSCy Schubert     return ret;
89*7f2fe78bSCy Schubert }
90*7f2fe78bSCy Schubert 
91*7f2fe78bSCy Schubert /* Log KDC-stop event. Returns 0 on success. */
92*7f2fe78bSCy Schubert static krb5_error_code
j_kdc_stop(krb5_audit_moddata auctx,krb5_boolean ev_success)93*7f2fe78bSCy Schubert j_kdc_stop(krb5_audit_moddata auctx, krb5_boolean ev_success)
94*7f2fe78bSCy Schubert {
95*7f2fe78bSCy Schubert     krb5_error_code ret = 0;
96*7f2fe78bSCy Schubert     char *jout = NULL;
97*7f2fe78bSCy Schubert 
98*7f2fe78bSCy Schubert     ret = kau_j_kdc_stop(ev_success, &jout);
99*7f2fe78bSCy Schubert     if (ret)
100*7f2fe78bSCy Schubert         return ret;
101*7f2fe78bSCy Schubert     k5_mutex_lock(&lock);
102*7f2fe78bSCy Schubert     fprintf(au_fd,"%s\n", jout);
103*7f2fe78bSCy Schubert     fflush(au_fd);
104*7f2fe78bSCy Schubert     k5_mutex_unlock(&lock);
105*7f2fe78bSCy Schubert     free(jout);
106*7f2fe78bSCy Schubert     return ret;
107*7f2fe78bSCy Schubert }
108*7f2fe78bSCy Schubert 
109*7f2fe78bSCy Schubert /* Log AS_REQ event. Returns 0 on success. */
110*7f2fe78bSCy Schubert static krb5_error_code
j_as_req(krb5_audit_moddata auctx,krb5_boolean ev_success,krb5_audit_state * state)111*7f2fe78bSCy Schubert j_as_req(krb5_audit_moddata auctx, krb5_boolean ev_success,
112*7f2fe78bSCy Schubert          krb5_audit_state *state)
113*7f2fe78bSCy Schubert {
114*7f2fe78bSCy Schubert     krb5_error_code ret = 0;
115*7f2fe78bSCy Schubert     char *jout = NULL;
116*7f2fe78bSCy Schubert 
117*7f2fe78bSCy Schubert     ret = kau_j_as_req(ev_success, state, &jout);
118*7f2fe78bSCy Schubert     if (ret)
119*7f2fe78bSCy Schubert         return ret;
120*7f2fe78bSCy Schubert     k5_mutex_lock(&lock);
121*7f2fe78bSCy Schubert     fprintf(au_fd,"%s\n", jout);
122*7f2fe78bSCy Schubert     fflush(au_fd);
123*7f2fe78bSCy Schubert     k5_mutex_unlock(&lock);
124*7f2fe78bSCy Schubert     free(jout);
125*7f2fe78bSCy Schubert     return ret;
126*7f2fe78bSCy Schubert }
127*7f2fe78bSCy Schubert 
128*7f2fe78bSCy Schubert /* Log TGS_REQ event. Returns 0 on success. */
129*7f2fe78bSCy Schubert static krb5_error_code
j_tgs_req(krb5_audit_moddata auctx,krb5_boolean ev_success,krb5_audit_state * state)130*7f2fe78bSCy Schubert j_tgs_req(krb5_audit_moddata auctx, krb5_boolean ev_success,
131*7f2fe78bSCy Schubert           krb5_audit_state *state)
132*7f2fe78bSCy Schubert {
133*7f2fe78bSCy Schubert     krb5_error_code ret = 0;
134*7f2fe78bSCy Schubert     char *jout = NULL;
135*7f2fe78bSCy Schubert 
136*7f2fe78bSCy Schubert     ret = kau_j_tgs_req(ev_success, state, &jout);
137*7f2fe78bSCy Schubert     if (ret)
138*7f2fe78bSCy Schubert         return ret;
139*7f2fe78bSCy Schubert     k5_mutex_lock(&lock);
140*7f2fe78bSCy Schubert     fprintf(au_fd,"%s\n", jout);
141*7f2fe78bSCy Schubert     fflush(au_fd);
142*7f2fe78bSCy Schubert     k5_mutex_unlock(&lock);
143*7f2fe78bSCy Schubert     free(jout);
144*7f2fe78bSCy Schubert     return ret;
145*7f2fe78bSCy Schubert }
146*7f2fe78bSCy Schubert 
147*7f2fe78bSCy Schubert /* Log S4U2SELF event. Returns 0 on success. */
148*7f2fe78bSCy Schubert static krb5_error_code
j_tgs_s4u2self(krb5_audit_moddata auctx,krb5_boolean ev_success,krb5_audit_state * state)149*7f2fe78bSCy Schubert j_tgs_s4u2self(krb5_audit_moddata auctx, krb5_boolean ev_success,
150*7f2fe78bSCy Schubert                krb5_audit_state *state)
151*7f2fe78bSCy Schubert {
152*7f2fe78bSCy Schubert     krb5_error_code ret = 0;
153*7f2fe78bSCy Schubert     char *jout = NULL;
154*7f2fe78bSCy Schubert 
155*7f2fe78bSCy Schubert     ret = kau_j_tgs_s4u2self(ev_success, state, &jout);
156*7f2fe78bSCy Schubert     if (ret)
157*7f2fe78bSCy Schubert         return ret;
158*7f2fe78bSCy Schubert     k5_mutex_lock(&lock);
159*7f2fe78bSCy Schubert     fprintf(au_fd,"%s\n", jout);
160*7f2fe78bSCy Schubert     fflush(au_fd);
161*7f2fe78bSCy Schubert     k5_mutex_unlock(&lock);
162*7f2fe78bSCy Schubert     free(jout);
163*7f2fe78bSCy Schubert     return ret;
164*7f2fe78bSCy Schubert }
165*7f2fe78bSCy Schubert 
166*7f2fe78bSCy Schubert /* Log S4U2PROXY event. Returns 0 on success. */
167*7f2fe78bSCy Schubert static krb5_error_code
j_tgs_s4u2proxy(krb5_audit_moddata auctx,krb5_boolean ev_success,krb5_audit_state * state)168*7f2fe78bSCy Schubert j_tgs_s4u2proxy(krb5_audit_moddata auctx, krb5_boolean ev_success,
169*7f2fe78bSCy Schubert                 krb5_audit_state *state)
170*7f2fe78bSCy Schubert {
171*7f2fe78bSCy Schubert     krb5_error_code ret = 0;
172*7f2fe78bSCy Schubert     char *jout = NULL;
173*7f2fe78bSCy Schubert 
174*7f2fe78bSCy Schubert     ret = kau_j_tgs_s4u2proxy(ev_success, state, &jout);
175*7f2fe78bSCy Schubert     if (ret)
176*7f2fe78bSCy Schubert         return ret;
177*7f2fe78bSCy Schubert     k5_mutex_lock(&lock);
178*7f2fe78bSCy Schubert     fprintf(au_fd,"%s\n", jout);
179*7f2fe78bSCy Schubert     fflush(au_fd);
180*7f2fe78bSCy Schubert     k5_mutex_unlock(&lock);
181*7f2fe78bSCy Schubert     free(jout);
182*7f2fe78bSCy Schubert     return ret;
183*7f2fe78bSCy Schubert }
184*7f2fe78bSCy Schubert 
185*7f2fe78bSCy Schubert /* Log user-to-user event. Returns 0 on success. */
186*7f2fe78bSCy Schubert static krb5_error_code
j_tgs_u2u(krb5_audit_moddata auctx,krb5_boolean ev_success,krb5_audit_state * state)187*7f2fe78bSCy Schubert j_tgs_u2u(krb5_audit_moddata auctx, krb5_boolean ev_success,
188*7f2fe78bSCy Schubert           krb5_audit_state *state)
189*7f2fe78bSCy Schubert {
190*7f2fe78bSCy Schubert     krb5_error_code ret = 0;
191*7f2fe78bSCy Schubert     char *jout = NULL;
192*7f2fe78bSCy Schubert 
193*7f2fe78bSCy Schubert     ret = kau_j_tgs_u2u(ev_success, state, &jout);
194*7f2fe78bSCy Schubert     if (ret)
195*7f2fe78bSCy Schubert         return ret;
196*7f2fe78bSCy Schubert     k5_mutex_lock(&lock);
197*7f2fe78bSCy Schubert     fprintf(au_fd,"%s\n", jout);
198*7f2fe78bSCy Schubert     fflush(au_fd);
199*7f2fe78bSCy Schubert     k5_mutex_unlock(&lock);
200*7f2fe78bSCy Schubert     free(jout);
201*7f2fe78bSCy Schubert     return ret;
202*7f2fe78bSCy Schubert }
203*7f2fe78bSCy Schubert 
204*7f2fe78bSCy Schubert krb5_error_code
audit_test_initvt(krb5_context context,int maj_ver,int min_ver,krb5_plugin_vtable vtable)205*7f2fe78bSCy Schubert audit_test_initvt(krb5_context context, int maj_ver, int min_ver,
206*7f2fe78bSCy Schubert                   krb5_plugin_vtable vtable)
207*7f2fe78bSCy Schubert {
208*7f2fe78bSCy Schubert     krb5_audit_vtable vt;
209*7f2fe78bSCy Schubert 
210*7f2fe78bSCy Schubert     if (maj_ver != 1)
211*7f2fe78bSCy Schubert         return KRB5_PLUGIN_VER_NOTSUPP;
212*7f2fe78bSCy Schubert 
213*7f2fe78bSCy Schubert     vt = (krb5_audit_vtable)vtable;
214*7f2fe78bSCy Schubert     vt->name = "test";
215*7f2fe78bSCy Schubert 
216*7f2fe78bSCy Schubert     vt->open = open_au;
217*7f2fe78bSCy Schubert     vt->close = close_au;
218*7f2fe78bSCy Schubert     vt->kdc_start = j_kdc_start;
219*7f2fe78bSCy Schubert     vt->kdc_stop = j_kdc_stop;
220*7f2fe78bSCy Schubert     vt->as_req = j_as_req;
221*7f2fe78bSCy Schubert     vt->tgs_req = j_tgs_req;
222*7f2fe78bSCy Schubert     vt->tgs_s4u2self = j_tgs_s4u2self;
223*7f2fe78bSCy Schubert     vt->tgs_s4u2proxy = j_tgs_s4u2proxy;
224*7f2fe78bSCy Schubert     vt->tgs_u2u = j_tgs_u2u;
225*7f2fe78bSCy Schubert 
226*7f2fe78bSCy Schubert     return 0;
227*7f2fe78bSCy Schubert }
228