]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/unique.c
Reworked recent backend API changes, now using a separate struct,
[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-2007 The OpenLDAP Foundation.
6  * Portions Copyright 2004,2006-2007 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 Corporation for
19  * inclusion in OpenLDAP Software, with subsequent enhancements by
20  * Matthew Backes at Symas Corporation.  This work was sponsored by
21  * Hewlett-Packard.
22  */
23
24 #include "portable.h"
25
26 #ifdef SLAPD_OVER_UNIQUE
27
28 #include <stdio.h>
29
30 #include <ac/string.h>
31 #include <ac/socket.h>
32
33 #include "slap.h"
34 #include "config.h"
35
36 #define UNIQUE_DEFAULT_URI ("ldap:///??sub")
37
38 static slap_overinst unique;
39
40 typedef struct unique_attrs_s {
41         struct unique_attrs_s *next;          /* list of attrs */
42         AttributeDescription *attr;
43 } unique_attrs;
44
45 typedef struct unique_domain_uri_s {
46         struct unique_domain_uri_s *next;
47         struct berval *dn;
48         struct berval *ndn;
49         struct berval *filter;
50         struct unique_attrs_s *attrs;
51         int scope;
52 } unique_domain_uri;
53
54 typedef struct unique_domain_s {
55         struct unique_domain_s *next;
56         struct berval *domain_spec;
57         struct unique_domain_uri_s *uri;
58         char ignore;                          /* polarity of attributes */
59         char strict;                          /* null considered unique too */
60 } unique_domain;
61
62 typedef struct unique_data_s {
63         struct unique_domain_s *domains;
64         struct unique_domain_s *legacy;
65         char legacy_strict_set;
66 } unique_data;
67
68 typedef struct unique_counter_s {
69         struct berval *ndn;
70         int count;
71 } unique_counter;
72
73 enum {
74         UNIQUE_BASE = 1,
75         UNIQUE_IGNORE,
76         UNIQUE_ATTR,
77         UNIQUE_STRICT,
78         UNIQUE_URI
79 };
80
81 static ConfigDriver unique_cf_base;
82 static ConfigDriver unique_cf_attrs;
83 static ConfigDriver unique_cf_strict;
84 static ConfigDriver unique_cf_uri;
85
86 static ConfigTable uniquecfg[] = {
87         { "unique_base", "basedn", 2, 2, 0, ARG_DN|ARG_MAGIC|UNIQUE_BASE,
88           unique_cf_base, "( OLcfgOvAt:10.1 NAME 'olcUniqueBase' "
89           "DESC 'Subtree for uniqueness searches' "
90           "EQUALITY distinguishedNameMatch "
91           "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
92         { "unique_ignore", "attribute...", 2, 0, 0, ARG_MAGIC|UNIQUE_IGNORE,
93           unique_cf_attrs, "( OLcfgOvAt:10.2 NAME 'olcUniqueIgnore' "
94           "DESC 'Attributes for which uniqueness shall not be enforced' "
95           "EQUALITY caseIgnoreMatch "
96           "ORDERING caseIgnoreOrderingMatch "
97           "SUBSTR caseIgnoreSubstringsMatch "
98           "SYNTAX OMsDirectoryString )", NULL, NULL },
99         { "unique_attributes", "attribute...", 2, 0, 0, ARG_MAGIC|UNIQUE_ATTR,
100           unique_cf_attrs, "( OLcfgOvAt:10.3 NAME 'olcUniqueAttribute' "
101           "DESC 'Attributes for which uniqueness shall be enforced' "
102           "EQUALITY caseIgnoreMatch "
103           "ORDERING caseIgnoreOrderingMatch "
104           "SUBSTR caseIgnoreSubstringsMatch "
105           "SYNTAX OMsDirectoryString )", NULL, NULL },
106         { "unique_strict", "on|off", 1, 2, 0, ARG_MAGIC|UNIQUE_STRICT,
107           unique_cf_strict, "( OLcfgOvAt:10.4 NAME 'olcUniqueStrict' "
108           "DESC 'Enforce uniqueness of null values' "
109           "EQUALITY booleanMatch "
110           "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
111         { "unique_uri", "ldapuri", 2, 3, 0, ARG_MAGIC|UNIQUE_URI,
112           unique_cf_uri, "( OLcfgOvAt:10.5 NAME 'olcUniqueURI' "
113           "DESC 'List of keywords and LDAP URIs for a uniqueness domain' "
114           "EQUALITY caseExactMatch "
115           "ORDERING caseExactOrderingMatch "
116           "SUBSTR caseExactSubstringsMatch "
117           "SYNTAX OMsDirectoryString )", NULL, NULL },
118         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
119 };
120
121 static ConfigOCs uniqueocs[] = {
122         { "( OLcfgOvOc:10.1 "
123           "NAME 'olcUniqueConfig' "
124           "DESC 'Attribute value uniqueness configuration' "
125           "SUP olcOverlayConfig "
126           "MAY ( olcUniqueBase $ olcUniqueIgnore $ "
127           "olcUniqueAttribute $ olcUniqueStrict $ "
128           "olcUniqueURI ) )",
129           Cft_Overlay, uniquecfg },
130         { NULL, 0, NULL }
131 };
132
133 static void
134 unique_free_domain_uri ( unique_domain_uri *uri )
135 {
136         unique_domain_uri *next_uri = NULL;
137         unique_attrs *attr, *next_attr = NULL;
138
139         while ( uri ) {
140                 next_uri = uri->next;
141                 ber_bvfree ( uri->dn );
142                 ber_bvfree ( uri->ndn );
143                 ber_bvfree ( uri->filter );
144                 attr = uri->attrs;
145                 while ( attr ) {
146                         next_attr = attr->next;
147                         ch_free (attr);
148                         attr = next_attr;
149                 }
150                 ch_free ( uri );
151                 uri = next_uri;
152         }
153 }
154
155 /* free an entire stack of domains */
156 static void
157 unique_free_domain ( unique_domain *domain )
158 {
159         unique_domain *next_domain = NULL;
160
161         while ( domain ) {
162                 next_domain = domain->next;
163                 ber_bvfree ( domain->domain_spec );
164                 unique_free_domain_uri ( domain->uri );
165                 ch_free ( domain );
166                 domain = next_domain;
167         }
168 }
169
170 static int
171 unique_new_domain_uri ( unique_domain_uri **urip,
172                         const LDAPURLDesc *url_desc,
173                         ConfigArgs *c )
174 {
175         int i, rc = LDAP_SUCCESS;
176         unique_domain_uri *uri;
177         struct berval bv = {0, NULL};
178         BackendDB *be = (BackendDB *)c->be;
179         char ** attr_str;
180         AttributeDescription * ad;
181         const char * text;
182
183         uri = ch_calloc ( 1, sizeof ( unique_domain_uri ) );
184
185         if ( url_desc->lud_dn && url_desc->lud_dn[0] ) {
186                 ber_str2bv( url_desc->lud_dn, 0, 1, &bv );
187                 rc = dnPrettyNormal( NULL,
188                                      &bv,
189                                      uri->dn,
190                                      uri->ndn,
191                                      NULL );
192                 if ( rc != LDAP_SUCCESS ) {
193                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
194                                   "<%s> invalid DN %d (%s)",
195                                   url_desc->lud_dn, rc, ldap_err2string( rc ));
196                         rc = ARG_BAD_CONF;
197                         goto exit;
198                 }
199
200                 if ( !dnIsSuffix ( uri->ndn, &be->be_nsuffix[0] ) ) {
201                         sprintf ( c->cr_msg,
202                                   "dn <%s> is not a suffix of backend base dn <%s>",
203                                   uri->dn->bv_val,
204                                   be->be_nsuffix[0].bv_val );
205                         rc = ARG_BAD_CONF;
206                         goto exit;
207                 }
208         }
209
210         attr_str = url_desc->lud_attrs;
211         if ( attr_str ) {
212                 for ( i=0; attr_str[i]; ++i ) {
213                         unique_attrs * attr;
214                         ad = NULL;
215                         if ( slap_str2ad ( attr_str[i], &ad, &text )
216                              == LDAP_SUCCESS) {
217                                 attr = ch_calloc ( 1,
218                                                    sizeof ( unique_attrs ) );
219                                 attr->attr = ad;
220                                 attr->next = uri->attrs;
221                                 uri->attrs = attr;
222                         } else {
223                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
224                                           "unique: attribute: %s: %s",
225                                           attr_str[i], text );
226                                 rc = ARG_BAD_CONF;
227                                 goto exit;
228                         }
229                 }
230         }
231
232         uri->scope = url_desc->lud_scope;
233         if ( !uri->scope ) {
234                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
235                           "unique: uri with base scope will always be unique");
236                 rc = ARG_BAD_CONF;
237                 goto exit;
238         }
239
240         if (url_desc->lud_filter) {
241                 Filter * f;
242                 uri->filter = ber_str2bv( url_desc->lud_filter, 0, 1, NULL);
243                 f = str2filter( uri->filter->bv_val );
244                 if ( !f ) {
245                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
246                                   "unique: bad filter");
247                         rc = ARG_BAD_CONF;
248                         goto exit;
249                 }
250                 filter_free( f );
251         }
252 exit:
253         if ( bv.bv_val ) ber_memfree ( bv.bv_val );
254         uri->next = *urip;
255         *urip = uri;
256         if ( rc ) {
257                 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
258                         "%s: %s\n", c->log, c->cr_msg, 0 );
259                 unique_free_domain_uri ( uri );
260                 *urip = NULL;
261         }
262         return rc;
263 }
264
265 static int
266 unique_new_domain_uri_basic ( unique_domain_uri **urip,
267                               ConfigArgs *c )
268 {
269         LDAPURLDesc *url_desc = NULL;
270         int rc;
271
272         rc = ldap_url_parse ( UNIQUE_DEFAULT_URI, &url_desc );
273         if ( rc ) return rc;
274         rc = unique_new_domain_uri ( urip, url_desc, c );
275         ldap_free_urldesc ( url_desc );
276         return rc;
277 }
278
279 /* if *domain is non-null, it's pushed down the stack.
280  * note that the entire stack is freed if there is an error,
281  * so build added domains in a separate stack before adding them
282  *
283  * domain_specs look like
284  *
285  * [strict ][ignore ]uri[[ uri]...]
286  * e.g. "ldap:///ou=foo,o=bar?uid?sub ldap:///ou=baz,o=bar?uid?sub"
287  *      "strict ldap:///ou=accounts,o=bar?uid,uidNumber?one"
288  *      etc
289  *
290  * so finally strictness is per-domain
291  * but so is ignore-state, and that would be better as a per-url thing
292  */
293 static int
294 unique_new_domain ( unique_domain **domainp,
295                     char *domain_spec,
296                     ConfigArgs *c )
297 {
298         char *uri_start;
299         int rc = LDAP_SUCCESS;
300         int uri_err = 0;
301         unique_domain * domain;
302         LDAPURLDesc *url_desc, *url_descs = NULL;
303
304         Debug(LDAP_DEBUG_TRACE, "==> unique_new_domain <%s>\n",
305               domain_spec, 0, 0);
306
307         domain = ch_calloc ( 1, sizeof (unique_domain) );
308         domain->domain_spec = ber_str2bv( domain_spec, 0, 1, NULL );
309
310         uri_start = domain_spec;
311         if ( strncasecmp ( uri_start, "ignore ",
312                            STRLENOF( "ignore " ) ) == 0 ) {
313                 domain->ignore = 1;
314                 uri_start += STRLENOF( "ignore " );
315         }
316         if ( strncasecmp ( uri_start, "strict ",
317                            STRLENOF( "strict " ) ) == 0 ) {
318                 domain->strict = 1;
319                 uri_start += STRLENOF( "strict " );
320                 if ( !domain->ignore
321                      && strncasecmp ( uri_start, "ignore ",
322                                       STRLENOF( "ignore " ) ) == 0 ) {
323                         domain->ignore = 1;
324                         uri_start += STRLENOF( "ignore " );
325                 }
326         }
327         rc = ldap_url_parselist_ext ( &url_descs, uri_start, " ", 0 );
328         if ( rc ) {
329                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
330                           "<%s> invalid ldap urilist",
331                           uri_start );
332                 rc = ARG_BAD_CONF;
333                 goto exit;
334         }
335
336         for ( url_desc = url_descs;
337               url_desc;
338               url_desc = url_descs->lud_next ) {
339                 rc = unique_new_domain_uri ( &domain->uri,
340                                              url_desc,
341                                              c );
342                 if ( rc ) {
343                         rc = ARG_BAD_CONF;
344                         uri_err = 1;
345                         goto exit;
346                 }
347         }
348
349 exit:
350         if ( url_descs ) ldap_free_urldesc ( url_descs );
351         domain->next = *domainp;
352         *domainp = domain;
353         if ( rc ) {
354                 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
355                         "%s: %s\n", c->log, c->cr_msg, 0 );
356                 unique_free_domain ( domain );
357                 *domainp = NULL;
358         }
359         return rc;
360 }
361
362 static int
363 unique_cf_base( ConfigArgs *c )
364 {
365         BackendDB *be = (BackendDB *)c->be;
366         slap_overinst *on = (slap_overinst *)c->bi;
367         unique_data *private = (unique_data *) on->on_bi.bi_private;
368         unique_domain *domains = private->domains;
369         unique_domain *legacy = private->legacy;
370         int rc = ARG_BAD_CONF;
371
372         switch ( c->op ) {
373         case SLAP_CONFIG_EMIT:
374                 rc = 0;
375                 if ( legacy && legacy->uri && legacy->uri->dn ) {
376                         rc = value_add_one ( &c->rvalue_vals,
377                                              legacy->uri->dn );
378                         if ( rc ) return rc;
379                         rc = value_add_one ( &c->rvalue_nvals,
380                                              legacy->uri->ndn );
381                         if ( rc ) return rc;
382                 }
383                 break;
384         case LDAP_MOD_DELETE:
385                 assert ( legacy && legacy->uri && legacy->uri->dn );
386                 rc = 0;
387                 ber_bvfree ( legacy->uri->dn );
388                 ber_bvfree ( legacy->uri->ndn );
389                 legacy->uri->dn = NULL;
390                 legacy->uri->ndn = NULL;
391                 if ( !legacy->uri->attrs
392                      && !legacy->uri->dn ) {
393                         unique_free_domain_uri ( legacy->uri );
394                         legacy->uri = NULL;
395                 }
396                 if ( !legacy->uri && !private->legacy_strict_set ) {
397                         unique_free_domain ( legacy );
398                         private->legacy = legacy = NULL;
399                 }
400                 break;
401         case LDAP_MOD_ADD:
402         case SLAP_CONFIG_ADD:
403                 if ( domains ) {
404                         sprintf ( c->cr_msg,
405                                   "cannot set legacy attrs when URIs are present" );
406                         Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
407                                 c->cr_msg, NULL, NULL );
408                         rc = ARG_BAD_CONF;
409                         break;
410                 }
411                 if ( !dnIsSuffix ( &c->value_ndn,
412                                    &be->be_nsuffix[0] ) ) {
413                         sprintf ( c->cr_msg,
414                                   "dn is not a suffix of backend base" );
415                         Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
416                                 c->cr_msg, NULL, NULL );
417                         rc = ARG_BAD_CONF;
418                         break;
419                 }
420                 if ( !legacy ) {
421                         unique_new_domain ( &private->legacy,
422                                             UNIQUE_DEFAULT_URI,
423                                             c );
424                         legacy = private->legacy;
425                 }
426                 if ( !legacy->uri )
427                         unique_new_domain_uri_basic ( &legacy->uri, c );
428                 ber_bvfree ( legacy->uri->dn );
429                 ber_bvfree ( legacy->uri->ndn );
430                 legacy->uri->dn = ber_bvdup ( &c->value_dn );
431                 legacy->uri->ndn = ber_bvdup ( &c->value_ndn );
432                 rc = 0;
433                 break;
434         default:
435                 abort();
436         }
437
438         return rc;
439 }
440
441 static int
442 unique_cf_attrs( ConfigArgs *c )
443 {
444         slap_overinst *on = (slap_overinst *)c->bi;
445         unique_data *private = (unique_data *) on->on_bi.bi_private;
446         unique_domain *domains = private->domains;
447         unique_domain *legacy = private->legacy;
448         unique_attrs *new_attrs = NULL;
449         unique_attrs *attr, *next_attr, *reverse_attrs;
450         unique_attrs **attrp;
451         int rc = ARG_BAD_CONF;
452         int i;
453
454         switch ( c->op ) {
455         case SLAP_CONFIG_EMIT:
456                 if ( legacy
457                      && (c->type == UNIQUE_IGNORE) == legacy->ignore
458                      && legacy->uri )
459                         for ( attr = legacy->uri->attrs;
460                               attr;
461                               attr = attr->next )
462                                 value_add_one( &c->rvalue_vals,
463                                                &attr->attr->ad_cname );
464                 rc = 0;
465                 break;
466         case LDAP_MOD_DELETE:
467                 if ( legacy
468                      && (c->type == UNIQUE_IGNORE) == legacy->ignore
469                      && legacy->uri
470                      && legacy->uri->attrs) {
471                         if ( c->valx < 0 ) { /* delete all */
472                                 for ( attr = legacy->uri->attrs;
473                                       attr;
474                                       attr = next_attr ) {
475                                         next_attr = attr->next;
476                                         ch_free ( attr );
477                                 }
478                                 legacy->uri->attrs = NULL;
479                         } else { /* delete by index */
480                                 attrp = &legacy->uri->attrs;
481                                 for ( i=0; i < c->valx; ++i )
482                                         attrp = &(*attrp)->next;
483                                 attr = *attrp;
484                                 *attrp = attr->next;
485                                 ch_free (attr);
486                         }
487                         if ( !legacy->uri->attrs
488                              && !legacy->uri->dn ) {
489                                 unique_free_domain_uri ( legacy->uri );
490                                 legacy->uri = NULL;
491                         }
492                         if ( !legacy->uri && !private->legacy_strict_set ) {
493                                 unique_free_domain ( legacy );
494                                 private->legacy = legacy = NULL;
495                         }
496                 }
497                 rc = 0;
498                 break;
499         case LDAP_MOD_ADD:
500         case SLAP_CONFIG_ADD:
501                 if ( domains ) {
502                         sprintf ( c->cr_msg,
503                                   "cannot set legacy attrs when URIs are present" );
504                         Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
505                                 c->cr_msg, NULL, NULL );
506                         rc = ARG_BAD_CONF;
507                         break;
508                 }
509                 if ( legacy
510                      && legacy->uri
511                      && legacy->uri->attrs
512                      && (c->type == UNIQUE_IGNORE) != legacy->ignore ) {
513                         sprintf ( c->cr_msg,
514                                   "cannot set both attrs and ignore-attrs" );
515                         Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
516                                 c->cr_msg, NULL, NULL );
517                         rc = ARG_BAD_CONF;
518                         break;
519                 }
520                 if ( !legacy ) {
521                         unique_new_domain ( &private->legacy,
522                                             UNIQUE_DEFAULT_URI,
523                                             c );
524                         legacy = private->legacy;
525                 }
526                 if ( !legacy->uri )
527                         unique_new_domain_uri_basic ( &legacy->uri, c );
528                 rc = 0;
529                 for ( i=1; c->argv[i]; ++i ) {
530                         AttributeDescription * ad = NULL;
531                         const char * text;
532                         if ( slap_str2ad ( c->argv[i], &ad, &text )
533                              == LDAP_SUCCESS) {
534
535                                 attr = ch_calloc ( 1,
536                                         sizeof ( unique_attrs ) );
537                                 attr->attr = ad;
538                                 attr->next = new_attrs;
539                                 new_attrs = attr;
540                         } else {
541                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
542                                           "unique: attribute: %s: %s",
543                                           c->argv[i], text );
544                                 for ( attr = new_attrs;
545                                       attr;
546                                       attr=next_attr ) {
547                                         next_attr = attr->next;
548                                         ch_free ( attr );
549                                 }
550                                 rc = ARG_BAD_CONF;
551                                 break;
552                         }
553                 }
554                 if ( rc ) break;
555
556                 /* (nconc legacy->uri->attrs (nreverse new_attrs)) */
557                 reverse_attrs = NULL;
558                 for ( attr = new_attrs;
559                       attr;
560                       attr = next_attr ) {
561                         next_attr = attr->next;
562                         attr->next = reverse_attrs;
563                         reverse_attrs = attr;
564                 }
565                 for ( attrp = &legacy->uri->attrs;
566                       *attrp;
567                       attrp = &(*attrp)->next ) ;
568                 *attrp = reverse_attrs;
569
570                 legacy->ignore = ( c->type == UNIQUE_IGNORE );
571                 break;
572         default:
573                 abort();
574         }
575
576         if ( rc ) {
577                 Debug ( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
578                         "%s: %s\n", c->log, c->cr_msg, 0 );
579         }
580         return rc;
581 }
582
583 static int
584 unique_cf_strict( ConfigArgs *c )
585 {
586         slap_overinst *on = (slap_overinst *)c->bi;
587         unique_data *private = (unique_data *) on->on_bi.bi_private;
588         unique_domain *domains = private->domains;
589         unique_domain *legacy = private->legacy;
590         int rc = ARG_BAD_CONF;
591
592         switch ( c->op ) {
593         case SLAP_CONFIG_EMIT:
594                 /* We process the boolean manually instead of using
595                  * ARG_ON_OFF so that we can three-state it;
596                  * olcUniqueStrict is either TRUE, FALSE, or missing,
597                  * and missing is necessary to add olcUniqueURIs...
598                  */
599                 if ( private->legacy_strict_set ) {
600                         struct berval bv;
601                         bv.bv_val = legacy->strict ? "TRUE" : "FALSE";
602                         bv.bv_len = legacy->strict ?
603                                 STRLENOF("TRUE") :
604                                 STRLENOF("FALSE");
605                         value_add_one ( &c->rvalue_vals, &bv );
606                 }
607                 rc = 0;
608                 break;
609         case LDAP_MOD_DELETE:
610                 if ( legacy ) {
611                         legacy->strict = 0;
612                         if ( ! legacy->uri ) {
613                                 unique_free_domain ( legacy );
614                                 private->legacy = NULL;
615                         }
616                 }
617                 private->legacy_strict_set = 0;
618                 rc = 0;
619                 break;
620         case LDAP_MOD_ADD:
621         case SLAP_CONFIG_ADD:
622                 if ( domains ) {
623                         sprintf ( c->cr_msg,
624                                   "cannot set legacy attrs when URIs are present" );
625                         Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
626                                 c->cr_msg, NULL, NULL );
627                         rc = ARG_BAD_CONF;
628                         break;
629                 }
630                 if ( ! legacy ) {
631                         unique_new_domain ( &private->legacy,
632                                             UNIQUE_DEFAULT_URI,
633                                             c );
634                         legacy = private->legacy;
635                 }
636                 /* ... not using ARG_ON_OFF makes this necessary too */
637                 assert ( c->argc == 2 );
638                 legacy->strict = (strcasecmp ( c->argv[1], "TRUE" ) == 0);
639                 private->legacy_strict_set = 1;
640                 rc = 0;
641                 break;
642         default:
643                 abort();
644         }
645
646         return rc;
647 }
648
649 static int
650 unique_cf_uri( ConfigArgs *c )
651 {
652         slap_overinst *on = (slap_overinst *)c->bi;
653         unique_data *private = (unique_data *) on->on_bi.bi_private;
654         unique_domain *domains = private->domains;
655         unique_domain *legacy = private->legacy;
656         unique_domain *domain = NULL, **domainp = NULL;
657         int rc = ARG_BAD_CONF;
658         int i;
659
660         switch ( c->op ) {
661         case SLAP_CONFIG_EMIT:
662                 for ( domain = domains;
663                       domain;
664                       domain = domain->next ) {
665                         rc = value_add_one ( &c->rvalue_vals,
666                                              domain->domain_spec );
667                         if ( rc ) break;
668                         rc = value_add_one ( &c->rvalue_nvals,
669                                              domain->domain_spec );
670                         if ( rc ) break;
671                 }
672                 break;
673         case LDAP_MOD_DELETE:
674                 if ( c->valx < 0 ) { /* delete them all! */
675                         unique_free_domain ( domains );
676                         private->domains = NULL;
677                 } else { /* delete just one */
678                         domainp = &private->domains;
679                         for ( i=0; i < c->valx && *domainp; ++i )
680                                 domainp = &(*domainp)->next;
681
682                         /* If *domainp is null, we walked off the end
683                          * of the list.  This happens when back-config
684                          * and the overlay are out-of-sync, like when
685                          * rejecting changes before ITS#4752 gets
686                          * fixed.
687                          *
688                          * This should never happen, but will appear
689                          * if you backport this version of
690                          * slapo-unique without the config-undo fixes
691                          *
692                          * test024 Will hit this case in such a
693                          * situation.
694                          */
695                         assert (*domainp != NULL);
696
697                         domain = *domainp;
698                         *domainp = domain->next;
699                         domain->next = NULL;
700                         unique_free_domain ( domain );
701                 }
702                 rc = 0;
703                 break;
704
705         case SLAP_CONFIG_ADD: /* fallthrough */
706         case LDAP_MOD_ADD:
707                 if ( legacy ) {
708                         sprintf ( c->cr_msg,
709                                   "cannot set Uri when legacy attrs are present" );
710                         Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
711                                 c->cr_msg, NULL, NULL );
712                         rc = ARG_BAD_CONF;
713                         break;
714                 }
715                 rc = 0;
716                 if ( c->line ) rc = unique_new_domain ( &domain, c->line, c );
717                 else rc = unique_new_domain ( &domain, c->argv[1], c );
718                 if ( rc ) break;
719                 assert ( domain->next == NULL );
720                 for ( domainp = &private->domains;
721                       *domainp;
722                       domainp = &(*domainp)->next ) ;
723                 *domainp = domain;
724
725                 break;
726
727         default:
728                 abort ();
729         }
730
731         return rc;
732 }
733
734 /*
735 ** allocate new unique_data;
736 ** initialize, copy basedn;
737 ** store in on_bi.bi_private;
738 **
739 */
740
741 static int
742 unique_db_init(
743         BackendDB       *be,
744         ConfigReply     *cr
745 )
746 {
747         slap_overinst *on = (slap_overinst *)be->bd_info;
748         unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
749
750         Debug(LDAP_DEBUG_TRACE, "==> unique_db_init\n", 0, 0, 0);
751
752         *privatep = ch_calloc ( 1, sizeof ( unique_data ) );
753
754         return 0;
755 }
756
757 static int
758 unique_db_destroy(
759         BackendDB       *be,
760         ConfigReply     *cr
761 )
762 {
763         slap_overinst *on = (slap_overinst *)be->bd_info;
764         unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
765         unique_data *private = *privatep;
766
767         Debug(LDAP_DEBUG_TRACE, "==> unique_db_destroy\n", 0, 0, 0);
768
769         if ( private ) {
770                 unique_domain *domains = private->domains;
771                 unique_domain *legacy = private->legacy;
772
773                 unique_free_domain ( domains );
774                 unique_free_domain ( legacy );
775                 ch_free ( private );
776                 *privatep = NULL;
777         }
778
779         return 0;
780 }
781
782 static int
783 unique_open(
784         BackendDB *be,
785         ConfigReply *cr
786 )
787 {
788         Debug(LDAP_DEBUG_TRACE, "unique_open: overlay initialized\n", 0, 0, 0);
789
790         return 0;
791 }
792
793
794 /*
795 ** Leave unique_data but wipe out config
796 **
797 */
798
799 static int
800 unique_close(
801         BackendDB *be,
802         ConfigReply *cr
803 )
804 {
805         slap_overinst *on       = (slap_overinst *) be->bd_info;
806         unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
807         unique_data *private = *privatep;
808
809         Debug(LDAP_DEBUG_TRACE, "==> unique_close\n", 0, 0, 0);
810
811         if ( private ) {
812                 unique_domain *domains = private->domains;
813                 unique_domain *legacy = private->legacy;
814
815                 unique_free_domain ( domains );
816                 unique_free_domain ( legacy );
817                 memset ( private, 0, sizeof ( unique_data ) );
818         }
819
820         return ( 0 );
821 }
822
823
824 /*
825 ** search callback
826 **      if this is a REP_SEARCH, count++;
827 **
828 */
829
830 static int count_attr_cb(
831         Operation *op,
832         SlapReply *rs
833 )
834 {
835         unique_counter *uc;
836
837         /* because you never know */
838         if(!op || !rs) return(0);
839
840         /* Only search entries are interesting */
841         if(rs->sr_type != REP_SEARCH) return(0);
842
843         uc = op->o_callback->sc_private;
844
845         /* Ignore the current entry */
846         if ( dn_match( uc->ndn, &rs->sr_entry->e_nname )) return(0);
847
848         Debug(LDAP_DEBUG_TRACE, "==> count_attr_cb <%s>\n",
849                 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "UNKNOWN_DN", 0, 0);
850
851         uc->count++;
852
853         return(0);
854 }
855
856 /* count the length of one attribute ad
857  * (and all of its values b)
858  * in the proposed filter
859  */
860 static int
861 count_filter_len(
862         unique_domain *domain,
863         unique_domain_uri *uri,
864         AttributeDescription *ad,
865         BerVarray b
866 )
867 {
868         unique_attrs *attr;
869         int i;
870         int ks = 0;
871
872         while ( !is_at_operational( ad->ad_type ) ) {
873                 if ( uri->attrs ) {
874                         for ( attr = uri->attrs; attr; attr = attr->next ) {
875                                 if ( ad == attr->attr ) {
876                                         break;
877                                 }
878                         }
879                         if ( ( domain->ignore && attr )
880                              || (!domain->ignore && !attr )) {
881                                 break;
882                         }
883                 }
884                 if ( b && b[0].bv_val ) {
885                         for (i = 0; b[i].bv_val; i++ ) {
886                                 /* note: make room for filter escaping... */
887                                 ks += ( 3 * b[i].bv_len ) + ad->ad_cname.bv_len + STRLENOF( "(=)" );
888                         }
889                 } else if ( domain->strict ) {
890                         ks += ad->ad_cname.bv_len + STRLENOF( "(=*)" ); /* (attr=*) */
891                 }
892                 break;
893         }
894
895         return ks;
896 }
897
898 static char *
899 build_filter(
900         unique_domain *domain,
901         unique_domain_uri *uri,
902         AttributeDescription *ad,
903         BerVarray b,
904         char *kp,
905         void *ctx
906 )
907 {
908         unique_attrs *attr;
909         int i;
910
911         while ( !is_at_operational( ad->ad_type ) ) {
912                 if ( uri->attrs ) {
913                         for ( attr = uri->attrs; attr; attr = attr->next ) {
914                                 if ( ad == attr->attr ) {
915                                         break;
916                                 }
917                         }
918                         if ( ( domain->ignore && attr )
919                              || (!domain->ignore && !attr )) {
920                                 break;
921                         }
922                 }
923                 if ( b && b[0].bv_val ) {
924                         for ( i = 0; b[i].bv_val; i++ ) {
925                                 struct berval   bv;
926
927                                 ldap_bv2escaped_filter_value_x( &b[i], &bv, 1, ctx );
928                                 kp += sprintf( kp, "(%s=%s)", ad->ad_cname.bv_val, bv.bv_val );
929                                 if ( bv.bv_val != b[i].bv_val ) {
930                                         ber_memfree_x( bv.bv_val, ctx );
931                                 }
932                         }
933                 } else if ( domain->strict ) {
934                         kp += sprintf( kp, "(%s=*)", ad->ad_cname.bv_val );
935                 }
936                 break;
937         }
938         return kp;
939 }
940
941 static int
942 unique_search(
943         Operation *op,
944         Operation *nop,
945         struct berval * dn,
946         int scope,
947         SlapReply *rs,
948         char *key
949 )
950 {
951         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
952         SlapReply nrs = { REP_RESULT };
953         slap_callback cb = { NULL, NULL, NULL, NULL }; /* XXX */
954         unique_counter uq = { NULL, 0 };
955         int rc;
956
957         Debug(LDAP_DEBUG_TRACE, "==> unique_search %s\n", key, 0, 0);
958
959         nop->ors_filter = str2filter_x(nop, key);
960         ber_str2bv(key, 0, 0, &nop->ors_filterstr);
961
962         cb.sc_response  = (slap_response*)count_attr_cb;
963         cb.sc_private   = &uq;
964         nop->o_callback = &cb;
965         nop->o_tag      = LDAP_REQ_SEARCH;
966         nop->ors_scope  = scope;
967         nop->ors_deref  = LDAP_DEREF_NEVER;
968         nop->ors_limit  = NULL;
969         nop->ors_slimit = SLAP_NO_LIMIT;
970         nop->ors_tlimit = SLAP_NO_LIMIT;
971         nop->ors_attrs  = slap_anlist_no_attrs;
972         nop->ors_attrsonly = 1;
973
974         uq.ndn = &op->o_req_ndn;
975
976         nop->o_req_ndn = *dn;
977         nop->o_ndn = op->o_bd->be_rootndn;
978
979         nop->o_bd = on->on_info->oi_origdb;
980         rc = nop->o_bd->be_search(nop, &nrs);
981         filter_free_x(nop, nop->ors_filter);
982         op->o_tmpfree( key, op->o_tmpmemctx );
983
984         if(rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT) {
985                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
986                 send_ldap_error(op, rs, rc, "unique_search failed");
987                 return(rs->sr_err);
988         }
989
990         Debug(LDAP_DEBUG_TRACE, "=> unique_search found %d records\n", uq.count, 0, 0);
991
992         if(uq.count) {
993                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
994                 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
995                         "some attributes not unique");
996                 return(rs->sr_err);
997         }
998
999         return(SLAP_CB_CONTINUE);
1000 }
1001
1002 static int
1003 unique_add(
1004         Operation *op,
1005         SlapReply *rs
1006 )
1007 {
1008         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1009         unique_data *private = (unique_data *) on->on_bi.bi_private;
1010         unique_domain *domains = private->domains;
1011         unique_domain *legacy = private->legacy;
1012         unique_domain *domain;
1013         Operation nop = *op;
1014         Attribute *a;
1015         char *key, *kp;
1016         int rc = SLAP_CB_CONTINUE;
1017
1018         Debug(LDAP_DEBUG_TRACE, "==> unique_add <%s>\n",
1019               op->o_req_dn.bv_val, 0, 0);
1020
1021         for ( domain = legacy ? legacy : domains;
1022               domain;
1023               domain = domain->next ) {
1024                 unique_domain_uri *uri;
1025                 int ks = 0;
1026
1027                 for ( uri = domain->uri;
1028                       uri;
1029                       uri = uri->next ) {
1030
1031                         if ( uri->ndn
1032                              && !dnIsSuffix( &op->o_req_ndn, uri->ndn ))
1033                                 continue;
1034
1035                         if(!(a = op->ora_e->e_attrs)) {
1036                                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1037                                 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1038                                                 "unique_add() got null op.ora_e.e_attrs");
1039                                 rc = rs->sr_err;
1040                                 break;
1041
1042                         } else {
1043                                 for(; a; a = a->a_next) {
1044                                         ks += count_filter_len ( domain,
1045                                                                  uri,
1046                                                                  a->a_desc,
1047                                                                  a->a_vals);
1048                                 }
1049                         }
1050
1051                         /* skip this domain-uri if it isn't involved */
1052                         if ( !ks ) continue;
1053
1054                         if ( uri->filter && uri->filter->bv_len )
1055                                 ks += uri->filter->bv_len + STRLENOF ("(&)");
1056                         kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1057
1058                         if ( uri->filter && uri->filter->bv_len )
1059                                 kp += sprintf (kp, "(&%s", uri->filter->bv_val);
1060                         kp += sprintf(kp, "(|");
1061
1062                         for(a = op->ora_e->e_attrs; a; a = a->a_next)
1063                                 kp = build_filter(domain,
1064                                                   uri,
1065                                                   a->a_desc,
1066                                                   a->a_vals,
1067                                                   kp,
1068                                                   op->o_tmpmemctx);
1069
1070                         kp += sprintf(kp, ")");
1071                         if ( uri->filter && uri->filter->bv_len )
1072                                 kp += sprintf (kp, ")");
1073
1074                         rc = unique_search ( op,
1075                                              &nop,
1076                                              uri->ndn ?
1077                                              uri->ndn :
1078                                              &op->o_bd->be_nsuffix[0],
1079                                              uri->scope,
1080                                              rs,
1081                                              key);
1082
1083                         if ( rc != SLAP_CB_CONTINUE ) break;
1084                 }
1085                 if ( rc != SLAP_CB_CONTINUE ) break;
1086         }
1087
1088         return rc;
1089 }
1090
1091
1092 static int
1093 unique_modify(
1094         Operation *op,
1095         SlapReply *rs
1096 )
1097 {
1098         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1099         unique_data *private = (unique_data *) on->on_bi.bi_private;
1100         unique_domain *domains = private->domains;
1101         unique_domain *legacy = private->legacy;
1102         unique_domain *domain;
1103         Operation nop = *op;
1104         Modifications *m;
1105         char *key, *kp;
1106         int rc = SLAP_CB_CONTINUE;
1107
1108         Debug(LDAP_DEBUG_TRACE, "==> unique_modify <%s>\n",
1109               op->o_req_dn.bv_val, 0, 0);
1110
1111         for ( domain = legacy ? legacy : domains;
1112               domain;
1113               domain = domain->next ) {
1114                 unique_domain_uri *uri;
1115                 int ks = 0;
1116
1117                 for ( uri = domain->uri;
1118                       uri;
1119                       uri = uri->next ) {
1120
1121                         if ( uri->ndn
1122                              && !dnIsSuffix( &op->o_req_ndn, uri->ndn ))
1123                                 continue;
1124
1125                         if ( !(m = op->orm_modlist) ) {
1126                                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1127                                 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1128                                                 "unique_modify() got null op.orm_modlist");
1129                                 rc = rs->sr_err;
1130                                 break;
1131
1132                         } else
1133                                 for ( ; m; m = m->sml_next)
1134                                         if ( (m->sml_op & LDAP_MOD_OP)
1135                                              != LDAP_MOD_DELETE )
1136                                                 ks += count_filter_len
1137                                                         ( domain,
1138                                                           uri,
1139                                                           m->sml_desc,
1140                                                           m->sml_values);
1141
1142                         /* skip this domain-uri if it isn't involved */
1143                         if ( !ks ) continue;
1144
1145                         if ( uri->filter && uri->filter->bv_len )
1146                                 ks += uri->filter->bv_len + STRLENOF ("(&)");
1147                         kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1148
1149                         if ( uri->filter && uri->filter->bv_len )
1150                                 kp += sprintf (kp, "(&%s", uri->filter->bv_val);
1151                         kp += sprintf(kp, "(|");
1152
1153                         for(m = op->orm_modlist; m; m = m->sml_next)
1154                                 if ( (m->sml_op & LDAP_MOD_OP)
1155                                      != LDAP_MOD_DELETE )
1156                                         kp = build_filter ( domain,
1157                                                             uri,
1158                                                             m->sml_desc,
1159                                                             m->sml_values,
1160                                                             kp,
1161                                                             op->o_tmpmemctx );
1162
1163                         kp += sprintf (kp, ")");
1164                         if ( uri->filter && uri->filter->bv_len )
1165                                 kp += sprintf (kp, ")");
1166
1167                         rc = unique_search ( op,
1168                                              &nop,
1169                                              uri->ndn ?
1170                                              uri->ndn :
1171                                              &op->o_bd->be_nsuffix[0],
1172                                              uri->scope,
1173                                              rs,
1174                                              key);
1175
1176                         if ( rc != SLAP_CB_CONTINUE ) break;
1177                 }
1178                 if ( rc != SLAP_CB_CONTINUE ) break;
1179         }
1180
1181         return rc;
1182 }
1183
1184
1185 static int
1186 unique_modrdn(
1187         Operation *op,
1188         SlapReply *rs
1189 )
1190 {
1191         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1192         unique_data *private = (unique_data *) on->on_bi.bi_private;
1193         unique_domain *domains = private->domains;
1194         unique_domain *legacy = private->legacy;
1195         unique_domain *domain;
1196         Operation nop = *op;
1197         char *key, *kp;
1198         LDAPRDN newrdn;
1199         struct berval bv[2];
1200         int rc = SLAP_CB_CONTINUE;
1201
1202         Debug(LDAP_DEBUG_TRACE, "==> unique_modrdn <%s> <%s>\n",
1203                 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
1204
1205         for ( domain = legacy ? legacy : domains;
1206               domain;
1207               domain = domain->next ) {
1208                 unique_domain_uri *uri;
1209                 int ks = 0;
1210
1211                 for ( uri = domain->uri;
1212                       uri;
1213                       uri = uri->next ) {
1214                         int i;
1215
1216                         if ( uri->ndn
1217                              && !dnIsSuffix( &op->o_req_ndn, uri->ndn )
1218                              && (!op->orr_nnewSup
1219                                  || !dnIsSuffix( op->orr_nnewSup, uri->ndn )))
1220                                 continue;
1221
1222                         if ( ldap_bv2rdn_x ( &op->oq_modrdn.rs_newrdn,
1223                                              &newrdn,
1224                                              (char **)&rs->sr_text,
1225                                              LDAP_DN_FORMAT_LDAP,
1226                                              op->o_tmpmemctx ) ) {
1227                                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1228                                 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1229                                                 "unknown type(s) used in RDN");
1230                                 rc = rs->sr_err;
1231                                 break;
1232                         }
1233
1234                         rc = SLAP_CB_CONTINUE;
1235                         for ( i=0; newrdn[i]; i++) {
1236                                 AttributeDescription *ad = NULL;
1237                                 if ( slap_bv2ad( &newrdn[i]->la_attr, &ad, &rs->sr_text )) {
1238                                         ldap_rdnfree_x( newrdn, op->o_tmpmemctx );
1239                                         rs->sr_err = LDAP_INVALID_SYNTAX;
1240                                         send_ldap_result( op, rs );
1241                                         rc = rs->sr_err;
1242                                         break;
1243                                 }
1244                                 newrdn[i]->la_private = ad;
1245                         }
1246                         if ( rc != SLAP_CB_CONTINUE ) break;
1247
1248                         bv[1].bv_val = NULL;
1249                         bv[1].bv_len = 0;
1250
1251                         for ( i=0; newrdn[i]; i++ ) {
1252                                 bv[0] = newrdn[i]->la_value;
1253                                 ks += count_filter_len ( domain,
1254                                                          uri,
1255                                                          newrdn[i]->la_private,
1256                                                          bv);
1257                         }
1258
1259                         /* skip this domain if it isn't involved */
1260                         if ( !ks ) continue;
1261
1262                         if ( uri->filter && uri->filter->bv_len )
1263                                 ks += uri->filter->bv_len + STRLENOF ("(&)");
1264                         kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1265
1266                         if ( uri->filter && uri->filter->bv_len )
1267                                 kp += sprintf (kp, "(&%s", uri->filter->bv_val);
1268                         kp += sprintf(kp, "(|");
1269
1270                         for ( i=0; newrdn[i]; i++) {
1271                                 bv[0] = newrdn[i]->la_value;
1272                                 kp = build_filter ( domain,
1273                                                     uri,
1274                                                     newrdn[i]->la_private,
1275                                                     bv,
1276                                                     kp,
1277                                                     op->o_tmpmemctx);
1278                         }
1279
1280                         kp += sprintf(kp, ")");
1281                         if ( uri->filter && uri->filter->bv_len )
1282                                 kp += sprintf (kp, ")");
1283
1284                         rc = unique_search ( op,
1285                                              &nop,
1286                                              uri->ndn ?
1287                                              uri->ndn :
1288                                              &op->o_bd->be_nsuffix[0],
1289                                              uri->scope,
1290                                              rs,
1291                                              key);
1292
1293                         if ( rc != SLAP_CB_CONTINUE ) break;
1294                 }
1295                 if ( rc != SLAP_CB_CONTINUE ) break;
1296         }
1297
1298         return rc;
1299 }
1300
1301 /*
1302 ** init_module is last so the symbols resolve "for free" --
1303 ** it expects to be called automagically during dynamic module initialization
1304 */
1305
1306 int
1307 unique_initialize()
1308 {
1309         int rc;
1310
1311         /* statically declared just after the #includes at top */
1312         memset (&unique, 0, sizeof(unique));
1313
1314         unique.on_bi.bi_type = "unique";
1315         unique.on_bi.bi_db_init = unique_db_init;
1316         unique.on_bi.bi_db_destroy = unique_db_destroy;
1317         unique.on_bi.bi_db_open = unique_open;
1318         unique.on_bi.bi_db_close = unique_close;
1319         unique.on_bi.bi_op_add = unique_add;
1320         unique.on_bi.bi_op_modify = unique_modify;
1321         unique.on_bi.bi_op_modrdn = unique_modrdn;
1322
1323         unique.on_bi.bi_cf_ocs = uniqueocs;
1324         rc = config_register_schema( uniquecfg, uniqueocs );
1325         if ( rc ) return rc;
1326
1327         return(overlay_register(&unique));
1328 }
1329
1330 #if SLAPD_OVER_UNIQUE == SLAPD_MOD_DYNAMIC && defined(PIC)
1331 int init_module(int argc, char *argv[]) {
1332         return unique_initialize();
1333 }
1334 #endif
1335
1336 #endif /* SLAPD_OVER_UNIQUE */