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