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