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