1 /* lastbind.c - Record timestamp of the last successful bind to entries */
4 * Copyright 2009 Jonathan Clarke <jonathan@phillipoux.net>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 * This work is loosely derived from the ppolicy overlay.
22 * This file implements an overlay that stores the timestamp of the
23 * last successful bind operation in a directory entry.
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.
30 #ifdef SLAPD_OVER_LASTBIND
37 #include <ac/string.h>
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 */
48 /* Operational attributes */
49 static AttributeDescription *ad_authTimestamp;
51 /* This is the definition used by ISODE, as supplied to us in
52 * ITS#6238 Followup #9
54 static struct schema_info {
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 )",
69 /* configuration attribute and objectclass */
70 static ConfigTable lastbindcfg[] = {
71 { "lastbind-precision", "seconds", 2, 2, 0,
73 (void *)offsetof(lastbind_info, timestamp_precision),
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 }
87 static ConfigOCs lastbindocs[] = {
89 "NAME 'olcLastBindConfig' "
90 "DESC 'Last Bind configuration' "
91 "SUP olcOverlayConfig "
92 "MAY ( olcLastBindPrecision $ olcLastBindForwardUpdates) )",
93 Cft_Overlay, lastbindcfg, NULL, NULL },
98 parse_time( char *atm )
101 struct lutil_timet tt;
102 time_t ret = (time_t)-1;
104 if ( lutil_parsetime( atm, &tm ) == 0) {
105 lutil_tm2time( &tm, &tt );
112 lastbind_bind_response( Operation *op, SlapReply *rs )
114 Modifications *mod = NULL;
115 BackendInfo *bi = op->o_bd->bd_info;
119 /* we're only interested if the bind was successful */
120 if ( rs->sr_err != LDAP_SUCCESS )
121 return SLAP_CB_CONTINUE;
123 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
124 op->o_bd->bd_info = bi;
126 if ( rc != LDAP_SUCCESS ) {
127 return SLAP_CB_CONTINUE;
131 lastbind_info *lbi = (lastbind_info *) op->o_callback->sc_private;
133 time_t now, bindtime = (time_t)-1;
136 char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ];
137 struct berval timestamp;
139 /* get the current time */
140 now = slap_get_time();
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 );
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) {
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, ×tamp );
160 m = ch_calloc( sizeof(Modifications), 1 );
161 m->sml_op = LDAP_MOD_REPLACE;
163 m->sml_type = ad_authTimestamp->ad_cname;
164 m->sml_desc = ad_authTimestamp;
166 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
167 m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
169 ber_dupbv( &m->sml_values[0], ×tamp );
170 ber_dupbv( &m->sml_nvalues[0], ×tamp );
176 be_entry_release_r( op, e );
178 /* perform the update, if necessary */
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;
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;
195 * Code for forwarding of updates adapted from ppolicy.c of slapo-ppolicy
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.
203 if ( SLAP_SHADOW( op->o_bd ) && lbi->forward_updates ) {
204 op2.o_bd = frontendDB;
206 /* Must use Relax control since these are no-user-mod */
207 op2.o_relax = SLAP_CONTROL_CRITICAL;
211 BER_BVZERO( &c.ldctl_value );
212 c.ldctl_iscritical = 1;
213 c.ldctl_oid = LDAP_CONTROL_RELAX;
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;
220 /* TODO: not sure what this does in slapo-ppolicy */
222 op2.o_bd->bd_info = (BackendInfo *)on->on_info;
226 rc = op->o_bd->be_modify( &op2, &r2 );
227 slap_mods_free( mod, 1 );
230 op->o_bd->bd_info = bi;
231 return SLAP_CB_CONTINUE;
235 lastbind_bind( Operation *op, SlapReply *rs )
238 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
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;
248 return SLAP_CB_CONTINUE;
257 slap_overinst *on = (slap_overinst *) be->bd_info;
259 /* initialize private structure to store configuration */
260 on->on_bi.bi_private = ch_calloc( 1, sizeof(lastbind_info) );
271 slap_overinst *on = (slap_overinst *) be->bd_info;
272 lastbind_info *lbi = (lastbind_info *) on->on_bi.bi_private;
274 /* free private structure to store configuration */
280 static slap_overinst lastbind;
282 int lastbind_initialize()
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 );
290 Debug( LDAP_DEBUG_ANY,
291 "lastbind_initialize: register_at failed\n", 0, 0, 0 );
296 ad_authTimestamp->ad_type->sat_flags |= SLAP_AT_MANAGEABLE;
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;
303 /* register configuration directives */
304 lastbind.on_bi.bi_cf_ocs = lastbindocs;
305 code = config_register_schema( lastbindcfg, lastbindocs );
306 if ( code ) return code;
308 return overlay_register( &lastbind );
311 #if SLAPD_OVER_LASTBIND == SLAPD_MOD_DYNAMIC
312 int init_module(int argc, char *argv[]) {
313 return lastbind_initialize();
317 #endif /* defined(SLAPD_OVER_LASTBIND) */