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