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