protocol-native: get pid/gid/uid on FreeBSD using LOCAL_PEERCRED

This commit is contained in:
Greg V 2020-11-09 01:16:25 +03:00 committed by Wim Taymans
parent 6ffb997c5a
commit c1cda26ce8

View file

@ -36,6 +36,9 @@
#if HAVE_PWD_H #if HAVE_PWD_H
#include <pwd.h> #include <pwd.h>
#endif #endif
#if defined(__FreeBSD__)
#include <sys/ucred.h>
#endif
#include <spa/pod/iter.h> #include <spa/pod/iter.h>
#include <spa/utils/result.h> #include <spa/utils/result.h>
@ -370,6 +373,9 @@ static struct client_data *client_new(struct server *s, int fd)
struct pw_protocol *protocol = s->this.protocol; struct pw_protocol *protocol = s->this.protocol;
socklen_t len; socklen_t len;
struct ucred ucred; struct ucred ucred;
#if defined(__FreeBSD__)
struct xucred xucred;
#endif
struct pw_context *context = protocol->context; struct pw_context *context = protocol->context;
struct pw_properties *props; struct pw_properties *props;
char buffer[1024]; char buffer[1024];
@ -380,7 +386,7 @@ static struct client_data *client_new(struct server *s, int fd)
if (props == NULL) if (props == NULL)
goto exit; goto exit;
#ifndef __FreeBSD__ #if defined(__linux__)
len = sizeof(ucred); len = sizeof(ucred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) < 0) { if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) < 0) {
pw_log_warn("server %p: no peercred: %m", s); pw_log_warn("server %p: no peercred: %m", s);
@ -398,6 +404,17 @@ static struct client_data *client_new(struct server *s, int fd)
pw_properties_setf(props, PW_KEY_SEC_LABEL, "%.*s", pw_properties_setf(props, PW_KEY_SEC_LABEL, "%.*s",
(int)len, buffer); (int)len, buffer);
} }
#elif defined(__FreeBSD__)
len = sizeof(xucred);
if (getsockopt(fd, 0, LOCAL_PEERCRED, &xucred, &len) < 0) {
pw_log_warn("server %p: no peercred: %m", s);
} else {
#if __FreeBSD__ >= 13
pw_properties_setf(props, PW_KEY_SEC_PID, "%d", xucred.cr_pid);
#endif
pw_properties_setf(props, PW_KEY_SEC_UID, "%d", xucred.cr_uid);
pw_properties_setf(props, PW_KEY_SEC_GID, "%d", xucred.cr_gid);
}
#endif #endif
pw_properties_setf(props, PW_KEY_MODULE_ID, "%d", d->module->global->id); pw_properties_setf(props, PW_KEY_MODULE_ID, "%d", d->module->global->id);