Samba janitor: adding mbp's umask patch :-).
[tprouty/samba.git] / source / lib / util_sock.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Tim Potter      2000-2001
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /* the last IP received from */
25 struct in_addr lastip;
26
27 /* the last port received from */
28 int lastport=0;
29
30 int smb_read_error = 0;
31
32 /****************************************************************************
33  Determine if a file descriptor is in fact a socket.
34 ****************************************************************************/
35
36 BOOL is_a_socket(int fd)
37 {
38         int v,l;
39         l = sizeof(int);
40         return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
41 }
42
43 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
44
45 typedef struct smb_socket_option {
46         const char *name;
47         int level;
48         int option;
49         int value;
50         int opttype;
51 } smb_socket_option;
52
53 static const smb_socket_option socket_options[] = {
54   {"SO_KEEPALIVE",      SOL_SOCKET,    SO_KEEPALIVE,    0,                 OPT_BOOL},
55   {"SO_REUSEADDR",      SOL_SOCKET,    SO_REUSEADDR,    0,                 OPT_BOOL},
56   {"SO_BROADCAST",      SOL_SOCKET,    SO_BROADCAST,    0,                 OPT_BOOL},
57 #ifdef TCP_NODELAY
58   {"TCP_NODELAY",       IPPROTO_TCP,   TCP_NODELAY,     0,                 OPT_BOOL},
59 #endif
60 #ifdef IPTOS_LOWDELAY
61   {"IPTOS_LOWDELAY",    IPPROTO_IP,    IP_TOS,          IPTOS_LOWDELAY,    OPT_ON},
62 #endif
63 #ifdef IPTOS_THROUGHPUT
64   {"IPTOS_THROUGHPUT",  IPPROTO_IP,    IP_TOS,          IPTOS_THROUGHPUT,  OPT_ON},
65 #endif
66 #ifdef SO_REUSEPORT
67   {"SO_REUSEPORT",      SOL_SOCKET,    SO_REUSEPORT,    0,                 OPT_BOOL},
68 #endif
69 #ifdef SO_SNDBUF
70   {"SO_SNDBUF",         SOL_SOCKET,    SO_SNDBUF,       0,                 OPT_INT},
71 #endif
72 #ifdef SO_RCVBUF
73   {"SO_RCVBUF",         SOL_SOCKET,    SO_RCVBUF,       0,                 OPT_INT},
74 #endif
75 #ifdef SO_SNDLOWAT
76   {"SO_SNDLOWAT",       SOL_SOCKET,    SO_SNDLOWAT,     0,                 OPT_INT},
77 #endif
78 #ifdef SO_RCVLOWAT
79   {"SO_RCVLOWAT",       SOL_SOCKET,    SO_RCVLOWAT,     0,                 OPT_INT},
80 #endif
81 #ifdef SO_SNDTIMEO
82   {"SO_SNDTIMEO",       SOL_SOCKET,    SO_SNDTIMEO,     0,                 OPT_INT},
83 #endif
84 #ifdef SO_RCVTIMEO
85   {"SO_RCVTIMEO",       SOL_SOCKET,    SO_RCVTIMEO,     0,                 OPT_INT},
86 #endif
87   {NULL,0,0,0,0}};
88
89 /****************************************************************************
90  Print socket options.
91 ****************************************************************************/
92
93 static void print_socket_options(int s)
94 {
95         int value, vlen = 4;
96         const smb_socket_option *p = &socket_options[0];
97
98         for (; p->name != NULL; p++) {
99                 if (getsockopt(s, p->level, p->option, (void *)&value, &vlen) == -1) {
100                         DEBUG(5,("Could not test socket option %s.\n", p->name));
101                 } else {
102                         DEBUG(5,("socket option %s = %d\n",p->name,value));
103                 }
104         }
105  }
106
107 /****************************************************************************
108  Set user socket options.
109 ****************************************************************************/
110
111 void set_socket_options(int fd, const char *options)
112 {
113         fstring tok;
114
115         while (next_token(&options,tok," \t,", sizeof(tok))) {
116                 int ret=0,i;
117                 int value = 1;
118                 char *p;
119                 BOOL got_value = False;
120
121                 if ((p = strchr_m(tok,'='))) {
122                         *p = 0;
123                         value = atoi(p+1);
124                         got_value = True;
125                 }
126
127                 for (i=0;socket_options[i].name;i++)
128                         if (strequal(socket_options[i].name,tok))
129                                 break;
130
131                 if (!socket_options[i].name) {
132                         DEBUG(0,("Unknown socket option %s\n",tok));
133                         continue;
134                 }
135
136                 switch (socket_options[i].opttype) {
137                 case OPT_BOOL:
138                 case OPT_INT:
139                         ret = setsockopt(fd,socket_options[i].level,
140                                                 socket_options[i].option,(char *)&value,sizeof(int));
141                         break;
142
143                 case OPT_ON:
144                         if (got_value)
145                                 DEBUG(0,("syntax error - %s does not take a value\n",tok));
146
147                         {
148                                 int on = socket_options[i].value;
149                                 ret = setsockopt(fd,socket_options[i].level,
150                                                         socket_options[i].option,(char *)&on,sizeof(int));
151                         }
152                         break;    
153                 }
154       
155                 if (ret != 0)
156                         DEBUG(0,("Failed to set socket option %s (Error %s)\n",tok, strerror(errno) ));
157         }
158
159         print_socket_options(fd);
160 }
161
162 /****************************************************************************
163  Read from a socket.
164 ****************************************************************************/
165
166 ssize_t read_udp_socket(int fd,char *buf,size_t len)
167 {
168         ssize_t ret;
169         struct sockaddr_in sock;
170         socklen_t socklen = sizeof(sock);
171
172         memset((char *)&sock,'\0',socklen);
173         memset((char *)&lastip,'\0',sizeof(lastip));
174         ret = (ssize_t)sys_recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
175         if (ret <= 0) {
176                 DEBUG(2,("read socket failed. ERRNO=%s\n",strerror(errno)));
177                 return(0);
178         }
179
180         lastip = sock.sin_addr;
181         lastport = ntohs(sock.sin_port);
182
183         DEBUG(10,("read_udp_socket: lastip %s lastport %d read: %d\n",
184                         inet_ntoa(lastip), lastport, ret));
185
186         return(ret);
187 }
188
189 /****************************************************************************
190  Read data from a socket with a timout in msec.
191  mincount = if timeout, minimum to read before returning
192  maxcount = number to be read.
193  time_out = timeout in milliseconds
194 ****************************************************************************/
195
196 ssize_t read_socket_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
197 {
198         fd_set fds;
199         int selrtn;
200         ssize_t readret;
201         size_t nread = 0;
202         struct timeval timeout;
203         
204         /* just checking .... */
205         if (maxcnt <= 0)
206                 return(0);
207         
208         smb_read_error = 0;
209         
210         /* Blocking read */
211         if (time_out <= 0) {
212                 if (mincnt == 0) mincnt = maxcnt;
213                 
214                 while (nread < mincnt) {
215                         readret = sys_read(fd, buf + nread, maxcnt - nread);
216                         
217                         if (readret == 0) {
218                                 DEBUG(5,("read_socket_with_timeout: blocking read. EOF from client.\n"));
219                                 smb_read_error = READ_EOF;
220                                 return -1;
221                         }
222                         
223                         if (readret == -1) {
224                                 DEBUG(0,("read_socket_with_timeout: read error = %s.\n", strerror(errno) ));
225                                 smb_read_error = READ_ERROR;
226                                 return -1;
227                         }
228                         nread += readret;
229                 }
230                 return((ssize_t)nread);
231         }
232         
233         /* Most difficult - timeout read */
234         /* If this is ever called on a disk file and 
235            mincnt is greater then the filesize then
236            system performance will suffer severely as 
237            select always returns true on disk files */
238         
239         /* Set initial timeout */
240         timeout.tv_sec = (time_t)(time_out / 1000);
241         timeout.tv_usec = (long)(1000 * (time_out % 1000));
242         
243         for (nread=0; nread < mincnt; ) {      
244                 FD_ZERO(&fds);
245                 FD_SET(fd,&fds);
246                 
247                 selrtn = sys_select_intr(fd+1,&fds,NULL,NULL,&timeout);
248                 
249                 /* Check if error */
250                 if (selrtn == -1) {
251                         /* something is wrong. Maybe the socket is dead? */
252                         DEBUG(0,("read_socket_with_timeout: timeout read. select error = %s.\n", strerror(errno) ));
253                         smb_read_error = READ_ERROR;
254                         return -1;
255                 }
256                 
257                 /* Did we timeout ? */
258                 if (selrtn == 0) {
259                         DEBUG(10,("read_socket_with_timeout: timeout read. select timed out.\n"));
260                         smb_read_error = READ_TIMEOUT;
261                         return -1;
262                 }
263                 
264                 readret = sys_read(fd, buf+nread, maxcnt-nread);
265                 
266                 if (readret == 0) {
267                         /* we got EOF on the file descriptor */
268                         DEBUG(5,("read_socket_with_timeout: timeout read. EOF from client.\n"));
269                         smb_read_error = READ_EOF;
270                         return -1;
271                 }
272                 
273                 if (readret == -1) {
274                         /* the descriptor is probably dead */
275                         DEBUG(0,("read_socket_with_timeout: timeout read. read error = %s.\n", strerror(errno) ));
276                         smb_read_error = READ_ERROR;
277                         return -1;
278                 }
279                 
280                 nread += readret;
281         }
282         
283         /* Return the number we got */
284         return (ssize_t)nread;
285 }
286
287 /****************************************************************************
288   read data from the client, reading exactly N bytes. 
289 ****************************************************************************/
290
291 ssize_t read_data(int fd,char *buffer,size_t N)
292 {
293         ssize_t ret;
294         size_t total=0;  
295  
296         smb_read_error = 0;
297
298         while (total < N) {
299                 ret = sys_read(fd,buffer + total,N - total);
300
301                 if (ret == 0) {
302                         DEBUG(10,("read_data: read of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) ));
303                         smb_read_error = READ_EOF;
304                         return 0;
305                 }
306
307                 if (ret == -1) {
308                         DEBUG(0,("read_data: read failure for %d. Error = %s\n", (int)(N - total), strerror(errno) ));
309                         smb_read_error = READ_ERROR;
310                         return -1;
311                 }
312                 total += ret;
313         }
314         return (ssize_t)total;
315 }
316
317 /****************************************************************************
318  Read data from a socket, reading exactly N bytes. 
319 ****************************************************************************/
320
321 static ssize_t read_socket_data(int fd,char *buffer,size_t N)
322 {
323         ssize_t ret;
324         size_t total=0;  
325  
326         smb_read_error = 0;
327
328         while (total < N) {
329                 ret = sys_read(fd,buffer + total,N - total);
330
331                 if (ret == 0) {
332                         DEBUG(10,("read_socket_data: recv of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) ));
333                         smb_read_error = READ_EOF;
334                         return 0;
335                 }
336
337                 if (ret == -1) {
338                         DEBUG(0,("read_socket_data: recv failure for %d. Error = %s\n", (int)(N - total), strerror(errno) ));
339                         smb_read_error = READ_ERROR;
340                         return -1;
341                 }
342                 total += ret;
343         }
344         return (ssize_t)total;
345 }
346
347 /****************************************************************************
348  Write data to a fd.
349 ****************************************************************************/
350
351 ssize_t write_data(int fd,char *buffer,size_t N)
352 {
353         size_t total=0;
354         ssize_t ret;
355
356         while (total < N) {
357                 ret = sys_write(fd,buffer + total,N - total);
358
359                 if (ret == -1) {
360                         DEBUG(0,("write_data: write failure. Error = %s\n", strerror(errno) ));
361                         return -1;
362                 }
363                 if (ret == 0)
364                         return total;
365
366                 total += ret;
367         }
368         return (ssize_t)total;
369 }
370
371 /****************************************************************************
372  Write data to a socket - use send rather than write.
373 ****************************************************************************/
374
375 static ssize_t write_socket_data(int fd,char *buffer,size_t N)
376 {
377         size_t total=0;
378         ssize_t ret;
379
380         while (total < N) {
381                 ret = sys_send(fd,buffer + total,N - total,0);
382
383                 if (ret == -1) {
384                         DEBUG(0,("write_socket_data: write failure. Error = %s\n", strerror(errno) ));
385                         return -1;
386                 }
387                 if (ret == 0)
388                         return total;
389
390                 total += ret;
391         }
392         return (ssize_t)total;
393 }
394
395 /****************************************************************************
396 write to a socket
397 ****************************************************************************/
398
399 ssize_t write_socket(int fd,char *buf,size_t len)
400 {
401         ssize_t ret=0;
402
403         DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
404         ret = write_socket_data(fd,buf,len);
405       
406         DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
407         if(ret <= 0)
408                 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n", 
409                         (int)len, fd, strerror(errno) ));
410
411         return(ret);
412 }
413
414 /****************************************************************************
415 send a keepalive packet (rfc1002)
416 ****************************************************************************/
417
418 BOOL send_keepalive(int client)
419 {
420         unsigned char buf[4];
421
422         buf[0] = SMBkeepalive;
423         buf[1] = buf[2] = buf[3] = 0;
424
425         return(write_socket_data(client,(char *)buf,4) == 4);
426 }
427
428
429 /****************************************************************************
430 read 4 bytes of a smb packet and return the smb length of the packet
431 store the result in the buffer
432 This version of the function will return a length of zero on receiving
433 a keepalive packet.
434 timeout is in milliseconds.
435 ****************************************************************************/
436
437 static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int timeout)
438 {
439         ssize_t len=0;
440         int msg_type;
441         BOOL ok = False;
442
443         while (!ok) {
444                 if (timeout > 0)
445                         ok = (read_socket_with_timeout(fd,inbuf,4,4,timeout) == 4);
446                 else 
447                         ok = (read_socket_data(fd,inbuf,4) == 4);
448
449                 if (!ok)
450                         return(-1);
451
452                 len = smb_len(inbuf);
453                 msg_type = CVAL(inbuf,0);
454
455                 if (msg_type == SMBkeepalive) 
456                         DEBUG(5,("Got keepalive packet\n"));
457         }
458
459         DEBUG(10,("got smb length of %d\n",len));
460
461         return(len);
462 }
463
464 /****************************************************************************
465 read 4 bytes of a smb packet and return the smb length of the packet
466 store the result in the buffer. This version of the function will
467 never return a session keepalive (length of zero).
468 timeout is in milliseconds.
469 ****************************************************************************/
470
471 ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
472 {
473         ssize_t len;
474
475         for(;;) {
476                 len = read_smb_length_return_keepalive(fd, inbuf, timeout);
477
478                 if(len < 0)
479                         return len;
480
481                 /* Ignore session keepalives. */
482                 if(CVAL(inbuf,0) != SMBkeepalive)
483                         break;
484         }
485
486         DEBUG(10,("read_smb_length: got smb length of %d\n",len));
487
488         return len;
489 }
490
491 /****************************************************************************
492   read an smb from a fd. Note that the buffer *MUST* be of size
493   BUFFER_SIZE+SAFETY_MARGIN.
494   The timeout is in milliseconds. 
495   This function will return on a
496   receipt of a session keepalive packet.
497 ****************************************************************************/
498
499 BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
500 {
501         ssize_t len,ret;
502
503         smb_read_error = 0;
504
505         memset(buffer,'\0',smb_size + 100);
506
507         len = read_smb_length_return_keepalive(fd,buffer,timeout);
508         if (len < 0) {
509                 DEBUG(10,("receive_smb: length < 0!\n"));
510
511                 /*
512                  * Correct fix. smb_read_error may have already been
513                  * set. Only set it here if not already set. Global
514                  * variables still suck :-). JRA.
515                  */
516
517                 if (smb_read_error == 0)
518                         smb_read_error = READ_ERROR;
519                 return False;
520         }
521
522         /*
523          * A WRITEX with CAP_LARGE_WRITEX can be 64k worth of data plus 65 bytes
524          * of header. Don't print the error if this fits.... JRA.
525          */
526
527         if (len > (BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE)) {
528                 DEBUG(0,("Invalid packet length! (%d bytes).\n",len));
529                 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2)) {
530
531                         /*
532                          * Correct fix. smb_read_error may have already been
533                          * set. Only set it here if not already set. Global
534                          * variables still suck :-). JRA.
535                          */
536
537                         if (smb_read_error == 0)
538                                 smb_read_error = READ_ERROR;
539                         return False;
540                 }
541         }
542
543         if(len > 0) {
544                 ret = read_socket_data(fd,buffer+4,len);
545                 if (ret != len) {
546                         if (smb_read_error == 0)
547                                 smb_read_error = READ_ERROR;
548                         return False;
549                 }
550         }
551
552         return(True);
553 }
554
555 /****************************************************************************
556   send an smb to a fd 
557 ****************************************************************************/
558
559 BOOL send_smb(int fd,char *buffer)
560 {
561         size_t len;
562         size_t nwritten=0;
563         ssize_t ret;
564         len = smb_len(buffer) + 4;
565
566         while (nwritten < len) {
567                 ret = write_socket(fd,buffer+nwritten,len - nwritten);
568                 if (ret <= 0) {
569                         DEBUG(0,("Error writing %d bytes to client. %d. (%s)\n",
570                                 (int)len,(int)ret, strerror(errno) ));
571                         return False;
572                 }
573                 nwritten += ret;
574         }
575
576         return True;
577 }
578
579 /****************************************************************************
580  Open a socket of the specified type, port, and address for incoming data.
581 ****************************************************************************/
582
583 int open_socket_in( int type, int port, int dlevel, uint32 socket_addr, BOOL rebind )
584 {
585         struct sockaddr_in sock;
586         int res;
587
588         memset( (char *)&sock, '\0', sizeof(sock) );
589
590 #ifdef HAVE_SOCK_SIN_LEN
591         sock.sin_len         = sizeof(sock);
592 #endif
593         sock.sin_port        = htons( port );
594         sock.sin_family      = AF_INET;
595         sock.sin_addr.s_addr = socket_addr;
596
597         res = socket( AF_INET, type, 0 );
598         if( res == -1 ) {
599                 if( DEBUGLVL(0) ) {
600                         dbgtext( "open_socket_in(): socket() call failed: " );
601                         dbgtext( "%s\n", strerror( errno ) );
602                 }
603                 return -1;
604         }
605
606         /* This block sets/clears the SO_REUSEADDR and possibly SO_REUSEPORT. */
607         {
608                 int val = rebind ? 1 : 0;
609                 if( setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val)) == -1 ) {
610                         if( DEBUGLVL( dlevel ) ) {
611                                 dbgtext( "open_socket_in(): setsockopt: " );
612                                 dbgtext( "SO_REUSEADDR = %s ", val?"True":"False" );
613                                 dbgtext( "on port %d failed ", port );
614                                 dbgtext( "with error = %s\n", strerror(errno) );
615                         }
616                 }
617 #ifdef SO_REUSEPORT
618                 if( setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val)) == -1 ) {
619                         if( DEBUGLVL( dlevel ) ) {
620                                 dbgtext( "open_socket_in(): setsockopt: ");
621                                 dbgtext( "SO_REUSEPORT = %s ", val?"True":"False" );
622                                 dbgtext( "on port %d failed ", port );
623                                 dbgtext( "with error = %s\n", strerror(errno) );
624                         }
625                 }
626 #endif /* SO_REUSEPORT */
627         }
628
629         /* now we've got a socket - we need to bind it */
630         if( bind( res, (struct sockaddr *)&sock, sizeof(sock) ) == -1 ) {
631                 if( DEBUGLVL(dlevel) && (port == SMB_PORT1 || port == SMB_PORT2 || port == NMB_PORT) ) {
632                         dbgtext( "bind failed on port %d ", port );
633                         dbgtext( "socket_addr = %s.\n", inet_ntoa( sock.sin_addr ) );
634                         dbgtext( "Error = %s\n", strerror(errno) );
635                 }
636                 close( res ); 
637                 return( -1 ); 
638         }
639
640         DEBUG( 10, ( "bind succeeded on port %d\n", port ) );
641
642         return( res );
643  }
644
645 /****************************************************************************
646   create an outgoing socket. timeout is in milliseconds.
647   **************************************************************************/
648
649 int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
650 {
651   struct sockaddr_in sock_out;
652   int res,ret;
653   int connect_loop = 250; /* 250 milliseconds */
654   int loops = (timeout) / connect_loop;
655
656   /* create a socket to write to */
657   res = socket(PF_INET, type, 0);
658   if (res == -1) 
659     { DEBUG(0,("socket error\n")); return -1; }
660
661   if (type != SOCK_STREAM) return(res);
662   
663   memset((char *)&sock_out,'\0',sizeof(sock_out));
664   putip((char *)&sock_out.sin_addr,(char *)addr);
665   
666   sock_out.sin_port = htons( port );
667   sock_out.sin_family = PF_INET;
668
669   /* set it non-blocking */
670   set_blocking(res,False);
671
672   DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
673   
674   /* and connect it to the destination */
675 connect_again:
676   ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
677
678   /* Some systems return EAGAIN when they mean EINPROGRESS */
679   if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
680         errno == EAGAIN) && loops--) {
681     msleep(connect_loop);
682     goto connect_again;
683   }
684
685   if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
686          errno == EAGAIN)) {
687       DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
688       close(res);
689       return -1;
690   }
691
692 #ifdef EISCONN
693   if (ret < 0 && errno == EISCONN) {
694     errno = 0;
695     ret = 0;
696   }
697 #endif
698
699   if (ret < 0) {
700     DEBUG(2,("error connecting to %s:%d (%s)\n",
701              inet_ntoa(*addr),port,strerror(errno)));
702     close(res);
703     return -1;
704   }
705
706   /* set it blocking again */
707   set_blocking(res,True);
708
709   return res;
710 }
711
712 /*
713   open a connected UDP socket to host on port
714 */
715 int open_udp_socket(const char *host, int port)
716 {
717         int type = SOCK_DGRAM;
718         struct sockaddr_in sock_out;
719         int res;
720         struct in_addr *addr;
721
722         addr = interpret_addr2(host);
723
724         res = socket(PF_INET, type, 0);
725         if (res == -1) {
726                 return -1;
727         }
728
729         memset((char *)&sock_out,'\0',sizeof(sock_out));
730         putip((char *)&sock_out.sin_addr,(char *)addr);
731         sock_out.sin_port = htons(port);
732         sock_out.sin_family = PF_INET;
733
734         if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) {
735                 close(res);
736                 return -1;
737         }
738
739         return res;
740 }
741
742
743 /* the following 3 client_*() functions are nasty ways of allowing
744    some generic functions to get info that really should be hidden in
745    particular modules */
746 static int client_fd = -1;
747
748 void client_setfd(int fd)
749 {
750         client_fd = fd;
751 }
752
753 char *client_name(void)
754 {
755         return get_socket_name(client_fd,False);
756 }
757
758 char *client_addr(void)
759 {
760         return get_socket_addr(client_fd);
761 }
762
763 /*******************************************************************
764  matchname - determine if host name matches IP address. Used to
765  confirm a hostname lookup to prevent spoof attacks
766  ******************************************************************/
767 static BOOL matchname(char *remotehost,struct in_addr  addr)
768 {
769         struct hostent *hp;
770         int     i;
771         
772         if ((hp = sys_gethostbyname(remotehost)) == 0) {
773                 DEBUG(0,("sys_gethostbyname(%s): lookup failure.\n", remotehost));
774                 return False;
775         } 
776
777         /*
778          * Make sure that gethostbyname() returns the "correct" host name.
779          * Unfortunately, gethostbyname("localhost") sometimes yields
780          * "localhost.domain". Since the latter host name comes from the
781          * local DNS, we just have to trust it (all bets are off if the local
782          * DNS is perverted). We always check the address list, though.
783          */
784         
785         if (strcasecmp(remotehost, hp->h_name)
786             && strcasecmp(remotehost, "localhost")) {
787                 DEBUG(0,("host name/name mismatch: %s != %s\n",
788                          remotehost, hp->h_name));
789                 return False;
790         }
791         
792         /* Look up the host address in the address list we just got. */
793         for (i = 0; hp->h_addr_list[i]; i++) {
794                 if (memcmp(hp->h_addr_list[i], (char *) & addr, sizeof(addr)) == 0)
795                         return True;
796         }
797         
798         /*
799          * The host name does not map to the original host address. Perhaps
800          * someone has compromised a name server. More likely someone botched
801          * it, but that could be dangerous, too.
802          */
803         
804         DEBUG(0,("host name/address mismatch: %s != %s\n",
805                  inet_ntoa(addr), hp->h_name));
806         return False;
807 }
808
809  
810 /*******************************************************************
811  return the DNS name of the remote end of a socket
812  ******************************************************************/
813 char *get_socket_name(int fd, BOOL force_lookup)
814 {
815         static pstring name_buf;
816         static fstring addr_buf;
817         struct hostent *hp;
818         struct in_addr addr;
819         char *p;
820
821         /* reverse lookups can be *very* expensive, and in many
822            situations won't work because many networks don't link dhcp
823            with dns. To avoid the delay we avoid the lookup if
824            possible */
825         if (!lp_hostname_lookups() && (force_lookup == False)) {
826                 return get_socket_addr(fd);
827         }
828         
829         p = get_socket_addr(fd);
830
831         /* it might be the same as the last one - save some DNS work */
832         if (strcmp(p, addr_buf) == 0) return name_buf;
833
834         pstrcpy(name_buf,"UNKNOWN");
835         if (fd == -1) return name_buf;
836
837         fstrcpy(addr_buf, p);
838
839         addr = *interpret_addr2(p);
840         
841         /* Look up the remote host name. */
842         if ((hp = gethostbyaddr((char *)&addr.s_addr, sizeof(addr.s_addr), AF_INET)) == 0) {
843                 DEBUG(1,("Gethostbyaddr failed for %s\n",p));
844                 pstrcpy(name_buf, p);
845         } else {
846                 pstrcpy(name_buf,(char *)hp->h_name);
847                 if (!matchname(name_buf, addr)) {
848                         DEBUG(0,("Matchname failed on %s %s\n",name_buf,p));
849                         pstrcpy(name_buf,"UNKNOWN");
850                 }
851         }
852
853         alpha_strcpy(name_buf, name_buf, "_-.", sizeof(name_buf));
854         if (strstr(name_buf,"..")) {
855                 pstrcpy(name_buf, "UNKNOWN");
856         }
857
858         return name_buf;
859 }
860
861 /*******************************************************************
862  return the IP addr of the remote end of a socket as a string 
863  ******************************************************************/
864 char *get_socket_addr(int fd)
865 {
866         struct sockaddr sa;
867         struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
868         int     length = sizeof(sa);
869         static fstring addr_buf;
870
871         fstrcpy(addr_buf,"0.0.0.0");
872
873         if (fd == -1) {
874                 return addr_buf;
875         }
876         
877         if (getpeername(fd, &sa, &length) < 0) {
878                 DEBUG(0,("getpeername failed. Error was %s\n", strerror(errno) ));
879                 return addr_buf;
880         }
881         
882         fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
883         
884         return addr_buf;
885 }
886
887
888 /*******************************************************************
889  Create protected unix domain socket.
890
891  Some unixes cannot set permissions on a ux-dom-sock, so we
892  have to make sure that the directory contains the protection
893  permissions instead.
894  ******************************************************************/
895
896 int create_pipe_sock(const char *socket_dir,
897                      const char *socket_name,
898                      mode_t dir_perms)
899 {
900 #ifdef HAVE_UNIXSOCKET
901         struct sockaddr_un sunaddr;
902         struct stat st;
903         int sock;
904         mode_t old_umask;
905         pstring path;
906         
907         old_umask = umask(0);
908         
909         /* Create the socket directory or reuse the existing one */
910         
911         if (lstat(socket_dir, &st) == -1) {
912                 if (errno == ENOENT) {
913                         /* Create directory */
914                         if (mkdir(socket_dir, dir_perms) == -1) {
915                                 DEBUG(0, ("error creating socket directory "
916                                         "%s: %s\n", socket_dir, 
917                                         strerror(errno)));
918                                 goto out_umask;
919                         }
920                 } else {
921                         DEBUG(0, ("lstat failed on socket directory %s: %s\n",
922                                 socket_dir, strerror(errno)));
923                         goto out_umask;
924                 }
925         } else {
926                 /* Check ownership and permission on existing directory */
927                 if (!S_ISDIR(st.st_mode)) {
928                         DEBUG(0, ("socket directory %s isn't a directory\n",
929                                 socket_dir));
930                         goto out_umask;
931                 }
932                 if ((st.st_uid != sec_initial_uid()) || 
933                                 ((st.st_mode & 0777) != dir_perms)) {
934                         DEBUG(0, ("invalid permissions on socket directory "
935                                 "%s\n", socket_dir));
936                         goto out_umask;
937                 }
938         }
939         
940         /* Create the socket file */
941         
942         sock = socket(AF_UNIX, SOCK_STREAM, 0);
943         
944         if (sock == -1) {
945                 perror("socket");
946                 goto out_umask;
947         }
948         
949         snprintf(path, sizeof(path), "%s/%s", socket_dir, socket_name);
950         
951         unlink(path);
952         memset(&sunaddr, 0, sizeof(sunaddr));
953         sunaddr.sun_family = AF_UNIX;
954         safe_strcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path)-1);
955         
956         if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
957                 DEBUG(0, ("bind failed on pipe socket %s: %s\n", path,
958                         strerror(errno)));
959                 goto out_close;
960         }
961         
962         if (listen(sock, 5) == -1) {
963                 DEBUG(0, ("listen failed on pipe socket %s: %s\n", path,
964                         strerror(errno)));
965                 goto out_close;
966         }
967         
968         umask(old_umask);
969         return sock;
970
971 out_close:
972         close(sock);
973
974 out_umask:
975         umask(old_umask);
976         return -1;
977
978 #else
979         DEBUG(0, ("create_pipe_sock: No Unix sockets on this system\n"));
980         return -1;
981 #endif /* HAVE_UNIXSOCKET */
982 }
983
984 /*******************************************************************
985 this is like socketpair but uses tcp. It is used by the Samba
986 regression test code
987 The function guarantees that nobody else can attach to the socket,
988 or if they do that this function fails and the socket gets closed
989 returns 0 on success, -1 on failure
990 the resulting file descriptors are symmetrical
991  ******************************************************************/
992 static int socketpair_tcp(int fd[2])
993 {
994         int listener;
995         struct sockaddr_in sock;
996         struct sockaddr_in sock2;
997         socklen_t socklen = sizeof(sock);
998         int connect_done = 0;
999         
1000         fd[0] = fd[1] = listener = -1;
1001
1002         memset(&sock, 0, sizeof(sock));
1003         
1004         if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) goto failed;
1005
1006         memset(&sock2, 0, sizeof(sock2));
1007 #ifdef HAVE_SOCK_SIN_LEN
1008         sock2.sin_len = sizeof(sock2);
1009 #endif
1010         sock2.sin_family = PF_INET;
1011
1012         bind(listener, (struct sockaddr *)&sock2, sizeof(sock2));
1013
1014         if (listen(listener, 1) != 0) goto failed;
1015
1016         if (getsockname(listener, (struct sockaddr *)&sock, &socklen) != 0) goto failed;
1017
1018         if ((fd[1] = socket(PF_INET, SOCK_STREAM, 0)) == -1) goto failed;
1019
1020         set_blocking(fd[1], 0);
1021
1022         sock.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1023
1024         if (connect(fd[1],(struct sockaddr *)&sock,sizeof(sock)) == -1) {
1025                 if (errno != EINPROGRESS) goto failed;
1026         } else {
1027                 connect_done = 1;
1028         }
1029
1030         if ((fd[0] = accept(listener, (struct sockaddr *)&sock, &socklen)) == -1) goto failed;
1031
1032         close(listener);
1033         if (connect_done == 0) {
1034                 if (connect(fd[1],(struct sockaddr *)&sock,sizeof(sock)) != 0
1035                     && errno != EISCONN) goto failed;
1036         }
1037
1038         set_blocking(fd[1], 1);
1039
1040         /* all OK! */
1041         return 0;
1042
1043  failed:
1044         if (fd[0] != -1) close(fd[0]);
1045         if (fd[1] != -1) close(fd[1]);
1046         if (listener != -1) close(listener);
1047         return -1;
1048 }
1049
1050
1051 /*******************************************************************
1052 run a program on a local tcp socket, this is used to launch smbd
1053 when regression testing
1054 the return value is a socket which is attached to a subprocess
1055 running "prog". stdin and stdout are attached. stderr is left
1056 attached to the original stderr
1057  ******************************************************************/
1058 int sock_exec(const char *prog)
1059 {
1060         int fd[2];
1061         if (socketpair_tcp(fd) != 0) {
1062                 DEBUG(0,("socketpair_tcp failed (%s)\n", strerror(errno)));
1063                 return -1;
1064         }
1065         if (fork() == 0) {
1066                 close(fd[0]);
1067                 close(0);
1068                 close(1);
1069                 dup(fd[1]);
1070                 dup(fd[1]);
1071                 exit(system(prog));
1072         }
1073         close(fd[1]);
1074         return fd[0];
1075 }