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>
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 extern char *bound_dn, *bound_pw;
73 struct PSAPaddr *psap_cpy();
75 extern int ldap_compat;
78 Debug( LDAP_DEBUG_TRACE, "client_request%s\n",
79 udp ? " udp" : "", 0, 0 );
82 * Get the ldap message, which is a sequence of message id
83 * and then the actual request choice.
87 if ( (tag = ber_get_next( clientsb, &len, &ber )) == LBER_DEFAULT ) {
88 Debug( LDAP_DEBUG_ANY, "ber_get_next failed\n", 0, 0, 0 );
92 #ifdef LDAP_CONNECTIONLESS
93 if ( udp && dosyslog ) {
94 syslog( LOG_INFO, "UDP request from unknown (%s)",
95 inet_ntoa( ((struct sockaddr_in *)
96 clientsb->sb_fromaddr)->sin_addr ));
101 if ( ldap_debug & LDAP_DEBUG_BER )
102 trace_ber( tag, len, ber.ber_buf, stderr, 1, 1 );
107 * This tag should be a normal SEQUENCE tag. In release 2.0 this
108 * tag is 0x10. In the new stuff this is 0x30. To distinguish
109 * between 3.0 and the "correct" stuff, we look for an extra
110 * sequence tag after the bind tag.
113 Debug( LDAP_DEBUG_ANY, "bound %d\n", bound, 0, 0 );
116 if ( tag == OLD_LDAP_TAG_MESSAGE ) {
117 Debug( LDAP_DEBUG_ANY, "version 2.0 detected\n", 0,
120 syslog( LOG_INFO, "old version 2.0 detected" );
129 tber = ber; /* struct copy */
131 ttag = ber_skip_tag( &tber, &tlen );
132 tber.ber_ptr += tlen;
133 /* bind sequence header */
134 ttag = ber_skip_tag( &tber, &tlen );
135 ttag = ber_peek_tag( &tber, &tlen );
137 Debug( LDAP_DEBUG_ANY, "checking for 3.0 tag 0x%x\n",
139 if ( ttag == LBER_SEQUENCE ) {
140 Debug( LDAP_DEBUG_ANY, "version 3.0 detected\n",
144 "old version 3.0 detected" );
152 if ( ber_get_int( &ber, &msgid ) != LDAP_TAG_MSGID ) {
153 send_ldap_result( clientsb, LBER_DEFAULT, msgid,
154 LDAP_PROTOCOL_ERROR, NULL, "Not an LDAP message" );
159 #ifdef LDAP_CONNECTIONLESS
163 ber_get_stringa( &ber, &logdn );
164 if ( logdn != NULL ) {
166 syslog( LOG_INFO, "UDP requestor: %s", logdn );
168 Debug( LDAP_DEBUG_ANY, "UDP requestor: %s\n", logdn, 0, 0 );
172 #endif /* LDAP_CONNECTIONLESS */
175 if ( ldap_compat == 30 )
176 tag = ber_skip_tag( &ber, &len );
179 tag = ber_peek_tag( &ber, &len );
180 if ( !udp && bound == 0 && tag != LDAP_REQ_BIND
182 && tag != OLD_LDAP_REQ_BIND
185 send_ldap_result( clientsb, tag, msgid, LDAP_OPERATIONS_ERROR,
186 NULL, "Bind operation must come first" );
191 #ifdef LDAP_CONNECTIONLESS
192 if (udp && tag != LDAP_REQ_SEARCH && tag != LDAP_REQ_ABANDON ) {
193 send_ldap_result( clientsb, tag, msgid, LDAP_OPERATIONS_ERROR,
194 NULL, "Only search is supported over UDP/CLDAP" );
199 if ( get_cldap_msg( msgid, tag,
200 (struct sockaddr *)clientsb->sb_fromaddr ) != NULL ) {
202 * duplicate request: toss this one
204 Debug( LDAP_DEBUG_TRACE,
205 "client_request tossing dup request id %d from %s\n",
206 msgid, inet_ntoa( ((struct sockaddr_in *)
207 clientsb->sb_fromaddr)->sin_addr ), 0 );
213 copyofber = ber_dup( &ber );
215 m = add_msg( msgid, tag, copyofber, dsaconn, udp,
216 #ifdef LDAP_CONNECTIONLESS
217 (struct sockaddr *)clientsb->sb_fromaddr );
223 * Call the appropriate routine to handle the request. If it
224 * returns a nonzero result, the message requires a response, and
225 * so it's left in the queue of outstanding requests, otherwise
229 if ( do_request( clientsb, m, &ber, &bound ) == 0 ) {
237 * do_request - called when a client makes a request, or when a referral
238 * error is returned. In the latter case, a connection is made to the
239 * referred to DSA, and do_request() is called to retry the operation over
240 * that connection. In the former case, do_request() is called to try
241 * the operation over the default association.
252 int resp_required = 0;
254 Debug( LDAP_DEBUG_TRACE, "do_request\n", 0, 0, 0 );
256 switch ( m->m_msgtype ) {
258 case OLD_LDAP_REQ_BIND:
261 resp_required = do_bind( clientsb, m, ber, bound );
265 case OLD_LDAP_REQ_UNBIND:
268 case LDAP_REQ_UNBIND_30:
270 case LDAP_REQ_UNBIND:
276 case OLD_LDAP_REQ_ADD:
279 resp_required = do_add( clientsb, m, ber );
283 case OLD_LDAP_REQ_DELETE:
286 case LDAP_REQ_DELETE_30:
288 case LDAP_REQ_DELETE:
289 resp_required = do_delete( clientsb, m, ber );
293 case OLD_LDAP_REQ_MODRDN:
295 case LDAP_REQ_MODRDN:
296 resp_required = do_modrdn( clientsb, m, ber );
300 case OLD_LDAP_REQ_MODIFY:
302 case LDAP_REQ_MODIFY:
303 resp_required = do_modify( clientsb, m, ber );
307 case OLD_LDAP_REQ_COMPARE:
309 case LDAP_REQ_COMPARE:
310 resp_required = do_compare( clientsb, m, ber );
314 case OLD_LDAP_REQ_SEARCH:
316 case LDAP_REQ_SEARCH:
317 resp_required = do_search( clientsb, m, ber );
321 case OLD_LDAP_REQ_ABANDON:
324 case LDAP_REQ_ABANDON_30:
326 case LDAP_REQ_ABANDON:
327 resp_required = do_abandon( m->m_conn, ber, m->m_uniqid );
331 Debug( LDAP_DEBUG_ANY, "unknown operation %d\n", m->m_msgtype,
334 send_ldap_msgresult( clientsb, m->m_msgtype, m,
335 LDAP_PROTOCOL_ERROR, NULL, "Unknown request type" );
339 return( resp_required );
343 * initiate_dap_operation - initiate a dap operation, rebinding and retrying
344 * the request if necessary. If the request is successfully initiated, 0 is
345 * returned. Otherwise, an indication of the error is returned.
349 initiate_dap_operation(
356 int i, rc, bound = 0;
357 struct DAPindication di;
359 Debug( LDAP_DEBUG_TRACE, "initiate_dap_operation\n", 0, 0, 0 );
361 if ( m->m_conn->c_ad == -1 && do_bind_real( m->m_conn, &bound,
362 &matched ) != LDAP_SUCCESS )
363 return( LDAP_UNAVAILABLE );
365 for ( i = 0; i < 2; i++ ) {
368 rc = DapCompare( m->m_conn->c_ad, m->m_uniqid,
369 (struct ds_compare_arg *) arg, &di, ROS_ASYNC );
373 rc = DapSearch( m->m_conn->c_ad, m->m_uniqid,
374 (struct ds_search_arg *) arg, &di, ROS_ASYNC );
378 rc = DapAddEntry( m->m_conn->c_ad, m->m_uniqid,
379 (struct ds_addentry_arg *) arg, &di, ROS_ASYNC );
383 rc = DapRemoveEntry( m->m_conn->c_ad, m->m_uniqid,
384 (struct ds_removeentry_arg *) arg, &di, ROS_ASYNC );
388 rc = DapModifyEntry( m->m_conn->c_ad, m->m_uniqid,
389 (struct ds_modifyentry_arg *) arg, &di, ROS_ASYNC );
393 rc = DapRead( m->m_conn->c_ad, m->m_uniqid,
394 (struct ds_read_arg *) arg, &di, ROS_ASYNC );
398 rc = DapModifyRDN( m->m_conn->c_ad, m->m_uniqid,
399 (struct ds_modifyrdn_arg *) arg, &di, ROS_ASYNC );
406 Debug( LDAP_DEBUG_TRACE, "operation initiated %d\n", rc, 0,
413 * the operation was not invoked - try rebinding, then
417 (void) dap_unbind( m->m_conn->c_ad );
419 if ( do_bind_real( m->m_conn, &bound, &matched )
424 m->m_conn->c_ad = -1;
426 return( LDAP_UNAVAILABLE ); /* DSA was unreachable */
437 int read_pdu /* If non-zero, PDU was read from client. 0 == PDU is being written */
441 PS input_ps = NULLPS;
445 Debug( LDAP_DEBUG_TRACE, "trace_ber(tag=%#x, ber=%#lx, len=%d)\n", tag,
446 (unsigned long) ber, len );
448 if ( (buf = (unsigned char *) malloc( len + 6 )) == NULL ) {
449 fprintf( trace_file, "Unable to allocate memory\n" );
458 SAFEMEMCPY( buf + 6, ber, len );
460 SAFEMEMCPY( buf, ber, len );
462 if ( (input_ps = ps_alloc( str_open )) == NULLPS )
463 fprintf( trace_file, "ps_alloc failed\n" );
464 else if ( str_setup( input_ps, (char *)buf, len + 6, 1 ) != OK )
465 fprintf( trace_file, "str_setup\n" );
466 else if ( (pe = ps2pe( input_ps )) == NULLPE ) {
467 fprintf(trace_file, "ps2pe: %s\n",
468 ps_error( input_ps->ps_errno ) );
469 lber_bprint( (char *) buf, len + 6 );
474 "-", NULLCP, NULLCP, LLOG_PDUS,
475 LLOG_NONE, -1, 0, NOTOK
477 struct type_LDAP_LDAPMessage *ldap_msg = NULL;
479 if ( decode_LDAP_LDAPMessage(pe, 1, 0, NULL, &ldap_msg)
483 "Error decoding LDAPMessage:\n [%s]\n",
485 fprintf( trace_file, "Here is the PDU:\n" );
486 vsetfp( trace_file, NULL );
489 if (log.ll_events & LLOG_PDUS) {
490 pvpdu (&log, print_LDAP_LDAPMessage_P, pe,
492 "<Bad LDAPMessage>" : "<LDAPMessage>",
496 PLOGP(&log, LDAP_LDAPMessage, pe, failed ? "<Bad LDAPMessage>" : "<LDAPMessage>", read_pdu);
499 free_LDAP_LDAPMessage(ldap_msg);
501 vsetfp( trace_file, NULL );