2 * Copyright (c) 1995 Regents of the University of Michigan.
9 static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
20 #if defined( DOS ) || defined( _WIN32 )
25 #if !defined(MACOS) && !defined(DOS) && !defined( _WIN32 )
27 #include <sys/types.h>
28 #include <sys/socket.h>
30 #include <sys/param.h>
32 #include <netinet/in.h>
42 #ifndef INADDR_LOOPBACK
43 #define INADDR_LOOPBACK ((unsigned long) 0x7f000001)
46 #ifndef MAXHOSTNAMELEN
47 #define MAXHOSTNAMELEN 64
52 * ldap_open - initialize and connect to an ldap server. A magic cookie to
53 * be used for future communication is returned on success, NULL on failure.
54 * "host" may be a space-separated list of hosts or IP addresses
58 * ld = ldap_open( hostname, port );
62 ldap_open( char *host, int port )
67 #endif /* LDAP_REFERRALS */
69 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
71 if (( ld = ldap_init( host, port )) == NULL ) {
76 if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
77 NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
78 strdup( ld->ld_defhost )) == NULL )) {
79 ldap_ld_free( ld, 0 );
82 srv->lsrv_port = ld->ld_defport;
84 if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
85 if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
87 ldap_ld_free( ld, 0 );
90 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
92 #else /* LDAP_REFERRALS */
93 if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
94 ld->ld_defport, &ld->ld_host, 0 ) < 0 ) {
95 ldap_ld_free( ld, 0 );
98 #endif /* LDAP_REFERRALS */
100 Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
101 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
108 * ldap_init - initialize the LDAP library. A magic cookie to be used for
109 * future communication is returned on success, NULL on failure.
110 * "defhost" may be a space-separated list of hosts or IP addresses
114 * ld = ldap_open( default_hostname, default_port );
117 ldap_init( char *defhost, int defport )
121 Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
124 if ( (ld = (LDAP *) calloc( 1, sizeof(LDAP) )) == NULL ) {
128 #ifdef LDAP_REFERRALS
129 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
133 ld->ld_options = LDAP_OPT_REFERRALS;
134 #endif /* LDAP_REFERRALS */
136 if ( defhost != NULL &&
137 ( ld->ld_defhost = strdup( defhost )) == NULL ) {
138 #ifdef LDAP_REFERRALS
139 ldap_free_select_info( ld->ld_selectinfo );
140 #endif /* LDAP_REFERRALS */
146 ld->ld_defport = ( defport == 0 ) ? LDAP_PORT : defport;
147 ld->ld_version = LDAP_VERSION;
148 ld->ld_lberoptions = LBER_USE_DER;
149 ld->ld_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
151 #ifdef LDAP_REFERRALS
152 ld->ld_options |= LDAP_OPT_REFERRALS;
153 #endif /* LDAP_REFERRALS */
155 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
156 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
157 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
158 ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
159 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
160 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
167 open_ldap_connection( LDAP *ld, Sockbuf *sb, char *host, int defport,
168 char **krbinstancep, int async )
172 char *curhost, hostname[ 2*MAXHOSTNAMELEN ];
174 Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
176 defport = htons( defport );
178 if ( host != NULL ) {
179 for ( p = host; p != NULL && *p != '\0'; p = q ) {
180 if (( q = strchr( p, ' ' )) != NULL ) {
181 strncpy( hostname, p, q - p );
182 hostname[ q - p ] = '\0';
184 while ( *q == ' ' ) {
188 curhost = p; /* avoid copy if possible */
192 if (( r = strchr( curhost, ':' )) != NULL ) {
193 if ( curhost != hostname ) {
194 strcpy( hostname, curhost ); /* now copy */
195 r = hostname + ( r - curhost );
199 port = htons( (short)atoi( r ));
204 if (( rc = ldap_connect_to_host( sb, curhost, 0L,
205 port, async )) != -1 ) {
210 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
218 if ( krbinstancep != NULL ) {
220 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
221 ( p = strchr( *krbinstancep, '.' )) != NULL ) {
226 #endif /* KERBEROS */