]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/group.c
attribute & objectclass mapping rules
[openldap] / servers / slapd / back-ldap / group.c
1 /* group.c - ldap backend acl group routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/socket.h>
13 #include <ac/string.h>
14
15 #include "slap.h"
16 #include "back-ldap.h"
17
18
19 /* return 0 IFF op_dn is a value in member attribute
20  * of entry with gr_dn AND that entry has an objectClass
21  * value of groupOfNames
22  */
23 int
24 ldap_back_group(
25         Backend *be,
26         Connection *conn,
27         Operation *op,
28         Entry   *target,
29         const char      *gr_ndn,
30         const char      *op_ndn,
31         ObjectClass* group_oc,
32         AttributeDescription* group_at
33 )
34 {
35         struct ldapinfo *li = (struct ldapinfo *) be->be_private;    
36         int rc = 1;
37         Attribute   *attr;
38         Entry *e;
39         struct berval bv;
40         LDAPMessage     *result;
41         char *gattr[2];
42         char *filter;
43         LDAP *ld;
44
45         AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
46         char *group_oc_name = NULL;
47         char *group_at_name = group_at->ad_cname->bv_val;
48
49         if( group_oc->soc_names && group_oc->soc_names[0] ) {
50                 group_oc_name = group_oc->soc_names[0];
51         } else {
52                 group_oc_name = group_oc->soc_oid;
53         }
54
55         if (target != NULL && strcmp(target->e_ndn, gr_ndn) == 0) {
56                 /* we already have a copy of the entry */
57                 /* attribute and objectclass mapping has already been done */
58                 e = target;
59
60                 if( is_entry_objectclass( e, group_oc ) ) {
61                         return(1);
62                 }
63
64                 if ((attr = attr_find(e->e_attrs, group_at)) == NULL)
65                         return(1);
66
67                 bv.bv_val = (char *) op_ndn;
68                 bv.bv_len = strlen( op_ndn );         
69                 if( value_find( group_at, attr->a_vals, &bv ) == 0  )
70                         return(1);
71
72         } else {
73                 group_oc_name = ldap_back_map(&li->oc_map, group_oc_name, 0);
74                 if (group_oc_name == NULL)
75                         return(1);
76                 group_at_name = ldap_back_map(&li->at_map, group_at_name, 0);
77                 if (group_at_name == NULL)
78                         return(1);
79
80                 filter = ch_malloc(sizeof("(&(objectclass=)(=))")
81                                                         + strlen(group_oc_name)
82                                                         + strlen(group_at_name)
83                                                         + strlen(op_ndn) + 1);
84                 if (filter == NULL)
85                         return(1);
86
87                 if (ldap_initialize(&ld, li->url) != LDAP_SUCCESS) {
88                         ch_free(filter);
89                         return(1);
90                 }
91
92                 if (ldap_bind_s(ld, li->binddn, li->bindpw, LDAP_AUTH_SIMPLE) == LDAP_SUCCESS) {
93                         strcpy(filter, "(&(objectclass=");
94                         strcat(filter, group_oc_name);
95                         strcat(filter, ")(");
96                         strcat(filter, group_at_name);
97                         strcat(filter, "=");
98                         strcat(filter, op_ndn);
99                         strcat(filter, "))");
100
101                         gattr[0] = "objectclass";
102                         gattr[1] = NULL;
103                         if (ldap_search_ext_s(ld, gr_ndn, LDAP_SCOPE_BASE, filter,
104                                                                         gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
105                                                                         LDAP_NO_LIMIT, &result) == LDAP_SUCCESS)
106                         {
107                                 if (ldap_first_entry(ld, result) != NULL)
108                                         rc = 0;
109                                 ldap_msgfree(result);
110                         }
111                 }
112                 ldap_unbind(ld);
113                 ch_free(filter);
114                 return(rc);
115     }
116
117         return(0);
118 }
119