$OpenBSD: patch-gio_gcredentials_c,v 1.2 2011/05/28 07:32:06 ajacoutot Exp $

From 77f4f5aa02458e6c7f909dc6087a39d9b75d0ba6 Mon Sep 17 00:00:00 2001
From: Antoine Jacoutot <ajacoutot@openbsd.org>
Date: Fri, 27 May 2011 13:51:08 +0000
Subject: Add glib credentials support to OpenBSD.

--- gio/gcredentials.c.orig	Fri Nov 12 14:52:52 2010
+++ gio/gcredentials.c	Wed Dec  8 08:36:10 2010
@@ -27,6 +27,11 @@
 #include <sys/socket.h>
 #include <string.h>
 #endif
+#ifdef __OpenBSD__
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <string.h>
+#endif
 #include <stdlib.h>
 
 #include <gobject/gvaluecollector.h>
@@ -63,6 +68,9 @@
  *
  * On FreeBSD, the native credential type is a <type>struct cmsgcred</type>.
  * This corresponds to %G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED.
+ *
+ * On OpenBSD, the native credential type is a <type>struct sockpeercred</type>.
+ * This corresponds to %G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED.
  */
 
 /**
@@ -82,6 +90,8 @@ struct _GCredentials
   struct ucred native;
 #elif defined(__FreeBSD__)
   struct cmsgcred native;
+#elif defined(__OpenBSD__)
+  struct sockpeercred native;
 #else
 #ifdef __GNUC__
 #warning Please add GCredentials support for your OS
@@ -135,6 +145,10 @@ g_credentials_init (GCredentials *credentials)
   credentials->native.cmcred_pid  = getpid ();
   credentials->native.cmcred_euid = geteuid ();
   credentials->native.cmcred_gid  = getegid ();
+#elif defined(__OpenBSD__)
+  credentials->native.pid = getpid ();
+  credentials->native.uid = geteuid ();
+  credentials->native.gid = getegid ();
 #endif
 }
 
@@ -196,6 +210,16 @@ 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 defined(__OpenBSD__)
+  g_string_append (ret, "openbsd-sockpeercred:");
+  if (credentials->native.pid != -1)
+    g_string_append_printf (ret, "pid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.pid);
+  if (credentials->native.uid != -1)
+    g_string_append_printf (ret, "uid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.uid);
+  if (credentials->native.gid != -1)
+    g_string_append_printf (ret, "gid=%" G_GINT64_FORMAT ",", (gint64) credentials->native.gid);
+  if (ret->str[ret->len - 1] == ',')
+    ret->str[ret->len - 1] = '\0';
 #else
   g_string_append (ret, "unknown");
 #endif
@@ -239,6 +263,9 @@ g_credentials_is_same_user (GCredentials  *credentials
 #elif defined(__FreeBSD__)
   if (credentials->native.cmcred_euid == other_credentials->native.cmcred_euid)
     ret = TRUE;
+#elif defined(__OpenBSD__)
+  if (credentials->native.uid == other_credentials->native.uid)
+    ret = TRUE;
 #else
   g_set_error_literal (error,
                        G_IO_ERROR,
@@ -300,6 +327,17 @@ g_credentials_get_native (GCredentials     *credential
     {
       ret = &credentials->native;
     }
+#elif defined(__OpenBSD__)
+  if (native_type != G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED)
+    {
+      g_warning ("g_credentials_get_native: Trying to get credentials of type %d but only "
+                 "G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED is supported.",
+                 native_type);
+    }
+  else
+    {
+      ret = &credentials->native;
+    }
 #else
   g_warning ("g_credentials_get_native: Trying to get credentials but GLib has no support "
              "for the native credentials type. Please add support.");
@@ -350,6 +388,17 @@ g_credentials_set_native (GCredentials     *credential
     {
       memcpy (&credentials->native, native, sizeof (struct cmsgcred));
     }
+#elif defined(__OpenBSD__)
+  if (native_type != G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED)
+    {
+      g_warning ("g_credentials_set_native: Trying to set credentials of type %d "
+                 "but only G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED is supported.",
+                 native_type);
+    }
+  else
+    {
+      memcpy (&credentials->native, native, sizeof (struct sockpeercred));
+    }
 #else
   g_warning ("g_credentials_set_native: Trying to set credentials but GLib has no support "
              "for the native credentials type. Please add support.");
@@ -388,6 +437,8 @@ g_credentials_get_unix_user (GCredentials    *credenti
   ret = credentials->native.uid;
 #elif defined(__FreeBSD__)
   ret = credentials->native.cmcred_euid;
+#elif defined(__OpenBSD__)
+  ret = credentials->native.uid;
 #else
   ret = -1;
   g_set_error_literal (error,
@@ -433,6 +484,9 @@ g_credentials_set_unix_user (GCredentials    *credenti
   ret = TRUE;
 #elif defined(__FreeBSD__)
   credentials->native.cmcred_euid = uid;
+  ret = TRUE;
+#elif defined(__OpenBSD__)
+  credentials->native.uid = uid;
   ret = TRUE;
 #else
   g_set_error_literal (error,
