cd39102
diff -up cups-2.2.5/config.h.in.lspp cups-2.2.5/config.h.in
cd39102
--- cups-2.2.5/config.h.in.lspp	2017-10-13 20:22:26.000000000 +0200
cd39102
+++ cups-2.2.5/config.h.in	2017-10-17 19:06:19.640228964 +0200
a5a6beb
@@ -730,4 +730,11 @@ 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_ */
cd39102
diff -up cups-2.2.5/config-scripts/cups-lspp.m4.lspp cups-2.2.5/config-scripts/cups-lspp.m4
cd39102
--- cups-2.2.5/config-scripts/cups-lspp.m4.lspp	2017-10-17 19:06:19.640228964 +0200
cd39102
+++ cups-2.2.5/config-scripts/cups-lspp.m4	2017-10-17 19:06:19.640228964 +0200
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
cd39102
diff -up cups-2.2.5/configure.ac.lspp cups-2.2.5/configure.ac
cd39102
--- cups-2.2.5/configure.ac.lspp	2017-10-13 20:22:26.000000000 +0200
cd39102
+++ cups-2.2.5/configure.ac	2017-10-17 19:06:19.640228964 +0200
5d14999
@@ -38,6 +38,8 @@ sinclude(config-scripts/cups-startup.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=""
cd39102
diff -up cups-2.2.5/filter/common.c.lspp cups-2.2.5/filter/common.c
cd39102
--- cups-2.2.5/filter/common.c.lspp	2017-10-13 20:22:26.000000000 +0200
cd39102
+++ cups-2.2.5/filter/common.c	2017-10-17 19:06:19.640228964 +0200
5d14999
@@ -17,6 +17,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
 
5d14999
@@ -299,6 +305,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
  /*
5d14999
@@ -321,6 +339,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
   */
5d14999
@@ -401,7 +537,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
 /*
cd39102
diff -up cups-2.2.5/filter/pstops.c.lspp cups-2.2.5/filter/pstops.c
cd39102
--- cups-2.2.5/filter/pstops.c.lspp	2017-10-13 20:22:26.000000000 +0200
cd39102
+++ cups-2.2.5/filter/pstops.c	2017-10-17 19:06:19.641228955 +0200
5d14999
@@ -3176,6 +3176,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
  /*
5d14999
@@ -3198,6 +3210,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
   */
5d14999
@@ -3276,7 +3406,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
 /*
cd39102
diff -up cups-2.2.5/Makedefs.in.lspp cups-2.2.5/Makedefs.in
cd39102
--- cups-2.2.5/Makedefs.in.lspp	2017-10-13 20:22:26.000000000 +0200
cd39102
+++ cups-2.2.5/Makedefs.in	2017-10-17 19:06:19.641228955 +0200
cd39102
@@ -161,7 +161,7 @@ LDFLAGS		=	-L../cgi-bin -L../cups -L../f
6fc5472
 			@LDFLAGS@ @RELROFLAGS@ @PIEFLAGS@ $(OPTIM)
7dc7717
 LINKCUPS	=	@LINKCUPS@ $(LIBGSSAPI) $(DNSSDLIBS) $(SSLLIBS) $(LIBZ)
6fc5472
 LINKCUPSIMAGE	=	@LINKCUPSIMAGE@
6fc5472
-LIBS		=	$(LINKCUPS) $(COMMONLIBS)
6fc5472
+LIBS		=	$(LINKCUPS) $(COMMONLIBS) @LIBAUDIT@ @LIBSELINUX@
6dd0746
 ONDEMANDFLAGS	=	@ONDEMANDFLAGS@
6dd0746
 ONDEMANDLIBS	=	@ONDEMANDLIBS@
6fc5472
 OPTIM		=	@OPTIM@
cd39102
diff -up cups-2.2.5/scheduler/client.c.lspp cups-2.2.5/scheduler/client.c
cd39102
--- cups-2.2.5/scheduler/client.c.lspp	2017-10-13 20:22:26.000000000 +0200
cd39102
+++ cups-2.2.5/scheduler/client.c	2017-10-17 19:06:19.689228556 +0200
5d14999
@@ -22,12 +22,20 @@
6dd0746
 #define _HTTP_NO_PRIVATE
8936a5c
 #include "cupsd.h"
8936a5c
 
8955d8c
+#ifndef _GNU_SOURCE
cb06f24
+#define _GNU_SOURCE
8955d8c
+#endif /* !defined(_GNU_SOURCE) */
5b01ebd
 #ifdef __APPLE__
5b01ebd
 #  include <libproc.h>
5b01ebd
 #endif /* __APPLE__ */
8955d8c
 #ifdef HAVE_TCPD_H
cb06f24
 #  include <tcpd.h>
cb06f24
 #endif /* HAVE_TCPD_H */
8936a5c
+#ifdef WITH_LSPP
8955d8c
+#  include <selinux/selinux.h>
8955d8c
+#  include <selinux/context.h>
8955d8c
+#  include <fcntl.h>
8936a5c
+#endif /* WITH_LSPP */
8936a5c
 
8955d8c
 
8936a5c
 /*
cd39102
@@ -268,6 +276,59 @@ 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
+
6dd0746
+    if (getsockopt(httpGetFd(con->http), 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
+      {
6dd0746
+        httpClose(con->http);
40da4be
+        cupsdLogClient(con, CUPSD_LOG_ERROR,
40da4be
+		       "Unable to determine client auid for client pid=%d",
40da4be
+		       cr.pid);
8936a5c
+        free(con);
8936a5c
+        return;
8936a5c
+      }
40da4be
+      cupsdLogClient(con, CUPSD_LOG_INFO,
40da4be
+		     "peer's pid=%d, uid=%d, gid=%d, auid=%d",
40da4be
+		     cr.pid, cr.uid, cr.gid, con->auid);
8936a5c
+    }
8936a5c
+    else
8936a5c
+    {
6dd0746
+      httpClose(con->http);
40da4be
+      cupsdLogClient(con, CUPSD_LOG_ERROR, "getsockopt() failed");
8936a5c
+      free(con);
8936a5c
+      return; 
8936a5c
+    }
e7076f3
+
8936a5c
+   /*
8936a5c
+    * get the context of the peer connection
8936a5c
+    */
6dd0746
+    if (getpeercon(httpGetFd(con->http), &con->scon))
8936a5c
+    {
6dd0746
+      httpClose(con->http);
40da4be
+      cupsdLogClient(con, CUPSD_LOG_ERROR, "getpeercon() failed");
8936a5c
+      free(con);
8936a5c
+      return; 
8936a5c
+    }
e7076f3
+
40da4be
+    cupsdLogClient(con, CUPSD_LOG_INFO, "client context=%s", con->scon);
8936a5c
+  }
8936a5c
+  else
8936a5c
+  {
40da4be
+    cupsdLogClient(con, CUPSD_LOG_DEBUG, "skipping getpeercon()");
8936a5c
+    cupsdSetString(&con->scon, UNKNOWN_SL);
8936a5c
+  }
8936a5c
+#endif /* WITH_LSPP */
8936a5c
+
4743e10
 #ifdef AF_LOCAL
