openldap/servers/slapd/back-ldap/group.c

211 lines
5.4 KiB
C
Raw Normal View History

2000-06-10 09:32:36 +08:00
/* group.c - ldap backend acl group routine */
/* $OpenLDAP$ */
/*
2003-01-04 04:20:47 +08:00
* Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
2000-06-10 09:32:36 +08:00
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include "portable.h"
#include <stdio.h>
#include <ac/socket.h>
#include <ac/string.h>
#include "slap.h"
#include "back-ldap.h"
#include "lutil.h"
2000-06-10 09:32:36 +08:00
/* return 0 IFF op_dn is a value in group_at (member) attribute
2000-06-10 09:32:36 +08:00
* of entry with gr_dn AND that entry has an objectClass
* value of group_oc (groupOfNames)
2000-06-10 09:32:36 +08:00
*/
int
ldap_back_group(
Backend *be,
Connection *conn,
Operation *op,
Entry *target,
struct berval *gr_ndn,
struct berval *op_ndn,
ObjectClass *group_oc,
2000-06-10 09:32:36 +08:00
AttributeDescription* group_at
)
{
struct ldapinfo *li = (struct ldapinfo *) be->be_private;
int rc = 1;
Attribute *attr;
2000-06-10 09:32:36 +08:00
LDAPMessage *result;
char *gattr[2];
2001-12-28 12:20:08 +08:00
char *filter = NULL, *ptr;
2000-06-10 09:32:36 +08:00
LDAP *ld;
2001-12-28 12:20:08 +08:00
struct berval mop_ndn = { 0, NULL }, mgr_ndn = { 0, NULL };
2000-06-10 09:32:36 +08:00
AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
2002-01-02 19:00:36 +08:00
struct berval group_oc_name = {0, NULL};
struct berval group_at_name = group_at->ad_cname;
2000-06-10 09:32:36 +08:00
if( group_oc->soc_names && group_oc->soc_names[0] ) {
2002-01-02 19:00:36 +08:00
group_oc_name.bv_val = group_oc->soc_names[0];
2000-06-10 09:32:36 +08:00
} else {
2002-01-02 19:00:36 +08:00
group_oc_name.bv_val = group_oc->soc_oid;
2000-06-10 09:32:36 +08:00
}
2002-01-02 19:00:36 +08:00
if (group_oc_name.bv_val)
group_oc_name.bv_len = strlen(group_oc_name.bv_val);
2000-06-10 09:32:36 +08:00
if (target != NULL && dn_match( &target->e_nname, gr_ndn ) ) {
2000-06-10 09:32:36 +08:00
/* we already have a copy of the entry */
2001-02-20 03:14:12 +08:00
/* attribute and objectclass mapping has already been done */
2000-06-10 09:32:36 +08:00
/*
* first we need to check if the objectClass attribute
* has been retieved; otherwise we need to repeat the search
*/
attr = attr_find( target->e_attrs, ad_objectClass );
if ( attr != NULL ) {
/*
* Now we can check for the group objectClass value
*/
if( !is_entry_objectclass( target, group_oc, 0 ) ) {
return(1);
}
/*
* This part has been reworked: the group attr compare
* fails only if the attribute is PRESENT but the value
* is NOT PRESENT; if the attribute is NOT PRESENT, the
* search must be repeated as well.
* This may happen if a search for an entry has already
* been performed (target is not null) but the group
* attribute has not been required
*/
if ((attr = attr_find(target->e_attrs, group_at)) != NULL) {
if( value_find_ex( group_at, SLAP_MR_VALUE_NORMALIZED_MATCH,
attr->a_vals, op_ndn ) != LDAP_SUCCESS )
return(1);
return(0);
} /* else: repeat the search */
} /* else: repeat the search */
} /* else: do the search */
/*
* Rewrite the op ndn if needed
*/
#ifdef ENABLE_REWRITE
switch ( rewrite_session( li->rwinfo, "bindDn",
2001-12-28 12:20:08 +08:00
op_ndn->bv_val, conn, &mop_ndn.bv_val ) ) {
case REWRITE_REGEXEC_OK:
2001-12-28 12:20:08 +08:00
if ( mop_ndn.bv_val == NULL ) {
mop_ndn = *op_ndn;
2000-06-10 09:32:36 +08:00
}
#ifdef NEW_LOGGING
LDAP_LOG( BACK_LDAP, DETAIL1,
"[rw] bindDn (op ndn in group): \"%s\" -> \"%s\"\n",
op_ndn->bv_val, mop_ndn.bv_val, 0 );
#else /* !NEW_LOGGING */
Debug( LDAP_DEBUG_ARGS,
"rw> bindDn (op ndn in group): \"%s\" -> \"%s\"\n%s",
2001-12-28 12:20:08 +08:00
op_ndn->bv_val, mop_ndn.bv_val, "" );
#endif /* !NEW_LOGGING */
break;
case REWRITE_REGEXEC_UNWILLING:
case REWRITE_REGEXEC_ERR:
goto cleanup;
}
2000-06-10 09:32:36 +08:00
/*
* Rewrite the gr ndn if needed
*/
switch ( rewrite_session( li->rwinfo, "searchBase",
2001-12-28 12:20:08 +08:00
gr_ndn->bv_val, conn, &mgr_ndn.bv_val ) ) {
case REWRITE_REGEXEC_OK:
2001-12-28 12:20:08 +08:00
if ( mgr_ndn.bv_val == NULL ) {
mgr_ndn = *gr_ndn;
}
#ifdef NEW_LOGGING
LDAP_LOG( BACK_LDAP, DETAIL1,
"[rw] searchBase (gr ndn in group): \"%s\" -> \"%s\"\n%s",
gr_ndn->bv_val, mgr_ndn.bv_val, "" );
#else /* !NEW_LOGGING */
Debug( LDAP_DEBUG_ARGS,
"rw> searchBase (gr ndn in group):"
" \"%s\" -> \"%s\"\n%s",
2001-12-28 12:20:08 +08:00
gr_ndn->bv_val, mgr_ndn.bv_val, "" );
#endif /* !NEW_LOGGING */
break;
case REWRITE_REGEXEC_UNWILLING:
case REWRITE_REGEXEC_ERR:
goto cleanup;
}
#else /* !ENABLE_REWRITE */
2001-12-28 12:20:08 +08:00
ldap_back_dn_massage( li, op_ndn, &mop_ndn, 1, 1 );
if ( mop_ndn.bv_val == NULL ) {
goto cleanup;
}
2001-12-28 12:20:08 +08:00
ldap_back_dn_massage( li, gr_ndn, &mgr_ndn, 1, 1 );
if ( mgr_ndn.bv_val == NULL ) {
goto cleanup;
}
#endif /* !ENABLE_REWRITE */
2002-01-02 19:00:36 +08:00
ldap_back_map(&li->oc_map, &group_oc_name, &group_oc_name, 0);
if (group_oc_name.bv_val == NULL)
goto cleanup;
2002-01-02 19:00:36 +08:00
ldap_back_map(&li->at_map, &group_at_name, &group_at_name, 0);
if (group_at_name.bv_val == NULL)
goto cleanup;
filter = ch_malloc(sizeof("(&(objectclass=)(=))")
2002-01-02 19:00:36 +08:00
+ group_oc_name.bv_len
+ group_at_name.bv_len
2001-12-28 12:20:08 +08:00
+ mop_ndn.bv_len + 1);
if (filter == NULL)
goto cleanup;
if (ldap_initialize(&ld, li->url) != LDAP_SUCCESS) {
goto cleanup;
}
2000-06-10 09:32:36 +08:00
if (ldap_bind_s(ld, li->binddn, li->bindpw, LDAP_AUTH_SIMPLE)
!= LDAP_SUCCESS) {
goto cleanup;
}
2000-06-10 09:32:36 +08:00
ptr = lutil_strcopy(filter, "(&(objectclass=");
ptr = lutil_strcopy(ptr, group_oc_name.bv_val);
ptr = lutil_strcopy(ptr, ")(");
ptr = lutil_strcopy(ptr, group_at_name.bv_val);
ptr = lutil_strcopy(ptr, "=");
ptr = lutil_strcopy(ptr, mop_ndn.bv_val);
2001-12-28 12:20:08 +08:00
strcpy(ptr, "))");
gattr[0] = "objectclass";
gattr[1] = NULL;
2001-12-28 12:20:08 +08:00
if (ldap_search_ext_s(ld, mgr_ndn.bv_val, LDAP_SCOPE_BASE, filter,
gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
LDAP_NO_LIMIT, &result) == LDAP_SUCCESS) {
if (ldap_first_entry(ld, result) != NULL)
rc = 0;
ldap_msgfree(result);
}
2000-06-10 09:32:36 +08:00
2001-06-03 16:53:13 +08:00
cleanup:;
if ( ld != NULL ) {
2000-06-10 09:32:36 +08:00
ldap_unbind(ld);
}
ch_free(filter);
2001-12-28 12:20:08 +08:00
if ( mop_ndn.bv_val != op_ndn->bv_val ) {
free( mop_ndn.bv_val );
}
2001-12-28 12:20:08 +08:00
if ( mgr_ndn.bv_val != gr_ndn->bv_val ) {
free( mgr_ndn.bv_val );
}
return(rc);
2000-06-10 09:32:36 +08:00
}