Registration is now invite-only. Any user can make an invite, you need to create it here and give resulting link to someone to register.

About project

The Common Unix Printing System provides a portable printing layer for
UNIX(TM) operating systems. It has been developed by Easy Software Products
to promote a standard printing solution for all UNIX vendors and users.
CUPS provides the System V and Berkeley command-line interfaces.
This is the main package needed for CUPS servers (machines where a
printer is connected to or which host a queue for a network
printer). It can also be used on CUPS clients so that they simply pick
up broadcasted printer information from other CUPS servers and do not
need to be assigned to a specific CUPS server by an
/etc/cups/client.conf file.

Last commit

avatar
mikhailnov has added e64b57daf3
Make web interface work

Do not fail when AUID cannot be determined.
When trying to access the web interface, PID is 0 (why?!),
/proc/0/loginuid does not exist.

Log just like this:
I [17/Aug/2022:07:42:12 +0300] [Client 1] Unable to determine client auid for client pid=0
W [17/Aug/2022:07:42:12 +0300] [Client 1] peer's pid=0, uid=-1, gid=-1, auid=-1
E [17/Aug/2022:07:42:12 +0300] [Client 1] getpeercon() failed
W [17/Aug/2022:07:42:12 +0300] [Client 1] client context=UNKNOWN SL
and do not fail the connection.

Taken from ALT Linux:
https://git.altlinux.org/gears/c/cups.git?p=cups.git;a=blob;f=patches/ALT-lspp-context-via-tcp.patch;h=880311d5143a13d01cbc67b0f08c548354345b57;hb=HEAD

Files in

100644 | 2004 lines (1907 sloc) | 61.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
diff --git a/Makedefs.in b/Makedefs.in
index 8541b4976..a467ed5cb 100644
--- a/Makedefs.in
+++ b/Makedefs.in
@@ -165,7 +165,7 @@ LDFLAGS		=	-L../cgi-bin -L../cups -L../filter -L../ppdc \
 			@LDFLAGS@ @RELROFLAGS@ @PIEFLAGS@ $(OPTIM)
 LINKCUPS	=	@LINKCUPS@ $(LIBGSSAPI) $(DNSSDLIBS) $(SSLLIBS) $(LIBZ)
 LINKCUPSIMAGE	=	@LINKCUPSIMAGE@
-LIBS		=	$(LINKCUPS) $(COMMONLIBS)
+LIBS		=	$(LINKCUPS) $(COMMONLIBS) @LIBAUDIT@ @LIBSELINUX@
 ONDEMANDFLAGS	=	@ONDEMANDFLAGS@
 ONDEMANDLIBS	=	@ONDEMANDLIBS@
 OPTIM		=	@OPTIM@
diff --git a/config-scripts/cups-lspp.m4 b/config-scripts/cups-lspp.m4
new file mode 100644
index 000000000..a3cae89a5
--- /dev/null
+++ b/config-scripts/cups-lspp.m4
@@ -0,0 +1,36 @@
+dnl
+dnl   LSPP code for the Common UNIX Printing System (CUPS).
+dnl
+dnl   Copyright 2005-2006 by Hewlett-Packard Development Company, L.P.
+dnl
+dnl   This program is free software; you can redistribute it and/or modify
+dnl   it under the terms of the GNU General Public License as published by
+dnl   the Free Software Foundation; version 2.
+dnl
+dnl   This program is distributed in the hope that it will be useful, but
+dnl   WITHOUT ANY WARRANTY; without even the implied warranty of
+dnl   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+dnl   General Public License for more details.
+dnl
+dnl   You should have received a copy of the GNU General Public License
+dnl   along with this program; if not, write to the Free Software Foundation,
+dnl   Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA
+dnl
+
+dnl Are we trying to meet LSPP requirements
+AC_ARG_ENABLE(lspp, [  --enable-lspp           turn on auditing and label support, default=no])
+
+if test x"$enable_lspp" != xno; then
+    #case "$uname" in
+        #Linux)
+            AC_CHECK_LIB(audit,audit_log_user_message, [LIBAUDIT="-laudit" AC_SUBST(LIBAUDIT)])
+            AC_CHECK_HEADER(libaudit.h)
+            AC_CHECK_LIB(selinux,getpeercon, [LIBSELINUX="-lselinux" AC_SUBST(LIBSELINUX)])
+            AC_CHECK_HEADER(selinux/selinux.h)
+            AC_DEFINE(WITH_LSPP)
+            #;;
+        #*)
+            # All others
+            #;;
+    #esac
+fi
diff --git a/config.h.in b/config.h.in
index d81c59e36..79da02ced 100644
--- a/config.h.in
+++ b/config.h.in
@@ -737,4 +737,11 @@ static __inline int _cups_abs(int i) { return (i < 0 ? -i : i); }
 #  endif /* __GNUC__ || __STDC_VERSION__ */
 #endif /* !HAVE_ABS && !abs */
 
+/*
+ * Are we trying to meet LSPP requirements?
+ */
+
+#undef WITH_LSPP
+
+
 #endif /* !_CUPS_CONFIG_H_ */
diff --git a/configure.ac b/configure.ac
index 38db5f52d..725130fbd 100644
--- a/configure.ac
+++ b/configure.ac
@@ -38,6 +38,8 @@ sinclude(config-scripts/cups-startup.m4)
 sinclude(config-scripts/cups-defaults.m4)
 sinclude(config-scripts/cups-scripting.m4)
 
+sinclude(config-scripts/cups-lspp.m4)
+
 INSTALL_LANGUAGES=""
 UNINSTALL_LANGUAGES=""
 LANGFILES=""
diff --git a/filter/common.c b/filter/common.c
index 86b5f8ec6..65b2848a0 100644
--- a/filter/common.c
+++ b/filter/common.c
@@ -17,6 +17,12 @@
  * Include necessary headers...
  */
 
+#include "config.h"
+#ifdef WITH_LSPP
+#define _GNU_SOURCE
+#include <string.h>
+#endif /* WITH_LSPP */
+
 #include "common.h"
 #include <locale.h>
 
