2 * Copyright (c) 1995 Regents of the University of Michigan.
5 * request.c - sending of ldap requests; handling of referrals
9 static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
19 #if defined( DOS ) || defined( _WIN32 )
33 #include <sys/types.h>
34 #include <sys/socket.h>
37 #include <sys/select.h>
43 #include "ucx_select.h"
51 #endif /* USE_SYSCONF */
54 #if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
56 static LDAPConn *find_connection( LDAP *ld, LDAPServer *srv, int any );
57 static void use_connection( LDAP *ld, LDAPConn *lc );
58 static void free_servers( LDAPServer *srvlist );
59 #else /* NEEDPROTOS */
60 static LDAPConn *find_connection();
61 static void use_connection();
62 static void free_servers();
63 #endif /* NEEDPROTOS */
64 #endif /* LDAP_REFERRALS || LDAP_DNS */
69 static LDAPServer *dn2servers( LDAP *ld, char *dn );
70 #else /* NEEDPROTOS */
71 static LDAPServer *dn2servers();
72 #endif /* NEEDPROTOS */
77 static BerElement *re_encode_request( LDAP *ld, BerElement *origber,
78 int msgid, char **dnp );
79 #else /* NEEDPROTOS */
80 static BerElement *re_encode_request();
81 #endif /* NEEDPROTOS */
82 #endif /* LDAP_REFERRALS */
86 ldap_alloc_ber_with_options( LDAP *ld )
90 if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULLBER ) {
91 ld->ld_errno = LDAP_NO_MEMORY;
92 #ifdef STR_TRANSLATION
94 ldap_set_ber_options( ld, ber );
95 #endif /* STR_TRANSLATION */
103 ldap_set_ber_options( LDAP *ld, BerElement *ber )
105 ber->ber_options = ld->ld_lberoptions;
106 #ifdef STR_TRANSLATION
107 if (( ld->ld_lberoptions & LBER_TRANSLATE_STRINGS ) != 0 ) {
108 ber_set_string_translators( ber,
109 ld->ld_lber_encode_translate_proc,
110 ld->ld_lber_decode_translate_proc );
112 #endif /* STR_TRANSLATION */
117 ldap_send_initial_request( LDAP *ld, unsigned long msgtype, char *dn,
120 #if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
122 #endif /* LDAP_REFERRALS || LDAP_DNS */
124 Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
126 #if !defined( LDAP_REFERRALS ) && !defined( LDAP_DNS )
127 if ( ber_flush( &ld->ld_sb, ber, 1 ) != 0 ) {
128 ld->ld_errno = LDAP_SERVER_DOWN;
132 ld->ld_errno = LDAP_SUCCESS;
133 return( ld->ld_msgid );
134 #else /* !LDAP_REFERRALS && !LDAP_DNS */
137 if (( ld->ld_options & LDAP_OPT_DNS ) != 0 && ldap_is_dns_dn( dn )) {
138 if (( servers = dn2servers( ld, dn )) == NULL ) {
144 if ( ldap_debug & LDAP_DEBUG_TRACE ) {
147 for ( srv = servers; srv != NULL;
148 srv = srv->lsrv_next ) {
150 "LDAP server %s: dn %s, port %d\n",
151 srv->lsrv_host, ( srv->lsrv_dn == NULL ) ?
152 "(default)" : srv->lsrv_dn,
156 #endif /* LDAP_DEBUG */
158 #endif /* LDAP_DNS */
160 * use of DNS is turned off or this is an X.500 DN...
161 * use our default connection
166 #endif /* LDAP_DNS */
168 return( ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, servers,
170 #endif /* !LDAP_REFERRALS && !LDAP_DNS */
175 #if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
177 ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest
178 *parentreq, LDAPServer *srvlist, LDAPConn *lc, int bind )
183 Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
186 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
189 if ( srvlist == NULL ) {
192 if (( lc = find_connection( ld, srvlist, 1 )) ==
194 if ( bind && (parentreq != NULL) ) {
195 /* Remember the bind in the parent */
197 ++parentreq->lr_outrefcnt;
199 lc = ldap_new_connection( ld, &srvlist, 0, 1, bind );
201 free_servers( srvlist );
205 if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
207 if ( ld->ld_errno == LDAP_SUCCESS ) {
208 ld->ld_errno = LDAP_SERVER_DOWN;
211 /* Forget about the bind */
212 --parentreq->lr_outrefcnt;
217 use_connection( ld, lc );
218 if (( lr = (LDAPRequest *)calloc( 1, sizeof( LDAPRequest ))) ==
220 ld->ld_errno = LDAP_NO_MEMORY;
221 ldap_free_connection( ld, lc, 0, 0 );
224 /* Forget about the bind */
225 --parentreq->lr_outrefcnt;
229 lr->lr_msgid = msgid;
230 lr->lr_status = LDAP_REQST_INPROGRESS;
231 lr->lr_res_errno = LDAP_SUCCESS; /* optimistic */
234 if ( parentreq != NULL ) { /* sub-request */
236 /* Increment if we didn't do it before the bind */
237 ++parentreq->lr_outrefcnt;
239 lr->lr_origid = parentreq->lr_origid;
240 lr->lr_parentcnt = parentreq->lr_parentcnt + 1;
241 lr->lr_parent = parentreq;
242 lr->lr_refnext = parentreq->lr_refnext;
243 parentreq->lr_refnext = lr;
244 } else { /* original request */
245 lr->lr_origid = lr->lr_msgid;
248 if (( lr->lr_next = ld->ld_requests ) != NULL ) {
249 lr->lr_next->lr_prev = lr;
251 ld->ld_requests = lr;
254 if ( ber_flush( lc->lconn_sb, ber, 0 ) != 0 ) {
258 if ( errno == EWOULDBLOCK ) {
259 /* need to continue write later */
260 lr->lr_status = LDAP_REQST_WRITING;
261 ldap_mark_select_write( ld, lc->lconn_sb );
264 ld->ld_errno = LDAP_SERVER_DOWN;
265 ldap_free_request( ld, lr );
266 ldap_free_connection( ld, lc, 0, 0 );
273 if ( parentreq == NULL ) {
274 ber->ber_end = ber->ber_ptr;
275 ber->ber_ptr = ber->ber_buf;
278 /* sent -- waiting for a response */
279 ldap_mark_select_read( ld, lc->lconn_sb );
282 ld->ld_errno = LDAP_SUCCESS;
288 ldap_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb,
289 int connect, int bind )
292 LDAPServer *prevsrv, *srv;
296 * make a new LDAP server connection
297 * XXX open connection synchronously for now
299 if (( lc = (LDAPConn *)calloc( 1, sizeof( LDAPConn ))) == NULL ||
300 ( !use_ldsb && ( sb = (Sockbuf *)calloc( 1, sizeof( Sockbuf )))
305 ld->ld_errno = LDAP_NO_MEMORY;
309 lc->lconn_sb = ( use_ldsb ) ? &ld->ld_sb : sb;
314 for ( srv = *srvlistp; srv != NULL; srv = srv->lsrv_next ) {
315 if ( open_ldap_connection( ld, lc->lconn_sb,
316 srv->lsrv_host, srv->lsrv_port,
317 &lc->lconn_krbinstance, 0 ) != -1 ) {
325 free( (char *)lc->lconn_sb );
328 ld->ld_errno = LDAP_SERVER_DOWN;
332 if ( prevsrv == NULL ) {
333 *srvlistp = srv->lsrv_next;
335 prevsrv->lsrv_next = srv->lsrv_next;
337 lc->lconn_server = srv;
340 lc->lconn_status = LDAP_CONNST_CONNECTED;
341 lc->lconn_next = ld->ld_conns;
345 * XXX for now, we always do a synchronous bind. This will have
346 * to change in the long run...
349 int err, freepasswd, authmethod;
350 char *binddn, *passwd;
351 LDAPConn *savedefconn;
353 freepasswd = err = 0;
355 if ( ld->ld_rebindproc == NULL ) {
356 binddn = passwd = "";
357 authmethod = LDAP_AUTH_SIMPLE;
359 if (( err = (*ld->ld_rebindproc)( ld, &binddn, &passwd,
360 &authmethod, 0 )) == LDAP_SUCCESS ) {
370 savedefconn = ld->ld_defconn;
372 ++lc->lconn_refcnt; /* avoid premature free */
374 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) !=
379 ld->ld_defconn = savedefconn;
383 (*ld->ld_rebindproc)( ld, &binddn, &passwd,
388 ldap_free_connection( ld, lc, 1, 0 );
398 find_connection( LDAP *ld, LDAPServer *srv, int any )
400 * return an existing connection (if any) to the server srv
401 * if "any" is non-zero, check for any server in the "srv" chain
407 for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
408 for ( ls = srv; ls != NULL; ls = ls->lsrv_next ) {
409 if ( lc->lconn_server->lsrv_host != NULL &&
410 ls->lsrv_host != NULL && strcasecmp(
411 ls->lsrv_host, lc->lconn_server->lsrv_host ) == 0
412 && ls->lsrv_port == lc->lconn_server->lsrv_port ) {
427 use_connection( LDAP *ld, LDAPConn *lc )
430 lc->lconn_lastused = time( 0 );
435 ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
437 LDAPConn *tmplc, *prevlc;
439 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection\n", 0, 0, 0 );
441 if ( force || --lc->lconn_refcnt <= 0 ) {
442 if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
443 ldap_mark_select_clear( ld, lc->lconn_sb );
445 ldap_send_unbind( ld, lc->lconn_sb );
447 ldap_close_connection( lc->lconn_sb );
448 if ( lc->lconn_sb->sb_ber.ber_buf != NULL ) {
449 free( lc->lconn_sb->sb_ber.ber_buf );
453 for ( tmplc = ld->ld_conns; tmplc != NULL;
454 tmplc = tmplc->lconn_next ) {
456 if ( prevlc == NULL ) {
457 ld->ld_conns = tmplc->lconn_next;
459 prevlc->lconn_next = tmplc->lconn_next;
465 free_servers( lc->lconn_server );
466 if ( lc->lconn_krbinstance != NULL ) {
467 free( lc->lconn_krbinstance );
469 if ( lc->lconn_sb != &ld->ld_sb ) {
470 free( (char *)lc->lconn_sb );
473 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
476 lc->lconn_lastused = time( 0 );
477 Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
478 lc->lconn_refcnt, 0, 0 );
485 ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
489 fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
490 for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
491 if ( lc->lconn_server != NULL ) {
492 fprintf( stderr, "* host: %s port: %d%s\n",
493 ( lc->lconn_server->lsrv_host == NULL ) ? "(null)"
494 : lc->lconn_server->lsrv_host,
495 lc->lconn_server->lsrv_port, ( lc->lconn_sb ==
496 &ld->ld_sb ) ? " (default)" : "" );
498 fprintf( stderr, " refcnt: %d status: %s\n", lc->lconn_refcnt,
499 ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
500 "NeedSocket" : ( lc->lconn_status ==
501 LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
502 fprintf( stderr, " last used: %s\n",
503 ctime( &lc->lconn_lastused ));
512 ldap_dump_requests_and_responses( LDAP *ld )
517 fprintf( stderr, "** Outstanding Requests:\n" );
518 if (( lr = ld->ld_requests ) == NULL ) {
519 fprintf( stderr, " Empty\n" );
521 for ( ; lr != NULL; lr = lr->lr_next ) {
522 fprintf( stderr, " * msgid %d, origid %d, status %s\n",
523 lr->lr_msgid, lr->lr_origid, ( lr->lr_status ==
524 LDAP_REQST_INPROGRESS ) ? "InProgress" :
525 ( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
526 ( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
528 fprintf( stderr, " outstanding referrals %d, parent count %d\n",
529 lr->lr_outrefcnt, lr->lr_parentcnt );
532 fprintf( stderr, "** Response Queue:\n" );
533 if (( lm = ld->ld_responses ) == NULLMSG ) {
534 fprintf( stderr, " Empty\n" );
536 for ( ; lm != NULLMSG; lm = lm->lm_next ) {
537 fprintf( stderr, " * msgid %d, type %d\n",
538 lm->lm_msgid, lm->lm_msgtype );
539 if (( l = lm->lm_chain ) != NULL ) {
540 fprintf( stderr, " chained responses:\n" );
541 for ( ; l != NULLMSG; l = l->lm_chain ) {
543 " * msgid %d, type %d\n",
544 l->lm_msgid, l->lm_msgtype );
549 #endif /* LDAP_DEBUG */
553 ldap_free_request( LDAP *ld, LDAPRequest *lr )
555 LDAPRequest *tmplr, *nextlr;
557 Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
558 lr->lr_origid, lr->lr_msgid, 0 );
560 if ( lr->lr_parent != NULL ) {
561 --lr->lr_parent->lr_outrefcnt;
563 /* free all referrals (child requests) */
564 for ( tmplr = lr->lr_refnext; tmplr != NULL; tmplr = nextlr ) {
565 nextlr = tmplr->lr_refnext;
566 ldap_free_request( ld, tmplr );
570 if ( lr->lr_prev == NULL ) {
571 ld->ld_requests = lr->lr_next;
573 lr->lr_prev->lr_next = lr->lr_next;
576 if ( lr->lr_next != NULL ) {
577 lr->lr_next->lr_prev = lr->lr_prev;
580 if ( lr->lr_ber != NULL ) {
581 ber_free( lr->lr_ber, 1 );
584 if ( lr->lr_res_error != NULL ) {
585 free( lr->lr_res_error );
588 if ( lr->lr_res_matched != NULL ) {
589 free( lr->lr_res_matched );
597 free_servers( LDAPServer *srvlist )
601 while ( srvlist != NULL ) {
602 nextsrv = srvlist->lsrv_next;
603 if ( srvlist->lsrv_dn != NULL ) {
604 free( srvlist->lsrv_dn );
606 if ( srvlist->lsrv_host != NULL ) {
607 free( srvlist->lsrv_host );
613 #endif /* LDAP_REFERRALS || LDAP_DNS */
616 #ifdef LDAP_REFERRALS
618 * XXX merging of errors in this routine needs to be improved
621 ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
623 int rc, count, len, newdn;
626 #endif /* LDAP_DNS */
627 char *p, *ports, *ref, *tmpref, *refdn, *unfollowed;
628 LDAPRequest *origreq;
632 Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
634 ld->ld_errno = LDAP_SUCCESS; /* optimistic */
637 if ( *errstrp == NULL ) {
641 len = strlen( *errstrp );
642 for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
643 if (( *p == 'R' || *p == 'r' ) && strncasecmp( p,
644 LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
646 p += LDAP_REF_STR_LEN;
651 if ( len < LDAP_REF_STR_LEN ) {
655 if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
656 Debug( LDAP_DEBUG_ANY,
657 "more than %d referral hops (dropping)\n",
658 ld->ld_refhoplimit, 0, 0 );
659 /* XXX report as error in ld->ld_errno? */
663 /* find original request */
664 for ( origreq = lr; origreq->lr_parent != NULL;
665 origreq = origreq->lr_parent ) {
672 /* parse out & follow referrals */
673 for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
676 #endif /* LDAP_DNS */
678 if (( p = strchr( ref, '\n' )) != NULL ) {
685 if ( len > LDAP_LDAP_REF_STR_LEN && strncasecmp( ref,
686 LDAP_LDAP_REF_STR, LDAP_LDAP_REF_STR_LEN ) == 0 ) {
687 Debug( LDAP_DEBUG_TRACE,
688 "chasing LDAP referral: <%s>\n", ref, 0, 0 );
691 #endif /* LDAP_DNS */
692 tmpref = ref + LDAP_LDAP_REF_STR_LEN;
694 } else if ( len > LDAP_DX_REF_STR_LEN && strncasecmp( ref,
695 LDAP_DX_REF_STR, LDAP_DX_REF_STR_LEN ) == 0 ) {
696 Debug( LDAP_DEBUG_TRACE,
697 "chasing DX referral: <%s>\n", ref, 0, 0 );
698 tmpref = ref + LDAP_DX_REF_STR_LEN;
699 #endif /* LDAP_DNS */
701 Debug( LDAP_DEBUG_TRACE,
702 "ignoring unknown referral <%s>\n", ref, 0, 0 );
703 rc = ldap_append_referral( ld, &unfollowed, ref );
709 if (( refdn = strchr( tmpref, '/' )) != NULL ) {
716 if (( ber = re_encode_request( ld, origreq->lr_ber,
717 ++ld->ld_msgid, &refdn )) == NULL ) {
723 #endif /* LDAP_DNS */
724 if (( srv = (LDAPServer *)calloc( 1,
725 sizeof( LDAPServer ))) == NULL ) {
727 ld->ld_errno = LDAP_NO_MEMORY;
731 if (( srv->lsrv_host = strdup( tmpref )) == NULL ) {
734 ld->ld_errno = LDAP_NO_MEMORY;
738 if (( ports = strchr( srv->lsrv_host, ':' )) != NULL ) {
740 srv->lsrv_port = atoi( ports );
742 srv->lsrv_port = LDAP_PORT;
746 srv = dn2servers( ld, tmpref );
748 #endif /* LDAP_DNS */
750 if ( srv != NULL && ldap_send_server_request( ld, ber, ld->ld_msgid,
751 lr, srv, NULL, 1 ) >= 0 ) {
754 Debug( LDAP_DEBUG_ANY,
755 "Unable to chase referral (%s)\n",
756 ldap_err2string( ld->ld_errno ), 0, 0 );
757 rc = ldap_append_referral( ld, &unfollowed, ref );
760 if ( !newdn && refdn != NULL ) {
766 *errstrp = unfollowed;
768 return(( rc == 0 ) ? count : rc );
773 ldap_append_referral( LDAP *ld, char **referralsp, char *s )
777 if ( *referralsp == NULL ) {
779 *referralsp = (char *)malloc( strlen( s ) + LDAP_REF_STR_LEN
783 *referralsp = (char *)realloc( *referralsp,
784 strlen( *referralsp ) + strlen( s ) + 2 );
787 if ( *referralsp == NULL ) {
788 ld->ld_errno = LDAP_NO_MEMORY;
793 strcpy( *referralsp, LDAP_REF_STR );
795 strcat( *referralsp, "\n" );
797 strcat( *referralsp, s );
805 re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp )
808 * XXX this routine knows way too much about how the lber library works!
810 unsigned long along, tag;
813 BerElement tmpber, *ber;
816 Debug( LDAP_DEBUG_TRACE,
817 "re_encode_request: new msgid %d, new dn <%s>\n",
818 msgid, ( *dnp == NULL ) ? "NONE" : *dnp, 0 );
823 * all LDAP requests are sequences that start with a message id,
824 * followed by a sequence that is tagged with the operation code
826 if ( ber_scanf( &tmpber, "{i", &along ) != LDAP_TAG_MSGID ||
827 ( tag = ber_skip_tag( &tmpber, &along )) == LBER_DEFAULT ) {
828 ld->ld_errno = LDAP_DECODING_ERROR;
832 if (( ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
836 /* bind requests have a version number before the DN & other stuff */
837 if ( tag == LDAP_REQ_BIND && ber_get_int( &tmpber, (long *)&ver ) ==
839 ld->ld_errno = LDAP_DECODING_ERROR;
844 /* the rest of the request is the DN followed by other stuff */
845 if ( ber_get_stringa( &tmpber, &orig_dn ) == LBER_DEFAULT ) {
850 if ( *dnp == NULL ) {
856 if ( tag == LDAP_REQ_BIND ) {
857 rc = ber_printf( ber, "{it{is", msgid, tag, ver, *dnp );
859 rc = ber_printf( ber, "{it{s", msgid, tag, *dnp );
867 if ( ber_write( ber, tmpber.ber_ptr, ( tmpber.ber_end -
868 tmpber.ber_ptr ), 0 ) != ( tmpber.ber_end - tmpber.ber_ptr ) ||
869 ber_printf( ber, "}}" ) == -1 ) {
870 ld->ld_errno = LDAP_ENCODING_ERROR;
876 if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
877 Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
881 #endif /* LDAP_DEBUG */
888 ldap_find_request_by_msgid( LDAP *ld, int msgid )
892 for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
893 if ( msgid == lr->lr_msgid ) {
900 #endif /* LDAP_REFERRALS */
905 dn2servers( LDAP *ld, char *dn ) /* dn can also be a domain.... */
907 char *p, *domain, *host, *server_dn, **dxs;
909 LDAPServer *srvlist, *prevsrv, *srv;
911 if (( domain = strrchr( dn, '@' )) != NULL ) {
917 if (( dxs = ldap_getdxbyname( domain )) == NULL ) {
918 ld->ld_errno = LDAP_NO_MEMORY;
924 for ( i = 0; dxs[ i ] != NULL; ++i ) {
927 if ( strchr( dxs[ i ], ':' ) == NULL ) {
929 } else if ( strlen( dxs[ i ] ) >= 7 &&
930 strncmp( dxs[ i ], "ldap://", 7 ) == 0 ) {
932 if (( p = strchr( host, ':' )) == NULL ) {
938 if (( p = strchr( p, '/' )) != NULL ) {
940 if ( *server_dn == '\0' ) {
948 if ( host != NULL ) { /* found a server we can use */
949 if (( srv = (LDAPServer *)calloc( 1,
950 sizeof( LDAPServer ))) == NULL ) {
951 free_servers( srvlist );
953 break; /* exit loop & return */
956 /* add to end of list of servers */
957 if ( srvlist == NULL ) {
960 prevsrv->lsrv_next = srv;
965 if (( srv->lsrv_host = strdup( host )) == NULL ||
966 ( server_dn != NULL && ( srv->lsrv_dn =
967 strdup( server_dn )) == NULL )) {
968 free_servers( srvlist );
970 break; /* exit loop & return */
972 srv->lsrv_port = port;
976 ldap_value_free( dxs );
978 if ( srvlist == NULL ) {
979 ld->ld_errno = LDAP_SERVER_DOWN;
984 #endif /* LDAP_DNS */