Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/gnome-keyring/ChangeLog,v
retrieving revision 1.198
diff -u -p -r1.198 ChangeLog
--- ChangeLog	23 Aug 2006 19:52:35 -0000	1.198
+++ ChangeLog	28 Aug 2006 07:36:20 -0000
@@ -1,3 +1,8 @@
+2006-08-28  Julio M. Merino Vidal  <jmmv@NetBSD.org>
+
+	* gnome-keyring-daemon.c, gnome-keyring-daemon-io.c: Add support for
+	LOCAL_CREDS socket credentials so that the keyring works under NetBSD.
+
 2006-08-23  Dan Williams  <dcbw@redhat.com>
 
 	* gnome-keyring.c: (gnome_keyring_find_network_password_sync):
Index: gnome-keyring-daemon-io.c
===================================================================
RCS file: /cvs/gnome/gnome-keyring/gnome-keyring-daemon-io.c,v
retrieving revision 1.13
diff -u -p -r1.13 gnome-keyring-daemon-io.c
--- gnome-keyring-daemon-io.c	14 Apr 2006 13:20:37 -0000	1.13
+++ gnome-keyring-daemon-io.c	28 Aug 2006 07:36:20 -0000
@@ -93,6 +93,24 @@ static void gnome_keyring_client_state_m
 
 
 static gboolean
+set_local_creds (int fd, gboolean on)
+{
+  gboolean retval = TRUE;
+
+#if defined(LOCAL_CREDS) && !defined(HAVE_CMSGCRED)
+  int val = on ? 1 : 0;
+  if (setsockopt (fd, 0, LOCAL_CREDS, &val, sizeof (val)) < 0)
+    {
+      g_warning ("Unable to set LOCAL_CREDS socket option on fd %d\n", fd);
+      retval = FALSE;
+    }
+#endif
+
+  return retval;
+}
+
+
+static gboolean
 read_unix_socket_credentials (int fd,
 			      pid_t *pid,
 			      uid_t *uid)
@@ -101,27 +119,30 @@ read_unix_socket_credentials (int fd,
 	struct iovec iov;
 	char buf;
 	
-#ifdef HAVE_CMSGCRED 
+#if defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)
+	/* Prefer CMSGCRED over LOCAL_CREDS because the former provides the
+	 * remote PID. */
+#if defined(HAVE_CMSGCRED)
 	struct cmsgcred *cred;
+	const size_t cmsglen = CMSG_LEN (sizeof (struct cmsgcred));
+	const size_t cmsgspace = CMSG_SPACE (sizeof (struct cmsgcred));
+#else /* defined(LOCAL_CREDS) */
+	struct sockcred *cred;
+	const size_t cmsglen = CMSG_LEN (sizeof (struct sockcred));
+	const size_t cmsgspace = CMSG_SPACE (sizeof (struct sockcred));
+#endif
 	union {
 		struct cmsghdr hdr;
-		char cred[CMSG_SPACE (sizeof (struct cmsgcred))];
+		char cred[cmsgspace];
 	} cmsg;
 #endif
 	
 	*pid = 0;
 	*uid = 0;
 	
-#if defined(LOCAL_CREDS) && defined(HAVE_CMSGCRED) && !defined(__FreeBSD__)
-	/* Set the socket to receive credentials on the next message */
-	{
-		int on = 1;
-		if (setsockopt (fd, 0, LOCAL_CREDS, &on, sizeof (on)) < 0) {
-			g_warning ("Unable to set LOCAL_CREDS socket option\n");
-			return FALSE;
-		}
-	}
-#endif
+	/* If LOCAL_CREDS are used in this platform, they have already been
+	 * initialized by init_connection prior to sending of the credentials
+	 * byte we receive below. */
 	
 	iov.iov_base = &buf;
 	iov.iov_len = 1;
@@ -130,10 +151,10 @@ read_unix_socket_credentials (int fd,
 	msg.msg_iov = &iov;
 	msg.msg_iovlen = 1;
 	
-#ifdef HAVE_CMSGCRED
+#if defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)
 	memset (&cmsg, 0, sizeof (cmsg));
 	msg.msg_control = (caddr_t) &cmsg;
-	msg.msg_controllen = CMSG_SPACE (sizeof (struct cmsgcred));
+	msg.msg_controllen = cmsgspace;
 #endif
 
  again:
@@ -151,9 +172,8 @@ read_unix_socket_credentials (int fd,
 		return FALSE;
 	}
 
-#ifdef HAVE_CMSGCRED
-	if (cmsg.hdr.cmsg_len < CMSG_LEN (sizeof (struct cmsgcred)) ||
-	    cmsg.hdr.cmsg_type != SCM_CREDS) {
+#if defined(HAVE_CMSGCRED) || defined(LOCAL_CREDS)
+	if (cmsg.hdr.cmsg_len < cmsglen || cmsg.hdr.cmsg_type != SCM_CREDS) {
 		g_warning ("Message from recvmsg() was not SCM_CREDS\n");
 		return FALSE;
 	}
@@ -177,6 +197,11 @@ read_unix_socket_credentials (int fd,
 		cred = (struct cmsgcred *) CMSG_DATA (&cmsg);
 		*pid = cred->cmcred_pid;
 		*uid = cred->cmcred_euid;
+#elif defined(LOCAL_CREDS)
+		cred = (struct sockcred *) CMSG_DATA (&cmsg);
+		*pid = 0;
+		*uid = cred->sc_euid;
+		set_local_creds(fd, FALSE);
 #elif defined(HAVE_GETPEERUCRED)
 		ucred_t *uc = NULL;
 
@@ -639,6 +664,12 @@ create_master_socket (const char **path)
 	
 	if (listen (sock, 128) < 0) {
 		perror ("listen");
+		cleanup_socket_dir ();
+		return FALSE;
+	}
+
+        if (!set_local_creds (sock, TRUE)) {
+		close (sock);
 		cleanup_socket_dir ();
 		return FALSE;
 	}
Index: gnome-keyring-daemon.c
===================================================================
RCS file: /cvs/gnome/gnome-keyring/gnome-keyring-daemon.c,v
retrieving revision 1.37
diff -u -p -r1.37 gnome-keyring-daemon.c
--- gnome-keyring-daemon.c	11 Aug 2006 09:09:48 -0000	1.37
+++ gnome-keyring-daemon.c	28 Aug 2006 07:36:20 -0000
@@ -340,6 +340,7 @@ gnome_keyring_application_ref_new_from_p
 	app_ref = g_new0 (GnomeKeyringApplicationRef, 1);
 
 #if defined(__linux__) || defined(__FreeBSD__)
+	g_assert (pid > 0);
 	{
 		char *buffer;
 		int len;
