[glib] gcredentials: add NetBSD support



commit afce39c228eacfecb45bd9de0f58e7ca767e36ed
Author: Patrick Welche <prlw1 cam ac uk>
Date:   Tue Apr 15 15:09:22 2014 +0100

    gcredentials: add NetBSD support
    
    https://bugzilla.gnome.org/show_bug.cgi?id=728256

 gio/gcredentials.c            |   28 ++++++++++++++++++++++++++++
 gio/gcredentialsprivate.h     |    8 ++++++++
 gio/gioenums.h                |    2 ++
 gio/gsocket.c                 |   17 +++++++++++++++++
 gio/gunixcredentialsmessage.c |    2 ++
 gio/tests/credentials.c       |    8 ++++++++
 6 files changed, 65 insertions(+), 0 deletions(-)
---
diff --git a/gio/gcredentials.c b/gio/gcredentials.c
index ccbd143..3c01027 100644
--- a/gio/gcredentials.c
+++ b/gio/gcredentials.c
@@ -59,6 +59,9 @@
  * credential type is a struct cmsgcred. This corresponds
  * to %G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED.
  *
+ * On NetBSD, the native credential type is a struct unpcbid.
+ * This corresponds to %G_CREDENTIALS_TYPE_NETBSD_UNPCBID.
+ *
  * On OpenBSD, the native credential type is a struct sockpeercred.
  * This corresponds to %G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED.
  *
@@ -84,6 +87,8 @@ struct _GCredentials
   struct ucred native;
 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
   struct cmsgcred native;
+#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
+  struct unpcbid native;
 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
   struct sockpeercred native;
 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
@@ -145,6 +150,10 @@ g_credentials_init (GCredentials *credentials)
   credentials->native.cmcred_pid  = getpid ();
   credentials->native.cmcred_euid = geteuid ();
   credentials->native.cmcred_gid  = getegid ();
+#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
+  credentials->native.unp_pid = getpid ();
+  credentials->native.unp_euid = geteuid ();
+  credentials->native.unp_egid = getegid ();
 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
   credentials->native.pid = getpid ();
   credentials->native.uid = geteuid ();
@@ -212,6 +221,15 @@ g_credentials_to_string (GCredentials *credentials)
     g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.cmcred_euid);
   if (credentials->native.cmcred_gid != -1)
     g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.cmcred_gid);
+#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
+  g_string_append (ret, "netbsd-unpcbid:");
+  if (credentials->native.unp_pid != -1)
+    g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.unp_pid);
+  if (credentials->native.unp_euid != -1)
+    g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.unp_euid);
+  if (credentials->native.unp_egid != -1)
+    g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.unp_egid);
+  ret->str[ret->len - 1] = '\0';
 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
   g_string_append (ret, "openbsd-sockpeercred:");
   if (credentials->native.pid != -1)
