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"
52 typedef enum slap_aci_scope_t {
53 SLAP_ACI_SCOPE_ENTRY = 0x1,
54 SLAP_ACI_SCOPE_CHILDREN = 0x2,
55 SLAP_ACI_SCOPE_SUBTREE = ( SLAP_ACI_SCOPE_ENTRY | SLAP_ACI_SCOPE_CHILDREN )
90 static const struct berval aci_bv[] = {
102 BER_BVC("access-id"),
117 BER_BVC(SLAPD_GROUP_CLASS),
118 BER_BVC(SLAPD_GROUP_ATTR),
119 BER_BVC(SLAPD_ROLE_CLASS),
120 BER_BVC(SLAPD_ROLE_ATTR),
122 BER_BVC(SLAPD_ACI_SET_ATTR),
127 static AttributeDescription *slap_ad_aci;
132 struct berval *val );
142 OpenLDAPaciNormalize(
150 #define OpenLDAPaciMatch octetStringMatch
154 struct berval *list )
161 for ( i = 0; acl_get_part( list, i, ',', &bv ) >= 0; i++ ) {
162 if ( bv.bv_len <= 0 ) {
166 switch ( *bv.bv_val ) {
168 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
169 * define any equivalent to the AUTH right, so I've just used
172 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
175 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
176 * the right 'd' to mean "delete"; we hijack it to mean
177 * "disclose" for consistency wuith the rest of slapd.
179 ACL_PRIV_SET(mask, ACL_PRIV_DISCLOSE);
182 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
185 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
186 * the right 's' to mean "set", but in the examples states
187 * that the right 's' means "search". The latter definition
190 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
193 ACL_PRIV_SET(mask, ACL_PRIV_READ);
196 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
210 const struct berval *attr,
213 struct berval bv, left, right;
216 for ( i = 0; acl_get_part( list, i, ',', &bv ) >= 0; i++ ) {
217 if ( acl_get_part(&bv, 0, '=', &left ) < 0
218 || acl_get_part( &bv, 1, '=', &right ) < 0 )
220 if ( ber_bvstrcasecmp( attr, &bv ) == 0 ) {
224 } else if ( val == NULL ) {
225 if ( ber_bvstrcasecmp( attr, &left ) == 0 ) {
230 if ( ber_bvstrcasecmp( attr, &left ) == 0 ) {
231 /* FIXME: this is also totally undocumented! */
232 /* this is experimental code that implements a
233 * simple (prefix) match of the attribute value.
234 * the ACI draft does not provide for aci's that
235 * apply to specific values, but it would be
236 * nice to have. If the <attr> part of an aci's
237 * rights list is of the form <attr>=<value>,
238 * that means the aci applies only to attrs with
239 * the given value. Furthermore, if the attr is
240 * of the form <attr>=<value>*, then <value> is
241 * treated as a prefix, and the aci applies to
242 * any value with that prefix.
244 * Ideally, this would allow r.e. matches.
246 if ( acl_get_part( &right, 0, '*', &left ) < 0
247 || right.bv_len <= left.bv_len )
249 if ( ber_bvstrcasecmp( val, &right ) == 0 ) {
253 } else if ( val->bv_len >= left.bv_len ) {
254 if ( strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0 ) {
266 aci_list_get_attr_rights(
268 const struct berval *attr,
275 /* loop through each rights/attr pair, skip first part (action) */
277 for ( i = 1; acl_get_part( list, i + 1, ';', &bv ) >= 0; i += 2 ) {
278 if ( aci_list_has_attr( &bv, attr, val ) == 0 ) {
282 if ( acl_get_part( list, i, ';', &bv ) < 0 ) {
286 mask |= aci_list_map_rights( &bv );
295 const struct berval *attr,
297 slap_access_t *grant,
298 slap_access_t *deny )
300 struct berval perm, actn;
304 if ( attr == NULL || BER_BVISEMPTY( attr )
305 || ber_bvstrcasecmp( attr, &aci_bv[ ACI_BV_ENTRY ] ) == 0 )
307 attr = &aci_bv[ ACI_BV_BR_ENTRY ];
313 /* loop through each permissions clause */
314 for ( i = 0; acl_get_part( list, i, '$', &perm ) >= 0; i++ ) {
315 if ( acl_get_part( &perm, 0, ';', &actn ) < 0 ) {
319 if ( ber_bvstrcasecmp( &aci_bv[ ACI_BV_GRANT ], &actn ) == 0 ) {
322 } else if ( ber_bvstrcasecmp( &aci_bv[ ACI_BV_DENY ], &actn ) == 0 ) {
330 *mask |= aci_list_get_attr_rights( &perm, attr, val );
331 *mask |= aci_list_get_attr_rights( &perm, &aci_bv[ ACI_BV_BR_ALL ], NULL );
340 const struct berval *defgrpoc,
341 const struct berval *defgrpat,
348 struct berval subjdn;
351 ObjectClass *grp_oc = NULL;
352 AttributeDescription *grp_ad = NULL;
356 /* format of string is "{group|role}/objectClassValue/groupAttrName" */
357 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
361 if ( acl_get_part( subj, 1, '/', &grpoc ) < 0 ) {
365 if ( acl_get_part( subj, 2, '/', &grpat ) < 0 ) {
369 rc = slap_bv2ad( &grpat, &grp_ad, &text );
370 if ( rc != LDAP_SUCCESS ) {
376 grp_oc = oc_bvfind( &grpoc );
378 if ( grp_oc != NULL && grp_ad != NULL ) {
379 char buf[ ACI_BUF_SIZE ];
380 struct berval bv, ndn;
382 bv.bv_len = sizeof( buf ) - 1;
383 bv.bv_val = (char *)&buf;
384 if ( acl_string_expand( &bv, &subjdn,
385 e->e_ndn, nmatch, matches ) )
391 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
393 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
394 grp_oc, grp_ad ) == 0 );
395 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
407 AttributeDescription *desc,
412 slap_access_t *grant,
414 slap_aci_scope_t asserted_scope )
425 assert( !BER_BVISNULL( &desc->ad_cname ) );
427 /* parse an aci of the form:
428 oid # scope # action;rights;attr;rights;attr
429 $ action;rights;attr;rights;attr # type # subject
431 [NOTE: the following comment is very outdated,
432 as the draft version it refers to (Ando, 2004-11-20)].
434 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
435 a full description of the format for this attribute.
436 Differences: "this" in the draft is "self" here, and
437 "self" and "public" is in the position of type.
439 <scope> = {entry|children|subtree}
440 <type> = {public|users|access-id|subtree|onelevel|children|
441 self|dnattr|group|role|set|set-ref}
443 This routine now supports scope={ENTRY,CHILDREN}
445 - ENTRY applies to "entry" and "subtree";
446 - CHILDREN aplies to "children" and "subtree"
449 /* check that the aci has all 5 components */
450 if ( acl_get_part( aci, 4, '#', NULL ) < 0 ) {
454 /* check that the aci family is supported */
455 /* FIXME: the OID is ignored? */
456 if ( acl_get_part( aci, 0, '#', &bv ) < 0 ) {
460 /* check that the scope matches */
461 if ( acl_get_part( aci, 1, '#', &scope ) < 0 ) {
465 /* note: scope can be either ENTRY or CHILDREN;
466 * they respectively match "entry" and "children" in bv
467 * both match "subtree" */
468 switch ( asserted_scope ) {
469 case SLAP_ACI_SCOPE_ENTRY:
470 if ( ber_bvcmp( &scope, &aci_bv[ ACI_BV_ENTRY ] ) != 0
471 && ber_bvstrcasecmp( &scope, &aci_bv[ ACI_BV_SUBTREE ] ) != 0 )
477 case SLAP_ACI_SCOPE_CHILDREN:
478 if ( ber_bvcmp( &scope, &aci_bv[ ACI_BV_CHILDREN ] ) != 0
479 && ber_bvstrcasecmp( &scope, &aci_bv[ ACI_BV_SUBTREE ] ) != 0 )
485 case SLAP_ACI_SCOPE_SUBTREE:
486 /* TODO: add assertion? */
490 /* get the list of permissions clauses, bail if empty */
491 if ( acl_get_part( aci, 2, '#', &perms ) <= 0 ) {
496 /* check if any permissions allow desired access */
497 if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
501 /* see if we have a DN match */
502 if ( acl_get_part( aci, 3, '#', &type ) < 0 ) {
507 /* see if we have a public (i.e. anonymous) access */
508 if ( ber_bvcmp( &aci_bv[ ACI_BV_PUBLIC ], &type ) == 0 ) {
512 /* otherwise require an identity */
513 if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
517 /* see if we have a users access */
518 if ( ber_bvcmp( &aci_bv[ ACI_BV_USERS ], &type ) == 0 ) {
522 /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
523 * just grab all the berval up to its end (ITS#3303).
524 * NOTE: the problem could be solved by providing the DN with
525 * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would
526 * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
528 if ( acl_get_part( aci, 4, '#', &sdn ) < 0 ) {
532 sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
533 sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
535 /* get the type options, if any */
536 if ( acl_get_part( &type, 1, '/', &opts ) > 0 ) {
537 opts.bv_len = type.bv_len - ( opts.bv_val - type.bv_val );
538 type.bv_len = opts.bv_val - type.bv_val - 1;
544 if ( ber_bvcmp( &aci_bv[ ACI_BV_ACCESS_ID ], &type ) == 0 ) {
545 return dn_match( &op->o_ndn, &sdn );
547 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_SUBTREE ], &type ) == 0 ) {
548 return dnIsSuffix( &op->o_ndn, &sdn );
550 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_ONELEVEL ], &type ) == 0 ) {
553 dnParent( &sdn, &pdn );
555 return dn_match( &op->o_ndn, &pdn );
557 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_CHILDREN ], &type ) == 0 ) {
558 return ( !dn_match( &op->o_ndn, &sdn ) && dnIsSuffix( &op->o_ndn, &sdn ) );
560 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_SELF ], &type ) == 0 ) {
561 return dn_match( &op->o_ndn, &e->e_nname );
563 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_DNATTR ], &type ) == 0 ) {
565 AttributeDescription *ad = NULL;
568 rc = slap_bv2ad( &sdn, &ad, &text );
569 assert( rc == LDAP_SUCCESS );
572 for ( at = attrs_find( e->e_attrs, ad );
574 at = attrs_find( at->a_next, ad ) )
576 if ( value_find_ex( ad,
577 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
578 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
580 &op->o_ndn, op->o_tmpmemctx ) == 0 )
589 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_GROUP ], &type ) == 0 ) {
593 if ( BER_BVISNULL( &opts ) ) {
594 oc = aci_bv[ ACI_BV_GROUP_CLASS ];
595 at = aci_bv[ ACI_BV_GROUP_ATTR ];
598 if ( acl_get_part( &opts, 0, '/', &oc ) < 0 ) {
602 if ( acl_get_part( &opts, 1, '/', &at ) < 0 ) {
603 at = aci_bv[ ACI_BV_GROUP_ATTR ];
607 if ( aci_group_member( &sdn, &oc, &at, op, e, nmatch, matches ) )
612 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_ROLE ], &type ) == 0 ) {
616 if ( BER_BVISNULL( &opts ) ) {
617 oc = aci_bv[ ACI_BV_ROLE_CLASS ];
618 at = aci_bv[ ACI_BV_ROLE_ATTR ];
621 if ( acl_get_part( &opts, 0, '/', &oc ) < 0 ) {
625 if ( acl_get_part( &opts, 1, '/', &at ) < 0 ) {
626 at = aci_bv[ ACI_BV_ROLE_ATTR ];
630 if ( aci_group_member( &sdn, &oc, &at, op, e, nmatch, matches ) )
635 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_SET ], &type ) == 0 ) {
636 if ( acl_match_set( &sdn, op, e, NULL ) ) {
640 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_SET_REF ], &type ) == 0 ) {
641 if ( acl_match_set( &sdn, op, e, (struct berval *)&aci_bv[ ACI_BV_SET_ATTR ] ) ) {
646 /* it passed normalization! */
656 /* OpenLDAP eXperimental Syntax */
657 static slap_syntax_defs_rec aci_syntax_def = {
658 "( 1.3.6.1.4.1.4203.666.2.1 DESC 'OpenLDAP Experimental ACI' )",
663 static slap_mrule_defs_rec aci_mr_def = {
664 "( 1.3.6.1.4.1.4203.666.4.2 NAME 'OpenLDAPaciMatch' "
665 "SYNTAX 1.3.6.1.4.1.4203.666.2.1 )",
666 SLAP_MR_HIDE | SLAP_MR_EQUALITY, NULL,
667 NULL, OpenLDAPaciNormalize, OpenLDAPaciMatch,
675 AttributeDescription **ad;
677 "OpenLDAPaci", "( 1.3.6.1.4.1.4203.666.1.5 "
678 "NAME 'OpenLDAPaci' "
679 "DESC 'OpenLDAP access control information (experimental)' "
680 "EQUALITY OpenLDAPaciMatch "
681 "SYNTAX 1.3.6.1.4.1.4203.666.2.1 "
682 "USAGE directoryOperation )",
687 LDAPAttributeType *at;
693 rc = register_syntax( &aci_syntax_def );
698 /* ACI equality rule */
699 rc = register_matching_rule( &aci_mr_def );
705 at = ldap_str2attributetype( aci_at.desc,
706 &rc, &text, LDAP_SCHEMA_ALLOW_ALL );
708 Debug( LDAP_DEBUG_ANY,
709 "aci_init: AttributeType \"%s\" parse failed: %s %s\n",
710 aci_at.name, ldap_scherr2str( rc ), text );
714 rc = at_add( at, 0, &sat, &text );
715 if ( rc != LDAP_SUCCESS ) {
716 ldap_attributetype_free( at );
717 Debug( LDAP_DEBUG_ANY,
718 "aci_init: AttributeType \"%s\" load failed: %s %s\n",
719 aci_at.name, scherr2str( rc ), text );
724 rc = slap_str2ad( aci_at.name,
726 if ( rc != LDAP_SUCCESS ) {
727 Debug( LDAP_DEBUG_ANY,
728 "aci_init: unable to find AttributeDescription "
730 aci_at.name, rc, text );
735 sat->sat_flags |= aci_at.flags;
749 AttributeDescription *ad = NULL;
750 const char *text = NULL;
752 if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
753 fprintf( stderr, "%s: line %d: "
754 "inappropriate style \"%s\" in \"aci\" by clause\n",
755 fname, lineno, style_strings[sty] );
759 if ( right != NULL && *right != '\0' ) {
760 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
762 "%s: line %d: aci \"%s\": %s\n",
763 fname, lineno, right, text );
771 if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
772 fprintf( stderr, "%s: line %d: "
773 "aci \"%s\": inappropriate syntax: %s\n",
774 fname, lineno, right,
775 ad->ad_type->sat_syntax_oid );
785 dynacl_aci_unparse( void *priv, struct berval *bv )
787 AttributeDescription *ad = ( AttributeDescription * )priv;
790 assert( ad != NULL );
792 bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
793 ptr = lutil_strcopy( bv->bv_val, " aci=" );
794 ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
795 bv->bv_len = ptr - bv->bv_val;
805 AttributeDescription *desc,
809 slap_access_t *grantp,
810 slap_access_t *denyp )
812 AttributeDescription *ad = ( AttributeDescription * )priv;
814 slap_access_t tgrant, tdeny, grant, deny;
816 char accessmaskbuf[ACCESSMASK_MAXLEN];
817 char accessmaskbuf1[ACCESSMASK_MAXLEN];
818 #endif /* LDAP_DEBUG */
820 if ( BER_BVISEMPTY( &e->e_nname ) ) {
821 /* no ACIs in the root DSE */
825 /* start out with nothing granted, nothing denied */
829 /* get the aci attribute */
830 at = attr_find( e->e_attrs, ad );
834 /* the aci is an multi-valued attribute. The
835 * rights are determined by OR'ing the individual
836 * rights given by the acis.
838 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
839 if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
840 nmatch, matches, &grant, &deny,
841 SLAP_ACI_SCOPE_ENTRY ) != 0 )
848 Debug( LDAP_DEBUG_ACL, " <= aci_mask grant %s deny %s\n",
849 accessmask2str( tgrant, accessmaskbuf, 1 ),
850 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
853 /* If the entry level aci didn't contain anything valid for the
854 * current operation, climb up the tree and evaluate the
855 * acis with scope set to subtree
857 if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
858 struct berval parent_ndn;
860 dnParent( &e->e_nname, &parent_ndn );
861 while ( !BER_BVISEMPTY( &parent_ndn ) ){
863 BerVarray bvals = NULL;
866 /* to solve the chicken'n'egg problem of accessing
867 * the OpenLDAPaci attribute, the direct access
868 * to the entry's attribute is unchecked; however,
869 * further accesses to OpenLDAPaci values in the
870 * ancestors occur through backend_attribute(), i.e.
871 * with the identity of the operation, requiring
872 * further access checking. For uniformity, this
873 * makes further requests occur as the rootdn, if
874 * any, i.e. searching for the OpenLDAPaci attribute
875 * is considered an internal search. If this is not
876 * acceptable, then the same check needs be performed
877 * when accessing the entry's attribute. */
878 struct berval save_o_dn, save_o_ndn;
880 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
881 save_o_dn = op->o_dn;
882 save_o_ndn = op->o_ndn;
884 op->o_dn = op->o_bd->be_rootdn;
885 op->o_ndn = op->o_bd->be_rootndn;
888 Debug( LDAP_DEBUG_ACL, " checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
889 ret = backend_attribute( op, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
891 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
892 op->o_dn = save_o_dn;
893 op->o_ndn = save_o_ndn;
903 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++ ) {
904 if ( aci_mask( op, e, desc, val,
908 SLAP_ACI_SCOPE_CHILDREN ) != 0 )
912 /* evaluation stops as soon as either a "deny" or a
913 * "grant" directive matches.
915 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
919 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
920 accessmask2str( tgrant, accessmaskbuf, 1 ),
921 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
925 case LDAP_NO_SUCH_ATTRIBUTE:
926 /* just go on if the aci-Attribute is not present in
929 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
933 case LDAP_NO_SUCH_OBJECT:
934 /* We have reached the base object */
935 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
947 dnParent( &parent_ndn, &parent_ndn );
957 /* need to register this at some point */
958 static slap_dynacl_t dynacl_aci = {
969 dynacl_aci_init( void )
976 rc = slap_dynacl_register( &dynacl_aci );
983 /* ACI syntax validation */
986 * Matches given berval to array of bervals
988 * >=0 if one if the array elements equals to this berval
989 * -1 if string was not found in array
994 const struct berval *arr[] )
998 if ( BER_BVISEMPTY( bv ) ) {
1002 for ( i = 0; arr[ i ] != NULL ; i++ ) {
1003 if ( ber_bvstrcasecmp( bv, arr[ i ] ) == 0 ) {
1012 /* Returns what have left in input berval after current sub */
1017 struct berval *tail )
1021 tail->bv_val = sub->bv_val + sub->bv_len;
1022 head_len = (unsigned long) tail->bv_val - (unsigned long) val->bv_val;
1023 tail->bv_len = val->bv_len - head_len;
1028 * aci is accepted in following form:
1029 * oid#scope#rights#type#subject
1031 * oid := numeric OID
1032 * scope := entry|children
1033 * rights := right[[$right]...]
1034 * right := (grant|deny);action
1035 * action := perms;attr[[;perms;attr]...]
1036 * perms := perm[[,perm]...]
1038 * attr := attributeType|[all]
1039 * type := public|users|self|dnattr|group|role|set|set-ref|
1040 * access_id|subtree|onelevel|children
1043 OpenLDAPaciValidatePerms(
1044 struct berval *perms )
1048 for ( i = 0; i < perms->bv_len; ) {
1049 switch ( perms->bv_val[ i ] ) {
1059 return LDAP_INVALID_SYNTAX;
1062 if ( ++i == perms->bv_len ) {
1063 return LDAP_SUCCESS;
1066 while ( i < perms->bv_len && perms->bv_val[ i ] == ' ' )
1069 assert( i != perms->bv_len );
1071 if ( perms->bv_val[ i ] != ',' ) {
1072 return LDAP_INVALID_SYNTAX;
1077 } while ( perms->bv_val[ i ] == ' ' );
1080 return LDAP_SUCCESS;
1083 static const struct berval *ACIgrantdeny[] = {
1084 &aci_bv[ ACI_BV_GRANT ],
1085 &aci_bv[ ACI_BV_DENY ],
1090 OpenLDAPaciValidateRight(
1091 struct berval *action )
1093 struct berval bv = BER_BVNULL;
1097 if ( acl_get_part( action, 0, ';', &bv ) < 0 ||
1098 bv_getcaseidx( &bv, ACIgrantdeny ) == -1 )
1100 return LDAP_INVALID_SYNTAX;
1103 for ( i = 0; acl_get_part( action, i + 1, ';', &bv ) >= 0; i++ ) {
1106 if ( OpenLDAPaciValidatePerms( &bv ) != LDAP_SUCCESS )
1108 return LDAP_INVALID_SYNTAX;
1113 AttributeDescription *ad = NULL;
1114 const char *text = NULL;
1116 /* could be "[all]" or an attribute description */
1117 if ( ber_bvstrcasecmp( &bv, &aci_bv[ ACI_BV_BR_ALL ] ) == 0 ) {
1121 if ( slap_bv2ad( &bv, &ad, &text ) != LDAP_SUCCESS ) {
1122 return LDAP_INVALID_SYNTAX;
1127 /* "perms;attr" go in pairs */
1128 if ( i > 0 && ( i & 1 ) == 0 ) {
1129 return LDAP_SUCCESS;
1132 return LDAP_INVALID_SYNTAX;
1135 return LDAP_SUCCESS;
1139 OpenLDAPaciNormalizeRight(
1140 struct berval *action,
1141 struct berval *naction,
1144 struct berval grantdeny,
1151 if ( acl_get_part( action, 0, ';', &grantdeny ) < 0 ) {
1152 return LDAP_INVALID_SYNTAX;
1154 idx = bv_getcaseidx( &grantdeny, ACIgrantdeny );
1156 return LDAP_INVALID_SYNTAX;
1159 ber_dupbv_x( naction, (struct berval *)ACIgrantdeny[ idx ], ctx );
1161 for ( i = 1; acl_get_part( action, i, ';', &bv ) >= 0; i++ ) {
1164 if ( OpenLDAPaciValidatePerms( &bv ) != LDAP_SUCCESS )
1166 return LDAP_INVALID_SYNTAX;
1174 /* could be "[all]" or an attribute description */
1175 if ( ber_bvstrcasecmp( &bv, &aci_bv[ ACI_BV_BR_ALL ] ) == 0 ) {
1176 bv = aci_bv[ ACI_BV_BR_ALL ];
1179 AttributeDescription *ad = NULL;
1180 const char *text = NULL;
1183 rc = slap_bv2ad( &bv, &ad, &text );
1184 if ( rc != LDAP_SUCCESS ) {
1185 return LDAP_INVALID_SYNTAX;
1191 naction->bv_val = ber_memrealloc_x( naction->bv_val,
1192 naction->bv_len + STRLENOF( ";" )
1193 + perms.bv_len + STRLENOF( ";" )
1197 ptr = &naction->bv_val[ naction->bv_len ];
1200 ptr = lutil_strncopy( ptr, perms.bv_val, perms.bv_len );
1203 ptr = lutil_strncopy( ptr, bv.bv_val, bv.bv_len );
1205 naction->bv_len += STRLENOF( ";" ) + perms.bv_len
1206 + STRLENOF( ";" ) + bv.bv_len;
1210 /* perms;attr go in pairs */
1211 if ( i > 1 && ( i & 1 ) ) {
1212 return LDAP_SUCCESS;
1215 return LDAP_INVALID_SYNTAX;
1220 OpenLDAPaciValidateRights(
1221 struct berval *actions )
1224 struct berval bv = BER_BVNULL;
1227 for ( i = 0; acl_get_part( actions, i, '$', &bv ) >= 0; i++ ) {
1228 if ( OpenLDAPaciValidateRight( &bv ) != LDAP_SUCCESS ) {
1229 return LDAP_INVALID_SYNTAX;
1233 return LDAP_SUCCESS;
1237 OpenLDAPaciNormalizeRights(
1238 struct berval *actions,
1239 struct berval *nactions,
1243 struct berval bv = BER_BVNULL;
1246 BER_BVZERO( nactions );
1247 for ( i = 0; acl_get_part( actions, i, '$', &bv ) >= 0; i++ ) {
1251 rc = OpenLDAPaciNormalizeRight( &bv, &nbv, ctx );
1252 if ( rc != LDAP_SUCCESS ) {
1253 ber_memfree_x( nactions->bv_val, ctx );
1254 BER_BVZERO( nactions );
1255 return LDAP_INVALID_SYNTAX;
1262 nactions->bv_val = ber_memrealloc_x( nactions->bv_val,
1263 nactions->bv_len + STRLENOF( "$" )
1266 nactions->bv_val[ nactions->bv_len ] = '$';
1267 AC_MEMCPY( &nactions->bv_val[ nactions->bv_len + 1 ],
1268 nbv.bv_val, nbv.bv_len + 1 );
1269 ber_memfree_x( nbv.bv_val, ctx );
1270 nactions->bv_len += STRLENOF( "$" ) + nbv.bv_len;
1275 return LDAP_SUCCESS;
1278 static const struct berval *OpenLDAPaciscopes[] = {
1279 &aci_bv[ ACI_BV_ENTRY ],
1280 &aci_bv[ ACI_BV_CHILDREN ],
1281 &aci_bv[ ACI_BV_SUBTREE ],
1286 static const struct berval *OpenLDAPacitypes[] = {
1288 &aci_bv[ ACI_BV_GROUP ],
1289 &aci_bv[ ACI_BV_ROLE ],
1291 /* set to one past the last DN-valued type with options (/) */
1292 #define LAST_OPTIONAL 2
1294 &aci_bv[ ACI_BV_ACCESS_ID ],
1295 &aci_bv[ ACI_BV_SUBTREE ],
1296 &aci_bv[ ACI_BV_ONELEVEL ],
1297 &aci_bv[ ACI_BV_CHILDREN ],
1299 /* set to one past the last DN-valued type */
1300 #define LAST_DNVALUED 6
1303 &aci_bv[ ACI_BV_DNATTR ],
1304 &aci_bv[ ACI_BV_PUBLIC ],
1305 &aci_bv[ ACI_BV_USERS ],
1306 &aci_bv[ ACI_BV_SELF ],
1307 &aci_bv[ ACI_BV_SET ],
1308 &aci_bv[ ACI_BV_SET_REF ],
1314 OpenLDAPaciValidate(
1316 struct berval *val )
1318 struct berval oid = BER_BVNULL,
1320 rights = BER_BVNULL,
1322 subject = BER_BVNULL;
1325 if ( BER_BVISEMPTY( val ) ) {
1326 return LDAP_INVALID_SYNTAX;
1330 if ( acl_get_part( val, 0, '#', &oid ) < 0 ||
1331 numericoidValidate( NULL, &oid ) != LDAP_SUCCESS )
1333 /* NOTE: the numericoidValidate() is rather pedantic;
1334 * I'd replace it with X-ORDERED VALUES so that
1335 * it's guaranteed values are maintained and used
1336 * in the desired order */
1337 return LDAP_INVALID_SYNTAX;
1341 if ( acl_get_part( val, 1, '#', &scope ) < 0 ||
1342 bv_getcaseidx( &scope, OpenLDAPaciscopes ) == -1 )
1344 return LDAP_INVALID_SYNTAX;
1348 if ( acl_get_part( val, 2, '#', &rights ) < 0 ||
1349 OpenLDAPaciValidateRights( &rights ) != LDAP_SUCCESS )
1351 return LDAP_INVALID_SYNTAX;
1355 if ( acl_get_part( val, 3, '#', &type ) < 0 ) {
1356 return LDAP_INVALID_SYNTAX;
1358 idx = bv_getcaseidx( &type, OpenLDAPacitypes );
1362 if ( acl_get_part( &type, 0, '/', &isgr ) < 0 ) {
1363 return LDAP_INVALID_SYNTAX;
1366 idx = bv_getcaseidx( &isgr, OpenLDAPacitypes );
1367 if ( idx == -1 || idx >= LAST_OPTIONAL ) {
1368 return LDAP_INVALID_SYNTAX;
1373 bv_get_tail( val, &type, &subject );
1374 if ( subject.bv_val[ 0 ] != '#' ) {
1375 return LDAP_INVALID_SYNTAX;
1378 if ( idx >= LAST_DNVALUED ) {
1379 if ( OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_DNATTR ] ) {
1380 AttributeDescription *ad = NULL;
1381 const char *text = NULL;
1384 rc = slap_bv2ad( &subject, &ad, &text );
1385 if ( rc != LDAP_SUCCESS ) {
1386 return LDAP_INVALID_SYNTAX;
1389 if ( ad->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
1390 /* FIXME: allow nameAndOptionalUID? */
1391 return LDAP_INVALID_SYNTAX;
1396 return LDAP_SUCCESS;
1398 } else if ( OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_GROUP ]
1399 || OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_ROLE ] )
1401 /* do {group|role}/oc/at check */
1402 struct berval ocbv = BER_BVNULL,
1405 ocbv.bv_val = ber_bvchr( &type, '/' );
1406 if ( ocbv.bv_val != NULL ) {
1408 ocbv.bv_len = type.bv_len
1409 - ( ocbv.bv_val - type.bv_val );
1411 atbv.bv_val = ber_bvchr( &ocbv, '/' );
1412 if ( atbv.bv_val != NULL ) {
1413 AttributeDescription *ad = NULL;
1414 const char *text = NULL;
1418 atbv.bv_len = type.bv_len
1419 - ( atbv.bv_val - type.bv_val );
1420 ocbv.bv_len = atbv.bv_val - ocbv.bv_val - 1;
1422 rc = slap_bv2ad( &atbv, &ad, &text );
1423 if ( rc != LDAP_SUCCESS ) {
1424 return LDAP_INVALID_SYNTAX;
1428 if ( oc_bvfind( &ocbv ) == NULL ) {
1429 return LDAP_INVALID_SYNTAX;
1434 if ( BER_BVISEMPTY( &subject ) ) {
1435 /* empty DN invalid */
1436 return LDAP_INVALID_SYNTAX;
1442 /* FIXME: pass DN syntax? */
1443 return dnValidate( NULL, &subject );
1447 OpenLDAPaciPrettyNormal(
1453 struct berval oid = BER_BVNULL,
1455 rights = BER_BVNULL,
1456 nrights = BER_BVNULL,
1459 subject = BER_BVNULL,
1460 nsubject = BER_BVNULL;
1467 if ( BER_BVISEMPTY( val ) ) {
1468 return LDAP_INVALID_SYNTAX;
1471 /* oid: if valid, it's already normalized */
1472 if ( acl_get_part( val, 0, '#', &oid ) < 0 ||
1473 numericoidValidate( NULL, &oid ) != LDAP_SUCCESS )
1475 return LDAP_INVALID_SYNTAX;
1478 /* scope: normalize by replacing with OpenLDAPaciscopes */
1479 if ( acl_get_part( val, 1, '#', &scope ) < 0 ) {
1480 return LDAP_INVALID_SYNTAX;
1482 idx = bv_getcaseidx( &scope, OpenLDAPaciscopes );
1484 return LDAP_INVALID_SYNTAX;
1486 scope = *OpenLDAPaciscopes[ idx ];
1489 if ( acl_get_part( val, 2, '#', &rights ) < 0 ) {
1490 return LDAP_INVALID_SYNTAX;
1492 if ( OpenLDAPaciNormalizeRights( &rights, &nrights, ctx )
1495 return LDAP_INVALID_SYNTAX;
1499 if ( acl_get_part( val, 3, '#', &type ) < 0 ) {
1500 rc = LDAP_INVALID_SYNTAX;
1503 idx = bv_getcaseidx( &type, OpenLDAPacitypes );
1507 if ( acl_get_part( &type, 0, '/', &isgr ) < 0 ) {
1508 rc = LDAP_INVALID_SYNTAX;
1512 idx = bv_getcaseidx( &isgr, OpenLDAPacitypes );
1513 if ( idx == -1 || idx >= LAST_OPTIONAL ) {
1514 rc = LDAP_INVALID_SYNTAX;
1518 ntype = *OpenLDAPacitypes[ idx ];
1521 bv_get_tail( val, &type, &subject );
1523 if ( BER_BVISEMPTY( &subject ) || subject.bv_val[ 0 ] != '#' ) {
1524 rc = LDAP_INVALID_SYNTAX;
1531 if ( idx < LAST_DNVALUED ) {
1532 /* FIXME: pass DN syntax? */
1534 rc = dnNormalize( 0, NULL, NULL,
1535 &subject, &nsubject, ctx );
1537 rc = dnPretty( NULL, &subject, &nsubject, ctx );
1540 if ( rc == LDAP_SUCCESS ) {
1547 if ( OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_GROUP ]
1548 || OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_ROLE ] )
1550 /* do {group|role}/oc/at check */
1551 struct berval ocbv = BER_BVNULL,
1554 ocbv.bv_val = ber_bvchr( &type, '/' );
1555 if ( ocbv.bv_val != NULL ) {
1556 ObjectClass *oc = NULL;
1557 AttributeDescription *ad = NULL;
1558 const char *text = NULL;
1562 bv.bv_len = ntype.bv_len;
1565 ocbv.bv_len = type.bv_len - ( ocbv.bv_val - type.bv_val );
1567 atbv.bv_val = ber_bvchr( &ocbv, '/' );
1568 if ( atbv.bv_val != NULL ) {
1570 atbv.bv_len = type.bv_len
1571 - ( atbv.bv_val - type.bv_val );
1572 ocbv.bv_len = atbv.bv_val - ocbv.bv_val - 1;
1574 rc = slap_bv2ad( &atbv, &ad, &text );
1575 if ( rc != LDAP_SUCCESS ) {
1576 rc = LDAP_INVALID_SYNTAX;
1580 bv.bv_len += STRLENOF( "/" ) + ad->ad_cname.bv_len;
1583 oc = oc_bvfind( &ocbv );
1585 rc = LDAP_INVALID_SYNTAX;
1589 bv.bv_len += STRLENOF( "/" ) + oc->soc_cname.bv_len;
1590 bv.bv_val = ber_memalloc_x( bv.bv_len + 1, ctx );
1593 ptr = lutil_strncopy( ptr, ntype.bv_val, ntype.bv_len );
1596 ptr = lutil_strncopy( ptr,
1597 oc->soc_cname.bv_val,
1598 oc->soc_cname.bv_len );
1602 ptr = lutil_strncopy( ptr,
1603 ad->ad_cname.bv_val,
1604 ad->ad_cname.bv_len );
1613 } else if ( OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_DNATTR ] ) {
1614 AttributeDescription *ad = NULL;
1615 const char *text = NULL;
1618 rc = slap_bv2ad( &subject, &ad, &text );
1619 if ( rc != LDAP_SUCCESS ) {
1620 rc = LDAP_INVALID_SYNTAX;
1624 if ( ad->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
1625 /* FIXME: allow nameAndOptionalUID? */
1626 rc = LDAP_INVALID_SYNTAX;
1630 nsubject = ad->ad_cname;
1635 oid.bv_len + STRLENOF( "#" )
1636 + scope.bv_len + STRLENOF( "#" )
1637 + rights.bv_len + STRLENOF( "#" )
1638 + ntype.bv_len + STRLENOF( "#" )
1641 out->bv_val = ber_memalloc_x( out->bv_len + 1, ctx );
1642 ptr = lutil_strncopy( out->bv_val, oid.bv_val, oid.bv_len );
1645 ptr = lutil_strncopy( ptr, scope.bv_val, scope.bv_len );
1648 ptr = lutil_strncopy( ptr, nrights.bv_val, nrights.bv_len );
1651 ptr = lutil_strncopy( ptr, ntype.bv_val, ntype.bv_len );
1654 if ( !BER_BVISNULL( &nsubject ) ) {
1655 ptr = lutil_strncopy( ptr, nsubject.bv_val, nsubject.bv_len );
1660 if ( freesubject ) {
1661 ber_memfree_x( nsubject.bv_val, ctx );
1665 ber_memfree_x( ntype.bv_val, ctx );
1668 if ( !BER_BVISNULL( &nrights ) ) {
1669 ber_memfree_x( nrights.bv_val, ctx );
1682 return OpenLDAPaciPrettyNormal( val, out, ctx, 0 );
1686 OpenLDAPaciNormalize(
1694 return OpenLDAPaciPrettyNormal( val, out, ctx, 1 );
1697 #if SLAPD_ACI_ENABLED == SLAPD_MOD_DYNAMIC
1699 * FIXME: need config and Makefile.am code to ease building
1703 init_module( int argc, char *argv[] )
1705 return dynacl_aci_init();
1707 #endif /* SLAPD_ACI_ENABLED == SLAPD_MOD_DYNAMIC */
1709 #endif /* SLAPD_ACI_ENABLED */