]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
VC++ Port: round 1
[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 #include "portable.h"
9
10 #ifndef lint 
11 static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
12 #endif
13
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 #include <ac/socket.h>
18 #include <ac/string.h>
19
20 #ifdef HAVE_SYS_PARAM_H
21 #include <sys/param.h>
22 #endif
23
24 #include "lber.h"
25 #include "ldap.h"
26 #include "ldap-int.h"
27
28 #ifdef LDAP_DEBUG
29 int     ldap_debug;
30 #endif
31
32 #ifndef INADDR_LOOPBACK
33 #define INADDR_LOOPBACK ((unsigned long) 0x7f000001)
34 #endif
35
36 #ifndef MAXHOSTNAMELEN
37 #define MAXHOSTNAMELEN  64
38 #endif
39
40
41 /*
42  * ldap_open - initialize and connect to an ldap server.  A magic cookie to
43  * be used for future communication is returned on success, NULL on failure.
44  * "host" may be a space-separated list of hosts or IP addresses
45  *
46  * Example:
47  *      LDAP    *ld;
48  *      ld = ldap_open( hostname, port );
49  */
50
51 LDAP *
52 ldap_open( char *host, int port )
53 {
54         LDAP            *ld;
55 #ifdef LDAP_REFERRALS
56         LDAPServer      *srv;
57 #endif /* LDAP_REFERRALS */
58
59         Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
60
61         if (( ld = ldap_init( host, port )) == NULL ) {
62                 return( NULL );
63         }
64
65 #ifdef LDAP_REFERRALS
66         if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
67             NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
68             strdup( ld->ld_defhost )) == NULL )) {
69                 ldap_ld_free( ld, 0 );
70                 return( NULL );
71         }
72         srv->lsrv_port = ld->ld_defport;
73
74         if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
75                 if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
76                 free( (char *)srv );
77                 ldap_ld_free( ld, 0 );
78                 return( NULL );
79         }
80         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
81
82 #else /* LDAP_REFERRALS */
83         if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
84             ld->ld_defport, &ld->ld_host, 0 ) < 0 ) {
85                 ldap_ld_free( ld, 0 );
86                 return( NULL );
87         }
88 #endif /* LDAP_REFERRALS */
89
90         Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
91                 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
92
93         return( ld );
94 }
95
96
97 /*
98  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
99  * future communication is returned on success, NULL on failure.
100  * "defhost" may be a space-separated list of hosts or IP addresses
101  *
102  * Example:
103  *      LDAP    *ld;
104  *      ld = ldap_open( default_hostname, default_port );
105  */
106 LDAP *
107 ldap_init( char *defhost, int defport )
108 {
109         LDAP                    *ld;
110
111         Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
112
113
114         if ( (ld = (LDAP *) calloc( 1, sizeof(LDAP) )) == NULL ) {
115                 return( NULL );
116         }
117
118 #ifdef LDAP_REFERRALS
119         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
120                 free( (char*)ld );
121                 return( NULL );
122         }
123         ld->ld_options = LDAP_OPT_REFERRALS;
124 #endif /* LDAP_REFERRALS */
125
126         if ( defhost != NULL &&
127             ( ld->ld_defhost = strdup( defhost )) == NULL ) {
128 #ifdef LDAP_REFERRALS
129                 ldap_free_select_info( ld->ld_selectinfo );
130 #endif /* LDAP_REFERRALS */
131                 free( (char*)ld );
132                 return( NULL );
133         }
134
135
136         ld->ld_defport = ( defport == 0 ) ? LDAP_PORT : defport;
137         ld->ld_version = LDAP_VERSION;
138         ld->ld_lberoptions = LBER_USE_DER;
139         ld->ld_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
140
141 #ifdef LDAP_REFERRALS
142         ld->ld_options |= LDAP_OPT_REFERRALS;
143 #endif /* LDAP_REFERRALS */
144
145 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
146         ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
147 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
148         ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
149 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
150 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
151
152         return( ld );
153 }
154
155
156 int
157 open_ldap_connection( LDAP *ld, Sockbuf *sb, char *host, int defport,
158         char **krbinstancep, int async )
159 {
160         int                     rc = -1;
161         int                             port;
162         char                    *p, *q, *r;
163         char                    *curhost, hostname[ 2*MAXHOSTNAMELEN ];
164
165         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
166
167         defport = htons( (short) defport );
168
169         if ( host != NULL ) {
170                 for ( p = host; p != NULL && *p != '\0'; p = q ) {
171                         if (( q = strchr( p, ' ' )) != NULL ) {
172                                 strncpy( hostname, p, q - p );
173                                 hostname[ q - p ] = '\0';
174                                 curhost = hostname;
175                                 while ( *q == ' ' ) {
176                                     ++q;
177                                 }
178                         } else {
179                                 curhost = p;    /* avoid copy if possible */
180                                 q = NULL;
181                         }
182
183                         if (( r = strchr( curhost, ':' )) != NULL ) {
184                             if ( curhost != hostname ) {
185                                 strcpy( hostname, curhost );    /* now copy */
186                                 r = hostname + ( r - curhost );
187                                 curhost = hostname;
188                             }
189                             *r++ = '\0';
190                             port = htons( (short)atoi( r ));
191                         } else {
192                             port = defport;   
193                         }
194
195                         if (( rc = ldap_connect_to_host( sb, curhost, 0L,
196                             port, async )) != -1 ) {
197                                 break;
198                         }
199                 }
200         } else {
201                 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
202                     defport, async );
203         }
204
205         if ( rc == -1 ) {
206                 return( rc );
207         }
208
209         if ( krbinstancep != NULL ) {
210 #ifdef KERBEROS
211                 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
212                     ( p = strchr( *krbinstancep, '.' )) != NULL ) {
213                         *p = '\0';
214                 }
215 #else /* KERBEROS */
216                 krbinstancep = NULL;
217 #endif /* KERBEROS */
218         }
219
220         return( 0 );
221 }