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>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
23 #include <quipu/commonarg.h>
24 #include <quipu/ds_error.h>
25 #include <quipu/dap2.h>
26 #include <quipu/dua.h>
30 #include <sys/syslog.h>
42 #include "LDAP-types.h"
43 #if ISODEPACKAGE == IC
44 #include <compat/logger.h>
51 * client_request - called by do_queries() when there is activity on the
52 * client socket. It expects to be able to get an LDAP message from the
53 * client socket, parses the first couple of fields, and then calls
54 * do_request() to handle the request. If do_request() (or something
55 * called by it) returns a response to the client (e.g., in the case of
56 * an error), then client_request() is done. If the request is not
57 * responded to (and needs a response), it is added to the queue of
58 * outstanding requests. It will be responded to later via dsa_response(),
59 * once the DSA operation completes.
72 BerElement ber, *copyofber;
75 extern char *bound_dn, *bound_pw;
76 struct PSAPaddr *psap_cpy();
78 extern int ldap_compat;
81 Debug( LDAP_DEBUG_TRACE, "client_request%s\n",
82 udp ? " udp" : "", 0, 0 );
85 * Get the ldap message, which is a sequence of message id
86 * and then the actual request choice.
90 if ( (tag = ber_get_next( clientsb, &len, &ber )) == LBER_DEFAULT ) {
91 Debug( LDAP_DEBUG_ANY, "ber_get_next failed\n", 0, 0, 0 );
96 if ( udp && dosyslog ) {
97 syslog( LOG_INFO, "UDP request from unknown (%s)",
98 inet_ntoa( ((struct sockaddr_in *)
99 clientsb->sb_fromaddr)->sin_addr ));
104 if ( ldap_debug & LDAP_DEBUG_BER )
105 trace_ber( tag, len, ber.ber_buf, stderr, 1, 1 );
110 * This tag should be a normal SEQUENCE tag. In release 2.0 this
111 * tag is 0x10. In the new stuff this is 0x30. To distinguish
112 * between 3.0 and the "correct" stuff, we look for an extra
113 * sequence tag after the bind tag.
116 Debug( LDAP_DEBUG_ANY, "bound %d\n", bound, 0, 0 );
119 if ( tag == OLD_LDAP_TAG_MESSAGE ) {
120 Debug( LDAP_DEBUG_ANY, "version 2.0 detected\n", 0,
123 syslog( LOG_INFO, "old version 2.0 detected" );
132 tber = ber; /* struct copy */
134 ttag = ber_skip_tag( &tber, &tlen );
135 tber.ber_ptr += tlen;
136 /* bind sequence header */
137 ttag = ber_skip_tag( &tber, &tlen );
138 ttag = ber_peek_tag( &tber, &tlen );
140 Debug( LDAP_DEBUG_ANY, "checking for 3.0 tag 0x%x\n",
142 if ( ttag == LBER_SEQUENCE ) {
143 Debug( LDAP_DEBUG_ANY, "version 3.0 detected\n",
147 "old version 3.0 detected" );
155 if ( ber_get_int( &ber, &msgid ) != LDAP_TAG_MSGID ) {
156 send_ldap_result( clientsb, LBER_DEFAULT, msgid,
157 LDAP_PROTOCOL_ERROR, NULL, "Not an LDAP message" );
166 ber_get_stringa( &ber, &logdn );
167 if ( logdn != NULL ) {
169 syslog( LOG_INFO, "UDP requestor: %s", logdn );
171 Debug( LDAP_DEBUG_ANY, "UDP requestor: %s\n", logdn, 0, 0 );
178 if ( ldap_compat == 30 )
179 tag = ber_skip_tag( &ber, &len );
182 tag = ber_peek_tag( &ber, &len );
183 if ( !udp && bound == 0 && tag != LDAP_REQ_BIND
185 && tag != OLD_LDAP_REQ_BIND
188 send_ldap_result( clientsb, tag, msgid, LDAP_OPERATIONS_ERROR,
189 NULL, "Bind operation must come first" );
195 if (udp && tag != LDAP_REQ_SEARCH && tag != LDAP_REQ_ABANDON ) {
196 send_ldap_result( clientsb, tag, msgid, LDAP_OPERATIONS_ERROR,
197 NULL, "Only search is supported over UDP/CLDAP" );
202 if ( get_cldap_msg( msgid, tag,
203 (struct sockaddr *)clientsb->sb_fromaddr ) != NULL ) {
205 * duplicate request: toss this one
207 Debug( LDAP_DEBUG_TRACE,
208 "client_request tossing dup request id %d from %s\n",
209 msgid, inet_ntoa( ((struct sockaddr_in *)
210 clientsb->sb_fromaddr)->sin_addr ), 0 );
216 copyofber = ber_dup( &ber );
218 m = add_msg( msgid, tag, copyofber, dsaconn, udp,
220 (struct sockaddr *)clientsb->sb_fromaddr );
226 * Call the appropriate routine to handle the request. If it
227 * returns a nonzero result, the message requires a response, and
228 * so it's left in the queue of outstanding requests, otherwise
232 if ( do_request( clientsb, m, &ber, &bound ) == 0 ) {
240 * do_request - called when a client makes a request, or when a referral
241 * error is returned. In the latter case, a connection is made to the
242 * referred to DSA, and do_request() is called to retry the operation over
243 * that connection. In the former case, do_request() is called to try
244 * the operation over the default association.
255 int resp_required = 0;
257 Debug( LDAP_DEBUG_TRACE, "do_request\n", 0, 0, 0 );
259 switch ( m->m_msgtype ) {
261 case OLD_LDAP_REQ_BIND:
264 resp_required = do_bind( clientsb, m, ber, bound );
268 case OLD_LDAP_REQ_UNBIND:
271 case LDAP_REQ_UNBIND_30:
273 case LDAP_REQ_UNBIND:
279 case OLD_LDAP_REQ_ADD:
282 resp_required = do_add( clientsb, m, ber );
286 case OLD_LDAP_REQ_DELETE:
289 case LDAP_REQ_DELETE_30:
291 case LDAP_REQ_DELETE:
292 resp_required = do_delete( clientsb, m, ber );
296 case OLD_LDAP_REQ_MODRDN:
298 case LDAP_REQ_MODRDN:
299 resp_required = do_modrdn( clientsb, m, ber );
303 case OLD_LDAP_REQ_MODIFY:
305 case LDAP_REQ_MODIFY:
306 resp_required = do_modify( clientsb, m, ber );
310 case OLD_LDAP_REQ_COMPARE:
312 case LDAP_REQ_COMPARE:
313 resp_required = do_compare( clientsb, m, ber );
317 case OLD_LDAP_REQ_SEARCH:
319 case LDAP_REQ_SEARCH:
320 resp_required = do_search( clientsb, m, ber );
324 case OLD_LDAP_REQ_ABANDON:
327 case LDAP_REQ_ABANDON_30:
329 case LDAP_REQ_ABANDON:
330 resp_required = do_abandon( m->m_conn, ber, m->m_uniqid );
334 Debug( LDAP_DEBUG_ANY, "unknown operation %d\n", m->m_msgtype,
337 send_ldap_msgresult( clientsb, m->m_msgtype, m,
338 LDAP_PROTOCOL_ERROR, NULL, "Unknown request type" );
342 return( resp_required );
346 * initiate_dap_operation - initiate a dap operation, rebinding and retrying
347 * the request if necessary. If the request is successfully initiated, 0 is
348 * returned. Otherwise, an indication of the error is returned.
352 initiate_dap_operation(
359 int i, rc, bound = 0;
360 struct DAPindication di;
362 Debug( LDAP_DEBUG_TRACE, "initiate_dap_operation\n", 0, 0, 0 );
364 if ( m->m_conn->c_ad == -1 && do_bind_real( m->m_conn, &bound,
365 &matched ) != LDAP_SUCCESS )
366 return( LDAP_UNAVAILABLE );
368 for ( i = 0; i < 2; i++ ) {
371 rc = DapCompare( m->m_conn->c_ad, m->m_uniqid,
372 (struct ds_compare_arg *) arg, &di, ROS_ASYNC );
376 rc = DapSearch( m->m_conn->c_ad, m->m_uniqid,
377 (struct ds_search_arg *) arg, &di, ROS_ASYNC );
381 rc = DapAddEntry( m->m_conn->c_ad, m->m_uniqid,
382 (struct ds_addentry_arg *) arg, &di, ROS_ASYNC );
386 rc = DapRemoveEntry( m->m_conn->c_ad, m->m_uniqid,
387 (struct ds_removeentry_arg *) arg, &di, ROS_ASYNC );
391 rc = DapModifyEntry( m->m_conn->c_ad, m->m_uniqid,
392 (struct ds_modifyentry_arg *) arg, &di, ROS_ASYNC );
396 rc = DapRead( m->m_conn->c_ad, m->m_uniqid,
397 (struct ds_read_arg *) arg, &di, ROS_ASYNC );
401 rc = DapModifyRDN( m->m_conn->c_ad, m->m_uniqid,
402 (struct ds_modifyrdn_arg *) arg, &di, ROS_ASYNC );
409 Debug( LDAP_DEBUG_TRACE, "operation initiated %d\n", rc, 0,
416 * the operation was not invoked - try rebinding, then
420 (void) dap_unbind( m->m_conn->c_ad );
422 if ( do_bind_real( m->m_conn, &bound, &matched )
427 m->m_conn->c_ad = -1;
429 return( LDAP_UNAVAILABLE ); /* DSA was unreachable */
440 int read_pdu /* If non-zero, PDU was read from client. 0 == PDU is being written */
444 PS input_ps = NULLPS;
448 Debug( LDAP_DEBUG_TRACE, "trace_ber(tag=%#x, ber=%#lx, len=%d)\n", tag,
449 (unsigned long) ber, len );
451 if ( (buf = (unsigned char *) malloc( len + 6 )) == NULL ) {
452 fprintf( trace_file, "Unable to allocate memory\n" );
461 SAFEMEMCPY( buf + 6, ber, len );
463 SAFEMEMCPY( buf, ber, len );
465 if ( (input_ps = ps_alloc( str_open )) == NULLPS )
466 fprintf( trace_file, "ps_alloc failed\n" );
467 else if ( str_setup( input_ps, (char *)buf, len + 6, 1 ) != OK )
468 fprintf( trace_file, "str_setup\n" );
469 else if ( (pe = ps2pe( input_ps )) == NULLPE ) {
470 fprintf(trace_file, "ps2pe: %s\n",
471 ps_error( input_ps->ps_errno ) );
472 lber_bprint( (char *) buf, len + 6 );
477 "-", NULLCP, NULLCP, LLOG_PDUS,
478 LLOG_NONE, -1, 0, NOTOK
480 struct type_LDAP_LDAPMessage *ldap_msg = NULL;
482 if ( decode_LDAP_LDAPMessage(pe, 1, 0, NULL, &ldap_msg)
486 "Error decoding LDAPMessage:\n [%s]\n",
488 fprintf( trace_file, "Here is the PDU:\n" );
489 vsetfp( trace_file, NULL );
492 if (log.ll_events & LLOG_PDUS) {
493 pvpdu (&log, print_LDAP_LDAPMessage_P, pe,
495 "<Bad LDAPMessage>" : "<LDAPMessage>",
499 PLOGP(&log, LDAP_LDAPMessage, pe, failed ? "<Bad LDAPMessage>" : "<LDAPMessage>", read_pdu);
502 free_LDAP_LDAPMessage(ldap_msg);
504 vsetfp( trace_file, NULL );