2 * Copyright (c) 2018 Cesnet z.s.p.o.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the Institute nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include "kadm5_locl.h"
37 struct prune_principal_hook_ctx
{
38 kadm5_server_context
*context
;
39 enum kadm5_hook_stage stage
;
41 krb5_const_principal princ
;
45 static krb5_error_code KRB5_LIB_CALL
46 prune_principal_hook_cb(krb5_context context
,
52 const struct kadm5_hook_ftable
*ftable
= hook
;
53 struct prune_principal_hook_ctx
*ctx
= userctx
;
55 ret
= ftable
->prune(context
, hookctx
,
56 ctx
->stage
, ctx
->code
, ctx
->princ
, ctx
->kvno
);
57 if (ret
!= 0 && ret
!= KRB5_PLUGIN_NO_HANDLE
)
58 _kadm5_s_set_hook_error_message(ctx
->context
, ret
, "prune",
61 /* only pre-commit plugins can abort */
62 if (ret
== 0 || ctx
->stage
== KADM5_HOOK_STAGE_POSTCOMMIT
)
63 ret
= KRB5_PLUGIN_NO_HANDLE
;
69 prune_principal_hook(kadm5_server_context
*context
,
70 enum kadm5_hook_stage stage
,
72 krb5_const_principal princ
,
76 struct prune_principal_hook_ctx ctx
;
78 ctx
.context
= context
;
84 ret
= _krb5_plugin_run_f(context
->context
, &kadm5_hook_plugin_data
,
85 0, &ctx
, prune_principal_hook_cb
);
86 if (ret
== KRB5_PLUGIN_NO_HANDLE
)
93 kadm5_s_prune_principal(void *server_handle
,
97 kadm5_server_context
*context
= server_handle
;
101 memset(&ent
, 0, sizeof(ent
));
102 if (!context
->keep_open
) {
103 ret
= context
->db
->hdb_open(context
->context
, context
->db
, O_RDWR
, 0);
108 ret
= kadm5_log_init(context
);
112 /* NOTE: We do not use hdb_fetch_kvno() here */
113 ret
= context
->db
->hdb_fetch_kvno(context
->context
, context
->db
, princ
,
114 HDB_F_DECRYPT
|HDB_F_GET_ANY
|HDB_F_ADMIN_DATA
,
119 ret
= prune_principal_hook(context
, KADM5_HOOK_STAGE_PRECOMMIT
,
124 ret
= hdb_prune_keys_kvno(context
->context
, &ent
, kvno
);
128 ret
= hdb_seal_keys(context
->context
, context
->db
, &ent
);
132 ret
= kadm5_log_modify(context
, &ent
, KADM5_KEY_DATA
);
134 (void) prune_principal_hook(context
, KADM5_HOOK_STAGE_POSTCOMMIT
,
138 hdb_free_entry(context
->context
, context
->db
, &ent
);
140 (void) kadm5_log_end(context
);
142 if (!context
->keep_open
) {
144 ret2
= context
->db
->hdb_close(context
->context
, context
->db
);
145 if (ret
== 0 && ret2
!= 0)
148 return _kadm5_error_code(ret
);