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