1 /* dyngroup.c - Demonstration of overlay code */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 /* This is an altered version */
18 * Copyright 2003, Howard Chu, All rights reserved. <hyc@symas.com>
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:
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.
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.
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.
35 * 4. This notice may not be removed or altered.
38 * This work was initially developed by Howard Chu for inclusion in
46 #include <ac/string.h>
47 #include <ac/socket.h>
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".
58 typedef struct adpair {
59 struct adpair *ap_next;
60 AttributeDescription *ap_mem;
61 AttributeDescription *ap_uri;
65 dyngroup_response( Operation *op, SlapReply *rs )
67 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
68 adpair *ap = on->on_bi.bi_private;
70 /* If we've been configured and the current response is
71 * what we're looking for...
73 if ( ap && op->o_tag == LDAP_REQ_COMPARE &&
74 rs->sr_err == LDAP_NO_SUCH_ATTRIBUTE ) {
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.
82 int cache = op->o_do_not_cache;
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;
93 /* Default is to just fall through to the normal processing */
94 return SLAP_CB_CONTINUE;
97 static int dyngroup_config(
105 slap_overinst *on = (slap_overinst *) be->bd_info;
106 adpair ap = { NULL, NULL, NULL }, *a2;
108 if ( strcasecmp( argv[0], "attrpair" ) == 0 ) {
111 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
112 "attribute description missing in "
113 "\"attrpair <member-attribute> <URL-attribute>\" line.\n",
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 );
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 );
129 /* The on->on_bi.bi_private pointer can be used for
130 * anything this instance of the overlay needs.
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;
147 slap_overinst *on = (slap_overinst *) be->bd_info;
150 for ( ap = on->on_bi.bi_private; ap; ap = a2 ) {
157 static slap_overinst dyngroup;
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.
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;
170 return overlay_register( &dyngroup );