summaryrefslogtreecommitdiffstats
path: root/pki/patches/pki-core-9.0.3-r1908.patch
blob: 4017203483d3872f4cd98bdacf11c9b0a4b47c95 (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
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
Index: base/setup/pkiremove
===================================================================
--- base/setup/pkiremove	(revision 1907)
+++ base/setup/pkiremove	(revision 1908)
@@ -279,25 +279,26 @@
 
 sub remove_fcontext
 {
-    my ($fcontext, $fname, $ftype) = @_;
-    my $errs;
-
+    my ($fcontext, $fname, $ftype, $cmd_ref) = @_;
     emit(sprintf("remove_fcontext(%s)\n", join(", ", @_)), "debug");
 
-    return if $dry_run;
+    my $tmp = `$semanage fcontext -l -C |grep $fcontext |grep $fname |wc -l`;
+    chomp $tmp;
+    if ($tmp eq "0" ) {
+        emit("File context $fcontext for $fname defined in policy, not deleted", "debug");
+        return 0;
+    } 
 
     if ($ftype eq "f") {
-        $errs = `$semanage fcontext -d -t $fcontext -f -- $fname 2>&1`
+        $$cmd_ref .= "fcontext -d -t $fcontext -f -- $fname\n";
     } else {
-        $errs = `$semanage fcontext -d -t $fcontext $fname 2>&1`
-    }
-    if (($? != 0)  && ($errs !~ /defined in policy, cannot be deleted/)) {
-        emit("Failed setting selinux file context $fcontext for $fname ($errs)", "error");
+        $$cmd_ref .= "fcontext -d -t $fcontext $fname\n";
     }
 }
 
-sub remove_selinux_fcontexts
+sub get_selinux_fcontexts
 {
+    my ($cmd_ref) = @_;
     my $setype                = "pki_" . $subsystem_type;
     my $default_instance_name = "pki-" . $subsystem_type;
     my $default_instance_root = "/var/lib";
@@ -324,47 +325,46 @@
         $conf_path = readlink $conf_path;
     };
 
-    print STDOUT "Removing selinux file contexts.\n";
-
     # For backwards compatibility, support removal of instances
     # which use the legacy start/stop implementation
     if (entity_exists("$default_initscripts_path/$pki_instance_name")) {
         # remove context for "$default_initscripts_path/$pki_instance_name"
         if ($pki_instance_name ne $default_instance_name) {
             remove_fcontext($setype . "_script_exec_t", 
-                              "/etc/rc\\.d/init\\.d/$pki_instance_name", "f");
+                              "/etc/rc\\.d/init\\.d/$pki_instance_name", "f", $cmd_ref);
         }
     }
 
     # remove context for $pki_instance_root/$pki_instance_name
     if (($pki_instance_name ne $default_instance_name) || ($pki_instance_root ne $default_instance_root)) {
         remove_fcontext($setype . "_var_lib_t", 
-            "\"$pki_instance_root/$pki_instance_name(/.*)?\"", "a");
+            "\"$pki_instance_root/$pki_instance_name(/.*)?\"", "a", $cmd_ref);
     }
 
     # remove context for /var/run/$pki_instance_name.pid
     if (($java_component) && ($pki_instance_name ne $default_instance_name)) {
         remove_fcontext($setype . "_var_run_t", 
-            "/var/run/$pki_instance_name\\.pid", "f");
+            "/var/run/$pki_instance_name" . '.pid', "f", $cmd_ref);
     }
 
     # remove context for $log_path
     if ($log_path ne $default_log_path) {
         remove_fcontext($setype . "_log_t",
-            "\"$log_path(/.*)?\"", "a"); 
+            "\"$log_path(/.*)?\"", "a", $cmd_ref); 
     }
 
     # remove context for $conf_path
     if ($conf_path ne $default_conf_path) {
          remove_fcontext($setype . "_etc_rw_t",
-             "\"$conf_path(/.*)?\"", "a");
+             "\"$conf_path(/.*)?\"", "a", $cmd_ref);
     }
 
 }
 
 