@@ -299,6 +305,18 @@ WriteLabelProlog(const char *label,	/* I - Page label */
 {
   const char	*classification;	/* CLASSIFICATION environment variable */
   const char	*ptr;			/* Temporary string pointer */
+#ifdef WITH_LSPP
+  int           i,                      /* counter */
+                n,                      /* counter */
+                lines,                  /* number of lines needed */
+                line_len,               /* index into tmp_label */
+                label_len,              /* length of the label in characters */
+                label_index,            /* index into the label */
+                longest,                /* length of the longest line */
+                longest_line,           /* index to the longest line */
+                max_width;              /* maximum width in characters */
+  char          **wrapped_label;        /* label with line breaks */
+#endif /* WITH_LSPP */
 
 
  /*
@@ -321,6 +339,124 @@ WriteLabelProlog(const char *label,	/* I - Page label */
     return;
   }
 
+#ifdef WITH_LSPP
+  if (strncmp(classification, "LSPP:", 5) == 0 && label == NULL)
+  {
+   /*
+    * Based on the 12pt fixed width font below determine the max_width
+    */
+    max_width = width / 8;
+    longest_line = 0;
+    longest = 0;
+    classification += 5; // Skip the "LSPP:"
+    label_len = strlen(classification);
+
+    if (label_len > max_width)
+    {
+      lines = 1 + (int)(label_len / max_width);
+      line_len = (int)(label_len / lines);
+      wrapped_label = malloc(sizeof(*wrapped_label) * lines);
+      label_index = i = n = 0;
+      while (classification[label_index])
+      {
+        if ((label_index + line_len) > label_len)
+          break;
+        switch (classification[label_index + line_len + i])
+        {
+          case ':':
+          case ',':
+          case '-':
+            i++;
+            wrapped_label[n++] = strndup(&classification[label_index], (line_len + i));
+            label_index += line_len + i;
+            i = 0;
+            break;
+          default:
+            i++;
+            break;
+        }
+        if ((i + line_len) == max_width)
+        {
+          wrapped_label[n++] = strndup(&(classification[label_index]), (line_len + i));
+          label_index = label_index + line_len + i;
+          i = 0;
+        }
+      }
+      wrapped_label[n] = strndup(&classification[label_index], label_len - label_index);
+    }
+    else
+    {
+      lines = 1;
+      wrapped_label = malloc(sizeof(*wrapped_label));
+      wrapped_label[0] = (char*)classification;
+    }
+
+    for (n = 0; n < lines; n++ )
+    {
+      printf("userdict/ESPp%c(", ('a' + n));
+      for (ptr = wrapped_label[n], i = 0; *ptr; ptr ++, i++)
+        if (*ptr < 32 || *ptr > 126)
+          printf("\\%03o", *ptr);
+        else
+        {
+          if (*ptr == '(' || *ptr == ')' || *ptr == '\\')
+            putchar('\\');
+
+          printf("%c", *ptr);
+        }
+      if (i > longest)
+      {
+        longest = i;
+        longest_line = n;
+      }
+      printf(")put\n");
+    }
+
+   /*
+    * For LSPP use a fixed width font so that line wrapping can be calculated
+    */
+
+    puts("userdict/ESPlf /Nimbus-Mono findfont 12 scalefont put");
+
+   /*
+    * Finally, the procedure to write the labels on the page...
+    */
+
+    printf("userdict/ESPwl{\n"
+           "  ESPlf setfont\n");
+    printf("  ESPp%c stringwidth pop dup 12 add exch -0.5 mul %.0f add\n ",
+           'a' + longest_line, width * 0.5f);
+    for (n = 1; n < lines; n++)
+      printf(" dup");
+    printf("\n  1 setgray\n");
+    printf("  dup 6 sub %.0f %d index %.0f ESPrf\n",
+           (bottom - 2.0), (2 + lines), 6.0 + (16.0 * lines));
+    printf("  dup 6 sub %.0f %d index %.0f ESPrf\n",
+           (top - 6.0 - (16.0 * lines)), (2 + lines), 4.0 + (16.0 * lines));
+    printf("  0 setgray\n");
+    printf("  dup 6 sub %.0f %d index %.0f ESPrs\n",
+           (bottom - 2.0), (2 + lines), 6.0 + (16.0 * lines));
+    printf("  dup 6 sub %.0f %d index %.0f ESPrs\n",
+           (top - 6.0 - (16.0 * lines)), (2 + lines), 4.0 + (16.0 * lines));
+    for (n = 0; n < lines; n ++)
+    {
+      printf("  dup %.0f moveto ESPp%c show\n",
+             bottom + 6.0 + ((lines - (n+1)) * 16.0), 'a' + n);
+      printf("  %.0f moveto ESPp%c show\n", top + 2.0 - ((n + 1) * 16.0), 'a' + n);
+    }
+    printf("  pop\n"
+           "}bind put\n");
+
+   /*
+    * Do some clean up at the end of the LSPP special case
+    */
+    free(wrapped_label);
+
+  }
+  else
+  {
+#endif /* !WITH_LSPP */
+  
  /*
   * Set the classification + page label string...
   */
@@ -401,7 +537,10 @@ WriteLabelProlog(const char *label,	/* I - Page label */
   printf("  %.0f moveto ESPpl show\n", top - 14.0);
   puts("pop");
   puts("}bind put");
+  }
+#ifdef WITH_LSPP
 }
+#endif /* WITH_LSPP */
 
 
 /*
diff --git a/filter/pstops.c b/filter/pstops.c
index fab60d93e..4001d0a61 100644
--- a/filter/pstops.c
+++ b/filter/pstops.c
@@ -3175,6 +3175,18 @@ write_label_prolog(pstops_doc_t *doc,	/* I - Document info */
 {
   const char	*classification;	/* CLASSIFICATION environment variable */
   const char	*ptr;			/* Temporary string pointer */
+#ifdef WITH_LSPP
+  int           i,                      /* counter */
+                n,                      /* counter */
+                lines,                  /* number of lines needed */
+                line_len,               /* index into tmp_label */
+                label_len,              /* length of the label in characters */
+                label_index,            /* index into the label */
+                longest,                /* length of the longest line */
+                longest_line,           /* index to the longest line */
+                max_width;              /* maximum width in characters */
+  char          **wrapped_label;        /* label with line breaks */
+#endif /* WITH_LSPP */
 
 
  /*
@@ -3197,6 +3209,124 @@ write_label_prolog(pstops_doc_t *doc,	/* I - Document info */
     return;
   }
 
+#ifdef WITH_LSPP
+  if (strncmp(classification, "LSPP:", 5) == 0 && label == NULL)
+  {
+   /*
+    * Based on the 12pt fixed width font below determine the max_width
+    */
+    max_width = width / 8;
+    longest_line = 0;
+    longest = 0;
+    classification += 5; // Skip the "LSPP:"
+    label_len = strlen(classification);
+
+    if (label_len > max_width)
+    {
+      lines = 1 + (int)(label_len / max_width);
+      line_len = (int)(label_len / lines);
+      wrapped_label = malloc(sizeof(*wrapped_label) * lines);
+      label_index = i = n = 0;
+      while (classification[label_index])
+      {
+        if ((label_index + line_len) > label_len)
+          break;
+        switch (classification[label_index + line_len + i])
+        {
+          case ':':
+          case ',':
+          case '-':
+            i++;
+            wrapped_label[n++] = strndup(&classification[label_index], (line_len + i));
+            label_index += line_len + i;
+            i = 0;
+            break;
+          default:
+            i++;
+            break;
+        }
+        if ((i + line_len) == max_width)
+        {
+          wrapped_label[n++] = strndup(&(classification[label_index]), (line_len + i));
+          label_index = label_index + line_len + i;
+          i = 0;
+        }
+      }
+      wrapped_label[n] = strndup(&classification[label_index], label_len - label_index);
+    }
+    else
+    {
+      lines = 1;
+      wrapped_label = malloc(sizeof(*wrapped_label));
+      wrapped_label[0] = (char*)classification;
+    }
+
+    for (n = 0; n < lines; n++ )
+    {
+      printf("userdict/ESPp%c(", ('a' + n));
+      for (ptr = wrapped_label[n], i = 0; *ptr; ptr ++, i++)
+        if (*ptr < 32 || *ptr > 126)
+          printf("\\%03o", *ptr);
+        else
+        {
+          if (*ptr == '(' || *ptr == ')' || *ptr == '\\')
+            putchar('\\');
+
+          printf("%c", *ptr);
+        }
+      if (i > longest)
+      {
+        longest = i;
+        longest_line = n;
+      }
+      printf(")put\n");
+    }
+
+   /*
+    * For LSPP use a fixed width font so that line wrapping can be calculated
+    */
+
+    puts("userdict/ESPlf /Nimbus-Mono findfont 12 scalefont put");
+
+   /*
+    * Finally, the procedure to write the labels on the page...
+    */
+
+    printf("userdict/ESPwl{\n"
+           "  ESPlf setfont\n");
+    printf("  ESPp%c stringwidth pop dup 12 add exch -0.5 mul %.0f add\n ",
+           'a' + longest_line, width * 0.5f);
+    for (n = 1; n < lines; n++)
+      printf(" dup");
+    printf("\n  1 setgray\n");
+    printf("  dup 6 sub %.0f %d index %.0f ESPrf\n",
+           (bottom - 2.0), (2 + lines), 6.0 + (16.0 * lines));
+    printf("  dup 6 sub %.0f %d index %.0f ESPrf\n",
+           (top - 6.0 - (16.0 * lines)), (2 + lines), 4.0 + (16.0 * lines));
+    printf("  0 setgray\n");
+    printf("  dup 6 sub %.0f %d index %.0f ESPrs\n",
+           (bottom - 2.0), (2 + lines), 6.0 + (16.0 * lines));
+    printf("  dup 6 sub %.0f %d index %.0f ESPrs\n",
+           (top - 6.0 - (16.0 * lines)), (2 + lines), 4.0 + (16.0 * lines));
+    for (n = 0; n < lines; n ++)
+    {
+      printf("  dup %.0f moveto ESPp%c show\n",
+             bottom + 6.0 + ((lines - (n+1)) * 16.0), 'a' + n);
+      printf("  %.0f moveto ESPp%c show\n", top + 2.0 - ((n + 1) * 16.0), 'a' + n);
+    }
+    printf("  pop\n"
+           "}bind put\n");
+
+   /*
+    * Do some clean up at the end of the LSPP special case
+    */
+    free(wrapped_label);
+
+  }
+  else
+  {
+#endif /* !WITH_LSPP */
+
  /*
   * Set the classification + page label string...
   */
@@ -3275,7 +3405,10 @@ write_label_prolog(pstops_doc_t *doc,	/* I - Document info */
   doc_printf(doc, "  %.0f moveto ESPpl show\n", top - 14.0);
   doc_puts(doc, "pop\n");
   doc_puts(doc, "}bind put\n");
+  }
+#ifdef WITH_LSPP
 }
+#endif /* WITH_LSPP */
 
 
 /*
diff --git a/scheduler/client.c b/scheduler/client.c
index 680508047..e3ca01ea3 100644
--- a/scheduler/client.c
+++ b/scheduler/client.c
@@ -22,12 +22,20 @@
 #define _HTTP_NO_PRIVATE
 #include "cupsd.h"
 
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif /* !defined(_GNU_SOURCE) */
 #ifdef __APPLE__
 #  include <libproc.h>
 #endif /* __APPLE__ */
 #ifdef HAVE_TCPD_H
 #  include <tcpd.h>
 #endif /* HAVE_TCPD_H */
+#ifdef WITH_LSPP
+#  include <selinux/selinux.h>
+#  include <selinux/context.h>
+#  include <fcntl.h>
+#endif /* WITH_LSPP */
 
 
 /*
@@ -268,6 +276,54 @@ cupsdAcceptClient(cupsd_listener_t *lis)/* I - Listener socket */
   }
 #endif /* HAVE_TCPD_H */
 
