Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / back-monitor / database.c
blob10b60a57398532f305d2bf984c978f99536ff107
1 /* database.c - deals with database subsystem */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/back-monitor/database.c,v 1.80.2.10 2008/05/26 18:57:01 ando Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2008 The OpenLDAP Foundation.
6 * Portions Copyright 2001-2003 Pierangelo Masarati.
7 * All rights reserved.
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.
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>.
17 /* ACKNOWLEDGEMENTS:
18 * This work was initially developed by Pierangelo Masarati for inclusion
19 * in OpenLDAP Software.
22 #include "portable.h"
24 #include <stdio.h>
25 #include <ac/string.h>
26 #include <ac/unistd.h>
28 #include "slap.h"
29 #include "back-monitor.h"
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) */
36 static int
37 monitor_subsys_database_modify(
38 Operation *op,
39 SlapReply *rs,
40 Entry *e );
42 static struct restricted_ops_t {
43 struct berval op;
44 unsigned int tag;
45 } restricted_ops[] = {
46 { BER_BVC( "add" ), SLAP_RESTRICT_OP_ADD },
47 { BER_BVC( "bind" ), SLAP_RESTRICT_OP_BIND },
48 { BER_BVC( "compare" ), SLAP_RESTRICT_OP_COMPARE },
49 { BER_BVC( "delete" ), SLAP_RESTRICT_OP_DELETE },
50 { BER_BVC( "extended" ), SLAP_RESTRICT_OP_EXTENDED },
51 { BER_BVC( "modify" ), SLAP_RESTRICT_OP_MODIFY },
52 { BER_BVC( "rename" ), SLAP_RESTRICT_OP_RENAME },
53 { BER_BVC( "search" ), SLAP_RESTRICT_OP_SEARCH },
54 { BER_BVNULL, 0 }
55 }, restricted_exops[] = {
56 { BER_BVC( LDAP_EXOP_START_TLS ), SLAP_RESTRICT_EXOP_START_TLS },
57 { BER_BVC( LDAP_EXOP_MODIFY_PASSWD ), SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
58 { BER_BVC( LDAP_EXOP_WHO_AM_I ), SLAP_RESTRICT_EXOP_WHOAMI },
59 { BER_BVC( LDAP_EXOP_CANCEL ), SLAP_RESTRICT_EXOP_CANCEL },
60 { BER_BVNULL, 0 }
63 static int
64 init_readOnly( monitor_info_t *mi, Entry *e, slap_mask_t restrictops )
66 struct berval *tf = ( ( restrictops & SLAP_RESTRICT_OP_MASK ) == SLAP_RESTRICT_OP_WRITES ) ?
67 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv;
69 return attr_merge_one( e, mi->mi_ad_readOnly, tf, tf );
72 static int
73 init_restrictedOperation( monitor_info_t *mi, Entry *e, slap_mask_t restrictops )
75 int i, rc;
77 for ( i = 0; restricted_ops[ i ].op.bv_val; i++ ) {
78 if ( restrictops & restricted_ops[ i ].tag ) {
79 rc = attr_merge_one( e, mi->mi_ad_restrictedOperation,
80 &restricted_ops[ i ].op,
81 &restricted_ops[ i ].op );
82 if ( rc ) {
83 return rc;
88 for ( i = 0; restricted_exops[ i ].op.bv_val; i++ ) {
89 if ( restrictops & restricted_exops[ i ].tag ) {
90 rc = attr_merge_one( e, mi->mi_ad_restrictedOperation,
91 &restricted_exops[ i ].op,
92 &restricted_exops[ i ].op );
93 if ( rc ) {
94 return rc;
99 return LDAP_SUCCESS;
102 static int
103 monitor_subsys_database_init_one(
104 monitor_info_t *mi,
105 BackendDB *be,
106 monitor_subsys_t *ms,
107 monitor_subsys_t *ms_backend,
108 monitor_subsys_t *ms_overlay,
109 struct berval *rdn,
110 Entry *e_database,
111 Entry ***epp )
113 char buf[ BACKMONITOR_BUFSIZE ];
114 int j;
115 slap_overinfo *oi = NULL;
116 BackendInfo *bi, *bi2;
117 Entry *e;
118 monitor_entry_t *mp;
119 char *rdnval = strchr( rdn->bv_val, '=' ) + 1;
120 struct berval bv;
122 bi = be->bd_info;
124 if ( overlay_is_over( be ) ) {
125 oi = (slap_overinfo *)be->bd_info->bi_private;
126 bi = oi->oi_orig;
129 /* Subordinates are not exposed as their own naming context */
130 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
131 return 0;
134 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, rdn,
135 mi->mi_oc_monitoredObject, mi, NULL, NULL );
137 if ( e == NULL ) {
138 Debug( LDAP_DEBUG_ANY,
139 "monitor_subsys_database_init: "
140 "unable to create entry \"%s,%s\"\n",
141 rdn->bv_val, ms->mss_dn.bv_val, 0 );
142 return( -1 );
145 ber_str2bv( bi->bi_type, 0, 0, &bv );
146 attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
147 attr_merge_one( e, mi->mi_ad_monitorIsShadow,
148 SLAP_SHADOW( be ) ? (struct berval *)&slap_true_bv :
149 (struct berval *)&slap_false_bv, NULL );
151 if ( SLAP_MONITOR( be ) ) {
152 attr_merge( e, slap_schema.si_ad_monitorContext,
153 be->be_suffix, be->be_nsuffix );
154 attr_merge( e_database, slap_schema.si_ad_monitorContext,
155 be->be_suffix, be->be_nsuffix );
157 } else {
158 if ( be->be_suffix == NULL ) {
159 Debug( LDAP_DEBUG_ANY,
160 "monitor_subsys_database_init: "
161 "missing suffix for %s\n",
162 rdnval, 0, 0 );
163 return -1;
165 attr_merge( e, slap_schema.si_ad_namingContexts,
166 be->be_suffix, be->be_nsuffix );
167 attr_merge( e_database, slap_schema.si_ad_namingContexts,
168 be->be_suffix, be->be_nsuffix );
171 (void)init_readOnly( mi, e, be->be_restrictops );
172 (void)init_restrictedOperation( mi, e, be->be_restrictops );
174 if ( SLAP_SHADOW( be ) && be->be_update_refs ) {
175 attr_merge_normalize( e, mi->mi_ad_monitorUpdateRef,
176 be->be_update_refs, NULL );
179 if ( oi != NULL ) {
180 slap_overinst *on = oi->oi_list,
181 *on1 = on;
183 for ( ; on; on = on->on_next ) {
184 slap_overinst *on2;
186 for ( on2 = on1; on2 != on; on2 = on2->on_next ) {
187 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
188 break;
192 if ( on2 != on ) {
193 break;
196 ber_str2bv( on->on_bi.bi_type, 0, 0, &bv );
197 attr_merge_normalize_one( e, mi->mi_ad_monitorOverlay,
198 &bv, NULL );
200 /* find the overlay number, j */
201 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
202 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
203 break;
206 assert( on2 != NULL );
208 snprintf( buf, sizeof( buf ),
209 "cn=Overlay %d,%s",
210 j, ms_overlay->mss_dn.bv_val );
211 ber_str2bv( buf, 0, 0, &bv );
212 attr_merge_normalize_one( e,
213 slap_schema.si_ad_seeAlso,
214 &bv, NULL );
218 j = -1;
219 LDAP_STAILQ_FOREACH( bi2, &backendInfo, bi_next ) {
220 j++;
221 if ( bi2->bi_type == bi->bi_type ) {
222 snprintf( buf, sizeof( buf ),
223 "cn=Backend %d,%s",
224 j, ms_backend->mss_dn.bv_val );
225 bv.bv_val = buf;
226 bv.bv_len = strlen( buf );
227 attr_merge_normalize_one( e,
228 slap_schema.si_ad_seeAlso,
229 &bv, NULL );
230 break;
233 /* we must find it! */
234 assert( j >= 0 );
236 mp = monitor_entrypriv_create();
237 if ( mp == NULL ) {
238 return -1;
240 e->e_private = ( void * )mp;
241 mp->mp_info = ms;
242 mp->mp_flags = ms->mss_flags
243 | MONITOR_F_SUB;
245 if ( monitor_cache_add( mi, e ) ) {
246 Debug( LDAP_DEBUG_ANY,
247 "monitor_subsys_database_init: "
248 "unable to add entry \"%s,%s\"\n",
249 rdn->bv_val, ms->mss_dn.bv_val, 0 );
250 return( -1 );
253 #if defined(LDAP_SLAPI)
254 monitor_back_add_plugin( mi, be, e );
255 #endif /* defined(LDAP_SLAPI) */
257 if ( oi != NULL ) {
258 Entry **ep_overlay = &mp->mp_children;
259 monitor_entry_t *mp_overlay;
260 slap_overinst *on = oi->oi_list;
261 int o;
263 for ( o = 0; on; o++, on = on->on_next ) {
264 Entry *e_overlay;
265 slap_overinst *on2;
267 /* find the overlay number, j */
268 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
269 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
270 break;
273 assert( on2 != NULL );
275 bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Overlay %d", o );
276 bv.bv_val = buf;
278 e_overlay = monitor_entry_stub( &e->e_name, &e->e_nname, &bv,
279 mi->mi_oc_monitoredObject, mi, NULL, NULL );
281 if ( e_overlay == NULL ) {
282 Debug( LDAP_DEBUG_ANY,
283 "monitor_subsys_database_init: "
284 "unable to create entry "
285 "\"cn=Overlay %d,%s,%s\"\n",
286 o, rdn->bv_val, ms->mss_dn.bv_val );
287 return( -1 );
289 ber_str2bv( on->on_bi.bi_type, 0, 0, &bv );
290 attr_merge_normalize_one( e_overlay, mi->mi_ad_monitoredInfo, &bv, NULL );
292 bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Overlay %d,%s",
293 j, ms_overlay->mss_dn.bv_val );
294 bv.bv_val = buf;
295 attr_merge_normalize_one( e_overlay, slap_schema.si_ad_seeAlso,
296 &bv, NULL );
298 if ( SLAP_MONITOR( be ) ) {
299 attr_merge( e_overlay, slap_schema.si_ad_monitorContext,
300 be->be_suffix, be->be_nsuffix );
302 } else {
303 attr_merge( e_overlay, slap_schema.si_ad_namingContexts,
304 be->be_suffix, be->be_nsuffix );
307 mp_overlay = monitor_entrypriv_create();
308 if ( mp_overlay == NULL ) {
309 return -1;
311 e_overlay->e_private = ( void * )mp_overlay;
312 mp_overlay->mp_info = ms;
313 mp_overlay->mp_flags = ms->mss_flags
314 | MONITOR_F_SUB;
316 if ( monitor_cache_add( mi, e_overlay ) ) {
317 Debug( LDAP_DEBUG_ANY,
318 "monitor_subsys_database_init: "
319 "unable to add entry "
320 "\"cn=Overlay %d,%s,%s\"\n",
321 o, rdn->bv_val, ms->mss_dn.bv_val );
322 return( -1 );
325 *ep_overlay = e_overlay;
326 ep_overlay = &mp_overlay->mp_next;
330 **epp = e;
331 *epp = &mp->mp_next;
333 return 0;
337 monitor_back_register_database(
338 BackendDB *be,
339 struct berval *ndn )
341 monitor_info_t *mi;
342 Entry *e_database, **ep;
343 int i, rc;
344 monitor_entry_t *mp;
345 monitor_subsys_t *ms_backend,
346 *ms_database,
347 *ms_overlay;
348 struct berval bv;
349 char buf[ BACKMONITOR_BUFSIZE ];
351 assert( be_monitor != NULL );
353 if ( !monitor_subsys_is_opened() ) {
354 return monitor_back_register_database_limbo( be, ndn );
357 mi = ( monitor_info_t * )be_monitor->be_private;
359 ms_backend = monitor_back_get_subsys( SLAPD_MONITOR_BACKEND_NAME );
360 if ( ms_backend == NULL ) {
361 Debug( LDAP_DEBUG_ANY,
362 "monitor_back_register_database: "
363 "unable to get "
364 "\"" SLAPD_MONITOR_BACKEND_NAME "\" "
365 "subsystem\n",
366 0, 0, 0 );
367 return -1;
370 ms_database = monitor_back_get_subsys( SLAPD_MONITOR_DATABASE_NAME );
371 if ( ms_database == NULL ) {
372 Debug( LDAP_DEBUG_ANY,
373 "monitor_back_register_database: "
374 "unable to get "
375 "\"" SLAPD_MONITOR_DATABASE_NAME "\" "
376 "subsystem\n",
377 0, 0, 0 );
378 return -1;
381 ms_overlay = monitor_back_get_subsys( SLAPD_MONITOR_OVERLAY_NAME );
382 if ( ms_overlay == NULL ) {
383 Debug( LDAP_DEBUG_ANY,
384 "monitor_back_register_database: "
385 "unable to get "
386 "\"" SLAPD_MONITOR_OVERLAY_NAME "\" "
387 "subsystem\n",
388 0, 0, 0 );
389 return -1;
392 if ( monitor_cache_get( mi, &ms_database->mss_ndn, &e_database ) ) {
393 Debug( LDAP_DEBUG_ANY,
394 "monitor_subsys_database_init: "
395 "unable to get entry \"%s\"\n",
396 ms_database->mss_ndn.bv_val, 0, 0 );
397 return( -1 );
400 mp = ( monitor_entry_t * )e_database->e_private;
401 for ( i = -1, ep = &mp->mp_children; *ep; i++ ) {
402 Attribute *a;
404 a = attr_find( (*ep)->e_attrs, slap_schema.si_ad_namingContexts );
405 if ( a ) {
406 int j, k;
408 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
409 for ( k = 0; !BER_BVISNULL( &be->be_nsuffix[ k ] ); k++ ) {
410 if ( dn_match( &a->a_nvals[ j ], &be->be_nsuffix[ k ] ) ) {
411 rc = 0;
412 goto done;
418 mp = ( monitor_entry_t * )(*ep)->e_private;
420 assert( mp != NULL );
421 ep = &mp->mp_next;
424 bv.bv_val = buf;
425 bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Database %d", i );
426 if ( bv.bv_len >= sizeof( buf ) ) {
427 rc = -1;
428 goto done;
431 rc = monitor_subsys_database_init_one( mi, be,
432 ms_database, ms_backend, ms_overlay, &bv, e_database, &ep );
433 if ( rc != 0 ) {
434 goto done;
436 /* database_init_one advanced ep past where we want.
437 * But it stored the entry we want in mp->mp_next.
439 ep = &mp->mp_next;
441 done:;
442 monitor_cache_release( mi, e_database );
443 if ( rc == 0 && ndn && ep && *ep ) {
444 *ndn = (*ep)->e_nname;
447 return rc;
451 monitor_subsys_database_init(
452 BackendDB *be,
453 monitor_subsys_t *ms )
455 monitor_info_t *mi;
456 Entry *e_database, **ep;
457 int i, rc;
458 monitor_entry_t *mp;
459 monitor_subsys_t *ms_backend,
460 *ms_overlay;
461 struct berval bv;
463 assert( be != NULL );
465 ms->mss_modify = monitor_subsys_database_modify;
467 mi = ( monitor_info_t * )be->be_private;
469 ms_backend = monitor_back_get_subsys( SLAPD_MONITOR_BACKEND_NAME );
470 if ( ms_backend == NULL ) {
471 Debug( LDAP_DEBUG_ANY,
472 "monitor_subsys_database_init: "
473 "unable to get "
474 "\"" SLAPD_MONITOR_BACKEND_NAME "\" "
475 "subsystem\n",
476 0, 0, 0 );
477 return -1;
480 ms_overlay = monitor_back_get_subsys( SLAPD_MONITOR_OVERLAY_NAME );
481 if ( ms_overlay == NULL ) {
482 Debug( LDAP_DEBUG_ANY,
483 "monitor_subsys_database_init: "
484 "unable to get "
485 "\"" SLAPD_MONITOR_OVERLAY_NAME "\" "
486 "subsystem\n",
487 0, 0, 0 );
488 return -1;
491 if ( monitor_cache_get( mi, &ms->mss_ndn, &e_database ) ) {
492 Debug( LDAP_DEBUG_ANY,
493 "monitor_subsys_database_init: "
494 "unable to get entry \"%s\"\n",
495 ms->mss_ndn.bv_val, 0, 0 );
496 return( -1 );
499 (void)init_readOnly( mi, e_database, frontendDB->be_restrictops );
500 (void)init_restrictedOperation( mi, e_database, frontendDB->be_restrictops );
502 mp = ( monitor_entry_t * )e_database->e_private;
503 mp->mp_children = NULL;
504 ep = &mp->mp_children;
506 BER_BVSTR( &bv, "cn=Frontend" );
507 rc = monitor_subsys_database_init_one( mi, frontendDB,
508 ms, ms_backend, ms_overlay, &bv, e_database, &ep );
509 if ( rc != 0 ) {
510 return rc;
513 i = -1;
514 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
515 char buf[ BACKMONITOR_BUFSIZE ];
517 bv.bv_val = buf;
518 bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Database %d", ++i );
519 if ( bv.bv_len >= sizeof( buf ) ) {
520 return -1;
523 rc = monitor_subsys_database_init_one( mi, be,
524 ms, ms_backend, ms_overlay, &bv, e_database, &ep );
525 if ( rc != 0 ) {
526 return rc;
530 monitor_cache_release( mi, e_database );
532 return( 0 );
536 * v: array of values
537 * cur: must not contain the tags corresponding to the values in v
538 * delta: will contain the tags corresponding to the values in v
540 static int
541 value_mask( BerVarray v, slap_mask_t cur, slap_mask_t *delta )
543 for ( ; !BER_BVISNULL( v ); v++ ) {
544 struct restricted_ops_t *rops;
545 int i;
547 if ( OID_LEADCHAR( v->bv_val[ 0 ] ) ) {
548 rops = restricted_exops;
550 } else {
551 rops = restricted_ops;
554 for ( i = 0; !BER_BVISNULL( &rops[ i ].op ); i++ ) {
555 if ( ber_bvstrcasecmp( v, &rops[ i ].op ) != 0 ) {
556 continue;
559 if ( rops[ i ].tag & *delta ) {
560 return LDAP_OTHER;
563 if ( rops[ i ].tag & cur ) {
564 return LDAP_OTHER;
567 cur |= rops[ i ].tag;
568 *delta |= rops[ i ].tag;
570 break;
573 if ( BER_BVISNULL( &rops[ i ].op ) ) {
574 return LDAP_INVALID_SYNTAX;
578 return LDAP_SUCCESS;
581 static int
582 monitor_subsys_database_modify(
583 Operation *op,
584 SlapReply *rs,
585 Entry *e )
587 monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private;
588 int rc = LDAP_OTHER;
589 Attribute *save_attrs, *a;
590 Modifications *ml;
591 Backend *be;
592 int ro_gotval = 1, i, n;
593 slap_mask_t rp_add = 0, rp_delete = 0, rp_cur;
594 struct berval *tf;
596 i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n );
597 if ( i != 1 ) {
598 return SLAP_CB_CONTINUE;
601 if ( n < 0 || n >= nBackendDB ) {
602 rs->sr_text = "invalid database index";
603 return ( rs->sr_err = LDAP_NO_SUCH_OBJECT );
606 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
607 if ( n == 0 ) {
608 break;
610 n--;
612 /* do not allow some changes on back-monitor (needs work)... */
613 if ( SLAP_MONITOR( be ) ) {
614 rs->sr_text = "no modifications allowed to monitor database entry";
615 return ( rs->sr_err = LDAP_UNWILLING_TO_PERFORM );
618 rp_cur = be->be_restrictops;
620 save_attrs = e->e_attrs;
621 e->e_attrs = attrs_dup( e->e_attrs );
623 for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
624 Modification *mod = &ml->sml_mod;
626 if ( mod->sm_desc == mi->mi_ad_readOnly ) {
627 int val = -1;
629 if ( mod->sm_values ) {
630 if ( !BER_BVISNULL( &mod->sm_values[ 1 ] ) ) {
631 rs->sr_text = "attempting to modify multiple values of single-valued attribute";
632 rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
633 goto done;
636 if ( bvmatch( &slap_true_bv, mod->sm_values )) {
637 val = 1;
639 } else if ( bvmatch( &slap_false_bv, mod->sm_values )) {
640 val = 0;
642 } else {
643 assert( 0 );
644 rc = rs->sr_err = LDAP_INVALID_SYNTAX;
645 goto done;
649 switch ( mod->sm_op ) {
650 case LDAP_MOD_DELETE:
651 if ( ro_gotval < 1 ) {
652 rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
653 goto done;
655 ro_gotval--;
657 if ( val == 0 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
658 rc = rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
659 goto done;
662 if ( val == 1 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) != SLAP_RESTRICT_OP_WRITES ) {
663 rc = rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
664 goto done;
667 break;
669 case LDAP_MOD_REPLACE:
670 ro_gotval = 0;
671 /* fall thru */
673 case LDAP_MOD_ADD:
674 if ( ro_gotval > 0 ) {
675 rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
676 goto done;
678 ro_gotval++;
680 if ( val == 1 ) {
681 rp_add |= (~rp_cur) & SLAP_RESTRICT_OP_WRITES;
682 rp_cur |= SLAP_RESTRICT_OP_WRITES;
683 rp_delete &= ~SLAP_RESTRICT_OP_WRITES;
685 } else if ( val == 0 ) {
686 rp_delete |= rp_cur & SLAP_RESTRICT_OP_WRITES;
687 rp_cur &= ~SLAP_RESTRICT_OP_WRITES;
688 rp_add &= ~SLAP_RESTRICT_OP_WRITES;
690 break;
692 default:
693 rc = rs->sr_err = LDAP_OTHER;
694 goto done;
697 } else if ( mod->sm_desc == mi->mi_ad_restrictedOperation ) {
698 slap_mask_t mask = 0;
700 switch ( mod->sm_op ) {
701 case LDAP_MOD_DELETE:
702 if ( mod->sm_values == NULL ) {
703 rp_delete = rp_cur;
704 rp_cur = 0;
705 rp_add = 0;
706 break;
708 rc = value_mask( mod->sm_values, ~rp_cur, &mask );
709 if ( rc == LDAP_SUCCESS ) {
710 rp_delete |= mask;
711 rp_add &= ~mask;
712 rp_cur &= ~mask;
714 } else if ( rc == LDAP_OTHER ) {
715 rc = LDAP_NO_SUCH_ATTRIBUTE;
717 break;
719 case LDAP_MOD_REPLACE:
720 rp_delete = rp_cur;
721 rp_cur = 0;
722 rp_add = 0;
723 /* fall thru */
725 case LDAP_MOD_ADD:
726 rc = value_mask( mod->sm_values, rp_cur, &mask );
727 if ( rc == LDAP_SUCCESS ) {
728 rp_add |= mask;
729 rp_cur |= mask;
730 rp_delete &= ~mask;
732 } else if ( rc == LDAP_OTHER ) {
733 rc = rs->sr_err = LDAP_TYPE_OR_VALUE_EXISTS;
735 break;
737 default:
738 rc = rs->sr_err = LDAP_OTHER;
739 break;
742 if ( rc != LDAP_SUCCESS ) {
743 goto done;
746 } else if ( is_at_operational( mod->sm_desc->ad_type )) {
747 /* accept all operational attributes */
748 attr_delete( &e->e_attrs, mod->sm_desc );
749 rc = attr_merge( e, mod->sm_desc, mod->sm_values,
750 mod->sm_nvalues );
751 if ( rc ) {
752 rc = rs->sr_err = LDAP_OTHER;
753 break;
756 } else {
757 rc = rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
758 break;
762 /* sanity checks: */
763 if ( ro_gotval < 1 ) {
764 rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
765 goto done;
768 if ( ( rp_cur & SLAP_RESTRICT_OP_EXTENDED ) && ( rp_cur & SLAP_RESTRICT_EXOP_MASK ) ) {
769 rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
770 goto done;
773 if ( rp_delete & rp_add ) {
774 rc = rs->sr_err = LDAP_OTHER;
775 goto done;
778 /* check current value of readOnly */
779 if ( ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
780 tf = (struct berval *)&slap_true_bv;
782 } else {
783 tf = (struct berval *)&slap_false_bv;
786 a = attr_find( e->e_attrs, mi->mi_ad_readOnly );
787 if ( a == NULL ) {
788 rc = LDAP_OTHER;
789 goto done;
792 if ( !bvmatch( &a->a_vals[ 0 ], tf ) ) {
793 attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
794 rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, tf );
797 if ( rc == LDAP_SUCCESS ) {
798 if ( rp_delete ) {
799 if ( rp_delete == be->be_restrictops ) {
800 attr_delete( &e->e_attrs, mi->mi_ad_restrictedOperation );
802 } else {
803 a = attr_find( e->e_attrs, mi->mi_ad_restrictedOperation );
804 if ( a == NULL ) {
805 rc = rs->sr_err = LDAP_OTHER;
806 goto done;
809 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
810 if ( rp_delete & restricted_ops[ i ].tag ) {
811 int j;
813 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
814 int k;
816 if ( !bvmatch( &a->a_nvals[ j ], &restricted_ops[ i ].op ) ) {
817 continue;
820 ch_free( a->a_vals[ j ].bv_val );
821 ch_free( a->a_nvals[ j ].bv_val );
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 ];
828 BER_BVZERO( &a->a_vals[ k - 1 ] );
829 BER_BVZERO( &a->a_nvals[ k - 1 ] );
830 a->a_numvals--;
835 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
836 if ( rp_delete & restricted_exops[ i ].tag ) {
837 int j;
839 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
840 int k;
842 if ( !bvmatch( &a->a_nvals[ j ], &restricted_exops[ i ].op ) ) {
843 continue;
846 ch_free( a->a_vals[ j ].bv_val );
847 ch_free( a->a_nvals[ j ].bv_val );
849 for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
850 a->a_vals[ k - 1 ] = a->a_vals[ k ];
851 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
854 BER_BVZERO( &a->a_vals[ k - 1 ] );
855 BER_BVZERO( &a->a_nvals[ k - 1 ] );
856 a->a_numvals--;
861 if ( a->a_vals == NULL ) {
862 assert( a->a_numvals == 0 );
864 attr_delete( &e->e_attrs, mi->mi_ad_restrictedOperation );
869 if ( rp_add ) {
870 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
871 if ( rp_add & restricted_ops[ i ].tag ) {
872 attr_merge_one( e, mi->mi_ad_restrictedOperation,
873 &restricted_ops[ i ].op,
874 &restricted_ops[ i ].op );
878 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
879 if ( rp_add & restricted_exops[ i ].tag ) {
880 attr_merge_one( e, mi->mi_ad_restrictedOperation,
881 &restricted_exops[ i ].op,
882 &restricted_exops[ i ].op );
888 be->be_restrictops = rp_cur;
890 done:;
891 if ( rc == LDAP_SUCCESS ) {
892 attrs_free( save_attrs );
893 rc = SLAP_CB_CONTINUE;
895 } else {
896 Attribute *tmp = e->e_attrs;
897 e->e_attrs = save_attrs;
898 attrs_free( tmp );
900 return rc;
903 #if defined(LDAP_SLAPI)
904 static int
905 monitor_back_add_plugin( monitor_info_t *mi, Backend *be, Entry *e_database )
907 Slapi_PBlock *pCurrentPB;
908 int i, rc = LDAP_SUCCESS;
910 if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
912 * LDAP_OTHER is returned if no plugins are installed
914 rc = LDAP_OTHER;
915 goto done;
918 i = 0;
919 do {
920 Slapi_PluginDesc *srchdesc;
921 char buf[ BACKMONITOR_BUFSIZE ];
922 struct berval bv;
924 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
925 &srchdesc );
926 if ( rc != LDAP_SUCCESS ) {
927 goto done;
929 if ( srchdesc ) {
930 snprintf( buf, sizeof(buf),
931 "plugin %d name: %s; "
932 "vendor: %s; "
933 "version: %s; "
934 "description: %s",
936 srchdesc->spd_id,
937 srchdesc->spd_vendor,
938 srchdesc->spd_version,
939 srchdesc->spd_description );
940 } else {
941 snprintf( buf, sizeof(buf),
942 "plugin %d name: <no description available>", i );
945 ber_str2bv( buf, 0, 0, &bv );
946 attr_merge_normalize_one( e_database,
947 mi->mi_ad_monitoredInfo, &bv, NULL );
949 i++;
951 } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
952 && ( pCurrentPB != NULL ) );
954 done:
955 return rc;
957 #endif /* defined(LDAP_SLAPI) */