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