]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/database.c
+ Fixed slapd connectionless LDAP support
[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                         int             u;
365
366                         for ( u = 0; urls[ u ] != NULL; u++ ) {
367                                 struct berval   bv;
368
369                                 ber_str2bv( urls[ u ], 0, 0, &bv );
370
371                                 attr_merge_normalize_one( e,
372                                                 slap_schema.si_ad_labeledURI,
373                                                 &bv, NULL );
374                         }
375
376                         ldap_charray_free( urls );
377 #endif
378
379 #endif /* defined(SLAPD_LDAP) */
380 #if defined(SLAPD_META) 
381                 } else if ( strcmp( bi->bi_type, "meta" ) == 0 ) {
382                         metainfo_t      *mi = (metainfo_t *)be->be_private;
383                         int             t;
384
385                         for ( t = 0; t < mi->mi_ntargets; t++ ) {
386                                 char            **urls = ldap_str2charray( mi->mi_targets[ t ].mt_uri, " " );
387                                 int             u;
388
389                                 for ( u = 0; urls[ u ] != NULL; u++ ) {
390                                         struct berval   bv;
391
392                                         ber_str2bv( urls[ u ], 0, 0, &bv );
393
394                                         attr_merge_normalize_one( e,
395                                                 slap_schema.si_ad_labeledURI,
396                                                 &bv, NULL );
397                                 }
398                                 ldap_charray_free( urls );
399                         }
400 #endif /* defined(SLAPD_META) */
401                 }
402
403                 j = -1;
404                 LDAP_STAILQ_FOREACH( bi2, &backendInfo, bi_next ) {
405                         j++;
406                         if ( bi2->bi_type == bi->bi_type ) {
407                                 struct berval           bv;
408
409                                 snprintf( buf, sizeof( buf ), 
410                                         "cn=Backend %d,%s", 
411                                         j, ms_backend->mss_dn.bv_val );
412                                 bv.bv_val = buf;
413                                 bv.bv_len = strlen( buf );
414                                 attr_merge_normalize_one( e,
415                                                 slap_schema.si_ad_seeAlso,
416                                                 &bv, NULL );
417                                 break;
418                         }
419                 }
420                 /* we must find it! */
421                 assert( j >= 0 );
422
423                 mp = monitor_entrypriv_create();
424                 if ( mp == NULL ) {
425                         return -1;
426                 }
427                 e->e_private = ( void * )mp;
428                 mp->mp_info = ms;
429                 mp->mp_flags = ms->mss_flags
430                         | MONITOR_F_SUB;
431
432                 if ( monitor_cache_add( mi, e ) ) {
433                         Debug( LDAP_DEBUG_ANY,
434                                 "monitor_subsys_database_init: "
435                                 "unable to add entry \"cn=Database %d,%s\"\n",
436                                 i, ms->mss_dn.bv_val, 0 );
437                         return( -1 );
438                 }
439
440 #if defined(LDAP_SLAPI)
441                 monitor_back_add_plugin( mi, be, e );
442 #endif /* defined(LDAP_SLAPI) */
443
444                 if ( oi != NULL ) {
445                         Entry           **ep_overlay = &mp->mp_children;
446                         monitor_entry_t *mp_overlay;
447                         slap_overinst   *on = oi->oi_list;
448                         int             o;
449
450                         for ( o = 0; on; o++, on = on->on_next ) {
451                                 Entry                   *e_overlay;
452                                 slap_overinst           *on2;
453
454                                 /* find the overlay number, j */
455                                 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
456                                         if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
457                                                 break;
458                                         }
459                                 }
460                                 assert( on2 != NULL );
461
462                                 snprintf( buf, sizeof( buf ),
463                                                 "dn: cn=Overlay %d,cn=Database %d,%s\n"
464                                                 "objectClass: %s\n"
465                                                 "structuralObjectClass: %s\n"
466                                                 "cn: Overlay %d\n"
467                                                 "%s: %s\n"
468                                                 "seeAlso: cn=Overlay %d,%s\n"
469                                                 "creatorsName: %s\n"
470                                                 "modifiersName: %s\n"
471                                                 "createTimestamp: %s\n"
472                                                 "modifyTimestamp: %s\n",
473                                                 o,
474                                                 i,
475                                                 ms->mss_dn.bv_val,
476                                                 mi->mi_oc_monitoredObject->soc_cname.bv_val,
477                                                 mi->mi_oc_monitoredObject->soc_cname.bv_val,
478                                                 o,
479                                                 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
480                                                 on->on_bi.bi_type,
481                                                 j,
482                                                 ms_overlay->mss_dn.bv_val,
483                                                 mi->mi_creatorsName.bv_val,
484                                                 mi->mi_creatorsName.bv_val,
485                                                 mi->mi_startTime.bv_val,
486                                                 mi->mi_startTime.bv_val );
487                                 
488                                 e_overlay = str2entry( buf );
489                                 if ( e_overlay == NULL ) {
490                                         Debug( LDAP_DEBUG_ANY,
491                                                 "monitor_subsys_database_init: "
492                                                 "unable to create entry "
493                                                 "\"cn=Overlay %d,cn=Database %d,%s\"\n",
494                                                 o, i, ms->mss_dn.bv_val );
495                                         return( -1 );
496                                 }
497
498                                 mp_overlay = monitor_entrypriv_create();
499                                 if ( mp_overlay == NULL ) {
500                                         return -1;
501                                 }
502                                 e_overlay->e_private = ( void * )mp_overlay;
503                                 mp_overlay->mp_info = ms;
504                                 mp_overlay->mp_flags = ms->mss_flags
505                                         | MONITOR_F_SUB;
506                 
507                                 if ( monitor_cache_add( mi, e_overlay ) ) {
508                                         Debug( LDAP_DEBUG_ANY,
509                                                 "monitor_subsys_database_init: "
510                                                 "unable to add entry "
511                                                 "\"cn=Overlay %d,cn=Database %d,%s\"\n",
512                                                 o, i, ms->mss_dn.bv_val );
513                                         return( -1 );
514                                 }
515
516                                 *ep_overlay = e_overlay;
517                                 ep_overlay = &mp_overlay->mp_next;
518                         }
519                 }
520
521                 *ep = e;
522                 ep = &mp->mp_next;
523         }
524         
525         monitor_cache_release( mi, e_database );
526
527         return( 0 );
528 }
529
530 /*
531  * v: array of values
532  * cur: must not contain the tags corresponding to the values in v
533  * delta: will contain the tags corresponding to the values in v
534  */
535 static int
536 value_mask( BerVarray v, slap_mask_t cur, slap_mask_t *delta )
537 {
538         for ( ; !BER_BVISNULL( v ); v++ ) {
539                 struct restricted_ops_t         *rops;
540                 int                             i;
541
542                 if ( OID_LEADCHAR( v->bv_val[ 0 ] ) ) {
543                         rops = restricted_exops;
544
545                 } else {
546                         rops = restricted_ops;
547                 }
548
549                 for ( i = 0; !BER_BVISNULL( &rops[ i ].op ); i++ ) {
550                         if ( ber_bvstrcasecmp( v, &rops[ i ].op ) != 0 ) {
551                                 continue;
552                         }
553
554                         if ( rops[ i ].tag & *delta ) {
555                                 return LDAP_OTHER;
556                         }
557
558                         if ( rops[ i ].tag & cur ) {
559                                 return LDAP_OTHER;
560                         }
561
562                         cur |= rops[ i ].tag;
563                         *delta |= rops[ i ].tag;
564
565                         break;
566                 }
567
568                 if ( BER_BVISNULL( &rops[ i ].op ) ) {
569                         return LDAP_INVALID_SYNTAX;
570                 }
571         }
572
573         return LDAP_SUCCESS;
574 }
575
576 static int
577 monitor_subsys_database_modify(
578         Operation       *op,
579         SlapReply       *rs,
580         Entry           *e )
581 {
582         monitor_info_t  *mi = (monitor_info_t *)op->o_bd->be_private;
583         int             rc = LDAP_OTHER;
584         Attribute       *save_attrs, *a;
585         Modifications   *ml;
586         Backend         *be;
587         int             ro_gotval = 1, i, n;
588         slap_mask_t     rp_add = 0, rp_delete = 0, rp_cur;
589         struct berval   *tf;
590         
591         i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n );
592         if ( i != 1 ) {
593                 return SLAP_CB_CONTINUE;
594         }
595
596         if ( n < 0 || n >= nBackendDB ) {
597                 rs->sr_text = "invalid database index";
598                 return ( rs->sr_err = LDAP_NO_SUCH_OBJECT );
599         }
600
601         LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
602                 if ( n == 0 ) {
603                         break;
604                 }
605                 n--;
606         }
607         /* do not allow some changes on back-monitor (needs work)... */
608         if ( SLAP_MONITOR( be ) ) {
609                 rs->sr_text = "no modifications allowed to monitor database entry";
610                 return ( rs->sr_err = LDAP_UNWILLING_TO_PERFORM );
611         }
612                 
613         rp_cur = be->be_restrictops;
614
615         save_attrs = e->e_attrs;
616         e->e_attrs = attrs_dup( e->e_attrs );
617
618         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
619                 Modification *mod = &ml->sml_mod;
620
621                 if ( mod->sm_desc == mi->mi_ad_readOnly ) {
622                         int     val = -1;
623
624                         if ( mod->sm_values ) {
625                                 if ( !BER_BVISNULL( &mod->sm_values[ 1 ] ) ) {
626                                         rs->sr_text = "attempting to modify multiple values of single-valued attribute";
627                                         rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
628                                         goto done;
629                                 }
630
631                                 if ( bvmatch( &slap_true_bv, mod->sm_values )) {
632                                         val = 1;
633
634                                 } else if ( bvmatch( &slap_false_bv, mod->sm_values )) {
635                                         val = 0;
636
637                                 } else {
638                                         assert( 0 );
639                                         rc = rs->sr_err = LDAP_INVALID_SYNTAX;
640                                         goto done;
641                                 }
642                         }
643
644                         switch ( mod->sm_op ) {
645                         case LDAP_MOD_DELETE:
646                                 if ( ro_gotval < 1 ) {
647                                         rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
648                                         goto done;
649                                 }
650                                 ro_gotval--;
651
652                                 if ( val == 0 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
653                                         rc = rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
654                                         goto done;
655                                 }
656                                 
657                                 if ( val == 1 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) != SLAP_RESTRICT_OP_WRITES ) {
658                                         rc = rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
659                                         goto done;
660                                 }
661                                 
662                                 break;
663
664                         case LDAP_MOD_REPLACE:
665                                 ro_gotval = 0;
666                                 /* fall thru */
667
668                         case LDAP_MOD_ADD:
669                                 if ( ro_gotval > 0 ) {
670                                         rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
671                                         goto done;
672                                 }
673                                 ro_gotval++;
674
675                                 if ( val == 1 ) {
676                                         rp_add |= (~rp_cur) & SLAP_RESTRICT_OP_WRITES;
677                                         rp_cur |= SLAP_RESTRICT_OP_WRITES;
678                                         rp_delete &= ~SLAP_RESTRICT_OP_WRITES;
679                                         
680                                 } else if ( val == 0 ) {
681                                         rp_delete |= rp_cur & SLAP_RESTRICT_OP_WRITES;
682                                         rp_cur &= ~SLAP_RESTRICT_OP_WRITES;
683                                         rp_add &= ~SLAP_RESTRICT_OP_WRITES;
684                                 }
685                                 break;
686
687                         default:
688                                 rc = rs->sr_err = LDAP_OTHER;
689                                 goto done;
690                         }
691
692                 } else if ( mod->sm_desc == mi->mi_ad_restrictedOperation ) {
693                         slap_mask_t     mask = 0;
694
695                         switch ( mod->sm_op ) {
696                         case LDAP_MOD_DELETE:
697                                 if ( mod->sm_values == NULL ) {
698                                         rp_delete = rp_cur;
699                                         rp_cur = 0;
700                                         rp_add = 0;
701                                         break;
702                                 }
703                                 rc = value_mask( mod->sm_values, ~rp_cur, &mask );
704                                 if ( rc == LDAP_SUCCESS ) {
705                                         rp_delete |= mask;
706                                         rp_add &= ~mask;
707                                         rp_cur &= ~mask;
708
709                                 } else if ( rc == LDAP_OTHER ) {
710                                         rc = LDAP_NO_SUCH_ATTRIBUTE;
711                                 }
712                                 break;
713
714                         case LDAP_MOD_REPLACE:
715                                 rp_delete = rp_cur;
716                                 rp_cur = 0;
717                                 rp_add = 0;
718                                 /* fall thru */
719
720                         case LDAP_MOD_ADD:
721                                 rc = value_mask( mod->sm_values, rp_cur, &mask );
722                                 if ( rc == LDAP_SUCCESS ) {
723                                         rp_add |= mask;
724                                         rp_cur |= mask;
725                                         rp_delete &= ~mask;
726
727                                 } else if ( rc == LDAP_OTHER ) {
728                                         rc = rs->sr_err = LDAP_TYPE_OR_VALUE_EXISTS;
729                                 }
730                                 break;
731
732                         default:
733                                 rc = rs->sr_err = LDAP_OTHER;
734                                 break;
735                         }
736
737                         if ( rc != LDAP_SUCCESS ) {
738                                 goto done;
739                         }
740
741                 } else if ( is_at_operational( mod->sm_desc->ad_type )) {
742                 /* accept all operational attributes */
743                         attr_delete( &e->e_attrs, mod->sm_desc );
744                         rc = attr_merge( e, mod->sm_desc, mod->sm_values,
745                                 mod->sm_nvalues );
746                         if ( rc ) {
747                                 rc = rs->sr_err = LDAP_OTHER;
748                                 break;
749                         }
750
751                 } else {
752                         rc = rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
753                         break;
754                 }
755         }
756
757         /* sanity checks: */
758         if ( ro_gotval < 1 ) {
759                 rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
760                 goto done;
761         }
762
763         if ( ( rp_cur & SLAP_RESTRICT_OP_EXTENDED ) && ( rp_cur & SLAP_RESTRICT_EXOP_MASK ) ) {
764                 rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
765                 goto done;
766         }
767
768         if ( rp_delete & rp_add ) {
769                 rc = rs->sr_err = LDAP_OTHER;
770                 goto done;
771         }
772
773         /* check current value of readOnly */
774         if ( ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
775                 tf = (struct berval *)&slap_true_bv;
776
777         } else {
778                 tf = (struct berval *)&slap_false_bv;
779         }
780
781         a = attr_find( e->e_attrs, mi->mi_ad_readOnly );
782         if ( a == NULL ) {
783                 rc = LDAP_OTHER;
784                 goto done;
785         }
786
787         if ( !bvmatch( &a->a_vals[ 0 ], tf ) ) {
788                 attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
789                 rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, tf );
790         }
791
792         if ( rc == LDAP_SUCCESS ) {
793                 if ( rp_delete ) {
794                         if ( rp_delete == be->be_restrictops ) {
795                                 attr_delete( &e->e_attrs, mi->mi_ad_restrictedOperation );
796
797                         } else {
798                                 a = attr_find( e->e_attrs, mi->mi_ad_restrictedOperation );
799                                 if ( a == NULL ) {
800                                         rc = rs->sr_err = LDAP_OTHER;
801                                         goto done;
802                                 }
803
804                                 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
805                                         if ( rp_delete & restricted_ops[ i ].tag ) {
806                                                 int     j;
807                                         
808                                                 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
809                                                         int             k;
810
811                                                         if ( !bvmatch( &a->a_nvals[ j ], &restricted_ops[ i ].op ) ) {
812                                                                 continue;
813                                                         }
814
815                                                         ch_free( a->a_vals[ j ].bv_val );
816                                                         ch_free( a->a_nvals[ j ].bv_val );
817
818                                                         for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
819                                                                 a->a_vals[ k - 1 ] = a->a_vals[ k ];
820                                                                 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
821                                                         }
822         
823                                                         BER_BVZERO( &a->a_vals[ k - 1 ] );
824                                                         BER_BVZERO( &a->a_nvals[ k - 1 ] );
825                                                 }
826                                         }
827                                 }
828                                 
829                                 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
830                                         if ( rp_delete & restricted_exops[ i ].tag ) {
831                                                 int     j;
832                                         
833                                                 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
834                                                         int             k;
835
836                                                         if ( !bvmatch( &a->a_nvals[ j ], &restricted_exops[ i ].op ) ) {
837                                                                 continue;
838                                                         }
839
840                                                         ch_free( a->a_vals[ j ].bv_val );
841                                                         ch_free( a->a_nvals[ j ].bv_val );
842
843                                                         for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
844                                                                 a->a_vals[ k - 1 ] = a->a_vals[ k ];
845                                                                 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
846                                                         }
847         
848                                                         BER_BVZERO( &a->a_vals[ k - 1 ] );
849                                                         BER_BVZERO( &a->a_nvals[ k - 1 ] );
850                                                 }
851                                         }
852                                 }
853                         }
854                 }
855
856                 if ( rp_add ) {
857                         for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
858                                 if ( rp_add & restricted_ops[ i ].tag ) {
859                                         attr_merge_one( e, mi->mi_ad_restrictedOperation,
860                                                         &restricted_ops[ i ].op,
861                                                         &restricted_ops[ i ].op );
862                                 }
863                         }
864
865                         for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
866                                 if ( rp_add & restricted_exops[ i ].tag ) {
867                                         attr_merge_one( e, mi->mi_ad_restrictedOperation,
868                                                         &restricted_exops[ i ].op,
869                                                         &restricted_exops[ i ].op );
870                                 }
871                         }
872                 }
873         }
874
875         be->be_restrictops = rp_cur;
876
877 done:;
878         if ( rc == LDAP_SUCCESS ) {
879                 attrs_free( save_attrs );
880                 rc = SLAP_CB_CONTINUE;
881
882         } else {
883                 Attribute *tmp = e->e_attrs;
884                 e->e_attrs = save_attrs;
885                 attrs_free( tmp );
886         }
887         return rc;
888 }
889
890 #if defined(LDAP_SLAPI)
891 static int
892 monitor_back_add_plugin( monitor_info_t *mi, Backend *be, Entry *e_database )
893 {
894         Slapi_PBlock    *pCurrentPB; 
895         int             i, rc = LDAP_SUCCESS;
896
897         if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
898                 /*
899                  * LDAP_OTHER is returned if no plugins are installed
900                  */
901                 rc = LDAP_OTHER;
902                 goto done;
903         }
904
905         i = 0;
906         do {
907                 Slapi_PluginDesc        *srchdesc;
908                 char                    buf[ BACKMONITOR_BUFSIZE ];
909                 struct berval           bv;
910
911                 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
912                                 &srchdesc );
913                 if ( rc != LDAP_SUCCESS ) {
914                         goto done;
915                 }
916
917                 snprintf( buf, sizeof(buf),
918                                 "plugin %d name: %s; "
919                                 "vendor: %s; "
920                                 "version: %s; "
921                                 "description: %s", 
922                                 i,
923                                 srchdesc->spd_id,
924                                 srchdesc->spd_vendor,
925                                 srchdesc->spd_version,
926                                 srchdesc->spd_description );
927
928                 ber_str2bv( buf, 0, 0, &bv );
929                 attr_merge_normalize_one( e_database,
930                                 mi->mi_ad_monitoredInfo, &bv, NULL );
931
932                 i++;
933
934         } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
935                         && ( pCurrentPB != NULL ) );
936
937 done:
938         return rc;
939 }
940 #endif /* defined(LDAP_SLAPI) */