]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/retcode.c
ITS#5705
[openldap] / servers / slapd / overlays / retcode.c
1 /* retcode.c - customizable response for client testing purposes */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2005-2008 The OpenLDAP Foundation.
6  * Portions Copyright 2005 Pierangelo Masarati <ando@sys-net.it>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
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>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #ifdef SLAPD_OVER_RETCODE
25
26 #include <stdio.h>
27
28 #include <ac/unistd.h>
29 #include <ac/string.h>
30 #include <ac/ctype.h>
31 #include <ac/socket.h>
32
33 #include "slap.h"
34 #include "config.h"
35 #include "lutil.h"
36 #include "ldif.h"
37
38 static slap_overinst            retcode;
39
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;
48
49 static ObjectClass              *oc_errAbsObject;
50 static ObjectClass              *oc_errObject;
51 static ObjectClass              *oc_errAuxObject;
52
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)
67 } retcode_op_e;
68
69 typedef struct retcode_item_t {
70         struct berval           rdi_line;
71         struct berval           rdi_dn;
72         struct berval           rdi_ndn;
73         struct berval           rdi_text;
74         struct berval           rdi_matched;
75         int                     rdi_err;
76         BerVarray               rdi_ref;
77         int                     rdi_sleeptime;
78         Entry                   rdi_e;
79         slap_mask_t             rdi_mask;
80         struct berval           rdi_unsolicited_oid;
81         struct berval           rdi_unsolicited_data;
82
83         unsigned                rdi_flags;
84 #define RDI_PRE_DISCONNECT      (0x1U)
85 #define RDI_POST_DISCONNECT     (0x2U)
86
87         struct retcode_item_t   *rdi_next;
88 } retcode_item_t;
89
90 typedef struct retcode_t {
91         struct berval           rd_pdn;
92         struct berval           rd_npdn;
93
94         int                     rd_sleep;
95
96         retcode_item_t          *rd_item;
97
98         int                     rd_indir;
99 #define RETCODE_FINDIR          0x01
100 #define RETCODE_INDIR( rd )     ( (rd)->rd_indir )
101 } retcode_t;
102
103 static int
104 retcode_entry_response( Operation *op, SlapReply *rs, BackendInfo *bi, Entry *e );
105
106 static unsigned int
107 retcode_sleep( int s )
108 {
109         unsigned int r = 0;
110
111         /* sleep as required */
112         if ( s < 0 ) {
113 #if 0   /* use high-order bits for better randomness (Numerical Recipes in "C") */
114                 r = rand() % (-s);
115 #endif
116                 r = ((double)(-s))*rand()/(RAND_MAX + 1.0);
117         } else if ( s > 0 ) {
118                 r = (unsigned int)s;
119         }
120         if ( r ) {
121                 sleep( r );
122         }
123
124         return r;
125 }
126
127 static int
128 retcode_cleanup_cb( Operation *op, SlapReply *rs )
129 {
130         rs->sr_matched = NULL;
131         rs->sr_text = NULL;
132
133         if ( rs->sr_ref != NULL ) {
134                 ber_bvarray_free( rs->sr_ref );
135                 rs->sr_ref = NULL;
136         }
137
138         ch_free( op->o_callback );
139         op->o_callback = NULL;
140
141         return SLAP_CB_CONTINUE;
142 }
143
144 static int
145 retcode_send_onelevel( Operation *op, SlapReply *rs )
146 {
147         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
148         retcode_t       *rd = (retcode_t *)on->on_bi.bi_private;
149
150         retcode_item_t  *rdi;
151         
152         for ( rdi = rd->rd_item; rdi != NULL; rdi = rdi->rdi_next ) {
153                 if ( op->o_abandon ) {
154                         return rs->sr_err = SLAPD_ABANDON;
155                 }
156
157                 rs->sr_err = test_filter( op, &rdi->rdi_e, op->ors_filter );
158                 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
159                         /* safe default */
160                         rs->sr_attrs = op->ors_attrs;
161                         rs->sr_operational_attrs = NULL;
162                         rs->sr_ctrls = NULL;
163                         rs->sr_flags = 0;
164                         rs->sr_err = LDAP_SUCCESS;
165                         rs->sr_entry = &rdi->rdi_e;
166
167                         rs->sr_err = send_search_entry( op, rs );
168                         rs->sr_entry = NULL;
169
170                         switch ( rs->sr_err ) {
171                         case LDAP_UNAVAILABLE:  /* connection closed */
172                                 rs->sr_err = LDAP_OTHER;
173                                 /* fallthru */
174                         case LDAP_SIZELIMIT_EXCEEDED:
175                                 goto done;
176                         }
177                 }
178                 rs->sr_err = LDAP_SUCCESS;
179         }
180
181 done:;
182
183         send_ldap_result( op, rs );
184
185         return rs->sr_err;
186 }
187
188 static int
189 retcode_op_add( Operation *op, SlapReply *rs )
190 {
191         return retcode_entry_response( op, rs, NULL, op->ora_e );
192 }
193
194 typedef struct retcode_cb_t {
195         BackendInfo     *rdc_info;
196         unsigned        rdc_flags;
197         ber_tag_t       rdc_tag;
198         AttributeName   *rdc_attrs;
199 } retcode_cb_t;
200
201 static int
202 retcode_cb_response( Operation *op, SlapReply *rs )
203 {
204         retcode_cb_t    *rdc = (retcode_cb_t *)op->o_callback->sc_private;
205
206         op->o_tag = rdc->rdc_tag;
207         if ( rs->sr_type == REP_SEARCH ) {
208                 ber_tag_t       o_tag = op->o_tag;
209                 int             rc;
210
211                 if ( op->o_tag == LDAP_REQ_SEARCH ) {
212                         rs->sr_attrs = rdc->rdc_attrs;
213                 }
214                 rc = retcode_entry_response( op, rs, rdc->rdc_info, rs->sr_entry );
215                 op->o_tag = o_tag;
216
217                 return rc;
218         }
219
220         switch ( rs->sr_err ) {
221         case LDAP_SUCCESS:
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;
227                 }
228                 return 0;
229         }
230
231         return SLAP_CB_CONTINUE;
232 }
233
234 static int
235 retcode_op_internal( Operation *op, SlapReply *rs )
236 {
237         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
238
239         Operation       op2 = *op;
240         BackendDB       db = *op->o_bd;
241         slap_callback   sc = { 0 };
242         retcode_cb_t    rdc;
243
244         int             rc;
245
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;
254
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 );
259
260         db.bd_info = on->on_info->oi_orig;
261         op2.o_bd = &db;
262
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;
267         }
268         rdc.rdc_tag = op->o_tag;
269         sc.sc_response = retcode_cb_response;
270         sc.sc_private = &rdc;
271         op2.o_callback = &sc;
272
273         rc = op2.o_bd->be_search( &op2, rs );
274         op->o_abandon = op2.o_abandon;
275
276         filter_free_x( &op2, op2.ors_filter, 1 );
277         ber_memfree_x( op2.ors_filterstr.bv_val, op2.o_tmpmemctx );
278
279         if ( rdc.rdc_flags == SLAP_CB_CONTINUE ) {
280                 return SLAP_CB_CONTINUE;
281         }
282
283         return rc;
284 }
285
286 static int
287 retcode_op_func( Operation *op, SlapReply *rs )
288 {
289         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
290         retcode_t       *rd = (retcode_t *)on->on_bi.bi_private;
291
292         retcode_item_t  *rdi;
293         struct berval           nrdn, npdn;
294
295         slap_callback           *cb = NULL;
296
297         /* sleep as required */
298         retcode_sleep( rd->rd_sleep );
299
300         if ( !dnIsSuffix( &op->o_req_ndn, &rd->rd_npdn ) ) {
301                 if ( RETCODE_INDIR( rd ) ) {
302                         switch ( op->o_tag ) {
303                         case LDAP_REQ_ADD:
304                                 return retcode_op_add( op, rs );
305
306                         case LDAP_REQ_BIND:
307                                 /* skip if rootdn */
308                                 /* FIXME: better give the db a chance? */
309                                 if ( be_isroot_pw( op ) ) {
310                                         return LDAP_SUCCESS;
311                                 }
312                                 return retcode_op_internal( op, rs );
313
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 ) {
320                                                         break;
321                                                 }
322                                                 rs->sr_err = LDAP_SUCCESS;
323                                                 /* fallthru */
324
325                                         default:
326                                                 send_ldap_result( op, rs );
327                                                 break;
328                                         }
329                                         return rs->sr_err;
330                                 }
331                                 break;
332
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 );
338                         }
339                 }
340
341                 return SLAP_CB_CONTINUE;
342         }
343
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 )
347         {
348                 return retcode_send_onelevel( op, rs );
349         }
350
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;
357                 return rs->sr_err;
358         }
359
360         dnRdn( &op->o_req_ndn, &nrdn );
361
362         for ( rdi = rd->rd_item; rdi != NULL; rdi = rdi->rdi_next ) {
363                 struct berval   rdi_nrdn;
364
365                 dnRdn( &rdi->rdi_ndn, &rdi_nrdn );
366                 if ( dn_match( &nrdn, &rdi_nrdn ) ) {
367                         break;
368                 }
369         }
370
371         if ( rdi != NULL && rdi->rdi_mask != SN_DG_OP_ALL ) {
372                 retcode_op_e    o_tag = SN_DG_OP_NONE;
373
374                 switch ( op->o_tag ) {
375                 case LDAP_REQ_ADD:
376                         o_tag = SN_DG_OP_ADD;
377                         break;
378
379                 case LDAP_REQ_BIND:
380                         o_tag = SN_DG_OP_BIND;
381                         break;
382
383                 case LDAP_REQ_COMPARE:
384                         o_tag = SN_DG_OP_COMPARE;
385                         break;
386
387                 case LDAP_REQ_DELETE:
388                         o_tag = SN_DG_OP_DELETE;
389                         break;
390
391                 case LDAP_REQ_MODIFY:
392                         o_tag = SN_DG_OP_MODIFY;
393                         break;
394
395                 case LDAP_REQ_MODRDN:
396                         o_tag = SN_DG_OP_RENAME;
397                         break;
398
399                 case LDAP_REQ_SEARCH:
400                         o_tag = SN_DG_OP_SEARCH;
401                         break;
402
403                 case LDAP_REQ_EXTENDED:
404                         o_tag = SN_DG_EXTENDED;
405                         break;
406
407                 default:
408                         /* Should not happen */
409                         break;
410                 }
411
412                 if ( !( o_tag & rdi->rdi_mask ) ) {
413                         return SLAP_CB_CONTINUE;
414                 }
415         }
416
417         if ( rdi == NULL ) {
418                 rs->sr_matched = rd->rd_pdn.bv_val;
419                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
420                 rs->sr_text = "retcode not found";
421
422         } else {
423                 if ( rdi->rdi_flags & RDI_PRE_DISCONNECT ) {
424                         return rs->sr_err = SLAPD_DISCONNECT;
425                 }
426
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;
430
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 ) {
434                         BerVarray       ref;
435
436                         if ( rdi->rdi_ref != NULL ) {
437                                 ref = rdi->rdi_ref;
438                         } else {
439                                 ref = default_referral;
440                         }
441
442                         if ( ref != NULL ) {
443                                 rs->sr_ref = referral_rewrite( ref,
444                                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
445
446                         } else {
447                                 rs->sr_err = LDAP_OTHER;
448                                 rs->sr_text = "bad referral object";
449                         }
450                 }
451
452                 retcode_sleep( rdi->rdi_sleeptime );
453         }
454
455         switch ( op->o_tag ) {
456         case LDAP_REQ_EXTENDED:
457                 if ( rdi == NULL ) {
458                         break;
459                 }
460                 cb = ( slap_callback * )ch_malloc( sizeof( slap_callback ) );
461                 memset( cb, 0, sizeof( slap_callback ) );
462                 cb->sc_cleanup = retcode_cleanup_cb;
463                 op->o_callback = cb;
464                 break;
465
466         default:
467                 if ( rdi && !BER_BVISNULL( &rdi->rdi_unsolicited_oid ) ) {
468                         ber_int_t       msgid = op->o_msgid;
469
470                         /* RFC 4511 unsolicited response */
471
472                         op->o_msgid = 0;
473                         if ( strcmp( rdi->rdi_unsolicited_oid.bv_val, "0" ) == 0 ) {
474                                 send_ldap_result( op, rs );
475
476                         } else {
477                                 ber_tag_t       tag = op->o_tag;
478
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;
483                                 }
484                                 send_ldap_extended( op, rs );
485                                 rs->sr_rspoid = NULL;
486                                 rs->sr_rspdata = NULL;
487                                 op->o_tag = tag;
488
489                         }
490                         op->o_msgid = msgid;
491
492                 } else {
493                         send_ldap_result( op, rs );
494                 }
495
496                 if ( rs->sr_ref != NULL ) {
497                         ber_bvarray_free( rs->sr_ref );
498                         rs->sr_ref = NULL;
499                 }
500                 rs->sr_matched = NULL;
501                 rs->sr_text = NULL;
502
503                 if ( rdi && rdi->rdi_flags & RDI_POST_DISCONNECT ) {
504                         return rs->sr_err = SLAPD_DISCONNECT;
505                 }
506                 break;
507         }
508
509         return rs->sr_err;
510 }
511
512 static int
513 retcode_op2str( ber_tag_t op, struct berval *bv )
514 {
515         switch ( op ) {
516         case LDAP_REQ_BIND:
517                 BER_BVSTR( bv, "bind" );
518                 return 0;
519         case LDAP_REQ_ADD:
520                 BER_BVSTR( bv, "add" );
521                 return 0;
522         case LDAP_REQ_DELETE:
523                 BER_BVSTR( bv, "delete" );
524                 return 0;
525         case LDAP_REQ_MODRDN:
526                 BER_BVSTR( bv, "modrdn" );
527                 return 0;
528         case LDAP_REQ_MODIFY:
529                 BER_BVSTR( bv, "modify" );
530                 return 0;
531         case LDAP_REQ_COMPARE:
532                 BER_BVSTR( bv, "compare" );
533                 return 0;
534         case LDAP_REQ_SEARCH:
535                 BER_BVSTR( bv, "search" );
536                 return 0;
537         case LDAP_REQ_EXTENDED:
538                 BER_BVSTR( bv, "extended" );
539                 return 0;
540         }
541         return -1;
542 }
543
544 static int
545 retcode_entry_response( Operation *op, SlapReply *rs, BackendInfo *bi, Entry *e )
546 {
547         Attribute       *a;
548         int             err;
549         char            *next;
550         int             disconnect = 0;
551
552         if ( get_manageDSAit( op ) ) {
553                 return SLAP_CB_CONTINUE;
554         }
555
556         if ( !is_entry_objectclass_or_sub( e, oc_errAbsObject ) ) {
557                 return SLAP_CB_CONTINUE;
558         }
559
560         /* operation */
561         a = attr_find( e->e_attrs, ad_errOp );
562         if ( a != NULL ) {
563                 int             i,
564                                 gotit = 0;
565                 struct berval   bv = BER_BVNULL;
566
567                 (void)retcode_op2str( op->o_tag, &bv );
568
569                 if ( BER_BVISNULL( &bv ) ) {
570                         return SLAP_CB_CONTINUE;
571                 }
572
573                 for ( i = 0; !BER_BVISNULL( &a->a_nvals[ i ] ); i++ ) {
574                         if ( bvmatch( &a->a_nvals[ i ], &bv ) ) {
575                                 gotit = 1;
576                                 break;
577                         }
578                 }
579
580                 if ( !gotit ) {
581                         return SLAP_CB_CONTINUE;
582                 }
583         }
584
585         /* disconnect */
586         a = attr_find( e->e_attrs, ad_errDisconnect );
587         if ( a != NULL ) {
588                 if ( bvmatch( &a->a_nvals[ 0 ], &slap_true_bv ) ) {
589                         return rs->sr_err = SLAPD_DISCONNECT;
590                 }
591                 disconnect = 1;
592         }
593
594         /* error code */
595         a = attr_find( e->e_attrs, ad_errCode );
596         if ( a == NULL ) {
597                 return SLAP_CB_CONTINUE;
598         }
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;
602         }
603         rs->sr_err = err;
604
605         /* sleep time */
606         a = attr_find( e->e_attrs, ad_errSleepTime );
607         if ( a != NULL && a->a_nvals[ 0 ].bv_val[ 0 ] != '-' ) {
608                 int     sleepTime;
609
610                 if ( lutil_atoi( &sleepTime, a->a_nvals[ 0 ].bv_val ) == 0 ) {
611                         retcode_sleep( sleepTime );
612                 }
613         }
614
615         if ( rs->sr_err != LDAP_SUCCESS && !LDAP_API_ERROR( rs->sr_err )) {
616                 BackendDB       db = *op->o_bd,
617                                 *o_bd = op->o_bd;
618                 void            *o_callback = op->o_callback;
619
620                 /* message text */
621                 a = attr_find( e->e_attrs, ad_errText );
622                 if ( a != NULL ) {
623                         rs->sr_text = a->a_vals[ 0 ].bv_val;
624                 }
625
626                 /* matched DN */
627                 a = attr_find( e->e_attrs, ad_errMatchedDN );
628                 if ( a != NULL ) {
629                         rs->sr_matched = a->a_vals[ 0 ].bv_val;
630                 }
631
632                 if ( bi == NULL ) {
633                         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
634
635                         bi = on->on_info->oi_orig;
636                 }
637
638                 db.bd_info = bi;
639                 op->o_bd = &db;
640                 op->o_callback = NULL;
641
642                 /* referral */
643                 if ( rs->sr_err == LDAP_REFERRAL ) {
644                         BerVarray       refs = default_referral;
645
646                         a = attr_find( e->e_attrs, slap_schema.si_ad_ref );
647                         if ( a != NULL ) {
648                                 refs = a->a_vals;
649                         }
650                         rs->sr_ref = referral_rewrite( refs,
651                                 NULL, &op->o_req_dn, op->oq_search.rs_scope );
652         
653                         send_search_reference( op, rs );
654                         ber_bvarray_free( rs->sr_ref );
655                         rs->sr_ref = NULL;
656
657                 } else {
658                         a = attr_find( e->e_attrs, ad_errUnsolicitedOID );
659                         if ( a != NULL ) {
660                                 struct berval   oid = BER_BVNULL,
661                                                 data = BER_BVNULL;
662                                 ber_int_t       msgid = op->o_msgid;
663
664                                 /* RFC 4511 unsolicited response */
665
666                                 op->o_msgid = 0;
667
668                                 oid = a->a_nvals[ 0 ];
669
670                                 a = attr_find( e->e_attrs, ad_errUnsolicitedData );
671                                 if ( a != NULL ) {
672                                         data = a->a_nvals[ 0 ];
673                                 }
674
675                                 if ( strcmp( oid.bv_val, "0" ) == 0 ) {
676                                         send_ldap_result( op, rs );
677
678                                 } else {
679                                         ber_tag_t       tag = op->o_tag;
680
681                                         op->o_tag = LDAP_REQ_EXTENDED;
682                                         rs->sr_rspoid = oid.bv_val;
683                                         if ( !BER_BVISNULL( &data ) ) {
684                                                 rs->sr_rspdata = &data;
685                                         }
686                                         send_ldap_extended( op, rs );
687                                         rs->sr_rspoid = NULL;
688                                         rs->sr_rspdata = NULL;
689                                         op->o_tag = tag;
690                                 }
691                                 op->o_msgid = msgid;
692
693                         } else {
694                                 send_ldap_result( op, rs );
695                         }
696                 }
697
698                 rs->sr_text = NULL;
699                 rs->sr_matched = NULL;
700                 op->o_bd = o_bd;
701                 op->o_callback = o_callback;
702         }
703
704         if ( rs->sr_err != LDAP_SUCCESS ) {
705                 if ( disconnect ) {
706                         return rs->sr_err = SLAPD_DISCONNECT;
707                 }
708         
709                 op->o_abandon = 1;
710                 return rs->sr_err;
711         }
712
713         return SLAP_CB_CONTINUE;
714 }
715
716 static int
717 retcode_response( Operation *op, SlapReply *rs )
718 {
719         slap_overinst   *on = (slap_overinst *)op->o_bd->bd_info;
720         retcode_t       *rd = (retcode_t *)on->on_bi.bi_private;
721
722         if ( rs->sr_type != REP_SEARCH || !RETCODE_INDIR( rd ) ) {
723                 return SLAP_CB_CONTINUE;
724         }
725
726         return retcode_entry_response( op, rs, NULL, rs->sr_entry );
727 }
728
729 static int
730 retcode_db_init( BackendDB *be, ConfigReply *cr )
731 {
732         slap_overinst   *on = (slap_overinst *)be->bd_info;
733         retcode_t       *rd;
734
735         srand( getpid() );
736
737         rd = (retcode_t *)ch_malloc( sizeof( retcode_t ) );
738         memset( rd, 0, sizeof( retcode_t ) );
739
740         on->on_bi.bi_private = (void *)rd;
741
742         return 0;
743 }
744
745 static void
746 retcode_item_destroy( retcode_item_t *rdi )
747 {
748         ber_memfree( rdi->rdi_line.bv_val );
749
750         ber_memfree( rdi->rdi_dn.bv_val );
751         ber_memfree( rdi->rdi_ndn.bv_val );
752
753         if ( !BER_BVISNULL( &rdi->rdi_text ) ) {
754                 ber_memfree( rdi->rdi_text.bv_val );
755         }
756
757         if ( !BER_BVISNULL( &rdi->rdi_matched ) ) {
758                 ber_memfree( rdi->rdi_matched.bv_val );
759         }
760
761         if ( rdi->rdi_ref ) {
762                 ber_bvarray_free( rdi->rdi_ref );
763         }
764
765         BER_BVZERO( &rdi->rdi_e.e_name );
766         BER_BVZERO( &rdi->rdi_e.e_nname );
767
768         entry_clean( &rdi->rdi_e );
769
770         ch_free( rdi );
771 }
772
773 enum {
774         RC_PARENT = 1,
775         RC_ITEM
776 };
777
778 static ConfigDriver rc_cf_gen;
779
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' "
784                         "DESC '' "
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' "
789                         "DESC '' "
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' "
797                         "DESC '' "
798                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
799
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' "
804                         "DESC '' "
805                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
806
807         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
808 };
809
810 static ConfigOCs rcocs[] = {
811         { "( OLcfgOvOc:20.1 "
812                 "NAME 'olcRetcodeConfig' "
813                 "DESC 'Retcode configuration' "
814                 "SUP olcOverlayConfig "
815                 "MAY ( olcRetcodeParent "
816                         "$ olcRetcodeItem "
817                         "$ olcRetcodeInDir "
818                         "$ olcRetcodeSleep "
819                 ") )",
820                 Cft_Overlay, rccfg, NULL, NULL },
821         { NULL, 0, NULL }
822 };
823
824 static int
825 rc_cf_gen( ConfigArgs *c )
826 {
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;
830
831         if ( c->op == SLAP_CONFIG_EMIT ) {
832                 switch( c->type ) {
833                 case RC_PARENT:
834                         if ( !BER_BVISEMPTY( &rd->rd_pdn )) {
835                                 rc = value_add_one( &c->rvalue_vals,
836                                                     &rd->rd_pdn );
837                                 if ( rc == 0 ) {
838                                         rc = value_add_one( &c->rvalue_nvals,
839                                                             &rd->rd_npdn );
840                                 }
841                                 return rc;
842                         }
843                         rc = 0;
844                         break;
845
846                 case RC_ITEM: {
847                         retcode_item_t *rdi;
848                         int i;
849
850                         for ( rdi = rd->rd_item, i = 0; rdi; rdi = rdi->rdi_next, i++ ) {
851                                 char buf[4096];
852                                 struct berval bv;
853                                 char *ptr;
854
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 );
861                         }
862                         rc = 0;
863                         } break;
864
865                 default:
866                         assert( 0 );
867                         break;
868                 }
869
870                 return rc;
871
872         } else if ( c->op == LDAP_MOD_DELETE ) {
873                 switch( c->type ) {
874                 case RC_PARENT:
875                         if ( rd->rd_pdn.bv_val ) {
876                                 ber_memfree ( rd->rd_pdn.bv_val );
877                                 rc = 0;
878                         }
879                         if ( rd->rd_npdn.bv_val ) {
880                                 ber_memfree ( rd->rd_npdn.bv_val );
881                         }
882                         break;
883
884                 case RC_ITEM:
885                         if ( c->valx == -1 ) {
886                                 retcode_item_t *rdi, *next;
887
888                                 for ( rdi = rd->rd_item; rdi != NULL; rdi = next ) {
889                                         next = rdi->rdi_next;
890                                         retcode_item_destroy( rdi );
891                                 }
892
893                         } else {
894                                 retcode_item_t **rdip, *rdi;
895                                 int i;
896
897                                 for ( rdip = &rd->rd_item, i = 0; i <= c->valx && *rdip; i++, rdip = &(*rdip)->rdi_next )
898                                         ;
899                                 if ( *rdip == NULL ) {
900                                         return 1;
901                                 }
902                                 rdi = *rdip;
903                                 *rdip = rdi->rdi_next;
904
905                                 retcode_item_destroy( rdi );
906                         }
907                         rc = 0;
908                         break;
909
910                 default:
911                         assert( 0 );
912                         break;
913                 }
914                 return rc;      /* FIXME */
915         }
916
917         switch( c->type ) {
918         case RC_PARENT:
919                 if ( rd->rd_pdn.bv_val ) {
920                         ber_memfree ( rd->rd_pdn.bv_val );
921                 }
922                 if ( rd->rd_npdn.bv_val ) {
923                         ber_memfree ( rd->rd_npdn.bv_val );
924                 }
925                 rd->rd_pdn = c->value_dn;
926                 rd->rd_npdn = c->value_ndn;
927                 rc = 0;
928                 break;
929
930         case RC_ITEM: {
931                 retcode_item_t  rdi = { BER_BVNULL }, **rdip;
932                 struct berval           bv, rdn, nrdn;
933                 char                    *next = NULL;
934                 int                     i;
935
936                 if ( c->argc < 3 ) {
937                         snprintf( c->cr_msg, sizeof(c->cr_msg),
938                                 "\"retcode-item <RDN> <retcode> [<text>]\": "
939                                 "missing args" );
940                         Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
941                                 c->log, c->cr_msg, 0 );
942                         return ARG_BAD_CONF;
943                 }
944
945                 ber_str2bv( c->argv[ 1 ], 0, 0, &bv );
946                 
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",
951                                 c->argv[ 1 ], rc );
952                         Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
953                                 c->log, c->cr_msg, 0 );
954                         return ARG_BAD_CONF;
955                 }
956
957                 if ( !dnIsOneLevelRDN( &nrdn ) ) {
958                         snprintf( c->cr_msg, sizeof(c->cr_msg),
959                                 "value \"%s\" is not a RDN",
960                                 c->argv[ 1 ] );
961                         Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
962                                 c->log, c->cr_msg, 0 );
963                         return ARG_BAD_CONF;
964                 }
965
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 );
974                                 return ARG_BAD_CONF;
975                         }
976
977                         ber_dupbv( &rd->rd_pdn, &c->be->be_suffix[ 0 ] );
978                         ber_dupbv( &rd->rd_npdn, &c->be->be_nsuffix[ 0 ] );
979                 }
980
981                 build_new_dn( &rdi.rdi_dn, &rd->rd_pdn, &rdn, NULL );
982                 build_new_dn( &rdi.rdi_ndn, &rd->rd_npdn, &nrdn, NULL );
983
984                 ch_free( rdn.bv_val );
985                 ch_free( nrdn.bv_val );
986
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\"",
991                                 c->argv[ 2 ] );
992                         Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
993                                 c->log, c->cr_msg, 0 );
994                         return ARG_BAD_CONF;
995                 }
996
997                 rdi.rdi_mask = SN_DG_OP_ALL;
998
999                 if ( c->argc > 3 ) {
1000                         for ( i = 3; i < c->argc; i++ ) {
1001                                 if ( strncasecmp( c->argv[ i ], "op=", STRLENOF( "op=" ) ) == 0 )
1002                                 {
1003                                         char            **ops;
1004                                         int             j;
1005
1006                                         ops = ldap_str2charray( &c->argv[ i ][ STRLENOF( "op=" ) ], "," );
1007                                         assert( ops != NULL );
1008
1009                                         rdi.rdi_mask = SN_DG_OP_NONE;
1010
1011                                         for ( j = 0; ops[ j ] != NULL; j++ ) {
1012                                                 if ( strcasecmp( ops[ j ], "add" ) == 0 ) {
1013                                                         rdi.rdi_mask |= SN_DG_OP_ADD;
1014
1015                                                 } else if ( strcasecmp( ops[ j ], "bind" ) == 0 ) {
1016                                                         rdi.rdi_mask |= SN_DG_OP_BIND;
1017
1018                                                 } else if ( strcasecmp( ops[ j ], "compare" ) == 0 ) {
1019                                                         rdi.rdi_mask |= SN_DG_OP_COMPARE;
1020
1021                                                 } else if ( strcasecmp( ops[ j ], "delete" ) == 0 ) {
1022                                                         rdi.rdi_mask |= SN_DG_OP_DELETE;
1023
1024                                                 } else if ( strcasecmp( ops[ j ], "modify" ) == 0 ) {
1025                                                         rdi.rdi_mask |= SN_DG_OP_MODIFY;
1026
1027                                                 } else if ( strcasecmp( ops[ j ], "rename" ) == 0
1028                                                         || strcasecmp( ops[ j ], "modrdn" ) == 0 )
1029                                                 {
1030                                                         rdi.rdi_mask |= SN_DG_OP_RENAME;
1031
1032                                                 } else if ( strcasecmp( ops[ j ], "search" ) == 0 ) {
1033                                                         rdi.rdi_mask |= SN_DG_OP_SEARCH;
1034
1035                                                 } else if ( strcasecmp( ops[ j ], "extended" ) == 0 ) {
1036                                                         rdi.rdi_mask |= SN_DG_EXTENDED;
1037
1038                                                 } else if ( strcasecmp( ops[ j ], "auth" ) == 0 ) {
1039                                                         rdi.rdi_mask |= SN_DG_OP_AUTH;
1040
1041                                                 } else if ( strcasecmp( ops[ j ], "read" ) == 0 ) {
1042                                                         rdi.rdi_mask |= SN_DG_OP_READ;
1043
1044                                                 } else if ( strcasecmp( ops[ j ], "write" ) == 0 ) {
1045                                                         rdi.rdi_mask |= SN_DG_OP_WRITE;
1046
1047                                                 } else if ( strcasecmp( ops[ j ], "all" ) == 0 ) {
1048                                                         rdi.rdi_mask |= SN_DG_OP_ALL;
1049
1050                                                 } else {
1051                                                         snprintf( c->cr_msg, sizeof(c->cr_msg),
1052                                                                 "unknown op \"%s\"",
1053                                                                 ops[ j ] );
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;
1058                                                 }
1059                                         }
1060
1061                                         ldap_charray_free( ops );
1062
1063                                 } else if ( strncasecmp( c->argv[ i ], "text=", STRLENOF( "text=" ) ) == 0 )
1064                                 {
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;
1071                                         }
1072                                         ber_str2bv( &c->argv[ i ][ STRLENOF( "text=" ) ], 0, 1, &rdi.rdi_text );
1073
1074                                 } else if ( strncasecmp( c->argv[ i ], "matched=", STRLENOF( "matched=" ) ) == 0 )
1075                                 {
1076                                         struct berval   dn;
1077
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;
1084                                         }
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;
1093                                         }
1094
1095                                 } else if ( strncasecmp( c->argv[ i ], "ref=", STRLENOF( "ref=" ) ) == 0 )
1096                                 {
1097                                         char            **refs;
1098                                         int             j;
1099
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;
1106                                         }
1107
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 );
1116                                         }
1117
1118                                         refs = ldap_str2charray( &c->argv[ i ][ STRLENOF( "ref=" ) ], " " );
1119                                         assert( refs != NULL );
1120
1121                                         for ( j = 0; refs[ j ] != NULL; j++ ) {
1122                                                 struct berval   bv;
1123
1124                                                 ber_str2bv( refs[ j ], 0, 1, &bv );
1125                                                 ber_bvarray_add( &rdi.rdi_ref, &bv );
1126                                         }
1127
1128                                         ldap_charray_free( refs );
1129
1130                                 } else if ( strncasecmp( c->argv[ i ], "sleeptime=", STRLENOF( "sleeptime=" ) ) == 0 )
1131                                 {
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;
1138                                         }
1139
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;
1147                                         }
1148
1149                                 } else if ( strncasecmp( c->argv[ i ], "unsolicited=", STRLENOF( "unsolicited=" ) ) == 0 )
1150                                 {
1151                                         char            *data;
1152
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;
1159                                         }
1160
1161                                         data = strchr( &c->argv[ i ][ STRLENOF( "unsolicited=" ) ], ':' );
1162                                         if ( data != NULL ) {
1163                                                 struct berval   oid;
1164
1165                                                 if ( ldif_parse_line2( &c->argv[ i ][ STRLENOF( "unsolicited=" ) ],
1166                                                         &oid, &rdi.rdi_unsolicited_data, NULL ) )
1167                                                 {
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;
1173                                                 }
1174
1175                                                 ber_dupbv( &rdi.rdi_unsolicited_oid, &oid );
1176
1177                                         } else {
1178                                                 ber_str2bv( &c->argv[ i ][ STRLENOF( "unsolicited=" ) ], 0, 1,
1179                                                         &rdi.rdi_unsolicited_oid );
1180                                         }
1181
1182                                 } else if ( strncasecmp( c->argv[ i ], "flags=", STRLENOF( "flags=" ) ) == 0 )
1183                                 {
1184                                         char *arg = &c->argv[ i ][ STRLENOF( "flags=" ) ];
1185                                         if ( strcasecmp( arg, "disconnect" ) == 0 ) {
1186                                                 rdi.rdi_flags |= RDI_PRE_DISCONNECT;
1187
1188                                         } else if ( strcasecmp( arg, "pre-disconnect" ) == 0 ) {
1189                                                 rdi.rdi_flags |= RDI_PRE_DISCONNECT;
1190
1191                                         } else if ( strcasecmp( arg, "post-disconnect" ) == 0 ) {
1192                                                 rdi.rdi_flags |= RDI_POST_DISCONNECT;
1193
1194                                         } else {
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;
1200                                         }
1201
1202                                 } else {
1203                                         snprintf( c->cr_msg, sizeof(c->cr_msg),
1204                                                 "unknown option \"%s\"",
1205                                                 c->argv[ i ] );
1206                                         Debug( LDAP_DEBUG_CONFIG, "%s: retcode: %s\n",
1207                                                 c->log, c->cr_msg, 0 );
1208                                         return ARG_BAD_CONF;
1209                                 }
1210                         }
1211                 }
1212
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 ] );
1216                 }
1217                 next = rdi.rdi_line.bv_val = ch_malloc( rdi.rdi_line.bv_len + 1 );
1218
1219                 for ( i = 1; i < c->argc; i++ ) {
1220                         *next++ = '"';
1221                         next = lutil_strcopy( next, c->argv[ i ] );
1222                         *next++ = '"';
1223                         *next++ = ' ';
1224                 }
1225                 *--next = '\0';
1226                 
1227                 for ( rdip = &rd->rd_item; *rdip; rdip = &(*rdip)->rdi_next )
1228                         /* go to last */ ;
1229
1230                 
1231                 *rdip = ( retcode_item_t * )ch_malloc( sizeof( retcode_item_t ) );
1232                 *(*rdip) = rdi;
1233
1234                 rc = 0;
1235                 } break;
1236
1237         default:
1238                 rc = SLAP_CONF_UNKNOWN;
1239                 break;
1240         }
1241
1242         return rc;
1243 }
1244
1245 static int
1246 retcode_db_open( BackendDB *be, ConfigReply *cr)
1247 {
1248         slap_overinst   *on = (slap_overinst *)be->bd_info;
1249         retcode_t       *rd = (retcode_t *)on->on_bi.bi_private;
1250
1251         retcode_item_t  *rdi;
1252
1253         for ( rdi = rd->rd_item; rdi; rdi = rdi->rdi_next ) {
1254                 LDAPRDN                 rdn = NULL;
1255                 int                     rc, j;
1256                 char*                   p;
1257                 struct berval           val[ 3 ];
1258                 char                    buf[ SLAP_TEXT_BUFLEN ];
1259
1260                 /* DN */
1261                 rdi->rdi_e.e_name = rdi->rdi_dn;
1262                 rdi->rdi_e.e_nname = rdi->rdi_ndn;
1263
1264                 /* objectClass */
1265                 val[ 0 ] = oc_errObject->soc_cname;
1266                 val[ 1 ] = slap_schema.si_oc_extensibleObject->soc_cname;
1267                 BER_BVZERO( &val[ 2 ] );
1268
1269                 attr_merge( &rdi->rdi_e, slap_schema.si_ad_objectClass, val, NULL );
1270
1271                 /* RDN avas */
1272                 rc = ldap_bv2rdn( &rdi->rdi_dn, &rdn, (char **) &p,
1273                                 LDAP_DN_FORMAT_LDAP );
1274
1275                 assert( rc == LDAP_SUCCESS );
1276
1277                 for ( j = 0; rdn[ j ]; j++ ) {
1278                         LDAPAVA                 *ava = rdn[ j ];
1279                         AttributeDescription    *ad = NULL;
1280                         const char              *text;
1281
1282                         rc = slap_bv2ad( &ava->la_attr, &ad, &text );
1283                         assert( rc == LDAP_SUCCESS );
1284                         
1285                         attr_merge_normalize_one( &rdi->rdi_e, ad,
1286                                         &ava->la_value, NULL );
1287                 }
1288
1289                 ldap_rdnfree( rdn );
1290
1291                 /* error code */
1292                 snprintf( buf, sizeof( buf ), "%d", rdi->rdi_err );
1293                 ber_str2bv( buf, 0, 0, &val[ 0 ] );
1294
1295                 attr_merge_one( &rdi->rdi_e, ad_errCode, &val[ 0 ], NULL );
1296
1297                 if ( rdi->rdi_ref != NULL ) {
1298                         attr_merge_normalize( &rdi->rdi_e, slap_schema.si_ad_ref,
1299                                 rdi->rdi_ref, NULL );
1300                 }
1301
1302                 /* text */
1303                 if ( !BER_BVISNULL( &rdi->rdi_text ) ) {
1304                         val[ 0 ] = rdi->rdi_text;
1305
1306                         attr_merge_normalize_one( &rdi->rdi_e, ad_errText, &val[ 0 ], NULL );
1307                 }
1308
1309                 /* matched */
1310                 if ( !BER_BVISNULL( &rdi->rdi_matched ) ) {
1311                         val[ 0 ] = rdi->rdi_matched;
1312
1313                         attr_merge_normalize_one( &rdi->rdi_e, ad_errMatchedDN, &val[ 0 ], NULL );
1314                 }
1315
1316                 /* sleep time */
1317                 if ( rdi->rdi_sleeptime ) {
1318                         snprintf( buf, sizeof( buf ), "%d", rdi->rdi_sleeptime );
1319                         ber_str2bv( buf, 0, 0, &val[ 0 ] );
1320
1321                         attr_merge_one( &rdi->rdi_e, ad_errSleepTime, &val[ 0 ], NULL );
1322                 }
1323
1324                 /* operations */
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 );
1328                 }
1329
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 );
1333                 }
1334
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 );
1338                 }
1339
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 );
1343                 }
1344
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 );
1348                 }
1349
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 );
1353                 }
1354
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 );
1358                 }
1359
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 );
1363                 }
1364         }
1365
1366         return 0;
1367 }
1368
1369 static int
1370 retcode_db_destroy( BackendDB *be, ConfigReply *cr )
1371 {
1372         slap_overinst   *on = (slap_overinst *)be->bd_info;
1373         retcode_t       *rd = (retcode_t *)on->on_bi.bi_private;
1374
1375         if ( rd ) {
1376                 retcode_item_t  *rdi, *next;
1377
1378                 for ( rdi = rd->rd_item; rdi != NULL; rdi = next ) {
1379                         next = rdi->rdi_next;
1380                         retcode_item_destroy( rdi );
1381                 }
1382
1383                 if ( !BER_BVISNULL( &rd->rd_pdn ) ) {
1384                         ber_memfree( rd->rd_pdn.bv_val );
1385                 }
1386
1387                 if ( !BER_BVISNULL( &rd->rd_npdn ) ) {
1388                         ber_memfree( rd->rd_npdn.bv_val );
1389                 }
1390
1391                 ber_memfree( rd );
1392         }
1393
1394         return 0;
1395 }
1396
1397 #if SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC
1398 static
1399 #endif /* SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC */
1400 int
1401 retcode_initialize( void )
1402 {
1403         int             i, code;
1404
1405         static struct {
1406                 char                    *desc;
1407                 AttributeDescription    **ad;
1408         } retcode_at[] = {
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 "
1415                         "SINGLE-VALUE )",
1416                         &ad_errCode },
1417                 { "( 1.3.6.1.4.1.4203.666.11.4.1.2 "
1418                         "NAME ( 'errOp' ) "
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 )",
1423                         &ad_errOp},
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 "
1430                         "SINGLE-VALUE )",
1431                         &ad_errText },
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 "
1437                         "SINGLE-VALUE )",
1438                         &ad_errSleepTime },
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 "
1444                         "SINGLE-VALUE )",
1445                         &ad_errMatchedDN },
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 "
1451                         "SINGLE-VALUE )",
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 "
1457                         "SINGLE-VALUE )",
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 "
1463                         "SINGLE-VALUE )",
1464                         &ad_errDisconnect },
1465                 { NULL }
1466         };
1467
1468         static struct {
1469                 char            *desc;
1470                 ObjectClass     **oc;
1471         } retcode_oc[] = {
1472                 { "( 1.3.6.1.4.1.4203.666.11.4.3.0 "
1473                         "NAME ( 'errAbsObject' ) "
1474                         "SUP top ABSTRACT "
1475                         "MUST ( errCode ) "
1476                         "MAY ( "
1477                                 "cn "
1478                                 "$ description "
1479                                 "$ errOp "
1480                                 "$ errText "
1481                                 "$ errSleepTime "
1482                                 "$ errMatchedDN "
1483                                 "$ errUnsolicitedOID "
1484                                 "$ errUnsolicitedData "
1485                                 "$ errDisconnect "
1486                         ") )",
1487                         &oc_errAbsObject },
1488                 { "( 1.3.6.1.4.1.4203.666.11.4.3.1 "
1489                         "NAME ( 'errObject' ) "
1490                         "SUP errAbsObject STRUCTURAL "
1491                         ")",
1492                         &oc_errObject },
1493                 { "( 1.3.6.1.4.1.4203.666.11.4.3.2 "
1494                         "NAME ( 'errAuxObject' ) "
1495                         "SUP errAbsObject AUXILIARY "
1496                         ")",
1497                         &oc_errAuxObject },
1498                 { NULL }
1499         };
1500
1501
1502         for ( i = 0; retcode_at[ i ].desc != NULL; i++ ) {
1503                 code = register_at( retcode_at[ i ].desc, retcode_at[ i ].ad, 0 );
1504                 if ( code ) {
1505                         Debug( LDAP_DEBUG_ANY,
1506                                 "retcode: register_at failed\n", 0, 0, 0 );
1507                         return code;
1508                 }
1509
1510                 (*retcode_at[ i ].ad)->ad_type->sat_flags |= SLAP_AT_HIDE;
1511         }
1512
1513         for ( i = 0; retcode_oc[ i ].desc != NULL; i++ ) {
1514                 code = register_oc( retcode_oc[ i ].desc, retcode_oc[ i ].oc, 0 );
1515                 if ( code ) {
1516                         Debug( LDAP_DEBUG_ANY,
1517                                 "retcode: register_oc failed\n", 0, 0, 0 );
1518                         return code;
1519                 }
1520
1521                 (*retcode_oc[ i ].oc)->soc_flags |= SLAP_OC_HIDE;
1522         }
1523
1524         retcode.on_bi.bi_type = "retcode";
1525
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;
1529
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;
1537
1538         retcode.on_bi.bi_extended = retcode_op_func;
1539
1540         retcode.on_response = retcode_response;
1541
1542         retcode.on_bi.bi_cf_ocs = rcocs;
1543
1544         code = config_register_schema( rccfg, rcocs );
1545         if ( code ) {
1546                 return code;
1547         }
1548
1549         return overlay_register( &retcode );
1550 }
1551
1552 #if SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC
1553 int
1554 init_module( int argc, char *argv[] )
1555 {
1556         return retcode_initialize();
1557 }
1558 #endif /* SLAPD_OVER_RETCODE == SLAPD_MOD_DYNAMIC */
1559
1560 #endif /* SLAPD_OVER_RETCODE */