1 /* dyngroup.c - Demonstration of overlay code */
4 * Copyright 2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 * Copyright 2003, Howard Chu, All rights reserved. <hyc@symas.com>
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:
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.
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.
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.
25 * 4. This notice may not be removed or altered.
32 #include <ac/string.h>
33 #include <ac/socket.h>
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".
44 typedef struct adpair {
45 struct adpair *ap_next;
46 AttributeDescription *ap_mem;
47 AttributeDescription *ap_uri;
51 dyngroup_response( Operation *op, SlapReply *rs )
53 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
54 adpair *ap = on->on_bi.bi_private;
56 /* If we've been configured and the current response is
57 * what we're looking for...
59 if ( ap && op->o_tag == LDAP_REQ_COMPARE &&
60 rs->sr_err == LDAP_NO_SUCH_ATTRIBUTE ) {
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.
68 int cache = op->o_do_not_cache;
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;
79 /* Default is to just fall through to the normal processing */
80 return SLAP_CB_CONTINUE;
83 static int dyngroup_config(
91 slap_overinst *on = (slap_overinst *) be->bd_info;
92 adpair ap = { NULL, NULL, NULL }, *a2;
94 if ( strcasecmp( argv[0], "attrpair" ) == 0 ) {
97 Debug( LDAP_DEBUG_ANY,
98 "%s: line %d: attribute description missing in \"attrpair <member-attribute> <URL-attribute>\" line.\n",
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 );
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 );
114 /* The on->on_bi.bi_private pointer can be used for
115 * anything this instance of the overlay needs.
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;
132 slap_overinst *on = (slap_overinst *) be->bd_info;
135 for ( ap = on->on_bi.bi_private; ap; ap = a2 ) {
142 static slap_overinst dyngroup;
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.
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;
155 return overlay_register( &dyngroup );