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