1 /* unique.c - attribute uniqueness module */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2004 The OpenLDAP Foundation.
6 * Portions Copyright 2004 Symas Corporation.
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 the 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 Symas Corp. for inclusion in
19 * OpenLDAP Software. This work was sponsored by Hewlett-Packard.
24 #ifdef SLAPD_OVER_UNIQUE
28 #include <ac/string.h>
29 #include <ac/socket.h>
33 static slap_overinst unique;
35 typedef struct unique_attrs_s {
36 struct unique_attrs_s *next; /* list of attrs */
37 AttributeDescription *attr;
40 typedef struct unique_data_s {
41 const char *message; /* breadcrumbs */
42 struct unique_attrs_s *attrs; /* list of known attrs */
43 struct unique_attrs_s *ignore; /* list of ignored attrs */
44 BerValue dn; /* base of "unique tree" */
45 char strict; /* null considered unique too */
48 typedef struct unique_counter_s {
53 ** allocate new unique_data;
54 ** initialize, copy basedn;
55 ** store in on_bi.bi_private;
59 static int unique_db_init(
63 slap_overinst *on = (slap_overinst *)be->bd_info;
64 unique_data *ud = ch_malloc(sizeof(unique_data));
66 /* Debug(LDAP_DEBUG_TRACE, "==> unique_init\n", 0, 0, 0); */
68 ud->message = "_init";
73 /* default to the base of our configured database */
74 ber_dupbv(&ud->dn, &be->be_nsuffix[0]);
75 on->on_bi.bi_private = ud;
82 ** if command = attributes:
84 ** convert to attribute;
85 ** add to configured attribute list;
86 ** elseif command = base:
87 ** set our basedn to argument;
88 ** else complain about invalid directive;
92 static int unique_config(
100 slap_overinst *on = (slap_overinst *) be->bd_info;
101 unique_data *ud = on->on_bi.bi_private;
104 AttributeDescription *ad;
107 ud->message = "_config";
108 Debug(LDAP_DEBUG_TRACE, "==> unique_config\n", 0, 0, 0);
110 if(!strcasecmp(*argv, "unique_attributes") ||
111 !strcasecmp(*argv, "unique_ignore")) {
112 for(i = 1; i < argc; i++) {
113 for(up = ud->attrs; up; up = up->next)
114 if(!strcmp(argv[i], up->attr->ad_cname.bv_val)) {
115 Debug(LDAP_DEBUG_ANY,
116 "%s: line %d: duplicate attribute <s>, ignored\n",
117 fname, lineno, argv[i]);
121 if(slap_str2ad(argv[i], &ad, &text) != LDAP_SUCCESS) {
122 Debug(LDAP_DEBUG_ANY,
123 "%s: line %d: bad attribute <%s>, ignored\n",
124 fname, lineno, text);
126 } else if(ad->ad_next) {
127 Debug(LDAP_DEBUG_ANY,
128 "%s: line %d: multiple attributes match <%s>, ignored\n",
129 fname, lineno, argv[i]);
132 up = ch_malloc(sizeof(unique_attrs));
134 if(!strcasecmp(*argv, "unique_ignore")) {
135 up->next = ud->ignore;
138 up->next = ud->attrs;
141 Debug(LDAP_DEBUG_ANY, "%s: line %d: new attribute <%s>\n",
142 fname, lineno, argv[i]);
144 } else if(!strcasecmp(*argv, "unique_strict")) {
146 } else if(!strcasecmp(*argv, "unique_base")) {
148 ber_str2bv( argv[1], 0, 0, &bv );
149 ch_free(ud->dn.bv_val);
150 dnNormalize(0, NULL, NULL, &bv, &ud->dn, NULL);
151 Debug(LDAP_DEBUG_ANY, "%s: line %d: new base dn <%s>\n",
152 fname, lineno, argv[1]);
154 return(SLAP_CONF_UNKNOWN);
162 ** mostly, just print the init message;
171 slap_overinst *on = (slap_overinst *)be->bd_info;
172 unique_data *ud = on->on_bi.bi_private;
173 ud->message = "_open";
175 Debug(LDAP_DEBUG_TRACE, "unique_open: overlay initialized\n", 0, 0, 0);
182 ** foreach configured attribute:
185 ** (do not) free ud->message;
186 ** reset on_bi.bi_private;
187 ** free our config data;
196 slap_overinst *on = (slap_overinst *) be->bd_info;
197 unique_data *ud = on->on_bi.bi_private;
198 unique_attrs *ii, *ij;
199 ud->message = "_close";
201 Debug(LDAP_DEBUG_TRACE, "==> unique_close\n", 0, 0, 0);
203 for(ii = ud->attrs; ii; ii = ij) {
208 for(ii = ud->ignore; ii; ii = ij) {
213 ch_free(ud->dn.bv_val);
215 on->on_bi.bi_private = NULL; /* XXX */
225 ** if this is a REP_SEARCH, count++;
229 static int count_attr_cb(
234 /* because you never know */
235 if(!op || !rs) return(0);
237 /* Only search entries are interesting */
238 if(rs->sr_type != REP_SEARCH) return(0);
240 Debug(LDAP_DEBUG_TRACE, "==> count_attr_cb <%s>\n",
241 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "UNKNOWN_DN", 0, 0);
243 ((unique_counter*)op->o_callback->sc_private)->count++;
248 /* XXX extraneous (slap_response*) to avoid compiler warning */
250 static int unique_add(
256 SlapReply nrs = { REP_RESULT };
257 slap_callback cb = { NULL, NULL, NULL, NULL }; /* XXX */
258 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
265 unique_counter uq = { 0 };
266 unique_data *ud = on->on_bi.bi_private;
268 Debug(LDAP_DEBUG_TRACE, "==> unique_add <%s>\n", op->o_req_dn.bv_val, 0, 0);
270 /* validate backend. Should have already been done, but whatever */
271 nop.o_bd = select_backend(&ud->dn, 0, 1);
273 if (!nop.o_bd->be_search) {
274 op->o_bd->bd_info = (BackendInfo *) on->on_info;
275 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
276 "backend missing search function");
280 op->o_bd->bd_info = (BackendInfo *) on->on_info;
281 send_ldap_error(op, rs, LDAP_OTHER,
282 "no known backend? this shouldn't be happening!");
287 ** count everything first;
288 ** allocate some memory;
289 ** write the search key;
293 if(!(a = op->ora_e->e_attrs)) {
294 op->o_bd->bd_info = (BackendInfo *) on->on_info;
295 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
296 "unique_add() got null op.ora_e.e_attrs");
298 } else for(; a; a = a->a_next) {
299 if(is_at_operational(a->a_desc->ad_type)) continue;
301 for(up = ud->ignore; up; up = up->next)
302 if(a->a_desc == up->attr) break;
306 for(up = ud->attrs; up; up = up->next)
307 if(a->a_desc == up->attr) break;
310 if((b = a->a_vals) && b[0].bv_val) for(i = 0; b[i].bv_val; i++)
311 ks += b[i].bv_len + a->a_desc->ad_cname.bv_len + 3;
313 ks += a->a_desc->ad_cname.bv_len + 4; /* (attr=*) */
318 kp = key + sprintf(key, "(|");
320 for(a = op->ora_e->e_attrs; a; a = a->a_next) {
321 if(is_at_operational(a->a_desc->ad_type)) continue;
323 for(up = ud->ignore; up; up = up->next)
324 if(a->a_desc == up->attr) break;
328 for(up = ud->attrs; up; up = up->next)
329 if(a->a_desc == up->attr) break;
332 if((b = a->a_vals) && b[0].bv_val) for(i = 0; b[i].bv_val; i++)
333 kp += sprintf(kp, "(%s=%s)", a->a_desc->ad_cname.bv_val, b[i].bv_val);
335 kp += sprintf(kp, "(%s=*)", a->a_desc->ad_cname.bv_val);
338 kp += sprintf(kp, ")");
340 Debug(LDAP_DEBUG_TRACE, "=> unique_add %s\n", key, 0, 0);
342 nop.ors_filter = str2filter_x(&nop, key);
343 ber_str2bv(key, 0, 0, &nop.ors_filterstr);
345 cb.sc_response = (slap_response*)count_attr_cb;
347 nop.o_callback = &cb;
348 nop.o_tag = LDAP_REQ_SEARCH;
349 nop.ors_scope = LDAP_SCOPE_SUBTREE;
350 nop.ors_deref = LDAP_DEREF_NEVER;
351 nop.ors_slimit = SLAP_NO_LIMIT;
352 nop.ors_tlimit = SLAP_NO_LIMIT;
353 nop.o_req_ndn = ud->dn;
354 nop.o_ndn = op->o_bd->be_rootndn;
356 rc = nop.o_bd->be_search(&nop, &nrs);
357 filter_free_x(&nop, nop.ors_filter);
360 if(rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT) {
361 op->o_bd->bd_info = (BackendInfo *) on->on_info;
362 send_ldap_error(op, rs, rc, "unique_add search failed");
366 Debug(LDAP_DEBUG_TRACE, "=> unique_add found %d records\n", uq.count, 0, 0);
369 op->o_bd->bd_info = (BackendInfo *) on->on_info;
370 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
371 "some attributes not unique");
375 return(SLAP_CB_CONTINUE);
379 static int unique_modify(
385 SlapReply nrs = { REP_RESULT };
386 slap_callback cb = { NULL, (slap_response*)count_attr_cb, NULL, NULL };
387 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
392 int i, rc, ks = 16; /* a handful of extra bytes */
394 unique_counter uq = { 0 };
395 unique_data *ud = on->on_bi.bi_private;
397 Debug(LDAP_DEBUG_TRACE, "==> unique_modify <%s>\n", op->o_req_dn.bv_val, 0, 0);
399 nop.o_bd = select_backend(&ud->dn, 0, 1);
401 if (!nop.o_bd->be_search) {
402 op->o_bd->bd_info = (BackendInfo *) on->on_info;
403 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
404 "backend missing search function");
408 op->o_bd->bd_info = (BackendInfo *) on->on_info;
409 send_ldap_error(op, rs, LDAP_OTHER,
410 "no known backend? this shouldn't be happening!");
415 ** count everything first;
416 ** allocate some memory;
417 ** write the search key;
421 if(!(m = op->orm_modlist)) {
422 op->o_bd->bd_info = (BackendInfo *) on->on_info;
423 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
424 "unique_modify() got null op.orm_modlist");
426 } else for(; m; m = m->sml_next) {
427 if(is_at_operational(m->sml_desc->ad_type) ||
428 ((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE)) continue;
430 for(up = ud->ignore; up; up = up->next)
431 if(m->sml_desc == up->attr) break;
435 for(up = ud->attrs; up; up = up->next)
436 if(m->sml_desc == up->attr) break;
439 if((b = m->sml_values) && b[0].bv_val) for(i = 0; b[i].bv_val; i++)
440 ks += b[i].bv_len + m->sml_desc->ad_cname.bv_len + 3;
442 ks += m->sml_desc->ad_cname.bv_len + 4; /* (attr=*) */
447 kp = key + sprintf(key, "(|");
449 for(m = op->orm_modlist; m; m = m->sml_next) {
450 if(is_at_operational(m->sml_desc->ad_type) ||
451 ((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE)) continue;
453 for(up = ud->ignore; up; up = up->next)
454 if(m->sml_desc == up->attr) break;
458 for(up = ud->attrs; up; up = up->next)
459 if(m->sml_desc == up->attr) break;
462 if((b = m->sml_values) && b[0].bv_val) for(i = 0; b[i].bv_val; i++)
463 kp += sprintf(kp, "(%s=%s)", m->sml_desc->ad_cname.bv_val, b[i].bv_val);
465 kp += sprintf(kp, "(%s=*)", m->sml_desc->ad_cname.bv_val);
468 kp += sprintf(kp, ")");
470 Debug(LDAP_DEBUG_TRACE, "=> unique_modify %s\n", key, 0, 0);
472 nop.ors_filter = str2filter_x(&nop, key);
473 ber_str2bv(key, 0, 0, &nop.ors_filterstr);
475 cb.sc_response = (slap_response*)count_attr_cb;
477 nop.o_callback = &cb;
478 nop.o_tag = LDAP_REQ_SEARCH;
479 nop.ors_scope = LDAP_SCOPE_SUBTREE;
480 nop.ors_deref = LDAP_DEREF_NEVER;
481 nop.ors_slimit = SLAP_NO_LIMIT;
482 nop.ors_tlimit = SLAP_NO_LIMIT;
483 nop.o_req_ndn = ud->dn;
484 nop.o_ndn = op->o_bd->be_rootndn;
486 rc = nop.o_bd->be_search(&nop, &nrs);
489 if(rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT) {
490 op->o_bd->bd_info = (BackendInfo *) on->on_info;
491 send_ldap_error(op, rs, rc, "unique_modify search failed");
495 Debug(LDAP_DEBUG_TRACE, "=> unique_modify found %d records\n", uq.count, 0, 0);
498 op->o_bd->bd_info = (BackendInfo *) on->on_info;
499 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
500 "some attributes not unique");
504 return(SLAP_CB_CONTINUE);
509 static int unique_modrdn(
515 SlapReply nrs = { REP_RESULT };
516 slap_callback cb = { NULL, (slap_response*)count_attr_cb, NULL, NULL };
517 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
520 int i, rc, ks = 16; /* a handful of extra bytes */
522 unique_counter uq = { 0 };
523 unique_data *ud = on->on_bi.bi_private;
526 Debug(LDAP_DEBUG_TRACE, "==> unique_modrdn <%s> <%s>\n",
527 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
529 nop.o_bd = select_backend(&ud->dn, 0, 1);
531 if (!nop.o_bd->be_search) {
532 op->o_bd->bd_info = (BackendInfo *) on->on_info;
533 send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
534 "backend missing search function");
538 op->o_bd->bd_info = (BackendInfo *) on->on_info;
539 send_ldap_error(op, rs, LDAP_OTHER,
540 "no known backend? this shouldn't be happening!");
544 if(ldap_bv2rdn_x(&op->oq_modrdn.rs_newrdn, &newrdn,
545 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP, op->o_tmpmemctx )) {
546 op->o_bd->bd_info = (BackendInfo *) on->on_info;
547 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
548 "unknown type(s) used in RDN");
551 for(i = 0; newrdn[i]; i++) {
552 AttributeDescription *ad = NULL;
553 if ( slap_bv2ad( &newrdn[i]->la_attr, &ad, &rs->sr_text )) {
554 ldap_rdnfree_x( newrdn, op->o_tmpmemctx );
555 rs->sr_err = LDAP_INVALID_SYNTAX;
556 send_ldap_result( op, rs );
559 newrdn[i]->la_private = ad;
562 for(i = 0; newrdn[i]; i++) {
563 AttributeDescription *ad = newrdn[i]->la_private;
565 for(up = ud->ignore; up; up = up->next)
566 if(ad == up->attr) break;
570 for(up = ud->attrs; up; up = up->next)
571 if(ad == up->attr) break;
574 ks += newrdn[i]->la_value.bv_len + ad->ad_cname.bv_len + 3;
578 kp = key + sprintf(key, "(|");
580 for(i = 0; newrdn[i]; i++) {
581 AttributeDescription *ad = newrdn[i]->la_private;
583 for(up = ud->ignore; up; up = up->next)
584 if(ad == up->attr) break;
588 for(up = ud->attrs; up; up = up->next)
589 if(ad == up->attr) break;
592 kp += sprintf(kp, "(%s=%s)", ad->ad_cname.bv_val,
593 newrdn[i]->la_value.bv_val);
596 kp += sprintf(kp, ")");
599 Debug(LDAP_DEBUG_TRACE, "=> unique_modrdn %s\n", key, 0, 0);
601 nop.ors_filter = str2filter_x(&nop, key);
602 ber_str2bv(key, 0, 0, &nop.ors_filterstr);
604 cb.sc_response = (slap_response*)count_attr_cb;
606 nop.o_callback = &cb;
607 nop.o_tag = LDAP_REQ_SEARCH;
608 nop.ors_scope = LDAP_SCOPE_SUBTREE;
609 nop.ors_deref = LDAP_DEREF_NEVER;
610 nop.ors_slimit = SLAP_NO_LIMIT;
611 nop.ors_tlimit = SLAP_NO_LIMIT;
612 nop.o_req_ndn = ud->dn;
613 nop.o_ndn = op->o_bd->be_rootndn;
615 rc = nop.o_bd->be_search(&nop, &nrs);
617 ldap_rdnfree_x( newrdn, op->o_tmpmemctx );
619 if(rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT) {
620 op->o_bd->bd_info = (BackendInfo *) on->on_info;
621 send_ldap_error(op, rs, rc, "unique_modrdn search failed");
625 Debug(LDAP_DEBUG_TRACE, "=> unique_modrdn found %d records\n", uq.count, 0, 0);
628 op->o_bd->bd_info = (BackendInfo *) on->on_info;
629 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
630 "some attributes not unique");
634 return(SLAP_CB_CONTINUE);
638 ** init_module is last so the symbols resolve "for free" --
639 ** it expects to be called automagically during dynamic module initialization
644 /* statically declared just after the #includes at top */
645 unique.on_bi.bi_type = "unique";
646 unique.on_bi.bi_db_init = unique_db_init;
647 unique.on_bi.bi_db_config = unique_config;
648 unique.on_bi.bi_db_open = unique_open;
649 unique.on_bi.bi_db_close = unique_close;
650 unique.on_bi.bi_op_add = unique_add;
651 unique.on_bi.bi_op_modify = unique_modify;
652 unique.on_bi.bi_op_modrdn = unique_modrdn;
653 unique.on_bi.bi_op_delete = NULL;
655 return(overlay_register(&unique));
658 #if SLAPD_OVER_UNIQUE == SLAPD_MOD_DYNAMIC && defined(PIC)
659 int init_module(int argc, char *argv[]) {
660 return unique_init();
664 #endif /* SLAPD_OVER_UNIQUE */