+#ifdef WITH_LSPP
+  if (is_lspp_config())
+  {
+    struct ucred cr;
+    unsigned int cl=sizeof(cr);
+
+    if (getsockopt(httpGetFd(con->http), SOL_SOCKET, SO_PEERCRED, &cr, &cl) == 0)
+    {
+     /*
+      * client_pid_to_auid() can be racey
+      * In this case the pid is based on a socket connected to the client
+      */
+      if ((con->auid = client_pid_to_auid(cr.pid)) == -1)
+      {
+        cupsdLogClient(con, CUPSD_LOG_INFO,
+		       "Unable to determine client auid for client pid=%d",
+		       cr.pid);
+      }
+      cupsdLogClient(con, CUPSD_LOG_WARN,
+		     "peer's pid=%d, uid=%d, gid=%d, auid=%d",
+		     cr.pid, cr.uid, cr.gid, con->auid);
+    }
+    else
+    {
+      httpClose(con->http);
+      cupsdLogClient(con, CUPSD_LOG_ERROR, "getsockopt() failed");
+      free(con);
+      return; 
+    }
+
+   /*
+    * get the context of the peer connection
+    */
+    if (getpeercon(httpGetFd(con->http), &con->scon))
+    {
+      cupsdSetString(&con->scon, UNKNOWN_SL);
+      cupsdLogClient(con, CUPSD_LOG_ERROR, "getpeercon() failed");
+    }
+
+    cupsdLogClient(con, CUPSD_LOG_WARN, "client context=%s", con->scon);
+  }
+  else
+  {
+    cupsdLogClient(con, CUPSD_LOG_DEBUG, "skipping getpeercon()");
+    cupsdSetString(&con->scon, UNKNOWN_SL);
+  }
+#endif /* WITH_LSPP */
+
 #ifdef AF_LOCAL
   if (httpAddrFamily(httpGetAddress(con->http)) == AF_LOCAL)
   {
@@ -562,6 +618,13 @@ cupsdReadClient(cupsd_client_t *con)	/* I - Client to read from */
   mime_type_t		*type;		/* MIME type of file */
   cupsd_printer_t	*p;		/* Printer */
   static unsigned	request_id = 0;	/* Request ID for temp files */
+#ifdef WITH_LSPP
+  security_context_t	spoolcon;	/* context of the job file */
+  context_t		clicon;		/* contex_t container for con->scon */
+  context_t		tmpcon;		/* temp context to swap the level */
+  char			*clirange;	/* SELinux sensitivity range */
+  char			*cliclearance;	/* SELinux low end clearance */
+#endif /* WITH_LSPP */
 
 
   status = HTTP_STATUS_CONTINUE;
@@ -1938,6 +2001,73 @@ cupsdReadClient(cupsd_client_t *con)	/* I - Client to read from */
             fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
 	  }
 
+#ifdef WITH_LSPP
+	  if ( (con->file > 0) && (strncmp(con->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) != 0) )
+	  {
+	    if (fgetfilecon(con->file, &spoolcon) == -1)
+	    {
+	      cupsdSendError(con, HTTP_STATUS_SERVER_ERROR, CUPSD_AUTH_NONE);
+	      cupsdCloseClient(con);
+	      return;
+	    }
+	    clicon = context_new(con->scon);
+	    tmpcon = context_new(spoolcon);
+	    freecon(spoolcon);
+	    if (!clicon || !tmpcon)
+	    {
+	      cupsdSendError(con, HTTP_STATUS_SERVER_ERROR, CUPSD_AUTH_NONE);
+	      if (clicon)
+		context_free(clicon);
+	      if (tmpcon)
+		context_free(tmpcon);
+	      cupsdCloseClient(con);
+	      return;
+	    }
+	    clirange = (char *) context_range_get(clicon);
+	    if (clirange)
+	    {
+	      clirange = strdup(clirange);
+	      if ((cliclearance = strtok(clirange, "-")) != NULL)
+	      {
+		if (context_range_set(tmpcon, cliclearance) == -1)
+		{
+		  cupsdSendError(con, HTTP_STATUS_SERVER_ERROR, CUPSD_AUTH_NONE);
+		  free(clirange);
+		  context_free(tmpcon);
+		  context_free(clicon);
+		  cupsdCloseClient(con);
+		  return;
+		}
+	      }
+	      else
+	      {
+		if (context_range_set(tmpcon, (context_range_get(clicon))) == -1)
+		{
+		  cupsdSendError(con, HTTP_STATUS_SERVER_ERROR, CUPSD_AUTH_NONE);
+		  free(clirange);
+		  context_free(tmpcon);
+		  context_free(clicon);
+		  cupsdCloseClient(con);
+		  return;
+		}
+	      }
+	      free(clirange);
+	    }
+	    if (setfilecon(con->filename, context_str(tmpcon)) == -1)
+	    {
+	      cupsdSendError(con, HTTP_STATUS_SERVER_ERROR, CUPSD_AUTH_NONE);
+	      context_free(tmpcon);
+	      context_free(clicon);
+	      cupsdCloseClient(con);
+	      return;
+	    }
+	    cupsdLogClient(con, CUPSD_LOG_DEBUG2, "%s set to %s", 
+			   con->filename, context_str(tmpcon));
+	    context_free(tmpcon);
+	    context_free(clicon);
+	  }
+#endif /* WITH_LSPP */
+
 	  if (httpGetState(con->http) != HTTP_STATE_POST_SEND)
 	  {
 	    if (!httpWait(con->http, 0))
@@ -3485,6 +3615,49 @@ is_path_absolute(const char *path)	/* I - Input path */
   return (1);
 }
 
+#ifdef WITH_LSPP
+/*
+ * 'client_pid_to_auid()' - Using the client's pid, read /proc and determine the loginuid.
+ */
+
+uid_t client_pid_to_auid(pid_t clipid)
+{
+  uid_t uid;
+  int len, in;
+  char buf[16] = {0};
+  char fname[32] = {0};
+
+
+ /*
+  * Hopefully this pid is still the one we are interested in.
+  */
+  snprintf(fname, 32, "/proc/%d/loginuid", clipid);
+  in = open(fname, O_NOFOLLOW|O_RDONLY);
+
+  if (in < 0)
+    return (uid_t) -1;
+
+  errno = 0;
+
+  do {
+    len = read(in, buf, sizeof(buf));
+  } while (len < 0 && errno == EINTR);
+
+  close(in);
+
+  if (len < 0 || len >= sizeof(buf))
+    return (uid_t) -1;
+
+  errno = 0;
+  buf[len] = 0;
+  uid = strtol(buf, 0, 10);
+
+  if (errno != 0)
+    return (uid_t) -1;
+  else
+    return uid;
+}
+#endif /* WITH_LSPP */
 
 /*
  * 'pipe_command()' - Pipe the output of a command to the remote client.
diff --git a/scheduler/client.h b/scheduler/client.h
index beddab0cc..7f4e8d426 100644
--- a/scheduler/client.h
+++ b/scheduler/client.h
@@ -16,6 +16,13 @@
 #endif /* HAVE_AUTHORIZATION_H */
 
 
+/* Copyright (C) 2005 Trusted Computer Solutions, Inc. */
+/* (c) Copyright 2005-2006 Hewlett-Packard Development Company, L.P. */
+
+#ifdef WITH_LSPP
+#include <selinux/selinux.h>
+#endif /* WITH_LSPP */
+
 /*
  * HTTP client structure...
  */
@@ -66,6 +73,10 @@ struct cupsd_client_s
 #ifdef HAVE_AUTHORIZATION_H
   AuthorizationRef	authref;	/* Authorization ref */
 #endif /* HAVE_AUTHORIZATION_H */
+#ifdef WITH_LSPP
+  security_context_t	scon;		/* Security context of connection */
+  uid_t			auid;		/* Audit loginuid of the client */
+#endif /* WITH_LSPP */
 };
 
 #define HTTP(con) ((con)->http)
@@ -139,6 +150,9 @@ extern void	cupsdStartListening(void);
 extern void	cupsdStopListening(void);
 extern void	cupsdUpdateCGI(void);
 extern void	cupsdWriteClient(cupsd_client_t *con);
+#ifdef WITH_LSPP
+extern uid_t	client_pid_to_auid(pid_t clipid);
+#endif /* WITH_LSPP */
 
 #ifdef HAVE_SSL
 extern int	cupsdEndTLS(cupsd_client_t *con);
diff --git a/scheduler/conf.c b/scheduler/conf.c
index 11c94d2e4..8f5f6e269 100644
--- a/scheduler/conf.c
+++ b/scheduler/conf.c
@@ -40,6 +40,9 @@
 #  define INADDR_NONE	0xffffffff
 #endif /* !INADDR_NONE */
 
+#ifdef WITH_LSPP
+#  include <libaudit.h>
+#endif /* WITH_LSPP */
 
 /*
  * Configuration variable structure...
@@ -131,6 +134,10 @@ static const cupsd_var_t	cupsd_vars[] =
   { "ServerName",		&ServerName,		CUPSD_VARTYPE_STRING },
   { "StrictConformance",	&StrictConformance,	CUPSD_VARTYPE_BOOLEAN },
   { "Timeout",			&Timeout,		CUPSD_VARTYPE_TIME },
+#ifdef WITH_LSPP
+  { "AuditLog",			&AuditLog,		CUPSD_VARTYPE_INTEGER },
+  { "PerPageLabels",		&PerPageLabels,		CUPSD_VARTYPE_BOOLEAN },
+#endif /* WITH_LSPP */
   { "WebInterface",		&WebInterface,		CUPSD_VARTYPE_BOOLEAN }
 };
 static const cupsd_var_t	cupsfiles_vars[] =
@@ -544,6 +551,9 @@ cupsdReadConfiguration(void)
   const char	*tmpdir;		/* TMPDIR environment variable */
   struct stat	tmpinfo;		/* Temporary directory info */
   cupsd_policy_t *p;			/* Policy */
+#ifdef WITH_LSPP
+  char		*audit_message;		/* Audit message string */
+#endif /* WITH_LSPP */
 
 
  /*
@@ -863,6 +873,25 @@ cupsdReadConfiguration(void)
 
   RunUser = getuid();
 
+#ifdef WITH_LSPP
+  if (AuditLog != -1)
+  {
+   /*
+    * ClassifyOverride is set during read_configuration, if its ON, report it now
+    */
+    if (ClassifyOverride)
+      audit_log_user_message(AuditLog, AUDIT_USYS_CONFIG,
+                "[Config] ClassifyOverride=enabled Users can override print banners",
+                ServerName, NULL, NULL, 1);
+   /*
+    * PerPageLabel is set during read_configuration, if its OFF, report it now
+    */
+    if (!PerPageLabels)
+      audit_log_user_message(AuditLog, AUDIT_USYS_CONFIG,
+                "[Config] PerPageLabels=disabled", ServerName, NULL, NULL, 1);
+  }
+#endif /* WITH_LSPP */
+
   cupsdLogMessage(CUPSD_LOG_INFO, "Remote access is %s.",
                   RemotePort ? "enabled" : "disabled");
 
