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.
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <quipu/dsap.h>
19 #include <quipu/dap2.h>
20 #include <quipu/dua.h>
24 #include <sys/syslog.h>
32 extern int ldap_compat;
36 * dsa_response - called by do_queries() when there is activity on one of
37 * the DSA associations. It is passed the association descriptor on which
38 * the activity occurred, and the client socket. It figures out what kind
39 * of activity it was (e.g., result of a previously initiated operation,
40 * error return, etc), and calls the appropriate routine to send a response
41 * to the client, or to continue the operation in some cases (e.g., modify),
42 * or to chase a referral and retry an operation.
44 * If the client is actually given a response, dsa_response() removes the
45 * corresponding request from the queue of outstanding requests. If the
46 * activity was an error referral, a connection is made to the referred to
47 * DSA (if possible), and do_request() is called to retry the request.
56 struct DAPindication di;
59 struct DAPpreject *dp;
64 int incr, delete, rc, ldaperr;
66 Debug( LDAP_DEBUG_TRACE, "dsa_response on ad %d\n", dsaconn->c_ad, 0,
69 if ( (rc = DapInitWaitRequest( dsaconn->c_ad, OK, &di )) == DONE ) {
70 Debug( LDAP_DEBUG_ANY, "DapInitWaitRequest: DONE\n", 0, 0, 0 );
74 Debug( LDAP_DEBUG_ARGS, "DapInitWaitRequest: result %d type %d\n", rc,
78 switch ( di.di_type ) {
80 dr = &di.di_result.dr_res;
81 if ( (m = get_msg( di.di_result.dr_id )) == NULL ) {
82 Debug( LDAP_DEBUG_ANY, "DI_RESULT: can't find msg %d\n",
83 di.di_result.dr_id, 0, 0 );
87 Debug( LDAP_DEBUG_ARGS, "DI_RESULT: type %d\n",
88 dr->result_type, 0, 0 );
90 switch ( dr->result_type ) {
92 compare_result( clientsb, m, &dr->res_cm );
96 search_result( clientsb, m, &dr->res_sr );
100 add_result( clientsb, m );
104 delete_result( clientsb, m );
108 modify_result( clientsb, m );
112 if ( do_modify2( clientsb, m, &dr->res_rd ) != 0 )
117 modrdn_result( clientsb, m );
127 de = &di.di_error.de_err;
128 if ( (m = get_msg( di.di_error.de_id )) == NULL ) {
129 Debug( LDAP_DEBUG_ANY, "DI_ERROR: can't find msg %d\n",
130 di.di_error.de_id, 0, 0 );
133 if ( m->m_msgtype == LDAP_REQ_SEARCH
135 || m->m_msgtype == OLD_LDAP_REQ_SEARCH
139 else if ( m->m_msgtype == LDAP_REQ_DELETE )
140 incr = (LDAP_RES_DELETE - LDAP_REQ_DELETE);
144 Debug( LDAP_DEBUG_ARGS, "DI_ERROR\n", 0, 0, 0 );
147 * chase down referrals, retry operation there. only do
148 * this for modify-like operations, since we assume the
149 * dsa should have been able to chase anything else that
150 * wasn't really down.
153 if ( de->dse_type == DSE_REFERRAL ) {
156 switch ( m->m_msgtype ) {
158 case OLD_LDAP_REQ_ADD:
159 case OLD_LDAP_REQ_MODIFY:
160 case OLD_LDAP_REQ_MODRDN:
161 case OLD_LDAP_REQ_DELETE:
162 case OLD_LDAP_REQ_COMPARE:
163 case OLD_LDAP_REQ_SEARCH:
166 case LDAP_REQ_DELETE_30:
169 case LDAP_REQ_MODIFY:
170 case LDAP_REQ_MODRDN:
171 case LDAP_REQ_DELETE:
172 case LDAP_REQ_COMPARE:
173 case LDAP_REQ_SEARCH:
174 /* chase down the referral */
175 if ( (rc = chase_referral( clientsb, m, de,
176 &matched )) != LDAP_SUCCESS ) {
177 send_ldap_msgresult( clientsb,
178 m->m_msgtype + incr, m, rc,
179 matched, "Can't chase referral" );
184 /* now retry the operation */
185 bercopy = ber_dup( m->m_ber );
186 if ( do_request( clientsb, m, bercopy, &bound )
190 ber_free( bercopy, 0 );
195 send_ldap_msgresult( clientsb, m->m_msgtype +
196 incr, m, LDAP_UNAVAILABLE, NULL, "" );
200 } else if ( de->dse_type == DSE_ABANDONED ) {
205 /* not a referral - convert the error and return to client */
206 ldaperr = x500err2ldaperr( de, &matched );
209 print_error( de ); /* prints, then calls free */
214 send_ldap_msgresult( clientsb, m->m_msgtype + incr, m,
215 ldaperr, matched, "" );
221 if ( (m = get_msg( dp->dp_id )) == NULL ) {
222 Debug(LDAP_DEBUG_ANY, "DI_PREJECT: can't find msg %d\n",
227 Debug( LDAP_DEBUG_ARGS, "DI_PREJECT src %d rson %d inf (%s)\n",
228 dp->dp_source, dp->dp_reason, dp->dp_cc ? dp->dp_data
231 send_ldap_msgresult( clientsb, m->m_msgtype, m,
232 LDAP_UNAVAILABLE, NULL, "Got PREJECT from X.500" );
240 Debug( LDAP_DEBUG_ARGS, "DI_ABORT src %d rson %d inf (%s)\n",
241 da->da_source, da->da_reason, da->da_cc ? da->da_data
244 /* assume this always means more stuff coming... */
245 if ( da->da_reason == DA_ROS )
248 /* moby hack - but how else do you tell the difference? */
249 if ( isclosed( dsaconn->c_ad ) ) {
250 send_msg( dsaconn, clientsb, LDAP_UNAVAILABLE,
251 "Got ABORT from X.500" );
255 /* notify outstanding requests of the failure */
256 send_msg( dsaconn, clientsb, LDAP_OPERATIONS_ERROR,
257 "Got unknown ABORT from X.500" );
264 Debug( LDAP_DEBUG_ANY, "unknown result type %d\n", di.di_type,
267 dsaconn->c_ad = -1; /* better safe... */
272 if ( delete && m != NULL )
288 SAFEMEMCPY( (char *)sb->sb_useaddr, &m->m_clientaddr,
289 sizeof( struct sockaddr ));
290 Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n",
291 inet_ntoa(((struct sockaddr_in *)
292 sb->sb_useaddr)->sin_addr ),
293 ((struct sockaddr_in *)sb->sb_useaddr)->sin_port, 0 );
296 return( send_ldap_result( sb, tag, m->m_msgid, err, matched, text ) );
317 cldap = ( sb->sb_naddr > 0 );
320 Debug( LDAP_DEBUG_TRACE, "send_ldap_result\n", 0, 0, 0 );
322 if ( tag == LBER_DEFAULT )
324 tag = ldap_compat == 20 ? OLD_LBER_SEQUENCE : LBER_SEQUENCE;
329 if ( (ber = der_alloc()) == NULLBER ) {
330 Debug( LDAP_DEBUG_ANY, "der_alloc failed\n", 0, 0, 0 );
334 if ( version != 1 ) {
336 if ( ldap_compat == 20 ) {
337 rc = ber_printf( ber, "t{it{tess}}", OLD_LBER_SEQUENCE,
338 msgid, tag, LBER_INTEGER, err,
339 matched ? matched : "", text );
343 if ( ldap_compat == 30 ) {
344 rc = ber_printf( ber, "{it{{ess}}}", msgid, tag, err,
345 matched ? matched : "", text );
350 rc = ber_printf( ber, "{is{t{ess}}}", msgid, "", tag,
351 err, matched ? matched : "", text );
354 rc = ber_printf( ber, "{it{ess}}", msgid, tag, err, matched ?
355 matched : "", text );
357 /* version 1 always uses the broken stuff */
358 rc = ber_printf( ber, "t{it{is}}", OLD_LBER_SEQUENCE, msgid,
363 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
368 if ( ldap_debug & LDAP_DEBUG_BER )
369 trace_ber( 0, ber->ber_ptr - ber->ber_buf, ber->ber_buf,
373 if ( ber_flush( sb, ber, 1 ) != 0 ) {
374 Debug( LDAP_DEBUG_ANY, "ber_flush failed\n", 0, 0, 0 );