3db4109
diff -up patch-2.5.4/patch.c.selinux patch-2.5.4/patch.c
3db4109
--- patch-2.5.4/patch.c.selinux	2007-10-04 16:58:38.000000000 +0100
3db4109
+++ patch-2.5.4/patch.c	2007-10-04 16:58:38.000000000 +0100
3db4109
@@ -414,6 +414,13 @@ main (int argc, char **argv)
3db4109
 		  if (! inerrno && chmod (outname, instat.st_mode) != 0)
3db4109
 		    pfatal ("Can't set permissions on file %s",
3db4109
 			    quotearg (outname));
3db4109
+		  if (! inerrno && incontext &&
3db4109
+		      setfilecon (outname, incontext) != 0)
3db4109
+		    {
3db4109
+		      if (errno != ENODATA && errno != ENOTSUP)
3db4109
+			pfatal ("Can't set security context on file %s",
3db4109
+				quotearg (outname));
3db4109
+		    }
3db4109
 		}
3db4109
 	    }
3db4109
       }
3db4109
diff -up patch-2.5.4/common.h.selinux patch-2.5.4/common.h
3db4109
--- patch-2.5.4/common.h.selinux	1999-08-30 07:20:08.000000000 +0100
3db4109
+++ patch-2.5.4/common.h	2007-10-04 16:58:38.000000000 +0100
3db4109
@@ -39,6 +39,8 @@
3db4109
 #include <sys/types.h>
3db4109
 #include <time.h>
3db4109
 
3db4109
+#include <selinux/selinux.h>
3db4109
+
3db4109
 #include <sys/stat.h>
3db4109
 #if ! defined S_ISDIR && defined S_IFDIR
3db4109
 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
3db4109
@@ -168,6 +170,7 @@ XTERN char *outfile;
3db4109
 XTERN int inerrno;
3db4109
 XTERN int invc;
3db4109
 XTERN struct stat instat;
3db4109
+XTERN security_context_t incontext;
3db4109
 XTERN bool dry_run;
3db4109
 XTERN bool posixly_correct;
3db4109
 
3db4109
diff -up patch-2.5.4/inp.c.selinux patch-2.5.4/inp.c
3db4109
--- patch-2.5.4/inp.c.selinux	2007-10-04 16:58:38.000000000 +0100
3db4109
+++ patch-2.5.4/inp.c	2007-10-04 16:58:38.000000000 +0100
3db4109
@@ -154,7 +154,15 @@ get_input_file (char const *filename, ch
3db4109
     char *getbuf;
3db4109
 
3db4109
     if (inerrno == -1)
3db4109
-      inerrno = stat (inname, &instat) == 0 ? 0 : errno;
3db4109
+      {
3db4109
+	inerrno = stat (inname, &instat) == 0 ? 0 : errno;
3db4109
+	inerrno = getfilecon (inname, &incontext) == 0 ? 0 : errno;
3db4109
+	if (inerrno == ENODATA || inerrno == ENOTSUP)
3db4109
+	  {
3db4109
+	    inerrno = 0;
3db4109
+	    incontext = NULL;
3db4109
+	  }
3db4109
+      }
3db4109
 
3db4109
     /* Perhaps look for RCS or SCCS versions.  */
3db4109
     if (patch_get
3db4109
@@ -196,7 +204,7 @@ get_input_file (char const *filename, ch
3db4109
 	    }
3db4109
 
3db4109
 	    if (cs && version_get (filename, cs, ! inerrno, elsewhere, getbuf,
3db4109
-				   &instat))
3db4109
+				   &instat, &incontext))
3db4109
 	      inerrno = 0;
3db4109
 
3db4109
 	    free (getbuf);
