1 /* addpartial-overlay.c */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2004-2010 The OpenLDAP Foundation.
6 * Portions Copyright (C) 2004 Virginia Tech, David Hawes.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * http://www.OpenLDAP.org/license.html.
18 * This work was initially developed by David Hawes of Virginia Tech
19 * for inclusion in OpenLDAP Software.
23 * This is an OpenLDAP overlay that intercepts ADD requests, determines if a
24 * change has actually taken place for that record, and then performs a modify
25 * request for those values that have changed (modified, added, deleted). If
26 * the record has not changed in any way, it is ignored. If the record does not
27 * exist, the record falls through to the normal add mechanism. This overlay is
28 * useful for replicating from sources that are not LDAPs where it is easier to
29 * build entire records than to determine the changes (i.e. a database).
35 static int collect_error_msg_cb( Operation *op, SlapReply *rs);
37 static slap_overinst addpartial;
40 * The meat of the overlay. Search for the record, determine changes, take
41 * action or fall through.
43 static int addpartial_add( Operation *op, SlapReply *rs)
46 SlapReply nrs = { REP_RESULT };
49 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
52 toAdd = op->oq_add.rs_e;
54 Debug(LDAP_DEBUG_TRACE, "%s: toAdd->e_nname.bv_val: %s\n",
55 addpartial.on_bi.bi_type, toAdd->e_nname.bv_val,0);
57 /* if the user doesn't have access, fall through to the normal ADD */
58 if(!access_allowed(op, toAdd, slap_schema.si_ad_entry,
59 NULL, ACL_WRITE, NULL))
61 return SLAP_CB_CONTINUE;
64 rc = overlay_entry_get_ov(&nop, &nop.o_req_ndn, NULL, NULL, 0, &found, on);
66 if(rc != LDAP_SUCCESS)
68 Debug(LDAP_DEBUG_TRACE,
69 "%s: no entry found, falling through to normal add\n",
70 addpartial.on_bi.bi_type, 0, 0);
71 return SLAP_CB_CONTINUE;
75 Debug(LDAP_DEBUG_TRACE, "%s: found the dn\n", addpartial.on_bi.bi_type,
80 Attribute *attr = NULL;
83 Modifications *mods = NULL;
84 Modifications **modtail = &mods;
85 Modifications *mod = NULL;
87 Debug(LDAP_DEBUG_TRACE, "%s: have an entry!\n",
88 addpartial.on_bi.bi_type,0,0);
90 /* determine if the changes are in the found entry */
91 for(attr = toAdd->e_attrs; attr; attr = attr->a_next)
93 if(attr->a_desc->ad_type->sat_atype.at_usage != 0) continue;
95 at = attr_find(found->e_attrs, attr->a_desc);
98 Debug(LDAP_DEBUG_TRACE, "%s: Attribute %s not found!\n",
99 addpartial.on_bi.bi_type,
100 attr->a_desc->ad_cname.bv_val,0);
101 mod = (Modifications *) ch_malloc(sizeof(
104 mod->sml_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
105 mod->sml_op &= LDAP_MOD_OP;
106 mod->sml_next = NULL;
107 mod->sml_desc = attr->a_desc;
108 mod->sml_type = attr->a_desc->ad_cname;
109 mod->sml_values = attr->a_vals;
110 mod->sml_nvalues = attr->a_nvals;
111 mod->sml_numvals = attr->a_numvals;
113 modtail = &mod->sml_next;
117 MatchingRule *mr = attr->a_desc->ad_type->sat_equality;
121 Debug(LDAP_DEBUG_TRACE, "%s: Attribute %s found\n",
122 addpartial.on_bi.bi_type,
123 attr->a_desc->ad_cname.bv_val,0);
125 for(bv = attr->a_vals, acount = 0; bv->bv_val != NULL;
128 /* count num values for attr */
130 for(bv = at->a_vals, bcount = 0; bv->bv_val != NULL;
133 /* count num values for attr */
137 Debug(LDAP_DEBUG_TRACE, "%s: acount != bcount, %s\n",
138 addpartial.on_bi.bi_type,
140 mod = (Modifications *) ch_malloc(sizeof(
143 mod->sml_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
144 mod->sml_op &= LDAP_MOD_OP;
145 mod->sml_next = NULL;
146 mod->sml_desc = attr->a_desc;
147 mod->sml_type = attr->a_desc->ad_cname;
148 mod->sml_values = attr->a_vals;
149 mod->sml_nvalues = attr->a_nvals;
150 mod->sml_numvals = attr->a_numvals;
152 modtail = &mod->sml_next;
156 for(bv = attr->a_vals; bv->bv_val != NULL; bv++)
161 for(v = at->a_vals; v->bv_val != NULL; v++)
164 if(mr && ((r = value_match(&ret, attr->a_desc, mr,
165 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
166 bv, v, &text)) == 0))
173 Debug(LDAP_DEBUG_TRACE,
174 "%s: \tvalue DNE, r: %d \n",
175 addpartial.on_bi.bi_type,
177 ret = strcmp(bv->bv_val, v->bv_val);
185 Debug(LDAP_DEBUG_TRACE,
186 "%s: \tvalue %s exists, ret: %d\n",
187 addpartial.on_bi.bi_type, bv->bv_val, ret);
191 Debug(LDAP_DEBUG_TRACE,
192 "%s: \tvalue %s DNE, ret: %d\n",
193 addpartial.on_bi.bi_type, bv->bv_val, ret);
194 mod = (Modifications *) ch_malloc(sizeof(
197 mod->sml_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
198 mod->sml_op &= LDAP_MOD_OP;
199 mod->sml_next = NULL;
200 mod->sml_desc = attr->a_desc;
201 mod->sml_type = attr->a_desc->ad_cname;
202 mod->sml_values = attr->a_vals;
203 mod->sml_nvalues = attr->a_nvals;
204 mod->sml_numvals = attr->a_numvals;
206 modtail = &mod->sml_next;
213 /* determine if any attributes were deleted */
214 for(attr = found->e_attrs; attr; attr = attr->a_next)
216 if(attr->a_desc->ad_type->sat_atype.at_usage != 0) continue;
219 at = attr_find(toAdd->e_attrs, attr->a_desc);
222 Debug(LDAP_DEBUG_TRACE,
223 "%s: Attribute %s not found in new entry!!!\n",
224 addpartial.on_bi.bi_type,
225 attr->a_desc->ad_cname.bv_val, 0);
226 mod = (Modifications *) ch_malloc(sizeof(
229 mod->sml_op = LDAP_MOD_REPLACE;
230 mod->sml_next = NULL;
231 mod->sml_desc = attr->a_desc;
232 mod->sml_type = attr->a_desc->ad_cname;
233 mod->sml_values = NULL;
234 mod->sml_nvalues = NULL;
235 mod->sml_numvals = 0;
237 modtail = &mod->sml_next;
241 Debug(LDAP_DEBUG_TRACE,
242 "%s: Attribute %s found in new entry\n",
243 addpartial.on_bi.bi_type,
244 at->a_desc->ad_cname.bv_val, 0);
248 overlay_entry_release_ov(&nop, found, 0, on);
252 Modifications *m = NULL;
253 Modifications *toDel;
255 slap_callback nullcb = { NULL, collect_error_msg_cb,
258 Debug(LDAP_DEBUG_TRACE, "%s: mods to do...\n",
259 addpartial.on_bi.bi_type, 0, 0);
261 memset(&nrs, 0, sizeof(nrs));
262 nrs.sr_type = REP_RESULT;
263 nrs.sr_err = LDAP_SUCCESS;
267 nop.o_tag = LDAP_REQ_MODIFY;
268 nop.orm_modlist = mods;
269 nop.orm_no_opattrs = 0;
270 nop.o_callback = &nullcb;
271 nop.o_bd->bd_info = (BackendInfo *) on->on_info;
273 for(m = mods, modcount = 0; m; m = m->sml_next,
276 /* count number of mods */
279 Debug(LDAP_DEBUG_TRACE, "%s: number of mods: %d\n",
280 addpartial.on_bi.bi_type, modcount, 0);
282 if(nop.o_bd->be_modify)
284 rc = (nop.o_bd->be_modify)(&nop, &nrs);
287 if(rc == LDAP_SUCCESS)
289 Debug(LDAP_DEBUG_TRACE,
290 "%s: modify successful\n",
291 addpartial.on_bi.bi_type, 0, 0);
295 Debug(LDAP_DEBUG_TRACE, "%s: modify unsuccessful: %d\n",
296 addpartial.on_bi.bi_type, rc, 0);
298 if(nullcb.sc_private)
300 rs->sr_text = nullcb.sc_private;
304 Debug(LDAP_DEBUG_TRACE, "%s: freeing mods...\n",
305 addpartial.on_bi.bi_type, 0, 0);
307 for(toDel = mods; toDel; toDel = mods)
309 mods = mods->sml_next;
315 Debug(LDAP_DEBUG_TRACE, "%s: no mods to process\n",
316 addpartial.on_bi.bi_type, 0, 0);
321 Debug(LDAP_DEBUG_TRACE, "%s: no entry!\n",
322 addpartial.on_bi.bi_type, 0, 0);
325 op->o_callback = NULL;
326 send_ldap_result( op, rs );
327 ch_free((void *)rs->sr_text);
334 static int collect_error_msg_cb( Operation *op, SlapReply *rs)
338 op->o_callback->sc_private = (void *) ch_strdup(rs->sr_text);
344 int addpartial_init()
346 addpartial.on_bi.bi_type = "addpartial";
347 addpartial.on_bi.bi_op_add = addpartial_add;
349 return (overlay_register(&addpartial));
352 int init_module(int argc, char *argv[])
354 return addpartial_init();