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