]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/acl.c
Fix memory leak.
[openldap] / servers / slapd / acl.c
index f5c141bc4a0846fe5f905c04f76aec728910334f..31b1b80e9ca256c638bb4a4ab3809d6f7529609d 100644 (file)
@@ -1,7 +1,7 @@
 /* acl.c - routines to parse and check acl's */
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 
 static AccessControl * acl_get(
        AccessControl *ac, int *count,
        Backend *be, Operation *op,
-       Entry *e, char *attr,
+       Entry *e,
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+       AttributeDescription *desc,
+#else
+       const char *desc,
+#endif
        int nmatches, regmatch_t *matches );
 
 static slap_control_t acl_mask(
        AccessControl *ac, slap_access_mask_t *mask,
        Backend *be, Connection *conn, Operation *op,
-       Entry *e, char *attr, struct berval *val,
+       Entry *e,
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+       AttributeDescription *desc,
+#else
+       const char *desc,
+#endif
+       struct berval *val,
        regmatch_t *matches );
 
 #ifdef SLAPD_ACI_ENABLED
 static int aci_mask(
        Backend *be,
        Operation *op,
-       Entry *e, char *attr, struct berval *val, struct berval *aci,
-       regmatch_t *matches, slap_access_t *grant, slap_access_t *deny );
-
-char *supportedACIMechs[] = {
-       "1.3.6.1.4.1.4203.666.7.1",     /* experimental IETF aci family */
-       "1.3.6.1.4.1.4203.666.7.2",     /* experimental OpenLDAP aci family */
-       NULL
-};
+       Entry *e,
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+       AttributeDescription *desc,
+#else
+       const char *desc,
+#endif
+       struct berval *val,
+       struct berval *aci,
+       regmatch_t *matches,
+       slap_access_t *grant,
+       slap_access_t *deny );
 #endif
 
