]> git.sur5r.net Git - openldap/blob - libraries/msdos/opendos.c
Suck in global schema dn and operational attributes changes
[openldap] / libraries / msdos / opendos.c
1 /*
2  *  Copyright (c) 1992 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  open-dos.c
6  */
7
8 #ifndef lint 
9 static char copyright[] = "@(#) Copyright (c) 1992 Regents of the University of Michigan.\nAll rights reserved.\n";
10 #endif
11
12 #include "lber.h"
13 #include "ldap.h"
14 #include <stdio.h>
15 #include <string.h>
16 #include <malloc.h>
17 #ifdef PCNFS
18 #include <tklib.h>
19 #include <sys/tk_types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netdb.h>
23 #else /* PCNFS */
24 #include "hostform.h"
25 #include "externs.h"
26 #endif /* PCNFS */
27 #include "msdos.h"
28
29 #ifndef INADDR_LOOPBACK
30 #define INADDR_LOOPBACK ((u_long) 0x7f000001)
31 #endif
32
33 #ifdef LDAP_DEBUG
34 int     ldap_debug;
35 #endif
36
37 #ifndef PCNFS
38 u_long *lookup_addrs( char *host );
39 extern long inet_addr( char *addr );
40 extern struct machinfo *lookup( char *host );
41 #endif /* PCNFS */
42
43 /*
44  * ldap_open - initialize and connect to an ldap server.  A magic cookie to
45  * be used for future communication is returned on success, NULL on failure.
46  *
47  * Example:
48  *      LDAP    *ld;
49  *      ld = ldap_open( hostname, port );
50  */
51
52 #ifdef PCNFS
53 LDAP *ldap_open( host, port )
54     char        *host;
55     int         port;
56 {
57         int                     s;
58         unsigned long           address;
59         struct sockaddr_in      sock;
60         struct hostent          *hp;
61         LDAP                    *ld;
62         char                    *p, hostname[BUFSIZ];
63
64         Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
65
66         if ( host == NULL ) {
67                 fprintf(stderr, "No hostname!\n");
68                 return( NULL );
69         }
70         if ( (hp = gethostbyname( host )) == NULL ) {
71                 perror( "gethostbyname" );
72                 return( NULL );
73         }
74         SAFEMEMCPY( (char *) &address, (char *) hp->h_addr, hp->h_length );
75         strcpy( hostname, hp->h_name );
76         if ( (p = strchr( hostname, '.' )) != NULL )
77                 *p = '\0';
78         if ( port == 0 )
79                 port = LDAP_PORT;
80
81         if ( (s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
82                 tk_perror( "socket" );
83                 return( NULL );
84         }
85
86         memset( (char *)&sock, 0, sizeof(sock));
87         SAFEMEMCPY( &sock.sin_addr, hp->h_addr, hp->h_length );
88         sock.sin_family = hp->h_addrtype;
89         sock.sin_port = htons( (u_short) port);
90
91         if (connect( s, (struct sockaddr *)&sock, sizeof(sock) ) < 0 ) {
92                 tk_perror( "connect" );
93                 return( NULL );
94         }
95
96         if ( (ld = (LDAP *) calloc( sizeof(LDAP), 1 )) == NULL ) {
97                 close( s );
98                 return( NULL );
99         }
100         ld->ld_sb.sb_sd = s;
101         ld->ld_host = strdup( hostname );
102         ld->ld_version = LDAP_VERSION;
103
104         return( ld );
105 }
106 #else /* PCNFS */
107
108 LDAP *ldap_open( host, port )
109 char    *host;
110 int     port;
111 {
112         int                     s, i;
113         unsigned long           tmpaddr[2], *addrs;
114         LDAP                    *ld;
115         char                    *p, hostname[BUFSIZ];
116
117         Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
118
119         ncsainit();
120         tmpaddr[ 1 ] = 0;
121         addrs = tmpaddr;
122         if ( host != NULL ) {
123                 strcpy( hostname, host );
124                 if ( (tmpaddr[0] = inet_addr( host )) == -1 ) {
125                         if (( addrs = lookup_addrs( host )) == NULL ) {
126                                 netshut();
127                                 return( NULL );
128                         }
129                 }
130         } else {
131                 tmpaddr[0] = INADDR_LOOPBACK;
132                 strcpy( hostname, "localhost" );
133         }
134         if ( (p = strchr( hostname, '.' )) != NULL )
135                 *p = '\0';
136         if ( port == 0 )
137                 port = LDAP_PORT;
138
139         for ( i = 0; addrs[ i ] != 0; ++i ) {
140                 if ( (s = ncsaopen( addrs[ i ], port )) >= 0 ) {
141                         break;
142                 }
143         }
144
145         if ( addrs[ i ] == 0 ) {
146                 netshut();
147                 return( NULL );
148         }
149
150         if ( (ld = (LDAP *) calloc( sizeof(LDAP), 1 )) == NULL ) {
151                 netclose( s );
152                 netshut();
153                 return( NULL );
154         }
155         ld->ld_sb.sb_sd = s;
156         ld->ld_host = strdup( hostname );
157         ld->ld_version = LDAP_VERSION;
158
159         return( ld );
160 }
161
162
163 u_long *
164 lookup_addrs( host )
165     char        *host;
166 {
167     struct machinfo     *mr;
168     int                 numaddrs, i;
169     char                *ipp;
170     u_long              *addrs, along;
171
172     if (( mr = lookup( host )) == NULL ) {
173         return( NULL );
174     }
175
176     ipp = mr->hostip;
177 #ifdef NCSADOMAINFIX
178     numaddrs = 0;
179     while ( numaddrs < 4 ) {    /* maximum of 4 addresses */
180         SAFEMEMCPY( (char *)&along, (char *)ipp, sizeof( u_long ));
181         if ( along == 0 ) {
182                 break;
183         }
184         ++numaddrs;
185         ipp += 4;
186     }
187 #else /* NCSADOMAINFIX */
188     numaddrs = 1;
189 #endif /* NCSADOMAINFIX */
190
191     if (( addrs = (u_long *)malloc(( numaddrs + 1 ) * sizeof( u_long )))
192                 == NULL ) {
193         return( NULL );
194     }
195     addrs[ numaddrs ] = 0;
196
197     for ( i = 0, ipp = mr->hostip; i < numaddrs; ++i, ipp += 4 ) {
198         SAFEMEMCPY( (char *)&addrs[ i ], (char *)ipp, sizeof( u_long ));
199     }
200
201     return( addrs );
202 }
203
204 /*
205  * Stand alone inet_addr derived from BSD 4.3 Networking Release 2 by MCS
206  *
207  *  Copyright (c) 1992 Regents of the University of Michigan.
208  *  All rights reserved.
209  *
210  *  inet_addr.c
211  */
212
213 /*
214  * Copyright (c) 1983, 1990 Regents of the University of California.
215  * All rights reserved.
216  *
217  * Redistribution and use in source and binary forms, with or without
218  * modification, are permitted provided that the following conditions
219  * are met:
220  * 1. Redistributions of source code must retain the above copyright
221  *    notice, this list of conditions and the following disclaimer.
222  * 2. Redistributions in binary form must reproduce the above copyright
223  *    notice, this list of conditions and the following disclaimer in the
224  *    documentation and/or other materials provided with the distribution.
225  * 3. All advertising materials mentioning features or use of this software
226  *    must display the following acknowledgement:
227  *      This product includes software developed by the University of
228  *      California, Berkeley and its contributors.
229  * 4. Neither the name of the University nor the names of its contributors
230  *    may be used to endorse or promote products derived from this software
231  *    without specific prior written permission.
232  *
233  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
236  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
237  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
238  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
240  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
242  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
243  * SUCH DAMAGE.
244  */
245
246 #if defined(LIBC_SCCS) && !defined(lint)
247 static char sccsid[] = "@(#)inet_addr.c 5.10 (Berkeley) 2/24/91";
248 #endif /* LIBC_SCCS and not lint */
249
250 #define STANDALONE      1
251
252 #ifdef STANDALONE
253 #define INADDR_NONE     0xffffffff              /* -1 return */
254 #define const
255 int inet_aton( char *cp, u_long *addr);
256
257 #else STANDALONE
258 #include <netinet/in.h>
259 #include <arpa/inet.h>
260 #endif STANDALONE
261
262 #include <ctype.h>
263
264 #ifndef isascii
265 #define isascii(c)      ((unsigned)(c)<=0177)   /* for broken machines */
266 #endif isascii
267
268 /*
269  * Ascii internet address interpretation routine.
270  * The value returned is in network order.
271  */
272 long
273 inet_addr(cp)
274         register const char *cp;
275 {
276 #ifdef STANDALONE
277         u_long val;
278 #else STANDALONE
279         struct in_addr val;
280 #endif STANDALONE
281
282         if (inet_aton(cp, &val))
283 #ifdef STANDALONE
284                 return (val);
285 #else STANDALONE
286                 return (val.s_addr);
287 #endif STANDALONE
288         return (INADDR_NONE);
289 }
290
291 /* 
292  * Check whether "cp" is a valid ascii representation
293  * of an Internet address and convert to a binary address.
294  * Returns 1 if the address is valid, 0 if not.
295  * This replaces inet_addr, the return value from which
296  * cannot distinguish between failure and a local broadcast address.
297  */
298
299 inet_aton(cp, addr)
300         register char *cp;
301 #ifdef STANDALONE
302         u_long *addr;
303 #else STANDALONE
304         struct in_addr *addr;
305 #endif STANDALONE
306 {
307         register u_long val, base, n;
308         register char c;
309         u_long parts[4], *pp = parts;
310
311         for (;;) {
312                 /*
313                  * Collect number up to ``.''.
314                  * Values are specified as for C:
315                  * 0x=hex, 0=octal, other=decimal.
316                  */
317                 val = 0; base = 10;
318                 if (*cp == '0') {
319                         if (*++cp == 'x' || *cp == 'X')
320                                 base = 16, cp++;
321                         else
322                                 base = 8;
323                 }
324                 while ((c = *cp) != '\0') {
325                         if (isascii(c) && isdigit(c)) {
326                                 val = (val * base) + (c - '0');
327                                 cp++;
328                                 continue;
329                         }
330                         if (base == 16 && isascii(c) && isxdigit(c)) {
331                                 val = (val << 4) + 
332                                         (c + 10 - (islower(c) ? 'a' : 'A'));
333                                 cp++;
334                                 continue;
335                         }
336                         break;
337                 }
338                 if (*cp == '.') {
339                         /*
340                          * Internet format:
341                          *      a.b.c.d
342                          *      a.b.c   (with c treated as 16-bits)
343                          *      a.b     (with b treated as 24 bits)
344                          */
345                         if (pp >= parts + 3 || val > 0xff)
346                                 return (0);
347                         *pp++ = val, cp++;
348                 } else
349                         break;
350         }
351         /*
352          * Check for trailing characters.
353          */
354         if (*cp && (!isascii(*cp) || !isspace(*cp)))
355                 return (0);
356         /*
357          * Concoct the address according to
358          * the number of parts specified.
359          */
360         n = pp - parts + 1;
361         switch (n) {
362
363         case 1:                         /* a -- 32 bits */
364                 break;
365
366         case 2:                         /* a.b -- 8.24 bits */
367                 if (val > 0xffffff)
368                         return (0);
369                 val |= parts[0] << 24;
370                 break;
371
372         case 3:                         /* a.b.c -- 8.8.16 bits */
373                 if (val > 0xffff)
374                         return (0);
375                 val |= (parts[0] << 24) | (parts[1] << 16);
376                 break;
377
378         case 4:                         /* a.b.c.d -- 8.8.8.8 bits */
379                 if (val > 0xff)
380                         return (0);
381                 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
382                 break;
383         }
384         if (addr)
385 #ifdef STANDALONE
386                 *addr = htonl(val);
387 #else STANDALONE
388                 addr->s_addr = htonl(val);
389 #endif STANDALONE
390         return (1);
391 }
392
393 #endif /* PCNFS */