]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/usn/usn.c
Happy new year (belated)
[openldap] / contrib / slapd-modules / usn / usn.c
1 /* usn.c - Maintain Microsoft-style Update Sequence Numbers */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2007-2014 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Howard Chu for inclusion in
18  * OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #ifdef SLAPD_OVER_USN
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/socket.h>
29
30 #include "slap.h"
31 #include "config.h"
32
33 /* This overlay intercepts write operations and adds a Microsoft-style
34  * USN to the target entry.
35  */
36
37 typedef struct usn_info {
38         int ui_current;
39         ldap_pvt_thread_mutex_t ui_mutex;
40 } usn_info_t;
41
42 static AttributeDescription *ad_usnCreated, *ad_usnChanged;
43
44 static struct {
45         char *desc;
46         AttributeDescription **adp;
47 } as[] = {
48         { "( 1.2.840.113556.1.2.19 "
49             "NAME 'uSNCreated' "
50             "SYNTAX '1.2.840.113556.1.4.906' "
51                 "SINGLE-VALUE "
52                 "NO-USER-MODIFICATION )",
53                 &ad_usnCreated },
54         { "( 1.2.840.113556.1.2.120 "
55                 "NAME 'uSNChanged' "
56                 "SYNTAX '1.2.840.113556.1.4.906' "
57                 "SINGLE-VALUE "
58                 "NO-USER-MODIFICATION )",
59                 &ad_usnChanged },
60         { NULL }
61 };
62
63 static int
64 usn_func( Operation *op, SlapReply *rs )
65 {
66         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
67         usn_info_t              *ui = on->on_bi.bi_private;
68         int my_usn;
69         char intbuf[64];
70         struct berval bv[2];
71
72         ldap_pvt_thread_mutex_lock( &ui->ui_mutex );
73         ui->ui_current++;
74         my_usn = ui->ui_current;
75         ldap_pvt_thread_mutex_unlock( &ui->ui_mutex );
76
77         BER_BVZERO(&bv[1]);
78         bv[0].bv_val = intbuf;
79         bv[0].bv_len = snprintf( intbuf, sizeof(intbuf), "%d", my_usn );
80         switch(op->o_tag) {
81         case LDAP_REQ_ADD:
82                 attr_merge( op->ora_e, ad_usnCreated, bv, NULL );
83                 attr_merge( op->ora_e, ad_usnChanged, bv, NULL );
84                 break;
85         case LDAP_REQ_DELETE:
86                 /* Probably need to update root usnLastObjRem */
87                 break;
88         default: {
89                 /* Modify, ModDN */
90                 Modifications *ml, *mod = ch_calloc( sizeof( Modifications ), 1 );
91                 for ( ml = op->orm_modlist; ml && ml->sml_next; ml = ml->sml_next );
92                 ml->sml_next = mod;
93                 mod->sml_desc = ad_usnChanged;
94                 mod->sml_numvals = 1;
95                 value_add_one( &mod->sml_values, &bv[0] );
96                 mod->sml_nvalues = NULL;
97                 mod->sml_op = LDAP_MOD_REPLACE;
98                 mod->sml_flags = 0;
99                 mod->sml_next = NULL;
100                 break;
101                 }
102         }
103         return SLAP_CB_CONTINUE;
104 }
105
106 static int
107 usn_operational(
108         Operation *op,
109         SlapReply *rs )
110 {
111         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
112         usn_info_t              *ui = (usn_info_t *)on->on_bi.bi_private;
113
114         if ( rs->sr_entry &&
115                 dn_match( &rs->sr_entry->e_nname, op->o_bd->be_nsuffix )) {
116
117                 if ( SLAP_OPATTRS( rs->sr_attr_flags ) ||
118                         ad_inlist( ad_usnChanged, rs->sr_attrs )) {
119                         Attribute *a, **ap = NULL;
120                         char intbuf[64];
121                         struct berval bv;
122                         int my_usn;
123
124                         for ( a=rs->sr_entry->e_attrs; a; a=a->a_next ) {
125                                 if ( a->a_desc == ad_usnChanged )
126                                         break;
127                         }
128
129                         if ( !a ) {
130                                 for ( ap = &rs->sr_operational_attrs; *ap;
131                                         ap=&(*ap)->a_next );
132
133                                         a = attr_alloc( ad_usnChanged );
134                                         *ap = a;
135                                 }
136
137                         if ( !ap ) {
138                                 if ( rs_entry2modifiable( op,rs, on )) {
139                                         a = attr_find( rs->sr_entry->e_attrs,
140                                                 ad_usnChanged );
141                                 }
142                                 ber_bvarray_free( a->a_vals );
143                                 a->a_vals = NULL;
144                                 a->a_numvals = 0;
145                         }
146                         ldap_pvt_thread_mutex_lock( &ui->ui_mutex );
147                         my_usn = ui->ui_current;
148                         ldap_pvt_thread_mutex_unlock( &ui->ui_mutex );
149                         bv.bv_len = snprintf( intbuf, sizeof(intbuf), "%d", my_usn );
150                         bv.bv_val = intbuf;
151                         attr_valadd( a, &bv, NULL, 1 );
152                 }
153         }
154         return SLAP_CB_CONTINUE;
155 }
156
157 /* Read the old USN from the underlying DB. This code is
158  * stolen from the syncprov overlay.
159  */
160 static int
161 usn_db_open(
162         BackendDB *be,
163         ConfigReply *cr)
164 {
165         slap_overinst   *on = (slap_overinst *) be->bd_info;
166         usn_info_t *ui = (usn_info_t *)on->on_bi.bi_private;
167
168         Connection conn = { 0 };
169         OperationBuffer opbuf;
170         Operation *op;
171         Entry *e = NULL;
172         Attribute *a;
173         int rc;
174         void *thrctx = NULL;
175
176         thrctx = ldap_pvt_thread_pool_context();
177         connection_fake_init( &conn, &opbuf, thrctx );
178         op = &opbuf.ob_op;
179         op->o_bd = be;
180         op->o_dn = be->be_rootdn;
181         op->o_ndn = be->be_rootndn;
182
183         rc = overlay_entry_get_ov( op, be->be_nsuffix, NULL,
184                 slap_schema.si_ad_contextCSN, 0, &e, on );
185
186         if ( e ) {
187                 a = attr_find( e->e_attrs, ad_usnChanged );
188                 if ( a ) {
189                         ui->ui_current = atoi( a->a_vals[0].bv_val );
190                 }
191                 overlay_entry_release_ov( op, e, 0, on );
192         }
193         return 0;
194 }
195
196 static int
197 usn_db_init(
198         BackendDB *be,
199         ConfigReply *cr
200 )
201 {
202         slap_overinst   *on = (slap_overinst *)be->bd_info;
203         usn_info_t      *ui;
204
205         if ( SLAP_ISGLOBALOVERLAY( be ) ) {
206                 Debug( LDAP_DEBUG_ANY,
207                         "usn must be instantiated within a database.\n",
208                         0, 0, 0 );
209                 return 1;
210         }
211
212         ui = ch_calloc(1, sizeof(usn_info_t));
213         ldap_pvt_thread_mutex_init( &ui->ui_mutex );
214         on->on_bi.bi_private = ui;
215         return 0;
216 }
217
218 static int
219 usn_db_close(
220         BackendDB *be,
221         ConfigReply *cr
222 )
223 {
224         slap_overinst   *on = (slap_overinst *)be->bd_info;
225         usn_info_t      *ui = on->on_bi.bi_private;
226         Connection conn = {0};
227         OperationBuffer opbuf;
228         Operation *op;
229         SlapReply rs = {REP_RESULT};
230         void *thrctx;
231
232         Modifications mod;
233         slap_callback cb = {0};
234         char intbuf[64];
235         struct berval bv[2];
236
237         thrctx = ldap_pvt_thread_pool_context();
238         connection_fake_init( &conn, &opbuf, thrctx );
239         op = &opbuf.ob_op;
240         op->o_bd = be;
241         BER_BVZERO( &bv[1] );
242         bv[0].bv_len = snprintf( intbuf, sizeof(intbuf), "%d", ui->ui_current );
243         bv[0].bv_val = intbuf;
244         mod.sml_numvals = 1;
245         mod.sml_values = bv;
246         mod.sml_nvalues = NULL;
247         mod.sml_desc = ad_usnChanged;
248         mod.sml_op = LDAP_MOD_REPLACE;
249         mod.sml_flags = 0;
250         mod.sml_next = NULL;
251
252         cb.sc_response = slap_null_cb;
253         op->o_tag = LDAP_REQ_MODIFY;
254         op->o_callback = &cb;
255         op->orm_modlist = &mod;
256         op->orm_no_opattrs = 1;
257         op->o_dn = be->be_rootdn;
258         op->o_ndn = be->be_rootndn;
259         op->o_req_dn = op->o_bd->be_suffix[0];
260         op->o_req_ndn = op->o_bd->be_nsuffix[0];
261         op->o_bd->bd_info = on->on_info->oi_orig;
262         op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
263         op->o_no_schema_check = 1;
264         op->o_bd->be_modify( op, &rs );
265         if ( mod.sml_next != NULL ) {
266                 slap_mods_free( mod.sml_next, 1 );
267         }
268         return 0;
269 }
270
271 static int
272 usn_db_destroy(
273         BackendDB *be,
274         ConfigReply *cr
275 )
276 {
277         slap_overinst   *on = (slap_overinst *)be->bd_info;
278         usn_info_t      *ui = on->on_bi.bi_private;
279
280         ldap_pvt_thread_mutex_destroy( &ui->ui_mutex );
281         ch_free( ui );
282         on->on_bi.bi_private = NULL;
283         return 0;
284 }
285
286 /* This overlay is set up for dynamic loading via moduleload. For static
287  * configuration, you'll need to arrange for the slap_overinst to be
288  * initialized and registered by some other function inside slapd.
289  */
290
291 static slap_overinst usn;
292
293 int
294 usn_init( void )
295 {
296         int i, code;
297
298         memset( &usn, 0, sizeof( slap_overinst ) );
299         usn.on_bi.bi_type = "usn";
300         usn.on_bi.bi_db_init = usn_db_init;
301         usn.on_bi.bi_db_destroy = usn_db_destroy;
302         usn.on_bi.bi_db_open = usn_db_open;
303         usn.on_bi.bi_db_close = usn_db_close;
304
305         usn.on_bi.bi_op_modify = usn_func;
306         usn.on_bi.bi_op_modrdn = usn_func;
307         usn.on_bi.bi_op_add = usn_func;
308         usn.on_bi.bi_op_delete = usn_func;
309         usn.on_bi.bi_operational = usn_operational;
310
311         for ( i = 0; as[i].desc; i++ ) {
312                 code = register_at( as[i].desc, as[i].adp, 0 );
313                 if ( code ) {
314                         Debug( LDAP_DEBUG_ANY,
315                                 "usn_init: register_at #%d failed\n", i, 0, 0 );
316                         return code;
317                 }
318         }
319         return overlay_register( &usn );
320 }
321
322 #if SLAPD_OVER_USN == SLAPD_MOD_DYNAMIC
323 int
324 init_module( int argc, char *argv[] )
325 {
326         return usn_init();
327 }
328 #endif /* SLAPD_OVER_USN == SLAPD_MOD_DYNAMIC */
329
330 #endif /* defined(SLAPD_OVER_USN) */