3db4109
@@ -213,6 +221,7 @@ get_input_file (char const *filename, ch
3db4109
       {
3db4109
 	instat.st_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
3db4109
 	instat.st_size = 0;
3db4109
+	incontext = NULL;
3db4109
       }
3db4109
     else if (! S_ISREG (instat.st_mode))
3db4109
       fatal ("File %s is not a regular file -- can't patch",
3db4109
diff -up patch-2.5.4/util.c.selinux patch-2.5.4/util.c
3db4109
--- patch-2.5.4/util.c.selinux	2007-10-04 16:58:38.000000000 +0100
3db4109
+++ patch-2.5.4/util.c	2007-10-04 16:58:38.000000000 +0100
3db4109
@@ -266,7 +266,8 @@ static char const CLEARTOOL_CO[] = "clea
3db4109
    *GETBUF and *DIFFBUF must be freed by the caller.  */
3db4109
 char const *
3db4109
 version_controller (char const *filename, int readonly,
3db4109
-		    struct stat const *filestat, char **getbuf, char **diffbuf)
3db4109
+		    struct stat const *filestat,
3db4109
+		    char **getbuf, char **diffbuf)
3db4109
 {
3db4109
   struct stat cstat;
3db4109
   char const *filebase = base_name (filename);
3db4109
@@ -375,7 +376,8 @@ version_controller (char const *filename
3db4109
    Return nonzero if successful.  */
3db4109
 int
3db4109
 version_get (char const *filename, char const *cs, int exists, int readonly,
3db4109
-	     char const *getbuf, struct stat *filestat)
3db4109
+	     char const *getbuf, struct stat *filestat,
3db4109
+	     security_context_t *filecontext)
3db4109
 {
3db4109
   if (patch_get < 0)
3db4109
     {
3db4109
@@ -400,6 +402,13 @@ version_get (char const *filename, char 
3db4109
 	fatal ("Can't get file %s from %s", quotearg (filename), cs);
3db4109
       if (stat (filename, filestat) != 0)
3db4109
 	pfatal ("%s", quotearg (filename));
3db4109
+      if (filecontext && getfilecon (filename, filecontext) != 0)
3db4109
+	{
3db4109
+	  if (errno == ENODATA || errno == ENOTSUP)
3db4109
+	    *filecontext = NULL;
3db4109
+	  else
3db4109
+	    pfatal ("%s", quotearg (filename));
3db4109
+	}
3db4109
     }
3db4109
 
3db4109
   return 1;
3db4109
diff -up patch-2.5.4/util.h.selinux patch-2.5.4/util.h
3db4109
--- patch-2.5.4/util.h.selinux	1999-08-30 07:20:08.000000000 +0100
3db4109
+++ patch-2.5.4/util.h	2007-10-04 16:58:38.000000000 +0100
3db4109
@@ -21,7 +21,7 @@ char *fetchname PARAMS ((char *, int, ti
3db4109
 char *savebuf PARAMS ((char const *, size_t));
3db4109
 char *savestr PARAMS ((char const *));
3db4109
 char const *version_controller PARAMS ((char const *, int, struct stat const *, char **, char **));
3db4109
-int version_get PARAMS ((char const *, char const *, int, int, char const *, struct stat *));
3db4109
+int version_get PARAMS ((char const *, char const *, int, int, char const *, struct stat *, security_context_t *));
3db4109
 int create_file PARAMS ((char const *, int, mode_t));
3db4109
 int systemic PARAMS ((char const *));
3db4109
 char *format_linenum PARAMS ((char[LINENUM_LENGTH_BOUND + 1], LINENUM));
3db4109
diff -up patch-2.5.4/pch.c.selinux patch-2.5.4/pch.c
3db4109
--- patch-2.5.4/pch.c.selinux	2007-10-04 16:58:38.000000000 +0100
3db4109
+++ patch-2.5.4/pch.c	2007-10-04 16:58:38.000000000 +0100
3db4109
@@ -258,7 +258,12 @@ there_is_another_patch (void)
3db4109
 	  {
3db4109
 	    if (stat (inname, &instat) == 0)
3db4109
 	      {
3db4109
-		inerrno = 0;
3db4109
+		inerrno = getfilecon (inname, &incontext) == 0 ? 0 : errno;
3db4109
+		if (inerrno == ENODATA || inerrno == ENOTSUP)
3db4109
+		  {
3db4109
+		    inerrno = 0;
3db4109
+		    incontext = NULL;
3db4109
+		  }
3db4109
 		invc = -1;
3db4109
 	      }
3db4109
 	    else
3db4109
@@ -573,7 +578,7 @@ intuit_diff_type (void)
3db4109
 			  if (cs)
3db4109
 			    {
3db4109
 			      if (version_get (name[i], cs, 0, readonly,
3db4109
-					       getbuf, &st[i]))
3db4109
+					       getbuf, &st[i], NULL))
3db4109
 				stat_errno[i] = 0;
3db4109
 			      else
3db4109
 				version_controlled[i] = 0;
3db4109
diff -up patch-2.5.4/Makefile.in.selinux patch-2.5.4/Makefile.in
3db4109
--- patch-2.5.4/Makefile.in.selinux	2007-10-04 16:59:09.000000000 +0100
3db4109
+++ patch-2.5.4/Makefile.in	2007-10-04 17:00:59.000000000 +0100
3db4109
@@ -36,7 +36,7 @@ DEFS = @DEFS@
3db4109
 EXEEXT = @EXEEXT@
3db4109
 LDFLAGS = @LDFLAGS@
3db4109
 LIBOBJS = @LIBOBJS@
3db4109
-LIBS = @LIBS@
3db4109
+LIBS = @LIBS@ -lselinux
3db4109
 PACKAGE = @PACKAGE@
3db4109
 U = @U@
3db4109
 VERSION = @VERSION@