]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/database.c
2837113787f0deaa8e181ca42fbbe88e878a1ab9
[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 {
348         monitor_info_t          *mi;
349         Entry                   *e_database, **ep;
350         int                     i, rc;
351         monitor_entry_t         *mp;
352         monitor_subsys_t        *ms_backend,
353                                 *ms_database,
354                                 *ms_overlay;
355         struct berval           bv;
356
357         assert( be_monitor != NULL );
358
359         mi = ( monitor_info_t * )be_monitor->be_private;
360
361         ms_backend = monitor_back_get_subsys( SLAPD_MONITOR_BACKEND_NAME );
362         if ( ms_backend == NULL ) {
363                 Debug( LDAP_DEBUG_ANY,
364                         "monitor_back_register_database: "
365                         "unable to get "
366                         "\"" SLAPD_MONITOR_BACKEND_NAME "\" "
367                         "subsystem\n",
368                         0, 0, 0 );
369                 return -1;
370         }
371
372         ms_database = monitor_back_get_subsys( SLAPD_MONITOR_DATABASE_NAME );
373         if ( ms_backend == NULL ) {
374                 Debug( LDAP_DEBUG_ANY,
375                         "monitor_back_register_database: "
376                         "unable to get "
377                         "\"" SLAPD_MONITOR_DATABASE_NAME "\" "
378                         "subsystem\n",
379                         0, 0, 0 );
380                 return -1;
381         }
382
383         ms_overlay = monitor_back_get_subsys( SLAPD_MONITOR_OVERLAY_NAME );
384         if ( ms_overlay == NULL ) {
385                 Debug( LDAP_DEBUG_ANY,
386                         "monitor_back_register_database: "
387                         "unable to get "
388                         "\"" SLAPD_MONITOR_OVERLAY_NAME "\" "
389                         "subsystem\n",
390                         0, 0, 0 );
391                 return -1;
392         }
393
394         if ( monitor_cache_get( mi, &ms_database->mss_ndn, &e_database ) ) {
395                 Debug( LDAP_DEBUG_ANY,
396                         "monitor_subsys_database_init: "
397                         "unable to get entry \"%s\"\n",
398                         ms_database->mss_ndn.bv_val, 0, 0 );
399                 return( -1 );
400         }
401
402         mp = ( monitor_entry_t * )e_database->e_private;
403         for ( i = -1, ep = &mp->mp_children; *ep; i++ ) {
404                 mp = ( monitor_entry_t * )(*ep)->e_private;
405
406                 assert( mp != NULL );
407                 ep = &mp->mp_next;
408         }
409
410         {       
411                 char            buf[ BACKMONITOR_BUFSIZE ];
412
413                 bv.bv_val = buf;
414                 bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Database %d", i );
415                 if ( bv.bv_len >= sizeof( buf ) ) {
416                         return -1;
417                 }
418                 
419                 rc = monitor_subsys_database_init_one( mi, be,
420                         ms_database, ms_backend, ms_overlay, &bv, e_database, &ep );
421                 if ( rc != 0 ) {
422                         return rc;
423                 }
424         }
425         
426         monitor_cache_release( mi, e_database );
427
428         return 0;
429 }
430
431 int
432 monitor_subsys_database_init(
433         BackendDB               *be,
434         monitor_subsys_t        *ms )
435 {
436         monitor_info_t          *mi;
437         Entry                   *e_database, **ep;
438         int                     i, rc;
439         monitor_entry_t         *mp;
440         monitor_subsys_t        *ms_backend,
441                                 *ms_overlay;
442         struct berval           bv;
443
444         assert( be != NULL );
445
446         ms->mss_modify = monitor_subsys_database_modify;
447
448         mi = ( monitor_info_t * )be->be_private;
449
450         ms_backend = monitor_back_get_subsys( SLAPD_MONITOR_BACKEND_NAME );
451         if ( ms_backend == NULL ) {
452                 Debug( LDAP_DEBUG_ANY,
453                         "monitor_subsys_database_init: "
454                         "unable to get "
455                         "\"" SLAPD_MONITOR_BACKEND_NAME "\" "
456                         "subsystem\n",
457                         0, 0, 0 );
458                 return -1;
459         }
460
461         ms_overlay = monitor_back_get_subsys( SLAPD_MONITOR_OVERLAY_NAME );
462         if ( ms_overlay == NULL ) {
463                 Debug( LDAP_DEBUG_ANY,
464                         "monitor_subsys_database_init: "
465                         "unable to get "
466                         "\"" SLAPD_MONITOR_OVERLAY_NAME "\" "
467                         "subsystem\n",
468                         0, 0, 0 );
469                 return -1;
470         }
471
472         if ( monitor_cache_get( mi, &ms->mss_ndn, &e_database ) ) {
473                 Debug( LDAP_DEBUG_ANY,
474                         "monitor_subsys_database_init: "
475                         "unable to get entry \"%s\"\n",
476                         ms->mss_ndn.bv_val, 0, 0 );
477                 return( -1 );
478         }
479
480         (void)init_readOnly( mi, e_database, frontendDB->be_restrictops );
481         (void)init_restrictedOperation( mi, e_database, frontendDB->be_restrictops );
482
483         mp = ( monitor_entry_t * )e_database->e_private;
484         mp->mp_children = NULL;
485         ep = &mp->mp_children;
486
487         BER_BVSTR( &bv, "cn=Frontend" );
488         rc = monitor_subsys_database_init_one( mi, frontendDB,
489                 ms, ms_backend, ms_overlay, &bv, e_database, &ep );
490         if ( rc != 0 ) {
491                 return rc;
492         }
493
494         i = -1;
495         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
496                 char            buf[ BACKMONITOR_BUFSIZE ];
497
498                 bv.bv_val = buf;
499                 bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Database %d", ++i );
500                 if ( bv.bv_len >= sizeof( buf ) ) {
501                         return -1;
502                 }
503                 
504                 rc = monitor_subsys_database_init_one( mi, be,
505                         ms, ms_backend, ms_overlay, &bv, e_database, &ep );
506                 if ( rc != 0 ) {
507                         return rc;
508                 }
509         }
510         
511         monitor_cache_release( mi, e_database );
512
513         return( 0 );
514 }
515
516 /*
517  * v: array of values
518  * cur: must not contain the tags corresponding to the values in v
519  * delta: will contain the tags corresponding to the values in v
520  */
521 static int
522 value_mask( BerVarray v, slap_mask_t cur, slap_mask_t *delta )
523 {
524         for ( ; !BER_BVISNULL( v ); v++ ) {
525                 struct restricted_ops_t         *rops;
526                 int                             i;
527
528                 if ( OID_LEADCHAR( v->bv_val[ 0 ] ) ) {
529                         rops = restricted_exops;
530
531                 } else {
532                         rops = restricted_ops;
533                 }
534
535                 for ( i = 0; !BER_BVISNULL( &rops[ i ].op ); i++ ) {
536                         if ( ber_bvstrcasecmp( v, &rops[ i ].op ) != 0 ) {
537                                 continue;
538                         }
539
540                         if ( rops[ i ].tag & *delta ) {
541                                 return LDAP_OTHER;
542                         }
543
544                         if ( rops[ i ].tag & cur ) {
545                                 return LDAP_OTHER;
546                         }
547
548                         cur |= rops[ i ].tag;
549                         *delta |= rops[ i ].tag;
550
551                         break;
552                 }
553
554                 if ( BER_BVISNULL( &rops[ i ].op ) ) {
555                         return LDAP_INVALID_SYNTAX;
556                 }
557         }
558
559         return LDAP_SUCCESS;
560 }
561
562 static int
563 monitor_subsys_database_modify(
564         Operation       *op,
565         SlapReply       *rs,
566         Entry           *e )
567 {
568         monitor_info_t  *mi = (monitor_info_t *)op->o_bd->be_private;
569         int             rc = LDAP_OTHER;
570         Attribute       *save_attrs, *a;
571         Modifications   *ml;
572         Backend         *be;
573         int             ro_gotval = 1, i, n;
574         slap_mask_t     rp_add = 0, rp_delete = 0, rp_cur;
575         struct berval   *tf;
576         
577         i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n );
578         if ( i != 1 ) {
579                 return SLAP_CB_CONTINUE;
580         }
581
582         if ( n < 0 || n >= nBackendDB ) {
583                 rs->sr_text = "invalid database index";
584                 return ( rs->sr_err = LDAP_NO_SUCH_OBJECT );
585         }
586
587         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
588                 if ( n == 0 ) {
589                         break;
590                 }
591                 n--;
592         }
593         /* do not allow some changes on back-monitor (needs work)... */
594         if ( SLAP_MONITOR( be ) ) {
595                 rs->sr_text = "no modifications allowed to monitor database entry";
596                 return ( rs->sr_err = LDAP_UNWILLING_TO_PERFORM );
597         }
598                 
599         rp_cur = be->be_restrictops;
600
601         save_attrs = e->e_attrs;
602         e->e_attrs = attrs_dup( e->e_attrs );
603
604         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
605                 Modification *mod = &ml->sml_mod;
606
607                 if ( mod->sm_desc == mi->mi_ad_readOnly ) {
608                         int     val = -1;
609
610                         if ( mod->sm_values ) {
611                                 if ( !BER_BVISNULL( &mod->sm_values[ 1 ] ) ) {
612                                         rs->sr_text = "attempting to modify multiple values of single-valued attribute";
613                                         rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
614                                         goto done;
615                                 }
616
617                                 if ( bvmatch( &slap_true_bv, mod->sm_values )) {
618                                         val = 1;
619
620                                 } else if ( bvmatch( &slap_false_bv, mod->sm_values )) {
621                                         val = 0;
622
623                                 } else {
624                                         assert( 0 );
625                                         rc = rs->sr_err = LDAP_INVALID_SYNTAX;
626                                         goto done;
627                                 }
628                         }
629
630                         switch ( mod->sm_op ) {
631                         case LDAP_MOD_DELETE:
632                                 if ( ro_gotval < 1 ) {
633                                         rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
634                                         goto done;
635                                 }
636                                 ro_gotval--;
637
638                                 if ( val == 0 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
639                                         rc = rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
640                                         goto done;
641                                 }
642                                 
643                                 if ( val == 1 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) != SLAP_RESTRICT_OP_WRITES ) {
644                                         rc = rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
645                                         goto done;
646                                 }
647                                 
648                                 break;
649
650                         case LDAP_MOD_REPLACE:
651                                 ro_gotval = 0;
652                                 /* fall thru */
653
654                         case LDAP_MOD_ADD:
655                                 if ( ro_gotval > 0 ) {
656                                         rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
657                                         goto done;
658                                 }
659                                 ro_gotval++;
660
661                                 if ( val == 1 ) {
662                                         rp_add |= (~rp_cur) & SLAP_RESTRICT_OP_WRITES;
663                                         rp_cur |= SLAP_RESTRICT_OP_WRITES;
664                                         rp_delete &= ~SLAP_RESTRICT_OP_WRITES;
665                                         
666                                 } else if ( val == 0 ) {
667                                         rp_delete |= rp_cur & SLAP_RESTRICT_OP_WRITES;
668                                         rp_cur &= ~SLAP_RESTRICT_OP_WRITES;
669                                         rp_add &= ~SLAP_RESTRICT_OP_WRITES;
670                                 }
671                                 break;
672
673                         default:
674                                 rc = rs->sr_err = LDAP_OTHER;
675                                 goto done;
676                         }
677
678                 } else if ( mod->sm_desc == mi->mi_ad_restrictedOperation ) {
679                         slap_mask_t     mask = 0;
680
681                         switch ( mod->sm_op ) {
682                         case LDAP_MOD_DELETE:
683                                 if ( mod->sm_values == NULL ) {
684                                         rp_delete = rp_cur;
685                                         rp_cur = 0;
686                                         rp_add = 0;
687                                         break;
688                                 }
689                                 rc = value_mask( mod->sm_values, ~rp_cur, &mask );
690                                 if ( rc == LDAP_SUCCESS ) {
691                                         rp_delete |= mask;
692                                         rp_add &= ~mask;
693                                         rp_cur &= ~mask;
694
695                                 } else if ( rc == LDAP_OTHER ) {
696                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
697                                 }
698                                 break;
699
700                         case LDAP_MOD_REPLACE:
701                                 rp_delete = rp_cur;
702                                 rp_cur = 0;
703                                 rp_add = 0;
704                                 /* fall thru */
705
706                         case LDAP_MOD_ADD:
707                                 rc = value_mask( mod->sm_values, rp_cur, &mask );
708                                 if ( rc == LDAP_SUCCESS ) {
709                                         rp_add |= mask;
710                                         rp_cur |= mask;
711                                         rp_delete &= ~mask;
712
713                                 } else if ( rc == LDAP_OTHER ) {
714                                         rc = rs->sr_err = LDAP_TYPE_OR_VALUE_EXISTS;
715                                 }
716                                 break;
717
718                         default:
719                                 rc = rs->sr_err = LDAP_OTHER;
720                                 break;
721                         }
722
723                         if ( rc != LDAP_SUCCESS ) {
724                                 goto done;
725                         }
726
727                 } else if ( is_at_operational( mod->sm_desc->ad_type )) {
728                 /* accept all operational attributes */
729                         attr_delete( &e->e_attrs, mod->sm_desc );
730                         rc = attr_merge( e, mod->sm_desc, mod->sm_values,
731                                 mod->sm_nvalues );
732                         if ( rc ) {
733                                 rc = rs->sr_err = LDAP_OTHER;
734                                 break;
735                         }
736
737                 } else {
738                         rc = rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
739                         break;
740                 }
741         }
742
743         /* sanity checks: */
744         if ( ro_gotval < 1 ) {
745                 rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
746                 goto done;
747         }
748
749         if ( ( rp_cur & SLAP_RESTRICT_OP_EXTENDED ) && ( rp_cur & SLAP_RESTRICT_EXOP_MASK ) ) {
750                 rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
751                 goto done;
752         }
753
754         if ( rp_delete & rp_add ) {
755                 rc = rs->sr_err = LDAP_OTHER;
756                 goto done;
757         }
758
759         /* check current value of readOnly */
760         if ( ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
761                 tf = (struct berval *)&slap_true_bv;
762
763         } else {
764                 tf = (struct berval *)&slap_false_bv;
765         }
766
767         a = attr_find( e->e_attrs, mi->mi_ad_readOnly );
768         if ( a == NULL ) {
769                 rc = LDAP_OTHER;
770                 goto done;
771         }
772
773         if ( !bvmatch( &a->a_vals[ 0 ], tf ) ) {
774                 attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
775                 rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, tf );
776         }
777
778         if ( rc == LDAP_SUCCESS ) {
779                 if ( rp_delete ) {
780                         if ( rp_delete == be->be_restrictops ) {
781                                 attr_delete( &e->e_attrs, mi->mi_ad_restrictedOperation );
782
783                         } else {
784                                 a = attr_find( e->e_attrs, mi->mi_ad_restrictedOperation );
785                                 if ( a == NULL ) {
786                                         rc = rs->sr_err = LDAP_OTHER;
787                                         goto done;
788                                 }
789
790                                 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
791                                         if ( rp_delete & restricted_ops[ i ].tag ) {
792                                                 int     j;
793                                         
794                                                 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
795                                                         int             k;
796
797                                                         if ( !bvmatch( &a->a_nvals[ j ], &restricted_ops[ i ].op ) ) {
798                                                                 continue;
799                                                         }
800
801                                                         ch_free( a->a_vals[ j ].bv_val );
802                                                         ch_free( a->a_nvals[ j ].bv_val );
803
804                                                         for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
805                                                                 a->a_vals[ k - 1 ] = a->a_vals[ k ];
806                                                                 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
807                                                         }
808         
809                                                         BER_BVZERO( &a->a_vals[ k - 1 ] );
810                                                         BER_BVZERO( &a->a_nvals[ k - 1 ] );
811                                                 }
812                                         }
813                                 }
814                                 
815                                 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
816                                         if ( rp_delete & restricted_exops[ i ].tag ) {
817                                                 int     j;
818                                         
819                                                 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
820                                                         int             k;
821
822                                                         if ( !bvmatch( &a->a_nvals[ j ], &restricted_exops[ i ].op ) ) {
823                                                                 continue;
824                                                         }
825
826                                                         ch_free( a->a_vals[ j ].bv_val );
827                                                         ch_free( a->a_nvals[ j ].bv_val );
828
829                                                         for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
830                                                                 a->a_vals[ k - 1 ] = a->a_vals[ k ];
831                                                                 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
832                                                         }
833         
834                                                         BER_BVZERO( &a->a_vals[ k - 1 ] );
835                                                         BER_BVZERO( &a->a_nvals[ k - 1 ] );
836                                                 }
837                                         }
838                                 }
839                         }
840                 }
841
842                 if ( rp_add ) {
843                         for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
844                                 if ( rp_add & restricted_ops[ i ].tag ) {
845                                         attr_merge_one( e, mi->mi_ad_restrictedOperation,
846                                                         &restricted_ops[ i ].op,
847                                                         &restricted_ops[ i ].op );
848                                 }
849                         }
850
851                         for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
852                                 if ( rp_add & restricted_exops[ i ].tag ) {
853                                         attr_merge_one( e, mi->mi_ad_restrictedOperation,
854                                                         &restricted_exops[ i ].op,
855                                                         &restricted_exops[ i ].op );
856                                 }
857                         }
858                 }
859         }
860
861         be->be_restrictops = rp_cur;
862
863 done:;
864         if ( rc == LDAP_SUCCESS ) {
865                 attrs_free( save_attrs );
866                 rc = SLAP_CB_CONTINUE;
867
868         } else {
869                 Attribute *tmp = e->e_attrs;
870                 e->e_attrs = save_attrs;
871                 attrs_free( tmp );
872         }
873         return rc;
874 }
875
876 #if defined(LDAP_SLAPI)
877 static int
878 monitor_back_add_plugin( monitor_info_t *mi, Backend *be, Entry *e_database )
879 {
880         Slapi_PBlock    *pCurrentPB; 
881         int             i, rc = LDAP_SUCCESS;
882
883         if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
884                 /*
885                  * LDAP_OTHER is returned if no plugins are installed
886                  */
887                 rc = LDAP_OTHER;
888                 goto done;
889         }
890
891         i = 0;
892         do {
893                 Slapi_PluginDesc        *srchdesc;
894                 char                    buf[ BACKMONITOR_BUFSIZE ];
895                 struct berval           bv;
896
897                 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
898                                 &srchdesc );
899                 if ( rc != LDAP_SUCCESS ) {
900                         goto done;
901                 }
902
903                 snprintf( buf, sizeof(buf),
904                                 "plugin %d name: %s; "
905                                 "vendor: %s; "
906                                 "version: %s; "
907                                 "description: %s", 
908                                 i,
909                                 srchdesc->spd_id,
910                                 srchdesc->spd_vendor,
911                                 srchdesc->spd_version,
912                                 srchdesc->spd_description );
913
914                 ber_str2bv( buf, 0, 0, &bv );
915                 attr_merge_normalize_one( e_database,
916                                 mi->mi_ad_monitoredInfo, &bv, NULL );
917
918                 i++;
919
920         } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
921                         && ( pCurrentPB != NULL ) );
922
923 done:
924         return rc;
925 }
926 #endif /* defined(LDAP_SLAPI) */