Struct ::passwd::Passwd

A representation of the user and group databases

This can be used to handle /etc/passwd and related files. Typically, you would:

  • Create an instance early in the main phase
  • Add things to it as needed (next to the associated packages)
  • Apply it at the end of the main phase

A rough example:

// Mappings for the IDs that systemd auto-assigns inconsistently from computer to computer
const USER_MAPPING = [("systemd-journald", 900), /* ... */]
const GROUP_MAPPING = [("systemd-journald", 900), /* ... */]

pub async fn phase_main(props, cmds, package_managers) {
   let passwd = passwd::Passwd::new(USER_MAPPING, GROUP_MAPPING)?;

   let files = package_managers.files();
   // These two files MUST come first as other files later on refer to them,
   // and we are not order independent (unlike the real sysusers.d).
   passwd.add_from_sysusers(files, "systemd", "/usr/lib/sysusers.d/basic.conf")?;
   passwd.add_from_sysusers(files, "filesystem", "/usr/lib/sysusers.d/arch.conf")?;

   // Various other packages and other changes ...
   passwd.add_from_sysusers(files, "dbus", "/usr/lib/sysusers.d/dbus.conf")?;
   // ...

   // Add human user
   let me = passwd::User::new(1000, "me", "me", "");
   me.shell = "/bin/zsh";
   me.home = "/home/me";
   passwd.add_user_with_group(me);
   passwd.add_user_to_groups("me", ["wheel", "optical", "uucp", "users"]);

   // Don't store passwords in your git repo, load them from the system instead
   passwd.passwd_from_system(["me", "root"]);

   // Give root a login shell, we don't want /usr/bin/nologin!
   passwd.update_user("root", |user| {
       user.shell = "/bin/zsh";
       user
   });

   // Deal with the IDs not matching (because the mappings were created
   // before konfigkoll was in use for example)
   passwd.align_ids_with_system()?;

   // Apply changes
   passwd.apply(cmds)?;
}

Methods

fn new(user_ids: Vec, group_ids: Vec) -> Result

Create a new Passwd instance

Arguments

  • user_ids - A list of tuples of (username, uid) to use if sysusers files does not specify a UID
  • group_ids - A list of tuples of (groupname, gid) to use if sysusers files does not specify a GID
fn add_user(self, user: User) -> Tuple

Add a user to the passwd database

fn add_group(self, group: Group) -> Tuple

Add a group to the passwd database

Add a user to the passwd database (and add a matching group with the same ID)

fn add_user_to_groups(self, user: String, groups: Vec) -> Tuple

Add an already added user to one or more already added groups

Add an already added user to one or more already added groups

fn add_from_sysusers(self, package_manager: PackageManager, package: String, config_file: String) -> Result

Add users and groups declared in a systemd sysusers file

You need to provide a map of preferred IDs for any IDs not explicitly set in the sysusers file.

Arguments

  • package_manager - The package manager to use for reading the sysusers file
  • config_file - The path to the sysusers file

Set user passwords to what they are set to on the system for the given users

Read the passwd and group files from the system and update IDs to match the system (based on name)

fn update_group(self, group: String, func: Function) -> Tuple
fn update_user(self, user: String, func: Function) -> Tuple
fn apply(self, cmds: Commands) -> Result

Apply to commands