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