- message = NULL;
-
-#ifdef __linux__
- {
- GCredentials *credentials;
- struct ucred *ucred;
-
- if (level != SOL_SOCKET || type != SCM_CREDENTIALS)
- goto out;
-
- if (size != sizeof (struct ucred))
- {
- g_warning ("Expected a struct ucred (%" G_GSIZE_FORMAT " bytes) but "
- "got %" G_GSIZE_FORMAT " bytes of data",
- sizeof (struct ucred),
- size);
- goto out;
- }
-
- ucred = data;
-
- credentials = g_credentials_new ();
- g_credentials_set_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED, ucred);
- message = g_unix_credentials_message_new_with_credentials (credentials);
- g_object_unref (credentials);
- out:
- ;
- }
-#elif defined(__FreeBSD__)
- {
- GCredentials *credentials;
- struct cmsgcred *cred;
-
- if (level != SOL_SOCKET || type != SCM_CREDS)
- {
- goto out;
- }
- if (size < CMSG_LEN (sizeof *cred))
- {
- g_warning ("Expected a struct cmsgcred (%" G_GSIZE_FORMAT " bytes) but "
- "got %" G_GSIZE_FORMAT " bytes of data",
- CMSG_LEN (sizeof *cred),
- size);
- goto out;
- }
-
- cred = data;
-
- credentials = g_credentials_new ();
- g_credentials_set_native (credentials, G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED, cred);
- message = g_unix_credentials_message_new_with_credentials (credentials);
- g_object_unref (credentials);
- out:
- ;
- }
-#endif
+ if (level != SOL_SOCKET || type != g_unix_credentials_message_get_msg_type (NULL))
+ return NULL;
+
+ if (size != G_CREDENTIALS_NATIVE_SIZE)
+ {
+ g_warning ("Expected a credentials struct of %" G_GSIZE_FORMAT " bytes but "
+ "got %" G_GSIZE_FORMAT " bytes of data",
+ G_CREDENTIALS_NATIVE_SIZE, size);
+ return NULL;
+ }
+
+ credentials = g_credentials_new ();
+ g_credentials_set_native (credentials, G_CREDENTIALS_NATIVE_TYPE, data);
+
+ if (g_credentials_get_unix_user (credentials, NULL) == (uid_t) -1)
+ {
+ /* This happens on Linux if the remote side didn't pass the credentials */
+ g_object_unref (credentials);
+ return NULL;
+ }
+
+ message = g_unix_credentials_message_new_with_credentials (credentials);
+ g_object_unref (credentials);