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