]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/database.c
90ac1695508580319c38bd2c2e0a908df149719b
[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         Entry           *e
544 )
545 {
546         monitor_info_t  *mi = (monitor_info_t *)op->o_bd->be_private;
547         int             rc = LDAP_OTHER;
548         Attribute       *save_attrs, *a;
549         Modifications   *modlist = op->oq_modify.rs_modlist;
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 /* LDAP_UNWILLING_TO_PERFORM */ 0;
559
560         if ( n < 0 || n >= nBackendDB )
561                 return LDAP_NO_SUCH_OBJECT;
562
563         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
564                 if ( n == 0 ) break;
565                 n--;
566         }
567         /* do not allow some changes on back-monitor (needs work)... */
568         if ( SLAP_MONITOR( be ) )
569                 return LDAP_UNWILLING_TO_PERFORM;
570                 
571         rp_cur = be->be_restrictops;
572
573         save_attrs = e->e_attrs;
574         e->e_attrs = attrs_dup( e->e_attrs );
575
576         for ( ml=modlist; ml; ml=ml->sml_next ) {
577                 Modification *mod = &ml->sml_mod;
578
579                 if ( mod->sm_desc == mi->mi_ad_readOnly ) {
580                         int     val = -1;
581
582                         if ( mod->sm_values ) {
583                                 if ( !BER_BVISNULL( &mod->sm_values[ 1 ] ) ) {
584                                         rc = LDAP_CONSTRAINT_VIOLATION;
585                                         goto done;
586                                 }
587
588                                 if ( bvmatch( &slap_true_bv, mod->sm_values )) {
589                                         val = 1;
590
591                                 } else if ( bvmatch( &slap_false_bv, mod->sm_values )) {
592                                         val = 0;
593
594                                 } else {
595                                         rc = LDAP_INVALID_SYNTAX;
596                                         goto done;
597                                 }
598                         }
599
600                         switch ( mod->sm_op ) {
601                         case LDAP_MOD_DELETE:
602                                 if ( ro_gotval < 1 ) {
603                                         rc = LDAP_CONSTRAINT_VIOLATION;
604                                         goto done;
605                                 }
606                                 ro_gotval--;
607
608                                 if ( val == 0 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
609                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
610                                         goto done;
611                                 }
612                                 
613                                 if ( val == 1 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) != SLAP_RESTRICT_OP_WRITES ) {
614                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
615                                         goto done;
616                                 }
617                                 
618                                 break;
619
620                         case LDAP_MOD_REPLACE:
621                                 ro_gotval = 0;
622                                 /* fall thru */
623
624                         case LDAP_MOD_ADD:
625                                 if ( ro_gotval > 0 ) {
626                                         rc = LDAP_CONSTRAINT_VIOLATION;
627                                         goto done;
628                                 }
629                                 ro_gotval++;
630
631                                 if ( val == 1 ) {
632                                         rp_add |= (~rp_cur) & SLAP_RESTRICT_OP_WRITES;
633                                         rp_cur |= SLAP_RESTRICT_OP_WRITES;
634                                         rp_delete &= ~SLAP_RESTRICT_OP_WRITES;
635                                         
636                                 } else if ( val == 0 ) {
637                                         rp_delete |= rp_cur & SLAP_RESTRICT_OP_WRITES;
638                                         rp_cur &= ~SLAP_RESTRICT_OP_WRITES;
639                                         rp_add &= ~SLAP_RESTRICT_OP_WRITES;
640                                 }
641                                 break;
642
643                         default:
644                                 rc = LDAP_OTHER;
645                                 goto done;
646                         }
647
648                 } else if ( mod->sm_desc == mi->mi_ad_restrictedOperation ) {
649                         slap_mask_t     mask = 0;
650
651                         switch ( mod->sm_op ) {
652                         case LDAP_MOD_DELETE:
653                                 if ( mod->sm_values == NULL ) {
654                                         rp_delete = rp_cur;
655                                         rp_cur = 0;
656                                         rp_add = 0;
657                                         break;
658                                 }
659                                 rc = value_mask( mod->sm_values, ~rp_cur, &mask );
660                                 if ( rc == LDAP_SUCCESS ) {
661                                         rp_delete |= mask;
662                                         rp_add &= ~mask;
663                                         rp_cur &= ~mask;
664
665                                 } else if ( rc == LDAP_OTHER ) {
666                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
667                                 }
668                                 break;
669
670                         case LDAP_MOD_REPLACE:
671                                 rp_delete = rp_cur;
672                                 rp_cur = 0;
673                                 rp_add = 0;
674                                 /* fall thru */
675
676                         case LDAP_MOD_ADD:
677                                 rc = value_mask( mod->sm_values, rp_cur, &mask );
678                                 if ( rc == LDAP_SUCCESS ) {
679                                         rp_add |= mask;
680                                         rp_cur |= mask;
681                                         rp_delete &= ~mask;
682
683                                 } else if ( rc == LDAP_OTHER ) {
684                                         rc = LDAP_TYPE_OR_VALUE_EXISTS;
685                                 }
686                                 break;
687
688                         default:
689                                 rc = LDAP_OTHER;
690                                 break;
691                         }
692
693                         if ( rc != LDAP_SUCCESS ) {
694                                 goto done;
695                         }
696
697                 } else if ( is_at_operational( mod->sm_desc->ad_type )) {
698                 /* accept all operational attributes */
699                         attr_delete( &e->e_attrs, mod->sm_desc );
700                         rc = attr_merge( e, mod->sm_desc, mod->sm_values,
701                                 mod->sm_nvalues );
702                         if ( rc ) {
703                                 rc = LDAP_OTHER;
704                                 break;
705                         }
706
707                 } else {
708                         rc = LDAP_UNWILLING_TO_PERFORM;
709                         break;
710                 }
711         }
712
713         /* sanity checks: */
714         if ( ro_gotval < 1 ) {
715                 rc = LDAP_CONSTRAINT_VIOLATION;
716                 goto done;
717         }
718
719         if ( ( rp_cur & SLAP_RESTRICT_OP_EXTENDED ) && ( rp_cur & SLAP_RESTRICT_EXOP_MASK ) ) {
720                 rc = LDAP_CONSTRAINT_VIOLATION;
721                 goto done;
722         }
723
724         if ( rp_delete & rp_add ) {
725                 rc = LDAP_OTHER;
726                 goto done;
727         }
728
729         /* check current value of readOnly */
730         if ( ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
731                 tf = (struct berval *)&slap_true_bv;
732
733         } else {
734                 tf = (struct berval *)&slap_false_bv;
735         }
736
737         a = attr_find( e->e_attrs, mi->mi_ad_readOnly );
738         if ( a == NULL ) {
739                 rc = LDAP_OTHER;
740                 goto done;
741         }
742
743         if ( !bvmatch( &a->a_vals[ 0 ], tf ) ) {
744                 attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
745                 rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, tf );
746         }
747
748         if ( rc == LDAP_SUCCESS ) {
749                 if ( rp_delete ) {
750                         if ( rp_delete == be->be_restrictops ) {
751                                 attr_delete( &e->e_attrs, mi->mi_ad_restrictedOperation );
752
753                         } else {
754                                 a = attr_find( e->e_attrs, mi->mi_ad_restrictedOperation );
755                                 if ( a == NULL ) {
756                                         rc = LDAP_OTHER;
757                                         goto done;
758                                 }
759
760                                 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
761                                         if ( rp_delete & restricted_ops[ i ].tag ) {
762                                                 int     j;
763                                         
764                                                 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
765                                                         int             k;
766
767                                                         if ( !bvmatch( &a->a_nvals[ j ], &restricted_ops[ i ].op ) ) {
768                                                                 continue;
769                                                         }
770
771                                                         ch_free( a->a_vals[ j ].bv_val );
772                                                         ch_free( a->a_nvals[ j ].bv_val );
773
774                                                         for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
775                                                                 a->a_vals[ k - 1 ] = a->a_vals[ k ];
776                                                                 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
777                                                         }
778         
779                                                         BER_BVZERO( &a->a_vals[ k - 1 ] );
780                                                         BER_BVZERO( &a->a_nvals[ k - 1 ] );
781                                                 }
782                                         }
783                                 }
784                                 
785                                 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
786                                         if ( rp_delete & restricted_exops[ i ].tag ) {
787                                                 int     j;
788                                         
789                                                 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
790                                                         int             k;
791
792                                                         if ( !bvmatch( &a->a_nvals[ j ], &restricted_exops[ i ].op ) ) {
793                                                                 continue;
794                                                         }
795
796                                                         ch_free( a->a_vals[ j ].bv_val );
797                                                         ch_free( a->a_nvals[ j ].bv_val );
798
799                                                         for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
800                                                                 a->a_vals[ k - 1 ] = a->a_vals[ k ];
801                                                                 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
802                                                         }
803         
804                                                         BER_BVZERO( &a->a_vals[ k - 1 ] );
805                                                         BER_BVZERO( &a->a_nvals[ k - 1 ] );
806                                                 }
807                                         }
808                                 }
809                         }
810                 }
811
812                 if ( rp_add ) {
813                         for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
814                                 if ( rp_add & restricted_ops[ i ].tag ) {
815                                         attr_merge_one( e, mi->mi_ad_restrictedOperation,
816                                                         &restricted_ops[ i ].op,
817                                                         &restricted_ops[ i ].op );
818                                 }
819                         }
820
821                         for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
822                                 if ( rp_add & restricted_exops[ i ].tag ) {
823                                         attr_merge_one( e, mi->mi_ad_restrictedOperation,
824                                                         &restricted_exops[ i ].op,
825                                                         &restricted_exops[ i ].op );
826                                 }
827                         }
828                 }
829         }
830
831         be->be_restrictops = rp_cur;
832
833 done:;
834         if ( rc == LDAP_SUCCESS ) {
835                 attrs_free( save_attrs );
836
837         } else {
838                 Attribute *tmp = e->e_attrs;
839                 e->e_attrs = save_attrs;
840                 attrs_free( tmp );
841         }
842         return rc;
843 }
844
845 #if defined(LDAP_SLAPI)
846 static int
847 monitor_back_add_plugin( monitor_info_t *mi, Backend *be, Entry *e_database )
848 {
849         Slapi_PBlock    *pCurrentPB; 
850         int             i, rc = LDAP_SUCCESS;
851
852         if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
853                 /*
854                  * LDAP_OTHER is returned if no plugins are installed
855                  */
856                 rc = LDAP_OTHER;
857                 goto done;
858         }
859
860         i = 0;
861         do {
862                 Slapi_PluginDesc        *srchdesc;
863                 char                    buf[ BACKMONITOR_BUFSIZE ];
864                 struct berval           bv;
865
866                 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
867                                 &srchdesc );
868                 if ( rc != LDAP_SUCCESS ) {
869                         goto done;
870                 }
871
872                 snprintf( buf, sizeof(buf),
873                                 "plugin %d name: %s; "
874                                 "vendor: %s; "
875                                 "version: %s; "
876                                 "description: %s", 
877                                 i,
878                                 srchdesc->spd_id,
879                                 srchdesc->spd_vendor,
880                                 srchdesc->spd_version,
881                                 srchdesc->spd_description );
882
883                 ber_str2bv( buf, 0, 0, &bv );
884                 attr_merge_normalize_one( e_database,
885                                 mi->mi_ad_monitoredInfo, &bv, NULL );
886
887                 i++;
888
889         } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
890                         && ( pCurrentPB != NULL ) );
891
892 done:
893         return rc;
894 }
895 #endif /* defined(LDAP_SLAPI) */