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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
//! # exacl //! //! Manipulate file system access control lists (ACL) on `macOS`, `Linux`, and //! `FreeBSD`. //! //! ## Example //! //! ```no_run //! # fn main() -> Result<(), Box<dyn std::error::Error>> { //! use exacl::{getfacl, setfacl, AclEntry, Perm}; //! //! // Get the ACL from "./tmp/foo". //! let mut acl = getfacl("./tmp/foo", None)?; //! //! // Print the contents of the ACL. //! for entry in &acl { //! println!("{}", entry); //! } //! //! // Add an ACL entry to the end. //! acl.push(AclEntry::allow_user("some_user", Perm::READ, None)); //! //! // Set the ACL for "./tmp/foo". //! setfacl(&["./tmp/foo"], &acl, None)?; //! //! # Ok(()) } //! ``` //! //! ## API //! //! This module provides two high level functions, [`getfacl`] and [`setfacl`]. //! //! - [`getfacl`] retrieves the ACL for a file or directory. //! - [`setfacl`] sets the ACL for files or directories. //! //! On Linux and `FreeBSD`, the ACL contains entries for the default ACL, if //! present. //! //! Both [`getfacl`] and [`setfacl`] work with a `Vec<AclEntry>`. The //! [`AclEntry`] structure contains five fields: //! //! - kind : [`AclEntryKind`] - the kind of entry (User, Group, Other, Mask, //! or Unknown). //! - name : [`String`] - name of the principal being given access. You can //! use a user/group name, decimal uid/gid, or UUID (on macOS). //! - perms : [`Perm`] - permission bits for the entry. //! - flags : [`Flag`] - flags indicating whether an entry is inherited, etc. //! - allow : [`bool`] - true if entry is allowed; false means deny. Linux only //! supports allow=true. #![warn(missing_docs)] #![cfg_attr(docsrs, feature(doc_cfg))] mod acl; mod aclentry; mod bititer; mod failx; mod flag; mod format; mod perm; mod qualifier; mod sys; mod util; // Export Acl, AclOption, AclEntry, AclEntryKind, Flag and Perm. pub use acl::{Acl, AclOption}; pub use aclentry::{AclEntry, AclEntryKind}; pub use flag::Flag; pub use perm::Perm; use failx::custom_err; use std::io::{self, BufRead}; use std::path::Path; #[cfg(not(target_os = "macos"))] use failx::fail_custom; /// Get access control list (ACL) for a file or directory. /// /// On success, returns a vector of [`AclEntry`] with all access control entries /// for the specified path. The semantics and permissions of the access control /// list depend on the underlying platform. /// /// # macOS /// /// The ACL only includes the extended entries beyond the normal permssion mode /// of the file. macOS provides several ACL entry flags to specify how entries /// may be inherited by directory sub-items. If there's no extended ACL for a /// file, this function may return zero entries. /// /// If `path` points to a symlink, `getfacl` returns the ACL of the file pointed /// to by the symlink. Use [`AclOption::SYMLINK_ACL`] to obtain the ACL of a symlink /// itself. /// /// [`AclOption::DEFAULT_ACL`] option is not supported on macOS. /// /// # Linux /// /// The ACL includes entries related to the permission mode of the file. These /// are marked with empty names (""). /// /// Both the access ACL and the default ACL are returned in one list, with /// the default ACL entries indicated by a [`Flag::DEFAULT`] flag. /// /// If `path` points to a symlink, `getfacl` returns the ACL of the file pointed /// to by the symlink. [`AclOption::SYMLINK_ACL`] is not supported on Linux. /// /// [`AclOption::DEFAULT_ACL`] causes `getfacl` to only include entries for the /// default ACL, if present for a directory path. When called with /// [`AclOption::DEFAULT_ACL`], `getfacl` may return zero entries. /// /// # Example /// /// ```no_run /// # fn main() -> Result<(), Box<dyn std::error::Error>> { /// use exacl::getfacl; /// /// let entries = getfacl("./tmp/foo", None)?; /// # Ok(()) } /// ``` /// /// # Errors /// /// Returns an [`io::Error`] on failure. pub fn getfacl<P, O>(path: P, options: O) -> io::Result<Vec<AclEntry>> where P: AsRef<Path>, O: Into<Option<AclOption>>, { _getfacl(path.as_ref(), options.into().unwrap_or_default()) } #[cfg(target_os = "macos")] fn _getfacl(path: &Path, options: AclOption) -> io::Result<Vec<AclEntry>> { Acl::read(path, options)?.entries() } #[cfg(not(target_os = "macos"))] fn _getfacl(path: &Path, options: AclOption) -> io::Result<Vec<AclEntry>> { if options.contains(AclOption::ACCESS_ACL | AclOption::DEFAULT_ACL) { fail_custom("ACCESS_ACL and DEFAULT_ACL are mutually exclusive options") } else if options.intersects(AclOption::ACCESS_ACL | AclOption::DEFAULT_ACL) { Acl::read(path, options)?.entries() } else { let acl = Acl::read(path, options)?; let mut entries = acl.entries()?; if acl.is_posix() { let mut default = Acl::read( path, options | AclOption::DEFAULT_ACL | AclOption::IGNORE_EXPECTED_FILE_ERR, )? .entries()?; entries.append(&mut default); } Ok(entries) } } /// Set access control list (ACL) for specified files and directories. /// /// Sets the ACL for the specified paths using the given access control entries. /// The semantics and permissions of the access control list depend on the /// underlying platform. /// /// # macOS /// /// The ACL contains extended entries beyond the usual mode permission bits. /// An entry may allow or deny access to a specific user or group. /// To specify inherited entries, use the provided [Flag] values. /// /// ### macOS Example /// /// ```no_run /// # fn main() -> Result<(), Box<dyn std::error::Error>> { /// use exacl::{setfacl, AclEntry, Flag, Perm}; /// /// let entries = vec![ /// AclEntry::allow_user("some_user", Perm::READ | Perm::WRITE, None), /// AclEntry::deny_group("some_group", Perm::WRITE, None) /// ]; /// /// setfacl(&["./tmp/foo"], &entries, None)?; /// # Ok(()) } /// ``` /// /// # Linux /// /// Each entry can only allow access; denying access using allow=false is not /// supported on Linux. /// /// The ACL *must* contain entries for the permssion modes of the file. Use /// the [`AclEntry::allow_other`] and [`AclEntry::allow_mask`] functions to /// specify the mode's other and mask permissions. Use "" as the name for the /// file owner and group owner. /// /// If an ACL contains a named user or group, there should be a /// [`AclEntryKind::Mask`] entry included. If a one entry is not provided, one /// will be computed. /// /// The access control entries may include entries for the default ACL, if one /// is desired. When `setfacl` is called with no [`Flag::DEFAULT`] entries, it /// deletes the default ACL. /// /// ### Linux Example /// /// ```ignore /// # fn main() -> Result<(), Box<dyn std::error::Error>> { /// use exacl::{setfacl, AclEntry, Flag, Perm}; /// /// let entries = vec![ /// AclEntry::allow_user("", Perm::READ | Perm::WRITE, None), /// AclEntry::allow_group("", Perm::READ, None), /// AclEntry::allow_other(Perm::empty(), None), /// AclEntry::allow_user("some_user", Perm::READ | Perm::WRITE, None), /// ]; /// /// setfacl(&["./tmp/foo"], &entries, None)?; /// # Ok(()) } /// ``` /// /// # Errors /// /// Returns an [`io::Error`] on failure. pub fn setfacl<P, O>(paths: &[P], entries: &[AclEntry], options: O) -> io::Result<()> where P: AsRef<Path>, O: Into<Option<AclOption>>, { _setfacl(paths, entries, options.into().unwrap_or_default()) } #[cfg(target_os = "macos")] fn _setfacl<P>(paths: &[P], entries: &[AclEntry], options: AclOption) -> io::Result<()> where P: AsRef<Path>, { let acl = Acl::from_entries(entries).map_err(|err| custom_err("Invalid ACL", &err))?; for path in paths { acl.write(path.as_ref(), options)?; } Ok(()) } #[cfg(not(target_os = "macos"))] fn _setfacl<P>(paths: &[P], entries: &[AclEntry], options: AclOption) -> io::Result<()> where P: AsRef<Path>, { if options.contains(AclOption::ACCESS_ACL | AclOption::DEFAULT_ACL) { fail_custom("ACCESS_ACL and DEFAULT_ACL are mutually exclusive options")?; } else if options.intersects(AclOption::ACCESS_ACL | AclOption::DEFAULT_ACL) { let acl = Acl::from_entries(entries).map_err(|err| custom_err("Invalid ACL", &err))?; for path in paths { acl.write(path.as_ref(), options)?; } } else { let (access_acl, default_acl) = Acl::from_unified_entries(entries).map_err(|err| custom_err("Invalid ACL", &err))?; if access_acl.is_empty() { fail_custom("Invalid ACL: missing required entries")?; } for path in paths { let path = path.as_ref(); if access_acl.is_posix() { // Try to set default acl first. This will fail if path is not // a directory and default_acl is non-empty. This ordering // avoids leaving the file's ACL in a partially changed state // after an error (simply because it was a non-directory). default_acl.write( path, options | AclOption::DEFAULT_ACL | AclOption::IGNORE_EXPECTED_FILE_ERR, )?; } access_acl.write(path, options)?; } } Ok(()) } /// Write ACL entries to text. /// /// Each ACL entry is printed on a separate line. The five fields are separated /// by colons: /// /// ```text /// <allow>:<flags>:<kind>:<name>:<perms> /// /// <allow> - one of "allow" or "deny" /// <flags> - comma-separated list of flags /// <kind> - one of "user", "group", "other", "mask", "unknown" /// <name> - user/group name (or decimal id if not known) /// <perms> - comma-separated list of permissions /// ``` /// /// Each record, including the last, is terminated by a final newline. /// /// # Sample Output /// /// ```text /// allow::group:admin:read,write /// ``` /// /// # Errors /// /// Returns an [`io::Error`] on failure. pub fn to_writer<W: io::Write>(mut writer: W, entries: &[AclEntry]) -> io::Result<()> { for entry in entries { writeln!(writer, "{}", entry)?; } Ok(()) } /// Read ACL entries from text. /// /// Each ACL entry is presented on a separate line. A comment begins with `#` /// and proceeds to the end of the line. Within a field, leading or trailing /// white space are ignored. /// /// ```text /// Three allowed forms: /// /// <allow>:<flags>:<kind>:<name>:<perms> /// <flags>:<kind>:<name>:<perms> /// <kind>:<name>:<perms> /// /// <allow> - one of "allow" or "deny" /// <flags> - comma-separated list of flags /// <kind> - one of "user", "group", "other", "mask", "unknown" /// <name> - user/group name (decimal id accepted) /// <perms> - comma-separated list of permissions /// ``` /// /// Supported flags and permissions vary by platform. /// /// Supported abbreviations: d = default, r = read, w = write, x = execute, /// u = user, g = group, o = other, m = mask /// /// # Sample Input /// /// ```text /// allow::group:admin:read,write /// g:admin:rw # ignored /// d:u:chip:rw /// deny:file_inherit:user:chet:rwx /// ``` /// /// # Errors /// /// Returns an [`io::Error`] on failure. pub fn from_reader<R: io::Read>(reader: R) -> io::Result<Vec<AclEntry>> { let mut result = Vec::<AclEntry>::new(); let buf = io::BufReader::new(reader); for line_result in buf.lines() { let line = line_result?; let src_line = trim_comment(&line).trim(); if !src_line.is_empty() { result.push(src_line.parse::<AclEntry>()?); } } Ok(result) } /// Return line with end of line comment removed. fn trim_comment(line: &str) -> &str { line.find('#').map_or(line, |n| &line[0..n]) } /// Write ACL entries to text. /// /// See `to_writer` for the format. /// /// # Errors /// /// Returns an [`io::Error`] on failure. pub fn to_string(entries: &[AclEntry]) -> io::Result<String> { let mut buf = Vec::<u8>::with_capacity(128); to_writer(&mut buf, entries)?; String::from_utf8(buf).map_err(|err| io::Error::new(io::ErrorKind::Other, err)) } /// Read ACL entries from text. /// /// See `from_reader` for the format. /// /// # Errors /// /// Returns an [`io::Error`] on failure. pub fn from_str(s: &str) -> io::Result<Vec<AclEntry>> { from_reader(s.as_bytes()) }