6dd0746
   if (httpAddrFamily(httpGetAddress(con->http)) == AF_LOCAL)
5b01ebd
   {
cd39102
@@ -562,6 +623,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
 
6dd0746
   status = HTTP_STATUS_CONTINUE;
cd39102
@@ -1926,6 +1994,73 @@ cupsdReadClient(cupsd_client_t *con)	/*
8936a5c
             fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
8955d8c
 	  }
8955d8c
 
8936a5c
+#ifdef WITH_LSPP
8955d8c
+	  if (strncmp(con->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) != 0)
8955d8c
+	  {
8955d8c
+	    if (getfilecon(con->filename, &spoolcon) == -1)
8936a5c
+	    {
8955d8c
+	      cupsdSendError(con, HTTP_STATUS_SERVER_ERROR, CUPSD_AUTH_NONE);
8955d8c
+	      cupsdCloseClient(con);
8955d8c
+	      return;
8955d8c
+	    }
8955d8c
+	    clicon = context_new(con->scon);
8955d8c
+	    tmpcon = context_new(spoolcon);
8955d8c
+	    freecon(spoolcon);
8955d8c
+	    if (!clicon || !tmpcon)
8955d8c
+	    {
8955d8c
+	      cupsdSendError(con, HTTP_STATUS_SERVER_ERROR, CUPSD_AUTH_NONE);
8955d8c
+	      if (clicon)
8955d8c
+		context_free(clicon);
8955d8c
+	      if (tmpcon)
8955d8c
+		context_free(tmpcon);
8955d8c
+	      cupsdCloseClient(con);
8955d8c
+	      return;
8955d8c
+	    }
8955d8c
+	    clirange = (char *) context_range_get(clicon);
8955d8c
+	    if (clirange)
8955d8c
+	    {
8955d8c
+	      clirange = strdup(clirange);
8955d8c
+	      if ((cliclearance = strtok(clirange, "-")) != NULL)
8936a5c
+	      {
8955d8c
+		if (context_range_set(tmpcon, cliclearance) == -1)
8955d8c
+		{
8955d8c
+		  cupsdSendError(con, HTTP_STATUS_SERVER_ERROR, CUPSD_AUTH_NONE);
8955d8c
+		  free(clirange);
8936a5c
+		  context_free(tmpcon);
8955d8c
+		  context_free(clicon);
8955d8c
+		  cupsdCloseClient(con);
8955d8c
+		  return;
8955d8c
+		}
8936a5c
+	      }
8955d8c
+	      else
8936a5c
+	      {
8955d8c
+		if (context_range_set(tmpcon, (context_range_get(clicon))) == -1)
8936a5c
+		{
8955d8c
+		  cupsdSendError(con, HTTP_STATUS_SERVER_ERROR, CUPSD_AUTH_NONE);
8955d8c
+		  free(clirange);
8955d8c
+		  context_free(tmpcon);
8955d8c
+		  context_free(clicon);
8955d8c
+		  cupsdCloseClient(con);
8955d8c
+		  return;
8936a5c
+		}
8936a5c
+	      }
8955d8c
+	      free(clirange);
8955d8c
+	    }
8955d8c
+	    if (setfilecon(con->filename, context_str(tmpcon)) == -1)
8955d8c
+	    {
8955d8c
+	      cupsdSendError(con, HTTP_STATUS_SERVER_ERROR, CUPSD_AUTH_NONE);
8936a5c
+	      context_free(tmpcon);
8936a5c
+	      context_free(clicon);
8955d8c
+	      cupsdCloseClient(con);
8955d8c
+	      return;
8936a5c
+	    }
40da4be
+	    cupsdLogClient(con, CUPSD_LOG_DEBUG2, "%s set to %s", 
40da4be
+			   con->filename, context_str(tmpcon));
8955d8c
+	    context_free(tmpcon);
8955d8c
+	    context_free(clicon);
8955d8c
+	  }
8936a5c
+#endif /* WITH_LSPP */
8955d8c
+
6dd0746
 	  if (httpGetState(con->http) != HTTP_STATE_POST_SEND)
8955d8c
 	  {
8955d8c
 	    if (!httpWait(con->http, 0))
cd39102
@@ -3456,6 +3591,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)
8955d8c
+    return (uid_t) -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))
8955d8c
+    return (uid_t) -1;
38627a4
+
8936a5c
+  errno = 0;
8936a5c
+  buf[len] = 0;
8936a5c
+  uid = strtol(buf, 0, 10);
75d0e82
+
8936a5c
+  if (errno != 0)
8955d8c
+    return (uid_t) -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.
cd39102
diff -up cups-2.2.5/scheduler/client.h.lspp cups-2.2.5/scheduler/client.h
cd39102
--- cups-2.2.5/scheduler/client.h.lspp	2017-10-13 20:22:26.000000000 +0200
cd39102
+++ cups-2.2.5/scheduler/client.h	2017-10-17 19:06:19.690228548 +0200
5d14999
@@ -16,6 +16,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
  */
