]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/dyngroup.c
c351065c6538e68b98363f4553ced01d6d815ea8
[openldap] / servers / slapd / overlays / dyngroup.c
1 /* dyngroup.c - Demonstration of overlay code */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7 /*
8  * Copyright 2003, Howard Chu, All rights reserved. <hyc@symas.com>
9  * 
10  * Permission is granted to anyone to use this software for any purpose
11  * on any computer system, and to alter it and redistribute it, subject
12  * to the following restrictions:
13  * 
14  * 1. The author is not responsible for the consequences of use of this
15  *    software, no matter how awful, even if they arise from flaws in it.
16  * 
17  * 2. The origin of this software must not be misrepresented, either by
18  *    explicit claim or by omission.  Since few users ever read sources,
19  *    credits should appear in the documentation.
20  * 
21  * 3. Altered versions must be plainly marked as such, and must not be
22  *    misrepresented as being the original software.  Since few users
23  *    ever read sources, credits should appear in the documentation.
24  * 
25  * 4. This notice may not be removed or altered.
26  */
27
28 #include "portable.h"
29
30 #include <stdio.h>
31
32 #include <ac/string.h>
33 #include <ac/socket.h>
34
35 #include "slap.h"
36
37 /* This overlay extends the Compare operation to detect members of a
38  * dynamic group. It has no effect on any other operations. It must
39  * be configured with a pair of attributes to trigger on, e.g.
40  *      attrpair member memberURL
41  * will cause compares on "member" to trigger a compare on "memberURL".
42  */
43
44 typedef struct adpair {
45         struct adpair *ap_next;
46         AttributeDescription *ap_mem;
47         AttributeDescription *ap_uri;
48 } adpair;
49
50 static int
51 dyngroup_response( Operation *op, SlapReply *rs )
52 {
53         slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
54         adpair *ap = on->on_bi.bi_private;
55
56         /* If we've been configured and the current response is
57          * what we're looking for...
58          */
59         if ( ap && op->o_tag == LDAP_REQ_COMPARE &&
60                 rs->sr_err == LDAP_NO_SUCH_ATTRIBUTE ) {
61
62                 for (;ap;ap=ap->ap_next) {
63                         if ( op->oq_compare.rs_ava->aa_desc == ap->ap_mem ) {
64                                 /* This compare is for one of the attributes we're
65                                  * interested in. We'll use slapd's existing dyngroup
66                                  * evaluator to get the answer we want.
67                                  */
68                                 int cache = op->o_do_not_cache;
69                                 
70                                 op->o_do_not_cache = 1;
71                                 if ( backend_group( op, NULL, &op->o_req_ndn,
72                                         &op->oq_compare.rs_ava->aa_value, NULL, ap->ap_uri ) == 0 )
73                                         rs->sr_err = LDAP_COMPARE_TRUE;
74                                 op->o_do_not_cache = cache;
75                                 break;
76                         }
77                 }
78         }
79         /* Default is to just fall through to the normal processing */
80         return SLAP_CB_CONTINUE;
81 }
82
83 static int dyngroup_config(
84     BackendDB   *be,
85     const char  *fname,
86     int         lineno,
87     int         argc,
88     char        **argv
89 )
90 {
91         slap_overinst *on = (slap_overinst *) be->bd_info;
92         adpair ap = { NULL, NULL, NULL }, *a2;
93
94         if ( strcasecmp( argv[0], "attrpair" ) == 0 ) {
95                 const char *text;
96                 if ( argc != 3 ) {
97                         Debug( LDAP_DEBUG_ANY,
98                 "%s: line %d: attribute description missing in \"attrpair <member-attribute> <URL-attribute>\" line.\n",
99                         fname, lineno, 0 );
100                         return( 1 );
101                 }
102                 if ( slap_str2ad( argv[1], &ap.ap_mem, &text ) ) {
103                         Debug( LDAP_DEBUG_ANY,
104                 "%s: line %d: attribute description unknown \"attrpair\" line: %s.\n",
105                         fname, lineno, text );
106                         return( 1 );
107                 }
108                 if ( slap_str2ad( argv[2], &ap.ap_uri, &text ) ) {
109                         Debug( LDAP_DEBUG_ANY,
110                 "%s: line %d: attribute description unknown \"attrpair\" line: %s.\n",
111                         fname, lineno, text );
112                         return( 1 );
113                 }
114                 /* The on->on_bi.bi_private pointer can be used for
115                  * anything this instance of the overlay needs.
116                  */
117
118                 a2 = ch_malloc( sizeof(adpair) );
119                 a2->ap_next = on->on_bi.bi_private;
120                 a2->ap_mem = ap.ap_mem;
121                 a2->ap_uri = ap.ap_uri;
122                 on->on_bi.bi_private = a2;
123         }
124         return 0;
125 }
126
127 static int
128 dyngroup_close(
129         BackendDB *be
130 )
131 {
132         slap_overinst *on = (slap_overinst *) be->bd_info;
133         adpair *ap, *a2;
134
135         for ( ap = on->on_bi.bi_private; ap; ap = a2 ) {
136                 a2 = ap->ap_next;
137                 ch_free( ap );
138         }
139         return 0;
140 }
141
142 static slap_overinst dyngroup;
143
144 /* This overlay is set up for dynamic loading via moduleload. For static
145  * configuration, you'll need to arrange for the slap_overinst to be
146  * initialized and registered by some other function inside slapd.
147  */
148
149 int init_module(int argc, char *argv[]) {
150         dyngroup.on_bi.bi_type = "dyngroup";
151         dyngroup.on_bi.bi_db_config = dyngroup_config;
152         dyngroup.on_bi.bi_db_close = dyngroup_close;
153         dyngroup.on_response = dyngroup_response;
154
155         return overlay_register( &dyngroup );
156 }