| 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-2024 The OpenLDAP Foundation. |
| 6 |
* Copyright 2003 by Howard Chu. |
| 7 |
* All rights reserved. |
| 8 |
* |
| 9 |
* Redistribution and use in source and binary forms, with or without |
| 10 |
* modification, are permitted only as authorized by the OpenLDAP |
| 11 |
* Public License. |
| 12 |
* |
| 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>. |
| 16 |
*/ |
| 17 |
/* ACKNOWLEDGEMENTS: |
| 18 |
* This work was initially developed by Howard Chu for inclusion in |
| 19 |
* OpenLDAP Software. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "portable.h" |
| 23 |
|
| 24 |
#ifdef SLAPD_OVER_DYNGROUP |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
|
| 28 |
#include <ac/string.h> |
| 29 |
#include <ac/socket.h> |
| 30 |
|
| 31 |
#include "lutil.h" |
| 32 |
#include "slap.h" |
| 33 |
#include "slap-config.h" |
| 34 |
|
| 35 |
/* This overlay extends the Compare operation to detect members of a |
| 36 |
* dynamic group. It has no effect on any other operations. It must |
| 37 |
* be configured with a pair of attributes to trigger on, e.g. |
| 38 |
* attrpair member memberURL |
| 39 |
* will cause compares on "member" to trigger a compare on "memberURL". |
| 40 |
*/ |
| 41 |
|
| 42 |
typedef struct adpair { |
| 43 |
struct adpair *ap_next; |
| 44 |
AttributeDescription *ap_mem; |
| 45 |
AttributeDescription *ap_uri; |
| 46 |
} adpair; |
| 47 |
|
| 48 |
static int dgroup_cf( ConfigArgs *c ) |
| 49 |
{ |
| 50 |
slap_overinst *on = (slap_overinst *)c->bi; |
| 51 |
int rc = 1; |
| 52 |
|
| 53 |
switch( c->op ) { |
| 54 |
case SLAP_CONFIG_EMIT: |
| 55 |
{ |
| 56 |
adpair *ap; |
| 57 |
for ( ap = on->on_bi.bi_private; ap; ap = ap->ap_next ) { |
| 58 |
struct berval bv; |
| 59 |
char *ptr; |
| 60 |
bv.bv_len = ap->ap_mem->ad_cname.bv_len + 1 + |
| 61 |
ap->ap_uri->ad_cname.bv_len; |
| 62 |
bv.bv_val = ch_malloc( bv.bv_len + 1 ); |
| 63 |
ptr = lutil_strcopy( bv.bv_val, ap->ap_mem->ad_cname.bv_val ); |
| 64 |
*ptr++ = ' '; |
| 65 |
strcpy( ptr, ap->ap_uri->ad_cname.bv_val ); |
| 66 |
ber_bvarray_add( &c->rvalue_vals, &bv ); |
| 67 |
rc = 0; |
| 68 |
} |
| 69 |
} |
| 70 |
break; |
| 71 |
case LDAP_MOD_DELETE: |
| 72 |
if ( c->valx == -1 ) { |
| 73 |
adpair *ap; |
| 74 |
while (( ap = on->on_bi.bi_private )) { |
| 75 |
on->on_bi.bi_private = ap->ap_next; |
| 76 |
ch_free( ap ); |
| 77 |
} |
| 78 |
} else { |
| 79 |
adpair **app, *ap; |
| 80 |
int i; |
| 81 |
app = (adpair **)&on->on_bi.bi_private; |
| 82 |
for (i=0; i<=c->valx; i++, app = &ap->ap_next) { |
| 83 |
ap = *app; |
| 84 |
} |
| 85 |
*app = ap->ap_next; |
| 86 |
ch_free( ap ); |
| 87 |
} |
| 88 |
rc = 0; |
| 89 |
break; |
| 90 |
case SLAP_CONFIG_ADD: |
| 91 |
case LDAP_MOD_ADD: |
| 92 |
{ |
| 93 |
adpair ap = { NULL, NULL, NULL }, **app, *a2; |
| 94 |
const char *text; |
| 95 |
if ( slap_str2ad( c->argv[1], &ap.ap_mem, &text ) ) { |
| 96 |
snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s attribute description unknown: \"%s\"", |
| 97 |
c->argv[0], c->argv[1] ); |
| 98 |
Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, |
| 99 |
"%s: %s\n", c->log, c->cr_msg ); |
| 100 |
return ARG_BAD_CONF; |
| 101 |
} |
| 102 |
if ( slap_str2ad( c->argv[2], &ap.ap_uri, &text ) ) { |
| 103 |
snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s attribute description unknown: \"%s\"", |
| 104 |
c->argv[0], c->argv[2] ); |
| 105 |
Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE, |
| 106 |
"%s: %s\n", c->log, c->cr_msg ); |
| 107 |
return ARG_BAD_CONF; |
| 108 |
} |
| 109 |
/* The on->on_bi.bi_private pointer can be used for |
| 110 |
* anything this instance of the overlay needs. |
| 111 |
*/ |
| 112 |
a2 = ch_malloc( sizeof(adpair) ); |
| 113 |
|
| 114 |
for ( app = (adpair **)&on->on_bi.bi_private; *app; app = &(*app)->ap_next ) |
| 115 |
/* Get to the end */ ; |
| 116 |
|
| 117 |
a2->ap_mem = ap.ap_mem; |
| 118 |
a2->ap_uri = ap.ap_uri; |
| 119 |
a2->ap_next = *app; |
| 120 |
*app = a2; |
| 121 |
rc = 0; |
| 122 |
} |
| 123 |
} |
| 124 |
return rc; |
| 125 |
} |
| 126 |
|
| 127 |
static ConfigTable dgroupcfg[] = { |
| 128 |
{ "attrpair", "member-attribute> <URL-attribute", 3, 3, 0, |
| 129 |
ARG_MAGIC, dgroup_cf, |
| 130 |
"( OLcfgOvAt:17.1 NAME ( 'olcDynGroupAttrPair' 'olcDGAttrPair' ) " |
| 131 |
"EQUALITY caseIgnoreMatch " |
| 132 |
"DESC 'Member and MemberURL attribute pair' " |
| 133 |
"SYNTAX OMsDirectoryString )", NULL, NULL }, |
| 134 |
{ NULL, NULL, 0, 0, 0, ARG_IGNORED } |
| 135 |
}; |
| 136 |
|
| 137 |
static ConfigOCs dgroupocs[] = { |
| 138 |
{ "( OLcfgOvOc:17.1 " |
| 139 |
"NAME ( 'olcDynGroupConfig' 'olcDGConfig' ) " |
| 140 |
"DESC 'Dynamic Group configuration' " |
| 141 |
"SUP olcOverlayConfig " |
| 142 |
"MAY olcDynGroupAttrPair)", |
| 143 |
Cft_Overlay, dgroupcfg }, |
| 144 |
{ NULL, 0, NULL } |
| 145 |
}; |
| 146 |
|
| 147 |
static int |
| 148 |
dyngroup_response( Operation *op, SlapReply *rs ) |
| 149 |
{ |
| 150 |
slap_overinst *on = (slap_overinst *) op->o_bd->bd_info; |
| 151 |
adpair *ap = on->on_bi.bi_private; |
| 152 |
|
| 153 |
/* If we've been configured and the current response is |
| 154 |
* what we're looking for... |
| 155 |
*/ |
| 156 |
if ( ap && op->o_tag == LDAP_REQ_COMPARE && |
| 157 |
rs->sr_err == LDAP_NO_SUCH_ATTRIBUTE ) { |
| 158 |
|
| 159 |
for (;ap;ap=ap->ap_next) { |
| 160 |
if ( op->oq_compare.rs_ava->aa_desc == ap->ap_mem ) { |
| 161 |
/* This compare is for one of the attributes we're |
| 162 |
* interested in. We'll use slapd's existing dyngroup |
| 163 |
* evaluator to get the answer we want. |
| 164 |
*/ |
| 165 |
int cache = op->o_do_not_cache; |
| 166 |
|
| 167 |
op->o_do_not_cache = 1; |
| 168 |
rs->sr_err = backend_group( op, NULL, &op->o_req_ndn, |
| 169 |
&op->oq_compare.rs_ava->aa_value, NULL, ap->ap_uri ); |
| 170 |
op->o_do_not_cache = cache; |
| 171 |
switch ( rs->sr_err ) { |
| 172 |
case LDAP_SUCCESS: |
| 173 |
rs->sr_err = LDAP_COMPARE_TRUE; |
| 174 |
break; |
| 175 |
|
| 176 |
case LDAP_NO_SUCH_OBJECT: |
| 177 |
rs->sr_err = LDAP_COMPARE_FALSE; |
| 178 |
break; |
| 179 |
} |
| 180 |
break; |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
/* Default is to just fall through to the normal processing */ |
| 185 |
return SLAP_CB_CONTINUE; |
| 186 |
} |
| 187 |
|
| 188 |
static int |
| 189 |
dyngroup_destroy( |
| 190 |
BackendDB *be, |
| 191 |
ConfigReply *cr |
| 192 |
) |
| 193 |
{ |
| 194 |
slap_overinst *on = (slap_overinst *) be->bd_info; |
| 195 |
adpair *ap, *a2; |
| 196 |
|
| 197 |
for ( ap = on->on_bi.bi_private; ap; ap = a2 ) { |
| 198 |
a2 = ap->ap_next; |
| 199 |
ch_free( ap ); |
| 200 |
} |
| 201 |
return 0; |
| 202 |
} |
| 203 |
|
| 204 |
static slap_overinst dyngroup; |
| 205 |
|
| 206 |
/* This overlay is set up for dynamic loading via moduleload. For static |
| 207 |
* configuration, you'll need to arrange for the slap_overinst to be |
| 208 |
* initialized and registered by some other function inside slapd. |
| 209 |
*/ |
| 210 |
|
| 211 |
int dyngroup_initialize() { |
| 212 |
int code; |
| 213 |
|
| 214 |
dyngroup.on_bi.bi_type = "dyngroup"; |
| 215 |
dyngroup.on_bi.bi_flags = SLAPO_BFLAG_SINGLE; |
| 216 |
dyngroup.on_bi.bi_db_destroy = dyngroup_destroy; |
| 217 |
dyngroup.on_response = dyngroup_response; |
| 218 |
|
| 219 |
dyngroup.on_bi.bi_cf_ocs = dgroupocs; |
| 220 |
code = config_register_schema( dgroupcfg, dgroupocs ); |
| 221 |
if ( code ) return code; |
| 222 |
|
| 223 |
return overlay_register( &dyngroup ); |
| 224 |
} |
| 225 |
|
| 226 |
#if SLAPD_OVER_DYNGROUP == SLAPD_MOD_DYNAMIC |
| 227 |
int |
| 228 |
init_module( int argc, char *argv[] ) |
| 229 |
{ |
| 230 |
return dyngroup_initialize(); |
| 231 |
} |
| 232 |
#endif |
| 233 |
|
| 234 |
#endif /* defined(SLAPD_OVER_DYNGROUP) */ |