]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
Merged LDAPworldCurrent (P1-10,13,15,16,19-22)
[openldap] / libraries / libldap / open.c
1 /*
2  *  Copyright (c) 1995 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  open.c
6  */
7
8 #ifndef lint 
9 static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
10 #endif
11
12 #include <stdio.h>
13 #include <string.h>
14
15 #ifdef MACOS
16 #include <stdlib.h>
17 #include "macos.h"
18 #endif /* MACOS */
19
20 #if defined( DOS ) || defined( _WIN32 )
21 #include "msdos.h"
22 #include <stdlib.h>
23 #endif /* DOS */
24
25 #if !defined(MACOS) && !defined(DOS) && !defined( _WIN32 )
26 #include <sys/time.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #ifndef VMS
30 #include <sys/param.h>
31 #endif
32 #include <netinet/in.h>
33 #endif
34 #include "lber.h"
35 #include "ldap.h"
36 #include "ldap-int.h"
37
38 #ifdef LDAP_DEBUG
39 int     ldap_debug;
40 #endif
41
42 #ifndef INADDR_LOOPBACK
43 #define INADDR_LOOPBACK ((unsigned long) 0x7f000001)
44 #endif
45
46 #ifndef MAXHOSTNAMELEN
47 #define MAXHOSTNAMELEN  64
48 #endif
49
50
51 /*
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
55  *
56  * Example:
57  *      LDAP    *ld;
58  *      ld = ldap_open( hostname, port );
59  */
60
61 LDAP *
62 ldap_open( char *host, int port )
63 {
64         LDAP            *ld;
65 #ifdef LDAP_REFERRALS
66         LDAPServer      *srv;
67 #endif /* LDAP_REFERRALS */
68
69         Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
70
71         if (( ld = ldap_init( host, port )) == NULL ) {
72                 return( NULL );
73         }
74
75 #ifdef LDAP_REFERRALS
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 );
80                 return( NULL );
81         }
82         srv->lsrv_port = ld->ld_defport;
83
84         if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
85                 if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
86                 free( (char *)srv );
87                 ldap_ld_free( ld, 0 );
88                 return( NULL );
89         }
90         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
91
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 );
96                 return( NULL );
97         }
98 #endif /* LDAP_REFERRALS */
99
100         Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
101                 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
102
103         return( ld );
104 }
105
106
107 /*
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
111  *
112  * Example:
113  *      LDAP    *ld;
114  *      ld = ldap_open( default_hostname, default_port );
115  */
116 LDAP *
117 ldap_init( char *defhost, int defport )
118 {
119         LDAP                    *ld;
120
121         Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
122
123
124         if ( (ld = (LDAP *) calloc( 1, sizeof(LDAP) )) == NULL ) {
125                 return( NULL );
126         }
127
128 #ifdef LDAP_REFERRALS
129         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
130                 free( (char*)ld );
131                 return( NULL );
132         }
133         ld->ld_options = LDAP_OPT_REFERRALS;
134 #endif /* LDAP_REFERRALS */
135
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 */
141                 free( (char*)ld );
142                 return( NULL );
143         }
144
145
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;
150
151 #ifdef LDAP_REFERRALS
152         ld->ld_options |= LDAP_OPT_REFERRALS;
153 #endif /* LDAP_REFERRALS */
154
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 */
161
162         return( ld );
163 }
164
165
166 int
167 open_ldap_connection( LDAP *ld, Sockbuf *sb, char *host, int defport,
168         char **krbinstancep, int async )
169 {
170         int                     rc, port;
171         char                    *p, *q, *r;
172         char                    *curhost, hostname[ 2*MAXHOSTNAMELEN ];
173
174         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
175
176         defport = htons( defport );
177
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';
183                                 curhost = hostname;
184                                 while ( *q == ' ' ) {
185                                     ++q;
186                                 }
187                         } else {
188                                 curhost = p;    /* avoid copy if possible */
189                                 q = NULL;
190                         }
191
192                         if (( r = strchr( curhost, ':' )) != NULL ) {
193                             if ( curhost != hostname ) {
194                                 strcpy( hostname, curhost );    /* now copy */
195                                 r = hostname + ( r - curhost );
196                                 curhost = hostname;
197                             }
198                             *r++ = '\0';
199                             port = htons( (short)atoi( r ));
200                         } else {
201                             port = defport;   
202                         }
203
204                         if (( rc = ldap_connect_to_host( sb, curhost, 0L,
205                             port, async )) != -1 ) {
206                                 break;
207                         }
208                 }
209         } else {
210                 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
211                     defport, async );
212         }
213
214         if ( rc == -1 ) {
215                 return( rc );
216         }
217
218         if ( krbinstancep != NULL ) {
219 #ifdef KERBEROS
220                 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
221                     ( p = strchr( *krbinstancep, '.' )) != NULL ) {
222                         *p = '\0';
223                 }
224 #else /* KERBEROS */
225                 krbinstancep = NULL;
226 #endif /* KERBEROS */
227         }
228
229         return( 0 );
230 }