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-2005 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 #define ACI_BUF_SIZE 1024 /* use most appropriate size */
77 static const struct berval aci_bv[] = {
104 BER_BVC(SLAPD_GROUP_CLASS),
105 BER_BVC(SLAPD_GROUP_ATTR),
106 BER_BVC(SLAPD_ROLE_CLASS),
107 BER_BVC(SLAPD_ROLE_ATTR),
109 BER_BVC(SLAPD_ACI_SET_ATTR),
116 #endif /* SLAP_DYNACL */
117 AttributeDescription *slap_ad_aci;
122 struct berval *val );
132 OpenLDAPaciNormalize(
140 #define OpenLDAPaciMatch octetStringMatch
144 struct berval *list )
151 for ( i = 0; acl_get_part( list, i, ',', &bv ) >= 0; i++ ) {
152 if ( bv.bv_len <= 0 ) {
156 switch ( *bv.bv_val ) {
158 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt does not
159 * define any equivalent to the AUTH right, so I've just used
162 ACL_PRIV_SET(mask, ACL_PRIV_AUTH);
165 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
166 * the right 'd' to mean "delete"; we hijack it to mean
167 * "disclose" for consistency wuith the rest of slapd.
169 ACL_PRIV_SET(mask, ACL_PRIV_DISCLOSE);
172 ACL_PRIV_SET(mask, ACL_PRIV_COMPARE);
175 /* **** NOTE: draft-ietf-ldapext-aci-model-0.3.txt defines
176 * the right 's' to mean "set", but in the examples states
177 * that the right 's' means "search". The latter definition
180 ACL_PRIV_SET(mask, ACL_PRIV_SEARCH);
183 ACL_PRIV_SET(mask, ACL_PRIV_READ);
186 ACL_PRIV_SET(mask, ACL_PRIV_WRITE);
200 const struct berval *attr,
203 struct berval bv, left, right;
206 for ( i = 0; acl_get_part( list, i, ',', &bv ) >= 0; i++ ) {
207 if ( acl_get_part(&bv, 0, '=', &left ) < 0
208 || acl_get_part( &bv, 1, '=', &right ) < 0 )
210 if ( ber_bvstrcasecmp( attr, &bv ) == 0 ) {
214 } else if ( val == NULL ) {
215 if ( ber_bvstrcasecmp( attr, &left ) == 0 ) {
220 if ( ber_bvstrcasecmp( attr, &left ) == 0 ) {
221 /* FIXME: this is also totally undocumented! */
222 /* this is experimental code that implements a
223 * simple (prefix) match of the attribute value.
224 * the ACI draft does not provide for aci's that
225 * apply to specific values, but it would be
226 * nice to have. If the <attr> part of an aci's
227 * rights list is of the form <attr>=<value>,
228 * that means the aci applies only to attrs with
229 * the given value. Furthermore, if the attr is
230 * of the form <attr>=<value>*, then <value> is
231 * treated as a prefix, and the aci applies to
232 * any value with that prefix.
234 * Ideally, this would allow r.e. matches.
236 if ( acl_get_part( &right, 0, '*', &left ) < 0
237 || right.bv_len <= left.bv_len )
239 if ( ber_bvstrcasecmp( val, &right ) == 0 ) {
243 } else if ( val->bv_len >= left.bv_len ) {
244 if ( strncasecmp( val->bv_val, left.bv_val, left.bv_len ) == 0 ) {
256 aci_list_get_attr_rights(
258 const struct berval *attr,
265 /* loop through each rights/attr pair, skip first part (action) */
267 for ( i = 1; acl_get_part( list, i + 1, ';', &bv ) >= 0; i += 2 ) {
268 if ( aci_list_has_attr( &bv, attr, val ) == 0 ) {
272 if ( acl_get_part( list, i, ';', &bv ) < 0 ) {
276 mask |= aci_list_map_rights( &bv );
285 const struct berval *attr,
287 slap_access_t *grant,
288 slap_access_t *deny )
290 struct berval perm, actn;
294 if ( attr == NULL || BER_BVISEMPTY( attr )
295 || ber_bvstrcasecmp( attr, &aci_bv[ ACI_BV_ENTRY ] ) == 0 )
297 attr = &aci_bv[ ACI_BV_BR_ENTRY ];
303 /* loop through each permissions clause */
304 for ( i = 0; acl_get_part( list, i, '$', &perm ) >= 0; i++ ) {
305 if ( acl_get_part( &perm, 0, ';', &actn ) < 0 ) {
309 if ( ber_bvstrcasecmp( &aci_bv[ ACI_BV_GRANT ], &actn ) == 0 ) {
312 } else if ( ber_bvstrcasecmp( &aci_bv[ ACI_BV_DENY ], &actn ) == 0 ) {
320 *mask |= aci_list_get_attr_rights( &perm, attr, val );
321 *mask |= aci_list_get_attr_rights( &perm, &aci_bv[ ACI_BV_BR_ALL ], NULL );
330 const struct berval *defgrpoc,
331 const struct berval *defgrpat,
338 struct berval subjdn;
341 ObjectClass *grp_oc = NULL;
342 AttributeDescription *grp_ad = NULL;
346 /* format of string is "{group|role}/objectClassValue/groupAttrName" */
347 if ( acl_get_part( subj, 0, '/', &subjdn ) < 0 ) {
351 if ( acl_get_part( subj, 1, '/', &grpoc ) < 0 ) {
355 if ( acl_get_part( subj, 2, '/', &grpat ) < 0 ) {
359 rc = slap_bv2ad( &grpat, &grp_ad, &text );
360 if ( rc != LDAP_SUCCESS ) {
366 grp_oc = oc_bvfind( &grpoc );
368 if ( grp_oc != NULL && grp_ad != NULL ) {
369 char buf[ ACI_BUF_SIZE ];
370 struct berval bv, ndn;
372 bv.bv_len = sizeof( buf ) - 1;
373 bv.bv_val = (char *)&buf;
374 if ( acl_string_expand( &bv, &subjdn,
375 e->e_ndn, nmatch, matches ) )
381 if ( dnNormalize( 0, NULL, NULL, &bv, &ndn, op->o_tmpmemctx ) == LDAP_SUCCESS )
383 rc = ( backend_group( op, e, &ndn, &op->o_ndn,
384 grp_oc, grp_ad ) == 0 );
385 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
397 AttributeDescription *desc,
402 slap_access_t *grant,
404 slap_aci_scope_t asserted_scope )
415 assert( !BER_BVISNULL( &desc->ad_cname ) );
417 /* parse an aci of the form:
418 oid # scope # action;rights;attr;rights;attr
419 $ action;rights;attr;rights;attr # type # subject
421 [NOTE: the following comment is very outdated,
422 as the draft version it refers to (Ando, 2004-11-20)].
424 See draft-ietf-ldapext-aci-model-04.txt section 9.1 for
425 a full description of the format for this attribute.
426 Differences: "this" in the draft is "self" here, and
427 "self" and "public" is in the position of type.
429 <scope> = {entry|children|subtree}
430 <type> = {public|users|access-id|subtree|onelevel|children|
431 self|dnattr|group|role|set|set-ref}
433 This routine now supports scope={ENTRY,CHILDREN}
435 - ENTRY applies to "entry" and "subtree";
436 - CHILDREN aplies to "children" and "subtree"
439 /* check that the aci has all 5 components */
440 if ( acl_get_part( aci, 4, '#', NULL ) < 0 ) {
444 /* check that the aci family is supported */
445 /* FIXME: the OID is ignored? */
446 if ( acl_get_part( aci, 0, '#', &bv ) < 0 ) {
450 /* check that the scope matches */
451 if ( acl_get_part( aci, 1, '#', &scope ) < 0 ) {
455 /* note: scope can be either ENTRY or CHILDREN;
456 * they respectively match "entry" and "children" in bv
457 * both match "subtree" */
458 switch ( asserted_scope ) {
459 case SLAP_ACI_SCOPE_ENTRY:
460 if ( ber_bvcmp( &scope, &aci_bv[ ACI_BV_ENTRY ] ) != 0
461 && ber_bvstrcasecmp( &scope, &aci_bv[ ACI_BV_SUBTREE ] ) != 0 )
467 case SLAP_ACI_SCOPE_CHILDREN:
468 if ( ber_bvcmp( &scope, &aci_bv[ ACI_BV_CHILDREN ] ) != 0
469 && ber_bvstrcasecmp( &scope, &aci_bv[ ACI_BV_SUBTREE ] ) != 0 )
475 case SLAP_ACI_SCOPE_SUBTREE:
476 /* TODO: add assertion? */
480 /* get the list of permissions clauses, bail if empty */
481 if ( acl_get_part( aci, 2, '#', &perms ) <= 0 ) {
486 /* check if any permissions allow desired access */
487 if ( aci_list_get_rights( &perms, &desc->ad_cname, val, grant, deny ) == 0 ) {
491 /* see if we have a DN match */
492 if ( acl_get_part( aci, 3, '#', &type ) < 0 ) {
497 /* see if we have a public (i.e. anonymous) access */
498 if ( ber_bvcmp( &aci_bv[ ACI_BV_PUBLIC ], &type ) == 0 ) {
502 /* otherwise require an identity */
503 if ( BER_BVISNULL( &op->o_ndn ) || BER_BVISEMPTY( &op->o_ndn ) ) {
507 /* see if we have a users access */
508 if ( ber_bvcmp( &aci_bv[ ACI_BV_USERS ], &type ) == 0 ) {
512 /* NOTE: this may fail if a DN contains a valid '#' (unescaped);
513 * just grab all the berval up to its end (ITS#3303).
514 * NOTE: the problem could be solved by providing the DN with
515 * the embedded '#' encoded as hexpairs: "cn=Foo#Bar" would
516 * become "cn=Foo\23Bar" and be safely used by aci_mask(). */
518 if ( acl_get_part( aci, 4, '#', &sdn ) < 0 ) {
522 sdn.bv_val = type.bv_val + type.bv_len + STRLENOF( "#" );
523 sdn.bv_len = aci->bv_len - ( sdn.bv_val - aci->bv_val );
525 /* get the type options, if any */
526 if ( acl_get_part( &type, 1, '/', &opts ) > 0 ) {
527 opts.bv_len = type.bv_len - ( opts.bv_val - type.bv_val );
528 type.bv_len = opts.bv_val - type.bv_val - 1;
534 if ( ber_bvcmp( &aci_bv[ ACI_BV_ACCESS_ID ], &type ) == 0 ) {
535 return dn_match( &op->o_ndn, &sdn );
537 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_SUBTREE ], &type ) == 0 ) {
538 return dnIsSuffix( &op->o_ndn, &sdn );
540 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_ONELEVEL ], &type ) == 0 ) {
543 dnParent( &sdn, &pdn );
545 return dn_match( &op->o_ndn, &pdn );
547 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_CHILDREN ], &type ) == 0 ) {
548 return ( !dn_match( &op->o_ndn, &sdn ) && dnIsSuffix( &op->o_ndn, &sdn ) );
550 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_SELF ], &type ) == 0 ) {
551 return dn_match( &op->o_ndn, &e->e_nname );
553 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_DNATTR ], &type ) == 0 ) {
555 AttributeDescription *ad = NULL;
558 rc = slap_bv2ad( &sdn, &ad, &text );
559 assert( rc == LDAP_SUCCESS );
562 for ( at = attrs_find( e->e_attrs, ad );
564 at = attrs_find( at->a_next, ad ) )
566 if ( value_find_ex( ad,
567 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
568 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
570 &op->o_ndn, op->o_tmpmemctx ) == 0 )
579 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_GROUP ], &type ) == 0 ) {
583 if ( BER_BVISNULL( &opts ) ) {
584 oc = aci_bv[ ACI_BV_GROUP_CLASS ];
585 at = aci_bv[ ACI_BV_GROUP_ATTR ];
588 if ( acl_get_part( &opts, 0, '/', &oc ) < 0 ) {
592 if ( acl_get_part( &opts, 1, '/', &at ) < 0 ) {
593 at = aci_bv[ ACI_BV_GROUP_ATTR ];
597 if ( aci_group_member( &sdn, &oc, &at, op, e, nmatch, matches ) )
602 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_ROLE ], &type ) == 0 ) {
606 if ( BER_BVISNULL( &opts ) ) {
607 oc = aci_bv[ ACI_BV_ROLE_CLASS ];
608 at = aci_bv[ ACI_BV_ROLE_ATTR ];
611 if ( acl_get_part( &opts, 0, '/', &oc ) < 0 ) {
615 if ( acl_get_part( &opts, 1, '/', &at ) < 0 ) {
616 at = aci_bv[ ACI_BV_ROLE_ATTR ];
620 if ( aci_group_member( &sdn, &oc, &at, op, e, nmatch, matches ) )
625 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_SET ], &type ) == 0 ) {
626 if ( acl_match_set( &sdn, op, e, NULL ) ) {
630 } else if ( ber_bvcmp( &aci_bv[ ACI_BV_SET_REF ], &type ) == 0 ) {
631 if ( acl_match_set( &sdn, op, e, (struct berval *)&aci_bv[ ACI_BV_SET_ATTR ] ) ) {
636 /* it passed normalization! */
646 /* OpenLDAP eXperimental Syntax */
647 static slap_syntax_defs_rec aci_syntax_def = {
648 "( 1.3.6.1.4.1.4203.666.2.1 DESC 'OpenLDAP Experimental ACI' )",
653 static slap_mrule_defs_rec aci_mr_def = {
654 "( 1.3.6.1.4.1.4203.666.4.2 NAME 'OpenLDAPaciMatch' "
655 "SYNTAX 1.3.6.1.4.1.4203.666.2.1 )",
656 SLAP_MR_HIDE | SLAP_MR_EQUALITY, NULL,
657 NULL, OpenLDAPaciNormalize, OpenLDAPaciMatch,
665 AttributeDescription **ad;
667 "OpenLDAPaci", "( 1.3.6.1.4.1.4203.666.1.5 "
668 "NAME 'OpenLDAPaci' "
669 "DESC 'OpenLDAP access control information (experimental)' "
670 "EQUALITY OpenLDAPaciMatch "
671 "SYNTAX 1.3.6.1.4.1.4203.666.2.1 "
672 "USAGE directoryOperation )",
677 LDAPAttributeType *at;
683 rc = register_syntax( &aci_syntax_def );
688 /* ACI equality rule */
689 rc = register_matching_rule( &aci_mr_def );
695 at = ldap_str2attributetype( aci_at.desc,
696 &rc, &text, LDAP_SCHEMA_ALLOW_ALL );
698 Debug( LDAP_DEBUG_ANY,
699 "%s AttributeType load failed: %s %s\n",
700 aci_at.name, ldap_scherr2str( rc ), text );
704 rc = at_add( at, 0, &sat, &text );
705 if ( rc != LDAP_SUCCESS ) {
706 ldap_attributetype_free( at );
707 fprintf( stderr, "iMUX_monitor_schema_init: "
708 "AttributeType load failed: %s %s\n",
709 scherr2str( rc ), text );
714 rc = slap_str2ad( aci_at.name,
716 if ( rc != LDAP_SUCCESS ) {
717 Debug( LDAP_DEBUG_ANY,
718 "unable to find AttributeDescription "
720 aci_at.name, rc, text );
725 sat->sat_flags |= aci_at.flags;
740 AttributeDescription *ad = NULL;
741 const char *text = NULL;
743 if ( sty != ACL_STYLE_REGEX && sty != ACL_STYLE_BASE ) {
744 fprintf( stderr, "%s: line %d: "
745 "inappropriate style \"%s\" in \"aci\" by clause\n",
746 fname, lineno, style_strings[sty] );
750 if ( right != NULL && *right != '\0' ) {
751 if ( slap_str2ad( right, &ad, &text ) != LDAP_SUCCESS ) {
753 "%s: line %d: aci \"%s\": %s\n",
754 fname, lineno, right, text );
762 if ( !is_at_syntax( ad->ad_type, SLAPD_ACI_SYNTAX) ) {
763 fprintf( stderr, "%s: line %d: "
764 "aci \"%s\": inappropriate syntax: %s\n",
765 fname, lineno, right,
766 ad->ad_type->sat_syntax_oid );
776 dynacl_aci_unparse( void *priv, struct berval *bv )
778 AttributeDescription *ad = ( AttributeDescription * )priv;
781 assert( ad != NULL );
783 bv->bv_val = ch_malloc( STRLENOF(" aci=") + ad->ad_cname.bv_len + 1 );
784 ptr = lutil_strcopy( bv->bv_val, " aci=" );
785 ptr = lutil_strcopy( ptr, ad->ad_cname.bv_val );
786 bv->bv_len = ptr - bv->bv_val;
796 AttributeDescription *desc,
800 slap_access_t *grantp,
801 slap_access_t *denyp )
803 AttributeDescription *ad = ( AttributeDescription * )priv;
805 slap_access_t tgrant, tdeny, grant, deny;
807 char accessmaskbuf[ACCESSMASK_MAXLEN];
808 char accessmaskbuf1[ACCESSMASK_MAXLEN];
809 #endif /* LDAP_DEBUG */
811 if ( BER_BVISEMPTY( &e->e_nname ) ) {
812 /* no ACIs in the root DSE */
816 /* start out with nothing granted, nothing denied */
820 /* get the aci attribute */
821 at = attr_find( e->e_attrs, ad );
825 /* the aci is an multi-valued attribute. The
826 * rights are determined by OR'ing the individual
827 * rights given by the acis.
829 for ( i = 0; !BER_BVISNULL( &at->a_nvals[i] ); i++ ) {
830 if ( aci_mask( op, e, desc, val, &at->a_nvals[i],
831 nmatch, matches, &grant, &deny,
832 SLAP_ACI_SCOPE_ENTRY ) != 0 )
839 Debug( LDAP_DEBUG_ACL, " <= aci_mask grant %s deny %s\n",
840 accessmask2str( tgrant, accessmaskbuf, 1 ),
841 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
844 /* If the entry level aci didn't contain anything valid for the
845 * current operation, climb up the tree and evaluate the
846 * acis with scope set to subtree
848 if ( tgrant == ACL_PRIV_NONE && tdeny == ACL_PRIV_NONE ) {
849 struct berval parent_ndn;
851 dnParent( &e->e_nname, &parent_ndn );
852 while ( !BER_BVISEMPTY( &parent_ndn ) ){
854 BerVarray bvals = NULL;
857 /* to solve the chicken'n'egg problem of accessing
858 * the OpenLDAPaci attribute, the direct access
859 * to the entry's attribute is unchecked; however,
860 * further accesses to OpenLDAPaci values in the
861 * ancestors occur through backend_attribute(), i.e.
862 * with the identity of the operation, requiring
863 * further access checking. For uniformity, this
864 * makes further requests occur as the rootdn, if
865 * any, i.e. searching for the OpenLDAPaci attribute
866 * is considered an internal search. If this is not
867 * acceptable, then the same check needs be performed
868 * when accessing the entry's attribute. */
869 struct berval save_o_dn, save_o_ndn;
871 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
872 save_o_dn = op->o_dn;
873 save_o_ndn = op->o_ndn;
875 op->o_dn = op->o_bd->be_rootdn;
876 op->o_ndn = op->o_bd->be_rootndn;
879 Debug( LDAP_DEBUG_ACL, " checking ACI of \"%s\"\n", parent_ndn.bv_val, 0, 0 );
880 ret = backend_attribute( op, NULL, &parent_ndn, ad, &bvals, ACL_AUTH );
882 if ( !BER_BVISNULL( &op->o_bd->be_rootndn ) ) {
883 op->o_dn = save_o_dn;
884 op->o_ndn = save_o_ndn;
894 for ( i = 0; !BER_BVISNULL( &bvals[i] ); i++ ) {
895 if ( aci_mask( op, e, desc, val,
899 SLAP_ACI_SCOPE_CHILDREN ) != 0 )
903 /* evaluation stops as soon as either a "deny" or a
904 * "grant" directive matches.
906 if ( tgrant != ACL_PRIV_NONE || tdeny != ACL_PRIV_NONE ) {
910 Debug( LDAP_DEBUG_ACL, "<= aci_mask grant %s deny %s\n",
911 accessmask2str( tgrant, accessmaskbuf, 1 ),
912 accessmask2str( tdeny, accessmaskbuf1, 1 ), 0 );
916 case LDAP_NO_SUCH_ATTRIBUTE:
917 /* just go on if the aci-Attribute is not present in
920 Debug( LDAP_DEBUG_ACL, "no such attribute\n", 0, 0, 0 );
924 case LDAP_NO_SUCH_OBJECT:
925 /* We have reached the base object */
926 Debug( LDAP_DEBUG_ACL, "no such object\n", 0, 0, 0 );
938 dnParent( &parent_ndn, &parent_ndn );
948 /* need to register this at some point */
949 static slap_dynacl_t dynacl_aci = {
960 dynacl_aci_init( void )
967 rc = slap_dynacl_register( &dynacl_aci );
973 #endif /* SLAP_DYNACL */
975 /* ACI syntax validation */
978 * Matches given berval to array of bervals
980 * >=0 if one if the array elements equals to this berval
981 * -1 if string was not found in array
986 const struct berval *arr[] )
990 if ( BER_BVISEMPTY( bv ) ) {
994 for ( i = 0; arr[ i ] != NULL ; i++ ) {
995 if ( ber_bvstrcasecmp( bv, arr[ i ] ) == 0 ) {
1004 /* Returns what have left in input berval after current sub */
1009 struct berval *tail )
1013 tail->bv_val = sub->bv_val + sub->bv_len;
1014 head_len = (unsigned long) tail->bv_val - (unsigned long) val->bv_val;
1015 tail->bv_len = val->bv_len - head_len;
1020 * aci is accepted in following form:
1021 * oid#scope#rights#type#subject
1023 * oid := numeric OID
1024 * scope := entry|children
1025 * rights := right[[$right]...]
1026 * right := (grant|deny);action
1027 * action := perms;attr[[;perms;attr]...]
1028 * perms := perm[[,perm]...]
1030 * attr := attributeType|[all]
1031 * type := public|users|self|dnattr|group|role|set|set-ref|
1032 * access_id|subtree|onelevel|children
1035 OpenLDAPaciValidatePerms(
1036 struct berval *perms )
1040 for ( i = 0; i < perms->bv_len; ) {
1041 switch ( perms->bv_val[ i ] ) {
1051 return LDAP_INVALID_SYNTAX;
1054 if ( ++i == perms->bv_len ) {
1055 return LDAP_SUCCESS;
1058 while ( i < perms->bv_len && perms->bv_val[ i ] == ' ' )
1061 assert( i != perms->bv_len );
1063 if ( perms->bv_val[ i ] != ',' ) {
1064 return LDAP_INVALID_SYNTAX;
1069 } while ( perms->bv_val[ i ] == ' ' );
1072 return LDAP_SUCCESS;
1075 static const struct berval *ACIgrantdeny[] = {
1076 &aci_bv[ ACI_BV_GRANT ],
1077 &aci_bv[ ACI_BV_DENY ],
1082 OpenLDAPaciValidateRight(
1083 struct berval *action )
1085 struct berval bv = BER_BVNULL;
1089 if ( acl_get_part( action, 0, ';', &bv ) < 0 ||
1090 bv_getcaseidx( &bv, ACIgrantdeny ) == -1 )
1092 return LDAP_INVALID_SYNTAX;
1095 for ( i = 0; acl_get_part( action, i + 1, ';', &bv ) >= 0; i++ ) {
1098 if ( OpenLDAPaciValidatePerms( &bv ) != LDAP_SUCCESS )
1100 return LDAP_INVALID_SYNTAX;
1105 AttributeDescription *ad = NULL;
1106 const char *text = NULL;
1108 /* could be "[all]" or an attribute description */
1109 if ( ber_bvstrcasecmp( &bv, &aci_bv[ ACI_BV_BR_ALL ] ) == 0 ) {
1113 if ( slap_bv2ad( &bv, &ad, &text ) != LDAP_SUCCESS ) {
1114 return LDAP_INVALID_SYNTAX;
1119 /* "perms;attr" go in pairs */
1120 if ( i > 0 && ( i & 1 ) == 0 ) {
1121 return LDAP_SUCCESS;
1124 return LDAP_INVALID_SYNTAX;
1127 return LDAP_SUCCESS;
1131 OpenLDAPaciNormalizeRight(
1132 struct berval *action,
1133 struct berval *naction,
1136 struct berval grantdeny,
1143 if ( acl_get_part( action, 0, ';', &grantdeny ) < 0 ) {
1144 return LDAP_INVALID_SYNTAX;
1146 idx = bv_getcaseidx( &grantdeny, ACIgrantdeny );
1148 return LDAP_INVALID_SYNTAX;
1151 ber_dupbv_x( naction, (struct berval *)ACIgrantdeny[ idx ], ctx );
1153 for ( i = 1; acl_get_part( action, i, ';', &bv ) >= 0; i++ ) {
1156 if ( OpenLDAPaciValidatePerms( &bv ) != LDAP_SUCCESS )
1158 return LDAP_INVALID_SYNTAX;
1166 /* could be "[all]" or an attribute description */
1167 if ( ber_bvstrcasecmp( &bv, &aci_bv[ ACI_BV_BR_ALL ] ) == 0 ) {
1168 bv = aci_bv[ ACI_BV_BR_ALL ];
1171 AttributeDescription *ad = NULL;
1172 const char *text = NULL;
1175 rc = slap_bv2ad( &bv, &ad, &text );
1176 if ( rc != LDAP_SUCCESS ) {
1177 return LDAP_INVALID_SYNTAX;
1183 naction->bv_val = ber_memrealloc_x( naction->bv_val,
1184 naction->bv_len + STRLENOF( ";" )
1185 + perms.bv_len + STRLENOF( ";" )
1189 ptr = &naction->bv_val[ naction->bv_len ];
1192 ptr = lutil_strncopy( ptr, perms.bv_val, perms.bv_len );
1195 ptr = lutil_strncopy( ptr, bv.bv_val, bv.bv_len );
1197 naction->bv_len += STRLENOF( ";" ) + perms.bv_len
1198 + STRLENOF( ";" ) + bv.bv_len;
1202 /* perms;attr go in pairs */
1203 if ( i > 1 && ( i & 1 ) ) {
1204 return LDAP_SUCCESS;
1207 return LDAP_INVALID_SYNTAX;
1212 OpenLDAPaciValidateRights(
1213 struct berval *actions )
1216 struct berval bv = BER_BVNULL;
1219 for ( i = 0; acl_get_part( actions, i, '$', &bv ) >= 0; i++ ) {
1220 if ( OpenLDAPaciValidateRight( &bv ) != LDAP_SUCCESS ) {
1221 return LDAP_INVALID_SYNTAX;
1225 return LDAP_SUCCESS;
1229 OpenLDAPaciNormalizeRights(
1230 struct berval *actions,
1231 struct berval *nactions,
1235 struct berval bv = BER_BVNULL;
1238 BER_BVZERO( nactions );
1239 for ( i = 0; acl_get_part( actions, i, '$', &bv ) >= 0; i++ ) {
1243 rc = OpenLDAPaciNormalizeRight( &bv, &nbv, ctx );
1244 if ( rc != LDAP_SUCCESS ) {
1245 ber_memfree_x( nactions->bv_val, ctx );
1246 BER_BVZERO( nactions );
1247 return LDAP_INVALID_SYNTAX;
1254 nactions->bv_val = ber_memrealloc_x( nactions->bv_val,
1255 nactions->bv_len + STRLENOF( "$" )
1258 nactions->bv_val[ nactions->bv_len ] = '$';
1259 AC_MEMCPY( &nactions->bv_val[ nactions->bv_len + 1 ],
1260 nbv.bv_val, nbv.bv_len + 1 );
1261 ber_memfree_x( nbv.bv_val, ctx );
1262 nactions->bv_len += STRLENOF( "$" ) + nbv.bv_len;
1267 return LDAP_SUCCESS;
1270 static const struct berval *OpenLDAPaciscopes[] = {
1271 &aci_bv[ ACI_BV_ENTRY ],
1272 &aci_bv[ ACI_BV_CHILDREN ],
1273 &aci_bv[ ACI_BV_SUBTREE ],
1278 static const struct berval *OpenLDAPacitypes[] = {
1280 &aci_bv[ ACI_BV_GROUP ],
1281 &aci_bv[ ACI_BV_ROLE ],
1283 /* set to one past the last DN-valued type with options (/) */
1284 #define LAST_OPTIONAL 2
1286 &aci_bv[ ACI_BV_ACCESS_ID ],
1287 &aci_bv[ ACI_BV_SUBTREE ],
1288 &aci_bv[ ACI_BV_ONELEVEL ],
1289 &aci_bv[ ACI_BV_CHILDREN ],
1291 /* set to one past the last DN-valued type */
1292 #define LAST_DNVALUED 6
1295 &aci_bv[ ACI_BV_DNATTR ],
1296 &aci_bv[ ACI_BV_PUBLIC ],
1297 &aci_bv[ ACI_BV_USERS ],
1298 &aci_bv[ ACI_BV_SELF ],
1299 &aci_bv[ ACI_BV_SET ],
1300 &aci_bv[ ACI_BV_SET_REF ],
1306 OpenLDAPaciValidate(
1308 struct berval *val )
1310 struct berval oid = BER_BVNULL,
1312 rights = BER_BVNULL,
1314 subject = BER_BVNULL;
1317 if ( BER_BVISEMPTY( val ) ) {
1318 return LDAP_INVALID_SYNTAX;
1322 if ( acl_get_part( val, 0, '#', &oid ) < 0 ||
1323 numericoidValidate( NULL, &oid ) != LDAP_SUCCESS )
1325 /* NOTE: the numericoidValidate() is rather pedantic;
1326 * I'd replace it with X-ORDERED VALUES so that
1327 * it's guaranteed values are maintained and used
1328 * in the desired order */
1329 return LDAP_INVALID_SYNTAX;
1333 if ( acl_get_part( val, 1, '#', &scope ) < 0 ||
1334 bv_getcaseidx( &scope, OpenLDAPaciscopes ) == -1 )
1336 return LDAP_INVALID_SYNTAX;
1340 if ( acl_get_part( val, 2, '#', &rights ) < 0 ||
1341 OpenLDAPaciValidateRights( &rights ) != LDAP_SUCCESS )
1343 return LDAP_INVALID_SYNTAX;
1347 if ( acl_get_part( val, 3, '#', &type ) < 0 ) {
1348 return LDAP_INVALID_SYNTAX;
1350 idx = bv_getcaseidx( &type, OpenLDAPacitypes );
1354 if ( acl_get_part( &type, 0, '/', &isgr ) < 0 ) {
1355 return LDAP_INVALID_SYNTAX;
1358 idx = bv_getcaseidx( &isgr, OpenLDAPacitypes );
1359 if ( idx == -1 || idx >= LAST_OPTIONAL ) {
1360 return LDAP_INVALID_SYNTAX;
1365 bv_get_tail( val, &type, &subject );
1366 if ( subject.bv_val[ 0 ] != '#' ) {
1367 return LDAP_INVALID_SYNTAX;
1370 if ( idx >= LAST_DNVALUED ) {
1371 if ( OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_DNATTR ] ) {
1372 AttributeDescription *ad = NULL;
1373 const char *text = NULL;
1376 rc = slap_bv2ad( &subject, &ad, &text );
1377 if ( rc != LDAP_SUCCESS ) {
1378 return LDAP_INVALID_SYNTAX;
1381 if ( ad->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
1382 /* FIXME: allow nameAndOptionalUID? */
1383 return LDAP_INVALID_SYNTAX;
1388 return LDAP_SUCCESS;
1390 } else if ( OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_GROUP ]
1391 || OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_ROLE ] )
1393 /* do {group|role}/oc/at check */
1394 struct berval ocbv = BER_BVNULL,
1397 ocbv.bv_val = ber_bvchr( &type, '/' );
1398 if ( ocbv.bv_val != NULL ) {
1400 ocbv.bv_len = type.bv_len
1401 - ( ocbv.bv_val - type.bv_val );
1403 atbv.bv_val = ber_bvchr( &ocbv, '/' );
1404 if ( atbv.bv_val != NULL ) {
1405 AttributeDescription *ad = NULL;
1406 const char *text = NULL;
1410 atbv.bv_len = type.bv_len
1411 - ( atbv.bv_val - type.bv_val );
1412 ocbv.bv_len = atbv.bv_val - ocbv.bv_val - 1;
1414 rc = slap_bv2ad( &atbv, &ad, &text );
1415 if ( rc != LDAP_SUCCESS ) {
1416 return LDAP_INVALID_SYNTAX;
1420 if ( oc_bvfind( &ocbv ) == NULL ) {
1421 return LDAP_INVALID_SYNTAX;
1426 if ( BER_BVISEMPTY( &subject ) ) {
1427 /* empty DN invalid */
1428 return LDAP_INVALID_SYNTAX;
1434 /* FIXME: pass DN syntax? */
1435 return dnValidate( NULL, &subject );
1439 OpenLDAPaciPrettyNormal(
1445 struct berval oid = BER_BVNULL,
1447 rights = BER_BVNULL,
1448 nrights = BER_BVNULL,
1451 subject = BER_BVNULL,
1452 nsubject = BER_BVNULL;
1459 if ( BER_BVISEMPTY( val ) ) {
1460 return LDAP_INVALID_SYNTAX;
1463 /* oid: if valid, it's already normalized */
1464 if ( acl_get_part( val, 0, '#', &oid ) < 0 ||
1465 numericoidValidate( NULL, &oid ) != LDAP_SUCCESS )
1467 return LDAP_INVALID_SYNTAX;
1470 /* scope: normalize by replacing with OpenLDAPaciscopes */
1471 if ( acl_get_part( val, 1, '#', &scope ) < 0 ) {
1472 return LDAP_INVALID_SYNTAX;
1474 idx = bv_getcaseidx( &scope, OpenLDAPaciscopes );
1476 return LDAP_INVALID_SYNTAX;
1478 scope = *OpenLDAPaciscopes[ idx ];
1481 if ( acl_get_part( val, 2, '#', &rights ) < 0 ) {
1482 return LDAP_INVALID_SYNTAX;
1484 if ( OpenLDAPaciNormalizeRights( &rights, &nrights, ctx )
1487 return LDAP_INVALID_SYNTAX;
1491 if ( acl_get_part( val, 3, '#', &type ) < 0 ) {
1492 rc = LDAP_INVALID_SYNTAX;
1495 idx = bv_getcaseidx( &type, OpenLDAPacitypes );
1499 if ( acl_get_part( &type, 0, '/', &isgr ) < 0 ) {
1500 rc = LDAP_INVALID_SYNTAX;
1504 idx = bv_getcaseidx( &isgr, OpenLDAPacitypes );
1505 if ( idx == -1 || idx >= LAST_OPTIONAL ) {
1506 rc = LDAP_INVALID_SYNTAX;
1510 ntype = *OpenLDAPacitypes[ idx ];
1513 bv_get_tail( val, &type, &subject );
1515 if ( BER_BVISEMPTY( &subject ) || subject.bv_val[ 0 ] != '#' ) {
1516 rc = LDAP_INVALID_SYNTAX;
1523 if ( idx < LAST_DNVALUED ) {
1524 /* FIXME: pass DN syntax? */
1526 rc = dnNormalize( 0, NULL, NULL,
1527 &subject, &nsubject, ctx );
1529 rc = dnPretty( NULL, &subject, &nsubject, ctx );
1532 if ( rc == LDAP_SUCCESS ) {
1539 if ( OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_GROUP ]
1540 || OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_ROLE ] )
1542 /* do {group|role}/oc/at check */
1543 struct berval ocbv = BER_BVNULL,
1546 ocbv.bv_val = ber_bvchr( &type, '/' );
1547 if ( ocbv.bv_val != NULL ) {
1548 ObjectClass *oc = NULL;
1549 AttributeDescription *ad = NULL;
1550 const char *text = NULL;
1554 bv.bv_len = ntype.bv_len;
1557 ocbv.bv_len = type.bv_len - ( ocbv.bv_val - type.bv_val );
1559 atbv.bv_val = ber_bvchr( &ocbv, '/' );
1560 if ( atbv.bv_val != NULL ) {
1562 atbv.bv_len = type.bv_len
1563 - ( atbv.bv_val - type.bv_val );
1564 ocbv.bv_len = atbv.bv_val - ocbv.bv_val - 1;
1566 rc = slap_bv2ad( &atbv, &ad, &text );
1567 if ( rc != LDAP_SUCCESS ) {
1568 rc = LDAP_INVALID_SYNTAX;
1572 bv.bv_len += STRLENOF( "/" ) + ad->ad_cname.bv_len;
1575 oc = oc_bvfind( &ocbv );
1577 rc = LDAP_INVALID_SYNTAX;
1581 bv.bv_len += STRLENOF( "/" ) + oc->soc_cname.bv_len;
1582 bv.bv_val = ber_memalloc_x( bv.bv_len + 1, ctx );
1585 ptr = lutil_strncopy( ptr, ntype.bv_val, ntype.bv_len );
1588 ptr = lutil_strncopy( ptr,
1589 oc->soc_cname.bv_val,
1590 oc->soc_cname.bv_len );
1594 ptr = lutil_strncopy( ptr,
1595 ad->ad_cname.bv_val,
1596 ad->ad_cname.bv_len );
1605 } else if ( OpenLDAPacitypes[ idx ] == &aci_bv[ ACI_BV_DNATTR ] ) {
1606 AttributeDescription *ad = NULL;
1607 const char *text = NULL;
1610 rc = slap_bv2ad( &subject, &ad, &text );
1611 if ( rc != LDAP_SUCCESS ) {
1612 rc = LDAP_INVALID_SYNTAX;
1616 if ( ad->ad_type->sat_syntax != slap_schema.si_syn_distinguishedName ) {
1617 /* FIXME: allow nameAndOptionalUID? */
1618 rc = LDAP_INVALID_SYNTAX;
1622 nsubject = ad->ad_cname;
1627 oid.bv_len + STRLENOF( "#" )
1628 + scope.bv_len + STRLENOF( "#" )
1629 + rights.bv_len + STRLENOF( "#" )
1630 + ntype.bv_len + STRLENOF( "#" )
1633 out->bv_val = ber_memalloc_x( out->bv_len + 1, ctx );
1634 ptr = lutil_strncopy( out->bv_val, oid.bv_val, oid.bv_len );
1637 ptr = lutil_strncopy( ptr, scope.bv_val, scope.bv_len );
1640 ptr = lutil_strncopy( ptr, nrights.bv_val, nrights.bv_len );
1643 ptr = lutil_strncopy( ptr, ntype.bv_val, ntype.bv_len );
1646 if ( !BER_BVISNULL( &nsubject ) ) {
1647 ptr = lutil_strncopy( ptr, nsubject.bv_val, nsubject.bv_len );
1652 if ( freesubject ) {
1653 ber_memfree_x( nsubject.bv_val, ctx );
1657 ber_memfree_x( ntype.bv_val, ctx );
1660 if ( !BER_BVISNULL( &nrights ) ) {
1661 ber_memfree_x( nrights.bv_val, ctx );
1674 return OpenLDAPaciPrettyNormal( val, out, ctx, 0 );
1678 OpenLDAPaciNormalize(
1686 return OpenLDAPaciPrettyNormal( val, out, ctx, 1 );
1689 #if SLAPD_ACI_ENABLED == SLAPD_MOD_DYNAMIC
1691 * FIXME: need config and Makefile.am code to ease building
1695 init_module( int argc, char *argv[] )
1697 return slap_dynacl_register();
1699 #endif /* SLAPD_ACI_ENABLED == SLAPD_MOD_DYNAMIC */
1701 #endif /* SLAPD_ACI_ENABLED */