]> git.sur5r.net Git - openldap/blob - libraries/libldap/os-ip.c
Add O_EXCL
[openldap] / libraries / libldap / os-ip.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1995 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  os-ip.c -- platform-specific TCP & UDP related code
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/stdlib.h>
18
19 #include <ac/errno.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
22 #include <ac/time.h>
23 #include <ac/unistd.h>
24
25 #ifdef HAVE_IO_H
26 #include <io.h>
27 #endif /* HAVE_IO_H */
28
29 #include "ldap-int.h"
30
31 int ldap_int_tblsize = 0;
32
33 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
34 #  ifdef LDAP_PF_INET6
35 int ldap_int_inet4or6 = AF_UNSPEC;
36 #  else
37 int ldap_int_inet4or6 = AF_INET;
38 #  endif
39 #endif
40
41 /*
42  * nonblock connect code
43  * written by Lars Uffmann, <lars.uffmann@mediaway.net>.
44  *
45  * Copyright 1999, Lars Uffmann, All rights reserved.
46  * This software is not subject to any license of my employer
47  * mediaWays GmbH.
48  *
49  * OpenLDAP COPYING RESTRICTIONS APPLY, see COPYRIGHT file
50  *
51  * Read about the rationale in ldap_connect_timeout: 
52  * ftp://koobera.math.uic.edu/www/docs/connect.html.
53  */
54
55 #define osip_debug(ld,fmt,arg1,arg2,arg3) \
56 do { \
57         ldap_log_printf(NULL, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
58 } while(0)
59
60 static void
61 ldap_pvt_set_errno(int err)
62 {
63         errno = err;
64 }
65
66 int
67 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
68 {
69         struct timeval *new;
70
71         assert( dest != NULL );
72
73         if (src == NULL) {
74                 *dest = NULL;
75                 return 0;
76         }
77
78         new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
79
80         if( new == NULL ) {
81                 *dest = NULL;
82                 return 1;
83         }
84
85         AC_MEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
86
87         *dest = new;
88         return 0;
89 }
90
91 static int
92 ldap_pvt_ndelay_on(LDAP *ld, int fd)
93 {
94         osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
95         return ber_pvt_socket_set_nonblock( fd, 1 );
96 }
97    
98 static int
99 ldap_pvt_ndelay_off(LDAP *ld, int fd)
100 {
101         osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
102         return ber_pvt_socket_set_nonblock( fd, 0 );
103 }
104
105 static ber_socket_t
106 ldap_int_socket(LDAP *ld, int family, int type )
107 {
108         ber_socket_t s = socket(family, type, 0);
109         osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
110         return ( s );
111 }
112
113 static int
114 ldap_pvt_close_socket(LDAP *ld, int s)
115 {
116         osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
117         return tcp_close(s);
118 }
119
120 static int
121 ldap_int_prepare_socket(LDAP *ld, int s, int proto )
122 {
123         osip_debug(ld, "ldap_prepare_socket: %d\n", s,0,0);
124
125 #ifdef TCP_NODELAY
126         if( proto == LDAP_PROTO_TCP ) {
127                 int dummy = 1;
128                 if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
129                         (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
130                 {
131                         osip_debug(ld, "ldap_prepare_socket: "
132                                 "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
133                                 s, 0, 0);
134                 }
135         }
136 #endif
137
138         return 0;
139 }
140
141 #ifndef HAVE_WINSOCK
142
143 #undef TRACE
144 #define TRACE do { \
145         osip_debug(ld, \
146                 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
147                 s, \
148                 errno, \
149                 sock_errstr(errno) ); \
150 } while( 0 )
151
152 /*
153  * check the socket for errors after select returned.
154  */
155 static int
156 ldap_pvt_is_socket_ready(LDAP *ld, int s)
157 {
158         osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
159
160 #if defined( notyet ) /* && defined( SO_ERROR ) */
161 {
162         int so_errno;
163         socklen_t dummy = sizeof(so_errno);
164         if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
165                 == AC_SOCKET_ERROR )
166         {
167                 return -1;
168         }
169         if ( so_errno ) {
170                 ldap_pvt_set_errno(so_errno);
171                 TRACE;
172                 return -1;
173         }
174         return 0;
175 }
176 #else
177 {
178         /* error slippery */
179         struct sockaddr_in sin;
180         char ch;
181         socklen_t dummy = sizeof(sin);
182         if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
183                 == AC_SOCKET_ERROR )
184         {
185                 /* XXX: needs to be replace with ber_stream_read() */
186                 read(s, &ch, 1);
187                 TRACE;
188                 return -1;
189         }
190         return 0;
191 }
192 #endif
193         return -1;
194 }
195 #undef TRACE
196
197 #endif /* HAVE_WINSOCK */
198
199 static int
200 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
201         struct sockaddr *sin, socklen_t addrlen,
202         int async)
203 {
204         int rc;
205         struct timeval  tv, *opt_tv=NULL;
206         fd_set          wfds, *z=NULL;
207 #ifdef HAVE_WINSOCK
208         fd_set          efds;
209 #endif
210
211 #ifdef LDAP_CONNECTIONLESS
212         /* We could do a connect() but that would interfere with
213          * attempts to poll a broadcast address
214          */
215         if (LDAP_IS_UDP(ld)) {
216                 if (ld->ld_options.ldo_peer)
217                         ldap_memfree(ld->ld_options.ldo_peer);
218                 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
219                 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
220                 return ( 0 );
221         }
222 #endif
223         if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
224                 tv.tv_usec = opt_tv->tv_usec;
225                 tv.tv_sec = opt_tv->tv_sec;
226         }
227
228         osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
229                         s, opt_tv ? tv.tv_sec : -1L, async);
230
231         if ( ldap_pvt_ndelay_on(ld, s) == -1 )
232                 return ( -1 );
233
234         if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
235                 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
236                         return ( -1 );
237                 return ( 0 );
238         }
239
240 #ifdef HAVE_WINSOCK
241         ldap_pvt_set_errno( WSAGetLastError() );
242 #endif
243
244         if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
245                 return ( -1 );
246         }
247         
248 #ifdef notyet
249         if ( async ) return ( -2 );
250 #endif
251
252         FD_ZERO(&wfds);
253         FD_SET(s, &wfds );
254
255 #ifdef HAVE_WINSOCK
256         FD_ZERO(&efds);
257         FD_SET(s, &efds );
258 #endif
259
260         do {
261                 rc = select(ldap_int_tblsize, z, &wfds,
262 #ifdef HAVE_WINSOCK
263                         &efds,
264 #else
265                         z,
266 #endif
267                         opt_tv ? &tv : NULL);
268         } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
269                 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
270
271         if( rc == AC_SOCKET_ERROR ) return rc;
272
273 #ifdef HAVE_WINSOCK
274         /* This means the connection failed */
275         if ( FD_ISSET(s, &efds) ) {
276             int so_errno;
277             int dummy = sizeof(so_errno);
278             if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
279                         (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
280             {
281                 /* impossible */
282                 so_errno = WSAGetLastError();
283             }
284             ldap_pvt_set_errno(so_errno);
285             osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
286                        "errno: %d (%s)\n", s, errno, sock_errstr(errno));
287             return -1;
288         }
289 #endif
290         if ( FD_ISSET(s, &wfds) ) {
291 #ifndef HAVE_WINSOCK
292                 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
293                         return ( -1 );
294 #endif
295                 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
296                         return ( -1 );
297                 return ( 0 );
298         }
299         osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
300         ldap_pvt_set_errno( ETIMEDOUT );
301         return ( -1 );
302 }
303
304 #ifndef HAVE_INET_ATON
305 int
306 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
307 {
308         unsigned long u = inet_addr( host );
309         if ( u != 0xffffffff || u != (unsigned long) -1 ) {
310                 in->s_addr = u;
311                 return 1;
312         }
313         return 0;
314 }
315 #endif
316
317
318 int
319 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
320         int proto,
321         const char *host,
322         unsigned long address, int port, int async )
323 {
324         struct sockaddr_in      sin;
325         ber_socket_t            s = AC_SOCKET_INVALID;
326         int                     rc, i, use_hp = 0;
327         struct hostent          *hp = NULL;
328         char                    *ha_buf=NULL, *p, *q;
329         int                     socktype;
330
331         
332         switch(proto) {
333         case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
334                 osip_debug(ld, "ldap_connect_to_host: TCP %s:%d\n",host,port,0);
335                 break;
336         case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
337                 osip_debug(ld, "ldap_connect_to_host: TCP %s:%d\n",host,port,0);
338                 break;
339
340         default:
341                 osip_debug(ld, "ldap_connect_to_host: unknown proto: %d\n",
342                         proto, 0, 0);
343                 return -1;
344         }
345
346         if (host != NULL) {
347 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
348                 char serv[7];
349                 int err;
350                 struct addrinfo hints, *res, *sai;
351
352                 memset( &hints, '\0', sizeof(hints) );
353                 hints.ai_family = ldap_int_inet4or6;
354                 hints.ai_socktype = socktype;
355
356                 snprintf(serv, sizeof serv, "%d", port );
357                 if ( ( err = getaddrinfo(host, serv, &hints, &res) ) ) {
358                         osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
359                                 AC_GAI_STRERROR(err), 0, 0);
360                         return -1;
361                 }
362                 rc = -1;
363
364                 for( sai=res; sai != NULL; sai=sai->ai_next) {
365                         if( sai->ai_addr == NULL ) {
366                                 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
367                                         "ai_addr is NULL?\n", 0, 0, 0);
368                                 continue;
369                         }
370
371                         /* we assume AF_x and PF_x are equal for all x */
372                         s = ldap_int_socket( ld, sai->ai_family, socktype );
373                         if ( s == AC_SOCKET_INVALID ) {
374                                 continue;
375                         }
376
377                         if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
378                                 ldap_pvt_close_socket(ld, s);
379                                 break;
380                         }
381
382                         switch (sai->ai_family) {
383 #ifdef LDAP_PF_INET6
384                         case AF_INET6: {
385                                 char addr[INET6_ADDRSTRLEN];
386                                 inet_ntop( AF_INET6,
387                                         &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
388                                         addr, sizeof addr);
389                                 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n", 
390                                         addr, serv, 0);
391                         } break;
392 #endif
393                         case AF_INET: {
394                                 char addr[INET_ADDRSTRLEN];
395                                 inet_ntop( AF_INET,
396                                         &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
397                                         addr, sizeof addr);
398                                 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n", 
399                                         addr, serv, 0);
400                         } break;
401                         }
402
403                         rc = ldap_pvt_connect(ld, s, sai->ai_addr, sai->ai_addrlen, async);
404                         if ( (rc == 0) || (rc == -2) ) {
405                                 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
406                                 break;
407                         }
408                         ldap_pvt_close_socket(ld, s);
409                 }
410                 freeaddrinfo(res);
411                 return rc;
412
413 #else
414                 struct in_addr in;
415                 if (! inet_aton( host, &in) ) {
416                         int local_h_errno;
417                         struct hostent he_buf;
418                         rc = ldap_pvt_gethostbyname_a(host, &he_buf, &ha_buf,
419                                         &hp, &local_h_errno);
420
421                         if ( (rc < 0) || (hp == NULL) ) {
422 #ifdef HAVE_WINSOCK
423                                 ldap_pvt_set_errno( WSAGetLastError() );
424 #else
425                                 /* not exactly right, but... */
426                                 ldap_pvt_set_errno( EHOSTUNREACH );
427 #endif
428                                 if (ha_buf) LDAP_FREE(ha_buf);
429                                 return -1;
430                         }
431                         use_hp = 1;
432                 }
433                 address = in.s_addr;
434 #endif
435         }
436
437         rc = s = -1;
438         for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
439                 s = ldap_int_socket( ld, PF_INET, socktype );
440                 if ( s == AC_SOCKET_INVALID ) {
441                         /* use_hp ? continue : break; */
442                         break;
443                 }
444            
445                 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
446                         ldap_pvt_close_socket(ld, s);
447                         break;
448                 }
449
450                 (void)memset((char *)&sin, '\0', sizeof(struct sockaddr_in));
451                 sin.sin_family = AF_INET;
452                 sin.sin_port = htons((short) port);
453                 p = (char *)&sin.sin_addr;
454                 q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
455                 AC_MEMCPY(p, q, sizeof(sin.sin_addr) );
456
457                 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n", 
458                         inet_ntoa(sin.sin_addr),port,0);
459
460                 rc = ldap_pvt_connect(ld, s,
461                         (struct sockaddr *)&sin, sizeof(struct sockaddr_in),
462                         async);
463    
464                 if ( (rc == 0) || (rc == -2) ) {
465                         ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
466                         break;
467                 }
468
469                 ldap_pvt_close_socket(ld, s);
470
471                 if (!use_hp)
472                         break;
473         }
474         if (ha_buf) LDAP_FREE(ha_buf);
475         return rc;
476 }
477
478 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
479         defined( HAVE_CYRUS_SASL )
480 char *
481 ldap_host_connected_to( Sockbuf *sb )
482 {
483         struct hostent  *hp;
484         socklen_t               len;
485         struct sockaddr sa;
486         char                    *addr;
487         char                    *host;
488
489         /* buffers for gethostbyaddr_r */
490         struct hostent  he_buf;
491         int                             local_h_errno;
492         char                    *ha_buf=NULL;
493         ber_socket_t    sd;
494
495         (void)memset( (char *)&sa, '\0', sizeof( struct sockaddr ));
496         len = sizeof( sa );
497
498         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
499         if ( getpeername( sd, &sa, &len ) == -1 ) {
500                 return( NULL );
501         }
502
503         /*
504          * do a reverse lookup on the addr to get the official hostname.
505          * this is necessary for kerberos to work right, since the official
506          * hostname is used as the kerberos instance.
507          */
508
509         switch (sa.sa_family) {
510 #ifdef LDAP_PF_LOCAL
511         case AF_LOCAL:
512                 return LDAP_STRDUP( ldap_int_hostname );
513 #endif
514 #ifdef LDAP_PF_INET6
515         case AF_INET6:
516                 addr = (char *) &((struct sockaddr_in6 *)&sa)->sin6_addr;
517                 len = sizeof( struct in6_addr );
518                 break;
519 #endif
520         case AF_INET:
521                 addr = (char *) &((struct sockaddr_in *)&sa)->sin_addr;
522                 len = sizeof( struct in_addr );
523
524                 {
525                         struct sockaddr_in localhost;
526                         localhost.sin_addr.s_addr = htonl( INADDR_ANY );
527
528                         if( memcmp ( &localhost.sin_addr,
529                                 &((struct sockaddr_in *)&sa)->sin_addr,
530                                 sizeof(localhost.sin_addr) ) == 0 )
531                         {
532                                 return LDAP_STRDUP( ldap_int_hostname );
533                         }
534
535 #ifdef INADDR_LOOPBACK
536                         localhost.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
537
538                         if( memcmp ( &localhost.sin_addr,
539                                 &((struct sockaddr_in *)&sa)->sin_addr,
540                                 sizeof(localhost.sin_addr) ) == 0 )
541                         {
542                                 return LDAP_STRDUP( ldap_int_hostname );
543                         }
544 #endif
545                 }
546                 break;
547
548         default:
549                 return( NULL );
550                 break;
551         }
552
553         host = NULL;
554         if ((ldap_pvt_gethostbyaddr_a( addr, len,
555                 sa.sa_family, &he_buf, &ha_buf,
556                 &hp,&local_h_errno ) == 0 ) &&
557                 (hp != NULL) && ( hp->h_name != NULL ) )
558         {
559                 host = LDAP_STRDUP( hp->h_name );   
560         }
561
562         LDAP_FREE( ha_buf );
563         return host;
564 }
565 #endif
566
567
568 /* for UNIX */
569 struct selectinfo {
570         fd_set  si_readfds;
571         fd_set  si_writefds;
572         fd_set  si_use_readfds;
573         fd_set  si_use_writefds;
574 };
575
576
577 void
578 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
579 {
580         struct selectinfo       *sip;
581         ber_socket_t            sd;
582
583         sip = (struct selectinfo *)ld->ld_selectinfo;
584         
585         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
586         if ( !FD_ISSET( sd, &sip->si_writefds )) {
587                 FD_SET( sd, &sip->si_writefds );
588         }
589 }
590
591
592 void
593 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
594 {
595         struct selectinfo       *sip;
596         ber_socket_t            sd;
597
598         sip = (struct selectinfo *)ld->ld_selectinfo;
599
600         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
601         if ( !FD_ISSET( sd, &sip->si_readfds )) {
602                 FD_SET( sd, &sip->si_readfds );
603         }
604 }
605
606
607 void
608 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
609 {
610         struct selectinfo       *sip;
611         ber_socket_t            sd;
612
613         sip = (struct selectinfo *)ld->ld_selectinfo;
614
615         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
616         FD_CLR( sd, &sip->si_writefds );
617         FD_CLR( sd, &sip->si_readfds );
618 }
619
620
621 int
622 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
623 {
624         struct selectinfo       *sip;
625         ber_socket_t            sd;
626
627         sip = (struct selectinfo *)ld->ld_selectinfo;
628
629         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
630         return( FD_ISSET( sd, &sip->si_use_writefds ));
631 }
632
633
634 int
635 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
636 {
637         struct selectinfo       *sip;
638         ber_socket_t            sd;
639
640         sip = (struct selectinfo *)ld->ld_selectinfo;
641
642         ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
643         return( FD_ISSET( sd, &sip->si_use_readfds ));
644 }
645
646
647 void *
648 ldap_new_select_info( void )
649 {
650         struct selectinfo       *sip;
651
652         if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
653             sizeof( struct selectinfo ))) != NULL ) {
654                 FD_ZERO( &sip->si_readfds );
655                 FD_ZERO( &sip->si_writefds );
656         }
657
658         return( (void *)sip );
659 }
660
661
662 void
663 ldap_free_select_info( void *sip )
664 {
665         LDAP_FREE( sip );
666 }
667
668
669 void
670 ldap_int_ip_init( void )
671 {
672         int tblsize;
673 #if defined( HAVE_SYSCONF )
674         tblsize = sysconf( _SC_OPEN_MAX );
675 #elif defined( HAVE_GETDTABLESIZE )
676         tblsize = getdtablesize();
677 #else
678         tblsize = FD_SETSIZE;
679 #endif /* !USE_SYSCONF */
680
681 #ifdef FD_SETSIZE
682         if( tblsize > FD_SETSIZE )
683                 tblsize = FD_SETSIZE;
684 #endif  /* FD_SETSIZE*/
685         ldap_int_tblsize = tblsize;
686 }
687
688
689 int
690 ldap_int_select( LDAP *ld, struct timeval *timeout )
691 {
692         struct selectinfo       *sip;
693
694 #ifdef NEW_LOGGING
695         LDAP_LOG ( CONNECTION, ENTRY, "ldap_int_select\n", 0, 0, 0 );
696 #else
697         Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
698 #endif
699
700         if ( ldap_int_tblsize == 0 )
701                 ldap_int_ip_init();
702
703         sip = (struct selectinfo *)ld->ld_selectinfo;
704         sip->si_use_readfds = sip->si_readfds;
705         sip->si_use_writefds = sip->si_writefds;
706         
707         return( select( ldap_int_tblsize,
708                         &sip->si_use_readfds, &sip->si_use_writefds,
709                         NULL, timeout ));
710 }