@@ -1274,7 +1303,19 @@ cupsdReadConfiguration(void)
     cupsdClearString(&Classification);
 
   if (Classification)
+  {
     cupsdLogMessage(CUPSD_LOG_INFO, "Security set to \"%s\"", Classification);
+#ifdef WITH_LSPP
+    if (AuditLog != -1)
+    {
+      audit_message = NULL;
+      cupsdSetStringf(&audit_message, "[Config] Classification=%s", Classification);
+      audit_log_user_message(AuditLog, AUDIT_LABEL_LEVEL_CHANGE, audit_message,
+                             ServerName, NULL, NULL, 1);
+      cupsdClearString(&audit_message);
+    }
+#endif /* WITH_LSPP */
+  }
 
  /*
   * Check the MaxClients setting, and then allocate memory for it...
@@ -3832,6 +3873,16 @@ read_location(cups_file_t *fp,		/* I - Configuration file */
   return ((FatalErrors & CUPSD_FATAL_CONFIG) ? 0 : linenum);
 }
 
+#ifdef WITH_LSPP
+int is_lspp_config()
+{
+  /* Add "Classification off" to cupsd.conf to disable it, otherwise enabled */
+  if ((Classification != NULL) && (_cups_strcasecmp(Classification, "off") == 0))
+    return 0;
+  return 1;
+}
+#endif /* WITH_LSPP */
+
 
 /*
  * 'read_policy()' - Read a <Policy name> definition.
diff --git a/scheduler/conf.h b/scheduler/conf.h
index b608367ad..c6b6b3b8e 100644
--- a/scheduler/conf.h
+++ b/scheduler/conf.h
@@ -246,6 +246,13 @@ VAR char		*ServerKeychain		VALUE(NULL);
 					/* Keychain holding cert + key */
 #endif /* HAVE_SSL */
 
+#ifdef WITH_LSPP
+VAR int			AuditLog		VALUE(1),
+					/* File descriptor for audit */
+			PerPageLabels		VALUE(FALSE);
+					/* Put the label on each page */
+#endif /* WITH_LSPP */
+
 #ifdef HAVE_ONDEMAND
 VAR int			IdleExitTimeout		VALUE(60);
 					/* Time after which an idle cupsd will exit */
@@ -264,6 +271,9 @@ VAR int			HaveServerCreds		VALUE(0);
 VAR gss_cred_id_t	ServerCreds;	/* Server's GSS credentials */
 #endif /* HAVE_GSSAPI */
 
+#ifdef WITH_LSPP
+extern int		is_lspp_config(void);
+#endif /* WITH_LSPP */
 
 /*
  * Prototypes...
diff --git a/scheduler/cupsd.h b/scheduler/cupsd.h
index 54d2a4f22..812958642 100644
--- a/scheduler/cupsd.h
+++ b/scheduler/cupsd.h
@@ -11,6 +11,8 @@
  * file is missing or damaged, see the license at "http://www.cups.org/".
  */
 
+/* Copyright (C) 2005 Trusted Computer Solutions, Inc. */
+/* (c) Copyright 2005-2006 Hewlett-Packard Development Company, L.P. */
 
 /*
  * Include necessary headers.
@@ -36,6 +38,14 @@
 #  include <unistd.h>
 #endif /* _WIN32 */
 
+#include "config.h"
+#ifdef WITH_LSPP
+#  define MLS_CONFIG "mls"
+#  define TE_CONFIG "te"
+#  define SELINUX_CONFIG "SELinux"
+#  define UNKNOWN_SL "UNKNOWN SL"
+#endif /* WITH_LSPP */
+
 #include "mime.h"
 
 #if defined(HAVE_CDSASSL)
diff --git a/scheduler/ipp.c b/scheduler/ipp.c
index 07765a15b..723b344ce 100644
--- a/scheduler/ipp.c
+++ b/scheduler/ipp.c
@@ -14,6 +14,9 @@
  * missing or damaged, see the license at "http://www.cups.org/".
  */
 
+/* Copyright (C) 2005 Trusted Computer Solutions, Inc. */
+/* (c) Copyright 2005-2006 Hewlett-Packard Development Company, L.P. */
+
 /*
  * Include necessary headers...
  */
@@ -37,6 +40,14 @@ extern int mbr_check_membership_by_id(uuid_t user, gid_t group, int* ismember);
 #  endif /* HAVE_MEMBERSHIPPRIV_H */
 #endif /* __APPLE__ */
 
