1 /* retcode.c - customizable response for client testing purposes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005 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;
80 retcode_item_t *rd_item;
83 #define RETCODE_FNONE 0x00
84 #define RETCODE_FINDIR 0x01
85 #define RETCODE_INDIR( rd ) ( (rd)->rd_flags & RETCODE_FINDIR )
89 retcode_entry_response( Operation *op, SlapReply *rs, Entry *e );
92 retcode_cleanup_cb( Operation *op, SlapReply *rs )
94 rs->sr_matched = NULL;
97 if ( rs->sr_ref != NULL ) {
98 ber_bvarray_free( rs->sr_ref );
102 ch_free( op->o_callback );
103 op->o_callback = NULL;
105 return SLAP_CB_CONTINUE;
109 retcode_send_onelevel( Operation *op, SlapReply *rs )
111 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
112 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
116 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 rc = send_search_entry( op, rs );
141 case 0: /* entry sent ok */
143 case 1: /* entry not sent */
145 case -1: /* connection closed */
147 rs->sr_err = LDAP_OTHER;
151 rs->sr_err = LDAP_SUCCESS;
156 send_ldap_result( op, rs );
162 retcode_op_add( Operation *op, SlapReply *rs )
164 return retcode_entry_response( op, rs, op->ora_e );
167 typedef struct retcode_cb_t {
170 AttributeName *rdc_attrs;
174 retcode_cb_response( Operation *op, SlapReply *rs )
176 retcode_cb_t *rdc = (retcode_cb_t *)op->o_callback->sc_private;
178 if ( rs->sr_type == REP_SEARCH ) {
179 ber_tag_t o_tag = op->o_tag;
182 op->o_tag = rdc->rdc_tag;
183 if ( op->o_tag == LDAP_REQ_SEARCH ) {
184 rs->sr_attrs = rdc->rdc_attrs;
186 rc = retcode_entry_response( op, rs, rs->sr_entry );
192 if ( rs->sr_err == LDAP_SUCCESS ) {
193 if ( !op->o_abandon ) {
194 rdc->rdc_flags = SLAP_CB_CONTINUE;
199 return SLAP_CB_CONTINUE;
203 retcode_op_internal( Operation *op, SlapReply *rs )
205 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
208 SlapReply rs2 = { 0 };
209 BackendDB db = *op->o_bd;
210 slap_callback sc = { 0 };
215 op2.o_tag = LDAP_REQ_SEARCH;
216 op2.ors_scope = LDAP_SCOPE_BASE;
217 op2.ors_deref = LDAP_DEREF_NEVER;
218 op2.ors_tlimit = SLAP_NO_LIMIT;
219 op2.ors_slimit = SLAP_NO_LIMIT;
220 op2.ors_limit = NULL;
221 op2.ors_attrsonly = 0;
222 op2.ors_attrs = slap_anlist_all_attributes;
224 ber_str2bv_x( "(objectClass=errAbsObject)",
225 STRLENOF( "(objectClass=errAbsObject)" ),
226 1, &op2.ors_filterstr, op2.o_tmpmemctx );
227 op2.ors_filter = str2filter_x( &op2, op2.ors_filterstr.bv_val );
229 db.bd_info = on->on_info->oi_orig;
232 rdc.rdc_flags = RETCODE_FINDIR;
233 if ( op->o_tag == LDAP_REQ_SEARCH ) {
234 rdc.rdc_attrs = op->ors_attrs;
236 rdc.rdc_tag = op->o_tag;
237 sc.sc_response = retcode_cb_response;
238 sc.sc_private = &rdc;
239 op2.o_callback = ≻
241 rc = op2.o_bd->be_search( &op2, &rs2 );
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 if ( !dnIsSuffix( &op->o_req_ndn, &rd->rd_npdn ) ) {
265 if ( RETCODE_INDIR( rd ) ) {
266 switch ( op->o_tag ) {
268 return retcode_op_add( op, rs );
272 if ( be_isroot_pw( op ) ) {
273 return SLAP_CB_CONTINUE;
275 return retcode_op_internal( op, rs );
277 case LDAP_REQ_SEARCH:
278 if ( op->ors_scope == LDAP_SCOPE_BASE ) {
279 rs->sr_err = retcode_op_internal( op, rs );
280 if ( rs->sr_err == SLAP_CB_CONTINUE ) {
281 rs->sr_err = LDAP_SUCCESS;
283 send_ldap_result( op, rs );
288 case LDAP_REQ_MODIFY:
289 case LDAP_REQ_DELETE:
290 case LDAP_REQ_MODRDN:
291 case LDAP_REQ_COMPARE:
292 return retcode_op_internal( op, rs );
296 return SLAP_CB_CONTINUE;
299 if ( op->o_tag == LDAP_REQ_SEARCH
300 && op->ors_scope != LDAP_SCOPE_BASE
301 && op->o_req_ndn.bv_len == rd->rd_npdn.bv_len )
303 return retcode_send_onelevel( op, rs );
306 dnParent( &op->o_req_ndn, &npdn );
307 if ( npdn.bv_len != rd->rd_npdn.bv_len ) {
308 rs->sr_err = LDAP_NO_SUCH_OBJECT;
309 rs->sr_matched = rd->rd_pdn.bv_val;
310 send_ldap_result( op, rs );
311 rs->sr_matched = NULL;
315 dnRdn( &op->o_req_ndn, &nrdn );
317 for ( rdi = rd->rd_item; rdi != NULL; rdi = rdi->rdi_next ) {
318 struct berval rdi_nrdn;
320 dnRdn( &rdi->rdi_ndn, &rdi_nrdn );
321 if ( dn_match( &nrdn, &rdi_nrdn ) ) {
326 if ( rdi != NULL && rdi->rdi_mask != SN_DG_OP_ALL ) {
327 retcode_op_e o_tag = SN_DG_OP_NONE;
329 switch ( op->o_tag ) {
331 o_tag = SN_DG_OP_ADD;
335 o_tag = SN_DG_OP_BIND;
338 case LDAP_REQ_COMPARE:
339 o_tag = SN_DG_OP_COMPARE;
342 case LDAP_REQ_DELETE:
343 o_tag = SN_DG_OP_DELETE;
346 case LDAP_REQ_MODIFY:
347 o_tag = SN_DG_OP_MODIFY;
350 case LDAP_REQ_MODRDN:
351 o_tag = SN_DG_OP_RENAME;
354 case LDAP_REQ_SEARCH:
355 o_tag = SN_DG_OP_SEARCH;
358 case LDAP_REQ_EXTENDED:
359 o_tag = SN_DG_EXTENDED;
363 /* Should not happen */
367 if ( !( o_tag & rdi->rdi_mask ) ) {
368 return SLAP_CB_CONTINUE;
373 rs->sr_matched = rd->rd_pdn.bv_val;
374 rs->sr_err = LDAP_NO_SUCH_OBJECT;
375 rs->sr_text = "retcode not found";
378 rs->sr_err = rdi->rdi_err;
379 rs->sr_text = rdi->rdi_text.bv_val;
380 rs->sr_matched = rdi->rdi_matched.bv_val;
382 /* FIXME: we only honor the rdi_ref field in case rdi_err
383 * is LDAP_REFERRAL otherwise send_ldap_result() bails out */
384 if ( rs->sr_err == LDAP_REFERRAL ) {
387 if ( rdi->rdi_ref != NULL ) {
390 ref = default_referral;
394 rs->sr_ref = referral_rewrite( ref,
395 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
398 rs->sr_err = LDAP_OTHER;
399 rs->sr_text = "bad referral object";
403 if ( rdi->rdi_sleeptime > 0 ) {
404 sleep( rdi->rdi_sleeptime );
408 switch ( op->o_tag ) {
409 case LDAP_REQ_EXTENDED:
413 cb = ( slap_callback * )ch_malloc( sizeof( slap_callback ) );
414 memset( cb, 0, sizeof( slap_callback ) );
415 cb->sc_cleanup = retcode_cleanup_cb;
420 send_ldap_result( op, rs );
421 if ( rs->sr_ref != NULL ) {
422 ber_bvarray_free( rs->sr_ref );
425 rs->sr_matched = NULL;
434 retcode_op2str( ber_tag_t op, struct berval *bv )
438 BER_BVSTR( bv, "bind" );
441 BER_BVSTR( bv, "add" );
443 case LDAP_REQ_DELETE:
444 BER_BVSTR( bv, "delete" );
446 case LDAP_REQ_MODRDN:
447 BER_BVSTR( bv, "modrdn" );
449 case LDAP_REQ_MODIFY:
450 BER_BVSTR( bv, "modify" );
452 case LDAP_REQ_COMPARE:
453 BER_BVSTR( bv, "compare" );
455 case LDAP_REQ_SEARCH:
456 BER_BVSTR( bv, "search" );
458 case LDAP_REQ_EXTENDED:
459 BER_BVSTR( bv, "extended" );
466 retcode_entry_response( Operation *op, SlapReply *rs, Entry *e )
468 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
474 if ( get_manageDSAit( op ) ) {
475 return SLAP_CB_CONTINUE;
478 if ( !is_entry_objectclass_or_sub( e, oc_errAbsObject ) ) {
479 return SLAP_CB_CONTINUE;
483 a = attr_find( e->e_attrs, ad_errOp );
487 struct berval bv = BER_BVNULL;
489 (void)retcode_op2str( op->o_tag, &bv );
491 if ( BER_BVISNULL( &bv ) ) {
492 return SLAP_CB_CONTINUE;
495 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
496 if ( bvmatch( &a->a_nvals[ i ], &bv ) ) {
503 return SLAP_CB_CONTINUE;
508 a = attr_find( e->e_attrs, ad_errCode );
510 return SLAP_CB_CONTINUE;
512 err = strtol( a->a_nvals[ 0 ].bv_val, &next, 0 );
513 if ( next == a->a_nvals[ 0 ].bv_val || next[ 0 ] != '\0' ) {
514 return SLAP_CB_CONTINUE;
519 a = attr_find( e->e_attrs, ad_errSleepTime );
523 sleepTime = strtoul( a->a_nvals[ 0 ].bv_val, &next, 0 );
524 if ( next != a->a_nvals[ 0 ].bv_val && next[ 0 ] == '\0' ) {
529 if ( rs->sr_err != LDAP_SUCCESS ) {
530 BackendDB db = *op->o_bd;
531 void *o_callback = op->o_callback;
534 a = attr_find( e->e_attrs, ad_errText );
536 rs->sr_text = a->a_vals[ 0 ].bv_val;
540 a = attr_find( e->e_attrs, ad_errMatchedDN );
542 rs->sr_matched = a->a_vals[ 0 ].bv_val;
545 db.bd_info = on->on_info->oi_orig;
547 op->o_callback = NULL;
550 if ( rs->sr_err == LDAP_REFERRAL ) {
551 BerVarray refs = default_referral;
553 a = attr_find( e->e_attrs, slap_schema.si_ad_ref );
557 rs->sr_ref = referral_rewrite( refs,
558 NULL, &op->o_req_dn, op->oq_search.rs_scope );
560 send_search_reference( op, rs );
561 ber_bvarray_free( rs->sr_ref );
565 send_ldap_result( op, rs );
569 rs->sr_matched = NULL;
570 op->o_callback = o_callback;
573 if ( rs->sr_err != LDAP_SUCCESS ) {
578 return SLAP_CB_CONTINUE;
582 retcode_response( Operation *op, SlapReply *rs )
584 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
585 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
587 if ( rs->sr_type != REP_SEARCH || !RETCODE_INDIR( rd ) ) {
588 return SLAP_CB_CONTINUE;
591 return retcode_entry_response( op, rs, rs->sr_entry );
595 retcode_db_init( BackendDB *be )
597 slap_overinst *on = (slap_overinst *)be->bd_info;
600 rd = (retcode_t *)ch_malloc( sizeof( retcode_t ) );
601 memset( rd, 0, sizeof( retcode_t ) );
603 on->on_bi.bi_private = (void *)rd;
616 slap_overinst *on = (slap_overinst *)be->bd_info;
617 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
619 char *argv0 = argv[ 0 ] + STRLENOF( "retcode-" );
621 if ( strncasecmp( argv[ 0 ], "retcode-", STRLENOF( "retcode-" ) ) != 0 ) {
622 return SLAP_CONF_UNKNOWN;
625 if ( strcasecmp( argv0, "parent" ) == 0 ) {
630 fprintf( stderr, "%s: line %d: retcode: "
631 "\"retcode-parent <DN>\": missing <DN>\n",
636 if ( !BER_BVISNULL( &rd->rd_pdn ) ) {
637 fprintf( stderr, "%s: line %d: retcode: "
638 "parent already defined.\n", fname, lineno );
642 ber_str2bv( argv[ 1 ], 0, 0, &dn );
644 rc = dnPrettyNormal( NULL, &dn, &rd->rd_pdn, &rd->rd_npdn, NULL );
645 if ( rc != LDAP_SUCCESS ) {
646 fprintf( stderr, "%s: line %d: retcode: "
647 "unable to normalize parent DN \"%s\": %d\n",
648 fname, lineno, argv[ 1 ], rc );
652 } else if ( strcasecmp( argv0, "item" ) == 0 ) {
653 retcode_item_t rdi = { BER_BVNULL }, **rdip;
654 struct berval bv, rdn, nrdn;
659 fprintf( stderr, "%s: line %d: retcode: "
660 "\"retcode-item <RDN> <retcode> [<text>]\": "
666 ber_str2bv( argv[ 1 ], 0, 0, &bv );
668 rc = dnPrettyNormal( NULL, &bv, &rdn, &nrdn, NULL );
669 if ( rc != LDAP_SUCCESS ) {
670 fprintf( stderr, "%s: line %d: retcode: "
671 "unable to normalize RDN \"%s\": %d\n",
672 fname, lineno, argv[ 1 ], rc );
676 if ( !dnIsOneLevelRDN( &nrdn ) ) {
677 fprintf( stderr, "%s: line %d: retcode: "
678 "value \"%s\" is not a RDN\n",
679 fname, lineno, argv[ 1 ] );
683 if ( BER_BVISNULL( &rd->rd_npdn ) ) {
684 /* FIXME: we use the database suffix */
685 if ( be->be_nsuffix == NULL ) {
686 fprintf( stderr, "%s: line %d: retcode: "
687 "either \"retcode-parent\" "
688 "or \"suffix\" must be defined.\n",
693 ber_dupbv( &rd->rd_pdn, &be->be_suffix[ 0 ] );
694 ber_dupbv( &rd->rd_npdn, &be->be_nsuffix[ 0 ] );
697 build_new_dn( &rdi.rdi_dn, &rd->rd_pdn, &rdn, NULL );
698 build_new_dn( &rdi.rdi_ndn, &rd->rd_npdn, &nrdn, NULL );
700 ch_free( rdn.bv_val );
701 ch_free( nrdn.bv_val );
703 rdi.rdi_err = strtol( argv[ 2 ], &next, 0 );
704 if ( next == argv[ 2 ] || next[ 0 ] != '\0' ) {
705 fprintf( stderr, "%s: line %d: retcode: "
706 "unable to parse return code \"%s\"\n",
707 fname, lineno, argv[ 2 ] );
711 rdi.rdi_mask = SN_DG_OP_ALL;
716 for ( i = 3; i < argc; i++ ) {
717 if ( strncasecmp( argv[ i ], "op=", STRLENOF( "op=" ) ) == 0 )
722 ops = ldap_str2charray( &argv[ i ][ STRLENOF( "op=" ) ], "," );
723 assert( ops != NULL );
725 rdi.rdi_mask = SN_DG_OP_NONE;
727 for ( j = 0; ops[ j ] != NULL; j++ ) {
728 if ( strcasecmp( ops[ j ], "add" ) == 0 ) {
729 rdi.rdi_mask |= SN_DG_OP_ADD;
731 } else if ( strcasecmp( ops[ j ], "bind" ) == 0 ) {
732 rdi.rdi_mask |= SN_DG_OP_BIND;
734 } else if ( strcasecmp( ops[ j ], "compare" ) == 0 ) {
735 rdi.rdi_mask |= SN_DG_OP_COMPARE;
737 } else if ( strcasecmp( ops[ j ], "delete" ) == 0 ) {
738 rdi.rdi_mask |= SN_DG_OP_DELETE;
740 } else if ( strcasecmp( ops[ j ], "modify" ) == 0 ) {
741 rdi.rdi_mask |= SN_DG_OP_MODIFY;
743 } else if ( strcasecmp( ops[ j ], "rename" ) == 0
744 || strcasecmp( ops[ j ], "modrdn" ) == 0 )
746 rdi.rdi_mask |= SN_DG_OP_RENAME;
748 } else if ( strcasecmp( ops[ j ], "search" ) == 0 ) {
749 rdi.rdi_mask |= SN_DG_OP_SEARCH;
751 } else if ( strcasecmp( ops[ j ], "extended" ) == 0 ) {
752 rdi.rdi_mask |= SN_DG_EXTENDED;
754 } else if ( strcasecmp( ops[ j ], "auth" ) == 0 ) {
755 rdi.rdi_mask |= SN_DG_OP_AUTH;
757 } else if ( strcasecmp( ops[ j ], "read" ) == 0 ) {
758 rdi.rdi_mask |= SN_DG_OP_READ;
760 } else if ( strcasecmp( ops[ j ], "write" ) == 0 ) {
761 rdi.rdi_mask |= SN_DG_OP_WRITE;
763 } else if ( strcasecmp( ops[ j ], "all" ) == 0 ) {
764 rdi.rdi_mask |= SN_DG_OP_ALL;
767 fprintf( stderr, "retcode: unknown op \"%s\"\n",
773 ldap_charray_free( ops );
775 } else if ( strncasecmp( argv[ i ], "text=", STRLENOF( "text=" ) ) == 0 )
777 if ( !BER_BVISNULL( &rdi.rdi_text ) ) {
778 fprintf( stderr, "%s: line %d: retcode: "
779 "\"text\" already provided.\n",
783 ber_str2bv( &argv[ i ][ STRLENOF( "text=" ) ], 0, 1, &rdi.rdi_text );
785 } else if ( strncasecmp( argv[ i ], "matched=", STRLENOF( "matched=" ) ) == 0 )
789 if ( !BER_BVISNULL( &rdi.rdi_matched ) ) {
790 fprintf( stderr, "%s: line %d: retcode: "
791 "\"matched\" already provided.\n",
795 ber_str2bv( &argv[ i ][ STRLENOF( "matched=" ) ], 0, 0, &dn );
796 if ( dnPretty( NULL, &dn, &rdi.rdi_matched, NULL ) != LDAP_SUCCESS ) {
797 fprintf( stderr, "%s: line %d: retcode: "
798 "unable to prettify matched DN \"%s\".\n",
799 fname, lineno, &argv[ i ][ STRLENOF( "matched=" ) ] );
803 } else if ( strncasecmp( argv[ i ], "ref=", STRLENOF( "ref=" ) ) == 0 )
808 if ( rdi.rdi_ref != NULL ) {
809 fprintf( stderr, "%s: line %d: retcode: "
810 "\"ref\" already provided.\n",
815 if ( rdi.rdi_err != LDAP_REFERRAL ) {
816 fprintf( stderr, "%s: line %d: retcode: "
817 "providing \"ref\"\n"
818 "\talong with a non-referral "
819 "resultCode may cause slapd failures\n"
820 "\trelated to internal checks.\n",
824 refs = ldap_str2charray( &argv[ i ][ STRLENOF( "ref=" ) ], " " );
825 assert( refs != NULL );
827 for ( j = 0; refs[ j ] != NULL; j++ ) {
830 ber_str2bv( refs[ j ], 0, 1, &bv );
831 ber_bvarray_add( &rdi.rdi_ref, &bv );
834 ldap_charray_free( refs );
836 } else if ( strncasecmp( argv[ i ], "sleeptime=", STRLENOF( "sleeptime=" ) ) == 0 )
839 if ( rdi.rdi_sleeptime != 0 ) {
840 fprintf( stderr, "%s: line %d: retcode: "
841 "\"sleeptime\" already provided.\n",
846 rdi.rdi_sleeptime = strtol( &argv[ i ][ STRLENOF( "sleeptime=" ) ], &next, 10 );
847 if ( next == argv[ i ] || next[ 0 ] != '\0' ) {
848 fprintf( stderr, "%s: line %d: retcode: "
849 "unable to parse \"sleeptime=%s\".\n",
850 fname, lineno, &argv[ i ][ STRLENOF( "sleeptime=" ) ] );
855 fprintf( stderr, "%s: line %d: retcode: "
856 "unknown option \"%s\".\n",
857 fname, lineno, argv[ i ] );
863 for ( rdip = &rd->rd_item; *rdip; rdip = &(*rdip)->rdi_next )
867 *rdip = ( retcode_item_t * )ch_malloc( sizeof( retcode_item_t ) );
870 } else if ( strcasecmp( argv0, "indir" ) == 0 ) {
871 rd->rd_flags |= RETCODE_FINDIR;
874 return SLAP_CONF_UNKNOWN;
881 retcode_db_open( BackendDB *be )
883 slap_overinst *on = (slap_overinst *)be->bd_info;
884 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
888 for ( rdi = rd->rd_item; rdi; rdi = rdi->rdi_next ) {
892 struct berval val[ 3 ];
893 char buf[ SLAP_TEXT_BUFLEN ];
896 rdi->rdi_e.e_name = rdi->rdi_dn;
897 rdi->rdi_e.e_nname = rdi->rdi_ndn;
900 val[ 0 ] = oc_errObject->soc_cname;
901 val[ 1 ] = slap_schema.si_oc_extensibleObject->soc_cname;
902 BER_BVZERO( &val[ 2 ] );
904 attr_merge( &rdi->rdi_e, slap_schema.si_ad_objectClass, val, NULL );
907 rc = ldap_bv2rdn( &rdi->rdi_dn, &rdn, (char **) &p,
908 LDAP_DN_FORMAT_LDAP );
910 assert( rc == LDAP_SUCCESS );
912 for ( j = 0; rdn[ j ]; j++ ) {
913 LDAPAVA *ava = rdn[ j ];
914 AttributeDescription *ad = NULL;
917 rc = slap_bv2ad( &ava->la_attr, &ad, &text );
918 assert( rc == LDAP_SUCCESS );
920 attr_merge_normalize_one( &rdi->rdi_e, ad,
921 &ava->la_value, NULL );
927 snprintf( buf, sizeof( buf ), "%d", rdi->rdi_err );
928 ber_str2bv( buf, 0, 0, &val[ 0 ] );
930 attr_merge_one( &rdi->rdi_e, ad_errCode, &val[ 0 ], NULL );
932 if ( rdi->rdi_ref != NULL ) {
933 attr_merge_normalize( &rdi->rdi_e, slap_schema.si_ad_ref,
934 rdi->rdi_ref, NULL );
938 if ( !BER_BVISNULL( &rdi->rdi_text ) ) {
939 val[ 0 ] = rdi->rdi_text;
941 attr_merge_normalize_one( &rdi->rdi_e, ad_errText, &val[ 0 ], NULL );
945 if ( !BER_BVISNULL( &rdi->rdi_matched ) ) {
946 val[ 0 ] = rdi->rdi_matched;
948 attr_merge_normalize_one( &rdi->rdi_e, ad_errMatchedDN, &val[ 0 ], NULL );
952 if ( rdi->rdi_sleeptime > 0 ) {
953 snprintf( buf, sizeof( buf ), "%d", rdi->rdi_sleeptime );
954 ber_str2bv( buf, 0, 0, &val[ 0 ] );
956 attr_merge_one( &rdi->rdi_e, ad_errSleepTime, &val[ 0 ], NULL );
960 if ( rdi->rdi_mask & SN_DG_OP_ADD ) {
961 BER_BVSTR( &val[ 0 ], "add" );
962 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
965 if ( rdi->rdi_mask & SN_DG_OP_BIND ) {
966 BER_BVSTR( &val[ 0 ], "bind" );
967 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
970 if ( rdi->rdi_mask & SN_DG_OP_COMPARE ) {
971 BER_BVSTR( &val[ 0 ], "compare" );
972 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
975 if ( rdi->rdi_mask & SN_DG_OP_DELETE ) {
976 BER_BVSTR( &val[ 0 ], "delete" );
977 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
980 if ( rdi->rdi_mask & SN_DG_EXTENDED ) {
981 BER_BVSTR( &val[ 0 ], "extended" );
982 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
985 if ( rdi->rdi_mask & SN_DG_OP_MODIFY ) {
986 BER_BVSTR( &val[ 0 ], "modify" );
987 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
990 if ( rdi->rdi_mask & SN_DG_OP_RENAME ) {
991 BER_BVSTR( &val[ 0 ], "rename" );
992 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
995 if ( rdi->rdi_mask & SN_DG_OP_SEARCH ) {
996 BER_BVSTR( &val[ 0 ], "search" );
997 attr_merge_normalize_one( &rdi->rdi_e, ad_errOp, &val[ 0 ], NULL );
1005 retcode_db_destroy( BackendDB *be )
1007 slap_overinst *on = (slap_overinst *)be->bd_info;
1008 retcode_t *rd = (retcode_t *)on->on_bi.bi_private;
1011 retcode_item_t *rdi, *next;
1013 for ( rdi = rd->rd_item; rdi != NULL; rdi = next ) {
1014 ber_memfree( rdi->rdi_dn.bv_val );
1015 ber_memfree( rdi->rdi_ndn.bv_val );
1017 if ( !BER_BVISNULL( &rdi->rdi_text ) ) {
1018 ber_memfree( rdi->rdi_text.bv_val );
1021 if ( !BER_BVISNULL( &rdi->rdi_matched ) ) {
1022 ber_memfree( rdi->rdi_matched.bv_val );
1025 BER_BVZERO( &rdi->rdi_e.e_name );
1026 BER_BVZERO( &rdi->rdi_e.e_nname );
1028 entry_clean( &rdi->rdi_e );
1030 next = rdi->rdi_next;
1041 #if SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC
1043 #endif /* SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC */
1045 retcode_init( void )
1053 AttributeDescription **ad;
1055 { "errCode", "( 1.3.6.1.4.1.4203.666.11.4.1.1 "
1056 "NAME ( 'errCode' ) "
1057 "DESC 'LDAP error code' "
1058 "EQUALITY integerMatch "
1059 "ORDERING integerOrderingMatch "
1060 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
1063 { "errOp", "( 1.3.6.1.4.1.4203.666.11.4.1.2 "
1065 "DESC 'Operations the errObject applies to' "
1066 "EQUALITY caseIgnoreMatch "
1067 "SUBSTR caseIgnoreSubstringsMatch "
1068 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )",
1070 { "errText", "( 1.3.6.1.4.1.4203.666.11.4.1.3 "
1071 "NAME ( 'errText' ) "
1072 "DESC 'LDAP error textual description' "
1073 "EQUALITY caseIgnoreMatch "
1074 "SUBSTR caseIgnoreSubstringsMatch "
1075 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
1078 { "errSleepTime", "( 1.3.6.1.4.1.4203.666.11.4.1.4 "
1079 "NAME ( 'errSleepTime' ) "
1080 "DESC 'Time to wait before returning the error' "
1081 "EQUALITY integerMatch "
1082 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
1085 { "errMatchedDN", "( 1.3.6.1.4.1.4203.666.11.4.1.5 "
1086 "NAME ( 'errMatchedDN' ) "
1087 "DESC 'Value to be returned as matched DN' "
1088 "EQUALITY distinguishedNameMatch "
1089 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
1100 { "errAbsObject", "( 1.3.6.1.4.1.4203.666.11.4.3.0 "
1101 "NAME ( 'errAbsObject' ) "
1113 { "errObject", "( 1.3.6.1.4.1.4203.666.11.4.3.1 "
1114 "NAME ( 'errObject' ) "
1115 "SUP errAbsObject STRUCTURAL "
1118 { "errAuxObject", "( 1.3.6.1.4.1.4203.666.11.4.3.2 "
1119 "NAME ( 'errAuxObject' ) "
1120 "SUP errAbsObject AUXILIARY "
1127 for ( i = 0; retcode_at[ i ].name != NULL; i++ ) {
1128 LDAPAttributeType *at;
1130 at = ldap_str2attributetype( retcode_at[ i ].desc,
1131 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
1133 fprintf( stderr, "retcode: "
1134 "AttributeType load failed: %s %s\n",
1135 ldap_scherr2str( code ), err );
1139 #if LDAP_VENDOR_VERSION_MINOR == X || LDAP_VENDOR_VERSION_MINOR > 2
1140 code = at_add( at, 0, NULL, &err );
1142 code = at_add( at, &err );
1145 if ( code != LDAP_SUCCESS ) {
1146 fprintf( stderr, "retcode: "
1147 "AttributeType load failed: %s %s\n",
1148 scherr2str( code ), err );
1152 code = slap_str2ad( retcode_at[ i ].name,
1153 retcode_at[ i ].ad, &err );
1154 if ( code != LDAP_SUCCESS ) {
1155 fprintf( stderr, "retcode: unable to find "
1156 "AttributeDescription \"%s\": %d (%s)\n",
1157 retcode_at[ i ].name, code, err );
1162 for ( i = 0; retcode_oc[ i ].name != NULL; i++ ) {
1163 LDAPObjectClass *oc;
1165 oc = ldap_str2objectclass( retcode_oc[ i ].desc,
1166 &code, &err, LDAP_SCHEMA_ALLOW_ALL );
1168 fprintf( stderr, "retcode: "
1169 "ObjectClass load failed: %s %s\n",
1170 ldap_scherr2str( code ), err );
1174 #if LDAP_VENDOR_VERSION_MINOR == X || LDAP_VENDOR_VERSION_MINOR > 2
1175 code = oc_add( oc, 0, NULL, &err );
1177 code = oc_add( oc, &err );
1180 if ( code != LDAP_SUCCESS ) {
1181 fprintf( stderr, "retcode: "
1182 "ObjectClass load failed: %s %s\n",
1183 scherr2str( code ), err );
1187 *retcode_oc[ i ].oc = oc_find( retcode_oc[ i ].name );
1188 if ( *retcode_oc[ i ].oc == NULL ) {
1189 fprintf( stderr, "retcode: unable to find "
1190 "objectClass \"%s\"\n",
1191 retcode_oc[ i ].name );
1196 retcode.on_bi.bi_type = "retcode";
1198 retcode.on_bi.bi_db_init = retcode_db_init;
1199 retcode.on_bi.bi_db_config = retcode_db_config;
1200 retcode.on_bi.bi_db_open = retcode_db_open;
1201 retcode.on_bi.bi_db_destroy = retcode_db_destroy;
1203 retcode.on_bi.bi_op_add = retcode_op_func;
1204 retcode.on_bi.bi_op_bind = retcode_op_func;
1205 retcode.on_bi.bi_op_compare = retcode_op_func;
1206 retcode.on_bi.bi_op_delete = retcode_op_func;
1207 retcode.on_bi.bi_op_modify = retcode_op_func;
1208 retcode.on_bi.bi_op_modrdn = retcode_op_func;
1209 retcode.on_bi.bi_op_search = retcode_op_func;
1211 retcode.on_bi.bi_extended = retcode_op_func;
1213 retcode.on_response = retcode_response;
1215 return overlay_register( &retcode );
1218 #if SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC
1220 init_module( int argc, char *argv[] )
1222 return retcode_init();
1224 #endif /* SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC */
1226 #endif /* SLAPD_OVER_RETCODE */