-static int     regex_matches(char *pat, char *str, char *buf, regmatch_t *matches);
-static void    string_expand(char *newbuf, int bufsiz, char *pattern,
+static int     regex_matches(
+       char *pat, char *str, char *buf, regmatch_t *matches);
+static void    string_expand(
+       char *newbuf, int bufsiz, char *pattern,
        char *match, regmatch_t *matches);
 
 
@@ -67,10 +83,13 @@ access_allowed(
     Connection         *conn,
     Operation          *op,
     Entry              *e,
-    char               *attr,
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+       AttributeDescription    *desc,
+#else
+    const char         *desc,
+#endif
     struct berval      *val,
-    slap_access_t      access
-)
+    slap_access_t      access )
 {
        int                             count;
        AccessControl   *a;
@@ -80,6 +99,12 @@ access_allowed(
        slap_access_mask_t mask;
        slap_control_t control;
 
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+       const char *attr = desc ? desc->ad_cname->bv_val : NULL;
+#else
+    const char *attr = desc;
+#endif
+
        regmatch_t       matches[MAXREMATCHES];
 
        Debug( LDAP_DEBUG_ACL,
@@ -105,7 +130,12 @@ access_allowed(
         * by ACL_WRITE checking as any found here are not provided
         * by the user
         */
-       if ( access >= ACL_WRITE && oc_check_op_no_usermod_attr( attr ) ) {
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+       if ( access >= ACL_WRITE && is_at_no_user_mod( desc->ad_type ) )
+#else
+       if ( access >= ACL_WRITE && oc_check_op_no_usermod_attr( attr ) )
+#endif
+       {
                Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
                        " %s access granted\n",
                        attr, 0, 0 );
@@ -141,7 +171,7 @@ access_allowed(
        a = NULL;
        count = 0;
 
-       while( a = acl_get( a, &count, be, op, e, attr, MAXREMATCHES, matches ) )
+       while( a = acl_get( a, &count, be, op, e, desc, MAXREMATCHES, matches ) )
        {
                int i;
 
@@ -159,7 +189,7 @@ access_allowed(
                }
 
                control = acl_mask( a, &mask, be, conn, op,
-                       e, attr, val, matches );
+                       e, desc, val, matches );
 
                if ( control != ACL_BREAK ) {
                        break;
@@ -202,14 +232,24 @@ acl_get(
     Backend            *be,
     Operation  *op,
     Entry              *e,
-    char               *attr,
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+       AttributeDescription *desc,
+#else
+    const char *desc,
+#endif
     int                        nmatch,
-    regmatch_t *matches
-)
+    regmatch_t *matches )
 {
+       const char *attr;
        assert( e != NULL );
        assert( count != NULL );
 
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+       attr = desc ? desc->ad_cname->bv_val : NULL;
+#else
+       attr = desc;
+#endif
+
        if( a == NULL ) {
                if( be == NULL ) {
                        a = global_acl;
@@ -250,7 +290,7 @@ acl_get(
                        *count, attr, 0);
 
                if ( attr == NULL || a->acl_attrs == NULL ||
-                       charray_inlist( a->acl_attrs, attr ) )
+                       ad_inlist( desc, a->acl_attrs ) )
                {
                        Debug( LDAP_DEBUG_ACL,
                                "<= acl_get: [%d] acl %s attr: %s\n",
@@ -282,7 +322,11 @@ acl_mask(
     Connection *conn,
     Operation  *op,
     Entry              *e,
-    char               *attr,
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+       AttributeDescription *desc,
+#else
+    const char *desc,
+#endif
     struct berval      *val,
        regmatch_t      *matches
 )
@@ -292,6 +336,11 @@ acl_mask(
 #ifdef LDAP_DEBUG
        char accessmaskbuf[ACCESSMASK_MAXLEN];
 #endif
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+       const char *attr = desc ? desc->ad_cname->bv_val : NULL;
+#else
+       const char *attr = desc;
+#endif
 
        assert( a != NULL );
        assert( mask != NULL );
@@ -398,9 +447,15 @@ acl_mask(
                }
 
                if ( b->a_dn_at != NULL && op->o_ndn != NULL ) {
-                       char *dn_at;
                        Attribute       *at;
                        struct berval   bv;
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+                       int match;
+                       const char *text;
+                       const char *desc = b->a_dn_at->ad_cname->bv_val;
+#else
+                       const char *desc = b->a_dn_at;
+#endif
 
                        Debug( LDAP_DEBUG_ACL, "<= check a_dn_at: %s\n",
                                b->a_dn_at, 0, 0);
@@ -408,44 +463,54 @@ acl_mask(
                        bv.bv_val = op->o_ndn;
                        bv.bv_len = strlen( bv.bv_val );
 
+                       /* see if asker is listed in dnattr */
 #ifdef SLAPD_SCHEMA_NOT_COMPAT
-                       dn_at = at_canonical_name( b->a_dn_at );
-#else
-                       dn_at = b->a_dn_at;
-#endif
+                       for( at = attrs_find( e->e_attrs, b->a_dn_at );
+                               at == NULL;
+                               at = attrs_find( e->e_attrs->a_next, b->a_dn_at ) )
+                       {
+                               if( value_find( b->a_dn_at, at->a_vals, &bv ) == 0 ) {
+                                       /* found it */
+                                       match = 1;
+                                       break;
+                               }
+                       }
 
-                       /* see if asker is listed in dnattr */ 
-                       if ( (at = attr_find( e->e_attrs, dn_at )) != NULL
-#ifdef SLAPD_SCHEMA_NOT_COMPAT
-                               /* not yet implemented */
-#else
-                               && value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0
-#endif
-                       )
+                       if( match ) {
+                               if ( b->a_dn_self && (val == NULL
+                                       || value_match( &match, b->a_dn_at,
+                                               b->a_dn_at->ad_type->sat_equality, val, &bv, &text ) )
+                                               != LDAP_SUCCESS
+                                       || match )
+                               {
+                                       continue;
+                               }
+                       } else if ( ! b->a_dn_self || val == NULL
+                               || value_match( &match, b->a_dn_at,
+                                       b->a_dn_at->ad_type->sat_equality, val, &bv, &text )
+                                       != LDAP_SUCCESS
+                               || match )
                        {
-                               if ( b->a_dn_self && 
-                                       (val == NULL
-#ifdef SLAPD_SCHEMA_NOT_COMPAT
-                                       /* not yet implemented */
+                               continue;
+                       }
 #else
-                                       || value_cmp( &bv, val, at->a_syntax, 2 )
-#endif
-                                       ) )
+                       /* see if asker is listed in dnattr */
+                       if ( (at = attr_find( e->e_attrs, b->a_dn_at )) != NULL &&
+                               value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
+                       {
+                               if ( b->a_dn_self && (val == NULL
+                                       || value_cmp( &bv, val, at->a_syntax, 2 ) ) )
                                {
                                        continue;
                                }
 
                        /* asker not listed in dnattr - check for self access */
                        } else if ( ! b->a_dn_self || val == NULL
-#ifdef SLAPD_SCHEMA_NOT_COMPAT
-                                       /* not yet implemented */
-#else
-                               || value_cmp( &bv, val, at->a_syntax, 2 ) != 0
-#endif
-                       )
+                               || value_cmp( &bv, val, at->a_syntax, 2 ) != 0 )
                        {
                                continue;
                        }
+#endif
                }
 
                if ( b->a_group_pat != NULL && op->o_ndn != NULL ) {
@@ -510,7 +575,7 @@ acl_mask(
                         */
                        for ( i = 0; at->a_vals[i] != NULL; i++ ) {
                                if (aci_mask( be, op,
-                                       e, attr, val, at->a_vals[i],
+                                       e, desc, val, at->a_vals[i],
                                        matches, &grant, &deny ) != 0)
                                {
                                        tgrant |= grant;
@@ -617,7 +682,7 @@ acl_check_modlist(
     Connection *conn,
     Operation  *op,
     Entry      *e,
-    LDAPModList        *mlist
+    Modifications      *mlist
 )
 {
        int             i;
@@ -654,29 +719,37 @@ acl_check_modlist(
 #endif
        }
 
-       for ( ; mlist != NULL; mlist = mlist->ml_next ) {
+       for ( ; mlist != NULL; mlist = mlist->sml_next ) {
                /*
                 * no-user-modification operational attributes are ignored
                 * by ACL_WRITE checking as any found here are not provided
                 * by the user
                 */
-               if ( oc_check_op_no_usermod_attr( mlist->ml_type ) ) {
-                       Debug( LDAP_DEBUG_ACL, "NoUserMod Operational attribute:"
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+               if ( is_at_no_user_mod( mlist->sml_desc->ad_type ) ) {
+                       Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
                                " modify access granted\n",
-                               mlist->ml_type, 0, 0 );
+                               mlist->sml_desc->ad_cname->bv_val, 0, 0 );
                        continue;
                }
+#else
+               if ( oc_check_op_no_usermod_attr( mlist->sml_type ) ) {
+                       Debug( LDAP_DEBUG_ACL, "acl: no-user-mod %s:"
+                               " modify access granted\n",
+                               mlist->sml_type, 0, 0 );
+                       continue;
+               }
+#endif
 
-               switch ( mlist->ml_op & ~LDAP_MOD_BVALUES ) {
+               switch ( mlist->sml_op ) {
                case LDAP_MOD_REPLACE:
                case LDAP_MOD_ADD:
-                       if ( mlist->ml_bvalues == NULL ) {
+                       if ( mlist->sml_bvalues == NULL ) {
                                break;
                        }
-                       for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
+                       for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
                                if ( ! access_allowed( be, conn, op, e,
-                                       mlist->ml_type, mlist->ml_bvalues[i],
-                                       ACL_WRITE ) )
+                                       mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
                                {
                                        return( 0 );
                                }
@@ -684,19 +757,17 @@ acl_check_modlist(
                        break;
 
                case LDAP_MOD_DELETE:
-                       if ( mlist->ml_bvalues == NULL ) {
+                       if ( mlist->sml_bvalues == NULL ) {
                                if ( ! access_allowed( be, conn, op, e,
-                                       mlist->ml_type, NULL, 
-                                       ACL_WRITE ) )
+                                       mlist->sml_desc, NULL, ACL_WRITE ) )
                                {
                                        return( 0 );
                                }
                                break;
                        }
-                       for ( i = 0; mlist->ml_bvalues[i] != NULL; i++ ) {
+                       for ( i = 0; mlist->sml_bvalues[i] != NULL; i++ ) {
                                if ( ! access_allowed( be, conn, op, e,
-                                       mlist->ml_type, mlist->ml_bvalues[i],
-                                       ACL_WRITE ) )
+                                       mlist->sml_desc, mlist->sml_bvalues[i], ACL_WRITE ) )
                                {
                                        return( 0 );
                                }
@@ -710,7 +781,7 @@ acl_check_modlist(
 
 #ifdef SLAPD_ACI_ENABLED
 static char *
-aci_bvstrdup (struct berval *bv)
+aci_bvstrdup( struct berval *bv )
 {
        char *s;
 
@@ -723,7 +794,9 @@ aci_bvstrdup (struct berval *bv)
 }
 
 static int
-aci_strbvcmp (char *s, struct berval *bv)
+aci_strbvcmp(
+       const char *s,
+       struct berval *bv )
 {
        int res, len;
 
@@ -739,7 +812,11 @@ aci_strbvcmp (char *s, struct berval *bv)
 }
 
 static int
-aci_get_part (struct berval *list, int ix, char sep, struct berval *bv)
+aci_get_part(
+       struct berval *list,
+       int ix,
+       char sep,
+       struct berval *bv )
 {
        int len;
        char *p;
@@ -774,8 +851,8 @@ aci_get_part (struct berval *list, int ix, char sep, struct berval *bv)
 }
 
 static int
-aci_list_map_rights (
-       struct berval *list)
+aci_list_map_rights(
+       struct berval *list )
 {
        struct berval bv;
        slap_access_t mask;
@@ -819,7 +896,10 @@ aci_list_map_rights (
 }
 
 static int
-aci_list_has_attr (struct berval *list, char *attr, struct berval *val)
+aci_list_has_attr(
+       struct berval *list,
+       const char *attr,
+       struct berval *val )
 {
        struct berval bv, left, right;
        int i;
@@ -865,7 +945,10 @@ aci_list_has_attr (struct berval *list, char *attr, struct berval *val)
 }
 
 static slap_access_t
-aci_list_get_attr_rights (struct berval *list, char *attr, struct berval *val)
+aci_list_get_attr_rights(
+       struct berval *list,
+       const char *attr,
+       struct berval *val )
 {
     struct berval bv;
     slap_access_t mask;
@@ -884,12 +967,12 @@ aci_list_get_attr_rights (struct berval *list, char *attr, struct berval *val)
 }
 
 static int
-aci_list_get_rights (
+aci_list_get_rights(
        struct berval *list,
-       char *attr,
+       const char *attr,
        struct berval *val,
        slap_access_t *grant,
-       slap_access_t *deny)
+       slap_access_t *deny )
 {
     struct berval perm, actn;
     slap_access_t *mask;
@@ -924,8 +1007,8 @@ aci_list_get_rights (
 static int
 aci_group_member (
        struct berval *subj,
-       char *grpoc,
-       char *grpat,
+       const char *defgrpoc,
+       const char *defgrpat,
     Backend            *be,
     Entry              *e,
     Operation          *op,
@@ -933,48 +1016,82 @@ aci_group_member (
 )
 {
        struct berval bv;
-       char *subjdn, *grpdn;
-       int rc = 0;
+       char *subjdn, *grpdn = NULL;
+       char *grpoc;
+       char *grpat;
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+       ObjectClass *grp_oc = NULL;
+       AttributeDescription *grp_ad = NULL;
+       char *text;
+#else
+       char *grp_oc;
+       char *grp_ad;
+#endif
+       int rc;
 
        /* format of string is "group/objectClassValue/groupAttrName" */
-       if (aci_get_part(subj, 0, '/', &bv) < 0)
+       if (aci_get_part(subj, 0, '/', &bv) < 0) {
                return(0);
+       }
+
        subjdn = aci_bvstrdup(&bv);
-       if (subjdn == NULL)
+       if (subjdn == NULL) {
                return(0);
+       }
 
-       if (aci_get_part(subj, 1, '/', &bv) < 0)
-               grpoc = ch_strdup(grpoc);
-       else
+       if (aci_get_part(subj, 1, '/', &bv) < 0) {
+               grpoc = ch_strdup( defgrpoc );
+       } else {
                grpoc = aci_bvstrdup(&bv);
+       }
 
-       if (aci_get_part(subj, 2, '/', &bv) < 0)
-               grpat = ch_strdup(grpat);
-       else
+       if (aci_get_part(subj, 2, '/', &bv) < 0) {
+               grpat = ch_strdup( defgrpat );
+       } else {
                grpat = aci_bvstrdup(&bv);
+       }
+
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+       rc = slap_str2ad( grpat, &grp_ad, &text );
+       if( rc != LDAP_SUCCESS ) {
+               rc = 0;
+               goto done;
+       }
+#else
+       grp_ad = grpat;
+#endif
+       rc = 0;
 
        grpdn = (char *)ch_malloc(1024);
-       if (grpoc != NULL && grpat != NULL && grpdn != NULL) {
+
+       if (grp_oc != NULL && grp_ad != NULL && grpdn != NULL) {
                string_expand(grpdn, 1024, subjdn, e->e_ndn, matches);
                if ( dn_normalize(grpdn) != NULL ) {
-                       rc = (backend_group(be, e, grpdn, op->o_ndn, grpoc, grpat) == 0);
+                       rc = (backend_group(be, e, grpdn, op->o_ndn, grp_oc, grp_ad) == 0);
                }
-               ch_free(grpdn);
        }
-       if (grpat != NULL)
-               ch_free(grpat);
-       if (grpoc != NULL)
-               ch_free(grpoc);
+
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+done:
+       if( grp_ad != NULL ) ad_free( grp_ad, 1 );
+#endif
+       ch_free(grpdn);
+       ch_free(grpat);
+       ch_free(grpoc);
        ch_free(subjdn);
        return(rc);
 }
 
 static int
-aci_mask (
+aci_mask(
     Backend                    *be,
     Operation          *op,
     Entry                      *e,
-    char                       *attr,
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+       AttributeDescription *desc,
+#else
+    const char         *attr,
+#endif
     struct berval      *val,
     struct berval      *aci,
        regmatch_t              *matches,
@@ -985,6 +1102,9 @@ aci_mask (
     struct berval bv, perms, sdn;
     char *subjdn;
        int rc, i;
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
+       char *attr;
+#endif
 
        /* parse an aci of the form:
                oid#scope#action;rights;attr;rights;attr$action;rights;attr;rights;attr#dnType#subjectDN
@@ -1002,12 +1122,6 @@ aci_mask (
        /* check that the aci family is supported */
        if (aci_get_part(aci, 0, '#', &bv) < 0)
                return(0);
-       for (i = 0; supportedACIMechs[i] != NULL; i++) {
-               if (aci_strbvcmp( supportedACIMechs[i], &bv ) == 0)
-                       break;
-       }
-       if (supportedACIMechs[i] == NULL)
-               return(0);
 
        /* check that the scope is "entry" */
        if (aci_get_part(aci, 1, '#', &bv) < 0
@@ -1048,12 +1162,41 @@ aci_mask (
                        return(1);
 
        } else if (aci_strbvcmp( "dnattr", &bv ) == 0) {
+               char *dnattr = aci_bvstrdup(&sdn);
+#ifdef SLAPD_SCHEMA_NOT_COMPAT
                Attribute *at;
-               char *attrname;
+               AttributeDescription *ad = NULL;
+               const char *text;
+
+               rc = slap_str2ad( dnattr, &ad, &text );
+               ch_free( dnattr );
+
+               if( rc != LDAP_SUCCESS ) {
+                       return 0;
+               }
 
-               attrname = aci_bvstrdup(&sdn);
-               at = attr_find(e->e_attrs, attrname);
-               ch_free(attrname);
+               rc = 0;
+
+               bv.bv_val = op->o_ndn;
+               bv.bv_len = strlen( bv.bv_val );
+
+               for(at = attrs_find( e->e_attrs, ad );
+                       at != NULL;
+                       at = attrs_find( at->a_next, ad ) )
+               {
+                       if (value_find( ad, at->a_vals, &bv) == 0 ) {
+                               rc = 1;
+                               break;
+                       }
+               }
+
+               ad_free( ad, 1 );
+               return rc;
+
+#else
+               Attribute *at;
+               at = attr_find( e->e_attrs, dnattr );
+               ch_free( dnattr );
 
                if (at != NULL) {
                        bv.bv_val = op->o_ndn;
@@ -1062,27 +1205,20 @@ aci_mask (
                        if (value_find( at->a_vals, &bv, at->a_syntax, 3 ) == 0 )
                                return(1);
                }
+#endif
 
        } else if (aci_strbvcmp( "group", &bv ) == 0) {
-               if (aci_group_member(&sdn, "groupOfNames", "member", be, e, op, matches))
+               if (aci_group_member(&sdn, SLAPD_GROUP_CLASS, SLAPD_GROUP_ATTR, be, e, op, matches))
                        return(1);
 
        } else if (aci_strbvcmp( "role", &bv ) == 0) {
-               if (aci_group_member(&sdn, "organizationalRole", "roleOccupant", be, e, op, matches))
+               if (aci_group_member(&sdn, SLAPD_ROLE_CLASS, SLAPD_ROLE_ATTR, be, e, op, matches))
                        return(1);
        }
 
        return(0);
 }
 
-char *
-get_supported_acimech (int index)
-{
-       if (index < 0 || index >= (sizeof(supportedACIMechs) / sizeof(char *)))
-               return(NULL);
-       return(supportedACIMechs[index]);
-}
-
 #endif /* SLAPD_ACI_ENABLED */
 
 static void