6fc5472
diff -up cups-1.7.1/config.h.in.lspp cups-1.7.1/config.h.in
6fc5472
--- cups-1.7.1/config.h.in.lspp	2014-01-08 18:43:27.282821790 +0100
6fc5472
+++ cups-1.7.1/config.h.in	2014-01-08 18:43:27.295821610 +0100
6fc5472
@@ -717,6 +717,13 @@ static __inline int _cups_abs(int i) { r
6fc5472
 #  endif /* __GNUC__ || __STDC_VERSION__ */
6fc5472
 #endif /* !HAVE_ABS && !abs */
6fc5472
 
6fc5472
+/*
6fc5472
+ * Are we trying to meet LSPP requirements?
6fc5472
+ */
6fc5472
+
6fc5472
+#undef WITH_LSPP
6fc5472
+
6fc5472
+
6fc5472
 #endif /* !_CUPS_CONFIG_H_ */
6fc5472
 
6fc5472
 /*
6fc5472
diff -up cups-1.7.1/config-scripts/cups-lspp.m4.lspp cups-1.7.1/config-scripts/cups-lspp.m4
6fc5472
--- cups-1.7.1/config-scripts/cups-lspp.m4.lspp	2014-01-08 18:43:27.295821610 +0100
6fc5472
+++ cups-1.7.1/config-scripts/cups-lspp.m4	2014-01-08 18:43:27.295821610 +0100
8936a5c
@@ -0,0 +1,36 @@
8936a5c
+dnl
8936a5c
+dnl   LSPP code for the Common UNIX Printing System (CUPS).
8936a5c
+dnl
8936a5c
+dnl   Copyright 2005-2006 by Hewlett-Packard Development Company, L.P.
8936a5c
+dnl
8936a5c
+dnl   This program is free software; you can redistribute it and/or modify
8936a5c
+dnl   it under the terms of the GNU General Public License as published by
8936a5c
+dnl   the Free Software Foundation; version 2.
8936a5c
+dnl
8936a5c
+dnl   This program is distributed in the hope that it will be useful, but
8936a5c
+dnl   WITHOUT ANY WARRANTY; without even the implied warranty of
8936a5c
+dnl   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8936a5c
+dnl   General Public License for more details.
8936a5c
+dnl
8936a5c
+dnl   You should have received a copy of the GNU General Public License
8936a5c
+dnl   along with this program; if not, write to the Free Software Foundation,
8936a5c
+dnl   Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA
8936a5c
+dnl
8936a5c
+
8936a5c
+dnl Are we trying to meet LSPP requirements
8936a5c
+AC_ARG_ENABLE(lspp, [  --enable-lspp           turn on auditing and label support, default=no])
8936a5c
+
8936a5c
+if test x"$enable_lspp" != xno; then
8936a5c
+    case "$uname" in
8936a5c
+        Linux)
8936a5c
+            AC_CHECK_LIB(audit,audit_log_user_message, [LIBAUDIT="-laudit" AC_SUBST(LIBAUDIT)])
8936a5c
+            AC_CHECK_HEADER(libaudit.h)
8936a5c
+            AC_CHECK_LIB(selinux,getpeercon, [LIBSELINUX="-lselinux" AC_SUBST(LIBSELINUX)])
8936a5c
+            AC_CHECK_HEADER(selinux/selinux.h)
8936a5c
+            AC_DEFINE(WITH_LSPP)
8936a5c
+            ;;
8936a5c
+        *)
8936a5c
+            # All others
8936a5c
+            ;;
8936a5c
+    esac
8936a5c
+fi
6fc5472
diff -up cups-1.7.1/configure.in.lspp cups-1.7.1/configure.in
6fc5472
--- cups-1.7.1/configure.in.lspp	2014-01-08 18:43:27.228822537 +0100
6fc5472
+++ cups-1.7.1/configure.in	2014-01-08 18:43:27.296821596 +0100
47ca1c0
@@ -37,6 +37,8 @@ sinclude(config-scripts/cups-systemd.m4)
47ca1c0
 sinclude(config-scripts/cups-defaults.m4)
8936a5c
 sinclude(config-scripts/cups-scripting.m4)
8936a5c
 
8936a5c
+sinclude(config-scripts/cups-lspp.m4)
8936a5c
+
8936a5c
 INSTALL_LANGUAGES=""
8936a5c
 UNINSTALL_LANGUAGES=""
8936a5c
 LANGFILES=""
6fc5472
diff -up cups-1.7.1/filter/common.c.lspp cups-1.7.1/filter/common.c
6fc5472
--- cups-1.7.1/filter/common.c.lspp	2013-05-29 13:51:34.000000000 +0200
6fc5472
+++ cups-1.7.1/filter/common.c	2014-01-08 18:43:27.296821596 +0100
8936a5c
@@ -30,6 +30,12 @@
8936a5c
  * Include necessary headers...
8936a5c
  */
8936a5c
 
8936a5c
+#include "config.h"
8936a5c
+#ifdef WITH_LSPP
8936a5c
+#define _GNU_SOURCE
8936a5c
+#include <string.h>
8936a5c
+#endif /* WITH_LSPP */
8936a5c
+
8936a5c
 #include "common.h"
8936a5c
 #include <locale.h>
8936a5c
 
8936a5c
@@ -312,6 +318,18 @@ WriteLabelProlog(const char *label,	/* I
8936a5c
 {
8936a5c
   const char	*classification;	/* CLASSIFICATION environment variable */
8936a5c
   const char	*ptr;			/* Temporary string pointer */
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  int           i,                      /* counter */
8936a5c
+                n,                      /* counter */
8936a5c
+                lines,                  /* number of lines needed */
8936a5c
+                line_len,               /* index into tmp_label */
8936a5c
+                label_len,              /* length of the label in characters */
8936a5c
+                label_index,            /* index into the label */
8936a5c
+                longest,                /* length of the longest line */
8936a5c
+                longest_line,           /* index to the longest line */
8936a5c
+                max_width;              /* maximum width in characters */
8936a5c
+  char          **wrapped_label;        /* label with line breaks */
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
8936a5c
 
8936a5c
  /*
8936a5c
@@ -334,6 +352,124 @@ WriteLabelProlog(const char *label,	/* I
8936a5c
     return;
8936a5c
   }
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  if (strncmp(classification, "LSPP:", 5) == 0 && label == NULL)
8936a5c
+  {
8936a5c
+   /*
8936a5c
+    * Based on the 12pt fixed width font below determine the max_width
8936a5c
+    */
8936a5c
+    max_width = width / 8;
8936a5c
+    longest_line = 0;
8936a5c
+    longest = 0;
8936a5c
+    classification += 5; // Skip the "LSPP:"
8936a5c
+    label_len = strlen(classification);
8936a5c
+
8936a5c
+    if (label_len > max_width)
8936a5c
+    {
8936a5c
+      lines = 1 + (int)(label_len / max_width);
8936a5c
+      line_len = (int)(label_len / lines);
85d072c
+      wrapped_label = malloc(sizeof(*wrapped_label) * lines);
8936a5c
+      label_index = i = n = 0;
8936a5c
+      while (classification[label_index])
8936a5c
+      {
8936a5c
+        if ((label_index + line_len) > label_len)
8936a5c
+          break;
8936a5c
+        switch (classification[label_index + line_len + i])
8936a5c
+        {
8936a5c
+          case ':':
8936a5c
+          case ',':
8936a5c
+          case '-':
8936a5c
+            i++;
8936a5c
+            wrapped_label[n++] = strndup(&classification[label_index], (line_len + i));
8936a5c
+            label_index += line_len + i;
8936a5c
+            i = 0;
8936a5c
+            break;
8936a5c
+          default:
8936a5c
+            i++;
8936a5c
+            break;
8936a5c
+        }
8936a5c
+        if ((i + line_len) == max_width)
8936a5c
+        {
8936a5c
+          wrapped_label[n++] = strndup(&(classification[label_index]), (line_len + i));
8936a5c
+          label_index = label_index + line_len + i;
8936a5c
+          i = 0;
8936a5c
+        }
8936a5c
+      }
8936a5c
+      wrapped_label[n] = strndup(&classification[label_index], label_len - label_index);
8936a5c
+    }
8936a5c
+    else
8936a5c
+    {
8936a5c
+      lines = 1;
85d072c
+      wrapped_label = malloc(sizeof(*wrapped_label));
8936a5c
+      wrapped_label[0] = (char*)classification;
8936a5c
+    }
8936a5c
+
8936a5c
+    for (n = 0; n < lines; n++ )
8936a5c
+    {
8936a5c
+      printf("userdict/ESPp%c(", ('a' + n));
8936a5c
+      for (ptr = wrapped_label[n], i = 0; *ptr; ptr ++, i++)
8936a5c
+        if (*ptr < 32 || *ptr > 126)
8936a5c
+          printf("\\%03o", *ptr);
8936a5c
+        else
8936a5c
+        {
8936a5c
+          if (*ptr == '(' || *ptr == ')' || *ptr == '\\')
8936a5c
+            putchar('\\');
e7076f3
+
8936a5c
+          printf("%c", *ptr);
8936a5c
+        }
8936a5c
+      if (i > longest)
8936a5c
+      {
8936a5c
+        longest = i;
8936a5c
+        longest_line = n;
8936a5c
+      }
8936a5c
+      printf(")put\n");
8936a5c
+    }
e7076f3
+
8936a5c
+   /*
8936a5c
+    * For LSPP use a fixed width font so that line wrapping can be calculated
8936a5c
+    */
75d0e82
+
8936a5c
+    puts("userdict/ESPlf /Nimbus-Mono findfont 12 scalefont put");
75d0e82
+
8936a5c
+   /*
8936a5c
+    * Finally, the procedure to write the labels on the page...
8936a5c
+    */
75d0e82
+
8936a5c
+    printf("userdict/ESPwl{\n"
8936a5c
+           "  ESPlf setfont\n");
8936a5c
+    printf("  ESPp%c stringwidth pop dup 12 add exch -0.5 mul %.0f add\n ",
8936a5c
+           'a' + longest_line, width * 0.5f);
8936a5c
+    for (n = 1; n < lines; n++)
8936a5c
+      printf(" dup");
8936a5c
+    printf("\n  1 setgray\n");
8936a5c
+    printf("  dup 6 sub %.0f %d index %.0f ESPrf\n",
8936a5c
+           (bottom - 2.0), (2 + lines), 6.0 + (16.0 * lines));
8936a5c
+    printf("  dup 6 sub %.0f %d index %.0f ESPrf\n",
8936a5c
+           (top - 6.0 - (16.0 * lines)), (2 + lines), 4.0 + (16.0 * lines));
8936a5c
+    printf("  0 setgray\n");
8936a5c
+    printf("  dup 6 sub %.0f %d index %.0f ESPrs\n",
8936a5c
+           (bottom - 2.0), (2 + lines), 6.0 + (16.0 * lines));
8936a5c
+    printf("  dup 6 sub %.0f %d index %.0f ESPrs\n",
8936a5c
+           (top - 6.0 - (16.0 * lines)), (2 + lines), 4.0 + (16.0 * lines));
8936a5c
+    for (n = 0; n < lines; n ++)
8936a5c
+    {
8936a5c
+      printf("  dup %.0f moveto ESPp%c show\n",
8936a5c
+             bottom + 6.0 + ((lines - (n+1)) * 16.0), 'a' + n);
8936a5c
+      printf("  %.0f moveto ESPp%c show\n", top + 2.0 - ((n + 1) * 16.0), 'a' + n);
8936a5c
+    }
8936a5c
+    printf("  pop\n"
8936a5c
+           "}bind put\n");
22cc213
+
8936a5c
+   /*
8936a5c
+    * Do some clean up at the end of the LSPP special case
8936a5c
+    */
8936a5c
+    free(wrapped_label);
75d0e82
+
8936a5c
+  }
8936a5c
+  else
8936a5c
+  {
8936a5c
+#endif /* !WITH_LSPP */
8936a5c
+  
8936a5c
  /*
8936a5c
   * Set the classification + page label string...
8936a5c
   */
8936a5c
@@ -414,7 +550,10 @@ WriteLabelProlog(const char *label,	/* I
8936a5c
   printf("  %.0f moveto ESPpl show\n", top - 14.0);
8936a5c
   puts("pop");
8936a5c
   puts("}bind put");
8936a5c
+  }
8936a5c
+#ifdef WITH_LSPP
8936a5c
 }
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
8936a5c
 
8936a5c
 /*
6fc5472
diff -up cups-1.7.1/filter/pstops.c.lspp cups-1.7.1/filter/pstops.c
6fc5472
--- cups-1.7.1/filter/pstops.c.lspp	2013-05-29 13:51:34.000000000 +0200
6fc5472
+++ cups-1.7.1/filter/pstops.c	2014-01-08 18:43:27.297821582 +0100
5b01ebd
@@ -3203,6 +3203,18 @@ write_label_prolog(pstops_doc_t *doc,	/*
92b094f
 {
92b094f
   const char	*classification;	/* CLASSIFICATION environment variable */
92b094f
   const char	*ptr;			/* Temporary string pointer */
92b094f
+#ifdef WITH_LSPP
92b094f
+  int           i,                      /* counter */
92b094f
+                n,                      /* counter */
92b094f
+                lines,                  /* number of lines needed */
92b094f
+                line_len,               /* index into tmp_label */
92b094f
+                label_len,              /* length of the label in characters */
92b094f
+                label_index,            /* index into the label */
92b094f
+                longest,                /* length of the longest line */
92b094f
+                longest_line,           /* index to the longest line */
92b094f
+                max_width;              /* maximum width in characters */
92b094f
+  char          **wrapped_label;        /* label with line breaks */
92b094f
+#endif /* WITH_LSPP */
92b094f
 
92b094f
 
92b094f
  /*
5b01ebd
@@ -3225,6 +3237,124 @@ write_label_prolog(pstops_doc_t *doc,	/*
92b094f
     return;
92b094f
   }
92b094f
 
92b094f
+#ifdef WITH_LSPP
92b094f
+  if (strncmp(classification, "LSPP:", 5) == 0 && label == NULL)
92b094f
+  {
92b094f
+   /*
92b094f
+    * Based on the 12pt fixed width font below determine the max_width
92b094f
+    */
92b094f
+    max_width = width / 8;
92b094f
+    longest_line = 0;
92b094f
+    longest = 0;
92b094f
+    classification += 5; // Skip the "LSPP:"
92b094f
+    label_len = strlen(classification);
92b094f
+
92b094f
+    if (label_len > max_width)
92b094f
+    {
92b094f
+      lines = 1 + (int)(label_len / max_width);
92b094f
+      line_len = (int)(label_len / lines);
85d072c
+      wrapped_label = malloc(sizeof(*wrapped_label) * lines);
92b094f
+      label_index = i = n = 0;
92b094f
+      while (classification[label_index])
92b094f
+      {
92b094f
+        if ((label_index + line_len) > label_len)
92b094f
+          break;
92b094f
+        switch (classification[label_index + line_len + i])
92b094f
+        {
92b094f
+          case ':':
92b094f
+          case ',':
92b094f
+          case '-':
92b094f
+            i++;
92b094f
+            wrapped_label[n++] = strndup(&classification[label_index], (line_len + i));
92b094f
+            label_index += line_len + i;
92b094f
+            i = 0;
92b094f
+            break;
92b094f
+          default:
92b094f
+            i++;
92b094f
+            break;
92b094f
+        }
92b094f
+        if ((i + line_len) == max_width)
92b094f
+        {
92b094f
+          wrapped_label[n++] = strndup(&(classification[label_index]), (line_len + i));
92b094f
+          label_index = label_index + line_len + i;
92b094f
+          i = 0;
92b094f
+        }
92b094f
+      }
92b094f
+      wrapped_label[n] = strndup(&classification[label_index], label_len - label_index);
92b094f
+    }
92b094f
+    else
92b094f
+    {
92b094f
+      lines = 1;
85d072c
+      wrapped_label = malloc(sizeof(*wrapped_label));
92b094f
+      wrapped_label[0] = (char*)classification;
92b094f
+    }
92b094f
+
92b094f
+    for (n = 0; n < lines; n++ )
92b094f
+    {
92b094f
+      printf("userdict/ESPp%c(", ('a' + n));
92b094f
+      for (ptr = wrapped_label[n], i = 0; *ptr; ptr ++, i++)
92b094f
+        if (*ptr < 32 || *ptr > 126)
92b094f
+          printf("\\%03o", *ptr);
92b094f
+        else
92b094f
+        {
92b094f
+          if (*ptr == '(' || *ptr == ')' || *ptr == '\\')
92b094f
+            putchar('\\');
92b094f
+
92b094f
+          printf("%c", *ptr);
92b094f
+        }
92b094f
+      if (i > longest)
92b094f
+      {
92b094f
+        longest = i;
92b094f
+        longest_line = n;
92b094f
+      }
92b094f
+      printf(")put\n");
92b094f
+    }
92b094f
+
92b094f
+   /*
92b094f
+    * For LSPP use a fixed width font so that line wrapping can be calculated
92b094f
+    */
92b094f
+
92b094f
+    puts("userdict/ESPlf /Nimbus-Mono findfont 12 scalefont put");
92b094f
+
92b094f
+   /*
92b094f
+    * Finally, the procedure to write the labels on the page...
92b094f
+    */
92b094f
+
92b094f
+    printf("userdict/ESPwl{\n"
92b094f
+           "  ESPlf setfont\n");
92b094f
+    printf("  ESPp%c stringwidth pop dup 12 add exch -0.5 mul %.0f add\n ",
92b094f
+           'a' + longest_line, width * 0.5f);
92b094f
+    for (n = 1; n < lines; n++)
92b094f
+      printf(" dup");
92b094f
+    printf("\n  1 setgray\n");
92b094f
+    printf("  dup 6 sub %.0f %d index %.0f ESPrf\n",
92b094f
+           (bottom - 2.0), (2 + lines), 6.0 + (16.0 * lines));
92b094f
+    printf("  dup 6 sub %.0f %d index %.0f ESPrf\n",
92b094f
+           (top - 6.0 - (16.0 * lines)), (2 + lines), 4.0 + (16.0 * lines));
92b094f
+    printf("  0 setgray\n");
92b094f
+    printf("  dup 6 sub %.0f %d index %.0f ESPrs\n",
92b094f
+           (bottom - 2.0), (2 + lines), 6.0 + (16.0 * lines));
92b094f
+    printf("  dup 6 sub %.0f %d index %.0f ESPrs\n",
92b094f
+           (top - 6.0 - (16.0 * lines)), (2 + lines), 4.0 + (16.0 * lines));
92b094f
+    for (n = 0; n < lines; n ++)
92b094f
+    {
92b094f
+      printf("  dup %.0f moveto ESPp%c show\n",
92b094f
+             bottom + 6.0 + ((lines - (n+1)) * 16.0), 'a' + n);
92b094f
+      printf("  %.0f moveto ESPp%c show\n", top + 2.0 - ((n + 1) * 16.0), 'a' + n);
92b094f
+    }
92b094f
+    printf("  pop\n"
92b094f
+           "}bind put\n");
92b094f
+
92b094f
+   /*
92b094f
+    * Do some clean up at the end of the LSPP special case
92b094f
+    */
92b094f
+    free(wrapped_label);
92b094f
+
92b094f
+  }
92b094f
+  else
92b094f
+  {
92b094f
+#endif /* !WITH_LSPP */
92b094f
+
92b094f
  /*
92b094f
   * Set the classification + page label string...
92b094f
   */
5b01ebd
@@ -3303,7 +3433,10 @@ write_label_prolog(pstops_doc_t *doc,	/*
92b094f
   doc_printf(doc, "  %.0f moveto ESPpl show\n", top - 14.0);
92b094f
   doc_puts(doc, "pop\n");
92b094f
   doc_puts(doc, "}bind put\n");
92b094f
+  }
92b094f
+#ifdef WITH_LSPP
92b094f
 }
92b094f
+#endif /* WITH_LSPP */
92b094f
 
92b094f
 
92b094f
 /*
6fc5472
diff -up cups-1.7.1/Makedefs.in.lspp cups-1.7.1/Makedefs.in
6fc5472
--- cups-1.7.1/Makedefs.in.lspp	2014-01-08 18:43:27.282821790 +0100
6fc5472
+++ cups-1.7.1/Makedefs.in	2014-01-08 18:43:27.295821610 +0100
6fc5472
@@ -149,7 +149,7 @@ LDFLAGS		=	-L../cgi-bin -L../cups -L../f
6fc5472
 			@LDFLAGS@ @RELROFLAGS@ @PIEFLAGS@ $(OPTIM)
6fc5472
 LINKCUPS	=	@LINKCUPS@ $(LIBGSSAPI) $(SSLLIBS) $(DNSSDLIBS) $(LIBZ)
6fc5472
 LINKCUPSIMAGE	=	@LINKCUPSIMAGE@
6fc5472
-LIBS		=	$(LINKCUPS) $(COMMONLIBS)
6fc5472
+LIBS		=	$(LINKCUPS) $(COMMONLIBS) @LIBAUDIT@ @LIBSELINUX@
6fc5472
 OPTIM		=	@OPTIM@
6fc5472
 OPTIONS		=
6fc5472
 PAMLIBS		=	@PAMLIBS@
6fc5472
diff -up cups-1.7.1/scheduler/client.c.lspp cups-1.7.1/scheduler/client.c
6fc5472
--- cups-1.7.1/scheduler/client.c.lspp	2013-09-27 16:09:25.000000000 +0200
6fc5472
+++ cups-1.7.1/scheduler/client.c	2014-01-08 18:43:27.298821568 +0100
5b01ebd
@@ -49,6 +50,7 @@
8936a5c
 
8936a5c
 #include "cupsd.h"
8936a5c
 
cb06f24
+#define _GNU_SOURCE
5b01ebd
 #ifdef __APPLE__
5b01ebd
 #  include <libproc.h>
5b01ebd
 #endif /* __APPLE__ */
5b01ebd
@@ -56,6 +58,11 @@
cb06f24
 #  include <tcpd.h>
cb06f24
 #endif /* HAVE_TCPD_H */
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+#include <selinux/selinux.h>
8936a5c
+#include <selinux/context.h>
8936a5c
+#include <fcntl.h>
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
8936a5c
 /*
47ca1c0
  * Local globals...
5b01ebd
@@ -378,6 +385,57 @@ cupsdAcceptClient(cupsd_listener_t *lis)
8936a5c
   }
cb06f24
 #endif /* HAVE_TCPD_H */
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  if (is_lspp_config())
8936a5c
+  {
8936a5c
+    struct ucred cr;
8936a5c
+    unsigned int cl=sizeof(cr);
e7076f3
+
8936a5c
+    if (getsockopt(con->http.fd, SOL_SOCKET, SO_PEERCRED, &cr, &cl) == 0)
8936a5c
+    {
8936a5c
+     /*
8936a5c
+      * client_pid_to_auid() can be racey
8936a5c
+      * In this case the pid is based on a socket connected to the client
8936a5c
+      */
8936a5c
+      if ((con->auid = client_pid_to_auid(cr.pid)) == -1)
8936a5c
+      {
8936a5c
+        close(con->http.fd);
8936a5c
+        cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdAcceptClient: "
8936a5c
+                        "unable to determine client auid for client pid=%d", cr.pid);
8936a5c
+        free(con);
8936a5c
+        return;
8936a5c
+      }
8936a5c
+      cupsdLogMessage(CUPSD_LOG_INFO, "cupsdAcceptClient: peer's pid=%d, uid=%d, gid=%d, auid=%d",
8936a5c
+                      cr.pid, cr.uid, cr.gid, con->auid);
8936a5c
+    }
8936a5c
+    else
8936a5c
+    {
8936a5c
+      close(con->http.fd);
8936a5c
+      cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdAcceptClient: getsockopt() failed");
8936a5c
+      free(con);
8936a5c
+      return; 
8936a5c
+    }
e7076f3
+
8936a5c
+   /*
8936a5c
+    * get the context of the peer connection
8936a5c
+    */
8936a5c
+    if (getpeercon(con->http.fd, &con->scon))
8936a5c
+    {
8936a5c
+      close(con->http.fd);
8936a5c
+      cupsdLogMessage(CUPSD_LOG_ERROR, "cupsdAcceptClient: getpeercon() failed");
8936a5c
+      free(con);
8936a5c
+      return; 
8936a5c
+    }
e7076f3
+
8936a5c
+    cupsdLogMessage(CUPSD_LOG_INFO, "cupsdAcceptClient: client context=%s", con->scon);
8936a5c
+  }
8936a5c
+  else
8936a5c
+  {
8936a5c
+    cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdAcceptClient: skipping getpeercon()");
8936a5c
+    cupsdSetString(&con->scon, UNKNOWN_SL);
8936a5c
+  }
8936a5c
+#endif /* WITH_LSPP */
8936a5c
+
4743e10
 #ifdef AF_LOCAL
4743e10
   if (con->http.hostaddr->addr.sa_family == AF_LOCAL)
5b01ebd
   {
7632e7a
@@ -714,6 +772,13 @@ cupsdReadClient(cupsd_client_t *con)	/*
8936a5c
   mime_type_t		*type;		/* MIME type of file */
8936a5c
   cupsd_printer_t	*p;		/* Printer */
8936a5c
   static unsigned	request_id = 0;	/* Request ID for temp files */
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  security_context_t	spoolcon;	/* context of the job file */
8936a5c
+  context_t		clicon;		/* contex_t container for con->scon */
8936a5c
+  context_t		tmpcon;		/* temp context to swap the level */
8936a5c
+  char			*clirange;	/* SELinux sensitivity range */
8936a5c
+  char			*cliclearance;	/* SELinux low end clearance */
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
8936a5c
 
8936a5c
   status = HTTP_CONTINUE;
7632e7a
@@ -2231,6 +2296,67 @@ cupsdReadClient(cupsd_client_t *con)	/*
8936a5c
 	    fchmod(con->file, 0640);
8936a5c
 	    fchown(con->file, RunUser, Group);
8936a5c
             fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
8936a5c
+#ifdef WITH_LSPP
8936a5c
+	    if (strncmp(con->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) != 0)
8936a5c
+	    {
8936a5c
+	      if (getfilecon(con->filename, &spoolcon) == -1)
8936a5c
+	      {
8936a5c
+		cupsdSendError(con, HTTP_SERVER_ERROR, CUPSD_AUTH_NONE);
8936a5c
+		return (cupsdCloseClient(con));
8936a5c
+	      }
8936a5c
+	      clicon = context_new(con->scon);
8936a5c
+	      tmpcon = context_new(spoolcon);
8936a5c
+	      freecon(spoolcon);
8936a5c
+	      if (!clicon || !tmpcon)
8936a5c
+	      {
8936a5c
+		cupsdSendError(con, HTTP_SERVER_ERROR, CUPSD_AUTH_NONE);
8936a5c
+		if (clicon)
8936a5c
+		  context_free(clicon);
8936a5c
+		if (tmpcon)
8936a5c
+		  context_free(tmpcon);
8936a5c
+		return (cupsdCloseClient(con));
8936a5c
+	      }
8936a5c
+	      clirange = context_range_get(clicon);
8936a5c
+	      if (clirange)
8936a5c
+	      {
8936a5c
+		clirange = strdup(clirange);
8936a5c
+		if ((cliclearance = strtok(clirange, "-")) != NULL)
8936a5c
+		{
8936a5c
+		  if (context_range_set(tmpcon, cliclearance) == -1)
8936a5c
+		  {
8936a5c
+		    cupsdSendError(con, HTTP_SERVER_ERROR, CUPSD_AUTH_NONE);
8936a5c
+		    free(clirange);
8936a5c
+		    context_free(tmpcon);
8936a5c
+		    context_free(clicon);
8936a5c
+		    return (cupsdCloseClient(con));
8936a5c
+		  }
8936a5c
+		}
8936a5c
+		else
8936a5c
+		{
8936a5c
+		  if (context_range_set(tmpcon, (context_range_get(clicon))) == -1)
8936a5c
+		  {
8936a5c
+		    cupsdSendError(con, HTTP_SERVER_ERROR, CUPSD_AUTH_NONE);
8936a5c
+		    free(clirange);
8936a5c
+		    context_free(tmpcon);
8936a5c
+		    context_free(clicon);
8936a5c
+		    return (cupsdCloseClient(con));
8936a5c
+		  }
8936a5c
+		}
8936a5c
+		free(clirange);
8936a5c
+	      }
8936a5c
+	      if (setfilecon(con->filename, context_str(tmpcon)) == -1)
8936a5c
+	      {
8936a5c
+		cupsdSendError(con, HTTP_SERVER_ERROR, CUPSD_AUTH_NONE);
8936a5c
+		context_free(tmpcon);
8936a5c
+		context_free(clicon);
8936a5c
+		return (cupsdCloseClient(con));
8936a5c
+	      }
8936a5c
+	      cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdReadClient: %s set to %s", 
8936a5c
+			      con->filename, context_str(tmpcon));
8936a5c
+	      context_free(tmpcon);
8936a5c
+	      context_free(clicon);
8936a5c
+	    }
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 	  }
8936a5c
 
5b01ebd
 	  if (con->http.state != HTTP_STATE_POST_SEND)
7632e7a
@@ -3727,6 +3853,49 @@ is_path_absolute(const char *path)	/* I
47ca1c0
   return (1);
47ca1c0
 }
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+/*
8936a5c
+ * 'client_pid_to_auid()' - Using the client's pid, read /proc and determine the loginuid.
8936a5c
+ */
e7076f3
+
8936a5c
+uid_t client_pid_to_auid(pid_t clipid)
8936a5c
+{
8936a5c
+  uid_t uid;
8936a5c
+  int len, in;
8936a5c
+  char buf[16] = {0};
8936a5c
+  char fname[32] = {0};
e7076f3
+
e7076f3
+
8936a5c
+ /*
8936a5c
+  * Hopefully this pid is still the one we are interested in.
8936a5c
+  */
8936a5c
+  snprintf(fname, 32, "/proc/%d/loginuid", clipid);
8936a5c
+  in = open(fname, O_NOFOLLOW|O_RDONLY);
e7076f3
+
8936a5c
+  if (in < 0)
8936a5c
+    return -1;
e7076f3
+
8936a5c
+  errno = 0;
75d0e82
+
8936a5c
+  do {
8936a5c
+    len = read(in, buf, sizeof(buf));
8936a5c
+  } while (len < 0 && errno == EINTR);
22cc213
+
8936a5c
+  close(in);
38627a4
+
8936a5c
+  if (len < 0 || len >= sizeof(buf))
8936a5c
+    return -1;
38627a4
+
8936a5c
+  errno = 0;
8936a5c
+  buf[len] = 0;
8936a5c
+  uid = strtol(buf, 0, 10);
75d0e82
+
8936a5c
+  if (errno != 0)
8936a5c
+    return -1;
8936a5c
+  else
8936a5c
+    return uid;
8936a5c
+}
8936a5c
+#endif /* WITH_LSPP */
47ca1c0
 
8936a5c
 /*
8936a5c
  * 'pipe_command()' - Pipe the output of a command to the remote client.
6fc5472
diff -up cups-1.7.1/scheduler/client.h.lspp cups-1.7.1/scheduler/client.h
6fc5472
--- cups-1.7.1/scheduler/client.h.lspp	2014-01-08 18:43:27.231822495 +0100
6fc5472
+++ cups-1.7.1/scheduler/client.h	2014-01-08 18:43:27.298821568 +0100
cb06f24
@@ -18,6 +18,13 @@
8936a5c
 #endif /* HAVE_AUTHORIZATION_H */
8936a5c
 
cb06f24
 
8936a5c
+/* Copyright (C) 2005 Trusted Computer Solutions, Inc. */
8936a5c
+/* (c) Copyright 2005-2006 Hewlett-Packard Development Company, L.P. */
9ad376b
+
8936a5c
+#ifdef WITH_LSPP
8936a5c
+#include <selinux/selinux.h>
8936a5c
+#endif /* WITH_LSPP */
d042370
+
8936a5c
 /*
8936a5c
  * HTTP client structure...
8936a5c
  */
7632e7a
@@ -65,6 +72,10 @@ struct cupsd_client_s
8936a5c
 #ifdef HAVE_AUTHORIZATION_H
8936a5c
   AuthorizationRef	authref;	/* Authorization ref */
8936a5c
 #endif /* HAVE_AUTHORIZATION_H */
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  security_context_t	scon;		/* Security context of connection */
8936a5c
+  uid_t			auid;		/* Audit loginuid of the client */
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 };
8936a5c
 
8936a5c
 #define HTTP(con) &((con)->http)
7632e7a
@@ -137,6 +148,9 @@ extern void	cupsdStartListening(void);
8936a5c
 extern void	cupsdStopListening(void);
8936a5c
 extern void	cupsdUpdateCGI(void);
8936a5c
 extern void	cupsdWriteClient(cupsd_client_t *con);
8936a5c
+#ifdef WITH_LSPP
8936a5c
+extern uid_t	client_pid_to_auid(pid_t clipid);
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
47ca1c0
 #ifdef HAVE_SSL
47ca1c0
 extern int	cupsdEndTLS(cupsd_client_t *con);
6fc5472
diff -up cups-1.7.1/scheduler/conf.c.lspp cups-1.7.1/scheduler/conf.c
6fc5472
--- cups-1.7.1/scheduler/conf.c.lspp	2014-01-08 18:43:27.291821665 +0100
6fc5472
+++ cups-1.7.1/scheduler/conf.c	2014-01-08 18:43:27.300821540 +0100
6fc5472
@@ -36,6 +36,9 @@
8936a5c
 #  define INADDR_NONE	0xffffffff
8936a5c
 #endif /* !INADDR_NONE */
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+#  include <libaudit.h>
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
8936a5c
 /*
8936a5c
  * Configuration variable structure...
6fc5472
@@ -123,6 +126,10 @@ static const cupsd_var_t	cupsd_vars[] =
2ca5fdf
   { "ServerName",		&ServerName,		CUPSD_VARTYPE_STRING },
2ca5fdf
   { "StrictConformance",	&StrictConformance,	CUPSD_VARTYPE_BOOLEAN },
2ca5fdf
   { "Timeout",			&Timeout,		CUPSD_VARTYPE_TIME },
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  { "AuditLog",			&AuditLog,		CUPSD_VARTYPE_INTEGER },
8936a5c
+  { "PerPageLabels",		&PerPageLabels,		CUPSD_VARTYPE_BOOLEAN },
8936a5c
+#endif /* WITH_LSPP */
2ca5fdf
   { "WebInterface",		&WebInterface,		CUPSD_VARTYPE_BOOLEAN }
2ca5fdf
 };
2ca5fdf
 static const cupsd_var_t	cupsfiles_vars[] =
6fc5472
@@ -523,6 +530,9 @@ cupsdReadConfiguration(void)
8936a5c
   const char	*tmpdir;		/* TMPDIR environment variable */
8936a5c
   struct stat	tmpinfo;		/* Temporary directory info */
cb06f24
   cupsd_policy_t *p;			/* Policy */
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  char		*audit_message;		/* Audit message string */
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
8936a5c
 
8936a5c
  /*
6fc5472
@@ -829,6 +839,25 @@ cupsdReadConfiguration(void)
8936a5c
 
8936a5c
   RunUser = getuid();
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  if (AuditLog != -1)
8936a5c
+  {
8936a5c
+   /*
8936a5c
+    * ClassifyOverride is set during read_configuration, if its ON, report it now
8936a5c
+    */
8936a5c
+    if (ClassifyOverride)
8936a5c
+      audit_log_user_message(AuditLog, AUDIT_USYS_CONFIG,
8936a5c
+                "[Config] ClassifyOverride=enabled Users can override print banners",
8936a5c
+                ServerName, NULL, NULL, 1);
8936a5c
+   /*
8936a5c
+    * PerPageLabel is set during read_configuration, if its OFF, report it now
8936a5c
+    */
8936a5c
+    if (!PerPageLabels)
8936a5c
+      audit_log_user_message(AuditLog, AUDIT_USYS_CONFIG,
8936a5c
+                "[Config] PerPageLabels=disabled", ServerName, NULL, NULL, 1);
8936a5c
+  }
8936a5c
+#endif /* WITH_LSPP */
f4b6623
+
a1088d0
   cupsdLogMessage(CUPSD_LOG_INFO, "Remote access is %s.",
ca53cd4
                   RemotePort ? "enabled" : "disabled");
a1088d0
 
6fc5472
@@ -1233,7 +1262,19 @@ cupsdReadConfiguration(void)
8936a5c
     cupsdClearString(&Classification);
8936a5c
 
8936a5c
   if (Classification)
8936a5c
+  {
8936a5c
     cupsdLogMessage(CUPSD_LOG_INFO, "Security set to \"%s\"", Classification);
8936a5c
+#ifdef WITH_LSPP
8936a5c
+    if (AuditLog != -1)
8936a5c
+    {
8936a5c
+      audit_message = NULL;
8936a5c
+      cupsdSetStringf(&audit_message, "[Config] Classification=%s", Classification);
8936a5c
+      audit_log_user_message(AuditLog, AUDIT_LABEL_LEVEL_CHANGE, audit_message,
8936a5c
+                             ServerName, NULL, NULL, 1);
8936a5c
+      cupsdClearString(&audit_message);
8936a5c
+    }
8936a5c
+#endif /* WITH_LSPP */
8936a5c
+  }
8936a5c
 
8936a5c
  /*
cb06f24
   * Check the MaxClients setting, and then allocate memory for it...
6fc5472
@@ -3622,6 +3663,18 @@ read_location(cups_file_t *fp,		/* I - C
cb98b41
   return ((FatalErrors & CUPSD_FATAL_CONFIG) ? 0 : linenum);
8936a5c
 }
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+int is_lspp_config()
8936a5c
+{
8936a5c
+  if (Classification != NULL)
5bd20b0
+    return ((_cups_strcasecmp(Classification, MLS_CONFIG) == 0) 
5bd20b0
+            || (_cups_strcasecmp(Classification, TE_CONFIG) == 0)
5bd20b0
+            || (_cups_strcasecmp(Classification, SELINUX_CONFIG) == 0));
8936a5c
+  else
8936a5c
+    return 0;
8936a5c
+}
8936a5c
+#endif /* WITH_LSPP */
e7076f3
+
8936a5c
 
8936a5c
 /*
cb98b41
  * 'read_policy()' - Read a <Policy name> definition.
6fc5472
diff -up cups-1.7.1/scheduler/conf.h.lspp cups-1.7.1/scheduler/conf.h
6fc5472
--- cups-1.7.1/scheduler/conf.h.lspp	2014-01-08 18:43:27.284821762 +0100
6fc5472
+++ cups-1.7.1/scheduler/conf.h	2014-01-08 18:43:27.301821527 +0100
7632e7a
@@ -250,6 +250,13 @@ VAR int			SSLOptions		VALUE(CUPSD_SSL_NO
cb06f24
 					/* SSL/TLS options */
8936a5c
 #endif /* HAVE_SSL */
47ca1c0
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+VAR int			AuditLog		VALUE(-1),
8936a5c
+					/* File descriptor for audit */
8936a5c
+			PerPageLabels		VALUE(TRUE);
8936a5c
+					/* Put the label on each page */
8936a5c
+#endif /* WITH_LSPP */
47ca1c0
+
8936a5c
 #ifdef HAVE_LAUNCHD
47ca1c0
 VAR int			LaunchdTimeout		VALUE(10);
47ca1c0
 					/* Time after which an idle cupsd will exit */
7632e7a
@@ -268,6 +275,9 @@ int			HaveServerCreds		VALUE(0);
47ca1c0
 gss_cred_id_t		ServerCreds;	/* Server's GSS credentials */
47ca1c0
 #endif /* HAVE_GSSAPI */
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
47ca1c0
+extern int		is_lspp_config(void);
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
8936a5c
 /*
8936a5c
  * Prototypes...
6fc5472
diff -up cups-1.7.1/scheduler/cupsd.h.lspp cups-1.7.1/scheduler/cupsd.h
6fc5472
--- cups-1.7.1/scheduler/cupsd.h.lspp	2014-01-08 18:43:27.264822039 +0100
6fc5472
+++ cups-1.7.1/scheduler/cupsd.h	2014-01-08 18:43:27.301821527 +0100
770e272
@@ -13,6 +13,8 @@
770e272
  *   file is missing or damaged, see the license at "http://www.cups.org/".
770e272
  */
770e272
 
770e272
+/* Copyright (C) 2005 Trusted Computer Solutions, Inc. */
770e272
+/* (c) Copyright 2005-2006 Hewlett-Packard Development Company, L.P. */
770e272
 
770e272
 /*
770e272
  * Include necessary headers.
770e272
@@ -37,13 +39,20 @@
770e272
 #  include <unistd.h>
770e272
 #endif /* WIN32 */
770e272
 
770e272
+#include "config.h"
770e272
+#ifdef WITH_LSPP
770e272
+#  define MLS_CONFIG "mls"
770e272
+#  define TE_CONFIG "te"
770e272
+#  define SELINUX_CONFIG "SELinux"
770e272
+#  define UNKNOWN_SL "UNKNOWN SL"
770e272
+#endif /* WITH_LSPP */
770e272
+
770e272
 #include "mime.h"
770e272
 
770e272
 #if defined(HAVE_CDSASSL)
770e272
 #  include <CoreFoundation/CoreFoundation.h>
770e272
 #endif /* HAVE_CDSASSL */
770e272
 
770e272
-
770e272
 /*
770e272
  * Some OS's don't have hstrerror(), most notably Solaris...
770e272
  */
6fc5472
diff -up cups-1.7.1/scheduler/ipp.c.lspp cups-1.7.1/scheduler/ipp.c
6fc5472
--- cups-1.7.1/scheduler/ipp.c.lspp	2014-01-08 18:43:27.188823090 +0100
6fc5472
+++ cups-1.7.1/scheduler/ipp.c	2014-01-08 18:46:34.235235319 +0100
6fc5472
@@ -16,6 +16,9 @@
6fc5472
  * file is missing or damaged, see the license at "http://www.cups.org/".
8936a5c
  */
8936a5c
 
8936a5c
+/* Copyright (C) 2005 Trusted Computer Solutions, Inc. */
8936a5c
+/* (c) Copyright 2005-2006 Hewlett-Packard Development Company, L.P. */
e7076f3
+
8936a5c
 /*
8936a5c
  * Include necessary headers...
8936a5c
  */
6fc5472
@@ -39,6 +42,14 @@ extern int mbr_check_membership_by_id(uu
8936a5c
 #  endif /* HAVE_MEMBERSHIPPRIV_H */
8936a5c
 #endif /* __APPLE__ */
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+#include <libaudit.h>
8936a5c
+#include <selinux/selinux.h>
8936a5c
+#include <selinux/context.h>
8936a5c
+#include <selinux/avc.h>
8936a5c
+#include <selinux/flask.h>
8936a5c
+#include <selinux/av_permissions.h>
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
8936a5c
 /*
8936a5c
  * Local functions...
6fc5472
@@ -63,6 +74,9 @@ static void	cancel_all_jobs(cupsd_client
8936a5c
 static void	cancel_job(cupsd_client_t *con, ipp_attribute_t *uri);
8936a5c
 static void	cancel_subscription(cupsd_client_t *con, int id);
34fc0f7
 static int	check_rss_recipient(const char *recipient);
8936a5c
+#ifdef WITH_LSPP
8936a5c
+static int	check_context(cupsd_client_t *con, cupsd_job_t *job);
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 static int	check_quotas(cupsd_client_t *con, cupsd_printer_t *p);
47ca1c0
 static void	close_job(cupsd_client_t *con, ipp_attribute_t *uri);
47ca1c0
 static void	copy_attrs(ipp_t *to, ipp_t *from, cups_array_t *ra,
6fc5472
@@ -1235,6 +1249,21 @@ add_job(cupsd_client_t  *con,		/* I - Cl
389e11e
     "time-at-creation",
389e11e
     "time-at-processing"
389e11e
   };
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  char		*audit_message;		/* Audit message string */
8936a5c
+  char		*printerfile;		/* device file pointed to by the printer */
8936a5c
+  char		*userheader = NULL;	/* User supplied job-sheets[0] */
8936a5c
+  char		*userfooter = NULL;	/* User supplied job-sheets[1] */
8936a5c
+  int		override = 0;		/* Was a banner overrode on a job */
8936a5c
+  security_id_t	clisid;			/* SELinux SID for the client */
8936a5c
+  security_id_t	psid;			/* SELinux SID for the printer */
8936a5c
+  context_t	printercon;		/* Printer's context string */
8936a5c
+  struct stat	printerstat;		/* Printer's stat buffer */
8936a5c
+  security_context_t	devcon;		/* Printer's SELinux context */
8936a5c
+  struct avc_entry_ref	avcref;		/* Pointer to the access vector cache */
8936a5c
+  security_class_t	tclass;		/* Object class for the SELinux check */
8936a5c
+  access_vector_t	avr;		/* Access method being requested */
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
8936a5c
 
8936a5c
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job(%p[%d], %p(%s), %p(%s/%s))",
6fc5472
@@ -1552,6 +1581,106 @@ add_job(cupsd_client_t  *con,		/* I - Cl
5b01ebd
     return (NULL);
389e11e
   }
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  if (is_lspp_config())
8936a5c
+  {
8936a5c
+    if (!con->scon || strncmp(con->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) == 0)
8936a5c
+    {
8936a5c
+      cupsdLogMessage(CUPSD_LOG_ERROR, "add_job: missing classification for connection \'%s\'!", printer->name);
8936a5c
+      send_ipp_status(con, IPP_INTERNAL_ERROR, _("Missing required security attributes."));
8936a5c
+      return (NULL);
8936a5c
+    }
e7076f3
+
8936a5c
+   /*
8936a5c
+    * Perform an access check so that if the user gets feedback at enqueue time
8936a5c
+    */
f4b6623
+
8936a5c
+    printerfile = strstr(printer->device_uri, "/dev/");
8936a5c
+    if (printerfile == NULL && (strncmp(printer->device_uri, "file:/", 6) == 0))
08a85d3
+      printerfile = printer->device_uri + strlen("file:");
f4b6623
+
8936a5c
+    if (printerfile != NULL)
8936a5c
+    {
8936a5c
+      cupsdLogMessage(CUPSD_LOG_DEBUG, "add_job: Attempting an access check on printer device %s",
8936a5c
+                      printerfile);
f4b6623
+
8936a5c
+      if (lstat(printerfile, &printerstat) < 0)
8936a5c
+      {
8936a5c
+	if (errno != ENOENT)
8936a5c
+	{
8936a5c
+	  send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Unable to stat the printer"));
8936a5c
+	  return (NULL);
8936a5c
+	}
8936a5c
+	/*
8936a5c
+	 * The printer does not exist, so for now assume it's a FileDevice
8936a5c
+	 */
8936a5c
+	tclass = SECCLASS_FILE;
8936a5c
+	avr = FILE__WRITE;
8936a5c
+      }
8936a5c
+      else if (S_ISCHR(printerstat.st_mode))
8936a5c
+      {
8936a5c
+	tclass = SECCLASS_CHR_FILE;
8936a5c
+	avr = CHR_FILE__WRITE;
8936a5c
+      }
8936a5c
+      else if (S_ISREG(printerstat.st_mode))
8936a5c
+      {
8936a5c
+	tclass = SECCLASS_FILE;
8936a5c
+	avr = FILE__WRITE;
8936a5c
+      }
8936a5c
+      else
8936a5c
+      {
8936a5c
+	send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Printer is not a character device or regular file"));
8936a5c
+	return (NULL);
8936a5c
+      }
9ed0ac5
+      static avc_initialized = 0;
9ed0ac5
+      if (!avc_initialized++)
9ed0ac5
+          avc_init("cupsd_enqueue_", NULL, NULL, NULL, NULL);
8936a5c
+      avc_entry_ref_init(&avcref);
8936a5c
+      if (avc_context_to_sid(con->scon, &clisid) != 0)
8936a5c
+      {
8936a5c
+        send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Unable to get the SELinux sid of the client"));
8936a5c
+        return (NULL);
8936a5c
+      }
8936a5c
+      if (getfilecon(printerfile, &devcon) == -1)
8936a5c
+      {
8936a5c
+        send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Unable to get the SELinux context of the printer"));
8936a5c
+        return (NULL);
8936a5c
+      }
8936a5c
+      printercon = context_new(devcon);
8936a5c
+      cupsdLogMessage(CUPSD_LOG_DEBUG, "add_job: printer context %s client context %s",
8936a5c
+                      context_str(printercon), con->scon);
8936a5c
+      context_free(printercon);
f4b6623
+
8936a5c
+      if (avc_context_to_sid(devcon, &psid) != 0)
8936a5c
+      {
8936a5c
+        send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Unable to get the SELinux sid of the printer"));
8936a5c
+        freecon(devcon);
8936a5c
+        return (NULL);
8936a5c
+      }
8936a5c
+      freecon(devcon);
8936a5c
+      if (avc_has_perm(clisid, psid, tclass, avr, &avcref, NULL) != 0)
8936a5c
+      {
8936a5c
+       /*
8936a5c
+        * The access check failed, so cancel the job and send an audit message
8936a5c
+        */
8936a5c
+        if (AuditLog != -1)
8936a5c
+        {
8936a5c
+          audit_message = NULL;
8936a5c
+          cupsdSetStringf(&audit_message, "job=? auid=%u acct=%s obj=%s refused"
8936a5c
+                          " unable to access printer=%s", con->auid,
8936a5c
+                          con->username, con->scon, printer->name);
8936a5c
+          audit_log_user_message(AuditLog, AUDIT_USER_LABELED_EXPORT, audit_message,
8936a5c
+                                 ServerName, NULL, NULL, 0);
8936a5c
+          cupsdClearString(&audit_message);
8936a5c
+        }
f4b6623
+
8936a5c
+        send_ipp_status(con, IPP_NOT_AUTHORIZED, _("SELinux prohibits access to the printer"));
8936a5c
+        return (NULL);
8936a5c
+      }
8936a5c
+    }
8936a5c
+  }
8936a5c
+#endif /* WITH_LSPP */
f4b6623
+
8936a5c
   if ((job = cupsdAddJob(priority, printer->name)) == NULL)
8936a5c
   {
8936a5c
     send_ipp_status(con, IPP_INTERNAL_ERROR,
6fc5472
@@ -1560,6 +1689,32 @@ add_job(cupsd_client_t  *con,		/* I - Cl
8936a5c
     return (NULL);
8936a5c
   }
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  if (is_lspp_config())
8936a5c
+  {
8936a5c
+   /*
8936a5c
+    * duplicate the security context and auid of the connection into the job structure
8936a5c
+    */
8936a5c
+    job->scon = strdup(con->scon);
8936a5c
+    job->auid = con->auid;
f4b6623
+
8936a5c
+   /* 
8936a5c
+    * add the security context to the request so that on a restart the security
8936a5c
+    * attributes will be able to be restored
8936a5c
+    */
8936a5c
+    ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "security-context", 
8936a5c
+		 NULL, job->scon);
8936a5c
+  }
8936a5c
+  else
8936a5c
+  {
8936a5c
+   /*
8936a5c
+    * Fill in the security context of the job as unlabeled
8936a5c
+    */
8936a5c
+    cupsdLogMessage(CUPSD_LOG_DEBUG, "add_job: setting context of job to %s", UNKNOWN_SL);
8936a5c
+    cupsdSetString(&job->scon, UNKNOWN_SL);
8936a5c
+  }
8936a5c
+#endif /* WITH_LSPP */
f4b6623
+
47ca1c0
   job->dtype   = printer->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE);
8936a5c
   job->attrs   = con->request;
47ca1c0
   job->dirty   = 1;
6fc5472
@@ -1769,6 +1924,29 @@ add_job(cupsd_client_t  *con,		/* I - Cl
1e5683c
       attr->values[0].string.text = _cupsStrRetain(printer->job_sheets[0]);
1e5683c
       attr->values[1].string.text = _cupsStrRetain(printer->job_sheets[1]);
8936a5c
     }
8936a5c
+#ifdef WITH_LSPP
8936a5c
+    else
8936a5c
+    {
8936a5c
+     /*
8936a5c
+      * The option was present, so capture the user supplied strings
8936a5c
+      */
8936a5c
+      userheader = strdup(attr->values[0].string.text);
f4b6623
+
8936a5c
+      if (attr->num_values > 1)
8936a5c
+        userfooter = strdup(attr->values[1].string.text);
8936a5c
+  
8936a5c
+      if (Classification != NULL && (strcmp(userheader, Classification) == 0)
8936a5c
+          && userfooter &&(strcmp(userfooter, Classification) == 0))
8936a5c
+      {
8936a5c
+       /*
8936a5c
+        * Since both values are Classification, the user is not trying to Override
8936a5c
+        */
8936a5c
+        free(userheader);
8936a5c
+        if (userfooter) free(userfooter);
8936a5c
+        userheader = userfooter = NULL;
8936a5c
+      }
8936a5c
+    }
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
8936a5c
     job->job_sheets = attr;
8936a5c
 
6fc5472
@@ -1799,6 +1977,9 @@ add_job(cupsd_client_t  *con,		/* I - Cl
cb06f24
 	                		     "job-sheets=\"%s,none\", "
cb06f24
 					     "job-originating-user-name=\"%s\"",
cb06f24
 	              Classification, job->username);
8936a5c
+#ifdef WITH_LSPP
cb06f24
+	  override = 1;
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 	}
8936a5c
 	else if (attr->num_values == 2 &&
8936a5c
 	         strcmp(attr->values[0].string.text,
6fc5472
@@ -1817,6 +1998,9 @@ add_job(cupsd_client_t  *con,		/* I - Cl
cb06f24
 					     "job-originating-user-name=\"%s\"",
cb06f24
 		      attr->values[0].string.text,
cb06f24
 		      attr->values[1].string.text, job->username);
8936a5c
+#ifdef WITH_LSPP
cb06f24
+	  override = 1;
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 	}
8936a5c
 	else if (strcmp(attr->values[0].string.text, Classification) &&
8936a5c
 	         strcmp(attr->values[0].string.text, "none") &&
6fc5472
@@ -1837,6 +2021,9 @@ add_job(cupsd_client_t  *con,		/* I - Cl
cb06f24
 			"job-originating-user-name=\"%s\"",
cb06f24
 			attr->values[0].string.text,
cb06f24
 			attr->values[1].string.text, job->username);
8936a5c
+#ifdef WITH_LSPP
cb06f24
+	  override = 1;
8936a5c
+#endif /* WITH_LSPP */
8936a5c
         }
8936a5c
       }
8936a5c
       else if (strcmp(attr->values[0].string.text, Classification) &&
6fc5472
@@ -1877,8 +2064,52 @@ add_job(cupsd_client_t  *con,		/* I - Cl
cb06f24
 		      "job-sheets=\"%s\", "
cb06f24
 		      "job-originating-user-name=\"%s\"",
cb06f24
 		      Classification, job->username);
8936a5c
+#ifdef WITH_LSPP
cb06f24
+	override = 1;
8936a5c
+#endif /* WITH_LSPP */
8936a5c
+      }
8936a5c
+#ifdef WITH_LSPP
8936a5c
+      if (is_lspp_config() && AuditLog != -1)
8936a5c
+      {
8936a5c
+        audit_message = NULL;
f4b6623
+
8936a5c
+        if (userheader || userfooter)
8936a5c
+        {
8936a5c
+          if (!override)
8936a5c
+          {
8936a5c
+           /*
8936a5c
+            * The user overrode the banner, so audit it
8936a5c
+            */
8936a5c
+            cupsdSetStringf(&audit_message, "job=%d user supplied job-sheets=%s,%s"
8936a5c
+                            " using banners=%s,%s", job->id, userheader,
8936a5c
+                            userfooter, attr->values[0].string.text,
8936a5c
+                            (attr->num_values > 1) ? attr->values[1].string.text : "(null)");
8936a5c
+            audit_log_user_message(AuditLog, AUDIT_LABEL_OVERRIDE, audit_message,
8936a5c
+                                   ServerName, NULL, NULL, 1);
cb06f24
+	  }
8936a5c
+          else
8936a5c
+          {
8936a5c
+           /*
8936a5c
+            * The user tried to override the banner, audit the failure
8936a5c
+            */
8936a5c
+            cupsdSetStringf(&audit_message, "job=%d user supplied job-sheets=%s,%s"
8936a5c
+                            " ignored banners=%s,%s", job->id, userheader,
8936a5c
+                            userfooter, attr->values[0].string.text,
8936a5c
+                            (attr->num_values > 1) ? attr->values[1].string.text : "(null)");
8936a5c
+            audit_log_user_message(AuditLog, AUDIT_LABEL_OVERRIDE, audit_message,
8936a5c
+                                   ServerName, NULL, NULL, 0);
cb06f24
+	  }
8936a5c
+          cupsdClearString(&audit_message);
cb06f24
+	}
8936a5c
       }
cb06f24
+
8936a5c
+      if (userheader)
8936a5c
+        free(userheader);
8936a5c
+      if (userfooter)
8936a5c
+        free(userfooter);
8936a5c
+#endif /* WITH_LSPP */
cb06f24
     }
cb06f24
+    
cb06f24
 
8936a5c
    /*
8936a5c
     * See if we need to add the starting sheet...
6fc5472
@@ -3650,6 +3881,111 @@ check_rss_recipient(
8936a5c
 }
8936a5c
 
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+/*
8936a5c
+ * 'check_context()' - Check SELinux security context of a user and job
8936a5c
+ */
f4b6623
+
8936a5c
+static int				/* O - 1 if OK, 0 if not, -1 on error */
8936a5c
+check_context(cupsd_client_t *con,	/* I - Client connection */
8936a5c
+             cupsd_job_t    *job)	/* I - Job */
8936a5c
+{
8936a5c
+  int			enforcing;	/* is SELinux in enforcing mode */
8936a5c
+  char			filename[1024]; /* Filename of the spool file */
8936a5c
+  security_id_t		clisid;		/* SELinux SID of the client */
8936a5c
+  security_id_t		jobsid;		/* SELinux SID of the job */
8936a5c
+  security_id_t		filesid;	/* SELinux SID of the spool file */
8936a5c
+  struct avc_entry_ref	avcref;		/* AVC entry cache pointer */
8936a5c
+  security_class_t	tclass;		/* SELinux security class */
8936a5c
+  access_vector_t	avr;		/* SELinux access being queried */
8936a5c
+  security_context_t	spoolfilecon;	/* SELinux context of the spool file */
f4b6623
+
75d0e82
+
8936a5c
+ /*
8936a5c
+  * Validate the input to be sure there are contexts to work with...
8936a5c
+  */
f4b6623
+
8936a5c
+  if (con->scon == NULL || job->scon == NULL
8936a5c
+      || strncmp(con->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) == 0
8936a5c
+      || strncmp(job->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) == 0)
8936a5c
+    return -1;
75d0e82
+
8936a5c
+  if ((enforcing = security_getenforce()) == -1)
8936a5c
+  {
8936a5c
+    cupsdLogMessage(CUPSD_LOG_ERROR, "Error while determining SELinux enforcement");
8936a5c
+    return -1;
8936a5c
+  }
8936a5c
+  cupsdLogMessage(CUPSD_LOG_DEBUG, "check_context: client context %s job context %s", con->scon, job->scon);
75d0e82
+
75d0e82
+
8936a5c
+ /*
8936a5c
+  * Initialize the avc engine...
8936a5c
+  */
22cc213
+
9ed0ac5
+  static avc_initialized = 0;
9ed0ac5
+  if (! avc_initialized++)
8936a5c
+  {
9ed0ac5
+    if (avc_init("cupsd", NULL, NULL, NULL, NULL) < 0)
9ed0ac5
+    {
9ed0ac5
+      cupsdLogMessage(CUPSD_LOG_ERROR, "check_context: unable avc_init");
9ed0ac5
+      return -1;
9ed0ac5
+    } 
8936a5c
+  } 
8936a5c
+  if (avc_context_to_sid(con->scon, &clisid) != 0)
8936a5c
+  {
8936a5c
+    cupsdLogMessage(CUPSD_LOG_ERROR, "check_context: unable to convert %s to SELinux sid", con->scon);
8936a5c
+    return -1;
8936a5c
+  }
85d072c
+  if (avc_context_to_sid(job->scon, &jobsid) != 0)
85d072c
+  {
85d072c
+    cupsdLogMessage(CUPSD_LOG_ERROR, "check_context: unable to convert %s to SELinux sid", job->scon);
85d072c
+    return -1;
85d072c
+  }
8936a5c
+  avc_entry_ref_init(&avcref);
8936a5c
+  tclass = SECCLASS_FILE;
8936a5c
+  avr = FILE__READ;
f4b6623
+
8936a5c
+ /*
8936a5c
+  * Perform the check with the client as the subject, first with the job as the object
8936a5c
+  *   if that fails then with the spool file as the object...
8936a5c
+  */
f4b6623
+
8936a5c
+  if (avc_has_perm_noaudit(clisid, jobsid, tclass, avr, &avcref, NULL) != 0)
8936a5c
+  {
8936a5c
+    cupsdLogMessage(CUPSD_LOG_INFO, "check_context: SELinux denied access based on the client context");
5dd9863
+
8936a5c
+    snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot, job->id);
8936a5c
+    if (getfilecon(filename, &spoolfilecon) == -1)
8936a5c
+    {
8936a5c
+      cupsdLogMessage(CUPSD_LOG_ERROR, "check_context: Unable to get spoolfile context");
8936a5c
+      return -1;
8936a5c
+    }
8936a5c
+    if (avc_context_to_sid(spoolfilecon, &filesid) != 0)
8936a5c
+    {
8936a5c
+      cupsdLogMessage(CUPSD_LOG_ERROR, "check_context: Unable to determine the SELinux sid for the spool file");
8936a5c
+      freecon(spoolfilecon);
8936a5c
+      return -1;
8936a5c
+    }
8936a5c
+    freecon(spoolfilecon);
8936a5c
+    if (avc_has_perm_noaudit(clisid, filesid, tclass, avr, &avcref, NULL) != 0)
8936a5c
+    {
8936a5c
+      cupsdLogMessage(CUPSD_LOG_INFO, "check_context: SELinux denied access to the spool file");
8936a5c
+      return 0;
8936a5c
+    }
8936a5c
+    cupsdLogMessage(CUPSD_LOG_INFO, "check_context: SELinux allowed access to the spool file");
8936a5c
+    return 1;
8936a5c
+  }
8936a5c
+  else
8936a5c
+    if (enforcing == 0)
8936a5c
+        cupsdLogMessage(CUPSD_LOG_INFO, "check_context: allowing operation due to permissive mode");
8936a5c
+    else
8936a5c
+        cupsdLogMessage(CUPSD_LOG_INFO, "check_context: SELinux allowed access based on the client context");
f4b6623
+
8936a5c
+  return 1;
8936a5c
+}
8936a5c
+#endif /* WITH_LSPP */
f4b6623
+
f4b6623
+
8936a5c
 /*
8936a5c
  * 'check_quotas()' - Check quotas for a printer and user.
8936a5c
  */
