Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 9 Jul 2017 11:47:14 +0200
From:      Stefan Ehmann <shoesoft@gmx.net>
To:        Johannes Lundberg <johalun0@gmail.com>, freebsd-current <freebsd-current@freebsd.org>
Subject:   Re: Getting PID of socket client
Message-ID:  <a42d8db6-675f-b021-fc50-6ef87d310623@gmx.net>
In-Reply-To: <CAECmPwsD=PYqoG9EA=Wyq16hcrdbf7SQ3Owxdf%2BWSvYWazJfXA@mail.gmail.com>
References:  <CAECmPwsD=PYqoG9EA=Wyq16hcrdbf7SQ3Owxdf%2BWSvYWazJfXA@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On 09.07.2017 10:03, Johannes Lundberg wrote:
> Hi
> 
> Without altering the client code (i.e. adding sendmsg(credentials)), is
> there anyway of getting the client PID (or path to binary) using the file
> descriptor returned by accept() on the server side?
> 
> Similar to Linux's getsockopt with SO_PEERCRED option.

You want the LOCAL_CREDS socket option from unix(4).

There's also a FreeBSD sample in the UNIX network programming book. 
Source is available from http://unpbook.com/src.html

With the following two changes it seems to work for me:
unixstrserv02.c: set LOCAL_CREDS
readcred.c: disable CONTROL_LEN check

$ ./unixstrserv02
PID of sender = 1001
real user ID = 1001
real group ID = 1001
effective user ID = 1001
3 groups: 0 5 920



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?a42d8db6-675f-b021-fc50-6ef87d310623>