lastbind.c revision 1.2 1 1.2 christos /* $NetBSD: lastbind.c,v 1.2 2021/08/14 16:14:52 christos Exp $ */
2 1.1 tron
3 1.1 tron /* lastbind.c - Record timestamp of the last successful bind to entries */
4 1.1 tron /* $OpenLDAP$ */
5 1.1 tron /*
6 1.1 tron * Copyright 2009 Jonathan Clarke <jonathan (at) phillipoux.net>.
7 1.1 tron * All rights reserved.
8 1.1 tron *
9 1.1 tron * Redistribution and use in source and binary forms, with or without
10 1.1 tron * modification, are permitted only as authorized by the OpenLDAP
11 1.1 tron * Public License.
12 1.1 tron *
13 1.1 tron * A copy of this license is available in the file LICENSE in the
14 1.1 tron * top-level directory of the distribution or, alternatively, at
15 1.1 tron * <http://www.OpenLDAP.org/license.html>.
16 1.1 tron */
17 1.1 tron /* ACKNOWLEDGEMENTS:
18 1.1 tron * This work is loosely derived from the ppolicy overlay.
19 1.1 tron */
20 1.1 tron
21 1.2 christos #include <sys/cdefs.h>
22 1.2 christos __RCSID("$NetBSD: lastbind.c,v 1.2 2021/08/14 16:14:52 christos Exp $");
23 1.2 christos
24 1.1 tron #include "portable.h"
25 1.1 tron
26 1.1 tron /*
27 1.1 tron * This file implements an overlay that stores the timestamp of the
28 1.1 tron * last successful bind operation in a directory entry.
29 1.1 tron *
30 1.1 tron * Optimization: to avoid performing a write on each bind,
31 1.1 tron * a precision for this timestamp may be configured, causing it to
32 1.1 tron * only be updated if it is older than a given number of seconds.
33 1.1 tron */
34 1.1 tron
35 1.1 tron #ifdef SLAPD_OVER_LASTBIND
36 1.1 tron
37 1.1 tron #include <ldap.h>
38 1.1 tron #include "lutil.h"
39 1.1 tron #include "slap.h"
40 1.1 tron #include <ac/errno.h>
41 1.1 tron #include <ac/time.h>
42 1.1 tron #include <ac/string.h>
43 1.1 tron #include <ac/ctype.h>
44 1.2 christos #include "slap-config.h"
45 1.1 tron
46 1.1 tron /* Per-instance configuration information */
47 1.1 tron typedef struct lastbind_info {
48 1.1 tron /* precision to update timestamp in authTimestamp attribute */
49 1.1 tron int timestamp_precision;
50 1.2 christos int forward_updates; /* use frontend for authTimestamp updates */
51 1.1 tron } lastbind_info;
52 1.1 tron
53 1.1 tron /* Operational attributes */
54 1.1 tron static AttributeDescription *ad_authTimestamp;
55 1.1 tron
56 1.1 tron /* This is the definition used by ISODE, as supplied to us in
57 1.1 tron * ITS#6238 Followup #9
58 1.1 tron */
59 1.1 tron static struct schema_info {
60 1.1 tron char *def;
61 1.1 tron AttributeDescription **ad;
62 1.1 tron } lastBind_OpSchema[] = {
63 1.1 tron { "( 1.3.6.1.4.1.453.16.2.188 "
64 1.1 tron "NAME 'authTimestamp' "
65 1.1 tron "DESC 'last successful authentication using any method/mech' "
66 1.1 tron "EQUALITY generalizedTimeMatch "
67 1.1 tron "ORDERING generalizedTimeOrderingMatch "
68 1.1 tron "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
69 1.1 tron "SINGLE-VALUE NO-USER-MODIFICATION USAGE dsaOperation )",
70 1.1 tron &ad_authTimestamp},
71 1.1 tron { NULL, NULL }
72 1.1 tron };
73 1.1 tron
74 1.1 tron /* configuration attribute and objectclass */
75 1.1 tron static ConfigTable lastbindcfg[] = {
76 1.1 tron { "lastbind-precision", "seconds", 2, 2, 0,
77 1.1 tron ARG_INT|ARG_OFFSET,
78 1.1 tron (void *)offsetof(lastbind_info, timestamp_precision),
79 1.2 christos "( OLcfgCtAt:5.1 "
80 1.1 tron "NAME 'olcLastBindPrecision' "
81 1.1 tron "DESC 'Precision of authTimestamp attribute' "
82 1.2 christos "EQUALITY integerMatch "
83 1.1 tron "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
84 1.2 christos { "lastbind_forward_updates", "on|off", 1, 2, 0,
85 1.2 christos ARG_ON_OFF|ARG_OFFSET,
86 1.2 christos (void *)offsetof(lastbind_info,forward_updates),
87 1.2 christos "( OLcfgAt:5.2 NAME 'olcLastBindForwardUpdates' "
88 1.2 christos "DESC 'Allow authTimestamp updates to be forwarded via updateref' "
89 1.2 christos "EQUALITY booleanMatch "
90 1.2 christos "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
91 1.1 tron { NULL, NULL, 0, 0, 0, ARG_IGNORED }
92 1.1 tron };
93 1.1 tron
94 1.1 tron static ConfigOCs lastbindocs[] = {
95 1.2 christos { "( OLcfgCtOc:5.1 "
96 1.1 tron "NAME 'olcLastBindConfig' "
97 1.1 tron "DESC 'Last Bind configuration' "
98 1.1 tron "SUP olcOverlayConfig "
99 1.2 christos "MAY ( olcLastBindPrecision $ olcLastBindForwardUpdates) )",
100 1.1 tron Cft_Overlay, lastbindcfg, NULL, NULL },
101 1.1 tron { NULL, 0, NULL }
102 1.1 tron };
103 1.1 tron
104 1.1 tron static time_t
105 1.1 tron parse_time( char *atm )
106 1.1 tron {
107 1.1 tron struct lutil_tm tm;
108 1.1 tron struct lutil_timet tt;
109 1.1 tron time_t ret = (time_t)-1;
110 1.1 tron
111 1.1 tron if ( lutil_parsetime( atm, &tm ) == 0) {
112 1.1 tron lutil_tm2time( &tm, &tt );
113 1.1 tron ret = tt.tt_sec;
114 1.1 tron }
115 1.1 tron return ret;
116 1.1 tron }
117 1.1 tron
118 1.1 tron static int
119 1.1 tron lastbind_bind_response( Operation *op, SlapReply *rs )
120 1.1 tron {
121 1.1 tron Modifications *mod = NULL;
122 1.1 tron BackendInfo *bi = op->o_bd->bd_info;
123 1.1 tron Entry *e;
124 1.1 tron int rc;
125 1.1 tron
126 1.1 tron /* we're only interested if the bind was successful */
127 1.1 tron if ( rs->sr_err != LDAP_SUCCESS )
128 1.1 tron return SLAP_CB_CONTINUE;
129 1.1 tron
130 1.1 tron rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
131 1.1 tron op->o_bd->bd_info = bi;
132 1.1 tron
133 1.1 tron if ( rc != LDAP_SUCCESS ) {
134 1.1 tron return SLAP_CB_CONTINUE;
135 1.1 tron }
136 1.1 tron
137 1.1 tron {
138 1.1 tron lastbind_info *lbi = (lastbind_info *) op->o_callback->sc_private;
139 1.1 tron
140 1.1 tron time_t now, bindtime = (time_t)-1;
141 1.1 tron Attribute *a;
142 1.1 tron Modifications *m;
143 1.1 tron char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ];
144 1.1 tron struct berval timestamp;
145 1.1 tron
146 1.1 tron /* get the current time */
147 1.1 tron now = slap_get_time();
148 1.1 tron
149 1.1 tron /* get authTimestamp attribute, if it exists */
150 1.1 tron if ((a = attr_find( e->e_attrs, ad_authTimestamp)) != NULL) {
151 1.1 tron bindtime = parse_time( a->a_nvals[0].bv_val );
152 1.1 tron
153 1.1 tron if (bindtime != (time_t)-1) {
154 1.1 tron /* if the recorded bind time is within our precision, we're done
155 1.1 tron * it doesn't need to be updated (save a write for nothing) */
156 1.1 tron if ((now - bindtime) < lbi->timestamp_precision) {
157 1.1 tron goto done;
158 1.1 tron }
159 1.1 tron }
160 1.1 tron }
161 1.1 tron
162 1.1 tron /* update the authTimestamp in the user's entry with the current time */
163 1.1 tron timestamp.bv_val = nowstr;
164 1.1 tron timestamp.bv_len = sizeof(nowstr);
165 1.1 tron slap_timestamp( &now, ×tamp );
166 1.1 tron
167 1.1 tron m = ch_calloc( sizeof(Modifications), 1 );
168 1.1 tron m->sml_op = LDAP_MOD_REPLACE;
169 1.1 tron m->sml_flags = 0;
170 1.1 tron m->sml_type = ad_authTimestamp->ad_cname;
171 1.1 tron m->sml_desc = ad_authTimestamp;
172 1.1 tron m->sml_numvals = 1;
173 1.1 tron m->sml_values = ch_calloc( sizeof(struct berval), 2 );
174 1.1 tron m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
175 1.1 tron
176 1.1 tron ber_dupbv( &m->sml_values[0], ×tamp );
177 1.1 tron ber_dupbv( &m->sml_nvalues[0], ×tamp );
178 1.1 tron m->sml_next = mod;
179 1.1 tron mod = m;
180 1.1 tron }
181 1.1 tron
182 1.1 tron done:
183 1.1 tron be_entry_release_r( op, e );
184 1.1 tron
185 1.1 tron /* perform the update, if necessary */
186 1.1 tron if ( mod ) {
187 1.1 tron Operation op2 = *op;
188 1.1 tron SlapReply r2 = { REP_RESULT };
189 1.1 tron slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
190 1.2 christos LDAPControl c, *ca[2];
191 1.2 christos lastbind_info *lbi = (lastbind_info *) op->o_callback->sc_private;
192 1.1 tron
193 1.1 tron /* This is a DSA-specific opattr, it never gets replicated. */
194 1.1 tron op2.o_tag = LDAP_REQ_MODIFY;
195 1.1 tron op2.o_callback = &cb;
196 1.1 tron op2.orm_modlist = mod;
197 1.2 christos op2.orm_no_opattrs = 0;
198 1.1 tron op2.o_dn = op->o_bd->be_rootdn;
199 1.1 tron op2.o_ndn = op->o_bd->be_rootndn;
200 1.2 christos
201 1.2 christos /*
202 1.2 christos * Code for forwarding of updates adapted from ppolicy.c of slapo-ppolicy
203 1.2 christos *
204 1.2 christos * If this server is a shadow and forward_updates is true,
205 1.2 christos * use the frontend to perform this modify. That will trigger
206 1.2 christos * the update referral, which can then be forwarded by the
207 1.2 christos * chain overlay. Obviously the updateref and chain overlay
208 1.2 christos * must be configured appropriately for this to be useful.
209 1.2 christos */
210 1.2 christos if ( SLAP_SHADOW( op->o_bd ) && lbi->forward_updates ) {
211 1.2 christos op2.o_bd = frontendDB;
212 1.2 christos
213 1.2 christos /* Must use Relax control since these are no-user-mod */
214 1.2 christos op2.o_relax = SLAP_CONTROL_CRITICAL;
215 1.2 christos op2.o_ctrls = ca;
216 1.2 christos ca[0] = &c;
217 1.2 christos ca[1] = NULL;
218 1.2 christos BER_BVZERO( &c.ldctl_value );
219 1.2 christos c.ldctl_iscritical = 1;
220 1.2 christos c.ldctl_oid = LDAP_CONTROL_RELAX;
221 1.2 christos } else {
222 1.2 christos /* If not forwarding, don't update opattrs and don't replicate */
223 1.2 christos if ( SLAP_SINGLE_SHADOW( op->o_bd )) {
224 1.2 christos op2.orm_no_opattrs = 1;
225 1.2 christos op2.o_dont_replicate = 1;
226 1.2 christos }
227 1.2 christos /* TODO: not sure what this does in slapo-ppolicy */
228 1.2 christos /*
229 1.2 christos op2.o_bd->bd_info = (BackendInfo *)on->on_info;
230 1.2 christos */
231 1.2 christos }
232 1.2 christos
233 1.2 christos rc = op2.o_bd->be_modify( &op2, &r2 );
234 1.1 tron slap_mods_free( mod, 1 );
235 1.1 tron }
236 1.1 tron
237 1.1 tron op->o_bd->bd_info = bi;
238 1.1 tron return SLAP_CB_CONTINUE;
239 1.1 tron }
240 1.1 tron
241 1.1 tron static int
242 1.1 tron lastbind_bind( Operation *op, SlapReply *rs )
243 1.1 tron {
244 1.1 tron slap_callback *cb;
245 1.1 tron slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
246 1.1 tron
247 1.1 tron /* setup a callback to intercept result of this bind operation
248 1.1 tron * and pass along the lastbind_info struct */
249 1.1 tron cb = op->o_tmpcalloc( sizeof(slap_callback), 1, op->o_tmpmemctx );
250 1.1 tron cb->sc_response = lastbind_bind_response;
251 1.1 tron cb->sc_next = op->o_callback->sc_next;
252 1.1 tron cb->sc_private = on->on_bi.bi_private;
253 1.1 tron op->o_callback->sc_next = cb;
254 1.1 tron
255 1.1 tron return SLAP_CB_CONTINUE;
256 1.1 tron }
257 1.1 tron
258 1.1 tron static int
259 1.1 tron lastbind_db_init(
260 1.1 tron BackendDB *be,
261 1.1 tron ConfigReply *cr
262 1.1 tron )
263 1.1 tron {
264 1.1 tron slap_overinst *on = (slap_overinst *) be->bd_info;
265 1.1 tron
266 1.1 tron /* initialize private structure to store configuration */
267 1.1 tron on->on_bi.bi_private = ch_calloc( 1, sizeof(lastbind_info) );
268 1.1 tron
269 1.1 tron return 0;
270 1.1 tron }
271 1.1 tron
272 1.1 tron static int
273 1.1 tron lastbind_db_close(
274 1.1 tron BackendDB *be,
275 1.1 tron ConfigReply *cr
276 1.1 tron )
277 1.1 tron {
278 1.1 tron slap_overinst *on = (slap_overinst *) be->bd_info;
279 1.1 tron lastbind_info *lbi = (lastbind_info *) on->on_bi.bi_private;
280 1.1 tron
281 1.1 tron /* free private structure to store configuration */
282 1.1 tron free( lbi );
283 1.1 tron
284 1.1 tron return 0;
285 1.1 tron }
286 1.1 tron
287 1.1 tron static slap_overinst lastbind;
288 1.1 tron
289 1.1 tron int lastbind_initialize()
290 1.1 tron {
291 1.1 tron int i, code;
292 1.1 tron
293 1.1 tron /* register operational schema for this overlay (authTimestamp attribute) */
294 1.1 tron for (i=0; lastBind_OpSchema[i].def; i++) {
295 1.1 tron code = register_at( lastBind_OpSchema[i].def, lastBind_OpSchema[i].ad, 0 );
296 1.1 tron if ( code ) {
297 1.1 tron Debug( LDAP_DEBUG_ANY,
298 1.2 christos "lastbind_initialize: register_at failed\n" );
299 1.1 tron return code;
300 1.1 tron }
301 1.1 tron }
302 1.1 tron
303 1.1 tron ad_authTimestamp->ad_type->sat_flags |= SLAP_AT_MANAGEABLE;
304 1.1 tron
305 1.1 tron lastbind.on_bi.bi_type = "lastbind";
306 1.2 christos lastbind.on_bi.bi_flags = SLAPO_BFLAG_SINGLE;
307 1.1 tron lastbind.on_bi.bi_db_init = lastbind_db_init;
308 1.1 tron lastbind.on_bi.bi_db_close = lastbind_db_close;
309 1.1 tron lastbind.on_bi.bi_op_bind = lastbind_bind;
310 1.1 tron
311 1.1 tron /* register configuration directives */
312 1.1 tron lastbind.on_bi.bi_cf_ocs = lastbindocs;
313 1.1 tron code = config_register_schema( lastbindcfg, lastbindocs );
314 1.1 tron if ( code ) return code;
315 1.1 tron
316 1.1 tron return overlay_register( &lastbind );
317 1.1 tron }
318 1.1 tron
319 1.1 tron #if SLAPD_OVER_LASTBIND == SLAPD_MOD_DYNAMIC
320 1.1 tron int init_module(int argc, char *argv[]) {
321 1.1 tron return lastbind_initialize();
322 1.1 tron }
323 1.1 tron #endif
324 1.1 tron
325 1.1 tron #endif /* defined(SLAPD_OVER_LASTBIND) */
326