+#ifdef WITH_LSPP
+#include <libaudit.h>
+#include <selinux/selinux.h>
+#include <selinux/context.h>
+#include <selinux/avc.h>
+#include <selinux/flask.h>
+#include <selinux/av_permissions.h>
+#endif /* WITH_LSPP */
 
 /*
  * Local functions...
@@ -61,6 +72,9 @@ static void	cancel_all_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
 static void	cancel_job(cupsd_client_t *con, ipp_attribute_t *uri);
 static void	cancel_subscription(cupsd_client_t *con, int id);
 static int	check_rss_recipient(const char *recipient);
+#ifdef WITH_LSPP
+static int	check_context(cupsd_client_t *con, cupsd_job_t *job);
+#endif /* WITH_LSPP */
 static int	check_quotas(cupsd_client_t *con, cupsd_printer_t *p);
 static void	close_job(cupsd_client_t *con, ipp_attribute_t *uri);
 static void	copy_attrs(ipp_t *to, ipp_t *from, cups_array_t *ra,
@@ -1248,6 +1262,21 @@ add_job(cupsd_client_t  *con,		/* I - Client connection */
     "time-at-creation",
     "time-at-processing"
   };
+#ifdef WITH_LSPP
+  char		*audit_message;		/* Audit message string */
+  char		*printerfile;		/* device file pointed to by the printer */
+  char		*userheader = NULL;	/* User supplied job-sheets[0] */
+  char		*userfooter = NULL;	/* User supplied job-sheets[1] */
+  int		override = 0;		/* Was a banner overrode on a job */
+  security_id_t	clisid;			/* SELinux SID for the client */
+  security_id_t	psid;			/* SELinux SID for the printer */
+  context_t	printercon;		/* Printer's context string */
+  struct stat	printerstat;		/* Printer's stat buffer */
+  security_context_t	devcon;		/* Printer's SELinux context */
+  struct avc_entry_ref	avcref;		/* Pointer to the access vector cache */
+  security_class_t	tclass;		/* Object class for the SELinux check */
+  access_vector_t	avr;		/* Access method being requested */
+#endif /* WITH_LSPP */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job(%p[%d], %p(%s), %p(%s/%s))",
@@ -1576,6 +1605,106 @@ add_job(cupsd_client_t  *con,		/* I - Client connection */
 
   attr = ippFindAttribute(con->request, "requesting-user-name", IPP_TAG_NAME);
 
+#ifdef WITH_LSPP
+  if (is_lspp_config())
+  {
+    if (!con->scon || strncmp(con->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) == 0)
+    {
+      cupsdLogMessage(CUPSD_LOG_ERROR, "add_job: missing classification for connection \'%s\'!", printer->name);
+      send_ipp_status(con, IPP_INTERNAL_ERROR, _("Missing required security attributes."));
+      return (NULL);
+    }
+
+   /*
+    * Perform an access check so that if the user gets feedback at enqueue time
+    */
+
+    printerfile = strstr(printer->device_uri, "/dev/");
+    if (printerfile == NULL && (strncmp(printer->device_uri, "file:/", 6) == 0))
+      printerfile = printer->device_uri + strlen("file:");
+
+    if (printerfile != NULL)
+    {
+      cupsdLogMessage(CUPSD_LOG_DEBUG, "add_job: Attempting an access check on printer device %s",
+                      printerfile);
+
+      if (lstat(printerfile, &printerstat) < 0)
+      {
+	if (errno != ENOENT)
+	{
+	  send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Unable to stat the printer"));
+	  return (NULL);
+	}
+	/*
+	 * The printer does not exist, so for now assume it's a FileDevice
+	 */
+	tclass = SECCLASS_FILE;
+	avr = FILE__WRITE;
+      }
+      else if (S_ISCHR(printerstat.st_mode))
+      {
+	tclass = SECCLASS_CHR_FILE;
+	avr = CHR_FILE__WRITE;
+      }
+      else if (S_ISREG(printerstat.st_mode))
+      {
+	tclass = SECCLASS_FILE;
+	avr = FILE__WRITE;
+      }
+      else
+      {
+	send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Printer is not a character device or regular file"));
+	return (NULL);
+      }
+      static int avc_initialized = 0;
+      if (!avc_initialized++)
+          avc_init("cupsd_enqueue_", NULL, NULL, NULL, NULL);
+      avc_entry_ref_init(&avcref);
+      if (avc_context_to_sid(con->scon, &clisid) != 0)
+      {
+        send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Unable to get the SELinux sid of the client"));
+        return (NULL);
+      }
+      if (getfilecon(printerfile, &devcon) == -1)
+      {
+        send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Unable to get the SELinux context of the printer"));
+        return (NULL);
+      }
+      printercon = context_new(devcon);
+      cupsdLogMessage(CUPSD_LOG_DEBUG, "add_job: printer context %s client context %s",
+                      context_str(printercon), con->scon);
+      context_free(printercon);
+
+      if (avc_context_to_sid(devcon, &psid) != 0)
+      {
+        send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Unable to get the SELinux sid of the printer"));
+        freecon(devcon);
+        return (NULL);
+      }
+      freecon(devcon);
+      if (avc_has_perm(clisid, psid, tclass, avr, &avcref, NULL) != 0)
+      {
+       /*
+        * The access check failed, so cancel the job and send an audit message
+        */
+        if (AuditLog != -1)
+        {
+          audit_message = NULL;
+          cupsdSetStringf(&audit_message, "job=? auid=%u acct=%s obj=%s refused"
+                          " unable to access printer=%s", con->auid,
+                          con->username, con->scon, printer->name);
+          audit_log_user_message(AuditLog, AUDIT_USER_LABELED_EXPORT, audit_message,
+                                 ServerName, NULL, NULL, 0);
+          cupsdClearString(&audit_message);
+        }
+
+        send_ipp_status(con, IPP_NOT_AUTHORIZED, _("SELinux prohibits access to the printer"));
+        return (NULL);
+      }
+    }
+  }
+#endif /* WITH_LSPP */
+
   if ((job = cupsdAddJob(priority, printer->name)) == NULL)
   {
     send_ipp_status(con, IPP_INTERNAL_ERROR,
@@ -1584,6 +1713,32 @@ add_job(cupsd_client_t  *con,		/* I - Client connection */
     return (NULL);
   }
 
+#ifdef WITH_LSPP
+  if (is_lspp_config())
+  {
+   /*
+    * duplicate the security context and auid of the connection into the job structure
+    */
+    job->scon = strdup(con->scon);
+    job->auid = con->auid;
+
+   /* 
+    * add the security context to the request so that on a restart the security
+    * attributes will be able to be restored
+    */
+    ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "security-context", 
+		 NULL, job->scon);
+  }
+  else
+  {
+   /*
+    * Fill in the security context of the job as unlabeled
+    */
+    cupsdLogMessage(CUPSD_LOG_DEBUG, "add_job: setting context of job to %s", UNKNOWN_SL);
+    cupsdSetString(&job->scon, UNKNOWN_SL);
+  }
+#endif /* WITH_LSPP */
+
   job->dtype   = printer->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE);
   job->attrs   = con->request;
   job->dirty   = 1;
@@ -1771,6 +1926,29 @@ add_job(cupsd_client_t  *con,		/* I - Client connection */
       ippSetString(job->attrs, &attr, 0, printer->job_sheets[0]);
       ippSetString(job->attrs, &attr, 1, printer->job_sheets[1]);
     }
+#ifdef WITH_LSPP
+    else
+    {
+     /*
+      * The option was present, so capture the user supplied strings
+      */
+      userheader = strdup(attr->values[0].string.text);
+
+      if (attr->num_values > 1)
+        userfooter = strdup(attr->values[1].string.text);
+
+      if (Classification != NULL && (strcmp(userheader, Classification) == 0)
+          && userfooter &&(strcmp(userfooter, Classification) == 0))
+      {
+       /*
+        * Since both values are Classification, the user is not trying to Override
+        */
+        free(userheader);
+        if (userfooter) free(userfooter);
+        userheader = userfooter = NULL;
+      }
+    }
+#endif /* WITH_LSPP */
 
     job->job_sheets = attr;
 
@@ -1801,6 +1979,9 @@ add_job(cupsd_client_t  *con,		/* I - Client connection */
 	                		     "job-sheets=\"%s,none\", "
 					     "job-originating-user-name=\"%s\"",
 	              Classification, job->username);
+#ifdef WITH_LSPP
+	  override = 1;
+#endif /* WITH_LSPP */
 	}
 	else if (attr->num_values == 2 &&
 	         strcmp(attr->values[0].string.text,
@@ -1819,6 +2000,9 @@ add_job(cupsd_client_t  *con,		/* I - Client connection */
 					     "job-originating-user-name=\"%s\"",
 		      attr->values[0].string.text,
 		      attr->values[1].string.text, job->username);
+#ifdef WITH_LSPP
+	  override = 1;
+#endif /* WITH_LSPP */
 	}
 	else if (strcmp(attr->values[0].string.text, Classification) &&
 	         strcmp(attr->values[0].string.text, "none") &&
@@ -1839,6 +2023,9 @@ add_job(cupsd_client_t  *con,		/* I - Client connection */
 			"job-originating-user-name=\"%s\"",
 			attr->values[0].string.text,
 			attr->values[1].string.text, job->username);
+#ifdef WITH_LSPP
+	  override = 1;
+#endif /* WITH_LSPP */
         }
       }
       else if (strcmp(attr->values[0].string.text, Classification) &&
@@ -1879,8 +2066,52 @@ add_job(cupsd_client_t  *con,		/* I - Client connection */
 		      "job-sheets=\"%s\", "
 		      "job-originating-user-name=\"%s\"",
 		      Classification, job->username);
+#ifdef WITH_LSPP
+	override = 1;
+#endif /* WITH_LSPP */
+      }
+#ifdef WITH_LSPP
+      if (is_lspp_config() && AuditLog != -1)
+      {
+        audit_message = NULL;
+
+        if (userheader || userfooter)
+        {
+          if (!override)
+          {
+           /*
+            * The user overrode the banner, so audit it
+            */
+            cupsdSetStringf(&audit_message, "job=%d user supplied job-sheets=%s,%s"
+                            " using banners=%s,%s", job->id, userheader,
+                            userfooter, attr->values[0].string.text,
+                            (attr->num_values > 1) ? attr->values[1].string.text : "(null)");
+            audit_log_user_message(AuditLog, AUDIT_LABEL_OVERRIDE, audit_message,
+                                   ServerName, NULL, NULL, 1);
+	  }
+          else
+          {
+           /*
+            * The user tried to override the banner, audit the failure
+            */
+            cupsdSetStringf(&audit_message, "job=%d user supplied job-sheets=%s,%s"
+                            " ignored banners=%s,%s", job->id, userheader,
+                            userfooter, attr->values[0].string.text,
+                            (attr->num_values > 1) ? attr->values[1].string.text : "(null)");
+            audit_log_user_message(AuditLog, AUDIT_LABEL_OVERRIDE, audit_message,
+                                   ServerName, NULL, NULL, 0);
+	  }
+          cupsdClearString(&audit_message);
+	}
       }
+
+      if (userheader)
+        free(userheader);
+      if (userfooter)
+        free(userfooter);
+#endif /* WITH_LSPP */
     }
+    
 
    /*
     * See if we need to add the starting sheet...
@@ -3653,6 +3884,128 @@ check_rss_recipient(
 }
 
 
+#ifdef WITH_LSPP
+/*
+ * 'check_context()' - Check SELinux security context of a user and job
+ */
+
+static int				/* O - 1 if OK, 0 if not, -1 on error */
+check_context(cupsd_client_t *con,	/* I - Client connection */
+             cupsd_job_t    *job)	/* I - Job */
+{
+  int			enforcing;	/* is SELinux in enforcing mode */
+  char			filename[1024]; /* Filename of the spool file */
+  security_id_t		clisid;		/* SELinux SID of the client */
+  security_id_t		jobsid;		/* SELinux SID of the job */
+  security_id_t		filesid;	/* SELinux SID of the spool file */
+  struct avc_entry_ref	avcref;		/* AVC entry cache pointer */
+  security_class_t	tclass;		/* SELinux security class */
+  access_vector_t	avr;		/* SELinux access being queried */
+  security_context_t	spoolfilecon;	/* SELinux context of the spool file */
+
+
+ /*
+  * Validate the input to be sure there are contexts to work with...
+  */
+
+  if (con->scon == NULL || job->scon == NULL
+      || strncmp(con->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) == 0
+      || strncmp(job->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) == 0)
+    return -1;
+
+  if ((enforcing = security_getenforce()) == -1)
+  {
+    cupsdLogJob(job, CUPSD_LOG_ERROR,
+		"Error while determining SELinux enforcement");
+    return -1;
+  }
+  cupsdLogJob(job, CUPSD_LOG_WARN,
+	      "check_context: client context %s job context %s",
+	      con->scon, job->scon);
+
+
+ /*
+  * Initialize the avc engine...
+  */
+
+  static int avc_initialized = 0;
+  if (! avc_initialized++)
+  {
+    if (avc_init("cupsd", NULL, NULL, NULL, NULL) < 0)
+    {
+      cupsdLogJob(job, CUPSD_LOG_ERROR, "check_context: unable avc_init");
+      return -1;
+    } 
+  } 
+  if (avc_context_to_sid(con->scon, &clisid) != 0)
+  {
+    cupsdLogJob(job, CUPSD_LOG_ERROR,
+		"check_context: unable to convert %s to SELinux sid",
+		con->scon);
+    return -1;
+  }
+  if (avc_context_to_sid(job->scon, &jobsid) != 0)
+  {
+    cupsdLogJob(job, CUPSD_LOG_ERROR,
+		"check_context: unable to convert %s to SELinux sid",
+		job->scon);
+    return -1;
+  }
+  avc_entry_ref_init(&avcref);
+  tclass = SECCLASS_FILE;
+  avr = FILE__READ;
+
+ /*
+  * Perform the check with the client as the subject, first with the job as the object
+  *   if that fails then with the spool file as the object...
+  */
+
+  if (avc_has_perm_noaudit(clisid, jobsid, tclass, avr, &avcref, NULL) != 0)
+  {
+    cupsdLogJob(job, CUPSD_LOG_WARN,
+		"check_context: SELinux denied access "
+		"based on the client context");
+
+    snprintf(filename, sizeof(filename), "%s/c%05d", RequestRoot, job->id);
+    if (getfilecon(filename, &spoolfilecon) == -1)
+    {
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
+		  "check_context: Unable to get spoolfile context");
+      return -1;
+    }
+    if (avc_context_to_sid(spoolfilecon, &filesid) != 0)
+    {
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
+		  "check_context: Unable to determine the "
+		  "SELinux sid for the spool file");
+      freecon(spoolfilecon);
+      return -1;
+    }
+    freecon(spoolfilecon);
+    if (avc_has_perm_noaudit(clisid, filesid, tclass, avr, &avcref, NULL) != 0)
+    {
+      cupsdLogJob(job, CUPSD_LOG_WARN,
+		  "check_context: SELinux denied access to the spool file");
+      return 0;
+    }
+    cupsdLogJob(job, CUPSD_LOG_DEBUG,
+		"check_context: SELinux allowed access to the spool file");
+    return 1;
+  }
+  else
+    if (enforcing == 0)
+      cupsdLogJob(job, CUPSD_LOG_DEBUG,
+		  "check_context: allowing operation due to permissive mode");
+    else
+      cupsdLogJob(job, CUPSD_LOG_DEBUG,
+		  "check_context: SELinux allowed access based on the "
+		  "client context");
+
+  return 1;
+}
+#endif /* WITH_LSPP */
+
+
 /*
  * 'check_quotas()' - Check quotas for a printer and user.
  */
