]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/lastbind/lastbind.c
ITS#6238 from Jonathan Clarke. Needs cleanup.
[openldap] / contrib / slapd-modules / lastbind / lastbind.c
1 /* lastbind.c - Record timestamp of the last successful bind to entries */
2 /* 
3  * Copyright 2009 Jonathan Clarke <jonathan@phillipoux.net>.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted only as authorized by the OpenLDAP
8  * Public License.
9  *
10  * A copy of this license is available in the file LICENSE in the
11  * top-level directory of the distribution or, alternatively, at
12  * <http://www.OpenLDAP.org/license.html>.
13  */
14 /* ACKNOWLEDGEMENTS:
15  * This work is loosely derived from the ppolicy overlay.
16  */
17
18 #include "portable.h"
19
20 /*
21  *This file implements an overlay that stores the timestamp of the
22  * last successful bind operation in a directory entry.
23  * 
24  * Optimization: to avoid performing a write on each bind,
25  * a precision for this timestamp may be configured, causing it to
26  * only be updated if it is older than a given number of seconds.
27  */
28
29 #ifdef SLAPD_OVER_LASTBIND
30
31 #include <ldap.h>
32 #include "lutil.h"
33 #include "slap.h"
34 #include <ac/errno.h>
35 #include <ac/time.h>
36 #include <ac/string.h>
37 #include <ac/ctype.h>
38 #include "config.h"
39
40 // Per-instance configuration information
41 typedef struct lastbind_info {
42         // precision to update timestamp in bindTimestamp attribute
43         int timestamp_precision;
44 } lastbind_info;
45
46 // Operational attributes
47 static AttributeDescription *ad_bindTimestamp;
48
49 // TODO: use a real OID
50 #define BASE_OID_AT "OLcfgCtAt:99"
51 #define BASE_OID_OC "OLcfgCtOc:99"
52 static struct schema_info {
53         char *def;
54         AttributeDescription **ad;
55 } lastBind_OpSchema[] = {
56         {       "( "
57                 BASE_OID_AT
58                 ".1 "
59                 "NAME ( 'bindTimestamp' ) "
60                 "DESC 'The time the last successful bind occured' "
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 directoryOperation )",
65                 &ad_bindTimestamp},
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           "( "
75           BASE_OID_AT
76           ".2 "
77           "NAME 'olcLastBindPrecision' "
78           "DESC 'Precision of bindTimestamp attribute' "
79           "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
80         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
81 };
82
83 static ConfigOCs lastbindocs[] = {
84         { "( "
85           BASE_OID_OC
86           ".1 "
87           "NAME 'olcLastBindConfig' "
88           "DESC 'Last Bind configuration' "
89           "SUP olcOverlayConfig "
90           "MAY ( olcLastBindPrecision ) )",
91           Cft_Overlay, lastbindcfg, NULL, NULL },
92         { NULL, 0, NULL }
93 };
94
95 static time_t
96 parse_time( char *atm )
97 {
98         struct lutil_tm tm;
99         struct lutil_timet tt;
100         time_t ret = (time_t)-1;
101
102         if ( lutil_parsetime( atm, &tm ) == 0) {
103                 lutil_tm2time( &tm, &tt );
104                 ret = tt.tt_sec;
105         }
106         return ret;
107 }
108
109 static int
110 lastbind_bind_response( Operation *op, SlapReply *rs )
111 {
112         Modifications *mod = NULL;
113         BackendInfo *bi = op->o_bd->bd_info;
114         Entry *e;
115         int rc;
116
117         rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
118         op->o_bd->bd_info = bi;
119
120         if ( rc != LDAP_SUCCESS ) {
121                 return SLAP_CB_CONTINUE;
122         }
123
124         // we're only interested if the bind was successful
125         if ( rs->sr_err == LDAP_SUCCESS ) {
126                 lastbind_info *lbi = (lastbind_info *) op->o_callback->sc_private;
127
128                 time_t now, bindtime = (time_t)-1;
129                 Attribute *a;
130                 Modifications *m;
131                 char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ];
132                 struct berval timestamp;
133
134                 // get the current time
135                 now = slap_get_time();
136
137                 // get bindTimestamp attribute, if it exists
138                 if ((a = attr_find( e->e_attrs, ad_bindTimestamp)) != NULL) {
139                         bindtime = parse_time( a->a_nvals[0].bv_val );
140
141                         if (bindtime != (time_t)-1) {
142                                 // if the recorded bind time is within our precision, we're done
143                                 // it doesn't need to be updated (save a write for nothing)
144                                 if ((now - bindtime) < lbi->timestamp_precision) {
145                                         goto done;
146                                 }
147                         }
148                 }
149                 
150                 // update the bindTimestamp in the user's entry with the current time
151                 timestamp.bv_val = nowstr;
152                 timestamp.bv_len = sizeof(nowstr);
153                 slap_timestamp( &now, &timestamp );
154
155                 m = ch_calloc( sizeof(Modifications), 1 );
156                 m->sml_op = LDAP_MOD_REPLACE;
157                 m->sml_flags = 0;
158                 m->sml_type = ad_bindTimestamp->ad_cname;
159                 m->sml_desc = ad_bindTimestamp;
160                 m->sml_numvals = 1;
161                 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
162                 m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
163
164                 ber_dupbv( &m->sml_values[0], &timestamp );
165                 ber_dupbv( &m->sml_nvalues[0], &timestamp );
166                 m->sml_next = mod;
167                 mod = m;
168         }
169
170 done:
171         be_entry_release_r( op, e );
172
173         // perform the update, if necessary
174         if ( mod ) {
175                 Operation op2 = *op;
176                 SlapReply r2 = { REP_RESULT };
177                 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
178
179                 /* FIXME: Need to handle replication of the operational attribute... 
180                  * See password policy overlay */
181                 op2.o_tag = LDAP_REQ_MODIFY;
182                 op2.o_callback = &cb;
183                 op2.orm_modlist = mod;
184                 op2.o_dn = op->o_bd->be_rootdn;
185                 op2.o_ndn = op->o_bd->be_rootndn;
186                 rc = op->o_bd->be_modify( &op2, &r2 );
187                 slap_mods_free( mod, 1 );
188         }
189
190         op->o_bd->bd_info = bi;
191         return SLAP_CB_CONTINUE;
192 }
193
194 static int
195 lastbind_bind( Operation *op, SlapReply *rs )
196 {
197         slap_callback *cb;
198         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
199
200         // setup a callback to intercept result of this bind operation
201         // and pass along the lastbind_info struct
202         cb = op->o_tmpcalloc( sizeof(slap_callback), 1, op->o_tmpmemctx );
203         cb->sc_response = lastbind_bind_response;
204         cb->sc_next = op->o_callback->sc_next;
205         cb->sc_private = on->on_bi.bi_private;
206         op->o_callback->sc_next = cb;
207
208         return SLAP_CB_CONTINUE;
209 }
210
211 static int
212 lastbind_db_init(
213         BackendDB *be,
214         ConfigReply *cr
215 )
216 {
217         slap_overinst *on = (slap_overinst *) be->bd_info;
218         
219         // initialize private structure to store configuration
220         on->on_bi.bi_private = ch_calloc( 1, sizeof(lastbind_info) );
221
222         return 0;
223 }
224
225 static int
226 lastbind_db_close(
227         BackendDB *be,
228         ConfigReply *cr
229 )
230 {
231         slap_overinst *on = (slap_overinst *) be->bd_info;
232         lastbind_info *lbi = (lastbind_info *) on->on_bi.bi_private;
233
234         // free private structure to store configuration
235         free( lbi );
236
237         return 0;
238 }
239
240 static slap_overinst lastbind;
241
242 int lastbind_initialize()
243 {
244         int i, code;
245
246         // register operational schema for this overlay (bindTimestamp attribute)
247         for (i=0; lastBind_OpSchema[i].def; i++) {
248                 code = register_at( lastBind_OpSchema[i].def, lastBind_OpSchema[i].ad, 0 );
249                 if ( code ) {
250                         Debug( LDAP_DEBUG_ANY,
251                                 "lastbind_initialize: register_at failed\n", 0, 0, 0 );
252                         return code;
253                 }
254         }
255
256         lastbind.on_bi.bi_type = "lastbind";
257         lastbind.on_bi.bi_db_init = lastbind_db_init;
258         lastbind.on_bi.bi_db_close = lastbind_db_close;
259         lastbind.on_bi.bi_op_bind = lastbind_bind;
260
261         // register configuration directives
262         lastbind.on_bi.bi_cf_ocs = lastbindocs;
263         code = config_register_schema( lastbindcfg, lastbindocs );
264         if ( code ) return code;
265
266         return overlay_register( &lastbind );
267 }
268
269 #if SLAPD_OVER_LASTBIND == SLAPD_MOD_DYNAMIC
270 int init_module(int argc, char *argv[]) {
271         return lastbind_initialize();
272 }
273 #endif
274
275 #endif  /* defined(SLAPD_OVER_LASTBIND) */