]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/lastbind/lastbind.c
Allocate ConfigOID, use ISODE authTimestamp schema
[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_authTimestamp;
48
49 /* This is the definition used by ISODE, as supplied to us in
50  * ITS#6238 Followup #9
51  */
52 static struct schema_info {
53         char *def;
54         AttributeDescription **ad;
55 } lastBind_OpSchema[] = {
56         {       "( 1.3.6.1.4.1.453.16.2.188 "
57                 "NAME 'authTimestamp' "
58                 "DESC 'last successful authentication using any method/mech' "
59                 "EQUALITY generalizedTimeMatch "
60                 "ORDERING generalizedTimeOrderingMatch "
61                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
62                 "SINGLE-VALUE NO-USER-MODIFICATION USAGE dsaOperation )",
63                 &ad_authTimestamp},
64         { NULL, NULL }
65 };
66
67 /* configuration attribute and objectclass */
68 static ConfigTable lastbindcfg[] = {
69         { "lastbind-precision", "seconds", 2, 2, 0,
70           ARG_INT|ARG_OFFSET,
71           (void *)offsetof(lastbind_info, timestamp_precision),
72           "( OLcfgAt:5.1 "
73           "NAME 'olcLastBindPrecision' "
74           "DESC 'Precision of bindTimestamp attribute' "
75           "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
76         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
77 };
78
79 static ConfigOCs lastbindocs[] = {
80         { "( OLcfgOc:5.1 "
81           "NAME 'olcLastBindConfig' "
82           "DESC 'Last Bind configuration' "
83           "SUP olcOverlayConfig "
84           "MAY ( olcLastBindPrecision ) )",
85           Cft_Overlay, lastbindcfg, NULL, NULL },
86         { NULL, 0, NULL }
87 };
88
89 static time_t
90 parse_time( char *atm )
91 {
92         struct lutil_tm tm;
93         struct lutil_timet tt;
94         time_t ret = (time_t)-1;
95
96         if ( lutil_parsetime( atm, &tm ) == 0) {
97                 lutil_tm2time( &tm, &tt );
98                 ret = tt.tt_sec;
99         }
100         return ret;
101 }
102
103 static int
104 lastbind_bind_response( Operation *op, SlapReply *rs )
105 {
106         Modifications *mod = NULL;
107         BackendInfo *bi = op->o_bd->bd_info;
108         Entry *e;
109         int rc;
110
111         rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
112         op->o_bd->bd_info = bi;
113
114         if ( rc != LDAP_SUCCESS ) {
115                 return SLAP_CB_CONTINUE;
116         }
117
118         /* we're only interested if the bind was successful */
119         if ( rs->sr_err == LDAP_SUCCESS ) {
120                 lastbind_info *lbi = (lastbind_info *) op->o_callback->sc_private;
121
122                 time_t now, bindtime = (time_t)-1;
123                 Attribute *a;
124                 Modifications *m;
125                 char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ];
126                 struct berval timestamp;
127
128                 // get the current time
129                 now = slap_get_time();
130
131                 // get bindTimestamp attribute, if it exists
132                 if ((a = attr_find( e->e_attrs, ad_authTimestamp)) != NULL) {
133                         bindtime = parse_time( a->a_nvals[0].bv_val );
134
135                         if (bindtime != (time_t)-1) {
136                                 // if the recorded bind time is within our precision, we're done
137                                 // it doesn't need to be updated (save a write for nothing)
138                                 if ((now - bindtime) < lbi->timestamp_precision) {
139                                         goto done;
140                                 }
141                         }
142                 }
143                 
144                 // update the bindTimestamp in the user's entry with the current time
145                 timestamp.bv_val = nowstr;
146                 timestamp.bv_len = sizeof(nowstr);
147                 slap_timestamp( &now, &timestamp );
148
149                 m = ch_calloc( sizeof(Modifications), 1 );
150                 m->sml_op = LDAP_MOD_REPLACE;
151                 m->sml_flags = 0;
152                 m->sml_type = ad_authTimestamp->ad_cname;
153                 m->sml_desc = ad_authTimestamp;
154                 m->sml_numvals = 1;
155                 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
156                 m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
157
158                 ber_dupbv( &m->sml_values[0], &timestamp );
159                 ber_dupbv( &m->sml_nvalues[0], &timestamp );
160                 m->sml_next = mod;
161                 mod = m;
162         }
163
164 done:
165         be_entry_release_r( op, e );
166
167         // perform the update, if necessary
168         if ( mod ) {
169                 Operation op2 = *op;
170                 SlapReply r2 = { REP_RESULT };
171                 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
172
173                 /* FIXME: Need to handle replication of the operational attribute... 
174                  * See password policy overlay */
175                 op2.o_tag = LDAP_REQ_MODIFY;
176                 op2.o_callback = &cb;
177                 op2.orm_modlist = mod;
178                 op2.o_dn = op->o_bd->be_rootdn;
179                 op2.o_ndn = op->o_bd->be_rootndn;
180                 rc = op->o_bd->be_modify( &op2, &r2 );
181                 slap_mods_free( mod, 1 );
182         }
183
184         op->o_bd->bd_info = bi;
185         return SLAP_CB_CONTINUE;
186 }
187
188 static int
189 lastbind_bind( Operation *op, SlapReply *rs )
190 {
191         slap_callback *cb;
192         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
193
194         // setup a callback to intercept result of this bind operation
195         // and pass along the lastbind_info struct
196         cb = op->o_tmpcalloc( sizeof(slap_callback), 1, op->o_tmpmemctx );
197         cb->sc_response = lastbind_bind_response;
198         cb->sc_next = op->o_callback->sc_next;
199         cb->sc_private = on->on_bi.bi_private;
200         op->o_callback->sc_next = cb;
201
202         return SLAP_CB_CONTINUE;
203 }
204
205 static int
206 lastbind_db_init(
207         BackendDB *be,
208         ConfigReply *cr
209 )
210 {
211         slap_overinst *on = (slap_overinst *) be->bd_info;
212         
213         // initialize private structure to store configuration
214         on->on_bi.bi_private = ch_calloc( 1, sizeof(lastbind_info) );
215
216         return 0;
217 }
218
219 static int
220 lastbind_db_close(
221         BackendDB *be,
222         ConfigReply *cr
223 )
224 {
225         slap_overinst *on = (slap_overinst *) be->bd_info;
226         lastbind_info *lbi = (lastbind_info *) on->on_bi.bi_private;
227
228         // free private structure to store configuration
229         free( lbi );
230
231         return 0;
232 }
233
234 static slap_overinst lastbind;
235
236 int lastbind_initialize()
237 {
238         int i, code;
239
240         // register operational schema for this overlay (bindTimestamp attribute)
241         for (i=0; lastBind_OpSchema[i].def; i++) {
242                 code = register_at( lastBind_OpSchema[i].def, lastBind_OpSchema[i].ad, 0 );
243                 if ( code ) {
244                         Debug( LDAP_DEBUG_ANY,
245                                 "lastbind_initialize: register_at failed\n", 0, 0, 0 );
246                         return code;
247                 }
248         }
249
250         lastbind.on_bi.bi_type = "lastbind";
251         lastbind.on_bi.bi_db_init = lastbind_db_init;
252         lastbind.on_bi.bi_db_close = lastbind_db_close;
253         lastbind.on_bi.bi_op_bind = lastbind_bind;
254
255         // register configuration directives
256         lastbind.on_bi.bi_cf_ocs = lastbindocs;
257         code = config_register_schema( lastbindcfg, lastbindocs );
258         if ( code ) return code;
259
260         return overlay_register( &lastbind );
261 }
262
263 #if SLAPD_OVER_LASTBIND == SLAPD_MOD_DYNAMIC
264 int init_module(int argc, char *argv[]) {
265         return lastbind_initialize();
266 }
267 #endif
268
269 #endif  /* defined(SLAPD_OVER_LASTBIND) */