Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / openldap / dist / contrib / slapd-modules / denyop / denyop.c
blobada25d543e4b270847c938bca8d1577387fbfbe4
1 /* denyop.c - Denies operations */
2 /* $OpenLDAP: pkg/ldap/contrib/slapd-modules/denyop/denyop.c,v 1.2.2.3 2008/02/11 23:26:38 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2004-2008 The OpenLDAP Foundation.
6 * All rights reserved.
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.
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 /* ACKNOWLEDGEMENTS:
17 * This work was initially developed by Pierangelo Masarati for inclusion in
18 * OpenLDAP Software.
21 #include "portable.h"
23 #ifdef SLAPD_OVER_DENYOP
25 #include <stdio.h>
27 #include <ac/string.h>
28 #include <ac/socket.h>
30 #include "slap.h"
32 /* This overlay provides a quick'n'easy way to deny selected operations
33 * for a database whose backend implements the operations. It is intended
34 * to be less expensive than ACLs because its evaluation occurs before
35 * any backend specific operation is actually even initiated.
38 enum {
39 denyop_add = 0,
40 denyop_bind,
41 denyop_compare,
42 denyop_delete,
43 denyop_extended,
44 denyop_modify,
45 denyop_modrdn,
46 denyop_search,
47 denyop_unbind
48 } denyop_e;
50 typedef struct denyop_info {
51 int do_op[denyop_unbind + 1];
52 } denyop_info;
54 static int
55 denyop_func( Operation *op, SlapReply *rs )
57 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
58 denyop_info *oi = (denyop_info *)on->on_bi.bi_private;
59 int deny = 0;
61 switch( op->o_tag ) {
62 case LDAP_REQ_BIND:
63 deny = oi->do_op[denyop_bind];
64 break;
66 case LDAP_REQ_ADD:
67 deny = oi->do_op[denyop_add];
68 break;
70 case LDAP_REQ_DELETE:
71 deny = oi->do_op[denyop_delete];
72 break;
74 case LDAP_REQ_MODRDN:
75 deny = oi->do_op[denyop_modrdn];
76 break;
78 case LDAP_REQ_MODIFY:
79 deny = oi->do_op[denyop_modify];
80 break;
82 case LDAP_REQ_COMPARE:
83 deny = oi->do_op[denyop_compare];
84 break;
86 case LDAP_REQ_SEARCH:
87 deny = oi->do_op[denyop_search];
88 break;
90 case LDAP_REQ_EXTENDED:
91 deny = oi->do_op[denyop_extended];
92 break;
94 case LDAP_REQ_UNBIND:
95 deny = oi->do_op[denyop_unbind];
96 break;
99 if ( !deny ) {
100 return SLAP_CB_CONTINUE;
103 op->o_bd->bd_info = (BackendInfo *)on->on_info;
104 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
105 "operation not allowed within namingContext" );
107 return 0;
110 static int
111 denyop_over_init(
112 BackendDB *be
115 slap_overinst *on = (slap_overinst *) be->bd_info;
116 denyop_info *oi;
118 oi = (denyop_info *)ch_malloc(sizeof(denyop_info));
119 memset(oi, 0, sizeof(denyop_info));
120 on->on_bi.bi_private = oi;
122 return 0;
125 static int
126 denyop_config(
127 BackendDB *be,
128 const char *fname,
129 int lineno,
130 int argc,
131 char **argv
134 slap_overinst *on = (slap_overinst *) be->bd_info;
135 denyop_info *oi = (denyop_info *)on->on_bi.bi_private;
137 if ( strcasecmp( argv[0], "denyop" ) == 0 ) {
138 char *op;
140 if ( argc != 2 ) {
141 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
142 "operation list missing in "
143 "\"denyop <op-list>\" line.\n",
144 fname, lineno, 0 );
145 return( 1 );
148 /* The on->on_bi.bi_private pointer can be used for
149 * anything this instance of the overlay needs.
152 op = argv[1];
153 do {
154 char *next = strchr( op, ',' );
156 if ( next ) {
157 next[0] = '\0';
158 next++;
161 if ( strcmp( op, "add" ) == 0 ) {
162 oi->do_op[denyop_add] = 1;
164 } else if ( strcmp( op, "bind" ) == 0 ) {
165 oi->do_op[denyop_bind] = 1;
167 } else if ( strcmp( op, "compare" ) == 0 ) {
168 oi->do_op[denyop_compare] = 1;
170 } else if ( strcmp( op, "delete" ) == 0 ) {
171 oi->do_op[denyop_delete] = 1;
173 } else if ( strcmp( op, "extended" ) == 0 ) {
174 oi->do_op[denyop_extended] = 1;
176 } else if ( strcmp( op, "modify" ) == 0 ) {
177 oi->do_op[denyop_modify] = 1;
179 } else if ( strcmp( op, "modrdn" ) == 0 ) {
180 oi->do_op[denyop_modrdn] = 1;
182 } else if ( strcmp( op, "search" ) == 0 ) {
183 oi->do_op[denyop_search] = 1;
185 } else if ( strcmp( op, "unbind" ) == 0 ) {
186 oi->do_op[denyop_unbind] = 1;
188 } else {
189 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
190 "unknown operation \"%s\" at "
191 "\"denyop <op-list>\" line.\n",
192 fname, lineno, op );
193 return( 1 );
196 op = next;
197 } while ( op );
199 } else {
200 return SLAP_CONF_UNKNOWN;
202 return 0;
205 static int
206 denyop_destroy(
207 BackendDB *be
210 slap_overinst *on = (slap_overinst *) be->bd_info;
211 denyop_info *oi = (denyop_info *)on->on_bi.bi_private;
213 if ( oi ) {
214 ch_free( oi );
217 return 0;
220 /* This overlay is set up for dynamic loading via moduleload. For static
221 * configuration, you'll need to arrange for the slap_overinst to be
222 * initialized and registered by some other function inside slapd.
225 static slap_overinst denyop;
228 denyop_initialize( void )
230 memset( &denyop, 0, sizeof( slap_overinst ) );
231 denyop.on_bi.bi_type = "denyop";
232 denyop.on_bi.bi_db_init = denyop_over_init;
233 denyop.on_bi.bi_db_config = denyop_config;
234 denyop.on_bi.bi_db_destroy = denyop_destroy;
236 denyop.on_bi.bi_op_bind = denyop_func;
237 denyop.on_bi.bi_op_search = denyop_func;
238 denyop.on_bi.bi_op_compare = denyop_func;
239 denyop.on_bi.bi_op_modify = denyop_func;
240 denyop.on_bi.bi_op_modrdn = denyop_func;
241 denyop.on_bi.bi_op_add = denyop_func;
242 denyop.on_bi.bi_op_delete = denyop_func;
243 denyop.on_bi.bi_extended = denyop_func;
244 denyop.on_bi.bi_op_unbind = denyop_func;
246 denyop.on_response = NULL /* denyop_response */ ;
248 return overlay_register( &denyop );
251 #if SLAPD_OVER_DENYOP == SLAPD_MOD_DYNAMIC
253 init_module( int argc, char *argv[] )
255 return denyop_initialize();
257 #endif /* SLAPD_OVER_DENYOP == SLAPD_MOD_DYNAMIC */
259 #endif /* defined(SLAPD_OVER_DENYOP) */