1 /* dyngroup.c - Demonstration of overlay code */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2005 The OpenLDAP Foundation.
6 * Copyright 2003 by Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Howard Chu for inclusion in
24 #ifdef SLAPD_OVER_DYNGROUP
28 #include <ac/string.h>
29 #include <ac/socket.h>
33 /* This overlay extends the Compare operation to detect members of a
34 * dynamic group. It has no effect on any other operations. It must
35 * be configured with a pair of attributes to trigger on, e.g.
36 * attrpair member memberURL
37 * will cause compares on "member" to trigger a compare on "memberURL".
40 typedef struct adpair {
41 struct adpair *ap_next;
42 AttributeDescription *ap_mem;
43 AttributeDescription *ap_uri;
47 dyngroup_response( Operation *op, SlapReply *rs )
49 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
50 adpair *ap = on->on_bi.bi_private;
52 /* If we've been configured and the current response is
53 * what we're looking for...
55 if ( ap && op->o_tag == LDAP_REQ_COMPARE &&
56 rs->sr_err == LDAP_NO_SUCH_ATTRIBUTE ) {
58 for (;ap;ap=ap->ap_next) {
59 if ( op->oq_compare.rs_ava->aa_desc == ap->ap_mem ) {
60 /* This compare is for one of the attributes we're
61 * interested in. We'll use slapd's existing dyngroup
62 * evaluator to get the answer we want.
64 int cache = op->o_do_not_cache;
66 op->o_do_not_cache = 1;
67 rs->sr_err = backend_group( op, NULL, &op->o_req_ndn,
68 &op->oq_compare.rs_ava->aa_value, NULL, ap->ap_uri );
69 op->o_do_not_cache = cache;
70 if ( rs->sr_err == LDAP_SUCCESS )
71 rs->sr_err = LDAP_COMPARE_TRUE;
76 /* Default is to just fall through to the normal processing */
77 return SLAP_CB_CONTINUE;
80 static int dyngroup_config(
88 slap_overinst *on = (slap_overinst *) be->bd_info;
89 adpair ap = { NULL, NULL, NULL }, *a2;
91 if ( strcasecmp( argv[0], "attrpair" ) == 0 ) {
94 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
95 "attribute description missing in "
96 "\"attrpair <member-attribute> <URL-attribute>\" line.\n",
100 if ( slap_str2ad( argv[1], &ap.ap_mem, &text ) ) {
101 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
102 "attribute description unknown \"attrpair\" line: %s.\n",
103 fname, lineno, text );
106 if ( slap_str2ad( argv[2], &ap.ap_uri, &text ) ) {
107 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
108 "attribute description unknown \"attrpair\" line: %s.\n",
109 fname, lineno, text );
112 /* The on->on_bi.bi_private pointer can be used for
113 * anything this instance of the overlay needs.
116 a2 = ch_malloc( sizeof(adpair) );
117 a2->ap_next = on->on_bi.bi_private;
118 a2->ap_mem = ap.ap_mem;
119 a2->ap_uri = ap.ap_uri;
120 on->on_bi.bi_private = a2;
122 return SLAP_CONF_UNKNOWN;
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 dyngroup_init() {
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 );
158 #if SLAPD_OVER_DYNGROUP == SLAPD_MOD_DYNAMIC
159 int init_module(int argc, char *argv[]) {
160 return dyngroup_init();
164 #endif /* defined(SLAPD_OVER_DYNGROUP) */