6fc5472
@@ -4106,6 +4442,15 @@ copy_banner(cupsd_client_t *con,	/* I -
8936a5c
   char		attrname[255],		/* Name of attribute */
8936a5c
 		*s;			/* Pointer into name */
8936a5c
   ipp_attribute_t *attr;		/* Attribute */
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  const char	*mls_label;		/* SL of print job */
8936a5c
+  char		*jobrange;		/* SELinux sensitivity range */
8936a5c
+  char		*jobclearance;		/* SELinux low end clearance */
8936a5c
+  context_t	jobcon;			/* SELinux context of the job */
8936a5c
+  context_t	tmpcon;			/* Temp context to set the level */
8936a5c
+  security_context_t	spoolcon;	/* Context of the file in the spool */
8936a5c
+#endif /* WITH_LSPP */
f4b6623
+
8936a5c
 
8936a5c
 
34fc0f7
   cupsdLogMessage(CUPSD_LOG_DEBUG2,
6fc5472
@@ -4141,6 +4486,82 @@ copy_banner(cupsd_client_t *con,	/* I -
8936a5c
 
8936a5c
   fchmod(cupsFileNumber(out), 0640);
8936a5c
   fchown(cupsFileNumber(out), RunUser, Group);
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  if (job->scon != NULL &&
8936a5c
+      strncmp(job->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) != 0)
8936a5c
+  {
8936a5c
+    if (getfilecon(filename, &spoolcon) == -1)
8936a5c
+    {
8936a5c
+      cupsdLogMessage(CUPSD_LOG_ERROR,
8936a5c
+                      "copy_banner: Unable to get the context of the banner file %s - %s",
8936a5c
+                      filename, strerror(errno));
8936a5c
+      job->num_files --;
8936a5c
+      return (0);
8936a5c
+    }
8936a5c
+    tmpcon = context_new(spoolcon);
8936a5c
+    jobcon = context_new(job->scon);
8936a5c
+    freecon(spoolcon);
8936a5c
+    if (!tmpcon || !jobcon)
8936a5c
+    {
8936a5c
+      if (tmpcon)
8936a5c
+        context_free(tmpcon);
8936a5c
+      if (jobcon)
8936a5c
+        context_free(jobcon);
8936a5c
+      cupsdLogMessage(CUPSD_LOG_ERROR,
8936a5c
+                      "copy_banner: Unable to get the SELinux contexts");
8936a5c
+      job->num_files --;
8936a5c
+      return (0);
8936a5c
+    }
8936a5c
+    jobrange = context_range_get(jobcon);
8936a5c
+    if (jobrange)
8936a5c
+    {
8936a5c
+      jobrange = strdup(jobrange);
8936a5c
+      if ((jobclearance = strtok(jobrange, "-")) != NULL)
8936a5c
+      {
8936a5c
+	if (context_range_set(tmpcon, jobclearance) == -1)
8936a5c
+	{
8936a5c
+	  cupsdLogMessage(CUPSD_LOG_ERROR,
8936a5c
+			  "copy_banner: Unable to set the level of the context for file %s - %s",
8936a5c
+			  filename, strerror(errno));
8936a5c
+	  free(jobrange);
8936a5c
+	  context_free(jobcon);
8936a5c
+	  context_free(tmpcon);
8936a5c
+	  job->num_files --;
8936a5c
+	  return (0);
8936a5c
+	}
8936a5c
+      }
8936a5c
+      else
8936a5c
+      {
8936a5c
+	if (context_range_set(tmpcon, (context_range_get(jobcon))) == -1)
8936a5c
+	{
8936a5c
+	  cupsdLogMessage(CUPSD_LOG_ERROR,
8936a5c
+			  "copy_banner: Unable to set the level of the context for file %s - %s",
8936a5c
+			  filename, strerror(errno));
8936a5c
+	  free(jobrange);
8936a5c
+	  context_free(jobcon);
8936a5c
+	  context_free(tmpcon);
8936a5c
+	  job->num_files --;
8936a5c
+	  return (0);
8936a5c
+	}
8936a5c
+      }
8936a5c
+      free(jobrange);
8936a5c
+    }
8936a5c
+    if (setfilecon(filename, context_str(tmpcon)) == -1)
8936a5c
+    {
8936a5c
+      cupsdLogMessage(CUPSD_LOG_ERROR,
8936a5c
+                      "copy_banner: Unable to set the context of the banner file %s - %s",
8936a5c
+                      filename, strerror(errno));
8936a5c
+      context_free(jobcon);
8936a5c
+      context_free(tmpcon);
8936a5c
+      job->num_files --;
8936a5c
+      return (0);
8936a5c
+    }
8936a5c
+    cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_banner: %s set to %s",
8936a5c
+                    filename, context_str(tmpcon));
8936a5c
+    context_free(jobcon);
8936a5c
+    context_free(tmpcon);
8936a5c
+  }
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
8936a5c
  /*
8936a5c
   * Try the localized banner file under the subdirectory...
6fc5472
@@ -4235,6 +4656,24 @@ copy_banner(cupsd_client_t *con,	/* I -
8936a5c
       else
8936a5c
         s = attrname;
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+      if (strcmp(s, "mls-label") == 0)
8936a5c
+      {
8936a5c
+        if (job->scon != NULL && strncmp(job->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) != 0)
8936a5c
+        {
8936a5c
+          jobcon = context_new(job->scon);
5bd20b0
+          if (_cups_strcasecmp(name, MLS_CONFIG) == 0)
8936a5c
+            mls_label = context_range_get(jobcon);
5bd20b0
+          else if (_cups_strcasecmp(name, TE_CONFIG) == 0)
8936a5c
+            mls_label = context_type_get(jobcon);
8936a5c
+          else // default to using the whole context string
8936a5c
+            mls_label = context_str(jobcon);
8936a5c
+          cupsFilePuts(out, mls_label);
8936a5c
+          context_free(jobcon);
8936a5c
+        }
8936a5c
+        continue;
8936a5c
+      }
8936a5c
+#endif /* WITH_LSPP */
8936a5c
       if (!strcmp(s, "printer-name"))
8936a5c
       {
8936a5c
         cupsFilePuts(out, job->dest);
6fc5472
@@ -6101,6 +6540,22 @@ get_job_attrs(cupsd_client_t  *con,	/* I
4743e10
 
4743e10
   exclude = cupsdGetPrivateAttrs(policy, con, printer, job->username);
8936a5c
 
f4b6623
+
8936a5c
+#ifdef WITH_LSPP
8936a5c
+ /*
8936a5c
+  * Check SELinux...
8936a5c
+  */
8936a5c
+  if (is_lspp_config() && check_context(con, job) != 1)
8936a5c
+  {
8936a5c
+   /*
8936a5c
+    * Unfortunately we have to lie to the user...
8936a5c
+    */
8936a5c
+    send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
8936a5c
+    return;
8936a5c
+  }
8936a5c
+#endif /* WITH_LSPP */
d042370
+
ee6b344
+
8936a5c
  /*
8936a5c
   * Copy attributes...
8936a5c
   */
6fc5472
@@ -6443,6 +6898,11 @@ get_jobs(cupsd_client_t  *con,		/* I - C
4743e10
       if (username[0] && _cups_strcasecmp(username, job->username))
4743e10
 	continue;
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
4743e10
+      if (is_lspp_config() && check_context(con, job) != 1)
4743e10
+	continue;
8936a5c
+#endif /* WITH_LSPP */
e7076f3
+
4743e10
       if (count > 0)
4743e10
 	ippAddSeparator(con->response);
8936a5c
 
6fc5472
@@ -11013,6 +11473,11 @@ validate_user(cupsd_job_t    *job,	/* I
8936a5c
 
8936a5c
   strlcpy(username, get_username(con), userlen);
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  if (is_lspp_config() && check_context(con, job) != 1)
8936a5c
+    return 0;
8936a5c
+#endif /* WITH_LSPP */
e7076f3
+
8936a5c
  /*
8936a5c
   * Check the username against the owner...
8936a5c
   */
6fc5472
diff -up cups-1.7.1/scheduler/job.c.lspp cups-1.7.1/scheduler/job.c
6fc5472
--- cups-1.7.1/scheduler/job.c.lspp	2014-01-08 18:43:27.279821831 +0100
6fc5472
+++ cups-1.7.1/scheduler/job.c	2014-01-08 18:47:08.592759973 +0100
6fc5472
@@ -13,6 +13,9 @@
6fc5472
  * file is missing or damaged, see the license at "http://www.cups.org/".
8936a5c
  */
8936a5c
 
8936a5c
+/* Copyright (C) 2005 Trusted Computer Solutions, Inc. */
8936a5c
+/* (c) Copyright 2005-2006 Hewlett-Packard Development Company, L.P. */
e7076f3
+
8936a5c
 /*
8936a5c
  * Include necessary headers...
8936a5c
  */
6fc5472
@@ -28,6 +31,14 @@
4743e10
 #  endif /* HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H */
4743e10
 #endif /* __APPLE__ */
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+#include <libaudit.h>
8936a5c
+#include <selinux/selinux.h>
8936a5c
+#include <selinux/context.h>
8936a5c
+#include <selinux/avc.h>
8936a5c
+#include <selinux/flask.h>
8936a5c
+#include <selinux/av_permissions.h>
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
8936a5c
 /*
a1088d0
  * Design Notes for Job Management
6fc5472
@@ -525,6 +536,14 @@ cupsdContinueJob(cupsd_job_t *job)	/* I
4743e10
 					/* PRINTER_STATE_REASONS env var */
a1088d0
 			rip_max_cache[255];
a1088d0
 					/* RIP_MAX_CACHE env variable */
a1088d0
+#ifdef WITH_LSPP
a1088d0
+  char			*audit_message = NULL;	/* Audit message string */
a1088d0
+  context_t		jobcon;		/* SELinux context of the job */
a1088d0
+  char			*label_template = NULL;	/* SL to put in classification
a1088d0
+						   env var */
a1088d0
+  const char		*mls_label = NULL;	/* SL to put in classification
a1088d0
+						   env var */
a1088d0
+#endif /* WITH_LSPP */
a1088d0
 
a1088d0
 
a1088d0
   cupsdLogMessage(CUPSD_LOG_DEBUG2,
6fc5472
@@ -1034,6 +1053,67 @@ cupsdContinueJob(cupsd_job_t *job)	/* I
5b01ebd
   if (final_content_type[0])
5b01ebd
     envp[envc ++] = final_content_type;
a1088d0
 
a1088d0
+#ifdef WITH_LSPP
a1088d0
+  if (is_lspp_config())
a1088d0
+  {
a1088d0
+    if (!job->scon || strncmp(job->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) == 0)
a1088d0
+    {
a1088d0
+      if (AuditLog != -1)
a1088d0
+      {
a1088d0
+        audit_message = NULL;
a1088d0
+        cupsdSetStringf(&audit_message, "job=%d auid=%u acct=%s printer=%s title=%s",
a1088d0
+                        job->id, job->auid, job->username, job->printer->name, title);
a1088d0
+        audit_log_user_message(AuditLog, AUDIT_USER_UNLABELED_EXPORT, audit_message,
a1088d0
+                               ServerName, NULL, NULL, 1);
a1088d0
+        cupsdClearString(&audit_message);
a1088d0
+      }
a1088d0
+    }
a1088d0
+    else 
a1088d0
+    {
a1088d0
+      jobcon = context_new(job->scon);
a1088d0
+
a1088d0
+      if ((attr = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME)) == NULL)
a1088d0
+        label_template = strdup(Classification);
a1088d0
+      else if (attr->num_values > 1 &&
a1088d0
+               strcmp(attr->values[1].string.text, "none") != 0)
a1088d0
+        label_template = strdup(attr->values[1].string.text);
a1088d0
+      else
a1088d0
+        label_template = strdup(attr->values[0].string.text);
a1088d0
+
5bd20b0
+      if (_cups_strcasecmp(label_template, MLS_CONFIG) == 0)
a1088d0
+        mls_label = context_range_get(jobcon);
5bd20b0
+      else if (_cups_strcasecmp(label_template, TE_CONFIG) == 0)
a1088d0
+        mls_label = context_type_get(jobcon);
5bd20b0
+      else if (_cups_strcasecmp(label_template, SELINUX_CONFIG) == 0)
a1088d0
+        mls_label = context_str(jobcon);
a1088d0
+      else
a1088d0
+        mls_label = label_template;
a1088d0
+
a1088d0
+      if (mls_label && (PerPageLabels || banner_page))
a1088d0
+      {
a1088d0
+        snprintf(classification, sizeof(classification), "CLASSIFICATION=LSPP:%s", mls_label);
a1088d0
+        envp[envc ++] = classification;
a1088d0
+      }
a1088d0
+
a1088d0
+      if ((AuditLog != -1) && !banner_page)
a1088d0
+      {
a1088d0
+        audit_message = NULL;
a1088d0
+        cupsdSetStringf(&audit_message, "job=%d auid=%u acct=%s printer=%s title=%s"
a1088d0
+                        " obj=%s label=%s", job->id, job->auid, job->username,
a1088d0
+                        job->printer->name, title, job->scon, mls_label?mls_label:"none");
a1088d0
+        audit_log_user_message(AuditLog, AUDIT_USER_LABELED_EXPORT, audit_message,
a1088d0
+                               ServerName, NULL, NULL, 1);
a1088d0
+        cupsdClearString(&audit_message);
a1088d0
+      }
a1088d0
+      context_free(jobcon);
a1088d0
+      free(label_template);
a1088d0
+    }
a1088d0
+  }
a1088d0
+  else
a1088d0
+   /*
a1088d0
+    * Fall through to the non-LSPP behavior
a1088d0
+    */
a1088d0
+#endif /* WITH_LSPP */
a1088d0
   if (Classification && !banner_page)
a1088d0
   {
a1088d0
     if ((attr = ippFindAttribute(job->attrs, "job-sheets",
6fc5472
@@ -1800,6 +1880,20 @@ cupsdLoadJob(cupsd_job_t *job)		/* I - J
47ca1c0
       ippSetString(job->attrs, &job->reasons, 0, "none");
8936a5c
   }
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  if ((attr = ippFindAttribute(job->attrs, "security-context", IPP_TAG_NAME)) != NULL)
8936a5c
+    cupsdSetString(&job->scon, attr->values[0].string.text);
8936a5c
+  else if (is_lspp_config())
8936a5c
+  {
8936a5c
+   /*
8936a5c
+    * There was no security context so delete the job
8936a5c
+    */
8936a5c
+    cupsdLogMessage(CUPSD_LOG_ERROR, "LoadAllJobs: Missing or bad security-context attribute in control file \"%s\"!",
8936a5c
+                    jobfile);
3244c62
+    goto error;
8936a5c
+  }
8936a5c
+#endif /* WITH_LSPP */
e7076f3
+
8936a5c
   job->sheets     = ippFindAttribute(job->attrs, "job-media-sheets-completed",
8936a5c
                                      IPP_TAG_INTEGER);
8936a5c
   job->job_sheets = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME);
6fc5472
@@ -2188,6 +2282,14 @@ cupsdSaveJob(cupsd_job_t *job)		/* I - J
47ca1c0
 {
47ca1c0
   char		filename[1024];		/* Job control filename */
8936a5c
   cups_file_t	*fp;			/* Job file */
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  security_context_t	spoolcon;	/* context of the job control file */
8936a5c
+  context_t		jobcon;		/* contex_t container for job->scon */
8936a5c
+  context_t		tmpcon;		/* Temp context to swap the level */
a1088d0
+  char			*jobclearance;	/* SELinux low end clearance */
a1088d0
+  const char		*jobrange;	/* SELinux sensitivity range */
a1088d0
+  char			*jobrange_copy;	/* SELinux sensitivity range */
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
8936a5c
 
8936a5c
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
6fc5472
@@ -2200,6 +2302,76 @@ cupsdSaveJob(cupsd_job_t *job)		/* I - J
47ca1c0
 
8936a5c
   fchown(cupsFileNumber(fp), RunUser, Group);
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  if (job->scon && strncmp(job->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) != 0)
8936a5c
+  {
8936a5c
+    if (getfilecon(filename, &spoolcon) == -1)
8936a5c
+    {
8936a5c
+      cupsdLogMessage(CUPSD_LOG_ERROR,
8936a5c
+                      "Unable to get context of job control file \"%s\" - %s.",
8936a5c
+                      filename, strerror(errno));
8936a5c
+      return;
8936a5c
+    }
8936a5c
+    jobcon = context_new(job->scon);
8936a5c
+    tmpcon = context_new(spoolcon);
8936a5c
+    freecon(spoolcon);
8936a5c
+    if (!jobcon || !tmpcon)
8936a5c
+    {
8936a5c
+      if (jobcon)
8936a5c
+        context_free(jobcon);
8936a5c
+      if (tmpcon)
8936a5c
+        context_free(tmpcon);
8936a5c
+      cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to get SELinux contexts");
8936a5c
+      return;
8936a5c
+    }
8936a5c
+    jobrange = context_range_get(jobcon);
8936a5c
+    if (jobrange)
8936a5c
+    {
a1088d0
+      jobrange_copy = strdup(jobrange);
a1088d0
+      if ((jobclearance = strtok(jobrange_copy, "-")) != NULL)
8936a5c
+      {
8936a5c
+	if (context_range_set(tmpcon, jobclearance) == -1)
8936a5c
+	{
8936a5c
+	  cupsdLogMessage(CUPSD_LOG_ERROR,
8936a5c
+			  "Unable to set the range for job control file \"%s\" - %s.",
8936a5c
+			  filename, strerror(errno));
a1088d0
+	  free(jobrange_copy);
8936a5c
+	  context_free(tmpcon);
8936a5c
+	  context_free(jobcon);
8936a5c
+	  return;
8936a5c
+	}
8936a5c
+      }
8936a5c
+      else
8936a5c
+      {
8936a5c
+	if (context_range_set(tmpcon, (context_range_get(jobcon))) == -1)
8936a5c
+	{
8936a5c
+	  cupsdLogMessage(CUPSD_LOG_ERROR,
8936a5c
+			  "Unable to set the range for job control file \"%s\" - %s.",
8936a5c
+			  filename, strerror(errno));
a1088d0
+	  free(jobrange_copy);
8936a5c
+	  context_free(tmpcon);
8936a5c
+	  context_free(jobcon);
8936a5c
+	  return;
8936a5c
+	}
8936a5c
+      }
a1088d0
+      free(jobrange_copy);
8936a5c
+    }
8936a5c
+    if (setfilecon(filename, context_str(tmpcon)) == -1)
8936a5c
+    {
8936a5c
+      cupsdLogMessage(CUPSD_LOG_ERROR,
8936a5c
+                      "Unable to set context of job control file \"%s\" - %s.",
8936a5c
+                      filename, strerror(errno));
8936a5c
+      context_free(tmpcon);
8936a5c
+      context_free(jobcon);
8936a5c
+      return;
8936a5c
+    }
8936a5c
+    cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSaveJob(job=%p): new spool file context=%s",
8936a5c
+                    job, context_str(tmpcon));
8936a5c
+    context_free(tmpcon);
8936a5c
+    context_free(jobcon);
8936a5c
+  }
8936a5c
+#endif /* WITH_LSPP */
e7076f3
+
8936a5c
   job->attrs->state = IPP_IDLE;
8936a5c
 
8936a5c
   if (ippWriteIO(fp, (ipp_iocb_t)cupsFileWrite, 1, NULL,
6fc5472
@@ -3748,6 +3920,18 @@ get_options(cupsd_job_t *job,		/* I - Jo
a1088d0
 	  banner_page)
a1088d0
         continue;
a1088d0
 
a1088d0
+#ifdef WITH_LSPP
a1088d0
+     /*
a1088d0
+      * In LSPP mode refuse to honor the page-label
a1088d0
+      */
a1088d0
+      if (is_lspp_config() &&
a1088d0
+          !strcmp(attr->name, "page-label"))
a1088d0
+      {
a1088d0
+        cupsdLogMessage(CUPSD_LOG_DEBUG, "Ignoring page-label option due to LSPP mode");
a1088d0
+        continue;
a1088d0
+      }
a1088d0
+#endif /* WITH_LSPP */
a1088d0
+
a1088d0
      /*
a1088d0
       * Otherwise add them to the list...
a1088d0
       */
6fc5472
@@ -4472,6 +4656,18 @@ start_job(cupsd_job_t     *job,		/* I -
a1088d0
           cupsd_printer_t *printer)	/* I - Printer to print job */
a1088d0
 {
5b01ebd
   const char	*filename;		/* Support filename */
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  char			*audit_message = NULL;	/* Audit message string */
8936a5c
+  char			*printerfile = NULL;	/* Device file pointed to by the printer */
8936a5c
+  security_id_t		clisid;		/* SELinux SID for the client */
8936a5c
+  security_id_t		psid;		/* SELinux SID for the printer */
8936a5c
+  context_t		printercon;	/* Printer's context string */
8936a5c
+  struct stat		printerstat;	/* Printer's stat buffer */
8936a5c
+  security_context_t	devcon;		/* Printer's SELinux context */
8936a5c
+  struct avc_entry_ref	avcref;		/* Pointer to the access vector cache */
8936a5c
+  security_class_t	tclass;		/* Object class for the SELinux check */
8936a5c
+  access_vector_t	avr;		/* Access method being requested */
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
5b01ebd
 
5b01ebd
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_job(job=%p(%d), printer=%p(%s))",
6fc5472
@@ -4635,6 +4831,108 @@ start_job(cupsd_job_t     *job,		/* I -
7e278ad
   fcntl(job->side_pipes[1], F_SETFD,
7e278ad
 	fcntl(job->side_pipes[1], F_GETFD) | FD_CLOEXEC);
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  if (is_lspp_config())
8936a5c
+  {
8936a5c
+   /*
8936a5c
+    * Perform an access check before printing, but only if the printer starts with /dev/
8936a5c
+    */
8936a5c
+    printerfile = strstr(printer->device_uri, "/dev/");
8936a5c
+    if (printerfile == NULL && (strncmp(printer->device_uri, "file:/", 6) == 0))
08a85d3
+      printerfile = printer->device_uri + strlen("file:");
e7076f3
+
8936a5c
+    if (printerfile != NULL)
8936a5c
+    {
8936a5c
+      cupsdLogMessage(CUPSD_LOG_DEBUG,
8936a5c
+                      "StartJob: Attempting to check access on printer device %s", printerfile);
8936a5c
+      if (lstat(printerfile, &printerstat) < 0)
8936a5c
+      {
8936a5c
+	if (errno != ENOENT)
8936a5c
+	{
8936a5c
+	  cupsdLogMessage(CUPSD_LOG_ERROR, "StartJob: Unable to stat the printer");
a1088d0
+	  cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT, NULL);
8936a5c
+	  return ;
8936a5c
+	}
8936a5c
+	/*
8936a5c
+	 * The printer does not exist, so for now assume it's a FileDevice
8936a5c
+	 */
8936a5c
+	tclass = SECCLASS_FILE;
8936a5c
+	avr = FILE__WRITE;
8936a5c
+      }
8936a5c
+      else if (S_ISCHR(printerstat.st_mode))
8936a5c
+      {
8936a5c
+	tclass = SECCLASS_CHR_FILE;
8936a5c
+	avr = CHR_FILE__WRITE;
8936a5c
+      }
8936a5c
+      else if (S_ISREG(printerstat.st_mode))
8936a5c
+      {
8936a5c
+	tclass = SECCLASS_FILE;
8936a5c
+	avr = FILE__WRITE;
8936a5c
+      }
8936a5c
+      else
8936a5c
+      {
8936a5c
+	cupsdLogMessage(CUPSD_LOG_ERROR,
8936a5c
+			"StartJob: Printer is not a character device or regular file");
a1088d0
+	cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT, NULL);
8936a5c
+	return ;
8936a5c
+      }
9ed0ac5
+      static avc_initialized = 0;
9ed0ac5
+      if (!avc_initialized++)
9ed0ac5
+          avc_init("cupsd_dequeue_", NULL, NULL, NULL, NULL);
8936a5c
+      avc_entry_ref_init(&avcref);
8936a5c
+      if (avc_context_to_sid(job->scon, &clisid) != 0)
8936a5c
+      {
8936a5c
+        cupsdLogMessage(CUPSD_LOG_ERROR,
8936a5c
+                        "StartJob: Unable to determine the SELinux sid for the job");
a1088d0
+        cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT, NULL);
8936a5c
+        return ;
8936a5c
+      }
8936a5c
+      if (getfilecon(printerfile, &devcon) == -1)
8936a5c
+      {
8936a5c
+        cupsdLogMessage(CUPSD_LOG_ERROR, "StartJob: Unable to get the SELinux context of %s",
8936a5c
+                        printerfile);
a1088d0
+        cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT, NULL);
8936a5c
+        return ;
8936a5c
+      }
8936a5c
+      printercon = context_new(devcon);
8936a5c
+      cupsdLogMessage(CUPSD_LOG_DEBUG, "StartJob: printer context %s client context %s",
8936a5c
+                      context_str(printercon), job->scon);
8936a5c
+      context_free(printercon);
e7076f3
+
8936a5c
+      if (avc_context_to_sid(devcon, &psid) != 0)
8936a5c
+      {
8936a5c
+        cupsdLogMessage(CUPSD_LOG_ERROR,
8936a5c
+                        "StartJob: Unable to determine the SELinux sid for the printer");
8936a5c
+        freecon(devcon);
a1088d0
+        cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT, NULL);
8936a5c
+        return ;
8936a5c
+      }
8936a5c
+      freecon(devcon);
e7076f3
+
8936a5c
+      if (avc_has_perm(clisid, psid, tclass, avr, &avcref, NULL) != 0)
8936a5c
+      {
8936a5c
+       /*
8936a5c
+        * The access check failed, so cancel the job and send an audit message
8936a5c
+        */
8936a5c
+        if (AuditLog != -1)
8936a5c
+        {
8936a5c
+          audit_message = NULL;
8936a5c
+          cupsdSetStringf(&audit_message, "job=%d auid=%u acct=%s obj=%s canceled"
8936a5c
+                                          " unable to access printer=%s", job->id,
8936a5c
+                          job->auid, (job->username)?job->username:"?", job->scon, printer->name);
8936a5c
+          audit_log_user_message(AuditLog, AUDIT_USER_LABELED_EXPORT, audit_message,
8936a5c
+                                 ServerName, NULL, NULL, 0);
8936a5c
+          cupsdClearString(&audit_message);
8936a5c
+        }
ee6b344
+
a1088d0
+        cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT, NULL);
ee6b344
+
8936a5c
+        return ;
8936a5c
+      }
8936a5c
+    }
8936a5c
+  }
8936a5c
+#endif /* WITH_LSPP */
ee6b344
+
8936a5c
  /*
a1088d0
   * Now start the first file in the job...
8936a5c
   */
6fc5472
diff -up cups-1.7.1/scheduler/job.h.lspp cups-1.7.1/scheduler/job.h
6fc5472
--- cups-1.7.1/scheduler/job.h.lspp	2013-05-29 13:51:34.000000000 +0200
6fc5472
+++ cups-1.7.1/scheduler/job.h	2014-01-08 18:43:27.308821430 +0100
8936a5c
@@ -13,6 +13,13 @@
8936a5c
  *   file is missing or damaged, see the license at "http://www.cups.org/".
8936a5c
  */
8936a5c
 
8936a5c
+/* Copyright (C) 2005 Trusted Computer Solutions, Inc. */
8936a5c
+/* (c) Copyright 2005-2006 Hewlett-Packard Development Company, L.P. */
d042370
+
8936a5c
+#ifdef WITH_LSPP
8936a5c
+#include <selinux/selinux.h>
8936a5c
+#endif /* WITH_LSPP */
f4b6623
+
8936a5c
 /*
a1088d0
  * Constants...
8936a5c
  */
47ca1c0
@@ -82,6 +89,10 @@ struct cupsd_job_s			/**** Job request *
4743e10
   int			progress;	/* Printing progress */
47ca1c0
   int			num_keywords;	/* Number of PPD keywords */
47ca1c0
   cups_option_t		*keywords;	/* PPD keywords */
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  security_context_t	scon;		/* Security context of job */
8936a5c
+  uid_t			auid;		/* Audit loginuid for this job */
8936a5c
+#endif /* WITH_LSPP */
ca53cd4
 };
8936a5c
 
ca53cd4
 typedef struct cupsd_joblog_s		/**** Job log message ****/
6fc5472
diff -up cups-1.7.1/scheduler/main.c.lspp cups-1.7.1/scheduler/main.c
6fc5472
--- cups-1.7.1/scheduler/main.c.lspp	2014-01-08 18:43:27.265822025 +0100
6fc5472
+++ cups-1.7.1/scheduler/main.c	2014-01-08 18:43:27.308821430 +0100
389e11e
@@ -80,6 +82,9 @@
389e11e
 #  include <sys/param.h>
389e11e
 #endif /* HAVE_SYS_PARAM_H */
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+#  include <libaudit.h>
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
8936a5c
 /*
8936a5c
  * Local functions...
389e11e
@@ -143,6 +148,9 @@ main(int  argc,				/* I - Number of comm
0b2684f
 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
0b2684f
   struct sigaction	action;		/* Actions for POSIX signals */
0b2684f
 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
8936a5c
+#if WITH_LSPP
8936a5c
+  auditfail_t           failmode;       /* Action for audit_open failure */
8936a5c
+#endif /* WITH_LSPP */
5b01ebd
   int			run_as_child = 0;
5b01ebd
 					/* Needed for background fork/exec */
5b01ebd
 #ifdef __APPLE__
7632e7a
@@ -519,6 +527,25 @@ main(int  argc,				/* I - Number of comm
8936a5c
 #endif /* DEBUG */
8936a5c
   }
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  if ((AuditLog = audit_open()) < 0 )
8936a5c
+  {
8936a5c
+    if (get_auditfail_action(&failmode) == 0)
8936a5c
+    {
8936a5c
+      if (failmode == FAIL_LOG)
8936a5c
+      {
8936a5c
+        cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to connect to audit subsystem.");
8936a5c
+        AuditLog = -1;
8936a5c
+      }
8936a5c
+      else if (failmode == FAIL_TERMINATE)
8936a5c
+      {
8936a5c
+        fprintf(stderr, "cupsd: unable to start auditing, terminating");
8936a5c
+        return -1;
8936a5c
+      }
8936a5c
+    }
8936a5c
+  }
8936a5c
+#endif /* WITH_LSPP */
e7076f3
+
8936a5c
  /*
8936a5c
   * Set the timezone info...
8936a5c
   */
7632e7a
@@ -1197,6 +1224,11 @@ main(int  argc,				/* I - Number of comm
8936a5c
 
8936a5c
   cupsdStopSelect();
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  if (AuditLog != -1)
8936a5c
+    audit_close(AuditLog);
8936a5c
+#endif /* WITH_LSPP */
ee6b344
+
8936a5c
   return (!stop_scheduler);
8936a5c
 }
8936a5c
 
6fc5472
diff -up cups-1.7.1/scheduler/printers.c.lspp cups-1.7.1/scheduler/printers.c
6fc5472
--- cups-1.7.1/scheduler/printers.c.lspp	2014-01-08 18:43:27.161823463 +0100
6fc5472
+++ cups-1.7.1/scheduler/printers.c	2014-01-08 18:47:33.449416072 +0100
6fc5472
@@ -13,6 +13,8 @@
6fc5472
  * file is missing or damaged, see the license at "http://www.cups.org/".
8936a5c
  */
8936a5c
 
8936a5c
+/* (c) Copyright 2005-2006 Hewlett-Packard Development Company, L.P. */
ee6b344
+
8936a5c
 /*
8936a5c
  * Include necessary headers...
8936a5c
  */
6fc5472
@@ -37,6 +39,10 @@
47ca1c0
 #  include <asl.h>
47ca1c0
 #endif /* __APPLE__ */
8936a5c
 
8936a5c
+#ifdef WITH_LSPP
8936a5c
+#  include <libaudit.h>
8936a5c
+#  include <selinux/context.h>
8936a5c
+#endif /* WITH_LSPP */
47ca1c0
 
4743e10
 /*
4743e10
  * Local functions...
6fc5472
@@ -2023,6 +2029,13 @@ cupsdSetPrinterAttrs(cupsd_printer_t *p)
389e11e
   ipp_attribute_t *attr;		/* Attribute data */
389e11e
   char		*name,			/* Current user/group name */
389e11e
 		*filter;		/* Current filter */
8936a5c
+#ifdef WITH_LSPP
8936a5c
+  char		*audit_message;		/* Audit message string */
8936a5c
+  char		*printerfile;		/* Path to a local printer dev */
8936a5c
+  char		*rangestr;		/* Printer's range if its available */
8936a5c
+  security_context_t	devcon;		/* Printer SELinux context */
8936a5c
+  context_t	printercon;		/* context_t for the printer */
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
8936a5c
 
8936a5c
   DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p->name,
6fc5472
@@ -2145,6 +2158,45 @@ cupsdSetPrinterAttrs(cupsd_printer_t *p)
8936a5c
       attr->values[1].string.text = _cupsStrAlloc(Classification ?
8936a5c
 	                                   Classification : p->job_sheets[1]);
8936a5c
     }
8936a5c
+#ifdef WITH_LSPP
8936a5c
+    if (AuditLog != -1)
8936a5c
+    {
8936a5c
+      audit_message = NULL;
8936a5c
+      rangestr = NULL;
8936a5c
+      printercon = 0;
8936a5c
+      printerfile = strstr(p->device_uri, "/dev/");
8936a5c
+      if (printerfile == NULL && (strncmp(p->device_uri, "file:/", 6) == 0))
08a85d3
+        printerfile = p->device_uri + strlen("file:");
4f25cbd
+
8936a5c
+      if (printerfile != NULL)
8936a5c
+      {
8936a5c
+        if (getfilecon(printerfile, &devcon) == -1)
e0e1f21
+        {
e0e1f21
+          if(is_selinux_enabled())
39e46cc
+            cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSetPrinterAttrs: Unable to get printer context");
e0e1f21
+        }
8936a5c
+        else
8936a5c
+        {
8936a5c
+          printercon = context_new(devcon);
8936a5c
+          freecon(devcon);
8936a5c
+        }
8936a5c
+      }
e7076f3
+
8936a5c
+      if (printercon && context_range_get(printercon))
8936a5c
+        rangestr = strdup(context_range_get(printercon));
8936a5c
+      else
8936a5c
+        rangestr = strdup("unknown");
e7076f3
+
8936a5c
+      cupsdSetStringf(&audit_message, "printer=%s uri=%s banners=%s,%s range=%s",
cb06f24
+                      p->name, p->sanitized_device_uri, p->job_sheets[0], p->job_sheets[1], rangestr);
8936a5c
+      audit_log_user_message(AuditLog, AUDIT_LABEL_LEVEL_CHANGE, audit_message,
8936a5c
+                             ServerName, NULL, NULL, 1);
8936a5c
+      if (printercon)
8936a5c
+        context_free(printercon);
8936a5c
+      free(rangestr);
8936a5c
+      cupsdClearString(&audit_message);
8936a5c
+    }
8936a5c
+#endif /* WITH_LSPP */
8936a5c
   }
8936a5c
 
8936a5c
   p->raw    = 0;