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