2 Unix SMB/Netbios implementation.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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.
26 #undef Realloc /* SSLeay defines this and samba has a function of this name */
31 extern int DEBUGLEVEL
;
35 /* the port, where client connected */
38 /* the last IP received from */
39 struct in_addr lastip
;
41 /* the last port received from */
45 int smb_read_error
= 0;
48 /****************************************************************************
49 Determine if a file descriptor is in fact a socket.
50 ****************************************************************************/
51 BOOL
is_a_socket(int fd
)
55 return (getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&v
, &l
) == 0);
60 { OPT_BOOL
, OPT_INT
, OPT_ON
};
73 "SO_KEEPALIVE", SOL_SOCKET
, SO_KEEPALIVE
, 0, OPT_BOOL
}
76 "SO_REUSEADDR", SOL_SOCKET
, SO_REUSEADDR
, 0, OPT_BOOL
}
79 "SO_BROADCAST", SOL_SOCKET
, SO_BROADCAST
, 0, OPT_BOOL
}
83 "TCP_NODELAY", IPPROTO_TCP
, TCP_NODELAY
, 0, OPT_BOOL
}
88 "IPTOS_LOWDELAY", IPPROTO_IP
, IP_TOS
, IPTOS_LOWDELAY
, OPT_ON
}
91 #ifdef IPTOS_THROUGHPUT
93 "IPTOS_THROUGHPUT", IPPROTO_IP
, IP_TOS
, IPTOS_THROUGHPUT
,
99 "SO_REUSEPORT", SOL_SOCKET
, SO_REUSEPORT
, 0, OPT_BOOL
}
104 "SO_SNDBUF", SOL_SOCKET
, SO_SNDBUF
, 0, OPT_INT
}
109 "SO_RCVBUF", SOL_SOCKET
, SO_RCVBUF
, 0, OPT_INT
}
114 "SO_SNDLOWAT", SOL_SOCKET
, SO_SNDLOWAT
, 0, OPT_INT
}
119 "SO_RCVLOWAT", SOL_SOCKET
, SO_RCVLOWAT
, 0, OPT_INT
}
124 "SO_SNDTIMEO", SOL_SOCKET
, SO_SNDTIMEO
, 0, OPT_INT
}
129 "SO_RCVTIMEO", SOL_SOCKET
, SO_RCVTIMEO
, 0, OPT_INT
}
138 /****************************************************************************
139 Set user socket options.
140 ****************************************************************************/
141 void set_socket_options(int fd
, char *options
)
145 while (next_token(&options
, tok
, " \t,", sizeof(tok
)))
150 BOOL got_value
= False
;
152 if ((p
= strchr(tok
, '=')))
159 for (i
= 0; socket_options
[i
].name
; i
++)
160 if (strequal(socket_options
[i
].name
, tok
))
163 if (!socket_options
[i
].name
)
165 DEBUG(0, ("Unknown socket option %s\n", tok
));
169 switch (socket_options
[i
].opttype
)
173 ret
= setsockopt(fd
, socket_options
[i
].level
,
174 socket_options
[i
].option
,
175 (char *)&value
, sizeof(int));
181 ("syntax error - %s does not take a value\n",
185 int on
= socket_options
[i
].value
;
200 ("Failed to set socket option %s (Error %s)\n",
201 tok
, strerror(errno
)));
207 /****************************************************************************
209 ****************************************************************************/
212 /*******************************************************************
213 checks if write data is outstanding.
214 ********************************************************************/
215 int write_data_outstanding(int fd
, unsigned int time_out
, BOOL
*more
)
219 struct timeval timeout
;
224 timeout
.tv_sec
= (time_t) (time_out
/ 1000);
225 timeout
.tv_usec
= (long)(1000 * (time_out
% 1000));
227 selrtn
= sys_select(fd
+ 1, NULL
, &fds
, &timeout
);
233 (*more
) = FD_ISSET(fd
, &fds
);
237 /*******************************************************************
238 checks if read data is outstanding.
239 ********************************************************************/
240 int read_data_outstanding(int fd
, unsigned int time_out
)
244 struct timeval timeout
;
249 timeout
.tv_sec
= (time_t) (time_out
/ 1000);
250 timeout
.tv_usec
= (long)(1000 * (time_out
% 1000));
252 selrtn
= sys_select(fd
+ 1, &fds
, NULL
, &timeout
);
258 return FD_ISSET(fd
, &fds
) ? 1 : 0;
261 /****************************************************************************
263 ****************************************************************************/
264 ssize_t
read_udp_socket(int fd
, char *buf
, size_t len
)
267 struct sockaddr_in sock
;
270 socklen
= sizeof(sock
);
271 memset((char *)&sock
, 0, socklen
);
274 (ssize_t
) recvfrom(fd
, buf
, len
, 0, (struct sockaddr
*)&sock
,
278 DEBUG(2, ("read socket failed. ERRNO=%s\n", strerror(errno
)));
282 lastip
= sock
.sin_addr
;
283 lastport
= ntohs(sock
.sin_port
);
285 DEBUG(10, ("read_udp_socket: lastip %s lastport %d read: %d\n",
286 inet_ntoa(lastip
), lastport
, ret
));
291 /****************************************************************************
292 Read data from a socket with a timout in msec.
293 mincount = if timeout, minimum to read before returning
294 maxcount = number to be read.
295 time_out = timeout in milliseconds
296 ****************************************************************************/
298 ssize_t
read_socket_with_timeout(int fd
, char *buf
, size_t mincnt
,
299 size_t maxcnt
, unsigned int time_out
)
304 /* just checking .... */
316 while (nread
< mincnt
)
322 SSL_read(ssl
, buf
+ nread
,
328 read(fd
, buf
+ nread
, maxcnt
- nread
);
331 readret
= read(fd
, buf
+ nread
, maxcnt
- nread
);
332 #endif /* WITH_SSL */
337 ("read_socket_with_timeout: blocking read. EOF from client.\n"));
338 smb_read_error
= READ_EOF
;
345 ("read_socket_with_timeout: read error = %s.\n",
347 smb_read_error
= READ_ERROR
;
352 return ((ssize_t
) nread
);
355 /* Most difficult - timeout read */
356 /* If this is ever called on a disk file and
357 mincnt is greater then the filesize then
358 system performance will suffer severely as
359 select always returns true on disk files */
361 for (nread
= 0; nread
< mincnt
;)
363 int selrtn
= read_data_outstanding(fd
, time_out
);
368 /* something is wrong. Maybe the socket is dead? */
370 ("read_socket_with_timeout: timeout read. select error = %s.\n",
372 smb_read_error
= READ_ERROR
;
376 /* Did we timeout ? */
380 ("read_socket_with_timeout: timeout read. select timed out.\n"));
381 smb_read_error
= READ_TIMEOUT
;
388 readret
= SSL_read(ssl
, buf
+ nread
, maxcnt
- nread
);
392 readret
= read(fd
, buf
+ nread
, maxcnt
- nread
);
395 readret
= read(fd
, buf
+ nread
, maxcnt
- nread
);
396 #endif /* WITH_SSL */
400 /* we got EOF on the file descriptor */
402 ("read_socket_with_timeout: timeout read. EOF from client.\n"));
403 smb_read_error
= READ_EOF
;
409 /* the descriptor is probably dead */
411 ("read_socket_with_timeout: timeout read. read error = %s.\n",
413 smb_read_error
= READ_ERROR
;
419 /* Return the number we got */
420 return ((ssize_t
) nread
);
423 /****************************************************************************
424 Read data from a fd with a timout in msec.
425 mincount = if timeout, minimum to read before returning
426 maxcount = number to be read.
427 time_out = timeout in milliseconds
428 ****************************************************************************/
430 ssize_t
read_with_timeout(int fd
, char *buf
, size_t mincnt
, size_t maxcnt
,
431 unsigned int time_out
)
436 /* just checking .... */
446 while (nread
< mincnt
)
452 SSL_read(ssl
, buf
+ nread
,
458 read(fd
, buf
+ nread
, maxcnt
- nread
);
461 readret
= read(fd
, buf
+ nread
, maxcnt
- nread
);
462 #endif /* WITH_SSL */
469 return ((ssize_t
) nread
);
472 /* Most difficult - timeout read */
473 /* If this is ever called on a disk file and
474 mincnt is greater then the filesize then
475 system performance will suffer severely as
476 select always returns true on disk files */
478 for (nread
= 0; nread
< mincnt
;)
480 int selrtn
= read_data_outstanding(fd
, time_out
);
488 readret
= SSL_read(ssl
, buf
+ nread
, maxcnt
- nread
);
492 readret
= read(fd
, buf
+ nread
, maxcnt
- nread
);
495 readret
= read(fd
, buf
+ nread
, maxcnt
- nread
);
496 #endif /* WITH_SSL */
504 /* Return the number we got */
505 return ((ssize_t
) nread
);
508 /****************************************************************************
509 send a keepalive packet (rfc1002)
510 ****************************************************************************/
511 BOOL
send_keepalive(int client
)
513 unsigned char buf
[4];
516 buf
[1] = buf
[2] = buf
[3] = 0;
518 return (write_socket_data(client
, (char *)buf
, 4) == 4);
523 /****************************************************************************
524 read data from the client, reading exactly N bytes.
525 ****************************************************************************/
526 ssize_t
read_data(int fd
, char *buffer
, size_t N
)
538 ret
= SSL_read(ssl
, buffer
+ total
, N
- total
);
542 ret
= read(fd
, buffer
+ total
, N
- total
);
545 ret
= read(fd
, buffer
+ total
, N
- total
);
546 #endif /* WITH_SSL */
551 ("read_data: read of %d returned 0. Error = %s\n",
552 (int)(N
- total
), strerror(errno
)));
553 smb_read_error
= READ_EOF
;
559 ("read_data: read failure for %d. Error = %s\n",
560 (int)(N
- total
), strerror(errno
)));
561 smb_read_error
= READ_ERROR
;
566 return (ssize_t
) total
;
569 /****************************************************************************
570 Read data from a socket, reading exactly N bytes.
571 ****************************************************************************/
573 static ssize_t
read_socket_data(int fd
, char *buffer
, size_t N
)
585 ret
= SSL_read(ssl
, buffer
+ total
, N
- total
);
589 ret
= read(fd
, buffer
+ total
, N
- total
);
592 ret
= read(fd
, buffer
+ total
, N
- total
);
593 #endif /* WITH_SSL */
598 ("read_socket_data: recv of %d returned 0. Error = %s\n",
599 (int)(N
- total
), strerror(errno
)));
600 smb_read_error
= READ_EOF
;
606 ("read_socket_data: recv failure for %d. Error = %s\n",
607 (int)(N
- total
), strerror(errno
)));
608 smb_read_error
= READ_ERROR
;
613 return (ssize_t
) total
;
616 /****************************************************************************
618 ****************************************************************************/
619 ssize_t
write_data(int fd
, char *buffer
, size_t N
)
629 ret
= SSL_write(ssl
, buffer
+ total
, N
- total
);
633 ret
= write(fd
, buffer
+ total
, N
- total
);
636 ret
= write(fd
, buffer
+ total
, N
- total
);
637 #endif /* WITH_SSL */
642 ("write_data: write failure. Error = %s\n",
651 return (ssize_t
) total
;
654 /****************************************************************************
655 Write data to a socket - use send rather than write.
656 ****************************************************************************/
658 ssize_t
write_socket_data(int fd
, char *buffer
, size_t N
)
668 ret
= SSL_write(ssl
, buffer
+ total
, N
- total
);
672 ret
= send(fd
, buffer
+ total
, N
- total
, 0);
675 ret
= send(fd
, buffer
+ total
, N
- total
, 0);
676 #endif /* WITH_SSL */
681 ("write_socket_data: write failure. Error = %s\n",
690 return (ssize_t
) total
;
693 /****************************************************************************
695 ****************************************************************************/
696 ssize_t
write_socket(int fd
, char *buf
, size_t len
)
702 DEBUG(6, ("write_socket(%d,%d)\n", fd
, (int)len
));
703 ret
= write_socket_data(fd
, buf
, len
);
705 DEBUG(6, ("write_socket(%d,%d) wrote %d\n", fd
, (int)len
, (int)ret
));
708 ("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
709 (int)len
, fd
, strerror(errno
)));
715 /****************************************************************************
716 read 4 bytes of a smb packet and return the smb length of the packet
717 store the result in the buffer
718 This version of the function will return a length of zero on receiving
720 timeout is in milliseconds.
721 ****************************************************************************/
722 static ssize_t
read_smb_length_return_keepalive(int fd
, char *inbuf
,
723 unsigned int timeout
)
733 (read_socket_with_timeout
734 (fd
, inbuf
, 4, 4, timeout
) == 4);
736 ok
= (read_socket_data(fd
, inbuf
, 4) == 4);
741 len
= smb_len(inbuf
);
742 msg_type
= CVAL(inbuf
, 0);
744 if (msg_type
== 0x85)
745 DEBUG(5, ("Got keepalive packet\n"));
748 DEBUG(10, ("got smb length of %d\n", len
));
753 /****************************************************************************
754 read 4 bytes of a smb packet and return the smb length of the packet
755 store the result in the buffer. This version of the function will
756 never return a session keepalive (length of zero).
757 timeout is in milliseconds.
758 ****************************************************************************/
759 ssize_t
read_smb_length(int fd
, char *inbuf
, unsigned int timeout
)
765 len
= read_smb_length_return_keepalive(fd
, inbuf
, timeout
);
770 /* Ignore session keepalives. */
771 if (CVAL(inbuf
, 0) != 0x85)
775 DEBUG(10, ("read_smb_length: got smb length of %d\n", len
));
780 /****************************************************************************
781 read an smb from a fd. Note that the buffer *MUST* be of size
782 BUFFER_SIZE+SAFETY_MARGIN.
783 The timeout is in milliseconds.
784 This function will return on a
785 receipt of a session keepalive packet.
786 ****************************************************************************/
787 BOOL
receive_smb(int fd
, char *buffer
, unsigned int timeout
)
795 DEBUG(1, ("receive_smb: buffer==NULL\n"));
799 memset(buffer
, 0, smb_size
+ 100);
801 len
= read_smb_length_return_keepalive(fd
, buffer
, timeout
);
804 DEBUG(10, ("receive_smb: length < 0!\n"));
808 if (len
> BUFFER_SIZE
)
810 DEBUG(0, ("Invalid packet length! (%d bytes).\n", len
));
811 if (len
> BUFFER_SIZE
+ (SAFETY_MARGIN
/ 2))
819 ret
= read_socket_data(fd
, buffer
+ 4, len
);
822 smb_read_error
= READ_ERROR
;
829 /****************************************************************************
830 read an smb from a fd ignoring all keepalive packets. Note that the buffer
831 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
832 The timeout is in milliseconds
834 This is exactly the same as receive_smb except that it never returns
835 a session keepalive packet (just as receive_smb used to do).
836 receive_smb was changed to return keepalives as the oplock processing means this call
837 should never go into a blocking read.
838 ****************************************************************************/
840 BOOL
client_receive_smb(int fd
, char *buffer
, unsigned int timeout
)
846 ret
= receive_smb(fd
, buffer
, timeout
);
850 DEBUG(10, ("client_receive_smb failed\n"));
855 /* Ignore session keepalive packets. */
856 if (CVAL(buffer
, 0) != 0x85)
863 /****************************************************************************
864 send an null session message to a fd
865 ****************************************************************************/
867 BOOL
send_null_session_msg(int fd
)
873 char *buffer
= (char *)&blank
;
875 while (nwritten
< len
)
877 ret
= write_socket(fd
, buffer
+ nwritten
, len
- nwritten
);
881 ("send_null_session_msg: Error writing %d bytes to client. %d. Exiting\n",
882 (int)len
, (int)ret
));
888 DEBUG(10, ("send_null_session_msg: sent 4 null bytes to client.\n"));
892 /****************************************************************************
894 ****************************************************************************/
895 BOOL
send_smb(int fd
, char *buffer
)
900 len
= smb_len(buffer
) + 4;
902 while (nwritten
< len
)
904 ret
= write_socket(fd
, buffer
+ nwritten
, len
- nwritten
);
908 ("Error writing %d bytes to client. %d. Exiting\n",
909 (int)len
, (int)ret
));
920 /****************************************************************************
921 send a single packet to a port on another machine
922 ****************************************************************************/
923 BOOL
send_one_packet(char *buf
, int len
, struct in_addr ip
, int port
,
928 struct sockaddr_in sock_out
;
933 /* create a socket to write to */
934 out_fd
= socket(AF_INET
, type
, 0);
937 DEBUG(0, ("socket failed"));
941 /* set the address and port */
942 ZERO_STRUCT(sock_out
);
943 putip((char *)&sock_out
.sin_addr
, (char *)&ip
);
944 sock_out
.sin_port
= htons(port
);
945 sock_out
.sin_family
= AF_INET
;
949 ("sending a packet of len %d to (%s) on port %d of type %s\n",
950 len
, inet_ntoa(ip
), port
,
951 type
== SOCK_DGRAM
? "DGRAM" : "STREAM"));
956 (out_fd
, buf
, len
, 0, (struct sockaddr
*)&sock_out
,
957 sizeof(sock_out
)) >= 0);
960 DEBUG(0, ("Packet send to %s(%d) failed ERRNO=%s\n",
961 inet_ntoa(ip
), port
, strerror(errno
)));
968 /****************************************************************************
969 open a socket of the specified type, port and address for incoming data
970 ****************************************************************************/
971 int open_socket_in(int type
, int port
, int dlevel
, uint32 socket_addr
,
975 struct sockaddr_in sock
;
979 /* get my host name */
980 if (gethostname(host_name
, MAXHOSTNAMELEN
) == -1)
982 DEBUG(0, ("gethostname failed\n"));
987 if ((hp
= Get_Hostbyname(host_name
)) == 0)
989 DEBUG(0, ("Get_Hostbyname: Unknown host %s\n", host_name
));
994 memcpy((char *)&sock
.sin_addr
, (char *)hp
->h_addr
, hp
->h_length
);
996 #ifdef HAVE_SOCK_SIN_LEN
997 sock
.sin_len
= sizeof(sock
);
999 sock
.sin_port
= htons(port
);
1000 sock
.sin_family
= hp
->h_addrtype
;
1001 sock
.sin_addr
.s_addr
= socket_addr
;
1002 res
= socket(hp
->h_addrtype
, type
, 0);
1005 DEBUG(0, ("socket failed\n"));
1016 (res
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
,
1019 ("setsockopt: SO_REUSEADDR=%d on port %d failed with error = %s\n",
1020 val
, port
, strerror(errno
)));
1023 (res
, SOL_SOCKET
, SO_REUSEPORT
, (char *)&val
,
1026 ("setsockopt: SO_REUSEPORT=%d on port %d failed with error = %s\n",
1027 val
, port
, strerror(errno
)));
1028 #endif /* SO_REUSEPORT */
1031 /* now we've got a socket - we need to bind it */
1032 if (bind(res
, (struct sockaddr
*)&sock
, sizeof(sock
)) < 0)
1036 if (port
== SMB_PORT
|| port
== NMB_PORT
)
1038 ("bind failed on port %d socket_addr=%s (%s)\n",
1039 port
, inet_ntoa(sock
.sin_addr
),
1043 if (dlevel
> 0 && port
< 1000)
1046 if (port
>= 1000 && port
< 9000)
1047 return (open_socket_in
1048 (type
, port
+ 1, dlevel
, socket_addr
,
1054 DEBUG(3, ("bind succeeded on port %d\n", port
));
1060 /****************************************************************************
1061 create an outgoing socket. timeout is in milliseconds.
1062 **************************************************************************/
1063 int open_socket_out(int type
, struct in_addr
*addr
, int port
, int timeout
)
1065 struct sockaddr_in sock_out
;
1067 int connect_loop
= 250; /* 250 milliseconds */
1068 int loops
= (timeout
) / connect_loop
;
1070 /* create a socket to write to */
1071 res
= socket(PF_INET
, type
, 0);
1074 DEBUG(0, ("socket error\n"));
1078 if (type
!= SOCK_STREAM
)
1081 ZERO_STRUCT(sock_out
);
1082 putip((char *)&sock_out
.sin_addr
, (char *)addr
);
1084 sock_out
.sin_port
= htons(port
);
1085 sock_out
.sin_family
= PF_INET
;
1087 /* set it non-blocking */
1088 set_blocking(res
, False
);
1090 DEBUG(3, ("Connecting to %s at port %d\n", inet_ntoa(*addr
), port
));
1092 /* and connect it to the destination */
1094 ret
= connect(res
, (struct sockaddr
*)&sock_out
, sizeof(sock_out
));
1096 /* Some systems return EAGAIN when they mean EINPROGRESS */
1097 if (ret
< 0 && (errno
== EINPROGRESS
|| errno
== EALREADY
||
1098 errno
== EAGAIN
) && loops
--)
1100 msleep(connect_loop
);
1104 if (ret
< 0 && (errno
== EINPROGRESS
|| errno
== EALREADY
||
1108 ("timeout connecting to %s:%d\n", inet_ntoa(*addr
),
1115 if (ret
< 0 && errno
== EISCONN
)
1124 DEBUG(1, ("error connecting to %s:%d (%s)\n",
1125 inet_ntoa(*addr
), port
, strerror(errno
)));
1130 /* set it blocking again */
1131 set_blocking(res
, True
);
1137 /*******************************************************************
1138 Reset the 'done' variables so after a client process is created
1139 from a fork call these calls will be re-done. This should be
1140 expanded if more variables need reseting.
1141 ******************************************************************/
1144 void reset_globals_after_fork(void)
1147 * Re-seed the random crypto generator, so all smbd's
1148 * started from the same parent won't generate the same
1152 unsigned char dummy
;
1153 generate_random_buffer(&dummy
, 1, True
);
1157 /* the following 3 client_*() functions are nasty ways of allowing
1158 some generic functions to get info that really should be hidden in
1159 particular modules */
1160 static int client_fd
= -1;
1162 void client_setfd(int fd
)
1167 char *client_name(void)
1169 return get_socket_name(client_fd
);
1172 char *client_addr(void)
1174 return get_socket_addr(client_fd
);
1177 /*******************************************************************
1178 matchname - determine if host name matches IP address. Used to
1179 confirm a hostname lookup to prevent spoof attacks
1180 ******************************************************************/
1181 static BOOL
matchname(char *remotehost
, struct in_addr addr
)
1186 if ((hp
= Get_Hostbyname(remotehost
)) == 0)
1189 ("Get_Hostbyname(%s): lookup failure.\n", remotehost
));
1194 * Make sure that gethostbyname() returns the "correct" host name.
1195 * Unfortunately, gethostbyname("localhost") sometimes yields
1196 * "localhost.domain". Since the latter host name comes from the
1197 * local DNS, we just have to trust it (all bets are off if the local
1198 * DNS is perverted). We always check the address list, though.
1201 if (strcasecmp(remotehost
, hp
->h_name
)
1202 && strcasecmp(remotehost
, "localhost"))
1204 DEBUG(0, ("host name/name mismatch: %s != %s\n",
1205 remotehost
, hp
->h_name
));
1209 /* Look up the host address in the address list we just got. */
1210 for (i
= 0; hp
->h_addr_list
[i
]; i
++)
1212 if (memcmp(hp
->h_addr_list
[i
], (caddr_t
) & addr
, sizeof(addr
))
1218 * The host name does not map to the original host address. Perhaps
1219 * someone has compromised a name server. More likely someone botched
1220 * it, but that could be dangerous, too.
1223 DEBUG(0, ("host name/address mismatch: %s != %s\n",
1224 inet_ntoa(addr
), hp
->h_name
));
1228 /*******************************************************************
1229 return the DNS name of the remote end of a socket
1230 ******************************************************************/
1231 char *get_socket_name(int fd
)
1233 static pstring name_buf
;
1234 static fstring addr_buf
;
1236 struct in_addr addr
;
1239 p
= get_socket_addr(fd
);
1241 /* it might be the same as the last one - save some DNS work */
1242 if (strcmp(p
, addr_buf
) == 0)
1245 pstrcpy(name_buf
, "UNKNOWN");
1249 fstrcpy(addr_buf
, p
);
1251 if (inet_aton(p
, &addr
) == 0)
1254 /* Look up the remote host name. */
1257 gethostbyaddr((char *)&addr
.s_addr
, sizeof(addr
.s_addr
),
1260 DEBUG(1, ("Gethostbyaddr failed for %s\n", p
));
1261 pstrcpy(name_buf
, p
);
1265 pstrcpy(name_buf
, (char *)hp
->h_name
);
1266 if (!matchname(name_buf
, addr
))
1269 ("Matchname failed on %s %s\n", name_buf
, p
));
1270 pstrcpy(name_buf
, "UNKNOWN");
1276 /*******************************************************************
1277 return the IP addr of the remote end of a socket as a string
1278 ******************************************************************/
1279 char *get_socket_addr(int fd
)
1282 struct sockaddr_in
*sockin
= (struct sockaddr_in
*)(&sa
);
1283 int length
= sizeof(sa
);
1284 static fstring addr_buf
;
1286 fstrcpy(addr_buf
, "0.0.0.0");
1293 if (getpeername(fd
, &sa
, &length
) < 0)
1296 ("getpeername failed. Error was %s\n",
1301 fstrcpy(addr_buf
, (char *)inet_ntoa(sockin
->sin_addr
));
1306 /*******************************************************************
1307 opens and connects to a unix pipe socket
1308 ******************************************************************/
1309 int open_pipe_sock(char *path
)
1312 struct sockaddr_un sa
;
1314 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1318 DEBUG(0, ("unix socket open failed\n"));
1323 sa
.sun_family
= AF_UNIX
;
1324 safe_strcpy(sa
.sun_path
, path
, sizeof(sa
.sun_path
) - 1);
1326 DEBUG(10, ("socket open succeeded. file name: %s\n", sa
.sun_path
));
1328 if (connect(sock
, (struct sockaddr
*)&sa
, sizeof(sa
)) < 0)
1330 DEBUG(2, ("socket connect to %s failed: %s\n",
1331 sa
.sun_path
, strerror(errno
)));
1339 int create_pipe_socket(char *dir
, int dir_perms
, char *path
, int path_perms
)
1342 struct sockaddr_un sa
;
1344 DEBUG(0, ("create_pipe_socket: %s perms=%d %s perms=%d\n",
1345 dir
, dir_perms
, path
, path_perms
));
1348 ("*** Please someone examine create_pipe_socket and fix it ***\n"));
1349 DEBUG(0, ("*** if used other than for exclusive root access ***\n"));
1350 DEBUG(0, ("*** (see perms, which should be 0700 and 0600) ***\n"));
1351 DEBUG(0, ("*** there is a race condition to be exploited. ***\n"));
1353 mkdir(dir
, dir_perms
);
1355 if (chmod(dir
, dir_perms
) < 0)
1357 DEBUG(0, ("chmod on %s failed\n", dir
));
1363 DEBUG(0, ("remove on %s failed\n", path
));
1366 /* start listening on unix socket */
1367 s
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1371 DEBUG(0, ("socket open failed\n"));
1376 sa
.sun_family
= AF_UNIX
;
1377 safe_strcpy(sa
.sun_path
, path
, sizeof(sa
.sun_path
) - 1);
1379 if (bind(s
, (struct sockaddr
*)&sa
, sizeof(sa
)) < 0)
1381 DEBUG(0, ("socket bind to %s failed\n", sa
.sun_path
));
1389 DEBUG(0, ("bind failed\n"));
1394 if (path_perms
!= 0)
1396 chmod(path
, path_perms
);
1399 if (listen(s
, 5) == -1)
1401 DEBUG(0, ("listen failed\n"));
1405 DEBUG(5, ("unix socket opened: %s\n", path
));