2 * Copyright (c) 1995 Regents of the University of Michigan.
11 static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
17 #include <ac/socket.h>
18 #include <ac/string.h>
21 #ifdef HAVE_SYS_PARAM_H
22 #include <sys/param.h>
33 #ifndef INADDR_LOOPBACK
34 #define INADDR_LOOPBACK ((unsigned long) 0x7f000001)
37 #ifndef MAXHOSTNAMELEN
38 #define MAXHOSTNAMELEN 64
43 * ldap_open - initialize and connect to an ldap server. A magic cookie to
44 * be used for future communication is returned on success, NULL on failure.
45 * "host" may be a space-separated list of hosts or IP addresses
49 * ld = ldap_open( hostname, port );
53 ldap_open( char *host, int port )
58 #endif /* LDAP_REFERRALS */
60 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
62 if (( ld = ldap_init( host, port )) == NULL ) {
67 if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
68 NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
69 strdup( ld->ld_defhost )) == NULL )) {
70 ldap_ld_free( ld, 0 );
73 srv->lsrv_port = ld->ld_defport;
75 if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
76 if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
78 ldap_ld_free( ld, 0 );
81 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
83 #else /* LDAP_REFERRALS */
84 if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
85 ld->ld_defport, &ld->ld_host, 0 ) < 0 ) {
86 ldap_ld_free( ld, 0 );
89 #endif /* LDAP_REFERRALS */
91 Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
92 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
99 * ldap_init - initialize the LDAP library. A magic cookie to be used for
100 * future communication is returned on success, NULL on failure.
101 * "defhost" may be a space-separated list of hosts or IP addresses
105 * ld = ldap_open( default_hostname, default_port );
108 ldap_init( char *defhost, int defport )
112 Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
115 if ( (ld = (LDAP *) calloc( 1, sizeof(LDAP) )) == NULL ) {
119 #ifdef LDAP_REFERRALS
120 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
124 ld->ld_options = LDAP_OPT_REFERRALS;
125 #endif /* LDAP_REFERRALS */
127 if ( defhost != NULL &&
128 ( ld->ld_defhost = strdup( defhost )) == NULL ) {
129 #ifdef LDAP_REFERRALS
130 ldap_free_select_info( ld->ld_selectinfo );
131 #endif /* LDAP_REFERRALS */
137 ld->ld_defport = ( defport == 0 ) ? LDAP_PORT : defport;
138 ld->ld_version = LDAP_VERSION;
139 ld->ld_lberoptions = LBER_USE_DER;
140 ld->ld_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
142 #ifdef LDAP_REFERRALS
143 ld->ld_options |= LDAP_OPT_REFERRALS;
144 #endif /* LDAP_REFERRALS */
146 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
147 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
148 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
149 ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
150 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
151 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
158 open_ldap_connection( LDAP *ld, Sockbuf *sb, char *host, int defport,
159 char **krbinstancep, int async )
164 char *curhost, hostname[ 2*MAXHOSTNAMELEN ];
166 Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
168 defport = htons( (short) defport );
170 if ( host != NULL ) {
171 for ( p = host; p != NULL && *p != '\0'; p = q ) {
172 if (( q = strchr( p, ' ' )) != NULL ) {
173 strncpy( hostname, p, q - p );
174 hostname[ q - p ] = '\0';
176 while ( *q == ' ' ) {
180 curhost = p; /* avoid copy if possible */
184 if (( r = strchr( curhost, ':' )) != NULL ) {
185 if ( curhost != hostname ) {
186 strcpy( hostname, curhost ); /* now copy */
187 r = hostname + ( r - curhost );
191 port = htons( (short)atoi( r ));
196 if (( rc = ldap_connect_to_host( sb, curhost, 0L,
197 port, async )) != -1 ) {
202 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
210 if ( krbinstancep != NULL ) {
212 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
213 ( p = strchr( *krbinstancep, '.' )) != NULL ) {
216 #else /* HAVE_KERBEROS */
218 #endif /* HAVE_KERBEROS */