@@ -95,46 +95,43 @@ pub(super) fn ensure_safe_directory(
9595 directory : & Path ,
9696 owner : ManagedCertificateOwner ,
9797) -> Result < ( ) , AcmeCertificateInstallError > {
98- reject_existing_symlink_in_path ( directory) ?;
99- fs:: create_dir_all ( directory) . map_err ( |error| AcmeCertificateInstallError :: Io {
100- path : directory. to_path_buf ( ) ,
101- error,
102- } ) ?;
103- apply_owner_to_path ( directory, owner) ?;
104- reject_existing_symlink_in_path ( directory) ?;
105- let metadata =
106- fs:: symlink_metadata ( directory) . map_err ( |error| AcmeCertificateInstallError :: Io {
98+ #[ cfg( unix) ]
99+ {
100+ let directory_fd =
101+ crate :: acme_directory:: create_private_directory_all ( directory) . map_err ( |error| {
102+ AcmeCertificateInstallError :: Io {
103+ path : directory. to_path_buf ( ) ,
104+ error,
105+ }
106+ } ) ?;
107+ let directory_file = fs:: File :: from ( directory_fd) ;
108+ apply_owner_to_file ( & directory_file, directory, owner) ?;
109+ Ok ( ( ) )
110+ }
111+
112+ #[ cfg( not( unix) ) ]
113+ {
114+ reject_existing_symlink_in_path ( directory) ?;
115+ fs:: create_dir_all ( directory) . map_err ( |error| AcmeCertificateInstallError :: Io {
107116 path : directory. to_path_buf ( ) ,
108117 error,
109118 } ) ?;
110- if metadata. file_type ( ) . is_symlink ( ) || !metadata. is_dir ( ) {
111- return Err ( AcmeCertificateInstallError :: UnsafePath {
112- path : directory. to_path_buf ( ) ,
113- message : "path is not a real directory" . to_owned ( ) ,
114- } ) ;
115- }
116-
117- Ok ( ( ) )
118- }
119-
120- #[ cfg( unix) ]
121- fn apply_owner_to_path (
122- path : & Path ,
123- owner : ManagedCertificateOwner ,
124- ) -> Result < ( ) , AcmeCertificateInstallError > {
125- let Some ( owner) = owner else {
126- return Ok ( ( ) ) ;
127- } ;
119+ apply_owner_to_path ( directory, owner) ?;
120+ reject_existing_symlink_in_path ( directory) ?;
121+ let metadata =
122+ fs:: symlink_metadata ( directory) . map_err ( |error| AcmeCertificateInstallError :: Io {
123+ path : directory. to_path_buf ( ) ,
124+ error,
125+ } ) ?;
126+ if metadata. file_type ( ) . is_symlink ( ) || !metadata. is_dir ( ) {
127+ return Err ( AcmeCertificateInstallError :: UnsafePath {
128+ path : directory. to_path_buf ( ) ,
129+ message : "path is not a real directory" . to_owned ( ) ,
130+ } ) ;
131+ }
128132
129- rustix:: fs:: chown (
130- path,
131- Some ( rustix:: fs:: Uid :: from_raw ( owner. uid ) ) ,
132- Some ( rustix:: fs:: Gid :: from_raw ( owner. gid ) ) ,
133- )
134- . map_err ( |error| AcmeCertificateInstallError :: Io {
135- path : path. to_path_buf ( ) ,
136- error : error. into ( ) ,
137- } )
133+ Ok ( ( ) )
134+ }
138135}
139136
140137#[ cfg( not( unix) ) ]
@@ -192,6 +189,7 @@ pub(super) fn ensure_safe_destination(path: &Path) -> Result<(), AcmeCertificate
192189 }
193190}
194191
192+ #[ cfg( not( unix) ) ]
195193pub ( crate ) fn reject_existing_symlink_in_path (
196194 path : & Path ,
197195) -> Result < ( ) , AcmeCertificateInstallError > {
@@ -311,40 +309,11 @@ fn certificate_file_raw_mode(
311309pub ( super ) fn open_safe_certificate_directory (
312310 directory : & Path ,
313311) -> Result < rustix:: fd:: OwnedFd , AcmeCertificateInstallError > {
314- #[ cfg( target_os = "linux" ) ]
315- {
316- match rustix:: fs:: openat2 (
317- rustix:: fs:: CWD ,
318- directory,
319- rustix:: fs:: OFlags :: RDONLY
320- | rustix:: fs:: OFlags :: DIRECTORY
321- | rustix:: fs:: OFlags :: CLOEXEC ,
322- rustix:: fs:: Mode :: empty ( ) ,
323- rustix:: fs:: ResolveFlags :: NO_SYMLINKS | rustix:: fs:: ResolveFlags :: NO_MAGICLINKS ,
324- ) {
325- Ok ( fd) => return Ok ( fd) ,
326- Err ( rustix:: io:: Errno :: NOSYS | rustix:: io:: Errno :: INVAL ) => { }
327- Err ( error) => {
328- return Err ( AcmeCertificateInstallError :: Io {
329- path : directory. to_path_buf ( ) ,
330- error : error. into ( ) ,
331- } ) ;
332- }
312+ crate :: acme_directory:: open_directory_no_symlinks ( directory) . map_err ( |error| {
313+ AcmeCertificateInstallError :: Io {
314+ path : directory. to_path_buf ( ) ,
315+ error,
333316 }
334- }
335-
336- rustix:: fs:: openat (
337- rustix:: fs:: CWD ,
338- directory,
339- rustix:: fs:: OFlags :: RDONLY
340- | rustix:: fs:: OFlags :: DIRECTORY
341- | rustix:: fs:: OFlags :: NOFOLLOW
342- | rustix:: fs:: OFlags :: CLOEXEC ,
343- rustix:: fs:: Mode :: empty ( ) ,
344- )
345- . map_err ( |error| AcmeCertificateInstallError :: Io {
346- path : directory. to_path_buf ( ) ,
347- error : error. into ( ) ,
348317 } )
349318}
350319
0 commit comments