5d14999
@@ -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
 
6dd0746
 #define HTTP(con) ((con)->http)
5d14999
@@ -138,6 +149,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);
cd39102
diff -up cups-2.2.5/scheduler/conf.c.lspp cups-2.2.5/scheduler/conf.c
cd39102
--- cups-2.2.5/scheduler/conf.c.lspp	2017-10-17 19:06:19.637228989 +0200
cd39102
+++ cups-2.2.5/scheduler/conf.c	2017-10-17 19:06:19.691228540 +0200
5d14999
@@ -40,6 +40,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...
5d14999
@@ -131,6 +134,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[] =
a5a6beb
@@ -544,6 +551,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
  /*
a5a6beb
@@ -866,6 +876,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
 
cd39102
@@ -1286,7 +1315,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...
cd39102
@@ -3770,6 +3811,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.
cd39102
diff -up cups-2.2.5/scheduler/conf.h.lspp cups-2.2.5/scheduler/conf.h
cd39102
--- cups-2.2.5/scheduler/conf.h.lspp	2017-10-17 19:06:19.585229421 +0200
cd39102
+++ cups-2.2.5/scheduler/conf.h	2017-10-17 19:06:19.691228540 +0200
a5a6beb
@@ -250,6 +250,13 @@ VAR char		*ServerKeychain		VALUE(NULL);
6dd0746
 					/* Keychain holding cert + key */
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
+
5d14999
 #ifdef HAVE_ONDEMAND
