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