1 /* retcode.c - customizable response for client testing purposes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005-2006 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>
36 static slap_overinst retcode;
38 static AttributeDescription *ad_errCode;
39 static AttributeDescription *ad_errText;
40 static AttributeDescription *ad_errOp;
41 static AttributeDescription *ad_errSleepTime;
42 static AttributeDescription *ad_errMatchedDN;
43 static ObjectClass *oc_errAbsObject;
44 static ObjectClass *oc_errObject;
45 static ObjectClass *oc_errAuxObject;
47 typedef enum retcode_op_e {
48 SN_DG_OP_NONE = 0x0000,
49 SN_DG_OP_ADD = 0x0001,
50 SN_DG_OP_BIND = 0x0002,
51 SN_DG_OP_COMPARE = 0x0004,
52 SN_DG_OP_DELETE = 0x0008,
53 SN_DG_OP_MODIFY = 0x0010,
54 SN_DG_OP_RENAME = 0x0020,
55 SN_DG_OP_SEARCH = 0x0040,
56 SN_DG_EXTENDED = 0x0080,
57 SN_DG_OP_AUTH = SN_DG_OP_BIND,
58 SN_DG_OP_READ = (SN_DG_OP_COMPARE|SN_DG_OP_SEARCH),
59 SN_DG_OP_WRITE = (SN_DG_OP_ADD|SN_DG_OP_DELETE|SN_DG_OP_MODIFY|SN_DG_OP_RENAME),
60 SN_DG_OP_ALL = (SN_DG_OP_AUTH|SN_DG_OP_READ|SN_DG_OP_WRITE|SN_DG_EXTENDED)
63 typedef struct retcode_item_t {
65 struct berval rdi_ndn;
66 struct berval rdi_text;
67 struct berval rdi_matched;
73 struct retcode_item_t *rdi_next;
76 typedef struct retcode_t {
78 struct berval rd_npdn;
82 retcode_item_t *rd_item;
85 #define RETCODE_FNONE 0x00
86 #define RETCODE_FINDIR 0x01
87 #define RETCODE_INDIR( rd ) ( (rd)->rd_flags & RETCODE_FINDIR )
91 retcode_entry_response( Operation *op, SlapReply *rs, BackendInfo *bi, Entry *e );
94 retcode_cleanup_cb( Operation *op, SlapReply *rs )
96 rs->sr_matched = NULL;
99 if ( rs->sr_ref != NULL ) {
100 ber_bvarray_free( rs->sr_ref );
104 ch_free( op->o_callback );
105 op->o_callback = NULL;
107 return SLAP_CB_CONTINUE;
111 retcode_send_onelevel( Operation *op, SlapReply *rs )
113 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
114 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
118 for ( rdi = rd->rd_item; rdi != NULL; rdi = rdi->rdi_next ) {
119 if ( op->o_abandon ) {
120 return rs->sr_err = SLAPD_ABANDON;
123 rs->sr_err = test_filter( op, &rdi->rdi_e, op->ors_filter );
124 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
125 if ( op->ors_slimit == rs->sr_nentries ) {
126 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
131 rs->sr_attrs = op->ors_attrs;
132 rs->sr_operational_attrs = NULL;
135 rs->sr_err = LDAP_SUCCESS;
136 rs->sr_entry = &rdi->rdi_e;
138 rs->sr_err = send_search_entry( op, rs );
141 switch ( rs->sr_err ) {
142 case LDAP_UNAVAILABLE: /* connection closed */
143 rs->sr_err = LDAP_OTHER;
145 case LDAP_SIZELIMIT_EXCEEDED:
149 rs->sr_err = LDAP_SUCCESS;
154 send_ldap_result( op, rs );
160 retcode_op_add( Operation *op, SlapReply *rs )
162 return retcode_entry_response( op, rs, NULL, op->ora_e );
165 typedef struct retcode_cb_t {
166 BackendInfo *rdc_info;
169 AttributeName *rdc_attrs;
173 retcode_cb_response( Operation *op, SlapReply *rs )
175 retcode_cb_t *rdc = (retcode_cb_t *)op->o_callback->sc_private;
177 if ( rs->sr_type == REP_SEARCH ) {
178 ber_tag_t o_tag = op->o_tag;
181 op->o_tag = rdc->rdc_tag;
182 if ( op->o_tag == LDAP_REQ_SEARCH ) {
183 rs->sr_attrs = rdc->rdc_attrs;
185 rc = retcode_entry_response( op, rs, rdc->rdc_info, rs->sr_entry );
191 if ( rs->sr_err == LDAP_SUCCESS ) {
192 if ( !op->o_abandon ) {
193 rdc->rdc_flags = SLAP_CB_CONTINUE;
198 return SLAP_CB_CONTINUE;
202 retcode_op_internal( Operation *op, SlapReply *rs )
204 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
207 BackendDB db = *op->o_bd;
208 slap_callback sc = { 0 };
213 op2.o_tag = LDAP_REQ_SEARCH;
214 op2.ors_scope = LDAP_SCOPE_BASE;
215 op2.ors_deref = LDAP_DEREF_NEVER;
216 op2.ors_tlimit = SLAP_NO_LIMIT;
217 op2.ors_slimit = SLAP_NO_LIMIT;
218 op2.ors_limit = NULL;
219 op2.ors_attrsonly = 0;
220 op2.ors_attrs = slap_anlist_all_attributes;
222 ber_str2bv_x( "(objectClass=errAbsObject)",
223 STRLENOF( "(objectClass=errAbsObject)" ),
224 1, &op2.ors_filterstr, op2.o_tmpmemctx );
225 op2.ors_filter = str2filter_x( &op2, op2.ors_filterstr.bv_val );
227 db.bd_info = on->on_info->oi_orig;
230 rdc.rdc_info = on->on_info->oi_orig;
231 rdc.rdc_flags = RETCODE_FINDIR;
232 if ( op->o_tag == LDAP_REQ_SEARCH ) {
233 rdc.rdc_attrs = op->ors_attrs;
235 rdc.rdc_tag = op->o_tag;
236 sc.sc_response = retcode_cb_response;
237 sc.sc_private = &rdc;
238 op2.o_callback = ≻
240 rc = op2.o_bd->be_search( &op2, rs );
241 op->o_abandon = op2.o_abandon;
243 filter_free_x( &op2, op2.ors_filter );
244 ber_memfree_x( op2.ors_filterstr.bv_val, op2.o_tmpmemctx );
246 if ( rdc.rdc_flags == SLAP_CB_CONTINUE ) {
247 return SLAP_CB_CONTINUE;
254 retcode_op_func( Operation *op, SlapReply *rs )
256 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
257 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
260 struct berval nrdn, npdn;
262 slap_callback *cb = NULL;
264 /* sleep as required */
265 if ( rd->rd_sleep < 0 ) {
266 sleep( rand() % ( - rd->rd_sleep ) );
268 } else if ( rd->rd_sleep > 0 ) {
269 sleep( rd->rd_sleep );
272 if ( !dnIsSuffix( &op->o_req_ndn, &rd->rd_npdn ) ) {
273 if ( RETCODE_INDIR( rd ) ) {
274 switch ( op->o_tag ) {
276 return retcode_op_add( op, rs );
280 if ( be_isroot_pw( op ) ) {
281 return SLAP_CB_CONTINUE;
283 return retcode_op_internal( op, rs );
285 case LDAP_REQ_SEARCH:
286 if ( op->ors_scope == LDAP_SCOPE_BASE ) {
287 rs->sr_err = retcode_op_internal( op, rs );
288 switch ( rs->sr_err ) {
289 case SLAP_CB_CONTINUE:
290 if ( rs->sr_nentries == 0 ) {
293 rs->sr_err = LDAP_SUCCESS;
297 send_ldap_result( op, rs );
304 case LDAP_REQ_MODIFY:
305 case LDAP_REQ_DELETE:
306 case LDAP_REQ_MODRDN:
307 case LDAP_REQ_COMPARE:
308 return retcode_op_internal( op, rs );
312 return SLAP_CB_CONTINUE;
315 if ( op->o_tag == LDAP_REQ_SEARCH
316 && op->ors_scope != LDAP_SCOPE_BASE
317 && op->o_req_ndn.bv_len == rd->rd_npdn.bv_len )
319 return retcode_send_onelevel( op, rs );
322 dnParent( &op->o_req_ndn, &npdn );
323 if ( npdn.bv_len != rd->rd_npdn.bv_len ) {
324 rs->sr_err = LDAP_NO_SUCH_OBJECT;
325 rs->sr_matched = rd->rd_pdn.bv_val;
326 send_ldap_result( op, rs );
327 rs->sr_matched = NULL;
331 dnRdn( &op->o_req_ndn, &nrdn );
333 for ( rdi = rd->rd_item; rdi != NULL; rdi = rdi->rdi_next ) {
334 struct berval rdi_nrdn;
336 dnRdn( &rdi->rdi_ndn, &rdi_nrdn );
337 if ( dn_match( &nrdn, &rdi_nrdn ) ) {
342 if ( rdi != NULL && rdi->rdi_mask != SN_DG_OP_ALL ) {
343 retcode_op_e o_tag = SN_DG_OP_NONE;
345 switch ( op->o_tag ) {
347 o_tag = SN_DG_OP_ADD;
351 o_tag = SN_DG_OP_BIND;
354 case LDAP_REQ_COMPARE:
355 o_tag = SN_DG_OP_COMPARE;
358 case LDAP_REQ_DELETE:
359 o_tag = SN_DG_OP_DELETE;
362 case LDAP_REQ_MODIFY:
363 o_tag = SN_DG_OP_MODIFY;
366 case LDAP_REQ_MODRDN:
367 o_tag = SN_DG_OP_RENAME;
370 case LDAP_REQ_SEARCH:
371 o_tag = SN_DG_OP_SEARCH;
374 case LDAP_REQ_EXTENDED:
375 o_tag = SN_DG_EXTENDED;
379 /* Should not happen */
383 if ( !( o_tag & rdi->rdi_mask ) ) {
384 return SLAP_CB_CONTINUE;
389 rs->sr_matched = rd->rd_pdn.bv_val;
390 rs->sr_err = LDAP_NO_SUCH_OBJECT;
391 rs->sr_text = "retcode not found";
394 rs->sr_err = rdi->rdi_err;
395 rs->sr_text = rdi->rdi_text.bv_val;
396 rs->sr_matched = rdi->rdi_matched.bv_val;
398 /* FIXME: we only honor the rdi_ref field in case rdi_err
399 * is LDAP_REFERRAL otherwise send_ldap_result() bails out */
400 if ( rs->sr_err == LDAP_REFERRAL ) {
403 if ( rdi->rdi_ref != NULL ) {
406 ref = default_referral;
410 rs->sr_ref = referral_rewrite( ref,
411 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
414 rs->sr_err = LDAP_OTHER;
415 rs->sr_text = "bad referral object";
419 if ( rdi->rdi_sleeptime > 0 ) {
420 sleep( rdi->rdi_sleeptime );
424 switch ( op->o_tag ) {
425 case LDAP_REQ_EXTENDED:
429 cb = ( slap_callback * )ch_malloc( sizeof( slap_callback ) );
430 memset( cb, 0, sizeof( slap_callback ) );
431 cb->sc_cleanup = retcode_cleanup_cb;
436 send_ldap_result( op, rs );
437 if ( rs->sr_ref != NULL ) {
438 ber_bvarray_free( rs->sr_ref );
441 rs->sr_matched = NULL;
450 retcode_op2str( ber_tag_t op, struct berval *bv )
454 BER_BVSTR( bv, "bind" );
457 BER_BVSTR( bv, "add" );
459 case LDAP_REQ_DELETE:
460 BER_BVSTR( bv, "delete" );
462 case LDAP_REQ_MODRDN:
463 BER_BVSTR( bv, "modrdn" );
465 case LDAP_REQ_MODIFY:
466 BER_BVSTR( bv, "modify" );
468 case LDAP_REQ_COMPARE:
469 BER_BVSTR( bv, "compare" );
471 case LDAP_REQ_SEARCH:
472 BER_BVSTR( bv, "search" );
474 case LDAP_REQ_EXTENDED:
475 BER_BVSTR( bv, "extended" );
482 retcode_entry_response( Operation *op, SlapReply *rs, BackendInfo *bi, Entry *e )
488 if ( get_manageDSAit( op ) ) {
489 return SLAP_CB_CONTINUE;
492 if ( !is_entry_objectclass_or_sub( e, oc_errAbsObject ) ) {
493 return SLAP_CB_CONTINUE;
497 a = attr_find( e->e_attrs, ad_errOp );
501 struct berval bv = BER_BVNULL;
503 (void)retcode_op2str( op->o_tag, &bv );
505 if ( BER_BVISNULL( &bv ) ) {
506 return SLAP_CB_CONTINUE;
509 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
510 if ( bvmatch( &a->a_nvals[ i ], &bv ) ) {
517 return SLAP_CB_CONTINUE;
522 a = attr_find( e->e_attrs, ad_errCode );
524 return SLAP_CB_CONTINUE;
526 err = strtol( a->a_nvals[ 0 ].bv_val, &next, 0 );
527 if ( next == a->a_nvals[ 0 ].bv_val || next[ 0 ] != '\0' ) {
528 return SLAP_CB_CONTINUE;
533 a = attr_find( e->e_attrs, ad_errSleepTime );
537 sleepTime = strtoul( a->a_nvals[ 0 ].bv_val, &next, 0 );
538 if ( next != a->a_nvals[ 0 ].bv_val && next[ 0 ] == '\0' ) {
543 if ( rs->sr_err != LDAP_SUCCESS ) {
544 BackendDB db = *op->o_bd,
546 void *o_callback = op->o_callback;
549 a = attr_find( e->e_attrs, ad_errText );
551 rs->sr_text = a->a_vals[ 0 ].bv_val;
555 a = attr_find( e->e_attrs, ad_errMatchedDN );
557 rs->sr_matched = a->a_vals[ 0 ].bv_val;
561 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
563 bi = on->on_info->oi_orig;
568 op->o_callback = NULL;
571 if ( rs->sr_err == LDAP_REFERRAL ) {
572 BerVarray refs = default_referral;
574 a = attr_find( e->e_attrs, slap_schema.si_ad_ref );
578 rs->sr_ref = referral_rewrite( refs,
579 NULL, &op->o_req_dn, op->oq_search.rs_scope );
581 send_search_reference( op, rs );
582 ber_bvarray_free( rs->sr_ref );
586 send_ldap_result( op, rs );
590 rs->sr_matched = NULL;
592 op->o_callback = o_callback;
595 if ( rs->sr_err != LDAP_SUCCESS ) {
600 return SLAP_CB_CONTINUE;
604 retcode_response( Operation *op, SlapReply *rs )
606 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
607 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
609 if ( rs->sr_type != REP_SEARCH || !RETCODE_INDIR( rd ) ) {
610 return SLAP_CB_CONTINUE;
613 return retcode_entry_response( op, rs, NULL, rs->sr_entry );
617 retcode_db_init( BackendDB *be )
619 slap_overinst *on = (slap_overinst *)be->bd_info;
622 rd = (retcode_t *)ch_malloc( sizeof( retcode_t ) );
623 memset( rd, 0, sizeof( retcode_t ) );
625 on->on_bi.bi_private = (void *)rd;
638 slap_overinst *on = (slap_overinst *)be->bd_info;
639 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
641 char *argv0 = argv[ 0 ] + STRLENOF( "retcode-" );
643 if ( strncasecmp( argv[ 0 ], "retcode-", STRLENOF( "retcode-" ) ) != 0 ) {
644 return SLAP_CONF_UNKNOWN;
647 if ( strcasecmp( argv0, "parent" ) == 0 ) {
652 fprintf( stderr, "%s: line %d: retcode: "
653 "\"retcode-parent <DN>\": missing <DN>\n",
658 if ( !BER_BVISNULL( &rd->rd_pdn ) ) {
659 fprintf( stderr, "%s: line %d: retcode: "
660 "parent already defined.\n", fname, lineno );
664 ber_str2bv( argv[ 1 ], 0, 0, &dn );
666 rc = dnPrettyNormal( NULL, &dn, &rd->rd_pdn, &rd->rd_npdn, NULL );
667 if ( rc != LDAP_SUCCESS ) {
668 fprintf( stderr, "%s: line %d: retcode: "
669 "unable to normalize parent DN \"%s\": %d\n",
670 fname, lineno, argv[ 1 ], rc );
674 } else if ( strcasecmp( argv0, "item" ) == 0 ) {
675 retcode_item_t rdi = { BER_BVNULL }, **rdip;
676 struct berval bv, rdn, nrdn;
681 fprintf( stderr, "%s: line %d: retcode: "
682 "\"retcode-item <RDN> <retcode> [<text>]\": "
688 ber_str2bv( argv[ 1 ], 0, 0, &bv );
690 rc = dnPrettyNormal( NULL, &bv, &rdn, &nrdn, NULL );
691 if ( rc != LDAP_SUCCESS ) {
692 fprintf( stderr, "%s: line %d: retcode: "
693 "unable to normalize RDN \"%s\": %d\n",
694 fname, lineno, argv[ 1 ], rc );
698 if ( !dnIsOneLevelRDN( &nrdn ) ) {
699 fprintf( stderr, "%s: line %d: retcode: "
700 "value \"%s\" is not a RDN\n",
701 fname, lineno, argv[ 1 ] );
705 if ( BER_BVISNULL( &rd->rd_npdn ) ) {
706 /* FIXME: we use the database suffix */
707 if ( be->be_nsuffix == NULL ) {
708 fprintf( stderr, "%s: line %d: retcode: "
709 "either \"retcode-parent\" "
710 "or \"suffix\" must be defined.\n",
715 ber_dupbv( &rd->rd_pdn, &be->be_suffix[ 0 ] );
716 ber_dupbv( &rd->rd_npdn, &be->be_nsuffix[ 0 ] );
719 build_new_dn( &rdi.rdi_dn, &rd->rd_pdn, &rdn, NULL );
720 build_new_dn( &rdi.rdi_ndn, &rd->rd_npdn, &nrdn, NULL );
722 ch_free( rdn.bv_val );
723 ch_free( nrdn.bv_val );
725 rdi.rdi_err = strtol( argv[ 2 ], &next, 0 );
726 if ( next == argv[ 2 ] || next[ 0 ] != '\0' ) {
727 fprintf( stderr, "%s: line %d: retcode: "
728 "unable to parse return code \"%s\"\n",
729 fname, lineno, argv[ 2 ] );
733 rdi.rdi_mask = SN_DG_OP_ALL;
738 for ( i = 3; i < argc; i++ ) {
739 if ( strncasecmp( argv[ i ], "op=", STRLENOF( "op=" ) ) == 0 )
744 ops = ldap_str2charray( &argv[ i ][ STRLENOF( "op=" ) ], "," );
745 assert( ops != NULL );
747 rdi.rdi_mask = SN_DG_OP_NONE;
749 for ( j = 0; ops[ j ] != NULL; j++ ) {
750 if ( strcasecmp( ops[ j ], "add" ) == 0 ) {
751 rdi.rdi_mask |= SN_DG_OP_ADD;
753 } else if ( strcasecmp( ops[ j ], "bind" ) == 0 ) {
754 rdi.rdi_mask |= SN_DG_OP_BIND;
756 } else if ( strcasecmp( ops[ j ], "compare" ) == 0 ) {
757 rdi.rdi_mask |= SN_DG_OP_COMPARE;
759 } else if ( strcasecmp( ops[ j ], "delete" ) == 0 ) {
760 rdi.rdi_mask |= SN_DG_OP_DELETE;
762 } else if ( strcasecmp( ops[ j ], "modify" ) == 0 ) {
763 rdi.rdi_mask |= SN_DG_OP_MODIFY;
765 } else if ( strcasecmp( ops[ j ], "rename" ) == 0
766 || strcasecmp( ops[ j ], "modrdn" ) == 0 )
768 rdi.rdi_mask |= SN_DG_OP_RENAME;
770 } else if ( strcasecmp( ops[ j ], "search" ) == 0 ) {
771 rdi.rdi_mask |= SN_DG_OP_SEARCH;
773 } else if ( strcasecmp( ops[ j ], "extended" ) == 0 ) {
774 rdi.rdi_mask |= SN_DG_EXTENDED;
776 } else if ( strcasecmp( ops[ j ], "auth" ) == 0 ) {
777 rdi.rdi_mask |= SN_DG_OP_AUTH;
779 } else if ( strcasecmp( ops[ j ], "read" ) == 0 ) {
780 rdi.rdi_mask |= SN_DG_OP_READ;
782 } else if ( strcasecmp( ops[ j ], "write" ) == 0 ) {
783 rdi.rdi_mask |= SN_DG_OP_WRITE;
785 } else if ( strcasecmp( ops[ j ], "all" ) == 0 ) {
786 rdi.rdi_mask |= SN_DG_OP_ALL;
789 fprintf( stderr, "retcode: unknown op \"%s\"\n",
795 ldap_charray_free( ops );
797 } else if ( strncasecmp( argv[ i ], "text=", STRLENOF( "text=" ) ) == 0 )
799 if ( !BER_BVISNULL( &rdi.rdi_text ) ) {
800 fprintf( stderr, "%s: line %d: retcode: "
801 "\"text\" already provided.\n",
805 ber_str2bv( &argv[ i ][ STRLENOF( "text=" ) ], 0, 1, &rdi.rdi_text );
807 } else if ( strncasecmp( argv[ i ], "matched=", STRLENOF( "matched=" ) ) == 0 )
811 if ( !BER_BVISNULL( &rdi.rdi_matched ) ) {
812 fprintf( stderr, "%s: line %d: retcode: "
813 "\"matched\" already provided.\n",
817 ber_str2bv( &argv[ i ][ STRLENOF( "matched=" ) ], 0, 0, &dn );
818 if ( dnPretty( NULL, &dn, &rdi.rdi_matched, NULL ) != LDAP_SUCCESS ) {
819 fprintf( stderr, "%s: line %d: retcode: "
820 "unable to prettify matched DN \"%s\".\n",
821 fname, lineno, &argv[ i ][ STRLENOF( "matched=" ) ] );
825 } else if ( strncasecmp( argv[ i ], "ref=", STRLENOF( "ref=" ) ) == 0 )
830 if ( rdi.rdi_ref != NULL ) {
831 fprintf( stderr, "%s: line %d: retcode: "
832 "\"ref\" already provided.\n",
837 if ( rdi.rdi_err != LDAP_REFERRAL ) {
838 fprintf( stderr, "%s: line %d: retcode: "
839 "providing \"ref\"\n"
840 "\talong with a non-referral "
841 "resultCode may cause slapd failures\n"
842 "\trelated to internal checks.\n",
846 refs = ldap_str2charray( &argv[ i ][ STRLENOF( "ref=" ) ], " " );
847 assert( refs != NULL );
849 for ( j = 0; refs[ j ] != NULL; j++ ) {
852 ber_str2bv( refs[ j ], 0, 1, &bv );
853 ber_bvarray_add( &rdi.rdi_ref, &bv );
856 ldap_charray_free( refs );
858 } else if ( strncasecmp( argv[ i ], "sleeptime=", STRLENOF( "sleeptime=" ) ) == 0 )
861 if ( rdi.rdi_sleeptime != 0 ) {
862 fprintf( stderr, "%s: line %d: retcode: "
863 "\"sleeptime\" already provided.\n",
868 rdi.rdi_sleeptime = strtol( &argv[ i ][ STRLENOF( "sleeptime=" ) ], &next, 10 );
869 if ( next == argv[ i ] || next[ 0 ] != '\0' ) {
870 fprintf( stderr, "%s: line %d: retcode: "
871 "unable to parse \"sleeptime=%s\".\n",
872 fname, lineno, &argv[ i ][ STRLENOF( "sleeptime=" ) ] );
877 fprintf( stderr, "%s: line %d: retcode: "
878 "unknown option \"%s\".\n",
879 fname, lineno, argv[ i ] );
885 for ( rdip = &rd->rd_item; *rdip; rdip = &(*rdip)->rdi_next )
889 *rdip = ( retcode_item_t * )ch_malloc( sizeof( retcode_item_t ) );
892 } else if ( strcasecmp( argv0, "indir" ) == 0 ) {
893 rd->rd_flags |= RETCODE_FINDIR;
895 } else if ( strcasecmp( argv0, "sleep" ) == 0 ) {
898 fprintf( stderr, "%s: line %d: retcode: "
899 "\"retcode-sleep <time>\": missing <time>\n",
907 fprintf( stderr, "%s: line %d: retcode: "
908 "\"retcode-sleep <time>\": extra cruft after <time>\n",
913 if ( lutil_atoi( &rd->rd_sleep, argv[ 1 ] ) != 0 ) {
914 fprintf( stderr, "%s: line %d: retcode: "
915 "\"retcode-sleep <time>\": unable to parse <time>\n",
921 return SLAP_CONF_UNKNOWN;
928 retcode_db_open( BackendDB *be )
930 slap_overinst *on = (slap_overinst *)be->bd_info;
931 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
935 for ( rdi = rd->rd_item; rdi; rdi = rdi->rdi_next ) {
939 struct berval val[ 3 ];
940 char buf[ SLAP_TEXT_BUFLEN ];
943 rdi->rdi_e.e_name = rdi->rdi_dn;
944 rdi->rdi_e.e_nname = rdi->rdi_ndn;
947 val[ 0 ] = oc_errObject->soc_cname;
948 val[ 1 ] = slap_schema.si_oc_extensibleObject->soc_cname;
949 BER_BVZERO( &val[ 2 ] );
951 attr_merge( &rdi->rdi_e, slap_schema.si_ad_objectClass, val, NULL );
954 rc = ldap_bv2rdn( &rdi->rdi_dn, &rdn, (char **) &p,
955 LDAP_DN_FORMAT_LDAP );
957 assert( rc == LDAP_SUCCESS );
959 for ( j = 0; rdn[ j ]; j++ ) {
960 LDAPAVA *ava = rdn[ j ];
961 AttributeDescription *ad = NULL;
964 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
965 assert( rc == LDAP_SUCCESS );
967 attr_merge_normalize_one( &rdi->rdi_e, ad,
968 &ava->la_value, NULL );
974 snprintf( buf, sizeof( buf ), "%d", rdi->rdi_err );
975 ber_str2bv( buf, 0, 0, &val[ 0 ] );
977 attr_merge_one( &rdi->rdi_e, ad_errCode, &val[ 0 ], NULL );
979 if ( rdi->rdi_ref != NULL ) {
980 attr_merge_normalize( &rdi->rdi_e, slap_schema.si_ad_ref,
981 rdi->rdi_ref, NULL );
985 if ( !BER_BVISNULL( &rdi->rdi_text ) ) {
986 val[ 0 ] = rdi->rdi_text;
988 attr_merge_normalize_one( &rdi->rdi_e, ad_errText, &val[ 0 ], NULL );
992 if ( !BER_BVISNULL( &rdi->rdi_matched ) ) {
993 val[ 0 ] = rdi->rdi_matched;
995 attr_merge_normalize_one( &rdi->rdi_e, ad_errMatchedDN, &val[ 0 ], NULL );
999 if ( rdi->rdi_sleeptime > 0 ) {
1000 snprintf( buf, sizeof( buf ), "%d", rdi->rdi_sleeptime );
1001 ber_str2bv( buf, 0, 0, &val[ 0 ] );
1003 attr_merge_one( &rdi->rdi_e, ad_errSleepTime, &val[ 0 ], NULL );
1007 if ( rdi->rdi_mask & SN_DG_OP_ADD ) {
1008 BER_BVSTR( &val[ 0 ], "add" );
1009 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1012 if ( rdi->rdi_mask & SN_DG_OP_BIND ) {
1013 BER_BVSTR( &val[ 0 ], "bind" );
1014 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1017 if ( rdi->rdi_mask & SN_DG_OP_COMPARE ) {
1018 BER_BVSTR( &val[ 0 ], "compare" );
1019 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1022 if ( rdi->rdi_mask & SN_DG_OP_DELETE ) {
1023 BER_BVSTR( &val[ 0 ], "delete" );
1024 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1027 if ( rdi->rdi_mask & SN_DG_EXTENDED ) {
1028 BER_BVSTR( &val[ 0 ], "extended" );
1029 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1032 if ( rdi->rdi_mask & SN_DG_OP_MODIFY ) {
1033 BER_BVSTR( &val[ 0 ], "modify" );
1034 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1037 if ( rdi->rdi_mask & SN_DG_OP_RENAME ) {
1038 BER_BVSTR( &val[ 0 ], "rename" );
1039 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1042 if ( rdi->rdi_mask & SN_DG_OP_SEARCH ) {
1043 BER_BVSTR( &val[ 0 ], "search" );
1044 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1052 retcode_db_destroy( BackendDB *be )
1054 slap_overinst *on = (slap_overinst *)be->bd_info;
1055 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
1058 retcode_item_t *rdi, *next;
1060 for ( rdi = rd->rd_item; rdi != NULL; rdi = next ) {
1061 ber_memfree( rdi->rdi_dn.bv_val );
1062 ber_memfree( rdi->rdi_ndn.bv_val );
1064 if ( !BER_BVISNULL( &rdi->rdi_text ) ) {
1065 ber_memfree( rdi->rdi_text.bv_val );
1068 if ( !BER_BVISNULL( &rdi->rdi_matched ) ) {
1069 ber_memfree( rdi->rdi_matched.bv_val );
1072 if ( rdi->rdi_ref ) {
1073 ber_bvarray_free( rdi->rdi_ref );
1076 BER_BVZERO( &rdi->rdi_e.e_name );
1077 BER_BVZERO( &rdi->rdi_e.e_nname );
1079 entry_clean( &rdi->rdi_e );
1081 next = rdi->rdi_next;
1086 if ( !BER_BVISNULL( &rd->rd_pdn ) ) {
1087 ber_memfree( rd->rd_pdn.bv_val );
1090 if ( !BER_BVISNULL( &rd->rd_npdn ) ) {
1091 ber_memfree( rd->rd_npdn.bv_val );
1100 #if SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC
1102 #endif /* SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC */
1104 retcode_initialize( void )
1112 AttributeDescription **ad;
1114 { "errCode", "( 1.3.6.1.4.1.4203.666.11.4.1.1 "
1115 "NAME ( 'errCode' ) "
1116 "DESC 'LDAP error code' "
1117 "EQUALITY integerMatch "
1118 "ORDERING integerOrderingMatch "
1119 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
1122 { "errOp", "( 1.3.6.1.4.1.4203.666.11.4.1.2 "
1124 "DESC 'Operations the errObject applies to' "
1125 "EQUALITY caseIgnoreMatch "
1126 "SUBSTR caseIgnoreSubstringsMatch "
1127 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
1129 { "errText", "( 1.3.6.1.4.1.4203.666.11.4.1.3 "
1130 "NAME ( 'errText' ) "
1131 "DESC 'LDAP error textual description' "
1132 "EQUALITY caseIgnoreMatch "
1133 "SUBSTR caseIgnoreSubstringsMatch "
1134 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
1137 { "errSleepTime", "( 1.3.6.1.4.1.4203.666.11.4.1.4 "
1138 "NAME ( 'errSleepTime' ) "
1139 "DESC 'Time to wait before returning the error' "
1140 "EQUALITY integerMatch "
1141 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
1144 { "errMatchedDN", "( 1.3.6.1.4.1.4203.666.11.4.1.5 "
1145 "NAME ( 'errMatchedDN' ) "
1146 "DESC 'Value to be returned as matched DN' "
1147 "EQUALITY distinguishedNameMatch "
1148 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
1159 { "errAbsObject", "( 1.3.6.1.4.1.4203.666.11.4.3.0 "
1160 "NAME ( 'errAbsObject' ) "
1172 { "errObject", "( 1.3.6.1.4.1.4203.666.11.4.3.1 "
1173 "NAME ( 'errObject' ) "
1174 "SUP errAbsObject STRUCTURAL "
1177 { "errAuxObject", "( 1.3.6.1.4.1.4203.666.11.4.3.2 "
1178 "NAME ( 'errAuxObject' ) "
1179 "SUP errAbsObject AUXILIARY "
1186 for ( i = 0; retcode_at[ i ].name != NULL; i++ ) {
1187 LDAPAttributeType *at;
1189 at = ldap_str2attributetype( retcode_at[ i ].desc,
1190 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
1192 fprintf( stderr, "retcode: "
1193 "AttributeType load failed: %s %s\n",
1194 ldap_scherr2str( code ), err );
1198 #if LDAP_VENDOR_VERSION_MINOR == X || LDAP_VENDOR_VERSION_MINOR > 2
1199 code = at_add( at, 0, NULL, &err );
1201 code = at_add( at, &err );
1204 if ( code != LDAP_SUCCESS ) {
1205 fprintf( stderr, "retcode: "
1206 "AttributeType load failed: %s %s\n",
1207 scherr2str( code ), err );
1211 code = slap_str2ad( retcode_at[ i ].name,
1212 retcode_at[ i ].ad, &err );
1213 if ( code != LDAP_SUCCESS ) {
1214 fprintf( stderr, "retcode: unable to find "
1215 "AttributeDescription \"%s\": %d (%s)\n",
1216 retcode_at[ i ].name, code, err );
1221 for ( i = 0; retcode_oc[ i ].name != NULL; i++ ) {
1222 LDAPObjectClass *oc;
1224 oc = ldap_str2objectclass( retcode_oc[ i ].desc,
1225 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
1227 fprintf( stderr, "retcode: "
1228 "ObjectClass load failed: %s %s\n",
1229 ldap_scherr2str( code ), err );
1233 #if LDAP_VENDOR_VERSION_MINOR == X || LDAP_VENDOR_VERSION_MINOR > 2
1234 code = oc_add( oc, 0, NULL, &err );
1236 code = oc_add( oc, &err );
1239 if ( code != LDAP_SUCCESS ) {
1240 fprintf( stderr, "retcode: "
1241 "ObjectClass load failed: %s %s\n",
1242 scherr2str( code ), err );
1246 *retcode_oc[ i ].oc = oc_find( retcode_oc[ i ].name );
1247 if ( *retcode_oc[ i ].oc == NULL ) {
1248 fprintf( stderr, "retcode: unable to find "
1249 "objectClass \"%s\"\n",
1250 retcode_oc[ i ].name );
1255 retcode.on_bi.bi_type = "retcode";
1257 retcode.on_bi.bi_db_init = retcode_db_init;
1258 retcode.on_bi.bi_db_config = retcode_db_config;
1259 retcode.on_bi.bi_db_open = retcode_db_open;
1260 retcode.on_bi.bi_db_destroy = retcode_db_destroy;
1262 retcode.on_bi.bi_op_add = retcode_op_func;
1263 retcode.on_bi.bi_op_bind = retcode_op_func;
1264 retcode.on_bi.bi_op_compare = retcode_op_func;
1265 retcode.on_bi.bi_op_delete = retcode_op_func;
1266 retcode.on_bi.bi_op_modify = retcode_op_func;
1267 retcode.on_bi.bi_op_modrdn = retcode_op_func;
1268 retcode.on_bi.bi_op_search = retcode_op_func;
1270 retcode.on_bi.bi_extended = retcode_op_func;
1272 retcode.on_response = retcode_response;
1274 return overlay_register( &retcode );
1277 #if SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC
1279 init_module( int argc, char *argv[] )
1281 return retcode_initialize();
1283 #endif /* SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC */
1285 #endif /* SLAPD_OVER_RETCODE */