summaryrefslogtreecommitdiffstats
path: root/patches/logrotate-3.7.1/rh-selinux.patch
blob: 6798ce0b266527abb2d41064e6ce3aadd51a6c4a (plain)
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
Add selinux_getenforce() calls to work when not in enforcing mode
- use security_getenforce() instead of selinux_getenforcemode
From RedHat patches.

---
 logrotate.c |  126 +++++++++++++++++++++++++++++++++---------------------------
 1 file changed, 71 insertions(+), 55 deletions(-)

Index: logrotate-3.7.1/logrotate.c
===================================================================
--- logrotate-3.7.1.orig/logrotate.c
+++ logrotate-3.7.1/logrotate.c
@@ -17,6 +17,7 @@
 #include <selinux/selinux.h>
 static security_context_t prev_context=NULL;
 int selinux_enabled=0;
+int selinux_enforce=0;
 #endif
 
 #include "basenames.h"
@@ -293,38 +294,43 @@ static int copyTruncate(char * currLog, 
 	    return 1;
 	}
 #ifdef WITH_SELINUX
-	if ((selinux_enabled=(is_selinux_enabled()>0)))
-	  {
-	    security_context_t oldContext;
-	    if (fgetfilecon(fdcurr, &oldContext) >=0) {
-	      if (getfscreatecon(&prev_context) < 0) {
-		message(MESS_ERROR, "error getting default context: %s\n", 
-			strerror(errno));
-		freecon(oldContext);
-		return 1;
-	      }
-	      if (setfscreatecon(oldContext) < 0) {
-		message(MESS_ERROR, "error setting file context %s to %s: %s\n", 
-			saveLog, oldContext,strerror(errno));
-		freecon(oldContext);
-		return 1;
-	      }
-	      freecon(oldContext);
-	    } else {
-	      message(MESS_ERROR, "error getting file context %s: %s\n", currLog,
-		      strerror(errno));
-	      return 1;
-	    }
-	  }
+	if (selinux_enabled) {
+		security_context_t oldContext;
+		if (fgetfilecon(fdcurr, &oldContext) >=0) {
+			if (getfscreatecon(&prev_context) < 0) {
+				message(MESS_ERROR, "error getting default context: %s\n", 
+					strerror(errno));
+				if (selinux_enforce) {
+					freecon(oldContext);
+					return 1;
+				}
+			}
+			if (setfscreatecon(oldContext) < 0) {
+				message(MESS_ERROR, "error setting file context %s to %s: %s\n", 
+					saveLog, oldContext,strerror(errno));
+				if (selinux_enforce) {
+					freecon(oldContext);
+					return 1;
+				}
+			}
+			freecon(oldContext);
+		} else {
+			message(MESS_ERROR, "error getting file context %s: %s\n", currLog,
+				strerror(errno));
+			if (selinux_enforce) {
+				return 1;
+			}
+		}
+	}
 #endif
 	fdsave = open(saveLog, O_WRONLY | O_CREAT | O_TRUNC,sb->st_mode);
 #ifdef WITH_SELINUX
 	if (selinux_enabled) {
-	  setfscreatecon(prev_context);
-	  if (prev_context!= NULL) {
-	    freecon(prev_context);
-	    prev_context=NULL;
-	  }
+		setfscreatecon(prev_context);
+		if (prev_context!= NULL) {
+			freecon(prev_context);
+			prev_context=NULL;
+		}
 	}
 #endif
 	if (fdsave < 0) {
@@ -672,28 +678,34 @@ int rotateSingleLog(logInfo * log, int l
 	    (log->flags & LOG_FLAG_DELAYCOMPRESS) ? "" : compext);
     
 #ifdef WITH_SELINUX
-    if ((selinux_enabled=(is_selinux_enabled()>0))) {
-      security_context_t oldContext=NULL;
-      if (getfilecon(log->files[logNum], &oldContext)>0) {
-	if (getfscreatecon(&prev_context) < 0) {
-	  message(MESS_ERROR, "error getting default context: %s\n", 
-		  strerror(errno));
-	  freecon(oldContext);
-	  return 1;
-	}
-	if (setfscreatecon(oldContext) < 0) {
-	  message(MESS_ERROR, "error setting file context %s to %s: %s\n", 
-		  log->files[logNum], oldContext,strerror(errno));
-	  freecon(oldContext);
-	  return 1;
-	}
-	freecon(oldContext);
-      } else {
-	message(MESS_ERROR, "error getting file context %s: %s\n", 
-		log->files[logNum], 
-		strerror(errno));
-	return 1;
-      }
+    if (selinux_enabled) {
+	    security_context_t oldContext=NULL;
+	    if (getfilecon(log->files[logNum], &oldContext)>0) {
+		    if (getfscreatecon(&prev_context) < 0) {
+			    message(MESS_ERROR, "error getting default context: %s\n", 
+				    strerror(errno));
+			    if (selinux_enforce) {
+				    freecon(oldContext);
+				    return 1;
+			    }
+		    }
+		    if (setfscreatecon(oldContext) < 0) {
+			    message(MESS_ERROR, "error setting file context %s to %s: %s\n", 
+				    log->files[logNum], oldContext,strerror(errno));
+			    if (selinux_enforce) {
+				    freecon(oldContext);
+				    return 1;
+			    }
+		    }
+		    freecon(oldContext);
+	    } else {
+		    message(MESS_ERROR, "error getting file context %s: %s\n", 
+			    log->files[logNum], 
+			    strerror(errno));
+		    if (selinux_enforce) {
+			    return 1;
+		    }
+	    }
     }
 #endif
     for (i = rotateCount + logStart - 1; (i >= 0) && !hasErrors; i--) {
@@ -883,11 +895,11 @@ int rotateSingleLog(logInfo * log, int l
     
 #ifdef WITH_SELINUX
 	if (selinux_enabled) {
-	  setfscreatecon(prev_context);
-	  if (prev_context!= NULL) {
-	    freecon(prev_context);
-	    prev_context=NULL;
-	  }
+		setfscreatecon(prev_context);
+		if (prev_context!= NULL) {
+			freecon(prev_context);
+			prev_context=NULL;
+		}
 	}
 #endif
     free(dirName);
@@ -1249,6 +1261,10 @@ int main(int argc, const char ** argv) {
 	exit(1);
     }
 
+#ifdef WITH_SELINUX
+    selinux_enabled=(is_selinux_enabled()>0);
+    selinux_enforce=security_getenforce();
+#endif
     for (file = files; *file; file++) {
 	if (readConfigPath(*file, &defConfig, &logs, &numLogs)) {
 	    exit(1);