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>
31 extern int ldap_compat;
35 * dsa_response - called by do_queries() when there is activity on one of
36 * the DSA associations. It is passed the association descriptor on which
37 * the activity occurred, and the client socket. It figures out what kind
38 * of activity it was (e.g., result of a previously initiated operation,
39 * error return, etc), and calls the appropriate routine to send a response
40 * to the client, or to continue the operation in some cases (e.g., modify),
41 * or to chase a referral and retry an operation.
43 * If the client is actually given a response, dsa_response() removes the
44 * corresponding request from the queue of outstanding requests. If the
45 * activity was an error referral, a connection is made to the referred to
46 * DSA (if possible), and do_request() is called to retry the request.
55 struct DAPindication di;
58 struct DAPpreject *dp;
63 int incr, delete, rc, ldaperr;
65 Debug( LDAP_DEBUG_TRACE, "dsa_response on ad %d\n", dsaconn->c_ad, 0,
68 if ( (rc = DapInitWaitRequest( dsaconn->c_ad, OK, &di )) == DONE ) {
69 Debug( LDAP_DEBUG_ANY, "DapInitWaitRequest: DONE\n", 0, 0, 0 );
73 Debug( LDAP_DEBUG_ARGS, "DapInitWaitRequest: result %d type %d\n", rc,
77 switch ( di.di_type ) {
79 dr = &di.di_result.dr_res;
80 if ( (m = get_msg( di.di_result.dr_id )) == NULL ) {
81 Debug( LDAP_DEBUG_ANY, "DI_RESULT: can't find msg %d\n",
82 di.di_result.dr_id, 0, 0 );
86 Debug( LDAP_DEBUG_ARGS, "DI_RESULT: type %d\n",
87 dr->result_type, 0, 0 );
89 switch ( dr->result_type ) {
91 compare_result( clientsb, m, &dr->res_cm );
95 search_result( clientsb, m, &dr->res_sr );
99 add_result( clientsb, m );
103 delete_result( clientsb, m );
107 modify_result( clientsb, m );
111 if ( do_modify2( clientsb, m, &dr->res_rd ) != 0 )
116 modrdn_result( clientsb, m );
126 de = &di.di_error.de_err;
127 if ( (m = get_msg( di.di_error.de_id )) == NULL ) {
128 Debug( LDAP_DEBUG_ANY, "DI_ERROR: can't find msg %d\n",
129 di.di_error.de_id, 0, 0 );
132 if ( m->m_msgtype == LDAP_REQ_SEARCH
134 || m->m_msgtype == OLD_LDAP_REQ_SEARCH
138 else if ( m->m_msgtype == LDAP_REQ_DELETE )
139 incr = (LDAP_RES_DELETE - LDAP_REQ_DELETE);
143 Debug( LDAP_DEBUG_ARGS, "DI_ERROR\n", 0, 0, 0 );
146 * chase down referrals, retry operation there. only do
147 * this for modify-like operations, since we assume the
148 * dsa should have been able to chase anything else that
149 * wasn't really down.
152 if ( de->dse_type == DSE_REFERRAL ) {
155 switch ( m->m_msgtype ) {
157 case OLD_LDAP_REQ_ADD:
158 case OLD_LDAP_REQ_MODIFY:
159 case OLD_LDAP_REQ_MODRDN:
160 case OLD_LDAP_REQ_DELETE:
161 case OLD_LDAP_REQ_COMPARE:
162 case OLD_LDAP_REQ_SEARCH:
165 case LDAP_REQ_DELETE_30:
168 case LDAP_REQ_MODIFY:
169 case LDAP_REQ_MODRDN:
170 case LDAP_REQ_DELETE:
171 case LDAP_REQ_COMPARE:
172 case LDAP_REQ_SEARCH:
173 /* chase down the referral */
174 if ( (rc = chase_referral( clientsb, m, de,
175 &matched )) != LDAP_SUCCESS ) {
176 send_ldap_msgresult( clientsb,
177 m->m_msgtype + incr, m, rc,
178 matched, "Can't chase referral" );
183 /* now retry the operation */
184 bercopy = ber_dup( m->m_ber );
185 if ( do_request( clientsb, m, bercopy, &bound )
189 ber_free( bercopy, 0 );
194 send_ldap_msgresult( clientsb, m->m_msgtype +
195 incr, m, LDAP_UNAVAILABLE, NULL, "" );
199 } else if ( de->dse_type == DSE_ABANDONED ) {
204 /* not a referral - convert the error and return to client */
205 ldaperr = x500err2ldaperr( de, &matched );
208 print_error( de ); /* prints, then calls free */
213 send_ldap_msgresult( clientsb, m->m_msgtype + incr, m,
214 ldaperr, matched, "" );
220 if ( (m = get_msg( dp->dp_id )) == NULL ) {
221 Debug(LDAP_DEBUG_ANY, "DI_PREJECT: can't find msg %d\n",
226 Debug( LDAP_DEBUG_ARGS, "DI_PREJECT src %d rson %d inf (%s)\n",
227 dp->dp_source, dp->dp_reason, dp->dp_cc ? dp->dp_data
230 send_ldap_msgresult( clientsb, m->m_msgtype, m,
231 LDAP_UNAVAILABLE, NULL, "Got PREJECT from X.500" );
239 Debug( LDAP_DEBUG_ARGS, "DI_ABORT src %d rson %d inf (%s)\n",
240 da->da_source, da->da_reason, da->da_cc ? da->da_data
243 /* assume this always means more stuff coming... */
244 if ( da->da_reason == DA_ROS )
247 /* moby hack - but how else do you tell the difference? */
248 if ( isclosed( dsaconn->c_ad ) ) {
249 send_msg( dsaconn, clientsb, LDAP_UNAVAILABLE,
250 "Got ABORT from X.500" );
254 /* notify outstanding requests of the failure */
255 send_msg( dsaconn, clientsb, LDAP_OPERATIONS_ERROR,
256 "Got unknown ABORT from X.500" );
263 Debug( LDAP_DEBUG_ANY, "unknown result type %d\n", di.di_type,
266 dsaconn->c_ad = -1; /* better safe... */
271 if ( delete && m != NULL )
285 #ifdef LDAP_CONNECTIONLESS
287 SAFEMEMCPY( (char *)sb->sb_useaddr, &m->m_clientaddr,
288 sizeof( struct sockaddr ));
289 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
290 inet_ntoa(((struct sockaddr_in *)
291 sb->sb_useaddr)->sin_addr ),
292 ((struct sockaddr_in *)sb->sb_useaddr)->sin_port, 0 );
295 return( send_ldap_result( sb, tag, m->m_msgid, err, matched, text ) );
310 #ifdef LDAP_CONNECTIONLESS
315 #ifdef LDAP_CONNECTIONLESS
316 cldap = ( sb->sb_naddr > 0 );
319 Debug( LDAP_DEBUG_TRACE, "send_ldap_result\n", 0, 0, 0 );
321 if ( tag == LBER_DEFAULT )
323 tag = ldap_compat == 20 ? OLD_LBER_SEQUENCE : LBER_SEQUENCE;
328 if ( (ber = der_alloc()) == NULLBER ) {
329 Debug( LDAP_DEBUG_ANY, "der_alloc failed\n", 0, 0, 0 );
333 if ( version != 1 ) {
335 if ( ldap_compat == 20 ) {
336 rc = ber_printf( ber, "t{it{tess}}", OLD_LBER_SEQUENCE,
337 msgid, tag, LBER_INTEGER, err,
338 matched ? matched : "", text );
342 if ( ldap_compat == 30 ) {
343 rc = ber_printf( ber, "{it{{ess}}}", msgid, tag, err,
344 matched ? matched : "", text );
347 #ifdef LDAP_CONNECTIONLESS
349 rc = ber_printf( ber, "{is{t{ess}}}", msgid, "", tag,
350 err, matched ? matched : "", text );
353 rc = ber_printf( ber, "{it{ess}}", msgid, tag, err, matched ?
354 matched : "", text );
356 /* version 1 always uses the broken stuff */
357 rc = ber_printf( ber, "t{it{is}}", OLD_LBER_SEQUENCE, msgid,
362 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
367 if ( ldap_debug & LDAP_DEBUG_BER )
368 trace_ber( 0, ber->ber_ptr - ber->ber_buf, ber->ber_buf,
372 if ( ber_flush( sb, ber, 1 ) != 0 ) {
373 Debug( LDAP_DEBUG_ANY, "ber_flush failed\n", 0, 0, 0 );