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