summaryrefslogtreecommitdiffstats
path: root/src/kdc/kdc_audit.c
blob: c9a7f9f9d439e70950c0e92f22adff0c584076e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* kdc_audit.c - Interface for KDC audit plugins. */
/*
 * Copyright (C) 2013 by the Massachusetts Institute of Technology.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "k5-int.h"
#include "kdc_util.h"
#include "kdc_audit.h"
/* for krb5_klog_syslog */
#include <syslog.h>
#include "adm_proto.h"

struct audit_module_handle_st {
    struct krb5_audit_vtable_st vt;
    krb5_audit_moddata auctx;
};
typedef struct audit_module_handle_st *audit_module_handle;

static audit_module_handle *handles = NULL;

static void
free_handles(audit_module_handle *list)
{
    audit_module_handle *hp, hdl;

    if (list == NULL)
        return;

    for (hp = list; *hp != NULL; hp++) {
        hdl = *hp;
        if (hdl->vt.close != NULL)
            hdl->vt.close(hdl->auctx);
        free(hdl);
    }
    free(list);
}

/*
 * Load all available audit plugin modules and prepare for logging. The list of
 * modules is stored as an array in handles. Use unload_audit_modules() to free
 * resources allocated by this function.
 */
krb5_error_code
load_audit_modules(krb5_context context)
{
    krb5_error_code ret = 0;
    krb5_plugin_initvt_fn *modules = NULL, *mod;
    struct krb5_audit_vtable_st vtable;
    audit_module_handle *list = NULL, hdl = NULL;
    krb5_audit_moddata auctx;
    int count = 0;

    if (context == NULL || handles != NULL)
        return EINVAL;

    /* Get audit plugin vtable. */
    ret = k5_plugin_load_all(context, PLUGIN_INTERFACE_AUDIT, &modules);
    if (ret)
        return ret;

    /* Allocate handle, initialize vtable. */
    for (count = 0; modules[count] != NULL; count++);
    list = k5calloc(count + 1, sizeof(*list), &ret);
    if (list == NULL)
        goto cleanup;
    count = 0;
    for (mod = modules; *mod != NULL; mod++) {
        hdl = k5alloc(sizeof(*hdl), &ret);
        if (hdl == NULL)
            goto cleanup;
        ret = (*mod)(context, 1, 1, (krb5_plugin_vtable)&hdl->vt);
        if (ret) {
            free(hdl);
            hdl = NULL;
            continue;
        }

        vtable = hdl->vt;
        if (vtable.open != NULL) {
            ret = vtable.open(&auctx);
            if (ret) {
                krb5_klog_syslog(LOG_ERR,
                                 _("audit plugin %s failed to open. error=%i"),
                                 vtable.name, ret);
                goto cleanup;
            }
            hdl->auctx = auctx;
        }
        list[count++] = hdl;
        list[count] = NULL;
        hdl = NULL;
    }
    list[count] = NULL;
    handles = list;
    list = NULL;
    ret = 0;

cleanup:
    free(hdl);
    k5_plugin_free_modules(context, modules);
    free_handles(list);
    return ret;
}

/* Free resources allocated by load_audit_modules() function. */
void
unload_audit_modules(krb5_context context)
{
    free_handles(handles);
}

/*
 * Write the output ticket ID into newly-allocated buffer.
 * Returns 0 on success.
 */
krb5_error_code
kau_make_tkt_id(krb5_context context,
                const krb5_ticket *ticket, char **out)
{
    krb5_error_code ret = 0;
    char *hash = NULL, *ptr;
    krb5_checksum cksum;
    unsigned int i;

    *out = NULL;

    if (ticket == NULL)
        return EINVAL;

    ret = krb5_c_make_checksum(context, CKSUMTYPE_RSA_MD5, NULL, 0,
                               &ticket->enc_part.ciphertext, &cksum);
    if (ret)
        return ret;

    hash = k5alloc(cksum.length * 2 + 1, &ret);
    if (hash != NULL) {
        for (i = 0, ptr = hash; i < cksum.length; i++, ptr += 2)
            snprintf(ptr, 3, "%02X", cksum.contents[i]);
        *ptr = '\0';
        *out = hash;
    }
    krb5_free_checksum_contents(context, &cksum);

    return 0;
}

/*
 * Create and initialize krb5_audit_state structure.
 * Returns 0 on success.
 */
