@@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
5
5
use std:: io:: BufRead ;
6
6
use std:: io:: Write ;
7
7
use std:: net:: IpAddr ;
8
+ use std:: os:: unix:: fs:: PermissionsExt ;
8
9
9
10
#[ derive( Serialize , Deserialize , Debug ) ]
10
11
pub struct DnsConfig {
@@ -18,16 +19,17 @@ impl DnsConfig {
18
19
suffixes : & [ & str ] ,
19
20
hosts_entries : Option < & Vec < String > > ,
20
21
) -> anyhow:: Result < Self > {
21
- std:: fs:: create_dir_all ( format ! ( "/etc/netns/{}" , ns_name) )
22
- . with_context ( || format ! ( "Failed to create directory: /etc/netns/{}" , ns_name) ) ?;
22
+ let dir_path = format ! ( "/etc/netns/{}" , ns_name) ;
23
+ std:: fs:: create_dir_all ( & dir_path)
24
+ . with_context ( || format ! ( "Failed to create directory: {}" , & dir_path) ) ?;
25
+ std:: fs:: set_permissions ( & dir_path, PermissionsExt :: from_mode ( 0o644 ) )
26
+ . with_context ( || format ! ( "Failed to set directory permissions for {}" , dir_path) ) ?;
23
27
24
- let mut resolv = std:: fs:: File :: create ( format ! ( "/etc/netns/{}/resolv.conf" , ns_name) )
25
- . with_context ( || {
26
- format ! (
27
- "Failed to open resolv.conf: /etc/netns/{}/resolv.conf" ,
28
- ns_name
29
- )
30
- } ) ?;
28
+ let resolv_conf_path = format ! ( "/etc/netns/{}/resolv.conf" , ns_name) ;
29
+ let mut resolv = std:: fs:: File :: create ( & resolv_conf_path)
30
+ . with_context ( || format ! ( "Failed to open resolv.conf: {}" , & resolv_conf_path) ) ?;
31
+ std:: fs:: set_permissions ( & resolv_conf_path, PermissionsExt :: from_mode ( 0o644 ) )
32
+ . with_context ( || format ! ( "Failed to set file permissions for {}" , resolv_conf_path) ) ?;
31
33
32
34
debug ! (
33
35
"Setting namespace {} DNS server to {}" ,
@@ -59,8 +61,11 @@ impl DnsConfig {
59
61
}
60
62
61
63
if let Some ( my_hosts_entries) = hosts_entries {
62
- let mut hosts = std:: fs:: File :: create ( format ! ( "/etc/netns/{}/hosts" , ns_name) )
63
- . with_context ( || format ! ( "Failed to open hosts: /etc/netns/{}/hosts" , ns_name) ) ?;
64
+ let hosts_path = format ! ( "/etc/netns/{}/hosts" , ns_name) ;
65
+ let mut hosts = std:: fs:: File :: create ( & hosts_path)
66
+ . with_context ( || format ! ( "Failed to open hosts: {}" , & hosts_path) ) ?;
67
+ std:: fs:: set_permissions ( & hosts_path, PermissionsExt :: from_mode ( 0o644 ) )
68
+ . with_context ( || format ! ( "Failed to set file permissions for {}" , & hosts_path) ) ?;
64
69
65
70
for hosts_enty in my_hosts_entries {
66
71
writeln ! ( hosts, "{}" , hosts_enty) . with_context ( || {
@@ -73,14 +78,13 @@ impl DnsConfig {
73
78
let nsswitch_src = std:: fs:: File :: open ( "/etc/nsswitch.conf" )
74
79
. with_context ( || "Failed to open nsswitch.conf: /etc/nsswitch.conf" ) ?;
75
80
76
- let mut nsswitch =
77
- std:: fs:: File :: create ( format ! ( "/etc/netns/{}/nsswitch.conf" , ns_name) )
78
- . with_context ( || {
79
- format ! (
80
- "Failed to open nsswitch.conf: /etc/netns/{}/nsswitch.conf" ,
81
- ns_name
82
- )
83
- } ) ?;
81
+ let nsswitch_path = format ! ( "/etc/netns/{}/nsswitch.conf" , ns_name) ;
82
+ let mut nsswitch = std:: fs:: File :: create ( & nsswitch_path)
83
+ . with_context ( || format ! ( "Failed to open nsswitch.conf: {}" , nsswitch_path) ) ?;
84
+ std:: fs:: set_permissions ( & nsswitch_path, PermissionsExt :: from_mode ( 0o644 ) )
85
+ . with_context ( || {
86
+ format ! ( "Failed to set file permissions for {}" , & nsswitch_path)
87
+ } ) ?;
84
88
85
89
for line in std:: io:: BufReader :: new ( nsswitch_src) . lines ( ) {
86
90
writeln ! (
0 commit comments