6dd0746
 VAR int			IdleExitTimeout		VALUE(60);
47ca1c0
 					/* Time after which an idle cupsd will exit */
a5a6beb
@@ -268,6 +275,9 @@ VAR int			HaveServerCreds		VALUE(0);
5d14999
 VAR 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...
cd39102
diff -up cups-2.2.5/scheduler/cupsd.h.lspp cups-2.2.5/scheduler/cupsd.h
cd39102
--- cups-2.2.5/scheduler/cupsd.h.lspp	2017-10-17 19:06:19.626229080 +0200
cd39102
+++ cups-2.2.5/scheduler/cupsd.h	2017-10-17 19:06:19.691228540 +0200
5d14999
@@ -11,6 +11,8 @@
6dd0746
  * 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.
5d14999
@@ -36,13 +38,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
  */
cd39102
diff -up cups-2.2.5/scheduler/ipp.c.lspp cups-2.2.5/scheduler/ipp.c
cd39102
--- cups-2.2.5/scheduler/ipp.c.lspp	2017-10-17 19:06:19.599229305 +0200
cd39102
+++ cups-2.2.5/scheduler/ipp.c	2017-10-17 19:06:19.695228506 +0200
5d14999
@@ -14,6 +14,9 @@
a5a6beb
  * 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
  */