-sub remove_selinux_ports
+sub get_selinux_ports
 {
+    my ($cmd_ref) = @_;
     my $status;
     my $semanage = "/usr/sbin/semanage";
     my $secure_port;
@@ -411,17 +411,12 @@
     print(STDOUT "\n");
     foreach my $port (@ports) {
         my $setype = "pki_" . $subsystem_type . "_port_t";
-        my $errs;
-        print STDOUT "Removing port $port from selinux policy.\n";
-	next if $dry_run;
-        $errs = `$semanage port -d -t $setype -ptcp $port 2>&1`;
-        if ($? != 0)  {
-            if ($errs !~ /defined in policy, cannot be deleted/) {
-                warn "Port $port not removed from selinux policy correctly.\n";
-                print $errs;
+        my $tmp = `$semanage port -l -C |grep $setype |grep $port | wc -l`;
+        chomp $tmp;
+        if ($tmp eq "0") {
+            emit("Port context $setype for $port defined in policy, not deleting", "debug");        
             } else {
-                print "Port $port not removed from selinux policy because it is defined in policy.\nThis is OK.\n";
-            }
+            $$cmd_ref .= "port -d -t $setype -ptcp $port\n";
         }
     }
 }
@@ -459,11 +454,25 @@
     warn "Error updating security domain: " . $@ if $@;
 
     if (($^O eq "linux") && (is_Fedora() || (is_RHEL() && (! is_RHEL4())))) {
-        eval { remove_selinux_ports(); };
-        warn "Error removing selinux ports: " . $@ if $@; 
+        my $semanage_cmds = "";
+
+        eval { get_selinux_ports(\$semanage_cmds); };
+        warn "Error getting selinux ports: " . $@ if $@; 
 
-        eval { remove_selinux_fcontexts(); };
-        warn "Error removing selinux file contexts: " . $@ if $@;
+        eval { get_selinux_fcontexts(\$semanage_cmds); };
+        warn "Error getting selinux file contexts: " . $@ if $@;
+
+        print STDOUT "Removing selinux contexts\n";
+        if ($semanage_cmds ne "") {
+            emit("Executing selinux commands in batch mode.\n", "debug");
+            if (! $dry_run) {
+                if (! run_command("$semanage -S targeted -i - " . '<< _EOF' . "\n$semanage_cmds\n" . '_EOF' . "\n")) {
+                    emit("Error executing selinux batch commands\n", "error");
+                }
+            }
+        } else {
+            emit("No selinux contexts need to be removed.  No need to run semanage. \n");
+        } 
     }
 
     # De-register this instance with "chkconfig"
Index: base/setup/pkicreate
===================================================================
--- base/setup/pkicreate	(revision 1907)
+++ base/setup/pkicreate	(revision 1908)
@@ -2897,14 +2897,11 @@
 
 sub add_selinux_port
 {
-    my ($setype, $seport) = @_;
+    my ($setype, $seport, $cmds_ref) = @_;
     my $status = check_selinux_port($setype, $seport);
 
     if ($status == $SELINUX_PORT_UNDEFINED) {
-        emit("Setting selinux context $setype for $seport\n");
-        if (!run_command("$semanage port -a -t $setype -p tcp $seport")) {
-            emit("Failed setting selinux context $setype for $seport\n", "error");
-        }
+        $$cmds_ref .= "port -a -t $setype -p tcp $seport\n";
     } elsif ($status == $SELINUX_PORT_WRONGLY_DEFINED) {
         emit("Failed setting selinux context $setype for $seport\n", "error");
     }
@@ -2912,26 +2909,23 @@
 
 sub add_selinux_file_context
 {
-   my ($fcontext, $fname, $ftype) = @_;
+   my ($fcontext, $fname, $ftype, $cmds_ref) = @_;
    my ($result);
    
    emit(sprintf("add_selinux_file_context(%s)\n", join(", ", @_)), "debug");
 
-   return if $dry_run;
-   
    #check if fcontext has already been set
-   if (`$semanage fcontext -l -n |grep $fname |grep ":$fcontext:" | wc -l` == 1) {
-      #selinux context already defined
+   my $tmp = `$semanage fcontext -l -n |grep $fname |grep ":$fcontext:" | wc -l`;
+   chomp $tmp;
+   if ($tmp ne "0") {
+      emit("selinux fcontext for $fname already defined\n", "debug");
       return;
    }
-   emit("Setting selinux file context for $fname\n");
+
    if ($ftype eq "f") {
-       $result = run_command("$semanage fcontext -a -t $fcontext -f -- $fname");
+       $$cmds_ref .= "fcontext -a -t $fcontext -f -- $fname\n";
    } else {
-       $result = run_command("$semanage fcontext -a -t $fcontext $fname");
-   }
-   if (!$result) {
-       emit("Failed setting selinux file context $fcontext for $fname", "error");
+       $$cmds_ref .= "fcontext -a -t $fcontext $fname\n";
    }
 }
 
@@ -2950,6 +2944,8 @@
     my $log_path;
     my $ftype;
     my $java_component = 0;
+    my $semanage_cmds = "";
+    my @restorecon_cmds;
 
     emit("configuring SELinux ...\n");
 
@@ -2976,19 +2972,17 @@
 
     # set file contexts
     if ($java_component) {
-        emit("Restorecon file context for /usr/share/java/pki\n");
-        run_command("$restorecon -F -R /usr/share/java/pki");
+        push (@restorecon_cmds, "$restorecon -F -R /usr/share/java/pki");
     }
-    emit("Restorecon file context for /usr/share/pki\n");
-    run_command("$restorecon -F -R /usr/share/pki");
+    push (@restorecon_cmds, "$restorecon -F -R /usr/share/pki");
  
     # set file context for $pki_instance_root/$pki_instance_name
     if (($pki_instance_name ne $default_instance_name) || ($pki_instance_root ne $default_instance_root)) {
         add_selinux_file_context($setype . "_var_lib_t", 
-                                 "\"${pki_instance_root}/${pki_instance_name}(/.*)?\"", "a");
+                                 "\"${pki_instance_root}/${pki_instance_name}(/.*)?\"", 
+                                 "a", \$semanage_cmds);
     }
-    emit("Restorecon file context for $pki_instance_root/$pki_instance_name\n");
-    run_command("$restorecon -F -R $pki_instance_root/$pki_instance_name");
+    push(@restorecon_cmds, "$restorecon -F -R $pki_instance_root/$pki_instance_name");
 
 
     if ($java_component) {
@@ -2996,17 +2990,15 @@
         my $pidfile = $tomcat6_instance_pid_file_path;
         if ($pki_instance_name ne $default_instance_name) {
             add_selinux_file_context($setype . "_var_run_t",
-                $pidfile, "f");
+                $pidfile, "f", \$semanage_cmds);
         }
         if (-e $pidfile) {
-            emit("Restorecon file context for $pidfile\n");
-            run_command("$restorecon -F $pidfile");
+            push(@restorecon_cmds, "$restorecon -F $pidfile");
         }
 
         my $pidpath = $default_apache_pids_path;
         if (-e $pidpath) {
-            emit("Restorecon file context for $pidpath\n");
-            run_command("$restorecon -F -R $pidpath");
+            push(@restorecon_cmds, "$restorecon -F -R $pidpath");
         }
     }
 
@@ -3017,10 +3009,9 @@
     } else {
         if ($log_path ne $default_log_path) {
             add_selinux_file_context($setype . "_log_t", 
-                                     "\"$log_path(/.*)?\"", "a");
+                                     "\"$log_path(/.*)?\"", "a", \$semanage_cmds);
         }