@@ -278,6 +296,9 @@ g_credentials_is_same_user (GCredentials  *credentials,
 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
   if (credentials->native.cmcred_euid == other_credentials->native.cmcred_euid)
     ret = TRUE;
+#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
+  if (credentials->native.unp_euid == other_credentials->native.unp_euid)
+    ret = TRUE;
 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
   if (credentials->native.uid == other_credentials->native.uid)
     ret = TRUE;
@@ -431,6 +452,8 @@ g_credentials_get_unix_user (GCredentials    *credentials,
   ret = credentials->native.uid;
 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
   ret = credentials->native.cmcred_euid;
+#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
+  ret = credentials->native.unp_euid;
 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
   ret = credentials->native.uid;
 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
@@ -475,6 +498,8 @@ g_credentials_get_unix_pid (GCredentials    *credentials,
   ret = credentials->native.pid;
 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
   ret = credentials->native.cmcred_pid;
+#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
+  ret = credentials->native.unp_pid;
 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
   ret = credentials->native.pid;
 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
@@ -526,6 +551,9 @@ g_credentials_set_unix_user (GCredentials    *credentials,
 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
   credentials->native.cmcred_euid = uid;
   ret = TRUE;
+#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
+  credentials->native.unp_euid = uid;
+  ret = TRUE;
 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
   credentials->native.uid = uid;
   ret = TRUE;
diff --git a/gio/gcredentialsprivate.h b/gio/gcredentialsprivate.h
index c747cc7..bdec7ae 100644
--- a/gio/gcredentialsprivate.h
+++ b/gio/gcredentialsprivate.h
@@ -39,6 +39,14 @@
 #define G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1
 #define G_CREDENTIALS_SPOOFING_SUPPORTED 1
 
+#elif defined(__NetBSD__)
+#define G_CREDENTIALS_SUPPORTED 1
+#define G_CREDENTIALS_USE_NETBSD_UNPCBID 1
+#define G_CREDENTIALS_NATIVE_TYPE G_CREDENTIALS_TYPE_NETBSD_UNPCBID
+#define G_CREDENTIALS_NATIVE_SIZE (sizeof (struct unpcbid))
+#define G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1
+#define G_CREDENTIALS_SPOOFING_SUPPORTED 1
+
 #elif defined(__OpenBSD__)
 #define G_CREDENTIALS_SUPPORTED 1
 #define G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED 1
diff --git a/gio/gioenums.h b/gio/gioenums.h
index 00d4c88..1ea7c42 100644
--- a/gio/gioenums.h
+++ b/gio/gioenums.h
@@ -1364,6 +1364,7 @@ typedef enum
  * @G_CREDENTIALS_TYPE_INVALID: Indicates an invalid native credential type.
  * @G_CREDENTIALS_TYPE_LINUX_UCRED: The native credentials type is a <type>struct ucred</type>.
  * @G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED: The native credentials type is a <type>struct cmsgcred</type>.
+ * @G_CREDENTIALS_TYPE_NETBSD_UNPCBID: The native credentials type is a <type>struct unpcbid</type>.
  * @G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED: The native credentials type is a <type>struct 
sockpeercred</type>. Added in 2.30.
  * @G_CREDENTIALS_TYPE_SOLARIS_UCRED: The native credentials type is a <type>ucred_t</type>. Added in 2.40.
  *
@@ -1376,6 +1377,7 @@ typedef enum
   G_CREDENTIALS_TYPE_INVALID,
   G_CREDENTIALS_TYPE_LINUX_UCRED,
   G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED,
+  G_CREDENTIALS_TYPE_NETBSD_UNPCBID,
   G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED,
   G_CREDENTIALS_TYPE_SOLARIS_UCRED
 } GCredentialsType;
diff --git a/gio/gsocket.c b/gio/gsocket.c
index a233260..f06e49b 100644
--- a/gio/gsocket.c
+++ b/gio/gsocket.c
@@ -4469,6 +4469,23 @@ g_socket_get_credentials (GSocket   *socket,
                                   native_creds_buf);
       }
   }
+#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
+  {
+    struct unpcbid cred;
+    socklen_t optlen = sizeof (cred);
+
+    if (getsockopt (socket->priv->fd,
+                    0,
+                    LOCAL_PEEREID,
+                    &cred,
+                    &optlen) == 0)
+      {
+        ret = g_credentials_new ();
+        g_credentials_set_native (ret,
+                                  G_CREDENTIALS_NATIVE_TYPE,
+                                  &cred);
+      }
+  }
 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
   {
     ucred_t *ucred = NULL;
diff --git a/gio/gunixcredentialsmessage.c b/gio/gunixcredentialsmessage.c
index 351aea7..77612f1 100644
--- a/gio/gunixcredentialsmessage.c
+++ b/gio/gunixcredentialsmessage.c
@@ -89,6 +89,8 @@ g_unix_credentials_message_get_msg_type (GSocketControlMessage *message)
   return SCM_CREDENTIALS;
 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
   return SCM_CREDS;
+#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
+  return SCM_CREDS;
 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
   return SCM_UCRED;
 #elif G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
diff --git a/gio/tests/credentials.c b/gio/tests/credentials.c
index afd73cc..6d76099 100644
--- a/gio/tests/credentials.c
+++ b/gio/tests/credentials.c
@@ -100,6 +100,14 @@ test_basic (void)
           g_assert_cmpuint (native->cmcred_euid, ==, geteuid ());
           g_assert_cmpuint (native->cmcred_pid, ==, getpid ());
         }
+#elif G_CREDENTIALS_USE_NETBSD_UNPCBID
+        {
+          struct unpcbid *native = g_credentials_get_native (creds,
+              G_CREDENTIALS_TYPE_NETBSD_UNPCBID);
+
+          g_assert_cmpuint (native->unp_euid, ==, geteuid ());
+          g_assert_cmpuint (native->unp_pid, ==, getpid ());
+        }
 #elif G_CREDENTIALS_USE_OPENBSD_SOCKPEERCRED
         {
           struct sockpeercred *native = g_credentials_get_native (creds,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]