1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
From: Emmanuel Bouthenot <kolter@openics.org>
Date: Wed, 16 Dec 2009 00:22:57 +0100
Subject: Support GNU/kFreeBSD
---
egg/egg-unix-credentials.c | 22 +++++++++++++++++++---
1 file changed, 19 insertions(+), 3 deletions(-)
diff --git a/egg/egg-unix-credentials.c b/egg/egg-unix-credentials.c
index 6366d29..1c248bd 100644
--- a/egg/egg-unix-credentials.c
+++ b/egg/egg-unix-credentials.c
@@ -48,6 +48,10 @@
#include <ucred.h>
#endif
+#if defined(__FreeBSD_kernel__)
+#include <sys/ucred.h>
+#endif
+
int
egg_unix_credentials_read (int sock, pid_t *pid, uid_t *uid)
{
@@ -56,7 +60,7 @@ egg_unix_credentials_read (int sock, pid_t *pid, uid_t *uid)
char buf;
int ret;
-#if defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)
+#if (defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)) && !defined(__FreeBSD_kernel__)
/* Prefer CMSGCRED over LOCAL_CREDS because the former provides the
* remote PID. */
#if defined(HAVE_CMSGCRED)
@@ -84,7 +88,7 @@ egg_unix_credentials_read (int sock, pid_t *pid, uid_t *uid)
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
-#if defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)
+#if (defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)) && !defined(__FreeBSD_kernel__)
memset (&cmsg, 0, sizeof (cmsg));
msg.msg_control = (caddr_t) &cmsg;
msg.msg_controllen = CMSG_SPACE(sizeof *cred);
@@ -108,7 +112,7 @@ egg_unix_credentials_read (int sock, pid_t *pid, uid_t *uid)
return -1;
}
-#if defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)
+#if (defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)) && !defined(__FreeBSD_kernel__)
if (cmsg.hdr.cmsg_len < CMSG_LEN (sizeof *cred) ||
cmsg.hdr.cmsg_type != SCM_CREDS) {
fprintf (stderr, "message from recvmsg() was not SCM_CREDS\n");
@@ -134,6 +138,18 @@ egg_unix_credentials_read (int sock, pid_t *pid, uid_t *uid)
cr_len, (int) sizeof (cr));
return -1;
}
+#elif defined(LOCAL_PEERCRED) /* GNU/kFreeBSD */
+ struct xucred cr;
+ socklen_t cr_len = sizeof(cr);
+ *pid = 0;
+
+ if (getsockopt (sock, 0, LOCAL_PEERCRED, &cr, &cr_len) == 0) {
+ *uid = cr.cr_uid;
+ } else {
+ fprintf (stderr, "failed to getsockopt() credentials, returned len %d/%d\n",
+ cr_len, (int) sizeof (cr));
+ return -1;
+ }
#elif defined(HAVE_CMSGCRED)
cred = (struct cmsgcred *) CMSG_DATA (&cmsg.hdr);
*pid = cred->cmcred_pid;
|