3 * Copyright (c) 1990 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
18 #include <ac/signal.h>
19 #include <ac/socket.h>
20 #include <ac/string.h>
21 #include <ac/syslog.h>
25 #include <quipu/commonarg.h>
26 #include <quipu/ds_error.h>
27 #include <quipu/dap2.h>
28 #include <quipu/dua.h>
31 #include "../../libraries/liblber/lber-int.h" /* get struct berelement */
39 #include "LDAP-types.h"
40 #if ISODEPACKAGE == IC
41 #include <compat/logger.h>
48 * client_request - called by do_queries() when there is activity on the
49 * client socket. It expects to be able to get an LDAP message from the
50 * client socket, parses the first couple of fields, and then calls
51 * do_request() to handle the request. If do_request() (or something
52 * called by it) returns a response to the client (e.g., in the case of
53 * an error), then client_request() is done. If the request is not
54 * responded to (and needs a response), it is added to the queue of
55 * outstanding requests. It will be responded to later via dsa_response(),
56 * once the DSA operation completes.
69 BerElement ber, *copyofber;
72 #ifdef LDAP_CONNECTIONLESS
73 struct sockaddr_in *sai;
75 Debug( LDAP_DEBUG_TRACE, "client_request%s\n",
76 udp ? " udp" : "", 0, 0 );
79 * Get the ldap message, which is a sequence of message id
81 * and then the actual request choice.
84 ber_init_w_nullc( &ber, 0 );
85 if ( (tag = ber_get_next( clientsb, &len, &ber )) == LBER_DEFAULT ) {
86 Debug( LDAP_DEBUG_ANY, "ber_get_next failed\n", 0, 0, 0 );
90 #ifdef LDAP_CONNECTIONLESS
91 if ( udp && dosyslog ) {
92 sai = (struct sockaddr_in *)ber_pvt_sb_udp_get_src( &clientsb );
93 syslog( LOG_INFO, "UDP request from unknown (%s)",
94 inet_ntoa( sai->sin_addr ) );
99 if ( ldap_debug & LDAP_DEBUG_BER )
100 trace_ber( tag, len, ber.ber_buf, stderr, 1, 1 );
105 * This tag should be a normal SEQUENCE tag. In release 2.0 this
106 * tag is 0x10. In the new stuff this is 0x30. To distinguish
107 * between 3.0 and the "correct" stuff, we look for an extra
108 * sequence tag after the bind tag.
111 Debug( LDAP_DEBUG_ANY, "bound %d\n", bound, 0, 0 );
114 if ( tag == OLD_LDAP_TAG_MESSAGE ) {
115 Debug( LDAP_DEBUG_ANY, "version 2.0 detected\n", 0,
118 syslog( LOG_INFO, "old version 2.0 detected" );
127 tber = ber; /* struct copy */
129 ttag = ber_skip_tag( &tber, &tlen );
130 tber.ber_ptr += tlen;
131 /* bind sequence header */
132 ttag = ber_skip_tag( &tber, &tlen );
133 ttag = ber_peek_tag( &tber, &tlen );
135 Debug( LDAP_DEBUG_ANY, "checking for 3.0 tag 0x%lx\n",
137 if ( ttag == LBER_SEQUENCE ) {
138 Debug( LDAP_DEBUG_ANY, "version 3.0 detected\n",
142 "old version 3.0 detected" );
150 if ( ber_get_int( &ber, &msgid ) != LDAP_TAG_MSGID ) {
151 send_ldap_result( clientsb, LBER_DEFAULT, msgid,
152 LDAP_PROTOCOL_ERROR, NULL, "Not an LDAP message" );
157 #ifdef LDAP_CONNECTIONLESS
161 ber_get_stringa( &ber, &logdn );
162 if ( logdn != NULL ) {
164 syslog( LOG_INFO, "UDP requestor: %s", logdn );
166 Debug( LDAP_DEBUG_ANY, "UDP requestor: %s\n", logdn, 0, 0 );
170 #endif /* LDAP_CONNECTIONLESS */
173 if ( ldap_compat == 30 )
174 tag = ber_skip_tag( &ber, &len );
177 tag = ber_peek_tag( &ber, &len );
178 if ( !udp && bound == 0 && tag != LDAP_REQ_BIND
180 && tag != OLD_LDAP_REQ_BIND
183 send_ldap_result( clientsb, tag, msgid, LDAP_OPERATIONS_ERROR,
184 NULL, "Bind operation must come first" );
189 #ifdef LDAP_CONNECTIONLESS
190 if (udp && tag != LDAP_REQ_SEARCH && tag != LDAP_REQ_ABANDON ) {
191 send_ldap_result( clientsb, tag, msgid, LDAP_OPERATIONS_ERROR,
192 NULL, "Only search is supported over UDP/CLDAP" );
196 sai = (struct sockaddr_in *) ber_pvt_sb_udp_get_src( &clientsb );
198 if ( get_cldap_msg( msgid, tag,
199 (struct sockaddr *)sai ) != NULL ) {
201 * duplicate request: toss this one
203 Debug( LDAP_DEBUG_TRACE,
204 "client_request tossing dup request id %ld from %s\n",
205 msgid, inet_ntoa( sai->sin_addr ), 0 );
212 copyofber = ber_dup( &ber );
214 m = add_msg( msgid, tag, copyofber, dsaconn, udp,
215 #ifdef LDAP_CONNECTIONLESS
216 (struct sockaddr *)sai );
222 * Call the appropriate routine to handle the request. If it
223 * returns a nonzero result, the message requires a response, and
224 * so it's left in the queue of outstanding requests, otherwise
228 if ( do_request( clientsb, m, &ber, &bound ) == 0 ) {
236 * do_request - called when a client makes a request, or when a referral
237 * error is returned. In the latter case, a connection is made to the
238 * referred to DSA, and do_request() is called to retry the operation over
239 * that connection. In the former case, do_request() is called to try
240 * the operation over the default association.
251 int resp_required = 0;
253 Debug( LDAP_DEBUG_TRACE, "do_request\n", 0, 0, 0 );
255 switch ( m->m_msgtype ) {
257 case OLD_LDAP_REQ_BIND:
260 resp_required = do_bind( clientsb, m, ber, bound );
264 case OLD_LDAP_REQ_UNBIND:
267 case LDAP_REQ_UNBIND_30:
269 case LDAP_REQ_UNBIND:
275 case OLD_LDAP_REQ_ADD:
278 resp_required = do_add( clientsb, m, ber );
282 case OLD_LDAP_REQ_DELETE:
285 case LDAP_REQ_DELETE_30:
287 case LDAP_REQ_DELETE:
288 resp_required = do_delete( clientsb, m, ber );
292 case OLD_LDAP_REQ_MODRDN:
294 case LDAP_REQ_MODRDN:
295 resp_required = do_modrdn( clientsb, m, ber );
299 case OLD_LDAP_REQ_MODIFY:
301 case LDAP_REQ_MODIFY:
302 resp_required = do_modify( clientsb, m, ber );
306 case OLD_LDAP_REQ_COMPARE:
308 case LDAP_REQ_COMPARE:
309 resp_required = do_compare( clientsb, m, ber );
313 case OLD_LDAP_REQ_SEARCH:
315 case LDAP_REQ_SEARCH:
316 resp_required = do_search( clientsb, m, ber );
320 case OLD_LDAP_REQ_ABANDON:
323 case LDAP_REQ_ABANDON_30:
325 case LDAP_REQ_ABANDON:
326 resp_required = do_abandon( m->m_conn, ber, m->m_uniqid );
330 Debug( LDAP_DEBUG_ANY, "unknown operation %d\n", m->m_msgtype,
333 send_ldap_msgresult( clientsb, m->m_msgtype, m,
334 LDAP_PROTOCOL_ERROR, NULL, "Unknown request type" );
338 return( resp_required );
342 * initiate_dap_operation - initiate a dap operation, rebinding and retrying
343 * the request if necessary. If the request is successfully initiated, 0 is
344 * returned. Otherwise, an indication of the error is returned.
348 initiate_dap_operation(
355 int i, rc, bound = 0;
356 struct DAPindication di;
358 Debug( LDAP_DEBUG_TRACE, "initiate_dap_operation\n", 0, 0, 0 );
360 if ( m->m_conn->c_ad == -1 && do_bind_real( m->m_conn, &bound,
361 &matched ) != LDAP_SUCCESS )
362 return( LDAP_UNAVAILABLE );
364 for ( i = 0; i < 2; i++ ) {
367 rc = DapCompare( m->m_conn->c_ad, m->m_uniqid,
368 (struct ds_compare_arg *) arg, &di, ROS_ASYNC );
372 rc = DapSearch( m->m_conn->c_ad, m->m_uniqid,
373 (struct ds_search_arg *) arg, &di, ROS_ASYNC );
377 rc = DapAddEntry( m->m_conn->c_ad, m->m_uniqid,
378 (struct ds_addentry_arg *) arg, &di, ROS_ASYNC );
382 rc = DapRemoveEntry( m->m_conn->c_ad, m->m_uniqid,
383 (struct ds_removeentry_arg *) arg, &di, ROS_ASYNC );
387 rc = DapModifyEntry( m->m_conn->c_ad, m->m_uniqid,
388 (struct ds_modifyentry_arg *) arg, &di, ROS_ASYNC );
392 rc = DapRead( m->m_conn->c_ad, m->m_uniqid,
393 (struct ds_read_arg *) arg, &di, ROS_ASYNC );
397 rc = DapModifyRDN( m->m_conn->c_ad, m->m_uniqid,
398 (struct ds_modifyrdn_arg *) arg, &di, ROS_ASYNC );
405 Debug( LDAP_DEBUG_TRACE, "operation initiated %d\n", rc, 0,
412 * the operation was not invoked - try rebinding, then
416 (void) dap_unbind( m->m_conn->c_ad );
418 if ( do_bind_real( m->m_conn, &bound, &matched )
423 m->m_conn->c_ad = -1;
425 return( LDAP_UNAVAILABLE ); /* DSA was unreachable */
436 int read_pdu /* If non-zero, PDU was read from client. 0 == PDU is being written */
440 PS input_ps = NULLPS;
444 Debug( LDAP_DEBUG_TRACE, "trace_ber(tag=%#x, ber=%#lx, len=%d)\n", tag,
445 (unsigned long) ber, len );
447 if ( (buf = (unsigned char *) malloc( len + 6 )) == NULL ) {
448 fprintf( trace_file, "Unable to allocate memory\n" );
457 SAFEMEMCPY( buf + 6, ber, len );
459 SAFEMEMCPY( buf, ber, len );
461 if ( (input_ps = ps_alloc( str_open )) == NULLPS )
462 fprintf( trace_file, "ps_alloc failed\n" );
463 else if ( str_setup( input_ps, (char *)buf, len + 6, 1 ) != OK )
464 fprintf( trace_file, "str_setup\n" );
465 else if ( (pe = ps2pe( input_ps )) == NULLPE ) {
466 fprintf(trace_file, "ps2pe: %s\n",
467 ps_error( input_ps->ps_errno ) );
468 ber_bprint( (char *) buf, len + 6 );
473 "-", NULLCP, NULLCP, LLOG_PDUS,
474 LLOG_NONE, -1, 0, NOTOK
476 struct type_LDAP_LDAPMessage *ldap_msg = NULL;
478 if ( decode_LDAP_LDAPMessage(pe, 1, 0, NULL, &ldap_msg)
482 "Error decoding LDAPMessage:\n [%s]\n",
484 fprintf( trace_file, "Here is the PDU:\n" );
485 vsetfp( trace_file, NULL );
488 if (log.ll_events & LLOG_PDUS) {
489 pvpdu (&log, print_LDAP_LDAPMessage_P, pe,
491 "<Bad LDAPMessage>" : "<LDAPMessage>",
495 PLOGP(&log, LDAP_LDAPMessage, pe, failed ? "<Bad LDAPMessage>" : "<LDAPMessage>", read_pdu);
498 free_LDAP_LDAPMessage(ldap_msg);
500 vsetfp( trace_file, NULL );