5d14999
@@ -37,6 +40,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...
5d14999
@@ -61,6 +72,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,
cd39102
@@ -1286,6 +1300,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))",
cd39102
@@ -1597,6 +1626,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
+      }
d8d7143
+      static int 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,
cd39102
@@ -1605,6 +1734,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;
cd39102
@@ -1794,6 +1949,29 @@ add_job(cupsd_client_t  *con,		/* I - Cl
78f6707
       ippSetString(job->attrs, &attr, 0, printer->job_sheets[0]);
78f6707
       ippSetString(job->attrs, &attr, 1, 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);
78f6707
+
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
 
cd39102
@@ -1824,6 +2002,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,
cd39102
@@ -1842,6 +2023,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") &&
cd39102
@@ -1862,6 +2046,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) &&
cd39102
@@ -1902,8 +2089,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 */
40da4be
       }
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
+	}
40da4be
+      }
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...
cd39102
@@ -3686,6 +3917,128 @@ 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
+  {
40da4be
+    cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		"Error while determining SELinux enforcement");
8936a5c
+    return -1;
8936a5c
+  }
40da4be
+  cupsdLogJob(job, CUPSD_LOG_DEBUG,
40da4be
+	      "check_context: client context %s job context %s",
40da4be
+	      con->scon, job->scon);
75d0e82
+
75d0e82
+
8936a5c
+ /*
8936a5c
+  * Initialize the avc engine...
8936a5c
+  */
22cc213
+
d8d7143
+  static int avc_initialized = 0;
9ed0ac5
+  if (! avc_initialized++)
8936a5c
+  {
9ed0ac5
+    if (avc_init("cupsd", NULL, NULL, NULL, NULL) < 0)
9ed0ac5
+    {
40da4be
+      cupsdLogJob(job, CUPSD_LOG_ERROR, "check_context: unable avc_init");
9ed0ac5
+      return -1;
9ed0ac5
+    } 
8936a5c
+  } 
8936a5c
+  if (avc_context_to_sid(con->scon, &clisid) != 0)
8936a5c
+  {
40da4be
+    cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		"check_context: unable to convert %s to SELinux sid",
40da4be
+		con->scon);
8936a5c
+    return -1;
8936a5c
+  }
85d072c
+  if (avc_context_to_sid(job->scon, &jobsid) != 0)
85d072c
+  {
40da4be
+    cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		"check_context: unable to convert %s to SELinux sid",
40da4be
+		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
+  {
40da4be
+    cupsdLogJob(job, CUPSD_LOG_INFO,
40da4be
+		"check_context: SELinux denied access "
40da4be
+		"based on the client context");
5dd9863
+
8936a5c
+    snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot, job->id);
8936a5c
+    if (getfilecon(filename, &spoolfilecon) == -1)
8936a5c
+    {
40da4be
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		  "check_context: Unable to get spoolfile context");
8936a5c
+      return -1;
8936a5c
+    }
8936a5c
+    if (avc_context_to_sid(spoolfilecon, &filesid) != 0)
8936a5c
+    {
40da4be
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		  "check_context: Unable to determine the "
40da4be
+		  "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
+    {
40da4be
+      cupsdLogJob(job, CUPSD_LOG_INFO,
40da4be
+		  "check_context: SELinux denied access to the spool file");
8936a5c
+      return 0;
8936a5c
+    }
40da4be
+    cupsdLogJob(job, CUPSD_LOG_INFO,
40da4be
+		"check_context: SELinux allowed access to the spool file");
8936a5c
+    return 1;
8936a5c
+  }
8936a5c
+  else
8936a5c
+    if (enforcing == 0)
40da4be
+      cupsdLogJob(job, CUPSD_LOG_INFO,
40da4be
+		  "check_context: allowing operation due to permissive mode");
8936a5c
+    else
40da4be
+      cupsdLogJob(job, CUPSD_LOG_INFO,
40da4be
+		  "check_context: SELinux allowed access based on the "
40da4be
+		  "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
  */
cd39102
@@ -4142,6 +4495,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,
cd39102
@@ -4177,6 +4539,85 @@ 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
+    {
40da4be
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		  "Unable to get the context of the banner file %s - %s",
40da4be
+		  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);
40da4be
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		  "copy_banner: Unable to get the SELinux contexts");
8936a5c
+      job->num_files --;
8936a5c
+      return (0);
8936a5c
+    }
d8d7143
+    jobrange = (char *) 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
+	{
40da4be
+	  cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		      "copy_banner: Unable to set the "
40da4be
+		      "level of the context for file %s - %s",
40da4be
+		      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
+	{
40da4be
+	  cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		      "copy_banner: Unable to set the "
40da4be
+		      "level of the context for file %s - %s",
40da4be
+		      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
+    {
40da4be
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		  "copy_banner: Unable to set the "
40da4be
+		  "context of the banner file %s - %s",
40da4be
+		  filename, strerror(errno));
8936a5c
+      context_free(jobcon);
8936a5c
+      context_free(tmpcon);
8936a5c
+      job->num_files --;
8936a5c
+      return (0);
8936a5c
+    }
40da4be
+    cupsdLogJob(job, CUPSD_LOG_DEBUG2, "copy_banner: %s set to %s",
40da4be
+		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...
cd39102
@@ -4271,6 +4712,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);
cd39102
@@ -6459,6 +6918,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
   */
cd39102
@@ -6856,6 +7331,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
 
cd39102
@@ -11487,6 +11967,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
   */
cd39102
diff -up cups-2.2.5/scheduler/job.c.lspp cups-2.2.5/scheduler/job.c
cd39102
--- cups-2.2.5/scheduler/job.c.lspp	2017-10-17 19:06:19.607229238 +0200
cd39102
+++ cups-2.2.5/scheduler/job.c	2017-10-17 19:06:19.696228498 +0200
5d14999
@@ -11,6 +11,9 @@
a5a6beb
  * 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
  */
5d14999
@@ -26,6 +29,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
7dc7717
@@ -547,6 +558,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,
7dc7717
@@ -1083,6 +1102,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",
7dc7717
@@ -1908,6 +1988,22 @@ 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
+    */
40da4be
+    cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		"Missing or bad security-context attribute "
40da4be
+		"in control file \"%s\"!",
40da4be
+		jobfile);
3244c62
+    goto error;
8936a5c
+  }
8936a5c
+#endif /* WITH_LSPP */
e7076f3
+
8d1f53d
   job->impressions = ippFindAttribute(job->attrs, "job-impressions-completed", IPP_TAG_INTEGER);
8d1f53d
   job->sheets      = ippFindAttribute(job->attrs, "job-media-sheets-completed", IPP_TAG_INTEGER);
8d1f53d
   job->job_sheets  = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME);
