#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>
*
* 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.
*/
/**
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
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
}
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
#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,
{
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.");
{
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.");
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,
#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,
G_IO_ERROR,
g_data_input_stream_set_newline_type (dis, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
#ifdef G_OS_UNIX
- if (G_IS_UNIX_CONNECTION (auth->priv->stream) && g_unix_credentials_message_is_supported ())
+ if (G_IS_UNIX_CONNECTION (auth->priv->stream))
{
credentials = g_credentials_new ();
if (!g_unix_connection_send_credentials (G_UNIX_CONNECTION (auth->priv->stream),
/* first read the NUL-byte (TODO: read credentials if using a unix domain socket) */
#ifdef G_OS_UNIX
- if (G_IS_UNIX_CONNECTION (auth->priv->stream) && g_unix_credentials_message_is_supported ())
+ if (G_IS_UNIX_CONNECTION (auth->priv->stream))
{
local_error = NULL;
credentials = g_unix_connection_receive_credentials (G_UNIX_CONNECTION (auth->priv->stream),
cancellable,
&local_error);
- if (credentials == NULL)
+ if (credentials == NULL && !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
{
g_propagate_error (error, local_error);
goto out;
ret = NULL;
-#ifdef __linux__
+#if defined(__linux__) || defined(__OpenBSD__)
{
- struct ucred native_creds;
socklen_t optlen;
+#if defined(__linux__)
+ struct ucred native_creds;
optlen = sizeof (struct ucred);
+#elif defined(__OpenBSD__)
+ struct sockpeercred native_creds;
+ optlen = sizeof (struct sockpeercred);
+#endif
if (getsockopt (socket->priv->fd,
SOL_SOCKET,
SO_PEERCRED,
{
ret = g_credentials_new ();
g_credentials_set_native (ret,
+#if defined(__linux__)
G_CREDENTIALS_TYPE_LINUX_UCRED,
+#elif defined(__OpenBSD__)
+ G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED,
+#endif
&native_creds);
}
}
gboolean ret;
GOutputVector vector;
guchar nul_byte[1] = {'\0'};
+ gint num_messages;
g_return_val_if_fail (G_IS_UNIX_CONNECTION (connection), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
vector.buffer = &nul_byte;
vector.size = 1;
- scm = g_unix_credentials_message_new_with_credentials (credentials);
+
+ if (g_unix_credentials_message_is_supported ())
+ {
+ scm = g_unix_credentials_message_new_with_credentials (credentials);
+ num_messages = 1;
+ }
+ else
+ {
+ scm = NULL;
+ num_messages = 0;
+ }
+
g_object_get (connection, "socket", &socket, NULL);
if (g_socket_send_message (socket,
NULL, /* address */
&vector,
1,
&scm,
- 1,
+ num_messages,
G_SOCKET_MSG_NONE,
cancellable,
error) != 1)
out:
g_object_unref (socket);
- g_object_unref (scm);
+ if (scm != NULL)
+ g_object_unref (scm);
g_object_unref (credentials);
return ret;
}
goto out;
}
- if (nscm != 1)
+ if (g_unix_credentials_message_is_supported ())
{
- g_set_error (error,
- G_IO_ERROR,
- G_IO_ERROR_FAILED,
- _("Expecting 1 control message, got %d"),
- nscm);
- goto out;
- }
+ if (nscm != 1)
+ {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ _("Expecting 1 control message, got %d"),
+ nscm);
+ goto out;
+ }
+
+ if (!G_IS_UNIX_CREDENTIALS_MESSAGE (scms[0]))
+ {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ _("Unexpected type of ancillary data"));
+ goto out;
+ }
- if (!G_IS_UNIX_CREDENTIALS_MESSAGE (scms[0]))
+ ret = g_unix_credentials_message_get_credentials (G_UNIX_CREDENTIALS_MESSAGE (scms[0]));
+ g_object_ref (ret);
+ }
+ else
{
- g_set_error_literal (error,
- G_IO_ERROR,
- G_IO_ERROR_FAILED,
- _("Unexpected type of ancillary data"));
- goto out;
+ if (nscm != 0)
+ {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ _("Not expecting control message, but got %d"),
+ nscm);
+ goto out;
+ }
+ else
+ {
+ ret = g_socket_get_credentials (socket, error);
+ }
}
- ret = g_unix_credentials_message_get_credentials (G_UNIX_CREDENTIALS_MESSAGE (scms[0]));
- g_object_ref (ret);
-
out:
#ifdef __linux__