1 /* config.c - sock backend configuration file routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2007-2017 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>.
17 * This work was initially developed by Brian Candler for inclusion
18 * in OpenLDAP Software. Dynamic config support by Howard Chu.
25 #include <ac/string.h>
26 #include <ac/socket.h>
30 #include "back-sock.h"
32 static ConfigDriver bs_cf_gen;
33 static int sock_over_setup();
34 static slap_response sock_over_response;
43 /* The number of overlay-only config attrs */
44 #define NUM_OV_ATTRS 3
46 static ConfigTable bscfg[] = {
47 { "sockops", "ops", 2, 0, 0, ARG_MAGIC|BS_OPS,
48 bs_cf_gen, "( OLcfgDbAt:7.3 NAME 'olcOvSocketOps' "
49 "DESC 'Operation types to forward' "
50 "EQUALITY caseIgnoreMatch "
51 "SYNTAX OMsDirectoryString )", NULL, NULL },
52 { "sockresps", "resps", 2, 0, 0, ARG_MAGIC|BS_RESP,
53 bs_cf_gen, "( OLcfgDbAt:7.4 NAME 'olcOvSocketResps' "
54 "DESC 'Response types to forward' "
55 "EQUALITY caseIgnoreMatch "
56 "SYNTAX OMsDirectoryString )", NULL, NULL },
57 { "sockdnpat", "regexp", 2, 2, 0, ARG_MAGIC|BS_DNPAT,
58 bs_cf_gen, "( OLcfgDbAt:7.5 NAME 'olcOvSocketDNpat' "
59 "DESC 'DN pattern to match' "
60 "EQUALITY caseIgnoreMatch "
61 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
63 { "socketpath", "pathname", 2, 2, 0, ARG_STRING|ARG_OFFSET,
64 (void *)offsetof(struct sockinfo, si_sockpath),
65 "( OLcfgDbAt:7.1 NAME 'olcDbSocketPath' "
66 "DESC 'Pathname for Unix domain socket' "
67 "EQUALITY caseExactMatch "
68 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
69 { "extensions", "ext", 2, 0, 0, ARG_MAGIC|BS_EXT,
70 bs_cf_gen, "( OLcfgDbAt:7.2 NAME 'olcDbSocketExtensions' "
71 "DESC 'binddn, peername, or ssf' "
72 "EQUALITY caseIgnoreMatch "
73 "SYNTAX OMsDirectoryString )", NULL, NULL },
77 static ConfigOCs bsocs[] = {
79 "NAME 'olcDbSocketConfig' "
80 "DESC 'Socket backend configuration' "
81 "SUP olcDatabaseConfig "
82 "MUST olcDbSocketPath "
83 "MAY olcDbSocketExtensions )",
84 Cft_Database, bscfg+NUM_OV_ATTRS },
88 static ConfigOCs osocs[] = {
90 "NAME 'olcOvSocketConfig' "
91 "DESC 'Socket overlay configuration' "
92 "SUP olcOverlayConfig "
93 "MUST olcDbSocketPath "
94 "MAY ( olcDbSocketExtensions $ "
95 " olcOvSocketOps $ olcOvSocketResps $ "
96 " olcOvSocketDNpat ) )",
101 #define SOCK_OP_BIND 0x001
102 #define SOCK_OP_UNBIND 0x002
103 #define SOCK_OP_SEARCH 0x004
104 #define SOCK_OP_COMPARE 0x008
105 #define SOCK_OP_MODIFY 0x010
106 #define SOCK_OP_MODRDN 0x020
107 #define SOCK_OP_ADD 0x040
108 #define SOCK_OP_DELETE 0x080
110 #define SOCK_REP_RESULT 0x001
111 #define SOCK_REP_SEARCH 0x002
113 static slap_verbmasks bs_exts[] = {
114 { BER_BVC("binddn"), SOCK_EXT_BINDDN },
115 { BER_BVC("peername"), SOCK_EXT_PEERNAME },
116 { BER_BVC("ssf"), SOCK_EXT_SSF },
117 { BER_BVC("connid"), SOCK_EXT_CONNID },
121 static slap_verbmasks ov_ops[] = {
122 { BER_BVC("bind"), SOCK_OP_BIND },
123 { BER_BVC("unbind"), SOCK_OP_UNBIND },
124 { BER_BVC("search"), SOCK_OP_SEARCH },
125 { BER_BVC("compare"), SOCK_OP_COMPARE },
126 { BER_BVC("modify"), SOCK_OP_MODIFY },
127 { BER_BVC("modrdn"), SOCK_OP_MODRDN },
128 { BER_BVC("add"), SOCK_OP_ADD },
129 { BER_BVC("delete"), SOCK_OP_DELETE },
133 static slap_verbmasks ov_resps[] = {
134 { BER_BVC("result"), SOCK_REP_RESULT },
135 { BER_BVC("search"), SOCK_REP_SEARCH },
140 bs_cf_gen( ConfigArgs *c )
145 if ( c->be && c->table == Cft_Database )
146 si = c->be->be_private;
148 si = c->bi->bi_private;
152 if ( c->op == SLAP_CONFIG_EMIT ) {
155 return mask_to_verbs( bs_exts, si->si_extensions, &c->rvalue_vals );
157 return mask_to_verbs( ov_ops, si->si_ops, &c->rvalue_vals );
159 return mask_to_verbs( ov_resps, si->si_resps, &c->rvalue_vals );
161 value_add_one( &c->rvalue_vals, &si->si_dnpatstr );
164 } else if ( c->op == LDAP_MOD_DELETE ) {
168 si->si_extensions = 0;
171 slap_mask_t dels = 0;
172 rc = verbs_to_mask( c->argc, c->argv, bs_exts, &dels );
174 si->si_extensions ^= dels;
182 slap_mask_t dels = 0;
183 rc = verbs_to_mask( c->argc, c->argv, ov_ops, &dels );
193 slap_mask_t dels = 0;
194 rc = verbs_to_mask( c->argc, c->argv, ov_resps, &dels );
196 si->si_resps ^= dels;
200 regfree( &si->si_dnpat );
201 ch_free( si->si_dnpatstr.bv_val );
202 BER_BVZERO( &si->si_dnpatstr );
209 return verbs_to_mask( c->argc, c->argv, bs_exts, &si->si_extensions );
211 return verbs_to_mask( c->argc, c->argv, ov_ops, &si->si_ops );
213 return verbs_to_mask( c->argc, c->argv, ov_resps, &si->si_resps );
215 if ( !regcomp( &si->si_dnpat, c->argv[1], REG_EXTENDED|REG_ICASE|REG_NOSUB )) {
216 ber_str2bv( c->argv[1], 0, 1, &si->si_dnpatstr );
227 sock_back_init_cf( BackendInfo *bi )
230 bi->bi_cf_ocs = bsocs;
232 rc = config_register_schema( bscfg, bsocs );
234 rc = sock_over_setup();
238 /* sock overlay wrapper */
239 static slap_overinst sockover;
241 static int sock_over_db_init( Backend *be, struct config_reply_s *cr );
242 static int sock_over_db_destroy( Backend *be, struct config_reply_s *cr );
244 static BI_op_bind *sockfuncs[] = {
255 static const int sockopflags[] = {
266 static int sock_over_op(
271 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
272 void *private = op->o_bd->be_private;
275 slap_operation_t which;
278 case LDAP_REQ_BIND: which = op_bind; break;
279 case LDAP_REQ_UNBIND: which = op_unbind; break;
280 case LDAP_REQ_SEARCH: which = op_search; break;
281 case LDAP_REQ_COMPARE: which = op_compare; break;
282 case LDAP_REQ_MODIFY: which = op_modify; break;
283 case LDAP_REQ_MODRDN: which = op_modrdn; break;
284 case LDAP_REQ_ADD: which = op_add; break;
285 case LDAP_REQ_DELETE: which = op_delete; break;
287 return SLAP_CB_CONTINUE;
289 si = on->on_bi.bi_private;
290 if ( !(si->si_ops & sockopflags[which]))
291 return SLAP_CB_CONTINUE;
293 if ( !BER_BVISEMPTY( &si->si_dnpatstr ) &&
294 regexec( &si->si_dnpat, op->o_req_ndn.bv_val, 0, NULL, 0 ))
295 return SLAP_CB_CONTINUE;
297 op->o_bd->be_private = si;
299 op->o_callback = NULL;
300 sockfuncs[which]( op, rs );
301 op->o_bd->be_private = private;
307 sock_over_response( Operation *op, SlapReply *rs )
309 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
310 struct sockinfo *si = (struct sockinfo *)on->on_bi.bi_private;
313 if ( rs->sr_type == REP_RESULT ) {
314 if ( !( si->si_resps & SOCK_REP_RESULT ))
315 return SLAP_CB_CONTINUE;
316 } else if ( rs->sr_type == REP_SEARCH ) {
317 if ( !( si->si_resps & SOCK_REP_SEARCH ))
318 return SLAP_CB_CONTINUE;
320 return SLAP_CB_CONTINUE;
322 if (( fp = opensock( si->si_sockpath )) == NULL )
323 return SLAP_CB_CONTINUE;
325 if ( rs->sr_type == REP_RESULT ) {
326 /* write out the result */
327 fprintf( fp, "RESULT\n" );
328 fprintf( fp, "msgid: %ld\n", (long) op->o_msgid );
329 sock_print_conn( fp, op->o_conn, si );
330 fprintf( fp, "code: %d\n", rs->sr_err );
331 if ( rs->sr_matched )
332 fprintf( fp, "matched: %s\n", rs->sr_matched );
334 fprintf( fp, "info: %s\n", rs->sr_text );
336 /* write out the search entry */
338 fprintf( fp, "ENTRY\n" );
339 fprintf( fp, "msgid: %ld\n", (long) op->o_msgid );
340 sock_print_conn( fp, op->o_conn, si );
341 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
342 fprintf( fp, "%s", entry2str( rs->sr_entry, &len ) );
343 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
348 return SLAP_CB_CONTINUE;
356 sockover.on_bi.bi_type = "sock";
357 sockover.on_bi.bi_db_init = sock_over_db_init;
358 sockover.on_bi.bi_db_destroy = sock_over_db_destroy;
360 sockover.on_bi.bi_op_bind = sock_over_op;
361 sockover.on_bi.bi_op_unbind = sock_over_op;
362 sockover.on_bi.bi_op_search = sock_over_op;
363 sockover.on_bi.bi_op_compare = sock_over_op;
364 sockover.on_bi.bi_op_modify = sock_over_op;
365 sockover.on_bi.bi_op_modrdn = sock_over_op;
366 sockover.on_bi.bi_op_add = sock_over_op;
367 sockover.on_bi.bi_op_delete = sock_over_op;
368 sockover.on_response = sock_over_response;
370 sockover.on_bi.bi_cf_ocs = osocs;
372 rc = config_register_schema( bscfg, osocs );
375 return overlay_register( &sockover );
381 struct config_reply_s *cr
384 slap_overinst *on = (slap_overinst *)be->bd_info;
385 void *private = be->be_private;
386 void *cf_ocs = be->be_cf_ocs;
389 be->be_private = NULL;
390 rc = sock_back_db_init( be, cr );
391 on->on_bi.bi_private = be->be_private;
392 be->be_private = private;
393 be->be_cf_ocs = cf_ocs;
398 sock_over_db_destroy(
400 struct config_reply_s *cr
403 slap_overinst *on = (slap_overinst *)be->bd_info;
404 void *private = be->be_private;
407 be->be_private = on->on_bi.bi_private;
408 rc = sock_back_db_destroy( be, cr );
409 on->on_bi.bi_private = be->be_private;
410 be->be_private = private;