ce77444
--- cups-1.2.10/cups/auth.c.af_unix-auth	2007-01-10 16:48:37.000000000 +0000
ce77444
+++ cups-1.2.10/cups/auth.c	2007-03-29 16:59:51.000000000 +0100
ce77444
@@ -26,6 +26,8 @@
ce77444
  * Contents:
ce77444
  *
ce77444
  *   cupsDoAuthentication() - Authenticate a request.
ce77444
+ *   cups_peercred_auth()   - Find out if SO_PEERCRED authentication
ce77444
+ *                            is possible
ce77444
  *   cups_local_auth()      - Get the local authorization certificate if
ce77444
  *                            available/applicable...
ce77444
  */
ce77444
@@ -40,7 +42,9 @@
ce77444
 #include <ctype.h>
ce77444
 #include <errno.h>
ce77444
 #include <fcntl.h>
ce77444
+#include <pwd.h>
ce77444
 #include <sys/stat.h>
ce77444
+#include <sys/types.h>
ce77444
 #if defined(WIN32) || defined(__EMX__)
ce77444
 #  include <io.h>
ce77444
 #else
ce77444
@@ -177,6 +181,76 @@
ce77444
   return (0);
ce77444
 }
ce77444
 
ce77444
+/*
ce77444
+ * 'cups_peercred_auth()'
ce77444
+ *                     - UNIX Domain Sockets authentication
ce77444
+ */
ce77444
+
ce77444
+static int				/* O - 0 if available, -1 if not */
ce77444
+cups_peercred_auth(http_t *http)	/* I - HTTP connection to server */
ce77444
+{
ce77444
+#ifdef SO_PEERCRED
ce77444
+  long buflen;
ce77444
+  char *buf, *newbuf;
ce77444
+  struct passwd pwbuf, *pwbufptr;
ce77444
+  int r;
ce77444
+
ce77444
+  if (http->hostaddr->addr.sa_family != AF_LOCAL)
ce77444
+    return (-1);
ce77444
+
ce77444
+ /*
ce77444
+  * Are we trying to authenticate as ourselves?  If not, SO_PEERCRED
ce77444
+  * is no use.
ce77444
+  */
ce77444
+  buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
ce77444
+  buf = NULL;
ce77444
+  do
ce77444
+  {
ce77444
+    newbuf = realloc (buf, buflen);
ce77444
+    if (newbuf == NULL)
ce77444
+    {
ce77444
+      free (buf);
ce77444
+      return (-1);
ce77444
+    }
ce77444
+
ce77444
+    buf = newbuf;
ce77444
+    r = getpwnam_r (cupsUser(), &pwbuf, buf, buflen, &pwbufptr);
ce77444
+    if (r != 0)
ce77444
+    {
ce77444
+      if (r == ERANGE)
ce77444
+      {
ce77444
+	buflen *= 2;
ce77444
+	continue;
ce77444
+      }
ce77444
+
ce77444
+      free (buf);
ce77444
+      return (-1);
ce77444
+    }
ce77444
+  }
ce77444
+  while (r != 0);
ce77444
+
ce77444
+  if (pwbuf.pw_uid != getuid())
ce77444
+  {
ce77444
+    free (buf);
ce77444
+    return (-1);
ce77444
+  }
ce77444
+
ce77444
+  free (buf);
ce77444
+
ce77444
+ /*
ce77444
+  * Set the authorization string and return...
ce77444
+  */
ce77444
+
ce77444
+  snprintf(http->authstring, sizeof(http->authstring), "SO_PEERCRED");
ce77444
+
ce77444
+  DEBUG_printf(("cups_peercred_auth: Returning authstring = \"%s\"\n",
ce77444
+		http->authstring));
ce77444
+
ce77444
+  return (0);
ce77444
+#else
ce77444
+  return (-1);
ce77444
+#endif /* SO_PEERCRED */
ce77444
+}
ce77444
 
