]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/unique.c
Ignore matches from the entry being modified
[openldap] / servers / slapd / overlays / unique.c
1 /* unique.c - attribute uniqueness module */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2004-2005 The OpenLDAP Foundation.
6  * Portions Copyright 2004 Symas Corporation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
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>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Symas Corp. for inclusion in
19  * OpenLDAP Software.  This work was sponsored by Hewlett-Packard.
20  */
21
22 #include "portable.h"
23
24 #ifdef SLAPD_OVER_UNIQUE
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "slap.h"
32
33 static slap_overinst unique;
34
35 typedef struct unique_attrs_s {
36         struct unique_attrs_s *next;            /* list of attrs */
37         AttributeDescription *attr;
38 } unique_attrs;
39
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 */
46 } unique_data;
47
48 typedef struct unique_counter_s {
49         struct berval *ndn;
50         int count;
51 } unique_counter;
52
53 /*
54 ** allocate new unique_data;
55 ** initialize, copy basedn;
56 ** store in on_bi.bi_private;
57 **
58 */
59
60 static int unique_db_init(
61         BackendDB       *be
62 )
63 {
64         slap_overinst *on = (slap_overinst *)be->bd_info;
65         unique_data *ud   = ch_malloc(sizeof(unique_data));
66
67         /* Debug(LDAP_DEBUG_TRACE, "==> unique_init\n", 0, 0, 0); */
68
69         ud->message     = "_init";
70         ud->attrs       = NULL;
71         ud->ignore      = NULL;
72         ud->strict      = 0;
73
74         /* default to the base of our configured database */
75         ber_dupbv(&ud->dn, &be->be_nsuffix[0]);
76         on->on_bi.bi_private = ud;
77
78         return 0;
79 }
80
81
82 /*
83 ** if command = attributes:
84 **      foreach argument:
85 **              convert to attribute;
86 **              add to configured attribute list;
87 ** elseif command = base:
88 **      set our basedn to argument;
89 ** else complain about invalid directive;
90 **
91 */
92
93 static int unique_config(
94         BackendDB       *be,
95         const char      *fname,
96         int             lineno,
97         int             argc,
98         char            **argv
99 )
100 {
101         slap_overinst *on = (slap_overinst *) be->bd_info;
102         unique_data *ud   = on->on_bi.bi_private;
103         unique_attrs *up;
104         const char *text;
105         AttributeDescription *ad;
106         int i;
107
108         ud->message = "_config";
109         Debug(LDAP_DEBUG_TRACE, "==> unique_config\n", 0, 0, 0);
110
111         if(!strcasecmp(*argv, "unique_attributes") ||
112            !strcasecmp(*argv, "unique_ignore")) {
113                 for(i = 1; i < argc; i++) {
114                         for(up = ud->attrs; up; up = up->next)
115                             if(!strcmp(argv[i], up->attr->ad_cname.bv_val)) {
116                                 Debug(LDAP_DEBUG_ANY,
117                                         "%s: line %d: duplicate attribute <%s>, ignored\n",
118                                         fname, lineno, argv[i]);
119                                 continue;
120                         }
121                         ad = NULL;
122                         if(slap_str2ad(argv[i], &ad, &text) != LDAP_SUCCESS) {
123                                 Debug(LDAP_DEBUG_ANY,
124                                         "%s: line %d: bad attribute <%s>, ignored\n",
125                                         fname, lineno, text);
126                                 continue;               /* XXX */
127                         } else if(ad->ad_next) {
128                                 Debug(LDAP_DEBUG_ANY,
129                                         "%s: line %d: multiple attributes match <%s>, ignored\n",
130                                         fname, lineno, argv[i]);
131                                 continue;
132                         }
133                         up = ch_malloc(sizeof(unique_attrs));
134                         up->attr = ad;
135                         if(!strcasecmp(*argv, "unique_ignore")) {
136                                 up->next = ud->ignore;
137                                 ud->ignore = up;
138                         } else {
139                                 up->next = ud->attrs;
140                                 ud->attrs = up;
141                         }
142                         Debug(LDAP_DEBUG_ANY, "%s: line %d: new attribute <%s>\n",
143                                 fname, lineno, argv[i]);
144                 }
145         } else if(!strcasecmp(*argv, "unique_strict")) {
146                 ud->strict = 1;
147         } else if(!strcasecmp(*argv, "unique_base")) {
148                 struct berval bv;
149                 ber_str2bv( argv[1], 0, 0, &bv );
150                 ch_free(ud->dn.bv_val);
151                 dnNormalize(0, NULL, NULL, &bv, &ud->dn, NULL);
152                 Debug(LDAP_DEBUG_ANY, "%s: line %d: new base dn <%s>\n",
153                         fname, lineno, argv[1]);
154         } else {
155                 return(SLAP_CONF_UNKNOWN);
156         }
157
158         return(0);
159 }
160
161
162 /*
163 ** mostly, just print the init message;
164 **
165 */
166
167 static int
168 unique_open(
169         BackendDB *be
170 )
171 {
172         slap_overinst *on       = (slap_overinst *)be->bd_info;
173         unique_data *ud         = on->on_bi.bi_private;
174         ud->message             = "_open";
175
176         Debug(LDAP_DEBUG_TRACE, "unique_open: overlay initialized\n", 0, 0, 0);
177
178         return(0);
179 }
180
181
182 /*
183 ** foreach configured attribute:
184 **      free it;
185 ** free our basedn;
186 ** (do not) free ud->message;
187 ** reset on_bi.bi_private;
188 ** free our config data;
189 **
190 */
191
192 static int
193 unique_close(
194         BackendDB *be
195 )
196 {
197         slap_overinst *on       = (slap_overinst *) be->bd_info;
198         unique_data *ud         = on->on_bi.bi_private;
199         unique_attrs *ii, *ij;
200         ud->message             = "_close";
201
202         Debug(LDAP_DEBUG_TRACE, "==> unique_close\n", 0, 0, 0);
203
204         for(ii = ud->attrs; ii; ii = ij) {
205                 ij = ii->next;
206                 ch_free(ii);
207         }
208
209         for(ii = ud->ignore; ii; ii = ij) {
210                 ij = ii->next;
211                 ch_free(ii);
212         }
213
214         ch_free(ud->dn.bv_val);
215
216         on->on_bi.bi_private = NULL;    /* XXX */
217
218         ch_free(ud);
219
220         return(0);
221 }
222
223
224 /*
225 ** search callback
226 **      if this is a REP_SEARCH, count++;
227 **
228 */
229
230 static int count_attr_cb(
231         Operation *op,
232         SlapReply *rs
233 )
234 {
235         unique_counter *uc;
236
237         /* because you never know */
238         if(!op || !rs) return(0);
239
240         /* Only search entries are interesting */
241         if(rs->sr_type != REP_SEARCH) return(0);
242
243         uc = op->o_callback->sc_private;
244
245         /* Ignore the current entry */
246         if ( dn_match( uc->ndn, &rs->sr_entry->e_nname )) return(0);
247
248         Debug(LDAP_DEBUG_TRACE, "==> count_attr_cb <%s>\n",
249                 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "UNKNOWN_DN", 0, 0);
250
251         uc->count++;
252
253         return(0);
254 }
255
256 static int count_filter_len(
257         unique_data *ud,
258         AttributeDescription *ad,
259         BerVarray b,
260         int ks
261 )
262 {
263         unique_attrs *up;
264         int i;
265
266         while(!is_at_operational(ad->ad_type)) {
267                 if(ud->ignore) {
268                         for(up = ud->ignore; up; up = up->next)
269                                 if(ad == up->attr) break;
270                         if(up) break;
271                 }
272                 if(ud->attrs) {
273                         for(up = ud->attrs; up; up = up->next)
274                                 if(ad == up->attr) break;
275                         if(!up) break;
276                 }
277                 if(b && b[0].bv_val) for(i = 0; b[i].bv_val; i++)
278                         ks += b[i].bv_len + ad->ad_cname.bv_len + STRLENOF( "(=)" );
279                 else if(ud->strict)
280                         ks += ad->ad_cname.bv_len + STRLENOF( "(=*)" ); /* (attr=*) */
281                 break;
282         }
283         return ks;
284 }
285
286 static char *build_filter(
287         unique_data *ud,
288         AttributeDescription *ad,
289         BerVarray b,
290         char *kp
291 )
292 {
293         unique_attrs *up;
294         int i;
295
296         while(!is_at_operational(ad->ad_type)) {
297                 if(ud->ignore) {
298                         for(up = ud->ignore; up; up = up->next)
299                                 if(ad == up->attr) break;
300                         if(up) break;
301                 }
302                 if(ud->attrs) {
303                         for(up = ud->attrs; up; up = up->next)
304                                 if(ad == up->attr) break;
305                         if(!up) break;
306                 }
307                 if(b && b[0].bv_val) for(i = 0; b[i].bv_val; i++)
308                         kp += sprintf(kp, "(%s=%s)", ad->ad_cname.bv_val, b[i].bv_val);
309                 else if(ud->strict)
310                         kp += sprintf(kp, "(%s=*)", ad->ad_cname.bv_val);
311                 break;
312         }
313         return kp;
314 }
315
316 static int unique_search(
317         Operation *op,
318         Operation *nop,
319         SlapReply *rs,
320         char *key
321 )
322 {
323         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
324         unique_data *ud = on->on_bi.bi_private;
325         SlapReply nrs = { REP_RESULT };
326         slap_callback cb = { NULL, NULL, NULL, NULL }; /* XXX */
327         unique_counter uq = { NULL, 0 };
328         int rc;
329
330         nop->ors_filter = str2filter_x(nop, key);
331         ber_str2bv(key, 0, 0, &nop->ors_filterstr);
332
333         cb.sc_response  = (slap_response*)count_attr_cb;
334         cb.sc_private   = &uq;
335         nop->o_callback = &cb;
336         nop->o_tag      = LDAP_REQ_SEARCH;
337         nop->ors_scope  = LDAP_SCOPE_SUBTREE;
338         nop->ors_deref  = LDAP_DEREF_NEVER;
339         nop->ors_limit  = NULL;
340         nop->ors_slimit = SLAP_NO_LIMIT;
341         nop->ors_tlimit = SLAP_NO_LIMIT;
342         nop->ors_attrs  = slap_anlist_no_attrs;
343         nop->ors_attrsonly = 1;
344
345         uq.ndn = &op->o_req_ndn;
346
347         nop->o_req_ndn  = ud->dn;
348         nop->o_ndn = op->o_bd->be_rootndn;
349
350         rc = nop->o_bd->be_search(nop, &nrs);
351         filter_free_x(nop, nop->ors_filter);
352         ch_free( key );
353
354         if(rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT) {
355                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
356                 send_ldap_error(op, rs, rc, "unique_search failed");
357                 return(rs->sr_err);
358         }
359
360         Debug(LDAP_DEBUG_TRACE, "=> unique_search found %d records\n", uq.count, 0, 0);
361
362         if(uq.count) {
363                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
364                 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
365                         "some attributes not unique");
366                 return(rs->sr_err);
367         }
368
369         return(SLAP_CB_CONTINUE);
370 }
371
372 static int unique_add(
373         Operation *op,
374         SlapReply *rs
375 )
376 {
377         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
378         unique_data *ud = on->on_bi.bi_private;
379         Operation nop = *op;
380
381         Attribute *a;
382         char *key, *kp;
383         int ks = 16;
384
385         Debug(LDAP_DEBUG_TRACE, "==> unique_add <%s>\n", op->o_req_dn.bv_val, 0, 0);
386
387         /* validate backend. Should have already been done, but whatever */
388         nop.o_bd = select_backend(&ud->dn, 0, 1);
389         if(nop.o_bd) {
390                 if (!nop.o_bd->be_search) {
391                         op->o_bd->bd_info = (BackendInfo *) on->on_info;
392                         send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
393                         "backend missing search function");
394                         return(rs->sr_err);
395                 }
396         } else {
397                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
398                 send_ldap_error(op, rs, LDAP_OTHER,
399                         "no known backend? this shouldn't be happening!");
400                 return(rs->sr_err);
401         }
402
403 /*
404 ** count everything first;
405 ** allocate some memory;
406 ** write the search key;
407 **
408 */
409
410         if(!(a = op->ora_e->e_attrs)) {
411                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
412                 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
413                         "unique_add() got null op.ora_e.e_attrs");
414                 return(rs->sr_err);
415         } else for(; a; a = a->a_next) {
416                 ks = count_filter_len(ud, a->a_desc, a->a_vals, ks);
417         }
418
419         key = ch_malloc(ks);
420
421         kp = key + sprintf(key, "(|");
422
423         for(a = op->ora_e->e_attrs; a; a = a->a_next) {
424                 kp = build_filter(ud, a->a_desc, a->a_vals, kp);
425         }
426
427         sprintf(kp, ")");
428
429         Debug(LDAP_DEBUG_TRACE, "=> unique_add %s\n", key, 0, 0);
430
431         return unique_search(op, &nop, rs, key);
432 }
433
434
435 static int unique_modify(
436         Operation *op,
437         SlapReply *rs
438 )
439 {
440         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
441         unique_data *ud = on->on_bi.bi_private;
442         Operation nop = *op;
443
444         Modifications *m;
445         char *key, *kp;
446         int ks = 16;            /* a handful of extra bytes */
447
448         Debug(LDAP_DEBUG_TRACE, "==> unique_modify <%s>\n", op->o_req_dn.bv_val, 0, 0);
449
450         nop.o_bd = select_backend(&ud->dn, 0, 1);
451         if(nop.o_bd) {
452                 if (!nop.o_bd->be_search) {
453                         op->o_bd->bd_info = (BackendInfo *) on->on_info;
454                         send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
455                         "backend missing search function");
456                         return(rs->sr_err);
457                 }
458         } else {
459                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
460                 send_ldap_error(op, rs, LDAP_OTHER,
461                         "no known backend? this shouldn't be happening!");
462                 return(rs->sr_err);
463         }
464
465 /*
466 ** count everything first;
467 ** allocate some memory;
468 ** write the search key;
469 **
470 */
471
472         if(!(m = op->orm_modlist)) {
473                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
474                 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
475                         "unique_modify() got null op.orm_modlist");
476                 return(rs->sr_err);
477         } else for(; m; m = m->sml_next) {
478                 if ((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE) continue;
479                 ks = count_filter_len(ud, m->sml_desc, m->sml_values, ks);
480         }
481
482         key = ch_malloc(ks);
483
484         kp = key + sprintf(key, "(|");
485
486         for(m = op->orm_modlist; m; m = m->sml_next) {
487                 if ((m->sml_op & LDAP_MOD_OP) == LDAP_MOD_DELETE) continue;
488                 kp = build_filter(ud, m->sml_desc, m->sml_values, kp);
489         }
490
491         sprintf(kp, ")");
492
493         Debug(LDAP_DEBUG_TRACE, "=> unique_modify %s\n", key, 0, 0);
494
495         return unique_search(op, &nop, rs, key);
496 }
497
498
499 static int unique_modrdn(
500         Operation *op,
501         SlapReply *rs
502 )
503 {
504         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
505         unique_data *ud = on->on_bi.bi_private;
506         Operation nop = *op;
507
508         char *key, *kp;
509         int i, rc, ks = 16;             /* a handful of extra bytes */
510         LDAPRDN newrdn;
511         struct berval bv[2];
512
513         Debug(LDAP_DEBUG_TRACE, "==> unique_modrdn <%s> <%s>\n",
514                 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
515
516         nop.o_bd = select_backend(&ud->dn, 0, 1);
517         if(nop.o_bd) {
518                 if (!nop.o_bd->be_search) {
519                         op->o_bd->bd_info = (BackendInfo *) on->on_info;
520                         send_ldap_error(op, rs, LDAP_UNWILLING_TO_PERFORM,
521                         "backend missing search function");
522                         return(rs->sr_err);
523                 }
524         } else {
525                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
526                 send_ldap_error(op, rs, LDAP_OTHER,
527                         "no known backend? this shouldn't be happening!");
528                 return(rs->sr_err);
529         }
530
531         if(ldap_bv2rdn_x(&op->oq_modrdn.rs_newrdn, &newrdn,
532                 (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP, op->o_tmpmemctx )) {
533                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
534                 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
535                         "unknown type(s) used in RDN");
536                 return(rs->sr_err);
537         }
538         for(i = 0; newrdn[i]; i++) {
539                 AttributeDescription *ad = NULL;
540                 if ( slap_bv2ad( &newrdn[i]->la_attr, &ad, &rs->sr_text )) {
541                         ldap_rdnfree_x( newrdn, op->o_tmpmemctx );
542                         rs->sr_err = LDAP_INVALID_SYNTAX;
543                         send_ldap_result( op, rs );
544                         return(rs->sr_err);
545                 }
546                 newrdn[i]->la_private = ad;
547         }
548
549         bv[1].bv_val = NULL;
550         bv[1].bv_len = 0;
551
552         for(i = 0; newrdn[i]; i++) {
553                 bv[0] = newrdn[i]->la_value;
554                 ks = count_filter_len(ud, newrdn[i]->la_private, bv, ks);
555         }
556
557         key = ch_malloc(ks);
558         kp = key + sprintf(key, "(|");
559
560         for(i = 0; newrdn[i]; i++) {
561                 bv[0] = newrdn[i]->la_value;
562                 kp = build_filter(ud, newrdn[i]->la_private, bv, kp);
563         }
564
565         sprintf(kp, ")");
566
567         Debug(LDAP_DEBUG_TRACE, "=> unique_modrdn %s\n", key, 0, 0);
568
569         return unique_search(op, &nop, rs, key);
570 }
571
572 /*
573 ** init_module is last so the symbols resolve "for free" --
574 ** it expects to be called automagically during dynamic module initialization
575 */
576
577 int unique_init() {
578
579         /* statically declared just after the #includes at top */
580         unique.on_bi.bi_type = "unique";
581         unique.on_bi.bi_db_init = unique_db_init;
582         unique.on_bi.bi_db_config = unique_config;
583         unique.on_bi.bi_db_open = unique_open;
584         unique.on_bi.bi_db_close = unique_close;
585         unique.on_bi.bi_op_add = unique_add;
586         unique.on_bi.bi_op_modify = unique_modify;
587         unique.on_bi.bi_op_modrdn = unique_modrdn;
588         unique.on_bi.bi_op_delete = NULL;
589
590         return(overlay_register(&unique));
591 }
592
593 #if SLAPD_OVER_UNIQUE == SLAPD_MOD_DYNAMIC && defined(PIC)
594 int init_module(int argc, char *argv[]) {
595         return unique_init();
596 }
597 #endif
598
599 #endif /* SLAPD_OVER_UNIQUE */