-        emit("Restorecon file context for $log_path\n");
-        run_command("$restorecon -F -R $log_path");
+        push(@restorecon_cmds, "$restorecon -F -R $log_path");
     }
 
     # set file context for $conf_path
@@ -3029,41 +3020,62 @@
         emit("Error: Cannot set selinux context $setype" . "_etc_rw_t for directory /");
     } else {
         add_selinux_file_context($setype . "_etc_rw_t", 
-                                  "\"$conf_path(/.*)?\"", "a");
-        emit("Restorecon $conf_path\n");
-        run_command("$restorecon -F -R $conf_path");
+                                  "\"$conf_path(/.*)?\"", "a", \$semanage_cmds);
+        push(@restorecon_cmds, "$restorecon -F -R $conf_path");
     }
     
     if (! $java_component) {
-        emit("Restorecon file context for /usr/sbin/httpd.worker \n");
-        run_command("$restorecon -F -R /usr/sbin/httpd.worker");
+        push(@restorecon_cmds, "$restorecon -F -R /usr/sbin/httpd.worker");
     }    
 
     # add ports
     parse_selinux_ports();
     if ($secure_port != -1) {
-        add_selinux_port($setype_p, $secure_port);
+        add_selinux_port($setype_p, $secure_port, \$semanage_cmds);
     }
     if ($non_clientauth_secure_port != -1) {
-        add_selinux_port($setype_p, $non_clientauth_secure_port);
+        add_selinux_port($setype_p, $non_clientauth_secure_port, \$semanage_cmds);
     }
     if ($unsecure_port != -1) {
-        add_selinux_port($setype_p, $unsecure_port);
+        add_selinux_port($setype_p, $unsecure_port, \$semanage_cmds);
     }
     if ($tomcat_server_port != -1) {
-        add_selinux_port($setype_p, $tomcat_server_port);
+        add_selinux_port($setype_p, $tomcat_server_port, \$semanage_cmds);
     }
     if ($agent_secure_port != -1) {
-        add_selinux_port($setype_p, $agent_secure_port);
+        add_selinux_port($setype_p, $agent_secure_port, \$semanage_cmds);
     }
     if ($ee_secure_port != -1) {
-        add_selinux_port($setype_p, $ee_secure_port);
+        add_selinux_port($setype_p, $ee_secure_port, \$semanage_cmds);
     }
     if ($ee_secure_client_auth_port != -1) {
-        add_selinux_port($setype_p, $ee_secure_client_auth_port);
+        add_selinux_port($setype_p, $ee_secure_client_auth_port, \$semanage_cmds);
     }
     if ($admin_secure_port != -1) {
-        add_selinux_port($setype_p, $admin_secure_port);
+        add_selinux_port($setype_p, $admin_secure_port, \$semanage_cmds);
+    }
+
+    # now run the selinux commands in batch mode
+    if ($semanage_cmds ne "") {
+        emit("Running the semanage commands in batch mode\n", "debug");
+        if (! $dry_run) {
+            if(! run_command("$semanage -S targeted -i - " . ' << _EOF' . "\n$semanage_cmds\n" . '_EOF' . "\n")) {
+                emit("Failed executing semanage batch command \n", "error");
+            }
+        }
+    } else {
+        emit("Selinux contexts already set. No need to run semanage.\n", "debug");
+    }
+
+    #now run the restorecons
+    emit("Running restorecon commands\n", "debug");
+    foreach my $cmd (@restorecon_cmds) {
+        emit("$cmd\n", "debug");
+        if (! $dry_run) {
+            if (!run_command($cmd)) {
+                emit("Failed executing restorecon command; $cmd\n", "error"); 
+            }                          
+        }
     }
 
     return 1;