mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-10 09:58:01 +00:00
41f63b6e5d
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.
33 lines
656 B
C
33 lines
656 B
C
/*
|
|
* Unix: wrapper for getsockopt(SO_PEERCRED), conditionalised on
|
|
* appropriate autoconfery.
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
# include "uxconfig.h" /* leading space prevents mkfiles.pl trying to follow */
|
|
#endif
|
|
|
|
#ifdef HAVE_SO_PEERCRED
|
|
#define _GNU_SOURCE
|
|
#include <features.h>
|
|
#endif
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include "putty.h"
|
|
|
|
int so_peercred(int fd, int *pid, int *uid, int *gid)
|
|
{
|
|
#ifdef HAVE_SO_PEERCRED
|
|
struct ucred cr;
|
|
socklen_t crlen = sizeof(cr);
|
|
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &crlen) == 0) {
|
|
*pid = cr.pid;
|
|
*uid = cr.uid;
|
|
*gid = cr.gid;
|
|
return TRUE;
|
|
}
|
|
#endif
|
|
return FALSE;
|
|
}
|