krb5_error_code
kau_init_kdc_req(krb5_context context,
                 krb5_kdc_req *request, const krb5_fulladdr *from,
                 krb5_audit_state **state_out)
{
    krb5_error_code ret = 0;
    krb5_audit_state *state = NULL;

    state = k5calloc(1, sizeof(*state), &ret);
    if (state == NULL)
        return ret;

    state->request = request;
    state->cl_addr = from->address;
    state->cl_port = from->port;
    state->stage = AUTHN_REQ_CL;
    ret = krb5int_random_string(context, state->req_id,
                                sizeof(state->req_id));
    if (ret) {
        free(state);
        return ret;
    }
    *state_out = state;

    return 0;
}

/* Free resources allocated by kau_init_kdc_req() and kau_make_tkt_id()
 * routines. */
void
kau_free_kdc_req(krb5_audit_state *state)
{
    free(state->tkt_in_id);
    free(state->tkt_out_id);
    free(state->evid_tkt_id);
    free(state);
}

/* Call the KDC start/stop audit plugin entry points. */

void
kau_kdc_stop(krb5_context context, const krb5_boolean ev_success)
{
    audit_module_handle *hp, hdl;

    if (handles == NULL)
        return;

    for (hp = handles; *hp != NULL; hp++) {
        hdl = *hp;
        if (hdl->vt.kdc_stop != NULL)
            hdl->vt.kdc_stop(hdl->auctx, ev_success);
    }
}

void
kau_kdc_start(krb5_context context, const krb5_boolean ev_success)
{
    audit_module_handle *hp, hdl;

    if (handles == NULL)
        return;

    for (hp = handles; *hp != NULL; hp++) {
        hdl = *hp;
        if (hdl->vt.kdc_start != NULL)
            hdl->vt.kdc_start(hdl->auctx, ev_success);
    }
}

/* Call the AS-REQ audit plugin entry point. */
void
kau_as_req(krb5_context context, const krb5_boolean ev_success,
           krb5_audit_state *state)
{
    audit_module_handle *hp, hdl;

    if (handles == NULL)
        return;

    for (hp = handles; *hp != NULL; hp++) {
        hdl = *hp;
        if (hdl->vt.as_req != NULL)
            hdl->vt.as_req(hdl->auctx, ev_success, state);
    }
}

/* Call the TGS-REQ audit plugin entry point. */
void
kau_tgs_req(krb5_context context, const krb5_boolean ev_success,
            krb5_audit_state *state)
{
    audit_module_handle *hp, hdl;

    if (handles == NULL)
        return;

    for (hp = handles; *hp != NULL; hp++) {
        hdl = *hp;
        if (hdl->vt.tgs_req != NULL)
            hdl->vt.tgs_req(hdl->auctx, ev_success, state);
    }
}

/* Call the S4U2Self audit plugin entry point. */
void
kau_s4u2self(krb5_context context, const krb5_boolean ev_success,
             krb5_audit_state *state)
{
    audit_module_handle *hp, hdl;

    if (handles == NULL)
        return;

    for (hp = handles; *hp != NULL; hp++) {
        hdl = *hp;
        if (hdl->vt.tgs_s4u2self != NULL)
            hdl->vt.tgs_s4u2self(hdl->auctx, ev_success, state);
    }
}

/* Call the S4U2Proxy audit plugin entry point. */
void
kau_s4u2proxy(krb5_context context,const krb5_boolean ev_success,
              krb5_audit_state *state)
{
    audit_module_handle *hp, hdl;

    if (handles == NULL)
        return;

    for (hp = handles; *hp != NULL; hp++) {
        hdl = *hp;
        if (hdl->vt.tgs_s4u2proxy != NULL)
            hdl->vt.tgs_s4u2proxy(hdl->auctx, ev_success, state);
    }
}

/* Call the U2U audit plugin entry point. */
void
kau_u2u(krb5_context context, const krb5_boolean ev_success,
        krb5_audit_state *state)
{
    audit_module_handle *hp, hdl;

    if (handles == NULL)
        return;

    for (hp = handles; *hp != NULL; hp++) {
        hdl = *hp;
        if (hdl->vt.tgs_u2u != NULL)
            hdl->vt.tgs_u2u(hdl->auctx, ev_success, state);
    }
}