1 /* retcode.c - customizable response for client testing purposes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005-2008 The OpenLDAP Foundation.
6 * Portions Copyright 2005 Pierangelo Masarati <ando@sys-net.it>
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 Pierangelo Masarati for inclusion
19 * in OpenLDAP Software.
24 #ifdef SLAPD_OVER_RETCODE
28 #include <ac/unistd.h>
29 #include <ac/string.h>
31 #include <ac/socket.h>
38 static slap_overinst retcode;
40 static AttributeDescription *ad_errCode;
41 static AttributeDescription *ad_errText;
42 static AttributeDescription *ad_errOp;
43 static AttributeDescription *ad_errSleepTime;
44 static AttributeDescription *ad_errMatchedDN;
45 static AttributeDescription *ad_errUnsolicitedOID;
46 static AttributeDescription *ad_errUnsolicitedData;
47 static AttributeDescription *ad_errDisconnect;
49 static ObjectClass *oc_errAbsObject;
50 static ObjectClass *oc_errObject;
51 static ObjectClass *oc_errAuxObject;
53 typedef enum retcode_op_e {
54 SN_DG_OP_NONE = 0x0000,
55 SN_DG_OP_ADD = 0x0001,
56 SN_DG_OP_BIND = 0x0002,
57 SN_DG_OP_COMPARE = 0x0004,
58 SN_DG_OP_DELETE = 0x0008,
59 SN_DG_OP_MODIFY = 0x0010,
60 SN_DG_OP_RENAME = 0x0020,
61 SN_DG_OP_SEARCH = 0x0040,
62 SN_DG_EXTENDED = 0x0080,
63 SN_DG_OP_AUTH = SN_DG_OP_BIND,
64 SN_DG_OP_READ = (SN_DG_OP_COMPARE|SN_DG_OP_SEARCH),
65 SN_DG_OP_WRITE = (SN_DG_OP_ADD|SN_DG_OP_DELETE|SN_DG_OP_MODIFY|SN_DG_OP_RENAME),
66 SN_DG_OP_ALL = (SN_DG_OP_AUTH|SN_DG_OP_READ|SN_DG_OP_WRITE|SN_DG_EXTENDED)
69 typedef struct retcode_item_t {
70 struct berval rdi_line;
72 struct berval rdi_ndn;
73 struct berval rdi_text;
74 struct berval rdi_matched;
80 struct berval rdi_unsolicited_oid;
81 struct berval rdi_unsolicited_data;
84 #define RDI_PRE_DISCONNECT (0x1U)
85 #define RDI_POST_DISCONNECT (0x2U)
87 struct retcode_item_t *rdi_next;
90 typedef struct retcode_t {
92 struct berval rd_npdn;
96 retcode_item_t *rd_item;
99 #define RETCODE_FINDIR 0x01
100 #define RETCODE_INDIR( rd ) ( (rd)->rd_indir )
104 retcode_entry_response( Operation *op, SlapReply *rs, BackendInfo *bi, Entry *e );
107 retcode_sleep( int s )
111 /* sleep as required */
113 #if 0 /* use high-order bits for better randomness (Numerical Recipes in "C") */
116 r = ((double)(-s))*rand()/(RAND_MAX + 1.0);
117 } else if ( s > 0 ) {
128 retcode_cleanup_cb( Operation *op, SlapReply *rs )
130 rs->sr_matched = NULL;
133 if ( rs->sr_ref != NULL ) {
134 ber_bvarray_free( rs->sr_ref );
138 ch_free( op->o_callback );
139 op->o_callback = NULL;
141 return SLAP_CB_CONTINUE;
145 retcode_send_onelevel( Operation *op, SlapReply *rs )
147 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
148 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
152 for ( rdi = rd->rd_item; rdi != NULL; rdi = rdi->rdi_next ) {
153 if ( op->o_abandon ) {
154 return rs->sr_err = SLAPD_ABANDON;
157 rs->sr_err = test_filter( op, &rdi->rdi_e, op->ors_filter );
158 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
160 rs->sr_attrs = op->ors_attrs;
161 rs->sr_operational_attrs = NULL;
164 rs->sr_err = LDAP_SUCCESS;
165 rs->sr_entry = &rdi->rdi_e;
167 rs->sr_err = send_search_entry( op, rs );
170 switch ( rs->sr_err ) {
171 case LDAP_UNAVAILABLE: /* connection closed */
172 rs->sr_err = LDAP_OTHER;
174 case LDAP_SIZELIMIT_EXCEEDED:
178 rs->sr_err = LDAP_SUCCESS;
183 send_ldap_result( op, rs );
189 retcode_op_add( Operation *op, SlapReply *rs )
191 return retcode_entry_response( op, rs, NULL, op->ora_e );
194 typedef struct retcode_cb_t {
195 BackendInfo *rdc_info;
198 AttributeName *rdc_attrs;
202 retcode_cb_response( Operation *op, SlapReply *rs )
204 retcode_cb_t *rdc = (retcode_cb_t *)op->o_callback->sc_private;
206 op->o_tag = rdc->rdc_tag;
207 if ( rs->sr_type == REP_SEARCH ) {
208 ber_tag_t o_tag = op->o_tag;
211 if ( op->o_tag == LDAP_REQ_SEARCH ) {
212 rs->sr_attrs = rdc->rdc_attrs;
214 rc = retcode_entry_response( op, rs, rdc->rdc_info, rs->sr_entry );
220 switch ( rs->sr_err ) {
222 case LDAP_NO_SUCH_OBJECT:
223 /* in case of noSuchObject, stop the internal search
224 * for in-directory error stuff */
225 if ( !op->o_abandon ) {
226 rdc->rdc_flags = SLAP_CB_CONTINUE;
231 return SLAP_CB_CONTINUE;
235 retcode_op_internal( Operation *op, SlapReply *rs )
237 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
240 BackendDB db = *op->o_bd;
241 slap_callback sc = { 0 };
246 op2.o_tag = LDAP_REQ_SEARCH;
247 op2.ors_scope = LDAP_SCOPE_BASE;
248 op2.ors_deref = LDAP_DEREF_NEVER;
249 op2.ors_tlimit = SLAP_NO_LIMIT;
250 op2.ors_slimit = SLAP_NO_LIMIT;
251 op2.ors_limit = NULL;
252 op2.ors_attrsonly = 0;
253 op2.ors_attrs = slap_anlist_all_attributes;
255 ber_str2bv_x( "(objectClass=errAbsObject)",
256 STRLENOF( "(objectClass=errAbsObject)" ),
257 1, &op2.ors_filterstr, op2.o_tmpmemctx );
258 op2.ors_filter = str2filter_x( &op2, op2.ors_filterstr.bv_val );
260 db.bd_info = on->on_info->oi_orig;
263 rdc.rdc_info = on->on_info->oi_orig;
264 rdc.rdc_flags = RETCODE_FINDIR;
265 if ( op->o_tag == LDAP_REQ_SEARCH ) {
266 rdc.rdc_attrs = op->ors_attrs;
268 rdc.rdc_tag = op->o_tag;
269 sc.sc_response = retcode_cb_response;
270 sc.sc_private = &rdc;
271 op2.o_callback = ≻
273 rc = op2.o_bd->be_search( &op2, rs );
274 op->o_abandon = op2.o_abandon;
276 filter_free_x( &op2, op2.ors_filter, 1 );
277 ber_memfree_x( op2.ors_filterstr.bv_val, op2.o_tmpmemctx );
279 if ( rdc.rdc_flags == SLAP_CB_CONTINUE ) {
280 return SLAP_CB_CONTINUE;
287 retcode_op_func( Operation *op, SlapReply *rs )
289 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
290 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
293 struct berval nrdn, npdn;
295 slap_callback *cb = NULL;
297 /* sleep as required */
298 retcode_sleep( rd->rd_sleep );
300 if ( !dnIsSuffix( &op->o_req_ndn, &rd->rd_npdn ) ) {
301 if ( RETCODE_INDIR( rd ) ) {
302 switch ( op->o_tag ) {
304 return retcode_op_add( op, rs );
308 /* FIXME: better give the db a chance? */
309 if ( be_isroot_pw( op ) ) {
312 return retcode_op_internal( op, rs );
314 case LDAP_REQ_SEARCH:
315 if ( op->ors_scope == LDAP_SCOPE_BASE ) {
316 rs->sr_err = retcode_op_internal( op, rs );
317 switch ( rs->sr_err ) {
318 case SLAP_CB_CONTINUE:
319 if ( rs->sr_nentries == 0 ) {
322 rs->sr_err = LDAP_SUCCESS;
326 send_ldap_result( op, rs );
333 case LDAP_REQ_MODIFY:
334 case LDAP_REQ_DELETE:
335 case LDAP_REQ_MODRDN:
336 case LDAP_REQ_COMPARE:
337 return retcode_op_internal( op, rs );
341 return SLAP_CB_CONTINUE;
344 if ( op->o_tag == LDAP_REQ_SEARCH
345 && op->ors_scope != LDAP_SCOPE_BASE
346 && op->o_req_ndn.bv_len == rd->rd_npdn.bv_len )
348 return retcode_send_onelevel( op, rs );
351 dnParent( &op->o_req_ndn, &npdn );
352 if ( npdn.bv_len != rd->rd_npdn.bv_len ) {
353 rs->sr_err = LDAP_NO_SUCH_OBJECT;
354 rs->sr_matched = rd->rd_pdn.bv_val;
355 send_ldap_result( op, rs );
356 rs->sr_matched = NULL;
360 dnRdn( &op->o_req_ndn, &nrdn );
362 for ( rdi = rd->rd_item; rdi != NULL; rdi = rdi->rdi_next ) {
363 struct berval rdi_nrdn;
365 dnRdn( &rdi->rdi_ndn, &rdi_nrdn );
366 if ( dn_match( &nrdn, &rdi_nrdn ) ) {
371 if ( rdi != NULL && rdi->rdi_mask != SN_DG_OP_ALL ) {
372 retcode_op_e o_tag = SN_DG_OP_NONE;
374 switch ( op->o_tag ) {
376 o_tag = SN_DG_OP_ADD;
380 o_tag = SN_DG_OP_BIND;
383 case LDAP_REQ_COMPARE:
384 o_tag = SN_DG_OP_COMPARE;
387 case LDAP_REQ_DELETE:
388 o_tag = SN_DG_OP_DELETE;
391 case LDAP_REQ_MODIFY:
392 o_tag = SN_DG_OP_MODIFY;
395 case LDAP_REQ_MODRDN:
396 o_tag = SN_DG_OP_RENAME;
399 case LDAP_REQ_SEARCH:
400 o_tag = SN_DG_OP_SEARCH;
403 case LDAP_REQ_EXTENDED:
404 o_tag = SN_DG_EXTENDED;
408 /* Should not happen */
412 if ( !( o_tag & rdi->rdi_mask ) ) {
413 return SLAP_CB_CONTINUE;
418 rs->sr_matched = rd->rd_pdn.bv_val;
419 rs->sr_err = LDAP_NO_SUCH_OBJECT;
420 rs->sr_text = "retcode not found";
423 if ( rdi->rdi_flags & RDI_PRE_DISCONNECT ) {
424 return rs->sr_err = SLAPD_DISCONNECT;
427 rs->sr_err = rdi->rdi_err;
428 rs->sr_text = rdi->rdi_text.bv_val;
429 rs->sr_matched = rdi->rdi_matched.bv_val;
431 /* FIXME: we only honor the rdi_ref field in case rdi_err
432 * is LDAP_REFERRAL otherwise send_ldap_result() bails out */
433 if ( rs->sr_err == LDAP_REFERRAL ) {
436 if ( rdi->rdi_ref != NULL ) {
439 ref = default_referral;
443 rs->sr_ref = referral_rewrite( ref,
444 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
447 rs->sr_err = LDAP_OTHER;
448 rs->sr_text = "bad referral object";
452 retcode_sleep( rdi->rdi_sleeptime );
455 switch ( op->o_tag ) {
456 case LDAP_REQ_EXTENDED:
460 cb = ( slap_callback * )ch_malloc( sizeof( slap_callback ) );
461 memset( cb, 0, sizeof( slap_callback ) );
462 cb->sc_cleanup = retcode_cleanup_cb;
467 if ( rdi && !BER_BVISNULL( &rdi->rdi_unsolicited_oid ) ) {
468 ber_int_t msgid = op->o_msgid;
470 /* RFC 4511 unsolicited response */
473 if ( strcmp( rdi->rdi_unsolicited_oid.bv_val, "0" ) == 0 ) {
474 send_ldap_result( op, rs );
477 ber_tag_t tag = op->o_tag;
479 op->o_tag = LDAP_REQ_EXTENDED;
480 rs->sr_rspoid = rdi->rdi_unsolicited_oid.bv_val;
481 if ( !BER_BVISNULL( &rdi->rdi_unsolicited_data ) ) {
482 rs->sr_rspdata = &rdi->rdi_unsolicited_data;
484 send_ldap_extended( op, rs );
485 rs->sr_rspoid = NULL;
486 rs->sr_rspdata = NULL;
493 send_ldap_result( op, rs );
496 if ( rs->sr_ref != NULL ) {
497 ber_bvarray_free( rs->sr_ref );
500 rs->sr_matched = NULL;
503 if ( rdi && rdi->rdi_flags & RDI_POST_DISCONNECT ) {
504 return rs->sr_err = SLAPD_DISCONNECT;
513 retcode_op2str( ber_tag_t op, struct berval *bv )
517 BER_BVSTR( bv, "bind" );
520 BER_BVSTR( bv, "add" );
522 case LDAP_REQ_DELETE:
523 BER_BVSTR( bv, "delete" );
525 case LDAP_REQ_MODRDN:
526 BER_BVSTR( bv, "modrdn" );
528 case LDAP_REQ_MODIFY:
529 BER_BVSTR( bv, "modify" );
531 case LDAP_REQ_COMPARE:
532 BER_BVSTR( bv, "compare" );
534 case LDAP_REQ_SEARCH:
535 BER_BVSTR( bv, "search" );
537 case LDAP_REQ_EXTENDED:
538 BER_BVSTR( bv, "extended" );
545 retcode_entry_response( Operation *op, SlapReply *rs, BackendInfo *bi, Entry *e )
552 if ( get_manageDSAit( op ) ) {
553 return SLAP_CB_CONTINUE;
556 if ( !is_entry_objectclass_or_sub( e, oc_errAbsObject ) ) {
557 return SLAP_CB_CONTINUE;
561 a = attr_find( e->e_attrs, ad_errOp );
565 struct berval bv = BER_BVNULL;
567 (void)retcode_op2str( op->o_tag, &bv );
569 if ( BER_BVISNULL( &bv ) ) {
570 return SLAP_CB_CONTINUE;
573 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
574 if ( bvmatch( &a->a_nvals[ i ], &bv ) ) {
581 return SLAP_CB_CONTINUE;
586 a = attr_find( e->e_attrs, ad_errDisconnect );
588 if ( bvmatch( &a->a_nvals[ 0 ], &slap_true_bv ) ) {
589 return rs->sr_err = SLAPD_DISCONNECT;
595 a = attr_find( e->e_attrs, ad_errCode );
597 return SLAP_CB_CONTINUE;
599 err = strtol( a->a_nvals[ 0 ].bv_val, &next, 0 );
600 if ( next == a->a_nvals[ 0 ].bv_val || next[ 0 ] != '\0' ) {
601 return SLAP_CB_CONTINUE;
606 a = attr_find( e->e_attrs, ad_errSleepTime );
607 if ( a != NULL && a->a_nvals[ 0 ].bv_val[ 0 ] != '-' ) {
610 if ( lutil_atoi( &sleepTime, a->a_nvals[ 0 ].bv_val ) == 0 ) {
611 retcode_sleep( sleepTime );
615 if ( rs->sr_err != LDAP_SUCCESS && !LDAP_API_ERROR( rs->sr_err )) {
616 BackendDB db = *op->o_bd,
618 void *o_callback = op->o_callback;
621 a = attr_find( e->e_attrs, ad_errText );
623 rs->sr_text = a->a_vals[ 0 ].bv_val;
627 a = attr_find( e->e_attrs, ad_errMatchedDN );
629 rs->sr_matched = a->a_vals[ 0 ].bv_val;
633 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
635 bi = on->on_info->oi_orig;
640 op->o_callback = NULL;
643 if ( rs->sr_err == LDAP_REFERRAL ) {
644 BerVarray refs = default_referral;
646 a = attr_find( e->e_attrs, slap_schema.si_ad_ref );
650 rs->sr_ref = referral_rewrite( refs,
651 NULL, &op->o_req_dn, op->oq_search.rs_scope );
653 send_search_reference( op, rs );
654 ber_bvarray_free( rs->sr_ref );
658 a = attr_find( e->e_attrs, ad_errUnsolicitedOID );
660 struct berval oid = BER_BVNULL,
662 ber_int_t msgid = op->o_msgid;
664 /* RFC 4511 unsolicited response */
668 oid = a->a_nvals[ 0 ];
670 a = attr_find( e->e_attrs, ad_errUnsolicitedData );
672 data = a->a_nvals[ 0 ];
675 if ( strcmp( oid.bv_val, "0" ) == 0 ) {
676 send_ldap_result( op, rs );
679 ber_tag_t tag = op->o_tag;
681 op->o_tag = LDAP_REQ_EXTENDED;
682 rs->sr_rspoid = oid.bv_val;
683 if ( !BER_BVISNULL( &data ) ) {
684 rs->sr_rspdata = &data;
686 send_ldap_extended( op, rs );
687 rs->sr_rspoid = NULL;
688 rs->sr_rspdata = NULL;
694 send_ldap_result( op, rs );
699 rs->sr_matched = NULL;
701 op->o_callback = o_callback;
704 if ( rs->sr_err != LDAP_SUCCESS ) {
706 return rs->sr_err = SLAPD_DISCONNECT;
713 return SLAP_CB_CONTINUE;
717 retcode_response( Operation *op, SlapReply *rs )
719 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
720 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
722 if ( rs->sr_type != REP_SEARCH || !RETCODE_INDIR( rd ) ) {
723 return SLAP_CB_CONTINUE;
726 return retcode_entry_response( op, rs, NULL, rs->sr_entry );
730 retcode_db_init( BackendDB *be, ConfigReply *cr )
732 slap_overinst *on = (slap_overinst *)be->bd_info;
737 rd = (retcode_t *)ch_malloc( sizeof( retcode_t ) );
738 memset( rd, 0, sizeof( retcode_t ) );
740 on->on_bi.bi_private = (void *)rd;
746 retcode_item_destroy( retcode_item_t *rdi )
748 ber_memfree( rdi->rdi_line.bv_val );
750 ber_memfree( rdi->rdi_dn.bv_val );
751 ber_memfree( rdi->rdi_ndn.bv_val );
753 if ( !BER_BVISNULL( &rdi->rdi_text ) ) {
754 ber_memfree( rdi->rdi_text.bv_val );
757 if ( !BER_BVISNULL( &rdi->rdi_matched ) ) {
758 ber_memfree( rdi->rdi_matched.bv_val );
761 if ( rdi->rdi_ref ) {
762 ber_bvarray_free( rdi->rdi_ref );
765 BER_BVZERO( &rdi->rdi_e.e_name );
766 BER_BVZERO( &rdi->rdi_e.e_nname );
768 entry_clean( &rdi->rdi_e );
778 static ConfigDriver rc_cf_gen;
780 static ConfigTable rccfg[] = {
781 { "retcode-parent", "dn",
782 2, 2, 0, ARG_MAGIC|ARG_DN|RC_PARENT, rc_cf_gen,
783 "( OLcfgOvAt:20.1 NAME 'olcRetcodeParent' "
785 "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
786 { "retcode-item", "rdn> <retcode> <...",
787 3, 0, 0, ARG_MAGIC|RC_ITEM, rc_cf_gen,
788 "( OLcfgOvAt:20.2 NAME 'olcRetcodeItem' "
790 "EQUALITY caseIgnoreMatch "
791 "SYNTAX OMsDirectoryString "
792 "X-ORDERED 'VALUES' )", NULL, NULL },
793 { "retcode-indir", "on|off",
794 1, 2, 0, ARG_OFFSET|ARG_ON_OFF,
795 (void *)offsetof(retcode_t, rd_indir),
796 "( OLcfgOvAt:20.3 NAME 'olcRetcodeInDir' "
798 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
800 { "retcode-sleep", "sleeptime",
801 2, 2, 0, ARG_OFFSET|ARG_INT,
802 (void *)offsetof(retcode_t, rd_sleep),
803 "( OLcfgOvAt:20.4 NAME 'olcRetcodeSleep' "
805 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
807 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
810 static ConfigOCs rcocs[] = {
811 { "( OLcfgOvOc:20.1 "
812 "NAME 'olcRetcodeConfig' "
813 "DESC 'Retcode configuration' "
814 "SUP olcOverlayConfig "
815 "MAY ( olcRetcodeParent "
820 Cft_Overlay, rccfg, NULL, NULL },
825 rc_cf_gen( ConfigArgs *c )
827 slap_overinst *on = (slap_overinst *)c->bi;
828 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
829 int rc = ARG_BAD_CONF;
831 if ( c->op == SLAP_CONFIG_EMIT ) {
834 if ( !BER_BVISEMPTY( &rd->rd_pdn )) {
835 rc = value_add_one( &c->rvalue_vals,
838 rc = value_add_one( &c->rvalue_nvals,
850 for ( rdi = rd->rd_item, i = 0; rdi; rdi = rdi->rdi_next, i++ ) {
855 bv.bv_len = snprintf( buf, sizeof( buf ), SLAP_X_ORDERED_FMT, i );
856 bv.bv_len += rdi->rdi_line.bv_len;
857 ptr = bv.bv_val = ch_malloc( bv.bv_len + 1 );
858 ptr = lutil_strcopy( ptr, buf );
859 ptr = lutil_strncopy( ptr, rdi->rdi_line.bv_val, rdi->rdi_line.bv_len );
860 ber_bvarray_add( &c->rvalue_vals, &bv );
872 } else if ( c->op == LDAP_MOD_DELETE ) {
875 if ( rd->rd_pdn.bv_val ) {
876 ber_memfree ( rd->rd_pdn.bv_val );
879 if ( rd->rd_npdn.bv_val ) {
880 ber_memfree ( rd->rd_npdn.bv_val );
885 if ( c->valx == -1 ) {
886 retcode_item_t *rdi, *next;
888 for ( rdi = rd->rd_item; rdi != NULL; rdi = next ) {
889 next = rdi->rdi_next;
890 retcode_item_destroy( rdi );
894 retcode_item_t **rdip, *rdi;
897 for ( rdip = &rd->rd_item, i = 0; i <= c->valx && *rdip; i++, rdip = &(*rdip)->rdi_next )
899 if ( *rdip == NULL ) {
903 *rdip = rdi->rdi_next;
905 retcode_item_destroy( rdi );
914 return rc; /* FIXME */
919 if ( rd->rd_pdn.bv_val ) {
920 ber_memfree ( rd->rd_pdn.bv_val );
922 if ( rd->rd_npdn.bv_val ) {
923 ber_memfree ( rd->rd_npdn.bv_val );
925 rd->rd_pdn = c->value_dn;
926 rd->rd_npdn = c->value_ndn;
931 retcode_item_t rdi = { BER_BVNULL }, **rdip;
932 struct berval bv, rdn, nrdn;
937 snprintf( c->cr_msg, sizeof(c->cr_msg),
938 "\"retcode-item <RDN> <retcode> [<text>]\": "
940 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
941 c->log, c->cr_msg, 0 );
945 ber_str2bv( c->argv[ 1 ], 0, 0, &bv );
947 rc = dnPrettyNormal( NULL, &bv, &rdn, &nrdn, NULL );
948 if ( rc != LDAP_SUCCESS ) {
949 snprintf( c->cr_msg, sizeof(c->cr_msg),
950 "unable to normalize RDN \"%s\": %d",
952 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
953 c->log, c->cr_msg, 0 );
957 if ( !dnIsOneLevelRDN( &nrdn ) ) {
958 snprintf( c->cr_msg, sizeof(c->cr_msg),
959 "value \"%s\" is not a RDN",
961 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
962 c->log, c->cr_msg, 0 );
966 if ( BER_BVISNULL( &rd->rd_npdn ) ) {
967 /* FIXME: we use the database suffix */
968 if ( c->be->be_nsuffix == NULL ) {
969 snprintf( c->cr_msg, sizeof(c->cr_msg),
970 "either \"retcode-parent\" "
971 "or \"suffix\" must be defined" );
972 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
973 c->log, c->cr_msg, 0 );
977 ber_dupbv( &rd->rd_pdn, &c->be->be_suffix[ 0 ] );
978 ber_dupbv( &rd->rd_npdn, &c->be->be_nsuffix[ 0 ] );
981 build_new_dn( &rdi.rdi_dn, &rd->rd_pdn, &rdn, NULL );
982 build_new_dn( &rdi.rdi_ndn, &rd->rd_npdn, &nrdn, NULL );
984 ch_free( rdn.bv_val );
985 ch_free( nrdn.bv_val );
987 rdi.rdi_err = strtol( c->argv[ 2 ], &next, 0 );
988 if ( next == c->argv[ 2 ] || next[ 0 ] != '\0' ) {
989 snprintf( c->cr_msg, sizeof(c->cr_msg),
990 "unable to parse return code \"%s\"",
992 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
993 c->log, c->cr_msg, 0 );
997 rdi.rdi_mask = SN_DG_OP_ALL;
1000 for ( i = 3; i < c->argc; i++ ) {
1001 if ( strncasecmp( c->argv[ i ], "op=", STRLENOF( "op=" ) ) == 0 )
1006 ops = ldap_str2charray( &c->argv[ i ][ STRLENOF( "op=" ) ], "," );
1007 assert( ops != NULL );
1009 rdi.rdi_mask = SN_DG_OP_NONE;
1011 for ( j = 0; ops[ j ] != NULL; j++ ) {
1012 if ( strcasecmp( ops[ j ], "add" ) == 0 ) {
1013 rdi.rdi_mask |= SN_DG_OP_ADD;
1015 } else if ( strcasecmp( ops[ j ], "bind" ) == 0 ) {
1016 rdi.rdi_mask |= SN_DG_OP_BIND;
1018 } else if ( strcasecmp( ops[ j ], "compare" ) == 0 ) {
1019 rdi.rdi_mask |= SN_DG_OP_COMPARE;
1021 } else if ( strcasecmp( ops[ j ], "delete" ) == 0 ) {
1022 rdi.rdi_mask |= SN_DG_OP_DELETE;
1024 } else if ( strcasecmp( ops[ j ], "modify" ) == 0 ) {
1025 rdi.rdi_mask |= SN_DG_OP_MODIFY;
1027 } else if ( strcasecmp( ops[ j ], "rename" ) == 0
1028 || strcasecmp( ops[ j ], "modrdn" ) == 0 )
1030 rdi.rdi_mask |= SN_DG_OP_RENAME;
1032 } else if ( strcasecmp( ops[ j ], "search" ) == 0 ) {
1033 rdi.rdi_mask |= SN_DG_OP_SEARCH;
1035 } else if ( strcasecmp( ops[ j ], "extended" ) == 0 ) {
1036 rdi.rdi_mask |= SN_DG_EXTENDED;
1038 } else if ( strcasecmp( ops[ j ], "auth" ) == 0 ) {
1039 rdi.rdi_mask |= SN_DG_OP_AUTH;
1041 } else if ( strcasecmp( ops[ j ], "read" ) == 0 ) {
1042 rdi.rdi_mask |= SN_DG_OP_READ;
1044 } else if ( strcasecmp( ops[ j ], "write" ) == 0 ) {
1045 rdi.rdi_mask |= SN_DG_OP_WRITE;
1047 } else if ( strcasecmp( ops[ j ], "all" ) == 0 ) {
1048 rdi.rdi_mask |= SN_DG_OP_ALL;
1051 snprintf( c->cr_msg, sizeof(c->cr_msg),
1052 "unknown op \"%s\"",
1054 ldap_charray_free( ops );
1055 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
1056 c->log, c->cr_msg, 0 );
1057 return ARG_BAD_CONF;
1061 ldap_charray_free( ops );
1063 } else if ( strncasecmp( c->argv[ i ], "text=", STRLENOF( "text=" ) ) == 0 )
1065 if ( !BER_BVISNULL( &rdi.rdi_text ) ) {
1066 snprintf( c->cr_msg, sizeof(c->cr_msg),
1067 "\"text\" already provided" );
1068 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
1069 c->log, c->cr_msg, 0 );
1070 return ARG_BAD_CONF;
1072 ber_str2bv( &c->argv[ i ][ STRLENOF( "text=" ) ], 0, 1, &rdi.rdi_text );
1074 } else if ( strncasecmp( c->argv[ i ], "matched=", STRLENOF( "matched=" ) ) == 0 )
1078 if ( !BER_BVISNULL( &rdi.rdi_matched ) ) {
1079 snprintf( c->cr_msg, sizeof(c->cr_msg),
1080 "\"matched\" already provided" );
1081 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
1082 c->log, c->cr_msg, 0 );
1083 return ARG_BAD_CONF;
1085 ber_str2bv( &c->argv[ i ][ STRLENOF( "matched=" ) ], 0, 0, &dn );
1086 if ( dnPretty( NULL, &dn, &rdi.rdi_matched, NULL ) != LDAP_SUCCESS ) {
1087 snprintf( c->cr_msg, sizeof(c->cr_msg),
1088 "unable to prettify matched DN \"%s\"",
1089 &c->argv[ i ][ STRLENOF( "matched=" ) ] );
1090 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
1091 c->log, c->cr_msg, 0 );
1092 return ARG_BAD_CONF;
1095 } else if ( strncasecmp( c->argv[ i ], "ref=", STRLENOF( "ref=" ) ) == 0 )
1100 if ( rdi.rdi_ref != NULL ) {
1101 snprintf( c->cr_msg, sizeof(c->cr_msg),
1102 "\"ref\" already provided" );
1103 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
1104 c->log, c->cr_msg, 0 );
1105 return ARG_BAD_CONF;
1108 if ( rdi.rdi_err != LDAP_REFERRAL ) {
1109 snprintf( c->cr_msg, sizeof(c->cr_msg),
1110 "providing \"ref\" "
1111 "along with a non-referral "
1112 "resultCode may cause slapd failures "
1113 "related to internal checks" );
1114 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
1115 c->log, c->cr_msg, 0 );
1118 refs = ldap_str2charray( &c->argv[ i ][ STRLENOF( "ref=" ) ], " " );
1119 assert( refs != NULL );
1121 for ( j = 0; refs[ j ] != NULL; j++ ) {
1124 ber_str2bv( refs[ j ], 0, 1, &bv );
1125 ber_bvarray_add( &rdi.rdi_ref, &bv );
1128 ldap_charray_free( refs );
1130 } else if ( strncasecmp( c->argv[ i ], "sleeptime=", STRLENOF( "sleeptime=" ) ) == 0 )
1132 if ( rdi.rdi_sleeptime != 0 ) {
1133 snprintf( c->cr_msg, sizeof(c->cr_msg),
1134 "\"sleeptime\" already provided" );
1135 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
1136 c->log, c->cr_msg, 0 );
1137 return ARG_BAD_CONF;
1140 if ( lutil_atoi( &rdi.rdi_sleeptime, &c->argv[ i ][ STRLENOF( "sleeptime=" ) ] ) ) {
1141 snprintf( c->cr_msg, sizeof(c->cr_msg),
1142 "unable to parse \"sleeptime=%s\"",
1143 &c->argv[ i ][ STRLENOF( "sleeptime=" ) ] );
1144 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
1145 c->log, c->cr_msg, 0 );
1146 return ARG_BAD_CONF;
1149 } else if ( strncasecmp( c->argv[ i ], "unsolicited=", STRLENOF( "unsolicited=" ) ) == 0 )
1153 if ( !BER_BVISNULL( &rdi.rdi_unsolicited_oid ) ) {
1154 snprintf( c->cr_msg, sizeof(c->cr_msg),
1155 "\"unsolicited\" already provided" );
1156 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
1157 c->log, c->cr_msg, 0 );
1158 return ARG_BAD_CONF;
1161 data = strchr( &c->argv[ i ][ STRLENOF( "unsolicited=" ) ], ':' );
1162 if ( data != NULL ) {
1165 if ( ldif_parse_line2( &c->argv[ i ][ STRLENOF( "unsolicited=" ) ],
1166 &oid, &rdi.rdi_unsolicited_data, NULL ) )
1168 snprintf( c->cr_msg, sizeof(c->cr_msg),
1169 "unable to parse \"unsolicited\"" );
1170 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
1171 c->log, c->cr_msg, 0 );
1172 return ARG_BAD_CONF;
1175 ber_dupbv( &rdi.rdi_unsolicited_oid, &oid );
1178 ber_str2bv( &c->argv[ i ][ STRLENOF( "unsolicited=" ) ], 0, 1,
1179 &rdi.rdi_unsolicited_oid );
1182 } else if ( strncasecmp( c->argv[ i ], "flags=", STRLENOF( "flags=" ) ) == 0 )
1184 char *arg = &c->argv[ i ][ STRLENOF( "flags=" ) ];
1185 if ( strcasecmp( arg, "disconnect" ) == 0 ) {
1186 rdi.rdi_flags |= RDI_PRE_DISCONNECT;
1188 } else if ( strcasecmp( arg, "pre-disconnect" ) == 0 ) {
1189 rdi.rdi_flags |= RDI_PRE_DISCONNECT;
1191 } else if ( strcasecmp( arg, "post-disconnect" ) == 0 ) {
1192 rdi.rdi_flags |= RDI_POST_DISCONNECT;
1195 snprintf( c->cr_msg, sizeof(c->cr_msg),
1196 "unknown flag \"%s\"", arg );
1197 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
1198 c->log, c->cr_msg, 0 );
1199 return ARG_BAD_CONF;
1203 snprintf( c->cr_msg, sizeof(c->cr_msg),
1204 "unknown option \"%s\"",
1206 Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
1207 c->log, c->cr_msg, 0 );
1208 return ARG_BAD_CONF;
1213 rdi.rdi_line.bv_len = 2*(c->argc - 1) + c->argc - 2;
1214 for ( i = 1; i < c->argc; i++ ) {
1215 rdi.rdi_line.bv_len += strlen( c->argv[ i ] );
1217 next = rdi.rdi_line.bv_val = ch_malloc( rdi.rdi_line.bv_len + 1 );
1219 for ( i = 1; i < c->argc; i++ ) {
1221 next = lutil_strcopy( next, c->argv[ i ] );
1227 for ( rdip = &rd->rd_item; *rdip; rdip = &(*rdip)->rdi_next )
1231 *rdip = ( retcode_item_t * )ch_malloc( sizeof( retcode_item_t ) );
1238 rc = SLAP_CONF_UNKNOWN;
1246 retcode_db_open( BackendDB *be, ConfigReply *cr)
1248 slap_overinst *on = (slap_overinst *)be->bd_info;
1249 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
1251 retcode_item_t *rdi;
1253 for ( rdi = rd->rd_item; rdi; rdi = rdi->rdi_next ) {
1257 struct berval val[ 3 ];
1258 char buf[ SLAP_TEXT_BUFLEN ];
1261 rdi->rdi_e.e_name = rdi->rdi_dn;
1262 rdi->rdi_e.e_nname = rdi->rdi_ndn;
1265 val[ 0 ] = oc_errObject->soc_cname;
1266 val[ 1 ] = slap_schema.si_oc_extensibleObject->soc_cname;
1267 BER_BVZERO( &val[ 2 ] );
1269 attr_merge( &rdi->rdi_e, slap_schema.si_ad_objectClass, val, NULL );
1272 rc = ldap_bv2rdn( &rdi->rdi_dn, &rdn, (char **) &p,
1273 LDAP_DN_FORMAT_LDAP );
1275 assert( rc == LDAP_SUCCESS );
1277 for ( j = 0; rdn[ j ]; j++ ) {
1278 LDAPAVA *ava = rdn[ j ];
1279 AttributeDescription *ad = NULL;
1282 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
1283 assert( rc == LDAP_SUCCESS );
1285 attr_merge_normalize_one( &rdi->rdi_e, ad,
1286 &ava->la_value, NULL );
1289 ldap_rdnfree( rdn );
1292 snprintf( buf, sizeof( buf ), "%d", rdi->rdi_err );
1293 ber_str2bv( buf, 0, 0, &val[ 0 ] );
1295 attr_merge_one( &rdi->rdi_e, ad_errCode, &val[ 0 ], NULL );
1297 if ( rdi->rdi_ref != NULL ) {
1298 attr_merge_normalize( &rdi->rdi_e, slap_schema.si_ad_ref,
1299 rdi->rdi_ref, NULL );
1303 if ( !BER_BVISNULL( &rdi->rdi_text ) ) {
1304 val[ 0 ] = rdi->rdi_text;
1306 attr_merge_normalize_one( &rdi->rdi_e, ad_errText, &val[ 0 ], NULL );
1310 if ( !BER_BVISNULL( &rdi->rdi_matched ) ) {
1311 val[ 0 ] = rdi->rdi_matched;
1313 attr_merge_normalize_one( &rdi->rdi_e, ad_errMatchedDN, &val[ 0 ], NULL );
1317 if ( rdi->rdi_sleeptime ) {
1318 snprintf( buf, sizeof( buf ), "%d", rdi->rdi_sleeptime );
1319 ber_str2bv( buf, 0, 0, &val[ 0 ] );
1321 attr_merge_one( &rdi->rdi_e, ad_errSleepTime, &val[ 0 ], NULL );
1325 if ( rdi->rdi_mask & SN_DG_OP_ADD ) {
1326 BER_BVSTR( &val[ 0 ], "add" );
1327 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1330 if ( rdi->rdi_mask & SN_DG_OP_BIND ) {
1331 BER_BVSTR( &val[ 0 ], "bind" );
1332 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1335 if ( rdi->rdi_mask & SN_DG_OP_COMPARE ) {
1336 BER_BVSTR( &val[ 0 ], "compare" );
1337 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1340 if ( rdi->rdi_mask & SN_DG_OP_DELETE ) {
1341 BER_BVSTR( &val[ 0 ], "delete" );
1342 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1345 if ( rdi->rdi_mask & SN_DG_EXTENDED ) {
1346 BER_BVSTR( &val[ 0 ], "extended" );
1347 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1350 if ( rdi->rdi_mask & SN_DG_OP_MODIFY ) {
1351 BER_BVSTR( &val[ 0 ], "modify" );
1352 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1355 if ( rdi->rdi_mask & SN_DG_OP_RENAME ) {
1356 BER_BVSTR( &val[ 0 ], "rename" );
1357 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1360 if ( rdi->rdi_mask & SN_DG_OP_SEARCH ) {
1361 BER_BVSTR( &val[ 0 ], "search" );
1362 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1370 retcode_db_destroy( BackendDB *be, ConfigReply *cr )
1372 slap_overinst *on = (slap_overinst *)be->bd_info;
1373 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
1376 retcode_item_t *rdi, *next;
1378 for ( rdi = rd->rd_item; rdi != NULL; rdi = next ) {
1379 next = rdi->rdi_next;
1380 retcode_item_destroy( rdi );
1383 if ( !BER_BVISNULL( &rd->rd_pdn ) ) {
1384 ber_memfree( rd->rd_pdn.bv_val );
1387 if ( !BER_BVISNULL( &rd->rd_npdn ) ) {
1388 ber_memfree( rd->rd_npdn.bv_val );
1397 #if SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC
1399 #endif /* SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC */
1401 retcode_initialize( void )
1407 AttributeDescription **ad;
1409 { "( 1.3.6.1.4.1.4203.666.11.4.1.1 "
1410 "NAME ( 'errCode' ) "
1411 "DESC 'LDAP error code' "
1412 "EQUALITY integerMatch "
1413 "ORDERING integerOrderingMatch "
1414 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
1417 { "( 1.3.6.1.4.1.4203.666.11.4.1.2 "
1419 "DESC 'Operations the errObject applies to' "
1420 "EQUALITY caseIgnoreMatch "
1421 "SUBSTR caseIgnoreSubstringsMatch "
1422 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
1424 { "( 1.3.6.1.4.1.4203.666.11.4.1.3 "
1425 "NAME ( 'errText' ) "
1426 "DESC 'LDAP error textual description' "
1427 "EQUALITY caseIgnoreMatch "
1428 "SUBSTR caseIgnoreSubstringsMatch "
1429 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
1432 { "( 1.3.6.1.4.1.4203.666.11.4.1.4 "
1433 "NAME ( 'errSleepTime' ) "
1434 "DESC 'Time to wait before returning the error' "
1435 "EQUALITY integerMatch "
1436 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
1439 { "( 1.3.6.1.4.1.4203.666.11.4.1.5 "
1440 "NAME ( 'errMatchedDN' ) "
1441 "DESC 'Value to be returned as matched DN' "
1442 "EQUALITY distinguishedNameMatch "
1443 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
1446 { "( 1.3.6.1.4.1.4203.666.11.4.1.6 "
1447 "NAME ( 'errUnsolicitedOID' ) "
1448 "DESC 'OID to be returned within unsolicited response' "
1449 "EQUALITY objectIdentifierMatch "
1450 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
1452 &ad_errUnsolicitedOID },
1453 { "( 1.3.6.1.4.1.4203.666.11.4.1.7 "
1454 "NAME ( 'errUnsolicitedData' ) "
1455 "DESC 'Data to be returned within unsolicited response' "
1456 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 "
1458 &ad_errUnsolicitedData },
1459 { "( 1.3.6.1.4.1.4203.666.11.4.1.8 "
1460 "NAME ( 'errDisconnect' ) "
1461 "DESC 'Disconnect without notice' "
1462 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1464 &ad_errDisconnect },
1472 { "( 1.3.6.1.4.1.4203.666.11.4.3.0 "
1473 "NAME ( 'errAbsObject' ) "
1483 "$ errUnsolicitedOID "
1484 "$ errUnsolicitedData "
1488 { "( 1.3.6.1.4.1.4203.666.11.4.3.1 "
1489 "NAME ( 'errObject' ) "
1490 "SUP errAbsObject STRUCTURAL "
1493 { "( 1.3.6.1.4.1.4203.666.11.4.3.2 "
1494 "NAME ( 'errAuxObject' ) "
1495 "SUP errAbsObject AUXILIARY "
1502 for ( i = 0; retcode_at[ i ].desc != NULL; i++ ) {
1503 code = register_at( retcode_at[ i ].desc, retcode_at[ i ].ad, 0 );
1505 Debug( LDAP_DEBUG_ANY,
1506 "retcode: register_at failed\n", 0, 0, 0 );
1510 (*retcode_at[ i ].ad)->ad_type->sat_flags |= SLAP_AT_HIDE;
1513 for ( i = 0; retcode_oc[ i ].desc != NULL; i++ ) {
1514 code = register_oc( retcode_oc[ i ].desc, retcode_oc[ i ].oc, 0 );
1516 Debug( LDAP_DEBUG_ANY,
1517 "retcode: register_oc failed\n", 0, 0, 0 );
1521 (*retcode_oc[ i ].oc)->soc_flags |= SLAP_OC_HIDE;
1524 retcode.on_bi.bi_type = "retcode";
1526 retcode.on_bi.bi_db_init = retcode_db_init;
1527 retcode.on_bi.bi_db_open = retcode_db_open;
1528 retcode.on_bi.bi_db_destroy = retcode_db_destroy;
1530 retcode.on_bi.bi_op_add = retcode_op_func;
1531 retcode.on_bi.bi_op_bind = retcode_op_func;
1532 retcode.on_bi.bi_op_compare = retcode_op_func;
1533 retcode.on_bi.bi_op_delete = retcode_op_func;
1534 retcode.on_bi.bi_op_modify = retcode_op_func;
1535 retcode.on_bi.bi_op_modrdn = retcode_op_func;
1536 retcode.on_bi.bi_op_search = retcode_op_func;
1538 retcode.on_bi.bi_extended = retcode_op_func;
1540 retcode.on_response = retcode_response;
1542 retcode.on_bi.bi_cf_ocs = rcocs;
1544 code = config_register_schema( rccfg, rcocs );
1549 return overlay_register( &retcode );
1552 #if SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC
1554 init_module( int argc, char *argv[] )
1556 return retcode_initialize();
1558 #endif /* SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC */
1560 #endif /* SLAPD_OVER_RETCODE */