1 /* retcode.c - customizable response for client testing purposes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005-2007 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 {
71 struct berval rdi_ndn;
72 struct berval rdi_text;
73 struct berval rdi_matched;
79 struct berval rdi_unsolicited_oid;
80 struct berval rdi_unsolicited_data;
83 #define RDI_PRE_DISCONNECT (0x1U)
84 #define RDI_POST_DISCONNECT (0x2U)
86 struct retcode_item_t *rdi_next;
89 typedef struct retcode_t {
91 struct berval rd_npdn;
95 retcode_item_t *rd_item;
98 #define RETCODE_FNONE 0x00
99 #define RETCODE_FINDIR 0x01
100 #define RETCODE_INDIR( rd ) ( (rd)->rd_flags & RETCODE_FINDIR )
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 );
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 if ( be_isroot_pw( op ) ) {
311 return retcode_op_internal( op, rs );
313 case LDAP_REQ_SEARCH:
314 if ( op->ors_scope == LDAP_SCOPE_BASE ) {
315 rs->sr_err = retcode_op_internal( op, rs );
316 switch ( rs->sr_err ) {
317 case SLAP_CB_CONTINUE:
318 if ( rs->sr_nentries == 0 ) {
321 rs->sr_err = LDAP_SUCCESS;
325 send_ldap_result( op, rs );
332 case LDAP_REQ_MODIFY:
333 case LDAP_REQ_DELETE:
334 case LDAP_REQ_MODRDN:
335 case LDAP_REQ_COMPARE:
336 return retcode_op_internal( op, rs );
340 return SLAP_CB_CONTINUE;
343 if ( op->o_tag == LDAP_REQ_SEARCH
344 && op->ors_scope != LDAP_SCOPE_BASE
345 && op->o_req_ndn.bv_len == rd->rd_npdn.bv_len )
347 return retcode_send_onelevel( op, rs );
350 dnParent( &op->o_req_ndn, &npdn );
351 if ( npdn.bv_len != rd->rd_npdn.bv_len ) {
352 rs->sr_err = LDAP_NO_SUCH_OBJECT;
353 rs->sr_matched = rd->rd_pdn.bv_val;
354 send_ldap_result( op, rs );
355 rs->sr_matched = NULL;
359 dnRdn( &op->o_req_ndn, &nrdn );
361 for ( rdi = rd->rd_item; rdi != NULL; rdi = rdi->rdi_next ) {
362 struct berval rdi_nrdn;
364 dnRdn( &rdi->rdi_ndn, &rdi_nrdn );
365 if ( dn_match( &nrdn, &rdi_nrdn ) ) {
370 if ( rdi != NULL && rdi->rdi_mask != SN_DG_OP_ALL ) {
371 retcode_op_e o_tag = SN_DG_OP_NONE;
373 switch ( op->o_tag ) {
375 o_tag = SN_DG_OP_ADD;
379 o_tag = SN_DG_OP_BIND;
382 case LDAP_REQ_COMPARE:
383 o_tag = SN_DG_OP_COMPARE;
386 case LDAP_REQ_DELETE:
387 o_tag = SN_DG_OP_DELETE;
390 case LDAP_REQ_MODIFY:
391 o_tag = SN_DG_OP_MODIFY;
394 case LDAP_REQ_MODRDN:
395 o_tag = SN_DG_OP_RENAME;
398 case LDAP_REQ_SEARCH:
399 o_tag = SN_DG_OP_SEARCH;
402 case LDAP_REQ_EXTENDED:
403 o_tag = SN_DG_EXTENDED;
407 /* Should not happen */
411 if ( !( o_tag & rdi->rdi_mask ) ) {
412 return SLAP_CB_CONTINUE;
417 rs->sr_matched = rd->rd_pdn.bv_val;
418 rs->sr_err = LDAP_NO_SUCH_OBJECT;
419 rs->sr_text = "retcode not found";
422 if ( rdi->rdi_flags & RDI_PRE_DISCONNECT ) {
423 return rs->sr_err = SLAPD_DISCONNECT;
426 rs->sr_err = rdi->rdi_err;
427 rs->sr_text = rdi->rdi_text.bv_val;
428 rs->sr_matched = rdi->rdi_matched.bv_val;
430 /* FIXME: we only honor the rdi_ref field in case rdi_err
431 * is LDAP_REFERRAL otherwise send_ldap_result() bails out */
432 if ( rs->sr_err == LDAP_REFERRAL ) {
435 if ( rdi->rdi_ref != NULL ) {
438 ref = default_referral;
442 rs->sr_ref = referral_rewrite( ref,
443 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
446 rs->sr_err = LDAP_OTHER;
447 rs->sr_text = "bad referral object";
451 retcode_sleep( rdi->rdi_sleeptime );
454 switch ( op->o_tag ) {
455 case LDAP_REQ_EXTENDED:
459 cb = ( slap_callback * )ch_malloc( sizeof( slap_callback ) );
460 memset( cb, 0, sizeof( slap_callback ) );
461 cb->sc_cleanup = retcode_cleanup_cb;
466 if ( rdi && !BER_BVISNULL( &rdi->rdi_unsolicited_oid ) ) {
467 ber_int_t msgid = op->o_msgid;
469 /* RFC 4511 unsolicited response */
472 if ( strcmp( rdi->rdi_unsolicited_oid.bv_val, "0" ) == 0 ) {
473 send_ldap_result( op, rs );
476 ber_tag_t tag = op->o_tag;
478 op->o_tag = LDAP_REQ_EXTENDED;
479 rs->sr_rspoid = rdi->rdi_unsolicited_oid.bv_val;
480 if ( !BER_BVISNULL( &rdi->rdi_unsolicited_data ) ) {
481 rs->sr_rspdata = &rdi->rdi_unsolicited_data;
483 send_ldap_extended( op, rs );
484 rs->sr_rspoid = NULL;
485 rs->sr_rspdata = NULL;
492 send_ldap_result( op, rs );
495 if ( rs->sr_ref != NULL ) {
496 ber_bvarray_free( rs->sr_ref );
499 rs->sr_matched = NULL;
502 if ( rdi && rdi->rdi_flags & RDI_POST_DISCONNECT ) {
503 return rs->sr_err = SLAPD_DISCONNECT;
512 retcode_op2str( ber_tag_t op, struct berval *bv )
516 BER_BVSTR( bv, "bind" );
519 BER_BVSTR( bv, "add" );
521 case LDAP_REQ_DELETE:
522 BER_BVSTR( bv, "delete" );
524 case LDAP_REQ_MODRDN:
525 BER_BVSTR( bv, "modrdn" );
527 case LDAP_REQ_MODIFY:
528 BER_BVSTR( bv, "modify" );
530 case LDAP_REQ_COMPARE:
531 BER_BVSTR( bv, "compare" );
533 case LDAP_REQ_SEARCH:
534 BER_BVSTR( bv, "search" );
536 case LDAP_REQ_EXTENDED:
537 BER_BVSTR( bv, "extended" );
544 retcode_entry_response( Operation *op, SlapReply *rs, BackendInfo *bi, Entry *e )
551 if ( get_manageDSAit( op ) ) {
552 return SLAP_CB_CONTINUE;
555 if ( !is_entry_objectclass_or_sub( e, oc_errAbsObject ) ) {
556 return SLAP_CB_CONTINUE;
560 a = attr_find( e->e_attrs, ad_errOp );
564 struct berval bv = BER_BVNULL;
566 (void)retcode_op2str( op->o_tag, &bv );
568 if ( BER_BVISNULL( &bv ) ) {
569 return SLAP_CB_CONTINUE;
572 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
573 if ( bvmatch( &a->a_nvals[ i ], &bv ) ) {
580 return SLAP_CB_CONTINUE;
585 a = attr_find( e->e_attrs, ad_errDisconnect );
587 if ( bvmatch( &a->a_nvals[ 0 ], &slap_true_bv ) ) {
588 return rs->sr_err = SLAPD_DISCONNECT;
594 a = attr_find( e->e_attrs, ad_errCode );
596 return SLAP_CB_CONTINUE;
598 err = strtol( a->a_nvals[ 0 ].bv_val, &next, 0 );
599 if ( next == a->a_nvals[ 0 ].bv_val || next[ 0 ] != '\0' ) {
600 return SLAP_CB_CONTINUE;
605 a = attr_find( e->e_attrs, ad_errSleepTime );
606 if ( a != NULL && a->a_nvals[ 0 ].bv_val[ 0 ] != '-' ) {
609 if ( lutil_atoi( &sleepTime, a->a_nvals[ 0 ].bv_val ) == 0 ) {
610 retcode_sleep( sleepTime );
614 if ( rs->sr_err != LDAP_SUCCESS && !LDAP_API_ERROR( rs->sr_err )) {
615 BackendDB db = *op->o_bd,
617 void *o_callback = op->o_callback;
620 a = attr_find( e->e_attrs, ad_errText );
622 rs->sr_text = a->a_vals[ 0 ].bv_val;
626 a = attr_find( e->e_attrs, ad_errMatchedDN );
628 rs->sr_matched = a->a_vals[ 0 ].bv_val;
632 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
634 bi = on->on_info->oi_orig;
639 op->o_callback = NULL;
642 if ( rs->sr_err == LDAP_REFERRAL ) {
643 BerVarray refs = default_referral;
645 a = attr_find( e->e_attrs, slap_schema.si_ad_ref );
649 rs->sr_ref = referral_rewrite( refs,
650 NULL, &op->o_req_dn, op->oq_search.rs_scope );
652 send_search_reference( op, rs );
653 ber_bvarray_free( rs->sr_ref );
657 a = attr_find( e->e_attrs, ad_errUnsolicitedOID );
659 struct berval oid = BER_BVNULL,
661 ber_int_t msgid = op->o_msgid;
663 /* RFC 4511 unsolicited response */
667 oid = a->a_nvals[ 0 ];
669 a = attr_find( e->e_attrs, ad_errUnsolicitedData );
671 data = a->a_nvals[ 0 ];
674 if ( strcmp( oid.bv_val, "0" ) == 0 ) {
675 send_ldap_result( op, rs );
678 ber_tag_t tag = op->o_tag;
680 op->o_tag = LDAP_REQ_EXTENDED;
681 rs->sr_rspoid = oid.bv_val;
682 if ( !BER_BVISNULL( &data ) ) {
683 rs->sr_rspdata = &data;
685 send_ldap_extended( op, rs );
686 rs->sr_rspoid = NULL;
687 rs->sr_rspdata = NULL;
693 send_ldap_result( op, rs );
698 rs->sr_matched = NULL;
700 op->o_callback = o_callback;
703 if ( rs->sr_err != LDAP_SUCCESS ) {
705 return rs->sr_err = SLAPD_DISCONNECT;
712 return SLAP_CB_CONTINUE;
716 retcode_response( Operation *op, SlapReply *rs )
718 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
719 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
721 if ( rs->sr_type != REP_SEARCH || !RETCODE_INDIR( rd ) ) {
722 return SLAP_CB_CONTINUE;
725 return retcode_entry_response( op, rs, NULL, rs->sr_entry );
729 retcode_db_init( BackendDB *be, ConfigReply *cr )
731 slap_overinst *on = (slap_overinst *)be->bd_info;
736 rd = (retcode_t *)ch_malloc( sizeof( retcode_t ) );
737 memset( rd, 0, sizeof( retcode_t ) );
739 on->on_bi.bi_private = (void *)rd;
752 slap_overinst *on = (slap_overinst *)be->bd_info;
753 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
755 char *argv0 = argv[ 0 ] + STRLENOF( "retcode-" );
757 if ( strncasecmp( argv[ 0 ], "retcode-", STRLENOF( "retcode-" ) ) != 0 ) {
758 return SLAP_CONF_UNKNOWN;
761 if ( strcasecmp( argv0, "parent" ) == 0 ) {
766 fprintf( stderr, "%s: line %d: retcode: "
767 "\"retcode-parent <DN>\": missing <DN>\n",
772 if ( !BER_BVISNULL( &rd->rd_pdn ) ) {
773 fprintf( stderr, "%s: line %d: retcode: "
774 "parent already defined.\n", fname, lineno );
778 ber_str2bv( argv[ 1 ], 0, 0, &dn );
780 rc = dnPrettyNormal( NULL, &dn, &rd->rd_pdn, &rd->rd_npdn, NULL );
781 if ( rc != LDAP_SUCCESS ) {
782 fprintf( stderr, "%s: line %d: retcode: "
783 "unable to normalize parent DN \"%s\": %d\n",
784 fname, lineno, argv[ 1 ], rc );
788 } else if ( strcasecmp( argv0, "item" ) == 0 ) {
789 retcode_item_t rdi = { BER_BVNULL }, **rdip;
790 struct berval bv, rdn, nrdn;
795 fprintf( stderr, "%s: line %d: retcode: "
796 "\"retcode-item <RDN> <retcode> [<text>]\": "
802 ber_str2bv( argv[ 1 ], 0, 0, &bv );
804 rc = dnPrettyNormal( NULL, &bv, &rdn, &nrdn, NULL );
805 if ( rc != LDAP_SUCCESS ) {
806 fprintf( stderr, "%s: line %d: retcode: "
807 "unable to normalize RDN \"%s\": %d\n",
808 fname, lineno, argv[ 1 ], rc );
812 if ( !dnIsOneLevelRDN( &nrdn ) ) {
813 fprintf( stderr, "%s: line %d: retcode: "
814 "value \"%s\" is not a RDN\n",
815 fname, lineno, argv[ 1 ] );
819 if ( BER_BVISNULL( &rd->rd_npdn ) ) {
820 /* FIXME: we use the database suffix */
821 if ( be->be_nsuffix == NULL ) {
822 fprintf( stderr, "%s: line %d: retcode: "
823 "either \"retcode-parent\" "
824 "or \"suffix\" must be defined.\n",
829 ber_dupbv( &rd->rd_pdn, &be->be_suffix[ 0 ] );
830 ber_dupbv( &rd->rd_npdn, &be->be_nsuffix[ 0 ] );
833 build_new_dn( &rdi.rdi_dn, &rd->rd_pdn, &rdn, NULL );
834 build_new_dn( &rdi.rdi_ndn, &rd->rd_npdn, &nrdn, NULL );
836 ch_free( rdn.bv_val );
837 ch_free( nrdn.bv_val );
839 rdi.rdi_err = strtol( argv[ 2 ], &next, 0 );
840 if ( next == argv[ 2 ] || next[ 0 ] != '\0' ) {
841 fprintf( stderr, "%s: line %d: retcode: "
842 "unable to parse return code \"%s\"\n",
843 fname, lineno, argv[ 2 ] );
847 rdi.rdi_mask = SN_DG_OP_ALL;
852 for ( i = 3; i < argc; i++ ) {
853 if ( strncasecmp( argv[ i ], "op=", STRLENOF( "op=" ) ) == 0 )
858 ops = ldap_str2charray( &argv[ i ][ STRLENOF( "op=" ) ], "," );
859 assert( ops != NULL );
861 rdi.rdi_mask = SN_DG_OP_NONE;
863 for ( j = 0; ops[ j ] != NULL; j++ ) {
864 if ( strcasecmp( ops[ j ], "add" ) == 0 ) {
865 rdi.rdi_mask |= SN_DG_OP_ADD;
867 } else if ( strcasecmp( ops[ j ], "bind" ) == 0 ) {
868 rdi.rdi_mask |= SN_DG_OP_BIND;
870 } else if ( strcasecmp( ops[ j ], "compare" ) == 0 ) {
871 rdi.rdi_mask |= SN_DG_OP_COMPARE;
873 } else if ( strcasecmp( ops[ j ], "delete" ) == 0 ) {
874 rdi.rdi_mask |= SN_DG_OP_DELETE;
876 } else if ( strcasecmp( ops[ j ], "modify" ) == 0 ) {
877 rdi.rdi_mask |= SN_DG_OP_MODIFY;
879 } else if ( strcasecmp( ops[ j ], "rename" ) == 0
880 || strcasecmp( ops[ j ], "modrdn" ) == 0 )
882 rdi.rdi_mask |= SN_DG_OP_RENAME;
884 } else if ( strcasecmp( ops[ j ], "search" ) == 0 ) {
885 rdi.rdi_mask |= SN_DG_OP_SEARCH;
887 } else if ( strcasecmp( ops[ j ], "extended" ) == 0 ) {
888 rdi.rdi_mask |= SN_DG_EXTENDED;
890 } else if ( strcasecmp( ops[ j ], "auth" ) == 0 ) {
891 rdi.rdi_mask |= SN_DG_OP_AUTH;
893 } else if ( strcasecmp( ops[ j ], "read" ) == 0 ) {
894 rdi.rdi_mask |= SN_DG_OP_READ;
896 } else if ( strcasecmp( ops[ j ], "write" ) == 0 ) {
897 rdi.rdi_mask |= SN_DG_OP_WRITE;
899 } else if ( strcasecmp( ops[ j ], "all" ) == 0 ) {
900 rdi.rdi_mask |= SN_DG_OP_ALL;
903 fprintf( stderr, "retcode: unknown op \"%s\"\n",
905 ldap_charray_free( ops );
910 ldap_charray_free( ops );
912 } else if ( strncasecmp( argv[ i ], "text=", STRLENOF( "text=" ) ) == 0 )
914 if ( !BER_BVISNULL( &rdi.rdi_text ) ) {
915 fprintf( stderr, "%s: line %d: retcode: "
916 "\"text\" already provided.\n",
920 ber_str2bv( &argv[ i ][ STRLENOF( "text=" ) ], 0, 1, &rdi.rdi_text );
922 } else if ( strncasecmp( argv[ i ], "matched=", STRLENOF( "matched=" ) ) == 0 )
926 if ( !BER_BVISNULL( &rdi.rdi_matched ) ) {
927 fprintf( stderr, "%s: line %d: retcode: "
928 "\"matched\" already provided.\n",
932 ber_str2bv( &argv[ i ][ STRLENOF( "matched=" ) ], 0, 0, &dn );
933 if ( dnPretty( NULL, &dn, &rdi.rdi_matched, NULL ) != LDAP_SUCCESS ) {
934 fprintf( stderr, "%s: line %d: retcode: "
935 "unable to prettify matched DN \"%s\".\n",
936 fname, lineno, &argv[ i ][ STRLENOF( "matched=" ) ] );
940 } else if ( strncasecmp( argv[ i ], "ref=", STRLENOF( "ref=" ) ) == 0 )
945 if ( rdi.rdi_ref != NULL ) {
946 fprintf( stderr, "%s: line %d: retcode: "
947 "\"ref\" already provided.\n",
952 if ( rdi.rdi_err != LDAP_REFERRAL ) {
953 fprintf( stderr, "%s: line %d: retcode: "
954 "providing \"ref\"\n"
955 "\talong with a non-referral "
956 "resultCode may cause slapd failures\n"
957 "\trelated to internal checks.\n",
961 refs = ldap_str2charray( &argv[ i ][ STRLENOF( "ref=" ) ], " " );
962 assert( refs != NULL );
964 for ( j = 0; refs[ j ] != NULL; j++ ) {
967 ber_str2bv( refs[ j ], 0, 1, &bv );
968 ber_bvarray_add( &rdi.rdi_ref, &bv );
971 ldap_charray_free( refs );
973 } else if ( strncasecmp( argv[ i ], "sleeptime=", STRLENOF( "sleeptime=" ) ) == 0 )
975 if ( rdi.rdi_sleeptime != 0 ) {
976 fprintf( stderr, "%s: line %d: retcode: "
977 "\"sleeptime\" already provided.\n",
982 if ( lutil_atoi( &rdi.rdi_sleeptime, &argv[ i ][ STRLENOF( "sleeptime=" ) ] ) ) {
983 fprintf( stderr, "%s: line %d: retcode: "
984 "unable to parse \"sleeptime=%s\".\n",
985 fname, lineno, &argv[ i ][ STRLENOF( "sleeptime=" ) ] );
989 } else if ( strncasecmp( argv[ i ], "unsolicited=", STRLENOF( "unsolicited=" ) ) == 0 )
993 if ( !BER_BVISNULL( &rdi.rdi_unsolicited_oid ) ) {
994 fprintf( stderr, "%s: line %d: retcode: "
995 "\"unsolicited\" already provided.\n",
1000 data = strchr( &argv[ i ][ STRLENOF( "unsolicited=" ) ], ':' );
1001 if ( data != NULL ) {
1004 if ( ldif_parse_line2( &argv[ i ][ STRLENOF( "unsolicited=" ) ],
1005 &oid, &rdi.rdi_unsolicited_data, NULL ) )
1007 fprintf( stderr, "%s: line %d: retcode: "
1008 "unable to parse \"unsolicited\".\n",
1013 ber_dupbv( &rdi.rdi_unsolicited_oid, &oid );
1016 ber_str2bv( &argv[ i ][ STRLENOF( "unsolicited=" ) ], 0, 1,
1017 &rdi.rdi_unsolicited_oid );
1020 } else if ( strncasecmp( argv[ i ], "flags=", STRLENOF( "flags=" ) ) == 0 )
1022 char *arg = &argv[ i ][ STRLENOF( "flags=" ) ];
1023 if ( strcasecmp( arg, "disconnect" ) == 0 ) {
1024 rdi.rdi_flags |= RDI_PRE_DISCONNECT;
1026 } else if ( strcasecmp( arg, "pre-disconnect" ) == 0 ) {
1027 rdi.rdi_flags |= RDI_PRE_DISCONNECT;
1029 } else if ( strcasecmp( arg, "post-disconnect" ) == 0 ) {
1030 rdi.rdi_flags |= RDI_POST_DISCONNECT;
1033 fprintf( stderr, "%s: line %d: retcode: "
1034 "unknown flag \"%s\".\n",
1035 fname, lineno, arg );
1040 fprintf( stderr, "%s: line %d: retcode: "
1041 "unknown option \"%s\".\n",
1042 fname, lineno, argv[ i ] );
1048 for ( rdip = &rd->rd_item; *rdip; rdip = &(*rdip)->rdi_next )
1052 *rdip = ( retcode_item_t * )ch_malloc( sizeof( retcode_item_t ) );
1055 } else if ( strcasecmp( argv0, "indir" ) == 0 ) {
1056 rd->rd_flags |= RETCODE_FINDIR;
1058 } else if ( strcasecmp( argv0, "sleep" ) == 0 ) {
1061 fprintf( stderr, "%s: line %d: retcode: "
1062 "\"retcode-sleep <time>\": missing <time>\n",
1070 fprintf( stderr, "%s: line %d: retcode: "
1071 "\"retcode-sleep <time>\": extra cruft after <time>\n",
1076 if ( lutil_atoi( &rd->rd_sleep, argv[ 1 ] ) != 0 ) {
1077 fprintf( stderr, "%s: line %d: retcode: "
1078 "\"retcode-sleep <time>\": unable to parse <time>\n",
1084 return SLAP_CONF_UNKNOWN;
1091 retcode_db_open( BackendDB *be, ConfigReply *cr)
1093 slap_overinst *on = (slap_overinst *)be->bd_info;
1094 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
1096 retcode_item_t *rdi;
1098 for ( rdi = rd->rd_item; rdi; rdi = rdi->rdi_next ) {
1102 struct berval val[ 3 ];
1103 char buf[ SLAP_TEXT_BUFLEN ];
1106 rdi->rdi_e.e_name = rdi->rdi_dn;
1107 rdi->rdi_e.e_nname = rdi->rdi_ndn;
1110 val[ 0 ] = oc_errObject->soc_cname;
1111 val[ 1 ] = slap_schema.si_oc_extensibleObject->soc_cname;
1112 BER_BVZERO( &val[ 2 ] );
1114 attr_merge( &rdi->rdi_e, slap_schema.si_ad_objectClass, val, NULL );
1117 rc = ldap_bv2rdn( &rdi->rdi_dn, &rdn, (char **) &p,
1118 LDAP_DN_FORMAT_LDAP );
1120 assert( rc == LDAP_SUCCESS );
1122 for ( j = 0; rdn[ j ]; j++ ) {
1123 LDAPAVA *ava = rdn[ j ];
1124 AttributeDescription *ad = NULL;
1127 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
1128 assert( rc == LDAP_SUCCESS );
1130 attr_merge_normalize_one( &rdi->rdi_e, ad,
1131 &ava->la_value, NULL );
1134 ldap_rdnfree( rdn );
1137 snprintf( buf, sizeof( buf ), "%d", rdi->rdi_err );
1138 ber_str2bv( buf, 0, 0, &val[ 0 ] );
1140 attr_merge_one( &rdi->rdi_e, ad_errCode, &val[ 0 ], NULL );
1142 if ( rdi->rdi_ref != NULL ) {
1143 attr_merge_normalize( &rdi->rdi_e, slap_schema.si_ad_ref,
1144 rdi->rdi_ref, NULL );
1148 if ( !BER_BVISNULL( &rdi->rdi_text ) ) {
1149 val[ 0 ] = rdi->rdi_text;
1151 attr_merge_normalize_one( &rdi->rdi_e, ad_errText, &val[ 0 ], NULL );
1155 if ( !BER_BVISNULL( &rdi->rdi_matched ) ) {
1156 val[ 0 ] = rdi->rdi_matched;
1158 attr_merge_normalize_one( &rdi->rdi_e, ad_errMatchedDN, &val[ 0 ], NULL );
1162 if ( rdi->rdi_sleeptime ) {
1163 snprintf( buf, sizeof( buf ), "%d", rdi->rdi_sleeptime );
1164 ber_str2bv( buf, 0, 0, &val[ 0 ] );
1166 attr_merge_one( &rdi->rdi_e, ad_errSleepTime, &val[ 0 ], NULL );
1170 if ( rdi->rdi_mask & SN_DG_OP_ADD ) {
1171 BER_BVSTR( &val[ 0 ], "add" );
1172 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1175 if ( rdi->rdi_mask & SN_DG_OP_BIND ) {
1176 BER_BVSTR( &val[ 0 ], "bind" );
1177 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1180 if ( rdi->rdi_mask & SN_DG_OP_COMPARE ) {
1181 BER_BVSTR( &val[ 0 ], "compare" );
1182 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1185 if ( rdi->rdi_mask & SN_DG_OP_DELETE ) {
1186 BER_BVSTR( &val[ 0 ], "delete" );
1187 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1190 if ( rdi->rdi_mask & SN_DG_EXTENDED ) {
1191 BER_BVSTR( &val[ 0 ], "extended" );
1192 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1195 if ( rdi->rdi_mask & SN_DG_OP_MODIFY ) {
1196 BER_BVSTR( &val[ 0 ], "modify" );
1197 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1200 if ( rdi->rdi_mask & SN_DG_OP_RENAME ) {
1201 BER_BVSTR( &val[ 0 ], "rename" );
1202 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1205 if ( rdi->rdi_mask & SN_DG_OP_SEARCH ) {
1206 BER_BVSTR( &val[ 0 ], "search" );
1207 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1215 retcode_db_destroy( BackendDB *be, ConfigReply *cr )
1217 slap_overinst *on = (slap_overinst *)be->bd_info;
1218 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
1221 retcode_item_t *rdi, *next;
1223 for ( rdi = rd->rd_item; rdi != NULL; rdi = next ) {
1224 ber_memfree( rdi->rdi_dn.bv_val );
1225 ber_memfree( rdi->rdi_ndn.bv_val );
1227 if ( !BER_BVISNULL( &rdi->rdi_text ) ) {
1228 ber_memfree( rdi->rdi_text.bv_val );
1231 if ( !BER_BVISNULL( &rdi->rdi_matched ) ) {
1232 ber_memfree( rdi->rdi_matched.bv_val );
1235 if ( rdi->rdi_ref ) {
1236 ber_bvarray_free( rdi->rdi_ref );
1239 BER_BVZERO( &rdi->rdi_e.e_name );
1240 BER_BVZERO( &rdi->rdi_e.e_nname );
1242 entry_clean( &rdi->rdi_e );
1244 next = rdi->rdi_next;
1249 if ( !BER_BVISNULL( &rd->rd_pdn ) ) {
1250 ber_memfree( rd->rd_pdn.bv_val );
1253 if ( !BER_BVISNULL( &rd->rd_npdn ) ) {
1254 ber_memfree( rd->rd_npdn.bv_val );
1263 #if SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC
1265 #endif /* SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC */
1267 retcode_initialize( void )
1273 AttributeDescription **ad;
1275 { "( 1.3.6.1.4.1.4203.666.11.4.1.1 "
1276 "NAME ( 'errCode' ) "
1277 "DESC 'LDAP error code' "
1278 "EQUALITY integerMatch "
1279 "ORDERING integerOrderingMatch "
1280 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
1283 { "( 1.3.6.1.4.1.4203.666.11.4.1.2 "
1285 "DESC 'Operations the errObject applies to' "
1286 "EQUALITY caseIgnoreMatch "
1287 "SUBSTR caseIgnoreSubstringsMatch "
1288 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
1290 { "( 1.3.6.1.4.1.4203.666.11.4.1.3 "
1291 "NAME ( 'errText' ) "
1292 "DESC 'LDAP error textual description' "
1293 "EQUALITY caseIgnoreMatch "
1294 "SUBSTR caseIgnoreSubstringsMatch "
1295 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
1298 { "( 1.3.6.1.4.1.4203.666.11.4.1.4 "
1299 "NAME ( 'errSleepTime' ) "
1300 "DESC 'Time to wait before returning the error' "
1301 "EQUALITY integerMatch "
1302 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
1305 { "( 1.3.6.1.4.1.4203.666.11.4.1.5 "
1306 "NAME ( 'errMatchedDN' ) "
1307 "DESC 'Value to be returned as matched DN' "
1308 "EQUALITY distinguishedNameMatch "
1309 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
1312 { "( 1.3.6.1.4.1.4203.666.11.4.1.6 "
1313 "NAME ( 'errUnsolicitedOID' ) "
1314 "DESC 'OID to be returned within unsolicited response' "
1315 "EQUALITY objectIdentifierMatch "
1316 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 "
1318 &ad_errUnsolicitedOID },
1319 { "( 1.3.6.1.4.1.4203.666.11.4.1.7 "
1320 "NAME ( 'errUnsolicitedData' ) "
1321 "DESC 'Data to be returned within unsolicited response' "
1322 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 "
1324 &ad_errUnsolicitedData },
1325 { "( 1.3.6.1.4.1.4203.666.11.4.1.8 "
1326 "NAME ( 'errDisconnect' ) "
1327 "DESC 'Disconnect without notice' "
1328 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
1330 &ad_errDisconnect },
1338 { "( 1.3.6.1.4.1.4203.666.11.4.3.0 "
1339 "NAME ( 'errAbsObject' ) "
1349 "$ errUnsolicitedOID "
1350 "$ errUnsolicitedData "
1354 { "( 1.3.6.1.4.1.4203.666.11.4.3.1 "
1355 "NAME ( 'errObject' ) "
1356 "SUP errAbsObject STRUCTURAL "
1359 { "( 1.3.6.1.4.1.4203.666.11.4.3.2 "
1360 "NAME ( 'errAuxObject' ) "
1361 "SUP errAbsObject AUXILIARY "
1368 for ( i = 0; retcode_at[ i ].desc != NULL; i++ ) {
1369 code = register_at( retcode_at[ i ].desc, retcode_at[ i ].ad, 0 );
1371 Debug( LDAP_DEBUG_ANY,
1372 "retcode: register_at failed\n", 0, 0, 0 );
1377 for ( i = 0; retcode_oc[ i ].desc != NULL; i++ ) {
1378 code = register_oc( retcode_oc[ i ].desc, retcode_oc[ i ].oc, 0 );
1380 Debug( LDAP_DEBUG_ANY,
1381 "retcode: register_oc failed\n", 0, 0, 0 );
1386 retcode.on_bi.bi_type = "retcode";
1388 retcode.on_bi.bi_db_init = retcode_db_init;
1389 retcode.on_bi.bi_db_config = retcode_db_config;
1390 retcode.on_bi.bi_db_open = retcode_db_open;
1391 retcode.on_bi.bi_db_destroy = retcode_db_destroy;
1393 retcode.on_bi.bi_op_add = retcode_op_func;
1394 retcode.on_bi.bi_op_bind = retcode_op_func;
1395 retcode.on_bi.bi_op_compare = retcode_op_func;
1396 retcode.on_bi.bi_op_delete = retcode_op_func;
1397 retcode.on_bi.bi_op_modify = retcode_op_func;
1398 retcode.on_bi.bi_op_modrdn = retcode_op_func;
1399 retcode.on_bi.bi_op_search = retcode_op_func;
1401 retcode.on_bi.bi_extended = retcode_op_func;
1403 retcode.on_response = retcode_response;
1405 return overlay_register( &retcode );
1408 #if SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC
1410 init_module( int argc, char *argv[] )
1412 return retcode_initialize();
1414 #endif /* SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC */
1416 #endif /* SLAPD_OVER_RETCODE */