@@ -4109,6 +4462,15 @@ copy_banner(cupsd_client_t *con,	/* I - Client connection */
   char		attrname[255],		/* Name of attribute */
 		*s;			/* Pointer into name */
   ipp_attribute_t *attr;		/* Attribute */
+#ifdef WITH_LSPP
+  const char	*mls_label;		/* SL of print job */
+  char		*jobrange;		/* SELinux sensitivity range */
+  char		*jobclearance;		/* SELinux low end clearance */
+  context_t	jobcon;			/* SELinux context of the job */
+  context_t	tmpcon;			/* Temp context to set the level */
+  security_context_t	spoolcon;	/* Context of the file in the spool */
+#endif /* WITH_LSPP */
+
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2,
@@ -4144,6 +4506,85 @@ copy_banner(cupsd_client_t *con,	/* I - Client connection */
 
   fchmod(cupsFileNumber(out), 0640);
   fchown(cupsFileNumber(out), RunUser, Group);
+#ifdef WITH_LSPP
+  if (job->scon != NULL &&
+      strncmp(job->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) != 0)
+  {
+    if (getfilecon(filename, &spoolcon) == -1)
+    {
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
+		  "Unable to get the context of the banner file %s - %s",
+		  filename, strerror(errno));
+      job->num_files --;
+      return (0);
+    }
+    tmpcon = context_new(spoolcon);
+    jobcon = context_new(job->scon);
+    freecon(spoolcon);
+    if (!tmpcon || !jobcon)
+    {
+      if (tmpcon)
+        context_free(tmpcon);
+      if (jobcon)
+        context_free(jobcon);
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
+		  "copy_banner: Unable to get the SELinux contexts");
+      job->num_files --;
+      return (0);
+    }
+    jobrange = (char *) context_range_get(jobcon);
+    if (jobrange)
+    {
+      jobrange = strdup(jobrange);
+      if ((jobclearance = strtok(jobrange, "-")) != NULL)
+      {
+	if (context_range_set(tmpcon, jobclearance) == -1)
+	{
+	  cupsdLogJob(job, CUPSD_LOG_ERROR,
+		      "copy_banner: Unable to set the "
+		      "level of the context for file %s - %s",
+		      filename, strerror(errno));
+	  free(jobrange);
+	  context_free(jobcon);
+	  context_free(tmpcon);
+	  job->num_files --;
+	  return (0);
+	}
+      }
+      else
+      {
+	if (context_range_set(tmpcon, (context_range_get(jobcon))) == -1)
+	{
+	  cupsdLogJob(job, CUPSD_LOG_ERROR,
+		      "copy_banner: Unable to set the "
+		      "level of the context for file %s - %s",
+		      filename, strerror(errno));
+	  free(jobrange);
+	  context_free(jobcon);
+	  context_free(tmpcon);
+	  job->num_files --;
+	  return (0);
+	}
+      }
+      free(jobrange);
+    }
+    if (setfilecon(filename, context_str(tmpcon)) == -1)
+    {
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
+		  "copy_banner: Unable to set the "
+		  "context of the banner file %s - %s",
+		  filename, strerror(errno));
+      context_free(jobcon);
+      context_free(tmpcon);
+      job->num_files --;
+      return (0);
+    }
+    cupsdLogJob(job, CUPSD_LOG_DEBUG2, "copy_banner: %s set to %s",
+		filename, context_str(tmpcon));
+    context_free(jobcon);
+    context_free(tmpcon);
+  }
+#endif /* WITH_LSPP */
 
  /*
   * Try the localized banner file under the subdirectory...
@@ -4238,6 +4679,24 @@ copy_banner(cupsd_client_t *con,	/* I - Client connection */
       else
         s = attrname;
 
