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/signal.h>
18 #include <ac/socket.h>
19 #include <ac/string.h>
20 #include <ac/syslog.h>
24 #include <quipu/commonarg.h>
25 #include <quipu/ds_error.h>
26 #include <quipu/dap2.h>
27 #include <quipu/dua.h>
30 #include "../../libraries/liblber/lber-int.h" /* get struct berelement */
38 #include "LDAP-types.h"
39 #if ISODEPACKAGE == IC
40 #include <compat/logger.h>
47 * client_request - called by do_queries() when there is activity on the
48 * client socket. It expects to be able to get an LDAP message from the
49 * client socket, parses the first couple of fields, and then calls
50 * do_request() to handle the request. If do_request() (or something
51 * called by it) returns a response to the client (e.g., in the case of
52 * an error), then client_request() is done. If the request is not
53 * responded to (and needs a response), it is added to the queue of
54 * outstanding requests. It will be responded to later via dsa_response(),
55 * once the DSA operation completes.
68 BerElement ber, *copyofber;
72 Debug( LDAP_DEBUG_TRACE, "client_request%s\n",
73 udp ? " udp" : "", 0, 0 );
76 * Get the ldap message, which is a sequence of message id
77 * and then the actual request choice.
80 ber_init_w_nullc( &ber, 0 );
81 if ( (tag = ber_get_next( clientsb, &len, &ber )) == LBER_DEFAULT ) {
82 Debug( LDAP_DEBUG_ANY, "ber_get_next failed\n", 0, 0, 0 );
86 #ifdef LDAP_CONNECTIONLESS
87 if ( udp && dosyslog ) {
88 syslog( LOG_INFO, "UDP request from unknown (%s)",
89 inet_ntoa( ((struct sockaddr_in *)
90 clientsb->sb_fromaddr)->sin_addr ));
95 if ( ldap_debug & LDAP_DEBUG_BER )
96 trace_ber( tag, len, ber.ber_buf, stderr, 1, 1 );
101 * This tag should be a normal SEQUENCE tag. In release 2.0 this
102 * tag is 0x10. In the new stuff this is 0x30. To distinguish
103 * between 3.0 and the "correct" stuff, we look for an extra
104 * sequence tag after the bind tag.
107 Debug( LDAP_DEBUG_ANY, "bound %d\n", bound, 0, 0 );
110 if ( tag == OLD_LDAP_TAG_MESSAGE ) {
111 Debug( LDAP_DEBUG_ANY, "version 2.0 detected\n", 0,
114 syslog( LOG_INFO, "old version 2.0 detected" );
123 tber = ber; /* struct copy */
125 ttag = ber_skip_tag( &tber, &tlen );
126 tber.ber_ptr += tlen;
127 /* bind sequence header */
128 ttag = ber_skip_tag( &tber, &tlen );
129 ttag = ber_peek_tag( &tber, &tlen );
131 Debug( LDAP_DEBUG_ANY, "checking for 3.0 tag 0x%lx\n",
133 if ( ttag == LBER_SEQUENCE ) {
134 Debug( LDAP_DEBUG_ANY, "version 3.0 detected\n",
138 "old version 3.0 detected" );
146 if ( ber_get_int( &ber, &msgid ) != LDAP_TAG_MSGID ) {
147 send_ldap_result( clientsb, LBER_DEFAULT, msgid,
148 LDAP_PROTOCOL_ERROR, NULL, "Not an LDAP message" );
153 #ifdef LDAP_CONNECTIONLESS
157 ber_get_stringa( &ber, &logdn );
158 if ( logdn != NULL ) {
160 syslog( LOG_INFO, "UDP requestor: %s", logdn );
162 Debug( LDAP_DEBUG_ANY, "UDP requestor: %s\n", logdn, 0, 0 );
166 #endif /* LDAP_CONNECTIONLESS */
169 if ( ldap_compat == 30 )
170 tag = ber_skip_tag( &ber, &len );
173 tag = ber_peek_tag( &ber, &len );
174 if ( !udp && bound == 0 && tag != LDAP_REQ_BIND
176 && tag != OLD_LDAP_REQ_BIND
179 send_ldap_result( clientsb, tag, msgid, LDAP_OPERATIONS_ERROR,
180 NULL, "Bind operation must come first" );
185 #ifdef LDAP_CONNECTIONLESS
186 if (udp && tag != LDAP_REQ_SEARCH && tag != LDAP_REQ_ABANDON ) {
187 send_ldap_result( clientsb, tag, msgid, LDAP_OPERATIONS_ERROR,
188 NULL, "Only search is supported over UDP/CLDAP" );
193 if ( get_cldap_msg( msgid, tag,
194 (struct sockaddr *)clientsb->sb_fromaddr ) != NULL ) {
196 * duplicate request: toss this one
198 Debug( LDAP_DEBUG_TRACE,
199 "client_request tossing dup request id %ld from %s\n",
200 msgid, inet_ntoa( ((struct sockaddr_in *)
201 clientsb->sb_fromaddr)->sin_addr ), 0 );
207 copyofber = ber_dup( &ber );
209 m = add_msg( msgid, tag, copyofber, dsaconn, udp,
210 #ifdef LDAP_CONNECTIONLESS
211 (struct sockaddr *)clientsb->sb_fromaddr );
217 * Call the appropriate routine to handle the request. If it
218 * returns a nonzero result, the message requires a response, and
219 * so it's left in the queue of outstanding requests, otherwise
223 if ( do_request( clientsb, m, &ber, &bound ) == 0 ) {
231 * do_request - called when a client makes a request, or when a referral
232 * error is returned. In the latter case, a connection is made to the
233 * referred to DSA, and do_request() is called to retry the operation over
234 * that connection. In the former case, do_request() is called to try
235 * the operation over the default association.
246 int resp_required = 0;
248 Debug( LDAP_DEBUG_TRACE, "do_request\n", 0, 0, 0 );
250 switch ( m->m_msgtype ) {
252 case OLD_LDAP_REQ_BIND:
255 resp_required = do_bind( clientsb, m, ber, bound );
259 case OLD_LDAP_REQ_UNBIND:
262 case LDAP_REQ_UNBIND_30:
264 case LDAP_REQ_UNBIND:
270 case OLD_LDAP_REQ_ADD:
273 resp_required = do_add( clientsb, m, ber );
277 case OLD_LDAP_REQ_DELETE:
280 case LDAP_REQ_DELETE_30:
282 case LDAP_REQ_DELETE:
283 resp_required = do_delete( clientsb, m, ber );
287 case OLD_LDAP_REQ_MODRDN:
289 case LDAP_REQ_MODRDN:
290 resp_required = do_modrdn( clientsb, m, ber );
294 case OLD_LDAP_REQ_MODIFY:
296 case LDAP_REQ_MODIFY:
297 resp_required = do_modify( clientsb, m, ber );
301 case OLD_LDAP_REQ_COMPARE:
303 case LDAP_REQ_COMPARE:
304 resp_required = do_compare( clientsb, m, ber );
308 case OLD_LDAP_REQ_SEARCH:
310 case LDAP_REQ_SEARCH:
311 resp_required = do_search( clientsb, m, ber );
315 case OLD_LDAP_REQ_ABANDON:
318 case LDAP_REQ_ABANDON_30:
320 case LDAP_REQ_ABANDON:
321 resp_required = do_abandon( m->m_conn, ber, m->m_uniqid );
325 Debug( LDAP_DEBUG_ANY, "unknown operation %d\n", m->m_msgtype,
328 send_ldap_msgresult( clientsb, m->m_msgtype, m,
329 LDAP_PROTOCOL_ERROR, NULL, "Unknown request type" );
333 return( resp_required );
337 * initiate_dap_operation - initiate a dap operation, rebinding and retrying
338 * the request if necessary. If the request is successfully initiated, 0 is
339 * returned. Otherwise, an indication of the error is returned.
343 initiate_dap_operation(
350 int i, rc, bound = 0;
351 struct DAPindication di;
353 Debug( LDAP_DEBUG_TRACE, "initiate_dap_operation\n", 0, 0, 0 );
355 if ( m->m_conn->c_ad == -1 && do_bind_real( m->m_conn, &bound,
356 &matched ) != LDAP_SUCCESS )
357 return( LDAP_UNAVAILABLE );
359 for ( i = 0; i < 2; i++ ) {
362 rc = DapCompare( m->m_conn->c_ad, m->m_uniqid,
363 (struct ds_compare_arg *) arg, &di, ROS_ASYNC );
367 rc = DapSearch( m->m_conn->c_ad, m->m_uniqid,
368 (struct ds_search_arg *) arg, &di, ROS_ASYNC );
372 rc = DapAddEntry( m->m_conn->c_ad, m->m_uniqid,
373 (struct ds_addentry_arg *) arg, &di, ROS_ASYNC );
377 rc = DapRemoveEntry( m->m_conn->c_ad, m->m_uniqid,
378 (struct ds_removeentry_arg *) arg, &di, ROS_ASYNC );
382 rc = DapModifyEntry( m->m_conn->c_ad, m->m_uniqid,
383 (struct ds_modifyentry_arg *) arg, &di, ROS_ASYNC );
387 rc = DapRead( m->m_conn->c_ad, m->m_uniqid,
388 (struct ds_read_arg *) arg, &di, ROS_ASYNC );
392 rc = DapModifyRDN( m->m_conn->c_ad, m->m_uniqid,
393 (struct ds_modifyrdn_arg *) arg, &di, ROS_ASYNC );
400 Debug( LDAP_DEBUG_TRACE, "operation initiated %d\n", rc, 0,
407 * the operation was not invoked - try rebinding, then
411 (void) dap_unbind( m->m_conn->c_ad );
413 if ( do_bind_real( m->m_conn, &bound, &matched )
418 m->m_conn->c_ad = -1;
420 return( LDAP_UNAVAILABLE ); /* DSA was unreachable */
431 int read_pdu /* If non-zero, PDU was read from client. 0 == PDU is being written */
435 PS input_ps = NULLPS;
439 Debug( LDAP_DEBUG_TRACE, "trace_ber(tag=%#x, ber=%#lx, len=%d)\n", tag,
440 (unsigned long) ber, len );
442 if ( (buf = (unsigned char *) malloc( len + 6 )) == NULL ) {
443 fprintf( trace_file, "Unable to allocate memory\n" );
452 SAFEMEMCPY( buf + 6, ber, len );
454 SAFEMEMCPY( buf, ber, len );
456 if ( (input_ps = ps_alloc( str_open )) == NULLPS )
457 fprintf( trace_file, "ps_alloc failed\n" );
458 else if ( str_setup( input_ps, (char *)buf, len + 6, 1 ) != OK )
459 fprintf( trace_file, "str_setup\n" );
460 else if ( (pe = ps2pe( input_ps )) == NULLPE ) {
461 fprintf(trace_file, "ps2pe: %s\n",
462 ps_error( input_ps->ps_errno ) );
463 lber_bprint( (char *) buf, len + 6 );
468 "-", NULLCP, NULLCP, LLOG_PDUS,
469 LLOG_NONE, -1, 0, NOTOK
471 struct type_LDAP_LDAPMessage *ldap_msg = NULL;
473 if ( decode_LDAP_LDAPMessage(pe, 1, 0, NULL, &ldap_msg)
477 "Error decoding LDAPMessage:\n [%s]\n",
479 fprintf( trace_file, "Here is the PDU:\n" );
480 vsetfp( trace_file, NULL );
483 if (log.ll_events & LLOG_PDUS) {
484 pvpdu (&log, print_LDAP_LDAPMessage_P, pe,
486 "<Bad LDAPMessage>" : "<LDAPMessage>",
490 PLOGP(&log, LDAP_LDAPMessage, pe, failed ? "<Bad LDAPMessage>" : "<LDAPMessage>", read_pdu);
493 free_LDAP_LDAPMessage(ldap_msg);
495 vsetfp( trace_file, NULL );