lastbind.c revision 1.1.1.3.4.1 1 1.1.1.3.4.1 martin /* $NetBSD: lastbind.c,v 1.1.1.3.4.1 2020/04/13 07:56:08 martin 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.1.1.2 christos #include <sys/cdefs.h>
22 1.1.1.3.4.1 martin __RCSID("$NetBSD: lastbind.c,v 1.1.1.3.4.1 2020/04/13 07:56:08 martin Exp $");
23 1.1.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.1 tron #include "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.1.1.3.4.1 martin 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.1.1.2 christos "( OLcfgCtAt:5.1 "
80 1.1 tron "NAME 'olcLastBindPrecision' "
81 1.1 tron "DESC 'Precision of authTimestamp attribute' "
82 1.1 tron "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
83 1.1.1.3.4.1 martin { "lastbind_forward_updates", "on|off", 1, 2, 0,
84 1.1.1.3.4.1 martin ARG_ON_OFF|ARG_OFFSET,
85 1.1.1.3.4.1 martin (void *)offsetof(lastbind_info,forward_updates),
86 1.1.1.3.4.1 martin "( OLcfgAt:5.2 NAME 'olcLastBindForwardUpdates' "
87 1.1.1.3.4.1 martin "DESC 'Allow authTimestamp updates to be forwarded via updateref' "
88 1.1.1.3.4.1 martin "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
89 1.1 tron { NULL, NULL, 0, 0, 0, ARG_IGNORED }
90 1.1 tron };
91 1.1 tron
92 1.1 tron static ConfigOCs lastbindocs[] = {
93 1.1.1.2 christos { "( OLcfgCtOc:5.1 "
94 1.1 tron "NAME 'olcLastBindConfig' "
95 1.1 tron "DESC 'Last Bind configuration' "
96 1.1 tron "SUP olcOverlayConfig "
97 1.1.1.3.4.1 martin "MAY ( olcLastBindPrecision $ olcLastBindForwardUpdates) )",
98 1.1 tron Cft_Overlay, lastbindcfg, NULL, NULL },
99 1.1 tron { NULL, 0, NULL }
100 1.1 tron };
101 1.1 tron
102 1.1 tron static time_t
103 1.1 tron parse_time( char *atm )
104 1.1 tron {
105 1.1 tron struct lutil_tm tm;
106 1.1 tron struct lutil_timet tt;
107 1.1 tron time_t ret = (time_t)-1;
108 1.1 tron
109 1.1 tron if ( lutil_parsetime( atm, &tm ) == 0) {
110 1.1 tron lutil_tm2time( &tm, &tt );
111 1.1 tron ret = tt.tt_sec;
112 1.1 tron }
113 1.1 tron return ret;
114 1.1 tron }
115 1.1 tron
116 1.1 tron static int
117 1.1 tron lastbind_bind_response( Operation *op, SlapReply *rs )
118 1.1 tron {
119 1.1 tron Modifications *mod = NULL;
120 1.1 tron BackendInfo *bi = op->o_bd->bd_info;
121 1.1 tron Entry *e;
122 1.1 tron int rc;
123 1.1 tron
124 1.1 tron /* we're only interested if the bind was successful */
125 1.1 tron if ( rs->sr_err != LDAP_SUCCESS )
126 1.1 tron return SLAP_CB_CONTINUE;
127 1.1 tron
128 1.1 tron rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
129 1.1 tron op->o_bd->bd_info = bi;
130 1.1 tron
131 1.1 tron if ( rc != LDAP_SUCCESS ) {
132 1.1 tron return SLAP_CB_CONTINUE;
133 1.1 tron }
134 1.1 tron
135 1.1 tron {
136 1.1 tron lastbind_info *lbi = (lastbind_info *) op->o_callback->sc_private;
137 1.1 tron
138 1.1 tron time_t now, bindtime = (time_t)-1;
139 1.1 tron Attribute *a;
140 1.1 tron Modifications *m;
141 1.1 tron char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ];
142 1.1 tron struct berval timestamp;
143 1.1 tron
144 1.1 tron /* get the current time */
145 1.1 tron now = slap_get_time();
146 1.1 tron
147 1.1 tron /* get authTimestamp attribute, if it exists */
148 1.1 tron if ((a = attr_find( e->e_attrs, ad_authTimestamp)) != NULL) {
149 1.1 tron bindtime = parse_time( a->a_nvals[0].bv_val );
150 1.1 tron
151 1.1 tron if (bindtime != (time_t)-1) {
152 1.1 tron /* if the recorded bind time is within our precision, we're done
153 1.1 tron * it doesn't need to be updated (save a write for nothing) */
154 1.1 tron if ((now - bindtime) < lbi->timestamp_precision) {
155 1.1 tron goto done;
156 1.1 tron }
157 1.1 tron }
158 1.1 tron }
159 1.1 tron
160 1.1 tron /* update the authTimestamp in the user's entry with the current time */
161 1.1 tron timestamp.bv_val = nowstr;
162 1.1 tron timestamp.bv_len = sizeof(nowstr);
163 1.1 tron slap_timestamp( &now, ×tamp );
164 1.1 tron
165 1.1 tron m = ch_calloc( sizeof(Modifications), 1 );
166 1.1 tron m->sml_op = LDAP_MOD_REPLACE;
167 1.1 tron m->sml_flags = 0;
168 1.1 tron m->sml_type = ad_authTimestamp->ad_cname;
169 1.1 tron m->sml_desc = ad_authTimestamp;
170 1.1 tron m->sml_numvals = 1;
171 1.1 tron m->sml_values = ch_calloc( sizeof(struct berval), 2 );
172 1.1 tron m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
173 1.1 tron
174 1.1 tron ber_dupbv( &m->sml_values[0], ×tamp );
175 1.1 tron ber_dupbv( &m->sml_nvalues[0], ×tamp );
176 1.1 tron m->sml_next = mod;
177 1.1 tron mod = m;
178 1.1 tron }
179 1.1 tron
180 1.1 tron done:
181 1.1 tron be_entry_release_r( op, e );
182 1.1 tron
183 1.1 tron /* perform the update, if necessary */
184 1.1 tron if ( mod ) {
185 1.1 tron Operation op2 = *op;
186 1.1 tron SlapReply r2 = { REP_RESULT };
187 1.1 tron slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
188 1.1.1.3.4.1 martin LDAPControl c, *ca[2];
189 1.1.1.3.4.1 martin lastbind_info *lbi = (lastbind_info *) op->o_callback->sc_private;
190 1.1 tron
191 1.1 tron /* This is a DSA-specific opattr, it never gets replicated. */
192 1.1 tron op2.o_tag = LDAP_REQ_MODIFY;
193 1.1 tron op2.o_callback = &cb;
194 1.1 tron op2.orm_modlist = mod;
195 1.1.1.3.4.1 martin op2.orm_no_opattrs = 0;
196 1.1 tron op2.o_dn = op->o_bd->be_rootdn;
197 1.1 tron op2.o_ndn = op->o_bd->be_rootndn;
198 1.1.1.3.4.1 martin
199 1.1.1.3.4.1 martin /*
200 1.1.1.3.4.1 martin * Code for forwarding of updates adapted from ppolicy.c of slapo-ppolicy
201 1.1.1.3.4.1 martin *
202 1.1.1.3.4.1 martin * If this server is a shadow and forward_updates is true,
203 1.1.1.3.4.1 martin * use the frontend to perform this modify. That will trigger
204 1.1.1.3.4.1 martin * the update referral, which can then be forwarded by the
205 1.1.1.3.4.1 martin * chain overlay. Obviously the updateref and chain overlay
206 1.1.1.3.4.1 martin * must be configured appropriately for this to be useful.
207 1.1.1.3.4.1 martin */
208 1.1.1.3.4.1 martin if ( SLAP_SHADOW( op->o_bd ) && lbi->forward_updates ) {
209 1.1.1.3.4.1 martin op2.o_bd = frontendDB;
210 1.1.1.3.4.1 martin
211 1.1.1.3.4.1 martin /* Must use Relax control since these are no-user-mod */
212 1.1.1.3.4.1 martin op2.o_relax = SLAP_CONTROL_CRITICAL;
213 1.1.1.3.4.1 martin op2.o_ctrls = ca;
214 1.1.1.3.4.1 martin ca[0] = &c;
215 1.1.1.3.4.1 martin ca[1] = NULL;
216 1.1.1.3.4.1 martin BER_BVZERO( &c.ldctl_value );
217 1.1.1.3.4.1 martin c.ldctl_iscritical = 1;
218 1.1.1.3.4.1 martin c.ldctl_oid = LDAP_CONTROL_RELAX;
219 1.1.1.3.4.1 martin } else {
220 1.1.1.3.4.1 martin /* If not forwarding, don't update opattrs and don't replicate */
221 1.1.1.3.4.1 martin if ( SLAP_SINGLE_SHADOW( op->o_bd )) {
222 1.1.1.3.4.1 martin op2.orm_no_opattrs = 1;
223 1.1.1.3.4.1 martin op2.o_dont_replicate = 1;
224 1.1.1.3.4.1 martin }
225 1.1.1.3.4.1 martin /* TODO: not sure what this does in slapo-ppolicy */
226 1.1.1.3.4.1 martin /*
227 1.1.1.3.4.1 martin op2.o_bd->bd_info = (BackendInfo *)on->on_info;
228 1.1.1.3.4.1 martin */
229 1.1.1.3.4.1 martin }
230 1.1.1.3.4.1 martin
231 1.1 tron rc = op->o_bd->be_modify( &op2, &r2 );
232 1.1 tron slap_mods_free( mod, 1 );
233 1.1 tron }
234 1.1 tron
235 1.1 tron op->o_bd->bd_info = bi;
236 1.1 tron return SLAP_CB_CONTINUE;
237 1.1 tron }
238 1.1 tron
239 1.1 tron static int
240 1.1 tron lastbind_bind( Operation *op, SlapReply *rs )
241 1.1 tron {
242 1.1 tron slap_callback *cb;
243 1.1 tron slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
244 1.1 tron
245 1.1 tron /* setup a callback to intercept result of this bind operation
246 1.1 tron * and pass along the lastbind_info struct */
247 1.1 tron cb = op->o_tmpcalloc( sizeof(slap_callback), 1, op->o_tmpmemctx );
248 1.1 tron cb->sc_response = lastbind_bind_response;
249 1.1 tron cb->sc_next = op->o_callback->sc_next;
250 1.1 tron cb->sc_private = on->on_bi.bi_private;
251 1.1 tron op->o_callback->sc_next = cb;
252 1.1 tron
253 1.1 tron return SLAP_CB_CONTINUE;
254 1.1 tron }
255 1.1 tron
256 1.1 tron static int
257 1.1 tron lastbind_db_init(
258 1.1 tron BackendDB *be,
259 1.1 tron ConfigReply *cr
260 1.1 tron )
261 1.1 tron {
262 1.1 tron slap_overinst *on = (slap_overinst *) be->bd_info;
263 1.1 tron
264 1.1 tron /* initialize private structure to store configuration */
265 1.1 tron on->on_bi.bi_private = ch_calloc( 1, sizeof(lastbind_info) );
266 1.1 tron
267 1.1 tron return 0;
268 1.1 tron }
269 1.1 tron
270 1.1 tron static int
271 1.1 tron lastbind_db_close(
272 1.1 tron BackendDB *be,
273 1.1 tron ConfigReply *cr
274 1.1 tron )
275 1.1 tron {
276 1.1 tron slap_overinst *on = (slap_overinst *) be->bd_info;
277 1.1 tron lastbind_info *lbi = (lastbind_info *) on->on_bi.bi_private;
278 1.1 tron
279 1.1 tron /* free private structure to store configuration */
280 1.1 tron free( lbi );
281 1.1 tron
282 1.1 tron return 0;
283 1.1 tron }
284 1.1 tron
285 1.1 tron static slap_overinst lastbind;
286 1.1 tron
287 1.1 tron int lastbind_initialize()
288 1.1 tron {
289 1.1 tron int i, code;
290 1.1 tron
291 1.1 tron /* register operational schema for this overlay (authTimestamp attribute) */
292 1.1 tron for (i=0; lastBind_OpSchema[i].def; i++) {
293 1.1 tron code = register_at( lastBind_OpSchema[i].def, lastBind_OpSchema[i].ad, 0 );
294 1.1 tron if ( code ) {
295 1.1 tron Debug( LDAP_DEBUG_ANY,
296 1.1 tron "lastbind_initialize: register_at failed\n", 0, 0, 0 );
297 1.1 tron return code;
298 1.1 tron }
299 1.1 tron }
300 1.1 tron
301 1.1 tron ad_authTimestamp->ad_type->sat_flags |= SLAP_AT_MANAGEABLE;
302 1.1 tron
303 1.1 tron lastbind.on_bi.bi_type = "lastbind";
304 1.1 tron lastbind.on_bi.bi_db_init = lastbind_db_init;
305 1.1 tron lastbind.on_bi.bi_db_close = lastbind_db_close;
306 1.1 tron lastbind.on_bi.bi_op_bind = lastbind_bind;
307 1.1 tron
308 1.1 tron /* register configuration directives */
309 1.1 tron lastbind.on_bi.bi_cf_ocs = lastbindocs;
310 1.1 tron code = config_register_schema( lastbindcfg, lastbindocs );
311 1.1 tron if ( code ) return code;
312 1.1 tron
313 1.1 tron return overlay_register( &lastbind );
314 1.1 tron }
315 1.1 tron
316 1.1 tron #if SLAPD_OVER_LASTBIND == SLAPD_MOD_DYNAMIC
317 1.1 tron int init_module(int argc, char *argv[]) {
318 1.1 tron return lastbind_initialize();
319 1.1 tron }
320 1.1 tron #endif
321 1.1 tron
322 1.1 tron #endif /* defined(SLAPD_OVER_LASTBIND) */
323