+#ifdef WITH_LSPP
+      if (strcmp(s, "mls-label") == 0)
+      {
+        if (job->scon != NULL && strncmp(job->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) != 0)
+        {
+          jobcon = context_new(job->scon);
+          if (_cups_strcasecmp(name, MLS_CONFIG) == 0)
+            mls_label = context_range_get(jobcon);
+          else if (_cups_strcasecmp(name, TE_CONFIG) == 0)
+            mls_label = context_type_get(jobcon);
+          else // default to using the whole context string
+            mls_label = context_str(jobcon);
+          cupsFilePuts(out, mls_label);
+          context_free(jobcon);
+        }
+        continue;
+      }
+#endif /* WITH_LSPP */
       if (!strcmp(s, "printer-name"))
       {
         cupsFilePuts(out, job->dest);
@@ -6475,6 +6934,22 @@ get_job_attrs(cupsd_client_t  *con,	/* I - Client connection */
 
   exclude = cupsdGetPrivateAttrs(policy, con, printer, job->username);
 
+
+#ifdef WITH_LSPP
+ /*
+  * Check SELinux...
+  */
+  if (is_lspp_config() && check_context(con, job) != 1)
+  {
+   /*
+    * Unfortunately we have to lie to the user...
+    */
+    send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
+    return;
+  }
+#endif /* WITH_LSPP */
+
+
  /*
   * Copy attributes...
   */
@@ -6872,6 +7347,11 @@ get_jobs(cupsd_client_t  *con,		/* I - Client connection */
       if (username[0] && _cups_strcasecmp(username, job->username))
 	continue;
 
+#ifdef WITH_LSPP
+      if (is_lspp_config() && check_context(con, job) != 1)
+	continue;
+#endif /* WITH_LSPP */
+
       if (count > 0)
 	ippAddSeparator(con->response);
 
@@ -11470,6 +11950,11 @@ validate_user(cupsd_job_t    *job,	/* I - Job */
 
   strlcpy(username, get_username(con), userlen);
 
+#ifdef WITH_LSPP
+  if (is_lspp_config() && check_context(con, job) != 1)
+    return 0;
+#endif /* WITH_LSPP */
+
  /*
   * Check the username against the owner...
   */
diff --git a/scheduler/job.c b/scheduler/job.c
index a8373f5ba..273683e1c 100644
--- a/scheduler/job.c
+++ b/scheduler/job.c
@@ -11,6 +11,9 @@
  * missing or damaged, see the license at "http://www.cups.org/".
  */
 
+/* Copyright (C) 2005 Trusted Computer Solutions, Inc. */
+/* (c) Copyright 2005-2006 Hewlett-Packard Development Company, L.P. */
+
 /*
  * Include necessary headers...
  */
@@ -26,6 +29,14 @@
 #  endif /* HAVE_IOKIT_PWR_MGT_IOPMLIBPRIVATE_H */
 #endif /* __APPLE__ */
 
+#ifdef WITH_LSPP
+#include <libaudit.h>
+#include <selinux/selinux.h>
+#include <selinux/context.h>
+#include <selinux/avc.h>
+#include <selinux/flask.h>
+#include <selinux/av_permissions.h>
+#endif /* WITH_LSPP */
 
 /*
  * Design Notes for Job Management
@@ -547,6 +558,14 @@ cupsdContinueJob(cupsd_job_t *job)	/* I - Job */
 					/* PRINTER_STATE_REASONS env var */
 			rip_max_cache[255];
 					/* RIP_MAX_CACHE env variable */
+#ifdef WITH_LSPP
+  char			*audit_message = NULL;	/* Audit message string */
+  context_t		jobcon;		/* SELinux context of the job */
+  char			*label_template = NULL;	/* SL to put in classification
+						   env var */
+  const char		*mls_label = NULL;	/* SL to put in classification
+						   env var */
+#endif /* WITH_LSPP */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2,
@@ -1065,6 +1084,67 @@ cupsdContinueJob(cupsd_job_t *job)	/* I - Job */
   if (final_content_type[0])
     envp[envc ++] = final_content_type;
 
+#ifdef WITH_LSPP
+  if (is_lspp_config())
+  {
+    if (!job->scon || strncmp(job->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) == 0)
+    {
+      if (AuditLog != -1)
+      {
+        audit_message = NULL;
+        cupsdSetStringf(&audit_message, "job=%d auid=%u acct=%s printer=%s title=%s",
+                        job->id, job->auid, job->username, job->printer->name, title);
+        audit_log_user_message(AuditLog, AUDIT_USER_UNLABELED_EXPORT, audit_message,
+                               ServerName, NULL, NULL, 1);
+        cupsdClearString(&audit_message);
+      }
+    }
+    else 
+    {
+      jobcon = context_new(job->scon);
+
+      if ((attr = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME)) == NULL)
+        label_template = strdup(Classification);
+      else if (attr->num_values > 1 &&
+               strcmp(attr->values[1].string.text, "none") != 0)
+        label_template = strdup(attr->values[1].string.text);
+      else
+        label_template = strdup(attr->values[0].string.text);
+
+      if (_cups_strcasecmp(label_template, MLS_CONFIG) == 0)
+        mls_label = context_range_get(jobcon);
+      else if (_cups_strcasecmp(label_template, TE_CONFIG) == 0)
+        mls_label = context_type_get(jobcon);
+      else if (_cups_strcasecmp(label_template, SELINUX_CONFIG) == 0)
+        mls_label = context_str(jobcon);
+      else
+        mls_label = label_template;
+
+      if (mls_label && (PerPageLabels || banner_page))
+      {
+        snprintf(classification, sizeof(classification), "CLASSIFICATION=LSPP:%s", mls_label);
+        envp[envc ++] = classification;
+      }
+
+      if ((AuditLog != -1) && !banner_page)
+      {
+        audit_message = NULL;
+        cupsdSetStringf(&audit_message, "job=%d auid=%u acct=%s printer=%s title=%s"
+                        " obj=%s label=%s", job->id, job->auid, job->username,
+                        job->printer->name, title, job->scon, mls_label?mls_label:"none");
+        audit_log_user_message(AuditLog, AUDIT_USER_LABELED_EXPORT, audit_message,
+                               ServerName, NULL, NULL, 1);
+        cupsdClearString(&audit_message);
+      }
+      context_free(jobcon);
+      free(label_template);
+    }
+  }
+  else
+   /*
+    * Fall through to the non-LSPP behavior
+    */
+#endif /* WITH_LSPP */
   if (Classification && !banner_page)
   {
     if ((attr = ippFindAttribute(job->attrs, "job-sheets",
@@ -1844,6 +1924,22 @@ cupsdLoadJob(cupsd_job_t *job)		/* I - Job */
       ippSetString(job->attrs, &job->reasons, 0, "none");
   }
 
+#ifdef WITH_LSPP
+  if ((attr = ippFindAttribute(job->attrs, "security-context", IPP_TAG_NAME)) != NULL)
+    cupsdSetString(&job->scon, attr->values[0].string.text);
+  else if (is_lspp_config())
+  {
+   /*
+    * There was no security context so delete the job
+    */
+    cupsdLogJob(job, CUPSD_LOG_ERROR,
+		"Missing or bad security-context attribute "
+		"in control file \"%s\"!",
+		jobfile);
+    goto error;
+  }
+#endif /* WITH_LSPP */
+
   job->impressions = ippFindAttribute(job->attrs, "job-impressions-completed", IPP_TAG_INTEGER);
   job->sheets      = ippFindAttribute(job->attrs, "job-media-sheets-completed", IPP_TAG_INTEGER);
   job->job_sheets  = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME);
@@ -2259,6 +2355,15 @@ cupsdSaveJob(cupsd_job_t *job)		/* I - Job */
 {
   char		filename[1024];		/* Job control filename */
   cups_file_t	*fp;			/* Job file */
+  int jfd; /* Job file descriptor */
+#ifdef WITH_LSPP
+  security_context_t	spoolcon;	/* context of the job control file */
+  context_t		jobcon;		/* contex_t container for job->scon */
+  context_t		tmpcon;		/* Temp context to swap the level */
+  char			*jobclearance;	/* SELinux low end clearance */
+  const char		*jobrange;	/* SELinux sensitivity range */
+  char			*jobrange_copy;	/* SELinux sensitivity range */
+#endif /* WITH_LSPP */
 
 
   cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSaveJob(job=%p(%d)): job->attrs=%p",
@@ -2279,8 +2384,81 @@ cupsdSaveJob(cupsd_job_t *job)		/* I - Job */
   if ((fp = cupsdCreateConfFile(filename, ConfigFilePerm & 0600)) == NULL)
     return;
 
+  jfd = cupsFileNumber(fp);
   fchown(cupsFileNumber(fp), RunUser, Group);
 
+#ifdef WITH_LSPP
+  if ((jfd > 0) && job->scon && strncmp(job->scon, UNKNOWN_SL, strlen(UNKNOWN_SL)) != 0)
+  {
+    if (fgetfilecon(jfd, &spoolcon) == -1)
+    {
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
+		  "Unable to get context of job control file \"%s\" - %s.",
+		  filename, strerror(errno));
+      return;
+    }
+    jobcon = context_new(job->scon);
+    tmpcon = context_new(spoolcon);
+    freecon(spoolcon);
+    if (!jobcon || !tmpcon)
+    {
+      if (jobcon)
+        context_free(jobcon);
+      if (tmpcon)
+        context_free(tmpcon);
+      cupsdLogJob(job, CUPSD_LOG_ERROR, "Unable to get SELinux contexts");
+      return;
+    }
+    jobrange = context_range_get(jobcon);
+    if (jobrange)
+    {
+      jobrange_copy = strdup(jobrange);
+      if ((jobclearance = strtok(jobrange_copy, "-")) != NULL)
+      {
+	if (context_range_set(tmpcon, jobclearance) == -1)
+	{
+	  cupsdLogJob(job, CUPSD_LOG_ERROR,
+		      "Unable to set the range for "
+		      "job control file \"%s\" - %s.",
+		      filename, strerror(errno));
+	  free(jobrange_copy);
+	  context_free(tmpcon);
+	  context_free(jobcon);
+	  return;
+	}
+      }
+      else
+      {
+	if (context_range_set(tmpcon, (context_range_get(jobcon))) == -1)
+	{
+	  cupsdLogJob(job, CUPSD_LOG_ERROR,
+		      "Unable to set the range for "
+		      "job control file \"%s\" - %s.",
+		      filename, strerror(errno));
+	  free(jobrange_copy);
+	  context_free(tmpcon);
+	  context_free(jobcon);
+	  return;
+	}
+      }
+      free(jobrange_copy);
+    }
+    if (fsetfilecon(jfd, context_str(tmpcon)) == -1)
+    {
+      cupsdLogJob(job, CUPSD_LOG_ERROR,
+		  "Unable to set context of job control file \"%s\" - %s.",
+		  filename, strerror(errno));
+      context_free(tmpcon);
+      context_free(jobcon);
+      return;
+    }
+    cupsdLogJob(job, CUPSD_LOG_DEBUG, "New spool file context=%s",
+		 context_str(tmpcon));
+    context_free(tmpcon);
+    context_free(jobcon);
+  }
+#endif /* WITH_LSPP */
+
   job->attrs->state = IPP_IDLE;
 
   if (ippWriteIO(fp, (ipp_iocb_t)cupsFileWrite, 1, NULL,
@@ -3969,6 +4147,19 @@ get_options(cupsd_job_t *job,		/* I - Job */
 	  banner_page)
         continue;
 
+#ifdef WITH_LSPP
+     /*
+      * In LSPP mode refuse to honor the page-label
+      */
+      if (is_lspp_config() &&
+          !strcmp(attr->name, "page-label"))
+      {
+        cupsdLogJob(job, CUPSD_LOG_DEBUG,
+		    "Ignoring page-label option due to LSPP mode");
+        continue;
+      }
+#endif /* WITH_LSPP */
+
      /*
       * Otherwise add them to the list...
       */
@@ -4779,6 +4970,18 @@ start_job(cupsd_job_t     *job,		/* I - Job ID */
           cupsd_printer_t *printer)	/* I - Printer to print job */
 {
   const char	*filename;		/* Support filename */
+#ifdef WITH_LSPP
+  char			*audit_message = NULL;	/* Audit message string */
+  char			*printerfile = NULL;	/* Device file pointed to by the printer */
+  security_id_t		clisid;		/* SELinux SID for the client */
+  security_id_t		psid;		/* SELinux SID for the printer */
+  context_t		printercon;	/* Printer's context string */
+  struct stat		printerstat;	/* Printer's stat buffer */
+  security_context_t	devcon;		/* Printer's SELinux context */
+  struct avc_entry_ref	avcref;		/* Pointer to the access vector cache */
+  security_class_t	tclass;		/* Object class for the SELinux check */
+  access_vector_t	avr;		/* Access method being requested */
+#endif /* WITH_LSPP */
   ipp_attribute_t *cancel_after = ippFindAttribute(job->attrs,
 						   "job-cancel-after",
 						   IPP_TAG_INTEGER);
@@ -4967,6 +5170,113 @@ start_job(cupsd_job_t     *job,		/* I - Job ID */
   fcntl(job->side_pipes[1], F_SETFD,
 	fcntl(job->side_pipes[1], F_GETFD) | FD_CLOEXEC);
 
+#ifdef WITH_LSPP
+  if (is_lspp_config())
+  {
+   /*
+    * Perform an access check before printing, but only if the printer starts with /dev/
+    */
+    printerfile = strstr(printer->device_uri, "/dev/");
+    if (printerfile == NULL && (strncmp(printer->device_uri, "file:/", 6) == 0))
+      printerfile = printer->device_uri + strlen("file:");
+
+    if (printerfile != NULL)
+    {
+      cupsdLogJob(job, CUPSD_LOG_DEBUG,
+		  "Attempting to check access on printer device %s",
+		  printerfile);
+      if (lstat(printerfile, &printerstat) < 0)
+      {
+	if (errno != ENOENT)
+	{
+	  cupsdLogJob(job, CUPSD_LOG_ERROR,
+		      "Unable to stat the printer");
+	  cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT, NULL);
+	  return ;
+	}
+	/*
+	 * The printer does not exist, so for now assume it's a FileDevice
+	 */
+	tclass = SECCLASS_FILE;
+	avr = FILE__WRITE;
+      }
+      else if (S_ISCHR(printerstat.st_mode))
+      {
+	tclass = SECCLASS_CHR_FILE;
+	avr = CHR_FILE__WRITE;
+      }
+      else if (S_ISREG(printerstat.st_mode))
+      {
+	tclass = SECCLASS_FILE;
+	avr = FILE__WRITE;
+      }
+      else
+      {
+	cupsdLogJob(job, CUPSD_LOG_ERROR,
+		    "StartJob: Printer is not a character device or "
+		    "regular file");
+	cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT, NULL);
+	return ;
+      }
+      static int avc_initialized = 0;
+      if (!avc_initialized++)
+          avc_init("cupsd_dequeue_", NULL, NULL, NULL, NULL);
+      avc_entry_ref_init(&avcref);
+      if (avc_context_to_sid(job->scon, &clisid) != 0)
+      {
+        cupsdLogJob(job, CUPSD_LOG_ERROR,
+		    "Unable to determine the SELinux sid for the job");
+        cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT, NULL);
+        return ;
+      }
+      if (getfilecon(printerfile, &devcon) == -1)
+      {
+        cupsdLogJob(job, CUPSD_LOG_ERROR,
+		    "Unable to get the SELinux context of %s",
+		    printerfile);
+        cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT, NULL);
+        return ;
+      }
+      printercon = context_new(devcon);
+      cupsdLogJob(job, CUPSD_LOG_WARN,
+		  "Printer context %s client context %s",
+		  context_str(printercon), job->scon);
+      context_free(printercon);
+
+      if (avc_context_to_sid(devcon, &psid) != 0)
+      {
+        cupsdLogJob(job, CUPSD_LOG_ERROR,
+		    "Unable to determine the SELinux sid for the printer");
+        freecon(devcon);
+        cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT, NULL);
+        return ;
+      }
+      freecon(devcon);
+
+      if (avc_has_perm(clisid, psid, tclass, avr, &avcref, NULL) != 0)
+      {
+       /*
+        * The access check failed, so cancel the job and send an audit message
+        */
+        if (AuditLog != -1)
+        {
+          audit_message = NULL;
+          cupsdSetStringf(&audit_message, "job=%d auid=%u acct=%s obj=%s canceled"
+                                          " unable to access printer=%s", job->id,
+                          job->auid, (job->username)?job->username:"?", job->scon, printer->name);
+          audit_log_user_message(AuditLog, AUDIT_USER_LABELED_EXPORT, audit_message,
+                                 ServerName, NULL, NULL, 0);
+          cupsdClearString(&audit_message);
+        }
+
+        cupsdSetJobState(job, IPP_JOB_ABORTED, CUPSD_JOB_DEFAULT, NULL);
+
+        return ;
+      }
+    }
+  }
+#endif /* WITH_LSPP */
+
  /*
   * Now start the first file in the job...
   */
diff --git a/scheduler/job.h b/scheduler/job.h
index eb59029dd..9e8b967a7 100644
--- a/scheduler/job.h
+++ b/scheduler/job.h
@@ -11,6 +11,13 @@
  * missing or damaged, see the license at "http://www.cups.org/".
  */
 
+/* Copyright (C) 2005 Trusted Computer Solutions, Inc. */
+/* (c) Copyright 2005-2006 Hewlett-Packard Development Company, L.P. */
+
+#ifdef WITH_LSPP
+#include <selinux/selinux.h>
+#endif /* WITH_LSPP */
+
 /*
  * Constants...
  */
@@ -88,6 +95,10 @@ struct cupsd_job_s			/**** Job request ****/
   int			progress;	/* Printing progress */
   int			num_keywords;	/* Number of PPD keywords */
   cups_option_t		*keywords;	/* PPD keywords */
+#ifdef WITH_LSPP
+  security_context_t	scon;		/* Security context of job */
+  uid_t			auid;		/* Audit loginuid for this job */
+#endif /* WITH_LSPP */
 };
 
 typedef struct cupsd_joblog_s		/**** Job log message ****/
diff --git a/scheduler/main.c b/scheduler/main.c
index e65060e1f..1ce42fcd7 100644
--- a/scheduler/main.c
+++ b/scheduler/main.c
@@ -59,6 +59,9 @@
 #  include <sys/param.h>
 #endif /* HAVE_SYS_PARAM_H */
 
+#ifdef WITH_LSPP
+#  include <libaudit.h>
+#endif /* WITH_LSPP */
 
 /*
  * Local functions...
@@ -125,6 +128,9 @@ main(int  argc,				/* I - Number of command-line args */
 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
   struct sigaction	action;		/* Actions for POSIX signals */
 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
+#if WITH_LSPP
+  auditfail_t           failmode;       /* Action for audit_open failure */
+#endif /* WITH_LSPP */
 #ifdef __APPLE__
   int			use_sysman = 1;	/* Use system management functions? */
 #else
@@ -488,6 +494,25 @@ main(int  argc,				/* I - Number of command-line args */
     exit(errno);
   }
 
+#ifdef WITH_LSPP
+  if ((AuditLog = audit_open()) < 0 )
+  {
+    if (get_auditfail_action(&failmode) == 0)
+    {
+      if (failmode == FAIL_LOG)
+      {
+        cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to connect to audit subsystem.");
+        AuditLog = -1;
+      }
+      else if (failmode == FAIL_TERMINATE)
+      {
+        fprintf(stderr, "cupsd: unable to start auditing, terminating");
+        return -1;
+      }
+    }
+  }
+#endif /* WITH_LSPP */
+
  /*
   * Let the system know we are busy while we bring up cupsd...
   */
@@ -1188,6 +1213,11 @@ main(int  argc,				/* I - Number of command-line args */
 
   cupsdStopSelect();
 
+#ifdef WITH_LSPP
+  if (AuditLog != -1)
+    audit_close(AuditLog);
+#endif /* WITH_LSPP */
+
   return (!stop_scheduler);
 }
 
diff --git a/scheduler/printers.c b/scheduler/printers.c
index 22f5ad261..1f16db31d 100644
--- a/scheduler/printers.c
+++ b/scheduler/printers.c
@@ -11,6 +11,8 @@
  * missing or damaged, see the license at "http://www.cups.org/".
  */
 
+/* (c) Copyright 2005-2006 Hewlett-Packard Development Company, L.P. */
+
 /*
  * Include necessary headers...
  */
@@ -35,6 +37,10 @@
 #  include <asl.h>
 #endif /* __APPLE__ */
 
+#ifdef WITH_LSPP
+#  include <libaudit.h>
+#  include <selinux/context.h>
+#endif /* WITH_LSPP */
 
 /*
  * Local functions...
@@ -2196,6 +2202,13 @@ cupsdSetPrinterAttrs(cupsd_printer_t *p)/* I - Printer to setup */
   ipp_attribute_t *attr;		/* Attribute data */
   char		*name,			/* Current user/group name */
 		*filter;		/* Current filter */
+#ifdef WITH_LSPP
+  char		*audit_message;		/* Audit message string */
+  char		*printerfile;		/* Path to a local printer dev */
+  char		*rangestr;		/* Printer's range if its available */
+  security_context_t	devcon;		/* Printer SELinux context */
+  context_t	printercon;		/* context_t for the printer */
+#endif /* WITH_LSPP */
 
 
   DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p->name,
@@ -2323,6 +2336,45 @@ cupsdSetPrinterAttrs(cupsd_printer_t *p)/* I - Printer to setup */
       attr->values[1].string.text = _cupsStrAlloc(Classification ?
 	                                   Classification : p->job_sheets[1]);
     }
+#ifdef WITH_LSPP
+    if (AuditLog != -1)
+    {
+      audit_message = NULL;
+      rangestr = NULL;
+      printercon = 0;
+      printerfile = strstr(p->device_uri, "/dev/");
+      if (printerfile == NULL && (strncmp(p->device_uri, "file:/", 6) == 0))
+        printerfile = p->device_uri + strlen("file:");
+
+      if (printerfile != NULL)
+      {
+        if (getfilecon(printerfile, &devcon) == -1)
+        {
+          if(is_selinux_enabled())
+            cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSetPrinterAttrs: Unable to get printer context");
+        }
+        else
+        {
+          printercon = context_new(devcon);
+          freecon(devcon);
+        }
+      }
+
+      if (printercon && context_range_get(printercon))
+        rangestr = strdup(context_range_get(printercon));
+      else
+        rangestr = strdup("unknown");
+
+      cupsdSetStringf(&audit_message, "printer=%s uri=%s banners=%s,%s range=%s",
+                      p->name, p->sanitized_device_uri, p->job_sheets[0], p->job_sheets[1], rangestr);
+      audit_log_user_message(AuditLog, AUDIT_LABEL_LEVEL_CHANGE, audit_message,
+                             ServerName, NULL, NULL, 1);
+      if (printercon)
+        context_free(printercon);
+      free(rangestr);
+      cupsdClearString(&audit_message);
+    }
+#endif /* WITH_LSPP */
   }
 
   p->raw    = 0;