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