]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/unique.c
Enhanced unique configuration
[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->msg, sizeof( c->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->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->msg, sizeof( c->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->msg, sizeof( c->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->msg, sizeof( c->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->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->msg, sizeof( c->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->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->msg,
405                                   "cannot set legacy attrs when URIs are present" );
406                         Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
407                                 c->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->msg,
414                                   "dn is not a suffix of backend base" );
415                         Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
416                                 c->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->msg,
503                                   "cannot set legacy attrs when URIs are present" );
504                         Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
505                                 c->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->msg,
514                                   "cannot set both attrs and ignore-attrs" );
515                         Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
516                                 c->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->msg, sizeof( c->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->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->msg,
624                                   "cannot set legacy attrs when URIs are present" );
625                         Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
626                                 c->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);
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->msg,
709                                   "cannot set Uri when legacy attrs are present" );
710                         Debug ( LDAP_DEBUG_CONFIG, "unique config: %s\n",
711                                 c->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 )
745 {
746         slap_overinst *on = (slap_overinst *)be->bd_info;
747         unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
748
749         Debug(LDAP_DEBUG_TRACE, "==> unique_db_init\n", 0, 0, 0);
750
751         *privatep = ch_calloc ( 1, sizeof ( unique_data ) );
752
753         return 0;
754 }
755
756 static int
757 unique_db_destroy(
758         BackendDB       *be
759 )
760 {
761         slap_overinst *on = (slap_overinst *)be->bd_info;
762         unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
763         unique_data *private = *privatep;
764
765         Debug(LDAP_DEBUG_TRACE, "==> unique_db_destroy\n", 0, 0, 0);
766
767         if ( private ) {
768                 unique_domain *domains = private->domains;
769                 unique_domain *legacy = private->legacy;
770
771                 unique_free_domain ( domains );
772                 unique_free_domain ( legacy );
773                 ch_free ( private );
774                 *privatep = NULL;
775         }
776
777         return 0;
778 }
779
780 static int
781 unique_open(
782         BackendDB *be
783 )
784 {
785         Debug(LDAP_DEBUG_TRACE, "unique_open: overlay initialized\n", 0, 0, 0);
786
787         return 0;
788 }
789
790
791 /*
792 ** Leave unique_data but wipe out config
793 **
794 */
795
796 static int
797 unique_close(
798         BackendDB *be
799 )
800 {
801         slap_overinst *on       = (slap_overinst *) be->bd_info;
802         unique_data **privatep = (unique_data **) &on->on_bi.bi_private;
803         unique_data *private = *privatep;
804
805         Debug(LDAP_DEBUG_TRACE, "==> unique_close\n", 0, 0, 0);
806
807         if ( private ) {
808                 unique_domain *domains = private->domains;
809                 unique_domain *legacy = private->legacy;
810
811                 unique_free_domain ( domains );
812                 unique_free_domain ( legacy );
813                 memset ( private, 0, sizeof ( unique_data ) );
814         }
815
816         return ( 0 );
817 }
818
819
820 /*
821 ** search callback
822 **      if this is a REP_SEARCH, count++;
823 **
824 */
825
826 static int count_attr_cb(
827         Operation *op,
828         SlapReply *rs
829 )
830 {
831         unique_counter *uc;
832
833         /* because you never know */
834         if(!op || !rs) return(0);
835
836         /* Only search entries are interesting */
837         if(rs->sr_type != REP_SEARCH) return(0);
838
839         uc = op->o_callback->sc_private;
840
841         /* Ignore the current entry */
842         if ( dn_match( uc->ndn, &rs->sr_entry->e_nname )) return(0);
843
844         Debug(LDAP_DEBUG_TRACE, "==> count_attr_cb <%s>\n",
845                 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "UNKNOWN_DN", 0, 0);
846
847         uc->count++;
848
849         return(0);
850 }
851
852 /* count the length of one attribute ad
853  * (and all of its values b)
854  * in the proposed filter
855  */
856 static int
857 count_filter_len(
858         unique_domain *domain,
859         unique_domain_uri *uri,
860         AttributeDescription *ad,
861         BerVarray b
862 )
863 {
864         unique_attrs *attr;
865         int i;
866         int ks = 0;
867
868         while ( !is_at_operational( ad->ad_type ) ) {
869                 if ( uri->attrs ) {
870                         for ( attr = uri->attrs; attr; attr = attr->next ) {
871                                 if ( ad == attr->attr ) {
872                                         break;
873                                 }
874                         }
875                         if ( ( domain->ignore && attr )
876                              || (!domain->ignore && !attr )) {
877                                 break;
878                         }
879                 }
880                 if ( b && b[0].bv_val ) {
881                         for (i = 0; b[i].bv_val; i++ ) {
882                                 /* note: make room for filter escaping... */
883                                 ks += ( 3 * b[i].bv_len ) + ad->ad_cname.bv_len + STRLENOF( "(=)" );
884                         }
885                 } else if ( domain->strict ) {
886                         ks += ad->ad_cname.bv_len + STRLENOF( "(=*)" ); /* (attr=*) */
887                 }
888                 break;
889         }
890
891         return ks;
892 }
893
894 static char *
895 build_filter(
896         unique_domain *domain,
897         unique_domain_uri *uri,
898         AttributeDescription *ad,
899         BerVarray b,
900         char *kp,
901         void *ctx
902 )
903 {
904         unique_attrs *attr;
905         int i;
906
907         while ( !is_at_operational( ad->ad_type ) ) {
908                 if ( uri->attrs ) {
909                         for ( attr = uri->attrs; attr; attr = attr->next ) {
910                                 if ( ad == attr->attr ) {
911                                         break;
912                                 }
913                         }
914                         if ( ( domain->ignore && attr )
915                              || (!domain->ignore && !attr )) {
916                                 break;
917                         }
918                 }
919                 if ( b && b[0].bv_val ) {
920                         for ( i = 0; b[i].bv_val; i++ ) {
921                                 struct berval   bv;
922
923                                 ldap_bv2escaped_filter_value_x( &b[i], &bv, 1, ctx );
924                                 kp += sprintf( kp, "(%s=%s)", ad->ad_cname.bv_val, bv.bv_val );
925                                 if ( bv.bv_val != b[i].bv_val ) {
926                                         ber_memfree_x( bv.bv_val, ctx );
927                                 }
928                         }
929                 } else if ( domain->strict ) {
930                         kp += sprintf( kp, "(%s=*)", ad->ad_cname.bv_val );
931                 }
932                 break;
933         }
934         return kp;
935 }
936
937 static int
938 unique_search(
939         Operation *op,
940         Operation *nop,
941         struct berval * dn,
942         int scope,
943         SlapReply *rs,
944         char *key
945 )
946 {
947         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
948         SlapReply nrs = { REP_RESULT };
949         slap_callback cb = { NULL, NULL, NULL, NULL }; /* XXX */
950         unique_counter uq = { NULL, 0 };
951         int rc;
952
953         Debug(LDAP_DEBUG_TRACE, "==> unique_search %s\n", key, 0, 0);
954
955         nop->ors_filter = str2filter_x(nop, key);
956         ber_str2bv(key, 0, 0, &nop->ors_filterstr);
957
958         cb.sc_response  = (slap_response*)count_attr_cb;
959         cb.sc_private   = &uq;
960         nop->o_callback = &cb;
961         nop->o_tag      = LDAP_REQ_SEARCH;
962         nop->ors_scope  = scope;
963         nop->ors_deref  = LDAP_DEREF_NEVER;
964         nop->ors_limit  = NULL;
965         nop->ors_slimit = SLAP_NO_LIMIT;
966         nop->ors_tlimit = SLAP_NO_LIMIT;
967         nop->ors_attrs  = slap_anlist_no_attrs;
968         nop->ors_attrsonly = 1;
969
970         uq.ndn = &op->o_req_ndn;
971
972         nop->o_req_ndn = *dn;
973         nop->o_ndn = op->o_bd->be_rootndn;
974
975         nop->o_bd = on->on_info->oi_origdb;
976         rc = nop->o_bd->be_search(nop, &nrs);
977         filter_free_x(nop, nop->ors_filter);
978         op->o_tmpfree( key, op->o_tmpmemctx );
979
980         if(rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT) {
981                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
982                 send_ldap_error(op, rs, rc, "unique_search failed");
983                 return(rs->sr_err);
984         }
985
986         Debug(LDAP_DEBUG_TRACE, "=> unique_search found %d records\n", uq.count, 0, 0);
987
988         if(uq.count) {
989                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
990                 send_ldap_error(op, rs, LDAP_CONSTRAINT_VIOLATION,
991                         "some attributes not unique");
992                 return(rs->sr_err);
993         }
994
995         return(SLAP_CB_CONTINUE);
996 }
997
998 static int
999 unique_add(
1000         Operation *op,
1001         SlapReply *rs
1002 )
1003 {
1004         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1005         unique_data *private = (unique_data *) on->on_bi.bi_private;
1006         unique_domain *domains = private->domains;
1007         unique_domain *legacy = private->legacy;
1008         unique_domain *domain;
1009         Operation nop = *op;
1010         Attribute *a;
1011         char *key, *kp;
1012         int rc = SLAP_CB_CONTINUE;
1013
1014         Debug(LDAP_DEBUG_TRACE, "==> unique_add <%s>\n",
1015               op->o_req_dn.bv_val, 0, 0);
1016
1017         for ( domain = legacy ? legacy : domains;
1018               domain;
1019               domain = domain->next ) {
1020                 unique_domain_uri *uri;
1021                 int ks = 0;
1022
1023                 for ( uri = domain->uri;
1024                       uri;
1025                       uri = uri->next ) {
1026
1027                         if ( uri->ndn
1028                              && !dnIsSuffix( &op->o_req_ndn, uri->ndn ))
1029                                 continue;
1030
1031                         if(!(a = op->ora_e->e_attrs)) {
1032                                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1033                                 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1034                                                 "unique_add() got null op.ora_e.e_attrs");
1035                                 rc = rs->sr_err;
1036                                 break;
1037
1038                         } else {
1039                                 for(; a; a = a->a_next) {
1040                                         ks += count_filter_len ( domain,
1041                                                                  uri,
1042                                                                  a->a_desc,
1043                                                                  a->a_vals);
1044                                 }
1045                         }
1046
1047                         /* skip this domain-uri if it isn't involved */
1048                         if ( !ks ) continue;
1049
1050                         if ( uri->filter && uri->filter->bv_len )
1051                                 ks += uri->filter->bv_len + STRLENOF ("(&)");
1052                         kp = key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1053
1054                         if ( uri->filter && uri->filter->bv_len )
1055                                 kp += sprintf (kp, "(&%s", uri->filter->bv_val);
1056                         kp += sprintf(kp, "(|");
1057
1058                         for(a = op->ora_e->e_attrs; a; a = a->a_next)
1059                                 kp = build_filter(domain,
1060                                                   uri,
1061                                                   a->a_desc,
1062                                                   a->a_vals,
1063                                                   kp,
1064                                                   op->o_tmpmemctx);
1065
1066                         kp += sprintf(kp, ")");
1067                         if ( uri->filter && uri->filter->bv_len )
1068                                 kp += sprintf (kp, ")");
1069
1070                         rc = unique_search ( op,
1071                                              &nop,
1072                                              uri->ndn ?
1073                                              uri->ndn :
1074                                              &op->o_bd->be_nsuffix[0],
1075                                              uri->scope,
1076                                              rs,
1077                                              key);
1078
1079                         if ( rc != SLAP_CB_CONTINUE ) break;
1080                 }
1081                 if ( rc != SLAP_CB_CONTINUE ) break;
1082         }
1083
1084         return rc;
1085 }
1086
1087
1088 static int
1089 unique_modify(
1090         Operation *op,
1091         SlapReply *rs
1092 )
1093 {
1094         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1095         unique_data *private = (unique_data *) on->on_bi.bi_private;
1096         unique_domain *domains = private->domains;
1097         unique_domain *legacy = private->legacy;
1098         unique_domain *domain;
1099         Operation nop = *op;
1100         Modifications *m;
1101         char *key, *kp;
1102         int rc = SLAP_CB_CONTINUE;
1103
1104         Debug(LDAP_DEBUG_TRACE, "==> unique_modify <%s>\n",
1105               op->o_req_dn.bv_val, 0, 0);
1106
1107         for ( domain = legacy ? legacy : domains;
1108               domain;
1109               domain = domain->next ) {
1110                 unique_domain_uri *uri;
1111                 int ks = 0;
1112
1113                 for ( uri = domain->uri;
1114                       uri;
1115                       uri = uri->next ) {
1116
1117                         if ( uri->ndn
1118                              && !dnIsSuffix( &op->o_req_ndn, uri->ndn ))
1119                                 continue;
1120
1121                         if ( !(m = op->orm_modlist) ) {
1122                                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1123                                 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1124                                                 "unique_modify() got null op.orm_modlist");
1125                                 rc = rs->sr_err;
1126                                 break;
1127
1128                         } else
1129                                 for ( ; m; m = m->sml_next)
1130                                         if ( (m->sml_op & LDAP_MOD_OP)
1131                                              != LDAP_MOD_DELETE )
1132                                                 ks += count_filter_len
1133                                                         ( domain,
1134                                                           uri,
1135                                                           m->sml_desc,
1136                                                           m->sml_values);
1137
1138                         /* skip this domain-uri if it isn't involved */
1139                         if ( !ks ) continue;
1140
1141                         if ( uri->filter && uri->filter->bv_len )
1142                                 ks += uri->filter->bv_len;
1143                         key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1144
1145                         if ( uri->filter && uri->filter->bv_len )
1146                                 kp += sprintf ("(&(%s)", uri->filter->bv_val);
1147                         kp = key + sprintf(key, "(|");
1148
1149                         for(m = op->orm_modlist; m; m = m->sml_next)
1150                                 if ( (m->sml_op & LDAP_MOD_OP)
1151                                      != LDAP_MOD_DELETE )
1152                                         kp = build_filter ( domain,
1153                                                             uri,
1154                                                             m->sml_desc,
1155                                                             m->sml_values,
1156                                                             kp,
1157                                                             op->o_tmpmemctx );
1158
1159                         kp += sprintf (kp, ")");
1160                         if ( uri->filter && uri->filter->bv_len )
1161                                 kp += sprintf (kp, ")");
1162
1163                         rc = unique_search ( op,
1164                                              &nop,
1165                                              uri->ndn ?
1166                                              uri->ndn :
1167                                              &op->o_bd->be_nsuffix[0],
1168                                              uri->scope,
1169                                              rs,
1170                                              key);
1171
1172                         if ( rc != SLAP_CB_CONTINUE ) break;
1173                 }
1174                 if ( rc != SLAP_CB_CONTINUE ) break;
1175         }
1176
1177         return rc;
1178 }
1179
1180
1181 static int
1182 unique_modrdn(
1183         Operation *op,
1184         SlapReply *rs
1185 )
1186 {
1187         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1188         unique_data *private = (unique_data *) on->on_bi.bi_private;
1189         unique_domain *domains = private->domains;
1190         unique_domain *legacy = private->legacy;
1191         unique_domain *domain;
1192         Operation nop = *op;
1193         char *key, *kp;
1194         LDAPRDN newrdn;
1195         struct berval bv[2];
1196         int rc = SLAP_CB_CONTINUE;
1197
1198         Debug(LDAP_DEBUG_TRACE, "==> unique_modrdn <%s> <%s>\n",
1199                 op->o_req_dn.bv_val, op->orr_newrdn.bv_val, 0);
1200
1201         for ( domain = legacy ? legacy : domains;
1202               domain;
1203               domain = domain->next ) {
1204                 unique_domain_uri *uri;
1205                 int ks = 0;
1206
1207                 for ( uri = domain->uri;
1208                       uri;
1209                       uri = uri->next ) {
1210                         int i;
1211
1212                         if ( uri->ndn
1213                              && !dnIsSuffix( &op->o_req_ndn, uri->ndn )
1214                              && (!op->orr_nnewSup
1215                                  || !dnIsSuffix( op->orr_nnewSup, uri->ndn )))
1216                                 continue;
1217
1218                         if ( ldap_bv2rdn_x ( &op->oq_modrdn.rs_newrdn,
1219                                              &newrdn,
1220                                              (char **)&rs->sr_text,
1221                                              LDAP_DN_FORMAT_LDAP,
1222                                              op->o_tmpmemctx ) ) {
1223                                 op->o_bd->bd_info = (BackendInfo *) on->on_info;
1224                                 send_ldap_error(op, rs, LDAP_INVALID_SYNTAX,
1225                                                 "unknown type(s) used in RDN");
1226                                 rc = rs->sr_err;
1227                                 break;
1228                         }
1229
1230                         rc = SLAP_CB_CONTINUE;
1231                         for ( i=0; newrdn[i]; i++) {
1232                                 AttributeDescription *ad = NULL;
1233                                 if ( slap_bv2ad( &newrdn[i]->la_attr, &ad, &rs->sr_text )) {
1234                                         ldap_rdnfree_x( newrdn, op->o_tmpmemctx );
1235                                         rs->sr_err = LDAP_INVALID_SYNTAX;
1236                                         send_ldap_result( op, rs );
1237                                         rc = rs->sr_err;
1238                                         break;
1239                                 }
1240                                 newrdn[i]->la_private = ad;
1241                         }
1242                         if ( rc != SLAP_CB_CONTINUE ) break;
1243
1244                         bv[1].bv_val = NULL;
1245                         bv[1].bv_len = 0;
1246
1247                         for ( i=0; newrdn[i]; i++ ) {
1248                                 bv[0] = newrdn[i]->la_value;
1249                                 ks += count_filter_len ( domain,
1250                                                          uri,
1251                                                          newrdn[i]->la_private,
1252                                                          bv);
1253                         }
1254
1255                         /* skip this domain if it isn't involved */
1256                         if ( !ks ) continue;
1257
1258                         if ( uri->filter && uri->filter->bv_len )
1259                                 ks += uri->filter->bv_len;
1260                         key = op->o_tmpalloc(ks, op->o_tmpmemctx);
1261
1262                         if ( uri->filter && uri->filter->bv_len )
1263                                 kp += sprintf ("(&(%s)", uri->filter->bv_val);
1264                         kp = key + sprintf(key, "(|");
1265
1266                         for ( i=0; newrdn[i]; i++) {
1267                                 bv[0] = newrdn[i]->la_value;
1268                                 kp = build_filter ( domain,
1269                                                     uri,
1270                                                     newrdn[i]->la_private,
1271                                                     bv,
1272                                                     kp,
1273                                                     op->o_tmpmemctx);
1274                         }
1275
1276                         kp += sprintf(kp, ")");
1277                         if ( uri->filter && uri->filter->bv_len )
1278                                 kp += sprintf (kp, ")");
1279
1280                         rc = unique_search ( op,
1281                                              &nop,
1282                                              uri->ndn ?
1283                                              uri->ndn :
1284                                              &op->o_bd->be_nsuffix[0],
1285                                              uri->scope,
1286                                              rs,
1287                                              key);
1288
1289                         if ( rc != SLAP_CB_CONTINUE ) break;
1290                 }
1291                 if ( rc != SLAP_CB_CONTINUE ) break;
1292         }
1293
1294         return rc;
1295 }
1296
1297 /*
1298 ** init_module is last so the symbols resolve "for free" --
1299 ** it expects to be called automagically during dynamic module initialization
1300 */
1301
1302 int
1303 unique_initialize()
1304 {
1305         int rc;
1306
1307         /* statically declared just after the #includes at top */
1308         memset (&unique, 0, sizeof(unique));
1309
1310         unique.on_bi.bi_type = "unique";
1311         unique.on_bi.bi_db_init = unique_db_init;
1312         unique.on_bi.bi_db_destroy = unique_db_destroy;
1313         unique.on_bi.bi_db_open = unique_open;
1314         unique.on_bi.bi_db_close = unique_close;
1315         unique.on_bi.bi_op_add = unique_add;
1316         unique.on_bi.bi_op_modify = unique_modify;
1317         unique.on_bi.bi_op_modrdn = unique_modrdn;
1318
1319         unique.on_bi.bi_cf_ocs = uniqueocs;
1320         rc = config_register_schema( uniquecfg, uniqueocs );
1321         if ( rc ) return rc;
1322
1323         return(overlay_register(&unique));
1324 }
1325
1326 #if SLAPD_OVER_UNIQUE == SLAPD_MOD_DYNAMIC && defined(PIC)
1327 int init_module(int argc, char *argv[]) {
1328         return unique_initialize();
1329 }
1330 #endif
1331
1332 #endif /* SLAPD_OVER_UNIQUE */