]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/database.c
FIXME: needs work/cleanup
[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 /* FIXME: need a cleaner solution */
278 #ifndef PATH_MAX
279 #define PATH_MAX        4095
280 #endif /* ! PATH_MAX */
281                         char            path[ PATH_MAX ] = { '\0' };
282                         char            *fname = NULL;
283
284                         if ( strcmp( bi->bi_type, "bdb" ) == 0
285                                         || strcmp( bi->bi_type, "hdb" ) == 0 )
286                         {
287                                 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
288
289                                 fname = bdb->bi_dbenv_home;
290 #if 0
291                         } else if ( strcmp( bi->bi_type, "ldbm" ) == 0 ) {
292                                 struct ldbminfo *ldbm = (struct ldbminfo *) be->be_private;
293
294                                 /* FIXME: there's a conflict
295                                  * between back-bdb.h and back.ldbm.h;
296                                  * anyway, this code will be moved
297                                  * to the backends as soon as the
298                                  * issue with filtering on namingContexts
299                                  * is fixed */
300                                 fname = ldbm->li_directory;
301 #endif
302                         }
303
304                         len = strlen( fname );
305                         if ( fname[ 0 ] != '/' ) {
306                                 /* get full path name */
307                                 getcwd( path, sizeof( path ) );
308                                 pathlen = strlen( path );
309
310                                 if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
311                                         fname += 2;
312                                         len -= 2;
313                                 }
314                         }
315
316                         bv.bv_len = STRLENOF( "file://" ) + pathlen
317                                 + STRLENOF( "/" ) + len;
318                         bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
319                         AC_MEMCPY( bv.bv_val, "file://", STRLENOF( "file://" ) );
320                         if ( pathlen ) {
321                                 AC_MEMCPY( &bv.bv_val[ STRLENOF( "file://" ) ],
322                                                 path, pathlen );
323                                 bv.bv_val[ STRLENOF( "file://" ) + pathlen ] = '/';
324                                 pathlen++;
325                         }
326                         AC_MEMCPY( &bv.bv_val[ STRLENOF( "file://" ) + pathlen ],
327                                         fname, len );
328                         if ( bv.bv_val[ bv.bv_len - 1 ] != '/' ) {
329                                 bv.bv_val[ bv.bv_len ] = '/';
330                                 bv.bv_len++;
331                         }
332                         bv.bv_val[ bv.bv_len ] = '\0';
333
334                         attr_merge_normalize_one( e, slap_schema.si_ad_labeledURI,
335                                         &bv, NULL );
336
337                         ch_free( bv.bv_val );
338                 }
339 #endif /* defined(SLAPD_LDAP) || defined(SLAPD_HDB) */
340
341 #if defined(SLAPD_LDAP) 
342                 if ( strcmp( bi->bi_type, "ldap" ) == 0 ) {
343                         struct ldapinfo         *li = (struct ldapinfo *)be->be_private;
344                         struct berval           bv;
345
346                         bv.bv_val = li->url;
347                         bv.bv_len = strlen( bv.bv_val );
348                         attr_merge_normalize_one( e, slap_schema.si_ad_labeledURI,
349                                         &bv, NULL );
350                 }
351 #endif /* defined(SLAPD_LDAP) */
352
353                 for ( j = 0; j < nBackendInfo; j++ ) {
354                         if ( backendInfo[ j ].bi_type == bi->bi_type ) {
355                                 struct berval           bv;
356
357                                 snprintf( buf, sizeof( buf ), 
358                                         "cn=Backend %d,%s", 
359                                         j, ms_backend->mss_dn.bv_val );
360                                 bv.bv_val = buf;
361                                 bv.bv_len = strlen( buf );
362                                 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
363                                                 &bv, NULL );
364                                 break;
365                         }
366                 }
367                 /* we must find it! */
368                 assert( j >= 0 );
369
370                 mp = monitor_entrypriv_create();
371                 if ( mp == NULL ) {
372                         return -1;
373                 }
374                 e->e_private = ( void * )mp;
375                 mp->mp_info = ms;
376                 mp->mp_flags = ms->mss_flags
377                         | MONITOR_F_SUB;
378
379                 if ( monitor_cache_add( mi, e ) ) {
380                         Debug( LDAP_DEBUG_ANY,
381                                 "monitor_subsys_database_init: "
382                                 "unable to add entry \"cn=Database %d,%s\"\n",
383                                 i, ms->mss_dn.bv_val, 0 );
384                         return( -1 );
385                 }
386
387 #if defined(LDAP_SLAPI)
388                 monitor_back_add_plugin( be, e );
389 #endif /* defined(LDAP_SLAPI) */
390
391                 if ( oi != NULL ) {
392                         Entry           **ep_overlay = &mp->mp_children;
393                         monitor_entry_t *mp_overlay;
394                         slap_overinst   *on = oi->oi_list;
395                         int             o;
396
397                         for ( o = 0; on; o++, on = on->on_next ) {
398                                 Entry                   *e_overlay;
399                                 slap_overinst           *on2;
400
401                                 /* find the overlay number, j */
402                                 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
403                                         if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
404                                                 break;
405                                         }
406                                 }
407                                 assert( on2 );
408
409                                 snprintf( buf, sizeof( buf ),
410                                                 "dn: cn=Overlay %d,cn=Database %d,%s\n"
411                                                 "objectClass: %s\n"
412                                                 "structuralObjectClass: %s\n"
413                                                 "cn: Overlay %d\n"
414                                                 "description: This object contains the type of the overlay.\n"
415                                                 "%s: %s\n"
416                                                 "seeAlso: cn=Overlay %d,%s\n"
417                                                 "creatorsName: %s\n"
418                                                 "modifiersName: %s\n"
419                                                 "createTimestamp: %s\n"
420                                                 "modifyTimestamp: %s\n",
421                                                 o,
422                                                 i,
423                                                 ms->mss_dn.bv_val,
424                                                 mi->mi_oc_monitoredObject->soc_cname.bv_val,
425                                                 mi->mi_oc_monitoredObject->soc_cname.bv_val,
426                                                 o,
427                                                 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
428                                                 on->on_bi.bi_type,
429                                                 j,
430                                                 ms_overlay->mss_dn.bv_val,
431                                                 mi->mi_creatorsName.bv_val,
432                                                 mi->mi_creatorsName.bv_val,
433                                                 mi->mi_startTime.bv_val,
434                                                 mi->mi_startTime.bv_val );
435                                 
436                                 e_overlay = str2entry( buf );
437                                 if ( e_overlay == NULL ) {
438                                         Debug( LDAP_DEBUG_ANY,
439                                                 "monitor_subsys_database_init: "
440                                                 "unable to create entry "
441                                                 "\"cn=Overlay %d,cn=Database %d,%s\"\n",
442                                                 o, i, ms->mss_dn.bv_val );
443                                         return( -1 );
444                                 }
445
446                                 mp_overlay = monitor_entrypriv_create();
447                                 if ( mp_overlay == NULL ) {
448                                         return -1;
449                                 }
450                                 e_overlay->e_private = ( void * )mp_overlay;
451                                 mp_overlay->mp_info = ms;
452                                 mp_overlay->mp_flags = ms->mss_flags
453                                         | MONITOR_F_SUB;
454                 
455                                 if ( monitor_cache_add( mi, e_overlay ) ) {
456                                         Debug( LDAP_DEBUG_ANY,
457                                                 "monitor_subsys_database_init: "
458                                                 "unable to add entry "
459                                                 "\"cn=Overlay %d,cn=Database %d,%s\"\n",
460                                                 o, i, ms->mss_dn.bv_val );
461                                         return( -1 );
462                                 }
463
464                                 *ep_overlay = e_overlay;
465                                 ep_overlay = &mp_overlay->mp_next;
466                         }
467                 }
468
469                 *ep = e;
470                 ep = &mp->mp_next;
471         }
472         
473         monitor_cache_release( mi, e_database );
474
475         return( 0 );
476 }
477
478 /*
479  * v: array of values
480  * cur: must not contain the tags corresponding to the values in v
481  * delta: will contain the tags corresponding to the values in v
482  */
483 static int
484 value_mask( BerVarray v, slap_mask_t cur, slap_mask_t *delta )
485 {
486         for ( ; !BER_BVISNULL( v ); v++ ) {
487                 struct restricted_ops_t         *rops;
488                 int                             i;
489
490                 if ( OID_LEADCHAR( v->bv_val[ 0 ] ) ) {
491                         rops = restricted_exops;
492
493                 } else {
494                         rops = restricted_ops;
495                 }
496
497                 for ( i = 0; !BER_BVISNULL( &rops[ i ].op ); i++ ) {
498                         if ( ber_bvstrcasecmp( v, &rops[ i ].op ) != 0 ) {
499                                 continue;
500                         }
501
502                         if ( rops[ i ].tag & *delta ) {
503                                 return LDAP_OTHER;
504                         }
505
506                         if ( rops[ i ].tag & cur ) {
507                                 return LDAP_OTHER;
508                         }
509
510                         cur |= rops[ i ].tag;
511                         *delta |= rops[ i ].tag;
512
513                         break;
514                 }
515
516                 if ( BER_BVISNULL( &rops[ i ].op ) ) {
517                         return LDAP_INVALID_SYNTAX;
518                 }
519         }
520
521         return LDAP_SUCCESS;
522 }
523
524 int
525 monitor_subsys_database_modify(
526         Operation       *op,
527         Entry           *e
528 )
529 {
530         monitor_info_t  *mi = (monitor_info_t *)op->o_bd->be_private;
531         int             rc = LDAP_OTHER;
532         Attribute       *save_attrs, *a;
533         Modifications   *modlist = op->oq_modify.rs_modlist;
534         Modifications   *ml;
535         Backend         *be;
536         int             ro_gotval = 1, i, n;
537         slap_mask_t     rp_add = 0, rp_delete = 0, rp_cur;
538         struct berval   *tf;
539         
540         i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n );
541         if ( i != 1 )
542                 return LDAP_UNWILLING_TO_PERFORM;
543
544         if ( n < 0 || n >= nBackendDB )
545                 return LDAP_NO_SUCH_OBJECT;
546
547         /* do not allow some changes on back-monitor (needs work)... */
548         be = &backendDB[ n ];
549         if ( SLAP_MONITOR( be ) )
550                 return LDAP_UNWILLING_TO_PERFORM;
551                 
552         rp_cur = be->be_restrictops;
553
554         save_attrs = e->e_attrs;
555         e->e_attrs = attrs_dup( e->e_attrs );
556
557         for ( ml=modlist; ml; ml=ml->sml_next ) {
558                 Modification *mod = &ml->sml_mod;
559
560                 if ( mod->sm_desc == mi->mi_ad_readOnly ) {
561                         int     val = -1;
562
563                         if ( mod->sm_values ) {
564                                 if ( !BER_BVISNULL( &mod->sm_values[ 1 ] ) ) {
565                                         rc = LDAP_CONSTRAINT_VIOLATION;
566                                         goto done;
567                                 }
568
569                                 if ( bvmatch( &slap_true_bv, mod->sm_values )) {
570                                         val = 1;
571
572                                 } else if ( bvmatch( &slap_false_bv, mod->sm_values )) {
573                                         val = 0;
574
575                                 } else {
576                                         rc = LDAP_INVALID_SYNTAX;
577                                         goto done;
578                                 }
579                         }
580
581                         switch ( mod->sm_op ) {
582                         case LDAP_MOD_DELETE:
583                                 if ( ro_gotval < 1 ) {
584                                         rc = LDAP_CONSTRAINT_VIOLATION;
585                                         goto done;
586                                 }
587                                 ro_gotval--;
588
589                                 if ( val == 0 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
590                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
591                                         goto done;
592                                 }
593                                 
594                                 if ( val == 1 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) != SLAP_RESTRICT_OP_WRITES ) {
595                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
596                                         goto done;
597                                 }
598                                 
599                                 break;
600
601                         case LDAP_MOD_REPLACE:
602                                 ro_gotval = 0;
603                                 /* fall thru */
604
605                         case LDAP_MOD_ADD:
606                                 if ( ro_gotval > 0 ) {
607                                         rc = LDAP_CONSTRAINT_VIOLATION;
608                                         goto done;
609                                 }
610                                 ro_gotval++;
611
612                                 if ( val == 1 ) {
613                                         rp_add |= (~rp_cur) & SLAP_RESTRICT_OP_WRITES;
614                                         rp_cur |= SLAP_RESTRICT_OP_WRITES;
615                                         rp_delete &= ~SLAP_RESTRICT_OP_WRITES;
616                                         
617                                 } else if ( val == 0 ) {
618                                         rp_delete |= rp_cur & SLAP_RESTRICT_OP_WRITES;
619                                         rp_cur &= ~SLAP_RESTRICT_OP_WRITES;
620                                         rp_add &= ~SLAP_RESTRICT_OP_WRITES;
621                                 }
622                                 break;
623
624                         default:
625                                 rc = LDAP_OTHER;
626                                 goto done;
627                         }
628
629                 } else if ( mod->sm_desc == mi->mi_ad_restrictedOperation ) {
630                         slap_mask_t     mask = 0;
631
632                         switch ( mod->sm_op ) {
633                         case LDAP_MOD_DELETE:
634                                 if ( mod->sm_values == NULL ) {
635                                         rp_delete = rp_cur;
636                                         rp_cur = 0;
637                                         rp_add = 0;
638                                         break;
639                                 }
640                                 rc = value_mask( mod->sm_values, ~rp_cur, &mask );
641                                 if ( rc == LDAP_SUCCESS ) {
642                                         rp_delete |= mask;
643                                         rp_add &= ~mask;
644                                         rp_cur &= ~mask;
645
646                                 } else if ( rc == LDAP_OTHER ) {
647                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
648                                 }
649                                 break;
650
651                         case LDAP_MOD_REPLACE:
652                                 rp_delete = rp_cur;
653                                 rp_cur = 0;
654                                 rp_add = 0;
655                                 /* fall thru */
656
657                         case LDAP_MOD_ADD:
658                                 rc = value_mask( mod->sm_values, rp_cur, &mask );
659                                 if ( rc == LDAP_SUCCESS ) {
660                                         rp_add |= mask;
661                                         rp_cur |= mask;
662                                         rp_delete &= ~mask;
663
664                                 } else if ( rc == LDAP_OTHER ) {
665                                         rc = LDAP_TYPE_OR_VALUE_EXISTS;
666                                 }
667                                 break;
668
669                         default:
670                                 rc = LDAP_OTHER;
671                                 break;
672                         }
673
674                         if ( rc != LDAP_SUCCESS ) {
675                                 goto done;
676                         }
677
678                 } else if ( is_at_operational( mod->sm_desc->ad_type )) {
679                 /* accept all operational attributes */
680                         attr_delete( &e->e_attrs, mod->sm_desc );
681                         rc = attr_merge( e, mod->sm_desc, mod->sm_values,
682                                 mod->sm_nvalues );
683                         if ( rc ) {
684                                 rc = LDAP_OTHER;
685                                 break;
686                         }
687
688                 } else {
689                         rc = LDAP_UNWILLING_TO_PERFORM;
690                         break;
691                 }
692         }
693
694         /* sanity checks: */
695         if ( ro_gotval < 1 ) {
696                 rc = LDAP_CONSTRAINT_VIOLATION;
697                 goto done;
698         }
699
700         if ( ( rp_cur & SLAP_RESTRICT_OP_EXTENDED ) && ( rp_cur & SLAP_RESTRICT_EXOP_MASK ) ) {
701                 rc = LDAP_CONSTRAINT_VIOLATION;
702                 goto done;
703         }
704
705         if ( rp_delete & rp_add ) {
706                 rc = LDAP_OTHER;
707                 goto done;
708         }
709
710         /* check current value of readOnly */
711         if ( ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
712                 tf = (struct berval *)&slap_true_bv;
713
714         } else {
715                 tf = (struct berval *)&slap_false_bv;
716         }
717
718         a = attr_find( e->e_attrs, mi->mi_ad_readOnly );
719         if ( a == NULL ) {
720                 rc = LDAP_OTHER;
721                 goto done;
722         }
723
724         if ( !bvmatch( &a->a_vals[ 0 ], tf ) ) {
725                 attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
726                 rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, NULL );
727         }
728
729         if ( rc == LDAP_SUCCESS ) {
730                 if ( rp_delete ) {
731                         if ( rp_delete == be->be_restrictops ) {
732                                 attr_delete( &e->e_attrs, mi->mi_ad_restrictedOperation );
733
734                         } else {
735                                 a = attr_find( e->e_attrs, mi->mi_ad_restrictedOperation );
736                                 if ( a == NULL ) {
737                                         rc = LDAP_OTHER;
738                                         goto done;
739                                 }
740
741                                 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
742                                         if ( rp_delete & restricted_ops[ i ].tag ) {
743                                                 int     j;
744                                         
745                                                 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
746                                                         int             k;
747
748                                                         if ( !bvmatch( &a->a_nvals[ j ], &restricted_ops[ i ].op ) ) {
749                                                                 continue;
750                                                         }
751
752                                                         ch_free( a->a_vals[ j ].bv_val );
753                                                         ch_free( a->a_nvals[ j ].bv_val );
754
755                                                         for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
756                                                                 a->a_vals[ k - 1 ] = a->a_vals[ k ];
757                                                                 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
758                                                         }
759         
760                                                         BER_BVZERO( &a->a_vals[ k - 1 ] );
761                                                         BER_BVZERO( &a->a_nvals[ k - 1 ] );
762                                                 }
763                                         }
764                                 }
765                                 
766                                 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
767                                         if ( rp_delete & restricted_exops[ i ].tag ) {
768                                                 int     j;
769                                         
770                                                 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
771                                                         int             k;
772
773                                                         if ( !bvmatch( &a->a_nvals[ j ], &restricted_exops[ i ].op ) ) {
774                                                                 continue;
775                                                         }
776
777                                                         ch_free( a->a_vals[ j ].bv_val );
778                                                         ch_free( a->a_nvals[ j ].bv_val );
779
780                                                         for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
781                                                                 a->a_vals[ k - 1 ] = a->a_vals[ k ];
782                                                                 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
783                                                         }
784         
785                                                         BER_BVZERO( &a->a_vals[ k - 1 ] );
786                                                         BER_BVZERO( &a->a_nvals[ k - 1 ] );
787                                                 }
788                                         }
789                                 }
790                         }
791                 }
792
793                 if ( rp_add ) {
794                         for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
795                                 if ( rp_add & restricted_ops[ i ].tag ) {
796                                         attr_merge_one( e, mi->mi_ad_restrictedOperation,
797                                                         &restricted_ops[ i ].op, NULL );
798                                 }
799                         }
800
801                         for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
802                                 if ( rp_add & restricted_exops[ i ].tag ) {
803                                         attr_merge_one( e, mi->mi_ad_restrictedOperation,
804                                                         &restricted_exops[ i ].op, NULL );
805                                 }
806                         }
807                 }
808         }
809
810         be->be_restrictops = rp_cur;
811
812 done:;
813         if ( rc == LDAP_SUCCESS ) {
814                 attrs_free( save_attrs );
815
816         } else {
817                 Attribute *tmp = e->e_attrs;
818                 e->e_attrs = save_attrs;
819                 attrs_free( tmp );
820         }
821         return rc;
822 }
823
824 #if defined(LDAP_SLAPI)
825 static int
826 monitor_back_add_plugin( Backend *be, Entry *e_database )
827 {
828         Slapi_PBlock    *pCurrentPB; 
829         int             i, rc = LDAP_SUCCESS;
830         monitor_info_t  *mi = ( monitor_info_t * )be->be_private;
831
832         if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
833                 /*
834                  * LDAP_OTHER is returned if no plugins are installed
835                  */
836                 rc = LDAP_OTHER;
837                 goto done;
838         }
839
840         i = 0;
841         do {
842                 Slapi_PluginDesc        *srchdesc;
843                 char                    buf[ BACKMONITOR_BUFSIZE ];
844                 struct berval           bv;
845
846                 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
847                                 &srchdesc );
848                 if ( rc != LDAP_SUCCESS ) {
849                         goto done;
850                 }
851
852                 snprintf( buf, sizeof(buf),
853                                 "plugin %d name: %s; "
854                                 "vendor: %s; "
855                                 "version: %s; "
856                                 "description: %s", 
857                                 i,
858                                 srchdesc->spd_id,
859                                 srchdesc->spd_vendor,
860                                 srchdesc->spd_version,
861                                 srchdesc->spd_description );
862
863                 bv.bv_val = buf;
864                 bv.bv_len = strlen( buf );
865                 attr_merge_normalize_one( e_database,
866                                 mi->mi_ad_monitoredInfo, &bv, NULL );
867
868                 i++;
869
870         } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
871                         && ( pCurrentPB != NULL ) );
872
873 done:
874         return rc;
875 }
876 #endif /* defined(LDAP_SLAPI) */