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 */
40 #include "LDAP-types.h"
41 #if ISODEPACKAGE == IC
42 #include <compat/logger.h>
49 * client_request - called by do_queries() when there is activity on the
50 * client socket. It expects to be able to get an LDAP message from the
51 * client socket, parses the first couple of fields, and then calls
52 * do_request() to handle the request. If do_request() (or something
53 * called by it) returns a response to the client (e.g., in the case of
54 * an error), then client_request() is done. If the request is not
55 * responded to (and needs a response), it is added to the queue of
56 * outstanding requests. It will be responded to later via dsa_response(),
57 * once the DSA operation completes.
70 BerElement ber, *copyofber;
73 extern char *bound_dn, *bound_pw;
74 struct PSAPaddr *psap_cpy();
76 extern int ldap_compat;
79 Debug( LDAP_DEBUG_TRACE, "client_request%s\n",
80 udp ? " udp" : "", 0, 0 );
83 * Get the ldap message, which is a sequence of message id
84 * and then the actual request choice.
87 ber_init_w_nullc( &ber, 0 );
88 if ( (tag = ber_get_next( clientsb, &len, &ber )) == LBER_DEFAULT ) {
89 Debug( LDAP_DEBUG_ANY, "ber_get_next failed\n", 0, 0, 0 );
93 #ifdef LDAP_CONNECTIONLESS
94 if ( udp && dosyslog ) {
95 syslog( LOG_INFO, "UDP request from unknown (%s)",
96 inet_ntoa( ((struct sockaddr_in *)
97 clientsb->sb_fromaddr)->sin_addr ));
102 if ( ldap_debug & LDAP_DEBUG_BER )
103 trace_ber( tag, len, ber.ber_buf, stderr, 1, 1 );
108 * This tag should be a normal SEQUENCE tag. In release 2.0 this
109 * tag is 0x10. In the new stuff this is 0x30. To distinguish
110 * between 3.0 and the "correct" stuff, we look for an extra
111 * sequence tag after the bind tag.
114 Debug( LDAP_DEBUG_ANY, "bound %d\n", bound, 0, 0 );
117 if ( tag == OLD_LDAP_TAG_MESSAGE ) {
118 Debug( LDAP_DEBUG_ANY, "version 2.0 detected\n", 0,
121 syslog( LOG_INFO, "old version 2.0 detected" );
130 tber = ber; /* struct copy */
132 ttag = ber_skip_tag( &tber, &tlen );
133 tber.ber_ptr += tlen;
134 /* bind sequence header */
135 ttag = ber_skip_tag( &tber, &tlen );
136 ttag = ber_peek_tag( &tber, &tlen );
138 Debug( LDAP_DEBUG_ANY, "checking for 3.0 tag 0x%lx\n",
140 if ( ttag == LBER_SEQUENCE ) {
141 Debug( LDAP_DEBUG_ANY, "version 3.0 detected\n",
145 "old version 3.0 detected" );
153 if ( ber_get_int( &ber, &msgid ) != LDAP_TAG_MSGID ) {
154 send_ldap_result( clientsb, LBER_DEFAULT, msgid,
155 LDAP_PROTOCOL_ERROR, NULL, "Not an LDAP message" );
160 #ifdef LDAP_CONNECTIONLESS
164 ber_get_stringa( &ber, &logdn );
165 if ( logdn != NULL ) {
167 syslog( LOG_INFO, "UDP requestor: %s", logdn );
169 Debug( LDAP_DEBUG_ANY, "UDP requestor: %s\n", logdn, 0, 0 );
173 #endif /* LDAP_CONNECTIONLESS */
176 if ( ldap_compat == 30 )
177 tag = ber_skip_tag( &ber, &len );
180 tag = ber_peek_tag( &ber, &len );
181 if ( !udp && bound == 0 && tag != LDAP_REQ_BIND
183 && tag != OLD_LDAP_REQ_BIND
186 send_ldap_result( clientsb, tag, msgid, LDAP_OPERATIONS_ERROR,
187 NULL, "Bind operation must come first" );
192 #ifdef LDAP_CONNECTIONLESS
193 if (udp && tag != LDAP_REQ_SEARCH && tag != LDAP_REQ_ABANDON ) {
194 send_ldap_result( clientsb, tag, msgid, LDAP_OPERATIONS_ERROR,
195 NULL, "Only search is supported over UDP/CLDAP" );
200 if ( get_cldap_msg( msgid, tag,
201 (struct sockaddr *)clientsb->sb_fromaddr ) != NULL ) {
203 * duplicate request: toss this one
205 Debug( LDAP_DEBUG_TRACE,
206 "client_request tossing dup request id %d from %s\n",
207 msgid, inet_ntoa( ((struct sockaddr_in *)
208 clientsb->sb_fromaddr)->sin_addr ), 0 );
214 copyofber = ber_dup( &ber );
216 m = add_msg( msgid, tag, copyofber, dsaconn, udp,
217 #ifdef LDAP_CONNECTIONLESS
218 (struct sockaddr *)clientsb->sb_fromaddr );
224 * Call the appropriate routine to handle the request. If it
225 * returns a nonzero result, the message requires a response, and
226 * so it's left in the queue of outstanding requests, otherwise
230 if ( do_request( clientsb, m, &ber, &bound ) == 0 ) {
238 * do_request - called when a client makes a request, or when a referral
239 * error is returned. In the latter case, a connection is made to the
240 * referred to DSA, and do_request() is called to retry the operation over
241 * that connection. In the former case, do_request() is called to try
242 * the operation over the default association.
253 int resp_required = 0;
255 Debug( LDAP_DEBUG_TRACE, "do_request\n", 0, 0, 0 );
257 switch ( m->m_msgtype ) {
259 case OLD_LDAP_REQ_BIND:
262 resp_required = do_bind( clientsb, m, ber, bound );
266 case OLD_LDAP_REQ_UNBIND:
269 case LDAP_REQ_UNBIND_30:
271 case LDAP_REQ_UNBIND:
277 case OLD_LDAP_REQ_ADD:
280 resp_required = do_add( clientsb, m, ber );
284 case OLD_LDAP_REQ_DELETE:
287 case LDAP_REQ_DELETE_30:
289 case LDAP_REQ_DELETE:
290 resp_required = do_delete( clientsb, m, ber );
294 case OLD_LDAP_REQ_MODRDN:
296 case LDAP_REQ_MODRDN:
297 resp_required = do_modrdn( clientsb, m, ber );
301 case OLD_LDAP_REQ_MODIFY:
303 case LDAP_REQ_MODIFY:
304 resp_required = do_modify( clientsb, m, ber );
308 case OLD_LDAP_REQ_COMPARE:
310 case LDAP_REQ_COMPARE:
311 resp_required = do_compare( clientsb, m, ber );
315 case OLD_LDAP_REQ_SEARCH:
317 case LDAP_REQ_SEARCH:
318 resp_required = do_search( clientsb, m, ber );
322 case OLD_LDAP_REQ_ABANDON:
325 case LDAP_REQ_ABANDON_30:
327 case LDAP_REQ_ABANDON:
328 resp_required = do_abandon( m->m_conn, ber, m->m_uniqid );
332 Debug( LDAP_DEBUG_ANY, "unknown operation %d\n", m->m_msgtype,
335 send_ldap_msgresult( clientsb, m->m_msgtype, m,
336 LDAP_PROTOCOL_ERROR, NULL, "Unknown request type" );
340 return( resp_required );
344 * initiate_dap_operation - initiate a dap operation, rebinding and retrying
345 * the request if necessary. If the request is successfully initiated, 0 is
346 * returned. Otherwise, an indication of the error is returned.
350 initiate_dap_operation(
357 int i, rc, bound = 0;
358 struct DAPindication di;
360 Debug( LDAP_DEBUG_TRACE, "initiate_dap_operation\n", 0, 0, 0 );
362 if ( m->m_conn->c_ad == -1 && do_bind_real( m->m_conn, &bound,
363 &matched ) != LDAP_SUCCESS )
364 return( LDAP_UNAVAILABLE );
366 for ( i = 0; i < 2; i++ ) {
369 rc = DapCompare( m->m_conn->c_ad, m->m_uniqid,
370 (struct ds_compare_arg *) arg, &di, ROS_ASYNC );
374 rc = DapSearch( m->m_conn->c_ad, m->m_uniqid,
375 (struct ds_search_arg *) arg, &di, ROS_ASYNC );
379 rc = DapAddEntry( m->m_conn->c_ad, m->m_uniqid,
380 (struct ds_addentry_arg *) arg, &di, ROS_ASYNC );
384 rc = DapRemoveEntry( m->m_conn->c_ad, m->m_uniqid,
385 (struct ds_removeentry_arg *) arg, &di, ROS_ASYNC );
389 rc = DapModifyEntry( m->m_conn->c_ad, m->m_uniqid,
390 (struct ds_modifyentry_arg *) arg, &di, ROS_ASYNC );
394 rc = DapRead( m->m_conn->c_ad, m->m_uniqid,
395 (struct ds_read_arg *) arg, &di, ROS_ASYNC );
399 rc = DapModifyRDN( m->m_conn->c_ad, m->m_uniqid,
400 (struct ds_modifyrdn_arg *) arg, &di, ROS_ASYNC );
407 Debug( LDAP_DEBUG_TRACE, "operation initiated %d\n", rc, 0,
414 * the operation was not invoked - try rebinding, then
418 (void) dap_unbind( m->m_conn->c_ad );
420 if ( do_bind_real( m->m_conn, &bound, &matched )
425 m->m_conn->c_ad = -1;
427 return( LDAP_UNAVAILABLE ); /* DSA was unreachable */
438 int read_pdu /* If non-zero, PDU was read from client. 0 == PDU is being written */
442 PS input_ps = NULLPS;
446 Debug( LDAP_DEBUG_TRACE, "trace_ber(tag=%#x, ber=%#lx, len=%d)\n", tag,
447 (unsigned long) ber, len );
449 if ( (buf = (unsigned char *) malloc( len + 6 )) == NULL ) {
450 fprintf( trace_file, "Unable to allocate memory\n" );
459 SAFEMEMCPY( buf + 6, ber, len );
461 SAFEMEMCPY( buf, ber, len );
463 if ( (input_ps = ps_alloc( str_open )) == NULLPS )
464 fprintf( trace_file, "ps_alloc failed\n" );
465 else if ( str_setup( input_ps, (char *)buf, len + 6, 1 ) != OK )
466 fprintf( trace_file, "str_setup\n" );
467 else if ( (pe = ps2pe( input_ps )) == NULLPE ) {
468 fprintf(trace_file, "ps2pe: %s\n",
469 ps_error( input_ps->ps_errno ) );
470 lber_bprint( (char *) buf, len + 6 );
475 "-", NULLCP, NULLCP, LLOG_PDUS,
476 LLOG_NONE, -1, 0, NOTOK
478 struct type_LDAP_LDAPMessage *ldap_msg = NULL;
480 if ( decode_LDAP_LDAPMessage(pe, 1, 0, NULL, &ldap_msg)
484 "Error decoding LDAPMessage:\n [%s]\n",
486 fprintf( trace_file, "Here is the PDU:\n" );
487 vsetfp( trace_file, NULL );
490 if (log.ll_events & LLOG_PDUS) {
491 pvpdu (&log, print_LDAP_LDAPMessage_P, pe,
493 "<Bad LDAPMessage>" : "<LDAPMessage>",
497 PLOGP(&log, LDAP_LDAPMessage, pe, failed ? "<Bad LDAPMessage>" : "<LDAPMessage>", read_pdu);
500 free_LDAP_LDAPMessage(ldap_msg);
502 vsetfp( trace_file, NULL );