]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
Whoops.
[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 #include <stdlib.h>
15
16 #ifdef MACOS
17 #include "macos.h"
18 #endif /* MACOS */
19
20 #if defined( DOS ) || defined( _WIN32 )
21 #include "msdos.h"
22 #endif /* DOS */
23
24 #if !defined(MACOS) && !defined(DOS) && !defined( _WIN32 )
25 #include <sys/time.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #ifndef VMS
29 #include <sys/param.h>
30 #endif
31 #include <netinet/in.h>
32 #endif
33 #include "lber.h"
34 #include "ldap.h"
35 #include "ldap-int.h"
36
37 #ifdef LDAP_DEBUG
38 int     ldap_debug;
39 #endif
40
41 #ifndef INADDR_LOOPBACK
42 #define INADDR_LOOPBACK ((unsigned long) 0x7f000001)
43 #endif
44
45 #ifndef MAXHOSTNAMELEN
46 #define MAXHOSTNAMELEN  64
47 #endif
48
49
50 /*
51  * ldap_open - initialize and connect to an ldap server.  A magic cookie to
52  * be used for future communication is returned on success, NULL on failure.
53  * "host" may be a space-separated list of hosts or IP addresses
54  *
55  * Example:
56  *      LDAP    *ld;
57  *      ld = ldap_open( hostname, port );
58  */
59
60 LDAP *
61 ldap_open( char *host, int port )
62 {
63         LDAP            *ld;
64 #ifdef LDAP_REFERRALS
65         LDAPServer      *srv;
66 #endif /* LDAP_REFERRALS */
67
68         Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
69
70         if (( ld = ldap_init( host, port )) == NULL ) {
71                 return( NULL );
72         }
73
74 #ifdef LDAP_REFERRALS
75         if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
76             NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
77             strdup( ld->ld_defhost )) == NULL )) {
78                 ldap_ld_free( ld, 0 );
79                 return( NULL );
80         }
81         srv->lsrv_port = ld->ld_defport;
82
83         if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
84                 if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
85                 free( (char *)srv );
86                 ldap_ld_free( ld, 0 );
87                 return( NULL );
88         }
89         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
90
91 #else /* LDAP_REFERRALS */
92         if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
93             ld->ld_defport, &ld->ld_host, 0 ) < 0 ) {
94                 ldap_ld_free( ld, 0 );
95                 return( NULL );
96         }
97 #endif /* LDAP_REFERRALS */
98
99         Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
100                 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
101
102         return( ld );
103 }
104
105
106 /*
107  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
108  * future communication is returned on success, NULL on failure.
109  * "defhost" may be a space-separated list of hosts or IP addresses
110  *
111  * Example:
112  *      LDAP    *ld;
113  *      ld = ldap_open( default_hostname, default_port );
114  */
115 LDAP *
116 ldap_init( char *defhost, int defport )
117 {
118         LDAP                    *ld;
119
120         Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
121
122
123         if ( (ld = (LDAP *) calloc( 1, sizeof(LDAP) )) == NULL ) {
124                 return( NULL );
125         }
126
127 #ifdef LDAP_REFERRALS
128         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
129                 free( (char*)ld );
130                 return( NULL );
131         }
132         ld->ld_options = LDAP_OPT_REFERRALS;
133 #endif /* LDAP_REFERRALS */
134
135         if ( defhost != NULL &&
136             ( ld->ld_defhost = strdup( defhost )) == NULL ) {
137 #ifdef LDAP_REFERRALS
138                 ldap_free_select_info( ld->ld_selectinfo );
139 #endif /* LDAP_REFERRALS */
140                 free( (char*)ld );
141                 return( NULL );
142         }
143
144
145         ld->ld_defport = ( defport == 0 ) ? LDAP_PORT : defport;
146         ld->ld_version = LDAP_VERSION;
147         ld->ld_lberoptions = LBER_USE_DER;
148         ld->ld_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
149
150 #ifdef LDAP_REFERRALS
151         ld->ld_options |= LDAP_OPT_REFERRALS;
152 #endif /* LDAP_REFERRALS */
153
154 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
155         ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
156 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
157         ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
158 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
159 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
160
161         return( ld );
162 }
163
164
165 int
166 open_ldap_connection( LDAP *ld, Sockbuf *sb, char *host, int defport,
167         char **krbinstancep, int async )
168 {
169         int                     rc = -1;
170         int                             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 }