ce77444
 /*
ce77444
  * 'cups_local_auth()' - Get the local authorization certificate if
ce77444
@@ -234,7 +308,7 @@
ce77444
   {
ce77444
     DEBUG_printf(("cups_local_auth: Unable to open file %s: %s\n",
ce77444
                   filename, strerror(errno)));
ce77444
-    return (-1);
ce77444
+    return cups_peercred_auth(http);
ce77444
   }
ce77444
 
ce77444
  /*
ce77444
--- cups-1.2.10/scheduler/auth.c.af_unix-auth	2006-09-12 14:58:39.000000000 +0100
ce77444
+++ cups-1.2.10/scheduler/auth.c	2007-03-29 17:03:53.000000000 +0100
ce77444
@@ -60,6 +60,9 @@
ce77444
 
ce77444
 #include "cupsd.h"
ce77444
 #include <grp.h>
ce77444
+#include <pwd.h>
ce77444
+#include <sys/socket.h>
ce77444
+#include <sys/types.h>
ce77444
 #ifdef HAVE_SHADOW_H
ce77444
 #  include <shadow.h>
ce77444
 #endif /* HAVE_SHADOW_H */
ce77444
@@ -79,6 +82,9 @@
ce77444
 #ifdef HAVE_MEMBERSHIP_H
ce77444
 #  include <membership.h>
ce77444
 #endif /* HAVE_MEMBERSHIP_H */
ce77444
+#if !defined(WIN32) && !defined(__EMX__)
ce77444
+#  include <unistd.h>
ce77444
+#endif
ce77444
 
ce77444
 
ce77444
 /*
ce77444
@@ -384,6 +390,61 @@
ce77444
                     "cupsdAuthorize: No authentication data provided.");
ce77444
     return;
ce77444
   }
ce77444
+#ifdef SO_PEERCRED
ce77444
+  else if (!strncmp(authorization, "SO_PEERCRED", 3) &&
ce77444
+	   con->http.hostaddr->addr.sa_family == AF_LOCAL)
ce77444
+  {
ce77444
+    long buflen;
ce77444
+    char *buf, *newbuf;
ce77444
+    struct passwd pwbuf, *pwbufptr;
ce77444
+    struct ucred u;
ce77444
+    socklen_t ulen = sizeof(u);
ce77444
+    int r;
ce77444
+
ce77444
+    if (getsockopt(con->http.fd, SOL_SOCKET, SO_PEERCRED, &u, &ulen) == -1)
ce77444
+    {
ce77444
+      cupsdLogMessage(CUPSD_LOG_ERROR,
ce77444
+		      "cupsdAuthorize: getsockopt failed for SO_PEERCRED");
ce77444
+      return;
ce77444
+    }
ce77444
+
ce77444
+    buflen = sysconf (_SC_GETPW_R_SIZE_MAX);
ce77444
+    buf = NULL;
ce77444
+    do
ce77444
+    {
ce77444
+      newbuf = realloc (buf, buflen);
ce77444
+      if (newbuf == NULL)
ce77444
+      {
ce77444
+	free (buf);
ce77444
+	return;
ce77444
+      }
ce77444
+
ce77444
+      buf = newbuf;
ce77444
+
ce77444
+      /* Look up which username the UID is for. */
ce77444
+      r = getpwuid_r (u.uid, &pwbuf, buf, buflen, &pwbufptr);
ce77444
+      if (r != 0)
ce77444
+      {
ce77444
+	if (r == ERANGE)
ce77444
+	{
ce77444
+	  buflen *= 2;
ce77444
+	  continue;
ce77444
+	}
ce77444
+
ce77444
+	cupsdLogMessage(CUPSD_LOG_ERROR,
ce77444
+			"cupsdAuthorize: getpwuid_r failed after SO_PEERCRED");
ce77444
+	free (buf);
ce77444
+	return;
ce77444
+      }
ce77444
+    }
ce77444
+    while (r != 0);
ce77444
+
ce77444
+    strlcpy(username, pwbuf.pw_name, sizeof(username));
ce77444
+    free (buf);
ce77444
+    cupsdLogMessage(CUPSD_LOG_DEBUG2,
ce77444
+		    "cupsdAuthorize: using SO_PEERCRED (uid=%d)", u.uid);
ce77444
+  }
ce77444
+#endif /* SO_PEERCRED */
ce77444
   else if (!strncmp(authorization, "Local", 5) &&
ce77444
            !strcasecmp(con->http.hostname, "localhost"))
ce77444
   {