mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-01 11:32:48 -05:00
Log identifying information for the other end of connections.
When anyone connects to a PuTTY tool's listening socket - whether it's
a user of a local->remote port forwarding, a connection-sharing
downstream or a client of Pageant - we'd like to log as much
information as we can find out about where the connection came from.
To that end, I've implemented a function sk_peer_info() in the socket
abstraction, which returns a freeform text string as best it can (or
NULL, if it can't get anything at all) describing the thing at the
other end of the connection. For TCP connections, this is done using
getpeername() to get an IP address and port in the obvious way; for
Unix-domain sockets, we attempt SO_PEERCRED (conditionalised on some
moderately hairy autoconfery) to get the pid and owner of the peer. I
haven't implemented anything for Windows named pipes, but I will if I
hear of anything useful.
(cherry picked from commit c8f83979a3
)
Conflicts:
pageant.c
Cherry-picker's notes: the conflict was because the original commit
also added a use of the same feature in the centralised Pageant code,
which doesn't exist on this branch. Also I had to remove 'const' from
the type of the second parameter to wrap_send_port_open(), since this
branch hasn't had the same extensive const-fixing as master.
This commit is contained in:
@ -234,6 +234,11 @@ static const char *sk_handle_socket_error(Socket s)
|
||||
return ps->error;
|
||||
}
|
||||
|
||||
static char *sk_handle_peer_info(Socket s)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Socket make_handle_socket(HANDLE send_H, HANDLE recv_H, Plug plug,
|
||||
int overlapped)
|
||||
{
|
||||
@ -245,7 +250,8 @@ Socket make_handle_socket(HANDLE send_H, HANDLE recv_H, Plug plug,
|
||||
sk_handle_write_eof,
|
||||
sk_handle_flush,
|
||||
sk_handle_set_frozen,
|
||||
sk_handle_socket_error
|
||||
sk_handle_socket_error,
|
||||
sk_handle_peer_info,
|
||||
};
|
||||
|
||||
Handle_Socket ret;
|
||||
|
@ -160,6 +160,8 @@ DECL_WINDOWS_FUNCTION(static, struct servent FAR *, getservbyname,
|
||||
(const char FAR *, const char FAR *));
|
||||
DECL_WINDOWS_FUNCTION(static, unsigned long, inet_addr, (const char FAR *));
|
||||
DECL_WINDOWS_FUNCTION(static, char FAR *, inet_ntoa, (struct in_addr));
|
||||
DECL_WINDOWS_FUNCTION(static, const char FAR *, inet_ntop,
|
||||
(int, void FAR *, char *, size_t));
|
||||
DECL_WINDOWS_FUNCTION(static, int, connect,
|
||||
(SOCKET, const struct sockaddr FAR *, int));
|
||||
DECL_WINDOWS_FUNCTION(static, int, bind,
|
||||
@ -174,6 +176,8 @@ DECL_WINDOWS_FUNCTION(static, int, ioctlsocket,
|
||||
(SOCKET, long, u_long FAR *));
|
||||
DECL_WINDOWS_FUNCTION(static, SOCKET, accept,
|
||||
(SOCKET, struct sockaddr FAR *, int FAR *));
|
||||
DECL_WINDOWS_FUNCTION(static, int, getpeername,
|
||||
(SOCKET, struct sockaddr FAR *, int FAR *));
|
||||
DECL_WINDOWS_FUNCTION(static, int, recv, (SOCKET, char FAR *, int, int));
|
||||
DECL_WINDOWS_FUNCTION(static, int, WSAIoctl,
|
||||
(SOCKET, DWORD, LPVOID, DWORD, LPVOID, DWORD,
|
||||
@ -288,6 +292,7 @@ void sk_init(void)
|
||||
GET_WINDOWS_FUNCTION(winsock_module, getservbyname);
|
||||
GET_WINDOWS_FUNCTION(winsock_module, inet_addr);
|
||||
GET_WINDOWS_FUNCTION(winsock_module, inet_ntoa);
|
||||
GET_WINDOWS_FUNCTION(winsock_module, inet_ntop);
|
||||
GET_WINDOWS_FUNCTION(winsock_module, connect);
|
||||
GET_WINDOWS_FUNCTION(winsock_module, bind);
|
||||
GET_WINDOWS_FUNCTION(winsock_module, setsockopt);
|
||||
@ -297,6 +302,7 @@ void sk_init(void)
|
||||
GET_WINDOWS_FUNCTION(winsock_module, shutdown);
|
||||
GET_WINDOWS_FUNCTION(winsock_module, ioctlsocket);
|
||||
GET_WINDOWS_FUNCTION(winsock_module, accept);
|
||||
GET_WINDOWS_FUNCTION(winsock_module, getpeername);
|
||||
GET_WINDOWS_FUNCTION(winsock_module, recv);
|
||||
GET_WINDOWS_FUNCTION(winsock_module, WSAIoctl);
|
||||
|
||||
@ -861,6 +867,7 @@ static int sk_tcp_write_oob(Socket s, const char *data, int len);
|
||||
static void sk_tcp_write_eof(Socket s);
|
||||
static void sk_tcp_set_frozen(Socket s, int is_frozen);
|
||||
static const char *sk_tcp_socket_error(Socket s);
|
||||
static char *sk_tcp_peer_info(Socket s);
|
||||
|
||||
extern char *do_select(SOCKET skt, int startup);
|
||||
|
||||
@ -874,7 +881,8 @@ static Socket sk_tcp_accept(accept_ctx_t ctx, Plug plug)
|
||||
sk_tcp_write_eof,
|
||||
sk_tcp_flush,
|
||||
sk_tcp_set_frozen,
|
||||
sk_tcp_socket_error
|
||||
sk_tcp_socket_error,
|
||||
sk_tcp_peer_info,
|
||||
};
|
||||
|
||||
DWORD err;
|
||||
@ -1122,7 +1130,8 @@ Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
|
||||
sk_tcp_write_eof,
|
||||
sk_tcp_flush,
|
||||
sk_tcp_set_frozen,
|
||||
sk_tcp_socket_error
|
||||
sk_tcp_socket_error,
|
||||
sk_tcp_peer_info,
|
||||
};
|
||||
|
||||
Actual_Socket ret;
|
||||
@ -1173,7 +1182,8 @@ Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only,
|
||||
sk_tcp_write_eof,
|
||||
sk_tcp_flush,
|
||||
sk_tcp_set_frozen,
|
||||
sk_tcp_socket_error
|
||||
sk_tcp_socket_error,
|
||||
sk_tcp_peer_info,
|
||||
};
|
||||
|
||||
SOCKET s;
|
||||
@ -1744,6 +1754,38 @@ static const char *sk_tcp_socket_error(Socket sock)
|
||||
return s->error;
|
||||
}
|
||||
|
||||
static char *sk_tcp_peer_info(Socket sock)
|
||||
{
|
||||
Actual_Socket s = (Actual_Socket) sock;
|
||||
#ifdef NO_IPV6
|
||||
struct sockaddr_in addr;
|
||||
#else
|
||||
struct sockaddr_storage addr;
|
||||
#endif
|
||||
int addrlen = sizeof(addr);
|
||||
char buf[INET6_ADDRSTRLEN];
|
||||
|
||||
if (p_getpeername(s->s, (struct sockaddr *)&addr, &addrlen) < 0)
|
||||
return NULL;
|
||||
|
||||
if (((struct sockaddr *)&addr)->sa_family == AF_INET) {
|
||||
return dupprintf
|
||||
("%s:%d",
|
||||
p_inet_ntoa(((struct sockaddr_in *)&addr)->sin_addr),
|
||||
(int)p_ntohs(((struct sockaddr_in *)&addr)->sin_port));
|
||||
#ifndef NO_IPV6
|
||||
} else if (((struct sockaddr *)&addr)->sa_family == AF_INET6) {
|
||||
return dupprintf
|
||||
("[%s]:%d",
|
||||
p_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&addr)->sin6_addr,
|
||||
buf, sizeof(buf)),
|
||||
(int)p_ntohs(((struct sockaddr_in6 *)&addr)->sin6_port));
|
||||
#endif
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void sk_tcp_set_frozen(Socket sock, int is_frozen)
|
||||
{
|
||||
Actual_Socket s = (Actual_Socket) sock;
|
||||
|
@ -71,6 +71,11 @@ static const char *sk_namedpipeserver_socket_error(Socket s)
|
||||
return ps->error;
|
||||
}
|
||||
|
||||
static char *sk_namedpipeserver_peer_info(Socket s)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int create_named_pipe(Named_Pipe_Server_Socket ps, int first_instance)
|
||||
{
|
||||
SECURITY_ATTRIBUTES sa;
|
||||
@ -211,7 +216,8 @@ Socket new_named_pipe_listener(const char *pipename, Plug plug)
|
||||
NULL /* write_eof */,
|
||||
NULL /* flush */,
|
||||
NULL /* set_frozen */,
|
||||
sk_namedpipeserver_socket_error
|
||||
sk_namedpipeserver_socket_error,
|
||||
sk_namedpipeserver_peer_info,
|
||||
};
|
||||
|
||||
Named_Pipe_Server_Socket ret;
|
||||
|
Reference in New Issue
Block a user