]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/group.c
Add ldap_back_group routine.
[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         Entry   *target,
27         const char      *gr_ndn,
28         const char      *op_ndn,
29         ObjectClass* group_oc,
30         AttributeDescription* group_at
31 )
32 {
33         struct ldapinfo *li = (struct ldapinfo *) be->be_private;    
34         int rc = 1;
35         Attribute   *attr;
36         Entry *e;
37         struct berval bv;
38         LDAPMessage     *result;
39         char *gattr[2];
40         char *filter;
41         LDAP *ld;
42
43         AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
44         const char *group_oc_name = NULL;
45         const char *group_at_name = group_at->ad_cname->bv_val;
46
47         if( group_oc->soc_names && group_oc->soc_names[0] ) {
48                 group_oc_name = group_oc->soc_names[0];
49         } else {
50                 group_oc_name = group_oc->soc_oid;
51         }
52
53         if (target != NULL && strcmp(target->e_ndn, gr_ndn) == 0) {
54                 /* we already have a copy of the entry */
55                 e = target;
56
57                 if( is_entry_objectclass( e, group_oc ) ) {
58                         return(1);
59                 }
60
61                 if ((attr = attr_find(e->e_attrs, group_at)) == NULL)
62                         return(1);
63
64                 bv.bv_val = (char *) op_ndn;
65                 bv.bv_len = strlen( op_ndn );         
66                 if( value_find( group_at, attr->a_vals, &bv ) == 0  )
67                         return(1);
68
69         } else {
70                 filter = ch_malloc(sizeof("(&(objectclass=)(=))")
71                                                         + strlen(group_oc_name)
72                                                         + strlen(group_at_name)
73                                                         + strlen(op_ndn) + 1);
74                 if (filter == NULL)
75                         return(1);
76
77                 if (ldap_initialize(&ld, li->url) != LDAP_SUCCESS) {
78                         ch_free(filter);
79                         return(1);
80                 }
81
82                 if (ldap_bind_s(ld, li->binddn, li->bindpw, LDAP_AUTH_SIMPLE) == LDAP_SUCCESS) {
83                         strcpy(filter, "(&(objectclass=");
84                         strcat(filter, group_oc_name);
85                         strcat(filter, ")(");
86                         strcat(filter, group_at_name);
87                         strcat(filter, "=");
88                         strcat(filter, op_ndn);
89                         strcat(filter, "))");
90
91                         gattr[0] = "objectclass";
92                         gattr[1] = NULL;
93                         if (ldap_search_ext_s(ld, gr_ndn, LDAP_SCOPE_BASE, filter,
94                                                                         gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
95                                                                         LDAP_NO_LIMIT, &result) == LDAP_SUCCESS)
96                         {
97                                 if (ldap_first_entry(ld, result) != NULL)
98                                         rc = 0;
99                                 ldap_msgfree(result);
100                         }
101                 }
102                 ldap_unbind(ld);
103                 ch_free(filter);
104                 return(rc);
105     }
106
107         return(0);
108 }
109