3 * Copyright 1998-2007 The OpenLDAP Foundation.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted only as authorized by the OpenLDAP
10 * A copy of this license is available in the file LICENSE in the
11 * top-level directory of the distribution or, alternatively, at
12 * <http://www.OpenLDAP.org/license.html>.
17 #include <ac/string.h>
26 slap_style_t pg_style;
30 static ObjectClass *pg_posixGroup;
31 static AttributeDescription *pg_memberUid;
32 static ObjectClass *pg_posixAccount;
33 static AttributeDescription *pg_uidNumber;
35 static int pg_dynacl_destroy( void *priv );
48 const char *text = NULL;
51 ber_str2bv( pattern, 0, 0, &pat );
53 pg = ch_calloc( 1, sizeof( pg_t ) );
57 switch ( pg->pg_style ) {
59 rc = dnNormalize( 0, NULL, NULL, &pat, &pg->pg_pat, NULL );
60 if ( rc != LDAP_SUCCESS ) {
61 fprintf( stderr, "%s line %d: posixGroup ACL: "
62 "unable to normalize DN \"%s\".\n",
63 fname, lineno, pattern );
68 case ACL_STYLE_EXPAND:
69 ber_dupbv( &pg->pg_pat, &pat );
73 fprintf( stderr, "%s line %d: posixGroup ACL: "
74 "unsupported style \"%s\".\n",
75 fname, lineno, style_strings[ pg->pg_style ] );
79 /* TODO: use opts to allow the use of different
80 * group objects and member attributes */
81 if ( pg_posixGroup == NULL ) {
82 pg_posixGroup = oc_find( "posixGroup" );
83 if ( pg_posixGroup == NULL ) {
84 fprintf( stderr, "%s line %d: posixGroup ACL: "
85 "unable to lookup \"posixGroup\" "
91 pg_posixAccount = oc_find( "posixAccount" );
92 if ( pg_posixGroup == NULL ) {
93 fprintf( stderr, "%s line %d: posixGroup ACL: "
94 "unable to lookup \"posixAccount\" "
100 rc = slap_str2ad( "memberUid", &pg_memberUid, &text );
101 if ( rc != LDAP_SUCCESS ) {
102 fprintf( stderr, "%s line %d: posixGroup ACL: "
103 "unable to lookup \"memberUid\" "
104 "attributeDescription (%d: %s).\n",
105 fname, lineno, rc, text );
109 rc = slap_str2ad( "uidNumber", &pg_uidNumber, &text );
110 if ( rc != LDAP_SUCCESS ) {
111 fprintf( stderr, "%s line %d: posixGroup ACL: "
112 "unable to lookup \"uidNumber\" "
113 "attributeDescription (%d: %s).\n",
114 fname, lineno, rc, text );
123 (void)pg_dynacl_destroy( (void *)pg );
133 pg_t *pg = (pg_t *)priv;
136 bv->bv_len = STRLENOF( " dynacl/posixGroup.expand=" ) + pg->pg_pat.bv_len;
137 bv->bv_val = ch_malloc( bv->bv_len + 1 );
139 ptr = lutil_strcopy( bv->bv_val, " dynacl/posixGroup" );
141 switch ( pg->pg_style ) {
143 ptr = lutil_strcopy( ptr, ".exact=" );
146 case ACL_STYLE_EXPAND:
147 ptr = lutil_strcopy( ptr, ".expand=" );
154 ptr = lutil_strncopy( ptr, pg->pg_pat.bv_val, pg->pg_pat.bv_len );
157 bv->bv_len = ptr - bv->bv_val;
167 AttributeDescription *desc,
171 slap_access_t *grant,
172 slap_access_t *deny )
174 pg_t *pg = (pg_t *)priv;
178 Backend *be = op->o_bd,
181 struct berval group_ndn;
183 ACL_INVALIDATE( *deny );
186 if ( target && dn_match( &target->e_nname, &op->o_ndn ) ) {
191 user_be = op->o_bd = select_backend( &op->o_ndn, 0, 0 );
192 if ( op->o_bd == NULL ) {
196 rc = be_entry_get_rw( op, &op->o_ndn, pg_posixAccount, pg_uidNumber, 0, &user );
199 if ( rc != LDAP_SUCCESS || user == NULL ) {
205 if ( pg->pg_style == ACL_STYLE_EXPAND ) {
209 bv.bv_len = sizeof( buf ) - 1;
212 if ( acl_string_expand( &bv, &pg->pg_pat,
213 target->e_nname.bv_val,
219 if ( dnNormalize( 0, NULL, NULL, &bv, &group_ndn,
220 op->o_tmpmemctx ) != LDAP_SUCCESS )
222 /* did not expand to a valid dn */
227 group_ndn = pg->pg_pat;
230 if ( target && dn_match( &target->e_nname, &group_ndn ) ) {
235 group_be = op->o_bd = select_backend( &group_ndn, 0, 0 );
236 if ( op->o_bd == NULL ) {
239 rc = be_entry_get_rw( op, &group_ndn, pg_posixGroup, pg_memberUid, 0, &group );
242 if ( group_ndn.bv_val != pg->pg_pat.bv_val ) {
243 op->o_tmpfree( group_ndn.bv_val, op->o_tmpmemctx );
246 if ( rc == LDAP_SUCCESS && group != NULL ) {
250 a_uid = attr_find( user->e_attrs, pg_uidNumber );
251 if ( !a_uid || !BER_BVISNULL( &a_uid->a_nvals[ 1 ] ) ) {
252 rc = LDAP_NO_SUCH_ATTRIBUTE;
255 a_member = attr_find( group->e_attrs, pg_memberUid );
257 rc = LDAP_NO_SUCH_ATTRIBUTE;
260 rc = value_find_ex( pg_memberUid,
261 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
262 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
263 a_member->a_nvals, &a_uid->a_nvals[ 0 ],
269 rc = LDAP_NO_SUCH_OBJECT;
273 if ( rc == LDAP_SUCCESS ) {
274 ACL_LVL_ASSIGN_WRITE( *grant );
278 if ( group != NULL && group != target ) {
280 be_entry_release_r( op, group );
284 if ( user != NULL && user != target ) {
286 be_entry_release_r( op, user );
297 pg_t *pg = (pg_t *)priv;
300 if ( !BER_BVISNULL( &pg->pg_pat ) ) {
301 ber_memfree( pg->pg_pat.bv_val );
309 static struct slap_dynacl_t pg_dynacl = {
318 init_module( int argc, char *argv[] )
320 return slap_dynacl_register( &pg_dynacl );
323 #endif /* SLAP_DYNACL */