1 /* aci.c - routines to parse and check acl's */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
29 #ifdef SLAPD_ACI_ENABLED
35 #include <ac/socket.h>
36 #include <ac/string.h>
37 #include <ac/unistd.h>
43 /* use most appropriate size */
44 #define ACI_BUF_SIZE 1024
46 /* move to "stable" when no longer experimental */
47 #define SLAPD_ACI_SYNTAX "1.3.6.1.4.1.4203.666.2.1"
49 /* change this to "OpenLDAPset" */
50 #define SLAPD_ACI_SET_ATTR "template"
84 static const struct berval aci_bv[] = {
111 BER_BVC(SLAPD_GROUP_CLASS),
112 BER_BVC(SLAPD_GROUP_ATTR),
113 BER_BVC(SLAPD_ROLE_CLASS),
114 BER_BVC(SLAPD_ROLE_ATTR),
116 BER_BVC(SLAPD_ACI_SET_ATTR),
121 static AttributeDescription *slap_ad_aci;
126 struct berval *val );
136 OpenLDAPaciNormalize(
144 #define OpenLDAPaciMatch octetStringMatch
148 struct berval *list )
155 for ( i = 0; acl_get_part( list, i, ',', &bv ) >= 0; i++ ) {
156 if ( bv.bv_len <= 0 ) {
160 switch ( *bv.bv_val ) {
162 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
163 * define any equivalent to the AUTH right, so I've just used
166 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
169 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
170 * the right 'd' to mean "delete"; we hijack it to mean
171 * "disclose" for consistency wuith the rest of slapd.
173 ACL_PRIV_SET(mask, ACL_PRIV_DISCLOSE);
176 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
179 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
180 * the right 's' to mean "set", but in the examples states
181 * that the right 's' means "search". The latter definition
184 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
187 ACL_PRIV_SET(mask, ACL_PRIV_READ);
190 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
204 const struct berval *attr,
207 struct berval bv, left, right;
210 for ( i = 0; acl_get_part( list, i, ',', &bv ) >= 0; i++ ) {
211 if ( acl_get_part(&bv, 0, '=', &left ) < 0
212 || acl_get_part( &bv, 1, '=', &right ) < 0 )
214 if ( ber_bvstrcasecmp( attr, &bv ) == 0 ) {
218 } else if ( val == NULL ) {
219 if ( ber_bvstrcasecmp( attr, &left ) == 0 ) {
224 if ( ber_bvstrcasecmp( attr, &left ) == 0 ) {
225 /* FIXME: this is also totally undocumented! */
226 /* this is experimental code that implements a
227 * simple (prefix) match of the attribute value.
228 * the ACI draft does not provide for aci's that
229 * apply to specific values, but it would be
230 * nice to have. If the <attr> part of an aci's
231 * rights list is of the form <attr>=<value>,
232 * that means the aci applies only to attrs with
233 * the given value. Furthermore, if the attr is
234 * of the form <attr>=<value>*, then <value> is
235 * treated as a prefix, and the aci applies to
236 * any value with that prefix.
238 * Ideally, this would allow r.e. matches.
240 if ( acl_get_part( &right, 0, '*', &left ) < 0
241 || right.bv_len <= left.bv_len )
243 if ( ber_bvstrcasecmp( val, &right ) == 0 ) {
247 } else if ( val->bv_len >= left.bv_len ) {
248 if ( strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0 ) {
260 aci_list_get_attr_rights(
262 const struct berval *attr,
269 /* loop through each rights/attr pair, skip first part (action) */
271 for ( i = 1; acl_get_part( list, i + 1, ';', &bv ) >= 0; i += 2 ) {
272 if ( aci_list_has_attr( &bv, attr, val ) == 0 ) {
276 if ( acl_get_part( list, i, ';', &bv ) < 0 ) {
280 mask |= aci_list_map_rights( &bv );
289 const struct berval *attr,
291 slap_access_t *grant,
292 slap_access_t *deny )
294 struct berval perm, actn;
298 if ( attr == NULL || BER_BVISEMPTY( attr )
299 || ber_bvstrcasecmp( attr, &aci_bv[ ACI_BV_ENTRY ] ) == 0 )
301 attr = &aci_bv[ ACI_BV_BR_ENTRY ];
307 /* loop through each permissions clause */
308 for ( i = 0; acl_get_part( list, i, '$', &perm ) >= 0; i++ ) {
309 if ( acl_get_part( &perm, 0, ';', &actn ) < 0 ) {
313 if ( ber_bvstrcasecmp( &aci_bv[ ACI_BV_GRANT ], &actn ) == 0 ) {
316 } else if ( ber_bvstrcasecmp( &aci_bv[ ACI_BV_DENY ], &actn ) == 0 ) {
324 *mask |= aci_list_get_attr_rights( &perm, attr, val );
325 *mask |= aci_list_get_attr_rights( &perm, &aci_bv[ ACI_BV_BR_ALL ], NULL );
334 const struct berval *defgrpoc,
335 const struct berval *defgrpat,
342 struct berval subjdn;
345 ObjectClass *grp_oc = NULL;
346 AttributeDescription *grp_ad = NULL;
350 /* format of string is "{group|role}/objectClassValue/groupAttrName" */
351 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
355 if ( acl_get_part( subj, 1, '/', &grpoc ) < 0 ) {
359 if ( acl_get_part( subj, 2, '/', &grpat ) < 0 ) {
363 rc = slap_bv2ad( &grpat, &grp_ad, &text );
364 if ( rc != LDAP_SUCCESS ) {
370 grp_oc = oc_bvfind( &grpoc );
372 if ( grp_oc != NULL && grp_ad != NULL ) {
373 char buf[ ACI_BUF_SIZE ];
374 struct berval bv, ndn;
376 bv.bv_len = sizeof( buf ) - 1;
377 bv.bv_val = (char *)&buf;
378 if ( acl_string_expand( &bv, &subjdn,
379 e->e_ndn, nmatch, matches ) )
385 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
387 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
388 grp_oc, grp_ad ) == 0 );
389 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
401 AttributeDescription *desc,
406 slap_access_t *grant,
408 slap_aci_scope_t asserted_scope )
419 assert( !BER_BVISNULL( &desc->ad_cname ) );
421 /* parse an aci of the form:
422 oid # scope # action;rights;attr;rights;attr
423 $ action;rights;attr;rights;attr # type # subject
425 [NOTE: the following comment is very outdated,
426 as the draft version it refers to (Ando, 2004-11-20)].
428 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
429 a full description of the format for this attribute.
430 Differences: "this" in the draft is "self" here, and
431 "self" and "public" is in the position of type.
433 <scope> = {entry|children|subtree}
434 <type> = {public|users|access-id|subtree|onelevel|children|
435 self|dnattr|group|role|set|set-ref}
437 This routine now supports scope={ENTRY,CHILDREN}
439 - ENTRY applies to "entry" and "subtree";
440 - CHILDREN aplies to "children" and "subtree"
443 /* check that the aci has all 5 components */
444 if ( acl_get_part( aci, 4, '#', NULL ) < 0 ) {
448 /* check that the aci family is supported */
449 /* FIXME: the OID is ignored? */
450 if ( acl_get_part( aci, 0, '#', &bv ) < 0 ) {
454 /* check that the scope matches */
455 if ( acl_get_part( aci, 1, '#', &scope ) < 0 ) {
459 /* note: scope can be either ENTRY or CHILDREN;
460 * they respectively match "entry" and "children" in bv
461 * both match "subtree" */
462 switch ( asserted_scope ) {
463 case SLAP_ACI_SCOPE_ENTRY:
464 if ( ber_bvcmp( &scope, &aci_bv[ ACI_BV_ENTRY ] ) != 0
465 && ber_bvstrcasecmp( &scope, &aci_bv[ ACI_BV_SUBTREE ] ) != 0 )
471 case SLAP_ACI_SCOPE_CHILDREN:
472 if ( ber_bvcmp( &scope, &aci_bv[ ACI_BV_CHILDREN ] ) != 0
473 && ber_bvstrcasecmp( &scope, &aci_bv[ ACI_BV_SUBTREE ] ) != 0 )
479 case SLAP_ACI_SCOPE_SUBTREE:
480 /* TODO: add assertion? */
484 /* get the list of permissions clauses, bail if empty */
485 if ( acl_get_part( aci, 2, '#', &perms ) <= 0 ) {
490 /* check if any permissions allow desired access */
491 if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
495 /* see if we have a DN match */
496 if ( acl_get_part( aci, 3, '#', &type ) < 0 ) {
501 /* see if we have a public (i.e. anonymous) access */
502 if ( ber_bvcmp( &aci_bv[ ACI_BV_PUBLIC ], &type ) == 0 ) {
506 /* otherwise require an identity */
507 if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
511 /* see if we have a users access */
512 if ( ber_bvcmp( &aci_bv[ ACI_BV_USERS ], &type ) == 0 ) {
516 /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
517 * just grab all the berval up to its end (ITS#3303).
518 * NOTE: the problem could be solved by providing the DN with
519 * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would
520 * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
522 if ( acl_get_part( aci, 4, '#', &sdn ) < 0 ) {
526 sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
527 sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
529 /* get the type options, if any */
530 if ( acl_get_part( &type, 1, '/', &opts ) > 0 ) {
531 opts.bv_len = type.bv_len - ( opts.bv_val - type.bv_val );
532 type.bv_len = opts.bv_val - type.bv_val - 1;
538 if ( ber_bvcmp( &aci_bv[ ACI_BV_ACCESS_ID ], &type ) == 0 ) {
539 return dn_match( &op->o_ndn, &sdn );
541 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_SUBTREE ], &type ) == 0 ) {
542 return dnIsSuffix( &op->o_ndn, &sdn );
544 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_ONELEVEL ], &type ) == 0 ) {
547 dnParent( &sdn, &pdn );
549 return dn_match( &op->o_ndn, &pdn );
551 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_CHILDREN ], &type ) == 0 ) {
552 return ( !dn_match( &op->o_ndn, &sdn ) && dnIsSuffix( &op->o_ndn, &sdn ) );
554 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_SELF ], &type ) == 0 ) {
555 return dn_match( &op->o_ndn, &e->e_nname );
557 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_DNATTR ], &type ) == 0 ) {
559 AttributeDescription *ad = NULL;
562 rc = slap_bv2ad( &sdn, &ad, &text );
563 assert( rc == LDAP_SUCCESS );
566 for ( at = attrs_find( e->e_attrs, ad );
568 at = attrs_find( at->a_next, ad ) )
570 if ( value_find_ex( ad,
571 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
572 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
574 &op->o_ndn, op->o_tmpmemctx ) == 0 )
583 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_GROUP ], &type ) == 0 ) {
587 if ( BER_BVISNULL( &opts ) ) {
588 oc = aci_bv[ ACI_BV_GROUP_CLASS ];
589 at = aci_bv[ ACI_BV_GROUP_ATTR ];
592 if ( acl_get_part( &opts, 0, '/', &oc ) < 0 ) {
596 if ( acl_get_part( &opts, 1, '/', &at ) < 0 ) {
597 at = aci_bv[ ACI_BV_GROUP_ATTR ];
601 if ( aci_group_member( &sdn, &oc, &at, op, e, nmatch, matches ) )
606 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_ROLE ], &type ) == 0 ) {
610 if ( BER_BVISNULL( &opts ) ) {
611 oc = aci_bv[ ACI_BV_ROLE_CLASS ];
612 at = aci_bv[ ACI_BV_ROLE_ATTR ];
615 if ( acl_get_part( &opts, 0, '/', &oc ) < 0 ) {
619 if ( acl_get_part( &opts, 1, '/', &at ) < 0 ) {
620 at = aci_bv[ ACI_BV_ROLE_ATTR ];
624 if ( aci_group_member( &sdn, &oc, &at, op, e, nmatch, matches ) )
629 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_SET ], &type ) == 0 ) {
630 if ( acl_match_set( &sdn, op, e, NULL ) ) {
634 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_SET_REF ], &type ) == 0 ) {
635 if ( acl_match_set( &sdn, op, e, (struct berval *)&aci_bv[ ACI_BV_SET_ATTR ] ) ) {
640 /* it passed normalization! */
650 /* OpenLDAP eXperimental Syntax */
651 static slap_syntax_defs_rec aci_syntax_def = {
652 "( 1.3.6.1.4.1.4203.666.2.1 DESC 'OpenLDAP Experimental ACI' )",
657 static slap_mrule_defs_rec aci_mr_def = {
658 "( 1.3.6.1.4.1.4203.666.4.2 NAME 'OpenLDAPaciMatch' "
659 "SYNTAX 1.3.6.1.4.1.4203.666.2.1 )",
660 SLAP_MR_HIDE | SLAP_MR_EQUALITY, NULL,
661 NULL, OpenLDAPaciNormalize, OpenLDAPaciMatch,
669 AttributeDescription **ad;
671 "OpenLDAPaci", "( 1.3.6.1.4.1.4203.666.1.5 "
672 "NAME 'OpenLDAPaci' "
673 "DESC 'OpenLDAP access control information (experimental)' "
674 "EQUALITY OpenLDAPaciMatch "
675 "SYNTAX 1.3.6.1.4.1.4203.666.2.1 "
676 "USAGE directoryOperation )",
681 LDAPAttributeType *at;
687 rc = register_syntax( &aci_syntax_def );
692 /* ACI equality rule */
693 rc = register_matching_rule( &aci_mr_def );
699 at = ldap_str2attributetype( aci_at.desc,
700 &rc, &text, LDAP_SCHEMA_ALLOW_ALL );
702 Debug( LDAP_DEBUG_ANY,
703 "aci_init: AttributeType \"%s\" parse failed: %s %s\n",
704 aci_at.name, ldap_scherr2str( rc ), text );
708 rc = at_add( at, 0, &sat, &text );
709 if ( rc != LDAP_SUCCESS ) {
710 ldap_attributetype_free( at );
711 Debug( LDAP_DEBUG_ANY,
712 "aci_init: AttributeType \"%s\" load failed: %s %s\n",
713 aci_at.name, scherr2str( rc ), text );
718 rc = slap_str2ad( aci_at.name,
720 if ( rc != LDAP_SUCCESS ) {
721 Debug( LDAP_DEBUG_ANY,
722 "aci_init: unable to find AttributeDescription "
724 aci_at.name, rc, text );
729 sat->sat_flags |= aci_at.flags;
743 AttributeDescription *ad = NULL;
744 const char *text = NULL;
746 if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
747 fprintf( stderr, "%s: line %d: "
748 "inappropriate style \"%s\" in \"aci\" by clause\n",
749 fname, lineno, style_strings[sty] );
753 if ( right != NULL && *right != '\0' ) {
754 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
756 "%s: line %d: aci \"%s\": %s\n",
757 fname, lineno, right, text );
765 if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
766 fprintf( stderr, "%s: line %d: "
767 "aci \"%s\": inappropriate syntax: %s\n",
768 fname, lineno, right,
769 ad->ad_type->sat_syntax_oid );
779 dynacl_aci_unparse( void *priv, struct berval *bv )
781 AttributeDescription *ad = ( AttributeDescription * )priv;
784 assert( ad != NULL );
786 bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
787 ptr = lutil_strcopy( bv->bv_val, " aci=" );
788 ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
789 bv->bv_len = ptr - bv->bv_val;
799 AttributeDescription *desc,
803 slap_access_t *grantp,
804 slap_access_t *denyp )
806 AttributeDescription *ad = ( AttributeDescription * )priv;
808 slap_access_t tgrant, tdeny, grant, deny;
810 char accessmaskbuf[ACCESSMASK_MAXLEN];
811 char accessmaskbuf1[ACCESSMASK_MAXLEN];
812 #endif /* LDAP_DEBUG */
814 if ( BER_BVISEMPTY( &e->e_nname ) ) {
815 /* no ACIs in the root DSE */
819 /* start out with nothing granted, nothing denied */
823 /* get the aci attribute */
824 at = attr_find( e->e_attrs, ad );
828 /* the aci is an multi-valued attribute. The
829 * rights are determined by OR'ing the individual
830 * rights given by the acis.
832 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
833 if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
834 nmatch, matches, &grant, &deny,
835 SLAP_ACI_SCOPE_ENTRY ) != 0 )
842 Debug( LDAP_DEBUG_ACL, " <= aci_mask grant %s deny %s\n",
843 accessmask2str( tgrant, accessmaskbuf, 1 ),
844 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
847 /* If the entry level aci didn't contain anything valid for the
848 * current operation, climb up the tree and evaluate the
849 * acis with scope set to subtree
851 if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
852 struct berval parent_ndn;
854 dnParent( &e->e_nname, &parent_ndn );
855 while ( !BER_BVISEMPTY( &parent_ndn ) ){
857 BerVarray bvals = NULL;
860 /* to solve the chicken'n'egg problem of accessing
861 * the OpenLDAPaci attribute, the direct access
862 * to the entry's attribute is unchecked; however,
863 * further accesses to OpenLDAPaci values in the
864 * ancestors occur through backend_attribute(), i.e.
865 * with the identity of the operation, requiring
866 * further access checking. For uniformity, this
867 * makes further requests occur as the rootdn, if
868 * any, i.e. searching for the OpenLDAPaci attribute
869 * is considered an internal search. If this is not
870 * acceptable, then the same check needs be performed
871 * when accessing the entry's attribute. */
872 struct berval save_o_dn, save_o_ndn;
874 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
875 save_o_dn = op->o_dn;
876 save_o_ndn = op->o_ndn;
878 op->o_dn = op->o_bd->be_rootdn;
879 op->o_ndn = op->o_bd->be_rootndn;
882 Debug( LDAP_DEBUG_ACL, " checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
883 ret = backend_attribute( op, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
885 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
886 op->o_dn = save_o_dn;
887 op->o_ndn = save_o_ndn;
897 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++ ) {
898 if ( aci_mask( op, e, desc, val,
902 SLAP_ACI_SCOPE_CHILDREN ) != 0 )
906 /* evaluation stops as soon as either a "deny" or a
907 * "grant" directive matches.
909 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
913 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
914 accessmask2str( tgrant, accessmaskbuf, 1 ),
915 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
919 case LDAP_NO_SUCH_ATTRIBUTE:
920 /* just go on if the aci-Attribute is not present in
923 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
927 case LDAP_NO_SUCH_OBJECT:
928 /* We have reached the base object */
929 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
941 dnParent( &parent_ndn, &parent_ndn );
951 /* need to register this at some point */
952 static slap_dynacl_t dynacl_aci = {
963 dynacl_aci_init( void )
970 rc = slap_dynacl_register( &dynacl_aci );
977 /* ACI syntax validation */
980 * Matches given berval to array of bervals
982 * >=0 if one if the array elements equals to this berval
983 * -1 if string was not found in array
988 const struct berval *arr[] )
992 if ( BER_BVISEMPTY( bv ) ) {
996 for ( i = 0; arr[ i ] != NULL ; i++ ) {
997 if ( ber_bvstrcasecmp( bv, arr[ i ] ) == 0 ) {
1006 /* Returns what have left in input berval after current sub */
1011 struct berval *tail )
1015 tail->bv_val = sub->bv_val + sub->bv_len;
1016 head_len = (unsigned long) tail->bv_val - (unsigned long) val->bv_val;
1017 tail->bv_len = val->bv_len - head_len;
1022 * aci is accepted in following form:
1023 * oid#scope#rights#type#subject
1025 * oid := numeric OID
1026 * scope := entry|children
1027 * rights := right[[$right]...]
1028 * right := (grant|deny);action
1029 * action := perms;attr[[;perms;attr]...]
1030 * perms := perm[[,perm]...]
1032 * attr := attributeType|[all]
1033 * type := public|users|self|dnattr|group|role|set|set-ref|
1034 * access_id|subtree|onelevel|children
1037 OpenLDAPaciValidatePerms(
1038 struct berval *perms )
1042 for ( i = 0; i < perms->bv_len; ) {
1043 switch ( perms->bv_val[ i ] ) {
1053 return LDAP_INVALID_SYNTAX;
1056 if ( ++i == perms->bv_len ) {
1057 return LDAP_SUCCESS;
1060 while ( i < perms->bv_len && perms->bv_val[ i ] == ' ' )
1063 assert( i != perms->bv_len );
1065 if ( perms->bv_val[ i ] != ',' ) {
1066 return LDAP_INVALID_SYNTAX;
1071 } while ( perms->bv_val[ i ] == ' ' );
1074 return LDAP_SUCCESS;
1077 static const struct berval *ACIgrantdeny[] = {
1078 &aci_bv[ ACI_BV_GRANT ],
1079 &aci_bv[ ACI_BV_DENY ],
1084 OpenLDAPaciValidateRight(
1085 struct berval *action )
1087 struct berval bv = BER_BVNULL;
1091 if ( acl_get_part( action, 0, ';', &bv ) < 0 ||
1092 bv_getcaseidx( &bv, ACIgrantdeny ) == -1 )
1094 return LDAP_INVALID_SYNTAX;
1097 for ( i = 0; acl_get_part( action, i + 1, ';', &bv ) >= 0; i++ ) {
1100 if ( OpenLDAPaciValidatePerms( &bv ) != LDAP_SUCCESS )
1102 return LDAP_INVALID_SYNTAX;
1107 AttributeDescription *ad = NULL;
1108 const char *text = NULL;
1110 /* could be "[all]" or an attribute description */
1111 if ( ber_bvstrcasecmp( &bv, &aci_bv[ ACI_BV_BR_ALL ] ) == 0 ) {
1115 if ( slap_bv2ad( &bv, &ad, &text ) != LDAP_SUCCESS ) {
1116 return LDAP_INVALID_SYNTAX;
1121 /* "perms;attr" go in pairs */
1122 if ( i > 0 && ( i & 1 ) == 0 ) {
1123 return LDAP_SUCCESS;
1126 return LDAP_INVALID_SYNTAX;
1129 return LDAP_SUCCESS;
1133 OpenLDAPaciNormalizeRight(
1134 struct berval *action,
1135 struct berval *naction,
1138 struct berval grantdeny,
1145 if ( acl_get_part( action, 0, ';', &grantdeny ) < 0 ) {
1146 return LDAP_INVALID_SYNTAX;
1148 idx = bv_getcaseidx( &grantdeny, ACIgrantdeny );
1150 return LDAP_INVALID_SYNTAX;
1153 ber_dupbv_x( naction, (struct berval *)ACIgrantdeny[ idx ], ctx );
1155 for ( i = 1; acl_get_part( action, i, ';', &bv ) >= 0; i++ ) {
1158 if ( OpenLDAPaciValidatePerms( &bv ) != LDAP_SUCCESS )
1160 return LDAP_INVALID_SYNTAX;
1168 /* could be "[all]" or an attribute description */
1169 if ( ber_bvstrcasecmp( &bv, &aci_bv[ ACI_BV_BR_ALL ] ) == 0 ) {
1170 bv = aci_bv[ ACI_BV_BR_ALL ];
1173 AttributeDescription *ad = NULL;
1174 const char *text = NULL;
1177 rc = slap_bv2ad( &bv, &ad, &text );
1178 if ( rc != LDAP_SUCCESS ) {
1179 return LDAP_INVALID_SYNTAX;
1185 naction->bv_val = ber_memrealloc_x( naction->bv_val,
1186 naction->bv_len + STRLENOF( ";" )
1187 + perms.bv_len + STRLENOF( ";" )
1191 ptr = &naction->bv_val[ naction->bv_len ];
1194 ptr = lutil_strncopy( ptr, perms.bv_val, perms.bv_len );
1197 ptr = lutil_strncopy( ptr, bv.bv_val, bv.bv_len );
1199 naction->bv_len += STRLENOF( ";" ) + perms.bv_len
1200 + STRLENOF( ";" ) + bv.bv_len;
1204 /* perms;attr go in pairs */
1205 if ( i > 1 && ( i & 1 ) ) {
1206 return LDAP_SUCCESS;
1209 return LDAP_INVALID_SYNTAX;
1214 OpenLDAPaciValidateRights(
1215 struct berval *actions )
1218 struct berval bv = BER_BVNULL;
1221 for ( i = 0; acl_get_part( actions, i, '$', &bv ) >= 0; i++ ) {
1222 if ( OpenLDAPaciValidateRight( &bv ) != LDAP_SUCCESS ) {
1223 return LDAP_INVALID_SYNTAX;
1227 return LDAP_SUCCESS;
1231 OpenLDAPaciNormalizeRights(
1232 struct berval *actions,
1233 struct berval *nactions,
1237 struct berval bv = BER_BVNULL;
1240 BER_BVZERO( nactions );
1241 for ( i = 0; acl_get_part( actions, i, '$', &bv ) >= 0; i++ ) {
1245 rc = OpenLDAPaciNormalizeRight( &bv, &nbv, ctx );
1246 if ( rc != LDAP_SUCCESS ) {
1247 ber_memfree_x( nactions->bv_val, ctx );
1248 BER_BVZERO( nactions );
1249 return LDAP_INVALID_SYNTAX;
1256 nactions->bv_val = ber_memrealloc_x( nactions->bv_val,
1257 nactions->bv_len + STRLENOF( "$" )
1260 nactions->bv_val[ nactions->bv_len ] = '$';
1261 AC_MEMCPY( &nactions->bv_val[ nactions->bv_len + 1 ],
1262 nbv.bv_val, nbv.bv_len + 1 );
1263 ber_memfree_x( nbv.bv_val, ctx );
1264 nactions->bv_len += STRLENOF( "$" ) + nbv.bv_len;
1269 return LDAP_SUCCESS;
1272 static const struct berval *OpenLDAPaciscopes[] = {
1273 &aci_bv[ ACI_BV_ENTRY ],
1274 &aci_bv[ ACI_BV_CHILDREN ],
1275 &aci_bv[ ACI_BV_SUBTREE ],
1280 static const struct berval *OpenLDAPacitypes[] = {
1282 &aci_bv[ ACI_BV_GROUP ],
1283 &aci_bv[ ACI_BV_ROLE ],
1285 /* set to one past the last DN-valued type with options (/) */
1286 #define LAST_OPTIONAL 2
1288 &aci_bv[ ACI_BV_ACCESS_ID ],
1289 &aci_bv[ ACI_BV_SUBTREE ],
1290 &aci_bv[ ACI_BV_ONELEVEL ],
1291 &aci_bv[ ACI_BV_CHILDREN ],
1293 /* set to one past the last DN-valued type */
1294 #define LAST_DNVALUED 6
1297 &aci_bv[ ACI_BV_DNATTR ],
1298 &aci_bv[ ACI_BV_PUBLIC ],
1299 &aci_bv[ ACI_BV_USERS ],
1300 &aci_bv[ ACI_BV_SELF ],
1301 &aci_bv[ ACI_BV_SET ],
1302 &aci_bv[ ACI_BV_SET_REF ],
1308 OpenLDAPaciValidate(
1310 struct berval *val )
1312 struct berval oid = BER_BVNULL,
1314 rights = BER_BVNULL,
1316 subject = BER_BVNULL;
1319 if ( BER_BVISEMPTY( val ) ) {
1320 return LDAP_INVALID_SYNTAX;
1324 if ( acl_get_part( val, 0, '#', &oid ) < 0 ||
1325 numericoidValidate( NULL, &oid ) != LDAP_SUCCESS )
1327 /* NOTE: the numericoidValidate() is rather pedantic;
1328 * I'd replace it with X-ORDERED VALUES so that
1329 * it's guaranteed values are maintained and used
1330 * in the desired order */
1331 return LDAP_INVALID_SYNTAX;
1335 if ( acl_get_part( val, 1, '#', &scope ) < 0 ||
1336 bv_getcaseidx( &scope, OpenLDAPaciscopes ) == -1 )
1338 return LDAP_INVALID_SYNTAX;
1342 if ( acl_get_part( val, 2, '#', &rights ) < 0 ||
1343 OpenLDAPaciValidateRights( &rights ) != LDAP_SUCCESS )
1345 return LDAP_INVALID_SYNTAX;
1349 if ( acl_get_part( val, 3, '#', &type ) < 0 ) {
1350 return LDAP_INVALID_SYNTAX;
1352 idx = bv_getcaseidx( &type, OpenLDAPacitypes );
1356 if ( acl_get_part( &type, 0, '/', &isgr ) < 0 ) {
1357 return LDAP_INVALID_SYNTAX;
1360 idx = bv_getcaseidx( &isgr, OpenLDAPacitypes );
1361 if ( idx == -1 || idx >= LAST_OPTIONAL ) {
1362 return LDAP_INVALID_SYNTAX;
1367 bv_get_tail( val, &type, &subject );
1368 if ( subject.bv_val[ 0 ] != '#' ) {
1369 return LDAP_INVALID_SYNTAX;
1372 if ( idx >= LAST_DNVALUED ) {
1373 if ( OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_DNATTR ] ) {
1374 AttributeDescription *ad = NULL;
1375 const char *text = NULL;
1378 rc = slap_bv2ad( &subject, &ad, &text );
1379 if ( rc != LDAP_SUCCESS ) {
1380 return LDAP_INVALID_SYNTAX;
1383 if ( ad->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
1384 /* FIXME: allow nameAndOptionalUID? */
1385 return LDAP_INVALID_SYNTAX;
1390 return LDAP_SUCCESS;
1392 } else if ( OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_GROUP ]
1393 || OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_ROLE ] )
1395 /* do {group|role}/oc/at check */
1396 struct berval ocbv = BER_BVNULL,
1399 ocbv.bv_val = ber_bvchr( &type, '/' );
1400 if ( ocbv.bv_val != NULL ) {
1402 ocbv.bv_len = type.bv_len
1403 - ( ocbv.bv_val - type.bv_val );
1405 atbv.bv_val = ber_bvchr( &ocbv, '/' );
1406 if ( atbv.bv_val != NULL ) {
1407 AttributeDescription *ad = NULL;
1408 const char *text = NULL;
1412 atbv.bv_len = type.bv_len
1413 - ( atbv.bv_val - type.bv_val );
1414 ocbv.bv_len = atbv.bv_val - ocbv.bv_val - 1;
1416 rc = slap_bv2ad( &atbv, &ad, &text );
1417 if ( rc != LDAP_SUCCESS ) {
1418 return LDAP_INVALID_SYNTAX;
1422 if ( oc_bvfind( &ocbv ) == NULL ) {
1423 return LDAP_INVALID_SYNTAX;
1428 if ( BER_BVISEMPTY( &subject ) ) {
1429 /* empty DN invalid */
1430 return LDAP_INVALID_SYNTAX;
1436 /* FIXME: pass DN syntax? */
1437 return dnValidate( NULL, &subject );
1441 OpenLDAPaciPrettyNormal(
1447 struct berval oid = BER_BVNULL,
1449 rights = BER_BVNULL,
1450 nrights = BER_BVNULL,
1453 subject = BER_BVNULL,
1454 nsubject = BER_BVNULL;
1461 if ( BER_BVISEMPTY( val ) ) {
1462 return LDAP_INVALID_SYNTAX;
1465 /* oid: if valid, it's already normalized */
1466 if ( acl_get_part( val, 0, '#', &oid ) < 0 ||
1467 numericoidValidate( NULL, &oid ) != LDAP_SUCCESS )
1469 return LDAP_INVALID_SYNTAX;
1472 /* scope: normalize by replacing with OpenLDAPaciscopes */
1473 if ( acl_get_part( val, 1, '#', &scope ) < 0 ) {
1474 return LDAP_INVALID_SYNTAX;
1476 idx = bv_getcaseidx( &scope, OpenLDAPaciscopes );
1478 return LDAP_INVALID_SYNTAX;
1480 scope = *OpenLDAPaciscopes[ idx ];
1483 if ( acl_get_part( val, 2, '#', &rights ) < 0 ) {
1484 return LDAP_INVALID_SYNTAX;
1486 if ( OpenLDAPaciNormalizeRights( &rights, &nrights, ctx )
1489 return LDAP_INVALID_SYNTAX;
1493 if ( acl_get_part( val, 3, '#', &type ) < 0 ) {
1494 rc = LDAP_INVALID_SYNTAX;
1497 idx = bv_getcaseidx( &type, OpenLDAPacitypes );
1501 if ( acl_get_part( &type, 0, '/', &isgr ) < 0 ) {
1502 rc = LDAP_INVALID_SYNTAX;
1506 idx = bv_getcaseidx( &isgr, OpenLDAPacitypes );
1507 if ( idx == -1 || idx >= LAST_OPTIONAL ) {
1508 rc = LDAP_INVALID_SYNTAX;
1512 ntype = *OpenLDAPacitypes[ idx ];
1515 bv_get_tail( val, &type, &subject );
1517 if ( BER_BVISEMPTY( &subject ) || subject.bv_val[ 0 ] != '#' ) {
1518 rc = LDAP_INVALID_SYNTAX;
1525 if ( idx < LAST_DNVALUED ) {
1526 /* FIXME: pass DN syntax? */
1528 rc = dnNormalize( 0, NULL, NULL,
1529 &subject, &nsubject, ctx );
1531 rc = dnPretty( NULL, &subject, &nsubject, ctx );
1534 if ( rc == LDAP_SUCCESS ) {
1541 if ( OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_GROUP ]
1542 || OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_ROLE ] )
1544 /* do {group|role}/oc/at check */
1545 struct berval ocbv = BER_BVNULL,
1548 ocbv.bv_val = ber_bvchr( &type, '/' );
1549 if ( ocbv.bv_val != NULL ) {
1550 ObjectClass *oc = NULL;
1551 AttributeDescription *ad = NULL;
1552 const char *text = NULL;
1556 bv.bv_len = ntype.bv_len;
1559 ocbv.bv_len = type.bv_len - ( ocbv.bv_val - type.bv_val );
1561 atbv.bv_val = ber_bvchr( &ocbv, '/' );
1562 if ( atbv.bv_val != NULL ) {
1564 atbv.bv_len = type.bv_len
1565 - ( atbv.bv_val - type.bv_val );
1566 ocbv.bv_len = atbv.bv_val - ocbv.bv_val - 1;
1568 rc = slap_bv2ad( &atbv, &ad, &text );
1569 if ( rc != LDAP_SUCCESS ) {
1570 rc = LDAP_INVALID_SYNTAX;
1574 bv.bv_len += STRLENOF( "/" ) + ad->ad_cname.bv_len;
1577 oc = oc_bvfind( &ocbv );
1579 rc = LDAP_INVALID_SYNTAX;
1583 bv.bv_len += STRLENOF( "/" ) + oc->soc_cname.bv_len;
1584 bv.bv_val = ber_memalloc_x( bv.bv_len + 1, ctx );
1587 ptr = lutil_strncopy( ptr, ntype.bv_val, ntype.bv_len );
1590 ptr = lutil_strncopy( ptr,
1591 oc->soc_cname.bv_val,
1592 oc->soc_cname.bv_len );
1596 ptr = lutil_strncopy( ptr,
1597 ad->ad_cname.bv_val,
1598 ad->ad_cname.bv_len );
1607 } else if ( OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_DNATTR ] ) {
1608 AttributeDescription *ad = NULL;
1609 const char *text = NULL;
1612 rc = slap_bv2ad( &subject, &ad, &text );
1613 if ( rc != LDAP_SUCCESS ) {
1614 rc = LDAP_INVALID_SYNTAX;
1618 if ( ad->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
1619 /* FIXME: allow nameAndOptionalUID? */
1620 rc = LDAP_INVALID_SYNTAX;
1624 nsubject = ad->ad_cname;
1629 oid.bv_len + STRLENOF( "#" )
1630 + scope.bv_len + STRLENOF( "#" )
1631 + rights.bv_len + STRLENOF( "#" )
1632 + ntype.bv_len + STRLENOF( "#" )
1635 out->bv_val = ber_memalloc_x( out->bv_len + 1, ctx );
1636 ptr = lutil_strncopy( out->bv_val, oid.bv_val, oid.bv_len );
1639 ptr = lutil_strncopy( ptr, scope.bv_val, scope.bv_len );
1642 ptr = lutil_strncopy( ptr, nrights.bv_val, nrights.bv_len );
1645 ptr = lutil_strncopy( ptr, ntype.bv_val, ntype.bv_len );
1648 if ( !BER_BVISNULL( &nsubject ) ) {
1649 ptr = lutil_strncopy( ptr, nsubject.bv_val, nsubject.bv_len );
1654 if ( freesubject ) {
1655 ber_memfree_x( nsubject.bv_val, ctx );
1659 ber_memfree_x( ntype.bv_val, ctx );
1662 if ( !BER_BVISNULL( &nrights ) ) {
1663 ber_memfree_x( nrights.bv_val, ctx );
1676 return OpenLDAPaciPrettyNormal( val, out, ctx, 0 );
1680 OpenLDAPaciNormalize(
1688 return OpenLDAPaciPrettyNormal( val, out, ctx, 1 );
1691 #if SLAPD_ACI_ENABLED == SLAPD_MOD_DYNAMIC
1693 * FIXME: need config and Makefile.am code to ease building
1697 init_module( int argc, char *argv[] )
1699 return dynacl_aci_init();
1701 #endif /* SLAPD_ACI_ENABLED == SLAPD_MOD_DYNAMIC */
1703 #endif /* SLAPD_ACI_ENABLED */