7dc7717
@@ -2321,6 +2417,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",
7dc7717
@@ -2343,6 +2447,78 @@ 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
+    {
40da4be
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		  "Unable to get context of job control file \"%s\" - %s.",
40da4be
+		  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);
40da4be
+      cupsdLogJob(job, 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
+	{
40da4be
+	  cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		      "Unable to set the range for "
40da4be
+		      "job control file \"%s\" - %s.",
40da4be
+		      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
+	{
40da4be
+	  cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		      "Unable to set the range for "
40da4be
+		      "job control file \"%s\" - %s.",
40da4be
+		      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
+    {
40da4be
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		  "Unable to set context of job control file \"%s\" - %s.",
40da4be
+		  filename, strerror(errno));
8936a5c
+      context_free(tmpcon);
8936a5c
+      context_free(jobcon);
8936a5c
+      return;
8936a5c
+    }
40da4be
+    cupsdLogJob(job, CUPSD_LOG_DEBUG2, "New spool file context=%s",
8d1f53d
+		 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,
cd39102
@@ -3989,6 +4165,19 @@ 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
+      {
40da4be
+        cupsdLogJob(job, CUPSD_LOG_DEBUG,
40da4be
+		    "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
       */
cd39102
@@ -4750,6 +4939,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 */
6dd0746
   ipp_attribute_t *cancel_after = ippFindAttribute(job->attrs,
6dd0746
 						   "job-cancel-after",
6dd0746
 						   IPP_TAG_INTEGER);
cd39102
@@ -4926,6 +5127,113 @@ 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
+    {
40da4be
+      cupsdLogJob(job, CUPSD_LOG_DEBUG,
40da4be
+		  "Attempting to check access on printer device %s",
40da4be
+		  printerfile);
8936a5c
+      if (lstat(printerfile, &printerstat) < 0)
8936a5c
+      {
8936a5c
+	if (errno != ENOENT)
8936a5c
+	{
40da4be
+	  cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		      "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
+      {
40da4be
+	cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		    "StartJob: Printer is not a character device or "
40da4be
+		    "regular file");
a1088d0
+	cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT, NULL);
8936a5c
+	return ;
8936a5c
+      }
d8d7143
+      static int 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
+      {
40da4be
+        cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		    "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
+      {
40da4be
+        cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		    "Unable to get the SELinux context of %s",
40da4be
+		    printerfile);
a1088d0
+        cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT, NULL);
8936a5c
+        return ;
8936a5c
+      }
8936a5c
+      printercon = context_new(devcon);
40da4be
+      cupsdLogJob(job, CUPSD_LOG_DEBUG,
40da4be
+		  "Printer context %s client context %s",
40da4be
+		  context_str(printercon), job->scon);
8936a5c
+      context_free(printercon);
e7076f3
+
8936a5c
+      if (avc_context_to_sid(devcon, &psid) != 0)
8936a5c
+      {
40da4be
+        cupsdLogJob(job, CUPSD_LOG_ERROR,
40da4be
+		    "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
   */
cd39102
diff -up cups-2.2.5/scheduler/job.h.lspp cups-2.2.5/scheduler/job.h
cd39102
--- cups-2.2.5/scheduler/job.h.lspp	2017-10-13 20:22:26.000000000 +0200
cd39102
+++ cups-2.2.5/scheduler/job.h	2017-10-17 19:06:19.696228498 +0200
5d14999
@@ -11,6 +11,13 @@
a5a6beb
  * 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
  */
5d14999
@@ -88,6 +95,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 ****/
cd39102
diff -up cups-2.2.5/scheduler/main.c.lspp cups-2.2.5/scheduler/main.c
cd39102
--- cups-2.2.5/scheduler/main.c.lspp	2017-10-17 19:06:19.637228989 +0200
cd39102
+++ cups-2.2.5/scheduler/main.c	2017-10-17 19:08:26.642173026 +0200
5d14999
@@ -56,6 +56,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...
cd39102
@@ -122,6 +125,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
 #ifdef __APPLE__
fce8f58
   int			use_sysman = 1;	/* Use system management functions? */
fce8f58
 #else
cd39102
@@ -516,6 +522,25 @@ main(int  argc,				/* I - Number of comm
fce8f58
     exit(errno);
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
  /*
cd39102
   * Let the system know we are busy while we bring up cupsd...
8936a5c
   */
cd39102
@@ -1227,6 +1252,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
 
cd39102
diff -up cups-2.2.5/scheduler/printers.c.lspp cups-2.2.5/scheduler/printers.c
cd39102
--- cups-2.2.5/scheduler/printers.c.lspp	2017-10-17 19:06:19.587229404 +0200
cd39102
+++ cups-2.2.5/scheduler/printers.c	2017-10-17 19:06:19.697228490 +0200
5d14999
@@ -11,6 +11,8 @@
a5a6beb
  * 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
  */
5d14999
@@ -35,6 +37,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...
7dc7717
@@ -2212,6 +2218,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,
7dc7717
@@ -2339,6 +2352,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;