2 * Copyright (c) 1990 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
17 #include <ac/socket.h>
18 #include <ac/string.h>
19 #include <ac/syslog.h>
21 #include <quipu/dsap.h>
22 #include <quipu/dap2.h>
23 #include <quipu/dua.h>
26 #include "../../libraries/liblber/lber-int.h" /* get struct berelement */
31 * dsa_response - called by do_queries() when there is activity on one of
32 * the DSA associations. It is passed the association descriptor on which
33 * the activity occurred, and the client socket. It figures out what kind
34 * of activity it was (e.g., result of a previously initiated operation,
35 * error return, etc), and calls the appropriate routine to send a response
36 * to the client, or to continue the operation in some cases (e.g., modify),
37 * or to chase a referral and retry an operation.
39 * If the client is actually given a response, dsa_response() removes the
40 * corresponding request from the queue of outstanding requests. If the
41 * activity was an error referral, a connection is made to the referred to
42 * DSA (if possible), and do_request() is called to retry the request.
51 struct DAPindication di;
54 struct DAPpreject *dp;
59 int incr, delete, rc, ldaperr;
61 Debug( LDAP_DEBUG_TRACE, "dsa_response on ad %d\n", dsaconn->c_ad, 0,
64 if ( (rc = DapInitWaitRequest( dsaconn->c_ad, OK, &di )) == DONE ) {
65 Debug( LDAP_DEBUG_ANY, "DapInitWaitRequest: DONE\n", 0, 0, 0 );
69 Debug( LDAP_DEBUG_ARGS, "DapInitWaitRequest: result %d type %d\n", rc,
73 switch ( di.di_type ) {
75 dr = &di.di_result.dr_res;
76 if ( (m = get_msg( di.di_result.dr_id )) == NULL ) {
77 Debug( LDAP_DEBUG_ANY, "DI_RESULT: can't find msg %d\n",
78 di.di_result.dr_id, 0, 0 );
82 Debug( LDAP_DEBUG_ARGS, "DI_RESULT: type %d\n",
83 dr->result_type, 0, 0 );
85 switch ( dr->result_type ) {
87 compare_result( clientsb, m, &dr->res_cm );
91 search_result( clientsb, m, &dr->res_sr );
95 add_result( clientsb, m );
99 delete_result( clientsb, m );
103 modify_result( clientsb, m );
107 if ( do_modify2( clientsb, m, &dr->res_rd ) != 0 )
112 modrdn_result( clientsb, m );
122 de = &di.di_error.de_err;
123 if ( (m = get_msg( di.di_error.de_id )) == NULL ) {
124 Debug( LDAP_DEBUG_ANY, "DI_ERROR: can't find msg %d\n",
125 di.di_error.de_id, 0, 0 );
128 if ( m->m_msgtype == LDAP_REQ_SEARCH
130 || m->m_msgtype == OLD_LDAP_REQ_SEARCH
134 else if ( m->m_msgtype == LDAP_REQ_DELETE )
135 incr = (LDAP_RES_DELETE - LDAP_REQ_DELETE);
139 Debug( LDAP_DEBUG_ARGS, "DI_ERROR\n", 0, 0, 0 );
142 * chase down referrals, retry operation there. only do
143 * this for modify-like operations, since we assume the
144 * dsa should have been able to chase anything else that
145 * wasn't really down.
148 if ( de->dse_type == DSE_REFERRAL ) {
151 switch ( m->m_msgtype ) {
153 case OLD_LDAP_REQ_ADD:
154 case OLD_LDAP_REQ_MODIFY:
155 case OLD_LDAP_REQ_MODRDN:
156 case OLD_LDAP_REQ_DELETE:
157 case OLD_LDAP_REQ_COMPARE:
158 case OLD_LDAP_REQ_SEARCH:
161 case LDAP_REQ_DELETE_30:
164 case LDAP_REQ_MODIFY:
165 case LDAP_REQ_MODRDN:
166 case LDAP_REQ_DELETE:
167 case LDAP_REQ_COMPARE:
168 case LDAP_REQ_SEARCH:
169 /* chase down the referral */
170 if ( (rc = chase_referral( clientsb, m, de,
171 &matched )) != LDAP_SUCCESS ) {
172 send_ldap_msgresult( clientsb,
173 m->m_msgtype + incr, m, rc,
174 matched, "Can't chase referral" );
179 /* now retry the operation */
180 bercopy = ber_dup( m->m_ber );
181 if ( do_request( clientsb, m, bercopy, &bound )
185 ber_free( bercopy, 0 );
190 send_ldap_msgresult( clientsb, m->m_msgtype +
191 incr, m, LDAP_UNAVAILABLE, NULL, "" );
195 } else if ( de->dse_type == DSE_ABANDONED ) {
200 /* not a referral - convert the error and return to client */
201 ldaperr = x500err2ldaperr( de, &matched );
204 print_error( de ); /* prints, then calls free */
209 send_ldap_msgresult( clientsb, m->m_msgtype + incr, m,
210 ldaperr, matched, "" );
216 if ( (m = get_msg( dp->dp_id )) == NULL ) {
217 Debug(LDAP_DEBUG_ANY, "DI_PREJECT: can't find msg %d\n",
222 Debug( LDAP_DEBUG_ARGS, "DI_PREJECT src %d rson %d inf (%s)\n",
223 dp->dp_source, dp->dp_reason, dp->dp_cc ? dp->dp_data
226 send_ldap_msgresult( clientsb, m->m_msgtype, m,
227 LDAP_UNAVAILABLE, NULL, "Got PREJECT from X.500" );
235 Debug( LDAP_DEBUG_ARGS, "DI_ABORT src %d rson %d inf (%s)\n",
236 da->da_source, da->da_reason, da->da_cc ? da->da_data
239 /* assume this always means more stuff coming... */
240 if ( da->da_reason == DA_ROS )
243 /* moby hack - but how else do you tell the difference? */
244 if ( isclosed( dsaconn->c_ad ) ) {
245 send_msg( dsaconn, clientsb, LDAP_UNAVAILABLE,
246 "Got ABORT from X.500" );
250 /* notify outstanding requests of the failure */
251 send_msg( dsaconn, clientsb, LDAP_OPERATIONS_ERROR,
252 "Got unknown ABORT from X.500" );
259 Debug( LDAP_DEBUG_ANY, "unknown result type %d\n", di.di_type,
262 dsaconn->c_ad = -1; /* better safe... */
267 if ( delete && m != NULL )
281 #ifdef LDAP_CONNECTIONLESS
283 SAFEMEMCPY( (char *)sb->sb_useaddr, &m->m_clientaddr,
284 sizeof( struct sockaddr ));
285 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
286 inet_ntoa(((struct sockaddr_in *)
287 sb->sb_useaddr)->sin_addr ),
288 ((struct sockaddr_in *)sb->sb_useaddr)->sin_port, 0 );
291 return( send_ldap_result( sb, tag, m->m_msgid, err, matched, text ) );
306 #ifdef LDAP_CONNECTIONLESS
308 cldap = ( sb->sb_naddr > 0 );
311 Debug( LDAP_DEBUG_TRACE, "send_ldap_result\n", 0, 0, 0 );
313 if ( tag == LBER_DEFAULT )
315 tag = ldap_compat == 20 ? OLD_LBER_SEQUENCE : LBER_SEQUENCE;
320 if ( (ber = der_alloc()) == NULLBER ) {
321 Debug( LDAP_DEBUG_ANY, "der_alloc failed\n", 0, 0, 0 );
325 if ( version != 1 ) {
327 if ( ldap_compat == 20 ) {
328 rc = ber_printf( ber, "t{it{tess}}", OLD_LBER_SEQUENCE,
329 msgid, tag, LBER_INTEGER, err,
330 matched ? matched : "", text );
334 if ( ldap_compat == 30 ) {
335 rc = ber_printf( ber, "{it{{ess}}}", msgid, tag, err,
336 matched ? matched : "", text );
339 #ifdef LDAP_CONNECTIONLESS
341 rc = ber_printf( ber, "{is{t{ess}}}", msgid, "", tag,
342 err, matched ? matched : "", text );
345 rc = ber_printf( ber, "{it{ess}}", msgid, tag, err, matched ?
346 matched : "", text );
348 /* version 1 always uses the broken stuff */
349 rc = ber_printf( ber, "t{it{is}}", OLD_LBER_SEQUENCE, msgid,
354 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
359 if ( ldap_debug & LDAP_DEBUG_BER )
360 trace_ber( 0, ber->ber_ptr - ber->ber_buf, ber->ber_buf,
364 if ( ber_flush( sb, ber, 1 ) != 0 ) {
365 Debug( LDAP_DEBUG_ANY, "ber_flush failed\n", 0, 0, 0 );