]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/database.c
ITS#4965 again, registration cleanup from HEAD
[openldap] / servers / slapd / back-monitor / database.c
1 /* database.c - deals with database subsystem */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2001-2007 The OpenLDAP Foundation.
6  * Portions Copyright 2001-2003 Pierangelo Masarati.
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 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 Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include <ac/string.h>
26 #include <ac/unistd.h>
27
28 #include "slap.h"
29 #include "back-monitor.h"
30
31 #if defined(LDAP_SLAPI)
32 #include "slapi.h"
33 static int monitor_back_add_plugin( monitor_info_t *mi, Backend *be, Entry *e );
34 #endif /* defined(LDAP_SLAPI) */
35
36 /* for PATH_MAX on some systems (e.g. Solaris) */
37 #ifdef HAVE_LIMITS_H
38 #include <limits.h>
39 #endif /* HAVE_LIMITS_H */
40 #ifndef PATH_MAX
41 #define PATH_MAX        4095
42 #endif /* ! PATH_MAX */
43
44 static int
45 monitor_subsys_database_modify(
46         Operation       *op,
47         SlapReply       *rs,
48         Entry           *e );
49
50 static struct restricted_ops_t {
51         struct berval   op;
52         unsigned int    tag;
53 } restricted_ops[] = {
54         { BER_BVC( "add" ),                     SLAP_RESTRICT_OP_ADD },
55         { BER_BVC( "bind" ),                    SLAP_RESTRICT_OP_BIND },
56         { BER_BVC( "compare" ),                 SLAP_RESTRICT_OP_COMPARE },
57         { BER_BVC( "delete" ),                  SLAP_RESTRICT_OP_DELETE },
58         { BER_BVC( "extended" ),                SLAP_RESTRICT_OP_EXTENDED },
59         { BER_BVC( "modify" ),                  SLAP_RESTRICT_OP_MODIFY },
60         { BER_BVC( "rename" ),                  SLAP_RESTRICT_OP_RENAME },
61         { BER_BVC( "search" ),                  SLAP_RESTRICT_OP_SEARCH },
62         { BER_BVNULL,                           0 }
63 }, restricted_exops[] = {
64         { BER_BVC( LDAP_EXOP_START_TLS ),       SLAP_RESTRICT_EXOP_START_TLS },
65         { BER_BVC( LDAP_EXOP_MODIFY_PASSWD ),   SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
66         { BER_BVC( LDAP_EXOP_WHO_AM_I ),        SLAP_RESTRICT_EXOP_WHOAMI },
67         { BER_BVC( LDAP_EXOP_CANCEL ),  SLAP_RESTRICT_EXOP_CANCEL },
68         { BER_BVNULL,                           0 }
69 };
70
71 static int
72 init_readOnly( monitor_info_t *mi, Entry *e, slap_mask_t restrictops )
73 {
74         struct berval   *tf = ( ( restrictops & SLAP_RESTRICT_OP_MASK ) == SLAP_RESTRICT_OP_WRITES ) ?
75                 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv;
76
77         return attr_merge_one( e, mi->mi_ad_readOnly, tf, tf );
78 }
79
80 static int
81 init_restrictedOperation( monitor_info_t *mi, Entry *e, slap_mask_t restrictops )
82 {
83         int     i, rc;
84
85         for ( i = 0; restricted_ops[ i ].op.bv_val; i++ ) {
86                 if ( restrictops & restricted_ops[ i ].tag ) {
87                         rc = attr_merge_one( e, mi->mi_ad_restrictedOperation,
88                                         &restricted_ops[ i ].op,
89                                         &restricted_ops[ i ].op );
90                         if ( rc ) {
91                                 return rc;
92                         }
93                 }
94         }
95
96         for ( i = 0; restricted_exops[ i ].op.bv_val; i++ ) {
97                 if ( restrictops & restricted_exops[ i ].tag ) {
98                         rc = attr_merge_one( e, mi->mi_ad_restrictedOperation,
99                                         &restricted_exops[ i ].op,
100                                         &restricted_exops[ i ].op );
101                         if ( rc ) {
102                                 return rc;
103                         }
104                 }
105         }
106
107         return LDAP_SUCCESS;
108 }
109
110 static int
111 monitor_subsys_database_init_one(
112         monitor_info_t          *mi,
113         BackendDB               *be,
114         monitor_subsys_t        *ms,
115         monitor_subsys_t        *ms_backend,
116         monitor_subsys_t        *ms_overlay,
117         struct berval           *rdn,
118         Entry                   *e_database,
119         Entry                   ***epp )
120 {
121         char                    buf[ BACKMONITOR_BUFSIZE ];
122         int                     j;
123         slap_overinfo           *oi = NULL;
124         BackendInfo             *bi, *bi2;
125         Entry                   *e;
126         monitor_entry_t         *mp;
127         char                    *rdnval = strchr( rdn->bv_val, '=' ) + 1;
128         struct berval           bv;
129
130         bi = be->bd_info;
131
132         if ( overlay_is_over( be ) ) {
133                 oi = (slap_overinfo *)be->bd_info->bi_private;
134                 bi = oi->oi_orig;
135         }
136
137         /* Subordinates are not exposed as their own naming context */
138         if ( SLAP_GLUE_SUBORDINATE( be ) ) {
139                 return 0;
140         }
141
142         e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, rdn,
143                 mi->mi_oc_monitoredObject, mi, NULL, NULL );
144
145         if ( e == NULL ) {
146                 Debug( LDAP_DEBUG_ANY,
147                         "monitor_subsys_database_init: "
148                         "unable to create entry \"%s,%s\"\n",
149                         rdn->bv_val, ms->mss_dn.bv_val, 0 );
150                 return( -1 );
151         }
152
153         ber_str2bv( bi->bi_type, 0, 0, &bv );
154         attr_merge_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
155         attr_merge_one( e, mi->mi_ad_monitorIsShadow,
156                 SLAP_SHADOW( be ) ? (struct berval *)&slap_true_bv :
157                         (struct berval *)&slap_false_bv, NULL );
158
159         if ( SLAP_MONITOR( be ) ) {
160                 attr_merge( e, slap_schema.si_ad_monitorContext,
161                                 be->be_suffix, be->be_nsuffix );
162                 attr_merge( e_database, slap_schema.si_ad_monitorContext,
163                                 be->be_suffix, be->be_nsuffix );
164
165         } else {
166                 if ( be->be_suffix == NULL ) {
167                         Debug( LDAP_DEBUG_ANY,
168                                 "monitor_subsys_database_init: "
169                                 "missing suffix for %s\n",
170                                 rdnval, 0, 0 );
171                         return -1;
172                 }
173                 attr_merge( e, slap_schema.si_ad_namingContexts,
174                                 be->be_suffix, be->be_nsuffix );
175                 attr_merge( e_database, slap_schema.si_ad_namingContexts,
176                                 be->be_suffix, be->be_nsuffix );
177         }
178
179         (void)init_readOnly( mi, e, be->be_restrictops );
180         (void)init_restrictedOperation( mi, e, be->be_restrictops );
181
182         if ( SLAP_SHADOW( be ) && be->be_update_refs ) {
183                 attr_merge_normalize( e, mi->mi_ad_monitorUpdateRef,
184                                 be->be_update_refs, NULL );
185         }
186
187         if ( oi != NULL ) {
188                 slap_overinst   *on = oi->oi_list,
189                                 *on1 = on;
190
191                 for ( ; on; on = on->on_next ) {
192                         slap_overinst           *on2;
193
194                         for ( on2 = on1; on2 != on; on2 = on2->on_next ) {
195                                 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
196                                         break;
197                                 }
198                         }
199
200                         if ( on2 != on ) {
201                                 break;
202                         }
203                         
204                         ber_str2bv( on->on_bi.bi_type, 0, 0, &bv );
205                         attr_merge_normalize_one( e, mi->mi_ad_monitorOverlay,
206                                         &bv, NULL );
207
208                         /* find the overlay number, j */
209                         for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
210                                 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
211                                         break;
212                                 }
213                         }
214                         assert( on2 != NULL );
215
216                         snprintf( buf, sizeof( buf ), 
217                                 "cn=Overlay %d,%s", 
218                                 j, ms_overlay->mss_dn.bv_val );
219                         ber_str2bv( buf, 0, 0, &bv );
220                         attr_merge_normalize_one( e,
221                                         slap_schema.si_ad_seeAlso,
222                                         &bv, NULL );
223                 }
224         }
225
226         j = -1;
227         LDAP_STAILQ_FOREACH( bi2, &backendInfo, bi_next ) {
228                 j++;
229                 if ( bi2->bi_type == bi->bi_type ) {
230                         snprintf( buf, sizeof( buf ), 
231                                 "cn=Backend %d,%s", 
232                                 j, ms_backend->mss_dn.bv_val );
233                         bv.bv_val = buf;
234                         bv.bv_len = strlen( buf );
235                         attr_merge_normalize_one( e,
236                                         slap_schema.si_ad_seeAlso,
237                                         &bv, NULL );
238                         break;
239                 }
240         }
241         /* we must find it! */
242         assert( j >= 0 );
243
244         mp = monitor_entrypriv_create();
245         if ( mp == NULL ) {
246                 return -1;
247         }
248         e->e_private = ( void * )mp;
249         mp->mp_info = ms;
250         mp->mp_flags = ms->mss_flags
251                 | MONITOR_F_SUB;
252
253         if ( monitor_cache_add( mi, e ) ) {
254                 Debug( LDAP_DEBUG_ANY,
255                         "monitor_subsys_database_init: "
256                         "unable to add entry \"%s,%s\"\n",
257                         rdn->bv_val, ms->mss_dn.bv_val, 0 );
258                 return( -1 );
259         }
260
261 #if defined(LDAP_SLAPI)
262         monitor_back_add_plugin( mi, be, e );
263 #endif /* defined(LDAP_SLAPI) */
264
265         if ( oi != NULL ) {
266                 Entry           **ep_overlay = &mp->mp_children;
267                 monitor_entry_t *mp_overlay;
268                 slap_overinst   *on = oi->oi_list;
269                 int             o;
270
271                 for ( o = 0; on; o++, on = on->on_next ) {
272                         Entry                   *e_overlay;
273                         slap_overinst           *on2;
274
275                         /* find the overlay number, j */
276                         for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
277                                 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
278                                         break;
279                                 }
280                         }
281                         assert( on2 != NULL );
282
283                         bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Overlay %d", o );
284                         bv.bv_val = buf;
285
286                         e_overlay = monitor_entry_stub( &e->e_name, &e->e_nname, &bv,
287                                 mi->mi_oc_monitoredObject, mi, NULL, NULL );
288
289                         if ( e_overlay == NULL ) {
290                                 Debug( LDAP_DEBUG_ANY,
291                                         "monitor_subsys_database_init: "
292                                         "unable to create entry "
293                                         "\"cn=Overlay %d,%s,%s\"\n",
294                                         o, rdn->bv_val, ms->mss_dn.bv_val );
295                                 return( -1 );
296                         }
297                         ber_str2bv( on->on_bi.bi_type, 0, 0, &bv );
298                         attr_merge_one( e_overlay, mi->mi_ad_monitoredInfo, &bv, NULL );
299
300                         bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Overlay %d,%s",
301                                 j, ms_overlay->mss_dn.bv_val );
302                         bv.bv_val = buf;
303                         attr_merge_normalize_one( e_overlay, slap_schema.si_ad_seeAlso,
304                                 &bv, NULL );
305
306                         if ( SLAP_MONITOR( be ) ) {
307                                 attr_merge( e_overlay, slap_schema.si_ad_monitorContext,
308                                                 be->be_suffix, be->be_nsuffix );
309
310                         } else {
311                                 attr_merge( e_overlay, slap_schema.si_ad_namingContexts,
312                                                 be->be_suffix, be->be_nsuffix );
313                         }
314
315                         mp_overlay = monitor_entrypriv_create();
316                         if ( mp_overlay == NULL ) {
317                                 return -1;
318                         }
319                         e_overlay->e_private = ( void * )mp_overlay;
320                         mp_overlay->mp_info = ms;
321                         mp_overlay->mp_flags = ms->mss_flags
322                                 | MONITOR_F_SUB;
323         
324                         if ( monitor_cache_add( mi, e_overlay ) ) {
325                                 Debug( LDAP_DEBUG_ANY,
326                                         "monitor_subsys_database_init: "
327                                         "unable to add entry "
328                                         "\"cn=Overlay %d,%s,%s\"\n",
329                                         o, rdn->bv_val, ms->mss_dn.bv_val );
330                                 return( -1 );
331                         }
332
333                         *ep_overlay = e_overlay;
334                         ep_overlay = &mp_overlay->mp_next;
335                 }
336         }
337
338         **epp = e;
339         *epp = &mp->mp_next;
340
341         return 0;
342 }
343
344 int
345 monitor_back_register_database(
346         BackendDB               *be,
347         struct berval   *ndn )
348 {
349         monitor_info_t          *mi;
350         Entry                   *e_database, **ep;
351         int                     i, rc;
352         monitor_entry_t         *mp;
353         monitor_subsys_t        *ms_backend,
354                                 *ms_database,
355                                 *ms_overlay;
356         struct berval           bv;
357         char                    buf[ BACKMONITOR_BUFSIZE ];
358
359         assert( be_monitor != NULL );
360
361         if ( !monitor_subsys_is_opened() ) {
362                 return monitor_back_register_database_limbo( be, ndn );
363         }
364
365         mi = ( monitor_info_t * )be_monitor->be_private;
366
367         ms_backend = monitor_back_get_subsys( SLAPD_MONITOR_BACKEND_NAME );
368         if ( ms_backend == NULL ) {
369                 Debug( LDAP_DEBUG_ANY,
370                         "monitor_back_register_database: "
371                         "unable to get "
372                         "\"" SLAPD_MONITOR_BACKEND_NAME "\" "
373                         "subsystem\n",
374                         0, 0, 0 );
375                 return -1;
376         }
377
378         ms_database = monitor_back_get_subsys( SLAPD_MONITOR_DATABASE_NAME );
379         if ( ms_database == NULL ) {
380                 Debug( LDAP_DEBUG_ANY,
381                         "monitor_back_register_database: "
382                         "unable to get "
383                         "\"" SLAPD_MONITOR_DATABASE_NAME "\" "
384                         "subsystem\n",
385                         0, 0, 0 );
386                 return -1;
387         }
388
389         ms_overlay = monitor_back_get_subsys( SLAPD_MONITOR_OVERLAY_NAME );
390         if ( ms_overlay == NULL ) {
391                 Debug( LDAP_DEBUG_ANY,
392                         "monitor_back_register_database: "
393                         "unable to get "
394                         "\"" SLAPD_MONITOR_OVERLAY_NAME "\" "
395                         "subsystem\n",
396                         0, 0, 0 );
397                 return -1;
398         }
399
400         if ( monitor_cache_get( mi, &ms_database->mss_ndn, &e_database ) ) {
401                 Debug( LDAP_DEBUG_ANY,
402                         "monitor_subsys_database_init: "
403                         "unable to get entry \"%s\"\n",
404                         ms_database->mss_ndn.bv_val, 0, 0 );
405                 return( -1 );
406         }
407
408         mp = ( monitor_entry_t * )e_database->e_private;
409         for ( i = -1, ep = &mp->mp_children; *ep; i++ ) {
410                 Attribute       *a;
411
412                 a = attr_find( (*ep)->e_attrs, slap_schema.si_ad_namingContexts );
413                 if ( a ) {
414                         int             j, k;
415
416                         for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
417                                 for ( k = 0; !BER_BVISNULL( &be->be_nsuffix[ k ] ); k++ ) {
418                                         if ( dn_match( &a->a_nvals[ j ], &be->be_nsuffix[ k ] ) ) {
419                                                 rc = 0;
420                                                 goto done;
421                                         }
422                                 }
423                         }
424                 }
425
426                 mp = ( monitor_entry_t * )(*ep)->e_private;
427
428                 assert( mp != NULL );
429                 ep = &mp->mp_next;
430         }
431
432         bv.bv_val = buf;
433         bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Database %d", i );
434         if ( bv.bv_len >= sizeof( buf ) ) {
435                 rc = -1;
436                 goto done;
437         }
438         
439         rc = monitor_subsys_database_init_one( mi, be,
440                 ms_database, ms_backend, ms_overlay, &bv, e_database, &ep );
441         if ( rc != 0 ) {
442                 goto done;
443         }
444         /* database_init_one advanced ep past where we want.
445          * But it stored the entry we want in mp->mp_next.
446          */
447         ep = &mp->mp_next;
448
449 done:;
450         monitor_cache_release( mi, e_database );
451         if ( rc == 0 && ndn && ep && *ep ) {
452                 *ndn = (*ep)->e_nname;
453         }
454
455         return rc;
456 }
457
458 int
459 monitor_subsys_database_init(
460         BackendDB               *be,
461         monitor_subsys_t        *ms )
462 {
463         monitor_info_t          *mi;
464         Entry                   *e_database, **ep;
465         int                     i, rc;
466         monitor_entry_t         *mp;
467         monitor_subsys_t        *ms_backend,
468                                 *ms_overlay;
469         struct berval           bv;
470
471         assert( be != NULL );
472
473         ms->mss_modify = monitor_subsys_database_modify;
474
475         mi = ( monitor_info_t * )be->be_private;
476
477         ms_backend = monitor_back_get_subsys( SLAPD_MONITOR_BACKEND_NAME );
478         if ( ms_backend == NULL ) {
479                 Debug( LDAP_DEBUG_ANY,
480                         "monitor_subsys_database_init: "
481                         "unable to get "
482                         "\"" SLAPD_MONITOR_BACKEND_NAME "\" "
483                         "subsystem\n",
484                         0, 0, 0 );
485                 return -1;
486         }
487
488         ms_overlay = monitor_back_get_subsys( SLAPD_MONITOR_OVERLAY_NAME );
489         if ( ms_overlay == NULL ) {
490                 Debug( LDAP_DEBUG_ANY,
491                         "monitor_subsys_database_init: "
492                         "unable to get "
493                         "\"" SLAPD_MONITOR_OVERLAY_NAME "\" "
494                         "subsystem\n",
495                         0, 0, 0 );
496                 return -1;
497         }
498
499         if ( monitor_cache_get( mi, &ms->mss_ndn, &e_database ) ) {
500                 Debug( LDAP_DEBUG_ANY,
501                         "monitor_subsys_database_init: "
502                         "unable to get entry \"%s\"\n",
503                         ms->mss_ndn.bv_val, 0, 0 );
504                 return( -1 );
505         }
506
507         (void)init_readOnly( mi, e_database, frontendDB->be_restrictops );
508         (void)init_restrictedOperation( mi, e_database, frontendDB->be_restrictops );
509
510         mp = ( monitor_entry_t * )e_database->e_private;
511         mp->mp_children = NULL;
512         ep = &mp->mp_children;
513
514         BER_BVSTR( &bv, "cn=Frontend" );
515         rc = monitor_subsys_database_init_one( mi, frontendDB,
516                 ms, ms_backend, ms_overlay, &bv, e_database, &ep );
517         if ( rc != 0 ) {
518                 return rc;
519         }
520
521         i = -1;
522         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
523                 char            buf[ BACKMONITOR_BUFSIZE ];
524
525                 bv.bv_val = buf;
526                 bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Database %d", ++i );
527                 if ( bv.bv_len >= sizeof( buf ) ) {
528                         return -1;
529                 }
530                 
531                 rc = monitor_subsys_database_init_one( mi, be,
532                         ms, ms_backend, ms_overlay, &bv, e_database, &ep );
533                 if ( rc != 0 ) {
534                         return rc;
535                 }
536         }
537         
538         monitor_cache_release( mi, e_database );
539
540         return( 0 );
541 }
542
543 /*
544  * v: array of values
545  * cur: must not contain the tags corresponding to the values in v
546  * delta: will contain the tags corresponding to the values in v
547  */
548 static int
549 value_mask( BerVarray v, slap_mask_t cur, slap_mask_t *delta )
550 {
551         for ( ; !BER_BVISNULL( v ); v++ ) {
552                 struct restricted_ops_t         *rops;
553                 int                             i;
554
555                 if ( OID_LEADCHAR( v->bv_val[ 0 ] ) ) {
556                         rops = restricted_exops;
557
558                 } else {
559                         rops = restricted_ops;
560                 }
561
562                 for ( i = 0; !BER_BVISNULL( &rops[ i ].op ); i++ ) {
563                         if ( ber_bvstrcasecmp( v, &rops[ i ].op ) != 0 ) {
564                                 continue;
565                         }
566
567                         if ( rops[ i ].tag & *delta ) {
568                                 return LDAP_OTHER;
569                         }
570
571                         if ( rops[ i ].tag & cur ) {
572                                 return LDAP_OTHER;
573                         }
574
575                         cur |= rops[ i ].tag;
576                         *delta |= rops[ i ].tag;
577
578                         break;
579                 }
580
581                 if ( BER_BVISNULL( &rops[ i ].op ) ) {
582                         return LDAP_INVALID_SYNTAX;
583                 }
584         }
585
586         return LDAP_SUCCESS;
587 }
588
589 static int
590 monitor_subsys_database_modify(
591         Operation       *op,
592         SlapReply       *rs,
593         Entry           *e )
594 {
595         monitor_info_t  *mi = (monitor_info_t *)op->o_bd->be_private;
596         int             rc = LDAP_OTHER;
597         Attribute       *save_attrs, *a;
598         Modifications   *ml;
599         Backend         *be;
600         int             ro_gotval = 1, i, n;
601         slap_mask_t     rp_add = 0, rp_delete = 0, rp_cur;
602         struct berval   *tf;
603         
604         i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n );
605         if ( i != 1 ) {
606                 return SLAP_CB_CONTINUE;
607         }
608
609         if ( n < 0 || n >= nBackendDB ) {
610                 rs->sr_text = "invalid database index";
611                 return ( rs->sr_err = LDAP_NO_SUCH_OBJECT );
612         }
613
614         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
615                 if ( n == 0 ) {
616                         break;
617                 }
618                 n--;
619         }
620         /* do not allow some changes on back-monitor (needs work)... */
621         if ( SLAP_MONITOR( be ) ) {
622                 rs->sr_text = "no modifications allowed to monitor database entry";
623                 return ( rs->sr_err = LDAP_UNWILLING_TO_PERFORM );
624         }
625                 
626         rp_cur = be->be_restrictops;
627
628         save_attrs = e->e_attrs;
629         e->e_attrs = attrs_dup( e->e_attrs );
630
631         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
632                 Modification *mod = &ml->sml_mod;
633
634                 if ( mod->sm_desc == mi->mi_ad_readOnly ) {
635                         int     val = -1;
636
637                         if ( mod->sm_values ) {
638                                 if ( !BER_BVISNULL( &mod->sm_values[ 1 ] ) ) {
639                                         rs->sr_text = "attempting to modify multiple values of single-valued attribute";
640                                         rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
641                                         goto done;
642                                 }
643
644                                 if ( bvmatch( &slap_true_bv, mod->sm_values )) {
645                                         val = 1;
646
647                                 } else if ( bvmatch( &slap_false_bv, mod->sm_values )) {
648                                         val = 0;
649
650                                 } else {
651                                         assert( 0 );
652                                         rc = rs->sr_err = LDAP_INVALID_SYNTAX;
653                                         goto done;
654                                 }
655                         }
656
657                         switch ( mod->sm_op ) {
658                         case LDAP_MOD_DELETE:
659                                 if ( ro_gotval < 1 ) {
660                                         rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
661                                         goto done;
662                                 }
663                                 ro_gotval--;
664
665                                 if ( val == 0 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
666                                         rc = rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
667                                         goto done;
668                                 }
669                                 
670                                 if ( val == 1 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) != SLAP_RESTRICT_OP_WRITES ) {
671                                         rc = rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
672                                         goto done;
673                                 }
674                                 
675                                 break;
676
677                         case LDAP_MOD_REPLACE:
678                                 ro_gotval = 0;
679                                 /* fall thru */
680
681                         case LDAP_MOD_ADD:
682                                 if ( ro_gotval > 0 ) {
683                                         rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
684                                         goto done;
685                                 }
686                                 ro_gotval++;
687
688                                 if ( val == 1 ) {
689                                         rp_add |= (~rp_cur) & SLAP_RESTRICT_OP_WRITES;
690                                         rp_cur |= SLAP_RESTRICT_OP_WRITES;
691                                         rp_delete &= ~SLAP_RESTRICT_OP_WRITES;
692                                         
693                                 } else if ( val == 0 ) {
694                                         rp_delete |= rp_cur & SLAP_RESTRICT_OP_WRITES;
695                                         rp_cur &= ~SLAP_RESTRICT_OP_WRITES;
696                                         rp_add &= ~SLAP_RESTRICT_OP_WRITES;
697                                 }
698                                 break;
699
700                         default:
701                                 rc = rs->sr_err = LDAP_OTHER;
702                                 goto done;
703                         }
704
705                 } else if ( mod->sm_desc == mi->mi_ad_restrictedOperation ) {
706                         slap_mask_t     mask = 0;
707
708                         switch ( mod->sm_op ) {
709                         case LDAP_MOD_DELETE:
710                                 if ( mod->sm_values == NULL ) {
711                                         rp_delete = rp_cur;
712                                         rp_cur = 0;
713                                         rp_add = 0;
714                                         break;
715                                 }
716                                 rc = value_mask( mod->sm_values, ~rp_cur, &mask );
717                                 if ( rc == LDAP_SUCCESS ) {
718                                         rp_delete |= mask;
719                                         rp_add &= ~mask;
720                                         rp_cur &= ~mask;
721
722                                 } else if ( rc == LDAP_OTHER ) {
723                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
724                                 }
725                                 break;
726
727                         case LDAP_MOD_REPLACE:
728                                 rp_delete = rp_cur;
729                                 rp_cur = 0;
730                                 rp_add = 0;
731                                 /* fall thru */
732
733                         case LDAP_MOD_ADD:
734                                 rc = value_mask( mod->sm_values, rp_cur, &mask );
735                                 if ( rc == LDAP_SUCCESS ) {
736                                         rp_add |= mask;
737                                         rp_cur |= mask;
738                                         rp_delete &= ~mask;
739
740                                 } else if ( rc == LDAP_OTHER ) {
741                                         rc = rs->sr_err = LDAP_TYPE_OR_VALUE_EXISTS;
742                                 }
743                                 break;
744
745                         default:
746                                 rc = rs->sr_err = LDAP_OTHER;
747                                 break;
748                         }
749
750                         if ( rc != LDAP_SUCCESS ) {
751                                 goto done;
752                         }
753
754                 } else if ( is_at_operational( mod->sm_desc->ad_type )) {
755                 /* accept all operational attributes */
756                         attr_delete( &e->e_attrs, mod->sm_desc );
757                         rc = attr_merge( e, mod->sm_desc, mod->sm_values,
758                                 mod->sm_nvalues );
759                         if ( rc ) {
760                                 rc = rs->sr_err = LDAP_OTHER;
761                                 break;
762                         }
763
764                 } else {
765                         rc = rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
766                         break;
767                 }
768         }
769
770         /* sanity checks: */
771         if ( ro_gotval < 1 ) {
772                 rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
773                 goto done;
774         }
775
776         if ( ( rp_cur & SLAP_RESTRICT_OP_EXTENDED ) && ( rp_cur & SLAP_RESTRICT_EXOP_MASK ) ) {
777                 rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
778                 goto done;
779         }
780
781         if ( rp_delete & rp_add ) {
782                 rc = rs->sr_err = LDAP_OTHER;
783                 goto done;
784         }
785
786         /* check current value of readOnly */
787         if ( ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
788                 tf = (struct berval *)&slap_true_bv;
789
790         } else {
791                 tf = (struct berval *)&slap_false_bv;
792         }
793
794         a = attr_find( e->e_attrs, mi->mi_ad_readOnly );
795         if ( a == NULL ) {
796                 rc = LDAP_OTHER;
797                 goto done;
798         }
799
800         if ( !bvmatch( &a->a_vals[ 0 ], tf ) ) {
801                 attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
802                 rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, tf );
803         }
804
805         if ( rc == LDAP_SUCCESS ) {
806                 if ( rp_delete ) {
807                         if ( rp_delete == be->be_restrictops ) {
808                                 attr_delete( &e->e_attrs, mi->mi_ad_restrictedOperation );
809
810                         } else {
811                                 a = attr_find( e->e_attrs, mi->mi_ad_restrictedOperation );
812                                 if ( a == NULL ) {
813                                         rc = rs->sr_err = LDAP_OTHER;
814                                         goto done;
815                                 }
816
817                                 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
818                                         if ( rp_delete & restricted_ops[ i ].tag ) {
819                                                 int     j;
820                                         
821                                                 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
822                                                         int             k;
823
824                                                         if ( !bvmatch( &a->a_nvals[ j ], &restricted_ops[ i ].op ) ) {
825                                                                 continue;
826                                                         }
827
828                                                         ch_free( a->a_vals[ j ].bv_val );
829                                                         ch_free( a->a_nvals[ j ].bv_val );
830
831                                                         for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
832                                                                 a->a_vals[ k - 1 ] = a->a_vals[ k ];
833                                                                 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
834                                                         }
835         
836                                                         BER_BVZERO( &a->a_vals[ k - 1 ] );
837                                                         BER_BVZERO( &a->a_nvals[ k - 1 ] );
838                                                 }
839                                         }
840                                 }
841                                 
842                                 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
843                                         if ( rp_delete & restricted_exops[ i ].tag ) {
844                                                 int     j;
845                                         
846                                                 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
847                                                         int             k;
848
849                                                         if ( !bvmatch( &a->a_nvals[ j ], &restricted_exops[ i ].op ) ) {
850                                                                 continue;
851                                                         }
852
853                                                         ch_free( a->a_vals[ j ].bv_val );
854                                                         ch_free( a->a_nvals[ j ].bv_val );
855
856                                                         for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
857                                                                 a->a_vals[ k - 1 ] = a->a_vals[ k ];
858                                                                 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
859                                                         }
860         
861                                                         BER_BVZERO( &a->a_vals[ k - 1 ] );
862                                                         BER_BVZERO( &a->a_nvals[ k - 1 ] );
863                                                 }
864                                         }
865                                 }
866                         }
867                 }
868
869                 if ( rp_add ) {
870                         for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
871                                 if ( rp_add & restricted_ops[ i ].tag ) {
872                                         attr_merge_one( e, mi->mi_ad_restrictedOperation,
873                                                         &restricted_ops[ i ].op,
874                                                         &restricted_ops[ i ].op );
875                                 }
876                         }
877
878                         for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
879                                 if ( rp_add & restricted_exops[ i ].tag ) {
880                                         attr_merge_one( e, mi->mi_ad_restrictedOperation,
881                                                         &restricted_exops[ i ].op,
882                                                         &restricted_exops[ i ].op );
883                                 }
884                         }
885                 }
886         }
887
888         be->be_restrictops = rp_cur;
889
890 done:;
891         if ( rc == LDAP_SUCCESS ) {
892                 attrs_free( save_attrs );
893                 rc = SLAP_CB_CONTINUE;
894
895         } else {
896                 Attribute *tmp = e->e_attrs;
897                 e->e_attrs = save_attrs;
898                 attrs_free( tmp );
899         }
900         return rc;
901 }
902
903 #if defined(LDAP_SLAPI)
904 static int
905 monitor_back_add_plugin( monitor_info_t *mi, Backend *be, Entry *e_database )
906 {
907         Slapi_PBlock    *pCurrentPB; 
908         int             i, rc = LDAP_SUCCESS;
909
910         if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
911                 /*
912                  * LDAP_OTHER is returned if no plugins are installed
913                  */
914                 rc = LDAP_OTHER;
915                 goto done;
916         }
917
918         i = 0;
919         do {
920                 Slapi_PluginDesc        *srchdesc;
921                 char                    buf[ BACKMONITOR_BUFSIZE ];
922                 struct berval           bv;
923
924                 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
925                                 &srchdesc );
926                 if ( rc != LDAP_SUCCESS ) {
927                         goto done;
928                 }
929
930                 snprintf( buf, sizeof(buf),
931                                 "plugin %d name: %s; "
932                                 "vendor: %s; "
933                                 "version: %s; "
934                                 "description: %s", 
935                                 i,
936                                 srchdesc->spd_id,
937                                 srchdesc->spd_vendor,
938                                 srchdesc->spd_version,
939                                 srchdesc->spd_description );
940
941                 ber_str2bv( buf, 0, 0, &bv );
942                 attr_merge_normalize_one( e_database,
943                                 mi->mi_ad_monitoredInfo, &bv, NULL );
944
945                 i++;
946
947         } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
948                         && ( pCurrentPB != NULL ) );
949
950 done:
951         return rc;
952 }
953 #endif /* defined(LDAP_SLAPI) */