mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-10 01:48:00 +00:00
c8f83979a3
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.
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;
|
|
}
|